From 12ecb30da8feaf43dce390b3eb845c7ed8bdedc2 Mon Sep 17 00:00:00 2001 From: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com> Date: Thu, 8 Oct 2026 09:14:17 +0000 Subject: [PATCH] fix: address Codex review on PR #3604 --- application/state/useAIState.ts | 9 +- .../toolOutputPersistenceSetup.test.ts | 51 ++++++++++ .../ai/harness/toolOutputPersistenceSetup.ts | 92 +++++++++++++++++++ .../ai/harness/toolOutputStore.test.ts | 34 +++++++ infrastructure/ai/harness/toolOutputStore.ts | 24 ++++- .../ai/harness/turnDrivers/cattyTurnDriver.ts | 67 +------------- 6 files changed, 208 insertions(+), 69 deletions(-) create mode 100644 infrastructure/ai/harness/toolOutputPersistenceSetup.test.ts create mode 100644 infrastructure/ai/harness/toolOutputPersistenceSetup.ts diff --git a/application/state/useAIState.ts b/application/state/useAIState.ts index 267662f91..f2b31293b 100644 --- a/application/state/useAIState.ts +++ b/application/state/useAIState.ts @@ -57,6 +57,7 @@ import { removeProviderReferences } from './aiProviderCleanup'; import { publishAISessionsSnapshot } from './aiSessionsStore'; import { collectForkHandleIds, planSessionFork } from '../../domain/aiSessionFork'; import { getAgentRuntime } from '../../infrastructure/ai/harness/globalAgentRuntime'; +import { installToolOutputPersistence } from '../../infrastructure/ai/harness/toolOutputPersistenceSetup'; import { AI_STATE_CHANGED_DRAFTS_BY_SCOPE, AI_STATE_CHANGED_PANEL_VIEW_BY_SCOPE, @@ -725,7 +726,13 @@ export function useAIState() { // active until those records exist under its own session id, or the very // first turn could see handles as missing. try { - await getAgentRuntime().getToolOutputStore(sessionId).rehomeChatSession( + const store = getAgentRuntime().getToolOutputStore(sessionId); + // The persistence adapter is normally configured when a turn starts; + // forking can be the very first action after an app restart, so + // configure it here too or retained handles cannot be restored from + // durable storage before rehoming. + await installToolOutputPersistence(store); + await store.rehomeChatSession( sessionId, fork.id, collectForkHandleIds(plan.messages), diff --git a/infrastructure/ai/harness/toolOutputPersistenceSetup.test.ts b/infrastructure/ai/harness/toolOutputPersistenceSetup.test.ts new file mode 100644 index 000000000..e669c537e --- /dev/null +++ b/infrastructure/ai/harness/toolOutputPersistenceSetup.test.ts @@ -0,0 +1,51 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { getNetcattyBridge } from '../aiChatStreamingSupport'; +import { ToolOutputStore } from './toolOutputStore'; +import { installToolOutputPersistence } from './toolOutputPersistenceSetup'; + +type BridgeStub = NonNullable>; + +test('installToolOutputPersistence configures the durable adapter from bridge methods', async () => { + const files = new Map(); + const store = new ToolOutputStore(); + await installToolOutputPersistence(store, { + getToolOutputPersistenceStatus: async () => ({ durable: true }), + writeToolOutputTemp: async (_record, content) => { + files.set('/netcatty/temp.log', content); + return { ok: true, path: '/netcatty/temp.log' }; + }, + restoreToolOutputTemp: async () => null, + readToolOutputTemp: async path => { + const content = files.get(path); + if (content == null) return null; + return { + mode: 'head', + content, + totalChars: content.length, + startOffset: 0, + endOffset: content.length, + nextOffset: content.length, + hasMore: false, + }; + }, + deleteToolOutputTemp: async path => { + files.delete(path); + return { ok: true }; + }, + } as unknown as BridgeStub); + + const handle = store.store({ chatSessionId: 'chat-1', capabilityId: 'test', content: 'durable please' }); + await handle.spillPromise; + assert.equal(files.get('/netcatty/temp.log'), 'durable please'); +}); + +test('installToolOutputPersistence clears the adapter when the bridge cannot persist', async () => { + const store = new ToolOutputStore(); + await installToolOutputPersistence(store, {} as unknown as BridgeStub); + const handle = store.store({ chatSessionId: 'chat-1', capabilityId: 'test', content: 'memory only' }); + await handle.spillPromise; + // Without a temp-output bridge there is no adapter: the handle stays in + // memory only, matching the no-adapater behavior before any turn starts. + assert.equal(handle.filePath, undefined); +}); diff --git a/infrastructure/ai/harness/toolOutputPersistenceSetup.ts b/infrastructure/ai/harness/toolOutputPersistenceSetup.ts new file mode 100644 index 000000000..0f9db5a72 --- /dev/null +++ b/infrastructure/ai/harness/toolOutputPersistenceSetup.ts @@ -0,0 +1,92 @@ +import { getNetcattyBridge } from '../aiChatStreamingSupport'; +import type { + PersistedToolOutputRecord, + ReadToolOutputInput, + ToolOutputReadResult, + ToolOutputStore, +} from './toolOutputStore'; + +type NetcattyBridge = NonNullable>; + +type ToolOutputTempBridge = NetcattyBridge & { + getToolOutputPersistenceStatus?: () => Promise<{ durable: boolean; reason?: string }>; + writeToolOutputTemp?: ( + record: PersistedToolOutputRecord, + content: string, + ) => Promise<{ ok: boolean; path?: string; error?: string }>; + restoreToolOutputTemp?: ( + handleId: string, + chatSessionId: string, + ) => Promise<{ path: string; record: PersistedToolOutputRecord } | null>; + readToolOutputTemp?: ( + path: string, + request: ReadToolOutputInput, + ) => Promise | null>; + deleteToolOutputTemp?: (path: string) => Promise<{ ok: boolean }>; + deleteChatToolOutputsTemp?: (chatSessionId: string) => Promise<{ deletedCount: number }>; + deleteTerminalToolOutputsTemp?: ( + chatSessionId: string, + terminalSessionId: string, + ) => Promise<{ deletedCount: number }>; +}; + +/** + * Configure the durable tool-output persistence adapter on the shared + * `ToolOutputStore` from the netcatty bridge's temp-output methods. + * + * Turn drivers call this when a turn starts, so callers that can run before + * any turn (e.g. the "Fork from here" flow right after an app restart) must + * call it too — otherwise `restore` stays unset and rehoming cannot pull + * retained handles back from durable storage. + */ +export async function installToolOutputPersistence( + store: ToolOutputStore, + bridge?: NetcattyBridge, +): Promise { + const toolOutputTempBridge = (bridge ?? getNetcattyBridge()) as ToolOutputTempBridge | undefined; + const persistenceStatus: { durable: boolean; reason?: string } | undefined = + await toolOutputTempBridge?.getToolOutputPersistenceStatus?.() + .catch(() => ({ durable: false })); + if ( + toolOutputTempBridge?.writeToolOutputTemp + && toolOutputTempBridge.readToolOutputTemp + && toolOutputTempBridge.deleteToolOutputTemp + ) { + store.setPersistence?.({ + write: async (record, content) => { + if (!persistenceStatus?.durable) { + throw new Error(persistenceStatus?.reason || 'Secure local storage is unavailable.'); + } + const result = await toolOutputTempBridge.writeToolOutputTemp!(record, content); + if (!result.ok || !result.path) { + throw new Error(result.error || 'Unable to persist tool output.'); + } + return result.path; + }, + restore: persistenceStatus?.durable && toolOutputTempBridge.restoreToolOutputTemp + ? (handleId, chatSessionId) => toolOutputTempBridge.restoreToolOutputTemp!(handleId, chatSessionId) + : undefined, + read: (path, request) => toolOutputTempBridge.readToolOutputTemp!(path, request), + delete: async path => { + await toolOutputTempBridge.deleteToolOutputTemp!(path); + }, + deleteSession: toolOutputTempBridge.deleteChatToolOutputsTemp + ? async chatSessionId => { + await toolOutputTempBridge.deleteChatToolOutputsTemp!(chatSessionId); + } + : undefined, + deleteTerminalSession: toolOutputTempBridge.deleteTerminalToolOutputsTemp + ? async (chatSessionId, terminalSessionId) => { + await toolOutputTempBridge.deleteTerminalToolOutputsTemp!(chatSessionId, terminalSessionId); + } + : undefined, + deleteTerminalEverywhere: toolOutputTempBridge.deleteTerminalToolOutputsEverywhereTemp + ? async terminalSessionId => { + await toolOutputTempBridge.deleteTerminalToolOutputsEverywhereTemp!(terminalSessionId); + } + : undefined, + }); + } else { + store.setPersistence?.(undefined); + } +} diff --git a/infrastructure/ai/harness/toolOutputStore.test.ts b/infrastructure/ai/harness/toolOutputStore.test.ts index 9eda42fcf..3e25bf1fe 100644 --- a/infrastructure/ai/harness/toolOutputStore.test.ts +++ b/infrastructure/ai/harness/toolOutputStore.test.ts @@ -54,6 +54,40 @@ test('ToolOutputStore rehomes handles into a forked chat session namespace', asy assert.equal(store.read({ handleId: 'tool-output-none' }, 'chat-fork'), null); }); +test('ToolOutputStore clones only the handles referenced by the retained prefix', async () => { + const store = new ToolOutputStore({ + maxHandlesGlobal: 4, + maxCharsGlobal: 200, + }); + const retained = store.store({ + chatSessionId: 'chat-1', + capabilityId: 'terminal.execute', + content: 'A'.repeat(30), + }); + const discarded = store.store({ + chatSessionId: 'chat-1', + capabilityId: 'terminal.execute', + content: 'B'.repeat(30), + }); + const unrelated = store.store({ + chatSessionId: 'chat-other', + capabilityId: 'terminal.execute', + content: 'C'.repeat(30), + }); + + await store.rehomeChatSession('chat-1', 'chat-fork', [retained.id]); + + // Only the retained handle reaches the fork; the clone must not consume the + // shared quota for outputs the fork never references. + assert.ok(store.get(retained.id, 'chat-fork')); + assert.equal(store.get(discarded.id, 'chat-fork'), undefined); + // The source session keeps both of its handles, and unrelated sessions are + // not evicted to make room for duplicated output. + assert.ok(store.get(retained.id, 'chat-1')); + assert.ok(store.get(discarded.id, 'chat-1')); + assert.ok(store.get(unrelated.id, 'chat-other')); +}); + test('ToolOutputStore rehomed spilled handles become durably owned by the target', async () => { const files = new Map(); const deletedPaths: string[] = []; diff --git a/infrastructure/ai/harness/toolOutputStore.ts b/infrastructure/ai/harness/toolOutputStore.ts index b5f89784c..dc9fe413d 100644 --- a/infrastructure/ai/harness/toolOutputStore.ts +++ b/infrastructure/ai/harness/toolOutputStore.ts @@ -350,7 +350,9 @@ export class ToolOutputStore { * the live cache (e.g. after an app restart or cache expiry) are restored * from persistence under the source session before cloning, so forking a * historical session still produces target-owned records instead of a - * no-op. + * no-op. When `retainHandleIds` is provided, only those handles are + * cloned; outputs saved by turns after the fork boundary stay in the + * source session. */ async rehomeChatSession( sourceChatSessionId: string, @@ -371,12 +373,28 @@ export class ToolOutputStore { } const sourceMap = this.bySession.get(sourceChatSessionId); if (!sourceMap || sourceMap.size === 0) return; + // When the caller names the handles the retained prefix references, clone + // only those: copying the whole source session would pull in outputs the + // fork never reads and could evict still-valid handles (source or other + // sessions) purely to make room for them. + const selected: [string, ToolOutputHandle][] = []; + if (retainHandleIds) { + const seen = new Set(); + for (const handleId of retainHandleIds) { + if (seen.has(handleId)) continue; + seen.add(handleId); + const handle = sourceMap.get(handleId); + if (handle) selected.push([handleId, handle]); + } + } else { + selected.push(...sourceMap.entries()); + } // Let pending spills settle so each source handle's durable ownership is // decided: either the content is still in memory (write pending/failed) or // the handle owns a durable file path. - await Promise.allSettled([...sourceMap.values()].map(handle => handle.spillPromise)); + await Promise.allSettled(selected.map(([, handle]) => handle.spillPromise)); const targetMap = this.bySession.get(targetChatSessionId) ?? new Map(); - for (const [handleId, handle] of sourceMap) { + for (const [handleId, handle] of selected) { if (targetMap.has(handleId)) continue; const copy: ToolOutputHandle = { ...handle, diff --git a/infrastructure/ai/harness/turnDrivers/cattyTurnDriver.ts b/infrastructure/ai/harness/turnDrivers/cattyTurnDriver.ts index 300aa5f23..1eb4fdb06 100644 --- a/infrastructure/ai/harness/turnDrivers/cattyTurnDriver.ts +++ b/infrastructure/ai/harness/turnDrivers/cattyTurnDriver.ts @@ -44,6 +44,7 @@ import { hadToolProgressBeforeRequestTooLarge, processCattyStream } from './catt import type { CattyTurnInput, TurnDriver, TurnDriverContext } from './types'; import { fitLargeUserInputForModel } from '../largeUserInput'; import { buildPromptContextSnapshot } from '../promptContextSnapshot'; +import { installToolOutputPersistence } from '../toolOutputPersistenceSetup'; export class CattyTurnDriver implements TurnDriver { readonly backend = 'catty' as const; @@ -75,71 +76,7 @@ async function runCattyTurn(input: CattyTurnInput, ctx: TurnDriverContext): Prom } = input; const netcattyBridge = (bridge ?? getNetcattyBridge()) as NonNullable>; - const toolOutputTempBridge = netcattyBridge as typeof netcattyBridge & { - getToolOutputPersistenceStatus?: () => Promise<{ durable: boolean; reason?: string }>; - writeToolOutputTemp?: ( - record: import('../toolOutputStore').PersistedToolOutputRecord, - content: string, - ) => Promise<{ ok: boolean; path?: string; error?: string }>; - restoreToolOutputTemp?: ( - handleId: string, - chatSessionId: string, - ) => Promise<{ path: string; record: import('../toolOutputStore').PersistedToolOutputRecord } | null>; - readToolOutputTemp?: ( - path: string, - request: import('../toolOutputStore').ReadToolOutputInput, - ) => Promise | null>; - deleteToolOutputTemp?: (path: string) => Promise<{ ok: boolean }>; - deleteChatToolOutputsTemp?: (chatSessionId: string) => Promise<{ deletedCount: number }>; - deleteTerminalToolOutputsTemp?: ( - chatSessionId: string, - terminalSessionId: string, - ) => Promise<{ deletedCount: number }>; - }; - const persistenceStatus = await toolOutputTempBridge.getToolOutputPersistenceStatus?.() - .catch(() => ({ durable: false })); - if ( - toolOutputTempBridge.writeToolOutputTemp - && toolOutputTempBridge.readToolOutputTemp - && toolOutputTempBridge.deleteToolOutputTemp - ) { - ctx.toolOutputStore.setPersistence?.({ - write: async (record, content) => { - if (!persistenceStatus?.durable) { - throw new Error(persistenceStatus?.reason || 'Secure local storage is unavailable.'); - } - const result = await toolOutputTempBridge.writeToolOutputTemp!(record, content); - if (!result.ok || !result.path) { - throw new Error(result.error || 'Unable to persist tool output.'); - } - return result.path; - }, - restore: persistenceStatus?.durable && toolOutputTempBridge.restoreToolOutputTemp - ? (handleId, chatSessionId) => toolOutputTempBridge.restoreToolOutputTemp!(handleId, chatSessionId) - : undefined, - read: (path, request) => toolOutputTempBridge.readToolOutputTemp!(path, request), - delete: async path => { - await toolOutputTempBridge.deleteToolOutputTemp!(path); - }, - deleteSession: toolOutputTempBridge.deleteChatToolOutputsTemp - ? async chatSessionId => { - await toolOutputTempBridge.deleteChatToolOutputsTemp!(chatSessionId); - } - : undefined, - deleteTerminalSession: toolOutputTempBridge.deleteTerminalToolOutputsTemp - ? async (chatSessionId, terminalSessionId) => { - await toolOutputTempBridge.deleteTerminalToolOutputsTemp!(chatSessionId, terminalSessionId); - } - : undefined, - deleteTerminalEverywhere: toolOutputTempBridge.deleteTerminalToolOutputsEverywhereTemp - ? async terminalSessionId => { - await toolOutputTempBridge.deleteTerminalToolOutputsEverywhereTemp!(terminalSessionId); - } - : undefined, - }); - } else { - ctx.toolOutputStore.setPersistence?.(undefined); - } + await installToolOutputPersistence(ctx.toolOutputStore, netcattyBridge); await clearChatSessionCancelled(sessionId, netcattyBridge); if (netcattyBridge.aiMcpUpdateSessions) { await netcattyBridge.aiMcpUpdateSessions(context.terminalSessions, sessionId);