From 09c9e6cfc2e88fb251bdd8033f6906b7d8263985 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:26:21 +0100 Subject: [PATCH] fix(server): do not rerun intents refused by the NOA budget The NOA budget refusal now wraps an exported ErrNoaBudget sentinel, which survives the Norma harness unchanged; retryableWorkerModelError excludes it, so a deterministic refusal is no longer replayed twice by the engine. --- agent/noa_budget.go | 8 +++++++- agent/noa_budget_test.go | 21 +++++++++++++++++++++ server/engine.go | 4 +++- server/noa_budget_retry_test.go | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 server/noa_budget_retry_test.go diff --git a/agent/noa_budget.go b/agent/noa_budget.go index d1c8d0d..95009be 100644 --- a/agent/noa_budget.go +++ b/agent/noa_budget.go @@ -2,6 +2,7 @@ package agent import ( "context" + "errors" "fmt" "iter" @@ -12,6 +13,11 @@ import ( // noaRequestOverhead covers request framing the input estimate does not count. const noaRequestOverhead = 512 +// ErrNoaBudget marks a request refused by the NOA budget check. The refusal is +// deterministic (the same history fails the same way), so callers must not +// retry it. +var ErrNoaBudget = errors.New("noa request budget exceeded") + // noaBudgetProvider refuses, before any transport, a request that cannot fit the // window noa was configured with. noa trims what it is allowed to drop; when // the protected part (system prompt, recent turns, the output reservation) @@ -24,7 +30,7 @@ type noaBudgetProvider struct { func (p *noaBudgetProvider) check(req llm.CompletionRequest) error { estimate := sidequestion.EstimateInputTokens(req) + max(req.MaxTokens, 0) + noaRequestOverhead if estimate > p.window { - return fmt.Errorf("Norma NOA request estimate %d exceeds configured budget %d; protected context cannot fit, increase the configured window or start a new session", estimate, p.window) + return fmt.Errorf("%w: estimate %d exceeds configured budget %d; protected context cannot fit, increase the configured window or start a new session", ErrNoaBudget, estimate, p.window) } return nil } diff --git a/agent/noa_budget_test.go b/agent/noa_budget_test.go index 6a1c359..2f30dd9 100644 --- a/agent/noa_budget_test.go +++ b/agent/noa_budget_test.go @@ -8,10 +8,25 @@ import ( "testing" "github.com/Autumn-27/norma/agentcore" + "github.com/Autumn-27/norma/harness" "github.com/Autumn-27/norma/llm" "github.com/Autumn-27/norma/noaadapter" ) +// A worker whose protected context exceeds a tiny NOA window ends with +// model_error and an error that still wraps ErrNoaBudget after the Norma harness +// and the capture layer: the engine's retry check relies on that. +func TestWorkerNoaBudgetRefusalKeepsSentinelThroughHarness(t *testing.T) { + f := newSessionFixture(t) + inner := &countingProvider{} + w := NewWorker(inner, "test-model", t.TempDir(), nil, 2000, 3) + w.SetNoaEnabled(func() bool { return true }) + reason, _, err := w.Execute(t.Context(), "work#1", f.expID, nil, f.ts, f.intent, passHooks{}, nil, nil, nil) + if reason != harness.ReasonModelError || !errors.Is(err, ErrNoaBudget) || inner.calls != 0 { + t.Fatalf("reason=%q err=%v provider calls=%d; want model_error wrapping ErrNoaBudget and no call", reason, err, inner.calls) + } +} + // countingProvider answers with one text turn and counts the calls it receives. type countingProvider struct{ calls int } @@ -83,6 +98,9 @@ func TestNoaBudgetRefusesIrreducibleRequestBeforeTransport(t *testing.T) { if err == nil || !strings.Contains(err.Error(), "exceeds configured budget 32000") || inner.calls != 0 { t.Fatalf("err=%v provider calls=%d", err, inner.calls) } + if !errors.Is(err, ErrNoaBudget) { + t.Fatalf("atomic refusal does not wrap ErrNoaBudget: %v", err) + } if _, _, _, err := p.Complete(t.Context(), llm.CompletionRequest{System: []string{"short"}, MaxTokens: 1000}); err != nil || inner.calls != 1 { t.Fatalf("a request that fits was refused or not sent: err=%v calls=%d", err, inner.calls) } @@ -103,6 +121,9 @@ func TestNoaBudgetRefusesIrreducibleRequestBeforeTransport(t *testing.T) { if err == nil || !strings.Contains(err.Error(), "exceeds configured budget 32000") || events != 0 || inner.calls != 0 { t.Fatalf("err=%v events=%d provider calls=%d", err, events, inner.calls) } + if !errors.Is(err, ErrNoaBudget) { + t.Fatalf("stream refusal does not wrap ErrNoaBudget: %v", err) + } for _, e := range p.Stream(t.Context(), llm.CompletionRequest{System: []string{"short"}, MaxTokens: 1000}) { if e != nil { t.Fatal(e) diff --git a/server/engine.go b/server/engine.go index 12d492c..eef8645 100644 --- a/server/engine.go +++ b/server/engine.go @@ -79,8 +79,10 @@ var errWorkControlConflict = errors.New("work control conflict") // retryableWorkerModelError excludes errors already handled by the task router. // In particular, a quota error after partial streaming advances the task cursor // for the next LLM call but must not replay this whole intent on the backup. +// A NOA budget refusal is deterministic and never reached the provider, so a +// rerun would only fail the same way. func retryableWorkerModelError(reason harness.TerminalReason, err error) bool { - return reason == harness.ReasonModelError && !isTaskLLMRuntimeError(err) + return reason == harness.ReasonModelError && !isTaskLLMRuntimeError(err) && !errors.Is(err, agent.ErrNoaBudget) } // Engine drives the event-driven exploration loop with real LLM agents diff --git a/server/noa_budget_retry_test.go b/server/noa_budget_retry_test.go new file mode 100644 index 0000000..525067d --- /dev/null +++ b/server/noa_budget_retry_test.go @@ -0,0 +1,32 @@ +package server + +import ( + "errors" + "fmt" + "testing" + + "github.com/Autumn-27/artex/agent" + "github.com/Autumn-27/norma/harness" +) + +// A NOA budget refusal is deterministic: the engine must not rerun the intent, +// whether the sentinel arrives bare or wrapped. +func TestWorkerModelErrorRetrySkipsNoaBudget(t *testing.T) { + for _, tc := range []struct { + name string + err error + want bool + }{ + {"noa budget", agent.ErrNoaBudget, false}, + {"noa budget wrapped", fmt.Errorf("%w: estimate 64000 exceeds configured budget 32000", agent.ErrNoaBudget), false}, + {"noa budget wrapped twice", fmt.Errorf("worker: %w", fmt.Errorf("%w: estimate", agent.ErrNoaBudget)), false}, + {"generic model error", errors.New("connection reset by peer"), true}, + {"task router error", &taskLLMError{taskID: "7", cause: errors.New("insufficient_quota")}, false}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := retryableWorkerModelError(harness.ReasonModelError, tc.err); got != tc.want { + t.Fatalf("retryableWorkerModelError(model_error, %v) = %v, want %v", tc.err, got, tc.want) + } + }) + } +}