fix(compaction): omit hardcoded thinking=disabled to avoid Anthropic 400 (#550)

This commit is contained in:
BaaRai
2026-11-27 01:08:45 +01:00
parent 4851aeea5a
commit efe588a526
3 changed files with 169 additions and 3 deletions
+18 -2
View File
@@ -537,16 +537,32 @@ func buildCompressionInput(g *coldGraph, b block, nodeByID map[int64]*db.Node) s
return sb.String()
}
// isThinkingBetweenToolsError reports an Anthropic 400 telling the caller that
// the model rejects thinking.type="disabled" and requires "between_tools" instead
// (e.g. Claude Sonnet 5.5, #550).
func isThinkingBetweenToolsError(err error) bool {
if err == nil {
return false
}
msg := strings.ToLower(err.Error())
return strings.Contains(msg, "between_tools") && strings.Contains(msg, "thinking")
}
// compress runs the §4 body LLM call on one block. Uses the same model the agent
// runs on; thinking disabled (a pure summarization step).
// runs on without forcing thinking.type="disabled" (models such as Claude Sonnet
// 5.5 reject {"type":"disabled"} with HTTP 400; inheriting the profile's working
// thinking setting avoids sending an unsupported parameter, #550).
func (c *Compactor) compress(ctx context.Context, g *coldGraph, b block, nodeByID map[int64]*db.Node) (string, error) {
req := llm.CompletionRequest{
System: []string{compressionSystem},
Messages: []llm.Message{llm.UserText(buildCompressionInput(g, b, nodeByID))},
MaxTokens: 1500,
Thinking: "disabled",
}
msg, _, _, err := c.prov.Complete(ctx, req)
if err != nil && isThinkingBetweenToolsError(err) {
req.Thinking = "between_tools"
msg, _, _, err = c.prov.Complete(ctx, req)
}
if err != nil {
return "", err
}
+146
View File
@@ -0,0 +1,146 @@
package agent
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"github.com/Autumn-27/artex/db"
"github.com/Autumn-27/norma/llm"
)
func sampleCompactionInputs() (*coldGraph, block, map[int64]*db.Node) {
n1 := &db.Node{ID: 10, Kind: db.KindIntent, State: "done", Payload: json.RawMessage(`{"summary":"Scan /api/login"}`)}
n2 := &db.Node{ID: 11, Kind: db.KindFact, State: "confirmed", Payload: json.RawMessage(`{"summary":"Login returns 401"}`)}
g := newColdGraph(
[]cgNode{
{ID: 10, Kind: db.KindIntent, State: "done"},
{ID: 11, Kind: db.KindFact, State: "confirmed"},
},
[]cgEdge{
{From: 10, Rel: db.RelYields, To: 11},
},
)
b := block{Members: []int64{10, 11}}
return g, b, map[int64]*db.Node{10: n1, 11: n2}
}
// Regression for #550: compaction must not hardcode thinking={"type":"disabled"},
// which Claude Sonnet 5.5 rejects with HTTP 400 invalid_request_error.
func TestCompactorCompressDoesNotSendThinkingDisabled(t *testing.T) {
var (
mu sync.Mutex
bodies []map[string]any
)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
var m map[string]any
_ = json.Unmarshal(raw, &m)
mu.Lock()
bodies = append(bodies, m)
mu.Unlock()
if th, ok := m["thinking"].(map[string]any); ok && th["type"] == "disabled" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"type":"error","error":{"type":"invalid_request_error","message":"To turn thinking off on this model, send \"thinking\": {\"type\": \"between_tools\"} instead of {\"type\": \"disabled\"}. The model does not think before responding."}}`))
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"id":"msg_1","type":"message","role":"assistant","content":[{"type":"text","text":"Consolidated summary (#10,#11)."}],"stop_reason":"end_turn","usage":{"input_tokens":10,"output_tokens":6}}`))
}))
defer srv.Close()
prov, err := llm.NewProvider(llm.Config{
Format: llm.FormatAnthropic,
BaseURL: srv.URL,
APIKey: "test-key",
Model: "claude-sonnet-5-5",
})
if err != nil {
t.Fatal(err)
}
c := NewCompactor(prov, "claude-sonnet-5-5")
g, b, nodes := sampleCompactionInputs()
got, err := c.compress(context.Background(), g, b, nodes)
if err != nil {
t.Fatalf("compress failed: %v", err)
}
if got != "Consolidated summary (#10,#11)." {
t.Fatalf("unexpected compressed body: %q", got)
}
mu.Lock()
defer mu.Unlock()
if len(bodies) != 1 {
t.Fatalf("expected 1 request, got %d", len(bodies))
}
if _, hasThinking := bodies[0]["thinking"]; hasThinking {
t.Fatalf("compaction request must not send rejected thinking parameter when profile has none: %+v", bodies[0]["thinking"])
}
}
// Regression for #550: if a profile itself sets ThinkingType="disabled" and the
// provider returns the Anthropic 400 requesting thinking.type="between_tools",
// compaction retries once with "between_tools" and succeeds.
func TestCompactorCompressRetriesWithBetweenToolsOnAnthropic400(t *testing.T) {
var (
mu sync.Mutex
thinkingTypes []string
)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
var m map[string]any
_ = json.Unmarshal(raw, &m)
tt := ""
if th, ok := m["thinking"].(map[string]any); ok {
tt, _ = th["type"].(string)
}
mu.Lock()
thinkingTypes = append(thinkingTypes, tt)
mu.Unlock()
if tt == "disabled" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"type":"error","error":{"type":"invalid_request_error","message":"To turn thinking off on this model, send \"thinking\": {\"type\": \"between_tools\"} instead of {\"type\": \"disabled\"}."}}`))
return
}
if tt != "between_tools" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"type":"error","error":{"type":"invalid_request_error","message":"unexpected thinking mode"}}`))
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"id":"msg_2","type":"message","role":"assistant","content":[{"type":"text","text":"Summary via between_tools (#10,#11)."}],"stop_reason":"end_turn","usage":{"input_tokens":10,"output_tokens":6}}`))
}))
defer srv.Close()
prov, err := llm.NewProvider(llm.Config{
Format: llm.FormatAnthropic,
BaseURL: srv.URL,
APIKey: "test-key",
Model: "claude-sonnet-5-5",
ThinkingType: "disabled",
})
if err != nil {
t.Fatal(err)
}
c := NewCompactor(prov, "claude-sonnet-5-5")
g, b, nodes := sampleCompactionInputs()
got, err := c.compress(context.Background(), g, b, nodes)
if err != nil {
t.Fatalf("compress failed on between_tools fallback: %v", err)
}
if got != "Summary via between_tools (#10,#11)." {
t.Fatalf("unexpected compressed body: %q", got)
}
mu.Lock()
defer mu.Unlock()
if len(thinkingTypes) != 2 || thinkingTypes[0] != "disabled" || thinkingTypes[1] != "between_tools" {
t.Fatalf("expected thinking sequence [disabled between_tools], got %v", thinkingTypes)
}
}
+5 -1
View File
@@ -873,7 +873,11 @@ func (s *Server) emitTaskLLMTransition(t *Task, transition db.TaskLLMTransition,
}
mode := "automatic"
kind := "llm_switch"
summary := fmt.Sprintf("%s quota exhausted", llmAuditProfileLabel(previous))
reasonLabel := "failed consecutive calls"
if isQuotaExhaustedError(cause) {
reasonLabel = "quota exhausted"
}
summary := fmt.Sprintf("%s %s", llmAuditProfileLabel(previous), reasonLabel)
if transition.NextProfileID != nil {
summary += fmt.Sprintf(", subsequent calls switched to %s", llmAuditProfileLabel(next))
} else {