feat: add session-scoped workflow approval gate commands

This commit is contained in:
William Valentin
2026-02-18 10:35:42 -08:00
parent 1508bcf9cb
commit f34a974210
13 changed files with 369 additions and 6 deletions
+94
View File
@@ -1,6 +1,7 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
import { AgentRouter } from '../agents/router.js';
import { AgentConfigRegistry } from '../agents/registry.js';
import { HookEngine } from '../hooks/index.js';
import type { ModelTier } from '../models/router.js';
import { createMessageRouter } from './routing.js';
import { AgentOrchestrator } from '../backends/index.js';
@@ -817,6 +818,99 @@ describe('daemon command fast-path integration', () => {
const outbound = reply.mock.calls[0]?.[0] as OutboundMessage | undefined;
expect(outbound?.text).toContain('Backend: codex');
});
it('lists and resolves pending approvals for the current session via /approvals + /approve', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
const session = {
id: 'telegram:approvals-user',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn(() => undefined),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const hookEngine = new HookEngine({ confirm: [], log: [], silent: [] });
const pendingPromise = hookEngine.requestConfirmation(
'shell.exec',
{ command: 'rm -rf /tmp/example' },
{ sessionId: session.id, channel: 'telegram', sender: 'approvals-user' },
);
const pending = hookEngine.getPendingConfirmations({ sessionId: session.id });
const pendingId = pending[0]?.id;
if (!pendingId) {
throw new Error('Expected pending approval id');
}
const commandRegistry = new CommandRegistry();
registerBuiltinCommands(commandRegistry);
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' } },
} as unknown as MessageRouterDeps['config'],
commandRegistry,
hookEngine,
});
const reply = vi.fn(async (_message: OutboundMessage) => {});
await router.handler({
id: 'approvals-1',
channel: 'telegram',
senderId: 'approvals-user',
text: '/approvals',
timestamp: Date.now(),
metadata: { isCommand: true, command: 'approvals' },
} as MessageRouterInput, reply);
const listReply = reply.mock.calls[0]?.[0] as OutboundMessage | undefined;
expect(String(listReply?.text)).toContain('Pending approvals:');
expect(String(listReply?.text)).toContain(pendingId);
await router.handler({
id: 'approvals-2',
channel: 'telegram',
senderId: 'approvals-user',
text: `/approve ${pendingId}`,
timestamp: Date.now(),
metadata: { isCommand: true, command: 'approve', commandArgs: pendingId },
} as MessageRouterInput, reply);
const approveReply = reply.mock.calls[1]?.[0] as OutboundMessage | undefined;
expect(String(approveReply?.text)).toContain('Approved: shell.exec');
await expect(pendingPromise).resolves.toEqual({ approved: true });
expect(processSpy).not.toHaveBeenCalled();
});
});
describe('daemon external backend integration', () => {