Phase 2 reactions v2 priority and cooldown

This commit is contained in:
William Valentin
2026-02-25 10:36:56 -08:00
parent e4ee6acce8
commit 7b170cff4d
12 changed files with 417 additions and 25 deletions
+154
View File
@@ -2484,6 +2484,160 @@ describe('daemon reactions routing integration', () => {
}),
);
});
it('skips reactions for command messages', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');
const session = {
id: 'gmail:reaction-command',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn(() => undefined),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const router = createMessageRouter({
sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
modelRouter: {
getAvailableTiers: () => ['default'],
getAllLabels: () => ({ default: 'default' }),
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: 'default',
memory_extraction: 'default',
classification: 'default',
tool_summarisation: 'default',
complex_reasoning: 'default',
},
max_delegation_depth: 1,
max_iterations: 3,
},
automation: {
reactions: [{
name: 'boss-email',
enabled: true,
on: ['gmail'],
filter: { contains: 'boss@company.com' },
run: 'Summarize: {{text}}',
}],
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
} as unknown as MessageRouterDeps['config'],
});
await router.handler({
id: 'r3',
channel: 'gmail',
senderId: 'reaction-command',
text: '/status',
timestamp: Date.now(),
metadata: { isCommand: true, command: 'status' },
} as MessageRouterInput, vi.fn(async (_message: OutboundMessage) => {}));
expect(processSpy).toHaveBeenCalledTimes(1);
const [prompt] = processSpy.mock.calls[0] ?? [];
expect(prompt).toBe('/status');
expect(mockAuditLogger.reactionMatch).not.toHaveBeenCalled();
});
it('suppresses reactions during cooldown windows', async () => {
vi.useFakeTimers();
try {
vi.setSystemTime(new Date('2026-02-25T00:00:00Z'));
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');
const session = {
id: 'gmail:reaction-cooldown',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn(() => undefined),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const router = createMessageRouter({
sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
modelRouter: {
getAvailableTiers: () => ['default'],
getAllLabels: () => ({ default: 'default' }),
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: 'default',
memory_extraction: 'default',
classification: 'default',
tool_summarisation: 'default',
complex_reasoning: 'default',
},
max_delegation_depth: 1,
max_iterations: 3,
},
automation: {
reactions: [{
name: 'boss-email',
enabled: true,
on: ['gmail'],
filter: { contains: 'boss@company.com' },
run: 'Summarize: {{text}}',
cooldown_ms: 60000,
}],
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
} as unknown as MessageRouterDeps['config'],
});
const reply = vi.fn(async (_message: OutboundMessage) => {});
await router.handler({
id: 'r4',
channel: 'gmail',
senderId: 'reaction-cooldown',
text: 'boss@company.com update',
timestamp: Date.now(),
} as MessageRouterInput, reply);
vi.setSystemTime(new Date('2026-02-25T00:00:30Z'));
await router.handler({
id: 'r5',
channel: 'gmail',
senderId: 'reaction-cooldown',
text: 'boss@company.com update again',
timestamp: Date.now(),
} as MessageRouterInput, reply);
expect(processSpy).toHaveBeenCalledTimes(2);
const secondPrompt = processSpy.mock.calls[1]?.[0];
expect(secondPrompt).toBe('boss@company.com update again');
expect(mockAuditLogger.reactionSkip).toHaveBeenCalledWith(
expect.objectContaining({
session_id: 'gmail:reaction-cooldown',
reason: 'cooldown',
}),
);
} finally {
vi.useRealTimers();
}
});
});
describe('daemon auto-escalate integration', () => {