Compare commits

..
Author SHA1 Message Date
BaaRai 02a67bf9c5 fix(intercept): deny always-allow for git, cmake, ctest and cpack
The argv allow denylist is the only gate that stops an always-allow rule
from naming a program whose flags can run arbitrary commands. git (aliases,
hooks, -c), cmake (-P), ctest and cpack run commands from user-controlled
files, so an allow prefix for them is never safe. Also key the arity table
by the normalised program name so a path-qualified binary derives the same
prefix length as the bare name.
2026-11-20 01:23:45 +01:00
11 changed files with 83 additions and 304 deletions
+4 -4
View File
@@ -15,12 +15,12 @@ import (
func TestArgvAllowKeepsDenyAndTargetGate(t *testing.T) {
ic := intercept.New(nil)
ic.SetRulesForTest([]db.InterceptRule{
{ID: 1, Name: "no force push", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "string",
{ID: 1, Name: "no force flag", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "string",
Pattern: "--force", Action: "deny", Message: "force push is forbidden"},
{ID: 2, Name: "argv allow curl", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
Pattern: `["curl"]`, Action: "allow"},
{ID: 3, Name: "argv allow git push", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
Pattern: `["git","push"]`, Action: "allow"},
{ID: 3, Name: "argv allow nmap", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
Pattern: `["nmap"]`, Action: "allow"},
})
g := NewWithInterceptor(ic)
gate := &fakeGate{t: t, check: unpinnedOnce, request: func(int, []intercept.TargetRequest, *intercept.Decision) intercept.TargetResult {
@@ -29,7 +29,7 @@ func TestArgvAllowKeepsDenyAndTargetGate(t *testing.T) {
g.gate = gate
ctx := gateOn(context.Background(), "7", time.Minute)
if block, msg := pre(g, ctx, "Bash", `{"command":"git push --force origin"}`); !block || !strings.Contains(msg, "force push is forbidden") {
if block, msg := pre(g, ctx, "Bash", `{"command":"nmap --force 10.0.0.1"}`); !block || !strings.Contains(msg, "force push is forbidden") {
t.Fatalf("deny over argv allow: block=%v msg=%q", block, msg)
}
if gate.checks != 0 {
+1 -3
View File
@@ -530,9 +530,7 @@ func TestGateOffLeavesEveryBranchUnchanged(t *testing.T) {
for name, ctx := range off {
before := len(g.rows())
// 1. A tool outside the enabled set: passes, nothing is written.
// Monitor and ExecuteExtraTool are in the default set now, so use read-only
// tools and an MCP name (inner names are not in the set).
for _, c := range []struct{ tool, input string }{{"list_assets", curl(deny)}, {"mcp__scan__run", `{"target":"203.0.113.7","note":"` + deny + `"}`}} {
for _, c := range []struct{ tool, input string }{{"Monitor", curl(deny)}, {"mcp__scan__run", `{"target":"203.0.113.7","note":"` + deny + `"}`}} {
if block, msg := pre(guard, ctx, c.tool, c.input); block {
t.Fatalf("%s: %s blocked: %s", name, c.tool, msg)
}
+2
View File
@@ -27,6 +27,8 @@ var alwaysAllowDenylist = func() map[string]bool {
// package managers and build tools: run package scripts and hooks
"npm npx pnpm yarn bunx corepack pip pipx uv uvx poetry conda composer gem bundle bundler rake cargo go " +
"mvn gradle dotnet apt apt-get dpkg dnf yum rpm pacman apk brew snap flatpak",
// version control and build generators: run hooks, aliases and script files
"git cmake ctest cpack",
// orchestration: run images, playbooks and plans
"docker docker-compose podman nerdctl kubectl helm ansible ansible-playbook terraform pulumi vagrant",
// destructive
+27 -7
View File
@@ -13,12 +13,13 @@ func TestArgvAllowDenylist(t *testing.T) {
"python", "python3", "python3.11", "/usr/bin/python3.11", "php8.2", "ruby3.2", "perl5", "pip3",
"env", "/usr/bin/env", "bash", "/bin/sh", "find", "rm", "mkfs", "mkfs.ext4", "/sbin/mkfs.xfs",
"docker", "kubectl", "npm", "sudo", "xargs", "Rscript", ".",
"git", "/usr/bin/git", "cmake", "cmake3", "/usr/bin/cmake", "ctest", "cpack",
} {
if !ArgvAllowDenied(prog) {
t.Errorf("%q is not denied", prog)
}
}
for _, prog := range []string{"nmap", "git", "/usr/bin/git", "curl", "ls", "gh", "x", "7z", "pythonic"} {
for _, prog := range []string{"nmap", "curl", "ls", "gh", "x", "7z", "pythonic"} {
if ArgvAllowDenied(prog) {
t.Errorf("%q is denied, want allowable", prog)
}
@@ -30,8 +31,8 @@ func TestAlwaysAllowPrefix(t *testing.T) {
b, _ := json.Marshal(map[string]string{"command": cmd})
return b
}
if got, err := AlwaysAllowPrefix("Bash", bash("git push --force origin main")); err != nil || !slices.Equal(got, []string{"git", "push"}) {
t.Fatalf("git push: %q %v", got, err)
if got, err := AlwaysAllowPrefix("Bash", bash("gh pr list --limit 5")); err != nil || !slices.Equal(got, []string{"gh", "pr", "list"}) {
t.Fatalf("gh pr list: %q %v", got, err)
}
if got, err := AlwaysAllowPrefix("Bash", bash("nmap -p 80 10.0.0.1")); err != nil || !slices.Equal(got, []string{"nmap"}) {
t.Fatalf("nmap: %q %v", got, err)
@@ -40,10 +41,10 @@ func TestAlwaysAllowPrefix(t *testing.T) {
tool string
input []byte
}{
"not Bash": {"shell_open", bash("git push")},
"compound": {"Bash", bash("git push; id")},
"substitution": {"Bash", bash("git push $(id)")},
"redirection": {"Bash", bash("git push > out")},
"not Bash": {"shell_open", bash("gh pr list")},
"compound": {"Bash", bash("gh pr list; id")},
"substitution": {"Bash", bash("gh pr list $(id)")},
"redirection": {"Bash", bash("gh pr list > out")},
"empty": {"Bash", bash("")},
"no command": {"Bash", []byte(`{}`)},
"not json": {"Bash", []byte(`not json`)},
@@ -52,6 +53,11 @@ func TestAlwaysAllowPrefix(t *testing.T) {
"find": {"Bash", bash("find . -exec sh -c id ;")},
"rm": {"Bash", bash("rm -rf /")},
"docker": {"Bash", bash("docker run --privileged x")},
"git": {"Bash", bash("git status")},
"path to git": {"Bash", bash("/usr/bin/git status")},
"git config": {"Bash", bash("git config user.name x")},
"cmake": {"Bash", bash("cmake --version")},
"ctest": {"Bash", bash("ctest -N")},
} {
if got, err := AlwaysAllowPrefix(c.tool, c.input); err == nil {
t.Errorf("%s: prefix %q, want a refusal", name, got)
@@ -59,6 +65,20 @@ func TestAlwaysAllowPrefix(t *testing.T) {
}
}
// The arity table is keyed by program name, so a path-qualified binary derives
// the same prefix length as the bare name.
func TestArgvPrefixLenUsesBasename(t *testing.T) {
if got := argvPrefixLen([]string{"/usr/bin/gh", "pr", "list"}); got != 3 {
t.Errorf("path-qualified gh prefix length = %d, want 3", got)
}
if got := argvPrefixLen([]string{"/usr/bin/git", "status"}); got != 2 {
t.Errorf("path-qualified git prefix length = %d, want 2", got)
}
if got, err := ArgvPrefix("/usr/bin/gh pr list"); err != nil || !slices.Equal(got, []string{"/usr/bin/gh", "pr", "list"}) {
t.Errorf("path-qualified gh prefix = %q, %v", got, err)
}
}
// The denylist neutralises argv allow rules at matching time (a rule created
// before the denylist, or written straight into the database), and only them:
// an argv deny or ask rule for the same program still matches.
+9 -1
View File
@@ -25,9 +25,17 @@ var argvArity = map[string]int{
}
// argvPrefixLen returns the number of leading tokens that form the prefix.
// The arity table is keyed by program name, so a path-qualified binary
// (/usr/bin/git) uses the same arity as the bare name.
func argvPrefixLen(tokens []string) int {
if len(tokens) == 0 {
return 0
}
key := make([]string, len(tokens))
copy(key, tokens)
key[0] = argvProgramName(tokens[0])
for n := len(tokens); n > 0; n-- {
if a, ok := argvArity[strings.Join(tokens[:n], " ")]; ok {
if a, ok := argvArity[strings.Join(key[:n], " ")]; ok {
return min(a, len(tokens))
}
}
+8 -8
View File
@@ -214,20 +214,20 @@ func TestParseArgvPattern(t *testing.T) {
func TestArgvAllowBelowDeny(t *testing.T) {
ic := New(nil)
ic.SetRulesForTest([]db.InterceptRule{
{ID: 1, Name: "no force push", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "regex",
{ID: 1, Name: "no force flag", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "regex",
Pattern: `--force`, Action: "deny"},
{ID: 2, Name: "argv allow git push", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
Pattern: `["git","push"]`, Action: "allow"},
{ID: 2, Name: "argv allow nmap", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
Pattern: `["nmap"]`, Action: "allow"},
})
dec, ok := ic.Match("Bash", []byte(`{"command":"git push --force"}`))
dec, ok := ic.Match("Bash", []byte(`{"command":"nmap --force 10.0.0.1"}`))
if !ok || dec.Action != "deny" || dec.RuleID != 1 {
t.Fatalf("force push: %+v %v, want the deny rule", dec, ok)
t.Fatalf("force flag: %+v %v, want the deny rule", dec, ok)
}
dec, ok = ic.Match("Bash", []byte(`{"command":"git push origin"}`))
dec, ok = ic.Match("Bash", []byte(`{"command":"nmap 10.0.0.1"}`))
if !ok || dec.Action != "allow" || dec.RuleID != 2 {
t.Fatalf("push: %+v %v, want the argv allow", dec, ok)
t.Fatalf("nmap: %+v %v, want the argv allow", dec, ok)
}
if _, ok := ic.Match("Bash", []byte(`{"command":"git push; id"}`)); ok {
if _, ok := ic.Match("Bash", []byte(`{"command":"nmap 10.0.0.1; id"}`)); ok {
t.Fatal("a compound command matched the argv rule")
}
if _, ok := compileRule(db.InterceptRule{MatchType: "argv", Pattern: `["-x"]`}); ok {
-48
View File
@@ -1,48 +0,0 @@
package intercept
import "testing"
// TestDefaultEnabledToolsCoverExecutors pins the tools that must be gated when no
// intercept_enabled_tools setting has been saved. ExecuteExtraTool and Monitor
// dispatch MCP / deferred tools and background commands, so leaving them out let
// those calls bypass every rule.
func TestDefaultEnabledToolsCoverExecutors(t *testing.T) {
got := map[string]bool{}
for _, name := range defaultEnabledTools {
got[name] = true
}
for _, want := range []string{"Bash", "Write", "Edit", "MultiEdit", "ExecuteExtraTool", "Monitor"} {
if !got[want] {
t.Errorf("defaultEnabledTools must include %q", want)
}
}
for _, readOnly := range []string{"list_assets", "traffic_search", "Read"} {
if got[readOnly] {
t.Errorf("defaultEnabledTools must not gate the read-only tool %q by default", readOnly)
}
}
}
// TestEnabledToolsFromSettingNeverDisablesTheGate: an unset or malformed setting
// must keep the defaults, not turn the rule system off.
func TestEnabledToolsFromSettingNeverDisablesTheGate(t *testing.T) {
for _, tc := range []struct {
name string
val string
ok bool
}{
{"unset", "", false},
{"malformed", "{not json", true},
{"wrong shape", `"Bash"`, true},
} {
got := enabledToolsFromSetting(tc.val, tc.ok)
if !got["Bash"] || !got["ExecuteExtraTool"] || !got["Monitor"] {
t.Errorf("%s: enabled tools %v, want the defaults", tc.name, got)
}
}
// A valid explicit list replaces the defaults.
got := enabledToolsFromSetting(`["Bash"]`, true)
if !got["Bash"] || got["WebFetch"] {
t.Fatalf("explicit list: %v, want only Bash", got)
}
}
+18 -27
View File
@@ -266,10 +266,6 @@ var defaultEnabledTools = []string{
"Bash", "WebFetch", "web_search",
"shell_open", "shell_send",
"Write", "Edit", "MultiEdit",
// ExecuteExtraTool dispatches MCP and deferred tools, and Monitor starts a
// background command; both execute third-party or shell commands, so they
// belong in the default gate with the rest.
"ExecuteExtraTool", "Monitor",
}
// New creates an Interceptor backed by d. The rule cache is lazy-loaded on
@@ -309,30 +305,25 @@ func (i *Interceptor) loadLocked() error {
// Load enabled-tools set from settings, falling back to hard-coded defaults.
val, ok, _ := i.db.GetSetting("intercept_enabled_tools")
i.enabledTools = enabledToolsFromSetting(val, ok)
return nil
}
// enabledToolsFromSetting maps the stored intercept_enabled_tools value to the
// gate's set. A malformed value must not silently disable the gate, so it falls
// back to the same defaults as an unset setting.
func enabledToolsFromSetting(val string, ok bool) map[string]bool {
m := make(map[string]bool, len(defaultEnabledTools))
for _, n := range defaultEnabledTools {
m[n] = true
}
if !ok {
return m
m := make(map[string]bool, len(defaultEnabledTools))
for _, n := range defaultEnabledTools {
m[n] = true
}
i.enabledTools = m
} else {
var names []string
if json.Unmarshal([]byte(val), &names) != nil {
i.enabledTools = map[string]bool{}
} else {
m := make(map[string]bool, len(names))
for _, n := range names {
m[n] = true
}
i.enabledTools = m
}
}
var names []string
if json.Unmarshal([]byte(val), &names) != nil {
return m
}
m = make(map[string]bool, len(names))
for _, n := range names {
m[n] = true
}
return m
return nil
}
func (i *Interceptor) rules() ([]compiledRule, error) {
@@ -390,7 +381,7 @@ func (i *Interceptor) GetEnabledTools() ([]string, error) {
}
var names []string
if err := json.Unmarshal([]byte(val), &names); err != nil {
return nil, fmt.Errorf("intercept_enabled_tools is not valid JSON: %w", err)
return []string{}, nil
}
return names, nil
}
+10 -23
View File
@@ -1182,27 +1182,6 @@ func (e *Engine) runWorkerStep(ctx context.Context, t *Task, name string, worker
return e.runIntent(ctx, t, name, worker, intent, token, "", "")
}
// intentWorkContext resolves the cached per-task exec context and returns a
// per-intent child context with the rotated LLM profile pin attached (#85,
// #376). Attaching the pin after execContextFor ensures a cached e.execCtx
// does not drop it; attaching it after the paused/deleting check ensures a
// rolled-back claim never advances the rotation counter.
func (e *Engine) intentWorkContext(ctx context.Context, t *Task) (ectx, workCtx context.Context, workCancel context.CancelCauseFunc, ok bool) {
ectx = e.clockCtx(e.execContextFor(ctx, t.ID), t, false) // cancellable by Pause; carries the task deadline
if ectx.Err() != nil || e.IsDeleting(t.ID) {
return ectx, nil, nil, false
}
// per-work child context so the planner's kill_work can stop just this work.
workCtx, workCancel = context.WithCancelCause(ectx)
// Per-intent LLM rotation (#85): each claimed worker intent runs on the
// next chain head. The pin travels through the run context into the
// provider resolver; non-worker roles and failover keep the active head.
if head, pinned := t.nextRotatedProfile(); pinned {
workCtx = withLLMProfilePin(workCtx, head)
}
return ectx, workCtx, workCancel, true
}
// runIntent executes and fully settles one already-claimed (state=running) intent.
// Both the pool worker loop (via runWorkerStep) and the human-message handler (via
// runDetachedIntent, a dedicated goroutine outside the worker pool) call it, so the
@@ -1215,17 +1194,25 @@ func (e *Engine) intentWorkContext(ctx context.Context, t *Task) (ectx, workCtx
// while the worker is busy, and every intent state write below is fenced on it,
// so a worker whose claim was taken over cannot overwrite the new claimer.
func (e *Engine) runIntent(ctx context.Context, t *Task, name string, worker *agent.Worker, intent *db.Node, token, requestID, message string) bool {
// Per-intent LLM rotation (#85): each claimed worker intent runs on the
// next chain head. The pin travels through the run context into the
// provider resolver; non-worker roles and failover keep the active head.
if head, ok := t.nextRotatedProfile(); ok {
ctx = withLLMProfilePin(ctx, head)
}
hasChatMessage := message != ""
e.stampFirstRun(t) // first real execution → stamp first_run_at + compute the deadline (only for tasks with a timeout)
e.touch(t.ID)
emit := func(r db.Activity) { e.emitActivity(t, r) }
ectx, workCtx, workCancel, ok := e.intentWorkContext(ctx, t)
if !ok {
ectx := e.clockCtx(e.execContextFor(ctx, t.ID), t, false) // cancellable by Pause; carries the task deadline
if ectx.Err() != nil || e.IsDeleting(t.ID) {
if err := t.Store.SettleIntentClaim(intent.ID, token, "open"); err != nil {
log.Printf("[worker %s] task %s intent #%d claim rollback failed: %v", name, t.ID, intent.ID, err)
}
return true
}
// per-work child context so the planner's kill_work can stop just this work.
workCtx, workCancel := context.WithCancelCause(ectx)
e.registerWork(intent.ID, workCancel)
stopHeartbeat := startClaimHeartbeat(t.Store, db.IntentClaimHeartbeatInterval, name, intent.ID, token, workCancel)
// wrap the guard hooks so steer_work can inject a mid-run course-correction
+4 -4
View File
@@ -7,7 +7,7 @@ import (
)
func TestValidateArgvRuleReq(t *testing.T) {
base := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: `["git","push"]`, Action: "allow"}
base := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: `["nmap"]`, Action: "allow"}
if err := validateInterceptRuleReq(base); err != nil {
t.Fatalf("valid argv rule refused: %v", err)
}
@@ -15,7 +15,7 @@ func TestValidateArgvRuleReq(t *testing.T) {
"tool_name target": func(r *interceptRuleReq) { r.MatchTarget = "tool_name" },
"empty pattern": func(r *interceptRuleReq) { r.Pattern = "" },
"empty token": func(r *interceptRuleReq) { r.Pattern = `["git",""]` },
"flag token": func(r *interceptRuleReq) { r.Pattern = `["git","-C"]` },
"flag token": func(r *interceptRuleReq) { r.Pattern = `["nmap","-C"]` },
"metacharacter": func(r *interceptRuleReq) { r.Pattern = `["nmap;id"]` },
"nine tokens": func(r *interceptRuleReq) { r.Pattern = `["a","b","c","d","e","f","g","h","i"]` },
"not an array": func(r *interceptRuleReq) { r.Pattern = `git push` },
@@ -31,7 +31,7 @@ func TestValidateArgvRuleReq(t *testing.T) {
// The manual rule form refuses an argv allow rule for a denylisted program,
// with the message of the "always allow" endpoint; deny and ask stay creatable.
func TestValidateArgvAllowDenylist(t *testing.T) {
for _, pattern := range []string{`["python"]`, `["python3.11"]`, `["/usr/bin/env"]`, `["find"]`, `["docker","run"]`} {
for _, pattern := range []string{`["python"]`, `["python3.11"]`, `["/usr/bin/env"]`, `["find"]`, `["docker","run"]`, `["git","push"]`, `["cmake"]`} {
req := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: pattern, Action: "allow"}
tokens, _ := intercept.ParseArgvPattern(pattern)
want := intercept.CheckArgvAllow(tokens)
@@ -45,7 +45,7 @@ func TestValidateArgvAllowDenylist(t *testing.T) {
}
}
}
for _, pattern := range []string{`["nmap"]`, `["git","push"]`} {
for _, pattern := range []string{`["nmap"]`, `["curl"]`} {
req := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: pattern, Action: "allow"}
if err := validateInterceptRuleReq(req); err != nil {
t.Errorf("allow %s refused: %v", pattern, err)
-179
View File
@@ -3,14 +3,12 @@ package server
import (
"context"
"fmt"
"iter"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/db"
"github.com/Autumn-27/norma/llm"
)
@@ -213,180 +211,3 @@ func TestNextRotatedProfileRoundRobin(t *testing.T) {
t.Fatal("disabled rotation must not assign a pin")
}
}
type pinCapture struct {
id int64
ok bool
}
type pinCaptureProvider struct {
mu sync.Mutex
seen []pinCapture
}
func (p *pinCaptureProvider) record(ctx context.Context) {
id, ok := llmProfilePinFrom(ctx)
p.mu.Lock()
p.seen = append(p.seen, pinCapture{id: id, ok: ok})
p.mu.Unlock()
}
func (p *pinCaptureProvider) Stream(ctx context.Context, _ llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
p.record(ctx)
return func(yield func(llm.StreamEvent, error) bool) {
if !yield(llm.StreamEvent{Type: llm.SETextDelta, Text: "done"}, nil) {
return
}
if !yield(llm.StreamEvent{Type: llm.SEMessageDelta, StopReason: "end_turn"}, nil) {
return
}
yield(llm.StreamEvent{Type: llm.SEMessageStop}, nil)
}
}
func (p *pinCaptureProvider) Complete(ctx context.Context, _ llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
p.record(ctx)
return llm.Message{Role: llm.RoleAssistant, Content: []llm.ContentBlock{llm.TextBlock("done")}}, "end_turn", llm.Usage{}, nil
}
// Regression test for #376 (DB-free): warming e.execContextFor first (as the
// planner does) must not drop the per-intent LLM profile pin on subsequent
// worker intents, and a paused/deleting rollback must not advance rotation.
func TestIntentWorkContextRotatesWithCachedExecCtx(t *testing.T) {
const (
taskID = "77"
profileA = int64(101)
profileB = int64(202)
)
e := NewEngine(nil)
tk := &Task{ID: taskID, LLMProfileIDs: []int64{profileA, profileB}, LLMRotate: true}
// Warm the per-task cached execCtx, simulating the planner running first.
warm := e.execContextFor(context.Background(), taskID)
if warm == nil || warm.Err() != nil {
t.Fatalf("warm execContextFor returned invalid ctx: %v", warm)
}
// A paused task must roll back without advancing the rotation counter.
e.Pause(taskID, agent.AbortPausedByUser)
if _, _, _, ok := e.intentWorkContext(context.Background(), tk); ok {
t.Fatal("paused task unexpectedly yielded a work context")
}
e.Resume(tk)
// Re-warm cached execCtx after resume (again simulating planner before worker).
_ = e.execContextFor(context.Background(), taskID)
// A deleting task must also roll back without advancing the rotation counter.
e.BeginDelete(taskID)
if _, _, _, ok := e.intentWorkContext(context.Background(), tk); ok {
t.Fatal("deleting task unexpectedly yielded a work context")
}
e.AbortDelete(taskID, false)
// Two consecutive claimed intents must carry distinct rotated pins (A then B).
_, workA, cancelA, ok := e.intentWorkContext(context.Background(), tk)
if !ok {
t.Fatal("first intentWorkContext failed")
}
defer cancelA(nil)
gotA, okA := llmProfilePinFrom(workA)
if !okA || gotA != profileA {
t.Fatalf("first workCtx pin = (%d, %v), want (%d, true)", gotA, okA, profileA)
}
_, workB, cancelB, ok := e.intentWorkContext(context.Background(), tk)
if !ok {
t.Fatal("second intentWorkContext failed")
}
defer cancelB(nil)
gotB, okB := llmProfilePinFrom(workB)
if !okB || gotB != profileB {
t.Fatalf("second workCtx pin = (%d, %v), want (%d, true)", gotB, okB, profileB)
}
if gotA == gotB {
t.Fatalf("expected distinct pinned profile IDs, both were %d", gotA)
}
}
// Regression test for #376 (end-to-end through runIntent): warm execContextFor
// first (simulating the planner running before workers), then run two intents
// through runIntent and assert the two work contexts carry distinct pinned
// profile IDs (A then B).
func TestRunIntentRotatesPinnedProfileWithCachedExecCtx(t *testing.T) {
m, err := NewManager(t.TempDir(), "")
if err != nil {
t.Skipf("postgres unavailable: %v", err)
}
defer m.Close()
ids := make([]int64, 0, 2)
for i := 0; i < 2; i++ {
id, err := m.pg.SaveProfile(&db.LLMProfile{
Name: fmt.Sprintf("runintent-rotate-%d-%d", time.Now().UnixNano(), i),
Format: "openai",
BaseURL: "http://127.0.0.1:1/v1",
Model: "test",
APIKey: "test-key",
})
if err != nil {
t.Fatal(err)
}
ids = append(ids, id)
defer m.pg.DeleteProfile(id)
}
on := true
task, err := m.CreateTaskWithOptions("runIntent rotation", "verify pin survives cached execCtx", db.TaskCreateOptions{
LLMProfileIDs: ids,
LLMRotate: &on,
})
if err != nil {
t.Fatal(err)
}
defer m.DeleteTask(task.ID, DeleteTaskOptions{})
e := NewEngine(m)
// Warm the per-task cached execCtx before any worker intent runs.
_ = e.execContextFor(context.Background(), task.ID)
// Verify paused rollback through runIntent does not advance rotation.
if _, err := task.Store.AddIntent(map[string]any{"summary": "rolled back while paused"}, 1, nil, "planner"); err != nil {
t.Fatal(err)
}
pausedIntent, pausedToken := e.claimNext(task, "work#0")
if pausedIntent == nil {
t.Fatal("failed to claim intent for paused rollback check")
}
e.Pause(task.ID, agent.AbortPausedByUser)
probe := &pinCaptureProvider{}
worker := agent.NewWorker(probe, "test-model", m.dir, nil, 0, 2)
e.runIntent(context.Background(), task, "work#0", worker, pausedIntent, pausedToken, "", "")
if len(probe.seen) != 0 {
t.Fatalf("paused runIntent called provider %d times, want 0", len(probe.seen))
}
e.Resume(task)
// Re-warm cached execCtx after resume (simulating planner before workers).
_ = e.execContextFor(context.Background(), task.ID)
// Run two intents through runIntent and assert distinct pins: ids[0] then ids[1].
if _, err := task.Store.AddIntent(map[string]any{"summary": "rotate intent 2"}, 1, nil, "planner"); err != nil {
t.Fatal(err)
}
for i, wantID := range ids {
name := fmt.Sprintf("work#%d", i+1)
intent, token := e.claimNext(task, name)
if intent == nil {
t.Fatalf("intent %d not claimed", i+1)
}
p := &pinCaptureProvider{}
w := agent.NewWorker(p, "test-model", m.dir, nil, 0, 2)
e.runIntent(context.Background(), task, name, w, intent, token, "", "")
if len(p.seen) == 0 {
t.Fatalf("intent %d: provider was not called", i+1)
}
got := p.seen[0]
if !got.ok || got.id != wantID {
t.Fatalf("intent %d: workCtx pin = (%d, %v), want (%d, true)", i+1, got.id, got.ok, wantID)
}
}
}