mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 15:41:17 +00:00
fix: address Codex review on PR #3604
This commit is contained in:
1 parent
11e716dd49
commit
12ecb30da8
6 files changed
+208
-69
No files matched your search
@@ -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),
|
||||
|
||||
@@ -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<ReturnType<typeof getNetcattyBridge>>;
|
||||
|
||||
test('installToolOutputPersistence configures the durable adapter from bridge methods', async () => {
|
||||
const files = new Map<string, string>();
|
||||
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);
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
import { getNetcattyBridge } from '../aiChatStreamingSupport';
|
||||
import type {
|
||||
PersistedToolOutputRecord,
|
||||
ReadToolOutputInput,
|
||||
ToolOutputReadResult,
|
||||
ToolOutputStore,
|
||||
} from './toolOutputStore';
|
||||
|
||||
type NetcattyBridge = NonNullable<ReturnType<typeof getNetcattyBridge>>;
|
||||
|
||||
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<Omit<ToolOutputReadResult, 'handleId' | 'storedChars' | 'sourceTruncated'> | 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<void> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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<string, { record: PersistedToolOutputRecord; content: string }>();
|
||||
const deletedPaths: string[] = [];
|
||||
|
||||
@@ -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<string>();
|
||||
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<string, ToolOutputHandle>();
|
||||
for (const [handleId, handle] of sourceMap) {
|
||||
for (const [handleId, handle] of selected) {
|
||||
if (targetMap.has(handleId)) continue;
|
||||
const copy: ToolOutputHandle = {
|
||||
...handle,
|
||||
|
||||
@@ -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<ReturnType<typeof getNetcattyBridge>>;
|
||||
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<Omit<import('../toolOutputStore').ToolOutputReadResult, 'handleId' | 'storedChars' | 'sourceTruncated'> | 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);
|
||||
|
||||
Reference in new issue
Block a user