| 1 | package runtimepolicy |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "reasonix/internal/evidence" |
| 7 | ) |
| 8 | |
| 9 | func TestParseConstraintsScopesMutationBans(t *testing.T) { |
| 10 | tests := []struct { |
| 11 | name string |
| 12 | instruction string |
| 13 | forbid bool |
| 14 | }{ |
| 15 | {name: "scoped PR", instruction: "Do not modify PR #10. Create a branch and implement the repair."}, |
| 16 | {name: "scoped product", instruction: "Do not modify Strategy Lab. Commit the Battleboard fix."}, |
| 17 | {name: "scoped list", instruction: "Do not modify:\n- main\n- PR #10\nCreate feature/fix and commit."}, |
| 18 | {name: "unrelated issues", instruction: "Implement the bounded repair. Do not fix unrelated old issues."}, |
| 19 | {name: "descriptive no changes", instruction: "No changes to Battleboard mission prompts — fix the routing layer instead."}, |
| 20 | {name: "read only child", instruction: "Implementation writes are allowed; the independent reviewer is a strict read-only child."}, |
| 21 | {name: "scoped config", instruction: "Write AUDIT.md with the result. Do not change any config."}, |
| 22 | {name: "mixed analysis and fix", instruction: "Analyze the failure, then fix the implementation."}, |
| 23 | {name: "read one file then fix", instruction: "Read only the first file, then fix the implementation."}, |
| 24 | {name: "global analyze only", instruction: "Analyze only the payment flow.", forbid: true}, |
| 25 | {name: "global trailing analyze only", instruction: "Audit the payment flow, analyze only.", forbid: true}, |
| 26 | {name: "global read only review", instruction: "Read-only review of PR #10.", forbid: true}, |
| 27 | {name: "global read only repository", instruction: "Read only the repository.", forbid: true}, |
| 28 | {name: "bare do not modify", instruction: "Do not modify.", forbid: true}, |
| 29 | {name: "broad do not modify", instruction: "Do not modify anything.", forbid: true}, |
| 30 | {name: "without modifying", instruction: "Review the repository without modifying anything.", forbid: true}, |
| 31 | {name: "without changes", instruction: "Inspect the issue without changes.", forbid: true}, |
| 32 | {name: "do not edit files", instruction: "Do not edit any files.", forbid: true}, |
| 33 | {name: "workspace ban", instruction: "Do not change the workspace.", forbid: true}, |
| 34 | {name: "bare no changes", instruction: "No changes.", forbid: true}, |
| 35 | {name: "broad no changes", instruction: "Make no changes to anything.", forbid: true}, |
| 36 | {name: "reproduce only", instruction: "Reproduce only the crash.", forbid: true}, |
| 37 | {name: "scoped Chinese", instruction: "不要修改配置文件,生成 AUDIT.md。"}, |
| 38 | {name: "scoped Chinese issue", instruction: "不要修复无关问题,只处理当前缺陷并提交。"}, |
| 39 | {name: "global Chinese analyze", instruction: "只分析支付流程。", forbid: true}, |
| 40 | {name: "global Chinese read only", instruction: "只读检查当前仓库。", forbid: true}, |
| 41 | {name: "bare Chinese mutation ban", instruction: "不要修改。", forbid: true}, |
| 42 | {name: "global Chinese workspace", instruction: "不要修改当前工作区。", forbid: true}, |
| 43 | {name: "global Chinese reproduce", instruction: "只复现崩溃。", forbid: true}, |
| 44 | } |
| 45 | for _, tt := range tests { |
| 46 | t.Run(tt.name, func(t *testing.T) { |
| 47 | got := ParseConstraints(StripQuotedConstraints(tt.instruction)) |
| 48 | if got.ForbidMutation != tt.forbid { |
| 49 | t.Fatalf("ForbidMutation = %v, want %v; constraints=%+v", got.ForbidMutation, tt.forbid, got) |
| 50 | } |
| 51 | if got.AllowsMutation() == tt.forbid { |
| 52 | t.Fatalf("AllowsMutation = %v, want %v", got.AllowsMutation(), !tt.forbid) |
| 53 | } |
| 54 | decision := (ConstraintGuard{Constraints: got}).BeforeTool(CallContext{ |
| 55 | Profile: evidence.EffectProfile{Known: true, WorkspaceWrite: true}, |
| 56 | }) |
| 57 | if (decision.Action == GuardDeny) != tt.forbid { |
| 58 | t.Fatalf("writer decision = %+v, forbid=%v", decision, tt.forbid) |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // TestParseConstraintsRecognizesAnExplicitRebuild keeps the rebuild waiver tied |
| 65 | // to the user's own explicit phrasing; nothing else may set it. |
| 66 | func TestParseConstraintsRecognizesAnExplicitRebuild(t *testing.T) { |
| 67 | cases := []struct { |
| 68 | text string |
| 69 | want bool |
| 70 | }{ |
| 71 | {"Please rewrite notes.md from scratch.", true}, |
| 72 | {"把 notes.md 完全重写一遍", true}, |
| 73 | {"rewrite the whole file", true}, |
| 74 | {"add a section to notes.md", false}, |
| 75 | {"read notes.md and fix the typo", false}, |
| 76 | {"", false}, |
| 77 | } |
| 78 | for _, tc := range cases { |
| 79 | if got := ParseConstraints(tc.text).AllowRebuild; got != tc.want { |
| 80 | t.Fatalf("ParseConstraints(%q).AllowRebuild = %v, want %v", tc.text, got, tc.want) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 |