patchwarden 0.4.0 → 0.6.1
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.cmd +51 -0
- package/README.en.md +1022 -0
- package/README.md +897 -358
- package/dist/assessments/agentAssessor.d.ts +15 -0
- package/dist/assessments/agentAssessor.js +293 -0
- package/dist/assessments/assessmentStore.d.ts +133 -0
- package/dist/assessments/assessmentStore.js +238 -0
- package/dist/assessments/confirmCli.d.ts +9 -0
- package/dist/assessments/confirmCli.js +26 -0
- package/dist/config.d.ts +22 -1
- package/dist/config.js +156 -3
- package/dist/direct/directAudit.d.ts +23 -0
- package/dist/direct/directAudit.js +309 -0
- package/dist/direct/directGuards.d.ts +20 -0
- package/dist/direct/directGuards.js +137 -0
- package/dist/direct/directPatch.d.ts +17 -0
- package/dist/direct/directPatch.js +113 -0
- package/dist/direct/directSessionStore.d.ts +63 -0
- package/dist/direct/directSessionStore.js +192 -0
- package/dist/direct/directVerification.d.ts +12 -0
- package/dist/direct/directVerification.js +96 -0
- package/dist/doctor.js +73 -5
- package/dist/logging.d.ts +52 -0
- package/dist/logging.js +123 -0
- package/dist/runner/agentInvocation.d.ts +23 -0
- package/dist/runner/agentInvocation.js +106 -0
- package/dist/runner/changeCapture.d.ts +70 -0
- package/dist/runner/changeCapture.js +278 -6
- package/dist/runner/runTask.js +271 -65
- package/dist/runner/simpleProcess.d.ts +21 -0
- package/dist/runner/simpleProcess.js +206 -0
- package/dist/runner/watch.js +17 -2
- package/dist/security/commandGuard.d.ts +2 -1
- package/dist/security/commandGuard.js +25 -3
- package/dist/security/riskEngine.d.ts +27 -0
- package/dist/security/riskEngine.js +118 -0
- package/dist/security/runtimeGuard.d.ts +6 -0
- package/dist/security/runtimeGuard.js +28 -0
- package/dist/smoke-test.js +1353 -7
- 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/applyPatch.d.ts +16 -0
- package/dist/tools/applyPatch.js +41 -0
- package/dist/tools/auditSession.d.ts +5 -0
- package/dist/tools/auditSession.js +12 -0
- package/dist/tools/auditTask.d.ts +7 -0
- package/dist/tools/auditTask.js +105 -7
- package/dist/tools/createDirectSession.d.ts +14 -0
- package/dist/tools/createDirectSession.js +54 -0
- package/dist/tools/createTask.d.ts +48 -1
- package/dist/tools/createTask.js +298 -47
- package/dist/tools/finalizeDirectSession.d.ts +19 -0
- package/dist/tools/finalizeDirectSession.js +84 -0
- package/dist/tools/getTaskSummary.d.ts +50 -0
- package/dist/tools/getTaskSummary.js +79 -2
- package/dist/tools/healthCheck.d.ts +27 -12
- package/dist/tools/healthCheck.js +44 -6
- package/dist/tools/readWorkspaceFile.d.ts +7 -1
- package/dist/tools/readWorkspaceFile.js +48 -2
- package/dist/tools/registry.js +270 -19
- package/dist/tools/runVerification.d.ts +16 -0
- package/dist/tools/runVerification.js +32 -0
- package/dist/tools/safeStatus.d.ts +19 -0
- package/dist/tools/safeStatus.js +72 -0
- package/dist/tools/savePlan.d.ts +1 -0
- package/dist/tools/savePlan.js +38 -7
- package/dist/tools/searchWorkspace.d.ts +19 -0
- package/dist/tools/searchWorkspace.js +205 -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/taskTemplates.js +2 -1
- package/dist/tools/toolCatalog.d.ts +4 -3
- package/dist/tools/toolCatalog.js +34 -5
- package/dist/tools/waitForTask.d.ts +2 -2
- package/dist/tools/waitForTask.js +1 -1
- 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/chatgpt-usage.md +101 -0
- package/docs/performance-notes.md +55 -0
- package/docs/release-checklist.md +14 -0
- package/docs/release-v0.6.0.md +71 -0
- package/docs/release-v0.6.1.md +75 -0
- package/examples/config.example.json +5 -0
- package/examples/openai-tunnel/README.md +11 -4
- package/examples/openai-tunnel/chatgpt-test-prompt.md +18 -14
- package/examples/openai-tunnel/tunnel-client.example.yaml +8 -0
- package/package.json +13 -8
- package/scripts/brand-check.js +58 -12
- package/scripts/control-smoke.js +206 -0
- package/scripts/http-mcp-smoke.js +10 -2
- package/scripts/launchers/Check-PatchWarden-Health.cmd +6 -0
- package/scripts/launchers/Reset-PatchWarden-Tunnel-Key.cmd +6 -0
- package/scripts/launchers/Restart-PatchWarden.cmd +6 -0
- package/scripts/launchers/Start-PatchWarden-Direct-Tunnel.cmd +7 -0
- package/scripts/launchers/Start-PatchWarden-Tunnel.cmd +7 -0
- package/scripts/lifecycle-smoke.js +402 -8
- package/scripts/manage-patchwarden.ps1 +639 -0
- package/scripts/mcp-manifest-check.js +123 -58
- package/scripts/mcp-smoke.js +160 -2
- package/scripts/pack-clean.js +160 -5
- package/scripts/package-manifest-check.js +78 -0
- package/scripts/patchwarden-mcp-direct.cmd +7 -0
- package/scripts/patchwarden-mcp-stdio.cmd +1 -1
- package/scripts/restart-patchwarden.ps1 +5 -4
- package/scripts/start-patchwarden-tunnel.ps1 +261 -30
- package/scripts/tunnel-supervisor-smoke.js +36 -4
- package/scripts/unit-tests.js +36 -0
- package/scripts/watcher-supervisor-smoke.js +10 -6
- package/src/assessments/agentAssessor.ts +324 -0
- package/src/assessments/assessmentStore.ts +426 -0
- package/src/assessments/confirmCli.ts +29 -0
- package/src/config.ts +178 -4
- package/src/direct/directAudit.ts +400 -0
- package/src/direct/directGuards.ts +279 -0
- package/src/direct/directPatch.ts +258 -0
- package/src/direct/directSessionStore.ts +345 -0
- package/src/direct/directVerification.ts +138 -0
- package/src/doctor.ts +103 -7
- package/src/logging.ts +152 -0
- package/src/runner/agentInvocation.ts +125 -0
- package/src/runner/changeCapture.ts +352 -6
- package/src/runner/runTask.ts +279 -63
- package/src/runner/simpleProcess.ts +223 -0
- package/src/runner/watch.ts +18 -2
- package/src/security/commandGuard.ts +46 -4
- package/src/security/riskEngine.ts +160 -0
- package/src/security/runtimeGuard.ts +41 -0
- package/src/smoke-test.ts +1291 -4
- 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/applyPatch.ts +86 -0
- package/src/tools/auditSession.ts +28 -0
- package/src/tools/auditTask.ts +111 -7
- package/src/tools/createDirectSession.ts +113 -0
- package/src/tools/createTask.ts +405 -55
- package/src/tools/finalizeDirectSession.ts +144 -0
- package/src/tools/getTaskSummary.ts +133 -2
- package/src/tools/healthCheck.ts +45 -6
- package/src/tools/readWorkspaceFile.ts +85 -2
- package/src/tools/registry.ts +305 -19
- package/src/tools/runVerification.ts +58 -0
- package/src/tools/safeStatus.ts +96 -0
- package/src/tools/savePlan.ts +57 -7
- package/src/tools/searchWorkspace.ts +275 -0
- package/src/tools/syncFile.ts +122 -0
- package/src/tools/taskTemplates.ts +2 -1
- package/src/tools/toolCatalog.ts +37 -6
- package/src/tools/waitForTask.ts +3 -3
- package/src/version.ts +2 -2
- package/src/watcherStatus.ts +101 -4
- package/tsconfig.json +18 -17
- package/Check-PatchWarden-Health.cmd +0 -6
- package/Reset-PatchWarden-Tunnel-Key.cmd +0 -6
- package/Restart-PatchWarden.cmd +0 -19
- package/Start-PatchWarden-Tunnel.cmd +0 -7
- package/docs/release-v0.3.0.md +0 -43
- package/docs/release-v0.4.0.md +0 -74
package/src/tools/savePlan.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { resolve, join } from "node:path";
|
|
1
|
+
import { mkdirSync, writeFileSync, existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { resolve, join, relative } from "node:path";
|
|
3
3
|
import { getPlansDir, getConfig } from "../config.js";
|
|
4
4
|
import { guardPath } from "../security/pathGuard.js";
|
|
5
5
|
import { guardPlanContent } from "../security/planGuard.js";
|
|
6
|
+
import { PatchWardenError } from "../errors.js";
|
|
6
7
|
|
|
7
8
|
export interface SavePlanInput {
|
|
8
9
|
title: string;
|
|
9
10
|
content: string;
|
|
11
|
+
plan_ref?: string;
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export interface SavePlanOutput {
|
|
@@ -19,9 +21,57 @@ export function savePlan(input: SavePlanInput): SavePlanOutput {
|
|
|
19
21
|
const config = getConfig();
|
|
20
22
|
const plansDir = getPlansDir(config);
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
let content = input.content || "";
|
|
25
|
+
let title = input.title || "";
|
|
23
26
|
|
|
24
|
-
|
|
27
|
+
if (input.plan_ref) {
|
|
28
|
+
// 收缩 #3: plan_ref 只能读取 .patchwarden/plans 内文件,禁止任意路径
|
|
29
|
+
// plan_ref is relative to plansDir, not workspaceRoot
|
|
30
|
+
const targetPath = resolve(plansDir, input.plan_ref);
|
|
31
|
+
// Guard: must stay inside workspace and inside plansDir
|
|
32
|
+
guardPath(targetPath, config.workspaceRoot, config.plansDir);
|
|
33
|
+
const relativeToPlans = relative(plansDir, targetPath);
|
|
34
|
+
if (relativeToPlans.startsWith("..")) {
|
|
35
|
+
throw new PatchWardenError(
|
|
36
|
+
"plan_ref_outside_plans_dir",
|
|
37
|
+
`plan_ref must point to a file inside .patchwarden/plans.`,
|
|
38
|
+
"Use a path relative to .patchwarden/plans/.",
|
|
39
|
+
true,
|
|
40
|
+
{ plan_ref: input.plan_ref, resolved: targetPath }
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
if (!existsSync(targetPath)) {
|
|
44
|
+
throw new PatchWardenError(
|
|
45
|
+
"plan_ref_not_found",
|
|
46
|
+
`plan_ref file not found: "${input.plan_ref}".`,
|
|
47
|
+
"Place the plan file under .patchwarden/plans/ first, then reference it.",
|
|
48
|
+
true,
|
|
49
|
+
{ plan_ref: input.plan_ref }
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
content = readFileSync(targetPath, "utf-8");
|
|
53
|
+
if (!title || title.trim() === "") {
|
|
54
|
+
title = "Plan from file";
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
// Without plan_ref, content is required
|
|
58
|
+
if (!content || content.trim() === "") {
|
|
59
|
+
throw new PatchWardenError(
|
|
60
|
+
"plan_content_required",
|
|
61
|
+
"save_plan requires content or plan_ref.",
|
|
62
|
+
"Pass content with the plan text, or use plan_ref to load a file from .patchwarden/plans."
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Default title when empty
|
|
68
|
+
if (!title || title.trim() === "") {
|
|
69
|
+
title = "Inline plan";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
guardPlanContent(title, content);
|
|
73
|
+
|
|
74
|
+
const planId = `plan_${Date.now()}_${sanitizeTitle(title)}`;
|
|
25
75
|
const planDir = resolve(plansDir, planId);
|
|
26
76
|
|
|
27
77
|
// Guards: plan dir & file must stay inside workspace
|
|
@@ -29,13 +79,13 @@ export function savePlan(input: SavePlanInput): SavePlanOutput {
|
|
|
29
79
|
mkdirSync(planDir, { recursive: true });
|
|
30
80
|
|
|
31
81
|
const planFile = join(planDir, "plan.md");
|
|
32
|
-
const header = `# ${
|
|
33
|
-
writeFileSync(planFile, header +
|
|
82
|
+
const header = `# ${title}\n\n> Plan ID: ${planId}\n> Created: ${new Date().toISOString()}\n\n`;
|
|
83
|
+
writeFileSync(planFile, header + content, "utf-8");
|
|
34
84
|
|
|
35
85
|
return {
|
|
36
86
|
plan_id: planId,
|
|
37
87
|
path: planFile,
|
|
38
|
-
title:
|
|
88
|
+
title: title,
|
|
39
89
|
};
|
|
40
90
|
}
|
|
41
91
|
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { readdirSync, readFileSync, statSync, existsSync } from "node:fs";
|
|
2
|
+
import { relative, join, extname } from "node:path";
|
|
3
|
+
import { getConfig } from "../config.js";
|
|
4
|
+
import { PatchWardenError } from "../errors.js";
|
|
5
|
+
import { isSensitivePath } from "../security/sensitiveGuard.js";
|
|
6
|
+
import {
|
|
7
|
+
readDirectSession,
|
|
8
|
+
validateDirectSessionFreshness,
|
|
9
|
+
} from "../direct/directSessionStore.js";
|
|
10
|
+
import { guardDirectSessionActive } from "../direct/directGuards.js";
|
|
11
|
+
|
|
12
|
+
// ── Types ──────────────────────────────────────────────────────────
|
|
13
|
+
|
|
14
|
+
export interface SearchWorkspaceInput {
|
|
15
|
+
session_id: string;
|
|
16
|
+
query: string;
|
|
17
|
+
max_results?: number;
|
|
18
|
+
case_sensitive?: boolean;
|
|
19
|
+
max_preview_chars?: number;
|
|
20
|
+
include_globs?: string[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SearchMatch {
|
|
24
|
+
path: string;
|
|
25
|
+
line: number;
|
|
26
|
+
preview: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface SearchWorkspaceOutput {
|
|
30
|
+
results: SearchMatch[];
|
|
31
|
+
total_matches: number;
|
|
32
|
+
truncated: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ── Constants ──────────────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
const SKIP_DIRECTORIES = new Set([
|
|
38
|
+
".git",
|
|
39
|
+
".patchwarden",
|
|
40
|
+
"node_modules",
|
|
41
|
+
"dist",
|
|
42
|
+
"release",
|
|
43
|
+
"coverage",
|
|
44
|
+
".next",
|
|
45
|
+
".turbo",
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
const BINARY_EXTENSIONS = new Set([
|
|
49
|
+
".exe",
|
|
50
|
+
".dll",
|
|
51
|
+
".so",
|
|
52
|
+
".png",
|
|
53
|
+
".jpg",
|
|
54
|
+
".jpeg",
|
|
55
|
+
".gif",
|
|
56
|
+
".bmp",
|
|
57
|
+
".ico",
|
|
58
|
+
".webp",
|
|
59
|
+
".pdf",
|
|
60
|
+
".mp3",
|
|
61
|
+
".mp4",
|
|
62
|
+
".zip",
|
|
63
|
+
".gz",
|
|
64
|
+
".tar",
|
|
65
|
+
".bin",
|
|
66
|
+
".dat",
|
|
67
|
+
".class",
|
|
68
|
+
".jar",
|
|
69
|
+
".wasm",
|
|
70
|
+
".node",
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
// Safety valve: stop scanning after this many total matches to prevent
|
|
74
|
+
// runaway searches in large repositories with very common terms.
|
|
75
|
+
const MAX_TOTAL_MATCHES = 10_000;
|
|
76
|
+
|
|
77
|
+
// ── Tool implementation ────────────────────────────────────────────
|
|
78
|
+
|
|
79
|
+
export function searchWorkspace(
|
|
80
|
+
input: SearchWorkspaceInput
|
|
81
|
+
): SearchWorkspaceOutput {
|
|
82
|
+
const config = getConfig();
|
|
83
|
+
|
|
84
|
+
// ── Validate session freshness ───────────────────────────────────
|
|
85
|
+
|
|
86
|
+
const validation = validateDirectSessionFreshness(input.session_id);
|
|
87
|
+
if (!validation.valid || !validation.session) {
|
|
88
|
+
throw new PatchWardenError(
|
|
89
|
+
validation.failure_reason || "session_invalid",
|
|
90
|
+
`Direct session "${input.session_id}" is not valid: ${validation.failure_reason}.`,
|
|
91
|
+
"Create a new direct session with create_direct_session.",
|
|
92
|
+
true,
|
|
93
|
+
{
|
|
94
|
+
session_id: input.session_id,
|
|
95
|
+
failure_reason: validation.failure_reason,
|
|
96
|
+
operation: "search_workspace",
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const session = validation.session;
|
|
102
|
+
guardDirectSessionActive(session);
|
|
103
|
+
|
|
104
|
+
// ── Validate query ───────────────────────────────────────────────
|
|
105
|
+
|
|
106
|
+
if (!input.query || input.query.trim() === "") {
|
|
107
|
+
throw new PatchWardenError(
|
|
108
|
+
"invalid_input",
|
|
109
|
+
"query is required and must be a non-empty string.",
|
|
110
|
+
"Provide a search query to look for in file contents.",
|
|
111
|
+
true,
|
|
112
|
+
{ operation: "search_workspace", session_id: input.session_id }
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ── Resolve parameters ───────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
const maxResults = input.max_results ?? 20;
|
|
119
|
+
const caseSensitive = input.case_sensitive ?? false;
|
|
120
|
+
const maxPreviewChars = input.max_preview_chars ?? 200;
|
|
121
|
+
const includeGlobs = input.include_globs;
|
|
122
|
+
const query = caseSensitive ? input.query : input.query.toLowerCase();
|
|
123
|
+
|
|
124
|
+
const repoPath = session.resolved_repo_path;
|
|
125
|
+
|
|
126
|
+
if (!existsSync(repoPath)) {
|
|
127
|
+
throw new PatchWardenError(
|
|
128
|
+
"repo_not_found",
|
|
129
|
+
`Session repo path "${repoPath}" no longer exists.`,
|
|
130
|
+
"Create a new direct session with create_direct_session.",
|
|
131
|
+
true,
|
|
132
|
+
{
|
|
133
|
+
session_id: input.session_id,
|
|
134
|
+
resolved_repo_path: repoPath,
|
|
135
|
+
operation: "search_workspace",
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ── Recursive search ─────────────────────────────────────────────
|
|
141
|
+
|
|
142
|
+
const results: SearchMatch[] = [];
|
|
143
|
+
let totalMatches = 0;
|
|
144
|
+
let truncated = false;
|
|
145
|
+
|
|
146
|
+
const visit = (directory: string): void => {
|
|
147
|
+
if (truncated && totalMatches >= MAX_TOTAL_MATCHES) return;
|
|
148
|
+
|
|
149
|
+
let entries;
|
|
150
|
+
try {
|
|
151
|
+
entries = readdirSync(directory, { withFileTypes: true });
|
|
152
|
+
} catch {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
for (const entry of entries) {
|
|
157
|
+
if (totalMatches >= MAX_TOTAL_MATCHES) {
|
|
158
|
+
truncated = true;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Skip blacklisted directories
|
|
163
|
+
if (entry.isDirectory() && SKIP_DIRECTORIES.has(entry.name)) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const fullPath = join(directory, entry.name);
|
|
168
|
+
const relPath = relative(repoPath, fullPath).replace(/\\/g, "/");
|
|
169
|
+
|
|
170
|
+
if (entry.isDirectory()) {
|
|
171
|
+
visit(fullPath);
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (!entry.isFile()) continue;
|
|
176
|
+
|
|
177
|
+
// Skip sensitive files
|
|
178
|
+
if (isSensitivePath(relPath)) continue;
|
|
179
|
+
|
|
180
|
+
// Skip binary files by extension
|
|
181
|
+
const ext = extname(entry.name).toLowerCase();
|
|
182
|
+
if (BINARY_EXTENSIONS.has(ext)) continue;
|
|
183
|
+
|
|
184
|
+
// Apply include_globs filter
|
|
185
|
+
if (includeGlobs && includeGlobs.length > 0) {
|
|
186
|
+
if (!matchesAnyGlob(relPath, includeGlobs)) continue;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Read file content and search line by line
|
|
190
|
+
let content: string;
|
|
191
|
+
try {
|
|
192
|
+
const fileStat = statSync(fullPath);
|
|
193
|
+
if (fileStat.size > config.directMaxFileBytes) continue;
|
|
194
|
+
content = readFileSync(fullPath, "utf-8");
|
|
195
|
+
} catch {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const lines = content.split(/\r?\n/);
|
|
200
|
+
for (let i = 0; i < lines.length; i++) {
|
|
201
|
+
if (totalMatches >= MAX_TOTAL_MATCHES) {
|
|
202
|
+
truncated = true;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const line = lines[i];
|
|
207
|
+
const haystack = caseSensitive ? line : line.toLowerCase();
|
|
208
|
+
if (haystack.includes(query)) {
|
|
209
|
+
totalMatches++;
|
|
210
|
+
if (results.length < maxResults) {
|
|
211
|
+
const preview =
|
|
212
|
+
line.length > maxPreviewChars
|
|
213
|
+
? line.slice(0, maxPreviewChars)
|
|
214
|
+
: line;
|
|
215
|
+
results.push({
|
|
216
|
+
path: relPath,
|
|
217
|
+
line: i + 1,
|
|
218
|
+
preview,
|
|
219
|
+
});
|
|
220
|
+
} else {
|
|
221
|
+
truncated = true;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
visit(repoPath);
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
results,
|
|
232
|
+
total_matches: totalMatches,
|
|
233
|
+
truncated,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ── Glob matching ──────────────────────────────────────────────────
|
|
238
|
+
|
|
239
|
+
function matchesAnyGlob(filePath: string, globs: string[]): boolean {
|
|
240
|
+
for (const pattern of globs) {
|
|
241
|
+
if (matchGlob(filePath, pattern)) return true;
|
|
242
|
+
}
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Simple glob matching: "*" matches any sequence of characters (including
|
|
248
|
+
* path separators), "?" matches a single character.
|
|
249
|
+
*
|
|
250
|
+
* Examples:
|
|
251
|
+
* "*.ts" matches "foo.ts", "src/bar.ts"
|
|
252
|
+
* "src/*.ts" matches "src/foo.ts", "src/sub/bar.ts"
|
|
253
|
+
* "*.test.ts" matches "foo.test.ts", "src/bar.test.ts"
|
|
254
|
+
*/
|
|
255
|
+
function matchGlob(filePath: string, pattern: string): boolean {
|
|
256
|
+
const normalizedPattern = pattern.replace(/\\/g, "/");
|
|
257
|
+
const regex = globToRegex(normalizedPattern);
|
|
258
|
+
return regex.test(filePath);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function globToRegex(pattern: string): RegExp {
|
|
262
|
+
let regexStr = "";
|
|
263
|
+
for (const char of pattern) {
|
|
264
|
+
if (char === "*") {
|
|
265
|
+
regexStr += ".*";
|
|
266
|
+
} else if (char === "?") {
|
|
267
|
+
regexStr += ".";
|
|
268
|
+
} else if (/[.+^${}()|[\]\\]/.test(char)) {
|
|
269
|
+
regexStr += "\\" + char;
|
|
270
|
+
} else {
|
|
271
|
+
regexStr += char;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return new RegExp("^" + regexStr + "$");
|
|
275
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync, statSync } from "node:fs";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { dirname, resolve, relative, isAbsolute } from "node:path";
|
|
4
|
+
import { PatchWardenError } from "../errors.js";
|
|
5
|
+
import { getConfig, type PatchWardenConfig } from "../config.js";
|
|
6
|
+
import { guardDirectPath, guardDirectWritePath } from "../direct/directGuards.js";
|
|
7
|
+
import { guardSensitivePath } from "../security/sensitiveGuard.js";
|
|
8
|
+
import { computeFileSha256 } from "../direct/directPatch.js";
|
|
9
|
+
|
|
10
|
+
export interface SyncFileResult {
|
|
11
|
+
source_path: string;
|
|
12
|
+
target_path: string;
|
|
13
|
+
before_target_sha256: string | null;
|
|
14
|
+
after_target_sha256: string;
|
|
15
|
+
source_sha256: string;
|
|
16
|
+
copied_bytes: number;
|
|
17
|
+
changed: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Copy a file from source to target within the same session repo.
|
|
22
|
+
* Both source and target must be inside the session's repo_path.
|
|
23
|
+
*/
|
|
24
|
+
export function syncFile(
|
|
25
|
+
sessionId: string,
|
|
26
|
+
sourcePath: string,
|
|
27
|
+
targetPath: string,
|
|
28
|
+
options?: {
|
|
29
|
+
expected_source_sha256?: string;
|
|
30
|
+
expected_target_sha256?: string;
|
|
31
|
+
},
|
|
32
|
+
config?: PatchWardenConfig
|
|
33
|
+
): SyncFileResult {
|
|
34
|
+
const cfg = config || getConfig();
|
|
35
|
+
const sessionsDir = resolve(cfg.workspaceRoot, cfg.directSessionsDir);
|
|
36
|
+
|
|
37
|
+
// Load session to get repo_path
|
|
38
|
+
const sessionFile = resolve(sessionsDir, sessionId, "session.json");
|
|
39
|
+
if (!existsSync(sessionFile)) {
|
|
40
|
+
throw new PatchWardenError(
|
|
41
|
+
"direct_session_not_found",
|
|
42
|
+
`Direct session "${sessionId}" not found.`,
|
|
43
|
+
"Create a direct session first using direct_start_session.",
|
|
44
|
+
true,
|
|
45
|
+
{ session_id: sessionId }
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const session = JSON.parse(readFileSync(sessionFile, "utf-8"));
|
|
50
|
+
const repoPath = resolve(session.repo_path);
|
|
51
|
+
const workspaceRoot = cfg.workspaceRoot;
|
|
52
|
+
|
|
53
|
+
// Guard source path — must be inside repo
|
|
54
|
+
const resolvedSource = guardDirectPath(sourcePath, repoPath, workspaceRoot);
|
|
55
|
+
guardSensitivePath(resolvedSource);
|
|
56
|
+
|
|
57
|
+
if (!existsSync(resolvedSource)) {
|
|
58
|
+
throw new PatchWardenError(
|
|
59
|
+
"source_file_not_found",
|
|
60
|
+
`Source file does not exist: "${sourcePath}".`,
|
|
61
|
+
"Ensure the source path is correct.",
|
|
62
|
+
true,
|
|
63
|
+
{ source_path: sourcePath }
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Guard target path — must be inside repo, not in blocked dirs
|
|
68
|
+
const resolvedTarget = guardDirectWritePath(targetPath, repoPath, workspaceRoot);
|
|
69
|
+
guardSensitivePath(resolvedTarget);
|
|
70
|
+
|
|
71
|
+
// Verify source sha256 if provided
|
|
72
|
+
const sourceSha256 = computeFileSha256(resolvedSource);
|
|
73
|
+
if (options?.expected_source_sha256 && options.expected_source_sha256 !== sourceSha256) {
|
|
74
|
+
throw new PatchWardenError(
|
|
75
|
+
"source_hash_mismatch",
|
|
76
|
+
`Source file hash mismatch. Expected "${options.expected_source_sha256}" but got "${sourceSha256}".`,
|
|
77
|
+
"Re-read the source file to get the current sha256.",
|
|
78
|
+
true,
|
|
79
|
+
{ expected_sha256: options.expected_source_sha256, actual_sha256: sourceSha256 }
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Get target sha256 before copy
|
|
84
|
+
let beforeTargetSha256: string | null = null;
|
|
85
|
+
if (existsSync(resolvedTarget)) {
|
|
86
|
+
beforeTargetSha256 = computeFileSha256(resolvedTarget);
|
|
87
|
+
// Verify target sha256 if provided
|
|
88
|
+
if (options?.expected_target_sha256 && options.expected_target_sha256 !== beforeTargetSha256) {
|
|
89
|
+
throw new PatchWardenError(
|
|
90
|
+
"target_hash_mismatch",
|
|
91
|
+
`Target file hash mismatch. Expected "${options.expected_target_sha256}" but got "${beforeTargetSha256}".`,
|
|
92
|
+
"Re-read the target file to get the current sha256.",
|
|
93
|
+
true,
|
|
94
|
+
{ expected_sha256: options.expected_target_sha256, actual_sha256: beforeTargetSha256 }
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Read source content
|
|
100
|
+
const sourceContent = readFileSync(resolvedSource);
|
|
101
|
+
const copiedBytes = sourceContent.length;
|
|
102
|
+
|
|
103
|
+
// Create target directory if needed
|
|
104
|
+
mkdirSync(dirname(resolvedTarget), { recursive: true });
|
|
105
|
+
|
|
106
|
+
// Write to target
|
|
107
|
+
writeFileSync(resolvedTarget, sourceContent, "utf-8");
|
|
108
|
+
|
|
109
|
+
// Compute after hash
|
|
110
|
+
const afterTargetSha256 = computeFileSha256(resolvedTarget);
|
|
111
|
+
const changed = beforeTargetSha256 !== afterTargetSha256;
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
source_path: sourcePath,
|
|
115
|
+
target_path: targetPath,
|
|
116
|
+
before_target_sha256: beforeTargetSha256,
|
|
117
|
+
after_target_sha256: afterTargetSha256,
|
|
118
|
+
source_sha256: sourceSha256,
|
|
119
|
+
copied_bytes: copiedBytes,
|
|
120
|
+
changed,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
@@ -42,7 +42,8 @@ export function expandTaskTemplate(input: TaskTemplateInput): ExpandedTaskTempla
|
|
|
42
42
|
`## Goal\n${goal}`,
|
|
43
43
|
"## Safety boundaries",
|
|
44
44
|
"- Work only inside the resolved repository path.",
|
|
45
|
-
"-
|
|
45
|
+
"- Leave repository changes uncommitted for review; remote operations are outside this task.",
|
|
46
|
+
"- Keep credentials private and do not modify unrelated files.",
|
|
46
47
|
"- Report exact files inspected or changed and any remaining uncertainty.",
|
|
47
48
|
];
|
|
48
49
|
|
package/src/tools/toolCatalog.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { PATCHWARDEN_VERSION, TOOL_SCHEMA_EPOCH } from "../version.js";
|
|
3
3
|
|
|
4
|
-
export type ToolProfile = "full" | "chatgpt_core";
|
|
4
|
+
export type ToolProfile = "full" | "chatgpt_core" | "chatgpt_direct";
|
|
5
5
|
|
|
6
6
|
export interface CatalogTool {
|
|
7
7
|
name: string;
|
|
@@ -35,24 +35,55 @@ export const CHATGPT_CORE_TOOL_NAMES = [
|
|
|
35
35
|
"list_tasks",
|
|
36
36
|
"cancel_task",
|
|
37
37
|
"audit_task",
|
|
38
|
+
"safe_status",
|
|
39
|
+
] as const;
|
|
40
|
+
|
|
41
|
+
export const CHATGPT_DIRECT_TOOL_NAMES = [
|
|
42
|
+
"health_check",
|
|
43
|
+
"list_workspace",
|
|
44
|
+
"create_direct_session",
|
|
45
|
+
"search_workspace",
|
|
46
|
+
"read_workspace_file",
|
|
47
|
+
"apply_patch",
|
|
48
|
+
"run_verification",
|
|
49
|
+
"finalize_direct_session",
|
|
50
|
+
"audit_session",
|
|
51
|
+
"sync_file",
|
|
38
52
|
] as const;
|
|
39
53
|
|
|
40
54
|
let lastSnapshot: ToolCatalogSnapshot | null = null;
|
|
41
55
|
|
|
42
56
|
export function resolveToolProfile(configProfile?: string): ToolProfile {
|
|
43
57
|
const raw = (process.env.PATCHWARDEN_TOOL_PROFILE || configProfile || "full").trim();
|
|
44
|
-
if (raw !== "full" && raw !== "chatgpt_core") {
|
|
45
|
-
throw new Error(`Invalid tool profile "${raw}". Expected "full" or "
|
|
58
|
+
if (raw !== "full" && raw !== "chatgpt_core" && raw !== "chatgpt_direct") {
|
|
59
|
+
throw new Error(`Invalid tool profile "${raw}". Expected "full", "chatgpt_core", or "chatgpt_direct".`);
|
|
46
60
|
}
|
|
47
61
|
return raw;
|
|
48
62
|
}
|
|
49
63
|
|
|
50
|
-
export function selectToolsForProfile<T extends CatalogTool>(tools: T[], profile: ToolProfile): T[] {
|
|
64
|
+
export function selectToolsForProfile<T extends CatalogTool>(tools: T[], profile: ToolProfile, enableDirectProfile?: boolean): T[] {
|
|
51
65
|
if (profile === "full") return tools;
|
|
52
66
|
const byName = new Map(tools.map((tool) => [tool.name, tool]));
|
|
53
|
-
|
|
67
|
+
|
|
68
|
+
if (profile === "chatgpt_core") {
|
|
69
|
+
return CHATGPT_CORE_TOOL_NAMES.map((name) => {
|
|
70
|
+
const tool = byName.get(name);
|
|
71
|
+
if (!tool) throw new Error(`chatgpt_core tool profile requires missing tool "${name}".`);
|
|
72
|
+
return tool;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// chatgpt_direct
|
|
77
|
+
if (!enableDirectProfile) {
|
|
78
|
+
// Degraded mode: only expose health_check for diagnostics
|
|
79
|
+
const tool = byName.get("health_check");
|
|
80
|
+
if (!tool) throw new Error(`chatgpt_direct degraded mode requires missing tool "health_check".`);
|
|
81
|
+
return [tool];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return CHATGPT_DIRECT_TOOL_NAMES.map((name) => {
|
|
54
85
|
const tool = byName.get(name);
|
|
55
|
-
if (!tool) throw new Error(`
|
|
86
|
+
if (!tool) throw new Error(`chatgpt_direct tool profile requires missing tool "${name}".`);
|
|
56
87
|
return tool;
|
|
57
88
|
});
|
|
58
89
|
}
|
package/src/tools/waitForTask.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
2
2
|
import { getTaskStatus } from "./getTaskStatus.js";
|
|
3
|
-
import { getTaskSummary, type
|
|
3
|
+
import { getTaskSummary, type TaskSummaryResult } from "./getTaskSummary.js";
|
|
4
4
|
|
|
5
5
|
const TERMINAL_STATUSES = new Set([
|
|
6
6
|
"done",
|
|
@@ -32,7 +32,7 @@ export interface WaitForTaskOutput {
|
|
|
32
32
|
name: "wait_for_task" | "audit_task" | "health_check";
|
|
33
33
|
arguments: Record<string, unknown>;
|
|
34
34
|
};
|
|
35
|
-
summary?:
|
|
35
|
+
summary?: TaskSummaryResult;
|
|
36
36
|
progress_summary?: WaitForTaskProgressSummary;
|
|
37
37
|
}
|
|
38
38
|
|
|
@@ -85,6 +85,6 @@ export async function waitForTask(taskId: string, waitSeconds = 25): Promise<Wai
|
|
|
85
85
|
: executionBlocked
|
|
86
86
|
? { name: "health_check", arguments: {} }
|
|
87
87
|
: { name: "wait_for_task", arguments: { task_id: taskId, timeout_seconds: waitSeconds } },
|
|
88
|
-
...(terminal ? { summary: getTaskSummary(taskId) } : { progress_summary: progressSummary }),
|
|
88
|
+
...(terminal ? { summary: getTaskSummary(taskId, { view: "compact" }) } : { progress_summary: progressSummary }),
|
|
89
89
|
};
|
|
90
90
|
}
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const PATCHWARDEN_VERSION = "0.
|
|
2
|
-
export const TOOL_SCHEMA_EPOCH = "2026-06-
|
|
1
|
+
export const PATCHWARDEN_VERSION = "0.6.1";
|
|
2
|
+
export const TOOL_SCHEMA_EPOCH = "2026-06-24-v7";
|