Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

End to end, rounds 11-12: compaction and questions at their edges, and a turn shown starting twice (#751)

Dee8d27ce437c0f
diff --git a/cli/e2e/compaction.e2e.ts b/cli/e2e/compaction.e2e.ts
new file mode 100644
index 000000000..44da445be
--- /dev/null
+++ b/cli/e2e/compaction.e2e.ts
@@ -0,0 +1,130 @@
+/**
+ * Compaction, for Claude Code and Codex: `/compact` between turns and an automatic compaction in the
+ * middle of one. Claude Code announces the same session again after it, and the daemon re-reads the
+ * transcript while it still tails it — the path where turns used to be shown twice or lost. Every turn
+ * after a compaction starts and ends once, live, and the history still reads back whole.
+ */
+import { mkdirSync } from 'node:fs'
+import { join } from 'node:path'
+import { afterEach, describe, expect, it, onTestFailed } from 'vitest'
+import { LocalClient, type Frame } from './harness/client.js'
+import { IsolatedDaemon, until } from './harness/daemon.js'
+
+type Engine = 'claude' | 'codex'
+const engines: Engine[] = ['claude', 'codex']
+
+const row = async (client: LocalClient, agentId: string) =>
+ ((await client.request<{ agents: Array<Record<string, any>> }>('agents_list', { includeStopped: true }, 30_000)).agents)
+ .find((agent) => agent.id === agentId)
+async function create(daemon: IsolatedDaemon, client: LocalClient, engine: Engine, folder: string): Promise<Record<string, any>> {
+ const cwd = join(daemon.projectsDir, folder)
+ mkdirSync(cwd, { recursive: true })
+ const created = await client.request('agent_create', { engine, cwd, bypassPermission: true }, 90_000)
+ expect(created.error, `${folder}: ${JSON.stringify(created)}`).toBeUndefined()
+ return until(`${folder} to bind its conversation`, async () => {
+ const agent = await row(client, created.agent.id)
+ return agent?.sessionId && agent.status === 'active' ? agent : null
+ }, 60_000, 500)
+}
+const isTurn = (type: string, agentId: string) => (frame: Frame) => frame.type === type && frame.agentId === agentId
+async function turn(client: LocalClient, agentId: string, content: string): Promise<void> {
+ const ended = client.next(isTurn('turn_ended', agentId), 45_000, `turn_ended (${content})`)
+ client.send('message', { agentId, content })
+ await ended
+}
+/** Types the compaction command into the pane, as a person types `/compact`. */
+async function compact(daemon: IsolatedDaemon, client: LocalClient, agent: Record<string, any>): Promise<void> {
+ await daemon.tmux.run('send-keys', '-t', agent.tmuxPane, '!compact', 'Enter')
+ await until('the compaction to land', async () => (await daemon.capture(agent.tmuxPane)).includes('(compacted)') || null, 15_000, 100)
+ // Claude Code's repeat SessionStart: let the re-read it causes finish.
+ await new Promise((resolve) => setTimeout(resolve, 1_000))
+}
+const count = (client: LocalClient, since: number, type: string, agentId: string) =>
+ client.frames.slice(since).filter(isTurn(type, agentId)).length
+const users = async (client: LocalClient, sessionId: string): Promise<string[]> => {
+ const page = await client.request<{ events?: Array<{ type: string; payload: Record<string, any> }>; error?: string }>('session_get', { sessionId, limit: 200 }, 30_000)
+ expect(page.error, JSON.stringify(page)).toBeUndefined()
+ return (page.events ?? []).filter((event) => event.type === 'user_message').map((event) => String(event.payload.content))
+}
+
+describe('compaction', () => {
+ let daemon: IsolatedDaemon | undefined
+ afterEach(async () => { await daemon?.close(); daemon = undefined })
+ const fresh = async () => {
+ const d = await IsolatedDaemon.create()
+ daemon = d
+ onTestFailed(() => { console.log(`---- daemon log\n${d.log().split('\n').slice(-150).join('\n')}`) })
+ await d.start()
+ return d
+ }
+
+ it.each(engines)('%s: after /compact every turn starts and ends once, live, and the history reads back whole', async (engine) => {
+ const d = await fresh()
+ const client = await LocalClient.connect(d)
+ const agent = await create(d, client, engine, `compact-${engine}`)
+ for (const n of [1, 2, 3]) await turn(client, agent.id, `before ${n}`)
+ await compact(d, client, agent)
+ const since = client.frames.length
+ for (const n of [1, 2, 3]) await turn(client, agent.id, `after ${n}`)
+ await new Promise((resolve) => setTimeout(resolve, 1_000))
+ expect(count(client, since, 'turn_started', agent.id)).toBe(3)
+ expect(count(client, since, 'turn_ended', agent.id)).toBe(3)
+ expect((await row(client, agent.id))?.status).toBe('active')
+ expect(await users(client, agent.sessionId)).toEqual(['before 1', 'before 2', 'before 3', 'after 1', 'after 2', 'after 3'])
+ client.close()
+ })
+
+ it.each(engines)('%s: a compaction in the middle of a turn: the turn ends once, and the next one is fresh', async (engine) => {
+ const d = await fresh()
+ const client = await LocalClient.connect(d)
+ const agent = await create(d, client, engine, `compact-mid-${engine}`)
+ await turn(client, agent.id, 'a turn before')
+ const since = client.frames.length
+ await turn(client, agent.id, '!compactmid')
+ await new Promise((resolve) => setTimeout(resolve, 1_500))
+ expect(count(client, since, 'turn_started', agent.id)).toBe(1)
+ expect(count(client, since, 'turn_ended', agent.id)).toBe(1)
+ const next = client.frames.length
+ await turn(client, agent.id, 'the turn after')
+ expect(count(client, next, 'turn_started', agent.id)).toBe(1)
+ expect((await row(client, agent.id))?.activity?.state).not.toBe('working')
+ client.close()
+ })
+
+ it('five compactions in a row, a turn after each: nothing doubles, nothing is lost', async () => {
+ const d = await fresh()
+ const client = await LocalClient.connect(d)
+ const agent = await create(d, client, 'claude', 'compact-five')
+ const since = client.frames.length
+ for (const n of [1, 2, 3, 4, 5]) {
+ await compact(d, client, agent)
+ await turn(client, agent.id, `turn ${n}`)
+ }
+ await new Promise((resolve) => setTimeout(resolve, 1_000))
+ expect(count(client, since, 'turn_started', agent.id)).toBe(5)
+ expect(count(client, since, 'turn_ended', agent.id)).toBe(5)
+ const texts = client.frames.slice(since).filter((frame) => frame.type === 'text_delta' && frame.agentId === agent.id)
+ .map((frame) => String(frame.payload?.content ?? ''))
+ for (const n of [1, 2, 3, 4, 5]) expect(texts.filter((text) => text.includes(`: turn ${n}`)).length, `answer ${n}`).toBe(1)
+ client.close()
+ })
+
+ it.each(engines)('%s: a compaction while the daemon is down: it comes back, and turns are live again', async (engine) => {
+ const d = await fresh()
+ let client = await LocalClient.connect(d)
+ const agent = await create(d, client, engine, `compact-down-${engine}`)
+ await turn(client, agent.id, 'before')
+ client.close()
+ await d.stop()
+ // The engine compacts while nobody watches: its SessionStart finds no daemon.
+ await d.tmux.run('send-keys', '-t', agent.tmuxPane, '!compact', 'Enter')
+ await until('the compaction to land', async () => (await d.capture(agent.tmuxPane)).includes('(compacted)') || null, 15_000, 100)
+ await d.start()
+ client = await LocalClient.connect(d)
+ await until('the agent to be back', async () => (await row(client, agent.id))?.status === 'active' || null, 60_000, 500)
+ const since = client.frames.length
+ await turn(client, agent.id, 'after the restart')
+ expect(count(client, since, 'turn_started', agent.id)).toBe(1)
+ client.close()
+ })
+})
diff --git a/cli/e2e/harness/fakeEngine.mjs b/cli/e2e/harness/fakeEngine.mjs
index 29037747b..9c1cf8e93 100644
--- a/cli/e2e/harness/fakeEngine.mjs
+++ b/cli/e2e/harness/fakeEngine.mjs
@@ -15,8 +15,10 @@
// dialog and answers with their choice, `!permit <command>` asks permission to run a command the way
// the engine does and runs it only if allowed, `!flood <KiB>` prints that much to the terminal, in
// numbered lines, the way a build log or a long diff does, `!clear` starts a new conversation in the
-// same pane as Claude Code's `/clear` and Codex's `/new` do, `!exit` ends the process. Everything else
-// is echoed as the answer.
+// same pane as Claude Code's `/clear` and Codex's `/new` do, `!compact` compacts the conversation as
+// `/compact` does (and Claude Code then announces the same session again), `!compactmid` compacts in the
+// middle of a turn as an automatic compaction does, `!exit` ends the process. Everything else is
+// echoed as the answer.
import { randomUUID } from 'node:crypto'
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
@@ -158,6 +160,14 @@ export async function run(engine, config) {
}
}
}
+ const compact = () => {
+ if (engine === 'claude') {
+ claude({ type: 'system', subtype: 'compact_boundary', content: 'Conversation compacted', compactMetadata: { trigger: 'manual', preTokens: 4096 } })
+ claude({ type: 'user', isCompactSummary: true, message: { role: 'user', content: 'This session is being continued from a previous conversation that ran out of context.' } })
+ } else {
+ codex('compacted', { message: 'Summary of the conversation so far.', replacement_history: [] })
+ }
+ }
const finish = (text) => {
if (engine === 'claude') {
claude({ type: 'assistant', message: { id: `msg_${turn}`, role: 'assistant', model: config.claudeModel ?? 'claude-opus-5-5', content: [{ type: 'text', text }], stop_reason: 'end_turn' } })
@@ -175,6 +185,15 @@ export async function run(engine, config) {
const prompt = raw.trim()
if (!prompt) { draw(); return }
if (prompt === '!exit') { process.stdout.write('\x1b[?2004l\r\n'); process.exit(0) }
+ if (prompt === '!compact') {
+ // `/compact` is a command, not a turn: a summary replaces the history, and Claude Code announces
+ // the same session again (SessionStart, source compact), which makes the daemon re-read it.
+ compact()
+ process.stdout.write('\r\n(compacted)\r\n')
+ draw()
+ if (engine === 'claude') await hook('session-start', { hookEvent: 'SessionStart', source: 'compact' })
+ return
+ }
if (prompt === '!clear') {
// The old conversation ends (its open turn first), a new id and transcript begin, and the engine
// says both through its hooks, as the real CLIs do.
@@ -241,6 +260,13 @@ export async function run(engine, config) {
finish(`flooded ${kib} KiB`)
return
}
+ if (directive?.[1] === 'compactmid') {
+ // An automatic compaction in the middle of a turn: the turn goes on after it and ends once.
+ await new Promise((resolve) => setTimeout(resolve, 300))
+ compact()
+ if (engine === 'claude') await hook('session-start', { hookEvent: 'SessionStart', source: 'compact' })
+ await new Promise((resolve) => setTimeout(resolve, 300))
+ }
if (directive?.[1] === 'hold') return
if (directive?.[1] === 'permit') {
const command = directive[2] || 'printf hi'
diff --git a/cli/e2e/questions-edges.e2e.ts b/cli/e2e/questions-edges.e2e.ts
new file mode 100644
index 000000000..49cc82fa5
--- /dev/null
+++ b/cli/e2e/questions-edges.e2e.ts
@@ -0,0 +1,158 @@
+/**
+ * An agent's question at its edges, for Claude Code and Codex: asked while no window is open, answered
+ * in the terminal by hand, answered by two windows at once, cancelled with Esc, and still open across a
+ * daemon restart. Every window ends with the same answer to "is this question still open?", and the
+ * agent gets exactly one answer.
+ */
+import { mkdirSync } from 'node:fs'
+import { join } from 'node:path'
+import { afterEach, describe, expect, it, onTestFailed } from 'vitest'
+import { LocalClient, type Frame } from './harness/client.js'
+import { IsolatedDaemon, until } from './harness/daemon.js'
+
+type Engine = 'claude' | 'codex'
+const engines: Engine[] = ['claude', 'codex']
+
+const row = async (client: LocalClient, agentId: string) =>
+ ((await client.request<{ agents: Array<Record<string, any>> }>('agents_list', { includeStopped: true }, 30_000)).agents)
+ .find((agent) => agent.id === agentId)
+async function create(daemon: IsolatedDaemon, client: LocalClient, engine: Engine, folder: string): Promise<Record<string, any>> {
+ const cwd = join(daemon.projectsDir, folder)
+ mkdirSync(cwd, { recursive: true })
+ const created = await client.request('agent_create', { engine, cwd, bypassPermission: true }, 90_000)
+ expect(created.error, `${folder}: ${JSON.stringify(created)}`).toBeUndefined()
+ return until(`${folder} to bind its conversation`, async () => {
+ const agent = await row(client, created.agent.id)
+ return agent?.sessionId && agent.status === 'active' ? agent : null
+ }, 60_000, 500)
+}
+const isTurn = (type: string, agentId: string) => (frame: Frame) => frame.type === type && frame.agentId === agentId
+const asked = (agentId: string) => (frame: Frame) => frame.type === 'commander_question' && frame.agentId === agentId
+const closed = (requestId: string) => (frame: Frame) => frame.type === 'commander_question_close' && frame.payload?.requestId === requestId
+async function answer(client: LocalClient, agentId: string, requestId: string, answers: Record<string, string>): Promise<Record<string, any>> {
+ const reply = client.next((frame) => frame.type === 'question_response_result' && frame.payload?.requestId === requestId, 45_000, 'question_response_result')
+ client.send('question_response', { requestId, agentId, answers })
+ return (await reply).payload as Record<string, any>
+}
+/** The digit that picks Coffee in each engine's own dialog: Claude takes it, Codex takes it then Enter. */
+async function pickInPane(daemon: IsolatedDaemon, engine: Engine, pane: string, keys: string[]): Promise<void> {
+ for (const key of keys) await daemon.tmux.run('send-keys', '-t', pane, key)
+ if (engine === 'codex' && keys[0] !== 'Escape') await daemon.tmux.run('send-keys', '-t', pane, 'Enter')
+}
+
+describe('a question at its edges', () => {
+ let daemon: IsolatedDaemon | undefined
+ afterEach(async () => { await daemon?.close(); daemon = undefined })
+ const fresh = async () => {
+ const d = await IsolatedDaemon.create()
+ daemon = d
+ onTestFailed(() => { console.log(`---- daemon log\n${d.log().split('\n').slice(-150).join('\n')}`) })
+ await d.start()
+ return d
+ }
+
+ it.each(engines)('%s: asked while no window is open, it reaches the window that opens next, and its answer is the one used', async (engine) => {
+ const d = await fresh()
+ const first = await LocalClient.connect(d)
+ const agent = await create(d, first, engine, `ask-unwatched-${engine}`)
+ first.send('message', { agentId: agent.id, content: '!ask' })
+ await first.next(isTurn('turn_started', agent.id), 30_000, 'turn_started')
+ first.close()
+ // The dialog is drawn with nobody watching; a window that opens afterwards is told about it.
+ await until('the dialog to be drawn', async () => (await d.capture(agent.tmuxPane)).includes('Which drink would you like?') || null, 15_000, 100)
+ await new Promise((resolve) => setTimeout(resolve, 2_000))
+ // Nobody could answer it, so it was not asked; the window that opens is asked, and is the one
+ // that answers.
+ const late = await LocalClient.connect(d)
+ const question = await late.waitFor(asked(agent.id), 30_000, 'commander_question once a window can answer')
+ const ended = late.next(isTurn('turn_ended', agent.id), 45_000, 'turn_ended')
+ const result = await answer(late, agent.id, question.payload!.requestId, { [question.payload!.questions[0].q]: 'Coffee' })
+ expect(result.error, JSON.stringify(result)).toBeUndefined()
+ await ended
+ expect(await d.capture(agent.tmuxPane)).toContain('you chose Coffee')
+ late.close()
+ })
+
+ it.each(engines)('%s: answered in the terminal by hand, every window is told it is closed, and a late answer types nothing', async (engine) => {
+ const d = await fresh()
+ const client = await LocalClient.connect(d)
+ const other = await LocalClient.connect(d)
+ const agent = await create(d, client, engine, `ask-by-hand-${engine}`)
+ const question = client.next(asked(agent.id), 30_000, 'commander_question')
+ client.send('message', { agentId: agent.id, content: '!ask' })
+ const shown = await question
+ const requestId = shown.payload!.requestId as string
+ const closedHere = client.next(closed(requestId), 30_000, 'commander_question_close')
+ const closedThere = other.next(closed(requestId), 30_000, 'commander_question_close in the other window')
+ const ended = client.next(isTurn('turn_ended', agent.id), 45_000, 'turn_ended')
+ await pickInPane(d, engine, agent.tmuxPane, ['2'])
+ await Promise.all([closedHere, closedThere, ended])
+ expect(await d.capture(agent.tmuxPane)).toContain('you chose Coffee')
+ const late = await answer(client, agent.id, requestId, { [shown.payload!.questions[0].q]: 'Tea' })
+ expect(late.error, JSON.stringify(late)).toBeTruthy()
+ expect(await d.capture(agent.tmuxPane)).not.toContain('you chose Tea')
+ for (const one of [client, other]) one.close()
+ })
+
+ it.each(engines)('%s: two windows answering at once: the agent gets one answer, and the other window is told why not', async (engine) => {
+ const d = await fresh()
+ const a = await LocalClient.connect(d)
+ const b = await LocalClient.connect(d)
+ const agent = await create(d, a, engine, `ask-twice-${engine}`)
+ const question = a.next(asked(agent.id), 30_000, 'commander_question')
+ a.send('message', { agentId: agent.id, content: '!ask' })
+ const shown = await question
+ const requestId = shown.payload!.requestId as string
+ const q = shown.payload!.questions[0].q as string
+ const ended = a.next(isTurn('turn_ended', agent.id), 45_000, 'turn_ended')
+ const [first, second] = await Promise.all([answer(a, agent.id, requestId, { [q]: 'Tea' }), answer(b, agent.id, requestId, { [q]: 'Coffee' })])
+ await ended
+ const wins = [first, second].filter((result) => !result.error)
+ expect(wins, JSON.stringify([first, second])).toHaveLength(1)
+ const pane = await d.capture(agent.tmuxPane)
+ expect([pane.includes('you chose Tea'), pane.includes('you chose Coffee')].filter(Boolean)).toHaveLength(1)
+ // The agent goes on: the next question is asked, and answered. (Its id is the same when the
+ // dialog is the same: ids are named from the dialog so they survive a daemon restart.)
+ const next = a.next(asked(agent.id), 30_000, 'the next commander_question')
+ a.send('message', { agentId: agent.id, content: '!ask' })
+ const again = await next
+ const after = a.next(isTurn('turn_ended', agent.id), 45_000, 'turn_ended')
+ expect((await answer(a, agent.id, again.payload!.requestId, { [q]: 'Coffee' })).error).toBeUndefined()
+ await after
+ for (const one of [a, b]) one.close()
+ })
+
+ it.each(engines)('%s: cancelled with Esc in the terminal, every window lets go of it', async (engine) => {
+ const d = await fresh()
+ const client = await LocalClient.connect(d)
+ const agent = await create(d, client, engine, `ask-esc-${engine}`)
+ const question = client.next(asked(agent.id), 30_000, 'commander_question')
+ client.send('message', { agentId: agent.id, content: '!ask' })
+ const shown = await question
+ const gone = client.next(closed(shown.payload!.requestId), 30_000, 'commander_question_close')
+ const ended = client.next(isTurn('turn_ended', agent.id), 45_000, 'turn_ended')
+ await pickInPane(d, engine, agent.tmuxPane, ['Escape'])
+ await Promise.all([gone, ended])
+ expect(await d.capture(agent.tmuxPane)).toContain('(question cancelled)')
+ client.close()
+ })
+
+ it.each(engines)('%s: still open across a daemon restart: the restarted daemon asks again, and the answer lands', async (engine) => {
+ const d = await fresh()
+ let client = await LocalClient.connect(d)
+ const agent = await create(d, client, engine, `ask-restart-${engine}`)
+ const question = client.next(asked(agent.id), 30_000, 'commander_question')
+ client.send('message', { agentId: agent.id, content: '!ask' })
+ await question
+ client.close()
+ await d.restart()
+ client = await LocalClient.connect(d)
+ const again = await client.waitFor(asked(agent.id), 45_000, 'the question asked again after the restart')
+ const ended = client.next(isTurn('turn_ended', agent.id), 45_000, 'turn_ended')
+ const result = await answer(client, agent.id, again.payload!.requestId, { [again.payload!.questions[0].q]: 'Tea' })
+ expect(result.error, JSON.stringify(result)).toBeUndefined()
+ await ended
+ expect(await d.capture(agent.tmuxPane)).toContain('you chose Tea')
+ client.close()
+ })
+})
diff --git a/cli/src/core/transcripts/attach.spec.ts b/cli/src/core/transcripts/attach.spec.ts
index 2fc368aeb..d8ad9f4c2 100644
--- a/cli/src/core/transcripts/attach.spec.ts
+++ b/cli/src/core/transcripts/attach.spec.ts
@@ -228,6 +228,8 @@ describe('attaching a session', () => {
vi.mocked(run.deps.watcher.tails).mockReturnValue(false)
await run.attach.attachSession(s, true)
expect(run.deps.watcher.addSession).toHaveBeenCalledTimes(1)
+ // The prompt left a turn open, but the held tail had delivered its start already: never again.
+ expect(run.deps.emit).not.toHaveBeenCalledWith(s.sessionId, [expect.objectContaining({ type: 'turn_started' })], { resumed: true })
})
it('keeps the live normalizer when the re-read fails or outlasts its hold', async () => {
diff --git a/cli/src/core/transcripts/attach.ts b/cli/src/core/transcripts/attach.ts
index 694c5b62d..34e0e4777 100644
--- a/cli/src/core/transcripts/attach.ts
+++ b/cli/src/core/transcripts/attach.ts
@@ -381,7 +381,11 @@ export function createAttach({
// A first prompt that lands while this attach is running is already in the transcript we just
// folded, so its turn_started was consumed as history and the live turn would end up untracked.
// Replay that one event, after the attach log, so the recovery is visible in order.
- if (historyTurnOpen) {
+ // Not for a tail this attach took over: everything before the hold was delivered live, the turn's
+ // start included. Claude Code announces its session again when it compacts, often in the middle of
+ // a long turn, and replaying the start showed every window that turn starting twice
+ // (e2e/compaction.e2e.ts).
+ if (historyTurnOpen && !handover.hold) {
const opened = historyEvents.findLast((event) => event.type === 'turn_started')
if (opened) {
console.log(`[agent] ${sid(session.agentId)} resumed the turn already open at attach`)
diff --git a/docs/design/2026-10-03-harnessd.md b/docs/design/2026-10-03-harnessd.md
index 59527bb25..50fbd39c1 100644
--- a/docs/design/2026-10-03-harnessd.md
+++ b/docs/design/2026-10-03-harnessd.md
@@ -588,6 +588,17 @@ Found while mapping, and fixed on their own rather than inside a move:
through, a discovery probe taken before it opened a second agent for the engine it had seen
starting, on a pane that no longer existed. A stop now compares a row that had no process yet
without one. A probe is no longer evidence for a route held or released after it began.
+ - Part 19, found by the eleventh round (`e2e/compaction.e2e.ts`, `/compact` and an automatic
+ compaction mid-turn). Claude Code announces its session again when it compacts, and the daemon
+ re-reads the transcript while taking over its live tail. The re-read found the turn still open
+ and replayed its start, which the tail had already delivered, so every window saw a compacting
+ turn start twice. A re-read that takes over a held tail no longer replays it. The twelfth round
+ (`e2e/questions-edges.e2e.ts`) found nothing to fix. It covered a question asked with no window
+ open (asked once one can answer), one answered by hand in the terminal (every window lets go,
+ and a late answer types nothing), two windows answering at once (one answer lands), Esc, and a
+ question still open across a restart. Kept as it is, for now: a question's id is named from
+ its dialog so that it survives a restart. So a dialog asked again word for word has the same id,
+ and an answer delayed past the first one could land on the repeat.
## Testing