mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-19 22:50:55 +08:00
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 #327 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
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 ""
|
|
}
|