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.

fix(cli): copilot --resume left the device spinning on a finished conversation

0xken153d3cb0800201
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 4dfa8652d..9c6be8edb 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -87,7 +87,7 @@ import { findGrokTranscript } from './engines/grok/session.js'
import { AgyNormalizer, lastAgyTurnText } from './engines/agy/normalizer.js'
import { findAgyTranscript } from './engines/agy/session.js'
import { agyPaneIdle } from './engines/agy/runtimeProfile.js'
-import { CopilotNormalizer, lastCopilotTurnText } from './engines/copilot/normalizer.js'
+import { CopilotNormalizer, copilotHistoryTurnOpen, lastCopilotTurnText } from './engines/copilot/normalizer.js'
import { findCopilotTranscript } from './engines/copilot/session.js'
import { PiNormalizer, lastPiTurnText } from './engines/pi/normalizer.js'
import { HermesReader, readHermesMessages } from './engines/hermes/reader.js'
@@ -1019,10 +1019,17 @@ async function runForeground(token: string): Promise<void> {
historyTurnOpen = false
}
} else if (session.engine === 'copilot') {
- // A JSONL tail like claude/agy. Its turn lifecycle comes from the agentStop hook, not the file.
+ // A JSONL tail like claude/agy. Its turn lifecycle comes from the agentStop hook, not the file —
+ // which is exactly why a fold cannot be trusted on its own: `copilot --resume` replays a finished
+ // conversation, the fold opens a turn on its last `user.message`, and no hook is coming to close
+ // it. Ask the records where the last activity actually ended.
const normalizer = new CopilotNormalizer()
historyTurnOpen = fold((line) => normalizer.ingest(line), () => normalizer.turnOpen)
copilotNormalizers.set(session.sessionId, normalizer)
+ if (historyTurnOpen && !copilotHistoryTurnOpen(lines)) {
+ normalizer.closeTurn()
+ historyTurnOpen = false
+ }
} else if (session.engine === 'pi') {
const normalizer = new PiNormalizer('live')
// Hydrate state silently; never replay history live — except a turn left open, below.
diff --git a/cli/src/engines/README.md b/cli/src/engines/README.md
index f34419181..1d37f431c 100644
--- a/cli/src/engines/README.md
+++ b/cli/src/engines/README.md
@@ -300,6 +300,23 @@ conversation reports the last turn as still open and nothing is ever coming to c
`turn_heartbeat` every second, indefinitely). Its pane knows — `? for shortcuts` when idle,
`esc to cancel` while busy — so attach reads the pane once and closes the folded turn.
+**A fold is not a live stream, and `--resume` is where that bites.** Every engine here whose turn is
+closed by a HOOK has the same hole: attaching to an existing conversation folds its transcript, the
+fold opens a turn on the last user message, and no hook is coming to close it — the tile spins on a
+conversation that finished hours ago. It has now shipped twice, on agy and on Copilot.
+
+Whatever closes a turn LIVE, you also need an answer for "was the last exchange in this FILE
+finished?", and the two are rarely the same source:
+
+- agy's transcript records no end at all, so it asks the PANE (`? for shortcuts` idle vs
+ `esc to cancel` busy) once at attach.
+- Copilot's records `assistant.turn_end` — useless as a live boundary, since it marks a model
+ round-trip and one exchange holds several, but its POSITION settles history exactly:
+ open iff something started after the last one ended (`copilotHistoryTurnOpen`).
+
+Test it by folding a fixture that ENDS, and asserting the fold and the answer disagree. A fixture that
+stops mid-turn will pass a broken implementation.
+
**Two announce events for one turn is a thing that happens.** Copilot fires `userPromptSubmitted` and
then `sessionStart` (measured 2.5s apart, in that order — its sessionStart comes AFTER the first
prompt). Both register, registration is idempotent, and that is fine — but `handleRegistered` treats a
diff --git a/cli/src/engines/copilot/normalizer.spec.ts b/cli/src/engines/copilot/normalizer.spec.ts
index c6b613dd5..370c287f2 100644
--- a/cli/src/engines/copilot/normalizer.spec.ts
+++ b/cli/src/engines/copilot/normalizer.spec.ts
@@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url'
import {
CopilotNormalizer,
copilotEvent,
+ copilotHistoryTurnOpen,
copilotMessagesToEvents,
copilotSessionModel,
copilotToolName,
@@ -108,3 +109,28 @@ describe('copilot tool names', () => {
expect(copilotToolName('some_future_tool')).toBe('Some Future Tool')
})
})
+
+describe('resuming a finished conversation', () => {
+ it('does not report the last turn as still running', () => {
+ // `copilot --resume` folds this whole file at attach. The fold opens a turn on the last
+ // `user.message` and nothing closes it — the agentStop hook only fires for a NEW turn — so the
+ // device sat on "busy loading" for a conversation that had already finished.
+ const normalizer = new CopilotNormalizer()
+ for (const line of LINES) normalizer.ingest(line)
+ expect(normalizer.turnOpen).toBe(true) // what the fold alone believes
+ expect(copilotHistoryTurnOpen(LINES)).toBe(false) // what the records actually say
+ })
+
+ it('still reports a genuinely unfinished exchange as open', () => {
+ const upToFirstPrompt: string[] = []
+ for (const line of LINES) {
+ upToFirstPrompt.push(line)
+ if (copilotEvent(line)?.type === 'user.message') break
+ }
+ expect(copilotHistoryTurnOpen(upToFirstPrompt)).toBe(true)
+ })
+
+ it('treats a session that shut down as closed', () => {
+ expect(copilotHistoryTurnOpen([...LINES, JSON.stringify({ type: 'session.shutdown', data: {} })])).toBe(false)
+ })
+})
diff --git a/cli/src/engines/copilot/normalizer.ts b/cli/src/engines/copilot/normalizer.ts
index af871a012..40001f8c5 100644
--- a/cli/src/engines/copilot/normalizer.ts
+++ b/cli/src/engines/copilot/normalizer.ts
@@ -241,6 +241,34 @@ export function lastCopilotTurnText(lines: string[]): LastTurnText | null {
return assistantText ? { userMessage, assistantText } : null
}
+/**
+ * Is the LAST exchange in a recorded conversation still running?
+ *
+ * The live path never asks this — a turn opens on `user.message` and closes on the `agentStop` hook.
+ * But `--resume` folds an existing file at attach, and a turn opened by that fold has no hook coming
+ * to close it: the device sat on "busy loading" for a conversation that finished hours ago. (agy had
+ * the same failure and had to ask the PANE, because its transcript records no end at all. Copilot's
+ * does, so this is answerable from the file.)
+ *
+ * `assistant.turn_end` marks a model round-trip, not the exchange — but its POSITION still settles the
+ * question: a turn is only unfinished if something started after the last one ended.
+ */
+export function copilotHistoryTurnOpen(lines: string[]): boolean {
+ let lastOpener = -1
+ let lastEnd = -1
+ let shutdown = -1
+ lines.forEach((line, index) => {
+ const event = copilotEvent(line)
+ if (!event) return
+ if (event.type === 'user.message' || event.type === 'assistant.turn_start') lastOpener = index
+ else if (event.type === 'assistant.turn_end') lastEnd = index
+ else if (event.type === 'session.shutdown') shutdown = index
+ })
+ // A shutdown after the last activity ends the conversation whatever came before it.
+ if (shutdown > lastOpener && shutdown > lastEnd) return false
+ return lastOpener > lastEnd
+}
+
/** The model Copilot resolved for this session — `session.model_change`, then each assistant message. */
export function copilotSessionModel(lines: string[]): string | null {
let model = ''