Compare commits

...
Author SHA1 Message Date
BaaRai 46751cfb9e fix(intercept): gate ExecuteExtraTool and Monitor by default
The default intercept-enabled set left out ExecuteExtraTool (the dispatcher for MCP and deferred tools) and Monitor (background commands), so calls through them bypassed every rule unless the operator had saved their own enabled-tools list.

A malformed intercept_enabled_tools value also produced an empty set, which turned the rule system off; it now falls back to the defaults, and GetEnabledTools reports the parse error instead of pretending nothing is gated.
2026-11-20 01:48:50 +01:00
BaaRai 4a29dbfd6d Merge pull request #384 from BaaRai/fix/llm-rotate-context
fix(server): attach the rotated LLM profile pin after execContextFor
2026-11-20 01:28:50 +01:00
BaaRai b3c9708bf3 fix(server): attach rotated LLM profile pin after execContextFor (#376) 2026-11-20 01:01:08 +01:00
BaaRai d41da1b2bb Merge pull request #383 from BaaRai/chore/export-ignore-offensive-skills
chore: exclude the offensive research skills from generated source archives
2026-11-20 00:50:22 +01:00
BaaRai 4c7dace5f2 chore: exclude the offensive research skills from generated source archives 2026-11-20 00:50:05 +01:00
BaaRai 0255f6af2c Merge pull request #377 from BaaRai/fix/argv-compound-matching
intercept: match deny/ask argv rules across compound commands
2026-11-19 20:33:57 +01:00
BaaRai 5c95f13403 Merge pull request #378 from BaaRai/chore/export-ignore-pocs
chore: exclude the php2shell PoCs from generated source archives
2026-11-19 20:31:35 +01:00
BaaRai 6c121845d7 chore: exclude the php2shell PoCs from generated source archives 2026-11-19 20:31:17 +01:00
BaaRai bbdb064afc fix(intercept): over-approximate unknown options on known wrappers and expand parallel flags 2026-11-19 18:56:42 +01:00
BaaRai 0258a0570b fix(intercept): parse command lines after parallel ::: when no command precedes it 2026-11-19 18:18:18 +01:00
BaaRai da00bf7c8a fix(intercept): close shell-option, env -S, xargs -l and find -exec argv bypasses 2026-11-19 17:57:59 +01:00
BaaRai 8f0b73f514 fix(intercept): match argv deny and ask rules across compound and wrapped Bash commands 2026-11-19 17:15:53 +01:00
BaaRai 8e6acb0638 Merge pull request #371 from mathkartel/docs/openai-compatible-gateways
docs: recipe for OpenAI-compatible gateways
2026-11-19 16:31:32 +01:00
BaaRai 35fbda765a Merge pull request #372 from BaaRai/fix/workspace-symlink-confinement
fix(server): confine workspace and skill paths against symlink escapes
2026-11-19 16:28:24 +01:00
mathkartel 95dd5e9711 docs: recipe for OpenAI-compatible gateways (NotHoly example) 2026-11-19 11:58:37 -03:00
9 changed files with 1752 additions and 35 deletions
+3
View File
@@ -1,2 +1,5 @@
*.bat text eol=crlf
*.sh text eol=lf
scripts/poc/ export-ignore
skills/edr-bypass-re/ export-ignore
skills/php2shell/ export-ignore
+8
View File
@@ -428,6 +428,14 @@ runtime supports it.
`/chat/completions` (or `/responses` for `openai-responses`), so their base **does** end in `/v1`
(for example `https://api.openai.com/v1`).
**OpenAI-compatible gateways**: any provider that serves an OpenAI-style
`/v1/chat/completions` endpoint works through the **OpenAI** format — no dedicated format
needed. Create a profile with format **OpenAI**, set **Base URL** to the gateway's `/v1`
root, paste the key, then click the **↻** button next to *Model* to load the provider's
catalog (`GET /v1/models`) and pick a model; models the provider adds later appear on the
next refresh. For example, [NotHoly](https://notholy.com) (offensive-security models) uses
base URL `https://api.notholy.com/v1` with an API key from its console.
**Concurrency**: work agents per task, in "System Settings" (default 3).
**Untrusted-output boundary (on by default)**: tool results for the worker, planner, chat and main
+3 -1
View File
@@ -530,7 +530,9 @@ 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.
for _, c := range []struct{ tool, input string }{{"Monitor", curl(deny)}, {"mcp__scan__run", `{"target":"203.0.113.7","note":"` + deny + `"}`}} {
// 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 + `"}`}} {
if block, msg := pre(guard, ctx, c.tool, c.input); block {
t.Fatalf("%s: %s blocked: %s", name, c.tool, msg)
}
File diff suppressed because it is too large Load Diff
+291
View File
@@ -0,0 +1,291 @@
package intercept
import (
"encoding/json"
"testing"
"github.com/Autumn-27/artex/db"
)
func bashInput(cmd string) []byte {
b, _ := json.Marshal(map[string]string{"command": cmd})
return b
}
func matchSingleArgvRule(action, pattern, toolName string, input []byte) (Decision, bool) {
ic := New(nil)
ic.SetRulesForTest([]db.InterceptRule{{
ID: 1,
Name: action + " " + pattern,
Enabled: true,
Priority: 100,
MatchTarget: "tool_input",
MatchType: "argv",
Pattern: pattern,
Action: action,
}})
return ic.Match(toolName, input)
}
func TestArgvDenyAndAskCompoundAndWrapped(t *testing.T) {
nmapShouldMatch := []struct {
name string
cmd string
}{
{"plain", "nmap -p 80 x"},
{"command wrapper", "command nmap -p 80 x"},
{"command double dash", "command -- nmap -p 80 x"},
{"command -p", "command -p nmap -p 80 x"},
{"path qualified", "/usr/bin/nmap -p 80 x"},
{"sudo", "sudo nmap -p 80 x"},
{"sudo with flags and assign", "sudo -u root -n FOO=1 nmap -p 80 x"},
{"doas", "doas -u root nmap -p 80 x"},
{"env with assign", "env FOO=1 nmap -p 80 x"},
{"env with flags", "env -i -u HOME -C /tmp -S arg FOO=1 nmap -p 80 x"},
{"env with expansion in assign", "env FOO=$(id) nmap -p 80 x"},
{"env -S separate", "env -S 'nmap -p 80 x'"},
{"env -S attached", "env -S'nmap -p 80 x'"},
{"env --split-string equals", "env --split-string='nmap -p 80 x'"},
{"env --split-string separate", "env --split-string 'nmap -p 80 x'"},
{"env -S with env flags", "env -S '-i FOO=1 nmap -p 80 x'"},
{"timeout", "timeout 30 nmap -p 80 x"},
{"timeout with flags", "timeout -k 5 -s KILL 30 nmap -p 80 x"},
{"nohup", "nohup nmap -p 80 x"},
{"setsid", "setsid -f nmap -p 80 x"},
{"stdbuf", "stdbuf -oL -eL nmap -p 80 x"},
{"nice -n", "nice -n 10 nmap -p 80 x"},
{"nice numeric flag", "nice -10 nmap -p 80 x"},
{"ionice", "ionice -c 3 nmap -p 80 x"},
{"taskset", "taskset -c 0-1 nmap -p 80 x"},
{"chrt", "chrt -f 50 nmap -p 80 x"},
{"watch", "watch -n 5 nmap -p 80 x"},
{"watch -d permanent", "watch -d permanent nmap -p 80 x"},
{"flock", "flock -n /tmp/lock nmap -p 80 x"},
{"flock -c after lockfile", "flock /tmp/lock -c 'nmap -p 80 x'"},
{"flock -c before lockfile", "flock -n -c 'nmap -p 80 x' /tmp/lock"},
{"strace", "strace -f -o /tmp/trace nmap -p 80 x"},
{"ltrace", "ltrace -f -o /tmp/ltrace nmap -p 80 x"},
{"xargs", "echo 10.0.0.1 | xargs -n 1 nmap -p 80"},
{"xargs -I", "xargs -I{} nmap -p 80 {}"},
{"xargs -l bare", "xargs -l nmap"},
{"xargs -l5 attached", "xargs -l5 nmap"},
{"xargs -l 5 separate", "xargs -l 5 nmap"},
{"find -exec", `find . -maxdepth 0 -exec nmap -p 80 x \;`},
{"find -execdir", `find . -execdir nmap -p 80 x \;`},
{"find -ok", `find . -ok nmap -p 80 x \;`},
{"find -okdir plus", `find . -okdir nmap -p 80 x +`},
{"find multiple -exec clauses", `find . -exec echo {} \; -exec nmap -p 80 {} \;`},
{"find -exec shell wrapper", `find . -exec sh -ec 'nmap -p 80 x' \;`},
{"parallel bare", "parallel nmap"},
{"parallel with jobs and :::", "parallel -j 4 nmap -p 80 {} ::: 10.0.0.1"},
{"parallel quoted shell string", "parallel -j 4 'nmap -p 80 {}' ::: 10.0.0.1"},
{"parallel no command ::: bare", "parallel ::: nmap"},
{"parallel no command ::: quoted line", `parallel ::: "nmap -p 80 x"`},
{"parallel no command -j2 ::: bare", "parallel -j2 ::: nmap"},
{"parallel no command ::: multiple lines", "parallel ::: echo nmap"},
{"parallel --link ::: nmap", "parallel --link ::: nmap"},
{"parallel --xargs ::: nmap", "parallel --xargs ::: nmap"},
{"parallel unknown no-arg long opt", "parallel --future-flag ::: nmap"},
{"parallel unknown value long opt", "parallel --future-val 42 nmap -p 80 x"},
{"sudo unknown flag over-approximated", "sudo --unknown-flag nmap -p 80 x"},
{"env unknown flag over-approximated", "env -Z nmap -p 80 x"},
{"busybox", "busybox nmap -p 80 x"},
{"busybox sh -c", "busybox sh -c 'nmap -p 80 x'"},
{"chroot", "chroot / nmap -p 80 x"},
{"chroot with userspec", "chroot --userspec=0:0 /mnt nmap -p 80 x"},
{"exec", "exec -- nmap -p 80 x"},
{"exec -a", "exec -a fake nmap -p 80 x"},
{"semicolon list", "nmap -p 80 x; true"},
{"and list", "true && nmap -p 80 x"},
{"or list", "false || nmap -p 80 x"},
{"pipeline", "nmap -p 80 x | tee out"},
{"subshell", "(nmap -p 80 x)"},
{"redirect", "nmap -p 80 x > out"},
{"background", "nmap -p 80 x &"},
{"prefix assign", "FOO=1 nmap -p 80 x"},
{"expansion in later arg", "nmap $(id)"},
{"param expansion in later arg", "nmap $TARGET"},
{"command subst inside echo", "echo $(nmap -p 80 x)"},
{"bash -c single quoted", "bash -c 'nmap -p 80 x'"},
{"bash -x -c", "bash -x -c 'nmap -p 80 x'"},
{"bash -lc", "bash -lc 'nmap -p 80 x'"},
{"bash -ec", "bash -ec 'nmap -p 80 x'"},
{"bash -o pipefail -c", "bash -o pipefail -c 'nmap -p 80 x'"},
{"bash +o pipefail -c", "bash +o pipefail -c 'nmap -p 80 x'"},
{"bash -O extglob -c", "bash -O extglob -c 'nmap -p 80 x'"},
{"bash --norc -c", "bash --norc -c 'nmap -p 80 x'"},
{"bash --noprofile --rcfile /dev/null -c --", "bash --noprofile --rcfile /dev/null -c -- 'nmap -p 80 x'"},
{"sh -c double quoted", `sh -c "nmap -p 80 x"`},
{"zsh -c", "zsh -c 'nmap -p 80 x'"},
{"dash -c", "dash -c 'nmap -p 80 x'"},
{"ksh -c", "ksh -c 'nmap -p 80 x'"},
{"fish -c", "fish -c 'nmap -p 80 x'"},
{"fish --command", "fish --command='nmap -p 80 x'"},
{"path qualified bash -c", "/bin/bash -c 'nmap -p 80 x'"},
{"eval quoted", "eval 'nmap -p 80 x'"},
{"eval separate args", "eval nmap -p 80 x"},
{"eval with trailing subst", "eval nmap $(id)"},
{"stacked wrappers", "sudo env FOO=1 timeout 30 nohup command /usr/bin/nmap -p 80 x"},
{"nested payload depth 2", `bash -c "sh -c 'nmap -p 80 x'"`},
{"nested payload depth 3", `bash -c "sh -c \"eval 'nmap -p 80 x'\""`},
{"bad nested payload does not block later call", `bash -c "if then"; nmap -p 80 x`},
}
for _, action := range []string{"deny", "ask"} {
for _, tc := range nmapShouldMatch {
t.Run(action+"/match/"+tc.name, func(t *testing.T) {
dec, ok := matchSingleArgvRule(action, `["nmap"]`, "Bash", bashInput(tc.cmd))
if !ok || dec.Action != action {
t.Errorf("%s [\"nmap\"] on %q: got (%+v, %v), want match with action %q", action, tc.cmd, dec, ok, action)
}
})
}
}
nmapShouldNotMatch := []struct {
name string
cmd string
}{
{"argument to echo", "echo nmap"},
{"prefix of other binary", "nmapx"},
{"argument in pipeline", "echo x | grep nmap"},
{"command -v lookup", "command -v nmap"},
{"command -V lookup", "command -V nmap"},
{"sudo -l list", "sudo -l nmap"},
{"sudo -e edit", "sudo -e nmap"},
{"env no command", "env FOO=1"},
{"ionice pid mode", "ionice -p 1234"},
{"taskset pid mode", "taskset -p 1234"},
{"chrt pid mode", "chrt -p 1234"},
{"xargs -l echo nmap does not match nmap", "xargs -l echo nmap"},
{"shell unknown option before -c", "bash --unknown-opt -c 'nmap -p 80 x'"},
{"shell opaque option before -c", "bash $OPT -c 'nmap -p 80 x'"},
{"shell script mode with -x", "bash -x nmap"},
{"shell script file", "bash nmap"},
{"shell -- before -c treats -c as script", "bash -- -c 'nmap -p 80 x'"},
{"find -exec echo nmap", `find . -exec echo nmap \;`},
{"parallel echo with nmap input", "parallel echo ::: nmap"},
{"parallel no command ::: echo nmap quoted", `parallel ::: "echo nmap"`},
{"parallel echo nmap ::: x", "parallel echo nmap ::: x"},
{"parallel --link echo nmap ::: x", "parallel --link echo nmap ::: x"},
{"parallel :::: file input", "parallel :::: nmap"},
{"parallel :::+ separator", "parallel :::+ nmap"},
{"chroot without command", "chroot /"},
{"busybox list mode", "busybox --list"},
{"eval opaque leading arg", "eval $CMD nmap"},
{"opaque command token", "$CMD -p 80 x"},
{"nested payload depth 4 exceeds limit", `bash -c "sh -c \"bash -c 'eval \\\"nmap -p 80 x\\\"'\""`},
{"unparsable single quote", "nmap 'unterminated"},
{"unparsable subst", "nmap $("},
{"empty command", ""},
}
for _, action := range []string{"deny", "ask"} {
for _, tc := range nmapShouldNotMatch {
t.Run(action+"/nomatch/"+tc.name, func(t *testing.T) {
if dec, ok := matchSingleArgvRule(action, `["nmap"]`, "Bash", bashInput(tc.cmd)); ok {
t.Errorf("%s [\"nmap\"] on %q unexpectedly matched: %+v", action, tc.cmd, dec)
}
})
}
}
// Non-Bash tool never matches argv rules.
if dec, ok := matchSingleArgvRule("deny", `["nmap"]`, "shell_open", bashInput("nmap -p 80 x")); ok {
t.Errorf("non-Bash tool matched deny rule: %+v", dec)
}
}
func TestArgvDenyMultiTokenAndPathRules(t *testing.T) {
cases := []struct {
name string
pattern string
cmd string
want bool
}{
{"git config path qualified with flag", `["git","config"]`, "/usr/bin/git config --list", true},
{"git config via command", `["git","config"]`, "command git config --list", true},
{"git config via sudo", `["git","config"]`, "sudo git config user.name foo", true},
{"git config opaque subcommand", `["git","config"]`, "git $SUB --list", false},
{"git status does not match git config", `["git","config"]`, "git status", false},
{"exact path rule matches exact path", `["/usr/bin/nmap"]`, "/usr/bin/nmap -p 80 x", true},
{"exact path rule does not match bare name", `["/usr/bin/nmap"]`, "nmap -p 80 x", false},
{"exact path rule does not match other path", `["/usr/bin/nmap"]`, "/opt/bin/nmap -p 80 x", false},
{"builtin wrapper matches echo", `["echo"]`, "builtin -- echo hi", true},
{"xargs with no command does not invent echo", `["echo"]`, "xargs -n 1", false},
{"wrapper itself can still be denied", `["sudo"]`, "sudo nmap -p 80 x", true},
{"shell itself can still be denied", `["bash"]`, "bash -c 'echo hi'", true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, got := matchSingleArgvRule("deny", tc.pattern, "Bash", bashInput(tc.cmd))
if got != tc.want {
t.Errorf("deny %s on %q = %v, want %v", tc.pattern, tc.cmd, got, tc.want)
}
})
}
}
func TestArgvAllowRemainsStrict(t *testing.T) {
cases := []struct {
cmd string
want bool
}{
{"nmap -p 80 x", true},
{"command nmap x", false},
{"nmap x; true", false},
{"bash -c 'nmap x'", false},
{"/usr/bin/nmap x", false},
{"sudo nmap x", false},
{"env FOO=1 nmap x", false},
{"nmap $(id)", false},
}
for _, tc := range cases {
t.Run(tc.cmd, func(t *testing.T) {
_, got := matchSingleArgvRule("allow", `["nmap"]`, "Bash", bashInput(tc.cmd))
if got != tc.want {
t.Errorf("allow [\"nmap\"] on %q = %v, want %v", tc.cmd, got, tc.want)
}
})
}
}
func TestArgvCompoundMalformedInputDoesNotPanic(t *testing.T) {
inputs := [][]byte{
nil,
[]byte(``),
[]byte(`not json`),
[]byte(`{}`),
bashInput(`nmap 'unterminated`),
bashInput(`nmap $(`),
bashInput(`if then fi; (( (`),
bashInput(`bash -c "unterminated`),
bashInput(`eval "((("`),
}
for _, action := range []string{"deny", "ask", "allow"} {
for _, in := range inputs {
if dec, ok := matchSingleArgvRule(action, `["nmap"]`, "Bash", in); ok {
t.Errorf("%s matched malformed input %q: %+v", action, string(in), dec)
}
}
}
}
func TestArgvPriorityPreservedWithCompoundDeny(t *testing.T) {
ic := New(nil)
ic.SetRulesForTest([]db.InterceptRule{
{ID: 1, Name: "allow simple nmap", Enabled: true, Priority: 100, MatchTarget: "tool_input", MatchType: "argv", Pattern: `["nmap"]`, Action: "allow"},
{ID: 2, Name: "deny compound nmap", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "argv", Pattern: `["nmap"]`, Action: "deny"},
})
// Simple command hits the higher-priority allow rule.
dec, ok := ic.Match("Bash", bashInput("nmap -p 80 x"))
if !ok || dec.Action != "allow" || dec.RuleID != 1 {
t.Fatalf("simple nmap: got (%+v, %v), want allow rule 1", dec, ok)
}
// Compound command fails the strict allow rule and falls through to the deny rule.
dec, ok = ic.Match("Bash", bashInput("nmap -p 80 x; true"))
if !ok || dec.Action != "deny" || dec.RuleID != 2 {
t.Fatalf("compound nmap: got (%+v, %v), want deny rule 2", dec, ok)
}
}
+48
View File
@@ -0,0 +1,48 @@
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)
}
}
+41 -24
View File
@@ -266,6 +266,10 @@ 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
@@ -305,27 +309,32 @@ 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")
if !ok {
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
}
}
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
}
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
}
func (i *Interceptor) rules() ([]compiledRule, error) {
i.mu.RLock()
if i.cached != nil {
@@ -381,7 +390,7 @@ func (i *Interceptor) GetEnabledTools() ([]string, error) {
}
var names []string
if err := json.Unmarshal([]byte(val), &names); err != nil {
return []string{}, nil
return nil, fmt.Errorf("intercept_enabled_tools is not valid JSON: %w", err)
}
return names, nil
}
@@ -765,12 +774,20 @@ func (i *Interceptor) SetRulesForTest(rules []db.InterceptRule) {
func ruleMatches(r compiledRule, toolName string, input []byte) bool {
if r.MatchType == "argv" {
// Bash only, on the parsed command; match_target is always tool_input.
// The denylist neutralises allow rules only: an argv deny or ask rule
// for a denylisted program still matches.
if r.Action == "allow" && CheckArgvAllow(r.argv) != nil {
return false
// Allow rules stay strictly limited to a single simple command with
// exact derived prefix and denylist enforcement. Deny and ask rules
// walk every call in a compound Bash command, stripping known wrappers
// and recursing into eval / shell -c payloads.
if r.Action == "allow" {
if CheckArgvAllow(r.argv) != nil {
return false
}
return r.MatchTarget == "tool_input" && argvRuleMatches(r.argv, toolName, input)
}
return r.MatchTarget == "tool_input" && argvRuleMatches(r.argv, toolName, input)
if r.Action == "deny" || r.Action == "ask" {
return r.MatchTarget == "tool_input" && argvCompoundMatches(r.argv, toolName, input)
}
return false
}
var subject string
switch r.MatchTarget {
+23 -10
View File
@@ -1182,6 +1182,27 @@ 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
@@ -1194,25 +1215,17 @@ func (e *Engine) runWorkerStep(ctx context.Context, t *Task, name string, worker
// 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 := e.clockCtx(e.execContextFor(ctx, t.ID), t, false) // cancellable by Pause; carries the task deadline
if ectx.Err() != nil || e.IsDeleting(t.ID) {
ectx, workCtx, workCancel, ok := e.intentWorkContext(ctx, t)
if !ok {
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
+179
View File
@@ -3,12 +3,14 @@ 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"
)
@@ -211,3 +213,180 @@ 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)
}
}
}