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,130 @@
|
|
|
1
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
2
|
+
import { strict as assert } from "node:assert";
|
|
3
|
+
import { mkdtempSync, rmSync, writeFileSync, readFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { reloadConfig } from "../../config.js";
|
|
7
|
+
import { recommendAgentForTask } from "../../tools/recommendAgentForTask.js";
|
|
8
|
+
import { exportTaskEvidencePack, listEvidencePacks, readEvidencePack } from "../../tools/evidencePack.js";
|
|
9
|
+
import { writeTaskLineage } from "../../tools/taskLineage.js";
|
|
10
|
+
let tempDir;
|
|
11
|
+
let prevConfigEnv;
|
|
12
|
+
function writeConfig() {
|
|
13
|
+
const configPath = join(tempDir, "patchwarden.config.json");
|
|
14
|
+
writeFileSync(configPath, JSON.stringify({
|
|
15
|
+
workspaceRoot: tempDir,
|
|
16
|
+
tasksDir: ".patchwarden/tasks",
|
|
17
|
+
plansDir: ".patchwarden/plans",
|
|
18
|
+
assessmentsDir: ".patchwarden/assessments",
|
|
19
|
+
agents: {
|
|
20
|
+
codex: { command: "codex", args: [] },
|
|
21
|
+
opencode: { command: "opencode", args: [] },
|
|
22
|
+
},
|
|
23
|
+
allowedTestCommands: ["npm test", "npm run build"],
|
|
24
|
+
defaultTaskTimeoutSeconds: 30,
|
|
25
|
+
maxTaskTimeoutSeconds: 120,
|
|
26
|
+
}), "utf-8");
|
|
27
|
+
prevConfigEnv = process.env.PATCHWARDEN_CONFIG;
|
|
28
|
+
process.env.PATCHWARDEN_CONFIG = configPath;
|
|
29
|
+
reloadConfig();
|
|
30
|
+
}
|
|
31
|
+
describe("v1.5 evidence packs and agent recommendations", () => {
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
tempDir = mkdtempSync(join(tmpdir(), "pw-evidence-"));
|
|
34
|
+
writeConfig();
|
|
35
|
+
});
|
|
36
|
+
afterEach(() => {
|
|
37
|
+
if (prevConfigEnv === undefined)
|
|
38
|
+
delete process.env.PATCHWARDEN_CONFIG;
|
|
39
|
+
else
|
|
40
|
+
process.env.PATCHWARDEN_CONFIG = prevConfigEnv;
|
|
41
|
+
reloadConfig();
|
|
42
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
43
|
+
});
|
|
44
|
+
it("recommends an agent without creating task evidence", () => {
|
|
45
|
+
const recommendation = recommendAgentForTask({
|
|
46
|
+
repo_path: ".",
|
|
47
|
+
goal: "Refactor two modules safely",
|
|
48
|
+
scope_files: ["src/a.ts", "src/b.ts"],
|
|
49
|
+
});
|
|
50
|
+
assert.equal(recommendation.bounded, true);
|
|
51
|
+
assert.ok(["codex", "opencode"].includes(recommendation.recommended_agent));
|
|
52
|
+
assert.ok(Array.isArray(recommendation.suggested_verify_commands));
|
|
53
|
+
const payload = JSON.stringify(recommendation);
|
|
54
|
+
assert.ok(!payload.includes("stdout"));
|
|
55
|
+
assert.ok(!payload.includes("stderr"));
|
|
56
|
+
assert.ok(!payload.includes("diff"));
|
|
57
|
+
});
|
|
58
|
+
it("exports bounded BOM-free evidence pack from lineage", () => {
|
|
59
|
+
writeTaskLineage({
|
|
60
|
+
lineage_id: "lineage_v15_pack",
|
|
61
|
+
goal: "Export evidence",
|
|
62
|
+
repo_path: ".",
|
|
63
|
+
created_at: "2026-07-05T12:00:00.000Z",
|
|
64
|
+
updated_at: "2026-07-05T12:01:00.000Z",
|
|
65
|
+
final_status: "accepted",
|
|
66
|
+
stop_reason: "success",
|
|
67
|
+
next_action: "accept",
|
|
68
|
+
main_task: "task-main",
|
|
69
|
+
fix_tasks: [],
|
|
70
|
+
cleanup_tasks: [],
|
|
71
|
+
direct_sessions: [{
|
|
72
|
+
session_id: "direct-one",
|
|
73
|
+
status: "passed",
|
|
74
|
+
command_count: 1,
|
|
75
|
+
passed_commands: 1,
|
|
76
|
+
failed_commands: 0,
|
|
77
|
+
timed_out_commands: 0,
|
|
78
|
+
audit_decision: "pass",
|
|
79
|
+
changed_files_total: 0,
|
|
80
|
+
next_action: "accept",
|
|
81
|
+
}],
|
|
82
|
+
rounds: [{
|
|
83
|
+
iteration: 1,
|
|
84
|
+
task_id: "task-main",
|
|
85
|
+
role: "main",
|
|
86
|
+
status: "done_by_agent",
|
|
87
|
+
terminal: true,
|
|
88
|
+
verification_status: "passed",
|
|
89
|
+
audit_verdict: "pass",
|
|
90
|
+
fail_checks: [],
|
|
91
|
+
warn_checks: [],
|
|
92
|
+
next_action: "accept",
|
|
93
|
+
}],
|
|
94
|
+
warnings: [],
|
|
95
|
+
errors: [],
|
|
96
|
+
worktree: {
|
|
97
|
+
isolation_mode: "worktree",
|
|
98
|
+
worktree_id: "wt-one",
|
|
99
|
+
worktree_path: join(tempDir, "_workspacetrees", "wt-one"),
|
|
100
|
+
branch: "pw-test",
|
|
101
|
+
cleanup: "keep",
|
|
102
|
+
status: "active",
|
|
103
|
+
next_action: "merge or discard explicitly",
|
|
104
|
+
},
|
|
105
|
+
agent_routing: {
|
|
106
|
+
requested_agent: "auto",
|
|
107
|
+
selected_agent: "codex",
|
|
108
|
+
reason: "test route",
|
|
109
|
+
fallback: false,
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
const pack = exportTaskEvidencePack({ lineage_id: "lineage_v15_pack" });
|
|
113
|
+
assert.equal(pack.bounded, true);
|
|
114
|
+
assert.equal(pack.lineage.worktree.worktree_id, "wt-one");
|
|
115
|
+
assert.equal(pack.lineage.agent_routing?.selected_agent, "codex");
|
|
116
|
+
const raw = readFileSync(pack.files.json);
|
|
117
|
+
assert.notEqual(raw[0], 0xef);
|
|
118
|
+
JSON.parse(raw.toString("utf-8"));
|
|
119
|
+
const payload = JSON.stringify(pack);
|
|
120
|
+
assert.ok(!payload.includes("stdout_tail"));
|
|
121
|
+
assert.ok(!payload.includes("stderr_tail"));
|
|
122
|
+
assert.ok(!payload.includes("diff.patch"));
|
|
123
|
+
assert.ok(!payload.includes("verification.log"));
|
|
124
|
+
const readBack = readEvidencePack("lineage_v15_pack");
|
|
125
|
+
assert.equal(readBack?.lineage_id, "lineage_v15_pack");
|
|
126
|
+
const listed = listEvidencePacks();
|
|
127
|
+
assert.equal(listed.total, 1);
|
|
128
|
+
assert.equal(listed.evidence_packs[0].lineage_id, "lineage_v15_pack");
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import { strict as assert } from "node:assert";
|
|
3
|
+
import { getReadySubgoals, getBlockedSubgoals, suggestNextSubgoal, detectCycle, topologicalSort, } from "../../goal/goalGraph.js";
|
|
4
|
+
import { PatchWardenError } from "../../errors.js";
|
|
5
|
+
// ── Helpers ───────────────────────────────────────────────────────
|
|
6
|
+
function makeSubgoal(overrides) {
|
|
7
|
+
return {
|
|
8
|
+
title: "Subgoal " + overrides.id,
|
|
9
|
+
status: "ready",
|
|
10
|
+
depends_on: [],
|
|
11
|
+
task_ids: [],
|
|
12
|
+
...overrides,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function makeGoalStatus(subgoals, overrides = {}) {
|
|
16
|
+
const now = new Date().toISOString();
|
|
17
|
+
return {
|
|
18
|
+
goal_id: "goal_test_001",
|
|
19
|
+
title: "Test Goal",
|
|
20
|
+
status: "active",
|
|
21
|
+
repo_path: "/repo/test",
|
|
22
|
+
created_at: now,
|
|
23
|
+
updated_at: now,
|
|
24
|
+
subgoals,
|
|
25
|
+
...overrides,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
// ── Tests ─────────────────────────────────────────────────────────
|
|
29
|
+
describe("goalGraph", () => {
|
|
30
|
+
describe("getReadySubgoals", () => {
|
|
31
|
+
it("无 subgoal 返回空数组", () => {
|
|
32
|
+
const goal = makeGoalStatus([]);
|
|
33
|
+
assert.deepEqual(getReadySubgoals(goal), []);
|
|
34
|
+
});
|
|
35
|
+
it("ready 且无依赖 → 返回", () => {
|
|
36
|
+
const goal = makeGoalStatus([
|
|
37
|
+
makeSubgoal({ id: "subgoal-001", depends_on: [] }),
|
|
38
|
+
]);
|
|
39
|
+
const ready = getReadySubgoals(goal);
|
|
40
|
+
assert.equal(ready.length, 1);
|
|
41
|
+
assert.equal(ready[0].id, "subgoal-001");
|
|
42
|
+
});
|
|
43
|
+
it("ready 但依赖未 accepted → 不返回", () => {
|
|
44
|
+
const goal = makeGoalStatus([
|
|
45
|
+
makeSubgoal({ id: "subgoal-001", status: "running" }),
|
|
46
|
+
makeSubgoal({ id: "subgoal-002", depends_on: ["subgoal-001"] }),
|
|
47
|
+
]);
|
|
48
|
+
const ready = getReadySubgoals(goal);
|
|
49
|
+
assert.equal(ready.length, 0);
|
|
50
|
+
});
|
|
51
|
+
it("ready 且依赖已 accepted → 返回", () => {
|
|
52
|
+
const goal = makeGoalStatus([
|
|
53
|
+
makeSubgoal({ id: "subgoal-001", status: "accepted", accepted_at: "2026-01-01T00:00:00.000Z" }),
|
|
54
|
+
makeSubgoal({ id: "subgoal-002", depends_on: ["subgoal-001"] }),
|
|
55
|
+
]);
|
|
56
|
+
const ready = getReadySubgoals(goal);
|
|
57
|
+
assert.equal(ready.length, 1);
|
|
58
|
+
assert.equal(ready[0].id, "subgoal-002");
|
|
59
|
+
});
|
|
60
|
+
it("非 ready 状态(running 等)→ 不返回", () => {
|
|
61
|
+
const goal = makeGoalStatus([
|
|
62
|
+
makeSubgoal({ id: "subgoal-001", status: "running" }),
|
|
63
|
+
makeSubgoal({ id: "subgoal-002", status: "done_by_agent" }),
|
|
64
|
+
makeSubgoal({ id: "subgoal-003", status: "accepted" }),
|
|
65
|
+
]);
|
|
66
|
+
const ready = getReadySubgoals(goal);
|
|
67
|
+
assert.equal(ready.length, 0);
|
|
68
|
+
});
|
|
69
|
+
it("依赖引用不存在的 subgoal → 不返回", () => {
|
|
70
|
+
const goal = makeGoalStatus([
|
|
71
|
+
makeSubgoal({ id: "subgoal-001", depends_on: ["subgoal-999"] }),
|
|
72
|
+
]);
|
|
73
|
+
const ready = getReadySubgoals(goal);
|
|
74
|
+
assert.equal(ready.length, 0);
|
|
75
|
+
});
|
|
76
|
+
it("不修改传入的 goalStatus(不可变)", () => {
|
|
77
|
+
const goal = makeGoalStatus([
|
|
78
|
+
makeSubgoal({ id: "subgoal-001", depends_on: [] }),
|
|
79
|
+
]);
|
|
80
|
+
const snapshot = JSON.stringify(goal);
|
|
81
|
+
getReadySubgoals(goal);
|
|
82
|
+
assert.equal(JSON.stringify(goal), snapshot);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
describe("getBlockedSubgoals", () => {
|
|
86
|
+
it("无 blocked 返回空数组", () => {
|
|
87
|
+
const goal = makeGoalStatus([
|
|
88
|
+
makeSubgoal({ id: "subgoal-001", depends_on: [] }),
|
|
89
|
+
makeSubgoal({ id: "subgoal-002", status: "running" }),
|
|
90
|
+
]);
|
|
91
|
+
assert.deepEqual(getBlockedSubgoals(goal), []);
|
|
92
|
+
});
|
|
93
|
+
it("ready 但依赖 running → 返回,blocked_by 包含该 id", () => {
|
|
94
|
+
const goal = makeGoalStatus([
|
|
95
|
+
makeSubgoal({ id: "subgoal-001", status: "running" }),
|
|
96
|
+
makeSubgoal({ id: "subgoal-002", depends_on: ["subgoal-001"] }),
|
|
97
|
+
]);
|
|
98
|
+
const blocked = getBlockedSubgoals(goal);
|
|
99
|
+
assert.equal(blocked.length, 1);
|
|
100
|
+
assert.equal(blocked[0].subgoal.id, "subgoal-002");
|
|
101
|
+
assert.deepEqual(blocked[0].blocked_by, ["subgoal-001"]);
|
|
102
|
+
});
|
|
103
|
+
it("依赖引用不存在 → blocked_by 包含该不存在的 id", () => {
|
|
104
|
+
const goal = makeGoalStatus([
|
|
105
|
+
makeSubgoal({ id: "subgoal-001", depends_on: ["subgoal-999"] }),
|
|
106
|
+
]);
|
|
107
|
+
const blocked = getBlockedSubgoals(goal);
|
|
108
|
+
assert.equal(blocked.length, 1);
|
|
109
|
+
assert.equal(blocked[0].subgoal.id, "subgoal-001");
|
|
110
|
+
assert.deepEqual(blocked[0].blocked_by, ["subgoal-999"]);
|
|
111
|
+
});
|
|
112
|
+
it("依赖部分 accepted 部分 running → blocked_by 只含未 accepted 的", () => {
|
|
113
|
+
const goal = makeGoalStatus([
|
|
114
|
+
makeSubgoal({ id: "subgoal-001", status: "accepted", accepted_at: "2026-01-01T00:00:00.000Z" }),
|
|
115
|
+
makeSubgoal({ id: "subgoal-002", status: "running" }),
|
|
116
|
+
makeSubgoal({ id: "subgoal-003", depends_on: ["subgoal-001", "subgoal-002"] }),
|
|
117
|
+
]);
|
|
118
|
+
const blocked = getBlockedSubgoals(goal);
|
|
119
|
+
assert.equal(blocked.length, 1);
|
|
120
|
+
assert.deepEqual(blocked[0].blocked_by, ["subgoal-002"]);
|
|
121
|
+
});
|
|
122
|
+
it("非 ready 状态的 subgoal 不计入 blocked", () => {
|
|
123
|
+
const goal = makeGoalStatus([
|
|
124
|
+
makeSubgoal({ id: "subgoal-001", status: "running", depends_on: ["subgoal-999"] }),
|
|
125
|
+
]);
|
|
126
|
+
const blocked = getBlockedSubgoals(goal);
|
|
127
|
+
assert.equal(blocked.length, 0);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
describe("suggestNextSubgoal", () => {
|
|
131
|
+
it("有 ready → 返回第一个 ready", () => {
|
|
132
|
+
const goal = makeGoalStatus([
|
|
133
|
+
makeSubgoal({ id: "subgoal-001", depends_on: [] }),
|
|
134
|
+
]);
|
|
135
|
+
const suggestion = suggestNextSubgoal(goal);
|
|
136
|
+
assert.equal(suggestion.subgoal_id, "subgoal-001");
|
|
137
|
+
assert.equal(suggestion.title, "Subgoal subgoal-001");
|
|
138
|
+
assert.deepEqual(suggestion.depends_on, []);
|
|
139
|
+
assert.equal(suggestion.reason, undefined);
|
|
140
|
+
});
|
|
141
|
+
it("无 ready 有 blocked → 返回 null + blocked_by", () => {
|
|
142
|
+
const goal = makeGoalStatus([
|
|
143
|
+
makeSubgoal({ id: "subgoal-001", status: "running" }),
|
|
144
|
+
makeSubgoal({ id: "subgoal-002", depends_on: ["subgoal-001"] }),
|
|
145
|
+
]);
|
|
146
|
+
const suggestion = suggestNextSubgoal(goal);
|
|
147
|
+
assert.equal(suggestion.subgoal_id, null);
|
|
148
|
+
assert.equal(suggestion.reason, "dependencies_not_met");
|
|
149
|
+
assert.deepEqual(suggestion.blocked_by, ["subgoal-002"]);
|
|
150
|
+
});
|
|
151
|
+
it("无 ready 无 blocked → 返回 null + no_ready_subgoal", () => {
|
|
152
|
+
const goal = makeGoalStatus([
|
|
153
|
+
makeSubgoal({ id: "subgoal-001", status: "accepted", accepted_at: "2026-01-01T00:00:00.000Z" }),
|
|
154
|
+
makeSubgoal({ id: "subgoal-002", status: "running" }),
|
|
155
|
+
]);
|
|
156
|
+
const suggestion = suggestNextSubgoal(goal);
|
|
157
|
+
assert.equal(suggestion.subgoal_id, null);
|
|
158
|
+
assert.equal(suggestion.reason, "no_ready_subgoal");
|
|
159
|
+
assert.equal(suggestion.blocked_by, undefined);
|
|
160
|
+
});
|
|
161
|
+
it("多个 ready → 返回数组顺序第一个", () => {
|
|
162
|
+
const goal = makeGoalStatus([
|
|
163
|
+
makeSubgoal({ id: "subgoal-001", depends_on: [] }),
|
|
164
|
+
makeSubgoal({ id: "subgoal-002", depends_on: [] }),
|
|
165
|
+
makeSubgoal({ id: "subgoal-003", depends_on: [] }),
|
|
166
|
+
]);
|
|
167
|
+
const suggestion = suggestNextSubgoal(goal);
|
|
168
|
+
assert.equal(suggestion.subgoal_id, "subgoal-001");
|
|
169
|
+
});
|
|
170
|
+
it("空 subgoals → no_ready_subgoal", () => {
|
|
171
|
+
const goal = makeGoalStatus([]);
|
|
172
|
+
const suggestion = suggestNextSubgoal(goal);
|
|
173
|
+
assert.equal(suggestion.subgoal_id, null);
|
|
174
|
+
assert.equal(suggestion.reason, "no_ready_subgoal");
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
describe("detectCycle", () => {
|
|
178
|
+
it("无依赖无环", () => {
|
|
179
|
+
const goal = makeGoalStatus([
|
|
180
|
+
makeSubgoal({ id: "subgoal-001", depends_on: [] }),
|
|
181
|
+
makeSubgoal({ id: "subgoal-002", depends_on: [] }),
|
|
182
|
+
]);
|
|
183
|
+
assert.equal(detectCycle(goal), null);
|
|
184
|
+
});
|
|
185
|
+
it("线性依赖 A→B 无环(B depends_on A)", () => {
|
|
186
|
+
const goal = makeGoalStatus([
|
|
187
|
+
makeSubgoal({ id: "A", depends_on: [] }),
|
|
188
|
+
makeSubgoal({ id: "B", depends_on: ["A"] }),
|
|
189
|
+
]);
|
|
190
|
+
assert.equal(detectCycle(goal), null);
|
|
191
|
+
});
|
|
192
|
+
it("A→B→A 有环,返回环路径", () => {
|
|
193
|
+
const goal = makeGoalStatus([
|
|
194
|
+
makeSubgoal({ id: "A", depends_on: ["B"] }),
|
|
195
|
+
makeSubgoal({ id: "B", depends_on: ["A"] }),
|
|
196
|
+
]);
|
|
197
|
+
const cycle = detectCycle(goal);
|
|
198
|
+
assert.ok(cycle !== null);
|
|
199
|
+
assert.ok(cycle.length >= 2);
|
|
200
|
+
assert.ok(cycle.includes("A"));
|
|
201
|
+
assert.ok(cycle.includes("B"));
|
|
202
|
+
});
|
|
203
|
+
it("自环 A→A 有环", () => {
|
|
204
|
+
const goal = makeGoalStatus([
|
|
205
|
+
makeSubgoal({ id: "A", depends_on: ["A"] }),
|
|
206
|
+
]);
|
|
207
|
+
const cycle = detectCycle(goal);
|
|
208
|
+
assert.ok(cycle !== null);
|
|
209
|
+
assert.ok(cycle.includes("A"));
|
|
210
|
+
});
|
|
211
|
+
it("引用不存在的 subgoal 不算环", () => {
|
|
212
|
+
const goal = makeGoalStatus([
|
|
213
|
+
makeSubgoal({ id: "A", depends_on: ["nonexistent"] }),
|
|
214
|
+
]);
|
|
215
|
+
assert.equal(detectCycle(goal), null);
|
|
216
|
+
});
|
|
217
|
+
it("三节点环 A→B→C→A", () => {
|
|
218
|
+
const goal = makeGoalStatus([
|
|
219
|
+
makeSubgoal({ id: "A", depends_on: ["C"] }),
|
|
220
|
+
makeSubgoal({ id: "B", depends_on: ["A"] }),
|
|
221
|
+
makeSubgoal({ id: "C", depends_on: ["B"] }),
|
|
222
|
+
]);
|
|
223
|
+
const cycle = detectCycle(goal);
|
|
224
|
+
assert.ok(cycle !== null);
|
|
225
|
+
assert.ok(cycle.includes("A"));
|
|
226
|
+
assert.ok(cycle.includes("B"));
|
|
227
|
+
assert.ok(cycle.includes("C"));
|
|
228
|
+
});
|
|
229
|
+
it("无环的复杂依赖图", () => {
|
|
230
|
+
const goal = makeGoalStatus([
|
|
231
|
+
makeSubgoal({ id: "A", depends_on: [] }),
|
|
232
|
+
makeSubgoal({ id: "B", depends_on: ["A"] }),
|
|
233
|
+
makeSubgoal({ id: "C", depends_on: ["A"] }),
|
|
234
|
+
makeSubgoal({ id: "D", depends_on: ["B", "C"] }),
|
|
235
|
+
]);
|
|
236
|
+
assert.equal(detectCycle(goal), null);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
describe("topologicalSort", () => {
|
|
240
|
+
it("线性 A→B(B 依赖 A)→ 返回 [A, B]", () => {
|
|
241
|
+
const goal = makeGoalStatus([
|
|
242
|
+
makeSubgoal({ id: "A", depends_on: [] }),
|
|
243
|
+
makeSubgoal({ id: "B", depends_on: ["A"] }),
|
|
244
|
+
]);
|
|
245
|
+
const sorted = topologicalSort(goal);
|
|
246
|
+
assert.equal(sorted.length, 2);
|
|
247
|
+
const aIndex = sorted.indexOf("A");
|
|
248
|
+
const bIndex = sorted.indexOf("B");
|
|
249
|
+
assert.ok(aIndex < bIndex, "A should come before B");
|
|
250
|
+
});
|
|
251
|
+
it("有环抛错", () => {
|
|
252
|
+
const goal = makeGoalStatus([
|
|
253
|
+
makeSubgoal({ id: "A", depends_on: ["B"] }),
|
|
254
|
+
makeSubgoal({ id: "B", depends_on: ["A"] }),
|
|
255
|
+
]);
|
|
256
|
+
assert.throws(() => topologicalSort(goal), (err) => {
|
|
257
|
+
assert.ok(err instanceof PatchWardenError);
|
|
258
|
+
assert.equal(err.reason, "dependency_cycle");
|
|
259
|
+
return true;
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
it("单节点无依赖", () => {
|
|
263
|
+
const goal = makeGoalStatus([
|
|
264
|
+
makeSubgoal({ id: "A", depends_on: [] }),
|
|
265
|
+
]);
|
|
266
|
+
assert.deepEqual(topologicalSort(goal), ["A"]);
|
|
267
|
+
});
|
|
268
|
+
it("菱形依赖:D 依赖 B、C,B/C 依赖 A → A 最前,D 最后", () => {
|
|
269
|
+
const goal = makeGoalStatus([
|
|
270
|
+
makeSubgoal({ id: "A", depends_on: [] }),
|
|
271
|
+
makeSubgoal({ id: "B", depends_on: ["A"] }),
|
|
272
|
+
makeSubgoal({ id: "C", depends_on: ["A"] }),
|
|
273
|
+
makeSubgoal({ id: "D", depends_on: ["B", "C"] }),
|
|
274
|
+
]);
|
|
275
|
+
const sorted = topologicalSort(goal);
|
|
276
|
+
assert.equal(sorted.length, 4);
|
|
277
|
+
const aIndex = sorted.indexOf("A");
|
|
278
|
+
const bIndex = sorted.indexOf("B");
|
|
279
|
+
const cIndex = sorted.indexOf("C");
|
|
280
|
+
const dIndex = sorted.indexOf("D");
|
|
281
|
+
assert.ok(aIndex < bIndex, "A before B");
|
|
282
|
+
assert.ok(aIndex < cIndex, "A before C");
|
|
283
|
+
assert.ok(bIndex < dIndex, "B before D");
|
|
284
|
+
assert.ok(cIndex < dIndex, "C before D");
|
|
285
|
+
});
|
|
286
|
+
it("引用不存在的 subgoal 不影响排序", () => {
|
|
287
|
+
const goal = makeGoalStatus([
|
|
288
|
+
makeSubgoal({ id: "A", depends_on: ["nonexistent"] }),
|
|
289
|
+
makeSubgoal({ id: "B", depends_on: ["A"] }),
|
|
290
|
+
]);
|
|
291
|
+
const sorted = topologicalSort(goal);
|
|
292
|
+
assert.equal(sorted.length, 2);
|
|
293
|
+
const aIndex = sorted.indexOf("A");
|
|
294
|
+
const bIndex = sorted.indexOf("B");
|
|
295
|
+
assert.ok(aIndex < bIndex, "A before B");
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|