patchwarden 0.6.4 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (254) hide show
  1. package/README.en.md +54 -20
  2. package/README.md +48 -21
  3. package/dist/agents/agentRouter.d.ts +40 -0
  4. package/dist/agents/agentRouter.js +95 -0
  5. package/dist/config.d.ts +1 -1
  6. package/dist/config.js +6 -2
  7. package/dist/controlCenter.js +212 -3
  8. package/dist/direct/directSessionStore.d.ts +2 -0
  9. package/dist/direct/directVerification.js +7 -0
  10. package/dist/doctor.js +32 -1
  11. package/dist/goal/acceptanceEngine.d.ts +62 -0
  12. package/dist/goal/acceptanceEngine.js +103 -0
  13. package/dist/goal/acceptanceTemplate.d.ts +10 -0
  14. package/dist/goal/acceptanceTemplate.js +104 -0
  15. package/dist/goal/goalGraph.d.ts +58 -0
  16. package/dist/goal/goalGraph.js +189 -0
  17. package/dist/goal/goalProgress.d.ts +81 -0
  18. package/dist/goal/goalProgress.js +216 -0
  19. package/dist/goal/goalStatus.d.ts +60 -0
  20. package/dist/goal/goalStatus.js +137 -0
  21. package/dist/goal/goalStore.d.ts +79 -0
  22. package/dist/goal/goalStore.js +211 -0
  23. package/dist/goal/handoffExport.d.ts +34 -0
  24. package/dist/goal/handoffExport.js +183 -0
  25. package/dist/goal/subgoalSync.d.ts +40 -0
  26. package/dist/goal/subgoalSync.js +72 -0
  27. package/dist/goal/worktreeManager.d.ts +96 -0
  28. package/dist/goal/worktreeManager.js +292 -0
  29. package/dist/logging.d.ts +44 -0
  30. package/dist/logging.js +68 -0
  31. package/dist/policy/projectPolicy.d.ts +55 -0
  32. package/dist/policy/projectPolicy.js +286 -0
  33. package/dist/release/releaseGate.d.ts +99 -0
  34. package/dist/release/releaseGate.js +475 -0
  35. package/dist/runner/postTaskCleanup.d.ts +13 -0
  36. package/dist/runner/postTaskCleanup.js +147 -0
  37. package/dist/runner/runTask.js +50 -10
  38. package/dist/security/discoveryTokenStore.d.ts +62 -0
  39. package/dist/security/discoveryTokenStore.js +100 -0
  40. package/dist/security/toolInvocationGuard.d.ts +35 -0
  41. package/dist/security/toolInvocationGuard.js +153 -0
  42. package/dist/smoke-test.js +18 -10
  43. package/dist/taskRuntime.d.ts +17 -0
  44. package/dist/test/unit/acceptance-engine.test.d.ts +1 -0
  45. package/dist/test/unit/acceptance-engine.test.js +228 -0
  46. package/dist/test/unit/agent-router.test.d.ts +1 -0
  47. package/dist/test/unit/agent-router.test.js +287 -0
  48. package/dist/test/unit/audit-checks.test.d.ts +1 -0
  49. package/dist/test/unit/audit-checks.test.js +350 -0
  50. package/dist/test/unit/diagnose-task.test.d.ts +1 -0
  51. package/dist/test/unit/diagnose-task.test.js +457 -0
  52. package/dist/test/unit/discovery-token-store.test.d.ts +1 -0
  53. package/dist/test/unit/discovery-token-store.test.js +139 -0
  54. package/dist/test/unit/evidence-pack.test.d.ts +1 -0
  55. package/dist/test/unit/evidence-pack.test.js +130 -0
  56. package/dist/test/unit/goal-graph.test.d.ts +1 -0
  57. package/dist/test/unit/goal-graph.test.js +298 -0
  58. package/dist/test/unit/goal-progress.test.d.ts +1 -0
  59. package/dist/test/unit/goal-progress.test.js +381 -0
  60. package/dist/test/unit/goal-status.test.d.ts +1 -0
  61. package/dist/test/unit/goal-status.test.js +215 -0
  62. package/dist/test/unit/goal-store.test.d.ts +1 -0
  63. package/dist/test/unit/goal-store.test.js +253 -0
  64. package/dist/test/unit/goal-subgoal-task.test.d.ts +1 -0
  65. package/dist/test/unit/goal-subgoal-task.test.js +55 -0
  66. package/dist/test/unit/goal-tools-registry.test.d.ts +1 -0
  67. package/dist/test/unit/goal-tools-registry.test.js +190 -0
  68. package/dist/test/unit/handoff-export.test.d.ts +1 -0
  69. package/dist/test/unit/handoff-export.test.js +263 -0
  70. package/dist/test/unit/invoke-discovered-tool.test.d.ts +1 -0
  71. package/dist/test/unit/invoke-discovered-tool.test.js +167 -0
  72. package/dist/test/unit/logging.test.js +127 -5
  73. package/dist/test/unit/post-task-cleanup.test.d.ts +1 -0
  74. package/dist/test/unit/post-task-cleanup.test.js +48 -0
  75. package/dist/test/unit/project-policy-release-mode.test.d.ts +1 -0
  76. package/dist/test/unit/project-policy-release-mode.test.js +125 -0
  77. package/dist/test/unit/reconcile-tasks.test.d.ts +1 -0
  78. package/dist/test/unit/reconcile-tasks.test.js +456 -0
  79. package/dist/test/unit/release-gate.test.d.ts +1 -0
  80. package/dist/test/unit/release-gate.test.js +242 -0
  81. package/dist/test/unit/run-task-loop.test.d.ts +1 -0
  82. package/dist/test/unit/run-task-loop.test.js +380 -0
  83. package/dist/test/unit/safe-views.test.d.ts +1 -0
  84. package/dist/test/unit/safe-views.test.js +171 -0
  85. package/dist/test/unit/schema-drift-check.test.d.ts +1 -0
  86. package/dist/test/unit/schema-drift-check.test.js +176 -0
  87. package/dist/test/unit/subgoal-sync.test.d.ts +1 -0
  88. package/dist/test/unit/subgoal-sync.test.js +183 -0
  89. package/dist/test/unit/tool-invocation-guard.test.d.ts +1 -0
  90. package/dist/test/unit/tool-invocation-guard.test.js +432 -0
  91. package/dist/test/unit/tool-usage-stats.test.d.ts +1 -0
  92. package/dist/test/unit/tool-usage-stats.test.js +300 -0
  93. package/dist/test/unit/toolSearch.test.d.ts +1 -0
  94. package/dist/test/unit/toolSearch.test.js +571 -0
  95. package/dist/test/unit/worktree-manager.test.d.ts +1 -0
  96. package/dist/test/unit/worktree-manager.test.js +176 -0
  97. package/dist/tools/auditTask.d.ts +103 -1
  98. package/dist/tools/auditTask.js +461 -8
  99. package/dist/tools/cancelTask.js +1 -1
  100. package/dist/tools/checkReleaseGate.d.ts +21 -0
  101. package/dist/tools/checkReleaseGate.js +22 -0
  102. package/dist/tools/createTask.d.ts +18 -2
  103. package/dist/tools/createTask.js +29 -1
  104. package/dist/tools/diagnoseTask.d.ts +45 -0
  105. package/dist/tools/diagnoseTask.js +347 -0
  106. package/dist/tools/discardWorktree.d.ts +24 -0
  107. package/dist/tools/discardWorktree.js +27 -0
  108. package/dist/tools/discoverTools.d.ts +11 -0
  109. package/dist/tools/discoverTools.js +39 -0
  110. package/dist/tools/evidencePack.d.ts +39 -0
  111. package/dist/tools/evidencePack.js +168 -0
  112. package/dist/tools/explainTool.d.ts +11 -0
  113. package/dist/tools/explainTool.js +21 -0
  114. package/dist/tools/getTaskSummary.js +2 -1
  115. package/dist/tools/goalSubgoalTask.d.ts +51 -0
  116. package/dist/tools/goalSubgoalTask.js +110 -0
  117. package/dist/tools/healthCheck.d.ts +1 -0
  118. package/dist/tools/healthCheck.js +5 -1
  119. package/dist/tools/invokeDiscoveredTool.d.ts +37 -0
  120. package/dist/tools/invokeDiscoveredTool.js +112 -0
  121. package/dist/tools/listTasks.d.ts +3 -1
  122. package/dist/tools/listTasks.js +14 -1
  123. package/dist/tools/mergeWorktree.d.ts +24 -0
  124. package/dist/tools/mergeWorktree.js +27 -0
  125. package/dist/tools/recommendAgentForTask.d.ts +19 -0
  126. package/dist/tools/recommendAgentForTask.js +56 -0
  127. package/dist/tools/reconcileTasks.d.ts +41 -0
  128. package/dist/tools/reconcileTasks.js +352 -0
  129. package/dist/tools/registry.js +926 -2
  130. package/dist/tools/releaseMode.d.ts +50 -0
  131. package/dist/tools/releaseMode.js +370 -0
  132. package/dist/tools/runDirectVerificationBundle.d.ts +26 -0
  133. package/dist/tools/runDirectVerificationBundle.js +64 -0
  134. package/dist/tools/runTaskLoop.d.ts +57 -0
  135. package/dist/tools/runTaskLoop.js +417 -0
  136. package/dist/tools/runVerification.d.ts +4 -0
  137. package/dist/tools/runVerification.js +4 -0
  138. package/dist/tools/safeStatus.d.ts +31 -1
  139. package/dist/tools/safeStatus.js +43 -1
  140. package/dist/tools/safeViews.d.ts +262 -0
  141. package/dist/tools/safeViews.js +252 -0
  142. package/dist/tools/schemaDriftCheck.d.ts +46 -0
  143. package/dist/tools/schemaDriftCheck.js +80 -0
  144. package/dist/tools/taskLineage.d.ts +91 -0
  145. package/dist/tools/taskLineage.js +175 -0
  146. package/dist/tools/toolCatalog.d.ts +4 -3
  147. package/dist/tools/toolCatalog.js +39 -11
  148. package/dist/tools/toolRegistry.d.ts +61 -0
  149. package/dist/tools/toolRegistry.js +834 -0
  150. package/dist/tools/toolSearch.d.ts +110 -0
  151. package/dist/tools/toolSearch.js +331 -0
  152. package/dist/tools/toolUsageStats.d.ts +41 -0
  153. package/dist/tools/toolUsageStats.js +116 -0
  154. package/dist/tools/waitForTask.js +1 -0
  155. package/dist/version.d.ts +2 -2
  156. package/dist/version.js +2 -2
  157. package/dist/watcherStatus.d.ts +29 -0
  158. package/dist/watcherStatus.js +92 -1
  159. package/docs/chatgpt-usage.md +31 -0
  160. package/docs/control-center/README.md +9 -0
  161. package/package.json +3 -2
  162. package/scripts/checks/control-center-smoke.js +87 -0
  163. package/scripts/checks/control-smoke.js +2 -2
  164. package/scripts/checks/doctor-smoke.js +1 -0
  165. package/scripts/checks/lifecycle-smoke.js +13 -9
  166. package/scripts/checks/mcp-manifest-check.js +65 -6
  167. package/scripts/checks/mcp-smoke.js +60 -9
  168. package/scripts/checks/watcher-supervisor-smoke.js +2 -2
  169. package/src/agents/agentRouter.ts +149 -0
  170. package/src/config.ts +9 -3
  171. package/src/controlCenter.ts +215 -4
  172. package/src/direct/directSessionStore.ts +2 -0
  173. package/src/direct/directVerification.ts +7 -0
  174. package/src/doctor.ts +36 -1
  175. package/src/goal/acceptanceEngine.ts +160 -0
  176. package/src/goal/acceptanceTemplate.ts +121 -0
  177. package/src/goal/goalGraph.ts +225 -0
  178. package/src/goal/goalProgress.ts +301 -0
  179. package/src/goal/goalStatus.ts +234 -0
  180. package/src/goal/goalStore.ts +306 -0
  181. package/src/goal/handoffExport.ts +211 -0
  182. package/src/goal/subgoalSync.ts +82 -0
  183. package/src/goal/worktreeManager.ts +404 -0
  184. package/src/logging.ts +91 -0
  185. package/src/policy/projectPolicy.ts +344 -0
  186. package/src/release/releaseGate.ts +567 -0
  187. package/src/runner/postTaskCleanup.ts +154 -0
  188. package/src/runner/runTask.ts +49 -10
  189. package/src/security/discoveryTokenStore.ts +157 -0
  190. package/src/security/toolInvocationGuard.ts +251 -0
  191. package/src/smoke-test.ts +15 -7
  192. package/src/taskRuntime.ts +17 -0
  193. package/src/test/unit/acceptance-engine.test.ts +261 -0
  194. package/src/test/unit/agent-router.test.ts +342 -0
  195. package/src/test/unit/audit-checks.test.ts +567 -0
  196. package/src/test/unit/diagnose-task.test.ts +544 -0
  197. package/src/test/unit/discovery-token-store.test.ts +181 -0
  198. package/src/test/unit/evidence-pack.test.ts +142 -0
  199. package/src/test/unit/goal-graph.test.ts +347 -0
  200. package/src/test/unit/goal-progress.test.ts +538 -0
  201. package/src/test/unit/goal-status.test.ts +270 -0
  202. package/src/test/unit/goal-store.test.ts +318 -0
  203. package/src/test/unit/goal-subgoal-task.test.ts +72 -0
  204. package/src/test/unit/goal-tools-registry.test.ts +243 -0
  205. package/src/test/unit/handoff-export.test.ts +295 -0
  206. package/src/test/unit/invoke-discovered-tool.test.ts +216 -0
  207. package/src/test/unit/logging.test.ts +177 -5
  208. package/src/test/unit/post-task-cleanup.test.ts +53 -0
  209. package/src/test/unit/project-policy-release-mode.test.ts +156 -0
  210. package/src/test/unit/reconcile-tasks.test.ts +551 -0
  211. package/src/test/unit/release-gate.test.ts +314 -0
  212. package/src/test/unit/run-task-loop.test.ts +425 -0
  213. package/src/test/unit/safe-views.test.ts +184 -0
  214. package/src/test/unit/schema-drift-check.test.ts +260 -0
  215. package/src/test/unit/subgoal-sync.test.ts +236 -0
  216. package/src/test/unit/tool-invocation-guard.test.ts +542 -0
  217. package/src/test/unit/tool-usage-stats.test.ts +384 -0
  218. package/src/test/unit/toolSearch.test.ts +756 -0
  219. package/src/test/unit/worktree-manager.test.ts +247 -0
  220. package/src/tools/auditTask.ts +831 -402
  221. package/src/tools/cancelTask.ts +1 -1
  222. package/src/tools/checkReleaseGate.ts +45 -0
  223. package/src/tools/createTask.ts +64 -6
  224. package/src/tools/diagnoseTask.ts +460 -0
  225. package/src/tools/discardWorktree.ts +42 -0
  226. package/src/tools/discoverTools.ts +51 -0
  227. package/src/tools/evidencePack.ts +205 -0
  228. package/src/tools/explainTool.ts +26 -0
  229. package/src/tools/getTaskSummary.ts +2 -1
  230. package/src/tools/goalSubgoalTask.ts +170 -0
  231. package/src/tools/healthCheck.ts +4 -1
  232. package/src/tools/invokeDiscoveredTool.ts +163 -0
  233. package/src/tools/listTasks.ts +16 -2
  234. package/src/tools/listWorkspace.ts +71 -71
  235. package/src/tools/mergeWorktree.ts +42 -0
  236. package/src/tools/recommendAgentForTask.ts +79 -0
  237. package/src/tools/reconcileTasks.ts +464 -0
  238. package/src/tools/registry.ts +1093 -73
  239. package/src/tools/releaseMode.ts +450 -0
  240. package/src/tools/runDirectVerificationBundle.ts +98 -0
  241. package/src/tools/runTaskLoop.ts +526 -0
  242. package/src/tools/runVerification.ts +8 -0
  243. package/src/tools/safeStatus.ts +73 -2
  244. package/src/tools/safeViews.ts +273 -0
  245. package/src/tools/schemaDriftCheck.ts +120 -0
  246. package/src/tools/taskLineage.ts +300 -0
  247. package/src/tools/toolCatalog.ts +40 -11
  248. package/src/tools/toolRegistry.ts +896 -0
  249. package/src/tools/toolSearch.ts +464 -0
  250. package/src/tools/toolUsageStats.ts +151 -0
  251. package/src/tools/waitForTask.ts +1 -0
  252. package/src/version.ts +2 -2
  253. package/src/watcherStatus.ts +97 -1
  254. package/ui/pages/dashboard.html +143 -2
@@ -0,0 +1,432 @@
1
+ import { describe, it } from "node:test";
2
+ import { strict as assert } from "node:assert";
3
+ import { checkInvocation } from "../../security/toolInvocationGuard.js";
4
+ import { PatchWardenError } from "../../errors.js";
5
+ // ── Helpers ───────────────────────────────────────────────────────
6
+ function makeToolMeta(overrides = {}) {
7
+ return {
8
+ name: "read_workspace_file",
9
+ title: "Read Workspace File",
10
+ summary: "读取工作区文件内容(自动脱敏敏感路径)",
11
+ description: "Read workspace file content with sensitive path redaction.",
12
+ profiles: ["full", "chatgpt_core", "chatgpt_direct"],
13
+ modes: ["delegate", "direct", "audit"],
14
+ tags: ["read", "file", "workspace"],
15
+ aliases: ["read_file"],
16
+ risk: "workspace_read_sensitive",
17
+ requiresConfirmation: false,
18
+ inputSchemaDigest: "sha256:abc123",
19
+ ...overrides,
20
+ };
21
+ }
22
+ function makeTokenRecord(overrides = {}) {
23
+ return {
24
+ token: "tok",
25
+ toolName: "read_workspace_file",
26
+ risk: "workspace_read_sensitive",
27
+ issuedAt: "2026-01-01T00:00:00.000Z",
28
+ expiresAt: "2026-01-01T00:10:00.000Z",
29
+ query: "read file",
30
+ schemaDigest: "sha256:abc123",
31
+ profile: "full",
32
+ ...overrides,
33
+ };
34
+ }
35
+ function makeInput(overrides = {}) {
36
+ return {
37
+ toolName: "read_workspace_file",
38
+ toolMeta: makeToolMeta(),
39
+ args: {},
40
+ discoveryTokenRecord: makeTokenRecord(),
41
+ profile: "full",
42
+ assessmentId: undefined,
43
+ ...overrides,
44
+ };
45
+ }
46
+ // 辅助:断言会抛出指定 reason 的 PatchWardenError
47
+ function assertPatchWardenError(fn, reason, messageContains) {
48
+ assert.throws(fn, (err) => {
49
+ assert.ok(err instanceof PatchWardenError, `expected PatchWardenError, got ${err?.constructor?.name}`);
50
+ assert.equal(err.reason, reason, `expected reason "${reason}", got "${err.reason}"`);
51
+ if (messageContains) {
52
+ assert.ok(err.message.includes(messageContains), `expected message to contain "${messageContains}", got "${err.message}"`);
53
+ }
54
+ assert.equal(err.blocked, true, "blocked should default to true");
55
+ return true;
56
+ });
57
+ }
58
+ // ── Tests ─────────────────────────────────────────────────────────
59
+ describe("toolInvocationGuard", () => {
60
+ describe("① token_tool_mismatch", () => {
61
+ it("toolName 与 record.toolName 不一致时抛 token_tool_mismatch", () => {
62
+ const input = makeInput({
63
+ toolName: "save_plan",
64
+ discoveryTokenRecord: makeTokenRecord({ toolName: "read_workspace_file" }),
65
+ });
66
+ assertPatchWardenError(() => checkInvocation(input), "token_tool_mismatch", "save_plan");
67
+ });
68
+ it("toolName 一致时不抛该错误(成功路径在后面统一验证)", () => {
69
+ const input = makeInput({
70
+ toolName: "read_workspace_file",
71
+ discoveryTokenRecord: makeTokenRecord({ toolName: "read_workspace_file" }),
72
+ });
73
+ // 不应抛 token_tool_mismatch;后续校验会通过
74
+ const result = checkInvocation(input);
75
+ assert.deepEqual(result, { allowed: true });
76
+ });
77
+ });
78
+ describe("② profile_not_allowed", () => {
79
+ it("toolMeta.profiles 不含当前 profile 时抛 profile_not_allowed", () => {
80
+ const input = makeInput({
81
+ toolMeta: makeToolMeta({
82
+ profiles: ["full", "chatgpt_core"],
83
+ }),
84
+ profile: "chatgpt_direct",
85
+ });
86
+ assertPatchWardenError(() => checkInvocation(input), "profile_not_allowed", "chatgpt_direct");
87
+ });
88
+ it("profile 在允许列表内时通过(后续校验继续)", () => {
89
+ const input = makeInput({
90
+ toolMeta: makeToolMeta({ profiles: ["full", "chatgpt_direct"] }),
91
+ profile: "chatgpt_direct",
92
+ });
93
+ const result = checkInvocation(input);
94
+ assert.deepEqual(result, { allowed: true });
95
+ });
96
+ });
97
+ describe("③ risk_exceeded", () => {
98
+ it("toolMeta.risk 高于 record.risk 时抛 risk_exceeded(schema 漂移)", () => {
99
+ const input = makeInput({
100
+ toolMeta: makeToolMeta({
101
+ name: "create_task",
102
+ risk: "workspace_write", // rank 2
103
+ }),
104
+ toolName: "create_task",
105
+ discoveryTokenRecord: makeTokenRecord({
106
+ toolName: "create_task",
107
+ risk: "workspace_read_sensitive", // rank 1
108
+ }),
109
+ // workspace_write 需要 assessmentId
110
+ assessmentId: "asm_risk_exceeded_test",
111
+ });
112
+ assertPatchWardenError(() => checkInvocation(input), "risk_exceeded", "workspace_write");
113
+ });
114
+ it("toolMeta.risk 等于 record.risk 时通过(相等不算超过)", () => {
115
+ const input = makeInput({
116
+ toolMeta: makeToolMeta({ risk: "workspace_read_sensitive" }),
117
+ discoveryTokenRecord: makeTokenRecord({ risk: "workspace_read_sensitive" }),
118
+ });
119
+ const result = checkInvocation(input);
120
+ assert.deepEqual(result, { allowed: true });
121
+ });
122
+ it("toolMeta.risk 低于 record.risk 时通过(更宽松 token 调更安全工具)", () => {
123
+ const input = makeInput({
124
+ toolMeta: makeToolMeta({
125
+ name: "health_check",
126
+ risk: "readonly", // rank 0
127
+ profiles: ["full"],
128
+ }),
129
+ toolName: "health_check",
130
+ discoveryTokenRecord: makeTokenRecord({
131
+ toolName: "health_check",
132
+ risk: "workspace_read_sensitive", // rank 1,更高
133
+ }),
134
+ });
135
+ const result = checkInvocation(input);
136
+ assert.deepEqual(result, { allowed: true });
137
+ });
138
+ });
139
+ describe("④ sensitive_path_blocked", () => {
140
+ it("workspace_read_sensitive 工具读 .env 路径时抛 sensitive_path_blocked", () => {
141
+ const input = makeInput({
142
+ args: { path: ".env" },
143
+ });
144
+ assertPatchWardenError(() => checkInvocation(input), "sensitive_path_blocked", ".env");
145
+ });
146
+ it("workspace_read_sensitive 工具读非敏感路径时通过", () => {
147
+ const input = makeInput({
148
+ args: { path: "src/main.ts" },
149
+ });
150
+ const result = checkInvocation(input);
151
+ assert.deepEqual(result, { allowed: true });
152
+ });
153
+ it("无路径参数时跳过敏感校验", () => {
154
+ const input = makeInput({
155
+ args: { query: "some text" },
156
+ });
157
+ const result = checkInvocation(input);
158
+ assert.deepEqual(result, { allowed: true });
159
+ });
160
+ it("支持 file / repo_path / target_path 等多种路径字段", () => {
161
+ // file 字段命中敏感
162
+ assertPatchWardenError(() => checkInvocation(makeInput({ args: { file: "id_rsa" } })), "sensitive_path_blocked");
163
+ // repo_path 字段命中敏感
164
+ assertPatchWardenError(() => checkInvocation(makeInput({ args: { repo_path: ".npmrc" } })), "sensitive_path_blocked");
165
+ // target_path 字段命中敏感
166
+ assertPatchWardenError(() => checkInvocation(makeInput({ args: { target_path: "config.json" } })), "sensitive_path_blocked");
167
+ });
168
+ });
169
+ describe("⑤ assessment_required", () => {
170
+ it("workspace_write 工具无 assessmentId 时抛 assessment_required", () => {
171
+ const input = makeInput({
172
+ toolMeta: makeToolMeta({
173
+ name: "save_plan",
174
+ risk: "workspace_write",
175
+ profiles: ["full"],
176
+ }),
177
+ toolName: "save_plan",
178
+ discoveryTokenRecord: makeTokenRecord({
179
+ toolName: "save_plan",
180
+ risk: "workspace_write",
181
+ }),
182
+ assessmentId: undefined,
183
+ });
184
+ assertPatchWardenError(() => checkInvocation(input), "assessment_required", "save_plan");
185
+ });
186
+ it("workspace_write 工具 assessmentId 为空字符串时抛 assessment_required", () => {
187
+ const input = makeInput({
188
+ toolMeta: makeToolMeta({
189
+ name: "save_plan",
190
+ risk: "workspace_write",
191
+ profiles: ["full"],
192
+ }),
193
+ toolName: "save_plan",
194
+ discoveryTokenRecord: makeTokenRecord({
195
+ toolName: "save_plan",
196
+ risk: "workspace_write",
197
+ }),
198
+ assessmentId: " ",
199
+ });
200
+ assertPatchWardenError(() => checkInvocation(input), "assessment_required");
201
+ });
202
+ it("workspace_write 工具有 assessmentId 时通过", () => {
203
+ const input = makeInput({
204
+ toolMeta: makeToolMeta({
205
+ name: "save_plan",
206
+ risk: "workspace_write",
207
+ profiles: ["full"],
208
+ }),
209
+ toolName: "save_plan",
210
+ discoveryTokenRecord: makeTokenRecord({
211
+ toolName: "save_plan",
212
+ risk: "workspace_write",
213
+ }),
214
+ assessmentId: "asm_workspace_write_ok",
215
+ });
216
+ const result = checkInvocation(input);
217
+ assert.deepEqual(result, { allowed: true });
218
+ });
219
+ });
220
+ describe("⑥ command_not_allowed", () => {
221
+ function commandInput(command) {
222
+ return makeInput({
223
+ toolMeta: makeToolMeta({
224
+ name: "run_verification",
225
+ risk: "command",
226
+ profiles: ["full", "chatgpt_direct"],
227
+ }),
228
+ toolName: "run_verification",
229
+ discoveryTokenRecord: makeTokenRecord({
230
+ toolName: "run_verification",
231
+ risk: "command",
232
+ }),
233
+ args: { command },
234
+ });
235
+ }
236
+ it("args.command 含管道 | 时抛 command_not_allowed", () => {
237
+ assertPatchWardenError(() => checkInvocation(commandInput("ls | grep secret")), "command_not_allowed");
238
+ });
239
+ it("args.command 含 & 时抛 command_not_allowed", () => {
240
+ assertPatchWardenError(() => checkInvocation(commandInput("rm -rf & background")), "command_not_allowed");
241
+ });
242
+ it("args.command 含 $() 时抛 command_not_allowed", () => {
243
+ assertPatchWardenError(() => checkInvocation(commandInput("echo $(cat .env)")), "command_not_allowed");
244
+ });
245
+ it("args.command 含反引号时抛 command_not_allowed", () => {
246
+ assertPatchWardenError(() => checkInvocation(commandInput("echo `whoami`")), "command_not_allowed");
247
+ });
248
+ it("args.command 含分号时抛 command_not_allowed", () => {
249
+ assertPatchWardenError(() => checkInvocation(commandInput("ls; rm -rf /")), "command_not_allowed");
250
+ });
251
+ it("args.command 不含元字符时通过(白名单由 handler 校验)", () => {
252
+ const result = checkInvocation(commandInput("npm test"));
253
+ assert.deepEqual(result, { allowed: true });
254
+ });
255
+ it("args.command 不存在时跳过(handler 内部再校验)", () => {
256
+ const result = checkInvocation(commandInput(undefined));
257
+ assert.deepEqual(result, { allowed: true });
258
+ });
259
+ it("args.command 为空字符串时跳过", () => {
260
+ const result = checkInvocation(commandInput(" "));
261
+ assert.deepEqual(result, { allowed: true });
262
+ });
263
+ });
264
+ describe("⑦ release_confirmation_required", () => {
265
+ it("release 工具无 assessmentId 时抛 release_confirmation_required", () => {
266
+ const input = makeInput({
267
+ toolMeta: makeToolMeta({
268
+ name: "publish_release",
269
+ risk: "release",
270
+ profiles: ["full"],
271
+ }),
272
+ toolName: "publish_release",
273
+ discoveryTokenRecord: makeTokenRecord({
274
+ toolName: "publish_release",
275
+ risk: "release",
276
+ }),
277
+ assessmentId: undefined,
278
+ });
279
+ assertPatchWardenError(() => checkInvocation(input), "release_confirmation_required", "publish_release");
280
+ });
281
+ it("release 工具有 assessmentId 时通过", () => {
282
+ const input = makeInput({
283
+ toolMeta: makeToolMeta({
284
+ name: "publish_release",
285
+ risk: "release",
286
+ profiles: ["full"],
287
+ }),
288
+ toolName: "publish_release",
289
+ discoveryTokenRecord: makeTokenRecord({
290
+ toolName: "publish_release",
291
+ risk: "release",
292
+ }),
293
+ assessmentId: "asm_release_confirm",
294
+ });
295
+ const result = checkInvocation(input);
296
+ assert.deepEqual(result, { allowed: true });
297
+ });
298
+ });
299
+ describe("⑧ credential_sensitive_blocked", () => {
300
+ it("credential_sensitive 工具总是被拒绝", () => {
301
+ const input = makeInput({
302
+ toolMeta: makeToolMeta({
303
+ name: "rotate_secret",
304
+ risk: "credential_sensitive",
305
+ profiles: ["full"],
306
+ }),
307
+ toolName: "rotate_secret",
308
+ discoveryTokenRecord: makeTokenRecord({
309
+ toolName: "rotate_secret",
310
+ risk: "credential_sensitive",
311
+ }),
312
+ assessmentId: "asm_does_not_matter",
313
+ });
314
+ assertPatchWardenError(() => checkInvocation(input), "credential_sensitive_blocked", "rotate_secret");
315
+ });
316
+ it("credential_sensitive 工具即使有 assessmentId 也被拒绝", () => {
317
+ const input = makeInput({
318
+ toolMeta: makeToolMeta({
319
+ name: "rotate_secret",
320
+ risk: "credential_sensitive",
321
+ profiles: ["full"],
322
+ }),
323
+ toolName: "rotate_secret",
324
+ discoveryTokenRecord: makeTokenRecord({
325
+ toolName: "rotate_secret",
326
+ risk: "credential_sensitive",
327
+ }),
328
+ assessmentId: "asm_credential",
329
+ });
330
+ assertPatchWardenError(() => checkInvocation(input), "credential_sensitive_blocked");
331
+ });
332
+ });
333
+ describe("成功场景", () => {
334
+ it("成功场景 1: readonly 工具 + 有效 token + profile 允许 → { allowed: true }", () => {
335
+ const input = makeInput({
336
+ toolMeta: makeToolMeta({
337
+ name: "health_check",
338
+ risk: "readonly",
339
+ profiles: ["full", "chatgpt_core", "chatgpt_direct"],
340
+ }),
341
+ toolName: "health_check",
342
+ discoveryTokenRecord: makeTokenRecord({
343
+ toolName: "health_check",
344
+ risk: "readonly",
345
+ }),
346
+ profile: "chatgpt_core",
347
+ args: {},
348
+ });
349
+ const result = checkInvocation(input);
350
+ assert.deepEqual(result, { allowed: true });
351
+ });
352
+ it("成功场景 2: workspace_write 工具 + 有效 token + 有 assessmentId → { allowed: true }", () => {
353
+ const input = makeInput({
354
+ toolMeta: makeToolMeta({
355
+ name: "create_task",
356
+ risk: "workspace_write",
357
+ profiles: ["full", "chatgpt_core"],
358
+ }),
359
+ toolName: "create_task",
360
+ discoveryTokenRecord: makeTokenRecord({
361
+ toolName: "create_task",
362
+ risk: "workspace_write",
363
+ }),
364
+ profile: "chatgpt_core",
365
+ assessmentId: "asm_success_write",
366
+ args: { prompt: "do something" },
367
+ });
368
+ const result = checkInvocation(input);
369
+ assert.deepEqual(result, { allowed: true });
370
+ });
371
+ it("成功场景 3: workspace_read_sensitive 工具 + 有效 token + 非敏感路径 → { allowed: true }", () => {
372
+ const input = makeInput({
373
+ toolMeta: makeToolMeta({
374
+ name: "read_workspace_file",
375
+ risk: "workspace_read_sensitive",
376
+ profiles: ["full", "chatgpt_core", "chatgpt_direct"],
377
+ }),
378
+ toolName: "read_workspace_file",
379
+ discoveryTokenRecord: makeTokenRecord({
380
+ toolName: "read_workspace_file",
381
+ risk: "workspace_read_sensitive",
382
+ }),
383
+ profile: "chatgpt_direct",
384
+ args: { path: "src/index.ts", file: "README.md" },
385
+ });
386
+ const result = checkInvocation(input);
387
+ assert.deepEqual(result, { allowed: true });
388
+ });
389
+ it("成功场景 4: command 工具 + 有效 token + 安全命令 → { allowed: true }", () => {
390
+ const input = makeInput({
391
+ toolMeta: makeToolMeta({
392
+ name: "run_verification",
393
+ risk: "command",
394
+ profiles: ["full", "chatgpt_direct"],
395
+ }),
396
+ toolName: "run_verification",
397
+ discoveryTokenRecord: makeTokenRecord({
398
+ toolName: "run_verification",
399
+ risk: "command",
400
+ }),
401
+ profile: "chatgpt_direct",
402
+ args: { command: "npm.cmd test" },
403
+ });
404
+ const result = checkInvocation(input);
405
+ assert.deepEqual(result, { allowed: true });
406
+ });
407
+ });
408
+ describe("GuardInput 类型守卫与边界", () => {
409
+ it("args.command 为非字符串类型时跳过(handler 内再校验)", () => {
410
+ const input = makeInput({
411
+ toolMeta: makeToolMeta({
412
+ name: "run_verification",
413
+ risk: "command",
414
+ profiles: ["full"],
415
+ }),
416
+ toolName: "run_verification",
417
+ discoveryTokenRecord: makeTokenRecord({
418
+ toolName: "run_verification",
419
+ risk: "command",
420
+ }),
421
+ args: { command: 123 },
422
+ });
423
+ const result = checkInvocation(input);
424
+ assert.deepEqual(result, { allowed: true });
425
+ });
426
+ it("GuardResult.allowed 始终为 true(失败时已抛错)", () => {
427
+ const result = checkInvocation(makeInput());
428
+ assert.equal(result.allowed, true);
429
+ assert.equal(Object.keys(result).length, 1, "GuardResult 只应有 allowed 字段");
430
+ });
431
+ });
432
+ });
@@ -0,0 +1 @@
1
+ export {};