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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
Stop a new Claude session looping on ENGINE_STALE_REPLY (#1434)
0xken15fd476278fb32
diff --git a/cli/src/core/engines/liveSessions.lifecycle.spec.ts b/cli/src/core/engines/liveSessions.lifecycle.spec.tsindex 44640d32c..37703cb89 100644--- a/cli/src/core/engines/liveSessions.lifecycle.spec.ts+++ b/cli/src/core/engines/liveSessions.lifecycle.spec.ts@@ -215,6 +215,38 @@ describe('live session lifecycle', () => {await p.live.stop(); p.live.retry(s)})+ it('reattaches a retry with a copy of the live row, not the snapshot it was scheduled with', async () => {+ // 2026-10-19: a snapshot taken before the runtime learned the CLI version made every retry stale, once a second.+ vi.useFakeTimers()+ const p = setup(), s = p.session()+ p.deps.reattach.mockRejectedValueOnce(new Error('stale'))+ p.rows.set(s.sessionId, { ...s, cliVersion: '2.1.303' })+ p.live.retry({ ...s })+ await vi.advanceTimersByTimeAsync(1_000)+ expect(p.deps.reattach).toHaveBeenLastCalledWith(p.rows.get(s.sessionId))+ expect((p.deps.reattach.mock.lastCall as unknown as unknown[])[0]).not.toBe(p.rows.get(s.sessionId))+ const replacement = { ...s, cliVersion: '2.1.304' }+ p.rows.set(s.sessionId, replacement)+ await vi.advanceTimersByTimeAsync(1_000)+ expect(p.deps.reattach).toHaveBeenCalledTimes(2)+ expect(p.deps.reattach).toHaveBeenLastCalledWith(replacement)+ await vi.advanceTimersByTimeAsync(5_000)+ expect(p.deps.reattach).toHaveBeenCalledTimes(2)+ })++ it('stops retrying when the row changes identity in place while a reattach is pending', async () => {+ vi.useFakeTimers()+ const p = setup(), s = p.session(), pending = deferred<void>()+ p.deps.reattach.mockReturnValueOnce(pending.promise)+ p.live.retry({ ...s })+ await vi.advanceTimersByTimeAsync(1_000)+ expect(p.deps.reattach).toHaveBeenCalledTimes(1)+ s.boundAt = 2+ pending.reject(new Error('stale')); await vi.advanceTimersByTimeAsync(0)+ await vi.advanceTimersByTimeAsync(5_000)+ expect(p.deps.reattach).toHaveBeenCalledTimes(1)+ })+it('does not resurrect a removed binding when a pending read detects a rewrite', async () => {const p = setup(), s = p.session(); await p.attach(s)const pending = deferred<LivePage>()diff --git a/cli/src/core/engines/liveSessions.ts b/cli/src/core/engines/liveSessions.tsindex 595582d3b..24e13e70a 100644--- a/cli/src/core/engines/liveSessions.ts+++ b/cli/src/core/engines/liveSessions.ts@@ -117,8 +117,13 @@ export function createLiveSessions(deps: LiveSessionDeps) {if (stopped || retries.has(id)) returnretries.set(id, setTimeout(() => {retries.delete(id)- if (identity !== transcriptReadIdentity(deps.bySession(id))) return- void deps.reattach(session).catch(error => { report(id, error); retry(session) })+ // Retry with the row as it is now. A snapshot from before the runtime learned the CLI version+ // fails the same staleness check every time (2026-10-19: 7 of 42 new Claude agents, once a second).+ const latest = deps.bySession(id)+ if (!latest || identity !== transcriptReadIdentity(latest)) return+ // A copy: the registry mutates rows in place, and the next retry must compare against this attempt's identity.+ const attempt = { ...latest }+ void deps.reattach(attempt).catch(error => { report(id, error); retry(attempt) })}, 1_000))}function schedule(id: string, delay = 40): void {diff --git a/cli/src/core/engines/runtimeSessions.spec.ts b/cli/src/core/engines/runtimeSessions.spec.tsindex f07cfaea6..560da0e95 100644--- a/cli/src/core/engines/runtimeSessions.spec.ts+++ b/cli/src/core/engines/runtimeSessions.spec.ts@@ -3,10 +3,11 @@ import type { RegisteredSession } from '../../lib/registry.js'import type { RuntimeContext, RuntimeProfile } from '../../engines/facets/runtime.js'import type { RuntimeAnswer, RuntimeOperation } from '../../engines/worker/runtimeProtocol.js'import { blankRuntimeState, encodeRuntimeProfile } from '../../engines/kit/runtime.js'+import { createLiveSessions, type LiveSessions } from './liveSessions.js'import { createRuntimeSessions, type RuntimeSessions } from './runtimeSessions.js'import { EngineReadError } from '../../engines/worker/protocol.js'-const active: RuntimeSessions[] = []+const active: RuntimeSessions[] = [], liveActive: LiveSessions[] = []const deferred = <T>() => { let resolve!: (value: T) => void; const promise = new Promise<T>(r => { resolve = r }); return { promise, resolve } }const session = (id = 'agent'): RegisteredSession => ({ agentId: id, sessionId: `${id}-conversation`, engine: 'claude',model: null, cliVersion: '2.1.209', cwd: '/tmp', transcriptPath: '/tmp/transcript', boundAt: 1 } as RegisteredSession)@@ -41,7 +42,7 @@ function setup() {const seed = () => sessions.read(s, { kind: 'records', records: [{ model: 'old', effort: 'low' }] })return { s, bindings, changed, read, sessions, seed }}-afterEach(() => { active.splice(0).forEach(s => s.stop()); vi.useRealTimers(); vi.restoreAllMocks() })+afterEach(() => { active.splice(0).forEach(s => s.stop()); liveActive.splice(0).forEach(l => void l.stop()); vi.useRealTimers(); vi.restoreAllMocks() })describe('core runtime profile authority', () => {it.each(['binding', 'route', 'control', 'version', 'forget', 'unchanged'] as const)(@@ -135,6 +136,73 @@ describe('core runtime profile authority', () => {expect(t.sessions.getState(t.s.sessionId).model).not.toBe('late')})+ it('stages from the version the live row holds, and still refuses a different identity or a change mid-stage', async () => {+ // 2026-10-19: a hook attach copied the row before discovery's attach taught it the CLI version.+ const t = setup(); t.s.cliVersion = null+ const copy = { ...t.s }+ t.s.cliVersion = '2.1.303'+ const staged = t.sessions.stage(copy, false, true)+ await staged.ingest([{ model: 'm', effort: 'low' }])+ expect(t.read.mock.calls[0]![1].session.cliVersion).toBe('2.1.303')+ expect(staged.commit()).toBe(true)+ expect(t.s.cliVersion).toBe('2.1.303')+ const mid = t.sessions.stage({ ...copy }, false, true)+ t.s.cliVersion = '2.1.304'+ await expect(mid.ingest([{ model: 'n' }])).rejects.toThrow('ENGINE_STALE_REPLY')+ expect(() => t.sessions.stage({ ...copy, sessionId: 'other' }, false, true)).toThrow('ENGINE_STALE_REPLY')+ expect(() => t.sessions.stage({ ...copy, agentId: 'other' }, false, true)).toThrow('ENGINE_STALE_REPLY')+ expect(() => t.sessions.stage({ ...copy, boundAt: 2 }, false, true)).toThrow('ENGINE_STALE_REPLY')+ })++ it('accepts a copy holding an older non-null version', async () => {+ const t = setup(); t.s.cliVersion = '2.1.303'+ const staged = t.sessions.stage({ ...t.s, cliVersion: '2.1.300' }, false, true)+ await staged.ingest([{ model: 'm', effort: 'low' }])+ expect(t.read.mock.calls[0]![1].session.cliVersion).toBe('2.1.303')+ expect(staged.commit()).toBe(true)+ })++ describe('with live sessions retrying through them', () => {+ // The 2026-10-19 race: the hook attach copies the row, discovery's attach learns the version, the copy stages.+ const wire = () => {+ const t = setup(); t.s.cliVersion = null+ const attempts: RegisteredSession[] = []+ const commits: boolean[] = []+ const live = createLiveSessions({ handles: () => true, bySession: id => [...t.bindings.values()].find(b => b.sessionId === id),+ transport: { pull: vi.fn(), forget: vi.fn(async () => {}), close: vi.fn(), connected: vi.fn(), disconnected: vi.fn() },+ frame: vi.fn(), watch: vi.fn(), unwatch: vi.fn(), log: vi.fn(),+ reattach: async copy => {+ attempts.push(copy)+ const staged = t.sessions.stage(copy, false, true)+ await staged.ingest([{ model: 'm', effort: 'low' }])+ commits.push(staged.commit())+ } })+ liveActive.push(live)+ return { ...t, live, attempts, commits }+ }+ it('(a) a copy taken before the version is learned stages on the first try', async () => {+ vi.useFakeTimers()+ const t = wire(), copy = { ...t.s }+ t.s.cliVersion = '2.1.303'+ await t.sessions.stage(copy, false, true).ingest([{ model: 'm' }])+ expect(t.attempts).toHaveLength(0)+ })+ it('(b) a stage that throws mid-way is retried once with the live row, then commits', async () => {+ vi.useFakeTimers()+ const t = wire(), copy = { ...t.s }+ const mid = t.sessions.stage(copy, false, true)+ t.s.cliVersion = '2.1.303'+ await expect(mid.ingest([{ model: 'n' }])).rejects.toThrow('ENGINE_STALE_REPLY')+ t.live.retry(copy)+ await vi.advanceTimersByTimeAsync(1_000)+ expect(t.attempts).toHaveLength(1)+ expect(t.attempts[0]!.cliVersion).toBe('2.1.303')+ expect(t.commits).toEqual([true])+ await vi.advanceTimersByTimeAsync(5_000)+ expect(t.attempts).toHaveLength(1)+ })+ })+it('does not let an old binding evict a newer view and fences staged configuration', async () => {const t = setup(); await t.seed()const newer = { ...t.s, sessionId: 'new-conversation', boundAt: 2 }diff --git a/cli/src/core/engines/runtimeSessions.ts b/cli/src/core/engines/runtimeSessions.tsindex 863eb5436..241c845a1 100644--- a/cli/src/core/engines/runtimeSessions.ts+++ b/cli/src/core/engines/runtimeSessions.ts@@ -142,7 +142,11 @@ export function createRuntimeSessions(deps: RuntimeSessionDeps) {}const stage = (s: RegisteredSession, hydrate: boolean, silent: boolean): RuntimeStage => {- const view = viewFor(s), revision = view.revision, version = s.cliVersion+ const view = viewFor(s), revision = view.revision+ // viewFor has just matched the live row's identity, so its version is the one to guard. A copy taken+ // before an accepted read learned the version (2026-10-19: 7 of 42 new Claude agents) is only older.+ s = { ...s, cliVersion: deps.resolve(s.agentId)!.cliVersion }+ const version = s.cliVersionconst context = contextFor(s, view)if (hydrate) context.state = { ...blankRuntimeState(), model: typeof s.model === 'string' ? s.model : null, cliVersion: s.cliVersion }let answer: RuntimeAnswer | null = null@@ -195,7 +199,7 @@ export function createRuntimeSessions(deps: RuntimeSessionDeps) {read,async capturePane(s: RegisteredSession, capture: (id: string, historyLines?: number) => Promise<string | null>,historyLines?: number, silent = false): Promise<string | null> {- const view = viewFor(s), revision = view.revision, version = s.cliVersion+ const view = viewFor(s), revision = view.revision, version = deps.resolve(s.agentId)!.cliVersionconst current = () => valid(view) && view.revision === revision && deps.resolve(view.agentId)?.cliVersion === versionconst text = await capture(s.agentId, historyLines)if (!text || !current()) return null