@ran-sh/dsh-crew 0.3.1 → 0.3.2
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/.claude-plugin/marketplace.json +17 -17
- package/.claude-plugin/plugin.json +8 -8
- package/.mcp.json +8 -8
- package/LICENSE +21 -21
- package/README.de.md +359 -359
- package/README.es.md +359 -359
- package/README.fr.md +359 -359
- package/README.hi.md +359 -359
- package/README.id.md +359 -359
- package/README.ja.md +359 -359
- package/README.ko.md +359 -359
- package/README.md +140 -140
- package/README.pt.md +359 -359
- package/README.ru.md +359 -359
- package/README.th.md +359 -359
- package/README.tr.md +359 -359
- package/README.vi.md +359 -359
- package/README.zh-TW.md +359 -359
- package/README.zh.md +140 -140
- package/agents/ds-flash.md +26 -26
- package/agents/ds-pro.md +32 -32
- package/agents/ds-reviewer.md +23 -23
- package/agents/ds-worker.md +22 -22
- package/codex/agents/ds-flash.toml +30 -30
- package/codex/agents/ds-pro.toml +31 -31
- package/codex/agents/ds-reviewer.toml +28 -28
- package/codex/agents/ds-worker.toml +28 -28
- package/codex/prompts/dsh-config.md +3 -3
- package/codex/prompts/dsh-status.md +1 -1
- package/commands/config.md +11 -11
- package/commands/off.md +5 -5
- package/commands/on.md +5 -5
- package/commands/status.md +5 -5
- package/cordis.patch.yml +4 -4
- package/lib/client.js +2765 -2765
- package/package.json +127 -127
- package/scripts/build-client.mjs +28 -28
- package/scripts/live-crew-smoke.mjs +39 -39
- package/scripts/live-policy-matrix.mjs +177 -177
- package/scripts/policy-probe.mjs +101 -101
- package/scripts/setup.mjs +295 -294
- package/scripts/smoke-real.mjs +110 -110
- package/scripts/smoke.mjs +78 -78
- package/scripts/verify-installer-fix.mjs +26 -26
- package/scripts/verify-npm-install.mjs +297 -276
- package/src/adaptive-routing.mjs +260 -260
- package/src/client/activation-summary.tsx +64 -64
- package/src/client/entry.tsx +236 -236
- package/src/client/index.tsx +1120 -1120
- package/src/config-readiness.mjs +59 -59
- package/src/delivery.mjs +205 -205
- package/src/dsh-cli-runtime.mjs +435 -251
- package/src/failure-classification.mjs +172 -172
- package/src/hub/entry.mjs +98 -98
- package/src/hub-client.mjs +132 -132
- package/src/hub-compatibility.mjs +49 -49
- package/src/i18n.mjs +19 -19
- package/src/install/cli.mjs +28 -28
- package/src/install/install-legacy.mjs +460 -460
- package/src/install/install.mjs +451 -451
- package/src/mcp-runtime.mjs +257 -257
- package/src/model-catalog.mjs +173 -173
- package/src/model-routing.mjs +391 -391
- package/src/policy.mjs +197 -197
- package/src/readiness-matrix.mjs +169 -169
- package/src/runtime-controls.mjs +90 -90
- package/src/runtime-identity.mjs +108 -108
- package/src/server.mjs +477 -477
- package/src/status-shard.mjs +52 -52
- package/src/structured-error-code.mjs +38 -38
- package/src/vision-route.mjs +138 -138
- package/src/workflow-runtime.mjs +573 -567
- package/src/workflow.mjs +160 -160
- package/src/workspace-audit.mjs +231 -231
- package/src/workspace-isolation.mjs +365 -306
- package/statusline/statusline.sh +14 -14
- package/statusline/worker-segment.sh +35 -35
- package/worker.cordis.yml +77 -77
package/src/workspace-audit.mjs
CHANGED
|
@@ -1,231 +1,231 @@
|
|
|
1
|
-
// Read-only workspace auditing for the auditable-delivery flow. Captures a
|
|
2
|
-
// before/after snapshot of a git working tree (status / stat / name-status)
|
|
3
|
-
// plus a bounded, redacted patch, so an orchestrator can review exactly what a
|
|
4
|
-
// worker changed before accepting the result.
|
|
5
|
-
//
|
|
6
|
-
// Discipline: strictly read-only. Only git porcelain reads are issued — never
|
|
7
|
-
// reset / stash / clean / checkout, and nothing here writes to disk. A
|
|
8
|
-
// non-git directory degrades to { kind: 'no-git' } instead of failing, and a
|
|
9
|
-
// pre-dirty workspace is flagged (dirtyBaseline) rather than hidden.
|
|
10
|
-
//
|
|
11
|
-
// The default `git` runner is replaceable with an injected runner in tests:
|
|
12
|
-
// it receives (argsArray, { cwd }) and resolves { code, stdout, stderr }.
|
|
13
|
-
|
|
14
|
-
import { execFile } from 'node:child_process';
|
|
15
|
-
import { existsSync } from 'node:fs';
|
|
16
|
-
import { win32 } from 'node:path';
|
|
17
|
-
import { promisify } from 'node:util';
|
|
18
|
-
|
|
19
|
-
const execFileAsync = promisify(execFile);
|
|
20
|
-
|
|
21
|
-
export const DIFF_LIMIT = 64 * 1024;
|
|
22
|
-
export const NOT_A_GIT_REPOSITORY = 'NOT_A_GIT_REPOSITORY';
|
|
23
|
-
export const GIT_NOT_FOUND = 'GIT_NOT_FOUND';
|
|
24
|
-
export const GIT_TIMEOUT = 'GIT_TIMEOUT';
|
|
25
|
-
export const GIT_TIMEOUT_MS = 8000;
|
|
26
|
-
|
|
27
|
-
const SENSITIVE_SUFFIXES = ['.pem', '.key'];
|
|
28
|
-
const SENSITIVE_PREFIXES = ['credentials', 'secret'];
|
|
29
|
-
const ENV_BASENAME = '.env';
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* True when a path looks like a credential/secret file whose patch content
|
|
33
|
-
* must be redacted: .env / .env.*, credentials*, secrets*, *.pem, *.key.
|
|
34
|
-
* The file's name-status entry stays visible — only the patch is hidden.
|
|
35
|
-
*/
|
|
36
|
-
export function isSensitivePath(relPath) {
|
|
37
|
-
const segments = String(relPath ?? '').replace(/\\/g, '/').split('/');
|
|
38
|
-
return segments.some((raw) => {
|
|
39
|
-
const seg = raw.toLowerCase();
|
|
40
|
-
if (seg === ENV_BASENAME || seg.startsWith(`${ENV_BASENAME}.`)) return true;
|
|
41
|
-
if (SENSITIVE_PREFIXES.some((p) => seg.startsWith(p))) return true;
|
|
42
|
-
return SENSITIVE_SUFFIXES.some((s) => seg.endsWith(s));
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
let resolvedGitExecutable = 'git';
|
|
47
|
-
|
|
48
|
-
export async function resolveWindowsGit({ exec = execFileAsync, env = process.env, exists = existsSync } = {}) {
|
|
49
|
-
try {
|
|
50
|
-
const located = await exec('where.exe', ['git'], { encoding: 'utf8', timeout: GIT_TIMEOUT_MS });
|
|
51
|
-
const first = String(located.stdout ?? '').split(/\r?\n/).map((line) => line.trim()).find(Boolean);
|
|
52
|
-
if (first) return first;
|
|
53
|
-
} catch {}
|
|
54
|
-
// Always compose candidate paths with Windows semantics. This helper is
|
|
55
|
-
// unit-tested on non-Windows hosts too, and host-native path.join() would
|
|
56
|
-
// otherwise turn C:\\... inputs into mixed/invalid paths on POSIX.
|
|
57
|
-
const candidates = [
|
|
58
|
-
env.ProgramFiles && win32.join(env.ProgramFiles, 'Git', 'cmd', 'git.exe'),
|
|
59
|
-
env.ProgramFiles && win32.join(env.ProgramFiles, 'Git', 'bin', 'git.exe'),
|
|
60
|
-
env.LOCALAPPDATA && win32.join(env.LOCALAPPDATA, 'Programs', 'Git', 'cmd', 'git.exe'),
|
|
61
|
-
].filter(Boolean);
|
|
62
|
-
return candidates.find((candidate) => exists(candidate));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async function defaultRunner(args, { cwd }) {
|
|
66
|
-
let out;
|
|
67
|
-
try {
|
|
68
|
-
out = await execFileAsync(resolvedGitExecutable, args, { cwd, encoding: 'utf8', maxBuffer: 8 * 1024 * 1024, timeout: GIT_TIMEOUT_MS });
|
|
69
|
-
} catch (error) {
|
|
70
|
-
const missing = error?.code === 'ENOENT' || /spawn git ENOENT/i.test(error?.message ?? '');
|
|
71
|
-
if (!missing || process.platform !== 'win32' || resolvedGitExecutable !== 'git') throw error;
|
|
72
|
-
const fallback = await resolveWindowsGit();
|
|
73
|
-
if (!fallback) throw error;
|
|
74
|
-
resolvedGitExecutable = fallback;
|
|
75
|
-
out = await execFileAsync(resolvedGitExecutable, args, { cwd, encoding: 'utf8', maxBuffer: 8 * 1024 * 1024, timeout: GIT_TIMEOUT_MS });
|
|
76
|
-
}
|
|
77
|
-
return { code: 0, stdout: out.stdout ?? '', stderr: out.stderr ?? '' };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Run one git read; normalize failure into a no-git reason, never throws. */
|
|
81
|
-
async function runGit(runner, args, opts) {
|
|
82
|
-
try {
|
|
83
|
-
const r = await runner(args, opts);
|
|
84
|
-
const stderr = r.stderr ?? '';
|
|
85
|
-
if (/not a git repository/i.test(stderr)) {
|
|
86
|
-
return { ok: false, reason: NOT_A_GIT_REPOSITORY, error: stderr.trim() };
|
|
87
|
-
}
|
|
88
|
-
return { ok: true, code: r.code ?? 0, stdout: r.stdout ?? '', stderr };
|
|
89
|
-
} catch (err) {
|
|
90
|
-
const msg = err?.message ?? String(err);
|
|
91
|
-
if (err?.code === 'ETIMEDOUT' || err?.killed === true || /timed out|timeout/i.test(msg)) return { ok: false, reason: GIT_TIMEOUT, error: 'git audit timed out' };
|
|
92
|
-
if (/ENOENT|spawn git/i.test(msg)) return { ok: false, reason: GIT_NOT_FOUND, error: msg };
|
|
93
|
-
return { ok: false, reason: NOT_A_GIT_REPOSITORY, error: msg };
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function splitFirstTab(line) {
|
|
98
|
-
const i = line.indexOf('\t');
|
|
99
|
-
return i === -1 ? [line, ''] : [line.slice(0, i), line.slice(i + 1)];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Parse `git diff --name-status` + `git status --porcelain` into structured
|
|
104
|
-
* change lists used by the patch builder. Renames/copies fold onto their
|
|
105
|
-
* destination path (the diff shows them there); untracked `??` rows are listed
|
|
106
|
-
* separately because git does not diff them read-only.
|
|
107
|
-
*/
|
|
108
|
-
export function parseChanges(nameStatus, statusPorcelain) {
|
|
109
|
-
const modified = [];
|
|
110
|
-
const deleted = [];
|
|
111
|
-
const renamed = [];
|
|
112
|
-
for (const line of String(nameStatus ?? '').split('\n')) {
|
|
113
|
-
const trimmed = line.trim();
|
|
114
|
-
if (!trimmed) continue;
|
|
115
|
-
const [code, rest] = splitFirstTab(trimmed);
|
|
116
|
-
const paths = rest.split('\t').filter(Boolean);
|
|
117
|
-
if (paths.length === 0) continue;
|
|
118
|
-
const x = code[0] ?? '';
|
|
119
|
-
const y = code[1] ?? '';
|
|
120
|
-
if (y === 'D' || x === 'D') { deleted.push(paths[paths.length - 1]); continue; }
|
|
121
|
-
if (x === 'R' || x === 'C') { renamed.push(paths[0]); modified.push(paths[paths.length - 1]); continue; }
|
|
122
|
-
modified.push(paths[paths.length - 1]);
|
|
123
|
-
}
|
|
124
|
-
const untracked = String(statusPorcelain ?? '').split('\n')
|
|
125
|
-
.map((l) => l.trim())
|
|
126
|
-
.filter((l) => l.startsWith('??'))
|
|
127
|
-
.map((l) => l.slice(2).trim())
|
|
128
|
-
.filter(Boolean);
|
|
129
|
-
return {
|
|
130
|
-
modified: [...new Set(modified)],
|
|
131
|
-
deleted: [...new Set(deleted)],
|
|
132
|
-
renamed: [...new Set(renamed)],
|
|
133
|
-
untracked: [...new Set(untracked)],
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Read-only pre-run snapshot of a workspace. Returns a git snapshot
|
|
139
|
-
* { kind:'git', status, nameStatus, stat, dirty, changes } or a degraded
|
|
140
|
-
* { kind:'no-git', reason, error } for non-repos / missing git. Never throws.
|
|
141
|
-
*/
|
|
142
|
-
export async function captureWorkspaceBaseline({ cwd, git } = {}) {
|
|
143
|
-
const runner = git ?? defaultRunner;
|
|
144
|
-
if (!cwd) return { kind: 'no-git', reason: NOT_A_GIT_REPOSITORY, error: 'workspace cwd is required' };
|
|
145
|
-
const [status, nameStatus, stat] = await Promise.all([
|
|
146
|
-
runGit(runner, ['status', '--porcelain'], { cwd }),
|
|
147
|
-
runGit(runner, ['diff', '--name-status'], { cwd }),
|
|
148
|
-
runGit(runner, ['diff', '--stat'], { cwd }),
|
|
149
|
-
]);
|
|
150
|
-
for (const r of [status, nameStatus, stat]) {
|
|
151
|
-
if (!r.ok) return { kind: 'no-git', reason: r.reason, error: r.error };
|
|
152
|
-
}
|
|
153
|
-
const changes = parseChanges(nameStatus.stdout, status.stdout);
|
|
154
|
-
return {
|
|
155
|
-
kind: 'git',
|
|
156
|
-
status: status.stdout,
|
|
157
|
-
nameStatus: nameStatus.stdout,
|
|
158
|
-
stat: stat.stdout,
|
|
159
|
-
dirty: changes.modified.length + changes.deleted.length + changes.renamed.length + changes.untracked.length > 0,
|
|
160
|
-
changes,
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/** Build the bounded, redacted patch for the changed (tracked) paths. */
|
|
165
|
-
async function buildPatch(runner, { cwd, changes, limit }) {
|
|
166
|
-
const { modified, deleted, untracked } = changes;
|
|
167
|
-
const nonSensitive = [...new Set([...modified, ...deleted])].filter((p) => !isSensitivePath(p));
|
|
168
|
-
const sensitive = [...new Set([...modified, ...deleted])].filter((p) => isSensitivePath(p));
|
|
169
|
-
const redacted = [];
|
|
170
|
-
let patch = '';
|
|
171
|
-
if (nonSensitive.length > 0) {
|
|
172
|
-
const r = await runGit(runner, ['diff', '--binary', '--', ...nonSensitive], { cwd });
|
|
173
|
-
if (!r.ok) return { failed: true, reason: r.reason, error: r.error };
|
|
174
|
-
patch += r.stdout;
|
|
175
|
-
}
|
|
176
|
-
for (const p of sensitive) {
|
|
177
|
-
redacted.push(p);
|
|
178
|
-
patch += `diff --git a/${p} b/${p}\n[REDACTED SENSITIVE FILE]\n`;
|
|
179
|
-
}
|
|
180
|
-
for (const p of untracked) {
|
|
181
|
-
if (isSensitivePath(p)) {
|
|
182
|
-
redacted.push(p);
|
|
183
|
-
patch += `[REDACTED SENSITIVE FILE: ${p} (untracked)]\n`;
|
|
184
|
-
} else {
|
|
185
|
-
patch += `[UNTRACKED FILE: ${p} (content not diffed read-only)]\n`;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
let truncated = false;
|
|
189
|
-
if (Buffer.byteLength(patch, 'utf8') > limit) {
|
|
190
|
-
patch = Buffer.from(patch, 'utf8').subarray(0, limit).toString('utf8');
|
|
191
|
-
truncated = true;
|
|
192
|
-
}
|
|
193
|
-
return { failed: false, patch, truncated, redacted };
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Read-only post-run snapshot: the after-state (status / stat / name-status)
|
|
198
|
-
* plus the bounded, redacted patch of everything the worker changed.
|
|
199
|
-
* `dirtyBaseline` records whether the workspace was already dirty before the
|
|
200
|
-
* worker started (the diff may then include pre-existing changes). Never
|
|
201
|
-
* throws; degrades to { kind:'no-git' } like captureWorkspaceBaseline.
|
|
202
|
-
*/
|
|
203
|
-
export async function captureWorkspaceDiff({ cwd, baseline, git, limit = DIFF_LIMIT } = {}) {
|
|
204
|
-
const runner = git ?? defaultRunner;
|
|
205
|
-
if (!cwd) return { kind: 'no-git', reason: NOT_A_GIT_REPOSITORY, error: 'workspace cwd is required' };
|
|
206
|
-
if (!baseline || baseline.kind !== 'git') {
|
|
207
|
-
return { kind: 'no-git', reason: baseline?.reason ?? NOT_A_GIT_REPOSITORY, error: baseline?.error };
|
|
208
|
-
}
|
|
209
|
-
const [status, nameStatus, stat] = await Promise.all([
|
|
210
|
-
runGit(runner, ['status', '--porcelain'], { cwd }),
|
|
211
|
-
runGit(runner, ['diff', '--name-status'], { cwd }),
|
|
212
|
-
runGit(runner, ['diff', '--stat'], { cwd }),
|
|
213
|
-
]);
|
|
214
|
-
for (const r of [status, nameStatus, stat]) {
|
|
215
|
-
if (!r.ok) return { kind: 'no-git', reason: r.reason, error: r.error };
|
|
216
|
-
}
|
|
217
|
-
const changes = parseChanges(nameStatus.stdout, status.stdout);
|
|
218
|
-
const patch = await buildPatch(runner, { cwd, changes, limit });
|
|
219
|
-
if (patch.failed) return { kind: 'no-git', reason: patch.reason, error: patch.error };
|
|
220
|
-
return {
|
|
221
|
-
kind: 'git',
|
|
222
|
-
status: status.stdout,
|
|
223
|
-
nameStatus: nameStatus.stdout,
|
|
224
|
-
stat: stat.stdout,
|
|
225
|
-
patch: patch.patch,
|
|
226
|
-
truncated: patch.truncated,
|
|
227
|
-
redacted: patch.redacted,
|
|
228
|
-
dirtyBaseline: baseline.dirty === true,
|
|
229
|
-
changes,
|
|
230
|
-
};
|
|
231
|
-
}
|
|
1
|
+
// Read-only workspace auditing for the auditable-delivery flow. Captures a
|
|
2
|
+
// before/after snapshot of a git working tree (status / stat / name-status)
|
|
3
|
+
// plus a bounded, redacted patch, so an orchestrator can review exactly what a
|
|
4
|
+
// worker changed before accepting the result.
|
|
5
|
+
//
|
|
6
|
+
// Discipline: strictly read-only. Only git porcelain reads are issued — never
|
|
7
|
+
// reset / stash / clean / checkout, and nothing here writes to disk. A
|
|
8
|
+
// non-git directory degrades to { kind: 'no-git' } instead of failing, and a
|
|
9
|
+
// pre-dirty workspace is flagged (dirtyBaseline) rather than hidden.
|
|
10
|
+
//
|
|
11
|
+
// The default `git` runner is replaceable with an injected runner in tests:
|
|
12
|
+
// it receives (argsArray, { cwd }) and resolves { code, stdout, stderr }.
|
|
13
|
+
|
|
14
|
+
import { execFile } from 'node:child_process';
|
|
15
|
+
import { existsSync } from 'node:fs';
|
|
16
|
+
import { win32 } from 'node:path';
|
|
17
|
+
import { promisify } from 'node:util';
|
|
18
|
+
|
|
19
|
+
const execFileAsync = promisify(execFile);
|
|
20
|
+
|
|
21
|
+
export const DIFF_LIMIT = 64 * 1024;
|
|
22
|
+
export const NOT_A_GIT_REPOSITORY = 'NOT_A_GIT_REPOSITORY';
|
|
23
|
+
export const GIT_NOT_FOUND = 'GIT_NOT_FOUND';
|
|
24
|
+
export const GIT_TIMEOUT = 'GIT_TIMEOUT';
|
|
25
|
+
export const GIT_TIMEOUT_MS = 8000;
|
|
26
|
+
|
|
27
|
+
const SENSITIVE_SUFFIXES = ['.pem', '.key'];
|
|
28
|
+
const SENSITIVE_PREFIXES = ['credentials', 'secret'];
|
|
29
|
+
const ENV_BASENAME = '.env';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* True when a path looks like a credential/secret file whose patch content
|
|
33
|
+
* must be redacted: .env / .env.*, credentials*, secrets*, *.pem, *.key.
|
|
34
|
+
* The file's name-status entry stays visible — only the patch is hidden.
|
|
35
|
+
*/
|
|
36
|
+
export function isSensitivePath(relPath) {
|
|
37
|
+
const segments = String(relPath ?? '').replace(/\\/g, '/').split('/');
|
|
38
|
+
return segments.some((raw) => {
|
|
39
|
+
const seg = raw.toLowerCase();
|
|
40
|
+
if (seg === ENV_BASENAME || seg.startsWith(`${ENV_BASENAME}.`)) return true;
|
|
41
|
+
if (SENSITIVE_PREFIXES.some((p) => seg.startsWith(p))) return true;
|
|
42
|
+
return SENSITIVE_SUFFIXES.some((s) => seg.endsWith(s));
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let resolvedGitExecutable = 'git';
|
|
47
|
+
|
|
48
|
+
export async function resolveWindowsGit({ exec = execFileAsync, env = process.env, exists = existsSync } = {}) {
|
|
49
|
+
try {
|
|
50
|
+
const located = await exec('where.exe', ['git'], { encoding: 'utf8', timeout: GIT_TIMEOUT_MS });
|
|
51
|
+
const first = String(located.stdout ?? '').split(/\r?\n/).map((line) => line.trim()).find(Boolean);
|
|
52
|
+
if (first) return first;
|
|
53
|
+
} catch {}
|
|
54
|
+
// Always compose candidate paths with Windows semantics. This helper is
|
|
55
|
+
// unit-tested on non-Windows hosts too, and host-native path.join() would
|
|
56
|
+
// otherwise turn C:\\... inputs into mixed/invalid paths on POSIX.
|
|
57
|
+
const candidates = [
|
|
58
|
+
env.ProgramFiles && win32.join(env.ProgramFiles, 'Git', 'cmd', 'git.exe'),
|
|
59
|
+
env.ProgramFiles && win32.join(env.ProgramFiles, 'Git', 'bin', 'git.exe'),
|
|
60
|
+
env.LOCALAPPDATA && win32.join(env.LOCALAPPDATA, 'Programs', 'Git', 'cmd', 'git.exe'),
|
|
61
|
+
].filter(Boolean);
|
|
62
|
+
return candidates.find((candidate) => exists(candidate));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function defaultRunner(args, { cwd }) {
|
|
66
|
+
let out;
|
|
67
|
+
try {
|
|
68
|
+
out = await execFileAsync(resolvedGitExecutable, args, { cwd, encoding: 'utf8', maxBuffer: 8 * 1024 * 1024, timeout: GIT_TIMEOUT_MS });
|
|
69
|
+
} catch (error) {
|
|
70
|
+
const missing = error?.code === 'ENOENT' || /spawn git ENOENT/i.test(error?.message ?? '');
|
|
71
|
+
if (!missing || process.platform !== 'win32' || resolvedGitExecutable !== 'git') throw error;
|
|
72
|
+
const fallback = await resolveWindowsGit();
|
|
73
|
+
if (!fallback) throw error;
|
|
74
|
+
resolvedGitExecutable = fallback;
|
|
75
|
+
out = await execFileAsync(resolvedGitExecutable, args, { cwd, encoding: 'utf8', maxBuffer: 8 * 1024 * 1024, timeout: GIT_TIMEOUT_MS });
|
|
76
|
+
}
|
|
77
|
+
return { code: 0, stdout: out.stdout ?? '', stderr: out.stderr ?? '' };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Run one git read; normalize failure into a no-git reason, never throws. */
|
|
81
|
+
async function runGit(runner, args, opts) {
|
|
82
|
+
try {
|
|
83
|
+
const r = await runner(args, opts);
|
|
84
|
+
const stderr = r.stderr ?? '';
|
|
85
|
+
if (/not a git repository/i.test(stderr)) {
|
|
86
|
+
return { ok: false, reason: NOT_A_GIT_REPOSITORY, error: stderr.trim() };
|
|
87
|
+
}
|
|
88
|
+
return { ok: true, code: r.code ?? 0, stdout: r.stdout ?? '', stderr };
|
|
89
|
+
} catch (err) {
|
|
90
|
+
const msg = err?.message ?? String(err);
|
|
91
|
+
if (err?.code === 'ETIMEDOUT' || err?.killed === true || /timed out|timeout/i.test(msg)) return { ok: false, reason: GIT_TIMEOUT, error: 'git audit timed out' };
|
|
92
|
+
if (/ENOENT|spawn git/i.test(msg)) return { ok: false, reason: GIT_NOT_FOUND, error: msg };
|
|
93
|
+
return { ok: false, reason: NOT_A_GIT_REPOSITORY, error: msg };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function splitFirstTab(line) {
|
|
98
|
+
const i = line.indexOf('\t');
|
|
99
|
+
return i === -1 ? [line, ''] : [line.slice(0, i), line.slice(i + 1)];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Parse `git diff --name-status` + `git status --porcelain` into structured
|
|
104
|
+
* change lists used by the patch builder. Renames/copies fold onto their
|
|
105
|
+
* destination path (the diff shows them there); untracked `??` rows are listed
|
|
106
|
+
* separately because git does not diff them read-only.
|
|
107
|
+
*/
|
|
108
|
+
export function parseChanges(nameStatus, statusPorcelain) {
|
|
109
|
+
const modified = [];
|
|
110
|
+
const deleted = [];
|
|
111
|
+
const renamed = [];
|
|
112
|
+
for (const line of String(nameStatus ?? '').split('\n')) {
|
|
113
|
+
const trimmed = line.trim();
|
|
114
|
+
if (!trimmed) continue;
|
|
115
|
+
const [code, rest] = splitFirstTab(trimmed);
|
|
116
|
+
const paths = rest.split('\t').filter(Boolean);
|
|
117
|
+
if (paths.length === 0) continue;
|
|
118
|
+
const x = code[0] ?? '';
|
|
119
|
+
const y = code[1] ?? '';
|
|
120
|
+
if (y === 'D' || x === 'D') { deleted.push(paths[paths.length - 1]); continue; }
|
|
121
|
+
if (x === 'R' || x === 'C') { renamed.push(paths[0]); modified.push(paths[paths.length - 1]); continue; }
|
|
122
|
+
modified.push(paths[paths.length - 1]);
|
|
123
|
+
}
|
|
124
|
+
const untracked = String(statusPorcelain ?? '').split('\n')
|
|
125
|
+
.map((l) => l.trim())
|
|
126
|
+
.filter((l) => l.startsWith('??'))
|
|
127
|
+
.map((l) => l.slice(2).trim())
|
|
128
|
+
.filter(Boolean);
|
|
129
|
+
return {
|
|
130
|
+
modified: [...new Set(modified)],
|
|
131
|
+
deleted: [...new Set(deleted)],
|
|
132
|
+
renamed: [...new Set(renamed)],
|
|
133
|
+
untracked: [...new Set(untracked)],
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Read-only pre-run snapshot of a workspace. Returns a git snapshot
|
|
139
|
+
* { kind:'git', status, nameStatus, stat, dirty, changes } or a degraded
|
|
140
|
+
* { kind:'no-git', reason, error } for non-repos / missing git. Never throws.
|
|
141
|
+
*/
|
|
142
|
+
export async function captureWorkspaceBaseline({ cwd, git } = {}) {
|
|
143
|
+
const runner = git ?? defaultRunner;
|
|
144
|
+
if (!cwd) return { kind: 'no-git', reason: NOT_A_GIT_REPOSITORY, error: 'workspace cwd is required' };
|
|
145
|
+
const [status, nameStatus, stat] = await Promise.all([
|
|
146
|
+
runGit(runner, ['status', '--porcelain'], { cwd }),
|
|
147
|
+
runGit(runner, ['diff', '--name-status'], { cwd }),
|
|
148
|
+
runGit(runner, ['diff', '--stat'], { cwd }),
|
|
149
|
+
]);
|
|
150
|
+
for (const r of [status, nameStatus, stat]) {
|
|
151
|
+
if (!r.ok) return { kind: 'no-git', reason: r.reason, error: r.error };
|
|
152
|
+
}
|
|
153
|
+
const changes = parseChanges(nameStatus.stdout, status.stdout);
|
|
154
|
+
return {
|
|
155
|
+
kind: 'git',
|
|
156
|
+
status: status.stdout,
|
|
157
|
+
nameStatus: nameStatus.stdout,
|
|
158
|
+
stat: stat.stdout,
|
|
159
|
+
dirty: changes.modified.length + changes.deleted.length + changes.renamed.length + changes.untracked.length > 0,
|
|
160
|
+
changes,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Build the bounded, redacted patch for the changed (tracked) paths. */
|
|
165
|
+
async function buildPatch(runner, { cwd, changes, limit }) {
|
|
166
|
+
const { modified, deleted, untracked } = changes;
|
|
167
|
+
const nonSensitive = [...new Set([...modified, ...deleted])].filter((p) => !isSensitivePath(p));
|
|
168
|
+
const sensitive = [...new Set([...modified, ...deleted])].filter((p) => isSensitivePath(p));
|
|
169
|
+
const redacted = [];
|
|
170
|
+
let patch = '';
|
|
171
|
+
if (nonSensitive.length > 0) {
|
|
172
|
+
const r = await runGit(runner, ['diff', '--binary', '--', ...nonSensitive], { cwd });
|
|
173
|
+
if (!r.ok) return { failed: true, reason: r.reason, error: r.error };
|
|
174
|
+
patch += r.stdout;
|
|
175
|
+
}
|
|
176
|
+
for (const p of sensitive) {
|
|
177
|
+
redacted.push(p);
|
|
178
|
+
patch += `diff --git a/${p} b/${p}\n[REDACTED SENSITIVE FILE]\n`;
|
|
179
|
+
}
|
|
180
|
+
for (const p of untracked) {
|
|
181
|
+
if (isSensitivePath(p)) {
|
|
182
|
+
redacted.push(p);
|
|
183
|
+
patch += `[REDACTED SENSITIVE FILE: ${p} (untracked)]\n`;
|
|
184
|
+
} else {
|
|
185
|
+
patch += `[UNTRACKED FILE: ${p} (content not diffed read-only)]\n`;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
let truncated = false;
|
|
189
|
+
if (Buffer.byteLength(patch, 'utf8') > limit) {
|
|
190
|
+
patch = Buffer.from(patch, 'utf8').subarray(0, limit).toString('utf8');
|
|
191
|
+
truncated = true;
|
|
192
|
+
}
|
|
193
|
+
return { failed: false, patch, truncated, redacted };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Read-only post-run snapshot: the after-state (status / stat / name-status)
|
|
198
|
+
* plus the bounded, redacted patch of everything the worker changed.
|
|
199
|
+
* `dirtyBaseline` records whether the workspace was already dirty before the
|
|
200
|
+
* worker started (the diff may then include pre-existing changes). Never
|
|
201
|
+
* throws; degrades to { kind:'no-git' } like captureWorkspaceBaseline.
|
|
202
|
+
*/
|
|
203
|
+
export async function captureWorkspaceDiff({ cwd, baseline, git, limit = DIFF_LIMIT } = {}) {
|
|
204
|
+
const runner = git ?? defaultRunner;
|
|
205
|
+
if (!cwd) return { kind: 'no-git', reason: NOT_A_GIT_REPOSITORY, error: 'workspace cwd is required' };
|
|
206
|
+
if (!baseline || baseline.kind !== 'git') {
|
|
207
|
+
return { kind: 'no-git', reason: baseline?.reason ?? NOT_A_GIT_REPOSITORY, error: baseline?.error };
|
|
208
|
+
}
|
|
209
|
+
const [status, nameStatus, stat] = await Promise.all([
|
|
210
|
+
runGit(runner, ['status', '--porcelain'], { cwd }),
|
|
211
|
+
runGit(runner, ['diff', '--name-status'], { cwd }),
|
|
212
|
+
runGit(runner, ['diff', '--stat'], { cwd }),
|
|
213
|
+
]);
|
|
214
|
+
for (const r of [status, nameStatus, stat]) {
|
|
215
|
+
if (!r.ok) return { kind: 'no-git', reason: r.reason, error: r.error };
|
|
216
|
+
}
|
|
217
|
+
const changes = parseChanges(nameStatus.stdout, status.stdout);
|
|
218
|
+
const patch = await buildPatch(runner, { cwd, changes, limit });
|
|
219
|
+
if (patch.failed) return { kind: 'no-git', reason: patch.reason, error: patch.error };
|
|
220
|
+
return {
|
|
221
|
+
kind: 'git',
|
|
222
|
+
status: status.stdout,
|
|
223
|
+
nameStatus: nameStatus.stdout,
|
|
224
|
+
stat: stat.stdout,
|
|
225
|
+
patch: patch.patch,
|
|
226
|
+
truncated: patch.truncated,
|
|
227
|
+
redacted: patch.redacted,
|
|
228
|
+
dirtyBaseline: baseline.dirty === true,
|
|
229
|
+
changes,
|
|
230
|
+
};
|
|
231
|
+
}
|