Repository navigation
fix: report provider refusals, do not rerun them as idle turns - #328
Merged
Merged
Conversation
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
approved these changes
Nov 18, 2026
BaaRai
left a comment
Owner
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-compatiblefinish_reason: "content_filter". Norma passes the stop reason through, andharness/query.goends any tool-less turn withReasonCompleted. As a result:steerHooks.Stoptakes the empty turn for an idle, thinking-only turn and injectsemptyTurnNudge. That resends the same conversation, up to the empty-response retry count.Within ARTEX, only
llmrec.Recordersees the stop reason.Change
llmrec:WithStopSink(ctx, func(stopReason string))is an optional context sink, in the style ofWithUsageSink. The recorder reports each completed call's final stop reason to it. That coversStreamafter the provider's last event, andCompleteon success. Without a sink nothing changes.agent:WithStopTracker,LastStopReasonandIsRefusalStop(refusal,content_filter).captureRunSessioninstalls one tracker per run. A nested run gets its own.terminalTextnow reports a completed run whose last call was refused asprovider refusal (stop_reason …). The detail says the intent was not completed.server: after a refusal,steerHooks.Stopdoes not inject the idle-turn rerun, and it logs why.CHANGELOG.md: a new entry under Unreleased → Fixed.Unchanged:
harness.TerminalReasonand intent flow. Callers still getReasonCompleted, and only the summary text and the nudge differ.end_turn, which are still nudged.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 ./...andgo vet ./...: OK.go test -count=1 -p 1 ./agent ./llmrec ./server: pass.go test -race -count=1 -p 1 ./agent ./llmrec ./server: pass.ARTEX_PG_DSNskipped themselves.New tests:
TestStopSinkSeesFinalStopReason(llmrec): the sink seesrefusalfrom a stream andstopfromComplete, 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): checksrefusalandcontent_filter. Controls:end_turnkeeps the completed wording, andmax_turnsisn't relabelled.TestSteerHooksStopDoesNotRerunProviderRefusal(server): no rerun and no quota used afterrefusalorcontent_filter. Control: an empty turn afterend_turnis still nudged.Negative control: with this PR's changes to
server/engine.go,agent/terminalreason.goandagent/capture.goreverted, and the new tests kept, both behaviour tests fail and show the original bug:TestTerminalTextNamesProviderRefusalgot(no text summary; terminal state completed: the model ended this turn normally…).TestSteerHooksStopDoesNotRerunProviderRefusalgot 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_recordscaptures; the behaviour after it is covered by the unit tests above.🤖 Generated with Claude Code