Skip to content

Serve approved archive recall from a verified local cache - #53

Open
jbwinters wants to merge 3 commits into
mainfrom
feat/recall-cache
Open

jbwinters wants to merge 3 commits into
mainfrom
feat/recall-cache

Conversation

@jbwinters

@jbwinters jbwinters commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Task Master 167.

Problem. An approved archive-recall turn waited up to 4 s for the hosted call, even though the local approval already pins the snapshot by sha256.

What changes

  • Caching. Once a turn verifies live, its snapshot is cached next to the approval in recall-cache.json.

  • Serving from the cache. The next matching turn uses the cached snapshot immediately if both hold:

    • it still matches the approval digest;
    • it was verified live within the last hour.

    The same paid hosted request then runs in the background. The server charges per delivered pack, so each served turn still makes one billed request.

  • Failed revalidation. If the background request doesn't verify (superseded or missing record, payment failure, deadline), the entry is dropped and the next turn waits for a live answer, as before.

  • Overlapping turns. A turn that overlaps a pending revalidation waits for it.

  • Pack ID. A cached turn carries no pack id; the background pack is in the activity log.

  • Revocation discards the cache, and stop() waits for pending revalidations.

Decision for Josh: each served turn still triggers one billed request. But if that request fails (payment, network, or a superseded record), the cached turn has already been delivered. That gives at most one unbilled or stale turn per approval, within an hour of the last live verification. If that's not acceptable, the alternative is to drop this PR, or to serve from the cache only when the previous request succeeded within a few minutes.

Verification

  • vitest run on Node 22.23.4: 80 files, 689 tests pass. Tests added or changed:
    • tests/runtime/archive-recall-cache.test.ts: tampered, expired and other-owner cache entries;
    • tests/runtime/hosted-lifecycle-security.test.ts: the one-turn lag, overlap serialization, and revocation clearing the cache.
  • An independent review found four defects: unbilled-turn exposure, overlapping turns, a write-after-drop race, and a stale pack id. All are fixed or documented in the second commit.
  • The task's benchmark check (paid-recall-repair wall time, with the 29/30 result holding) needs the paid task 161 pilot.

Update (1b68e2f), from the completion review

  • A per-approval generation counter, bumped on every drop, prevents a recall that started before the drop from restoring the cache entry. This covers the case where the 4 s wait times out and a foreground call runs alongside the background one.
  • The lifecycle test now asserts that each served turn made exactly one hosted, billed request.
  • Known limit: if the daemon stops abruptly mid-revalidation, the entry stays and can serve one more turn within its one-hour window.

… in the background

An approved turn waited up to 4 s on the hosted recall call even though
the approval already pins the snapshot by sha256.

- After a turn verifies, the snapshot is cached beside the approval.
- The next matching turn is served from it at once when it still
  matches the approval digest (and is under 7 days old). The same paid
  hosted request then runs in the background, so billing is unchanged
  (the server charges per delivered pack).
- If that request does not verify (superseded or missing record,
  payment failure, deadline), the cache entry is dropped and the next
  turn waits for a live answer as before. A changed record can be
  served once more before it is withheld.
- Revocation discards the cache. stop() waits for pending revalidations.

Task Master 167.
…one-hour window

From an independent review:
- A turn that overlaps a pending revalidation for the same approval
  waits for it (within the 4 s deadline) instead of serving the cache
  again; this also prevents a late verified write from restoring an
  entry a newer revalidation dropped.
- A cached turn carries no pack id, so outcomes are not attributed to
  the earlier pack; the cached pack id is in the activity record.
- The cache serves only within an hour of a live verification, and a
  timestamp from a clock far ahead is not trusted.
- The cached path also checks the profile's memory mode.
- Documented: a failed payment or superseded record can let one cached
  turn through before it is withheld.
…; count billed requests

From the completion review:
- After the 4 s wait times out, a foreground recall can run beside the
  background revalidation. A per-approval generation, bumped on every
  drop, stops a late verified result from restoring an entry a newer
  recall dropped.
- The lifecycle test now asserts each served turn made exactly one
  hosted (billed) request.

Known limit: if the daemon stops abruptly mid-revalidation, the entry
remains and can serve one more turn within its one-hour window.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant