From 0aec975434f71058d0d48f7cbd3253979f588e20 Mon Sep 17 00:00:00 2001 From: Ananovo Date: Wed, 28 Oct 2026 12:06:08 +0800 Subject: [PATCH] fix(codex): gate history actions until native thread is ready (#1604) * fix(codex): gate history actions until native thread is ready * fix(codex): clear history capabilities during bootstrap * style(codex): normalize CLI file line endings * fix(codex): await history capability persistence * fix(codex): restore goal status coverage * fix(codex): await bootstrap metadata persistence * chore: retrigger PR review * fix(codex): scope metadata readiness flush * fix(codex): gate metadata flush by stale history --- cli/src/agent/sessionFactory.test.ts | 73 ++++++++++++++++++- cli/src/agent/sessionFactory.ts | 4 + cli/src/codex/shared/runtime.ts | 12 ++- hub/src/sync/conversationHistoryCodex.test.ts | 40 ++++++++++ hub/src/sync/rpcGateway.ts | 4 +- hub/src/sync/syncEngine.ts | 17 ++++- shared/src/apiTypes.ts | 6 ++ 7 files changed, 147 insertions(+), 9 deletions(-) create mode 100644 hub/src/sync/conversationHistoryCodex.test.ts diff --git a/cli/src/agent/sessionFactory.test.ts b/cli/src/agent/sessionFactory.test.ts index 7f0aed55..3ac471e7 100644 --- a/cli/src/agent/sessionFactory.test.ts +++ b/cli/src/agent/sessionFactory.test.ts @@ -102,10 +102,11 @@ describe('bootstrapExistingSession', () => { delete process.env[HAPI_SESSION_ID_ENV] }) - it('loads an existing HAPI session and reports it to the runner', async () => { + it('does not wait for metadata when an existing session has no stale history capabilities', async () => { const session = createSession() const sessionClient = { - updateMetadata: vi.fn() + updateMetadata: vi.fn(), + flushMetadata: vi.fn(async () => false) } getSessionMock.mockResolvedValue(session) getOrCreateMachineMock.mockResolvedValue({ id: 'machine-1' }) @@ -115,7 +116,8 @@ describe('bootstrapExistingSession', () => { const result = await bootstrapExistingSession({ sessionId: 'hapi-session-1', flavor: 'codex', - workingDirectory: '/tmp/project' + workingDirectory: '/tmp/project', + requireMetadataFlush: metadata => Boolean(metadata?.capabilities?.conversationHistory) }) expect(result.sessionInfo.id).toBe('hapi-session-1') @@ -123,6 +125,7 @@ describe('bootstrapExistingSession', () => { expect(result.workingDirectory).toBe('/tmp/project') expect(sessionSyncClientMock).toHaveBeenCalledWith(session) expect(sessionClient.updateMetadata).toHaveBeenCalledOnce() + expect(sessionClient.flushMetadata).not.toHaveBeenCalled() expect(notifyRunnerSessionStartedMock).toHaveBeenCalledWith( 'hapi-session-1', expect.objectContaining({ @@ -229,7 +232,8 @@ describe('bootstrapExistingSession', () => { } } const sessionClient = { - updateMetadata: vi.fn() + updateMetadata: vi.fn(), + flushMetadata: vi.fn(async () => true) } getSessionMock.mockResolvedValue(session) getOrCreateMachineMock.mockResolvedValue({ id: 'machine-1' }) @@ -291,6 +295,67 @@ describe('bootstrapExistingSession', () => { ) }) + it('clears stale history capabilities before reporting a Codex session as started', async () => { + const session = createSession() + const existingMetadata = session.metadata + if (!existingMetadata) throw new Error('expected test session metadata') + + session.metadata = { + ...existingMetadata, + capabilities: { + terminal: true, + conversationHistory: { forkCurrent: true } + } + } + let releaseMetadataFlush: (() => void) | undefined + let resolveMetadataFlushStarted: (() => void) | undefined + const metadataFlushStarted = new Promise((resolve) => { + resolveMetadataFlushStarted = resolve + }) + const sessionClient = { + updateMetadata: vi.fn(), + flushMetadata: vi.fn(async () => { + resolveMetadataFlushStarted?.() + await new Promise((resolve) => { + releaseMetadataFlush = resolve + }) + return true + }) + } + getSessionMock.mockResolvedValue(session) + getOrCreateMachineMock.mockResolvedValue({ id: 'machine-1' }) + sessionSyncClientMock.mockReturnValue(sessionClient) + readSettingsMock.mockResolvedValue({ machineId: 'machine-1' }) + + const bootstrapPromise = bootstrapExistingSession({ + sessionId: 'hapi-session-1', + flavor: 'codex', + workingDirectory: '/tmp/project', + metadataOverrides: { + capabilities: { + terminal: true, + conversationHistory: undefined + } + }, + requireMetadataFlush: metadata => Boolean(metadata?.capabilities?.conversationHistory) + }) + await metadataFlushStarted + expect(notifyRunnerSessionStartedMock).not.toHaveBeenCalled() + releaseMetadataFlush!() + const result = await bootstrapPromise + + expect(result.metadata.capabilities).toEqual({ terminal: true }) + const updateHandler = sessionClient.updateMetadata.mock.calls[0][0] + expect(updateHandler(session.metadata).capabilities).toEqual({ terminal: true }) + expect(sessionClient.flushMetadata).toHaveBeenCalledOnce() + expect(notifyRunnerSessionStartedMock).toHaveBeenCalledWith( + 'hapi-session-1', + expect.objectContaining({ + capabilities: { terminal: true } + }) + ) + }) + it('advertises remote terminal capability in session metadata', () => { const metadata = buildSessionMetadata({ flavor: 'codex', diff --git a/cli/src/agent/sessionFactory.ts b/cli/src/agent/sessionFactory.ts index 82cac20b..cffaedcb 100644 --- a/cli/src/agent/sessionFactory.ts +++ b/cli/src/agent/sessionFactory.ts @@ -378,6 +378,7 @@ export async function bootstrapExistingSession(options: { startedBy?: SessionStartedBy workingDirectory: string metadataOverrides?: Partial + requireMetadataFlush?: (metadata: Metadata | null | undefined) => boolean }): Promise { const startedBy = options.startedBy ?? 'terminal' const api = await ApiClient.create() @@ -424,6 +425,9 @@ export async function bootstrapExistingSession(options: { const session = api.sessionSyncClient(sessionInfo) session.updateMetadata(buildUpdatedMetadata) + if (options.requireMetadataFlush?.(sessionInfo.metadata) && !await session.flushMetadata()) { + throw new Error('Unable to persist existing-session metadata') + } if (options.exportSessionEnv !== false) exportHapiSessionEnv(sessionInfo.id) diff --git a/cli/src/codex/shared/runtime.ts b/cli/src/codex/shared/runtime.ts index 4c178333..80cdf3c8 100644 --- a/cli/src/codex/shared/runtime.ts +++ b/cli/src/codex/shared/runtime.ts @@ -170,13 +170,21 @@ export async function runSharedRuntime(options: SharedLaunchOptions, onReady?: ( const prepare = async (cwd: string, existingSessionId?: string, parent?: SharedCodexRoot): Promise => { assertRunning(); const shared = { flavor: 'codex', startedBy: options.startedBy ?? 'terminal', workingDirectory: cwd, - exportSessionEnv: false, reportStarted: false, metadataOverrides: { capabilities: { terminal: true, concurrentClients: true }, + exportSessionEnv: false, reportStarted: false, metadataOverrides: { capabilities: { + terminal: true, + concurrentClients: true, + ...(existingSessionId ? { conversationHistory: undefined } : {}) + }, ...(parent ? { forkedFrom: parent.session.sessionId } : {}) } } as const; // reservedSessionId names one preallocated hub row — single-use. A second // create()/fork must mint a fresh row, not re-adopt (#1911 Opus Major). const reservedSessionId = takeReservedSessionId(options); const bootstrap = existingSessionId - ? await bootstrapExistingSession({ ...shared, sessionId: existingSessionId }) + ? await bootstrapExistingSession({ + ...shared, + sessionId: existingSessionId, + requireMetadataFlush: metadata => Boolean(metadata?.capabilities?.conversationHistory) + }) : await bootstrapSession({ ...shared, reservedSessionId, diff --git a/hub/src/sync/conversationHistoryCodex.test.ts b/hub/src/sync/conversationHistoryCodex.test.ts new file mode 100644 index 00000000..60c03c6f --- /dev/null +++ b/hub/src/sync/conversationHistoryCodex.test.ts @@ -0,0 +1,40 @@ +import { describe, expect, it } from 'bun:test' +import { Store } from '../store' +import { RpcRegistry } from '../socket/rpcRegistry' +import { SyncEngine } from './syncEngine' + +function createEngine() { + const store = new Store(':memory:') + const engine = new SyncEngine(store, {} as never, new RpcRegistry(), { broadcast() {} } as never) + return { store, engine } +} + +describe('Codex conversation-history hub integration', () => { + it('forwards serialized Fork RPC errors instead of using the missing-id fallback', async () => { + const { store, engine } = createEngine() + try { + const session = engine.getOrCreateSession('codex-fork-not-ready', { + path: '/tmp/project', + host: 'localhost', + machineId: 'machine-1', + flavor: 'codex', + codexSessionId: 'native-thread', + capabilities: { conversationHistory: { forkCurrent: true } } + }, null, 'default') + engine.handleSessionAlive({ sid: session.id, time: Date.now(), mode: 'remote' }) + store.messages.addMessage(session.id, { role: 'user', content: 'boundary' }, 'local-boundary') + store.messages.markMessagesInvoked(session.id, ['local-boundary'], Date.now()) + + ;(engine as any).rpcGateway.forkConversation = async () => ({ + error: 'Codex thread is not ready' + }) + + await expect(engine.forkConversation(session.id, 'default')).resolves.toEqual({ + type: 'error', + message: 'Codex thread is not ready' + }) + } finally { + engine.stop() + } + }) +}) diff --git a/hub/src/sync/rpcGateway.ts b/hub/src/sync/rpcGateway.ts index f8d93af7..acd01af0 100644 --- a/hub/src/sync/rpcGateway.ts +++ b/hub/src/sync/rpcGateway.ts @@ -573,13 +573,13 @@ export class RpcGateway { async forkConversation( sessionId: string, params: { messageLocalId?: string } - ): Promise { + ): Promise { return await this.sessionRpc( sessionId, RPC_METHODS.ForkConversation, params, 120_000 - ) as import('@hapi/protocol/apiTypes').ForkConversationRpcResult + ) as import('@hapi/protocol/apiTypes').ForkConversationRpcResponse } async clearConversation(sessionId: string): Promise<{ sessionId: string }> { diff --git a/hub/src/sync/syncEngine.ts b/hub/src/sync/syncEngine.ts index 64d20e12..2e4a3087 100644 --- a/hub/src/sync/syncEngine.ts +++ b/hub/src/sync/syncEngine.ts @@ -1478,7 +1478,22 @@ export class SyncEngine { return { type: 'error', message: error instanceof Error ? error.message : String(error) } } - if (!rpcResult?.nativeSessionId) { + if ( + rpcResult && + typeof rpcResult === 'object' && + 'error' in rpcResult && + typeof rpcResult.error === 'string' && + rpcResult.error.trim() + ) { + return { type: 'error', message: rpcResult.error } + } + + if ( + !rpcResult || + typeof rpcResult !== 'object' || + !('nativeSessionId' in rpcResult) || + !rpcResult.nativeSessionId + ) { return { type: 'error', message: 'Native fork did not return a session id' } } if (rpcResult.sessionId) { diff --git a/shared/src/apiTypes.ts b/shared/src/apiTypes.ts index 18997ec9..4e8faa6c 100644 --- a/shared/src/apiTypes.ts +++ b/shared/src/apiTypes.ts @@ -602,6 +602,12 @@ export type ForkConversationRpcResult = { forkSession?: boolean } +/** Raw RPC response, including generic serialized handler failures. */ +export type ForkConversationRpcResponse = ForkConversationRpcResult | { + /** Serialized CLI handler failure returned through the generic RPC layer. */ + error: string +} + export type RewindConversationErrorCode = | 'ambiguous_native_boundary' | 'ambiguous_native_boundary_fork_safe'