| 1 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { PetEngineTelemetry } from '../dist/core/pet-engine.js'; |
| 4 | |
| 5 | test('typed operation activity carries a safe action identity and goes stale', () => { |
| 6 | const e = new PetEngineTelemetry(); |
| 7 | for (const [i, kind] of ['reading','searching','editing','executing','testing','browsing','computer'].entries()) { |
| 8 | const at = i * 20_000; |
| 9 | e.observe({event:'turn_started',turn_id:'turn-'+i},at); |
| 10 | e.observe({event:'operation_activity_started',span_id:'private-span-'+i,activity_kind:kind,action_id:'action-'+i},at); |
| 11 | const activity=e.activity(at+100); |
| 12 | assert.equal(activity.actionId,'action-'+i); assert.equal(activity.activeSpans[0].actionId,'action-'+i); |
| 13 | assert.equal(activity.activityKind,kind); assert.equal(activity.authoritativePresence,'working'); |
| 14 | assert.equal(activity.freshness,'fresh'); assert.equal(activity.turnId,'turn-'+i); |
| 15 | assert.deepEqual(Object.keys(activity).sort(),['actionId','activeSpans','activityKind','authoritativePresence','cursor','doneEffectId', |
| 16 | 'failedToolAge','freshness','observed','observedAtMs','parallelAgentCount','schemaVersion','sessionId','turnId','turnOutcome']); |
| 17 | assert.equal(JSON.stringify(activity).includes('private-span'),false); |
| 18 | const stale=e.activity(at+12_001); |
| 19 | assert.equal(stale.freshness,'stale'); assert.equal(stale.activityKind,null); assert.equal(stale.actionId,undefined); assert.deepEqual(stale.activeSpans,[]); |
| 20 | } |
| 21 | // The pre-contract events carried tool names; the boundary refuses them. |
| 22 | assert.throws(() => e.observe({event:'tool_call_started',tool_call_id:'a',tool_name:'read_file'},200_000),/Invalid Engine pet metadata fields/); |
| 23 | assert.throws(() => e.observe({event:'turn_started'},200_000),/Missing Engine turn_id/); |
| 24 | assert.throws(() => e.observe({event:'operation_activity_started',span_id:'a',activity_kind:'shell'},200_000)); |
| 25 | for (const action_id of ['', 'x'.repeat(257), '鲸'.repeat(86), 'unsafe\nname']) |
| 26 | assert.throws(() => e.observe({event:'operation_activity_started',span_id:'a',activity_kind:'tool',action_id},200_000)); |
| 27 | }); |
| 28 | test('only a completed turn is Done; interrupted and failed turns are idle', () => { |
| 29 | for (const [outcome, presence] of [['completed','done'],['interrupted','idle'],['failed','idle']]) { |
| 30 | const e = new PetEngineTelemetry(); |
| 31 | e.observe({event:'turn_started',turn_id:'t'},0); |
| 32 | e.observe({event:'operation_activity_started',span_id:'s',activity_kind:'editing'},10); |
| 33 | e.observe({event:'turn_complete',turn_id:'t',turn_outcome:outcome},20); |
| 34 | const activity=e.activity(30); |
| 35 | assert.equal(activity.authoritativePresence,presence,outcome); |
| 36 | assert.equal(activity.doneEffectId,outcome==='completed'?'t':null); |
| 37 | assert.equal(activity.activityKind,null); assert.deepEqual(activity.activeSpans,[]); |
| 38 | } |
| 39 | const untracked = new PetEngineTelemetry(); |
| 40 | untracked.observe({event:'turn_complete',turn_outcome:'completed'},0); |
| 41 | assert.equal(untracked.activity(10).authoritativePresence,'idle'); |
| 42 | assert.throws(() => untracked.observe({event:'turn_complete',turn_id:null,turn_outcome:'completed'},20)); |
| 43 | // `/purge`, an edit rejection or a mid-turn session switch complete with no |
| 44 | // turn id. An outcome with no turn would fail the Rust projection |
| 45 | // invariant (`turn_outcome` requires `turn_id`) and drop the shared frame. |
| 46 | for (const outcome of ['completed','interrupted','failed']) { |
| 47 | const e = new PetEngineTelemetry(); |
| 48 | e.observe({event:'turn_started',turn_id:'t'},0); |
| 49 | e.observe({event:'turn_complete',turn_outcome:outcome},20); |
| 50 | const activity=e.activity(30); |
| 51 | assert.equal(activity.turnId,null,outcome); assert.equal(activity.turnOutcome,null,outcome); |
| 52 | assert.equal(activity.authoritativePresence,'idle',outcome); assert.equal(activity.doneEffectId,null,outcome); |
| 53 | } |
| 54 | }); |
| 55 | test('a new turn drops spans left open by the previous turn', () => { |
| 56 | const e = new PetEngineTelemetry(); |
| 57 | e.observe({event:'turn_started',turn_id:'t1'},0); |
| 58 | e.observe({event:'operation_activity_started',span_id:'orphan',activity_kind:'editing'},10); |
| 59 | e.observe({event:'turn_started',turn_id:'t2'},20); |
| 60 | const activity=e.activity(30); |
| 61 | assert.equal(activity.turnId,'t2'); assert.equal(activity.activityKind,null); assert.deepEqual(activity.activeSpans,[]); |
| 62 | }); |
| 63 | test('waiting and concurrent activity stay bounded', () => { |
| 64 | const e = new PetEngineTelemetry(); |
| 65 | for (const event of [{event:'turn_started',turn_id:'t'},{event:'operation_activity_started',span_id:'a',activity_kind:'computer'}, |
| 66 | ...Array.from({length:3},(_,i)=>({event:'agent_spawned',id:'private-'+i}))]) e.observe(event,0); |
| 67 | e.confirmWaiting(200,false); |
| 68 | const activity=e.activity(200); |
| 69 | assert.equal(activity.activityKind,'computer');assert.equal(activity.parallelAgentCount,3); |
| 70 | assert.equal(JSON.stringify(activity).includes('private-'),false); |
| 71 | e.observe({event:'approval_required',id:'private'},200); |
| 72 | e.confirmWaiting(600,true); |
| 73 | const waiting=e.activity(600); |
| 74 | assert.equal(waiting.authoritativePresence,'needs_you');assert.equal(waiting.activityKind,null); |
| 75 | e.observe({event:'approval_resolved',id:'private',outcome:'denied'},700); |
| 76 | e.confirmWaiting(800,false); |
| 77 | assert.equal(e.activity(800).authoritativePresence,'working'); |
| 78 | assert.equal(new PetEngineTelemetry().activity(800).freshness,'missing'); |
| 79 | }); |
| 80 | test('Engine pulses expire; a late failed completion tears at receipt time without rewriting history', () => { |
| 81 | const engine = new PetEngineTelemetry(); |
| 82 | engine.observe({ event: 'operation_activity_started', span_id: 'a', activity_kind: 'executing', action_id: 'exec_shell' }, 0); |
| 83 | engine.observe({ event: 'tool_call_heartbeat' }, 300); |
| 84 | const first = engine.bucket(0); |
| 85 | assert.equal(first.channel, 'code'); assert.equal(first.activeMs[3], 300); |
| 86 | assert.equal(engine.bucket(3).observed, 0); |
| 87 | engine.observe({ event: 'operation_activity_completed', span_id: 'a', activity_kind: 'executing', outcome: 'failed' }, 5900); |
| 88 | assert.equal(engine.activity(5900).failedToolAge.actionId, 'exec_shell'); |
| 89 | assert.equal(engine.activity(5900).actionId, 'exec_shell'); |
| 90 | assert.equal(engine.bucket(13).observed, 0); |
| 91 | assert.equal(engine.bucket(14).channel, 'error'); assert.equal(engine.bucket(14).errors, 1); |
| 92 | assert.deepEqual(engine.bucket(0), first); |
| 93 | engine.observe({ event: 'thinking_started', index: 1 }, 5901); |
| 94 | assert.equal(engine.activity(5901).actionId, undefined); |
| 95 | |
| 96 | }); |
| 97 | test('Engine worker boundary rejects payloads, invalid clocks and oversized active sets', () => { |
| 98 | const e = new PetEngineTelemetry(); |
| 99 | assert.throws(() => e.observe({ event: 'response_delta', index: 0, delta: 'private' }, 0)); |
| 100 | assert.throws(() => e.observe({ event: 'response_delta', index: 0, channel: 'fabricated' }, 0)); |
| 101 | e.observe({ event: 'thinking_started', index: 0 }, 100); |
| 102 | assert.throws(() => e.observe({ event: 'thinking_complete', index: 0 }, 90)); |
| 103 | for (let i = 1; i < 256; i++) e.observe({ event: 'thinking_started', index: i }, 100); |
| 104 | assert.throws(() => e.observe({ event: 'thinking_started', index: 257 }, 100)); |
| 105 | }); |
| 106 | |
| 107 | test('successive shared batches preserve active and waiting coverage exactly', () => { |
| 108 | let shared = new PetEngineTelemetry(); |
| 109 | const direct = new PetEngineTelemetry(); |
| 110 | for (const [at, event, waiting] of [ |
| 111 | [0, {event:'operation_activity_started', span_id:'build', activity_kind:'executing'}, false], |
| 112 | [300, {event:'tool_call_heartbeat'}, false], |
| 113 | [600, {event:'tool_call_heartbeat'}, false], |
| 114 | [800, {event:'operation_activity_completed', span_id:'build', activity_kind:'executing', outcome:'succeeded'}, false], |
| 115 | [900, {event:'approval_required', id:'permission'}, true], |
| 116 | [1200, {event:'agent_spawned', id:'worker'}, true], |
| 117 | [1500, {event:'agent_progress', id:'worker', worker_status:'running'}, true], |
| 118 | ]) { |
| 119 | shared = shared.clone(); |
| 120 | for (const engine of [shared, direct]) { |
| 121 | engine.observe(event, at); |
| 122 | engine.confirmWaiting(at, waiting); |
| 123 | } |
| 124 | for (let bucket=0; bucket<=Math.floor(at/400); bucket++) |
| 125 | assert.deepEqual(shared.bucket(bucket), direct.bucket(bucket)); |
| 126 | } |
| 127 | }); |
| 128 |