|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { setImmediate as nextTurn } from 'node:timers/promises'; |
| 3 | +import test from 'node:test'; |
| 4 | +import type { NodeHub } from '../src/fabric/network.js'; |
| 5 | +import { Admission } from '../src/kernel/admission.js'; |
| 6 | +import { makeBundle } from '../src/lang/bundle.js'; |
| 7 | +import type { Store } from '../src/store/store.js'; |
| 8 | +import { agent, setup } from './helpers.js'; |
| 9 | + |
| 10 | +test('occupancy keeps recent executions and reloads durable timing after eviction', async () => { |
| 11 | + const s = await setup(); |
| 12 | + try { |
| 13 | + s.store.publish('owner', makeBundle(agent())); |
| 14 | + const completed = await s.request('POST', '/v1/responses', { |
| 15 | + model: 'agent/test', |
| 16 | + input: 'fixture', |
| 17 | + }); |
| 18 | + assert.equal(completed.statusCode, 200, completed.body); |
| 19 | + assert.equal(completed.json().status, 'completed'); |
| 20 | + const runId = String(completed.json().id); |
| 21 | + const setDuration = s.store.prepare( |
| 22 | + "UPDATE run_summaries SET payload=json_set(payload,'$.created_at',0,'$.updated_at',?) WHERE run_id=?", |
| 23 | + ); |
| 24 | + setDuration.run(700, runId); |
| 25 | + const cold = 'agent/test'; |
| 26 | + const hot = 'agent/hot'; |
| 27 | + const agentOf = (id: string) => id.split(':', 1)[0]!; |
| 28 | + const reads: string[] = []; |
| 29 | + const lane = new Admission({ |
| 30 | + store: { |
| 31 | + actionWrites: new Set(), |
| 32 | + queuedRun: (id: string) => ({ |
| 33 | + id, |
| 34 | + owner: 'owner', |
| 35 | + agentId: agentOf(id), |
| 36 | + digest: 'fixture', |
| 37 | + }), |
| 38 | + bundle: (_owner: string, id: string) => ({ digest: 'fixture', agent: { id } }), |
| 39 | + queuedRunIdentities: (ids: string[]) => |
| 40 | + new Map(ids.map((id) => [id, { owner: 'owner', agentId: agentOf(id) }])), |
| 41 | + queueReason: () => 'admission', |
| 42 | + recentDurations(owner: string, id: string) { |
| 43 | + reads.push(id); |
| 44 | + return s.store.recentDurations(owner, id); |
| 45 | + }, |
| 46 | + } as unknown as Store, |
| 47 | + hub: {} as NodeHub, |
| 48 | + limits: { maxActiveRuns: 1, approvalTimeoutMs: 1000 }, |
| 49 | + closing: () => false, |
| 50 | + releaseRun: () => {}, |
| 51 | + execute: async () => {}, |
| 52 | + }); |
| 53 | + let sequence = 0; |
| 54 | + const execute = async (ids: string[]) => { |
| 55 | + lane.queue = ids.map((id) => id + ':' + sequence++); |
| 56 | + lane.pump(); |
| 57 | + for (let i = 0; i < 5 && lane.tasks.size; i++) await nextTurn(); |
| 58 | + assert.equal(lane.tasks.size, 0); |
| 59 | + assert.equal(lane.queue.length, 0); |
| 60 | + assert.equal(lane.active, 0); |
| 61 | + }; |
| 62 | + const estimate = () => { |
| 63 | + lane.queue = [cold + ':probe', 'target']; |
| 64 | + try { |
| 65 | + return lane.queueInsight('target', 'owner', 'queued')?.estimated_start_ms; |
| 66 | + } finally { |
| 67 | + lane.queue = []; |
| 68 | + } |
| 69 | + }; |
| 70 | + await execute([cold]); |
| 71 | + assert.equal(estimate(), 700); |
| 72 | + await execute([cold, cold, cold, cold, cold, hot]); |
| 73 | + await execute(Array.from({ length: 1022 }, (_, i) => 'agent/filler-' + i)); |
| 74 | + const profiles = (lane as unknown as { occupancy: Map<string, number[]> }).occupancy; |
| 75 | + const key = (id: string) => JSON.stringify(['owner', id]); |
| 76 | + assert.equal(profiles.size, 1024); |
| 77 | + assert.equal(profiles.get(key(cold))?.length, 6); |
| 78 | + estimate(); |
| 79 | + assert.deepEqual(reads, [cold], 'polling uses observed samples without renewing recency'); |
| 80 | + setDuration.run(27_000, runId); |
| 81 | + await execute([ |
| 82 | + ...Array.from({ length: 52 }, () => hot), |
| 83 | + 'agent/overflow-1', |
| 84 | + 'agent/overflow-2', |
| 85 | + ]); |
| 86 | + assert.equal(profiles.size, 1024); |
| 87 | + assert.equal(profiles.has(key(cold)), false); |
| 88 | + assert.equal( |
| 89 | + profiles.get(key(hot))?.length, |
| 90 | + 50, |
| 91 | + 'recent executions renew and bound the profile', |
| 92 | + ); |
| 93 | + assert.equal(estimate(), 27_000, 'eviction discards the old cached database sample too'); |
| 94 | + assert.deepEqual(reads, [cold, cold]); |
| 95 | + assert.equal(lane.slotBreaches, 0); |
| 96 | + } finally { |
| 97 | + await s.cleanup(); |
| 98 | + } |
| 99 | +}); |
0 commit comments