Skip to content

feat(bridge): attach the context-composition snapshot to the context meter - #14

Merged
dpolishuk merged 1 commit into
mainfrom
feat/context-snapshot-stage11
Nov 22, 2026
Merged

dpolishuk merged 1 commit into
mainfrom
feat/context-snapshot-stage11

Conversation

@dpolishuk

Copy link
Copy Markdown
Owner

Stage 11 of the November upstream plan (535c70c)

bb's contextWindow delta accepts a rich snapshot (categories, entries,
per-bucket tokens) that the plugin never sent — the audit's long-standing
gap. Upstream v3.18.x added v4/conversation/contextComposition
(revision-guarded, existing-only, names-and-sizes only), which is exactly
the missing source.

Change

  • contextSnapshot() (src/usage.ts, pure, unit-tested): maps a native
    request-phase measurement onto bb's snapshot grammar — 11 fine categories
    keep identity with the projected kind (messages/compact_summary/
    reminders/meta_user_context → used; prompt overhead + loaded tool
    schemas → reserved; deferred_tool_schemas → deferred), entries respect
    the wire caps (≤50 per category, ≤200-char names), a synthetic free
    bucket comes from the native freeTokens; draft previews drop.
  • finishTurn: one bounded (2 s) query after session/read; a
    per-session revision guard drops a measurement that raced a newer settle.
    Older CLIs answer method-not-found → today's plain meter, unchanged.
  • The snapshot can also supply the meter numbers (ring-aligned
    providerContextTokens + measured window) when the projection read has
    none — Usage.context prefers the projection and falls back.

Verification

  • 4 new tests: snapshot mapping unit table (kinds/entries/free/fallbacks/
    unusable drops), bridge integration (snapshot attached with mapped
    categories; no snapshot on an old-CLI session).
  • Full suite 298 green on this branch, typecheck, format:check.
  • Live note: local Desktop is 3.17.2 (no composition query in its bundle),
    so the live happy path on 3.18.x is pending the app update; the degrade
    path is what runs live today.

Docs: CHANGELOG, roadmap stage 11 done, knowledge/log.md.

Stack: based on #13 (stage 10); merge #13 first.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T17:06:57.057307Z 6ddd27e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4951add9fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/bridge.ts
Comment on lines +789 to +792
"v4/conversation/contextComposition",
{ sessionId: session.providerThreadId },
2000,
),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Mark the optional composition query as read-only

If the composition query takes longer than two seconds, ZCodeClient.request() defaults its fourth argument to readOnly = false, so its timeout calls fail() and closes the shared ZCode process. The surrounding catch cannot undo that: an optional context-meter refresh can terminate an otherwise healthy session and trigger crash recovery instead of falling back to the plain meter. Pass the read-only flag so a slow composition read only drops the snapshot.

Useful? React with 👍 / 👎.

Comment thread src/usage.ts Outdated
Comment on lines +87 to +88
const size = count(model.contextWindow) ?? fallback.size ?? null;
if (size === null || size === 0) return undefined;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Apply the selected reporting budget to the rich snapshot

When a user selects a budget variant such as test/model-256k for a native 1M-token model, finishTurn() passes the selected 256,000-token window, but this expression prefers the native window. Consequently, the same delta reports size: 256000 alongside snapshot.contextWindowTokens: 1000000; copying native freeTokens also makes the snapshot's free bucket exceed the selected budget. This contradicts the existing budget behavior covered in tests/core-features.test.ts. Apply the selected reporting window to the snapshot and recompute its free bucket against that window.

Useful? React with 👍 / 👎.

@dpolishuk

Copy link
Copy Markdown
Owner Author

Review round 1 fixed (this branch now includes the rebased stage-10 head plus 0de3126):

  • P1 (composition query could kill the child on timeout) — the request is now readOnly = true: a slow read drops only the snapshot, per the client's existing read-only timeout semantics.
  • P2 (snapshot ignored the reporting budget) — the snapshot reports against the bridge-supplied window (the selected budget variant), and the free bucket is recomputed as window - usedTokens instead of copying the native-window freeTokens, so the delta's size and the snapshot can no longer disagree.

Suite 298 green on the branch; stacked on #13.

@dpolishuk

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 4951add9fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@dpolishuk
dpolishuk force-pushed the feat/context-snapshot-stage11 branch from 4951add to caf1559 Compare November 22, 2026 11:13
@dpolishuk

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: caf15598de

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…meter

After a turn settles, the bridge queries the CLI's
v4/conversation/contextComposition (added upstream in v3.18.x; older CLIs
answer method-not-found and keep the plain meter) and maps the measurement
onto the contextWindow delta's rich snapshot:

- native fine categories keep their identity with the projected bb kind -
  conversation content (messages, compact summary, reminders, user context)
  is used, prompt overhead and loaded tool schemas are reserved, deferred
  tool schemas are deferred - with per-entry token counts (wire caps: 50
  entries, 200-char names);
- the snapshot reports against the bridge-supplied window - the selected
  reporting budget (e.g. a 256k variant of a 1M model) - and the free
  bucket is recomputed from it, so the delta and its snapshot never
  disagree;
- only request-phase measurements map; draft previews are dropped;
- the query is read-only and bounded (2 s): a slow read drops only the
  snapshot, never the shared child process; a per-session revision guard
  drops a measurement that raced a newer settle;
- usedTokens come from the ring-aligned providerContextTokens, and the
  snapshot can supply the meter numbers when the projection read has none.

Roadmap stage 11; live 3.18.x verification is pending the local Desktop
update (3.17.2 at implementation time).
@dpolishuk

Copy link
Copy Markdown
Owner Author

Both findings closed in caf1559 (now rebased onto the updated stage-10 head as 6ddd27e):

  • P1 (composition query could kill the child on timeout) — request is readOnly = true; a slow read drops only the snapshot.
  • P2 (snapshot ignored the reporting budget) — the snapshot reports against the bridge-supplied window and the free bucket is recomputed from it.

Suite 298 green.

@dpolishuk
dpolishuk force-pushed the feat/context-snapshot-stage11 branch from caf1559 to 6ddd27e Compare November 22, 2026 14:56
@dpolishuk

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 6ddd27ee57

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@dpolishuk
dpolishuk changed the base branch from chore/november-upstream-verification to main November 22, 2026 17:49
@dpolishuk
dpolishuk merged commit 3db7e6c into main Nov 22, 2026
1 of 3 checks passed
@dpolishuk
dpolishuk deleted the feat/context-snapshot-stage11 branch November 22, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant