Merge pull request #328 from Michael-JRead/fix/surface-provider-refusals

fix: report provider refusals, do not rerun them as idle turns
This commit is contained in:
BaaRai
2026-11-18 12:12:19 +01:00
committed by GitHub
9 changed files with 320 additions and 1 deletions
+1
View File
@@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b
### Fixed
- **Provider refusals reported as refusals**: a turn the provider's safety system declined (Anthropic `stop_reason: "refusal"`, OpenAI-compatible `finish_reason: "content_filter"`) has no text and no tool calls, so it ended as `completed` with "the model ended this turn normally", and the idle-turn rerun resent the same conversation up to the empty-response retry count, where it is expected to be refused again. The LLM recorder now hands each call's stop reason to the run (`llmrec.WithStopSink`, `agent.WithStopTracker`). After a refusal the idle-turn rerun is skipped, and the run summary says the provider refused the turn and the intent was not completed.
- **LLM health badge on every task path**: `/system/llm/` read a breaker state that only the failover Pool wrote, so a profile used by an explicit task chain, by a bound agent with failover off, or as the global provider stayed "Healthy" through a real outage. Every task LLM attempt now records its outcome through the same policy the Pool uses, keyed by the profile that served it. The LLM recording page also explains an empty list when the `llm_record` toggle is off instead of looking broken.
- **LLM health persistence** (#304): persisted breaker rows are applied in sequence at startup and written before the database closes, and a slow write no longer holds the registry lock while it runs.
- **Skill visibility survives restarts**: turning a skill off now writes an explicit disabled row instead of deleting it, so the startup seed, which only inserts missing rows, can no longer bring the skill back. Skills that were turned off before this version have no row to keep, so they come back once after the upgrade and must be turned off again.
+3
View File
@@ -37,6 +37,9 @@ func captureRun(ctx context.Context, opts agentcore.Options, input string, emit
func captureRunSession(ctx context.Context, s *agentcore.Session, input string, emit func(db.Activity)) (string, harness.TerminalReason, error) {
ctx, auditTrace := intercept.WithTrace(ctx, input, approvalHistory(s.Messages()))
defer auditTrace.Finish()
// Observe each model call's stop reason, so a provider refusal is reported as
// one (terminalText) and is not answered with an idle-turn rerun (steerHooks).
ctx = WithStopTracker(ctx)
var reason harness.TerminalReason
rec := func(r db.Activity) {
if r.Kind == "text" || r.Kind == "tool_result" {
+57
View File
@@ -0,0 +1,57 @@
package agent
import (
"context"
"sync"
"github.com/Autumn-27/artex/llmrec"
)
// IsRefusalStop reports whether a provider stop reason means the provider's
// safety system declined the request rather than the model ending its turn.
// Anthropic returns stop_reason "refusal" with an empty or partial message;
// OpenAI-compatible endpoints return finish_reason "content_filter", which Norma
// passes through unmapped.
func IsRefusalStop(stopReason string) bool {
return stopReason == "refusal" || stopReason == "content_filter"
}
// stopTracker keeps the stop reason of the latest model call of one run. The
// harness does not expose it: a refused turn has no text and no tool calls, so it
// ends as ReasonCompleted, the same as a model that chose to stop.
type stopTracker struct {
mu sync.Mutex
last string
}
func (t *stopTracker) set(stopReason string) {
t.mu.Lock()
t.last = stopReason
t.mu.Unlock()
}
func (t *stopTracker) get() string {
t.mu.Lock()
defer t.mu.Unlock()
return t.last
}
type stopTrackerKey struct{}
// WithStopTracker attaches a fresh stop tracker to ctx and subscribes it to the
// LLM recorder's stop sink, so every model call made under the returned context
// updates it. captureRunSession installs one per run; a nested run gets its own.
func WithStopTracker(ctx context.Context) context.Context {
t := &stopTracker{}
ctx = context.WithValue(ctx, stopTrackerKey{}, t)
return llmrec.WithStopSink(ctx, t.set)
}
// LastStopReason returns the stop reason of the latest model call made under ctx
// by the run that owns it, or "" when none was observed.
func LastStopReason(ctx context.Context) string {
if t, _ := ctx.Value(stopTrackerKey{}).(*stopTracker); t != nil {
return t.get()
}
return ""
}
+113
View File
@@ -0,0 +1,113 @@
package agent
import (
"context"
"iter"
"strings"
"testing"
"time"
"github.com/Autumn-27/artex/llmrec"
"github.com/Autumn-27/norma/harness"
"github.com/Autumn-27/norma/llm"
)
// stopProvider ends every streamed call with the given stop reason and no content,
// the shape of a provider refusal before any output.
type stopProvider struct{ stop string }
func (p stopProvider) Stream(context.Context, llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
return func(yield func(llm.StreamEvent, error) bool) {
if !yield(llm.StreamEvent{Type: llm.SEMessageStart, Usage: llm.Usage{InputTokens: 41}}, nil) {
return
}
if !yield(llm.StreamEvent{Type: llm.SEMessageDelta, StopReason: p.stop}, nil) {
return
}
yield(llm.StreamEvent{Type: llm.SEMessageStop}, nil)
}
}
func (p stopProvider) Complete(context.Context, llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
return llm.Message{Role: llm.RoleAssistant}, p.stop, llm.Usage{InputTokens: 41}, nil
}
// streamUnder runs one recorded streaming call with the given stop reason under ctx.
func streamUnder(t *testing.T, ctx context.Context, stop string) {
t.Helper()
rec := llmrec.Wrap(stopProvider{stop: stop}, nil, "model", "profile", "", "", func() bool { return false })
for _, err := range rec.Stream(ctx, llm.CompletionRequest{}) {
if err != nil {
t.Fatal(err)
}
}
}
func TestIsRefusalStop(t *testing.T) {
for stop, want := range map[string]bool{
"refusal": true, "content_filter": true,
"end_turn": false, "tool_use": false, "max_tokens": false, "stop": false, "": false,
} {
if got := IsRefusalStop(stop); got != want {
t.Errorf("IsRefusalStop(%q) = %v, want %v", stop, got, want)
}
}
}
// The tracker installed per run is fed by the LLM recorder, and each run sees only
// its own calls.
func TestStopTrackerFedByRecorder(t *testing.T) {
ctx := WithStopTracker(context.Background())
if got := LastStopReason(ctx); got != "" {
t.Fatalf("fresh tracker reports %q", got)
}
streamUnder(t, ctx, "refusal")
if got := LastStopReason(ctx); got != "refusal" {
t.Fatalf("LastStopReason = %q, want refusal", got)
}
// A later call overwrites it: the tracker describes the latest call only.
streamUnder(t, ctx, "end_turn")
if got := LastStopReason(ctx); got != "end_turn" {
t.Fatalf("LastStopReason = %q, want end_turn", got)
}
// A nested run gets its own tracker and does not leak into the outer one.
inner := WithStopTracker(ctx)
streamUnder(t, inner, "content_filter")
if LastStopReason(inner) != "content_filter" || LastStopReason(ctx) != "end_turn" {
t.Fatalf("nested tracker leaked: inner=%q outer=%q", LastStopReason(inner), LastStopReason(ctx))
}
if got := LastStopReason(context.Background()); got != "" {
t.Fatalf("context without a tracker reports %q", got)
}
}
func TestTerminalTextNamesProviderRefusal(t *testing.T) {
trace := &runTrace{startedAt: time.Now()}
for _, stop := range []string{"refusal", "content_filter"} {
ctx := WithStopTracker(context.Background())
streamUnder(t, ctx, stop)
summary, detail := terminalText(ctx, &harness.Terminal{Reason: harness.ReasonCompleted}, trace)
if !strings.Contains(summary, "provider refusal (stop_reason "+stop+")") || strings.Contains(summary, "ended this turn normally") {
t.Fatalf("%s: summary does not name the refusal: %q", stop, summary)
}
if !strings.Contains(detail, "stop_reason `"+stop+"`") || !strings.Contains(detail, "was not completed") {
t.Fatalf("%s: detail does not explain the refusal:\n%s", stop, detail)
}
}
// Control: an ordinary end of turn keeps the completed wording.
ctx := WithStopTracker(context.Background())
streamUnder(t, ctx, "end_turn")
summary, _ := terminalText(ctx, &harness.Terminal{Reason: harness.ReasonCompleted}, trace)
if !strings.Contains(summary, "terminal state completed") || strings.Contains(summary, "refusal") {
t.Fatalf("ordinary end of turn misreported: %q", summary)
}
// A refusal stop only relabels a completed run; other terminal states keep theirs.
ctx = WithStopTracker(context.Background())
streamUnder(t, ctx, "refusal")
summary, _ = terminalText(ctx, &harness.Terminal{Reason: harness.ReasonMaxTurns}, trace)
if !strings.Contains(summary, "run budget cap") {
t.Fatalf("max_turns relabelled by a refusal stop: %q", summary)
}
}
+14 -1
View File
@@ -44,6 +44,9 @@ var reasonHint = map[harness.TerminalReason]string{
harness.ReasonAbortedTools: "the run was cancelled during tool execution",
}
// refusalHint explains a turn the provider declined (see IsRefusalStop).
const refusalHint = "the provider's safety system declined this request, so the model produced no output; the intent was not completed, and resending the same request unchanged is expected to be refused again. With LLM Recording on, the raw response carries the provider's stop details"
// terminalText renders a terminal event with no final text into a compact summary
// and a Markdown detail block.
func terminalText(ctx context.Context, term *harness.Terminal, tr *runTrace) (string, string) {
@@ -54,6 +57,10 @@ func terminalText(ctx context.Context, term *harness.Terminal, tr *runTrace) (st
if reason == "" && ctx.Err() != nil {
aborted = true
}
// A provider refusal has no text and no tool calls, so the harness ends it as
// ReasonCompleted. Only the recorded stop reason tells the two apart.
stop := LastStopReason(ctx)
refused := reason == harness.ReasonCompleted && IsRefusalStop(stop)
var sum string
if aborted {
@@ -71,6 +78,8 @@ func terminalText(ctx context.Context, term *harness.Terminal, tr *runTrace) (st
sum = "(run interrupted: " + short + "; stopped during " + stage + progressSuffix(term, tr) + ", unfinished)"
} else if reason == harness.ReasonMaxTurns || reason == harness.ReasonTimeout {
sum = "(run budget cap reached (" + string(reason) + "); wrap-up wrote back facts" + progressSuffix(term, tr) + "; no text summary this run)"
} else if refused {
sum = "(no text summary; provider refusal (stop_reason " + stop + "): " + firstLine(refusalHint, 80) + ")"
} else {
hint := terminalReasonHint(reason)
sum = "(no text summary; terminal state " + terminalReasonLabel(reason) + ": " + firstLine(hint, 80) + ")"
@@ -80,7 +89,11 @@ func terminalText(ctx context.Context, term *harness.Terminal, tr *runTrace) (st
b.WriteString(sum)
b.WriteString("\n\n")
displayReason := terminalReasonLabel(reason)
fmt.Fprintf(&b, "- **Terminal state**: `%s` - %s\n", displayReason, terminalReasonHint(reason))
if refused {
fmt.Fprintf(&b, "- **Terminal state**: `%s`, but the last model call ended with stop_reason `%s` - %s\n", displayReason, stop, refusalHint)
} else {
fmt.Fprintf(&b, "- **Terminal state**: `%s` - %s\n", displayReason, terminalReasonHint(reason))
}
if aborted {
code, _, why, ok := AbortReason(ctx)
if ok {
+23
View File
@@ -69,6 +69,26 @@ func WithUsageSink(ctx context.Context, sink func(llm.Usage)) context.Context {
return context.WithValue(ctx, usageSinkContextKey{}, sink)
}
type stopSinkContextKey struct{}
// WithStopSink attaches a callback that receives the stop reason of every call
// made under ctx that completes normally, once per call, after the provider's
// last event ("" when the provider reported none). The harness keeps the stop
// reason only long enough to decide whether a turn used tools, so a host that
// must tell a provider refusal (Anthropic stop_reason "refusal", or
// finish_reason "content_filter" on OpenAI-compatible endpoints) from an
// ordinary end of turn observes it here.
func WithStopSink(ctx context.Context, sink func(stopReason string)) context.Context {
return context.WithValue(ctx, stopSinkContextKey{}, sink)
}
// reportStop hands stopReason to the ctx stop sink, if one is attached.
func reportStop(ctx context.Context, stopReason string) {
if sink, _ := ctx.Value(stopSinkContextKey{}).(func(string)); sink != nil {
sink(stopReason)
}
}
// Recorder wraps an llm.Provider and records every completion call.
type Recorder struct {
inner llm.Provider
@@ -223,6 +243,7 @@ func (r *Recorder) Stream(ctx context.Context, req llm.CompletionRequest) iter.S
}
// Stream completed normally.
reportStop(ctx, stopReason)
finish(streamErr)
}
}
@@ -254,6 +275,8 @@ func (r *Recorder) Complete(ctx context.Context, req llm.CompletionRequest) (llm
status := "ok"
if err != nil {
status = "error"
} else {
reportStop(ctx, stopReason)
}
r.recordUsage(ctx, taskID, expID, worker, usage, int(time.Since(start).Milliseconds()), status)
if recordBodies {
+42
View File
@@ -94,6 +94,48 @@ func TestUsageSinkSeesMeteredCalls(t *testing.T) {
}
}
// The stop sink sees the final stop reason of each call that completes, which the
// harness otherwise drops: a refusal is an empty, tool-less turn like any other.
func TestStopSinkSeesFinalStopReason(t *testing.T) {
var got []string
ctx := WithStopSink(context.Background(), func(stop string) { got = append(got, stop) })
recorder := Wrap(refusalStreamProvider{}, nil, "model", "profile", "", "", func() bool { return false })
for _, err := range recorder.Stream(ctx, llm.CompletionRequest{}) {
if err != nil {
t.Fatal(err)
}
}
if _, _, _, err := recorder.Complete(ctx, llm.CompletionRequest{}); err != nil {
t.Fatal(err)
}
if len(got) != 2 || got[0] != "refusal" || got[1] != "stop" {
t.Fatalf("sink saw %q, want [refusal stop]", got)
}
// Without a sink on the context nothing is reported and nothing breaks.
for _, err := range recorder.Stream(context.Background(), llm.CompletionRequest{}) {
if err != nil {
t.Fatal(err)
}
}
}
// refusalStreamProvider streams what Anthropic sends for a refusal before any
// output: message_start, then message_delta with stop_reason "refusal" and no
// content blocks at all.
type refusalStreamProvider struct{ completeProvider }
func (refusalStreamProvider) Stream(context.Context, llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
return func(yield func(llm.StreamEvent, error) bool) {
if !yield(llm.StreamEvent{Type: llm.SEMessageStart, Usage: llm.Usage{InputTokens: 41}}, nil) {
return
}
if !yield(llm.StreamEvent{Type: llm.SEMessageDelta, StopReason: "refusal", Usage: llm.Usage{InputTokens: 41}}, nil) {
return
}
yield(llm.StreamEvent{Type: llm.SEMessageStop}, nil)
}
}
type meteredStreamProvider struct{ completeProvider }
func (meteredStreamProvider) Stream(context.Context, llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
+8
View File
@@ -672,6 +672,14 @@ func (h steerHooks) Stop(ctx context.Context, messages []llm.Message) (bool, []s
if prevent || len(blocking) > 0 || h.nudges == nil || h.limit <= 0 || !isThinkingOnlyTurn(messages) {
return prevent, blocking, msg
}
// A refused turn is empty as well, but there the provider declined the request;
// the model did not think and forget to act. A rerun resends the same
// conversation and is refused again, so let the run end and be reported as a
// refusal (terminalText) instead.
if stop := agent.LastStopReason(ctx); agent.IsRefusalStop(stop) {
log.Printf("[work %s] provider refused the turn (stop_reason=%s), not injecting an idle-turn rerun", h.label, stop)
return prevent, blocking, msg
}
n := h.nudges.Add(1)
if n > int64(h.limit) {
log.Printf("[work %s] idle turns (thinking only, no body text, no tools) reached the rerun cap %d, letting it finish", h.label, h.limit)
+59
View File
@@ -2,12 +2,71 @@ package server
import (
"context"
"iter"
"sync/atomic"
"testing"
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/llmrec"
"github.com/Autumn-27/norma/llm"
)
// stopReasonProvider ends a streamed call with the given stop reason and no
// content, the shape of a provider refusal before any output.
type stopReasonProvider struct{ stop string }
func (p stopReasonProvider) Stream(context.Context, llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
return func(yield func(llm.StreamEvent, error) bool) {
if !yield(llm.StreamEvent{Type: llm.SEMessageStart, Usage: llm.Usage{InputTokens: 41}}, nil) {
return
}
if !yield(llm.StreamEvent{Type: llm.SEMessageDelta, StopReason: p.stop}, nil) {
return
}
yield(llm.StreamEvent{Type: llm.SEMessageStop}, nil)
}
}
func (p stopReasonProvider) Complete(context.Context, llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
return llm.Message{Role: llm.RoleAssistant}, p.stop, llm.Usage{}, nil
}
// runCtxAfter returns a run context whose latest recorded model call ended with stop.
func runCtxAfter(t *testing.T, stop string) context.Context {
t.Helper()
ctx := agent.WithStopTracker(context.Background())
rec := llmrec.Wrap(stopReasonProvider{stop: stop}, nil, "model", "profile", "", "", func() bool { return false })
for _, err := range rec.Stream(ctx, llm.CompletionRequest{}) {
if err != nil {
t.Fatal(err)
}
}
return ctx
}
// A provider refusal leaves an assistant turn with no text and no tools, the same
// shape as an idle turn, but rerunning it only sends the same conversation to be
// refused again.
func TestSteerHooksStopDoesNotRerunProviderRefusal(t *testing.T) {
refused := []llm.Message{llm.UserText("start"), {Role: llm.RoleAssistant}}
for _, stop := range []string{"refusal", "content_filter"} {
h := steerHooks{nudges: &atomic.Int64{}, limit: defaultEmptyTurnNudges, label: "worker-1 · #1"}
prevent, blocking, _ := h.Stop(runCtxAfter(t, stop), refused)
if prevent || blocking != nil {
t.Fatalf("%s: refusal was rerun: prevent=%v blocking=%v", stop, prevent, blocking)
}
if n := h.nudges.Load(); n != 0 {
t.Fatalf("%s: refusal consumed the idle-turn quota: %d", stop, n)
}
}
// Control: the same empty turn after an ordinary end of turn is still nudged.
h := steerHooks{nudges: &atomic.Int64{}, limit: defaultEmptyTurnNudges, label: "worker-1 · #1"}
if _, blocking, _ := h.Stop(runCtxAfter(t, "end_turn"), refused); len(blocking) != 1 || blocking[0] != emptyTurnNudge {
t.Fatalf("idle turn after end_turn not nudged: %v", blocking)
}
}
// Identifying and nudging empty turns (thinking only, no text, no tools); see steerHooks.Stop.
func assistantThinking(text string) llm.Message {