feat(runtime): forward Flynn prompt to pi and harden backend mode commands

This commit is contained in:
William Valentin
2026-02-24 10:41:27 -08:00
parent 4188c68130
commit 2e192ef407
9 changed files with 305 additions and 6 deletions
+155 -3
View File
@@ -1348,6 +1348,9 @@ describe('daemon external backend integration', () => {
} as MessageRouterInput, reply);
expect(piBackend.process).toHaveBeenCalled();
expect(piBackend.process).toHaveBeenCalledWith(expect.objectContaining({
systemPrompt: 'test prompt',
}));
expect(processSpy).not.toHaveBeenCalled();
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'pi embedded response' }));
});
@@ -1425,7 +1428,80 @@ describe('daemon external backend integration', () => {
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'native guarded response' }));
});
it('supports manual global pi deactivation and re-activation via /backend command', async () => {
it('forces native processing for pi_embedded no-tools mode on quick-check execution intent', 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-quick-check',
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-quick-check',
channel: 'telegram',
senderId: 'pi-no-tools-quick-check',
text: 'run a quick check',
timestamp: Date.now(),
} as MessageRouterInput, reply);
expect(piBackend.process).not.toHaveBeenCalled();
expect(processSpy).toHaveBeenCalled();
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'native guarded response' }));
});
it('supports manual global pi deactivation and re-activation via /runtime alias', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
.mockResolvedValue('native fallback response');
const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
@@ -1499,9 +1575,9 @@ describe('daemon external backend integration', () => {
id: 'm-backend-deactivate',
channel: 'telegram',
senderId: 'pi-manual-toggle',
text: '/backend deactivate pi',
text: '/runtime deactivate pi',
timestamp: Date.now(),
metadata: { isCommand: true, command: 'backend', commandArgs: 'deactivate pi' },
metadata: { isCommand: true, command: 'runtime', commandArgs: 'deactivate pi' },
} as MessageRouterInput, reply);
await router.handler({
@@ -1536,6 +1612,82 @@ describe('daemon external backend integration', () => {
expect(backendMode).toBe('force_pi_embedded');
expect(piBackend.process).toHaveBeenCalledTimes(1);
});
it('accepts full-command backend subcommand input and still returns status', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
.mockResolvedValue('native fallback response');
const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
const session = {
id: 'telegram:pi-runtime-status-normalization',
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 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,
},
backends: {
pi_embedded: { no_tools_mode: false },
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
} as unknown as MessageRouterDeps['config'],
commandRegistry,
externalBackends: {} as unknown as MessageRouterDeps['externalBackends'],
getBackendMode: () => 'config_default',
setBackendMode: vi.fn(),
});
const reply = vi.fn(async (_message: OutboundMessage) => {});
await router.handler({
id: 'm-runtime-status-normalized',
channel: 'telegram',
senderId: 'pi-runtime-status-normalization',
text: '/runtime status',
timestamp: Date.now(),
metadata: { isCommand: true, command: 'runtime', commandArgs: '/runtime status' },
} as MessageRouterInput, reply);
expect(processSpy).not.toHaveBeenCalled();
expect(reply).toHaveBeenCalledWith(expect.objectContaining({
text: expect.stringContaining('Backend mode:'),
}));
});
});
describe('daemon audio routing integration', () => {
+22 -1
View File
@@ -155,6 +155,9 @@ function shouldForceNativeForCapabilityQuery(text: string): boolean {
|| normalized.includes('tool list')
|| normalized.includes('list tools')
|| normalized.includes('what can you do')
|| normalized.includes('full access')
|| normalized.includes('do you have access')
|| normalized.includes('what access')
) {
return true;
}
@@ -163,6 +166,7 @@ function shouldForceNativeForCapabilityQuery(text: string): boolean {
|| /\b(?:what|which)\s+tools?\b/.test(normalized)
|| /\btools?\s+(?:do\s+you\s+have|are\s+available)\b/.test(normalized)
|| /\b(?:show|list|what\s+are)\s+(?:your\s+)?capabilities\b/.test(normalized)
|| /\bdo\s+you\s+have\s+(?:full\s+)?access\b/.test(normalized)
);
}
@@ -181,6 +185,9 @@ function shouldForceNativeForPiNoTools(text: string): boolean {
return (
/\b(?:run|execute)\s+(?:a\s+)?(?:shell|bash|command)\b/.test(normalized)
|| /\b(?:run|execute)\s+(?:a\s+)?(?:quick\s+)?check\b/.test(normalized)
|| /\b(?:quick\s+)?check\s+(?:access|status|logs?|health|config|setup)\b/.test(normalized)
|| /\b(?:verify|confirm)\s+(?:access|setup|status|config)\b/.test(normalized)
|| /\b(?:read|open|show|edit|write|patch|delete|list)\s+(?:the\s+)?(?:file|files|directory|repo|code)\b/.test(normalized)
|| /\b(?:search|fetch|browse|scrape)\s+(?:the\s+)?(?:web|internet|url|site)\b/.test(normalized)
|| /\b(?:use|call)\s+(?:a\s+)?tool\b/.test(normalized)
@@ -1197,7 +1204,11 @@ export function createMessageRouter(deps: {
},
backendCommand: (inputRaw: string) => {
const normalized = inputRaw.trim().toLowerCase();
let normalized = inputRaw.trim().toLowerCase();
// Accept both subcommand-only input ("status") and accidental full-command
// input ("/runtime status", "runtime status", "/backend status").
normalized = normalized.replace(/^(?:\/)?(?:runtime|backend)\b/, '').trim();
normalized = normalized.replace(/^\//, '').trim();
if (!normalized || normalized === 'status' || normalized === 'show') {
return formatBackendStatusLine(agent.getModelTier());
}
@@ -1245,6 +1256,12 @@ export function createMessageRouter(deps: {
return [
'Usage:',
'/runtime status',
'/runtime activate pi',
'/runtime deactivate pi',
'/runtime use config',
'',
'Alias:',
'/backend status',
'/backend activate pi',
'/backend deactivate pi',
@@ -1563,9 +1580,13 @@ export function createMessageRouter(deps: {
try {
const history = toExternalHistory(session.getHistory());
session.addMessage({ role: 'user', content: messageText });
const externalSystemPrompt = requestedBackend === 'pi_embedded'
? agent.getSystemPrompt(messageText)
: undefined;
const response = await selectedBackend.process({
prompt: messageText,
history,
...(externalSystemPrompt ? { systemPrompt: externalSystemPrompt } : {}),
});
auditLogger?.backendSuccess?.({
session_id: sessionIdForAudit,