Skip to content

feat(coref): co-reference-aware compaction - #80

Open
amiddavid wants to merge 31 commits into
mainfrom
feat/coref-compaction
Open

feat(coref): co-reference-aware compaction#80
amiddavid wants to merge 31 commits into
mainfrom
feat/coref-compaction

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Implements co-reference-aware compaction — picking what to
drop at a threshold crossing by looking at back-references rather than at content or age — and
measures the substrate it depends on.

What's here

  • internal/coref — the tier-1 reference index: which identifiers each tool output
    introduced, and whether any later model turn carried them forward. No bifrost, no components,
    no tokenizer dependency, because it has to stay interchangeable with deploy/harbor/coref.py's
    definition. Its fixture is the twin of coref_fixture.py, negative control included and
    asserted.
  • components/offload/coref — the Offload component. The one component that mutates the
    cached prefix on purpose, so: batched cuts, a per-session rewrite_budget, latched decisions
    replayed byte-for-byte, repairLostFreeze deliberately not consulted, side-effect-free planning.
  • deploy/harbor/coref.py + two converters (cc_capture.py, runlog_capture.py) — the
    measurement pass, plus the plumbing to run it on Claude Code transcripts and benchmark harness
    logs without an eval-box run.
  • Docs — the proposal, measured results, a
    component reference, and a
    one-page cheat sheet for the vocabulary.

The measurement, and the finding

Run on three corpora (none of them the eval-box captures — those were unreachable). The headline is
that they disagree by a factor of three:

Claude Code (interactive) UltraHorizon LOCA-bench
unreferenced 23% 78% 95%
closed 15% 8% 0%
open 60% 13% 4%
…restricted to ≥20 later turns 21% 70% 70%

Reference density is a property of the workload, not a constant. Interactive work on a coherent
codebase keeps returning to the same files and errors; benchmark tasks survey, extract, and move on.
The last row bounds the obvious tail bias and the ordering survives it.

Three more results:

  • Distance is not the discriminator; repetition is. Sweeping closed_dist over a 10× range
    moves the answer 2–3 points; sweeping open_reps 2→6 moves it 18. And 44% of mass was last
    referenced 40+ messages ago while 60% is open — most referenced mass is old and still hot. A
    distance-based A/B split would confidently cut repeatedly-referenced content.
  • A reference consumes a median 18.7% of what its output introduced — hypothesis A confirmed.
  • Break-even is workload-dependent: median required T is 95 turns on interactive traffic
    (15/30 sessions clear it) against 17 and 14 on the benchmarks. Batching moves it from unreachable
    to comfortable-on-benchmarks, marginal-on-interactive. Steps and deferred agent-compaction remain
    the load-bearing justification.

LOCA's 0% closed is the proposal's own §8 prediction landing: it argued LOCA would be a tier-2/3
stress test where references arrive transformed past what a substring match can see.

One bug worth calling out

The first measurement said 71% referenced. The rule deciding "identifier vs English word" accepted
any token of 10+ characters, so description, transparency, efficiency and conditions scored
as references. A manufactured reference makes an output look load-bearing, so this class of bug
fails by silently declining to compact — invisible to any metric that counts only what the
component did. Corrected to require interior structure, a digit, or camelCase; every false positive
is now a regression case. The residual is bounded at ~6 points of under-reporting.

Status

Opt-in, in no preset. cut_unreferenced is on by default and justified on every corpus.
cut_closed is off: its yield ranges 0–15% by workload, which is no basis for a default.

Next, in order: re-run on capture-swe/capture-tb at the eval box → enable cut_closed there →
observe-mode expand rate as the precision inner loop → only then the scored benchmarks.

Verification

gofmt clean · go vet ./... clean · go test ./... 24 packages, 0 failures · fixture reproduces
its documented ground truth.

Picks WHAT to drop at a threshold crossing by looking at back-references
rather than at content or age: if a later turn references an earlier tool
output, either the model already lifted the value it needed out of it (a
large cut is licensed) or it has marked the output as important (keep).

Three things the doc argues, two of which change the original idea:

- What a reference IS in our traffic, in three tiers, and the echo
  confound that decides whether tier 1 means anything at all: only
  tokens the output INTRODUCED can count, or the measurement trends
  toward "everything is referenced".
- Distance from the current turn is the wrong discriminator. A span
  referenced three times forty turns ago is a hot span that happens to
  be old. Open-vs-closed is the real axis, and it turns "certain enough"
  from a confidence score into a verifiable predicate.
- The cache arithmetic kills the naive version and specifies the real
  one. A cut at index i rewrites the suffix at 11.5x a cache-read, so a
  single early cut can never repay itself on tokens (T > 276 turns for
  5k cut at 20% depth). Batching, step reduction and deferring the
  agent's own compaction are what can pay, so the pass must be rare,
  batched and threshold-triggered.

Also records the constraints the codebase imposes on any such component:
decisions must be latched rather than re-derived (repairLostFreeze is
documented safe only for offloaders whose output is a pure function of
(content, config), which a history-dependent decision is not), cuts must
be one-way, and TailOnly is being violated on purpose so the cache-write
spend has to be budgeted and reported.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
internal/coref is the tier-1 reference index: which identifiers each tool
output INTRODUCED, and whether any later model turn carried them forward.
It depends on neither bifrost, the components package, nor the tokenizer,
which is deliberate — it has to stay interchangeable with the definition
in deploy/harbor/coref.py. If the two drift, the thresholds the offline
measurement produces are calibrated for a different algorithm than the
one that ships, silently. The Go fixture is the twin of coref_fixture.py
down to the four known answers AND the negative control: with the echo
guard disabled the src/config.py read must flip out of `unreferenced`,
and the test fails if it does not, so the control is asserted rather than
run once.

Prior-vocabulary exclusion is a firstSeen[token] -> index map rather than
a per-message snapshot of the running union: same answer, but
O(distinct tokens) instead of O(messages x tokens), which matters at the
transcript sizes this fires on.

components/offload/coref.go carries each of the design's constraints as a
tested behaviour rather than a comment:

- the index is built from the PRISTINE request, before any replay, so an
  earlier cut cannot remove identifiers from the exclusion sets and
  silently reclassify unrelated outputs;
- decisions are latched and replayed byte-for-byte even when fresh
  evidence would reclassify the span, and repairLostFreeze is
  deliberately NOT consulted (re-deriving a history-dependent decision at
  depth is the very byte-flip that repair exists to prevent);
- the prefix is mutated on purpose, under a per-session rewrite_budget,
  where an unreadable counter reads as EXHAUSTED rather than as zero —
  fail-open belongs on the request, not on an unbounded cache spend;
- planning is side-effect free, so a batch failing a gate leaves the
  request byte-identical;
- min_batch_frac and break_even implement the S*T > 11.5*W inequality,
  with T estimated from observed transcript growth and W bounded to the
  CACHED span, since content past the boundary would be written anyway.

cut_closed defaults to false and coref is in no preset: the closed cut
needs two calibrated thresholds, and calibration is the measurement's job.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
coref.py reports, per session, how much tool-output mass is never
referenced again, how far back references reach, how much of an output a
reference actually consumes, and what a batched cut would cost in
cache-writes against what it saves. coref_fixture.py pins four outputs
whose classification is fixed by construction, including the echo
confound and a negative control.

Two converters, because the eval-box captures were not reachable and both
of these cost zero API dollars — the runs already happened:

- cc_capture.py turns a Claude Code transcript (the agent's own
  append-only log of what it sent) into capture shape. It merges
  entry-per-block back into messages, since message COUNT is the axis
  recency is measured on, and segments at a token budget because these
  sessions span many context windows and no request ever held them whole.
- runlog_capture.py does the same for benchmark harness logs: loopb /
  UltraHorizon llm_calls.jsonl, litellm traces, and LOCA-bench
  all_trajectories.json. A DROP in message count is treated as a session
  boundary, because that is the harness clearing the agent's context, and
  measuring across a boundary the model cannot see would invent cuttable
  mass out of the reset.

Both emit only the largest body in full plus per-turn `turn_tokens`
records, and stamp an explicit `conv`. coref.py honours both fields when
present; a real capture sets neither. Without turn_tokens the Claude Code
transcripts alone expand to 47 GB of prefixes; without conv, segments
opening on a tool_result collided on the inferred key and 31 of them
grouped down to 24, discarding the rest.

Measured on three corpora (docs/results/coref-density.md), the headline is
that they disagree by a factor of three: unreferenced mass is 23% on
interactive Claude Code traffic, 78% on UltraHorizon and 95% on LOCA —
21%/70%/70% once restricted to outputs with at least 20 later turns, which
bounds the obvious tail bias. Reference density is a property of the
workload, not a constant. LOCA's 0% `closed` share is the design doc's own
prediction landing: it argued LOCA would be a tier-2/3 stress test where
references arrive transformed past what a substring match can see.

Also fixes the rule that decided the whole answer. An earlier version
accepted any token of 10+ characters, so `description`, `transparency`,
`efficiency` and `conditions` scored as references and referenced mass
came out at 71% instead of 60%. A manufactured reference makes an output
look load-bearing, so that class of bug fails by silently declining to
compact — invisible to any metric counting only what the component did.
Identifiers now need interior structure after trimming edge punctuation, a
digit, or camelCase; no bare length rule, and no stopword list, which
would not survive a change of domain or of language. The residual
(lowercase hyphenated compounds, indistinguishable from real names like
context-guru) is bounded at ~6 points of UNDER-reporting rather than
argued away.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
components/coref.md is the usual per-component page: how it works, why it
is batched, budgeted and rare, the full config table with what the
measurement already settles about each knob (closed_dist is nearly inert,
open_reps is the dial), and a section on what it deliberately does NOT do.

reference/coref-glossary.md is a one-page cheat sheet for the vocabulary
this work introduces — novel token, echo, open/closed/unreferenced,
closed_dist, open_reps, ref age vs consume lag, the three tiers, S/T/W and
break-even, latching, one-way, the rewrite budget — in the order you meet
them, each with why it exists rather than just what it means. The terms are
not guessable from their names and now appear across four documents, so
they need somewhere to be looked up.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
Comment thread docs/proposals/coref-compaction.md Outdated
deliberately excluded — they are the mass being reduced, not the goal.

That signal is **forward-looking and position-free**. It answers "what is the agent trying to
do", never "which earlier span does this turn point back at". Co-reference is therefore not a

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls explain this sentence, perhaps add an example

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewritten with a worked example rather than the assertion. It now walks turn 4 reads src/auth.py / turn 5 says "the bug is TOKEN_GRACE_SECONDS" / thirty turns later the agent is on tests — and shows that asked "is the turn-4 output still needed?", conversationGoal can only answer "the task is still about auth", which is true of every output and so decides nothing. The fact that settles it (the one value taken sits in turn 5, and turn 5 isn't going anywhere) is positional and backward-looking, which that signal cannot represent at all.

Comment thread docs/proposals/coref-compaction.md Outdated
tuning change to an existing input; it is a new input, and it is the only input that can
justify dropping a *large*, *early* span rather than projecting a recent one.

The deterministic projector (`internal/extract/deterministic.go`) has the adjacent primitives

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls explain this paragraph in more details, I find it hard to follow, especially for a proposal document

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expanded into a bulleted walk-through of the two existing pieces and what each contributes: deterministic.go's important-key list is already an answer to "which parts of an output would a model carry forward?", and contain.go today checks a shrunken output is a subset of its original. The reusable idea is the second one run backwards — today it asks "is this compacted text contained in the original?", inverted it asks "is this span of the original contained in a later message?", and the same primitive becomes a reference detector. Same test, opposite direction: one validates a rewrite, the other measures reuse.

Comment thread docs/proposals/coref-compaction.md Outdated

| Tier | Signal | Detectable |
|---|---|---|
| **1** | `tool_use_id` ↔ `tool_result` pairing; and **literal carry-over** — a span introduced by tool result *i* reappearing verbatim in a later `tool_use` argument or assistant text (paths, symbols, line numbers, IDs, hashes, error strings) | exact, zero LLM |

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an example column

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an EXAMPLE column. Same reference at each tier: Tier 1 TOKEN_GRACE_SECONDS = 0 reappearing verbatim in an Edit argument; Tier 2 [{"ms":1200},{"ms":1800}] → "total latency is 3 seconds" (the 3 appears nowhere — it was computed); Tier 3 a directory listing → "as I saw earlier, the tests live beside the source", which is unmistakable to a reader and shares no token at all.

"the model referred back to this" and "the value it took still exists in the request" are the
same fact. That is what makes the closed case cheap to establish rather than a second search.

Framed this way, `coref` is `dedup` generalized: from "this tool output is byte-identical to

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I fully agree. consider this scenario:
a tool output returned: { "name": "david", "id": 123, "address": "foobarbaz"}
, { "name": "osher", "id": 235, "address": "banana"} the agent said, I need to remember david 123 address.
the address itself wasn't coref, but the tool output is needed and cannot be removed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. doesnt this contradicts case B?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, and this was the most valuable comment on the PR — it found a real bug, not just a wording problem.

I ran your exact example through the index rather than reasoning about it, and it's worse than you flagged. david, 123, foobarbaz are short lowercase words and a 3-digit number — precisely what the precision rules in §2 exclude — so the output yields zero trackable tokens. Zero novel tokens means zero references, which scored unreferenced, which is the class the default config cuts. So the shipped default would have deleted that output while the agent was still asking for the address.

Two separate defects, both now fixed in e7a2623:

  1. Your conceptual point. "Any reference is a surviving copy" is too strong. The model referenced an anchor (david, 123) in order to point at a payload (foobarbaz) it never restated. An exact matcher can't distinguish an anchor reference from a payload reference — so closed can't rest on "referenced once, long ago" alone. That is now stated as the reason cut_closed ships off, rather than mere caution. It also inverts my §7 reading of used_frac: a low value is ambiguous, not evidence for case A, because "took the value, rest is chaff" and "took an anchor, still needs the payload" look identical.
  2. The concrete one. refs == 0 conflated two opposite states — "introduced 200 identifiers, nobody touched one" (evidence of deadness) and "introduced nothing I can see" (absence of evidence). There's now an opaque class that is never cut at any setting. It is not a corner case: 8% of tool-output mass on interactive traffic, 20% on UltraHorizon, 40% on LOCA-bench — the last being 11 outputs averaging 22k tokens of exactly the record-dump shape you described.

Re-measuring dropped the headline unreferenced figures from 23/78/95% to 13/51/22%, and break-even from 15/30 to 9/30 sessions. Your counter-example is now a test case on both sides of the implementation.

## 4. The economics, and why they reshape the design

This is where the proposal has to survive contact with what the repo already measured
([improvement plan §0 and §C](../results/improvement-plan.md)).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see improvement-plan in the docs

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs/results/improvement-plan.md does exist on main (verified with git cat-file -e main:docs/results/improvement-plan.md) and is in the mkdocs nav, so the link resolves on the published site. It's just not in this PR's diff, so GitHub can't render it as a clickable target here.

Comment thread docs/proposals/coref-compaction.md Outdated
answered yes on every corpus.
- **A reference consumes a median 18.7% of what its output introduced** (11.5% on UltraHorizon).
Hypothesis A — "took one value, does not need the rest" — is confirmed rather than assumed.
- **Tier-2 leakage is 2%** of model turns (a stated numeric absent from all prior context) — real, and

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u explain this more, I'm not following

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expanded. The short version: Tier 2 is a reference that arrived transformed, so by definition no substring match can find it — what's countable is a symptom. If a model turn states a numeric value appearing nowhere in any earlier message, it computed that number from something, and that something was almost certainly a tool output. 2% of turns look like that on interactive traffic, which is why a zero-LLM first version is viable.

Two caveats now stated, and the second is a self-inflicted one worth knowing: it's a lower bound (only numeric transformations leave this trace — reworded prose is invisible), and tightening the identifier rules also blinded the proxy, since bare numbers now need 5+ digits and most computed values are small. So its 0% on LOCA means "none among tokens the tokenizer still accepts", not "none" — on a corpus with 0% closed and 40% opaque, the honest reading is that Tier-2 references there are common and simply unmeasured.

Comment thread docs/reference/coref-glossary.md Outdated

| Verdict | Means | Cut it? |
|---|---|---|
| **`unreferenced`** | No later turn ever used anything this output introduced. | **Yes — the free cut.** No threshold needed, no model call. This is the shipped default (`cut_unreferenced`). |

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it a recent turn, it might not had the chance to be referenced, dont we need to guard from this ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, and no — there was no guard, which was a real gap. An output near the tail has had no chance to be referenced, so scoring it as unused would make a batched pass preferentially cut the most recent context, which is the worst possible choice. mask avoids this with keep_recent; coref had nothing.

Added min_later_turns (default 8): an output with fewer model turns after it is treated as open regardless of everything else. Worth noting what the state was before — the measurement had bounded this bias (LOCA's raw 95% fell to 70% when restricted to outputs with 20+ later turns) but nothing in the component guarded against it. Bounding a bias in a report is not the same as not having it in the code.

Comment thread docs/reference/coref-glossary.md Outdated

| Knob | Default | Means | Verdict from the data |
|---|---|---|---|
| **`closed_dist`** | 12 | How many messages **ago** the last reference must be before the output counts as `closed`. Newer than this ⇒ `open`. | **Nearly inert.** A 10× sweep (4→40) moves the answer 2–3 points. Don't tune it. |

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont tune it, but still matters?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair — that phrasing was self-contradictory. Rewritten to what's actually true: closed_dist is load-bearing but flat. Set it to 0 and the closed class stops existing, so it certainly matters; but anywhere in 4–40 gives the same answer within 2–3 points, so there's no return on tuning it. Leave it at the default and spend the effort on open_reps, which moves the answer 18 points across the same kind of range.

| **step reduction** | The real prize. `corr(Δsteps, Δcost) = +0.95`; unique token removal is ~0.02% of the bill. The objective is **steps and reward, not bytes**. |
| **deferring agent compaction** | Claude Code compacts itself at ~167k on a 200k model. Staying under that avoids a full-transcript summarization — a large cache event *and* a quality loss. Plausibly the biggest win. |

**The counter-intuitive consequence:** firing at 90% of the context window means `T` ≈ 0 — paying a

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love this. 💌
though it depends how much is being cut isn't it? and for the determinsitic one, its cheap to calculate and can tell u how much deferring is happening.

I would also appreciate some thought on what it means for larger context windows that are now more and more frequent.... up to 1M

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — and both of your points landed in the doc.

On "it depends how much is being cut": yes, and more sharply than I'd written it. The agent-compaction prize is a step function, not a slope — you either drop below the threshold or you don't, and cutting 90% of what was needed to get there is worth nothing. Which argues for sizing the batch against the threshold distance, something min_batch_frac cannot currently express. Noted as a limitation.

On deterministic measurement: agreed, and it's the cheapest real metric available here — compare the API-reported usage against the documented compaction threshold and count the turns of headroom the cut bought. No benchmark scoring, no seeds, no LLM judge. It isn't in the metrics yet; it should be.

On 1M windows — I worked this through and the answer surprised me. Break-even is scale-invariant. Rearranged, S × T > 11.5 × W is T > 11.5 × (W/S) — it depends only on the ratio of rewritten suffix to cut mass, never on absolute size. A 1M transcript with the same density of cuttable mass needs the same T. So a bigger window neither rescues nor damns the token economics; it only moves when the trigger fires. What improves the ratio is cutting a larger share of what lies after the shallowest cut — an argument for cutting deep and rarely, not for cutting more.

Three things do genuinely change, now a table in §7 of the cheat sheet: cache-read becomes the entire bill (so coref is a cost play at 1M rather than a fit play — the strongest argument for it there); the agent's own compaction recedes to ~967k, making that prize rarer but much larger; and the index cost scales linearly, so an incremental per-session index stops being an optimization and becomes a requirement.

Comment thread docs/reference/coref-glossary.md Outdated
| **one-way / monotonic** | Keep → cut only. New evidence can never un-cut, because un-cutting is another rewrite. Monotonicity is a cost requirement, not tidiness. |
| **`freeze` / `reapplyFrozen`** | The mechanism that does it: record the replacement text against the original's content hash, and replay it on every later turn at any depth. |
| **`TailOnly`** | The rule every *other* age-based offloader follows: never touch the already-cached prefix. `coref` deliberately violates it — that's its purpose — which is why the spend is budgeted. |
| **`repairLostFreeze`** | A repair `mask`/`failed_run` may use: re-derive a lost decision at depth, safe because their output is a pure function of `(content, config)`. **`coref` must never use it** — its decision is history-dependent, so re-deriving is the very byte-flip the repair exists to prevent. |

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since I'm not familiar with this repo yet, I would appreciate if you can in a comment explain this more

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expanded both from first principles rather than by name.

TailOnly is a helper on Ctx answering "may I safely modify the message at index i?" It returns false for anything the provider has already cached, because editing cached content breaks the prefix hash and forces a cache-write of everything after it. Every other age-based offloader (mask, failed_run, collapse) consults it and declines. coref deliberately ignores it — reaching into the cached prefix is the point, since by the time a session crosses the threshold all the mass is back there — which is exactly why its spend is budgeted rather than forbidden.

repairLostFreeze needs the background first: an offloader freezes its replacement text against the original's content hash and replays it every turn so the bytes stay stable. If the store drops that record (TTL, eviction), it would normally decline to act at depth — but then the message reverts to full text, which is itself a prefix change. So mask and failed_run may re-derive even deep in the prefix: their replacement is a pure function of (content, config), so re-deriving reproduces byte-for-byte what the provider already cached. coref must never do this, because its decision depends on the whole transcript — re-deriving against a longer one can yield a different class and different bytes, the precise flip the repair exists to prevent.

Review of #80 raised a counter-example that invalidated the measurement and
exposed a defect in the DEFAULT configuration:

    [{"name": "david", "id": 123, "address": "foobarbaz"},
     {"name": "osher", "id": 235, "address": "banana"}]
    model: "I need to remember david 123 address."

Two problems, one conceptual and one concrete.

The conceptual one: the design claimed that because coref only cuts tool outputs
and references live in model turns, any reference IS a surviving copy of the
value taken. It is not. Here the model references an ANCHOR (david, 123)
precisely in order to point at a payload (foobarbaz) it never restated. An exact
matcher cannot tell an anchor reference from a payload reference, so `closed`
cannot rest on "referenced once, long ago" alone — the substantive reason
cut_closed ships off, rather than mere caution. It also makes a LOW used_frac
ambiguous rather than evidence for case A: "took the value, rest is chaff" and
"took an anchor, still needs the payload" look identical.

The concrete one, and worse: run through the index, that output yields ZERO
trackable tokens. `david`, `123`, `foobarbaz` are short lowercase words and a
3-digit number, exactly what the precision rules exclude. No novel tokens means
no references, which scored `unreferenced` — the class the default config cuts.
Two states satisfy refs == 0 and they are opposites: "introduced 200
identifiers, nobody touched one" is evidence of deadness; "introduced nothing I
can see" is absence of evidence.

So `opaque` is its own class now, never cut at any setting. Not a corner case:
8% of tool-output mass on interactive traffic, 20% on UltraHorizon, 40% on
LOCA-bench — the last being 11 outputs averaging 22k tokens of record and
spreadsheet dumps. The first version would have deleted all of it on no
evidence.

The same review raised the mirror-image error: an output near the TAIL has had
no chance to be referenced, so scoring it unused makes a batched pass
preferentially cut the most RECENT context. min_later_turns (default 8) is
mask's keep_recent expressed in turns. The measurement had bounded this bias;
nothing guarded against it.

Aligning the two implementations exposed a third bug: the Go index counted a
"later turn" by whether it held distinctive tokens, while coref.py counted
model-authored surfaces. One definition now, asserted on both sides.

Re-measured, the numbers are materially lower and break-even materially worse,
since opaque and tail-protected mass left the cut set:

  unreferenced   23% -> 13%    78% -> 51%    95% -> 22%
  break-even    15/30 -> 9/30  7/10 -> 4/8   4/9 -> 2/6

which strengthens the conclusion that this must be justified on steps and
deferred agent-compaction, not on tokens.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
Editorial pass from #80. The proposal was written as an argument and read as one
only if you already knew the vocabulary; these are the places review said it did
not.

- §1 shows what "forward-looking and position-free" costs in practice rather than
  asserting it, with a worked turn-4/turn-5 example, and explains the two
  existing extract primitives and what inverting containment buys.
- §2's tier table gains an EXAMPLE column: the same reference as a literal match,
  as a computed value (1200ms + 1800ms -> "3 seconds"), and as pure prose ("as I
  saw earlier").
- §7 names the echo-exclusion guard inline instead of assuming the glossary, so
  the document is self-contained from the top.
- §7 states that every decision rule in it is about COST, that reward is a gate
  rather than a metric, and that this measurement cannot speak to reward by
  construction — it reads traffic that already happened.
- §8 stops describing LOCA's orphaned tool_use/tool_result 400s abstractly and
  points at the fix to port: repair_tool_pairing() in forever's
  _anthropic_auth_hop.py, two phases, with a repair counter. Adds that coref
  cannot cause that bug — it rewrites text in place and never removes a message.
- Implementation status moves out to proposals/coref-implementation.md. It goes
  stale every commit while the argument does not, and a proposal doubling as a
  changelog stops being reviewable as a proposal. Cross-references are named
  links now rather than bare section numbers.
- The glossary gains opaque, min_later_turns and later-turns; replaces the
  self-contradictory "nearly inert, don't tune it" phrasing for closed_dist with
  what is true (load-bearing but flat, so leave it alone); and explains TailOnly
  and repairLostFreeze from first principles instead of name-dropping them.
- New glossary section on 1M-token windows. Break-even turns out to be
  SCALE-INVARIANT — T > 11.5*(W/S) depends on the ratio, not the size — so a
  bigger window moves only WHEN the trigger fires. What does change: cache-read
  becomes the whole bill, the agent's own compaction prize gets rarer but much
  larger and is cheap to measure deterministically, and index cost scales
  linearly. Also notes the prize is a step function, so a batch should be sized
  against the threshold distance, which min_batch_frac cannot express.
- Results doc carries the corrected numbers and a "what review changed" section
  recording the defect and the delta.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
Review question: the claim "Tier-2 references there are common and unmeasured"
conflated two different scopes, and the answer is Tier 2 AND Tier 3.

derived_evidence is a Tier-2 proxy by construction — it looks for a numeric
value stated with no earlier occurrence, which catches a COMPUTED value. Tier 3
("as I noted earlier", "per the schema") carries no shared token and no novel
numeric, so that proxy could never see it. Tier 3 was therefore never measured
at all, at any point; it is not something the identifier-rule tightening broke.

But the inference about LOCA does span both. There a reference is either visible
to exact matching (the 36% open) or invisible, and invisible means Tier 2 or
Tier 3. So with 0% closed and 40% opaque, the defensible statement is that both
are common there and both unmeasured — for different reasons. Tier 2 has a
detector that is nearly blind; Tier 3 has none, by design rather than by
regression, which is why it sits in open questions instead of a measurement.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
Follow-up from review. Two changes to what a cut leaves behind, and one
correction to the docs that were overstating the safety story.

The claim being corrected: "a wrong cut is not a wrong answer, it is an expand
round-trip plus a cache-write". That holds only when the model NOTICES.
Expansion is model-initiated — the tool is advertised and the host loop merely
answers a call — and nothing in the system detects a bad cut. So a wrong cut has
three outcomes, not one:

  1. the model notices and expands the right marker  -> a round-trip + a write
  2. it notices but cannot tell which marker holds it -> several expands, or not
  3. it never notices                                 -> answers from less, silently

Only (1) was priced. Reversibility is a CAPABILITY, not a guarantee: the stash
guarantees the bytes can be recovered, never that they are. Tier 3 is where (3)
lives — a missing semantic reference leaves nothing to look up, so nothing
prompts the expand call, and the result is a plausible answer built on less
evidence. Two consequences now stated wherever the claim was made: expand-rate
is a precision metric for NOTICED errors only and is blind to (3) by
construction (so a falling expand rate is ambiguous, not good news), and reward
is therefore the only instrument that sees the worst failure — which is why it
is a gate rather than one number among several.

What the design can actually influence is the 1-vs-2 gap, hence:

- The marker no longer asserts "no later turn referred back to it". That is
  precisely the claim that is FALSE whenever the reference was transformed or
  semantic, and it read as reassurance — a marker that talks the model out of
  recovering content is worse than an opaque one. It now states what was removed
  and never why removing it was safe, enforced by a test that greps the marker
  for safety claims.
- For structured content the residue describes the SHAPE rather than peeking at
  the first line: "200 records, fields: address, id, name". That is addressable —
  an agent hunting for an address can tell this is the output to expand — where a
  peek of one arbitrary row cannot. Key order is sorted because the marker text
  is replayed byte-for-byte every later turn, so a map-ordered descriptor would
  flip the prefix and pay for a cache-write. The peek is still used for
  unstructured output, where the head does identify the whole.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
0.15 came from the illustrative arithmetic in the proposal's §4 and was never
checked against how much cuttable mass exists. Measured on the 19 real sessions
that passed Claude Code's 167k compaction threshold, Tier-1 matching finds a mean
4.4% of the request as `unreferenced` and 9.6% including `closed` — so the gate
admitted 1/19 sessions with cut_closed on and 0/19 at the shipped cut set.

A gate no traffic can clear is not a conservative default, it is an off switch
that looks like a threshold. 0.05 admits 16/19.

Recorded as a starting point rather than a claim: the right value is an
experimental result, and min_batch_frac is a poor proxy for the question that
actually matters (whether this cut is the one that defers the agent's own
compaction, and by enough turns not to pay a second cache-write).

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
The proposal has claimed throughout that deferring the agent's own compaction is
plausibly the largest win, and never measured how often it is reachable. This
writes down the gap, the corrected arithmetic, and the order to close it in —
without building any of it.

Corrected arithmetic. Clearing the threshold is not enough: cutting to exactly
the line buys one turn, then the transcript grows past it and you either eat the
compaction or pay a SECOND cache-write at maximum W. So the requirement is
(usage - threshold) + growthPerTurn * headroomTurns. Measured on the 19 sessions
that passed 167k, as a share of the request: H=0 needs 7.3% (10/19 achievable),
H=20 needs 12.6% (5/19), H=40 needs 18% (0/19), H=60 needs 23.5% (0/19). Mean
available cut is 4.4% (unreferenced) / 9.6% (+closed). So a bar high enough to
avoid paying twice is a bar Tier-1 matching cannot clear. Flagged that the
deficit column is partly an artifact of segmenting transcripts at 180k, while the
availability column is not.

The design. min_batch_frac asks "is my cut large?"; the question is "does my cut
change the outcome?" coref is the only component paying a prefix rewrite, while
mask and friends take 12-27% from the cache-safe tail for free — so coref is a
marginal contributor paying the most, and should cut only when DECISIVE: not when
the pipeline is already under the threshold (prize won, rewrite buys nothing) and
not when even coref cannot get it under (agent compacts anyway, so we pay the
write and eat the compaction).

Why it is hard: it reduces to one scalar, tokens-until-compaction, and the
threshold is compared against the provider's reported usage — all four tiers plus
a local tail — which includes system, tool definitions and last turn's output,
none of which a component can see. schema.MessagesTokens is a systematic
undercount by an unknown amount.

Three routes in increasing cost, ordered so the first may make the others
unnecessary: (1) measure whether the prize is in play at all, using
modes.Tracker's existing reset detection — nothing new, and ground truth rather
than estimate; (2) let the host supply the distance, since the proxy holds the raw
body including system and tools; (3) only then calibrate the offset and learn
marginal growth per session in the Store, with a cross-session prior so turn one
is not cold, biased conservative because under-estimating growth is the disaster
case and over-estimating merely cuts less often.

And none of it touches reward, which remains the only detector for the silent
failure in §4.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…t it refutes

Ten arms scored against held-out ground truth over 885 real tool outputs
($43.88, 8105 decisions). Four results contradict claims already in these
docs, so the corrections travel with the report rather than trailing it.

New: docs/results/coref-selection-experiment.md — method (firing point,
evidence window, held-out future, null baseline), per-arm results, ten
findings, and the limitations section, with per-finding confidence labels.

Corrected:
- cut_unreferenced is not a free safe cut. 11% false-drop, not a boundary
  artifact (57% of errors land 51+ turns out), irreducible with the
  available features, and a lower bound since ground truth is Tier-1 only.
- min_later_turns does not buy accuracy. Kept for the structural reason
  (a batched pass must not prefer the newest context); the safety framing
  is removed.
- Break-even collapse was overstated ~3x. ~4.5x at a defensible operating
  point, not 10-15x.
- A model in the verdict path loses to the deterministic index on both
  axes, and no combination beats the index alone. The intermediate design
  is refuted, not merely unproven.
- The summarizer comparison is withdrawn: identifier matching scores
  verbatim survival and cannot score a paraphrase. Only the 11%
  turns-needing-lost-content figure survives from it.

Also recorded, all previously undocumented:
- mask is structurally inert on sequential caching traffic. TailOnly's
  maxCachedIdx = prevLen-1 makes its candidate and permitted sets disjoint
  for any keep_recent >= 1 (0/8 masked in a probe); repairLostFreeze
  maintains existing masks but cannot create the first at depth. The
  published 12.5%/27.5% figures straddle the tail-gate commit.
- skipReduce makes coref and extract_llm mutually exclusive per output,
  first-come. They cannot compose in a pipeline; combining the two ideas
  means combining them inside one component's decision.
- MarkKeptVerbatim keys by content hash with no session component, so one
  expand exempts that content in every future session, and the flag shares
  the payload LRU so it can be evicted. Now step 0 of the plan.
- W is bounded by the nearest live cache_control breakpoint, not the whole
  suffix, which strengthens the batching argument.
- Scope: the proposal is explicitly caching-regime only, and the two
  conventions that changes (TailOnly for backward-looking offloaders,
  allow_on_caching_backend) are noted as deliberate changes.
- The whole thing narrowed to one falsifiable hypothesis, with two of its
  four clauses already failing on measured traffic.

Docs only; no Go changed.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
MarkKeptVerbatim keyed on the content hash alone, with no session
component. The hash is global, so ONE expand in ONE session permanently
exempted that byte-identical content from compaction in EVERY session
thereafter.

The consequence runs the wrong way. Content that recurs byte-identically
across sessions is exactly the content most worth compacting -- a config
dump, a manifest, a schema, a file the agent re-reads every time. So the
guard preferentially and permanently disabled compaction on the highest-
value targets, nothing reported it, and the effect reads as yield decaying
for no reason.

Scope the key by session: the loop the guard prevents is intra-session by
construction (the agent expands, the next turn of THAT session re-sends the
restored original), so a session that never expanded anything cannot be in
a loop and needs no exemption. That is the smallest scope that still
prevents every loop the guard was built for.

The scoped id travels out of apply.Trace.Session and through to the proxy's
expand loop rather than being recomputed there, so the mark is always
written under the id the pipeline compacted under. An empty session is a
no-op, not a global mark -- unreachable on the live path (observe mode
compacts nothing, so there is no marker to expand), and recording globally
would reinstate exactly the leak this removes.

Second half: store.KeptPrefix joins DefaultPinPrefixes. The flag belongs
there by the namespace's own criterion, which is easy to miss because its
payload is one byte -- losing it does not lose data, it loses the FACT that
the agent already asked for this content back, so the next turn re-compacts
it and every turn thereafter pays a round-trip plus a cache-write. Before
this, a one-byte guard competed for LRU capacity against the multi-kilobyte
rewind stashes it guards, and lost.

Two new tests cover the half that was wrong: the exemption does not leak to
another session, and it still holds for the session that earned it. Full
suite passes.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
… how the corpus was read

Answers what coref-implementation.md called 'the largest unexamined claim in
the proposal', for $0 and with no eval box. Also finds a defect in how every
earlier measurement here read its corpus, and the first clause of the
hypothesis that does not fail.

Reachability, counted over real isCompactSummary events rather than
reconstructed boundaries: the agent compacts itself in 6/35 sessions (17%),
and 5/17 (29%) of sessions past 200 model turns. So every expected-value
argument in the proposal must be multiplied by ~0.17-0.29 -- a factor no
version of it carried. Subagent transcripts are excluded as separate
conversations.

The corpus defect: a Claude Code transcript is a TREE, not a linear
conversation. The compacted transcripts carry 25-51 forks and 338-632 leaves
each, and the parentUuid graph is too fragmented to walk (longest chain
collapses to 5-78 entries out of 1,486-5,217). A linear read therefore spans
multiple context windows -- it produced a '777,339-token request' on a 200k
model, which is what exposed it. Absolute request sizes are NOT recoverable
from this corpus.

Checked rather than assumed whether that invalidates the existing numbers:
exact-duplicate tool outputs are 16% by count but only 3% of mass pooled, 2%
median, 8% worst. The duplicates are small repeated reads, not the large
outputs the measurements turn on, so every SHARE-based result in the density
pass and the selection experiment stands. Absolute token figures are now
labelled indicative.

The positive finding: the density pass measured a required-cut deficit of
7.3% and concluded H=40 was unreachable (0/19). That deficit is an artifact
of firing LATE -- cc_capture.py segments at 180k, which places the
measurement past the threshold. At the moment the agent compacts, usage IS
the threshold by definition, so a pass firing at the crossing faces only
growth x headroom, which needs no absolute size measurement. On that basis
20-60 turns of headroom is affordable. This vindicates the proposal's claim
that the profitable moment to compact is earlier than the moment of maximum
pressure, now from the deferral side as well as the cache side.

Reported with its sensitivity rather than at face value: the two growth
estimators in this repo disagree 2x (239 vs 514 tok/turn) and the H=40
verdict flips between them, so 'can it buy 40 turns' is genuinely open.
cut_closed ships off, and the 11% false-drop applies to every yes.

One earlier claim weakened: the selection experiment called its 11%
false-drop a clean lower bound. Abandoned branches can supply a later
reference the live conversation never made, which inflates false-drop, so it
is bracketed by two opposing biases instead.

Adds deploy/harbor/coref_reachability.py and
docs/results/coref-reachability.md. Docs and one new script; no Go changed.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…on, and why

Characterised on an M-series Mac with Docker 29.1.3 while trying to run the
reported benchmarks locally. Three things worth writing down, because the
failure mode is a silent all-zero run rather than an error.

Both benchmarks are amd64-only. SWE-bench says so in its image names.
Terminal-Bench 2.0 looks portable -- its task Dockerfiles use multi-arch bases
-- but all 89 task.toml files pin a prebuilt alexgshaw/<task>:20251031 image
that overrides the Dockerfile, and those are single-arch amd64. So both
emulate under QEMU.

Emulation works; Claude Code does not run under it. It is a bun-compiled
single-file executable and segfaults on start (qemu: uncaught target signal
11). Installing from npm rather than the native bootstrap does not help --
same executable, so the install succeeds and then claude --version segfaults.

The reason this belongs in REPRODUCE.md rather than a note: Harbor surfaces
the segfault as NonZeroAgentExitCodeError, which is indistinguishable from an
agent failure without reading the container log. The run returns reward=0 on
every task and reads as a catastrophic preset. Same class of trap as the
CG_LAN and port-clash gotchas already documented.

Also corrects the Docker Hub quota claim to measured values: 100/hr anonymous
vs 200/hr authenticated per the registry's own RateLimit headers, not the
order of magnitude previously implied. Authenticating still matters -- the
anonymous limit is per-IP -- but for the right reason.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…mark capture

coref.py grouped requests into sessions by hashing the first 200 characters
of the first user message. That is sound for interactive traffic, where every
session opens on a different human sentence, and catastrophic on benchmark
traffic, where every task instruction opens with the same standard preamble.

Measured on capture-swebench.jsonl: the 200-char prefix has 19 distinct
values and the most common covers 1,771 of 1,795 requests. Since only the
largest member of each group is analyzed, 18 of 19 groups held nothing but
stray single-message calls and the run reported ONE session's worth of data.

The capture already carried the right key: the Anthropic clients pack
{device_id, account_uuid, session_id} into metadata.user_id. Preferring it
recovers 50 sessions, 433 tool outputs and 355,771 tokens from the same
bytes -- a 17x larger corpus. Same class of defect as the conv collision
already fixed for cc_capture.py, and it fails the same silent way: no error,
just less data measured and reported with full confidence.

With it fixed, step 1 of the implementation plan is done -- the eval-box
measurement the acceptance criteria are written against, blocked since the
project started, now in docs/results/coref-evalbox.md:

- unreferenced is 28% of tool-output mass on capture-swebench, double the
  interactive figure and the best of any corpus. +closed is 48%. Confirms
  proposal §8's claim that SWE-bench is the Tier-1-rich substrate.
- But peak request is 12,607 tokens against a 167,000 compaction threshold,
  so the deferral prize -- the largest claimed win -- cannot occur on this
  corpus at all. Not a small cut; no pressure.
- Break-even clears in 6/48 sessions at a window the traffic actually uses,
  0/48 at 200k (the window artifact the density pass warned about).
- cut_closed still stays off: 20% here against 0% on LOCA, so the workload
  spread that made it undefendable is unchanged.

And a caveat that inverts the framing of every earlier doc: capture-tb and
capture-swe are smoke captures (6 and 2 outputs above 300 tokens). The
interactive corpus those docs apologised for is larger and deeper than the
corpus they were deferring to.

Measured on the eval box itself; the captures already existed, so $0.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
The headline per-component number in these docs was credited to the wrong
component in eight places. It belongs to extract_llm. Some of the team call
its LLM trimming of large file reads the "programming masker", and that name
collision is how the figure got attached to mask.

Three independent lines settle it:

- The arm that produced the number contains no mask. codesmart is described
  in config.go as "the SWE-bench study's winning config" and is
  [format, toon, dedup, failed_run, cmdfilter, extract_llm, extract,
  cachesplit]. mask was never in it.
- docs/results/comparison.md, the primary results page, already attributes
  the savings to extract_llm + extract + cmdfilter/dedup and does not mention
  mask at all. The measurement never claimed it.
- mask is structurally incapable of it on caching traffic: behind the tail
  gate its candidate set (outputs older than keep_recent, all present last
  turn) and its permitted set (index > MaxCachedIdx) are disjoint for any
  keep_recent >= 1.

Sites corrected: components.md (x2), how-to/choose-a-preset.md,
how-to/measure-savings.md, reference/presets.md, components/mask.md,
reference/coref-glossary.md, proposals/coref-compaction.md.

Also walks back one of my own sentences added earlier in this branch. It said
the published 12.5% / 27.5% figures "straddle a behaviour change" (the tail
gate commit). That was too generous -- the figures were never mask's to
straddle. What mask actually saves on caching traffic has never been
measured, and the docs now say so instead of implying a number.

Each corrected site names the confusion explicitly so the misattribution does
not come back the next time someone reads "masker" and reaches for mask.

Docs only; no code changed.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…e-read

Raised in review: tail-only extract_llm has no break-even constraint since it
does not invalidate the cache. Correct in mechanism, and it exposed a real
mispricing.

savedTokenValue priced EVERY saved token at the cache-read rate whenever the
request was cache-aware, on the reasoning that content the agent re-sends is
already in the cached prefix. That is true of a REPLAY turn and false of the
turn the cut is made -- and when cache-aware, extract_llm is confined to the
TAIL, which by definition has never been cached. On that turn the content is
billed as a cache-write ($3.75/MTok, dearer than fresh input) or as plain
fresh input if it falls past the last cache_control breakpoint. Either way it
is 10-12.5x the rate it was assigned.

Confirmed from live usage rather than argued: a real SWE-bench trial reported
52,561 cache_creation tokens against 746,047 cache_read across 18 turns. New
tail content is cache-created every turn.

tokenValue now carries firstToken (the applied turn) alongside perToken (each
replay), and the gate computes removed x (firstToken + reuses x perToken).
The non-caching path is unchanged by construction -- one rate, so
first + r*rate is exactly (1+r)*rate -- and a test pins that so a future edit
cannot silently reprice the workloads the published numbers came from.

Directly recomputed break-evens:

  caching, recurring   30,397 -> 11,550 tok/output  (2.63x)
  caching, first sight 42,556 -> 12,900 tok/output  (3.30x)

The shipping VERDICT survives the correction even though the number did not:
SWE-bench's largest measured tool output is 2,760 tokens, still ~4x short, so
extract_llm stays off by default on caching backends. What changes is
large-output workloads -- on LOCA captures the eligible set goes from 7 to 31
of 1,639 outputs.

Two consequences recorded because they affect tuning: the cached/non-caching
break-even ratio falls from ~20x to ~6.4x, and recurrence becomes a much
weaker lever (x1.12 rather than x1.40) because the applied turn now dominates
the sum.

Three existing tests encoded the old arithmetic and were updated rather than
deleted, including the drift guard that ties these figures to
docs/components/extract_llm.md -- which is updated in step with them.

Also adds docs/results/component-gating.md, the replay pass that found this.
Its other results: the tail gate costs mask 93% of its effect (50.67% ->
3.33%) and failed_run all of it (1.29% -> 0%); extract_llm cannot fire on
SWE-bench at all because its output floor exceeds the largest tool output the
workload produces; codesmart therefore saves ~1% on caching traffic; and the
binding constraint across all of this is tool-output SIZE, not context length,
which makes LOCA-bench the only benchmark in the set where any of these
components can act. One unexplained observation is recorded as unexplained
rather than guessed at: extract_llm spends ~640ms/request while acting zero
times.

Full suite passes.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
The first measurement in which coref acts on real captured traffic through
the live pipeline rather than being scored offline. LOCA-bench because
component-gating.md established it is the only benchmark in the set whose
tool outputs clear these components' thresholds. $0 -- deterministic arms.

Substrate: the 9 deepest real request bodies from the LOCA capture, 3.34 MB,
tool-output mass 4,940-232,505 tokens each, replayed cache-aware.

  mask                 acted 9/9   402,135 tok   52.3% shrink
  coref (defaults)     acted 2/9    48,532 tok    6.5% shrink
  coref + cut_closed   acted 2/9    48,532 tok    6.5%  -- IDENTICAL

coref works and the result is not favourable: mask removes 8.3x more.

The reason is in the classification. Of the 148 outputs above the 300-token
floor, 142 (96%) are  -- referenced recently or three-plus times -- 6
are opaque, and ZERO are closed. The detector is working; LOCA's agents
reference their tool results immediately and repeatedly, so it correctly
reports that almost nothing is safe to remove. Same signature the density
pass found on LOCA trajectories, now reproduced through a different path.

cut_closed is byte-for-byte identical to the default because there are no
closed outputs at all. The knob held back for a corpus that could justify it
turns out to be structurally inert on the one corpus where the component can
otherwise act -- which settles what the density pass could only bound.

What this sharpens: mask removes 353,603 tokens that coref classifies as
still live. One question decides which component is right, and it has never
been asked -- does mask's extra cutting cost reward on LOCA? If mask is
reward-neutral there, coref's caution buys nothing on the only workload where
it can act. If mask loses reward, that 353,603-token gap is exactly the
damage coref exists to prevent. Cheaper and sharper than the SWE-bench
reward-parity arm originally planned, and well-posed because both arms are
deterministic.

Caveats recorded in full: n=9, no reward, deepest-request-only, and LOCA is
the adverse corpus for a Tier-1 detector by design -- so a poor result here
is not evidence about Tier-1-rich long-horizon traffic, which no benchmark in
the set provides.

Also records a measurement mistake of mine: an earlier probe ran
[mask, coref, extract] together and reported coref doing nothing. mask ran
first and replaced every output with a short marker, so coref saw only
sub-floor content. That is the skipReduce first-refusal interaction observed
live, and the reason these arms must run one component at a time.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
… pre-filtered

The tail restriction on extract_llm is a cache-COST property, not a safety
property of the model call: when cache-aware the component may only touch
messages the provider has not cached, because mutating the cached prefix
forces a cache-write of the suffix. That is why the measured mass sits where
it cannot reach it -- on LOCA captures, cached_prefix_above_floor showed large
outputs skipped for no reason other than being in the prefix.

allow_cached_prefix (default FALSE) lifts the restriction, and because the
cost is real, enabling it also switches on two gates the tail path does not
have:

1. The co-reference index as a free eligibility pre-filter. A prefix output is
   a candidate only if it introduced identifiers AND no later model turn
   carried any of them forward. Anything still referenced (open) or that the
   index cannot see into (opaque) is refused with no model call at all. This
   runs FIRST, ahead of the model and economic gates, because it is the
   cheapest check available and the whole point is not paying to look at
   content a deterministic pass can already clear.

2. The S*T > 11.5*W break-even, applied to the prefix BATCH -- one cache-write
   serves all of it, so it cannot be decided per candidate.

The division of labour is the design: the index looks BACKWARD (what has
already been referenced and is therefore spent) and the model looks FORWARD
(how much of what remains will still be needed). Neither sees what the other
sees, which is why they compose rather than duplicate.

Supporting changes:

- New components/offload/prefix_econ.go holds the economics of deliberately
  mutating the cached prefix -- cacheWriteX, prefixRewritePays,
  estimateTurnsRemaining, modelTurns -- lifted out of coref.go, which now
  delegates. Two components that pay the same cache-write must not price it
  differently in two places.
- The co-reference classifier defaults (closed_dist 12, open_reps 3,
  min_later_turns 8) become shared named constants for the same reason: a
  pre-filter that classified an output differently from coref would be
  answering a different question from the component whose measurements
  calibrated it.
- prefix_min_later_turns exposes the opportunity floor for prefix candidates.

Six tests, including the two that matter most: prefix reach is OFF by default
and makes no model call (the regression guard for every workload the published
numbers came from), and a declined prefix batch does NOT suppress tail work --
the tail costs no write, so dropping it would make enabling the feature
strictly worse than leaving it off.

Full suite passes.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…nd the fold is mis-wired

First end-to-end measurement of the proposal's largest claimed win: does
compacting the full request body defer the summarization an agent otherwise
runs when its context fills? Yes, by 72%. Five arms, ~$0.16 total.

Setup: 197 sequential turns across 9 LOCA conversations, reconstructed as
growing prefixes (LOCA is append-only) and replayed in order under a stable
per-conversation session id, so MaxCachedIdx advances turn by turn. summarize
is wired at a 60k context max and runs LAST, so it fires only when compaction
failed to keep the turn under the max -- which makes firings the deferral
measurement.

  S1  summarize alone                 71 firings   64.6% shrink
  S2  codesmart - extract_llm         46   (-35%)  58.2%
  S3  + tail extract_llm              46   (+0)    58.2%
  S4  + coref                         20   (-72%)  45.6%
  S4b + extract_llm prefix reach      20   (-72%)  45.6%

1. Deferral works and coref does it: 28 firings, 1,008,646 tokens, taking
   summarizations from 46 to 20. The deterministic pipeline gets a third of
   the way for free. The tail extract_llm lever adds exactly nothing -- S2 and
   S3 are byte-identical, consistent with every other measurement of it here.

2. allow_cached_prefix engages correctly and contributes nothing. The gates
   prove it engaged: cached_prefix 6,597 -> gone, replaced by
   prefix_still_referenced 6,519 (rejected for free) with economic_gate rising
   25 -> 103. Outcome byte-identical to S4. 98.8% of prefix candidates are
   still referenced, and what survives cannot clear break-even.

3. The useful result is a design error of mine: the pre-filter selects the
   WRONG CLASS. For UNREFERENCED content, dropping strictly dominates
   trimming -- a model call can at best preserve part of what is already
   spent, while paying a call and a cache-write, where coref removes it
   outright for free. There is no work for the model in the only class it is
   allowed to see. Trimming belongs to CLOSED: referenced once, long ago,
   value taken and remainder chaff -- still partly live, so what to keep needs
   judgement. Repointing the pre-filter is a one-line change and the obvious
   next experiment.

This rewrite also RETRACTS the earlier version of this page. That run sent one
request per conversation, so every request was a cold first turn with
MaxCachedIdx = -1 and the tail gate never engaged. It inflated mask to 52.3%
(its non-tail figure) and produced a "mask removes 8.3x more than coref"
comparison that was an artifact of the setup. It also reported zero CLOSED
outputs on LOCA, which is false -- the sequential replay surfaces 25, because
CLOSED needs a reference that has since gone stale and that cannot exist when
every request is turn 1.

And once more, the largest single lever is neither component under discussion:
format, a lossless JSON repack, recovers 1,266,088 tokens in 119 firings --
more than coref, for free. Every lossy component is competing for the
remainder.

Reward remains unmeasured and remains the gate.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
@amiddavid
amiddavid force-pushed the feat/coref-compaction branch from ac3bdf2 to 48ffc1b Compare August 20, 2026 21:23
…ction explicit

Review raised the objection that sinks the previous default: even UNREFERENCED
content may need model judgement, because coref matches exact identifiers only.
A value the model summed, converted or reworded leaves no substring behind
(tiers 2 and 3), so 'unreferenced' means 'no later exact reuse', not 'unused'.
That is exactly why the 11% false-drop measured against held-out ground truth
is a LOWER bound.

So handing that class to the model is not asking it to trim -- it is asking it
to VETO, to notice an implicit reference the index structurally cannot see. It
yields little when the index was right and is the only available mechanism for
catching when it was wrong. That is a real trade-off, not a tuning detail, so
it becomes configuration rather than a constant.

prefix_classes defaults to [unreferenced, closed]:

  closed       — referenced once or twice, long ago. Something WAS taken, and
                 an exact matcher cannot tell 'took the value, rest is chaff'
                 from 'took an ANCHOR and still needs the payload it points
                 at'. That ambiguity is why coref's cut_closed ships off, and
                 it is precisely a judgement call -- a model can read the
                 output, see the reference was a name or id, and keep the
                 payload a blind cut would lose.
  unreferenced — the veto case above.

open and opaque are REFUSED at construction rather than accepted: open is
content a later turn demonstrably still uses, opaque is content the index
cannot see into at all, and for neither is there evidence of being spent.
Admitting them would turn the pre-filter into 'consider everything' and lose
the one property that makes prefix reach affordable. An unknown entry is also
an error rather than silently ignored.

Note this changes the shipped default from unreferenced-only to both classes.
Deliberate: the LOCA replay showed unreferenced-only contributes nothing (the
model can only preserve part of what is already spent, while coref drops it
outright for free), so a default that admits only that class is a default that
cannot help.

Two tests: the refusals and the unknown-entry error, and that narrowing to
closed-only actually narrows -- the model is not consulted about unreferenced
content. Full suite passes.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
Adopts the forever project's per-run iteration notation so the two projects
read side by side, and retro-fits the runs already made.

  docs/experiments/README.md              the log, its index, and conventions
  docs/experiments/captures/iter001/      component gating on capture-swebench
  docs/experiments/loca/iter001/          first LOCA replay -- RETRACTED
  docs/experiments/loca/iter002/          sequential replay, deferral 71 -> 20

The split is deliberate. An iteration page is a record of FACT: what was
executed, what came back, what it does and does not prove, and the artifact
paths so a number can be traced to the bytes that produced it. The
docs/results pages are the ARGUMENTS -- they synthesise across runs and get
rewritten as understanding changes. If the two disagree, the iteration page
wins.

Three conventions, each earned the hard way in this branch:

- Retractions stay. loca/iter001 keeps its wrong numbers behind a banner,
  because the cause (one request per conversation meant every request was a
  cold first turn, so the tail gate never engaged and mask looked 8.3x better
  than it is) is more instructive than the numbers were.
- Cost is always stated, even when it is $0, because free is a property worth
  knowing.
- Every arm names its binary. A binary built before allow_cached_prefix
  existed silently turned iter002's fold arm into coref-alone, and it was
  caught only because the gate counters came back byte-identical to the
  previous arm.

No code changed.

Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…er-claim

LOCA-bench reward is wired: its own ReAct agent and deterministic GEM scorer,
pointed straight at a context-guru proxy via LOCA_ANTHROPIC_BASE_URL, no
forever and no auth hop. Baseline at the 8K debug band scores 1.0, matching
forever's own iter001, with the proxy verifiably transparent on the off arm
(9 requests, 0 saved, no component acting).

Arms at 8K: all three score 1.0 (off / codesmart-minus-extract_llm / +coref),
input tokens -16% and -22%, steps 9 -> 8. Reward parity holds -- but only
format acted, so it confirms a LOSSLESS pipeline is harmless and says nothing
about coref. Recorded as such rather than as a result.

Two methodological findings from those arms:

- Back-to-back arms share the provider's prompt cache. cache_read was
  identical to the byte across all three (131,096 = 8 x 16,387) and
  cache_write fell to 0 after the first arm, which inherited the baseline's
  write. Cost comparisons across sequential arms are confounded by run order;
  only input/output tokens and steps are safe to compare.
- The 8K band is saturated, which for THIS question is a feature: a 1.0
  baseline is the ideal control for a regression test. forever needed headroom
  to show a lift; we need a ceiling to detect a loss.

Escalating to 128k produced three more failures, two of them mine, and the
page now records the sequence:

- EAGAIN on every band above 8K, chased through a full band bisect and
  attributed to LOCA's MCP transport. It was my own runner: a `| tail -25`
  made LOCA's stdout a pipe and Rich's band-scaled output overflowed it. The
  error names stdout as the writer; I read "write" and reached for the
  transport twice before checking my harness. Four runs and a bisect wasted.
- With the pipe gone, HTTP 400 with 42 orphaned tool_use ids -- exactly what
  the proposal's §8 predicts LOCA's trimmer does, including the instruction to
  port repair_tool_pairing() from forever rather than rediscover it. I
  rediscovered it first. Now a rig-side shim that lifts the function verbatim,
  sits BEFORE cg-proxy so compaction sees well-formed traffic, and counts
  repairs (354 across 42 requests, so orphaning is constant at this band).
- --max-tool-uses 100 is too small for the band: the run completed but scored
  0.0 with tool_use_counter 105, having hit the cap with 49 quizzes and 30
  assignments left to enumerate. trim_events 0 rules out context rot and the
  shim both.

And one correction to this page's own earlier claim: tool_success_counter is
NOT a general health signal. Passing runs emit a different feedback shape with
no such counter, and a 128k run showed it at 0 while the tools worked fine and
the real cause was the budget. In the first case the tools genuinely were
broken but the counter was incidental, not diagnostic. The durable lesson is
narrower -- read per-task eval.json rather than the summary, and confirm a
cause before naming one.

No code changed.

Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…the numbers

Reward at the 64k band, 12 tasks, three arms -- the first configuration in
this work with both real context pressure AND measurable headroom. Committed
BEFORE the results so the interpretation cannot be fitted to them.

iter003 ruled out the obvious bands: 8K is saturated at 1.0 (good regression
control, but only format fires so it says nothing about coref) and 128k gives
a genuine 0.0 (real context-rot collapse, but a zero floor at n=1 measures
nothing). A 3-task probe at 64k returned 1/3 -- partial, which is where signal
lives.

Two design points recorded because they are easy to get wrong later:

- Arm order puts the baseline LAST. Back-to-back arms share the provider's
  prompt cache, so whichever runs first pays the prefix write and the rest
  ride free. Running off last means the compaction arms cannot get a free ride
  from it. That does not remove the confound, it stops it flattering the arms
  under advocacy -- so cost is reported with the caveat, never as a clean
  saving.
- n=12 detects a gross effect only. A 1-2 task difference is noise at this
  size and will be reported as noise.

Pre-registered: arms >= baseline means reward-neutral-or-better under pressure
(which with iter002's 72% fewer summarizations is the first genuinely positive
case); arms < baseline means the cuts cost tasks and coref fails its own gate;
all three identical means the pipeline is not engaging even here and the
question moves to UltraHorizon.

No code changed.

Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…finally acts

Three arms at the 64k band over 12 tasks. Read naively the result says
compaction costs reward: baseline solves 4/12, det 2/12, full 3/12. It does
not say that, because the losses are a configuration bug of mine.

Every task error coincided exactly with a summarize firing -- det 1 and 1,
full 3 and 3, baseline 0 and 0 -- and all three errors are HTTP 400 SCHEMA
violations rather than model failures: role "tool" reaching the provider
(Anthropic takes tool results as user messages with tool_result blocks) and a
misplaced system message. components.md says plainly that summarize
restructures the transcript and must RUN ALONE so no other component's
in-place edits race apply's rebuild. I ran it with nine others. The count
correlation is exact, so the mechanism is not in doubt.

This also undermines iter002. That page reported 72% fewer summarizations
using the same summarize-in-a-pipeline configs, but it replayed through
/compact, which never forwards upstream -- so the malformed bodies were never
validated by a provider. The same pipeline 400s in production. The mechanism
(compaction reduces how often a context max is reached) still stands; the
specific configuration that produced 71 -> 20 is not shippable and the figure
must be re-earned with summarize isolated. More generally: a replay harness
that does not forward upstream cannot catch schema violations, which is a
structural blind spot in every /compact-based measurement here.

What did work is the fold. extract_llm acted for the FIRST time in this entire
investigation -- 27 firings, 584,125 tokens -- in the allow_cached_prefix arm,
taking total saving from 20.0% to 31.8%. Here extract_llm does the work and
coref contributes little, the reverse of iter002 where coref did everything
and the fold added nothing; the difference is the band, since 64k has prefix
content large enough to clear both the output floor and the break-even. That is
the first evidence the fold does something no other configuration achieves.

And once again format, a lossless JSON repack, is the largest single lever --
92% of the deterministic arm's total saving.

Two corrections to my own reporting: the mean accuracy I first computed
excluded errored tasks from the denominator, which flattered the compaction
arms, so the table now uses /12 throughout; and the arms' lower cost is not a
saving, since they errored out of tasks early and the prompt-cache confound
applies.

iteration 004b, rerunning without summarize, is in flight.

No code changed.

Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…ot on average

Re-ran iter004's question with summarize removed, since every task error there
coincided exactly with a summarize firing. Same 12 tasks, same 64k band, same
deterministic GEM scorer. Removing summarize took errors to ZERO in both arms,
confirming the diagnosis.

  off (baseline)   4/12 solved  0 errors  $21.34   0%    010000101010
  ns-det           4/12 solved  0 errors  $14.67  17.1%  010000101010
  ns-full (fold)   5/12 solved  0 errors  $22.64  20.3%  010001101100

The parity result is stronger than a matching average: ns-det's per-task
outcome string is BYTE-IDENTICAL to the baseline -- the same four tasks solved
and the same eight failed, not merely the same mean. Removing 17.1% of content
changed nothing about which tasks succeeded, at 31% lower cost, with zero
model calls.

The fold arm ran extract_llm 38 times and coref 17 times, removed 20.3%, and
did not lose tasks. Its +1 task is reported as NOISE, per the reading
pre-registered before the run: it gained tasks 6 and 10 and lost task 11, and
net +1 at n=12 is sampling variation, not evidence that compaction helps.

Two things kept honest:

- Cost is only partly interpretable. ns-det ran after the baseline so it
  inherits some of the prompt-cache confound. The direction that IS safe to
  read is the uncomfortable one: ns-full cost MORE than baseline ($22.64 vs
  $21.34) despite removing 20.3% of tokens, because 11 model calls plus
  pipeline overhead outweighed the saving. Removing tokens is not saving money.
- format remains the dominant lever -- 99% of ns-det's saving from a lossless
  JSON repack. That has now held in every configuration measured, replay and
  live, at every band.

Also updates the experiment log index, including flagging iter002 as
config-invalid: its deferral figure came from a pipeline that 400s in
production, so the mechanism stands but the number must be re-earned with
summarize isolated.

No code changed.

Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
summarize is unusable on live Anthropic traffic. It emits its summary as a
SYSTEM-role message and splices it in as [msgs[0], summary, tail...]. When
msgs[0] is itself the system prompt -- the normal case -- that puts a system
role at index 1 and the provider rejects the entire request:

  400 messages.1: role 'system' must precede an 'assistant' message or end
      the array

System content belongs in the top-level `system` field; a system role inside
`messages` must precede an assistant message or end the array. At index 1,
followed by the kept tail, it does neither.

Found by running LOCA-bench against a real API: every task that triggered a
summarization failed this way, INCLUDING in an arm with no other component
enabled -- so it is this component's own output, not a pipeline interaction.
Both code paths are fixed, the fresh-summary one and the checkpoint-replay one;
they must agree or a replayed turn would emit different bytes from the turn
that created it.

A user-role message carrying the summary is valid and conventional -- it is
what Claude Code's own compaction does.

WHY IT SHIPPED, which matters more than the fix:

- Nothing asserted the summary's role. The existing tests reference
  ChatMessageRoleSystem only for the INPUT system prompt at index 0.
- Every measurement in this branch replayed through /compact, which runs the
  pipeline and returns the rewritten body WITHOUT forwarding upstream. A body
  no provider ever validates cannot fail schema validation. That is a
  structural blind spot in replay-based measurement, not a one-off oversight.

Adds summarize_role_test.go asserting no system-role message appears anywhere
except index 0, verified as a real guard by temporarily restoring the old role
and watching it fail.

Consequences for results already recorded: iter002's deferral figure (72% fewer
summarizations) came from pipelines containing this component, measured via
/compact, so the malformed bodies were never rejected -- the mechanism stands
but the number must be re-earned. iter004's and iter005's task errors are all
explained by this defect. Those pages are already flagged; iter005 will be
written up against this cause.

Full suite passes.

Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
… bug

Re-ran the deferral question on live, provider-validated traffic, with
summarize given its own proxy and the two chained (LOCA -> shim -> cg-proxy A
compaction -> cg-proxy B summarize -> gateway) so it runs alone as
components.md requires, and so every request is actually validated.

The chain worked -- both stages reporting, zero pairing repairs -- but every
arm errored tasks, INCLUDING the compaction-free baseline, all with the same
400: messages.1 role 'system' must precede an 'assistant' message or end the
array.

The baseline erroring is what settles it. With A=off the only component in the
path is summarize, so this is not a pipeline interaction and not coref or
extract_llm. It is summarize's own output: it emits its summary as a
system-role message and splices it in as [msgs[0], summary, tail], so when
msgs[0] is the system prompt -- the normal case -- a system role lands at index
1 and the provider rejects the request. summarize cannot work on live
Anthropic traffic at all, and its shipped preset would fail immediately.

Fixed in 80e95d5 by emitting a user-role summary, both code paths, with a
regression test verified against the old role.

No deferral number: the comparison is void because the baseline lost tasks
too, so firing counts cannot be compared. The counts observed (4 and 3) are
not evidence even between themselves -- the arms took different trajectories
(260 vs 286 requests), so raw counts are incomparable and per-request they are
1.54% and 1.05% against a broken baseline. iteration 005b re-runs it fixed.

coref and extract_llm are explicitly NOT implicated: they ran clean here
(coref 61 firings / 637,825 tokens; extract_llm 35 / 680,166) and the arm with
neither of them failed identically.

Also adds a fourth convention to the log: REPLAY IS NOT VALIDATION. /compact
returns the rewritten body without forwarding upstream, so no provider checks
it. Replay measures what a component removes; it cannot tell you the result is
a valid request. That blind spot silently covered every /compact-based result
in this branch -- density, the eval-box pass, component gating, and iter002 --
and it is why a component that 400s on every use shipped unnoticed.

No code changed here (the fix is 80e95d5).

Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
Second schema defect in summarize, found only after the first (system-role
summary, 80e95d5) was fixed and the component could finally act on live
traffic.

summarize replaces a span with one summary message -- [msgs[0], summary,
msgs[end:]...] -- so the kept tail can begin part-way through a tool exchange.
Its leading tool_result blocks then answer tool_use blocks that were just
deleted, and the provider rejects the whole request:

  400 messages.0.content.2: unexpected `tool_use_id` found in `tool_result`
      blocks

Measured on live LOCA-bench traffic: 5 of 12 tasks failed this way, worse than
the 3 the system-role defect caused, because fixing that one let summarize act
more often.

dropOrphanedToolResults walks forward accumulating available tool_use ids and
drops any tool_result whose call is not among them. Wired into both splice
sites (fresh summary and checkpoint replay), which must agree or a replayed
turn would emit different bytes from the turn that created it.

Two design choices worth stating:

- A result may answer a call at a DISTANCE, not only in the immediately
  preceding message, because a summary can legitimately sit between the two. So
  the check is "was this id ever called", not "was it called last".
- The repair is one-directional: it DROPS orphaned results and never
  synthesises placeholders. A synthetic "[tool result unavailable]" would be a
  second lie on top of the summary -- the summary already claims to carry that
  content forward, so re-asserting a missing result invites the model to reason
  about an absence the summary is supposed to have described. The rig-side shim
  used for LOCA's own trimmer does synthesise, because it must preserve a
  foreign agent's history; a component summarising its own span need not.

This is an invariant for any component that DELETES messages, and the reason
coref never needed it: coref rewrites a tool message's text in place and never
removes a message, so pairing holds by construction.

Test covers both halves -- a well-formed history passes through untouched
(idempotence, and no dropping of distant-but-valid results) and the summarize
shape drops exactly the orphan.

Full suite passes.

Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…ct, and a stop

Fixing the system-role defect did not unblock the deferral experiment. It let
summarize act more often, which surfaced the next violation, and then the next:

  005   no fix                      3 errors   role 'system' mid-array
  005b  user-role summary (80e95d5) 5 errors   orphaned tool_result blocks
  005c  + drop orphans   (0971a32)  3 errors   unanswered tool_use blocks

Each defect masked the next. While the system-role bug fired, summarize barely
got to act; fixing it RAISED the error count to 5, which looked like a
regression and was really the component finally running far enough to break
differently.

The three are one family: summarize does not maintain the provider's
message-shape invariants -- a system role spliced mid-array, results kept whose
calls were deleted, and calls kept whose results were deleted. The third is the
converse of the second and arises because msgs[0] is preserved verbatim while
its results may sit inside the removed span.

Both fixes are real and tested and worth keeping. They do NOT make summarize
usable. The third needs phase 2 of a pairing repair -- synthesise a placeholder
result, or decline to preserve an unanswered call -- and when writing the
second fix I explicitly argued AGAINST synthesising, on the grounds that a fake
result is a second lie on top of the summary. That reasoning was wrong on the
decisive point: an invalid request is worse than an imperfect one. It is still a
design decision rather than a patch and should be made deliberately.

So I stopped rather than attempt a third fix in the same session. Three defects
in one component, each revealed only by fixing the last, is a signal about
readiness, not a queue of chores; a fourth patch written at speed would more
likely add a fourth defect than reach a working state.

Consequence for the deferral claim: UNMEASURABLE until summarize maintains
those invariants. The mechanism remains plausible from iter002's replay, but no
valid live measurement exists and the component the claim depends on cannot
currently send a request a provider accepts.

coref and extract_llm are not implicated -- they ran clean in every arm, and
the arms containing neither of them failed identically.

Adds a next lever worth more than the experiment: a static schema validator
over pipeline output in tests. All three defects are checkable against
Anthropic's documented rules without a provider, which would close the replay
blind spot without needing live traffic for every measurement.

Also records one unexplained failure: a raw HTML 400 with no Anthropic error
body, so not from the model API. Recorded, not diagnosed.

No code changed here.

Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
…cts at the root

Review reframed defect 3 correctly: an unanswered `tool_use` means the agent is
still WAITING on that tool, so summarize after the exchange completes rather
than through it. That dissolves the problem instead of patching it, and it
turns out both pairing defects were the same mistake seen from either side.

The boundaries were pure arithmetic -- preserve msgs[0], summarize
msgs[1 : len-keepLast] -- and knew nothing about tool pairing:

  msgs[0] preserved while its results sit in the span   -> unanswered call
  tail beginning on a tool_result whose call is in span -> orphaned result

summarizeSpan now enforces one rule, a tool exchange is atomic:

- END advances forward past any tool messages the kept tail would begin with,
  so the exchange is summarized whole. Advancing rather than retreating keeps
  call and result on the same side without ever keeping less context than the
  caller asked for.
- The HEAD is dropped when msgs[0] is an assistant message carrying tool calls,
  because its results necessarily lie inside the span. msgs[0] is preserved to
  retain the conversation's identity -- its system prompt or opening user turn
  -- and an assistant tool-call message is neither, so folding it into the
  summary loses nothing.

Applied to both paths, fresh summary and checkpoint replay, including advancing
the replayed boundary the same way; if they disagreed, a replayed turn would
emit different bytes from the turn that created it.

This needs no synthetic content, which is what I wanted and could not justify
when fixing defect 2. dropOrphanedToolResults stays as a defensive net rather
than the primary mechanism.

Test covers all three cases across keepLast 1..4: the tail never begins on a
tool message, an assistant tool-call head is not preserved, and a normal
system-prompt head still is.

Full suite passes.

Signed-off-by: DAVID AMID <DAVIDA@il.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New/ToDo

Development

Successfully merging this pull request may close these issues.

2 participants