From 46bae9b0f1e519b46f5bbbb37883c6016062c7d5 Mon Sep 17 00:00:00 2001 From: Ananovo Date: Sun, 1 Nov 2026 09:55:01 +0800 Subject: [PATCH] fix(codex): fork archived sessions without reopening the source (#2231) * fix(codex): fork archived sessions without reopening the source * fix(codex): reconcile interrupted archived fork recovery * fix(codex): release rejected forks and serialize source startup * fix(codex): fence unconfirmed resume startup across retries * test(codex): validate durable resume recovery and serialize archive * fix(codex): block local resume of pending archived fork sources * fix(hub): coalesce concurrent resume requests to preserve idempotent startup * fix(hub): bypass startup coalescing for PTY sessions --- cli/src/agent/sessionFactory.test.ts | 13 + cli/src/agent/sessionFactory.ts | 5 + cli/src/api/apiMachine.ts | 6 + cli/src/codex/codexAppServerClient.test.ts | 30 ++ cli/src/codex/codexAppServerClient.ts | 15 +- cli/src/codex/shared/archivedFork.test.ts | 162 ++++++++ cli/src/codex/shared/archivedFork.ts | 163 ++++++++ cli/src/codex/shared/registry.ts | 30 +- cli/src/codex/shared/root.test.ts | 28 +- cli/src/codex/shared/root.ts | 6 + docs/guide/codex-shared-sessions.md | 19 + hub/src/store/sessions.ts | 2 + hub/src/sync/archivedFork.test.ts | 381 ++++++++++++++++++ hub/src/sync/rpcGateway.ts | 7 + hub/src/sync/sessionModel.test.ts | 11 +- hub/src/sync/syncEngine.ts | 277 ++++++++++++- hub/src/web/routes/sessions.test.ts | 16 + hub/src/web/routes/sessions.ts | 2 +- shared/src/apiTypes.ts | 11 + shared/src/rpcMethods.ts | 1 + shared/src/runnerCapabilities.ts | 2 + shared/src/schemas.ts | 8 + web/e2e/codex-archived-fork.spec.ts | 163 ++++++++ web/playwright.live.config.ts | 8 +- .../components/AssistantChat/HappyThread.tsx | 2 + web/src/components/AssistantChat/context.tsx | 1 + .../messages/AssistantMessage.tsx | 4 +- .../messages/UserMessage.test.tsx | 25 +- .../AssistantChat/messages/UserMessage.tsx | 9 +- web/src/components/SessionChat.tsx | 4 +- 30 files changed, 1377 insertions(+), 34 deletions(-) create mode 100644 cli/src/codex/shared/archivedFork.test.ts create mode 100644 cli/src/codex/shared/archivedFork.ts create mode 100644 hub/src/sync/archivedFork.test.ts create mode 100644 web/e2e/codex-archived-fork.spec.ts diff --git a/cli/src/agent/sessionFactory.test.ts b/cli/src/agent/sessionFactory.test.ts index bc105e87..d39e396b 100644 --- a/cli/src/agent/sessionFactory.test.ts +++ b/cli/src/agent/sessionFactory.test.ts @@ -110,6 +110,19 @@ describe('bootstrapExistingSession', () => { delete process.env[HAPI_SESSION_ID_ENV] }) + it('refuses local Codex resume of a CLI-archived source with a pending fork before creating a socket or changing metadata', async () => { + const session = createSession() + session.metadata = { ...session.metadata!, lifecycleState: 'archived', archivedBy: 'cli', + codexArchivedFork: { operationId: '11111111-1111-4111-8111-111111111111', childSessionId: '22222222-2222-4222-8222-222222222222' } } + getSessionMock.mockResolvedValue(session) + getOrCreateMachineMock.mockResolvedValue({ id: 'machine-1' }) + readSettingsMock.mockResolvedValue({ machineId: 'machine-1' }) + await expect(bootstrapExistingSession({ sessionId: session.id, flavor: 'codex', workingDirectory: '/tmp/project' })) + .rejects.toThrow('pending archived fork') + expect(sessionSyncClientMock).not.toHaveBeenCalled() + expect(notifyRunnerSessionStartedMock).not.toHaveBeenCalled() + }) + it('does not wait for metadata when an existing session has no stale history capabilities', async () => { const session = createSession() const sessionClient = { diff --git a/cli/src/agent/sessionFactory.ts b/cli/src/agent/sessionFactory.ts index 2ce2e8c9..87b719f2 100644 --- a/cli/src/agent/sessionFactory.ts +++ b/cli/src/agent/sessionFactory.ts @@ -398,6 +398,11 @@ export async function bootstrapExistingSession(options: { }) const sessionInfo = await api.getSession(options.sessionId) + // Shared Codex cold-resume calls bootstrap while holding native ownership. + // The source reservation outlives the native fork through child startup. + if (options.flavor === 'codex' && sessionInfo.metadata?.codexArchivedFork) { + throw new Error(`HAPI session ${options.sessionId} has a pending archived fork; retry that fork before resuming the source`) + } // #1911 M1 belt: match hub merge-preserve scope — only hub-authored archive. // CLI self-archive (archivedBy=cli) must still reopen; hub clears before spawn // for intentional revive. CAS closed hub-side (success+preserve + ack EXIT). diff --git a/cli/src/api/apiMachine.ts b/cli/src/api/apiMachine.ts index bcd64e48..b1e38dbf 100644 --- a/cli/src/api/apiMachine.ts +++ b/cli/src/api/apiMachine.ts @@ -6,6 +6,7 @@ import { io, type Socket } from 'socket.io-client' import { readdir, stat } from 'node:fs/promises' import { join } from 'node:path' import { logger } from '@/ui/logger' +import { ForkArchivedCodexRequestSchema } from '@hapi/protocol/apiTypes' import { configuration } from '@/configuration' import type { ClientToServerEvents, ServerToClientEvents, Update, UpdateMachineBody } from '@hapi/protocol' import { @@ -456,6 +457,11 @@ export class ApiMachineClient { } setRPCHandlers({ spawnSession, stopSession, requestShutdown }: MachineRpcHandlers): void { + this.rpcHandlerManager.registerHandler(RPC_METHODS.ForkArchivedCodex, async (raw: unknown) => { + const request = ForkArchivedCodexRequestSchema.parse(raw) + const { forkArchivedCodex } = await import('@/codex/shared/archivedFork') + return forkArchivedCodex(request, () => this.pathPolicy.allowsSpawn(request.directory)) + }) this.rpcHandlerManager.registerHandler(RPC_METHODS.SpawnHappySession, async (params: any) => { const { directory, sessionId, existingSessionId, reservedSessionId, resumeSessionId, machineId, approvedNewDirectoryCreation, agent, model, effort, modelReasoningEffort, yolo, permissionMode, serviceTier, collaborationMode, copilotAgentMode, token, sessionType, worktreeName, startingMode, forkSession } = params || {} diff --git a/cli/src/codex/codexAppServerClient.test.ts b/cli/src/codex/codexAppServerClient.test.ts index b7df81ba..e2f3488c 100644 --- a/cli/src/codex/codexAppServerClient.test.ts +++ b/cli/src/codex/codexAppServerClient.test.ts @@ -29,6 +29,7 @@ vi.mock('@/ui/logger', () => ({ })); import { CodexAppServerClient, isIndeterminateError } from './codexAppServerClient'; +import { killProcessByChildProcess } from '@/utils/process'; function fakeStream(): EventEmitter & { setEncoding: ReturnType } { return Object.assign(new EventEmitter(), { setEncoding: vi.fn() }); @@ -36,6 +37,8 @@ function fakeStream(): EventEmitter & { setEncoding: ReturnType } function fakeChild() { return Object.assign(new EventEmitter(), { + exitCode: null as number | null, + signalCode: null as string | null, stdin: Object.assign(new EventEmitter(), { end: vi.fn(), write: vi.fn(), @@ -60,6 +63,33 @@ describe('CodexAppServerClient process cwd', () => { beforeEach(() => { execFileSyncMock.mockClear(); spawnMock.mockReset(); + vi.mocked(killProcessByChildProcess).mockClear(); + }); + + it('lets a completed read/fork helper exit on stdin EOF without process-tree probing', async () => { + const child = fakeChild(); + child.stdin.end.mockImplementation(() => { child.exitCode = 0; child.emit('exit', 0, null); }); + spawnMock.mockReturnValue(child); + const client = new CodexAppServerClient(); + await client.connect(); + await client.disconnect({ gracefulTimeoutMs: 100 }); + expect(child.stdin.end).toHaveBeenCalledTimes(1); + expect(killProcessByChildProcess).not.toHaveBeenCalled(); + expect(client.isConnected()).toBe(false); + }); + + it('falls back to exact child cleanup if graceful helper shutdown does not finish', async () => { + vi.useFakeTimers(); + try { + const child = fakeChild(); + spawnMock.mockReturnValue(child); + const client = new CodexAppServerClient(); + await client.connect(); + const closing = client.disconnect({ gracefulTimeoutMs: 100 }); + await vi.advanceTimersByTimeAsync(100); + await closing; + expect(killProcessByChildProcess).toHaveBeenCalledWith(child); + } finally { vi.useRealTimers(); } }); it('passes an explicit neutral cwd to the app-server process', async () => { diff --git a/cli/src/codex/codexAppServerClient.ts b/cli/src/codex/codexAppServerClient.ts index 4a8a83fd..21e5cb8a 100644 --- a/cli/src/codex/codexAppServerClient.ts +++ b/cli/src/codex/codexAppServerClient.ts @@ -519,7 +519,7 @@ export class CodexAppServerClient extends JsonLineParser { return response as ThreadGoalClearResponse; } - async disconnect(): Promise { + async disconnect(options?: { gracefulTimeoutMs?: number }): Promise { if (this.socket) { const socket = this.socket; this.socket = null; @@ -538,7 +538,18 @@ export class CodexAppServerClient extends JsonLineParser { try { child?.stdin.end(); - if (child) { + if (child && options?.gracefulTimeoutMs && child.exitCode === null && child.signalCode === null) { + // Read/fork helpers have no active turns after archiving their + // temporary child. EOF normally exits the native server; avoid + // an expensive Windows process-tree probe in the Runner loop. + await new Promise(resolve => { + const finish = () => { clearTimeout(timer); child.off('exit', finish); child.off('error', finish); resolve(); }; + const timer = setTimeout(finish, options.gracefulTimeoutMs); + child.once('exit', finish); child.once('error', finish); + if (child.exitCode !== null || child.signalCode !== null) finish(); + }); + } + if (child && child.exitCode === null && child.signalCode === null) { await killProcessByChildProcess(child); } } catch (error) { diff --git a/cli/src/codex/shared/archivedFork.test.ts b/cli/src/codex/shared/archivedFork.test.ts new file mode 100644 index 00000000..70d75b33 --- /dev/null +++ b/cli/src/codex/shared/archivedFork.test.ts @@ -0,0 +1,162 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { mkdtemp, mkdir, writeFile, readFile, rm } from 'node:fs/promises' +import { join } from 'node:path' +import { tmpdir } from 'node:os' +import { ApiClient } from '@/api/api' +import { CodexAppServerClient } from '../codexAppServerClient' +import { codexHome } from './registry' +import { forkArchivedCodex, performArchivedFork, type ArchivedForkJournal } from './archivedFork' +import type { ForkArchivedCodexRequest } from '@hapi/protocol/apiTypes' + +vi.mock('@/utils/process', async importOriginal => ({ + ...await importOriginal(), + // Ownership must conservatively retain a live PID with an unknown marker; + // the OS-specific marker probe is covered separately from this journal test. + getProcessStartMarker: () => undefined, + isProcessAlive: (pid: number) => pid === process.pid +})) + +function fixture() { + const request: ForkArchivedCodexRequest = { operationId: 'operation', sourceSessionId: 'hapi-source', sourceThreadId: 'source', directory: '/work', + boundary: { localId: 'second', turnId: 'turn-2' } } + const journal: ArchivedForkJournal = { request: structuredClone(request), restoreRequired: false, forkIssued: false, done: false } + let archived = true + const source = { id: 'source', cwd: '/work', status: { type: 'notLoaded' }, turns: [ + { id: 'turn-1', status: 'completed', items: [{ type: 'userMessage', clientId: 'first' }] }, + { id: 'turn-2', status: 'interrupted', items: [{ type: 'userMessage', clientId: 'second' }] } + ] } + const calls: string[] = [] + const native = vi.fn(async (method: string, params?: unknown): Promise => { + const p = params as Record + calls.push(`${method}:${p?.threadId ?? ''}`) + if (method === 'thread/read') return { thread: source } + if (method === 'thread/loaded/list') { expect(params).toEqual({}); return { data: [] } } + if (method === 'thread/unarchive') { archived = false; return {} } + if (method === 'thread/archive') { if (p.threadId === 'source') archived = true; return {} } + if (method === 'thread/fork') { + if (archived) throw new Error('session source is archived. Run `codex unarchive source` to unarchive it first.') + return { thread: { id: 'child', forkedFromId: 'source', parentThreadId: null } } + } + throw new Error(`Unexpected method ${method}`) + }) + const client = { request: native } as unknown as Parameters[0] + const snapshots: ArchivedForkJournal[] = [] + const save = vi.fn(async () => { snapshots.push(structuredClone(journal)) }) + const inactive = vi.fn(async () => {}) + return { request, journal, client, native, source, calls, save, snapshots, inactive, archived: () => archived, + run: () => performArchivedFork(client, request, journal, save, inactive) } +} + +describe('archived Codex native fork', () => { + it('unarchives, forks at the exact boundary, restores source, and releases the child writer', async () => { + const f = fixture() + await expect(f.run()).resolves.toBe('child') + expect(f.archived()).toBe(true) + expect(f.calls).toEqual(['thread/read:source', 'thread/read:source', 'thread/fork:source', 'thread/unarchive:source', + 'thread/fork:source', 'thread/loaded/list:', 'thread/archive:source', 'thread/archive:child']) + expect(f.native).toHaveBeenCalledWith('thread/fork', { threadId: 'source', beforeTurnId: 'turn-2', deferGoalContinuation: true, excludeTurns: true }) + expect(f.snapshots.some(snapshot => snapshot.restoreRequired && !snapshot.forkIssued)).toBe(true) + expect(f.journal).toMatchObject({ nativeSessionId: 'child', restoreRequired: false, done: true }) + const count = f.native.mock.calls.length + await expect(f.run()).resolves.toBe('child') + expect(f.native).toHaveBeenCalledTimes(count) + }) + + it('restores a source even when the second fork fails; never replays an unconfirmed mutation', async () => { + const f = fixture() + const original = f.native.getMockImplementation()! + f.native.mockImplementation(async (method, params) => { + if (method === 'thread/fork' && !f.archived()) throw new Error('transport lost') + return original(method, params) + }) + await expect(f.run()).rejects.toThrow('transport lost') + expect(f.archived()).toBe(true) + await expect(f.run()).rejects.toThrow('unconfirmed') + expect(f.native.mock.calls.filter(([method]) => method === 'thread/fork')).toHaveLength(2) + }) + + it('reconciles a persisted restore intent and known child without another fork', async () => { + const f = fixture() + Object.assign(f.journal, { restoreRequired: true, forkIssued: true, nativeSessionId: 'child' }) + await expect(f.run()).resolves.toBe('child') + expect(f.calls).toEqual(['thread/loaded/list:', 'thread/archive:source', 'thread/archive:child']) + }) + + it('retains recovery intent when restoring the source fails', async () => { + const f = fixture() + const original = f.native.getMockImplementation()! + f.native.mockImplementation(async (method, params) => { + if (method === 'thread/archive' && (params as { threadId: string }).threadId === 'source') throw new Error('archive unavailable') + return original(method, params) + }) + await expect(f.run()).rejects.toThrow('archive unavailable') + expect(f.journal).toMatchObject({ restoreRequired: true, nativeSessionId: 'child', done: false }) + f.native.mockImplementation(original) + await expect(f.run()).resolves.toBe('child') + expect(f.native.mock.calls.filter(([method]) => method === 'thread/fork')).toHaveLength(2) + }) + + it.each(['loaded', 'boundary', 'cwd', 'identity', 'unfinished', 'steered'])('rejects an unsafe %s source before native mutation', async kind => { + const f = fixture() + if (kind === 'loaded') f.source.status.type = 'idle' + if (kind === 'boundary') f.request.boundary!.turnId = 'missing' + if (kind === 'cwd') f.source.cwd = '/another' + if (kind === 'identity') f.source.id = 'another' + if (kind === 'unfinished') f.source.turns[1].status = 'inProgress' + if (kind === 'steered') f.request.boundary!.localId = 'not-first' + f.journal.request = structuredClone(f.request) + await expect(f.run()).rejects.toThrow() + expect(f.native.mock.calls.some(([method]) => ['thread/fork', 'thread/unarchive'].includes(method))).toBe(false) + }) + + it('refuses to restore over a newly loaded source', async () => { + const f = fixture() + f.journal.restoreRequired = true + f.native.mockResolvedValue({ data: ['source'] }) + await expect(f.run()).rejects.toThrow('potentially active') + expect(f.journal.restoreRequired).toBe(true) + expect(f.calls).toEqual([]) + }) + + it('stops before any native call when HAPI source was reopened', async () => { + const f = fixture() + f.inactive.mockRejectedValue(new Error('Source session changed')) + await expect(f.run()).rejects.toThrow('Source session changed') + expect(f.native).not.toHaveBeenCalled() + }) +}) + +describe('archived fork workspace preflight', () => { + afterEach(() => { vi.restoreAllMocks(); vi.unstubAllEnvs() }) + + it.each(['fresh', 'restore', 'unconfirmed', 'known-child'].flatMap(kind => ['policy', 'ownership'].map(preflight => ({ kind, preflight }))))('rejects $preflight preflight for a $kind operation and preserves recovery intent', async ({ kind, preflight }) => { + const home = await mkdtemp(join(tmpdir(), 'hapi-archived-preflight-')) + vi.stubEnv('CODEX_HOME', home) + vi.spyOn(ApiClient, 'create').mockResolvedValue({} as ApiClient) + const connect = vi.spyOn(CodexAppServerClient.prototype, 'connect').mockRejectedValue(new Error('unexpected native startup')) + const request = fixture().request + const target = join(home, 'hapi-archived-forks', `${request.operationId}.json`) + const journal: ArchivedForkJournal = { request, restoreRequired: kind === 'restore', + forkIssued: kind === 'unconfirmed' || kind === 'known-child', done: false, + ...(kind === 'known-child' ? { nativeSessionId: 'child' } : {}) } + try { + if (preflight === 'ownership') { + const owners = join(home, 'hapi-runtime-owners') + await mkdir(owners, { recursive: true }) + await writeFile(join(owners, 'live.json'), JSON.stringify({ id: 'other', pid: process.pid, marker: '', + endpoint: 'mock', command: 'codex', args: [], codexHome: codexHome(), hub: 'hub', authHash: 'hash', + sessions: { source: { threadId: request.sourceThreadId, namespace: 'ns', active: true } } })) + } + if (kind !== 'fresh') { + await mkdir(join(home, 'hapi-archived-forks'), { recursive: true }) + await writeFile(target, JSON.stringify(journal)) + } + expect(await forkArchivedCodex(request, async () => preflight !== 'policy')).toEqual({ + error: preflight === 'policy' ? 'Directory is outside workspace roots' + : 'Thread belongs to another Codex runtime. Use hapi resume source.', rollbackSafe: kind === 'fresh' + }) + expect(connect).not.toHaveBeenCalled() + if (kind !== 'fresh') expect(JSON.parse(await readFile(target, 'utf8'))).toEqual(journal) + } finally { await rm(home, { recursive: true, force: true }) } + }) +}) diff --git a/cli/src/codex/shared/archivedFork.ts b/cli/src/codex/shared/archivedFork.ts new file mode 100644 index 00000000..2c8634c4 --- /dev/null +++ b/cli/src/codex/shared/archivedFork.ts @@ -0,0 +1,163 @@ +import { mkdir, readFile, rename, writeFile } from 'node:fs/promises' +import { join, resolve } from 'node:path' +import { randomUUID } from 'node:crypto' +import { z } from 'zod' +import type { ForkArchivedCodexRequest, ForkArchivedCodexResponse } from '@hapi/protocol/apiTypes' +import { ApiClient } from '@/api/api' +import { CodexAppServerClient } from '../codexAppServerClient' +import { initializeSharedClient } from './launch' +import { codexHome, withThreadOwnership } from './registry' +import { record, string } from './gateway' + +const JournalSchema = z.object({ + request: z.object({ operationId: z.string(), sourceSessionId: z.string(), sourceThreadId: z.string(), directory: z.string(), + boundary: z.object({ localId: z.string(), turnId: z.string() }).optional() }), + restoreRequired: z.boolean(), forkIssued: z.boolean(), nativeSessionId: z.string().optional(), done: z.boolean() +}) +export type ArchivedForkJournal = z.infer +type NativeClient = Pick + +async function readHistory(client: NativeClient, threadId: string): Promise> { + let thread = record(record(await client.request('thread/read', { threadId, includeTurns: false })).thread) + if (thread.historyMode === 'paginated') { + const turns: unknown[] = [] + let cursor: string | undefined + do { + const page = record(await client.request('thread/turns/list', { threadId, cursor, sortDirection: 'asc', itemsView: 'full' })) + if (!Array.isArray(page.data)) throw new Error('Invalid native history page') + turns.push(...page.data) + cursor = string(page.nextCursor) + } while (cursor) + thread.turns = turns + } else thread = record(record(await client.request('thread/read', { threadId, includeTurns: true })).thread) + return thread +} + +/** Separated from transport/locks so failure and recovery paths are testable. */ +export async function performArchivedFork( + client: NativeClient, request: ForkArchivedCodexRequest, journal: ArchivedForkJournal, + save: () => Promise, assertSourceInactive: () => Promise +): Promise { + if (JSON.stringify(journal.request) !== JSON.stringify(request)) throw new Error('Archived fork reservation does not match this request') + if (journal.done && journal.nativeSessionId) return journal.nativeSessionId + const restoreSource = async () => { + if (!journal.restoreRequired) return + await assertSourceInactive() + const loaded = record(await client.request('thread/loaded/list', {})) + if (!Array.isArray(loaded.data) || loaded.data.includes(request.sourceThreadId)) { + throw new Error('Source was loaded during archived fork; refuse to archive a potentially active conversation') + } + await client.request('thread/archive', { threadId: request.sourceThreadId }) + journal.restoreRequired = false + await save() + } + // A previous process may have died after unarchive or after creating a child. + // Restore first; a dispatched fork without a returned id must never replay. + await restoreSource() + if (journal.forkIssued && !journal.nativeSessionId) { + throw new Error('Previous native fork outcome is unconfirmed; inspect the operation before retrying') + } + if (!journal.nativeSessionId) { + await assertSourceInactive() + const source = await readHistory(client, request.sourceThreadId) + if (source.id !== request.sourceThreadId || source.parentThreadId) throw new Error('Archived fork requires an exact native root thread') + if (resolve(string(source.cwd) ?? '') !== resolve(request.directory)) throw new Error('Native source directory does not match the HAPI session') + if (record(source.status).type !== 'notLoaded') throw new Error('Native source is loaded; stop it before forking archived history') + if (request.boundary) { + const turns = (Array.isArray(source.turns) ? source.turns : []).map(record) + const index = turns.findIndex(turn => turn.id === request.boundary!.turnId) + const selected = turns[index] + const user = (Array.isArray(selected?.items) ? selected.items.map(record) : []).find(item => item.type === 'userMessage') + const localId = string(user?.clientId ?? user?.clientUserMessageId) + ?? (user?.id ? `codex:${request.sourceThreadId}:user:${user.id}` : undefined) + if (index < 0 || localId !== request.boundary.localId + || turns.slice(0, index + 1).some(turn => !['completed', 'interrupted', 'failed'].includes(string(turn.status) ?? ''))) { + throw new Error('Historical fork boundary is missing, unfinished, or inside a steered native turn') + } + } + const fork = async () => { + await assertSourceInactive() + journal.forkIssued = true + await save() + const response = record(await client.request('thread/fork', { + threadId: request.sourceThreadId, + ...(request.boundary ? { beforeTurnId: request.boundary.turnId } : {}), + deferGoalContinuation: true, excludeTurns: true + })) + const child = record(response.thread) + const id = string(child.id) + if (!id || id === request.sourceThreadId || child.parentThreadId === request.sourceThreadId) { + throw new Error('Native fork did not create an independent root') + } + journal.nativeSessionId = id + await save() + } + try { + try { await fork() } + catch (error) { + // Only an explicit archived rejection proves no fork occurred. + if (!(error instanceof Error) || !error.message.startsWith(`session ${request.sourceThreadId} is archived.`)) throw error + journal.forkIssued = false + journal.restoreRequired = true + await save() + await assertSourceInactive() + await client.request('thread/unarchive', { threadId: request.sourceThreadId }) + await fork() + } + } finally { await restoreSource() } + } + // Release the temporary server's writer through native archive. The child + // launcher unarchives this exact id; no source resume or model turn occurs. + await client.request('thread/archive', { threadId: journal.nativeSessionId }) + journal.done = true + await save() + return journal.nativeSessionId! +} + +export async function forkArchivedCodex(request: ForkArchivedCodexRequest, allowsDirectory: () => Promise): Promise { + const home = codexHome() + const journalDirectory = join(home, 'hapi-archived-forks') + await mkdir(journalDirectory, { recursive: true, mode: 0o700 }) + const target = join(journalDirectory, `${request.operationId}.json`) + const loadJournal = async (): Promise => { + try { return JournalSchema.parse(JSON.parse(await readFile(target, 'utf8'))) } + catch (error) { + if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error + return { request, restoreRequired: false, forkIssued: false, done: false } + } + } + const failure = (error: unknown, journal?: ArchivedForkJournal): ForkArchivedCodexResponse => ({ + error: error instanceof Error ? error.message : String(error), + rollbackSafe: Boolean(journal && !journal.forkIssued && !journal.restoreRequired && !journal.nativeSessionId) + }) + return withThreadOwnership(home, request.sourceThreadId, `archived-fork:${request.operationId}`, async () => { + const api = await ApiClient.create() + const assertSourceInactive = async () => { + const source = await api.getSession(request.sourceSessionId) + if (source.active || source.metadata?.codexSessionId !== request.sourceThreadId + || source.metadata?.codexArchivedFork?.operationId !== request.operationId) { + throw new Error('Source session changed during archived fork') + } + } + const journal = await loadJournal() + const save = async () => { + const temporary = `${target}.${randomUUID()}.tmp` + await writeFile(temporary, JSON.stringify(journal), { mode: 0o600 }) + await rename(temporary, target) + } + const client = new CodexAppServerClient({ cwd: request.directory }) + client.setServerRequestHandler(() => {}) + try { + // Report policy rejection against the persisted journal: a fresh + // attempt is safe to release, but interrupted native work is not. + if (!await allowsDirectory()) throw new Error('Directory is outside workspace roots') + await initializeSharedClient(client) + return { nativeSessionId: await performArchivedFork(client, request, journal, save, assertSourceInactive) } + } catch (error) { + return failure(error, journal) + } finally { await client.disconnect({ gracefulTimeoutMs: 2_000 }) } + }, async error => { + try { return failure(error, await loadJournal()) } + catch { return failure(error) } // Unreadable journals cannot prove rollback safe. + }) +} diff --git a/cli/src/codex/shared/registry.ts b/cli/src/codex/shared/registry.ts index 0d4409e2..3ad2b3ad 100644 --- a/cli/src/codex/shared/registry.ts +++ b/cli/src/codex/shared/registry.ts @@ -79,21 +79,29 @@ export async function saveRuntime(record: CodexRuntimeRecord): Promise { } /** The store-wide lock spans cold resume, not merely registry writes. */ -export async function withThreadOwnership(home: string, threadId: string, ownId: string, work: () => Promise): Promise { +export async function withThreadOwnership(home: string, threadId: string, ownId: string, work: () => Promise, + onPreflightRejected?: (error: unknown) => Promise): Promise { const directory = join(home, 'hapi-runtime-owners'); await mkdir(directory, { recursive: true, mode: 0o700 }); return withSettingsFileLock(join(directory, 'ownership'), async () => { - for (const owner of await readRecords(directory, true)) { - if (owner.id === ownId || owner.codexHome !== home) continue; - if (owner.pendingCreations?.length && runtimeMayBeAlive(owner)) { - throw new Error(`Codex runtime ${owner.id} has an unconfirmed creation. Inspect/stop that runtime before cold resume; its thread ID may be unknown.`); - } - const match = Object.entries(owner.sessions).find(([, session]) => session.threadId === threadId && session.active); - if (!match) continue; - const orphanAlive = owner.serverPid && generationMayBeAlive(owner.serverPid, owner.serverMarker); - if (generationMayBeAlive(owner.pid, owner.marker) || orphanAlive) { - throw new Error(`Thread belongs to another Codex runtime. Use hapi resume ${match[0]}.${orphanAlive && !runtimeAlive(owner) ? ' Its wrapper exited; stop the orphaned runtime before recovery.' : ''}`); + try { + for (const owner of await readRecords(directory, true)) { + if (owner.id === ownId || owner.codexHome !== home) continue; + if (owner.pendingCreations?.length && runtimeMayBeAlive(owner)) { + throw new Error(`Codex runtime ${owner.id} has an unconfirmed creation. Inspect/stop that runtime before cold resume; its thread ID may be unknown.`); + } + const match = Object.entries(owner.sessions).find(([, session]) => session.threadId === threadId && session.active); + if (!match) continue; + const orphanAlive = owner.serverPid && generationMayBeAlive(owner.serverPid, owner.serverMarker); + if (generationMayBeAlive(owner.pid, owner.marker) || orphanAlive) { + throw new Error(`Thread belongs to another Codex runtime. Use hapi resume ${match[0]}.${orphanAlive && !runtimeAlive(owner) ? ' Its wrapper exited; stop the orphaned runtime before recovery.' : ''}`); + } } + } catch (error) { + // Still under the store lock, before work can mutate native state. + // Recovery callers can inspect their journal without racing a writer. + if (onPreflightRejected) return onPreflightRejected(error); + throw error; } return work(); }); diff --git a/cli/src/codex/shared/root.test.ts b/cli/src/codex/shared/root.test.ts index a4268d97..e61b1aa8 100644 --- a/cli/src/codex/shared/root.test.ts +++ b/cli/src/codex/shared/root.test.ts @@ -67,6 +67,7 @@ async function fixture(opts?: { hubArchived?: boolean; end?: RootHost['end']; cr const send = vi.fn(); const consumed = vi.fn(); const uncertain = vi.fn(); + const sessionReady = vi.fn(); const hubArchivedListeners: Array<() => void> = []; const session = { sessionId: 'sid', getMetadata: () => metadata, @@ -79,7 +80,7 @@ async function fixture(opts?: { hubArchived?: boolean; end?: RootHost['end']; cr if (event === 'hub-archived') hubArchivedListeners.push(listener); }, rpcHandlerManager: { registerHandler: (name: string, handler: (raw: unknown) => Promise) => rpc.set(name, handler) }, - sendSessionEvent() {}, sendAgentMessage: send, emitSessionReady() {}, + sendSessionEvent() {}, sendAgentMessage: send, emitSessionReady: sessionReady, sendUserMessage() {}, emitMessagesConsumed: consumed, emitSteerIndeterminate: uncertain, syncNativeQueuedMessage() {}, sendSessionDeath() {}, async flush() {}, close() {} } as unknown as ApiSessionClient; @@ -102,7 +103,7 @@ async function fixture(opts?: { hubArchived?: boolean; end?: RootHost['end']; cr responses: Array<{ id: string | number; result: unknown }>; }; return { - root, native, rpc, send, consumed, uncertain, metadata: () => metadata, state: () => state, updateState, + root, native, rpc, send, consumed, uncertain, sessionReady, metadata: () => metadata, state: () => state, updateState, drain: () => (root as unknown as { notifications: Promise }).notifications, reconnect: () => reconnect?.(), emitHubArchived: () => { for (const listener of hubArchivedListeners) listener(); }, @@ -111,6 +112,29 @@ async function fixture(opts?: { hubArchived?: boolean; end?: RootHost['end']; cr }; } +describe('native readiness after Hub reconnect', () => { + it('replays readiness only after an activated root revalidates native history', async () => { + const f = await fixture() + f.reconnect() + await vi.waitFor(() => expect(f.state().steeringActive).toBe(false)) + expect(f.sessionReady).not.toHaveBeenCalled() + await f.root.activate() + expect(f.sessionReady).toHaveBeenCalledTimes(1) + f.reconnect() + await vi.waitFor(() => expect(f.sessionReady).toHaveBeenCalledTimes(2)) + }) + + it('does not replay readiness if native revalidation fails', async () => { + const f = await fixture() + await f.root.activate() + const read = vi.spyOn(f.root.client, 'request').mockRejectedValue(new Error('native unavailable')) + f.reconnect() + await vi.waitFor(() => expect(read).toHaveBeenCalledWith('thread/read', expect.anything())) + await new Promise(resolve => setTimeout(resolve, 10)) + expect(f.sessionReady).toHaveBeenCalledTimes(1) + }) +}) + async function startCompaction(f: Awaited>, id = 'input', turnId = 'compacting-turn') { f.native.queue = [{ id: `native-${id}`, clientUserMessageId: id, input: [{ type: 'text', text: id }] }]; await f.root.refresh(); diff --git a/cli/src/codex/shared/root.ts b/cli/src/codex/shared/root.ts index a0c1b02f..1bf18055 100644 --- a/cli/src/codex/shared/root.ts +++ b/cli/src/codex/shared/root.ts @@ -138,6 +138,7 @@ export class SharedCodexRoot { }); this.session.onReconnect(() => { if (!this.threadId || this.closed || this.stopping) return; + const wasActivated = Boolean(this.heartbeat); // An emitted socket packet is not a durable hub acknowledgement. // Replay final native history with stable IDs after every reconnect. this.projection?.reset(); @@ -145,6 +146,11 @@ export class SharedCodexRoot { this.publishedPlanId = undefined; this.publishedCompactionIds = undefined; void this.refresh().then(() => this.refreshChildren(false)).then(() => this.queue.replay()) + .then(() => { + // Hub restart loses its ephemeral readiness fence. Replay + // it only after native revalidation and completed activation. + if (wasActivated && this.heartbeat && !this.closed && !this.stopping && this.client.isInitialized()) this.session.emitSessionReady(); + }) .catch(error => logger.debug('[Codex shared] hub resync', error)); }); } diff --git a/docs/guide/codex-shared-sessions.md b/docs/guide/codex-shared-sessions.md index deb1e212..52668a1d 100644 --- a/docs/guide/codex-shared-sessions.md +++ b/docs/guide/codex-shared-sessions.md @@ -90,6 +90,25 @@ being received on the same app-server connection. ## Recovery and local data +### Fork an archived conversation + +With an online Runner that advertises archived Codex forks, the Web chat can +fork an inactive Codex conversation directly. The source stays inactive and +archived in HAPI. A historical fork excludes the selected user message's native +turn and all later turns; Fork current retains the whole persisted conversation. + +The Runner reads the native history, temporarily unarchives the native source +when required, creates an independent branch, and restores the source archive. +No prompt is sent to the source and no model turn is started by the fork itself. +The new branch uses its own HAPI identity and Codex execution. + +If an operation is interrupted, retry the same fork boundary so HAPI can reconcile +its persisted reservation. Reopen, archive and delete are blocked while that +reservation is pending. An unconfirmed native fork without a returned thread id +is never automatically repeated: inspect the Runner's operation journal under +`$CODEX_HOME/hapi-archived-forks` before recovery. Missing history, an offline +machine, a loaded native source, or a Runner needing an upgrade produces an error. + - Hub outage: existing native threads continue. Reconnect reconciles history, queue and pending state with stable IDs. - Runner restart: use existing Runner process tracking/recovery. Live attachment diff --git a/hub/src/store/sessions.ts b/hub/src/store/sessions.ts index 3c3367af..5f4e13cb 100644 --- a/hub/src/store/sessions.ts +++ b/hub/src/store/sessions.ts @@ -71,6 +71,8 @@ const SIMPLE_RESUME_TOKENS = [ // Hub-owned metadata that must survive sparse CLI metadata replacements. // `null` remains the explicit-clear sentinel handled by carryForwardIfMissing. const HUB_OWNED_METADATA_FIELDS = [ + 'codexArchivedFork', + 'codexResumePending', 'titleSuggestionAutoAttemptedAt', 'titleSuggestionTurn', 'promptTitle' diff --git a/hub/src/sync/archivedFork.test.ts b/hub/src/sync/archivedFork.test.ts new file mode 100644 index 00000000..0a540764 --- /dev/null +++ b/hub/src/sync/archivedFork.test.ts @@ -0,0 +1,381 @@ +import { describe, expect, it } from 'bun:test' +import { randomUUID } from 'node:crypto' +import { Store } from '../store' +import { SyncEngine } from './syncEngine' +import { RpcRegistry } from '../socket/rpcRegistry' +import { MACHINE_CAPABILITIES } from '@hapi/protocol' +import type { RpcGateway } from './rpcGateway' +import type { ForkArchivedCodexRequest } from '@hapi/protocol/apiTypes' + +function fixture() { + const store = new Store(':memory:') + const engine = new SyncEngine(store, {} as never, new RpcRegistry(), { broadcast() {} } as never) + const machineId = randomUUID() + engine.getOrCreateMachine(machineId, { host: 'test', platform: 'win32', happyCliVersion: '1', + capabilities: [MACHINE_CAPABILITIES.ForkArchivedCodex] }, null, 'ns') + engine.handleMachineAlive({ machineId, time: Date.now() }) + const source = engine.getOrCreateSession('archived', { path: '/work', host: 'test', machineId, flavor: 'codex', + lifecycleState: 'archived', archivedBy: 'hub', codexSessionId: 'native-source', + conversationHistoryTurns: { boundary: 'turn-2' }, conversationHistoryPoints: { boundary: true } + }, null, 'ns') + store.messages.addMessage(source.id, { role: 'user', content: { type: 'text', text: 'old prompt' } }, 'boundary') + store.messages.markMessagesInvoked(source.id, ['boundary'], Date.now()) + const rpc = (engine as unknown as { rpcGateway: RpcGateway }).rpcGateway + let forks = 0, spawns = 0 + rpc.forkArchivedCodex = async (_machine, request) => { + forks++ + expect(request.sourceSessionId).toBe(source.id) + expect(engine.getSession(source.id)?.active).toBe(false) + expect(engine.getSession(source.id)?.metadata?.lifecycleState).toBe('archived') + return { nativeSessionId: 'native-child' } + } + rpc.spawnSession = async (...args) => { + spawns++ + const id = args[12]! + engine.handleSessionAlive({ sid: id, time: Date.now(), mode: 'remote' }) + engine.handleSessionReady({ sid: id, time: Date.now() }) + return { type: 'success', sessionId: id } + } + return { engine, store, source, machineId, rpc, counts: () => ({ forks, spawns }) } +} + +describe('archived Codex fork coordination', () => { + it('refuses local resume discovery of a CLI-archived source while its fork child is starting', async () => { + const f = fixture() + let entered!: () => void + const spawning = new Promise(resolve => { entered = resolve }) + let childId = '' + try { + const current = f.store.sessions.getSession(f.source.id)! + f.store.sessions.updateSessionMetadata(f.source.id, { ...current.metadata as object, archivedBy: 'cli' }, current.metadataVersion, 'ns') + ;(f.engine as unknown as { sessionCache: { refreshSession(id: string): void } }).sessionCache.refreshSession(f.source.id) + f.rpc.spawnSession = async (...args) => { + childId = args[12]! + f.engine.handleSessionAlive({ sid: childId, time: Date.now(), mode: 'remote' }) + entered() + return { type: 'success', sessionId: childId } + } + const pending = f.engine.forkConversation(f.source.id, 'ns') + await spawning + expect(f.engine.resolveLocalResumeTarget(f.source.id, 'ns')).toMatchObject({ type: 'error', code: 'resume_unavailable' }) + expect(f.engine.resolveLocalResumeTarget(childId, 'ns')).toMatchObject({ type: 'error', code: 'resume_unavailable' }) + expect(f.engine.getSession(f.source.id)?.active).toBe(false) + f.engine.handleSessionReady({ sid: childId, time: Date.now() }) + expect((await pending).type).toBe('success') + expect(f.engine.resolveLocalResumeTarget(f.source.id, 'ns').type).toBe('success') + } finally { f.engine.stop() } + }) + it('keeps an unconfirmed resume fenced across sparse metadata and late activation until native ready', async () => { + const f = fixture() + try { + f.rpc.spawnSession = async () => ({ type: 'error', message: 'startup unconfirmed' }) + const result = await f.engine.resumeSession(f.source.id, 'ns') + expect(result).toMatchObject({ type: 'error', rollbackSafe: false }) + expect(f.engine.getSession(f.source.id)?.metadata?.codexResumePending).toEqual({ machineId: f.machineId, nativeSessionId: 'native-source' }) + const current = f.store.sessions.getSession(f.source.id)! + f.store.sessions.updateSessionMetadata(f.source.id, { path: '/work', host: 'test' }, current.metadataVersion, 'ns') + ;(f.engine as unknown as { sessionCache: { refreshSession(id: string): void } }).sessionCache.refreshSession(f.source.id) + expect((await f.engine.forkConversation(f.source.id, 'ns')).type).toBe('error') + f.engine.handleSessionAlive({ sid: f.source.id, time: Date.now(), mode: 'remote' }) + expect((await f.engine.forkConversation(f.source.id, 'ns')).type).toBe('error') + expect(f.counts().forks).toBe(0) + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + f.engine.handleSessionReady({ sid: f.source.id, time: Date.now() }) + expect(f.engine.getSession(f.source.id)?.metadata?.codexResumePending).toBeUndefined() + } finally { f.engine.stop() } + }) + + it('retains an unconfirmed resume across Hub restart and reconciles only a proven stopped process', async () => { + const f = fixture() + let restarted: SyncEngine | undefined + try { + f.rpc.spawnSession = async () => ({ type: 'error', message: 'startup unconfirmed' }) + await f.engine.reopenSession(f.source.id, 'ns') + f.engine.stop() + restarted = new SyncEngine(f.store, {} as never, new RpcRegistry(), { broadcast() {} } as never) + expect((await restarted.forkConversation(f.source.id, 'ns')).type).toBe('error') + const rpc = (restarted as unknown as { rpcGateway: RpcGateway }).rpcGateway + rpc.stopRunnerSession = async () => 'unknown' + expect((await restarted.resumeSession(f.source.id, 'ns')).type).toBe('error') + expect(restarted.getSession(f.source.id)?.metadata?.codexResumePending).toBeDefined() + rpc.stopRunnerSession = async () => 'already_gone' + rpc.spawnSession = async () => ({ type: 'error', message: 'confirmed refusal', childStarted: false }) + await restarted.resumeSession(f.source.id, 'ns') + expect(restarted.getSession(f.source.id)?.metadata?.codexResumePending).toBeUndefined() + } finally { restarted?.stop(); f.engine.stop() } + }) + + it('releases confirmed pre-start failure immediately and permits an archived fork', async () => { + const f = fixture() + try { + const spawn = f.rpc.spawnSession + f.rpc.spawnSession = async () => ({ type: 'error', message: 'confirmed refusal', childStarted: false }) + await f.engine.reopenSession(f.source.id, 'ns') + expect(f.engine.getSession(f.source.id)?.metadata?.codexResumePending).toBeUndefined() + f.rpc.spawnSession = spawn + expect((await f.engine.forkConversation(f.source.id, 'ns')).type).toBe('success') + } finally { f.engine.stop() } + }) + + it('keeps an unconfirmed resume fenced until archive proves Runner termination', async () => { + const f = fixture() + try { + f.rpc.spawnSession = async () => ({ type: 'error', message: 'startup unconfirmed' }) + await f.engine.resumeSession(f.source.id, 'ns') + f.rpc.killSession = async () => ({}) + f.rpc.stopRunnerSession = async () => 'unknown' + await expect(f.engine.archiveSession(f.source.id)).rejects.toThrow('still running') + expect(f.engine.getSession(f.source.id)?.metadata?.codexResumePending).toBeDefined() + f.rpc.stopRunnerSession = async () => 'already_gone' + await f.engine.archiveSession(f.source.id) + expect(f.engine.getSession(f.source.id)?.metadata?.codexResumePending).toBeUndefined() + expect(f.engine.getSession(f.source.id)?.metadata?.lifecycleState).toBe('archived') + } finally { f.engine.stop() } + }) + + it.each(['resume', 'reopen'])('rejects a fork while an earlier %s is awaiting startup confirmation', async action => { + const f = fixture() + let finish!: () => void + let spawned!: () => void + const entered = new Promise(resolve => { spawned = resolve }) + const gate = new Promise(resolve => { finish = resolve }) + f.rpc.spawnSession = async (...args) => { + spawned() + await gate + f.engine.handleSessionAlive({ sid: args[12]!, time: Date.now(), mode: 'remote' }) + f.engine.handleSessionReady({ sid: args[12]!, time: Date.now() }) + return { type: 'success', sessionId: args[12]! } + } + try { + const pending = action === 'resume' ? f.engine.resumeSession(f.source.id, 'ns') : f.engine.reopenSession(f.source.id, 'ns') + await entered + const fork = await f.engine.forkConversation(f.source.id, 'ns', 'boundary') + expect(fork.type).toBe('error') + expect(f.counts().forks).toBe(0) + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + finish() + expect((await pending).type).toBe('success') + // The startup lock is released after confirmation, even though the + // original source is now active. + expect((await f.engine.reopenSession(f.source.id, 'ns')).type).toBe('success') + } finally { finish(); f.engine.stop() } + }) + + it.each(['resume', 'reopen'])('coalesces concurrent %s requests so parallel callers share in-flight startup', async action => { + const f = fixture() + let finish!: () => void + let spawned!: () => void + const entered = new Promise(resolve => { spawned = resolve }) + const gate = new Promise(resolve => { finish = resolve }) + let spawnCount = 0 + f.rpc.spawnSession = async (...args) => { + spawnCount++ + spawned() + await gate + f.engine.handleSessionAlive({ sid: args[12]!, time: Date.now(), mode: 'remote' }) + f.engine.handleSessionReady({ sid: args[12]!, time: Date.now() }) + return { type: 'success', sessionId: args[12]! } + } + try { + const call = () => action === 'resume' ? f.engine.resumeSession(f.source.id, 'ns') : f.engine.reopenSession(f.source.id, 'ns') + const first = call() + await entered + const second = call() + finish() + const [r1, r2] = await Promise.all([first, second]) + expect(r1.type).toBe('success') + expect(r2.type).toBe('success') + expect(spawnCount).toBe(1) + } finally { finish(); f.engine.stop() } + }) + + it('releases a fresh ownership rejection so source reconnection cannot trap lifecycle actions', async () => { + const f = fixture() + try { + f.rpc.forkArchivedCodex = async () => { + f.engine.handleSessionAlive({ sid: f.source.id, time: Date.now(), mode: 'remote' }) + return { error: 'Thread belongs to another Codex runtime', rollbackSafe: true } + } + expect((await f.engine.forkConversation(f.source.id, 'ns')).type).toBe('error') + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + expect((await f.engine.reopenSession(f.source.id, 'ns')).type).toBe('success') + } finally { f.engine.stop() } + }) + it.each(['offline', 'old-runner', 'queued', 'diverged', 'missing-native'])('rejects %s before reserving a child', async condition => { + const f = fixture() + try { + if (condition === 'offline') f.engine.getMachine(f.machineId)!.active = false + if (condition === 'old-runner') f.engine.getMachine(f.machineId)!.metadata!.capabilities = [] + if (condition === 'queued') f.store.messages.addMessage(f.source.id, { role: 'user', content: 'not invoked' }, 'queued') + if (condition === 'diverged' || condition === 'missing-native') { + const current = f.store.sessions.getSession(f.source.id)! + f.store.sessions.updateSessionMetadata(f.source.id, { + ...current.metadata as Record, + ...(condition === 'diverged' ? { conversationHistoryDiverged: true } : { codexSessionId: null }) + }, current.metadataVersion, 'ns') + ;(f.engine as unknown as { sessionCache: { refreshSession(id: string): void } }).sessionCache.refreshSession(f.source.id) + } + expect((await f.engine.forkConversation(f.source.id, 'ns')).type).toBe('error') + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + expect(f.counts()).toEqual({ forks: 0, spawns: 0 }) + } finally { f.engine.stop() } + }) + it('preserves a pending reservation across sparse CLI writes and releases it explicitly after success', async () => { + const f = fixture() + try { + const actual = f.rpc.forkArchivedCodex + f.rpc.forkArchivedCodex = async (machine, request) => { + const current = f.store.sessions.getSession(f.source.id)! + const write = f.store.sessions.updateSessionMetadata(f.source.id, + { path: '/work', host: 'test' }, current.metadataVersion, 'ns') + expect(write.result).toBe('success') + const saved = f.store.sessions.getSession(f.source.id)!.metadata as Record + expect(saved.codexArchivedFork).toBeDefined() + return actual.call(f.rpc, machine, request) + } + const result = await f.engine.forkConversation(f.source.id, 'ns') + expect(result.type).toBe('success') + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + } finally { f.engine.stop() } + }) + it('clears a reservation when native validation proves no mutation occurred', async () => { + const f = fixture() + try { + f.rpc.forkArchivedCodex = async () => ({ error: 'Native source history is missing', rollbackSafe: true }) + const result = await f.engine.forkConversation(f.source.id, 'ns') + expect(result.type).toBe('error') + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + expect(f.counts().spawns).toBe(0) + } finally { f.engine.stop() } + }) + it('creates one independent child without activating or altering archived source history', async () => { + const f = fixture() + try { + const before = f.store.messages.getAllMessages(f.source.id) + const result = await f.engine.forkConversation(f.source.id, 'ns', 'boundary') + expect(result.type).toBe('success') + if (result.type !== 'success') throw new Error(result.message) + const child = f.engine.getSession(result.sessionId)! + expect(child.metadata?.forkedFrom).toBe(f.source.id) + expect(child.metadata?.codexSessionId).toBe('native-child') + expect(child.active).toBe(true) + expect(f.engine.getSession(f.source.id)?.active).toBe(false) + expect(f.engine.getSession(f.source.id)?.metadata?.lifecycleState).toBe('archived') + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + expect(f.store.messages.getAllMessages(f.source.id)).toEqual(before) + expect(f.counts()).toEqual({ forks: 1, spawns: 1 }) + } finally { f.engine.stop() } + }) + + it('persists the reservation after lost native response, blocking reopen and delete until retry', async () => { + const f = fixture() + try { + const actual = f.rpc.forkArchivedCodex + let firstOperation: string | undefined + f.rpc.forkArchivedCodex = async (_machine, request) => { + firstOperation = request.operationId + throw new Error('connection lost') + } + expect((await f.engine.forkConversation(f.source.id, 'ns')).type).toBe('error') + expect((await f.engine.reopenSession(f.source.id, 'ns')).type).toBe('error') + expect((await f.engine.resumeSession(f.source.id, 'ns')).type).toBe('error') + await expect(f.engine.deleteSession(f.source.id)).rejects.toThrow('pending') + const reserved = f.engine.getSession(f.source.id)!.metadata!.codexArchivedFork!.childSessionId + expect((await f.engine.reopenSession(reserved, 'ns')).type).toBe('error') + expect((await f.engine.forkConversation(reserved, 'ns')).type).toBe('error') + await expect(f.engine.archiveSession(reserved)).rejects.toThrow('in progress') + await expect(f.engine.deleteSession(reserved)).rejects.toThrow('pending') + f.rpc.forkArchivedCodex = async (machine, request) => { + expect(request.operationId).toBe(firstOperation!) + return actual.call(f.rpc, machine, request) + } + expect((await f.engine.forkConversation(f.source.id, 'ns')).type).toBe('success') + } finally { f.engine.stop() } + }) + + it('reconciles a failed launcher and reuses its native child without another fork', async () => { + const f = fixture() + try { + const actual = f.rpc.spawnSession + f.rpc.spawnSession = async () => ({ type: 'error', message: 'launcher rejected', childStarted: false }) + expect((await f.engine.forkConversation(f.source.id, 'ns')).type).toBe('error') + const childId = f.engine.getSession(f.source.id)!.metadata!.codexArchivedFork!.childSessionId + let stopped: string | undefined + f.rpc.stopRunnerSession = async (_machine, id) => { stopped = id; return 'already_gone' } + f.rpc.spawnSession = actual + const result = await f.engine.forkConversation(f.source.id, 'ns') + expect(result).toEqual({ type: 'success', sessionId: childId }) + expect(stopped).toBe(childId) + expect(f.counts().forks).toBe(1) + } finally { f.engine.stop() } + }) + + it('retries an interrupted historical fork with its reserved native boundary after sparse metadata replacement', async () => { + const f = fixture() + try { + let original: ForkArchivedCodexRequest | undefined + f.rpc.forkArchivedCodex = async (_machine, request) => { + original = structuredClone(request) + throw new Error('native response lost') + } + expect((await f.engine.forkConversation(f.source.id, 'ns', 'boundary')).type).toBe('error') + const current = f.store.sessions.getSession(f.source.id)! + f.store.sessions.updateSessionMetadata(f.source.id, { path: '/work', host: 'test' }, current.metadataVersion, 'ns') + ;(f.engine as unknown as { sessionCache: { refreshSession(id: string): void } }).sessionCache.refreshSession(f.source.id) + expect(f.engine.getSession(f.source.id)?.metadata?.conversationHistoryTurns).toBeUndefined() + f.rpc.forkArchivedCodex = async (_machine, request) => { + expect(request).toEqual(original!) + return { nativeSessionId: 'native-child' } + } + expect((await f.engine.forkConversation(f.source.id, 'ns', 'boundary')).type).toBe('success') + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + } finally { f.engine.stop() } + }) + + it('does not complete an active child retry until native readiness is confirmed again', async () => { + const f = fixture() + try { + f.rpc.spawnSession = async (...args) => { + f.engine.handleSessionAlive({ sid: args[12]!, time: Date.now(), mode: 'remote' }) + return { type: 'error', message: 'startup interrupted', childStarted: false } + } + expect((await f.engine.forkConversation(f.source.id, 'ns')).type).toBe('error') + const child = f.engine.getSession(f.source.id)!.metadata!.codexArchivedFork!.childSessionId + let completed = false + const retry = f.engine.forkConversation(f.source.id, 'ns').then(result => { completed = true; return result }) + await new Promise(resolve => setTimeout(resolve, 10)) + expect(completed).toBe(false) + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeDefined() + f.engine.handleSessionReady({ sid: child, time: Date.now() }) + expect(await retry).toEqual({ type: 'success', sessionId: child }) + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + } finally { f.engine.stop() } + }) + + it('accepts a late native-ready binding after an ambiguous spawn acknowledgement', async () => { + const f = fixture() + try { + f.rpc.spawnSession = async (...args) => { + setTimeout(() => { + f.engine.handleSessionAlive({ sid: args[12]!, time: Date.now(), mode: 'remote' }) + f.engine.handleSessionReady({ sid: args[12]!, time: Date.now() }) + }, 10) + return { type: 'error', message: 'spawn acknowledgement timed out' } + } + const result = await f.engine.forkConversation(f.source.id, 'ns') + expect(result.type).toBe('success') + expect(f.counts().forks).toBe(1) + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + } finally { f.engine.stop() } + }) + + it('rejects wrong namespace and stale historical boundary before creating reservations', async () => { + const f = fixture() + try { + expect((await f.engine.forkConversation(f.source.id, 'other')).type).toBe('error') + expect((await f.engine.forkConversation(f.source.id, 'ns', 'missing')).type).toBe('error') + expect(f.engine.getSession(f.source.id)?.metadata?.codexArchivedFork).toBeUndefined() + expect(f.counts()).toEqual({ forks: 0, spawns: 0 }) + } finally { f.engine.stop() } + }) +}) diff --git a/hub/src/sync/rpcGateway.ts b/hub/src/sync/rpcGateway.ts index 4b98fae1..39076d7b 100644 --- a/hub/src/sync/rpcGateway.ts +++ b/hub/src/sync/rpcGateway.ts @@ -627,6 +627,13 @@ export class RpcGateway { ) as import('@hapi/protocol/apiTypes').ForkConversationRpcResponse } + async forkArchivedCodex(machineId: string, request: import('@hapi/protocol/apiTypes').ForkArchivedCodexRequest): Promise { + const result = await this.machineRpc(machineId, RPC_METHODS.ForkArchivedCodex, request, 120_000) as Record + if (typeof result?.error === 'string') return { error: result.error, rollbackSafe: result.rollbackSafe === true } + if (typeof result?.nativeSessionId !== 'string' || !result.nativeSessionId) throw new Error('Native archived fork did not return a session id') + return { nativeSessionId: result.nativeSessionId } + } + async clearConversation(sessionId: string): Promise<{ sessionId: string }> { return await this.sessionRpc(sessionId, RPC_METHODS.ClearConversation, {}, 120_000) as { sessionId: string } } diff --git a/hub/src/sync/sessionModel.test.ts b/hub/src/sync/sessionModel.test.ts index 29c0fa79..7076f127 100644 --- a/hub/src/sync/sessionModel.test.ts +++ b/hub/src/sync/sessionModel.test.ts @@ -1837,6 +1837,7 @@ describe('session model', () => { expect(outcome.result).toEqual({ type: 'success', sessionId: outcome.after.id }) expect(outcome.capturedResumeSessionIds).toEqual([rootThreadId]) + expect(outcome.after.metadata).toMatchObject({ codexResumePending: { nativeSessionId: rootThreadId } }) expect([ outcome.persistedCodexSessionId, outcome.cachedCodexSessionId, @@ -1845,7 +1846,7 @@ describe('session model', () => { ]).toEqual([ rootThreadId, rootThreadId, - outcome.before.metadataVersion + 1, + outcome.before.metadataVersion + 2, // recovered identity + durable startup fence outcome.before.updatedAt ]) }) @@ -1861,8 +1862,8 @@ describe('session model', () => { expect(outcome.result).toEqual({ type: 'success', sessionId: outcome.after.id }) expect(outcome.capturedResumeSessionIds).toEqual([metadataThreadId]) expect(outcome.after).toMatchObject({ - metadata: { codexSessionId: metadataThreadId }, - metadataVersion: outcome.before.metadataVersion, + metadata: { codexSessionId: metadataThreadId, codexResumePending: { nativeSessionId: metadataThreadId } }, + metadataVersion: outcome.before.metadataVersion + 1, updatedAt: outcome.before.updatedAt }) }) @@ -1931,7 +1932,7 @@ describe('session model', () => { ]).toEqual([ concurrentThreadId, concurrentThreadId, - outcome.before.metadataVersion, + outcome.before.metadataVersion + 1, outcome.before.updatedAt ]) }) @@ -1958,7 +1959,7 @@ describe('session model', () => { outcome.after.updatedAt ]).toEqual([ messageThreadId, - outcome.before.metadataVersion + 1, + outcome.before.metadataVersion + 2, outcome.before.updatedAt ]) }) diff --git a/hub/src/sync/syncEngine.ts b/hub/src/sync/syncEngine.ts index 035dce6a..e1186f71 100644 --- a/hub/src/sync/syncEngine.ts +++ b/hub/src/sync/syncEngine.ts @@ -214,6 +214,9 @@ export class SyncEngine { private readonly scratchlistUploadTails = new Map>() /** Coalesce duplicate clear requests so retries cannot spawn two fresh sessions. */ private readonly opencodeClearTails = new Map>() + /** Coalesce concurrent resume/reopen requests so parallel callers share in-flight startup. */ + private readonly resumeTails = new Map>() + private readonly reopenTails = new Map>() /** Serialize fork/rewind per session so concurrent native rollbacks cannot stack. */ private readonly historyActionsInFlight = new Set() /** @@ -665,6 +668,10 @@ export class SyncEngine { handleSessionReady(payload: { sid: string; time: number }): void { this.sessionReadyIds.add(payload.sid) const session = this.sessionCache.getSession(payload.sid) + if (session?.active && session.metadata?.codexResumePending + && session.metadata.codexResumePending.nativeSessionId === session.metadata.codexSessionId) { + this.writeCodexResumePending(session.id, session.namespace, undefined) + } if (session?.metadata?.piResumeAttempt) { void this.writePiResumeAttempt(payload.sid, session.namespace, null) .then(() => { @@ -1381,7 +1388,7 @@ export class SyncEngine { private async waitForExactNativeForkBound( childId: string, expectedNativeSessionId: string, - metadataKey: 'grokSessionId' | 'piSessionId', + metadataKey: 'grokSessionId' | 'piSessionId' | 'codexSessionId', requireSessionReady: boolean, timeoutMs: number = 60_000 ): Promise { @@ -1517,11 +1524,169 @@ export class SyncEngine { } } + private archivedForkPending(sessionId: string, namespace?: string): boolean { + const session = namespace ? this.sessionCache.getSessionByNamespace(sessionId, namespace) : this.sessionCache.getSession(sessionId) + if (!session) return false + if (session.metadata?.codexArchivedFork) return true + const parent = session.metadata?.forkedFrom + ? this.sessionCache.getSessionByNamespace(session.metadata.forkedFrom, session.namespace) : undefined + return parent?.metadata?.codexArchivedFork?.childSessionId === sessionId + } + + private isPtySession(sessionId: string, namespace: string): boolean { + const session = this.sessionCache.getSessionByNamespace(sessionId, namespace) + return (session?.agentState as { startingMode?: string } | null)?.startingMode === 'pty' + } + + private writeCodexResumePending(sessionId: string, namespace: string, + pending: NonNullable['codexResumePending']): void { + for (let retry = 0; retry < 5; retry++) { + const current = this.sessionCache.refreshSession(sessionId) + if (!current?.metadata || current.namespace !== namespace) throw new Error('Codex resume source changed') + const result = this.store.sessions.updateSessionMetadata(sessionId, + { ...current.metadata, codexResumePending: pending ?? null }, current.metadataVersion, namespace, { touchUpdatedAt: false }) + if (result.result === 'success') { this.sessionCache.refreshSession(sessionId); return } + if (result.result === 'error') break + } + throw new Error('Failed to persist Codex resume state') + } + + private async reconcileCodexResume(session: Session): Promise { + const pending = session.metadata?.codexResumePending + if (!pending) return true + if (session.active && this.sessionReadyIds.has(session.id) && session.metadata?.codexSessionId === pending.nativeSessionId) { + this.writeCodexResumePending(session.id, session.namespace, undefined) + return true + } + const stopped = await this.rpcGateway.stopRunnerSession(pending.machineId, session.id) + if (stopped !== 'stopped' && stopped !== 'already_gone') return false + if (this.sessionCache.getSession(session.id)?.active) this.handleSessionEnd({ sid: session.id, time: Date.now(), reason: 'error' }) + this.writeCodexResumePending(session.id, session.namespace, undefined) + return true + } + + private setArchivedForkAttempt(sessionId: string, namespace: string, + attempt: NonNullable['codexArchivedFork'], allowActiveClear = false): Session { + for (let retry = 0; retry < 5; retry++) { + const source = this.sessionCache.refreshSession(sessionId) + if (!source || source.namespace !== namespace || (source.active && !(allowActiveClear && !attempt)) || !source.metadata) throw new Error('Archived source changed during fork') + const result = this.store.sessions.updateSessionMetadata(sessionId, + { ...source.metadata, codexArchivedFork: attempt ?? null }, source.metadataVersion, namespace, { touchUpdatedAt: false }) + if (result.result === 'success') return this.sessionCache.refreshSession(sessionId)! + if (result.result === 'error') break + } + throw new Error('Failed to persist archived fork reservation') + } + + private async forkArchivedCodexSession(source: Session, namespace: string, messageLocalId?: string): Promise< + { type: 'success'; sessionId: string } | { type: 'error'; message: string } + > { + try { + const metadata = source.metadata + if (metadata?.flavor !== 'codex' || !metadata.codexSessionId || !metadata.machineId || !metadata.path) { + throw new Error('Inactive fork requires a Codex session with native history and a machine binding') + } + if (metadata.conversationHistoryDiverged) throw new Error('Conversation history is diverged; refuse further fork/rewind') + if (this.store.messages.getUninvokedLocalMessages(source.id).length) throw new Error('Session has queued messages') + const machine = this.getMachine(metadata.machineId) + if (!machine?.active || machine.namespace !== namespace) throw new Error('Source machine is offline') + if (!machine.metadata?.capabilities?.includes(MACHINE_CAPABILITIES.ForkArchivedCodex)) { + throw new Error('Upgrade the source Runner to fork archived Codex sessions') + } + let attempt = metadata.codexArchivedFork + if (attempt && attempt.boundaryLocalId !== messageLocalId) throw new Error('A previous archived fork is pending at another boundary; retry that fork first') + const turnId = attempt ? attempt.boundaryTurnId : messageLocalId ? metadata.conversationHistoryTurns?.[messageLocalId] : undefined + if (messageLocalId) { + if (!attempt) this.assertInvokedHistoryBoundary(source.id, messageLocalId) + if (!turnId) throw new Error('No native history point for this message') + } + if (!attempt) { + attempt = { operationId: randomUUID(), childSessionId: randomUUID(), + ...(messageLocalId ? { boundaryLocalId: messageLocalId, boundaryTurnId: turnId! } : {}) } + this.setArchivedForkAttempt(source.id, namespace, attempt) + } + const childId = attempt.childSessionId + let child = this.sessionCache.refreshSession(childId) + if (child?.active) { + if (!attempt.nativeSessionId || child.metadata?.codexSessionId !== attempt.nativeSessionId + || child.namespace !== namespace || child.metadata?.forkedFrom !== source.id) throw new Error('Archived fork child binding is unconfirmed') + if (!await this.waitForExactNativeForkBound(childId, attempt.nativeSessionId, 'codexSessionId', true, 90_000) + || !this.sessionCache.getSession(childId)?.active) throw new Error('Archived fork child native startup is not confirmed') + this.setArchivedForkAttempt(source.id, namespace, undefined) + return { type: 'success', sessionId: childId } + } + if (!child) { + child = this.sessionCache.getOrCreateSession(`archived-fork:${attempt.operationId}`, { + path: metadata.path, host: metadata.host, machineId: metadata.machineId, flavor: 'codex', + forkedFrom: source.id, startedBy: 'runner', + summary: buildForkSessionSummary(metadata) + }, null, namespace, source.model ?? undefined, source.effort ?? undefined, source.modelReasoningEffort ?? undefined, childId) + } else if (child.namespace !== namespace || child.metadata?.forkedFrom !== source.id) throw new Error('Invalid archived fork reservation') + if (attempt.spawnAttempted) { + const stopped = await this.rpcGateway.stopRunnerSession(metadata.machineId, childId) + if (stopped !== 'stopped' && stopped !== 'already_gone') throw new Error('Previous fork launcher outcome is unconfirmed; retry after cleanup') + } + if (!attempt.nativeSessionId) { + const result = await this.rpcGateway.forkArchivedCodex(metadata.machineId, { + operationId: attempt.operationId, sourceSessionId: source.id, + sourceThreadId: metadata.codexSessionId, directory: metadata.path, + ...(messageLocalId ? { boundary: { localId: messageLocalId, turnId: turnId! } } : {}) + }) + if ('error' in result) { + if (result.rollbackSafe) { + this.setArchivedForkAttempt(source.id, namespace, undefined, true) + await this.sessionCache.deleteSession(childId) + } + throw new Error(result.error) + } + if (result.nativeSessionId === metadata.codexSessionId) throw new Error('Native fork reused the source identity') + attempt = { ...attempt, nativeSessionId: result.nativeSessionId } + this.setArchivedForkAttempt(source.id, namespace, attempt) + } + child = this.sessionCache.refreshSession(childId)! + const updated = this.store.sessions.updateSessionMetadata(childId, + { ...child.metadata!, codexSessionId: attempt.nativeSessionId }, child.metadataVersion, namespace, { touchUpdatedAt: false }) + if (updated.result !== 'success') throw new Error('Failed to bind native fork to its child') + this.sessionCache.refreshSession(childId) + attempt = { ...attempt, spawnAttempted: true } + this.setArchivedForkAttempt(source.id, namespace, attempt) + const spawn = await this.rpcGateway.spawnSession(metadata.machineId, metadata.path, 'codex', + source.model ?? undefined, source.modelReasoningEffort ?? undefined, undefined, 'simple', undefined, + attempt.nativeSessionId, source.effort ?? undefined, source.permissionMode, source.serviceTier ?? undefined, + childId, source.collaborationMode) + if (spawn.type === 'success' && spawn.sessionId !== childId) throw new Error('Fork launcher returned a different session') + if (spawn.type === 'error' && spawn.childStarted === false) throw new Error(spawn.message) + // A slow Runner can confirm its already-created child after the + // spawn RPC deadline. Observe the reserved binding; never spawn a + // second engine just because that acknowledgement was delayed. + const deadline = Date.now() + 90_000 + let bound = false + do { + const ready = this.sessionCache.refreshSession(childId) + if (!ready || ready.metadata?.codexSessionId !== attempt.nativeSessionId) throw new Error('Fork child native binding changed') + bound = ready.active && this.sessionReadyIds.has(childId) + if (!bound) await new Promise(resolve => setTimeout(resolve, 250)) + } while (!bound && Date.now() < deadline) + if (!bound) throw new Error(spawn.type === 'error' ? spawn.message : 'Archived fork child did not become ready') + this.setArchivedForkAttempt(source.id, namespace, undefined) + return { type: 'success', sessionId: childId } + } catch (error) { + return { type: 'error', message: `${error instanceof Error ? error.message : String(error)}. The source was not reopened; retry the same fork to reconcile a pending operation.` } + } + } + async forkConversation( sessionId: string, namespace: string, messageLocalId?: string ): Promise<{ type: 'success'; sessionId: string } | { type: 'error'; message: string }> { + const candidate = this.sessionCache.getSessionByNamespace(sessionId, namespace) + if (candidate?.metadata?.codexResumePending) { + return { type: 'error', message: 'Codex resume startup is unconfirmed; retry resume or archive to confirm its process stopped before forking' } + } + if (this.archivedForkPending(sessionId, namespace) && (!candidate?.metadata?.codexArchivedFork || candidate.active)) { + return { type: 'error', message: 'Archived fork is pending; wait for its child binding before creating another branch' } + } if (this.historyActionsInFlight.has(sessionId)) { return { type: 'error', message: 'Conversation history action already in progress' } } @@ -1543,6 +1708,7 @@ export class SyncEngine { return { type: 'error', message: access.reason === 'not-found' ? 'Session not found' : 'Access denied' } } let source = access.session + if (!source.active) return this.forkArchivedCodexSession(source, namespace, messageLocalId) try { this.assertConversationHistoryIdle(source, Boolean(messageLocalId) && this.resolveFlavor(source) === 'codex') } catch (error) { @@ -1877,6 +2043,16 @@ export class SyncEngine { } async archiveSession(sessionId: string): Promise { + if (this.historyActionsInFlight.has(sessionId) || this.archivedForkPending(sessionId)) { + throw new Error('Archived fork is in progress; retry the fork to reconcile it before archiving') + } + this.historyActionsInFlight.add(sessionId) + try { + await this.archiveSessionUnlocked(sessionId) + } finally { this.historyActionsInFlight.delete(sessionId) } + } + + private async archiveSessionUnlocked(sessionId: string): Promise { // tiann/hapi#916 / #1910: KillSession is a session-socket RPC. A missing // target does not prove the runner child is dead (stale registration, // mid-reconnect, id rotation on resume). Always fall through to the @@ -1898,8 +2074,8 @@ export class SyncEngine { } const sessionMeta = this.sessionCache.getSession(sessionId)?.metadata - const machineId = sessionMeta?.machineId - const runnerSpawned = sessionMeta?.startedBy === 'runner' + const machineId = sessionMeta?.codexResumePending?.machineId ?? sessionMeta?.machineId + const runnerSpawned = Boolean(sessionMeta?.codexResumePending) || sessionMeta?.startedBy === 'runner' || sessionMeta?.startedFromRunner === true if (machineId && runnerSpawned) { let status: 'stopped' | 'already_gone' | 'still_alive' | 'unknown' @@ -1962,6 +2138,7 @@ export class SyncEngine { if ( stored && isMachineSpawnPreallocatedStub(stored) + && !sessionMeta?.codexResumePending && !(typeof sessionMeta?.hostPid === 'number' && sessionMeta.hostPid > 0) ) { status = 'already_gone' @@ -1996,6 +2173,8 @@ export class SyncEngine { } this.releaseSessionEnumerations(sessionId) this.handleSessionEnd({ sid: sessionId, time: Date.now() }) + const archivedSession = this.sessionCache.getSession(sessionId) + if (archivedSession?.metadata?.codexResumePending) this.writeCodexResumePending(sessionId, archivedSession.namespace, undefined) } /** @@ -2251,6 +2430,10 @@ export class SyncEngine { } async deleteSession(sessionId: string): Promise { + if (this.sessionCache.getSession(sessionId)?.metadata?.codexResumePending) throw new Error('Codex resume startup is unconfirmed; archive to confirm termination before deleting') + if (this.historyActionsInFlight.has(sessionId) || this.archivedForkPending(sessionId)) { + throw new Error('Archived fork is pending; reconcile it before deleting the source') + } await this.sessionCache.deleteSession(sessionId) this.titleTurnCompletedIds.delete(sessionId) this.automaticTitleSuggestionService.forgetSession(sessionId) @@ -2910,6 +3093,10 @@ export class SyncEngine { } } + if (this.archivedForkPending(access.sessionId, namespace)) { + return { type: 'error', message: 'Archived fork is pending; retry that fork before resuming', code: 'resume_unavailable' } + } + const session = access.session const metadata = session.metadata if (!metadata || typeof metadata.path !== 'string' || metadata.path.length === 0) { @@ -3273,6 +3460,34 @@ export class SyncEngine { } async resumeSession(sessionId: string, namespace: string, opts?: { permissionMode?: PermissionMode }): Promise { + if (this.archivedForkPending(sessionId, namespace)) { + return { type: 'error', message: 'Archived fork is in progress; retry the fork before reopening', code: 'resume_failed' } + } + const isPty = this.isPtySession(sessionId, namespace) + if (!isPty) { + const existing = this.resumeTails.get(sessionId) + if (existing) return await existing + } + + if (this.historyActionsInFlight.has(sessionId)) { + return { type: 'error', message: 'Archived fork is in progress; retry the fork before reopening', code: 'resume_failed' } + } + this.historyActionsInFlight.add(sessionId) + const promise = (async () => { + try { + return await this.resumeSessionUnlocked(sessionId, namespace, opts) + } finally { + this.historyActionsInFlight.delete(sessionId) + this.resumeTails.delete(sessionId) + } + })() + if (!isPty) { + this.resumeTails.set(sessionId, promise) + } + return await promise + } + + private async resumeSessionUnlocked(sessionId: string, namespace: string, opts?: { permissionMode?: PermissionMode }): Promise { const access = this.sessionCache.resolveSessionAccess(sessionId, namespace) if (!access.ok) { return { @@ -3283,6 +3498,10 @@ export class SyncEngine { } let initialSession = access.session + if (initialSession.metadata?.codexResumePending) { + if (!await this.reconcileCodexResume(initialSession)) return { type: 'error', message: 'Codex resume startup is still unconfirmed', code: 'resume_failed', rollbackSafe: false } + initialSession = this.sessionCache.getSessionByNamespace(sessionId, namespace) ?? initialSession + } if (await this.recoverInactiveReservedClear(initialSession, namespace)) { initialSession = this.sessionCache.getSessionByNamespace(sessionId, namespace) ?? initialSession } @@ -3474,6 +3693,10 @@ export class SyncEngine { } } + if (flavor === 'codex' && resumeToken) { + this.sessionReadyIds.delete(access.sessionId) + this.writeCodexResumePending(access.sessionId, namespace, { machineId: targetMachine.id, nativeSessionId: resumeToken }) + } const spawnResult = await this.rpcGateway.spawnSession( targetMachine.id, directory, @@ -3494,6 +3717,10 @@ export class SyncEngine { ) if (spawnResult.type !== 'success') { + if (flavor === 'codex' && resumeToken) { + if (spawnResult.childStarted === false) this.writeCodexResumePending(access.sessionId, namespace, undefined) + else return { type: 'error', message: spawnResult.message, code: 'resume_failed', rollbackSafe: false } + } if (requiresPiNativeReady) { const stopped = await this.terminateInPlacePiResume( targetMachine.id, @@ -3736,6 +3963,42 @@ export class SyncEngine { * needed to resume is missing. */ async reopenSession(sessionId: string, namespace: string): Promise { + if (this.archivedForkPending(sessionId, namespace)) { + return { type: 'error', message: 'Archived fork is in progress; retry the fork before reopening', code: 'resume_failed' } + } + const isPty = this.isPtySession(sessionId, namespace) + if (!isPty) { + const existing = this.reopenTails.get(sessionId) + if (existing) return await existing + + const existingResume = this.resumeTails.get(sessionId) + if (existingResume) { + const result = await existingResume + return result.type === 'error' + ? { type: 'error', message: result.message, code: result.code } + : { type: 'success', sessionId: result.sessionId, resumed: true } + } + } + + if (this.historyActionsInFlight.has(sessionId)) { + return { type: 'error', message: 'Archived fork is in progress; retry the fork before reopening', code: 'resume_failed' } + } + this.historyActionsInFlight.add(sessionId) + const promise = (async () => { + try { + return await this.reopenSessionUnlocked(sessionId, namespace) + } finally { + this.historyActionsInFlight.delete(sessionId) + this.reopenTails.delete(sessionId) + } + })() + if (!isPty) { + this.reopenTails.set(sessionId, promise) + } + return await promise + } + + private async reopenSessionUnlocked(sessionId: string, namespace: string): Promise { const access = this.sessionCache.resolveSessionAccess(sessionId, namespace) if (!access.ok) { return { @@ -3746,6 +4009,10 @@ export class SyncEngine { } let session = access.session + if (session.metadata?.codexResumePending) { + const result = await this.resumeSessionUnlocked(sessionId, namespace) + return result.type === 'error' ? result : { type: 'success', sessionId: result.sessionId, resumed: true } + } if (await this.recoverInactiveReservedClear(session, namespace)) { session = this.sessionCache.getSessionByNamespace(sessionId, namespace) ?? session } @@ -3830,7 +4097,7 @@ export class SyncEngine { // (after Pi/PTY attempt rows capture archiveSnapshot). Hub restart // between clear and spawn leaves a non-archived inactive row; // archiveSnapshot still rolls back on resume failure while alive. - const resumeResult = await this.resumeSession(access.sessionId, namespace) + const resumeResult = await this.resumeSessionUnlocked(access.sessionId, namespace) if (resumeResult.type === 'error') { // Never restore archived metadata over a live Pi child. A live // row blocks retry by itself and must remain visible as active. @@ -3862,7 +4129,7 @@ export class SyncEngine { // Not active and not archived (e.g. brand-new session that has not yet connected, // or one that ended without writing archive metadata). Forward to resume so the // operator still gets one-click revival. - const resumeResult = await this.resumeSession(access.sessionId, namespace) + const resumeResult = await this.resumeSessionUnlocked(access.sessionId, namespace) if (resumeResult.type === 'error') { return resumeResult } diff --git a/hub/src/web/routes/sessions.test.ts b/hub/src/web/routes/sessions.test.ts index 0eaff805..bcc0adcd 100644 --- a/hub/src/web/routes/sessions.test.ts +++ b/hub/src/web/routes/sessions.test.ts @@ -1750,6 +1750,22 @@ describe('sessions routes', () => { }) }) + it('dispatches an archived source fork to the engine without requiring an active CLI', async () => { + const source = createSession({ active: false, metadata: { path: '/work', host: 'test', flavor: 'codex', lifecycleState: 'archived' } }) + const calls: unknown[][] = [] + const { app } = createApp(source, { forkConversation: async (...args) => { + calls.push(args) + return { type: 'success', sessionId: 'new-child' } + } }) + const response = await app.request('/api/sessions/session-1/fork', { + method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ messageLocalId: 'boundary' }) + }) + expect(response.status).toBe(200) + expect(await response.json()).toEqual({ sessionId: 'new-child' }) + expect(calls[0]?.[0]).toBe('session-1') + expect(calls[0]?.[2]).toBe('boundary') + }) + it('forks via POST /sessions/:id/fork and returns the child session id', async () => { const calls: Array<{ sessionId: string; namespace: string; messageLocalId?: string }> = [] const session = createSession({ diff --git a/hub/src/web/routes/sessions.ts b/hub/src/web/routes/sessions.ts index 5b2d4e23..8a86e2ce 100644 --- a/hub/src/web/routes/sessions.ts +++ b/hub/src/web/routes/sessions.ts @@ -370,7 +370,7 @@ export function createSessionsRoutes(getSyncEngine: () => SyncEngine | null): Ho return engine } - const sessionResult = requireSessionFromParam(c, engine, { requireActive: true }) + const sessionResult = requireSessionFromParam(c, engine) if (sessionResult instanceof Response) { return sessionResult } diff --git a/shared/src/apiTypes.ts b/shared/src/apiTypes.ts index b3b6c77a..6b619bdb 100644 --- a/shared/src/apiTypes.ts +++ b/shared/src/apiTypes.ts @@ -656,6 +656,17 @@ export type ForkConversationRpcResponse = ForkConversationRpcResult | { error: string } +/** Machine-scoped native history operation; never sends a source prompt. */ +export const ForkArchivedCodexRequestSchema = z.object({ + operationId: z.string().uuid(), + sourceSessionId: z.string().uuid(), + sourceThreadId: z.string().min(1), + directory: z.string().min(1), + boundary: z.object({ localId: z.string().min(1), turnId: z.string().min(1) }).optional() +}) +export type ForkArchivedCodexRequest = z.infer +export type ForkArchivedCodexResponse = { nativeSessionId: string } | { error: string; rollbackSafe: boolean } + export type RewindConversationErrorCode = | 'ambiguous_native_boundary' | 'ambiguous_native_boundary_fork_safe' diff --git a/shared/src/rpcMethods.ts b/shared/src/rpcMethods.ts index 3e7c1239..12e13c85 100644 --- a/shared/src/rpcMethods.ts +++ b/shared/src/rpcMethods.ts @@ -61,6 +61,7 @@ export const RPC_METHODS = { /** Deliver one queued message into the active Pi turn (native steer). */ SteerQueuedMessage: 'steer-queued-message', ForkConversation: 'fork-conversation', + ForkArchivedCodex: 'fork-archived-codex', RewindConversation: 'rewind-conversation', ClearConversation: 'clear-conversation', ImplementCodexPlan: 'implement-codex-plan', diff --git a/shared/src/runnerCapabilities.ts b/shared/src/runnerCapabilities.ts index a5e9e69f..7ae20368 100644 --- a/shared/src/runnerCapabilities.ts +++ b/shared/src/runnerCapabilities.ts @@ -29,6 +29,7 @@ export type RunnerCapabilities = typeof RUNNER_CAPABILITIES * instead of a silent fail-closed product bug. */ export const MACHINE_CAPABILITIES = { + ForkArchivedCodex: 'fork-archived-codex', AgentAvailability: RPC_METHODS.AgentAvailability, CursorChatStoreStatus: RPC_METHODS.CursorChatStoreStatus, StopRunner: RPC_METHODS.StopRunner, @@ -41,6 +42,7 @@ export type MachineCapability = /** Capabilities this CLI generation registers on the machine socket. */ export const CURRENT_MACHINE_CAPABILITIES: readonly MachineCapability[] = [ + MACHINE_CAPABILITIES.ForkArchivedCodex, MACHINE_CAPABILITIES.AgentAvailability, MACHINE_CAPABILITIES.CursorChatStoreStatus, MACHINE_CAPABILITIES.StopRunner, diff --git a/shared/src/schemas.ts b/shared/src/schemas.ts index ce247cf9..7965eeb8 100644 --- a/shared/src/schemas.ts +++ b/shared/src/schemas.ts @@ -181,6 +181,14 @@ export const MetadataSchema = z.object({ }).optional(), // Set when native rewind succeeded but HAPI truncate/hydrate failed. conversationHistoryDiverged: z.boolean().optional(), + // Durable cold-fork reservation: retries reuse the same native/Hub child. + codexArchivedFork: z.object({ + operationId: z.string().uuid(), childSessionId: z.string().uuid(), + boundaryLocalId: z.string().optional(), boundaryTurnId: z.string().optional(), + nativeSessionId: z.string().optional(), spawnAttempted: z.boolean().optional() + }).optional(), + // A timed-out resume can still activate later; retain until native ready or proven stopped. + codexResumePending: z.object({ machineId: z.string(), nativeSessionId: z.string() }).optional(), worktree: WorktreeMetadataSchema.optional(), // Cached Pi model list — written by CLI, read by web (inactive session fallback). // Minimal shape: each entry must have modelId; other fields (provider, name, etc.) pass through. diff --git a/web/e2e/codex-archived-fork.spec.ts b/web/e2e/codex-archived-fork.spec.ts new file mode 100644 index 00000000..00f20137 --- /dev/null +++ b/web/e2e/codex-archived-fork.spec.ts @@ -0,0 +1,163 @@ +import { test, expect } from '@playwright/test' +import { createServer } from 'node:http' +import { mkdir, writeFile } from 'node:fs/promises' +import { join } from 'node:path' +import { randomUUID } from 'node:crypto' +import { installHapiAuth } from './helpers/hapi-live' + +const enabled = process.env.HAPI_LIVE === '1' && process.env.HAPI_ARCHIVED_FORK_TEST === '1' + && Boolean(process.env.HAPI_FORK_TEST_DIR) && Boolean(process.env.HAPI_ACCESS_TOKEN) + +for (const mobile of [false, true]) { + test(`archived Codex session forks without reopening source (${mobile ? 'mobile' : 'desktop'})`, async ({ page, request }, testInfo) => { + test.skip(!enabled, 'Requires an explicitly isolated Full environment and CODEX_HOME') + test.setTimeout(480_000) + const base = process.env.HAPI_FORK_LOCAL_URL ?? process.env.HAPI_URL! + const runtime = process.env.HAPI_FORK_TEST_DIR! + const auth = await request.post(`${base}/api/auth`, { data: { accessToken: process.env.HAPI_ACCESS_TOKEN } }) + expect(auth.ok()).toBe(true) + const headers = { Authorization: `Bearer ${(await auth.json()).token}` } + const api = async (path: string, data?: unknown) => { + const response = data === undefined ? await request.get(`${base}/api${path}`, { headers }) + : await request.post(`${base}/api${path}`, { headers, data, timeout: 240_000 }) + expect(response.ok(), `${path}: ${await response.text()}`).toBe(true) + return response.json() + } + const run = randomUUID() + const retained = `Remembered project context ${run}` + const excluded = `Alternative requirement ${run}` + const probe = `Child context probe ${run}` + const inputs: string[] = [] + const provider = createServer((incoming, response) => { + const chunks: Buffer[] = [] + incoming.on('data', chunk => chunks.push(Buffer.from(chunk))) + incoming.on('end', () => { + if (!incoming.url?.endsWith('/responses')) { response.writeHead(404).end(); return } + const input = Buffer.concat(chunks).toString() + inputs.push(input) + const id = randomUUID() + const text = input.includes(probe) ? 'Branch continued with the retained context.' + : input.includes(excluded) ? 'Alternative requirement recorded.' : 'Project context recorded.' + const events = [ + { type: 'response.created', response: { id } }, + { type: 'response.output_item.done', item: { id: `m_${id}`, type: 'message', role: 'assistant', content: [{ type: 'output_text', text }] } }, + { type: 'response.completed', response: { id, usage: { input_tokens: 12, output_tokens: 6, total_tokens: 18 } } } + ] + response.writeHead(200, { 'Content-Type': 'text/event-stream' }) + response.end(events.map(event => `event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`).join('')) + }) + }) + const sessions: string[] = [] + try { + await new Promise(resolve => provider.listen(0, '127.0.0.1', resolve)) + const port = (provider.address() as { port: number }).port + const home = join(runtime, 'codex-home') + await mkdir(home, { recursive: true }) + await writeFile(join(home, 'config.toml'), `model = "mock-model"\nmodel_provider = "fork_test"\napproval_policy = "never"\nsandbox_mode = "read-only"\n[model_providers.fork_test]\nname = "Archived fork acceptance"\nbase_url = "http://127.0.0.1:${port}/v1"\nwire_api = "responses"\nrequires_openai_auth = false\nsupports_websockets = false\nrequest_max_retries = 0\nstream_max_retries = 0\n[analytics]\nenabled = false\n[feedback]\nenabled = false\n`) + const cwd = join(runtime, `work-${run}`) + await mkdir(cwd, { recursive: true }) + await expect.poll(async () => (await api('/machines')).machines.length, { timeout: 90_000 }).toBe(1) + const { machines } = await api('/machines') + const spawn = await api(`/machines/${machines[0].id}/spawn`, { directory: cwd, agent: 'codex', model: 'mock-model', permissionMode: 'read-only' }) + // Source creation is fixture setup. A slow machine can confirm the + // original reserved row after the general spawn RPC has timed out. + let source = spawn.type === 'success' ? spawn.sessionId as string : undefined + if (!source) { + console.log(`Source fixture startup acknowledgement: ${JSON.stringify(spawn)}`) + await expect.poll(async () => { + const list = await api('/sessions') + source = list.sessions.find((session: { id: string; active: boolean; metadata?: { path?: string } }) => + session.active && session.metadata?.path === cwd)?.id + return Boolean(source) + }, { timeout: 90_000 }).toBe(true) + } + if (!source) throw new Error('Source fixture did not become ready') + sessions.push(source) + const detail = async (id: string) => (await api(`/sessions/${id}`)).session + const messages = async (id: string) => JSON.stringify(await api(`/sessions/${id}/messages?limit=200`)) + for (const [text, answer] of [[retained, 'Project context recorded.'], [excluded, 'Alternative requirement recorded.']]) { + await api(`/sessions/${source}/messages`, { localId: text, text }) + await expect.poll(() => messages(source), { timeout: 40_000 }).toContain(answer) + await expect.poll(async () => (await detail(source)).thinking).toBe(false) + } + const originalNative = (await detail(source)).metadata.codexSessionId + // Windows can report a late wrapper-shutdown error after native + // archive completed. Require the resulting archived/inactive state. + const archive = await request.post(`${base}/api/sessions/${source}/archive`, { headers, data: {}, timeout: 150_000 }) + await expect.poll(async () => (await detail(source)).active).toBe(false) + expect((await detail(source)).metadata.lifecycleState).toBe('archived') + console.log(`Source archive HTTP status: ${archive.status()}`) + // Archival can finish before Windows process-stop verification. + // Establish a responsive Runner before exercising the new feature. + await expect.poll(async () => { + try { + const ready = await request.post(`${base}/api/machines/${machines[0].id}/paths/exists`, + { headers, data: { paths: [cwd] }, timeout: 5_000 }) + return ready.ok() + } catch { return false } + }, { timeout: 90_000, intervals: [2_000] }).toBe(true) + const originalHistory = await messages(source) + const requestsBeforeFork = inputs.length + await installHapiAuth(page, base, process.env.HAPI_ACCESS_TOKEN!) + await page.setViewportSize(mobile ? { width: 390, height: 844 } : { width: 1440, height: 1000 }) + await page.goto(`${base}/sessions/${source}`) + const boundary = page.locator('[data-hapi-message-role="user"]').filter({ hasText: excluded }) + await expect(boundary).toBeVisible() + const tourDismiss = page.getByRole('button', { name: 'Got it', exact: true }) + if (await tourDismiss.isVisible()) await tourDismiss.click() + await boundary.hover() + await expect(boundary.getByRole('button', { name: 'Fork', exact: true })).toBeVisible() + await expect(boundary.getByRole('button', { name: 'Rewind', exact: true })).toHaveCount(0) + const screenshots = join(runtime, 'screenshots') + await mkdir(screenshots, { recursive: true }) + const device = `${testInfo.project.name || 'chromium'}-${mobile ? 'mobile' : 'desktop'}` + await page.screenshot({ path: join(screenshots, `${device}-01-archived-source.png`) }) + await boundary.getByRole('button', { name: 'Fork', exact: true }).click() + await page.screenshot({ path: join(screenshots, `${device}-02-confirm-fork.png`) }) + const pending = page.waitForResponse(response => response.url().endsWith(`/sessions/${source}/fork`) + && response.request().method() === 'POST', { timeout: 240_000 }) + await page.getByRole('dialog').getByRole('button', { name: 'Fork', exact: true }).click() + const response = await pending + expect(response.ok(), await response.text()).toBe(true) + const child = (await response.json()).sessionId as string + sessions.push(child) + await expect(page).toHaveURL(new RegExp(`/sessions/${child}$`)) + const childSession = await detail(child) + expect(childSession.active).toBe(true) + expect(childSession.metadata.codexSessionId).not.toBe(originalNative) + expect((await detail(source)).metadata).toMatchObject({ lifecycleState: 'archived', codexSessionId: originalNative }) + expect((await detail(source)).active).toBe(false) + expect(await messages(source)).toBe(originalHistory) + expect(inputs).toHaveLength(requestsBeforeFork) + const childHistory = await messages(child) + expect(childHistory).toContain(retained) + expect(childHistory).not.toContain(excluded) + await api(`/sessions/${child}/messages`, { text: probe, localId: probe }) + await expect.poll(() => messages(child), { timeout: 40_000 }).toContain('Branch continued with the retained context.') + const childInput = inputs.find(input => input.includes(probe))! + expect(childInput).toContain(retained) + expect(childInput).not.toContain(excluded) + await expect(page.getByText('Branch continued with the retained context.', { exact: true })).toBeVisible() + await expect.poll(async () => (await detail(child)).thinking).toBe(false) + for (const dismiss of await page.getByRole('button', { name: 'Dismiss', exact: true }).all()) { + if (await dismiss.isVisible()) await dismiss.click() + } + await page.screenshot({ path: join(screenshots, `${device}-03-independent-child.png`) }) + // A subsequent full-history fork does not reuse the previous branch. + const full = await api(`/sessions/${source}/fork`, {}) + sessions.push(full.sessionId) + expect(full.sessionId).not.toBe(child) + const fullHistory = await messages(full.sessionId) + expect(fullHistory).toContain(retained) + expect(fullHistory).toContain(excluded) + await writeFile(join(runtime, `${device}-archived-fork-evidence.json`), JSON.stringify({ source, sourceNative: originalNative, + child, childNative: childSession.metadata.codexSessionId, fullChild: full.sessionId, + sourceArchived: (await detail(source)).metadata.lifecycleState === 'archived', modelCallsDuringFork: 0, + retainedInherited: childInput.includes(retained), excludedInherited: childInput.includes(excluded) }, null, 2)) + } finally { + for (const id of sessions.reverse()) await api(`/sessions/${id}/archive`, {}).catch(() => {}) + provider.closeAllConnections() + await new Promise(resolve => provider.close(() => resolve())) + } + }) +} diff --git a/web/playwright.live.config.ts b/web/playwright.live.config.ts index 739a9fd3..b0f64c39 100644 --- a/web/playwright.live.config.ts +++ b/web/playwright.live.config.ts @@ -5,7 +5,13 @@ const chromePath = process.env.PLAYWRIGHT_CHROME_PATH /** Live hub session tests — no Vite; hits HAPI_URL with HAPI_LIVE=1. */ export default defineConfig({ testDir: './e2e', - testMatch: ['mermaid-lightbox-session.spec.ts', 'latex-bracket-math-session.spec.ts', 'codex-fork-while-running.spec.ts', 'late-codex-command-session.spec.ts'], + testMatch: [ + 'mermaid-lightbox-session.spec.ts', + 'latex-bracket-math-session.spec.ts', + 'codex-fork-while-running.spec.ts', + 'codex-archived-fork.spec.ts', + 'late-codex-command-session.spec.ts', + ], timeout: 120_000, expect: { timeout: 20_000 }, fullyParallel: false, diff --git a/web/src/components/AssistantChat/HappyThread.tsx b/web/src/components/AssistantChat/HappyThread.tsx index ecfcd08b..1a3e0a18 100644 --- a/web/src/components/AssistantChat/HappyThread.tsx +++ b/web/src/components/AssistantChat/HappyThread.tsx @@ -533,6 +533,7 @@ export function HappyThread(props: { sessionId: string metadata: SessionMetadataSummary | null disabled: boolean + allowInactiveFork?: boolean onRefresh: () => void onContinuePlan?: () => void onRetryMessage?: (localId: string) => void @@ -1773,6 +1774,7 @@ export function HappyThread(props: { onContinuePlan: props.onContinuePlan, onRetryMessage: props.onRetryMessage, historyActionPending: props.historyActionPending, + allowInactiveFork: props.allowInactiveFork, onForkConversation: props.onForkConversation, onRewindConversation: props.onRewindConversation, isLatestCompletedBoundary: props.isLatestCompletedBoundary, diff --git a/web/src/components/AssistantChat/context.tsx b/web/src/components/AssistantChat/context.tsx index 7b651985..3ddd09ec 100644 --- a/web/src/components/AssistantChat/context.tsx +++ b/web/src/components/AssistantChat/context.tsx @@ -14,6 +14,7 @@ export type HappyChatContextValue = { /** Hub-wide AGENT_NOTIFY_SUMMARY chat display; polled once at chat shell. */ showSessionSummaryInChat: boolean disabled: boolean + allowInactiveFork?: boolean onRefresh: () => void codexPlanProposalId?: string | null onContinuePlan?: (planId: string) => void diff --git a/web/src/components/AssistantChat/messages/AssistantMessage.tsx b/web/src/components/AssistantChat/messages/AssistantMessage.tsx index 4db9dd74..9bd8fbf3 100644 --- a/web/src/components/AssistantChat/messages/AssistantMessage.tsx +++ b/web/src/components/AssistantChat/messages/AssistantMessage.tsx @@ -62,9 +62,9 @@ export function HappyAssistantMessage() { const history = ctx.metadata?.capabilities?.conversationHistory const showForkCurrent = Boolean( - history?.forkCurrent + (history?.forkCurrent || ctx.allowInactiveFork) && ctx.isLatestCompletedBoundary?.(messageId) - && !ctx.disabled + && (!ctx.disabled || ctx.allowInactiveFork === true) && ctx.onForkConversation ) diff --git a/web/src/components/AssistantChat/messages/UserMessage.test.tsx b/web/src/components/AssistantChat/messages/UserMessage.test.tsx index 5c5a9e85..76506e71 100644 --- a/web/src/components/AssistantChat/messages/UserMessage.test.tsx +++ b/web/src/components/AssistantChat/messages/UserMessage.test.tsx @@ -15,7 +15,7 @@ const state = { const ctx = { metadata: { flavor: 'codex', capabilities: { conversationHistory: { forkCurrent: true, forkAtMessage: true, rewindToMessage: true } }, conversationHistoryPoints: { 'local-interrupted': true } as Record }, - disabled: false, historyActionPending: false, + disabled: false, historyActionPending: false, allowInactiveFork: false, isLatestCompletedBoundary: (_id: string) => true, onForkConversation: onFork, onRewindConversation: async () => {} } @@ -54,6 +54,7 @@ describe('user-only completed fork boundary', () => { ctx.metadata.capabilities.conversationHistory.forkAtMessage = true ctx.metadata.conversationHistoryPoints = { 'local-interrupted': true } ctx.disabled = false + ctx.allowInactiveFork = false ctx.historyActionPending = false }) @@ -77,6 +78,28 @@ describe('user-only completed fork boundary', () => { expect(onFork.mock.calls[0]).toEqual([]) }) + it('offers an archived Codex fork while leaving rewind disabled', async () => { + state.thread.isRunning = false + ctx.disabled = true + ctx.allowInactiveFork = true + mount() + expect(screen.queryByRole('button', { name: 'Rewind' })).toBeNull() + await confirmFork() + expect(onFork.mock.calls[0]).toEqual([]) + }) + + it('uses inactive-fork support when cached live capabilities no longer advertise fork', async () => { + state.thread.isRunning = false + ctx.disabled = true + ctx.allowInactiveFork = true + ctx.metadata.capabilities.conversationHistory.forkCurrent = false + ctx.metadata.capabilities.conversationHistory.forkAtMessage = false + try { + mount() + await confirmFork() + } finally { ctx.metadata.capabilities.conversationHistory.forkCurrent = true } + }) + it.each(['claude', 'pi', 'agy', 'dsh'])('does not unlock a running %s current fork', flavor => { ctx.metadata.flavor = flavor mount() diff --git a/web/src/components/AssistantChat/messages/UserMessage.tsx b/web/src/components/AssistantChat/messages/UserMessage.tsx index aa0c1d2a..e5b59cdf 100644 --- a/web/src/components/AssistantChat/messages/UserMessage.tsx +++ b/web/src/components/AssistantChat/messages/UserMessage.tsx @@ -52,11 +52,14 @@ export function HappyUserMessage() { const onRetry = canRetry ? () => ctx.onRetryMessage!(localId) : undefined const showStatus = shouldShowMessageStatus(status) - const history = ctx.metadata?.capabilities?.conversationHistory + const history = ctx.allowInactiveFork + ? { forkCurrent: true, forkAtMessage: true, rewindToMessage: false } + : ctx.metadata?.capabilities?.conversationHistory const hasNativePoint = typeof localId === 'string' && localId.length > 0 && ctx.metadata?.conversationHistoryPoints?.[localId] === true const isLatestBoundary = ctx.isLatestCompletedBoundary?.(messageId) === true + const forkEnabled = !ctx.disabled || ctx.allowInactiveFork === true // A user-only finished turn can be the latest completed boundary while // another turn runs. Branch before that user turn, never from the live tail. const runningCodex = threadIsRunning && ctx.metadata?.flavor === 'codex' @@ -64,14 +67,14 @@ export function HappyUserMessage() { history?.forkCurrent && isLatestBoundary && !runningCodex - && !ctx.disabled + && forkEnabled && ctx.onForkConversation ) const showHistoricalFork = Boolean( history?.forkAtMessage && hasNativePoint && (!isLatestBoundary || runningCodex) - && !ctx.disabled + && forkEnabled && ctx.onForkConversation ) const showFork = showCurrentFork || showHistoricalFork diff --git a/web/src/components/SessionChat.tsx b/web/src/components/SessionChat.tsx index d1d48919..09d4448e 100644 --- a/web/src/components/SessionChat.tsx +++ b/web/src/components/SessionChat.tsx @@ -2091,11 +2091,13 @@ function SessionChatInner(props: SessionChatProps) { sessionId={props.session.id} metadata={props.session.metadata} disabled={sessionInactive} + allowInactiveFork={sessionInactive && props.session.metadata?.flavor === 'codex' + && Boolean(props.session.metadata?.codexSessionId && props.session.metadata?.machineId)} onRefresh={props.onRefresh} onRetryMessage={props.onRetryMessage} onContinuePlan={() => focusComposerRef.current?.()} historyActionPending={historyActionPending} - onForkConversation={controlledByUser ? undefined : onForkConversation} + onForkConversation={controlledByUser && !sessionInactive ? undefined : onForkConversation} onRewindConversation={controlledByUser ? undefined : onRewindConversation} isLatestCompletedBoundary={isLatestCompletedBoundary} onViewModeChange={props.onViewModeChange}