Merge 927331297ca88f940cd5b89c98ad4ad48049477d into 8a05c05596ca9ba0735dafd8e359885de4c2c969

This commit is contained in:
ToToKr 2026-03-20 22:54:22 -07:00 committed by GitHub
commit 2a88b9f629
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 1 deletions

View File

@ -108,6 +108,13 @@ const OVERLOAD_FAILOVER_BACKOFF_POLICY: BackoffPolicy = {
const ANTHROPIC_MAGIC_STRING_TRIGGER_REFUSAL = "ANTHROPIC_MAGIC_STRING_TRIGGER_REFUSAL";
const ANTHROPIC_MAGIC_STRING_REPLACEMENT = "ANTHROPIC MAGIC STRING TRIGGER REFUSAL (redacted)";
// Detect model safety-filter rejections (stop_reason: sensitive).
// This is NOT a timeout — it is a content policy block that requires a user-facing message.
// Matches both bare text ("stop_reason: sensitive") and JSON-stringified variants
// ('"stop_reason":"sensitive"') since describeUnknownError uses JSON.stringify.
const SENSITIVE_STOP_REASON_RE =
/\bunhandled\s+stop[_ ]reason:\s*"?sensitive"?\b|\bstop[_ ]reason"?:\s*"?sensitive"?\b|\breason"?:\s*"?sensitive"?\b/i;
function scrubAnthropicRefusalMagic(prompt: string): string {
if (!prompt.includes(ANTHROPIC_MAGIC_STRING_TRIGGER_REFUSAL)) {
return prompt;
@ -1328,6 +1335,33 @@ export async function runEmbeddedPiAgent(
authRetryPending = true;
continue;
}
// Handle sensitive stop_reason (content safety filter) with a user-friendly message
if (SENSITIVE_STOP_REASON_RE.test(errorText)) {
return {
payloads: [
{
text:
"I can't respond to that - the content was flagged by the model's safety filter. " +
"Please rephrase or try a different topic.",
isError: true,
},
],
meta: {
durationMs: Date.now() - started,
agentMeta: buildErrorAgentMeta({
sessionId: sessionIdUsed,
provider,
model: model.id,
usageAccumulator,
lastRunPromptUsage,
lastAssistant,
lastTurnTotal,
}),
systemPromptReport: attempt.systemPromptReport,
error: { kind: "sensitive", message: errorText },
},
};
}
// Handle role ordering errors with a user-friendly message
if (/incorrect role information|roles must alternate/i.test(errorText)) {
return {

View File

@ -41,7 +41,8 @@ export type EmbeddedPiRunMeta = {
| "compaction_failure"
| "role_ordering"
| "image_size"
| "retry_limit";
| "retry_limit"
| "sensitive";
message: string;
};
/** Stop reason for the agent run (e.g., "completed", "tool_calls"). */