Skip to content

fix: report provider refusals, do not rerun them as idle turns - #328

Merged
BaaRai merged 1 commit into
BaaRai:mainfrom
Michael-JRead:fix/surface-provider-refusals
Nov 18, 2026
Merged

BaaRai merged 1 commit into
BaaRai:mainfrom
Michael-JRead:fix/surface-provider-refusals

Conversation

@Michael-JRead

Copy link
Copy Markdown
Contributor

Fixes #327.

Problem

A turn the provider's safety system declines has no text and no tool calls. That's Anthropic stop_reason: "refusal" or OpenAI-compatible finish_reason: "content_filter". Norma passes the stop reason through, and harness/query.go ends any tool-less turn with ReasonCompleted. As a result:

  • The summary is misleading. It reads "terminal state completed: the model ended this turn normally…", so a declined intent looks finished.
  • The refusal is rerun. steerHooks.Stop takes the empty turn for an idle, thinking-only turn and injects emptyTurnNudge. That resends the same conversation, up to the empty-response retry count.

Within ARTEX, only llmrec.Recorder sees the stop reason.

Change

  • llmrec: WithStopSink(ctx, func(stopReason string)) is an optional context sink, in the style of WithUsageSink. The recorder reports each completed call's final stop reason to it. That covers Stream after the provider's last event, and Complete on success. Without a sink nothing changes.
  • agent:
    • New helpers: WithStopTracker, LastStopReason and IsRefusalStop (refusal, content_filter).
    • captureRunSession installs one tracker per run. A nested run gets its own.
    • terminalText now reports a completed run whose last call was refused as provider refusal (stop_reason …). The detail says the intent was not completed.
  • server: after a refusal, steerHooks.Stop does not inject the idle-turn rerun, and it logs why.
  • CHANGELOG.md: a new entry under Unreleased → Fixed.

Unchanged:

  • No retry, fallback or failover on a refusal.
  • The returned harness.TerminalReason and intent flow. Callers still get ReasonCompleted, and only the summary text and the nudge differ.
  • Thinking-only turns after an ordinary end_turn, which are still nudged.
  • Every other terminal state.

A mid-stream refusal that already streamed some text keeps that text as the summary, as before. That case is left out of this PR.

Tested

All runs used Go 1.26.10 on linux/amd64, from a clean clone of this branch:

  • gofmt -l $(git ls-files '*.go'): clean.
  • go build ./... and go vet ./...: OK.
  • go test -count=1 -p 1 ./agent ./llmrec ./server: pass.
  • go test -race -count=1 -p 1 ./agent ./llmrec ./server: pass.
  • There was no PostgreSQL in my environment, so tests that need ARTEX_PG_DSN skipped themselves.

New tests:

  • TestStopSinkSeesFinalStopReason (llmrec): the sink sees refusal from a stream and stop from Complete, and a call without a sink still works.
  • TestIsRefusalStop (agent): which stop reasons count as refusals.
  • TestStopTrackerFedByRecorder (agent): a recorded call updates the tracker, the latest call wins, and a nested run doesn't leak into the outer one.
  • TestTerminalTextNamesProviderRefusal (agent): checks refusal and content_filter. Controls: end_turn keeps the completed wording, and max_turns isn't relabelled.
  • TestSteerHooksStopDoesNotRerunProviderRefusal (server): no rerun and no quota used after refusal or content_filter. Control: an empty turn after end_turn is still nudged.

Negative control: with this PR's changes to server/engine.go, agent/terminalreason.go and agent/capture.go reverted, and the new tests kept, both behaviour tests fail and show the original bug:

  • TestTerminalTextNamesProviderRefusal got (no text summary; terminal state completed: the model ended this turn normally…).
  • TestSteerHooksStopDoesNotRerunProviderRefusal got the [Idle-turn reminder] injected after a refusal.

Not tested: a live run against a provider after the change. The behaviour before the change comes from llm_records captures; the behaviour after it is covered by the unit tests above.

🤖 Generated with Claude Code

A turn the provider's safety system declines (Anthropic stop_reason
"refusal", OpenAI-compatible finish_reason "content_filter") has no text
and no tool calls. Norma passes the stop reason through and the harness
ends any tool-less turn with ReasonCompleted, so the run summary said the
model "ended this turn normally", and steerHooks.Stop took the empty turn
for an idle one and resent the conversation up to the empty-response retry
count.

The LLM recorder is the one layer that sees the stop reason. It now hands
each completed call's stop reason to an optional context sink
(llmrec.WithStopSink). captureRunSession installs a per-run tracker
(agent.WithStopTracker) on that sink. With it:

- steerHooks.Stop does not inject the idle-turn rerun after a refusal;
- terminalText reports "provider refusal (stop_reason ...)" and says the
  intent was not completed, instead of the completed wording.

Other terminal states and ordinary empty turns are unchanged.

Fixes BaaRai#327

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the change. The stop sink is the right seam: the recorder is the only layer that sees the stop reason, and a per-run tracker keeps the nudge and the terminal-text decision local. Skipping the idle-turn rerun in steerHooks.Stop matches the failure mode, since a rerun resends the same conversation, and the tests cover both provider shapes, the ordinary empty turn, and nested-run isolation. The terminal text now names the refusal while the harness reason stays completed, which is what actually happened. Thanks.

@BaaRai
BaaRai merged commit 4c486e1 into BaaRai:main Nov 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provider refusals (stop_reason "refusal") end as "completed" and are rerun as idle turns

3 participants