Skip to content

fix(apply): report a head-TTL relabel as a change on the messages path (#1354) - #1356

Merged
amiddavid merged 2 commits into
mainfrom
fix/1354-changed-head-ttl
Nov 24, 2026
Merged

amiddavid merged 2 commits into
mainfrom
fix/1354-changed-head-ttl

Conversation

@amiddavid

@amiddavid amiddavid commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Behavior change

None. Every host that sends the body reads res.Body, not res.Changed. The proxy already sent the 1-hour relabel on this path. Only a flag changes. No off-switch is needed.

Summary

apply.BodyOpts did not count a head-TTL relabel in Result.Changed on 2 of its 4 return sites: the messages-array path (line 922) and the equal-count path (line 931, the path most requests take). The 2 early-return paths already did.

What we found

  • The case: the count-changing rebuild declines (a message has no slot) and the head-TTL relabel applies. res.Body holds the 1h label. res.Changed was false.
  • Callers of Result.Changed:
    • apply.BodyFull returns it. No non-test code calls BodyFull, BodyWithModelWindow or Body.
    • cmd/cg-transcript-replay/main.go:180-181 writes it to its report and skips a branch on it. Replay tool only.
    • proxy/modes.go, proxy/proxy.go, dash/adaptcompaction.go read res.Body, res.Trace or res.Changes. They never read Changed.

What changed

  • apply/apply.go: add || headTTL1h at 2 sites: the messages path and the equal-count changed :=.
  • apply/headttl_changed_test.go: 2 new tests, one per site.
  • Other Changed sites checked: 442 and 467 (true already), 507 and 548 (have headTTL1h), 919 and finishRestore (true already).

How it was tested

macOS, Go 1.27.4.

  • New test fails before the fix, passes after.
  • gofmt, go vet ./..., CGO_ENABLED=0 go build ./...: clean.
  • CGO_ENABLED=1 go test -race ./apply/...: pass.
  • go test -race -run 'HeadTTL|OneHour|Relabel|1h' ./proxy/: pass.

Data dependence

None. The change reads a flag only. No stored customer data. No row in docs/data-dependence.md changes.

Fixes #1354

#1354)

BodyOpts set Result.Changed from the head-TTL relabel on two return
paths. The messages-array path left it out. When the count-changing
rebuild declined, the body held the 1h label but Changed was false.

The fix adds `|| headTTL1h` to that path, as on its two siblings.

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

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.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 offload-review skill, @amiddaid's session fresh 1 of 2 1 of 2 34c8fdd3ca77 blocking

Verdict: blocking

Findings

  1. apply/apply.go:931 — the equal-count path still drops a head-TTL-only relabel from Changed
    • What fails: a tenant opts into HeadTTL1h; the request passes the byte gate and holds head breakpoints; the pipeline changes no message and no other envelope rewrite fires. Result.Body then carries "ttl":"1h", but Result.Changed is false. A caller that gates on Changed — the impact named in #1354 — still reads "not applied".
    • Proof: line 498 relabels body before any path choice. Line 931: changed := systemSplit || toolSchema || filteredDecls > 0 — no headTTL1h. Line 1122: res.Body, res.Changed = out, changed. The flag is in scope and unused on this path. Lines 507 and 548 include it, and this PR adds it at line 922. The omission exists on the base 8f67d0c7 too, so it is pre-existing — but this PR claims to fix "a head-TTL relabel reported as a change on the messages path", and the equal-count writeback is the messages path most requests take. Issue #1354 lists three Changed sites; there are four.
    • Fix: add || headTTL1h at line 931. Add one test: the same head-TTL opts with a pipeline whose component declines (for example midsum on a transcript under its threshold), asserting Changed is true.

Non-blocking

  • The new test uses the midsum component, which ops_splice_test.go registers in the same package. Fine as is. A one-line comment in the test naming that file would help the next reader.
  • The fix changes reporting only. The wire body is the same before and after: on a declined rebuild, rebuildCountChanged returns the already-relabelled body, and the proxy forwards res.Body regardless of Changed. So no human-permission rule for behavior changes is triggered.

What I checked

  • Read the full diff: apply/apply.go:922 and the new apply/headttl_changed_test.go.
  • Traced headTTL1h from upgradeHeadTTL (apply.go:496-500) through every site that sets res.Changed: 442, 467, 507, 548, 919, 922, 1122, 2424, 2427, and apply/responses.go:515. All include the flag or cannot relabel, except line 931/1122 (finding 1).
  • Traced rebuildCountChanged (apply.go:1992-2005): on decline it returns the input body, which is the relabelled one. So res.Body was already correct; only the flag was wrong. The fix restores the documented contract "Changed is false when Body is the untouched input" (apply/opts.go:151-152) on this path.
  • Verified the test reaches the fixed line: midsum needs 4+ normalized messages and replaces messages 1..2, so the count changes and line 899 enters the rebuild. The role:123 message fails UnmarshalJSON, gets no slot (apply.go:1897-1898), so len(covered) != len(orig) and the rebuild declines. The pipeline [midsum] has no toolschema, toolfilter, cachesplit or cacheinject, so headTTL1h is the only true term at line 922 — the test fails on the base and passes on the head.
  • Verified the test's relabel preconditions: Anthropic provider, body over 200,000 bytes (the pad is 240,000), cache_control objects present in tools and system.
  • Checked Result.Changed consumers: no reader in this repo's hosts. proxy/modes.go:79-82 forwards res.Body unconditionally. The reader is a downstream library caller, as #1354 says.
  • Checked rules: no wire behavior change, no data-dependence change (the relabel is metadata-only), the test waits on no clock, no binaries added.

@amiddavid amiddavid left a comment

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.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 34c8fdd3ca77 blocking

Verdict: blocking

Findings

  1. apply/apply.go:931 — the equal-count path still omits headTTL1h, so the fix is incomplete
    • What fails: a request where only the head-TTL relabel fires and no component changes a message leaves Changed = false while Body carries the relabel. This is the same defect as #1354, on the other messages-path return — and the busier one: it is the path every request takes when the pipeline changes nothing (a warm, cache-frozen session).
    • Proof: trace the code.
      • apply/apply.go:497-499 — upgradeHeadTTL relabels the head, headTTL1h = true, body now differs from the input.
      • The pipeline changes no message and no count, so apply/apply.go:899 (len(chat.Input) != len(norm) || summaryStructureChanged(...)) is false and the count-changed block the PR fixed is skipped.
      • apply/apply.go:931 — changed := systemSplit || toolSchema || filteredDecls > 0. No headTTL1h. All three flags are false when the pipeline holds no cachesplit/toolschema/toolfilter or when they decline.
      • apply/apply.go:1071 — out := body, the relabeled body, no edits to splice.
      • apply/apply.go:1122 — res.Body, res.Changed = out, changed → the relabeled body goes out with Changed = false.
      • This breaks the contract at apply/opts.go:151: "Changed is false when Body is the untouched input." The three sibling returns already honor it: lines 507 and 548 (from #86) and line 922 (this PR) all include || headTTL1h. Line 931 is the one left behind.
      • Consumers read the flag: cmd/cg-transcript-replay/main.go:180 records it per call, and BodyFull (apply/apply.go:343) returns it to library importers. The proxy wires the relabel on the live path (proxy/modes.go:55-63), so the scenario is production traffic, not a corner.
    • Fix: changed := systemSplit || toolSchema || filteredDecls > 0 || headTTL1h at apply/apply.go:931. Add a test like the new one, but with a pipeline that declines (or a body midsum leaves alone), asserting res.Changed.

Non-blocking

  • apply/headttl.go:116-120 — a head mark that already carries ttl: "1h" sets upgraded = true without changing any byte. Changed is then true on all return paths while Body equals the input. The PR extends this to line 922. Pre-existing from #86 on lines 507/548, so not this PR's defect, but the contract sentence at opts.go:151 reads false in that case too.
  • apply/headttl_changed_test.go relies on midsum, a component another test file registers in init() (apply/ops_splice_test.go:33). This works, because one test binary holds all files of apply_test. It is fragile if that file ever moves. The existing traceOf tests already do this, so it follows the local pattern.

What I checked

  • The full diff (one line in apply/apply.go, one new test) and the code around each return path of BodyOpts.
  • The fix itself is correct for the path it touches: with ok = false, rebuildCountChanged returns the input body (apply/apply.go:2003-2005), which holds the relabel, and line 922 now reports it.
  • The new test reaches line 922: normalize skips the {"role":123} message (apply/apply.go:1897-1898), midsum changes the count, the rebuild declines because message 4 has no slot (apply/apply.go:2003), and the relabel fires (Anthropic, 240 KB pad over the 200,000-byte gate, marks on tools and system).
  • Every assignment to res.Changed in apply/apply.go and apply/responses.go, and every reader of Result.Changed in the repo (proxy forwards res.Body and ignores the flag; cg-transcript-replay and the public BodyFull API read it).
  • apply/headttl.go in full: the gate, the fail-open returns, the byte format the test asserts.
  • git log -S "headTTL1h": #86 added the flag to lines 507 and 548 but not to 922 or 931; this PR fixes 922 only.
  • Repo rules: no new data dependence (the relabel works from metadata only, and no feature row changes); no wire-behavior change, only the reported flag; fail-open untouched.
  • Live tests were not run (not approved for this review).

The equal-count path set `changed` without the head-TTL flag. Most
requests take this path: no component changes a message. The body held
the 1h label, but Changed was false.

Add `|| headTTL1h` there. Add a test for it.

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Round 1, both reviewers: the equal-count path (changed := ..., apply.go:931) had no headTTL1h. Fixed in 8d72023. Test: TestHeadTTLRelabelCountsAsChangedOnTheEqualCountPath. It fails without the fix and passes with it.

@amiddavid amiddavid left a comment

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.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 8d72023ff654 approve

Verdict: approve

Findings

None.

Earlier findings

  1. My round-1 finding (apply/apply.go:931, equal-count path omits headTTL1h) — fixed. Commit 8d72023f adds || headTTL1h at line 931, exactly as proposed. The new TestHeadTTLRelabelCountsAsChangedOnTheEqualCountPath covers it: 3 messages, so midsum declines under its 4-message threshold, the count stays equal, and the equal-count path runs. The pipeline [midsum] has no toolschema, toolfilter, cachesplit or cacheinject, so the relabel is the only true term — the test fails on 34c8fdd3 and passes on the head.
  2. Reviewer 2's round-1 finding (same defect) — fixed by the same commit. I checked it myself; the proof above is mine. I add nothing to reviewer 2's trace.

Non-blocking

  • Reviewer 2's note stands and I agree with it: a head mark that already carries ttl: "1h" sets upgraded = true without changing a byte (apply/headttl.go, the Already 1h branch), so Changed can be true while Body equals the input. Pre-existing from #86, on lines 507 and 548 as well. Not this PR's defect.
  • The new test also relies on midsum from ops_splice_test.go. Same minor note as round 1; it follows the local pattern.

What I checked

  • Last review: round 1, 34c8fdd, 2026-11-24T12:31:04Z.
  • New commits read: 1 — 8d72023f "fix(apply): count a head-TTL relabel on the equal-count path too (#1354)".
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files).
  • New items since my last review: reviewer 2's round-1 review (same finding as mine, checked, agreed); the author's comment naming the fix commit and test (verified against the code); the new commit itself (reviewed below).
  • The new commit's full diff: one line in apply/apply.go and one new test. Read both, plus the code around them.
  • Traced the new test's path through BodyOpts: normalize gives 3 slots (or 2 if the last message fails bifrost's unmarshal — either way under midsum's threshold), midsum returns nil, len(chat.Input) == len(norm) and the structure is unchanged, so line 899 skips the rebuild and control reaches line 931. out := body holds the relabel, no edits splice, so changed is exactly headTTL1h.
  • Checked the side effects of changed = true on this path: line 1105 (dumpPath is empty) and line 1115 (breakpoint recount — the relabel adds no breakpoint, and the test body has 4 marks against a cap of 4, so n > maxWireBreakpoints and n > c.ExistingBreakpoints are both false). No new log, no wire change.
  • Re-checked every site that sets res.Changed (apply.go:442, 467, 507, 548, 919, 922, 931/1122, 2424, 2427; responses.go:515): all now include headTTL1h or cannot relabel.
  • Repo rules: reporting-only change, no wire behavior, no data-dependence change, no clock waits, no binaries. Commit carries Signed-off-by and Assisted-By.
  • Live tests were not run (not approved for this review).

@amiddavid amiddavid left a comment

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.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 8d72023ff654 approve

Verdict: approve

Findings

None. My round-1 finding is fixed, and the new commit adds no defect.

Round-1 findings

  1. apply/apply.go:931 — equal-count path omitted headTTL1h — fixed in 8d72023f. Line 931 now reads changed := systemSplit || toolSchema || filteredDecls > 0 || headTTL1h. All four Changed sites on the messages path (507, 548, 922, 931) now include the flag, so the contract at apply/opts.go:151 holds on each of them.
  2. Reviewer 1 raised the same finding (their finding 1). I checked it myself in round 1 and agree; the same commit fixes it.

Non-blocking

  • Both round-1 non-blocking notes stand, unchanged and accepted: an already-1h head mark sets Changed = true with byte-identical body (pre-existing from #86), and the tests rely on midsum from ops_splice_test.go's init().
  • The new test repeats the 240 KB pad and body shape from the first test instead of sharing a helper. Style only.

What I checked

  • Last review: round 1, 34c8fdd3ca77795d8588b272a482abbd118e49fc, 2026-11-24T12:33:06Z.
  • New commits read: 1 — 8d72023f "fix(apply): count a head-TTL relabel on the equal-count path too (#1354)".
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from .review-in-reviews.txt, .review-in-comments.txt, .review-in-inline.txt).
  • New items since my last review: the fix commit, the author's comment naming the fix, and reviewer 1's round-1 review (now readable). The author's comment states the test fails without the fix and passes with it; my own trace agrees (below).
  • The full new diff (git diff 34c8fdd3..HEAD): one line in apply/apply.go, one new test in apply/headttl_changed_test.go.
  • The new test reaches the fixed line: 3 messages, all with string roles, so normalize slots all of them; midsum needs 4+ messages (apply/ops_splice_test.go:23) and declines, so the count stays equal and summaryStructureChanged is false (apply/summaryshape.go:14-25) — the equal-count path runs. No cachesplit, toolschema or toolfilter in the pipeline, so headTTL1h is the only true term. Without the fix line, Changed is false and the test fails.
  • The relabel preconditions hold: Anthropic provider, 240 KB pad over the 200,000-byte gate, cache_control marks on tools and system (apply/headttl.go:94-136).
  • Side effects of changed turning true on this path: the breakpoint-cap recount (apply/apply.go:1115) cannot false-alarm, because the relabel adds no breakpoint, so n > c.ExistingBreakpoints stays false. dumpChanges (apply/apply.go:1105) matches the existing behavior for envelope-only rewrites. finishRestore and the defers (OneHourFirst, tail mark) do not interfere: no restore plan on an empty store, and neither feature is configured.
  • The commit message follows the repo rules: conventional title, Signed-off-by, Assisted-By for AI attribution.
  • Live tests were not run (not approved for this review).

@amiddavid
amiddavid merged commit d847e7c into main Nov 24, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 24, 2026
amiddavid pushed a commit that referenced this pull request Nov 24, 2026
fix(pi): a Pi window open during an uninstall no longer fails

Pi reads models.json at start and on /model only. A window opened
while context-guru routed Pi kept the proxy URL in memory. After an
uninstall, every request of that window failed with ECONNREFUSED on
127.0.0.1:8787, 3 retries each (#1341). The files were right.

- Extension: on each prompt (input, before_agent_start), when the
  session's model is context-guru's route and the install record is
  gone, it reads models.json again and switches the session to the
  same model at its real endpoint (modelRegistry.refresh, find,
  pi.setModel). It says so once. Fail open: a removed context-guru
  must not break Pi. While the record exists, nothing changes.
- The extension keeps the last routed install record, so an
  uninstall from this same window recovers too.
- Installer: before uninstall, and before a re-run turns
  context-guru off, it names the open Pi windows (pgrep) and asks
  "Go on anyway? [y/N]": windows with an older package cannot switch
  by themselves. After an install it says that windows open before
  use context-guru only after a restart.

Tested: 65 installer tests (5 new), 57 extension tests (5 new). Real
Pi 1.6.0: a window opened while routed, then the uninstall while it
stays open. main: no answer, 4 ECONNREFUSED. Fix: answer ok, 0
errors, one notice naming the endpoint. pgrep finds a real Pi.

Behavior change: Pi users who uninstall or re-run the installer with
Pi windows open. Old: the installer went on; those windows failed
until restarted. New: the installer asks first (default no); a
window with this package switches to the real endpoint by itself.
To skip the question, answer y.

Data dependence: none. Process ids and the install record only.

Fixes #1341

fix(pi): the review findings on #1342

- A window that took the route after it started (/model) recovers
  too: each prompt reads the record once and keeps a record that
  routes this window's URL. A record on a new port does not hide the
  old URL, and the window moves to the new port ("context-guru moved
  to ...").
- "Still installed" now means: the current record routes this
  window's URL, not just that a record exists.
- Wiring tests (tests/recover.test.ts): the real extension, with its
  state folder in a temporary place, through its own handlers: the
  switch after an uninstall, one notice only, an uninstall from the
  same window, a late route, the moved port, and a setModel that
  throws (the prompt goes on).
- Installer: pgrep -u <uid>; the question only when context-guru
  routes Pi; the re-run refusal says "context-guru was not turned
  off" (Pi and the package may have changed); the line after an
  install has the right plural, and says to restart a window if it
  started before context-guru was on.
- The ChatGPT comment is back above before_provider_request.
- Docs: the how-to page and the design name the question.

test(pi): the recovered window keeps the same model, only its address

The owner asked whether the recovery after an uninstall changes the
model. It does not: it asks Pi for the same provider and id, and only
the baseUrl is read again from models.json. The wiring test now
asserts the provider and the id, not only the address.

Real Pi 1.6.0 (get_state after the prompt): provider anthropic, id
aws/claude-opus-5-5, the real endpoint; answer ok, 0 ECONNREFUSED.


Pi behavior change: Vitaly gave his yes on the PR; Pi changes are FYI to the owner. Round 3 (owner granted) re-checked the merge of main and the post-approval commit. Integrator check on current main + #1356 + #1342: the Pi unittest suites and go test ./pi-package/... pass.

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
Signed-off-by: Vitaly Zabershinsky <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

apply.BodyOpts: Result.Changed drops a head-TTL relabel on the messages-array (chat) path

1 participant