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
+5
View File
@@ -5,6 +5,11 @@ export type ExternalBackendName = 'claude_code' | 'opencode' | 'codex' | 'gemini
export interface ExternalBackendRequest {
prompt: string;
history: Array<{ role: 'user' | 'assistant'; content: string }>;
/**
* Optional assembled Flynn system prompt (SOUL/AGENTS/IDENTITY/USER/TOOLS + runtime sections).
* Backends that support system-level instructions can use this directly.
*/
systemPrompt?: string;
}
export interface ExternalBackend {
+4
View File
@@ -612,6 +612,10 @@ export class NativeAgent {
this.systemPrompt = prompt;
}
getSystemPrompt(): string {
return this.systemPrompt;
}
setOnToolUse(callback: ((event: ToolUseEvent) => void) | undefined): void {
this.onToolUse = callback;
}
+12
View File
@@ -532,6 +532,18 @@ export class AgentOrchestrator {
this._externalOnToolUse = callback;
}
/**
* Get the effective system prompt for the current turn.
* When a user message is provided, applies memory-context injection first
* so external backends can receive the same prompt context as native runs.
*/
getSystemPrompt(userMessage?: string): string {
if (typeof userMessage === 'string') {
this._injectMemoryContext(userMessage);
}
return this._agent.getSystemPrompt();
}
/** Request cancellation for the current primary-agent operation. */
cancel(): void {
this._agent.cancel();
+77
View File
@@ -60,6 +60,54 @@ describe('PiEmbeddedBackend', () => {
}
});
it('injects Flynn system prompt fields into session payload in hybrid mode', async () => {
const mod = createModule(`
export function createAgentSession() {
return {
run(payload) {
return { text: payload.systemPrompt ?? payload.system ?? "missing" };
},
};
}
`);
try {
const backend = new PiEmbeddedBackend({ module: mod.moduleUrl, timeoutMs: 2000, systemPromptMode: 'hybrid' });
const result = await backend.process({
prompt: 'hello',
history: [],
systemPrompt: 'SOUL + IDENTITY + USER + TOOLS',
});
expect(result).toBe('SOUL + IDENTITY + USER + TOOLS');
} finally {
mod.cleanup();
}
});
it('omits Flynn system prompt injection in pi_default mode', async () => {
const mod = createModule(`
export function createAgentSession() {
return {
run(payload) {
return { text: payload.systemPrompt ? "present" : "absent" };
},
};
}
`);
try {
const backend = new PiEmbeddedBackend({ module: mod.moduleUrl, timeoutMs: 2000, systemPromptMode: 'pi_default' });
const result = await backend.process({
prompt: 'hello',
history: [],
systemPrompt: 'should not be forwarded',
});
expect(result).toBe('absent');
} finally {
mod.cleanup();
}
});
it('throws when module has no supported session factory', async () => {
const mod = createModule('export const version = "0.0.0";');
@@ -100,6 +148,35 @@ describe('PiEmbeddedBackend', () => {
}
});
it('applies Flynn system prompt in Agent runtime via setSystemPrompt()', async () => {
const mod = createModule(`
export class Agent {
constructor() {
this.systemPrompt = "";
this.state = { messages: [] };
}
setSystemPrompt(prompt) {
this.systemPrompt = prompt;
}
async prompt(input) {
this.state.messages.push({ role: "assistant", content: [{ type: "text", text: this.systemPrompt + " :: " + input }] });
}
}
`);
try {
const backend = new PiEmbeddedBackend({ module: mod.moduleUrl, timeoutMs: 2000, systemPromptMode: 'flynn' });
const result = await backend.process({
prompt: 'hello',
history: [],
systemPrompt: 'use flynn prompt',
});
expect(result).toBe('use flynn prompt :: hello');
} finally {
mod.cleanup();
}
});
it('surfaces agent state error when no assistant text is produced', async () => {
const mod = createModule(`
export class Agent {
+23 -1
View File
@@ -41,6 +41,9 @@ export interface PiEmbeddedBackendOptions {
function buildPrompt(request: ExternalBackendRequest): string {
const lines: string[] = [];
if (request.systemPrompt?.trim()) {
lines.push(`SYSTEM: ${request.systemPrompt.trim()}`);
}
for (const item of request.history) {
if (!item.content.trim()) {
continue;
@@ -217,6 +220,9 @@ export class PiEmbeddedBackend implements ExternalBackend {
const prompt = buildPrompt(input);
const { moduleLike, moduleName } = await this.loadPiModule();
const factory = getSessionFactory(moduleLike);
const effectiveSystemPrompt = this.systemPromptMode !== 'pi_default'
? input.systemPrompt?.trim() || undefined
: undefined;
const requestPayload: Record<string, unknown> = {
prompt,
input: input.prompt,
@@ -227,6 +233,10 @@ export class PiEmbeddedBackend implements ExternalBackend {
],
...(this.model ? { model: this.model } : {}),
systemPromptMode: this.systemPromptMode,
...(effectiveSystemPrompt ? {
system: effectiveSystemPrompt,
systemPrompt: effectiveSystemPrompt,
} : {}),
};
if (factory) {
@@ -260,7 +270,7 @@ export class PiEmbeddedBackend implements ExternalBackend {
);
}
return withTimeout(
this.invokeAgentRuntime(AgentCtor, input, moduleName),
this.invokeAgentRuntime(AgentCtor, input, moduleName, effectiveSystemPrompt),
this.timeoutMs,
'Pi embedded request',
);
@@ -299,6 +309,7 @@ export class PiEmbeddedBackend implements ExternalBackend {
AgentCtor: new (options?: Record<string, unknown>) => unknown,
input: ExternalBackendRequest,
moduleName: string,
systemPrompt?: string,
): Promise<string> {
const modelSpec = this.model ?? this.defaultModelSpec;
const model = modelSpec
@@ -312,6 +323,10 @@ export class PiEmbeddedBackend implements ExternalBackend {
if (this.getApiKey) {
agentOptions.getApiKey = this.getApiKey;
}
if (systemPrompt) {
agentOptions.systemPrompt = systemPrompt;
agentOptions.system = systemPrompt;
}
const agent = Object.keys(agentOptions).length > 0
? new AgentCtor(agentOptions)
@@ -321,6 +336,13 @@ export class PiEmbeddedBackend implements ExternalBackend {
}
const agentObj = agent as PiSessionLike;
if (systemPrompt) {
const setSystemPrompt = agentObj.setSystemPrompt;
if (typeof setSystemPrompt === 'function') {
await Promise.resolve(setSystemPrompt.call(agent, systemPrompt));
}
}
if (model) {
const setModel = agentObj.setModel;
if (typeof setModel === 'function') {
+5
View File
@@ -343,6 +343,11 @@ describe('builtin /skill command', () => {
});
describe('builtin /backend command', () => {
it('registers /runtime as an alias', () => {
const cmd = createBackendCommand();
expect(cmd.aliases).toContain('runtime');
});
it('passes subcommand input to backendCommand service', async () => {
const cmd = createBackendCommand();
const backendCommand = vi.fn(() => 'Pi backend deactivated.');
+2 -1
View File
@@ -359,7 +359,8 @@ export function createSkillCommand(): CommandDefinition {
export function createBackendCommand(): CommandDefinition {
return {
name: 'backend',
description: 'Inspect or control runtime backend mode (status, activate pi, deactivate pi)',
aliases: ['runtime'],
description: 'Inspect or control runtime backend mode (status, activate pi, deactivate pi, use config)',
execute: async (args, ctx) => {
if (!ctx.services?.backendCommand) {
return notAvailable('Backend command');
+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,