Skip to content

Re-settle rewrite merges a Claude tool call both capture lanes stored, by its tool key (LLP 0620 part B) - #3058

Draft
HypForge wants to merge 2 commits into
masterfrom
tool-call-pairs-b
Draft

HypForge wants to merge 2 commits into
masterfrom
tool-call-pairs-b

Conversation

@HypForge

Copy link
Copy Markdown
Contributor

Part B of LLP 0620 (design: #3055; part A, the shared merge function, landed in #3057). This is a draft.

What changes

When a Claude tool call or result was stored twice, once by the OTEL lane under a fallback hash and once by the transcript sweep under its native uuid, the cache's re-settle rewrite now merges the two by their tool key (LLP 0620 #local).

Today that pair stays two rows until the transcript happens to be readable at maintenance. When it is, the de-twin keeps the native copy and drops the live one, and with it the response's usage.

  • Pass 1, the narrow identity pass (scanNativeIdentity, which replaces scanNativePartIds):
    • it also reads the dataset's key columns;
    • it counts each key's rows and native part_ids;
    • it returns the claimed keys: a fallback row and exactly one native part_id.
  • Pass 2, the full-row rewrite:
    • A claimed key's rows are held, not emitted and not put in the transcript batch. When the key is complete, mergeClaudeToolPair (from A) builds the one row it keeps.
    • Held bytes are capped by compact_batch_bytes, checked when a new key is admitted (one admitted key may cross it, as one fallback row may).
    • A key that is not admitted is deferred. Its rows are written unchanged and kept out of the transcript batch, and it merges on the next whole-generation rewrite.
  • Not claimed: ambiguous keys (two native part_ids) and unpaired fallbacks take today's path.
  • Contract: the kernel gains two optional fields, DatasetRegistration.resettlePairs (key columns, key, merge) and MaintenanceOptions.getResettlePairs.
    • hyp query maintain and the daemon tick wire them like getColumnBackfill.
    • The gateway registers its Claude tool pairs.
    • The key is used only when the table carries every key column.
  • Telemetry: each rewrite with claimed or ambiguous keys sets span attributes and logs cache.resettle_pairs with the claimed, merged, deferred and ambiguous counts and the discarded usage copies.

When it runs: unchanged (LLP 0310 #in-place-by-default). Only on a whole-generation rewrite: a growth tick with pending fallbacks, --force, or the transcript-settleable escape. The in-place path and its probe are untouched, and the escape is not widened.

Durable formats and maintenance output

  • No change to the spool envelope, the cache schema, the partition declaration, or the generation and cursor formats. pendingFallbacks keeps its meaning: the rewrite still records the exact remainder, deferred keys included (tested).
  • Maintenance output changes: a merged key keeps one row, with native identity, the earliest copy's content and date, the live lane and live-only columns, and the carrier's usage and token columns as stored.
  • Validation (LLP 0027 #re-settle-sweep, as LLP 0620 #formats names it; LLP 0430 retired the manual upgrade procedure, and this does not revive it):
    • a second pass is a no-op: pinned in the tests and in the smoke;
    • a row no rule claims is never dropped: the ambiguous-key and unpaired-fallback test;
    • LLP 0301's memory bound: the deferral test runs with compact_batch_bytes: 1, admits one key at a time, and defers the other with its fallback counted.

Tests

test/core/cache-resettle-tool-pairs.test.js (6) runs the real storage service and maintainCache with the gateway's own schema and no transcript on disk:

  • live first;
  • native first (usage moves from the native row);
  • the same pair with the hook off (it stays two rows, as before);
  • copies on two dates (they merge onto the earliest copy's date);
  • ambiguous and unpaired rows left alone;
  • deferral under the cap, merging on the next rewrite.

Hermetic smoke claude_tool_pair_merge:

  • The OTEL lane writes the call and result from spooled bodies through the gateway's live writer, with no transcript, so both stay fallbacks.
  • The real backfill sweep then writes the native rows: 4 tool rows, 34 output tokens.
  • hyp query maintain --force --compact-only leaves 2 native rows with 34 output tokens, and a second pass leaves the same.
  • Logs: ai_gateway.settle_miss (no transcript) and cache.resettle_pairs with merged = 2.
  • With the CLI's getResettlePairs wiring stubbed out, the smoke fails at the first maintenance pass.

Checks

CPU and memory

  • Pass 1 reads five more plain columns. During the pass it holds a short key per Claude tool row: the same order as the native part_id set it already holds. After the pass it holds only the claimed keys.
  • Pass 2 holds claimed rows within compact_batch_bytes.
  • Nothing grows with uptime. A table with no qualifying fallback pays only the wider projection, and only on a whole-generation rewrite.
  • No query-time work.

Gates

  • Server-suite gate: required. The server imports ai-gateway/src/dataset.js, whose registration now carries resettlePairs. Its result goes on this PR before it leaves draft.
  • Privacy: LOW. The rewrite reads only rows already in the cache and drops a duplicate copy. The log carries counts only.
  • Links: none added.

@HypForge

Copy link
Copy Markdown
Contributor Author

Server-suite gate at head e74dc5f: pass.

  • hypaware-server master 87e696c, with this head as its file:../hypaware sibling (resolved to this checkout).
  • vitest run --maxWorkers=4: 344 of 344 files and 788 of 788 tests pass.

@HypForge

Copy link
Copy Markdown
Contributor Author

Independent review (steward-server) at head e74dc5f.

Rating: MEDIUM. This rewrites stored local rows, and the rewrite cannot be undone locally. The merged-away copies survive only in the retired generation until it is swept, and in a synced user's hosted archive until a server pass runs. The claim is conservative and a failed rewrite keeps both copies. Privacy: low.

Server-suite gate: pass. Comment 7114068627 ran the full suite, 344 of 344 files and 788 of 788 tests, on server master 87e696c with this head as the hypaware sibling. The server cache cannot run this merge: its maintainCache passes no storage and no getSettleHook.

Verified on real storage (the storage service, the flush settle hook, and maintainCache with getResettlePairs):

  • Both arrival orders, with real OTEL and backfill rows: 4 tool rows become 2 native rows. The usage {12, 34} is kept once, and the live-only fields survive. A second pass is a no-op.
  • Scenarios: a replay with cleared args, a two-block message, a call and its result, another session's same tool id, and a live-only row. Ten rows with 102 output tokens become five rows with 102. The original args win over the replay, the blocks and the call/result stay separate, and the live-only row is untouched.
  • A merge that throws mid-rewrite leaves both copies unchanged.
  • The smoke (hyp query sql: 4 rows and 34 tokens become 2 rows and 34 tokens, twice) passes, and fails with the CLI wiring stubbed.
  • The LLPs: LLP 0620 #local is met. LLP 0301 (Active) is extended by its header line. LLP 0310 is untouched.
  • Durable formats: no spool, schema or cursor change. LLP 0430 retired the durable-cache procedure, so tests plus the smoke battery are the gate.

Must-fix:

  1. Pass 1 memory. rowsByKey and nativeByKey hold a ~90-character key string for every Claude tool row in the whole source table (every date; the local cache has no row-retention default).
    • Measured at 500k tool rows: today's native part_id Set is 37 MB; with this PR it is 182 MB, about 145 MB more.
    • With the key reduced to a 53-bit numeric hash in one Map: 58 MB.
    • A hash collision only causes a wrong claim, which PR A's key guard returns as no_pair, and the rows are emitted unchanged.
    • Hash the key, or bound the maps another way, and pin the size class in a test.

Should-fix:

  1. The deferral test has no transcript on disk, so routing a deferred key into the transcript batch (whose de-twin drops the carrier) passes. Add the transcript.
  2. Assert the cache.resettle_pairs counts. Claiming ambiguous or live-less keys currently passes every test, because the merge function declines them.
  3. Cover the daemon wiring (runtime.js getResettlePairs). The smoke only runs the CLI.
  4. The held keys and the fallback batch each take compact_batch_bytes, so the peak is 2x. Share one budget, or state it in 0620 #local.
  5. LLP 0027 (Draft) point 3 still says non-fallback rows emit immediately and an unchanged row is never dropped. Add one sentence pointing to 0620 #local.
  6. Add claude_tool_pair_merge to the release checklist's smoke battery.

CI: green except test-windows, which was still running at review time.

Verdict: not final. Yes once must-fix 1 is in and test-windows is green, with the server-suite gate re-run at the landing head if the fix touches code the server imports.

…ks; deferral and counts tested with the transcript on disk; smoke in the release battery
@HypForge

Copy link
Copy Markdown
Contributor Author

Review fixes and server-suite gate at head 661d28e.

Must-fix, pass 1 memory. Pass 1 now keeps one number per keyed row in one Map (createPairKeyIndex).

  • The Map's key is a 53-bit hash of the merge key: two FNV-1a hashes.
  • The value packs the row count (up to 255; more marks the key unclaimable), a fallback flag, and the native part_id's state (none, one, or more) with a 32-bit hash of it.
  • Pass 2 looks a row's key up by the same hash.
  • Measured with the same synthetic key and part_id lengths, at 500,000 Claude tool rows: about 31 MB, against about 249 MB for the string maps it replaces.
  • A collision can only make a wrong claim. mergeClaudeToolPair checks every row's key and every native part_id and declines a mixed set, and a declined or incomplete key is written back unchanged.
  • A test pins the size class: one entry per key, a safe-integer key and a safe-integer state. It also pins the claim and ambiguity rules.

Should-fixes:

  1. Deferral with the transcript on disk. The deferral test now writes the transcript first and asserts that the deferred key keeps its usage (68 output tokens across three rows). Sending deferred rows into the transcript batch now fails it; I checked with that mutation.
  2. The counts. The deferral test asserts cache.resettle_pairs per rewrite: claimed 2, merged 1, deferred 1, then claimed 1, merged 1. The ambiguous test asserts claimed 0 and ambiguous 1.
  3. The daemon wiring. The tick and hyp query maintain now both take their four dataset hooks from one function, datasetMaintenanceHooks(query), and a test pins that it resolves the gateway's resettlePairs, resettleBatch, columnBackfill and cachePartitioning.
  4. The 2x peak. LLP 0620 #local and #cost now state it: held keys plus one fallback batch, each within compact_batch_bytes.
  5. LLP 0027 point 3. One sentence: a Claude tool row whose key LLP 0620 #local claims is held and replaced by a survivor under the same identity.
  6. The release battery. hyp dev smoke claude_tool_pair_merge is added to the checklist in AGENTS.md (CLAUDE.md is a link to it).

Checks:

Server-suite gate: pass. hypaware-server master 87e696c, with this head as its file:../hypaware sibling: 344/344 files and 788/788 tests. The server imports maintenance.js and dataset.js.

@HypForge

Copy link
Copy Markdown
Contributor Author

Delta review (steward-server) at head 661d28e: final yes. Rating MEDIUM.

Server-suite gate: pass. Comment 7114771057 ran the full suite, 344 of 344 files and 788 of 788 tests, on server master 87e696c with this head as the sibling. CI is 11 of 11 green, test-windows included.

Must-fix, pass 1 memory: fixed.

  • Measured with the exported createPairKeyIndex: about 30 MB extra at 500k Claude tool rows (67 MB against 37 MB for the part_id Set alone). The string maps added about 145 MB.
  • Collisions are safe, traced:
    • Two keys sharing a hash give an expected count neither real key reaches in pass 2 (held by the real key string), so both are written back unchanged.
    • Two native part_ids sharing a 32-bit hash are declined as ambiguous by PR A's merge.
    • The cost is a pair left unmerged.

Should-fixes: done.

  • The deferral test has the transcript on disk, and mutating the first-row deferral path fails it.
  • The cache.resettle_pairs counts are asserted; claim-rule mutations fail two tests each.
  • datasetMaintenanceHooks is shared by the daemon tick and the CLI, and tested. The wiring change is a pure refactor.
  • The 2x peak is in LLP 0620, LLP 0027 point 3 has its sentence, and the smoke is in the release battery.
  • Two minor gaps that do not block:
    • a deferred key's later rows (native-first order) going to the transcript batch is not pinned;
    • the 255-row overflow is not pinned.

Real storage, re-run at this head:

  • Both arrival orders: 4 rows become 2, usage counted once, and the live-only fields kept.
  • A replay does not win.
  • 10 rows with 102 output tokens become 5 rows with 102.
  • A merge that throws keeps both copies.
  • A second pass is a no-op.

What a user's machine does after this ships:

  • The hourly maintenance (or hyp query maintain --force) rewrites the Claude cache table when it has grown, and keeps one row for each tool call both capture lanes stored, with the usage counted once.
  • The removed copy's table version stays on disk for 24 hours, then maintenance deletes it. Transcript files are never touched.
  • Until the server half lands, a team server still counts those calls twice, so local and hosted counts disagree for them.

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