mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
164 lines
6.2 KiB
Go
164 lines
6.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/norma/llm"
|
|
)
|
|
|
|
func TestConstraintBlockEmpty(t *testing.T) {
|
|
if got := constraintBlock(nil); got != "" {
|
|
t.Fatalf("nil store: %q", got)
|
|
}
|
|
d := testDB(t)
|
|
defer d.Close()
|
|
task, err := d.CreateTask("Constraint fixture", "Check the authorized endpoint", nil, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer d.DeleteTask(task.ID)
|
|
ts := d.Exploration(task.ExplorationID)
|
|
if got := constraintBlock(ts); got != "" {
|
|
t.Fatalf("empty constraints: %q", got)
|
|
}
|
|
if _, err = ts.AddConstraint("allow", " \t ", "human"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := constraintBlock(ts); got != "" {
|
|
t.Fatalf("blank constraint: %q", got)
|
|
}
|
|
}
|
|
|
|
// Both execution roles consume the same operation-constraint contract. Check
|
|
// the real request assembly so a prompt fix cannot silently miss one role or
|
|
// bypass the task's existing constraint-injection switch.
|
|
func TestConstraintBlockReachesPlannerAndWorker(t *testing.T) {
|
|
t.Setenv("ARTEX_SANDBOX", "off")
|
|
for _, scopeCase := range []struct {
|
|
name string
|
|
allow []string
|
|
}{
|
|
{name: "method only"},
|
|
{name: "bare CIDR", allow: []string{"10.0.0.0/24"}},
|
|
{name: "bare domain", allow: []string{"app.example.com"}},
|
|
{name: "bare IP", allow: []string{"192.0.2.10"}},
|
|
{name: "bare URL", allow: []string{"https://app.example.com/health"}},
|
|
{name: "bare port", allow: []string{"port 443"}},
|
|
{name: "explicit scope", allow: []string{"Authorized target scope: https://api.example.test on port 443."}},
|
|
{name: "explicit restriction", allow: []string{"ONLY api.example.test may be accessed."}},
|
|
} {
|
|
for _, role := range []string{"planner", "worker"} {
|
|
for _, inject := range []bool{true, false} {
|
|
name := scopeCase.name + "/" + role + "/disabled"
|
|
if inject {
|
|
name = scopeCase.name + "/" + role + "/enabled"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
d := testDB(t)
|
|
defer d.Close()
|
|
goal := "Check the operator-authorized endpoint without expanding scope."
|
|
task, err := d.CreateTask("Constraint injection fixture", goal, nil, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer d.DeleteTask(task.ID)
|
|
ts := d.Exploration(task.ExplorationID)
|
|
rules := []db.Constraint{
|
|
{Kind: "allow", Text: "Public proxies may be used."},
|
|
{Kind: "deny", Text: "Do not issue DELETE requests."},
|
|
}
|
|
for _, text := range scopeCase.allow {
|
|
rules = append(rules, db.Constraint{Kind: "allow", Text: text})
|
|
}
|
|
for _, r := range rules {
|
|
if _, err = ts.AddConstraint(r.Kind, r.Text, "human"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
block := constraintBlock(ts)
|
|
_, allowed, ok := strings.Cut(block, "Allowed operations:\n")
|
|
if !ok {
|
|
t.Fatal("allow section missing")
|
|
}
|
|
allowed, _, _ = strings.Cut(allowed, "\nForbidden operations:")
|
|
for _, r := range rules {
|
|
if r.Kind == "allow" && !strings.Contains("\n"+allowed+"\n", "\n- "+r.Text+"\n") {
|
|
t.Fatalf("allow entry was removed, prefixed or reclassified: %q", r.Text)
|
|
}
|
|
}
|
|
for _, semantic := range []string{
|
|
"Any allow entry naming a target, host, domain, URL, IP address, CIDR, or port is itself a binding target-scope boundary (a whitelist)",
|
|
"even without an 'Authorized target scope:' prefix or the word 'only'",
|
|
"An allowance for an operation or method that does not name a target or port, such as 'Public proxies may be used.', is an additional permission, not a complete whitelist",
|
|
} {
|
|
if !strings.Contains(block, semantic) {
|
|
t.Errorf("missing bare-target/method contract: %s", semantic)
|
|
}
|
|
}
|
|
if strings.Contains(block, "unless it falls within the allowed range above") {
|
|
t.Fatal("ordinary allow entries are still treated as a complete target whitelist")
|
|
}
|
|
for _, semantic := range []string{"additional permission", "not a complete whitelist", "scope declaration", "every deny", "nor expands target scope", "actual destination and operation", "still denied, even", "platform authorization denial", "merely sharing infrastructure does not establish target equivalence", "stop only that action"} {
|
|
if !strings.Contains(block, semantic) {
|
|
t.Fatalf("missing constraint distinction: %s", semantic)
|
|
}
|
|
}
|
|
requests := 0
|
|
provider := &progressProbeProvider{response: func(_ int, req llm.CompletionRequest) (llm.Message, string, error) {
|
|
requests++
|
|
system := strings.Join(req.System, "\n\n")
|
|
for _, r := range rules {
|
|
if strings.Contains(system, r.Text) != inject {
|
|
t.Fatalf("%s constraint injection=%v, rule=%q", role, inject, r.Text)
|
|
}
|
|
}
|
|
if inject && !strings.Contains(system, block) {
|
|
t.Fatal("the shared constraint contract was not injected intact")
|
|
}
|
|
if role == "planner" && inject {
|
|
found := false
|
|
for _, m := range req.Messages {
|
|
for _, b := range m.Content {
|
|
found = found || strings.Contains(b.Text, strings.TrimSpace(block))
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("planner goal anchor lost operation constraints")
|
|
}
|
|
}
|
|
return progressText("No tools executed in this request-assembly fixture."), "end_turn", nil
|
|
}}
|
|
if role == "planner" {
|
|
planner := NewPlanner(provider, "fixture", t.TempDir(), nil, 100000, 2)
|
|
planner.SetNonStreaming(func() bool { return true })
|
|
planner.SetConstraintInject(func() bool { return inject })
|
|
_, _, err = planner.Plan(t.Context(), task.ID, nil, ts, goal, nil, nil, nil)
|
|
if err != nil && !isEmptyPlanningRound(err) {
|
|
t.Fatal(err)
|
|
}
|
|
} else {
|
|
id, e := ts.AddIntent(map[string]any{"summary": goal}, 0, nil, "fixture")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
intent, e := ts.GetNode(id)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
worker := NewWorker(provider, "fixture", t.TempDir(), nil, 100000, 2)
|
|
worker.SetNonStreaming(func() bool { return true })
|
|
worker.SetConstraintInject(func() bool { return inject })
|
|
if _, _, err = worker.Execute(t.Context(), "fixture-worker", task.ID, nil, ts, intent, nil, nil, nil, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if requests == 0 {
|
|
t.Fatal("no assembled model request observed")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|