mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
fix(codex): persist Round duration across session reloads (#2025)
* fix(codex): persist Round duration across session reloads * fix(codex): preserve completed plan summaries between turns * test(ios): make plan transcript recycling deterministic * fix(ios): preserve reading anchors with stale sizing attributes
This commit is contained in:
@@ -28,6 +28,24 @@ describe('shared history projection', () => {
|
||||
expect(send.mock.calls).toEqual(live);
|
||||
});
|
||||
|
||||
it('keeps plan cards distinct across native turns and stable within each turn', async () => {
|
||||
const send = vi.fn();
|
||||
const session = { getMetadata: () => ({}), sendAgentMessage: send } as unknown as ApiSessionClient;
|
||||
const projection = new SharedCodexProjection(session, 'thread', async () => {});
|
||||
for (const turnId of ['first', 'second']) {
|
||||
for (const status of ['pending', 'completed']) {
|
||||
await projection.notification('turn/plan/updated', { threadId: 'thread', turnId,
|
||||
plan: [{ step: 'work', status }] });
|
||||
}
|
||||
}
|
||||
for (const type of ['tool-call', 'tool-call-result']) {
|
||||
expect(send.mock.calls.map(([body]) => body).filter(body => body.type === type).map(body => body.callId)).toEqual([
|
||||
'codex-plan-state:first', 'codex-plan-state:first',
|
||||
'codex-plan-state:second', 'codex-plan-state:second'
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
it.each([undefined, 'root'])('persists proposals without approval and replays the same IDs (parent: %s)', async parentThreadId => {
|
||||
const send = vi.fn();
|
||||
const session = { getMetadata: () => ({}), sendAgentMessage: send } as unknown as ApiSessionClient;
|
||||
@@ -83,6 +101,26 @@ describe('shared history projection', () => {
|
||||
}
|
||||
}
|
||||
});
|
||||
it('persists a live duration once and never times history snapshots', async () => {
|
||||
const send = vi.fn();
|
||||
const session = { getMetadata: () => ({}), sendAgentMessage: send } as unknown as ApiSessionClient;
|
||||
const projection = new SharedCodexProjection(session, 'thread', async () => {});
|
||||
await projection.history({ turns: [{ id: 'old', status: 'completed', items: [{ id: 'old-answer', type: 'agentMessage', text: 'history' }] }] });
|
||||
await projection.notification('turn/completed', { threadId: 'thread', turn: { id: 'old', status: 'completed' } }, undefined, 5000);
|
||||
await projection.history({ turns: [{ id: 'already-running', status: 'inProgress', items: [] }] });
|
||||
await projection.notification('turn/completed', { threadId: 'thread', turn: { id: 'already-running', status: 'completed' } }, undefined, 6000);
|
||||
expect(send.mock.calls.map(([body]) => body).filter(body => body.type === 'turn_duration')).toEqual([]);
|
||||
await projection.notification('turn/started', { threadId: 'thread', turn: { id: 'live' } }, undefined, 10_000);
|
||||
projection.reset();
|
||||
await projection.history({ turns: [{ id: 'live', status: 'inProgress', items: [] }] });
|
||||
await projection.notification('turn/completed', { threadId: 'thread', turn: { id: 'old', status: 'completed' } }, undefined, 15_000);
|
||||
await projection.notification('turn/completed', { threadId: 'thread', turn: { id: 'live', status: 'completed' } }, undefined, 49_426);
|
||||
await projection.notification('turn/completed', { threadId: 'thread', turn: { id: 'live', status: 'completed' } }, undefined, 50_000);
|
||||
expect(send.mock.calls.map(([body]) => body).filter(body => body.type === 'turn_duration')).toEqual([
|
||||
expect.objectContaining({ durationMs: 39_426, turn_id: 'live' })
|
||||
]);
|
||||
});
|
||||
|
||||
it('keeps each turn model through settings changes, reconnect replay and rerouting', async () => {
|
||||
const send = vi.fn();
|
||||
const session = { getMetadata: () => ({}), sendAgentMessage: send } as unknown as ApiSessionClient;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { CodexRoundDuration } from '../utils/roundDuration';
|
||||
import { createHash } from 'node:crypto';
|
||||
import type { ApiSessionClient } from '@/api/apiSession';
|
||||
import { normalizeSessionDisplayTitle } from '@/agent/sessionDisplayRename';
|
||||
@@ -35,6 +36,7 @@ function metadataHasDisplayTitle(metadata: { name?: string; summary?: { text: st
|
||||
/** Canonical V2 stream only. Stable message IDs also deduplicate snapshot replay at the hub. */
|
||||
export class SharedCodexProjection {
|
||||
private converter = new AppServerEventConverter();
|
||||
private readonly roundDuration = new CodexRoundDuration();
|
||||
private readonly emitted = new Set<string>();
|
||||
private readonly turns = new Map<string, string>();
|
||||
private readonly turnModels = new Map<string, string>();
|
||||
@@ -68,7 +70,7 @@ export class SharedCodexProjection {
|
||||
});
|
||||
}
|
||||
|
||||
async notification(method: string, params: unknown, modelAtReceipt?: string): Promise<void> {
|
||||
async notification(method: string, params: unknown, modelAtReceipt?: string, receivedAt = Date.now()): Promise<void> {
|
||||
const p = record(params);
|
||||
const item = record(p.item);
|
||||
if (!this.parentThreadId && p.threadId === this.threadId && string(item.id)) {
|
||||
@@ -88,6 +90,15 @@ export class SharedCodexProjection {
|
||||
}
|
||||
}
|
||||
await this.project(method, params, modelAtReceipt);
|
||||
if (p.threadId === this.threadId && !this.parentThreadId) {
|
||||
const turn = record(p.turn);
|
||||
const turnId = string(turn.id);
|
||||
if (method === 'turn/started') this.roundDuration.start(turnId, receivedAt);
|
||||
if (method === 'turn/completed') {
|
||||
const duration = this.roundDuration.finish(turnId, receivedAt, turn.status === 'completed');
|
||||
if (duration) this.send(duration, `duration:${turnId}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async project(method: string, params: unknown, modelAtReceipt?: string, fromHistory = false): Promise<void> {
|
||||
@@ -95,6 +106,9 @@ export class SharedCodexProjection {
|
||||
const p = record(params);
|
||||
const item = record(p.item);
|
||||
const turnId = string(p.turnId) ?? string(record(p.turn).id);
|
||||
const send = (body: Record<string, unknown>, key: string) => this.send({
|
||||
...body, ...(turnId ? { turn_id: turnId } : {})
|
||||
}, key);
|
||||
// Settings may already describe the next turn by the time queued
|
||||
// notifications are projected. Never attribute usage to that model.
|
||||
if (turnId && method === 'turn/started' && modelAtReceipt && !this.turnModels.has(turnId)) {
|
||||
@@ -118,7 +132,7 @@ export class SharedCodexProjection {
|
||||
}
|
||||
}
|
||||
if (this.parentThreadId && (method === 'turn/started' || method === 'turn/completed')) {
|
||||
this.send({ type: 'agent-run-update', agentId: this.threadId, cardId: `codex-agent:${this.threadId}`,
|
||||
send({ type: 'agent-run-update', agentId: this.threadId, cardId: `codex-agent:${this.threadId}`,
|
||||
status: method === 'turn/started' ? 'running' : record(p.turn).status === 'completed' ? 'completed' : 'failed'
|
||||
}, `lifecycle:${turnId}:${method}`);
|
||||
}
|
||||
@@ -126,32 +140,32 @@ export class SharedCodexProjection {
|
||||
for (const event of events) {
|
||||
const callId = string(event.call_id);
|
||||
const key = `${turnId ?? 'thread'}:${itemId ?? callId ?? createHash('sha256').update(JSON.stringify(event)).digest('hex')}:${event.type}`;
|
||||
if (event.type === 'agent_message') this.send({ type: 'message', message: event.message }, key);
|
||||
else if (event.type === 'agent_reasoning') this.send({ type: 'reasoning', message: event.text }, key);
|
||||
if (event.type === 'agent_message') send({ type: 'message', message: event.message }, key);
|
||||
else if (event.type === 'agent_reasoning') send({ type: 'reasoning', message: event.text }, key);
|
||||
else if (event.type === 'exec_command_begin' && callId) {
|
||||
this.send({ type: 'tool-call', name: 'CodexBash', callId, input: event }, key);
|
||||
send({ type: 'tool-call', name: 'CodexBash', callId, input: event }, key);
|
||||
} else if (event.type === 'exec_command_end' && callId) {
|
||||
this.send({ type: 'tool-call-result', callId, output: { ...event, stdout: event.output } }, key);
|
||||
send({ type: 'tool-call-result', callId, output: { ...event, stdout: event.output } }, key);
|
||||
} else if (event.type === 'patch_apply_begin' && callId) {
|
||||
this.send({ type: 'tool-call', name: 'CodexPatch', callId, input: { changes: event.changes, auto_approved: event.auto_approved } }, key);
|
||||
send({ type: 'tool-call', name: 'CodexPatch', callId, input: { changes: event.changes, auto_approved: event.auto_approved } }, key);
|
||||
} else if (event.type === 'patch_apply_end' && callId) {
|
||||
this.send({ type: 'tool-call-result', callId, output: { stdout: event.stdout, stderr: event.stderr, success: event.success } }, key);
|
||||
send({ type: 'tool-call-result', callId, output: { stdout: event.stdout, stderr: event.stderr, success: event.success } }, key);
|
||||
} else if (event.type === 'mcp_tool_call_begin' && callId) {
|
||||
const invocation = record(event.invocation);
|
||||
this.send({ type: 'tool-call', name: `mcp__${invocation.server}__${invocation.tool}`, callId, input: invocation.arguments ?? {} }, key);
|
||||
send({ type: 'tool-call', name: `mcp__${invocation.server}__${invocation.tool}`, callId, input: invocation.arguments ?? {} }, key);
|
||||
} else if (event.type === 'mcp_tool_call_end' && callId) {
|
||||
const result = record(event.result);
|
||||
this.send({ type: 'tool-call-result', callId, output: result.Ok ?? result.Err ?? event.result, is_error: 'Err' in result }, key);
|
||||
send({ type: 'tool-call-result', callId, output: result.Ok ?? result.Err ?? event.result, is_error: 'Err' in result }, key);
|
||||
} else if (event.type === 'codex_tool_call_begin' && callId) {
|
||||
this.send({ type: 'tool-call', name: event.name, callId, input: event.input ?? event.arguments }, key);
|
||||
send({ type: 'tool-call', name: event.name, callId, input: event.input ?? event.arguments }, key);
|
||||
} else if (event.type === 'codex_tool_call_end' && callId) {
|
||||
this.send({ type: 'tool-call-result', callId, output: event.output, is_error: event.is_error }, key);
|
||||
send({ type: 'tool-call-result', callId, output: event.output, is_error: event.is_error }, key);
|
||||
} else if (event.type === 'compaction_started') {
|
||||
// History replay re-projects every completed item's start;
|
||||
// a finished compaction must not announce a live status.
|
||||
if (fromHistory) continue;
|
||||
if (this.parentThreadId) {
|
||||
this.send({ type: 'compaction_started', statusText: '📦 Compaction started' }, key);
|
||||
send({ type: 'compaction_started', statusText: '📦 Compaction started' }, key);
|
||||
} else {
|
||||
// Status UI listens on session events; an agent message
|
||||
// would render as a normal assistant bubble instead.
|
||||
@@ -162,27 +176,28 @@ export class SharedCodexProjection {
|
||||
}
|
||||
} else if (event.type === 'token_count' || event.type === 'context_compacted' || event.type.startsWith('thread_goal_')) {
|
||||
const model = event.type === 'token_count' && turnId ? this.turnModels.get(turnId) : undefined;
|
||||
this.send({ ...event, ...(model ? { model } : {}), flavor: 'codex', scope: { role: 'parent', threadId: this.threadId }, scope_role: 'parent', thread_id: this.threadId }, key);
|
||||
send({ ...event, ...(model ? { model } : {}), flavor: 'codex', scope: { role: 'parent', threadId: this.threadId }, scope_role: 'parent', thread_id: this.threadId }, key);
|
||||
} else if (event.type === 'proposed_plan' && turnId && itemId) {
|
||||
const planId = codexPlanProposalId(this.threadId, turnId, itemId);
|
||||
this.send({ type: 'tool-call', name: 'ExitPlanMode', callId: planId, input: { plan: event.plan } }, key);
|
||||
send({ type: 'tool-call', name: 'ExitPlanMode', callId: planId, input: { plan: event.plan } }, key);
|
||||
// A proposal is durable content, not a native approval request.
|
||||
this.send({ type: 'tool-call-result', callId: planId, output: null }, `${key}:result`);
|
||||
send({ type: 'tool-call-result', callId: planId, output: null }, `${key}:result`);
|
||||
} else if (event.type === 'plan_update') {
|
||||
this.send({ type: 'tool-call', name: 'update_plan', callId: 'codex-plan-state', input: { plan: event.plan, source: 'codex' } }, key);
|
||||
this.send({ type: 'tool-call-result', callId: 'codex-plan-state', output: { plan: event.plan, source: 'codex', status: 'updated' } }, `${key}:result`);
|
||||
const callId = turnId ? `codex-plan-state:${turnId}` : 'codex-plan-state';
|
||||
send({ type: 'tool-call', name: 'update_plan', callId, input: { plan: event.plan, source: 'codex' } }, key);
|
||||
send({ type: 'tool-call-result', callId, output: { plan: event.plan, source: 'codex', status: 'updated' } }, `${key}:result`);
|
||||
} else if (event.type === 'generated_image' && typeof event.saved_path === 'string') {
|
||||
const image = await registerGeneratedImageFromPath({ path: event.saved_path, id: createHash('sha256').update(`${this.threadId}:${key}`).digest('hex'), fileName: string(event.file_name) });
|
||||
if (image) this.send({ type: 'generated-image', imageId: image.id, fileName: image.fileName, mimeType: image.mimeType }, key);
|
||||
if (image) send({ type: 'generated-image', imageId: image.id, fileName: image.fileName, mimeType: image.mimeType }, key);
|
||||
} else if (event.type === 'task_failed') {
|
||||
this.send({ type: 'message', message: `Codex error: ${event.error ?? event.message ?? 'Turn failed'}` }, key);
|
||||
send({ type: 'message', message: `Codex error: ${event.error ?? event.message ?? 'Turn failed'}` }, key);
|
||||
}
|
||||
}
|
||||
if (item.type === 'collabAgentToolCall') {
|
||||
const states = record(item.agentsStates);
|
||||
for (const [agentId, state] of Object.entries(states)) {
|
||||
const status = string(record(state).status) ?? 'running';
|
||||
this.send({ type: 'agent-run-update', agentId, cardId: `codex-agent:${agentId}`,
|
||||
send({ type: 'agent-run-update', agentId, cardId: `codex-agent:${agentId}`,
|
||||
status: status === 'completed' ? 'completed' : status === 'errored' ? 'failed' : 'running',
|
||||
summary: record(state).message, input: item, scope: { role: 'child', threadId: agentId, parentThreadId: this.threadId }, scope_role: 'child', thread_id: agentId
|
||||
}, `agent:${agentId}:${itemId}:${method}:${JSON.stringify(state)}`);
|
||||
|
||||
@@ -112,8 +112,9 @@ export class SharedCodexRoot {
|
||||
}
|
||||
this.publishPlan();
|
||||
}
|
||||
const receivedAt = Date.now();
|
||||
const modelAtReceipt = record(params).threadId === this.threadId ? this.settings.model ?? undefined : undefined;
|
||||
this.notifications = this.notifications.then(() => this.notification(method, params, modelAtReceipt)).catch(error => logger.debug('[Codex shared] projection', error));
|
||||
this.notifications = this.notifications.then(() => this.notification(method, params, modelAtReceipt, receivedAt)).catch(error => logger.debug('[Codex shared] projection', error));
|
||||
});
|
||||
this.client.setTransportAbandonedHandler(() => { void this.reconnect(); });
|
||||
this.session.onUserMessage((message, localId) => {
|
||||
@@ -272,7 +273,7 @@ export class SharedCodexRoot {
|
||||
for (const listener of this.settingsListeners) listener();
|
||||
this.publishPlan();
|
||||
}
|
||||
private async notification(method: string, params: unknown, modelAtReceipt?: string): Promise<void> {
|
||||
private async notification(method: string, params: unknown, modelAtReceipt?: string, receivedAt = Date.now()): Promise<void> {
|
||||
if (!this.threadId || this.closed) return;
|
||||
const p = record(params);
|
||||
if (method === 'thread/started') {
|
||||
@@ -312,7 +313,7 @@ export class SharedCodexRoot {
|
||||
if (method === 'thread/queue/changed') {
|
||||
for (const id of await this.queue.reconcile()) this.dequeuedInputs.add(id);
|
||||
}
|
||||
await this.projection.notification(method, params, modelAtReceipt); this.publishCompaction(); this.alive();
|
||||
await this.projection.notification(method, params, modelAtReceipt, receivedAt); this.publishCompaction(); this.alive();
|
||||
}
|
||||
async readThread(threadId = this.threadId): Promise<Record<string, unknown>> {
|
||||
let thread = record(record(await this.client.request('thread/read', { threadId, includeTurns: false })).thread);
|
||||
|
||||
Reference in New Issue
Block a user