Skip to content

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.

feat(push): a finished turn tells the phone WHY it ended

enesa695c068b9e2
diff --git a/src/core/push-notify.test.ts b/src/core/push-notify.test.ts
index 594a96005..9e40201e1 100644
--- a/src/core/push-notify.test.ts
+++ b/src/core/push-notify.test.ts
@@ -16,6 +16,7 @@ import {
onNodeNowChange,
recordAgentEvent,
recordRawToolEvent,
+ sweepStaleWorking,
initAgentStatusMirror,
_resetForTest,
type InboxEvent,
@@ -1088,6 +1089,137 @@ describe('createLiveUpdatePush', () => {
})
})
+ // The mirror sends message:'Stopped' for BOTH an interrupt and the stale sweep, and 'Finished'
+ // for a real completion — so the phone was deciding WHY a turn ended from a display STRING, and
+ // could not tell "you stopped it" from "we lost the host" at all. These two flags are the
+ // structured answer; they ride done edges only.
+ describe('done-edge end reason (interrupted / stale)', () => {
+ it('forwards interrupted:true on an interrupted done edge', async () => {
+ const { h, st } = wire()
+ st.emit({ nodeId: 'a', event: 'end', state: 'done', message: 'Stopped', interrupted: true, ts: 1 })
+ await vi.advanceTimersByTimeAsync(1000)
+ const u = liveBodyOf().updates[0]
+ expect(u.interrupted).toBe(true)
+ expect(u).not.toHaveProperty('stale')
+ h.stop()
+ })
+
+ it('forwards stale:true on a sweep-produced done edge', async () => {
+ const { h, st } = wire()
+ st.emit({ nodeId: 'a', event: 'end', state: 'done', message: 'Stopped', stale: true, ts: 1 })
+ await vi.advanceTimersByTimeAsync(1000)
+ const u = liveBodyOf().updates[0]
+ expect(u.stale).toBe(true)
+ expect(u).not.toHaveProperty('interrupted')
+ h.stop()
+ })
+
+ it('omits both on a real completion (a plain done edge)', async () => {
+ const { h, st } = wire()
+ st.emit({ nodeId: 'a', event: 'end', state: 'done', message: 'Finished', ts: 1 })
+ await vi.advanceTimersByTimeAsync(1000)
+ const u = liveBodyOf().updates[0]
+ expect(u).not.toHaveProperty('interrupted')
+ expect(u).not.toHaveProperty('stale')
+ h.stop()
+ })
+
+ it('never carries either on a working/needsYou edge even if the change object has them', async () => {
+ const { h, st } = wire()
+ // Belt-and-braces: only a done edge may carry an end reason.
+ st.emit({ nodeId: 'a', event: 'start', state: 'working', interrupted: true, stale: true, ts: 1 } as NodeStateChange)
+ st.emit({
+ nodeId: 'b',
+ event: 'update',
+ state: 'needsYou',
+ message: 'Approve',
+ interrupted: true,
+ stale: true,
+ ts: 2
+ } as NodeStateChange)
+ await vi.advanceTimersByTimeAsync(1000)
+ const updates = liveBodyOf().updates
+ expect(updates).toHaveLength(2)
+ for (const u of updates) {
+ expect(u).not.toHaveProperty('interrupted')
+ expect(u).not.toHaveProperty('stale')
+ }
+ h.stop()
+ })
+
+ it('now-update (activity/context) ticks never carry an end reason', async () => {
+ const { h, nw } = wire()
+ clock = 0
+ nw.emit({ nodeId: 'a', activity: 'Editing a.ts', contextPercent: 10, ts: 0 })
+ await vi.advanceTimersByTimeAsync(1000)
+ const u = liveBodyOf().updates[0]
+ expect(u).not.toHaveProperty('interrupted')
+ expect(u).not.toHaveProperty('stale')
+ h.stop()
+ })
+
+ describe('via the real mirror', () => {
+ let dir: string
+ beforeEach(() => {
+ _resetForTest()
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'push-endreason-'))
+ initAgentStatusMirror(path.join(dir, 'agent-status.json'))
+ })
+ afterEach(() => {
+ _resetForTest()
+ fs.rmSync(dir, { recursive: true, force: true })
+ })
+
+ function ev(p: Partial<NormalizedAgentEvent>): NormalizedAgentEvent {
+ return { nodeId: 'n1', agentId: 'claude', kind: 'state', ...p } as NormalizedAgentEvent
+ }
+
+ it("an interrupted turn rides as interrupted:true — and its message is the sweep's own 'Stopped'", async () => {
+ const h = createLiveUpdatePush(
+ liveDeps({ subscribeStateChange: onNodeStateChange, subscribeNowChange: onNodeNowChange })
+ )
+ recordAgentEvent(ev({ state: 'working', newTurn: true }))
+ recordAgentEvent(ev({ state: 'done', interrupted: true }))
+ await vi.advanceTimersByTimeAsync(1000)
+ const end = liveBodyOf().updates.find((u: Record<string, unknown>) => u.event === 'end')!
+ expect(end.interrupted).toBe(true)
+ expect(end).not.toHaveProperty('stale')
+ // The very collision this feature exists for: the string is the SAME as the sweep's.
+ expect(end.message).toBe('Stopped')
+ h.stop()
+ })
+
+ it('a sweep-presumed-gone session rides as stale:true under that same message', async () => {
+ const h = createLiveUpdatePush(
+ liveDeps({ subscribeStateChange: onNodeStateChange, subscribeNowChange: onNodeNowChange })
+ )
+ recordAgentEvent(ev({ state: 'working', newTurn: true }))
+ // Nobody heard from it for the stale window → the sweep fires the synthetic end.
+ expect(sweepStaleWorking(Date.now() + 60 * 60_000, 20 * 60_000)).toEqual(['n1'])
+ await vi.advanceTimersByTimeAsync(1000)
+ const end = liveBodyOf().updates.find((u: Record<string, unknown>) => u.event === 'end')!
+ expect(end.stale).toBe(true)
+ expect(end).not.toHaveProperty('interrupted')
+ expect(end.message).toBe('Stopped')
+ h.stop()
+ })
+
+ it('a real completion carries neither flag', async () => {
+ const h = createLiveUpdatePush(
+ liveDeps({ subscribeStateChange: onNodeStateChange, subscribeNowChange: onNodeNowChange })
+ )
+ recordAgentEvent(ev({ state: 'working', newTurn: true }))
+ recordAgentEvent(ev({ state: 'done', lastMessage: 'Done' }))
+ await vi.advanceTimersByTimeAsync(1000)
+ const end = liveBodyOf().updates.find((u: Record<string, unknown>) => u.event === 'end')!
+ expect(end.message).toBe('Finished')
+ expect(end).not.toHaveProperty('interrupted')
+ expect(end).not.toHaveProperty('stale')
+ h.stop()
+ })
+ })
+ })
+
describe('state-edge marking (edge flag)', () => {
it('marks every state edge (start/needsYou/end) with edge:true', async () => {
const { h, st } = wire()
diff --git a/src/core/push-notify.ts b/src/core/push-notify.ts
index 181a6f4e7..b6fcfc4a3 100644
--- a/src/core/push-notify.ts
+++ b/src/core/push-notify.ts
@@ -378,6 +378,19 @@ export interface LiveUpdateItem {
multiSelect?: boolean
/** approval needsYou only: the deterministic hook-reply ticket, letting an intent answer. */
pendingId?: string
+ /** done only: the turn ended because the user INTERRUPTED it (Esc/Ctrl-C), not because it
+ * finished. Omitted when false/absent, and never sent off a done edge — the backend also forces
+ * it null on non-done content-state. Without it the phone had only the `message` STRING
+ * ('Stopped' vs 'Finished') to tell the two apart, so a wording change silently altered
+ * behaviour. A consumer that CELEBRATES a completion must skip this (same rule as the notch
+ * HUD's `doneSeen`) — nothing was accomplished, so there is nothing to go and read. */
+ interrupted?: boolean
+ /** done only: this end came from the stale-working SWEEP (nobody heard from the session for
+ * WORKING_STALE_MS, so it is presumed gone), NOT from the session itself. Same omission rules and
+ * the same "never celebrate it" contract as `interrupted` — but a DIFFERENT fact: `interrupted`
+ * is "you stopped it", `stale` is "we lost the host". The mirror sends message:'Stopped' for
+ * both, which is exactly why the phone cannot infer this from the text. */
+ stale?: boolean
/** true on a state EDGE (start / needsYou / end, and the working update that follows an answered
* needs-you) — a user-visible state change. Absent on the ≥20s activity/context coalesced ticks.
* The backend uses it for APNs priority: an edge is priority 10, a tick priority 5 (iOS delays
@@ -504,6 +517,12 @@ export function createLiveUpdatePush(deps: LiveUpdateDeps): LiveUpdateHandle {
// interactive-push-live-activities addendum) — belt-and-braces gate on state so a working/done
// edge never carries them (the backend also nulls them on non-needsYou content-state).
const needsYou = c.state === 'needsYou'
+ // interrupted/stale are the WHY of an end edge, and they ride done edges only — same
+ // belt-and-braces gate as the needsYou block above (the backend nulls them off done too).
+ // They are forwarded because 'Stopped' is the mirror's message for BOTH the interrupt and the
+ // stale sweep: without these flags the phone cannot tell "you stopped it" from "we lost the
+ // host", and both are indistinguishable from a real completion except by that string.
+ const done = c.state === 'done'
buffer.push({
nodeId: c.nodeId,
...(title ? { nodeTitle: title } : {}),
@@ -522,6 +541,8 @@ export function createLiveUpdatePush(deps: LiveUpdateDeps): LiveUpdateHandle {
...(needsYou && c.options && c.options.length > 0 ? { options: c.options } : {}),
...(needsYou && c.multiSelect ? { multiSelect: true } : {}),
...(needsYou && c.pendingId ? { pendingId: c.pendingId } : {}),
+ ...(done && c.interrupted ? { interrupted: true } : {}),
+ ...(done && c.stale ? { stale: true } : {}),
ts: c.ts
})
scheduleFlush()