Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
feat(chat): show context compaction as a chip instead of the summary wall
review894da6d92e5d
diff --git a/src/core/transcript-reader.test.ts b/src/core/transcript-reader.test.tsindex bbd2f546b..853c31b2e 100644--- a/src/core/transcript-reader.test.ts+++ b/src/core/transcript-reader.test.ts@@ -603,6 +603,107 @@ describe('Edit / Write diffs (opt-in `rich`)', () => {})})+describe('Compaction chip (opt-in `rich`)', () => {+ // The measured records (CLI 2.1.314): the boundary is a `system` record, and the summary the model+ // continues from is a `user` record flagged `isCompactSummary` — the model's text, not the user's.+ const compactBoundary = (trigger: unknown, preTokens?: unknown, postTokens?: unknown): string =>+ JSON.stringify({+ type: 'system',+ subtype: 'compact_boundary',+ content: 'Conversation compacted',+ timestamp: '2026-11-02T10:00:00.000Z',+ compactMetadata: { trigger, preTokens, postTokens }+ })+ const compactSummaryUser = (text: string, flag: unknown = true): string =>+ JSON.stringify({+ type: 'user',+ isCompactSummary: flag,+ isVisibleInTranscriptOnly: true,+ timestamp: '2026-11-02T10:00:01.000Z',+ message: { role: 'user', content: text }+ })++ it('rich: a compact boundary is a Compaction chip and the compact summary record is skipped', () => {+ const out = parseChatMessages(+ [compactSummaryUser('This session is being continued…'), compactBoundary('auto', 274430, 16889)],+ { rich: true }+ )+ expect(out).toEqual([+ {+ role: 'assistant',+ parts: [{ kind: 'tool', name: 'Compaction', arg: 'auto', result: '274430 → 16889 tokens' }],+ at: expect.any(Number)+ }+ ])+ expect(out[0].at).toBe(Date.parse('2026-11-02T10:00:00.000Z'))+ })++ it('without rich the boundary yields nothing and the summary is a user message', () => {+ const raw = [compactBoundary('auto', 274430, 16889), compactSummaryUser('This session is being continued…')]+ const summary = [+ {+ role: 'user',+ parts: [{ kind: 'text', text: 'This session is being continued…' }],+ at: Date.parse('2026-11-02T10:00:01.000Z')+ }+ ]+ expect(parseChatMessages(raw)).toEqual(summary)+ expect(parseChatMessages(raw, { thinking: true })).toEqual(summary)+ })++ it('a boundary with no token counts has no result', () => {+ const noMeta = JSON.stringify({ type: 'system', subtype: 'compact_boundary', content: 'Conversation compacted' })+ const raw = [+ compactBoundary('manual'),+ compactBoundary('auto', 274430), // one count only+ compactBoundary('auto', '274430', '16889'), // counts that are not numbers+ compactBoundary(42, 1, 2), // a trigger that is not a string+ noMeta,+ JSON.stringify({ type: 'system', subtype: 'compact_boundary', compactMetadata: null })+ ]+ expect(parseChatMessages(raw, { rich: true }).flatMap((m) => m.parts)).toEqual([+ { kind: 'tool', name: 'Compaction', arg: 'manual' },+ { kind: 'tool', name: 'Compaction', arg: 'auto' },+ { kind: 'tool', name: 'Compaction', arg: 'auto' },+ { kind: 'tool', name: 'Compaction', arg: '', result: '1 → 2 tokens' },+ { kind: 'tool', name: 'Compaction', arg: '' },+ { kind: 'tool', name: 'Compaction', arg: '' }+ ])+ })++ it('the trigger is capped like any tool arg', () => {+ const [part] = parseChatMessages([compactBoundary('x'.repeat(250))], { rich: true }).flatMap((m) => m.parts)+ expect(part).toEqual({ kind: 'tool', name: 'Compaction', arg: 'x'.repeat(200) })+ })++ it('rich: another system record yields nothing, and a summary flag that is not `true` stays a user message', () => {+ const raw = [+ JSON.stringify({ type: 'system', subtype: 'turn_duration', durationMs: 1200 }),+ JSON.stringify({ type: 'system', content: 'Conversation compacted' }),+ compactSummaryUser('typed by a person', 'true')+ ]+ expect(parseChatMessages(raw, { rich: true })).toEqual([+ { role: 'user', parts: [{ kind: 'text', text: 'typed by a person' }], at: Date.parse('2026-11-02T10:00:01.000Z') }+ ])+ })++ it('rich rides the paged parser too: the chip carries its line offset as key', () => {+ const before = said('user', 'go')+ const file = Buffer.from(before + compactBoundary('auto', 9, 3) + '\n' + compactSummaryUser('continued…') + '\n')+ const rich = parseChatWindow(file, 0, claudeChatRecordParser({ rich: true })).messages+ expect(rich).toEqual([+ { role: 'user', parts: [{ kind: 'text', text: 'go' }], key: 0 },+ {+ role: 'assistant',+ parts: [{ kind: 'tool', name: 'Compaction', arg: 'auto', result: '9 → 3 tokens' }],+ at: Date.parse('2026-11-02T10:00:00.000Z'),+ key: Buffer.byteLength(before)+ }+ ])+ expect(parseChatWindow(file, 0).messages.map((m) => m.role)).toEqual(['user', 'user'])+ })+})+describe('readChatWindow — byte windows read from a real file', () => {let dir: stringafterEach(() => {diff --git a/src/core/transcript-reader.ts b/src/core/transcript-reader.tsindex 559b8a8a8..965c124ed 100644--- a/src/core/transcript-reader.ts+++ b/src/core/transcript-reader.ts@@ -15,6 +15,7 @@ import { BASH_COMMAND_TOOL, CHAT_TOOL_ARG_MAX, COMMAND_OUTPUT_TOOL } from '../shimport {AGENT_MESSAGE_TOOL,BACKGROUND_TASK_TOOL,+ COMPACTION_TOOL,SYSTEM_TOOL,expandPastedContent} from '../shared/chat-system-records'@@ -375,7 +376,9 @@ export interface ChatParseOptions {* media type is in `CHAT_IMAGE_TYPES` and `data` is a non-empty string. An image inside a* `tool_result` is tool output and stays out (`summarizeResult` ignores it, as before). Off by* default, like `thinking`: the golden fixtures and the phone's `chat.page` never set it.- * Also: a `diff` on each `Edit` / `Write` tool part whose input reads (`toolDiff`).+ * Also: a `diff` on each `Edit` / `Write` tool part whose input reads (`toolDiff`), and a+ * `Compaction` chip for each context compaction (`compactionPart`) in place of the long summary+ * record the model continues from, which is skipped.*/rich?: boolean}@@ -417,6 +420,28 @@ function toolDiff(name: string, input: unknown): ChatToolDiff | undefined {return undefined}+/**+ * A context compaction (`ChatParseOptions.rich`) as ONE tool part, from the `compactMetadata` of the+ * `type:'system'` record claude writes at the boundary — measured (CLI 2.1.314) as+ * `{subtype:'compact_boundary', content:'Conversation compacted', compactMetadata:{trigger:'auto' |+ * 'manual', preTokens, postTokens, …}}`. The trigger is the `arg` ('' when it is not a string; capped+ * like any tool arg); the `result` is `<preTokens> → <postTokens> tokens` in plain digits, only when+ * both are finite numbers.+ */+function compactionPart(meta: unknown): Extract<ChatPart, { kind: 'tool' }> {+ const m: Record<string, unknown> = isRecord(meta) ? meta : {}+ const part: Extract<ChatPart, { kind: 'tool' }> = {+ kind: 'tool',+ name: COMPACTION_TOOL,+ arg: typeof m.trigger === 'string' ? m.trigger.slice(0, CHAT_TOOL_ARG_MAX) : ''+ }+ const { preTokens: pre, postTokens: post } = m+ if (typeof pre === 'number' && Number.isFinite(pre) && typeof post === 'number' && Number.isFinite(post)) {+ part.result = `${pre} → ${post} tokens`+ }+ return part+}+const userTextOf = (text: string, opts: ChatParseOptions | undefined): string =>opts?.expandPastes === false ? text : expandPastedContent(text)@@ -597,6 +622,9 @@ function parseChatRecords(let o: Parameters<typeof isHiddenMetaRecord>[0] & {timestamp?: unknowneffort?: unknown+ subtype?: unknown+ compactMetadata?: unknown+ isCompactSummary?: unknownmessage?: { content?: unknown; model?: unknown }}try {@@ -616,6 +644,14 @@ function parseChatRecords(model = metaString(o.message?.model)effort = metaString(o.effort)}+ // Opt-in (see `ChatParseOptions.rich`): without it the boundary yields nothing and the summary+ // is a user message, as before. The summary is the model's own recap of the conversation so far,+ // not the user's words: the chip carries the fact, and the summary is skipped whatever its shape.+ if (opts?.rich && o.type === 'system' && o.subtype === 'compact_boundary') {+ push({ role: 'assistant', parts: [compactionPart(o.compactMetadata)] }, offset)+ continue+ }+ if (opts?.rich && o.type === 'user' && o.isCompactSummary === true) continueif (o.type === 'assistant' && Array.isArray(content)) {const parts: ChatPart[] = []for (const c of content as Array<{diff --git a/src/renderer/lib/chatThread.test.ts b/src/renderer/lib/chatThread.test.tsindex f44d8349a..e4f9fbc09 100644--- a/src/renderer/lib/chatThread.test.ts+++ b/src/renderer/lib/chatThread.test.ts@@ -66,13 +66,13 @@ describe('assistantTurnEnds (one action row per assistant turn)', () => {expect(ends.get(0)).toEqual({ copyText: '', at: undefined })})- it('a system chip (Background task / Agent message / System) stands alone and splits the turns around it', () => {+ it('a system chip (Background task / Agent message / System / Compaction) stands alone and splits the turns around it', () => {const chip = (name: string, at?: number): ChatMessage => ({role: 'assistant',parts: [{ kind: 'tool', name, arg: 'x', result: 'y' }],...(at === undefined ? {} : { at })})- for (const name of ['Background task', 'Agent message', 'System']) {+ for (const name of ['Background task', 'Agent message', 'System', 'Compaction']) {const msgs = [say('user', 'q'), say('assistant', 'first answer', 10), chip(name, 20), say('assistant', 'reply', 30)]const ends = assistantTurnEnds(msgs)expect([...ends.keys()]).toEqual([1, 3])diff --git a/src/renderer/lib/chatToolGroups.test.ts b/src/renderer/lib/chatToolGroups.test.tsindex 1081c1cf2..dbc26df6f 100644--- a/src/renderer/lib/chatToolGroups.test.ts+++ b/src/renderer/lib/chatToolGroups.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest'import type { ChatMessage, ChatPart } from '@shared/types'-import { BACKGROUND_TASK_TOOL } from '@shared/chat-system-records'+import { BACKGROUND_TASK_TOOL, COMPACTION_TOOL } from '@shared/chat-system-records'import { COMMAND_OUTPUT_TOOL } from '@shared/chat-command'import { groupToolCalls, memberKey, partPosition, toolGroupSummary } from './chatToolGroups'@@ -86,6 +86,22 @@ describe('groupToolCalls', () => {expect(layout.groupAt.size).toBe(0)})+ it('a Compaction chip between tool calls splits the run and is never folded (it is a system record)', () => {+ const compaction: ChatMessage = {+ role: 'assistant',+ parts: [tool(COMPACTION_TOOL, { arg: 'auto', result: '274430 → 16889 tokens' })]+ }+ const messages = [call('Bash'), call('Bash'), compaction, call('Bash'), call('Bash')]++ const layout = groupToolCalls(messages)++ expect(runs(messages)).toEqual([+ ['0:0', '1:0'],+ ['3:0', '4:0']+ ])+ expect(layout.absorbed.has('2:0')).toBe(false)+ })+it('keys a group by its first line offset, so it keeps its key while the run grows', () => {const before = groupToolCalls([user('go'), call('Bash', 100), call('Read', 200)])const after = groupToolCalls([user('go'), call('Bash', 100), call('Read', 200), call('Edit', 300)])diff --git a/src/shared/chat-fixtures/README.md b/src/shared/chat-fixtures/README.mdindex e8ffc9088..7c90fd24a 100644--- a/src/shared/chat-fixtures/README.md+++ b/src/shared/chat-fixtures/README.md@@ -105,6 +105,12 @@ nothing above changes. What it adds:when `file_path` is a non-empty string and the texts are strings (an empty `new_string` is adeletion and still a diff). Each text is capped like a tool `body` (65536 UTF-16 units, then`TOOL_BODY_TRUNCATED`). Any other shape yields no `diff`; `arg` and `result` are unchanged.+- A `{type:"system", subtype:"compact_boundary", compactMetadata}` record becomes ONE assistant+ message with ONE tool part `{name:"Compaction", arg, result?}`: `arg` is `compactMetadata.trigger`+ (`""` when not a string; its first 200 UTF-16 units, `CHAT_TOOL_ARG_MAX`), and `result` is+ `"<preTokens> → <postTokens> tokens"` (plain digits, U+2192) only when both are finite numbers. It+ is a system record (a turn boundary, never folded). The `type:"user"` record with+ `isCompactSummary: true` (the summary the model continues from) is skipped.Decision cases: plan `restore` / `acceptEdits` / `manual` / `revise` (the revise text is trimmed),question single / multi (labels joined with `, `) / free text (trimmed), and three refusals: adiff --git a/src/shared/chat-system-records.ts b/src/shared/chat-system-records.tsindex 407b9551a..f273d1976 100644--- a/src/shared/chat-system-records.ts+++ b/src/shared/chat-system-records.ts@@ -8,10 +8,12 @@ import type { ChatMessage } from './types'export const BACKGROUND_TASK_TOOL = 'Background task'export const AGENT_MESSAGE_TOOL = 'Agent message'export const SYSTEM_TOOL = 'System'-const SYSTEM_RECORD_TOOLS: ReadonlySet<string> = new Set([BACKGROUND_TASK_TOOL, AGENT_MESSAGE_TOOL, SYSTEM_TOOL])+/** A context compaction (the `compact_boundary` system record, read under `ChatParseOptions.rich`). */+export const COMPACTION_TOOL = 'Compaction'+const SYSTEM_RECORD_TOOLS: ReadonlySet<string> = new Set([BACKGROUND_TASK_TOOL, AGENT_MESSAGE_TOOL, SYSTEM_TOOL, COMPACTION_TOOL])/** A message the reader made from a system-injected record: an assistant message holding exactly- * ONE tool part named Background task / Agent message / System. */+ * ONE tool part named Background task / Agent message / System / Compaction. */export function isSystemRecordMessage(m: ChatMessage): boolean {if (m.role !== 'assistant' || m.parts.length !== 1) return falseconst p = m.parts[0]