feat(routing): add pi canary guardrails and backend audit telemetry

This commit is contained in:
William Valentin
2026-02-23 21:13:01 -08:00
parent ac61c9c3fb
commit a389cd659c
4 changed files with 224 additions and 13 deletions
+57 -8
View File
@@ -164,6 +164,27 @@ function shouldForceNativeForCapabilityQuery(text: string): boolean {
);
}
function shouldForceNativeForPiNoTools(text: string): boolean {
const normalized = text.trim().toLowerCase();
if (!normalized) {
return false;
}
if (
/`(?:shell\.exec|file\.(?:read|write|edit|patch|list)|web\.(?:fetch|search)|browser\.)/.test(normalized)
|| /\b(?:gmail|calendar|docs|drive|tasks|k8s|docker|minio)\b/.test(normalized)
) {
return true;
}
return (
/\b(?:run|execute)\s+(?:a\s+)?(?:shell|bash|command)\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)
);
}
function providerAcceptsNativeAudioContentParts(provider: string): boolean {
return (
provider === 'openai'
@@ -1390,11 +1411,26 @@ export function createMessageRouter(deps: {
const requestedBackend = agentConfig?.backend ?? deps.defaultName;
const forceNativeForCapabilityQuery = shouldForceNativeForCapabilityQuery(messageText);
const sessionIdForAudit = `${msg.channel}:${msg.senderId}`;
const hasAttachmentsForExternalBackend = Boolean(attachments && attachments.length > 0);
const selectedBackend = requestedBackend && requestedBackend !== 'native'
? deps.externalBackends?.[requestedBackend]
: undefined;
const selectedBackendForAudit: 'native' | ExternalBackendName = selectedBackend && requestedBackend && !forceNativeForCapabilityQuery
const externalBackendRequested = Boolean(selectedBackend && requestedBackend && requestedBackend !== 'native');
const forceNativeForPiNoTools = requestedBackend === 'pi_embedded'
&& deps.config.backends.pi_embedded.no_tools_mode
&& shouldForceNativeForPiNoTools(messageText);
let forcedNativeGuardReason: 'capability_query' | 'pi_no_tools_mode' | 'attachments_present' | undefined;
if (externalBackendRequested) {
if (forceNativeForCapabilityQuery) {
forcedNativeGuardReason = 'capability_query';
} else if (forceNativeForPiNoTools) {
forcedNativeGuardReason = 'pi_no_tools_mode';
} else if (hasAttachmentsForExternalBackend) {
forcedNativeGuardReason = 'attachments_present';
}
}
const sessionIdForAudit = `${msg.channel}:${msg.senderId}`;
const selectedBackendForAudit: 'native' | ExternalBackendName = selectedBackend && requestedBackend && !forcedNativeGuardReason
? requestedBackend
: 'native';
@@ -1403,14 +1439,18 @@ export function createMessageRouter(deps: {
channel: msg.channel,
sender: msg.senderId,
selected_backend: selectedBackendForAudit,
source: agentConfig?.backend
? 'agent_override'
: selectedBackend
? 'default_external'
: 'native',
source: forcedNativeGuardReason
? 'forced_native_guard'
: agentConfig?.backend
? 'agent_override'
: selectedBackend
? 'default_external'
: 'native',
...(forcedNativeGuardReason ? { guard_reason: forcedNativeGuardReason } : {}),
});
if (selectedBackend && (!attachments || attachments.length === 0) && !forceNativeForCapabilityQuery) {
if (selectedBackend && !hasAttachmentsForExternalBackend && !forceNativeForCapabilityQuery && !forceNativeForPiNoTools) {
const backendStartedAt = Date.now();
try {
const history = toExternalHistory(session.getHistory());
session.addMessage({ role: 'user', content: messageText });
@@ -1418,6 +1458,14 @@ export function createMessageRouter(deps: {
prompt: messageText,
history,
});
auditLogger?.backendSuccess?.({
session_id: sessionIdForAudit,
channel: msg.channel,
sender: msg.senderId,
backend: selectedBackend.name,
duration_ms: Date.now() - backendStartedAt,
response_length: response.length,
});
session.addMessage({ role: 'assistant', content: response });
const ttsAttachment = await maybeBuildTtsAttachment(response, msg.channel);
await reply({
@@ -1438,6 +1486,7 @@ export function createMessageRouter(deps: {
: (selectedBackend.name as ExternalBackendName),
to_backend: 'native',
reason: detail,
duration_ms: Date.now() - backendStartedAt,
});
}
}