okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-021-hostA' into f-021-int74e25e165

main
2.6 KiB43 linesraw
1/** The chat footer's "tok/s · last reply" is the answer-writing speed: the engine's output tokens over the time spent decoding them. */
2import test, { mock } from 'node:test';
3import assert from 'node:assert/strict';
4import { TurnProjector } from '../server/agent/events.mjs';
5
6const chunk = (attemptId, c) => ({ type: 'assistant-stream', frame: { type: 'chunk', attemptId, chunk: c } });
7
8/** One model request: `pieces` chunks `gap` ms apart of the given kind, then the engine's usage; tool runs and the wait for the prompt take their own time. */
9test('writing a big tool call counts as decoding time: 600 tokens over 10 s is 60 tok/s, not hundreds', () => {
10  mock.timers.enable({ apis: ['Date'], now: 1_000_000 });
11  try {
12    const p = new TurnProjector({ startedAt: Date.now() });
13    // the model reads a 30 s prompt first: no chunk yet
14    mock.timers.tick(30_000);
15    // it writes a file as a tool call: 100 chunks, 100 ms apart, 600 output tokens in the engine's count
16    for (let i = 0; i < 100; i++) {
17      p.consume(chunk('a1', { type: 'tool-call-delta', id: 'call-1', name: 'write', argumentsDelta: 'x'.repeat(24) }));
18      mock.timers.tick(100);
19    }
20    p.consume(chunk('a1', { type: 'usage', usage: { inputTokens: 5000, outputTokens: 600 } }));
21    // a short closing sentence
22    p.consume(chunk('a2', { type: 'text-delta', text: 'Done.' }));
23    p.consume({ type: 'event', event: { seq: 9, time: 9, type: 'turn/end', data: { reason: { kind: 'completed' } } } });
24    p.ended = { kind: 'completed' };
25    const done = p.finish({ model: 'm', contextMax: 32768 }).find(f => f.type === 'done');
26    assert.ok(done.tps >= 55 && done.tps <= 65, `${done.tps} tok/s`);
27    assert.equal(done.tokensOut, 600, "the engine's own count");
28  } finally { mock.timers.reset(); }
29});
30
31test('text and reasoning keep their speed, and tool-run time is not decoding time', () => {
32  mock.timers.enable({ apis: ['Date'], now: 2_000_000 });
33  try {
34    const p = new TurnProjector({ startedAt: Date.now() });
35    for (let i = 0; i < 50; i++) { p.consume(chunk('a1', { type: 'reasoning-delta', text: 'think ' })); mock.timers.tick(20); }
36    for (let i = 0; i < 50; i++) { p.consume(chunk('a1', { type: 'text-delta', text: 'word ' })); mock.timers.tick(20); }
37    p.consume(chunk('a1', { type: 'usage', usage: { inputTokens: 100, outputTokens: 100 } }));
38    mock.timers.tick(60_000);   // a slow command runs: not decoding
39    p.ended = { kind: 'completed' };
40    const done = p.finish({}).find(f => f.type === 'done');
41    assert.ok(done.tps >= 45 && done.tps <= 55, `${done.tps} tok/s`);
42  } finally { mock.timers.reset(); }
43});