feat(routing): add pi canary guardrails and backend audit telemetry
This commit is contained in:
@@ -1279,6 +1279,151 @@ describe('daemon external backend integration', () => {
|
||||
expect(processSpy).toHaveBeenCalled();
|
||||
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'native fallback response' }));
|
||||
});
|
||||
|
||||
it('uses pi_embedded backend for plain text canary turns', async () => {
|
||||
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
|
||||
const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
|
||||
const session = {
|
||||
id: 'telegram:pi-canary',
|
||||
addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
|
||||
history.push(msg);
|
||||
return msg;
|
||||
}),
|
||||
getHistory: vi.fn(() => [...history]),
|
||||
clear: vi.fn(),
|
||||
replaceHistory: vi.fn(),
|
||||
getConfig: vi.fn(() => undefined),
|
||||
setConfig: vi.fn(),
|
||||
deleteConfig: vi.fn(),
|
||||
};
|
||||
|
||||
const piBackend = {
|
||||
name: 'pi_embedded',
|
||||
process: vi.fn(async () => 'pi embedded response'),
|
||||
};
|
||||
|
||||
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,
|
||||
},
|
||||
backends: {
|
||||
pi_embedded: { no_tools_mode: false },
|
||||
},
|
||||
compaction: { enabled: false },
|
||||
models: { default: { provider: 'anthropic', model: 'claude' } },
|
||||
} as unknown as MessageRouterDeps['config'],
|
||||
externalBackends: { pi_embedded: piBackend } as unknown as MessageRouterDeps['externalBackends'],
|
||||
defaultName: 'pi_embedded',
|
||||
});
|
||||
|
||||
const reply = vi.fn(async (_message: OutboundMessage) => {});
|
||||
await router.handler({
|
||||
id: 'm-pi-canary',
|
||||
channel: 'telegram',
|
||||
senderId: 'pi-canary',
|
||||
text: 'just chat with me',
|
||||
timestamp: Date.now(),
|
||||
} as MessageRouterInput, reply);
|
||||
|
||||
expect(piBackend.process).toHaveBeenCalled();
|
||||
expect(processSpy).not.toHaveBeenCalled();
|
||||
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'pi embedded response' }));
|
||||
});
|
||||
|
||||
it('forces native processing for pi_embedded no-tools mode when prompt appears tool-oriented', async () => {
|
||||
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
|
||||
.mockResolvedValue('native guarded response');
|
||||
const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
|
||||
const session = {
|
||||
id: 'telegram:pi-no-tools',
|
||||
addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
|
||||
history.push(msg);
|
||||
return msg;
|
||||
}),
|
||||
getHistory: vi.fn(() => [...history]),
|
||||
clear: vi.fn(),
|
||||
replaceHistory: vi.fn(),
|
||||
getConfig: vi.fn(() => undefined),
|
||||
setConfig: vi.fn(),
|
||||
deleteConfig: vi.fn(),
|
||||
};
|
||||
|
||||
const piBackend = {
|
||||
name: 'pi_embedded',
|
||||
process: vi.fn(async () => 'pi embedded response'),
|
||||
};
|
||||
|
||||
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,
|
||||
},
|
||||
backends: {
|
||||
pi_embedded: { no_tools_mode: true },
|
||||
},
|
||||
compaction: { enabled: false },
|
||||
models: { default: { provider: 'anthropic', model: 'claude' } },
|
||||
} as unknown as MessageRouterDeps['config'],
|
||||
externalBackends: { pi_embedded: piBackend } as unknown as MessageRouterDeps['externalBackends'],
|
||||
defaultName: 'pi_embedded',
|
||||
});
|
||||
|
||||
const reply = vi.fn(async (_message: OutboundMessage) => {});
|
||||
await router.handler({
|
||||
id: 'm-pi-no-tools',
|
||||
channel: 'telegram',
|
||||
senderId: 'pi-no-tools',
|
||||
text: 'please read the file and run a shell command',
|
||||
timestamp: Date.now(),
|
||||
} as MessageRouterInput, reply);
|
||||
|
||||
expect(piBackend.process).not.toHaveBeenCalled();
|
||||
expect(processSpy).toHaveBeenCalled();
|
||||
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'native guarded response' }));
|
||||
});
|
||||
});
|
||||
|
||||
describe('daemon audio routing integration', () => {
|
||||
|
||||
Reference in New Issue
Block a user