mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-25 05:07:05 +00:00
perf(codex): reduce history replay during thread recovery (#2214)
* perf(codex): reduce history replay during thread recovery * fix(codex): restore live child subscriptions during bounded replay --------- Co-authored-by: gaodq <gaodq@koal.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import { codexPlanProposalId } from './plan';
|
||||
import { RPC_METHODS } from '@hapi/protocol/rpcMethods';
|
||||
|
||||
type NativeTurn = { id: string; status: string; items: unknown[] };
|
||||
type NativeRequest = { id: string | number; method: string; params: unknown };
|
||||
|
||||
vi.mock('../codexAppServerClient', () => ({
|
||||
CodexAppServerClient: class {
|
||||
@@ -17,9 +18,12 @@ vi.mock('../codexAppServerClient', () => ({
|
||||
queue: Array<{ id: string; clientUserMessageId: unknown; input: unknown }> = [];
|
||||
notify?: (method: string, params: unknown) => void;
|
||||
abandoned?: () => void;
|
||||
serverRequest?: (request: NativeRequest) => void;
|
||||
responses: Array<{ id: string | number; result: unknown }> = [];
|
||||
setNotificationHandler(handler: typeof this.notify) { this.notify = handler; }
|
||||
setTransportAbandonedHandler(handler: (() => void) | null) { this.abandoned = handler ?? undefined; }
|
||||
setServerRequestHandler() {}
|
||||
setServerRequestHandler(handler: (request: NativeRequest) => void) { this.serverRequest = handler; }
|
||||
respond(id: string | number, result: unknown) { this.responses.push({ id, result }); }
|
||||
async connect() {}
|
||||
async initialize() { this.initialized = true; }
|
||||
isInitialized() { return this.initialized; }
|
||||
@@ -94,6 +98,8 @@ async function fixture(opts?: { hubArchived?: boolean; end?: RootHost['end']; cr
|
||||
queue: Array<{ id: string; clientUserMessageId: string; input: unknown }>;
|
||||
notify(method: string, params: unknown): void;
|
||||
abandoned(): void;
|
||||
serverRequest(request: NativeRequest): void;
|
||||
responses: Array<{ id: string | number; result: unknown }>;
|
||||
};
|
||||
return {
|
||||
root, native, rpc, send, consumed, uncertain, metadata: () => metadata, state: () => state, updateState,
|
||||
@@ -119,6 +125,104 @@ async function startCompaction(f: Awaited<ReturnType<typeof fixture>>, id = 'inp
|
||||
return turn;
|
||||
}
|
||||
|
||||
describe('optional bounded history replay', () => {
|
||||
it.each([
|
||||
{ selected: true, fullHistory: false, bounded: true },
|
||||
{ selected: true, fullHistory: true, bounded: false },
|
||||
{ selected: false, fullHistory: false, bounded: false },
|
||||
])('reads the expected history window: %j', async ({ selected, fullHistory, bounded }) => {
|
||||
const f = await fixture();
|
||||
vi.stubEnv('HAPI_CODEX_LAZY_HISTORY_IDS', selected ? ' other, thread ' : 'other');
|
||||
const request = vi.spyOn(f.root.client, 'request');
|
||||
request.mockResolvedValueOnce({ thread: { id: 'thread', historyMode: 'paginated' } });
|
||||
if (bounded) request.mockResolvedValueOnce({ data: [{ id: 'new', items: [] }], nextCursor: 'older' });
|
||||
else request.mockResolvedValueOnce({ data: [{ id: 'old', items: [] }], nextCursor: 'next' })
|
||||
.mockResolvedValueOnce({ data: [{ id: 'new', items: [] }] });
|
||||
try {
|
||||
expect((await f.root.readThread('thread', fullHistory)).turns).toEqual(
|
||||
bounded ? [{ id: 'new', items: [] }] : [{ id: 'old', items: [] }, { id: 'new', items: [] }]);
|
||||
const calls = request.mock.calls.filter(([method]) => method === 'thread/turns/list');
|
||||
expect(calls).toHaveLength(bounded ? 1 : 2);
|
||||
expect(calls[0]?.[1]).toMatchObject(bounded
|
||||
? { sortDirection: 'desc', limit: 1, itemsView: 'full' }
|
||||
: { sortDirection: 'asc', itemsView: 'full' });
|
||||
} finally { request.mockRestore(); vi.unstubAllEnvs(); }
|
||||
});
|
||||
|
||||
it('retains legacy history reads when the native thread does not support pagination', async () => {
|
||||
const f = await fixture();
|
||||
vi.stubEnv('HAPI_CODEX_LAZY_HISTORY_IDS', 'thread');
|
||||
const request = vi.spyOn(f.root.client, 'request');
|
||||
request.mockResolvedValueOnce({ thread: { id: 'thread' } })
|
||||
.mockResolvedValueOnce({ thread: { id: 'thread', turns: [{ id: 'old', items: [] }] } });
|
||||
try {
|
||||
expect((await f.root.readThread()).turns).toEqual([{ id: 'old', items: [] }]);
|
||||
expect(request).toHaveBeenLastCalledWith('thread/read', { threadId: 'thread', includeTurns: true });
|
||||
} finally { request.mockRestore(); vi.unstubAllEnvs(); }
|
||||
});
|
||||
|
||||
it.each([true, false])('preserves initial subscription and child discovery; selected root: %s', async selected => {
|
||||
const f = await fixture();
|
||||
vi.stubEnv('HAPI_CODEX_LAZY_HISTORY_IDS', selected ? 'thread' : 'other');
|
||||
const request = vi.spyOn(f.root.client, 'request');
|
||||
try {
|
||||
await f.root.bind('thread', {}, true);
|
||||
expect(request).toHaveBeenCalledWith('thread/resume', { threadId: 'thread' });
|
||||
expect(request.mock.calls.some(([method]) => method === 'thread/list')).toBe(true);
|
||||
} finally { request.mockRestore(); vi.unstubAllEnvs(); }
|
||||
});
|
||||
|
||||
it('restores loaded child subscriptions, pending questions and live events after a lazy-root reconnect', async () => {
|
||||
const f = await fixture();
|
||||
vi.stubEnv('HAPI_CODEX_LAZY_HISTORY_IDS', 'thread');
|
||||
const question: NativeRequest = { id: 'native-question', method: 'item/tool/requestUserInput', params: {
|
||||
threadId: 'child', turnId: 'child-turn', itemId: 'question',
|
||||
questions: [{ id: 'choice', header: 'Choose', question: 'Which?', options: [{ label: 'A', description: 'A' }] }]
|
||||
} };
|
||||
const original = f.root.client.request.bind(f.root.client);
|
||||
const request = vi.spyOn(f.root.client, 'request').mockImplementation(async (method, params) => {
|
||||
const p = params as Record<string, unknown> | undefined;
|
||||
if (method === 'thread/list') return { data: [{ id: 'child', parentThreadId: 'thread' }, { id: 'unloaded', parentThreadId: 'thread' }] };
|
||||
if (method === 'thread/loaded/list') return { data: ['child', 'unrelated'] };
|
||||
if (method === 'thread/read' && p?.threadId === 'child') return { thread: { id: 'child', parentThreadId: 'thread' } };
|
||||
if (method === 'thread/resume' && p?.threadId === 'child') {
|
||||
f.native.serverRequest(question);
|
||||
f.native.notify('item/completed', { threadId: 'child', turnId: 'child-turn',
|
||||
item: { id: 'answer', type: 'agentMessage', text: 'Child still connected' } });
|
||||
return { thread: { id: 'child' } };
|
||||
}
|
||||
return original(method, params);
|
||||
});
|
||||
try {
|
||||
await f.root.activate();
|
||||
f.native.serverRequest(question);
|
||||
await vi.waitFor(() => expect(Object.keys(f.state().requests ?? {})).toHaveLength(1));
|
||||
const oldKey = Object.keys(f.state().requests!)[0];
|
||||
f.native.initialized = false; f.native.abandoned();
|
||||
await vi.waitFor(() => expect(request).toHaveBeenCalledWith('thread/resume', { threadId: 'child', excludeTurns: true }));
|
||||
await vi.waitFor(() => {
|
||||
expect(f.state().completedRequests?.[oldKey]?.status).toBe('canceled');
|
||||
expect(Object.keys(f.state().requests ?? {})).toHaveLength(1);
|
||||
expect(Object.keys(f.state().requests!)[0]).not.toBe(oldKey);
|
||||
});
|
||||
const newKey = Object.keys(f.state().requests!)[0];
|
||||
await f.rpc.get(RPC_METHODS.Permission)!({ id: newKey, approved: true, answers: { choice: ['A'] } });
|
||||
await vi.waitFor(() => expect(f.native.responses).toContainEqual({
|
||||
id: 'native-question', result: { answers: { choice: { answers: ['A'] } } }
|
||||
}));
|
||||
await vi.waitFor(() => expect(f.send).toHaveBeenCalledWith(expect.objectContaining({
|
||||
type: 'agent-run-trace', agentId: 'child', message: expect.objectContaining({ type: 'message', message: 'Child still connected' })
|
||||
}), expect.any(String)));
|
||||
expect(request.mock.calls.some(([method, params]) => method === 'thread/resume'
|
||||
&& ['unloaded', 'unrelated'].includes(String((params as Record<string, unknown>)?.threadId)))).toBe(false);
|
||||
expect(request.mock.calls.some(([method, params]) => method === 'thread/read'
|
||||
&& (params as Record<string, unknown>)?.includeTurns === true
|
||||
&& (params as Record<string, unknown>)?.threadId === 'child')).toBe(false);
|
||||
expect(request.mock.calls.some(([method, params]) => method === 'thread/turns/list'
|
||||
&& (params as Record<string, unknown>)?.threadId === 'child')).toBe(false);
|
||||
} finally { request.mockRestore(); vi.unstubAllEnvs(); }
|
||||
});
|
||||
});
|
||||
describe('historical fork during generation', () => {
|
||||
it('rejects steered and unknown-status boundaries before creating a child', async () => {
|
||||
const create = vi.fn<RootHost['create']>()
|
||||
|
||||
Reference in New Issue
Block a user