Skip to content

feat(thread): detect the client's own compaction for Codex, OpenCode and Pi - #1335

Merged
amiddavid merged 5 commits into
mainfrom
feat/client-compaction
Nov 24, 2026
Merged

amiddavid merged 5 commits into
mainfrom
feat/client-compaction

Conversation

@amiddavid

@amiddavid amiddavid commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Behavior change (a human approved it)

  • Human yes: feat(thread): detect the client's own compaction for Codex, OpenCode and Pi #1335 (comment)

  • Proxy-boundary confirmation: feat(thread): detect the client's own compaction for Codex, OpenCode and Pi #1335 (comment)

  • Who: users of Codex, OpenCode and Pi. Claude Code and Bob are unchanged.

  • Old: the proxy never saw these clients' own compaction. The thread id could change after a compaction. agent_compaction and AfterCompaction stayed 0.

  • New: the proxy sees the compaction. The compacting thread passes its id to the next request. The counters count it. A compaction body over 32 MiB is now allowed (up to 128 MiB) for these clients, as for Claude Code.

  • Paths these clients now reach (they reached none before):

    • the oversize bypass: a compaction body over 32 MiB skips the pipeline;
    • the agent_compaction count in /stats and the dashboard row;
    • the first request after a compaction (AfterCompaction, thread id passes on).
  • Phrase match: it now reads the Responses input (Codex), not only messages.

  • Body limit: a marked compaction body may now be up to 128 MiB, not 32 MiB. Any client can send the plugin header, so any client can lift the limit for one request. The Codex metadata counts only from a caller with a Codex User-Agent or originator. A prompt match already allows the same. A human decides.

  • Turn off: no switch. Remove the OpenCode or Pi plugin to drop the plugin mark. The phrase fallback has no switch either.

Closes #1314

Stacked on #1311 (feat/codex-session, agent/codex). The diff shows its 2 commits until it merges. I rebase then.

Summary

The proxy now detects the client's own compaction for Codex, OpenCode and Pi. A mark is the first choice. A phrase match is the fallback. Bob keeps its current match.

What changed

  1. Codex: the proxy reads x-codex-turn-metadata (header, or client_metadata in the body). request_kind: "compaction" means a compaction. Local and remote compaction both carry it.
  2. OpenCode: the plugin sets x-context-guru-request-kind: compaction. v1 uses chat.headers when agent is compaction. v2 uses http.request when kind is compaction.
  3. Pi: the plugin sets the same header from session_before_compact until session_compact or session_compact_failed. The mark is also removed on session_shutdown, and it ends after 10 minutes.
  4. Fallback: verbatim prompt openings of the three clients, last turn only.
  5. input: IsAgentCompaction now reads the Responses input (array or string) when there is no messages.
  6. The proxy strips x-context-guru-* before it forwards. The OpenCode fail-open path also drops the new header.
Client Signal Source
Codex x-codex-turn-metadata request_kind=compaction openai/codex rust-v0.172.0: core/src/responses_metadata.rs (REQUEST_KIND_KEY, CompactionTurnMetadata), core/src/client.rs:173
Codex fallback "You are performing a CONTEXT CHECKPOINT COMPACTION. " prompts/templates/compact/prompt.md, used by core/src/compact.rs
OpenCode v1 chat.headers agent=="compaction" sst/opencode v1.19.6: packages/opencode/src/session/compaction.ts, packages/plugin/src/index.ts
OpenCode v2 http.request kind=="compaction" v2.4.1: packages/plugin/src/promise/session.ts (SessionRequestKind)
OpenCode fallback "You MUST summarize the conversation above into a structured summary…" and "Update the existing checkpoint in the conversation above…" v2.4.1 packages/core/src/session/compaction.ts (buildPrompt); v1.19.6 imports it
Pi plugin flag during session_before_compact @earendil-works/pi-coding-agent 1.6.0 dist/core/extensions/types.d.ts, dist/core/agent-session.js
Pi fallback "The messages above are a conversation to summarize. Create a structured context checkpoint summary", the "NEW conversation messages…" update prompt (cut before <), and the "earlier context" turn-prefix prompt dist/core/compaction/compaction.js

Data dependence

  • Mark (header, Codex metadata): metadata only. No text is read.
  • Phrase fallback: reads last-turn text. It is the same deliberate exception as today for Claude Code. It is not stored.
  • Storage off / metadata only / everything: no difference. The detection runs on the request in flight. Nothing is stored.
  • docs/data-dependence.md (Thread tracker row) is updated. Decided by: the row's owner must confirm before merge.

How it was tested

macOS, Go 1.27.4, Node 22.23.

  • CGO_ENABLED=1 go test -race ./thread/... ./pi-package/... ./components/all/...: pass.
  • CGO_ENABLED=1 go test -race ./proxy/... ./proxyapp/...: pass (run in 4 parts, load average was about 390).
  • node --test opencode-plugin/test/*.test.mjs: 113 pass.
  • node --test pi-package/tests/*.test.ts: 53 pass (new compaction-mark.test.ts).
  • New tests: table cases for each phrase and for input; mark cases for header and Codex metadata; a proxy test that counts a marked request and checks that the upstream never sees the header.
  • Not run live against a real Codex, OpenCode or Pi.

Open questions

  1. Pi mark is unproven live. before_provider_headers runs through transformHeaders on the same stream function as a turn, so it should reach the summary call. Nobody ran it. The phrase fallback covers a miss.
  2. Codex /responses/compact. Codex up to 0.140 used this endpoint. It is gone by 0.160. The proxy routes only POST /openai/v1/responses, so no path match was added.
  3. Codex over WebSocket. The metadata is also in client_metadata. The proxy reads that key in the body, but only sees HTTP requests.
  4. Trust. Any client can send the plugin header. It can mark a request as a compaction and lift the body limit. The phrase match has the same effect today. The Codex metadata needs a Codex caller.
  5. Plugin version bumps are left to the release chore.

Where the logic lives

  • thread.IsAgentCompactionHint(markedByClient, body): generic. It reads messages and input, and the phrases.
  • agent/codex.IsCompaction(h, body): Codex turn metadata, header or client_metadata.
  • agent.PluginMarksCompaction(h): our own x-context-guru-request-kind header.
  • agent/all.MarkedCompaction(h, body): combines both for a host.
  • agent/all.IsClientCompaction(h, body): the one call for a host (mark, then thread.IsAgentCompactionHint). The proxy only calls it (proxy/agentcompaction.go). It is a temporary home: after refactor(agent): move Claude Code rules into agent/claudecode; add OpenCode and Pi labels #1312, agent.Identify returns the mark in the Client.
  • The bifrost adapter has no headers today. It passes nil, and the phrase match is the fallback.
  • Cortex: call agent/all.IsClientCompaction(pctx.Headers, body). No path is needed. Cortex must strip every x-context-guru-* header before it forwards, as the proxy does, or the mark leaks to the provider.
  • Bifrost adapter: IsClientCompaction(nil, body).

Technical notes

  • thread.IsAgentCompaction(body) stays.
  • No change to the agent.Agent interface.
  • Phrases contain no JSON-escaped characters, as the rules comment requires.

@amiddavid
amiddavid force-pushed the feat/client-compaction branch from 9ac9f74 to ddf36ad Compare November 24, 2026 09:23
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Behavior changes: approved by a human developer, with one condition.

@amiddavid approved both on 2026-11-24, in the session that owns this PR:

  1. Body limit: a compaction request from Codex, OpenCode or Pi may be up to 128 MiB, not 32 MiB. This is the same limit Claude Code and Bob get today.
  2. Compaction detection: for Codex, OpenCode and Pi, the main thread keeps its thread id after a compaction, and the compaction counts now count them.

Condition: the proxy-boundary session (#1309, #1315) must confirm that the design fits the proxy/library split before this PR merges. The PR owner records that confirmation here.

@amiddavid

Copy link
Copy Markdown
Collaborator Author

Proxy/library split: confirmed by the proxy-boundary session (#1309, #1315), with 2 changes.

At head ddf36ad7, the split is OK:

  • the client rules sit in library packages;
  • each rule takes http.Header;
  • each rule falls back safely when the headers are nil;
  • each rule cites its client's source and version.

Changes the PR owner makes:

  1. Move isClientCompaction from proxy/ into the library, so that every host makes 1 call, not 2 calls in a fixed order. Bifrost passes nil headers.
  2. Keep agent/all as the home for now, with a comment that names refactor(agent): move Claude Code rules into agent/claudecode; add OpenCode and Pi labels #1312. When refactor(agent): move Claude Code rules into agent/claudecode; add OpenCode and Pi labels #1312 lands, agent.Identify returns the compaction mark as part of Client.

Notes from the same review:

  • The 128 MiB body limit and how the proxy reads the body stay host code, in readGatedBody. Only the "is this a client compaction" check is a library rule.
  • A host must strip every x-context-guru-* header before it forwards a request. Cortex must do this too. This goes in the agent package doc.
  • The behavior-change note must also name the agent-compaction paths these clients now reach, and that the prompt-text match now reads the Responses input field.

…and Pi

Mark first, phrase second. Codex marks its compaction request in
x-codex-turn-metadata (request_kind). The OpenCode and Pi plugins set
x-context-guru-request-kind: compaction. A last-turn phrase match is the
fallback, now with the verbatim prompt openings of Codex, OpenCode and Pi.
The match reads the Responses input as well as messages.

Closes #1314

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

thread takes a plain hint. agent/codex reads Codex's turn metadata.
agent holds our own request-kind header. agent/all combines them for a host.

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
agent/all.IsClientCompaction takes headers and body. The proxy only calls it.
The agent package doc states the x-context-guru-* strip rule for hosts.

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid
amiddavid force-pushed the feat/client-compaction branch from cc562e8 to cb736c2 Compare November 24, 2026 11:02

@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 fresh 1 of 2 1 of 2 cb736c2942de approve

Verdict: approve

Findings

None. I found no wrong behavior, no data loss, no broken fail-open path, and no missing test
for the new behavior.

Non-blocking

  1. pi-package/extensions/context-guru.ts:431 — the Pi mark can stick to a session.

    • The compacting set is cleared only by session_compact and session_compact_failed.
      If neither fires (a compaction that hangs, or an event name that differs in a future Pi),
      every later request of that session carries the mark. Each is then counted as a
      compaction, sets the thread's compacting flag, feeds client_compaction to the replay
      cap, and gets the 128 MiB body limit. session_shutdown does not clear the set either.
    • Fix: clear the session in session_shutdown too, or store a timestamp and expire it.
    • The phrase fallback covers the opposite miss (mark never set), and a process restart
      clears the set. So this is robustness, not a defect I can trace to a wrong output.
  2. opencode-plugin/index.js:1390 — the v1 mark keys on the agent NAME.

    • input.agent === "compaction" matches any agent named compaction, including one a
      user defines themselves. Its requests are then marked as the client's compaction.
    • The v2 hook keys on the reserved kind field, which does not have this problem.
    • Fix (optional): nothing, or document that the name is the built-in agent's.
  3. Stale doc pointers name the old detector. apply/opts.go:38 (the host-contract
    example), thread/host.go:42, and the comments at proxy/proxy.go:1316 and
    proxy/proxy.go:1767 still tell a host to call thread.IsAgentCompaction. A host that
    copies the example gets the phrase match only, not the header mark. Fix: point them at
    agent/all.IsClientCompaction.

  4. One more full-body scan per request. isClientCompaction now runs
    gjson.GetBytes(body, "client_metadata") on every request before the phrase match, and on
    the oversize path the body is scanned several times. gjson is fast, so the cost is small.

  5. agent/codex.IsCompaction trusts the Codex header from any client. codex.Identify
    refuses callers that do not say they are Codex; IsCompaction does not. Any client can
    send x-codex-turn-metadata and mark itself. The effect stays inside that client's own
    thread tracking and body limit. The PR names this trust question (open question 4) and a
    human approved the limit lift.

What I checked

  • The full diff of all 4 commits, and the code around each change.
  • The detection flow: proxy.go:1372 and readGatedBody → agent/all.IsClientCompaction →
    agent.PluginMarksCompaction, codex.IsCompaction, thread.IsAgentCompactionHint. The
    result feeds threadFor, the counter, the dashboard row and the body limit, exactly as the
    old thread.IsAgentCompaction result did. thread.Resolve treats the flag as before.
  • The header cannot leak to a provider: copyHeaders (proxy.go:3222) strips every
    x-context-guru-* header on every forward path; the OpenCode fail-open direct path deletes
    both headers; Pi routes through the local Go proxy, so its mark is stripped the same way.
    The new proxy test asserts this on the wire.
  • Import graph: agent/all now imports agent, agent/codex and thread; thread imports
    no context-guru package, so there is no cycle and no library/host boundary break.
  • The input handling in thread/compaction.go: array items (role must be user, trailing
    system/developer skipped) and the plain-string form. The new phrases contain no character
    that JSON escaping would rewrite, as the file's own rule requires.
  • Tests read in full: thread tables, hint test, codex test, agent/all test, the proxy
    counting + no-leak test, the OpenCode v1/v2 tests, and the new Pi mark test. Helpers
    (agentCompactionCount, openAIBody, buildHandler) exist in the same package.
  • Data dependence: the thread-tracker row file and the regenerated page carry the same text;
    the text-reading exception is the same one as before, now with the metadata-only mark as
    the first choice. The behavior change is named at the top of the PR with a human's yes.
  • Limits: I could not verify the cited Codex, OpenCode and Pi source lines (external repos,
    and live runs are not approved). I took them as the PR states them; the phrase fallback
    covers a wrong mark, and the PR itself flags the Pi mark as unproven live. Go tests were
    not run.

@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 @amiddaid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 offload-review skill, @amiddaid's session fresh 2 of 2 1 of 2 cb736c2942de blocking

Verdict: blocking

Findings

  1. pi-package/extensions/context-guru.ts:505 — the compaction mark can stay on for the rest of a session
    • What fails: Pi fires session_before_compact, so the session id enters compacting. If the summary then ends without session_compact or session_compact_failed, the id never leaves the set. Every later request of that session then carries x-context-guru-request-kind: compaction. The mark wins over the text check (thread.IsAgentCompactionHint, thread/compaction.go:71). Each such request gets a wrong client_compaction mark on its dashboard row, a wrong agent_compaction count, and the 128 MiB body limit instead of 32 MiB.
    • Proof: the set has exactly two exits — session_compact_failed (line 508) and session_compact (line 529). session_shutdown (line 534) clears other per-session state in the same handler (checks.end, line 538) but not compacting. So a session that shuts down mid-compact keeps its id marked; Pi reuses a session id on /resume, and the extension is not reloaded for that. I could not verify from Pi's source that an aborted summary request always emits session_compact_failed. The new test covers only the two clean exits (compaction-mark.test.ts:43-56); no test covers a compact that ends any other way.
    • Fix: delete the id in session_shutdown too, next to checks.end. Add a test line: emit session_before_compact, then session_shutdown, then check the header is absent.

Non-blocking

  • agent/codex/codex.go:69 — IsCompaction reads x-codex-turn-metadata from any caller. Identify in the same package gates the ids on the User-Agent or originator saying "codex", exactly because any client could send them. The consequence here is small: a client that spoofs the header could already spoof the phrase. Consider gating on Identify for consistency with the package's own rule.
  • agent/codex/codex.go:73 — the body path assumes client_metadata is an object keyed by the header name. If Codex sends it as a JSON string instead, gjson.Result.Get on a string returns nothing and the path silently gives false. The header path still covers HTTP requests. I could not verify the shape against Codex source.
  • opencode-plugin/index.js:1392 and :1434 — the v1 mark depends on input.agent === "compaction", the v2 mark on event.kind. I could not verify these fields against the two hosts. If either is absent, only the mark is lost; the phrase fallback still detects the request. The tests mock the field, so they do not prove the host sends it.
  • apply/opts.go:38 — the host example still passes thread.IsAgentCompaction(orig). That is the fallback-only call. agent/all.IsClientCompaction is now "the ONE call for a host" (its own doc). The example contradicts it; an embedding host that copies the example misses the mark path.
  • Stale comments: thread/host.go:42 and dash/schema.go:80 still say the mark comes from thread.IsAgentCompaction. docs/design.md still describes agent/all/ as blank-imports only. Comments and the package map, no behavior.

What I checked

  • The full diff 98c9a740..cb736c29 (4 commits, 20 files) and the code around each change: proxy/proxy.go (chat, readGatedBody), proxy/agentcompaction.go, thread/compaction.go and the tracker's use of the flag (thread/thread.go Resolve), agent/requestkind.go, agent/codex, agent/all, both client plugins, and every new or changed test.
  • Fail-open and header hygiene: copyHeaders (proxy/proxy.go:3216) strips every x-context-guru-* header on each forward path, so the new header cannot leak upstream; the proxy test asserts it. The OpenCode fail-open direct() path deletes the new header too. Pi has no direct fail-open path, so no leak there.
  • No import cycle: thread imports no agent package; agent/all adds thread only. proxy keeps its library-only imports.
  • The new phrases keep the no-escaped-characters rule stated in thread/compaction.go: the Pi cuts land before <, so the match works whether the wire escapes it or not.
  • Datadep: the thread-tracker row file and the regenerated page match, and the text states the dependence correctly — the mark is metadata only, the phrase match reads the last turn in flight, nothing is stored.
  • Commits carry Signed-off-by and Assisted-By, conventional titles.
  • I did not read the PR description or any comment on it (round-1 independence). So I did not check that the PR names the behavior change at the top, as RELEASING.md requires.
  • I ran no builds or tests (not approved). Statements about tests come from reading them, not running them.

…ointers

Review round 1 of #1335.

- Pi: clear the mark on session_shutdown and drop it after 10 minutes.
- Codex: IsCompaction answers only for a caller that Identify accepts.
- OpenCode v1: cite where the compaction agent is named.
- Point the host examples and comments at agent/all.IsClientCompaction.

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

Copy link
Copy Markdown
Collaborator Author

Round 1 fixes are at 4c7c30d3d234.

Reviewer 1

  1. Pi mark can stick: fixed. session_shutdown now clears it. The mark also expires after 10 minutes. 2 new tests: shutdown in the middle of a compaction, and an entry past its limit.
  2. OpenCode v1 keys on the agent name: documented. A code comment now says a user agent with that name is also marked. It cites packages/opencode/src/agent/agent.ts (name: "compaction") and session/compaction.ts (agents.get("compaction")), v1.19.6. The prompt match covers the opposite miss.
  3. Stale pointers: fixed. apply/opts.go, thread/host.go, dash/schema.go, 2 comments in proxy/proxy.go and docs/design.md now name agent/all.IsClientCompaction.
  4. Extra body scan: accepted. The cost is small.
  5. Codex header trusted from any client: fixed. codex.IsCompaction now answers only when codex.Identify accepts the caller. 3 new test cases (nil header, not Codex, originator only). The proxy test also sends the metadata without a Codex User-Agent and checks it is ignored.

Reviewer 2

  1. Blocking, the Pi mark stays on: fixed. Same fix as above: clear in session_shutdown, plus the 10 minute limit. Test: session_before_compact, then session_shutdown, then no header.
  2. Gate on Identify: done. See above.
  3. client_metadata shape: checked in the Codex source. It is a JSON object with string values: Option<HashMap<String, String>> in codex-rs/codex-api/src/common.rs. The current read is right. The turn metadata is one string value that holds JSON. A comment cites this.
  4. OpenCode fields not verified against the hosts: checked. v2: SessionRequestKind in packages/plugin/src/promise/session.ts (v2.4.1) and the kind field of SessionHttpRequest. v1: chat.headers input has agent (packages/plugin/src/index.ts, v1.19.6), and the request code passes input.agent.name. Not run live. The prompt match is the fallback.
  5. Stale doc pointers: fixed. See reviewer 1, point 3.

Tests

  • The OpenCode plugin tests ran 3 more times: 120 pass, 0 fail each time. The 5 failures on my first run did not return. That run overlapped with the Go race tests on a machine with a load average of about 390. I did not find the cause.
  • Race tests pass for agent/..., thread, apply, internal/datadep, pi-package and the proxy compaction tests.

@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 4c7c30d3d234 approve

Verdict: approve

Findings

None. Every round-1 finding is fixed or answered with a reason I checked and accept. The fix
commit adds no new defect.

Earlier findings, and their state

Mine (round 1):

  1. Pi mark can stick — fixed. session_shutdown now deletes the session from the map
    (context-guru.ts:552). The mark also expires: the set became Map<string, number> and
    isCompacting drops an entry older than 10 minutes. Two new tests cover the shutdown
    mid-compaction and the expiry. The fail direction stays safe: a compaction slower than
    10 minutes loses its mark, and the phrase fallback in the proxy then detects the request.
  2. OpenCode v1 keys on the agent name — documented, accepted. The comment now cites
    agent.ts (name: "compaction") and session/compaction.ts (agents.get("compaction"))
    and says a user agent with that name is marked too. The effect is small and
    self-inflicted, and the phrase fallback covers the opposite miss. The reason is right.
  3. Stale doc pointers — fixed. apply/opts.go, thread/host.go, dash/schema.go, the
    two proxy/proxy.go comments and the docs/design.md package map now name
    agent/all.IsClientCompaction. The new apply/opts.go example passes req.Header, the
    correct call.
  4. Extra body scan — accepted by the author. The reason (small cost) is right; I said the
    same in round 1.
  5. Codex header trusted from any client — fixed. codex.IsCompaction now returns false
    unless codex.Identify accepts the caller (User-Agent or originator starting with
    "codex"). Three new test cases (nil header, not Codex, originator only) and a proxy test
    case with the metadata but no Codex User-Agent, still counted as not-a-compaction.

Reviewer 2 (round 1, posted after mine, so new to me this round):

  • Blocking, the Pi mark stays on — fixed. Same fix as my point 1. I checked it myself;
    agree it resolves the finding.
  • Gate on Identify — done. Same as my point 5.
  • client_metadata shape — checked by the author against Codex source. The new comment
    cites Option<HashMap<String, String>> in codex-rs/codex-api/src/common.rs. That shape
    matches what the code assumes: one string value that itself holds JSON. I cannot read the
    Codex repo here, so I take the citation; the header path covers HTTP requests either way.
    Accept.
  • OpenCode fields not verified live — checked by the author against both hosts' sources.
    The phrase fallback covers a miss. Accept.
  • Stale doc pointers — fixed. Same as my point 3.

The human's condition (comment at 09:24:16Z): the proxy-boundary session recorded its
confirmation at 09:25:20Z, and its two asks are in the code — the one library call
(agent/all.IsClientCompaction, proxy/agentcompaction.go is a thin wrapper) and the
temporary home with the #1312 note in agent/all/all.go. The agent package doc states the
host contract to strip x-context-guru-* headers. The condition is met.

Non-blocking

  • thread/host.go:42 — the new doc sentence reads "agent/all.IsClientCompaction of that same
    headers and body". Grammar only; the meaning is clear.

What I checked

  • Last review: round 1, cb736c2942de63380463638d5db09a9a84a3feaa, 2026-11-24T11:09:56Z.
  • New commits read: 1 (4c7c30d3 fix(compaction): expire the Pi mark, gate the Codex mark,
    fix stale pointers). Full diff read, plus the code around each change.
  • Read in full: 2 reviews, 3 comments, 0 inline comments (from the three saved files).
  • New items since my last review: reviewer 2's round-1 review (11:12:07Z), the author's
    round-1 reply (11:18:42Z), and the fix commit. Each is addressed above.
  • The fix commit, line by line: the Pi Map + isCompacting expiry and its two tests
    (including the node:test mock.method(Date, "now", …) usage, whose
    mock.mockImplementation / mock.restore API is valid); the session_shutdown delete next
    to checks.end; the test file's claim logic (a second contextGuru call with
    shared[LOADED] === PACKAGE_DIR still registers handlers, so tests 2 and 3 run real
    handlers); the codex.IsCompaction gate on Identify (nil or non-Codex headers give
    false, no panic — Identify checks len(h) == 0 first); the updated codex, proxy and
    design-doc comments.
  • The gated body path: with the gate, a nil-header host (the bifrost adapter) now gets false
    from codex.IsCompaction where round 1 could have read client_metadata. That matches the
    documented contract ("a host that has no headers passes a nil h and gets false"), and the
    proxy always has headers.
  • Commit carries Signed-off-by and Assisted-By, conventional title.
  • Limits: Go and Node tests were not run (not approved). The Codex, OpenCode and Pi source
    citations remain external; I take them as the author states them.

@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 @amiddaid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 offload-review skill, @amiddaid's session resumed 2 of 2 2 of 2 4c7c30d3d234 approve

Verdict: approve

Findings

None.

Round-1 findings

  1. Pi mark can stay on for a session — fixed. session_shutdown now clears the id (context-guru.ts:551), and the mark expires after 10 minutes (isCompacting, context-guru.ts:417). Two new tests cover both exits (compaction-mark.test.ts:53, 61). I also verified the fix is needed, not just hardening: in Pi 1.6.0 (dist/core/agent-session.js), an auto-compaction aborted before compaction_start emits neither clearing event, and a successful compaction whose saved-entry lookup misses emits neither either. The expiry bounds both to 10 minutes.

  2. Non-blocking notes — all resolved.

    • Codex gate: IsCompaction now answers only when Identify accepts the caller (codex.go:76). A nil header is handled (Identify returns false on an empty header). Tests added for nil, not-Codex, and originator-only.
    • client_metadata shape: the author's citation is right. I confirmed in openai/codex rust-v0.172.0 (codex-rs/codex-api/src/common.rs): Option<HashMap<String, String>>, with the turn metadata as one string value under x-codex-turn-metadata (stated in core/src/responses_metadata.rs, constant in core/src/client.rs). The current read is correct.
    • OpenCode fields: I confirmed in sst/opencode v1.19.6 that chat.headers input has agent: string, and that compaction runs as agents.get("compaction"). In v2.4.1, SessionHttpRequest.kind is a SessionRequestKind that includes "compaction". Both marks are wired to real fields.
    • Stale doc pointers: fixed in apply/opts.go, thread/host.go, dash/schema.go, both proxy/proxy.go comments, and docs/design.md.
  3. Reviewer 1's round-1 notes — I checked each myself. All five are addressed by the same commit: the Pi stick (same fix), the OpenCode v1 agent-name note (documented in the comment, with the source citation), the stale pointers (fixed), the extra body scan (accepted, cost is small), and the Codex trust (fixed with the Identify gate). Agree with the dispositions.

Non-blocking

  • The PR description is now slightly stale after the fix commit. Line "Any client can send the plugin header or the Codex metadata" — the Codex metadata path now requires a Codex User-Agent, so only the plugin header stays open. Line "from session_before_compact until session_compact or session_compact_failed" — the Pi mark now also expires after 10 minutes and clears on shutdown. Description text only; the code comments are correct.

What I checked

  • Last review: round 1, cb736c2, 2026-11-24T11:12:07Z.
  • New commits read: 1 — 4c7c30d3 fix(compaction): expire the Pi mark, gate the Codex mark, fix stale pointers.
  • Read in full: 2 reviews, 3 comments, 0 inline comments (from the three saved files), plus the PR body (88 lines), which round-1 independence had kept me from reading.
  • New items since my last review: commit 4c7c30d3 (full diff and the code around each change); the fix-summary comment at 11:18:42Z (each claim checked against the code); the PR body (behavior-change naming below). I also read reviewer 1's round-1 review and the two earlier comments (the human's behavior-change approval at 09:24:16Z, the proxy-boundary confirmation at 09:25:20Z) now that independence no longer applies.
  • The fix commit in detail: the Set → Map migration (all five usages updated, no stale .has/.add left), the isCompacting expiry logic, the Identify gate and its effect on the WebSocket body path, the updated proxy test (5 header cases, count still 2, the not-Codex case now ignored), and the test-helper change to a Codex User-Agent.
  • The LOADED guard does not block the new tests: it returns early only for a different package dir, so tests 2 and 3 register real handlers, and test 2 asserts the mark is present before shutdown (not vacuous).
  • Behavior-change rule: the PR description names the change at the top — who, old and new behavior, the paths these clients now reach, the Responses input reading, the trust question, and how to turn it off — with the human's yes and the proxy-boundary confirmation linked. Both confirmations match the two comments. The boundary session's two required changes are in the code: one library call (agent/all.IsClientCompaction), and the agent/all home with the #1312 note.
  • External source checks, new this round (public upstream files, read only): openai/codex rust-v0.172.0 — the header constant is x-codex-turn-metadata, request_kind serializes to "compaction", client_metadata is Option<HashMap<String, String>>, and the fallback phrase matches prompts/templates/compact/prompt.md. sst/opencode v1.19.6 and v2.4.1 — the agent and kind fields as above. Pi 1.6.0 — the three event names and the two no-event paths named in finding 1.
  • I ran no builds or tests (not approved). The author's test notes (race tests, plugin tests) are taken as reported.

@amiddavid
amiddavid merged commit 7f657d1 into main Nov 24, 2026
10 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 24, 2026
@amiddavid
amiddavid deleted the feat/client-compaction branch November 24, 2026 12:52
amiddavid added a commit that referenced this pull request Nov 24, 2026
chore(plugins): bump the plugin versions for v0.7.8

- Pi package: 0.1.6 -> 0.1.7 (#1326, #1330, #1335, #1342, #1344).
- OpenCode plugin: pinned proxy version v0.7.7 -> v0.7.8.
- Unchanged: Claude Code plugin 0.7.8 (test files only changed since
  v0.7.7), Codex 0.2.14, Bob 0.1.3.


Release owner (David Amid, 2026-11-24, integrator session): cut v0.7.8 now. Version bumps are under the owner standing approval.

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[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.

fix(thread): detect client compaction for Codex, OpenCode and Pi

1 participant