Skip to content

feat(clients): show a compaction divider's before and estimated after token counts - #789

Merged
huichen merged 2 commits into
mainfrom
feat/compaction-token-delta
Sep 23, 2026
Merged

huichen merged 2 commits into
mainfrom
feat/compaction-token-delta

Conversation

@huichen

@huichen huichen commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

What

After a context compaction commits, the desktop and mobile dividers now show both numbers the agent already reports: the pre-compaction token count and its estimate for the next turn's prompt.

Context compacted · 613,994 → 51,081 tokens (estimated)
上下文已压缩 · 613,994 → 51,081 tokens(预估)

The TUI has shown this pair ("Context compacted: 670217 → 35437 tokens (estimated)") since it was written; the two clients that draw the compaction marker as a hairline divider only read tokens_before.

Why nothing had to change in the agent

The pair is already on the wire in both paths:

  • compaction_committed carries tokens_before + tokens_after (agent/src/rpc/prompt_helpers.rs);
  • the durable checkpoint entry carries both, surfaced camelCase by future_rpc::message::checkpoint_metadata.

tokens_after is an estimate (history_after + fixed_input_tokens in compaction/finalize), not provider usage — hence the "(estimated)" / "(预估)" qualifier, matching the TUI.

Changes

  • packages/thread-projection: tokensAfter on the compaction MessageSegment; read from the durable entry projection (entry.checkpoint.tokensAfter) and from the live projector (compaction_committed/compaction_end payload).
  • Desktop: CompactionDivider renders the pair (automatic and manual), the standalone/manual live path passes it through, en/zh copy.
  • Mobile: same, plus TimelineSegment.tokensAfter.
  • A missing tokensAfter (a released run journal's compaction_end, a legacy row) keeps today's before-only label.

Mobile uses a terser sentence when the pair is present

The divider row is ~350px wide at 12px, and the desktop sentence plus the pair (你手动压缩了此对话的上下文 · 33,064 → 9,250 tokens(预估), measured 346px) wraps onto two lines, leaving the tail 估) alone on the second. The shorter phrasing measures 286px — one line — with the pair, the unit and the estimate marker intact.

Verification

  • desktop: tsc --noEmit, eslint, full vitest run (144 files / 1233 tests) — green; new MessageBlock.compaction.test.tsx renders the real divider and asserts the exact label in en and zh, the manual variant, the before-only fallback and that running/failed dividers carry no counts.
  • mobile: tsc --noEmit, eslint, full jest (121 suites / 1840 tests) — green; new divider unit tests plus tokensBefore/tokensAfter coverage in all three projector paths (single/batch/replay) and in the durable timeline.
  • Screenshot harness: the two shot mocks that emit a compaction now carry the second number (the real agent always sends it), and the compact-resume / compact-context-outcome / compact-context-delayed scenarios assert the pair instead of the bare label. Captured and measured in the harness on both platforms: the label is one line, scrollWidth == clientWidth, no clipping (mobile 286px in a 334px slot; desktop 272px in a 768px column).

… token counts

The agent has always reported both halves of a committed checkpoint
(`tokens_before` in the `compaction_committed` event and the checkpoint entry's
`tokens_before`/`tokens_after`), and the TUI has shown them as
"Context compacted: 670217 → 35437 tokens (estimated)" since it was written.
The desktop and mobile dividers only ever read `tokens_before`, so the two
clients that draw the divider as a hairline label showed what the turn was
about to send and nothing about what the compaction bought.

Carry `tokensAfter` through the shared thread projection (segment model, the
durable entry projection, the live run projector) and render the pair on both
dividers. `tokensAfter` is an estimate, not provider usage: it is the local
per-message estimate of the kept history plus the fixed system/tools overhead,
so the labels say so — matching the TUI's wording.

Mobile uses a terser sentence than the desktop when the pair is present. The
divider row is ~350px wide at 12px, and the desktop sentence plus the pair
("你手动压缩了此对话的上下文 · 33,064 → 9,250 tokens(预估)", 346px) wraps onto
two lines, leaving the tail "估)" alone on the second. Measured in the harness:
the shorter phrasing is 286px, one line, with the pair intact.

The two shot mocks that emit a compaction gain the second number (the real
agent always sends it), and the mobile/desktop scenarios now assert the pair
rather than the bare label.
@huichen
huichen enabled auto-merge (squash) September 23, 2026 01:43
@huichen
huichen merged commit 9366151 into main Sep 23, 2026
12 checks passed
@huichen
huichen deleted the feat/compaction-token-delta branch September 23, 2026 01:46
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