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