patchwarden 0.6.0 → 0.6.4
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/PatchWarden-Control-Tray.cmd +11 -0
- package/PatchWarden-Control.cmd +6 -0
- package/PatchWarden-Desktop.cmd +5 -0
- package/PatchWarden.cmd +1 -1
- package/README.en.md +106 -18
- package/README.md +30 -19
- package/Restart-PatchWarden-Control.cmd +6 -0
- package/Stop-PatchWarden.cmd +11 -0
- package/dist/controlCenter.d.ts +14 -0
- package/dist/controlCenter.js +2002 -0
- package/dist/doctor.js +4 -4
- package/dist/logging.d.ts +52 -0
- package/dist/logging.js +123 -0
- package/dist/runner/changeCapture.d.ts +41 -0
- package/dist/runner/changeCapture.js +171 -1
- package/dist/runner/runTask.js +204 -24
- package/dist/smoke-test.js +8 -8
- package/dist/test/unit/android-doctor.test.d.ts +1 -0
- package/dist/test/unit/android-doctor.test.js +118 -0
- package/dist/test/unit/chinese-path.test.d.ts +1 -0
- package/dist/test/unit/chinese-path.test.js +91 -0
- package/dist/test/unit/command-guard.test.d.ts +1 -0
- package/dist/test/unit/command-guard.test.js +160 -0
- package/dist/test/unit/direct-guards.test.d.ts +1 -0
- package/dist/test/unit/direct-guards.test.js +213 -0
- package/dist/test/unit/logging.test.d.ts +1 -0
- package/dist/test/unit/logging.test.js +275 -0
- package/dist/test/unit/path-guard.test.d.ts +1 -0
- package/dist/test/unit/path-guard.test.js +109 -0
- package/dist/test/unit/safe-status.test.d.ts +1 -0
- package/dist/test/unit/safe-status.test.js +165 -0
- package/dist/test/unit/sensitive-guard.test.d.ts +1 -0
- package/dist/test/unit/sensitive-guard.test.js +104 -0
- package/dist/test/unit/sync-file.test.d.ts +1 -0
- package/dist/test/unit/sync-file.test.js +154 -0
- package/dist/test/unit/watcher-status.test.d.ts +1 -0
- package/dist/test/unit/watcher-status.test.js +169 -0
- package/dist/tools/androidDoctor.d.ts +38 -0
- package/dist/tools/androidDoctor.js +391 -0
- package/dist/tools/auditTask.js +11 -5
- package/dist/tools/getTaskSummary.d.ts +3 -0
- package/dist/tools/getTaskSummary.js +15 -1
- package/dist/tools/healthCheck.d.ts +5 -0
- package/dist/tools/healthCheck.js +21 -0
- package/dist/tools/registry.js +53 -0
- package/dist/tools/safeStatus.d.ts +19 -0
- package/dist/tools/safeStatus.js +72 -0
- package/dist/tools/syncFile.d.ts +18 -0
- package/dist/tools/syncFile.js +65 -0
- package/dist/tools/taskOutputs.d.ts +2 -2
- package/dist/tools/toolCatalog.d.ts +2 -2
- package/dist/tools/toolCatalog.js +2 -0
- package/dist/version.d.ts +2 -2
- package/dist/version.js +2 -2
- package/dist/watcherStatus.d.ts +1 -0
- package/dist/watcherStatus.js +96 -4
- package/docs/control-center/README.md +33 -0
- package/docs/control-center/control-center-daily-driver.md +211 -0
- package/docs/control-center/control-center-mvp.md +205 -0
- package/docs/control-center/control-center-phase2.md +159 -0
- package/docs/demo.md +3 -0
- package/docs/performance-notes.md +55 -0
- package/docs/release-v0.6.1.md +75 -0
- package/docs/release-v0.6.4.md +45 -0
- package/examples/openai-tunnel/README.md +5 -5
- package/examples/openai-tunnel/tunnel-client.example.yaml +3 -3
- package/package.json +23 -14
- package/scripts/README.md +47 -0
- package/scripts/{brand-check.js → checks/brand-check.js} +2 -2
- package/scripts/checks/control-center-smoke.js +1098 -0
- package/scripts/{control-smoke.js → checks/control-smoke.js} +17 -3
- package/scripts/{doctor-smoke.js → checks/doctor-smoke.js} +1 -1
- package/scripts/{http-mcp-smoke.js → checks/http-mcp-smoke.js} +12 -4
- package/scripts/{lifecycle-smoke.js → checks/lifecycle-smoke.js} +347 -13
- package/scripts/{mcp-manifest-check.js → checks/mcp-manifest-check.js} +32 -9
- package/scripts/{mcp-smoke.js → checks/mcp-smoke.js} +14 -9
- package/scripts/{package-manifest-check.js → checks/package-manifest-check.js} +2 -1
- package/scripts/{tunnel-supervisor-smoke.js → checks/tunnel-supervisor-smoke.js} +2 -2
- package/scripts/checks/unit-tests.js +36 -0
- package/scripts/{watcher-supervisor-smoke.js → checks/watcher-supervisor-smoke.js} +11 -5
- package/scripts/control/control-center-tray.ps1 +281 -0
- package/scripts/{get-patchwarden-health.ps1 → control/get-patchwarden-health.ps1} +3 -3
- package/scripts/{manage-patchwarden.ps1 → control/manage-patchwarden.ps1} +30 -4
- package/scripts/control/restart-control-center.ps1 +173 -0
- package/scripts/{restart-patchwarden.ps1 → control/restart-patchwarden.ps1} +1 -1
- package/scripts/control/start-control-center.ps1 +263 -0
- package/scripts/{start-patchwarden-tunnel.ps1 → control/start-patchwarden-tunnel.ps1} +48 -6
- package/scripts/control/stop-patchwarden.ps1 +114 -0
- package/scripts/launchers/Check-PatchWarden-Health.cmd +1 -1
- package/scripts/launchers/Reset-PatchWarden-Tunnel-Key.cmd +1 -1
- package/scripts/launchers/Restart-PatchWarden.cmd +1 -1
- package/scripts/launchers/Start-PatchWarden-Direct-Tunnel.cmd +1 -1
- package/scripts/launchers/Start-PatchWarden-Tunnel.cmd +1 -1
- package/scripts/{patchwarden-mcp-direct.cmd → mcp/patchwarden-mcp-direct.cmd} +1 -1
- package/scripts/{patchwarden-mcp-stdio.cmd → mcp/patchwarden-mcp-stdio.cmd} +1 -1
- package/scripts/release/pack-clean.js +305 -0
- package/src/controlCenter.ts +2152 -0
- package/src/doctor.ts +5 -5
- package/src/logging.ts +152 -0
- package/src/runner/changeCapture.ts +212 -1
- package/src/runner/runTask.ts +220 -22
- package/src/smoke-test.ts +5 -5
- package/src/test/unit/android-doctor.test.ts +158 -0
- package/src/test/unit/chinese-path.test.ts +106 -0
- package/src/test/unit/command-guard.test.ts +221 -0
- package/src/test/unit/direct-guards.test.ts +297 -0
- package/src/test/unit/logging.test.ts +325 -0
- package/src/test/unit/path-guard.test.ts +150 -0
- package/src/test/unit/safe-status.test.ts +187 -0
- package/src/test/unit/sensitive-guard.test.ts +124 -0
- package/src/test/unit/sync-file.test.ts +231 -0
- package/src/test/unit/watcher-status.test.ts +190 -0
- package/src/tools/androidDoctor.ts +424 -0
- package/src/tools/auditTask.ts +11 -5
- package/src/tools/getTaskSummary.ts +22 -1
- package/src/tools/healthCheck.ts +22 -0
- package/src/tools/registry.ts +63 -0
- package/src/tools/safeStatus.ts +96 -0
- package/src/tools/syncFile.ts +122 -0
- package/src/tools/toolCatalog.ts +2 -0
- package/src/version.ts +2 -2
- package/src/watcherStatus.ts +101 -4
- package/ui/colors_and_type.css +141 -0
- package/ui/pages/audit.html +743 -0
- package/ui/pages/dashboard.html +1154 -0
- package/ui/pages/direct-sessions.html +652 -0
- package/ui/pages/logs.html +502 -0
- package/ui/pages/task-detail.html +1229 -0
- package/ui/pages/tasks.html +702 -0
- package/ui/pages/workspace.html +947 -0
- package/ui/partials/project-shell.html +362 -0
- package/ui/vendor/lucide.js +12 -0
- package/ui/vendor/tailwindcss-browser.js +947 -0
- package/scripts/pack-clean.js +0 -141
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
2
|
+
import { strict as assert } from "node:assert";
|
|
3
|
+
import { Logger, logUnhandledError, installGlobalHandlers } from "../../logging.js";
|
|
4
|
+
|
|
5
|
+
// ── Test helpers ──────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Capture everything written to stderr during `fn` and return it as an
|
|
9
|
+
* array of raw chunks (one per `process.stderr.write` call).
|
|
10
|
+
*/
|
|
11
|
+
function captureStderr(fn: () => void): string[] {
|
|
12
|
+
const chunks: string[] = [];
|
|
13
|
+
const original = process.stderr.write;
|
|
14
|
+
process.stderr.write = ((chunk: string | Uint8Array) => {
|
|
15
|
+
chunks.push(typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf-8"));
|
|
16
|
+
return true;
|
|
17
|
+
}) as typeof process.stderr.write;
|
|
18
|
+
try {
|
|
19
|
+
fn();
|
|
20
|
+
} finally {
|
|
21
|
+
process.stderr.write = original;
|
|
22
|
+
}
|
|
23
|
+
return chunks;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Capture stderr and return the parsed JSON objects (one per log line).
|
|
28
|
+
*/
|
|
29
|
+
function captureJson(fn: () => void): Record<string, unknown>[] {
|
|
30
|
+
const chunks = captureStderr(fn);
|
|
31
|
+
return chunks
|
|
32
|
+
.flatMap((chunk) => chunk.split("\n"))
|
|
33
|
+
.map((line) => line.trim())
|
|
34
|
+
.filter((line) => line.length > 0)
|
|
35
|
+
.map((line) => JSON.parse(line));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ── Logger: stderr output ─────────────────────────────────────────
|
|
39
|
+
|
|
40
|
+
describe("Logger — stderr output", () => {
|
|
41
|
+
it("writes JSON log entries to stderr (never stdout)", () => {
|
|
42
|
+
const log = new Logger();
|
|
43
|
+
const entries = captureJson(() => {
|
|
44
|
+
log.info("hello from test", { component: "unit-test" });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
assert.equal(entries.length, 1);
|
|
48
|
+
const entry = entries[0];
|
|
49
|
+
|
|
50
|
+
assert.equal(entry.level, "info");
|
|
51
|
+
assert.equal(entry.message, "hello from test");
|
|
52
|
+
assert.equal(entry.component, "unit-test");
|
|
53
|
+
assert.equal(typeof entry.timestamp, "string");
|
|
54
|
+
// timestamp must be a valid ISO date
|
|
55
|
+
assert.ok(!Number.isNaN(Date.parse(entry.timestamp as string)));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("supports warn and error levels", () => {
|
|
59
|
+
const log = new Logger();
|
|
60
|
+
const entries = captureJson(() => {
|
|
61
|
+
log.warn("careful");
|
|
62
|
+
log.error("broken");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
assert.equal(entries.length, 2);
|
|
66
|
+
assert.equal(entries[0].level, "warn");
|
|
67
|
+
assert.equal(entries[0].message, "careful");
|
|
68
|
+
assert.equal(entries[1].level, "error");
|
|
69
|
+
assert.equal(entries[1].message, "broken");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("does not write anything to stdout", () => {
|
|
73
|
+
const originalStdout = process.stdout.write;
|
|
74
|
+
let stdoutWritten = false;
|
|
75
|
+
process.stdout.write = (() => {
|
|
76
|
+
stdoutWritten = true;
|
|
77
|
+
return true;
|
|
78
|
+
}) as typeof process.stdout.write;
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const log = new Logger();
|
|
82
|
+
log.info("should not touch stdout");
|
|
83
|
+
log.warn("nor this");
|
|
84
|
+
log.error("nor this");
|
|
85
|
+
} finally {
|
|
86
|
+
process.stdout.write = originalStdout;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
assert.equal(stdoutWritten, false, "Logger must never write to stdout");
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// ── Logger: audit ─────────────────────────────────────────────────
|
|
94
|
+
|
|
95
|
+
describe("Logger.audit — format", () => {
|
|
96
|
+
it("produces an audit log entry with all required fields", () => {
|
|
97
|
+
const log = new Logger();
|
|
98
|
+
const entries = captureJson(() => {
|
|
99
|
+
log.audit("createTask", true, 150, undefined, "task-001");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
assert.equal(entries.length, 1);
|
|
103
|
+
const entry = entries[0];
|
|
104
|
+
|
|
105
|
+
// Required core fields
|
|
106
|
+
assert.equal(entry.level, "audit");
|
|
107
|
+
assert.equal(typeof entry.timestamp, "string");
|
|
108
|
+
assert.ok(!Number.isNaN(Date.parse(entry.timestamp as string)));
|
|
109
|
+
assert.equal(typeof entry.message, "string");
|
|
110
|
+
|
|
111
|
+
// Audit-specific fields
|
|
112
|
+
assert.equal(entry.tool, "createTask");
|
|
113
|
+
assert.equal(entry.ok, true);
|
|
114
|
+
assert.equal(entry.duration_ms, 150);
|
|
115
|
+
assert.equal(entry.task_id, "task-001");
|
|
116
|
+
|
|
117
|
+
// error_reason should be absent when not provided
|
|
118
|
+
assert.equal(entry.error_reason, undefined);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("includes error_reason when the tool call failed", () => {
|
|
122
|
+
const log = new Logger();
|
|
123
|
+
const entries = captureJson(() => {
|
|
124
|
+
log.audit("applyPatch", false, 3000, "command_blocked", "task-002");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
assert.equal(entries.length, 1);
|
|
128
|
+
const entry = entries[0];
|
|
129
|
+
|
|
130
|
+
assert.equal(entry.ok, false);
|
|
131
|
+
assert.equal(entry.error_reason, "command_blocked");
|
|
132
|
+
assert.equal(entry.task_id, "task-002");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("omits task_id when not provided", () => {
|
|
136
|
+
const log = new Logger();
|
|
137
|
+
const entries = captureJson(() => {
|
|
138
|
+
log.audit("healthCheck", true, 5);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
assert.equal(entries.length, 1);
|
|
142
|
+
const entry = entries[0];
|
|
143
|
+
assert.equal(entry.task_id, undefined);
|
|
144
|
+
assert.equal(entry.error_reason, undefined);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// ── Logger: verbose mode ──────────────────────────────────────────
|
|
149
|
+
|
|
150
|
+
describe("Logger — verbose mode (PATCHWARDEN_VERBOSE_LOG)", () => {
|
|
151
|
+
let savedEnv: string | undefined;
|
|
152
|
+
|
|
153
|
+
beforeEach(() => {
|
|
154
|
+
savedEnv = process.env.PATCHWARDEN_VERBOSE_LOG;
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
afterEach(() => {
|
|
158
|
+
if (savedEnv === undefined) {
|
|
159
|
+
delete process.env.PATCHWARDEN_VERBOSE_LOG;
|
|
160
|
+
} else {
|
|
161
|
+
process.env.PATCHWARDEN_VERBOSE_LOG = savedEnv;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("does not log args by default (verbose mode off)", () => {
|
|
166
|
+
delete process.env.PATCHWARDEN_VERBOSE_LOG;
|
|
167
|
+
|
|
168
|
+
const log = new Logger();
|
|
169
|
+
const sensitiveArgs = {
|
|
170
|
+
prompt: "fix the bug",
|
|
171
|
+
token: "ghp_" + "a".repeat(20),
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const entries = captureJson(() => {
|
|
175
|
+
log.audit("createTask", true, 100, undefined, "task-003", sensitiveArgs);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
assert.equal(entries.length, 1);
|
|
179
|
+
const entry = entries[0];
|
|
180
|
+
|
|
181
|
+
// args must NOT be present when verbose mode is off
|
|
182
|
+
assert.equal(entry.args, undefined);
|
|
183
|
+
// The raw token must not appear anywhere in the serialized entry
|
|
184
|
+
const raw = JSON.stringify(entry);
|
|
185
|
+
assert.ok(!raw.includes("ghp_"), "raw token must not leak into log output");
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("logs sanitized args when PATCHWARDEN_VERBOSE_LOG=true", () => {
|
|
189
|
+
process.env.PATCHWARDEN_VERBOSE_LOG = "true";
|
|
190
|
+
|
|
191
|
+
const log = new Logger();
|
|
192
|
+
const sensitiveArgs = {
|
|
193
|
+
prompt: "fix the bug",
|
|
194
|
+
token: "ghp_" + "a".repeat(20),
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const entries = captureJson(() => {
|
|
198
|
+
log.audit("createTask", true, 100, undefined, "task-004", sensitiveArgs);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
assert.equal(entries.length, 1);
|
|
202
|
+
const entry = entries[0];
|
|
203
|
+
|
|
204
|
+
// args must be present and redacted
|
|
205
|
+
assert.ok(entry.args !== undefined, "args should be logged in verbose mode");
|
|
206
|
+
const argsStr = String(entry.args);
|
|
207
|
+
assert.ok(argsStr.includes("[REDACTED TOKEN]"), "sensitive token must be redacted");
|
|
208
|
+
assert.ok(!argsStr.includes("ghp_"), "raw token must not appear in args");
|
|
209
|
+
// non-sensitive content is preserved
|
|
210
|
+
assert.ok(argsStr.includes("fix the bug"), "non-sensitive args content is preserved");
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("does not log args when PATCHWARDEN_VERBOSE_LOG is set but not 'true'", () => {
|
|
214
|
+
process.env.PATCHWARDEN_VERBOSE_LOG = "false";
|
|
215
|
+
|
|
216
|
+
const log = new Logger();
|
|
217
|
+
const entries = captureJson(() => {
|
|
218
|
+
log.audit("createTask", true, 100, undefined, "task-005", { prompt: "hello" });
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
assert.equal(entries.length, 1);
|
|
222
|
+
assert.equal(entries[0].args, undefined);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// ── logUnhandledError ─────────────────────────────────────────────
|
|
227
|
+
|
|
228
|
+
describe("logUnhandledError", () => {
|
|
229
|
+
it("produces structured JSON output for Error instances", () => {
|
|
230
|
+
const error = new Error("something went wrong");
|
|
231
|
+
error.name = "CustomError";
|
|
232
|
+
|
|
233
|
+
const entries = captureJson(() => {
|
|
234
|
+
logUnhandledError(error);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
assert.equal(entries.length, 1);
|
|
238
|
+
const entry = entries[0];
|
|
239
|
+
|
|
240
|
+
assert.equal(entry.level, "error");
|
|
241
|
+
assert.equal(entry.message, "unhandled_error");
|
|
242
|
+
assert.equal(entry.error, "something went wrong");
|
|
243
|
+
assert.equal(entry.error_name, "CustomError");
|
|
244
|
+
assert.equal(typeof entry.timestamp, "string");
|
|
245
|
+
assert.ok(!Number.isNaN(Date.parse(entry.timestamp as string)));
|
|
246
|
+
assert.equal(typeof entry.stack, "string");
|
|
247
|
+
assert.ok((entry.stack as string).includes("something went wrong"));
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it("handles non-Error values (string)", () => {
|
|
251
|
+
const entries = captureJson(() => {
|
|
252
|
+
logUnhandledError("a plain string rejection");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
assert.equal(entries.length, 1);
|
|
256
|
+
const entry = entries[0];
|
|
257
|
+
|
|
258
|
+
assert.equal(entry.level, "error");
|
|
259
|
+
assert.equal(entry.error, "a plain string rejection");
|
|
260
|
+
assert.equal(entry.error_name, "string");
|
|
261
|
+
assert.equal(entry.stack, undefined);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("handles non-Error values (object)", () => {
|
|
265
|
+
const entries = captureJson(() => {
|
|
266
|
+
logUnhandledError({ code: 42, detail: "weird" });
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
assert.equal(entries.length, 1);
|
|
270
|
+
const entry = entries[0];
|
|
271
|
+
|
|
272
|
+
assert.equal(entry.level, "error");
|
|
273
|
+
assert.ok(String(entry.error).includes("42"));
|
|
274
|
+
assert.equal(entry.error_name, "object");
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// ── installGlobalHandlers ─────────────────────────────────────────
|
|
279
|
+
|
|
280
|
+
describe("installGlobalHandlers", () => {
|
|
281
|
+
it("does not throw when called", () => {
|
|
282
|
+
// Save existing listeners so we can restore them afterwards and avoid
|
|
283
|
+
// the fatal `uncaughtException` handler interfering with the test
|
|
284
|
+
// runner.
|
|
285
|
+
const origUnhandled = process.listeners("unhandledRejection");
|
|
286
|
+
const origUncaught = process.listeners("uncaughtException");
|
|
287
|
+
|
|
288
|
+
process.removeAllListeners("unhandledRejection");
|
|
289
|
+
process.removeAllListeners("uncaughtException");
|
|
290
|
+
|
|
291
|
+
try {
|
|
292
|
+
assert.doesNotThrow(() => installGlobalHandlers());
|
|
293
|
+
|
|
294
|
+
// Verify the handlers were actually registered
|
|
295
|
+
assert.ok(process.listenerCount("unhandledRejection") >= 1);
|
|
296
|
+
assert.ok(process.listenerCount("uncaughtException") >= 1);
|
|
297
|
+
} finally {
|
|
298
|
+
// Restore original listeners
|
|
299
|
+
process.removeAllListeners("unhandledRejection");
|
|
300
|
+
process.removeAllListeners("uncaughtException");
|
|
301
|
+
for (const fn of origUnhandled) process.on("unhandledRejection", fn);
|
|
302
|
+
for (const fn of origUncaught) process.on("uncaughtException", fn);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("can be called multiple times without throwing", () => {
|
|
307
|
+
const origUnhandled = process.listeners("unhandledRejection");
|
|
308
|
+
const origUncaught = process.listeners("uncaughtException");
|
|
309
|
+
|
|
310
|
+
process.removeAllListeners("unhandledRejection");
|
|
311
|
+
process.removeAllListeners("uncaughtException");
|
|
312
|
+
|
|
313
|
+
try {
|
|
314
|
+
assert.doesNotThrow(() => {
|
|
315
|
+
installGlobalHandlers();
|
|
316
|
+
installGlobalHandlers();
|
|
317
|
+
});
|
|
318
|
+
} finally {
|
|
319
|
+
process.removeAllListeners("unhandledRejection");
|
|
320
|
+
process.removeAllListeners("uncaughtException");
|
|
321
|
+
for (const fn of origUnhandled) process.on("unhandledRejection", fn);
|
|
322
|
+
for (const fn of origUncaught) process.on("uncaughtException", fn);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
});
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
2
|
+
import { strict as assert } from "node:assert";
|
|
3
|
+
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, symlinkSync } from "node:fs";
|
|
4
|
+
import { join, resolve, sep } from "node:path";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { guardPath, guardWorkspacePath } from "../../security/pathGuard.js";
|
|
7
|
+
import { PatchWardenError } from "../../errors.js";
|
|
8
|
+
|
|
9
|
+
describe("guardPath", () => {
|
|
10
|
+
let tempDir: string;
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
tempDir = mkdtempSync(join(tmpdir(), "pw-pathguard-"));
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("allows paths inside workspace", () => {
|
|
21
|
+
const result = guardPath("src/main.ts", tempDir);
|
|
22
|
+
assert.ok(result.startsWith(tempDir));
|
|
23
|
+
assert.ok(result.includes("src"));
|
|
24
|
+
assert.ok(result.includes("main.ts"));
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("allows root workspace path", () => {
|
|
28
|
+
const result = guardPath(".", tempDir);
|
|
29
|
+
assert.equal(result, resolve(tempDir));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("allows empty path as workspace root", () => {
|
|
33
|
+
const result = guardPath("", tempDir);
|
|
34
|
+
assert.equal(result, resolve(tempDir));
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("rejects path traversal with ..", () => {
|
|
38
|
+
assert.throws(
|
|
39
|
+
() => guardPath("../../../etc/passwd", tempDir),
|
|
40
|
+
PatchWardenError
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("rejects path traversal nested with ..", () => {
|
|
45
|
+
assert.throws(
|
|
46
|
+
() => guardPath("src/../../../etc/passwd", tempDir),
|
|
47
|
+
PatchWardenError
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("rejects absolute path outside workspace", () => {
|
|
52
|
+
const outside = process.platform === "win32" ? "C:\\Windows\\System32" : "/etc";
|
|
53
|
+
assert.throws(
|
|
54
|
+
() => guardPath(outside, tempDir),
|
|
55
|
+
PatchWardenError
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("allows absolute path inside workspace", () => {
|
|
60
|
+
const insidePath = join(tempDir, "src", "main.ts");
|
|
61
|
+
mkdirSync(join(tempDir, "src"), { recursive: true });
|
|
62
|
+
writeFileSync(insidePath, "test", "utf-8");
|
|
63
|
+
const result = guardPath(insidePath, tempDir);
|
|
64
|
+
assert.equal(result, resolve(insidePath));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("enforces allowedPrefix", () => {
|
|
68
|
+
mkdirSync(join(tempDir, "allowed"), { recursive: true });
|
|
69
|
+
mkdirSync(join(tempDir, "forbidden"), { recursive: true });
|
|
70
|
+
// Path inside allowed prefix works
|
|
71
|
+
const okResult = guardPath("allowed/file.ts", tempDir, "allowed");
|
|
72
|
+
assert.ok(okResult.includes("allowed"));
|
|
73
|
+
// Path outside allowed prefix fails
|
|
74
|
+
assert.throws(
|
|
75
|
+
() => guardPath("forbidden/file.ts", tempDir, "allowed"),
|
|
76
|
+
PatchWardenError
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("handles Windows mixed separators", () => {
|
|
81
|
+
const mixedPath = "src\\subdir/file.ts";
|
|
82
|
+
const result = guardPath(mixedPath, tempDir);
|
|
83
|
+
assert.ok(result.startsWith(tempDir));
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("rejects symlink escape", { skip: process.platform === "win32" ? "Windows symlink permissions unstable" : undefined }, () => {
|
|
87
|
+
const target = mkdtempSync(join(tmpdir(), "pw-symlink-target-"));
|
|
88
|
+
try {
|
|
89
|
+
const linkPath = join(tempDir, "escape-link");
|
|
90
|
+
symlinkSync(target, linkPath);
|
|
91
|
+
assert.throws(
|
|
92
|
+
() => guardPath("escape-link/secret.txt", tempDir),
|
|
93
|
+
PatchWardenError
|
|
94
|
+
);
|
|
95
|
+
} finally {
|
|
96
|
+
rmSync(target, { recursive: true, force: true });
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("guardWorkspacePath", () => {
|
|
102
|
+
let tempDir: string;
|
|
103
|
+
|
|
104
|
+
beforeEach(() => {
|
|
105
|
+
tempDir = mkdtempSync(join(tmpdir(), "pw-wspath-"));
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
afterEach(() => {
|
|
109
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("allows relative path inside workspace", () => {
|
|
113
|
+
const result = guardWorkspacePath("my-repo", tempDir);
|
|
114
|
+
assert.equal(result, resolve(tempDir, "my-repo"));
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("allows absolute path inside workspace", () => {
|
|
118
|
+
const absPath = join(tempDir, "my-repo");
|
|
119
|
+
const result = guardWorkspacePath(absPath, tempDir);
|
|
120
|
+
assert.equal(result, resolve(absPath));
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("rejects path outside workspace", () => {
|
|
124
|
+
assert.throws(
|
|
125
|
+
() => guardWorkspacePath("../../../etc", tempDir),
|
|
126
|
+
PatchWardenError
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("rejects different drive letter on Windows", { skip: process.platform !== "win32" ? "Windows-only test" : undefined }, () => {
|
|
131
|
+
// If workspace is on C:, a D: path should be rejected
|
|
132
|
+
const wsDrive = tempDir.match(/^([A-Za-z]):/);
|
|
133
|
+
if (!wsDrive) return;
|
|
134
|
+
const otherDrive = wsDrive[1].toLowerCase() === "c" ? "D" : "C";
|
|
135
|
+
assert.throws(
|
|
136
|
+
() => guardWorkspacePath(`${otherDrive}:\\some\\repo`, tempDir),
|
|
137
|
+
(err: unknown) => err instanceof PatchWardenError && err.reason === "workspace_path_escape"
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("defaults to workspace root for empty input", () => {
|
|
142
|
+
const result = guardWorkspacePath("", tempDir);
|
|
143
|
+
assert.equal(result, resolve(tempDir));
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("defaults to workspace root for dot input", () => {
|
|
147
|
+
const result = guardWorkspacePath(".", tempDir);
|
|
148
|
+
assert.equal(result, resolve(tempDir));
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
2
|
+
import { strict as assert } from "node:assert";
|
|
3
|
+
import { mkdtempSync, rmSync, mkdirSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { safeStatus } from "../../tools/safeStatus.js";
|
|
7
|
+
import type { PatchWardenConfig } from "../../config.js";
|
|
8
|
+
|
|
9
|
+
function makeConfig(workspaceRoot: string): PatchWardenConfig {
|
|
10
|
+
return {
|
|
11
|
+
workspaceRoot,
|
|
12
|
+
plansDir: ".patchwarden/plans",
|
|
13
|
+
tasksDir: ".patchwarden/tasks",
|
|
14
|
+
assessmentsDir: ".patchwarden/assessments",
|
|
15
|
+
assessmentTtlSeconds: 3600,
|
|
16
|
+
agents: { codex: { command: "codex", args: ["exec", "{prompt}"] } },
|
|
17
|
+
allowedTestCommands: ["npm test"],
|
|
18
|
+
repoAllowedTestCommands: {},
|
|
19
|
+
maxReadFileBytes: 200_000,
|
|
20
|
+
defaultTaskTimeoutSeconds: 900,
|
|
21
|
+
maxTaskTimeoutSeconds: 3600,
|
|
22
|
+
watcherStaleSeconds: 30,
|
|
23
|
+
directSessionsDir: ".patchwarden/direct-sessions",
|
|
24
|
+
directSessionTtlSeconds: 3600,
|
|
25
|
+
directMaxPatchBytes: 200_000,
|
|
26
|
+
directMaxFileBytes: 500_000,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("safeStatus", () => {
|
|
31
|
+
let tempDir: string;
|
|
32
|
+
let config: PatchWardenConfig;
|
|
33
|
+
let tasksDir: string;
|
|
34
|
+
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
tempDir = mkdtempSync(join(tmpdir(), "pw-safestatus-"));
|
|
37
|
+
config = makeConfig(tempDir);
|
|
38
|
+
tasksDir = join(tempDir, ".patchwarden", "tasks");
|
|
39
|
+
mkdirSync(tasksDir, { recursive: true });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
function writeTaskStatus(taskId: string, status: Record<string, unknown>) {
|
|
47
|
+
const taskDir = join(tasksDir, taskId);
|
|
48
|
+
mkdirSync(taskDir, { recursive: true });
|
|
49
|
+
writeFileSync(join(taskDir, "status.json"), JSON.stringify(status, null, 2), "utf-8");
|
|
50
|
+
return taskDir;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function writeTaskRuntime(taskDir: string, runtime: Record<string, unknown>) {
|
|
54
|
+
writeFileSync(join(taskDir, "runtime.json"), JSON.stringify(runtime, null, 2), "utf-8");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
it("returns not_found for non-existent task", () => {
|
|
58
|
+
const result = safeStatus("non-existent-task", config);
|
|
59
|
+
assert.equal(result.task_id, "non-existent-task");
|
|
60
|
+
assert.equal(result.status, "not_found");
|
|
61
|
+
assert.equal(result.phase, null);
|
|
62
|
+
assert.equal(result.error_code, null);
|
|
63
|
+
assert.equal(result.error_summary, null);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("returns correct status for pending task", () => {
|
|
67
|
+
writeTaskStatus("task-pending-001", {
|
|
68
|
+
task_id: "task-pending-001",
|
|
69
|
+
status: "pending",
|
|
70
|
+
phase: "queued",
|
|
71
|
+
created_at: "2026-06-24T10:00:00Z",
|
|
72
|
+
updated_at: "2026-06-24T10:00:01Z",
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const result = safeStatus("task-pending-001", config);
|
|
76
|
+
assert.equal(result.task_id, "task-pending-001");
|
|
77
|
+
assert.equal(result.status, "pending");
|
|
78
|
+
assert.equal(result.created_at, "2026-06-24T10:00:00Z");
|
|
79
|
+
assert.equal(result.error_code, null);
|
|
80
|
+
assert.equal(result.error_summary, null);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("returns correct status for running task", () => {
|
|
84
|
+
const taskDir = writeTaskStatus("task-running-001", {
|
|
85
|
+
task_id: "task-running-001",
|
|
86
|
+
status: "running",
|
|
87
|
+
phase: "executing_agent",
|
|
88
|
+
created_at: "2026-06-24T10:00:00Z",
|
|
89
|
+
started_at: "2026-06-24T10:00:05Z",
|
|
90
|
+
updated_at: "2026-06-24T10:00:10Z",
|
|
91
|
+
});
|
|
92
|
+
writeTaskRuntime(taskDir, {
|
|
93
|
+
phase: "executing_agent",
|
|
94
|
+
last_heartbeat_at: "2026-06-24T10:00:12Z",
|
|
95
|
+
current_command: "codex exec",
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const result = safeStatus("task-running-001", config);
|
|
99
|
+
assert.equal(result.status, "running");
|
|
100
|
+
assert.equal(result.phase, "executing_agent");
|
|
101
|
+
assert.equal(result.current_command, "codex exec");
|
|
102
|
+
assert.equal(result.last_heartbeat_at, "2026-06-24T10:00:12Z");
|
|
103
|
+
assert.equal(result.started_at, "2026-06-24T10:00:05Z");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("returns correct status for done task", () => {
|
|
107
|
+
writeTaskStatus("task-done-001", {
|
|
108
|
+
task_id: "task-done-001",
|
|
109
|
+
status: "done",
|
|
110
|
+
phase: "completed",
|
|
111
|
+
created_at: "2026-06-24T10:00:00Z",
|
|
112
|
+
started_at: "2026-06-24T10:00:05Z",
|
|
113
|
+
finished_at: "2026-06-24T10:05:00Z",
|
|
114
|
+
verify_status: "passed",
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const result = safeStatus("task-done-001", config);
|
|
118
|
+
assert.equal(result.status, "done");
|
|
119
|
+
assert.equal(result.phase, "completed");
|
|
120
|
+
assert.equal(result.finished_at, "2026-06-24T10:05:00Z");
|
|
121
|
+
assert.equal(result.verify_status, "passed");
|
|
122
|
+
assert.equal(result.error_code, null);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("returns correct status for failed task", () => {
|
|
126
|
+
writeTaskStatus("task-failed-001", {
|
|
127
|
+
task_id: "task-failed-001",
|
|
128
|
+
status: "failed_verification",
|
|
129
|
+
phase: "failed_verification",
|
|
130
|
+
created_at: "2026-06-24T10:00:00Z",
|
|
131
|
+
started_at: "2026-06-24T10:00:05Z",
|
|
132
|
+
finished_at: "2026-06-24T10:05:00Z",
|
|
133
|
+
error: 'Verification command "npm test" exited with code 1.',
|
|
134
|
+
verify_status: "failed",
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const result = safeStatus("task-failed-001", config);
|
|
138
|
+
assert.equal(result.status, "failed_verification");
|
|
139
|
+
assert.equal(result.error_code, "failed_verification");
|
|
140
|
+
assert.ok(result.error_summary);
|
|
141
|
+
assert.ok(result.error_summary!.includes("npm test"));
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("does not return diff or file content", () => {
|
|
145
|
+
const taskDir = writeTaskStatus("task-safe-001", {
|
|
146
|
+
task_id: "task-safe-001",
|
|
147
|
+
status: "done",
|
|
148
|
+
phase: "completed",
|
|
149
|
+
created_at: "2026-06-24T10:00:00Z",
|
|
150
|
+
});
|
|
151
|
+
writeFileSync(join(taskDir, "result.md"), "# Secret result content", "utf-8");
|
|
152
|
+
writeFileSync(join(taskDir, "git.diff"), "secret diff content", "utf-8");
|
|
153
|
+
writeFileSync(join(taskDir, "test.log"), "secret test log", "utf-8");
|
|
154
|
+
|
|
155
|
+
const result = safeStatus("task-safe-001", config);
|
|
156
|
+
const resultStr = JSON.stringify(result);
|
|
157
|
+
assert.ok(!resultStr.includes("Secret result content"));
|
|
158
|
+
assert.ok(!resultStr.includes("secret diff content"));
|
|
159
|
+
assert.ok(!resultStr.includes("secret test log"));
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("truncates long error messages", () => {
|
|
163
|
+
const longError = "x".repeat(300);
|
|
164
|
+
writeTaskStatus("task-longerror-001", {
|
|
165
|
+
task_id: "task-longerror-001",
|
|
166
|
+
status: "failed",
|
|
167
|
+
phase: "failed",
|
|
168
|
+
error: longError,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const result = safeStatus("task-longerror-001", config);
|
|
172
|
+
assert.ok(result.error_summary!.length < 300);
|
|
173
|
+
assert.ok(result.error_summary!.endsWith("..."));
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("returns artifact_status when present", () => {
|
|
177
|
+
writeTaskStatus("task-artifact-001", {
|
|
178
|
+
task_id: "task-artifact-001",
|
|
179
|
+
status: "done",
|
|
180
|
+
phase: "completed",
|
|
181
|
+
artifact_status: "collected",
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const result = safeStatus("task-artifact-001", config);
|
|
185
|
+
assert.equal(result.artifact_status, "collected");
|
|
186
|
+
});
|
|
187
|
+
});
|