patchwarden 1.1.0 → 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 +41 -7
- package/README.md +36 -9
- package/dist/controlCenter.js +197 -1
- package/dist/direct/directSessionStore.d.ts +2 -0
- package/dist/direct/directVerification.js +7 -0
- package/dist/doctor.js +1 -1
- package/dist/policy/projectPolicy.d.ts +55 -0
- package/dist/policy/projectPolicy.js +286 -0
- package/dist/smoke-test.js +8 -8
- 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/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/run-task-loop.test.d.ts +1 -0
- package/dist/test/unit/run-task-loop.test.js +380 -0
- package/dist/test/unit/schema-drift-check.test.js +10 -9
- package/dist/tools/evidencePack.d.ts +39 -0
- package/dist/tools/evidencePack.js +168 -0
- package/dist/tools/recommendAgentForTask.d.ts +19 -0
- package/dist/tools/recommendAgentForTask.js +56 -0
- package/dist/tools/registry.js +376 -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/safeViews.d.ts +6 -0
- package/dist/tools/safeViews.js +2 -0
- package/dist/tools/taskLineage.d.ts +91 -0
- package/dist/tools/taskLineage.js +175 -0
- package/dist/tools/toolCatalog.d.ts +2 -2
- package/dist/tools/toolCatalog.js +6 -0
- package/dist/tools/toolRegistry.js +110 -0
- package/dist/version.d.ts +2 -2
- package/dist/version.js +2 -2
- package/docs/chatgpt-usage.md +31 -0
- package/docs/control-center/README.md +9 -0
- package/package.json +2 -2
- package/scripts/checks/control-center-smoke.js +87 -0
- package/scripts/checks/control-smoke.js +2 -2
- package/scripts/checks/mcp-manifest-check.js +12 -0
- package/scripts/checks/mcp-smoke.js +31 -7
- package/scripts/checks/watcher-supervisor-smoke.js +1 -1
- package/src/controlCenter.ts +198 -1
- package/src/direct/directSessionStore.ts +2 -0
- package/src/direct/directVerification.ts +7 -0
- package/src/doctor.ts +1 -1
- package/src/policy/projectPolicy.ts +344 -0
- package/src/smoke-test.ts +5 -5
- package/src/test/unit/evidence-pack.test.ts +142 -0
- package/src/test/unit/project-policy-release-mode.test.ts +156 -0
- package/src/test/unit/run-task-loop.test.ts +425 -0
- package/src/test/unit/schema-drift-check.test.ts +11 -9
- package/src/tools/evidencePack.ts +205 -0
- package/src/tools/listWorkspace.ts +71 -71
- package/src/tools/recommendAgentForTask.ts +79 -0
- package/src/tools/registry.ts +405 -2
- 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/safeViews.ts +2 -0
- package/src/tools/taskLineage.ts +300 -0
- package/src/tools/toolCatalog.ts +6 -0
- package/src/tools/toolRegistry.ts +110 -0
- package/src/version.ts +2 -2
- package/ui/pages/dashboard.html +143 -2
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
2
|
+
import { getConfig } from "../config.js";
|
|
3
|
+
import { createWorktree } from "../goal/worktreeManager.js";
|
|
4
|
+
import { guardWorkspacePath } from "../security/pathGuard.js";
|
|
5
|
+
import { createDirectSession } from "./createDirectSession.js";
|
|
6
|
+
import { createTask, type CreateTaskInput } from "./createTask.js";
|
|
7
|
+
import { recommendAgentForTask } from "./recommendAgentForTask.js";
|
|
8
|
+
import { runDirectVerificationBundle } from "./runDirectVerificationBundle.js";
|
|
9
|
+
import { waitForTask } from "./waitForTask.js";
|
|
10
|
+
import { safeAudit, safeAuditDirectSession, safeFinalizeDirectSession, safeResult, safeTestSummary } from "./safeViews.js";
|
|
11
|
+
import type { TaskTemplateName } from "./taskTemplates.js";
|
|
12
|
+
import {
|
|
13
|
+
createLineageId,
|
|
14
|
+
writeTaskLineage,
|
|
15
|
+
type SafeTaskLineage,
|
|
16
|
+
type TaskLineageDirectSession,
|
|
17
|
+
type TaskLineageRecord,
|
|
18
|
+
type TaskLineageRound,
|
|
19
|
+
type TaskLineageWorktree,
|
|
20
|
+
type TaskLoopStopReason,
|
|
21
|
+
} from "./taskLineage.js";
|
|
22
|
+
|
|
23
|
+
export interface RunTaskLoopInput {
|
|
24
|
+
repo_path: string;
|
|
25
|
+
goal: string;
|
|
26
|
+
verify_commands: string[];
|
|
27
|
+
agent?: string;
|
|
28
|
+
template?: TaskTemplateName;
|
|
29
|
+
max_iterations?: number;
|
|
30
|
+
task_timeout_seconds?: number;
|
|
31
|
+
auto_fix_tests?: boolean;
|
|
32
|
+
auto_cleanup_artifacts?: boolean;
|
|
33
|
+
stop_on_high_risk?: boolean;
|
|
34
|
+
direct_verify?: boolean;
|
|
35
|
+
direct_verify_commands?: string[];
|
|
36
|
+
direct_verify_timeout_seconds?: number;
|
|
37
|
+
scope_files?: string[];
|
|
38
|
+
isolation_mode?: "current_repo" | "worktree";
|
|
39
|
+
worktree_base_branch?: string;
|
|
40
|
+
worktree_cleanup?: "keep" | "archive" | "delete_ignored_only";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface RunTaskLoopOutput extends SafeTaskLineage {
|
|
44
|
+
created_task_count: number;
|
|
45
|
+
auto_fix_tests: boolean;
|
|
46
|
+
auto_cleanup_artifacts: boolean;
|
|
47
|
+
direct_verify: boolean;
|
|
48
|
+
isolation_mode: "current_repo" | "worktree";
|
|
49
|
+
worktree: TaskLineageWorktree;
|
|
50
|
+
stopped_before_execution: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface RunTaskLoopDeps {
|
|
54
|
+
createTask: typeof createTask;
|
|
55
|
+
waitForTask: typeof waitForTask;
|
|
56
|
+
safeResult: typeof safeResult;
|
|
57
|
+
safeAudit: typeof safeAudit;
|
|
58
|
+
safeTestSummary: typeof safeTestSummary;
|
|
59
|
+
createDirectSession: typeof createDirectSession;
|
|
60
|
+
runDirectVerificationBundle: typeof runDirectVerificationBundle;
|
|
61
|
+
safeFinalizeDirectSession: typeof safeFinalizeDirectSession;
|
|
62
|
+
safeAuditDirectSession: typeof safeAuditDirectSession;
|
|
63
|
+
writeTaskLineage: typeof writeTaskLineage;
|
|
64
|
+
createLineageId: typeof createLineageId;
|
|
65
|
+
recommendAgentForTask: typeof recommendAgentForTask;
|
|
66
|
+
createWorktree: typeof createWorktree;
|
|
67
|
+
now: () => Date;
|
|
68
|
+
sleep: (ms: number) => Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const DEFAULT_DEPS: RunTaskLoopDeps = {
|
|
72
|
+
createTask,
|
|
73
|
+
waitForTask,
|
|
74
|
+
safeResult,
|
|
75
|
+
safeAudit,
|
|
76
|
+
safeTestSummary,
|
|
77
|
+
createDirectSession,
|
|
78
|
+
runDirectVerificationBundle,
|
|
79
|
+
safeFinalizeDirectSession,
|
|
80
|
+
safeAuditDirectSession,
|
|
81
|
+
writeTaskLineage,
|
|
82
|
+
createLineageId,
|
|
83
|
+
recommendAgentForTask,
|
|
84
|
+
createWorktree,
|
|
85
|
+
now: () => new Date(),
|
|
86
|
+
sleep,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const TERMINAL_STATUSES = new Set([
|
|
90
|
+
"done",
|
|
91
|
+
"done_by_agent",
|
|
92
|
+
"failed",
|
|
93
|
+
"failed_verification",
|
|
94
|
+
"failed_scope_violation",
|
|
95
|
+
"failed_policy_violation",
|
|
96
|
+
"canceled",
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
export async function runTaskLoop(input: RunTaskLoopInput): Promise<RunTaskLoopOutput> {
|
|
100
|
+
return runTaskLoopWithDeps(input, DEFAULT_DEPS);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function runTaskLoopWithDeps(
|
|
104
|
+
input: RunTaskLoopInput,
|
|
105
|
+
deps: RunTaskLoopDeps
|
|
106
|
+
): Promise<RunTaskLoopOutput> {
|
|
107
|
+
const normalized = normalizeInput(input);
|
|
108
|
+
const resolvedRepoPath = guardWorkspacePath(normalized.repo_path, getConfig().workspaceRoot);
|
|
109
|
+
const routing = resolveAgentRouting(normalized, deps);
|
|
110
|
+
const selectedAgent = routing.selected_agent;
|
|
111
|
+
const now = deps.now().toISOString();
|
|
112
|
+
const lineage: TaskLineageRecord = {
|
|
113
|
+
lineage_id: deps.createLineageId(deps.now()),
|
|
114
|
+
goal: normalized.goal,
|
|
115
|
+
repo_path: resolvedRepoPath,
|
|
116
|
+
created_at: now,
|
|
117
|
+
updated_at: now,
|
|
118
|
+
final_status: "blocked",
|
|
119
|
+
stop_reason: "policy_blocked",
|
|
120
|
+
next_action: "inspect_lineage",
|
|
121
|
+
main_task: null,
|
|
122
|
+
fix_tasks: [],
|
|
123
|
+
cleanup_tasks: [],
|
|
124
|
+
direct_sessions: [],
|
|
125
|
+
rounds: [],
|
|
126
|
+
warnings: [],
|
|
127
|
+
errors: [],
|
|
128
|
+
worktree: {
|
|
129
|
+
isolation_mode: normalized.isolation_mode,
|
|
130
|
+
cleanup: normalized.worktree_cleanup,
|
|
131
|
+
status: normalized.isolation_mode === "worktree" ? "active" : "not_used",
|
|
132
|
+
requested_base_branch: normalized.worktree_base_branch,
|
|
133
|
+
next_action: normalized.isolation_mode === "worktree"
|
|
134
|
+
? "Review and explicitly merge or discard the worktree after acceptance."
|
|
135
|
+
: "none",
|
|
136
|
+
},
|
|
137
|
+
agent_routing: routing,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const finalize = (
|
|
141
|
+
finalStatus: TaskLineageRecord["final_status"],
|
|
142
|
+
stopReason: TaskLoopStopReason,
|
|
143
|
+
nextAction: string,
|
|
144
|
+
error?: string
|
|
145
|
+
): RunTaskLoopOutput => {
|
|
146
|
+
lineage.final_status = finalStatus;
|
|
147
|
+
lineage.stop_reason = stopReason;
|
|
148
|
+
lineage.next_action = nextAction;
|
|
149
|
+
lineage.updated_at = deps.now().toISOString();
|
|
150
|
+
if (error) lineage.errors.push(error);
|
|
151
|
+
const safe = deps.writeTaskLineage(lineage);
|
|
152
|
+
return {
|
|
153
|
+
...safe,
|
|
154
|
+
created_task_count: [lineage.main_task, ...lineage.fix_tasks, ...lineage.cleanup_tasks].filter(Boolean).length,
|
|
155
|
+
auto_fix_tests: normalized.auto_fix_tests,
|
|
156
|
+
auto_cleanup_artifacts: normalized.auto_cleanup_artifacts,
|
|
157
|
+
direct_verify: normalized.direct_verify,
|
|
158
|
+
isolation_mode: normalized.isolation_mode,
|
|
159
|
+
worktree: safe.worktree,
|
|
160
|
+
stopped_before_execution: lineage.main_task === null,
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
let taskRepoPath = resolvedRepoPath;
|
|
165
|
+
if (normalized.isolation_mode === "worktree") {
|
|
166
|
+
try {
|
|
167
|
+
const worktree = deps.createWorktree(lineage.lineage_id, "task_loop", resolvedRepoPath);
|
|
168
|
+
taskRepoPath = worktree.worktreePath;
|
|
169
|
+
lineage.repo_path = taskRepoPath;
|
|
170
|
+
lineage.worktree = {
|
|
171
|
+
isolation_mode: "worktree",
|
|
172
|
+
worktree_id: worktree.worktreeId,
|
|
173
|
+
worktree_path: worktree.worktreePath,
|
|
174
|
+
branch: worktree.branch,
|
|
175
|
+
requested_base_branch: normalized.worktree_base_branch,
|
|
176
|
+
cleanup: normalized.worktree_cleanup,
|
|
177
|
+
status: "active",
|
|
178
|
+
next_action: "Explicitly inspect and merge_worktree or discard_worktree after reviewing this lineage.",
|
|
179
|
+
};
|
|
180
|
+
} catch (err) {
|
|
181
|
+
lineage.worktree = {
|
|
182
|
+
isolation_mode: "worktree",
|
|
183
|
+
cleanup: normalized.worktree_cleanup,
|
|
184
|
+
requested_base_branch: normalized.worktree_base_branch,
|
|
185
|
+
status: "failed",
|
|
186
|
+
next_action: "Fix worktree creation prerequisites or rerun with isolation_mode=current_repo.",
|
|
187
|
+
};
|
|
188
|
+
return finalize("blocked", "policy_blocked", "Fix worktree creation prerequisites or rerun without worktree isolation.", err instanceof Error ? err.message : String(err));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let role: "main" | "fix_tests" = "main";
|
|
193
|
+
let latestFailurePrompt = normalized.goal;
|
|
194
|
+
|
|
195
|
+
for (let iteration = 1; iteration <= normalized.max_iterations; iteration++) {
|
|
196
|
+
const assessmentInput: CreateTaskInput = {
|
|
197
|
+
template: role === "main" ? normalized.template : "fix_tests",
|
|
198
|
+
goal: role === "main" ? normalized.goal : latestFailurePrompt,
|
|
199
|
+
repo_path: taskRepoPath,
|
|
200
|
+
agent: selectedAgent,
|
|
201
|
+
verify_commands: normalized.verify_commands,
|
|
202
|
+
timeout_seconds: normalized.task_timeout_seconds,
|
|
203
|
+
execution_mode: "assess_only",
|
|
204
|
+
};
|
|
205
|
+
const assessment = deps.createTask(assessmentInput as CreateTaskInput & { execution_mode: "assess_only" }) as any;
|
|
206
|
+
if (assessment.decision === "blocked") {
|
|
207
|
+
return finalize("blocked", "high_risk_blocked", "Risk assessment blocked task execution.", assessment.reason_codes?.join(", "));
|
|
208
|
+
}
|
|
209
|
+
if (assessment.decision === "needs_confirm") {
|
|
210
|
+
return finalize("blocked", "user_confirmation_required", "Ask the user to confirm the assessment before executing the loop.");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const created = deps.createTask({
|
|
214
|
+
execution_mode: "execute",
|
|
215
|
+
assessment_id: String(assessment.assessment_id || ""),
|
|
216
|
+
}) as any;
|
|
217
|
+
const taskId = String(created.task_id || "");
|
|
218
|
+
if (!taskId) {
|
|
219
|
+
return finalize("failed", "policy_blocked", "create_task returned no task_id.", "create_task returned no task_id");
|
|
220
|
+
}
|
|
221
|
+
if (role === "main") lineage.main_task = taskId;
|
|
222
|
+
else lineage.fix_tasks.push(taskId);
|
|
223
|
+
|
|
224
|
+
const wait = await waitUntilTerminal(taskId, normalized.task_timeout_seconds, deps);
|
|
225
|
+
if (wait.stop_reason) {
|
|
226
|
+
return finalize("blocked", wait.stop_reason, wait.next_action, wait.error);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const result = deps.safeResult(taskId, { max_items: 8 }) as any;
|
|
230
|
+
const tests = deps.safeTestSummary(taskId) as any;
|
|
231
|
+
const audit = deps.safeAudit(taskId, { max_items: 8 }) as any;
|
|
232
|
+
const round = buildRound(iteration, taskId, role, result, tests, audit);
|
|
233
|
+
lineage.rounds.push(round);
|
|
234
|
+
lineage.updated_at = deps.now().toISOString();
|
|
235
|
+
|
|
236
|
+
if (isSuccessfulRound(round)) {
|
|
237
|
+
if (normalized.direct_verify) {
|
|
238
|
+
const direct = await runDirectVerification(lineage.lineage_id, normalized, taskRepoPath, deps);
|
|
239
|
+
lineage.direct_sessions.push(direct.evidence);
|
|
240
|
+
if (direct.warning) lineage.warnings.push(direct.warning);
|
|
241
|
+
if (direct.stop_reason) {
|
|
242
|
+
return finalize(direct.final_status, direct.stop_reason, direct.next_action, direct.error);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return finalize("accepted", "success", "accept");
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (isHardStop(round, result, audit, normalized.stop_on_high_risk)) {
|
|
249
|
+
return finalize("blocked", hardStopReason(round, result), round.next_action || "review_task");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
latestFailurePrompt = buildFixGoal(normalized.goal, result, round);
|
|
253
|
+
if (!normalized.auto_fix_tests || round.status !== "failed_verification") {
|
|
254
|
+
return finalize("needs_fix", "verification_failed", round.next_action || "create_followup_task");
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
role = "fix_tests";
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return finalize("needs_fix", "max_iterations_reached", "review_lineage_and_create_manual_followup");
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function waitUntilTerminal(
|
|
264
|
+
taskId: string,
|
|
265
|
+
timeoutSeconds: number,
|
|
266
|
+
deps: RunTaskLoopDeps
|
|
267
|
+
): Promise<{ stop_reason?: TaskLoopStopReason; next_action: string; error?: string }> {
|
|
268
|
+
const deadline = Date.now() + timeoutSeconds * 1000;
|
|
269
|
+
while (Date.now() < deadline) {
|
|
270
|
+
const waited = await deps.waitForTask(taskId, 30) as any;
|
|
271
|
+
if (waited.terminal || TERMINAL_STATUSES.has(String(waited.status))) {
|
|
272
|
+
return { next_action: waited.next_action || "safe_audit" };
|
|
273
|
+
}
|
|
274
|
+
if (waited.next_tool_call?.name === "health_check" || waited.continuation_required === false) {
|
|
275
|
+
return {
|
|
276
|
+
stop_reason: "watcher_blocked",
|
|
277
|
+
next_action: waited.next_action || "health_check",
|
|
278
|
+
error: waited.progress_summary?.hint || "Watcher is blocked or unavailable.",
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
await deps.sleep(250);
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
stop_reason: "agent_timeout",
|
|
285
|
+
next_action: "inspect_task_status",
|
|
286
|
+
error: `Task loop timed out waiting for ${taskId}.`,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function buildRound(
|
|
291
|
+
iteration: number,
|
|
292
|
+
taskId: string,
|
|
293
|
+
role: TaskLineageRound["role"],
|
|
294
|
+
result: any,
|
|
295
|
+
tests: any,
|
|
296
|
+
audit: any
|
|
297
|
+
): TaskLineageRound {
|
|
298
|
+
const failChecks = Array.isArray(audit.fail_checks) ? audit.fail_checks.map((entry: any) => String(entry.name || entry)) : [];
|
|
299
|
+
const warnChecks = Array.isArray(audit.warn_checks) ? audit.warn_checks.map((entry: any) => String(entry.name || entry)) : [];
|
|
300
|
+
return {
|
|
301
|
+
iteration,
|
|
302
|
+
task_id: taskId,
|
|
303
|
+
role,
|
|
304
|
+
status: String(result.status || "unknown"),
|
|
305
|
+
terminal: Boolean(result.terminal),
|
|
306
|
+
verification_status: String(tests.status || result.verification?.status || "not_available"),
|
|
307
|
+
audit_verdict: String(audit.verdict || audit.acceptance?.verdict || "unknown"),
|
|
308
|
+
fail_checks: failChecks,
|
|
309
|
+
warn_checks: warnChecks,
|
|
310
|
+
next_action: String(result.next_action || audit.recommended_next_actions?.[0] || "review_task"),
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function isSuccessfulRound(round: TaskLineageRound): boolean {
|
|
315
|
+
return (
|
|
316
|
+
round.terminal &&
|
|
317
|
+
["done_by_agent", "done", "accepted"].includes(round.status) &&
|
|
318
|
+
round.verification_status === "passed" &&
|
|
319
|
+
round.fail_checks.length === 0 &&
|
|
320
|
+
round.warn_checks.length === 0 &&
|
|
321
|
+
round.audit_verdict === "pass"
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function isHardStop(round: TaskLineageRound, result: any, audit: any, stopOnHighRisk: boolean): boolean {
|
|
326
|
+
if (["failed_scope_violation", "failed_policy_violation", "canceled"].includes(round.status)) return true;
|
|
327
|
+
if (!stopOnHighRisk) return false;
|
|
328
|
+
const checkNames = [...round.fail_checks, ...round.warn_checks].join(" ").toLowerCase();
|
|
329
|
+
const reason = String(result.failure_reason || audit.acceptance?.reason || "").toLowerCase();
|
|
330
|
+
return /scope|secret|sensitive|publish|release|policy|push/.test(`${checkNames} ${reason}`);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function hardStopReason(round: TaskLineageRound, result: any): TaskLoopStopReason {
|
|
334
|
+
if (round.status === "failed_scope_violation" || round.status === "failed_policy_violation") return "policy_blocked";
|
|
335
|
+
if (String(result.failure_reason || "").toLowerCase().includes("timeout")) return "agent_timeout";
|
|
336
|
+
return "high_risk_blocked";
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function buildFixGoal(originalGoal: string, result: any, round: TaskLineageRound): string {
|
|
340
|
+
const failedCommand = result.failed_command ? ` Failed command: ${result.failed_command}.` : "";
|
|
341
|
+
return [
|
|
342
|
+
`Fix the failing verification for this PatchWarden loop without changing unrelated behavior.`,
|
|
343
|
+
`Original goal: ${originalGoal}`,
|
|
344
|
+
`Previous task: ${round.task_id}. Status: ${round.status}. Verification: ${round.verification_status}.${failedCommand}`,
|
|
345
|
+
"Do not commit, push, publish, weaken tests, or touch files outside the resolved repository path.",
|
|
346
|
+
].join("\n");
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function resolveAgentRouting(
|
|
350
|
+
normalized: ReturnType<typeof normalizeInput>,
|
|
351
|
+
deps: RunTaskLoopDeps
|
|
352
|
+
) {
|
|
353
|
+
if (normalized.agent && normalized.agent !== "auto") {
|
|
354
|
+
return {
|
|
355
|
+
requested_agent: normalized.agent,
|
|
356
|
+
selected_agent: normalized.agent,
|
|
357
|
+
reason: "explicit agent supplied",
|
|
358
|
+
fallback: false,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
const recommendation = deps.recommendAgentForTask({
|
|
362
|
+
repo_path: normalized.repo_path,
|
|
363
|
+
goal: normalized.goal,
|
|
364
|
+
scope_files: normalized.scope_files,
|
|
365
|
+
template: normalized.template,
|
|
366
|
+
});
|
|
367
|
+
return {
|
|
368
|
+
requested_agent: normalized.agent || null,
|
|
369
|
+
selected_agent: recommendation.recommended_agent,
|
|
370
|
+
reason: recommendation.reason,
|
|
371
|
+
fallback: recommendation.fallback,
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
async function runDirectVerification(
|
|
376
|
+
lineageId: string,
|
|
377
|
+
normalized: ReturnType<typeof normalizeInput>,
|
|
378
|
+
repoPath: string,
|
|
379
|
+
deps: RunTaskLoopDeps
|
|
380
|
+
): Promise<{
|
|
381
|
+
evidence: TaskLineageDirectSession;
|
|
382
|
+
stop_reason?: TaskLoopStopReason;
|
|
383
|
+
final_status: TaskLineageRecord["final_status"];
|
|
384
|
+
next_action: string;
|
|
385
|
+
error?: string;
|
|
386
|
+
warning?: string;
|
|
387
|
+
}> {
|
|
388
|
+
const config = getConfig();
|
|
389
|
+
if (config.enableDirectProfile !== true) {
|
|
390
|
+
return {
|
|
391
|
+
evidence: {
|
|
392
|
+
session_id: "not_created",
|
|
393
|
+
status: "skipped",
|
|
394
|
+
audit_decision: "not_run",
|
|
395
|
+
next_action: "Enable Direct profile locally before requesting direct_verify.",
|
|
396
|
+
},
|
|
397
|
+
stop_reason: "direct_profile_disabled",
|
|
398
|
+
final_status: "blocked",
|
|
399
|
+
next_action: "Enable enableDirectProfile locally or rerun run_task_loop with direct_verify=false.",
|
|
400
|
+
error: "Direct profile is disabled by local config.",
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
let sessionId = "";
|
|
405
|
+
try {
|
|
406
|
+
const session = deps.createDirectSession({
|
|
407
|
+
repo_path: repoPath,
|
|
408
|
+
title: `Direct verification for ${lineageId}`,
|
|
409
|
+
});
|
|
410
|
+
sessionId = session.session_id;
|
|
411
|
+
const bundle = await deps.runDirectVerificationBundle({
|
|
412
|
+
session_id: sessionId,
|
|
413
|
+
commands: normalized.direct_verify_commands,
|
|
414
|
+
timeout_seconds: normalized.direct_verify_timeout_seconds,
|
|
415
|
+
});
|
|
416
|
+
const finalized = deps.safeFinalizeDirectSession(sessionId, { max_items: 8 }) as any;
|
|
417
|
+
const audit = deps.safeAuditDirectSession(sessionId, { max_items: 8 }) as any;
|
|
418
|
+
const evidence: TaskLineageDirectSession = {
|
|
419
|
+
session_id: sessionId,
|
|
420
|
+
status: bundle.status,
|
|
421
|
+
command_count: bundle.command_count,
|
|
422
|
+
passed_commands: bundle.passed_commands,
|
|
423
|
+
failed_commands: bundle.failed_commands,
|
|
424
|
+
timed_out_commands: bundle.timed_out_commands,
|
|
425
|
+
audit_decision: audit.decision || "not_run",
|
|
426
|
+
changed_files_total: Number(finalized.changed_files_total || audit.evidence?.changed_files_total || 0),
|
|
427
|
+
next_action: String(audit.next_action || bundle.next_action || "review_direct_session"),
|
|
428
|
+
};
|
|
429
|
+
if (bundle.status !== "passed") {
|
|
430
|
+
return {
|
|
431
|
+
evidence,
|
|
432
|
+
stop_reason: "direct_verification_failed",
|
|
433
|
+
final_status: "needs_fix",
|
|
434
|
+
next_action: "Review Direct verification summary and create a normal follow-up task.",
|
|
435
|
+
error: "Direct verification failed.",
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
if (audit.decision === "fail") {
|
|
439
|
+
return {
|
|
440
|
+
evidence,
|
|
441
|
+
stop_reason: "direct_audit_failed",
|
|
442
|
+
final_status: "blocked",
|
|
443
|
+
next_action: "Review Direct audit findings before accepting the loop.",
|
|
444
|
+
error: "Direct audit failed.",
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
evidence,
|
|
449
|
+
final_status: "accepted",
|
|
450
|
+
next_action: "accept",
|
|
451
|
+
warning: audit.decision === "warn" ? "Direct audit completed with warnings." : undefined,
|
|
452
|
+
};
|
|
453
|
+
} catch (err) {
|
|
454
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
455
|
+
return {
|
|
456
|
+
evidence: {
|
|
457
|
+
session_id: sessionId || "not_created",
|
|
458
|
+
status: "failed",
|
|
459
|
+
audit_decision: "not_run",
|
|
460
|
+
next_action: "Review Direct verification configuration and command allow-list.",
|
|
461
|
+
},
|
|
462
|
+
stop_reason: "direct_verification_failed",
|
|
463
|
+
final_status: "needs_fix",
|
|
464
|
+
next_action: "Review Direct verification configuration and command allow-list.",
|
|
465
|
+
error: message,
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function normalizeInput(input: RunTaskLoopInput): Required<Omit<RunTaskLoopInput, "agent" | "scope_files" | "worktree_base_branch">> & { agent?: string; scope_files?: string[]; worktree_base_branch?: string } {
|
|
471
|
+
const config = getConfig();
|
|
472
|
+
const repoPath = String(input.repo_path || "").trim();
|
|
473
|
+
const goal = String(input.goal || "").trim();
|
|
474
|
+
if (!repoPath) throw new Error("repo_path is required.");
|
|
475
|
+
if (!goal) throw new Error("goal is required.");
|
|
476
|
+
if (!Array.isArray(input.verify_commands) || input.verify_commands.length === 0) {
|
|
477
|
+
throw new Error("verify_commands must contain at least one command.");
|
|
478
|
+
}
|
|
479
|
+
const maxIterations = input.max_iterations ?? 3;
|
|
480
|
+
if (!Number.isInteger(maxIterations) || maxIterations < 1 || maxIterations > 5) {
|
|
481
|
+
throw new Error("max_iterations must be an integer from 1 to 5.");
|
|
482
|
+
}
|
|
483
|
+
const timeoutSeconds = input.task_timeout_seconds ?? config.defaultTaskTimeoutSeconds;
|
|
484
|
+
if (!Number.isInteger(timeoutSeconds) || timeoutSeconds < 1 || timeoutSeconds > config.maxTaskTimeoutSeconds) {
|
|
485
|
+
throw new Error(`task_timeout_seconds must be an integer from 1 to ${config.maxTaskTimeoutSeconds}.`);
|
|
486
|
+
}
|
|
487
|
+
const template = input.template || "feature_small";
|
|
488
|
+
if (template !== "inspect_only" && template !== "feature_small" && template !== "release_check") {
|
|
489
|
+
throw new Error('template must be "inspect_only", "feature_small", or "release_check".');
|
|
490
|
+
}
|
|
491
|
+
const directVerifyCommands = Array.isArray(input.direct_verify_commands) && input.direct_verify_commands.length > 0
|
|
492
|
+
? input.direct_verify_commands
|
|
493
|
+
: input.verify_commands;
|
|
494
|
+
if (input.direct_verify === true && (!Array.isArray(directVerifyCommands) || directVerifyCommands.length === 0)) {
|
|
495
|
+
throw new Error("direct_verify_commands must contain at least one command when provided.");
|
|
496
|
+
}
|
|
497
|
+
const directVerifyTimeout = input.direct_verify_timeout_seconds ?? 120;
|
|
498
|
+
const maxDirectTimeout = Math.min(config.maxTaskTimeoutSeconds, config.directSessionTtlSeconds);
|
|
499
|
+
if (!Number.isInteger(directVerifyTimeout) || directVerifyTimeout < 1 || directVerifyTimeout > maxDirectTimeout) {
|
|
500
|
+
throw new Error(`direct_verify_timeout_seconds must be an integer from 1 to ${maxDirectTimeout}.`);
|
|
501
|
+
}
|
|
502
|
+
return {
|
|
503
|
+
repo_path: repoPath,
|
|
504
|
+
goal,
|
|
505
|
+
verify_commands: input.verify_commands.map((command) => String(command).trim()),
|
|
506
|
+
agent: input.agent ? String(input.agent) : undefined,
|
|
507
|
+
template,
|
|
508
|
+
max_iterations: maxIterations,
|
|
509
|
+
task_timeout_seconds: timeoutSeconds,
|
|
510
|
+
auto_fix_tests: input.auto_fix_tests !== false,
|
|
511
|
+
auto_cleanup_artifacts: input.auto_cleanup_artifacts !== false,
|
|
512
|
+
stop_on_high_risk: input.stop_on_high_risk !== false,
|
|
513
|
+
direct_verify: input.direct_verify === true,
|
|
514
|
+
direct_verify_commands: directVerifyCommands.map((command) => String(command).trim()),
|
|
515
|
+
direct_verify_timeout_seconds: directVerifyTimeout,
|
|
516
|
+
scope_files: Array.isArray(input.scope_files)
|
|
517
|
+
? input.scope_files.map((entry) => String(entry).trim()).filter(Boolean).slice(0, 50)
|
|
518
|
+
: undefined,
|
|
519
|
+
isolation_mode: input.isolation_mode === "worktree" ? "worktree" : "current_repo",
|
|
520
|
+
worktree_base_branch: input.worktree_base_branch ? String(input.worktree_base_branch).trim().slice(0, 160) : undefined,
|
|
521
|
+
worktree_cleanup:
|
|
522
|
+
input.worktree_cleanup === "archive" || input.worktree_cleanup === "delete_ignored_only"
|
|
523
|
+
? input.worktree_cleanup
|
|
524
|
+
: "keep",
|
|
525
|
+
};
|
|
526
|
+
}
|
|
@@ -16,6 +16,10 @@ export interface RunVerificationOutput {
|
|
|
16
16
|
exit_code: number | null;
|
|
17
17
|
passed: boolean;
|
|
18
18
|
timed_out: boolean;
|
|
19
|
+
redacted: boolean;
|
|
20
|
+
redaction_categories: string[];
|
|
21
|
+
started_at: string;
|
|
22
|
+
finished_at: string;
|
|
19
23
|
stdout_tail: string;
|
|
20
24
|
stderr_tail: string;
|
|
21
25
|
log_path: string;
|
|
@@ -48,6 +52,10 @@ export async function runVerification(
|
|
|
48
52
|
exit_code: result.run.exit_code,
|
|
49
53
|
passed: result.run.passed,
|
|
50
54
|
timed_out: result.run.timed_out,
|
|
55
|
+
redacted: Boolean(result.run.redacted),
|
|
56
|
+
redaction_categories: result.run.redaction_categories || [],
|
|
57
|
+
started_at: result.run.started_at,
|
|
58
|
+
finished_at: result.run.finished_at,
|
|
51
59
|
stdout_tail: result.run.stdout_tail,
|
|
52
60
|
stderr_tail: result.run.stderr_tail,
|
|
53
61
|
log_path: result.run.log_path,
|
package/src/tools/safeViews.ts
CHANGED
|
@@ -208,6 +208,8 @@ function summarizeVerificationRuns(runs: DirectSessionVerificationRun[]) {
|
|
|
208
208
|
exit_code: run.exit_code,
|
|
209
209
|
passed: run.passed,
|
|
210
210
|
timed_out: run.timed_out,
|
|
211
|
+
redacted: Boolean(run.redacted),
|
|
212
|
+
redaction_categories: run.redaction_categories || [],
|
|
211
213
|
started_at: run.started_at,
|
|
212
214
|
finished_at: run.finished_at,
|
|
213
215
|
}));
|