mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-25 05:07:05 +00:00
feat: show generation rate (tok/s) for Codex rounds (#2379)
* feat(codex): fill the round footer tok/s from app-server timing Measure each Codex round's generation window the way agy rounds do: the live turn span minus tool-execution and wait-for-the-user windows, over the thread-cumulative output-token delta, emitted on the turn's turn_duration so the round footer shows tok/s. Tool windows prefer the app-server's own item timing (startedAtMs/completedAtMs/durationMs). The token baseline is the last reported cumulative total: the CLI seeds a thread it created in this process (thread/start) to zero, while resumed and forked threads receive the app-server's attach-time usage replay and stay unseeded, so inherited history is never counted as a turn's output. Only the live root-turn feed reaches the ledger: history replays re-project old items and must not settle a round. Permission hooks keep timing waits for the user. * fix(codex): seed gateway-created threads for the first-turn tok/s baseline Native /new and /clear create threads through the gateway before/after hooks, whose after() binds the fresh root without the zero usage seed the runtime create path applies - so the new thread's first turn captured a null baseline and never showed tok/s. Seed it there too, same as create(): thread/start only, since forked threads receive the app-server's attach-time usage replay instead (tiann/hapi#2379). * fix(codex): keep the active generation ledger across a hub-only reconnect SharedCodexRoot's session.onReconnect reset the whole projection, but a hub reconnect only replays final history: the native turn keeps running without a new turn/started, so dropping the ledger identity, baseline and windows made generation.settle() return null at completion and the round permanently lost its tok/s. Add projection.resetAfterHubReconnect which replays without touching the ledger; the app-server reconnect path keeps the full reset, because the resumed turn re-announces itself and must re-measure from its own start (tiann/hapi#2379). * fix(codex): seed fresh usage before session-ready releases queued input The gateway thread/start seed ran in the after() hook, after the bind wrapper had already activated the root: registerControls and emitSessionReady open the input gates, so a queued Web prompt (or one submitted on session-ready while the runner notification is delayed) could start the thread's first turn before the zero baseline existed, and seeding afterward could not repair that captured-null turn. Move the seed into the bind wrapper right after root.bind, guarded by a freshThread flag the create and gateway paths set for thread/start only - forks keep their app-server attach-time replay baseline (tiann/hapi#2379).
This commit is contained in:
@@ -421,7 +421,79 @@ async function completePlan(f: Awaited<ReturnType<typeof fixture>>, status = 'co
|
||||
return codexPlanProposalId('thread', turn.id, 'plan-item');
|
||||
}
|
||||
|
||||
describe('shared permission modes', () => {
|
||||
it('keeps the active round measurable when only the hub reconnects', async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
vi.setSystemTime(1_000_000);
|
||||
const f = await fixture();
|
||||
await f.root.activate();
|
||||
const settled = async () => { for (let i = 0; i < 40; i++) await Promise.resolve(); await f.drain(); };
|
||||
f.native.notify('thread/tokenUsage/updated', { threadId: 'thread', tokenUsage: { total: { output_tokens: 100 } } });
|
||||
vi.setSystemTime(1_001_000);
|
||||
await f.drain();
|
||||
f.native.notify('turn/started', { threadId: 'thread', turn: { id: 'turn' } });
|
||||
vi.setSystemTime(1_002_000);
|
||||
await f.drain();
|
||||
// Hub-only reconnect: the native turn keeps running without a new
|
||||
// turn/started, so the active round's ledger identity, baseline
|
||||
// and windows must survive the replay.
|
||||
f.reconnect();
|
||||
await settled();
|
||||
f.native.notify('thread/tokenUsage/updated', { threadId: 'thread', tokenUsage: { total: { output_tokens: 160 } } });
|
||||
vi.setSystemTime(1_003_000);
|
||||
await f.drain();
|
||||
f.native.notify('turn/completed', { threadId: 'thread', turn: { id: 'turn', status: 'completed' } });
|
||||
vi.setSystemTime(1_004_000);
|
||||
await f.drain();
|
||||
expect(f.send.mock.calls.map(([body]) => body).filter(body => body.type === 'turn_duration')).toEqual([
|
||||
expect.objectContaining({ durationMs: 2_000, generationMs: 2_000, generationOutputTokens: 60 })
|
||||
]);
|
||||
} finally { vi.useRealTimers(); }
|
||||
});
|
||||
|
||||
it('keeps timing approval waits out of generation after a reconnect', async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
vi.setSystemTime(1_000_000);
|
||||
const f = await fixture();
|
||||
await f.root.activate();
|
||||
const settled = async () => { for (let i = 0; i < 40; i++) await Promise.resolve(); await f.drain(); };
|
||||
f.native.notify('thread/tokenUsage/updated', { threadId: 'thread', tokenUsage: { total: { output_tokens: 100 } } });
|
||||
vi.setSystemTime(1_001_000);
|
||||
await f.drain();
|
||||
// App-server reconnect: the re-armed permissions must keep the
|
||||
// host's user-wait hook, or approval waits count as generation.
|
||||
await (f.root as unknown as { reconnect(): Promise<void> }).reconnect();
|
||||
f.native.notify('turn/started', { threadId: 'thread', turn: { id: 'turn' } });
|
||||
vi.setSystemTime(1_002_000);
|
||||
await f.drain();
|
||||
const question: NativeRequest = { id: 'native-question', method: 'item/tool/requestUserInput', params: {
|
||||
threadId: 'thread', turnId: 'turn', itemId: 'question',
|
||||
questions: [{ id: 'choice', header: 'Choose', question: 'Which?', options: [{ label: 'A', description: 'A' }] }]
|
||||
} };
|
||||
vi.setSystemTime(1_003_000);
|
||||
f.native.serverRequest(question);
|
||||
for (let i = 0; i < 200 && Object.keys(f.state().requests ?? {}).length === 0; i++) await Promise.resolve();
|
||||
await f.drain();
|
||||
const key = Object.keys(f.state().requests!)[0];
|
||||
expect(key, 'approval surfaced after reconnect').toBeTruthy();
|
||||
vi.setSystemTime(1_003_500);
|
||||
await f.rpc.get(RPC_METHODS.Permission)!({ id: key, approved: true, answers: { choice: ['A'] } });
|
||||
await settled();
|
||||
f.native.notify('thread/tokenUsage/updated', { threadId: 'thread', tokenUsage: { total: { output_tokens: 160 } } });
|
||||
vi.setSystemTime(1_004_000);
|
||||
await f.drain();
|
||||
f.native.notify('turn/completed', { threadId: 'thread', turn: { id: 'turn', status: 'completed' } });
|
||||
vi.setSystemTime(1_005_000);
|
||||
await f.drain();
|
||||
// 3 s round with a 0.5 s wait for the user: 2.5 s of generation.
|
||||
expect(f.send.mock.calls.map(([body]) => body).filter(body => body.type === 'turn_duration')).toEqual([
|
||||
expect.objectContaining({ durationMs: 3_000, generationMs: 2_500, generationOutputTokens: 60 })
|
||||
]);
|
||||
} finally { vi.useRealTimers(); }
|
||||
});
|
||||
|
||||
describe('shared permission modes', () => {
|
||||
it('switches native auto-review on and back to the human reviewer', async () => {
|
||||
const f = await fixture();
|
||||
expect((await f.root.applySettings({ permissionMode: 'autoReview' })).applied.permissionMode).toBe('autoReview');
|
||||
|
||||
Reference in New Issue
Block a user