feat(runtime): surface and recover from model refusals

Claude Fable 5 safety classifiers decline requests with HTTP 200 +
stop_reason=refusal and empty content. The Anthropic stream parser
never read stop_reason, so a refused turn looked like a successful
empty response — the UI showed nothing and the agent appeared hung.

- llm: ChatEvent carries StopReason/StopCategory on EventDone;
  StopReasonRefusal constant
- anthropic: parse stop_reason + stop_details.category from
  message_delta (streaming) and the message (non-streaming); WARN log
  on refusal
- runtime Run + RunTurn: on refusal, retry the turn once on
  FallbackModel (refusals are model-specific; Opus-tier has no
  bio/cyber classifiers), discarding any partial output; without a
  fallback (or if the fallback also refuses) synthesize a visible
  'model declined this request' message with the policy category
  instead of silent emptiness
This commit is contained in:
sausheong committed 2026-06-10 21:13:48 +08:00
1 parent 3064a0da14
commit d5e8abf492
6 files changed
+320 -18

No files matched your search

+52
View File
@@ -485,6 +485,9 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon
streamSource := stream
retriedNonStreaming := false
retriedRefusal := false
var refused bool
var refusalCategory string
streamLoop:
for {
for event := range streamSource {
@@ -527,6 +530,8 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon
}()
case llm.EventDone:
refused = event.StopReason == llm.StopReasonRefusal
refusalCategory = event.StopCategory
if event.Usage != nil {
lastUsage = event.Usage
}
@@ -567,6 +572,53 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon
return
}
}
// Classifier/model refusal (Claude Fable 5+): HTTP 200,
// stop_reason=refusal, usually no content. Without handling,
// the turn ends as a silent empty success. Retry once on the
// fallback model (refusals are model-specific — Opus-tier
// has no bio/cyber classifiers); otherwise synthesize a
// visible explanation. Mid-stream refusals discard partial
// output per the API contract.
if refused {
if !retriedRefusal && r.FallbackModel != "" && r.FallbackModel != req.Model {
slog.Warn("model refused; retrying on fallback model",
"agent", r.AgentID,
"primary", req.Model,
"fallback", r.FallbackModel,
"category", refusalCategory)
tr.Mark("llm.refusal_fallback", "turn", turn,
"primary", req.Model, "fallback", r.FallbackModel,
"category", refusalCategory)
textContent.Reset()
toolCalls = nil
drainKickoffs(kickoffs)
kickoffs = map[string]chan kickoffResult{}
gotFirstToken = false
kickoffStopped = false
refused = false
retriedRefusal = true
req.Model = r.FallbackModel
retryStream, retryErr := r.LLM.ChatStream(ctx, req)
if retryErr != nil {
stopReason = "error"
r.emit(AgentEvent{Type: EventError, Error: retryErr})
return
}
streamSource = retryStream
continue streamLoop
}
textContent.Reset()
toolCalls = nil
drainKickoffs(kickoffs)
note := "The model declined this request (safety classifier"
if refusalCategory != "" {
note += ", category: " + refusalCategory
}
note += "). Try rephrasing, or switch this agent to a model without these classifiers."
textContent.WriteString(note)
r.emit(AgentEvent{Type: EventTextDelta, Text: note})
}
break streamLoop
}
tr.Mark("llm.stream_end", "turn", turn,