Phase 1 run-control semantics and run_state events

This commit is contained in:
William Valentin
2026-02-25 10:22:44 -08:00
parent ae21681958
commit e4ee6acce8
13 changed files with 485 additions and 120 deletions
+84
View File
@@ -437,6 +437,90 @@ describe('daemon command fast-path integration', () => {
});
});
it('preempts active runs when queue mode is interrupt', async () => {
const cancelSpy = vi.spyOn(AgentOrchestrator.prototype, 'cancel');
vi.spyOn(AgentOrchestrator.prototype, 'isCancellable').mockReturnValue(true);
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
let resolveFirst: ((value: string) => void) | undefined;
let markStarted: (() => void) | undefined;
const started = new Promise<void>((resolve) => { markStarted = resolve; });
processSpy
.mockImplementationOnce(() => {
markStarted?.();
return new Promise<string>((resolve) => { resolveFirst = resolve; });
})
.mockResolvedValueOnce('second');
const session = {
id: 'telegram:user-interrupt',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn((key: string) => (key === 'queue.mode' ? 'interrupt' : undefined)),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const router = createMessageRouter({
sessionManager: {
getSession: vi.fn(() => session),
} as unknown as MessageRouterDeps['sessionManager'],
modelRouter: {
getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
getLabel: (tier: string) => tier,
} as unknown as MessageRouterDeps['modelRouter'],
systemPrompt: 'test prompt',
toolRegistry: {
clone() { return this; },
register: vi.fn(),
} as unknown as MessageRouterDeps['toolRegistry'],
toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
config: {
agents: {
primary_tier: 'default',
delegation: {
compaction: 'fast',
memory_extraction: 'fast',
classification: 'fast',
tool_summarisation: 'fast',
complex_reasoning: 'complex',
},
max_delegation_depth: 3,
max_iterations: 10,
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
server: { queue: { mode: 'collect' } },
} as unknown as MessageRouterDeps['config'],
});
const reply = vi.fn(async (_message: OutboundMessage) => {});
const firstRun = router.handler({
id: 'm-interrupt-1',
channel: 'telegram',
senderId: 'user-interrupt',
text: 'first',
timestamp: Date.now(),
} as MessageRouterInput, reply);
await started;
await router.handler({
id: 'm-interrupt-2',
channel: 'telegram',
senderId: 'user-interrupt',
text: 'second',
timestamp: Date.now(),
} as MessageRouterInput, reply);
expect(cancelSpy).toHaveBeenCalled();
resolveFirst?.('first');
await firstRun;
});
it('emits run.state start and complete for non-command channel messages', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');
const mockAuditLogger = {