code-guardian-ai 0.0.0-stage → 0.3.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/.env.example +24 -0
- package/LICENSE +21 -0
- package/README.md +774 -2
- package/bin/cli.js +2 -0
- package/dist/build-manifest.json +136 -0
- package/dist/cli/commands/check.js +56 -0
- package/dist/cli/commands/doctor.js +55 -0
- package/dist/cli/commands/find.js +31 -0
- package/dist/cli/commands/init-files.js +13 -0
- package/dist/cli/commands/init.js +50 -0
- package/dist/cli/commands/optimize.js +53 -0
- package/dist/cli/commands/scan.js +47 -0
- package/dist/cli/index.js +62 -0
- package/dist/cli/output.js +4 -0
- package/dist/context.js +11 -0
- package/dist/core/ast/analysis.js +455 -0
- package/dist/core/ast/cached-info.js +46 -0
- package/dist/core/ast/discovery.js +485 -0
- package/dist/core/ast/extract-js.js +531 -0
- package/dist/core/ast/extract-py.js +298 -0
- package/dist/core/ast/graph.js +92 -0
- package/dist/core/ast/health.js +338 -0
- package/dist/core/ast/indexer.js +286 -0
- package/dist/core/ast/packages.js +343 -0
- package/dist/core/ast/parser.js +91 -0
- package/dist/core/ast/report.js +154 -0
- package/dist/core/ast/resolver.js +392 -0
- package/dist/core/ast/rules.js +37 -0
- package/dist/core/ast/scope-js.js +191 -0
- package/dist/core/ast/shared-index.js +131 -0
- package/dist/core/ast/tsconfig.js +301 -0
- package/dist/core/ast/types.js +1 -0
- package/dist/core/ast/util.js +156 -0
- package/dist/core/git/git.js +430 -0
- package/dist/core/llm/policy.js +35 -0
- package/dist/core/llm/provider.js +199 -0
- package/dist/core/prompt/compressor.js +118 -0
- package/dist/core/prompt/criteria.js +36 -0
- package/dist/core/prompt/find.js +47 -0
- package/dist/core/prompt/guidelines.js +22 -0
- package/dist/core/prompt/model-output.js +178 -0
- package/dist/core/prompt/refiner.js +194 -0
- package/dist/core/prompt/relevance.js +265 -0
- package/dist/core/prompt/untrusted.js +57 -0
- package/dist/core/security/masker.js +330 -0
- package/dist/core/security/outbound.js +40 -0
- package/dist/core/security/safe-file.js +396 -0
- package/dist/core/security/sandbox.js +181 -0
- package/dist/core/validation/baseline.js +121 -0
- package/dist/core/validation/delta.js +243 -0
- package/dist/core/validation/policy.js +123 -0
- package/dist/db/cache-key.js +221 -0
- package/dist/db/client.js +563 -0
- package/dist/db/schema-check.js +149 -0
- package/dist/db/schema.js +36 -0
- package/dist/db/windows-acl.js +150 -0
- package/dist/index.js +55 -0
- package/dist/mcp/server.js +20 -0
- package/dist/mcp/tools/find-reusable.js +22 -0
- package/dist/mcp/tools/get-context.js +39 -0
- package/dist/mcp/tools/optimize-prompt.js +31 -0
- package/dist/mcp/tools/result.js +31 -0
- package/dist/mcp/tools/validate-rules.js +28 -0
- package/dist/utils/config.js +117 -0
- package/dist/utils/errors.js +9 -0
- package/dist/utils/logger.js +19 -0
- package/dist/utils/pace.js +19 -0
- package/dist/utils/text.js +22 -0
- package/dist/utils/tokens.js +14 -0
- package/package.json +69 -4
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import { execFile, execFileSync, spawn, spawnSync } from 'node:child_process';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
import { InputError, errorCode } from '../../utils/errors.js';
|
|
5
|
+
import { log } from '../../utils/logger.js';
|
|
6
|
+
const run = promisify(execFile);
|
|
7
|
+
const REF = /^[A-Za-z0-9][\w./@{}~^-]{0,199}$/;
|
|
8
|
+
const TIMEOUT_MS = 20_000;
|
|
9
|
+
const MAX_BUFFER = 32 * 1024 * 1024;
|
|
10
|
+
const TASKKILL_TIMEOUT_MS = 3_000;
|
|
11
|
+
// After taskkill reports success the launcher must actually be gone; this is how long that is awaited before the cleanup is called degraded.
|
|
12
|
+
const TREE_EXIT_WAIT_MS = 1_000;
|
|
13
|
+
// When taskkill could not end the tree, only the launcher is killed (see killProcessTree). A descendant that is still running holds the
|
|
14
|
+
// pipes execFile waits on, so the pending call would not settle until it ends. Windows callers are released this long after a
|
|
15
|
+
// kill was requested: the taskkill timeout, the wait for the launcher to exit, and a margin.
|
|
16
|
+
const RELEASE_GRACE_MS = TASKKILL_TIMEOUT_MS + TREE_EXIT_WAIT_MS + 1_000;
|
|
17
|
+
// Repo config can name programs to run. fsmonitor, external diff and textconv are disabled on every call. Filter drivers
|
|
18
|
+
// (`filter.<name>.clean|smudge|process`) cannot be switched off from the command line, and `git diff <commit>` runs the clean
|
|
19
|
+
// filter of every file whose attributes name one, so `assertNoRepositoryCommands` refuses a repository that defines one.
|
|
20
|
+
const SAFE_ARGS = ['-c', 'core.fsmonitor=false', '-c', 'core.quotepath=false'];
|
|
21
|
+
const TASKKILL_NOT_FOUND = 128;
|
|
22
|
+
const isGone = (child) => child.exitCode !== null || child.signalCode !== null;
|
|
23
|
+
const taskkillExecutable = () => path.join(process.env.SystemRoot || 'C:\\Windows', 'System32', 'taskkill.exe');
|
|
24
|
+
const reasonOf = (err) => errorCode(err) ?? (err instanceof Error ? err.message : String(err));
|
|
25
|
+
// The launcher is killed directly when the tree could not be ended. A failure of that kill is part of the report, not swallowed.
|
|
26
|
+
function killLauncher(child) {
|
|
27
|
+
try {
|
|
28
|
+
return child.kill('SIGTERM') ? 'the launcher was killed' : 'the launcher could not be signalled (it may already have ended)';
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
return `killing the launcher failed (${reasonOf(err)})`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function degraded(child, why) {
|
|
35
|
+
return { pid: child.pid ?? -1, outcome: 'degraded', detail: `${why}; ${killLauncher(child)}, processes it started may still be running` };
|
|
36
|
+
}
|
|
37
|
+
function waitForExit(child, ms) {
|
|
38
|
+
if (isGone(child))
|
|
39
|
+
return Promise.resolve(true);
|
|
40
|
+
return new Promise((resolve) => {
|
|
41
|
+
const timer = setTimeout(() => finish(false), ms);
|
|
42
|
+
const finish = (gone) => {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
child.off('exit', onExit);
|
|
45
|
+
resolve(gone);
|
|
46
|
+
};
|
|
47
|
+
const onExit = () => finish(true);
|
|
48
|
+
child.once('exit', onExit);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
// Asynchronous: for cancellation and timeouts, where the event loop keeps running.
|
|
52
|
+
export function killProcessTree(child, options = {}) {
|
|
53
|
+
const pid = child.pid;
|
|
54
|
+
if (pid === undefined || isGone(child))
|
|
55
|
+
return Promise.resolve({ pid: pid ?? -1, outcome: 'exited-before-kill', detail: 'the process had already ended' });
|
|
56
|
+
if ((options.platform ?? process.platform) !== 'win32') {
|
|
57
|
+
child.kill('SIGTERM');
|
|
58
|
+
return Promise.resolve({ pid, outcome: 'process-signalled', detail: 'SIGTERM sent to the process itself' });
|
|
59
|
+
}
|
|
60
|
+
const exitWait = options.exitWaitMs ?? TREE_EXIT_WAIT_MS;
|
|
61
|
+
return new Promise((resolve) => {
|
|
62
|
+
let settled = false;
|
|
63
|
+
const done = (result) => {
|
|
64
|
+
if (settled)
|
|
65
|
+
return;
|
|
66
|
+
settled = true;
|
|
67
|
+
resolve(result);
|
|
68
|
+
};
|
|
69
|
+
let killer;
|
|
70
|
+
try {
|
|
71
|
+
// A taskkill that hangs must not leave the tree running with no fallback: spawn's timeout ends it, it closes with a null code,
|
|
72
|
+
// and that is handled like any other failure.
|
|
73
|
+
killer = spawn(options.taskkillPath ?? taskkillExecutable(), ['/pid', String(pid), '/t', '/f'], { stdio: 'ignore', windowsHide: true, timeout: options.taskkillTimeoutMs ?? TASKKILL_TIMEOUT_MS });
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
done(degraded(child, `taskkill could not be started (${reasonOf(err)})`));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
killer.once('error', (err) => done(degraded(child, `taskkill could not be run (${reasonOf(err)})`)));
|
|
80
|
+
killer.once('close', (code, signal) => {
|
|
81
|
+
if (settled)
|
|
82
|
+
return;
|
|
83
|
+
if (code === 0) {
|
|
84
|
+
// Success is only reported once the launcher is really gone.
|
|
85
|
+
void waitForExit(child, exitWait).then((gone) => done(gone ? { pid, outcome: 'tree-terminated', detail: 'taskkill /T /F reported success and the launcher ended' } : degraded(child, 'taskkill reported success but the launcher is still running')));
|
|
86
|
+
}
|
|
87
|
+
else if (code === TASKKILL_NOT_FOUND && (isGone(child) || !(options.isAlive ?? pidAlive)(pid))) {
|
|
88
|
+
done({ pid, outcome: 'exited-before-kill', detail: 'the process ended before taskkill reached it' });
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
done(degraded(child, code === null ? `taskkill did not finish (${signal ?? 'terminated'}; timeout ${options.taskkillTimeoutMs ?? TASKKILL_TIMEOUT_MS} ms)` : `taskkill failed with exit code ${code}`));
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
killer.unref();
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const sleepSync = (ms) => void Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
98
|
+
const pidAlive = (pid) => {
|
|
99
|
+
try {
|
|
100
|
+
process.kill(pid, 0);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
return errorCode(err) === 'EPERM';
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
// Synchronous: for shutdown, where the process exits right after and nothing could wait for an asynchronous taskkill.
|
|
108
|
+
export function killProcessTreeSync(child, options = {}) {
|
|
109
|
+
const pid = child.pid;
|
|
110
|
+
if (pid === undefined || isGone(child))
|
|
111
|
+
return { pid: pid ?? -1, outcome: 'exited-before-kill', detail: 'the process had already ended' };
|
|
112
|
+
if ((options.platform ?? process.platform) !== 'win32') {
|
|
113
|
+
child.kill('SIGTERM');
|
|
114
|
+
return { pid, outcome: 'process-signalled', detail: 'SIGTERM sent to the process itself' };
|
|
115
|
+
}
|
|
116
|
+
const timeout = options.taskkillTimeoutMs ?? TASKKILL_TIMEOUT_MS;
|
|
117
|
+
const result = spawnSync(options.taskkillPath ?? taskkillExecutable(), ['/pid', String(pid), '/t', '/f'], { stdio: 'ignore', windowsHide: true, timeout });
|
|
118
|
+
if (result.error)
|
|
119
|
+
return degraded(child, `taskkill could not be run or timed out (${reasonOf(result.error)})`);
|
|
120
|
+
const alive = options.isAlive ?? pidAlive;
|
|
121
|
+
// While this function blocks, Node cannot update `exitCode`, so an ended launcher is also recognised by its pid no longer existing.
|
|
122
|
+
if (result.status === TASKKILL_NOT_FOUND && (isGone(child) || !alive(pid)))
|
|
123
|
+
return { pid, outcome: 'exited-before-kill', detail: 'the process ended before taskkill reached it' };
|
|
124
|
+
if (result.status !== 0)
|
|
125
|
+
return degraded(child, result.status === null ? `taskkill did not finish (${result.signal ?? 'terminated'})` : `taskkill failed with exit code ${result.status}`);
|
|
126
|
+
// The 'exit' event cannot be delivered while this function blocks, so the launcher is looked for by pid, right after taskkill ended it.
|
|
127
|
+
const deadline = Date.now() + (options.exitWaitMs ?? TREE_EXIT_WAIT_MS);
|
|
128
|
+
while (alive(pid) && Date.now() < deadline)
|
|
129
|
+
sleepSync(25);
|
|
130
|
+
return alive(pid) ? degraded(child, 'taskkill reported success but the launcher is still running') : { pid, outcome: 'tree-terminated', detail: 'taskkill /T /F reported success and the launcher ended' };
|
|
131
|
+
}
|
|
132
|
+
const MAX_REPORTS = 20;
|
|
133
|
+
const degradedReports = [];
|
|
134
|
+
// The most recent cleanups that could not be confirmed (newest last, bounded). Empty means none happened, not that nothing was missed.
|
|
135
|
+
export const degradedCleanups = () => [...degradedReports];
|
|
136
|
+
function record(result) {
|
|
137
|
+
if (result.outcome === 'degraded') {
|
|
138
|
+
degradedReports.push(result);
|
|
139
|
+
if (degradedReports.length > MAX_REPORTS)
|
|
140
|
+
degradedReports.shift();
|
|
141
|
+
log.warn(`Windows git cleanup degraded (pid ${result.pid}): ${result.detail}`);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
log.debug(`git process cleanup: ${result.outcome} (pid ${result.pid})`);
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
// Every asynchronous git process that is still alive. A per-request signal kills its own process, but when the whole server shuts
|
|
149
|
+
// down (a signal, the client closing the pipe, a fatal error) nothing is left to enforce the call's timeout, so a git process that is
|
|
150
|
+
// still running would outlive the server. `terminateGitChildren` ends them. A process stays here until it has really exited, not until
|
|
151
|
+
// its caller was released: a launcher that could not be killed must still be found by shutdown.
|
|
152
|
+
const running = new Set();
|
|
153
|
+
// One kill attempt at a time per process (a cancellation and the timeout can both ask), and the last outcome for the error text.
|
|
154
|
+
const killing = new Map();
|
|
155
|
+
const lastCleanup = new WeakMap();
|
|
156
|
+
function requestKill(child, options = {}) {
|
|
157
|
+
const existing = killing.get(child);
|
|
158
|
+
if (existing)
|
|
159
|
+
return existing;
|
|
160
|
+
const attempt = killProcessTree(child, options).then((result) => {
|
|
161
|
+
lastCleanup.set(child, result);
|
|
162
|
+
return record(result);
|
|
163
|
+
});
|
|
164
|
+
killing.set(child, attempt);
|
|
165
|
+
void attempt.finally(() => killing.delete(child));
|
|
166
|
+
return attempt;
|
|
167
|
+
}
|
|
168
|
+
// The signal is handled here and not passed to execFile: Node's own abort handler is registered first and would end the
|
|
169
|
+
// launcher before the tree is killed. The pending call then rejects, and every caller turns that into the signal's reason.
|
|
170
|
+
// On Windows the timeout is enforced here too, for the same reason (execFile's timeout also ends only the launcher).
|
|
171
|
+
async function tracked(pending, signal, killOptions = {}) {
|
|
172
|
+
const { child } = pending;
|
|
173
|
+
running.add(child);
|
|
174
|
+
child.once('exit', () => running.delete(child));
|
|
175
|
+
const windows = process.platform === 'win32';
|
|
176
|
+
let release;
|
|
177
|
+
const released = windows ? new Promise((_, reject) => (release = reject)) : undefined;
|
|
178
|
+
let graceTimer;
|
|
179
|
+
const stop = (reason) => {
|
|
180
|
+
void requestKill(child, killOptions);
|
|
181
|
+
if (!release || graceTimer)
|
|
182
|
+
return;
|
|
183
|
+
// If the tree is gone, `pending` settles first and this never fires.
|
|
184
|
+
graceTimer = setTimeout(() => release?.(reason()), RELEASE_GRACE_MS);
|
|
185
|
+
graceTimer.unref();
|
|
186
|
+
};
|
|
187
|
+
const onAbort = () => stop(() => signal?.reason ?? new Error('git was cancelled'));
|
|
188
|
+
if (signal?.aborted)
|
|
189
|
+
onAbort();
|
|
190
|
+
else
|
|
191
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
192
|
+
const timer = windows
|
|
193
|
+
? setTimeout(() => stop(() => {
|
|
194
|
+
const cleanup = lastCleanup.get(child);
|
|
195
|
+
return new Error(cleanup?.outcome === 'degraded' ? `git timed out (process-tree cleanup degraded: ${cleanup.detail})` : 'git timed out');
|
|
196
|
+
}), TIMEOUT_MS - 500)
|
|
197
|
+
: undefined;
|
|
198
|
+
timer?.unref();
|
|
199
|
+
try {
|
|
200
|
+
if (!released)
|
|
201
|
+
return await pending;
|
|
202
|
+
pending.catch(() => undefined); // when released first, a later rejection of the abandoned call is not an unhandled one
|
|
203
|
+
return await Promise.race([pending, released]);
|
|
204
|
+
}
|
|
205
|
+
finally {
|
|
206
|
+
clearTimeout(timer);
|
|
207
|
+
clearTimeout(graceTimer);
|
|
208
|
+
signal?.removeEventListener('abort', onAbort);
|
|
209
|
+
// A call that settled has a process that ended; one that was released may still have a running launcher, which stays tracked.
|
|
210
|
+
if (child.pid === undefined || isGone(child))
|
|
211
|
+
running.delete(child);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Ends every running git process, synchronously. The summary says what was confirmed: `degraded` counts processes whose tree could not
|
|
215
|
+
// be confirmed ended (also logged and kept in `degradedCleanups()`).
|
|
216
|
+
export function terminateGitChildren(options = {}) {
|
|
217
|
+
const summary = { requested: 0, treeTerminated: 0, exitedBeforeKill: 0, processSignalled: 0, degraded: 0 };
|
|
218
|
+
for (const child of [...running]) {
|
|
219
|
+
const result = record(killProcessTreeSync(child, options));
|
|
220
|
+
summary.requested++;
|
|
221
|
+
if (result.outcome === 'tree-terminated')
|
|
222
|
+
summary.treeTerminated++;
|
|
223
|
+
else if (result.outcome === 'exited-before-kill')
|
|
224
|
+
summary.exitedBeforeKill++;
|
|
225
|
+
else if (result.outcome === 'process-signalled')
|
|
226
|
+
summary.processSignalled++;
|
|
227
|
+
else
|
|
228
|
+
summary.degraded++;
|
|
229
|
+
}
|
|
230
|
+
return summary;
|
|
231
|
+
}
|
|
232
|
+
function gitEnv() {
|
|
233
|
+
const env = {};
|
|
234
|
+
for (const [name, value] of Object.entries(process.env))
|
|
235
|
+
if (!name.startsWith('GIT_'))
|
|
236
|
+
env[name] = value;
|
|
237
|
+
return { ...env, GIT_TERMINAL_PROMPT: '0', GIT_OPTIONAL_LOCKS: '0', GIT_PAGER: 'cat', LC_ALL: 'C' };
|
|
238
|
+
}
|
|
239
|
+
// `signal` kills the git process when the caller cancels (an MCP client that gave up). A cancelled call throws the signal's
|
|
240
|
+
// reason; it is never turned into "git failed" or "file unreadable", which would be read as a result.
|
|
241
|
+
async function git(root, args, signal, maxBuffer = MAX_BUFFER, killOptions = {}) {
|
|
242
|
+
signal?.throwIfAborted();
|
|
243
|
+
try {
|
|
244
|
+
const { stdout } = await tracked(run('git', [...SAFE_ARGS, ...args], {
|
|
245
|
+
cwd: root,
|
|
246
|
+
env: gitEnv(),
|
|
247
|
+
timeout: TIMEOUT_MS,
|
|
248
|
+
maxBuffer,
|
|
249
|
+
encoding: 'utf8',
|
|
250
|
+
}), signal, killOptions);
|
|
251
|
+
return stdout;
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
signal?.throwIfAborted();
|
|
255
|
+
throw err;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const tryGit = (root, args, signal, killOptions = {}) => git(root, args, signal, MAX_BUFFER, killOptions).catch(() => {
|
|
259
|
+
signal?.throwIfAborted();
|
|
260
|
+
return null;
|
|
261
|
+
});
|
|
262
|
+
export async function detectRepo(root, signal, killOptions = {}) {
|
|
263
|
+
try {
|
|
264
|
+
await git(root, ['rev-parse', '--is-inside-work-tree'], signal, MAX_BUFFER, killOptions);
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
signal?.throwIfAborted();
|
|
268
|
+
return { ok: false, reason: errorCode(err) === 'ENOENT' ? 'git-not-found' : 'not-a-repository' };
|
|
269
|
+
}
|
|
270
|
+
const head = await tryGit(root, ['rev-parse', '--verify', '--quiet', 'HEAD^{commit}'], signal, killOptions);
|
|
271
|
+
return { ok: true, head: head?.trim() || null };
|
|
272
|
+
}
|
|
273
|
+
const STATUS = { A: 'added', M: 'modified', D: 'deleted', T: 'modified', R: 'renamed', C: 'added' };
|
|
274
|
+
// `git diff --name-status -z` output: STATUS NUL path NUL, with two paths for renames and copies.
|
|
275
|
+
export function parseNameStatus(raw) {
|
|
276
|
+
const tokens = raw.split('\0');
|
|
277
|
+
const changes = [];
|
|
278
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
279
|
+
const code = tokens[i];
|
|
280
|
+
if (!code)
|
|
281
|
+
continue;
|
|
282
|
+
const status = STATUS[code[0]];
|
|
283
|
+
if (!status)
|
|
284
|
+
continue;
|
|
285
|
+
if (code[0] === 'R' || code[0] === 'C') {
|
|
286
|
+
const oldPath = tokens[++i];
|
|
287
|
+
const newPath = tokens[++i];
|
|
288
|
+
if (newPath)
|
|
289
|
+
changes.push(code[0] === 'R' ? { status, path: newPath, oldPath } : { status, path: newPath });
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
const file = tokens[++i];
|
|
293
|
+
if (file)
|
|
294
|
+
changes.push({ status, path: file });
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return changes;
|
|
298
|
+
}
|
|
299
|
+
const splitNul = (raw) => raw.split('\0').filter(Boolean);
|
|
300
|
+
function isSafeRelative(p) {
|
|
301
|
+
const normalized = path.posix.normalize(p);
|
|
302
|
+
return !path.posix.isAbsolute(normalized) && normalized !== '..' && !normalized.startsWith('../');
|
|
303
|
+
}
|
|
304
|
+
// Commands a repository can make git run while it compares the working tree with a commit. Only the repository's own
|
|
305
|
+
// configuration is looked at (local and worktree scope, includes followed): the user's global and system configuration is
|
|
306
|
+
// trusted. These are exactly what Git for LFS installs; any other filter command is refused instead of being executed.
|
|
307
|
+
const LFS_FILTERS = new Set(['git-lfs clean -- %f', 'git-lfs smudge -- %f', 'git-lfs smudge --skip -- %f', 'git-lfs filter-process', 'git-lfs filter-process --skip']);
|
|
308
|
+
const FILTER_COMMAND = /^filter\.(.+)\.(clean|smudge|process)$/i;
|
|
309
|
+
export async function assertNoRepositoryCommands(root, signal) {
|
|
310
|
+
let raw;
|
|
311
|
+
try {
|
|
312
|
+
raw = await git(root, ['config', '--list', '--show-scope', '-z'], signal);
|
|
313
|
+
}
|
|
314
|
+
catch (err) {
|
|
315
|
+
signal?.throwIfAborted();
|
|
316
|
+
// Without a readable configuration nothing can be said about what a filter would run: fail closed.
|
|
317
|
+
throw new InputError(`Cannot read the repository's git configuration (${errorCode(err) ?? 'git failed'}); git 2.26 or newer is required`, 'unsafe_repository_config');
|
|
318
|
+
}
|
|
319
|
+
// With -z and --show-scope every entry is two NUL-terminated fields: the scope, then `key\nvalue`.
|
|
320
|
+
const fields = raw.split('\0');
|
|
321
|
+
for (let i = 0; i + 1 < fields.length; i += 2) {
|
|
322
|
+
const scope = fields[i];
|
|
323
|
+
const entry = fields[i + 1];
|
|
324
|
+
const newline = entry.indexOf('\n');
|
|
325
|
+
const key = newline === -1 ? entry : entry.slice(0, newline);
|
|
326
|
+
const value = newline === -1 ? '' : entry.slice(newline + 1);
|
|
327
|
+
if (scope !== 'local' && scope !== 'worktree')
|
|
328
|
+
continue;
|
|
329
|
+
if (FILTER_COMMAND.test(key) && !LFS_FILTERS.has(value.trim())) {
|
|
330
|
+
throw new InputError(`Refusing to run git: the repository's configuration defines a filter command (${key}) that git would execute. Remove it from .git/config to continue.`, 'unsafe_repository_config');
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
export async function collectChanges(root, repo, baseRef, signal) {
|
|
335
|
+
let baseSha = repo.head;
|
|
336
|
+
let baseLabel = repo.head ? 'HEAD' : 'no commits yet';
|
|
337
|
+
if (baseRef) {
|
|
338
|
+
if (!REF.test(baseRef))
|
|
339
|
+
throw new InputError('Invalid git ref', 'invalid_input');
|
|
340
|
+
const resolved = (await tryGit(root, ['rev-parse', '--verify', '--quiet', `${baseRef}^{commit}`], signal))?.trim();
|
|
341
|
+
if (!resolved)
|
|
342
|
+
throw new InputError(`Unknown git ref: ${baseRef}`, 'invalid_input');
|
|
343
|
+
const mergeBase = repo.head ? (await tryGit(root, ['merge-base', resolved, repo.head], signal))?.trim() : undefined;
|
|
344
|
+
baseSha = mergeBase || resolved;
|
|
345
|
+
baseLabel = `${baseRef} (${baseSha.slice(0, 7)})`;
|
|
346
|
+
}
|
|
347
|
+
else if (repo.head) {
|
|
348
|
+
baseLabel = `HEAD (${repo.head.slice(0, 7)})`;
|
|
349
|
+
}
|
|
350
|
+
if (baseSha)
|
|
351
|
+
await assertNoRepositoryCommands(root, signal);
|
|
352
|
+
const tracked = baseSha
|
|
353
|
+
? parseNameStatus(await git(root, ['diff', '--name-status', '-z', '-M', '--relative', '--no-ext-diff', '--no-textconv', baseSha], signal))
|
|
354
|
+
: [];
|
|
355
|
+
const untracked = splitNul(await git(root, ['ls-files', '--others', '--exclude-standard', '-z'], signal));
|
|
356
|
+
const everything = baseSha ? [] : splitNul(await git(root, ['ls-files', '-z'], signal));
|
|
357
|
+
const byPath = new Map();
|
|
358
|
+
for (const change of tracked)
|
|
359
|
+
byPath.set(change.path, change);
|
|
360
|
+
for (const file of [...untracked, ...everything])
|
|
361
|
+
if (!byPath.has(file))
|
|
362
|
+
byPath.set(file, { status: 'added', path: file });
|
|
363
|
+
const changes = [...byPath.values()].filter((c) => isSafeRelative(c.path) && (!c.oldPath || isSafeRelative(c.oldPath)));
|
|
364
|
+
return { baseSha, baseLabel, changes: changes.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0)) };
|
|
365
|
+
}
|
|
366
|
+
export async function readBlob(root, sha, rel, maxBytes, signal) {
|
|
367
|
+
if (!isSafeRelative(rel))
|
|
368
|
+
return { ok: false, reason: 'unreadable' };
|
|
369
|
+
signal?.throwIfAborted();
|
|
370
|
+
try {
|
|
371
|
+
const { stdout } = await tracked(run('git', [...SAFE_ARGS, 'show', '--no-textconv', `${sha}:./${rel}`], {
|
|
372
|
+
cwd: root,
|
|
373
|
+
env: gitEnv(),
|
|
374
|
+
timeout: TIMEOUT_MS,
|
|
375
|
+
maxBuffer: maxBytes + 1024,
|
|
376
|
+
encoding: 'buffer',
|
|
377
|
+
}), signal);
|
|
378
|
+
return stdout.length > maxBytes ? { ok: false, reason: 'too-large' } : { ok: true, data: stdout };
|
|
379
|
+
}
|
|
380
|
+
catch (err) {
|
|
381
|
+
signal?.throwIfAborted();
|
|
382
|
+
return { ok: false, reason: errorCode(err) === 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' ? 'too-large' : 'unreadable' };
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
export async function readAtCommit(root, sha, rel, maxBytes, signal) {
|
|
386
|
+
const blob = await readBlob(root, sha, rel, maxBytes, signal);
|
|
387
|
+
return blob.ok ? blob.data : null;
|
|
388
|
+
}
|
|
389
|
+
// Reads a small file (a config) synchronously; used where the caller is itself synchronous.
|
|
390
|
+
export function readAtCommitSync(root, sha, rel, maxBytes) {
|
|
391
|
+
if (!isSafeRelative(rel))
|
|
392
|
+
return null;
|
|
393
|
+
try {
|
|
394
|
+
const stdout = execFileSync('git', [...SAFE_ARGS, 'show', '--no-textconv', `${sha}:./${rel}`], {
|
|
395
|
+
cwd: root,
|
|
396
|
+
env: gitEnv(),
|
|
397
|
+
timeout: TIMEOUT_MS,
|
|
398
|
+
maxBuffer: maxBytes + 1024,
|
|
399
|
+
encoding: 'buffer',
|
|
400
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
401
|
+
});
|
|
402
|
+
return stdout.length > maxBytes ? null : stdout;
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
// Regular files tracked at `sha`, relative to the workspace root. Symlinks and submodules are left out,
|
|
409
|
+
// the same way the working-tree walk leaves them out.
|
|
410
|
+
export async function listTreeAtCommit(root, sha, signal) {
|
|
411
|
+
let raw;
|
|
412
|
+
try {
|
|
413
|
+
raw = await git(root, ['ls-tree', '-r', '-z', sha, '--', '.'], signal);
|
|
414
|
+
}
|
|
415
|
+
catch {
|
|
416
|
+
signal?.throwIfAborted();
|
|
417
|
+
return null;
|
|
418
|
+
}
|
|
419
|
+
const files = [];
|
|
420
|
+
for (const entry of raw.split('\0')) {
|
|
421
|
+
const tab = entry.indexOf('\t');
|
|
422
|
+
if (tab === -1)
|
|
423
|
+
continue;
|
|
424
|
+
const [mode, type] = entry.slice(0, tab).split(' ');
|
|
425
|
+
const file = entry.slice(tab + 1);
|
|
426
|
+
if (type === 'blob' && (mode === '100644' || mode === '100755') && isSafeRelative(file))
|
|
427
|
+
files.push(file);
|
|
428
|
+
}
|
|
429
|
+
return files;
|
|
430
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { log } from '../../utils/logger.js';
|
|
2
|
+
import { hasLlmKeys } from './provider.js';
|
|
3
|
+
const ON = new Set(['on', 'true', '1', 'yes']);
|
|
4
|
+
const OFF = new Set(['', 'off', 'false', '0', 'no']);
|
|
5
|
+
// Fail closed: nothing leaves the machine unless GUARDIAN_LLM=on is set explicitly, the project allows it and a key exists.
|
|
6
|
+
export function resolvePolicy(config, options = {}) {
|
|
7
|
+
const raw = process.env.GUARDIAN_LLM?.trim().toLowerCase();
|
|
8
|
+
if (options.disabled)
|
|
9
|
+
return { mode: 'local', reason: 'flag-off' };
|
|
10
|
+
if (raw === undefined)
|
|
11
|
+
return { mode: 'local', reason: 'env-unset' };
|
|
12
|
+
if (!ON.has(raw)) {
|
|
13
|
+
if (!OFF.has(raw))
|
|
14
|
+
log.warn(`GUARDIAN_LLM="${raw}" is not recognised, staying fully local`);
|
|
15
|
+
return { mode: 'local', reason: 'env-off' };
|
|
16
|
+
}
|
|
17
|
+
if (!config.llm)
|
|
18
|
+
return { mode: 'local', reason: 'config-off' };
|
|
19
|
+
if (!hasLlmKeys())
|
|
20
|
+
return { mode: 'local', reason: 'no-keys' };
|
|
21
|
+
return { mode: 'external', reason: 'enabled' };
|
|
22
|
+
}
|
|
23
|
+
export function describePolicy(policy) {
|
|
24
|
+
if (policy.mode === 'external') {
|
|
25
|
+
return 'External LLM mode: the request, up to 8 matching symbol signatures with file paths, and a folder summary are sent to Gemini or Groq after secret masking. No file contents or function bodies are sent. Set GUARDIAN_LLM=off to stay fully local.';
|
|
26
|
+
}
|
|
27
|
+
const why = {
|
|
28
|
+
'env-off': 'GUARDIAN_LLM is off',
|
|
29
|
+
'env-unset': 'GUARDIAN_LLM=on is not set',
|
|
30
|
+
'config-off': '.guardianrc has "llm": false',
|
|
31
|
+
'flag-off': '--no-llm was given',
|
|
32
|
+
'no-keys': 'no API key is set',
|
|
33
|
+
};
|
|
34
|
+
return `Fully local mode: nothing leaves this machine (${why[policy.reason]}).`;
|
|
35
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { createGoogleGenerativeAI } from '@ai-sdk/google';
|
|
2
|
+
import { createOpenAI } from '@ai-sdk/openai';
|
|
3
|
+
import { APICallError, generateText } from 'ai';
|
|
4
|
+
import { log } from '../../utils/logger.js';
|
|
5
|
+
import { OutboundBlocked, prepareOutbound } from '../security/outbound.js';
|
|
6
|
+
const DEFAULT_GEMINI_MODEL = 'gemini-3.1-flash-lite';
|
|
7
|
+
const DEFAULT_GROQ_MODEL = 'openai/gpt-oss-120b';
|
|
8
|
+
const GROQ_BASE_URL = 'https://api.groq.com/openai/v1';
|
|
9
|
+
const DEFAULT_TIMEOUT_MS = 12_000;
|
|
10
|
+
const MIN_TIMEOUT_MS = 200;
|
|
11
|
+
const MAX_TIMEOUT_MS = 60_000;
|
|
12
|
+
const DEFAULT_COOLDOWN_MS = 60_000;
|
|
13
|
+
const MAX_COOLDOWN_MS = 300_000;
|
|
14
|
+
const MAX_CALLS_PER_MINUTE = 20;
|
|
15
|
+
// Repeated transient failures (network, timeout, 5xx, unusable answer) of one provider start a cooldown that doubles with
|
|
16
|
+
// every further failure, up to MAX_COOLDOWN_MS, with jitter so that several processes do not return at the same moment.
|
|
17
|
+
// The first failure only falls back to the next provider: a single blip does not take a provider out of the chain.
|
|
18
|
+
const BACKOFF_BASE_MS = 5_000;
|
|
19
|
+
const BACKOFF_AFTER_FAILURES = 2;
|
|
20
|
+
const MODEL_ID = /^[\w./:-]{1,100}$/;
|
|
21
|
+
const recentCalls = [];
|
|
22
|
+
const cooldownUntil = new Map();
|
|
23
|
+
const transientFailures = new Map();
|
|
24
|
+
export function resetProviderState() {
|
|
25
|
+
recentCalls.length = 0;
|
|
26
|
+
cooldownUntil.clear();
|
|
27
|
+
transientFailures.clear();
|
|
28
|
+
}
|
|
29
|
+
// Cooldown after the Nth consecutive transient failure (N >= BACKOFF_AFTER_FAILURES): exponential, capped, between half
|
|
30
|
+
// and the full value ("equal jitter"), so it is never zero and never above the cap.
|
|
31
|
+
export function backoffMs(failures, random = Math.random) {
|
|
32
|
+
const exponent = Math.max(0, failures - BACKOFF_AFTER_FAILURES);
|
|
33
|
+
const ceiling = Math.min(BACKOFF_BASE_MS * 2 ** Math.min(exponent, 20), MAX_COOLDOWN_MS);
|
|
34
|
+
return Math.round(ceiling / 2 + random() * (ceiling / 2));
|
|
35
|
+
}
|
|
36
|
+
function withinRateLimit() {
|
|
37
|
+
const now = Date.now();
|
|
38
|
+
while (recentCalls.length > 0 && now - recentCalls[0] > 60_000)
|
|
39
|
+
recentCalls.shift();
|
|
40
|
+
if (recentCalls.length >= MAX_CALLS_PER_MINUTE)
|
|
41
|
+
return false;
|
|
42
|
+
recentCalls.push(now);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
function perProviderTimeout() {
|
|
46
|
+
const configured = Number(process.env.GUARDIAN_LLM_TIMEOUT_MS);
|
|
47
|
+
return Number.isFinite(configured) && configured > 0
|
|
48
|
+
? Math.min(Math.max(configured, MIN_TIMEOUT_MS), MAX_TIMEOUT_MS)
|
|
49
|
+
: DEFAULT_TIMEOUT_MS;
|
|
50
|
+
}
|
|
51
|
+
// `outer` is the caller's cancellation (an MCP client that gave up): it stops the request at once and its reason is thrown as it is.
|
|
52
|
+
async function withDeadline(ms, work, outer) {
|
|
53
|
+
outer?.throwIfAborted();
|
|
54
|
+
const controller = new AbortController();
|
|
55
|
+
let timer;
|
|
56
|
+
let onOuterAbort;
|
|
57
|
+
const stopped = new Promise((_, reject) => {
|
|
58
|
+
timer = setTimeout(() => {
|
|
59
|
+
const error = new Error(`No answer within ${ms} ms`);
|
|
60
|
+
error.name = 'TimeoutError';
|
|
61
|
+
controller.abort(error);
|
|
62
|
+
reject(error);
|
|
63
|
+
}, ms);
|
|
64
|
+
if (outer) {
|
|
65
|
+
onOuterAbort = () => {
|
|
66
|
+
controller.abort(outer.reason);
|
|
67
|
+
reject(outer.reason);
|
|
68
|
+
};
|
|
69
|
+
outer.addEventListener('abort', onOuterAbort, { once: true });
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
try {
|
|
73
|
+
return await Promise.race([work(controller.signal), stopped]);
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
clearTimeout(timer);
|
|
77
|
+
if (outer && onOuterAbort)
|
|
78
|
+
outer.removeEventListener('abort', onOuterAbort);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function modelId(envName, fallback) {
|
|
82
|
+
const value = process.env[envName];
|
|
83
|
+
return value && MODEL_ID.test(value) ? value : fallback;
|
|
84
|
+
}
|
|
85
|
+
function providers() {
|
|
86
|
+
const chain = [];
|
|
87
|
+
const geminiKey = process.env.GEMINI_API_KEY;
|
|
88
|
+
const groqKey = process.env.GROQ_API_KEY;
|
|
89
|
+
if (geminiKey) {
|
|
90
|
+
const google = createGoogleGenerativeAI({ apiKey: geminiKey });
|
|
91
|
+
chain.push({ name: 'gemini', model: google(modelId('GEMINI_MODEL', DEFAULT_GEMINI_MODEL)) });
|
|
92
|
+
}
|
|
93
|
+
if (groqKey) {
|
|
94
|
+
const groq = createOpenAI({ apiKey: groqKey, baseURL: GROQ_BASE_URL, name: 'groq' });
|
|
95
|
+
chain.push({ name: 'groq', model: groq.chat(modelId('GROQ_MODEL', DEFAULT_GROQ_MODEL)) });
|
|
96
|
+
}
|
|
97
|
+
return chain;
|
|
98
|
+
}
|
|
99
|
+
export function hasLlmKeys() {
|
|
100
|
+
return Boolean(process.env.GEMINI_API_KEY || process.env.GROQ_API_KEY);
|
|
101
|
+
}
|
|
102
|
+
function retryAfterMs(err) {
|
|
103
|
+
const seconds = Number(err.responseHeaders?.['retry-after']);
|
|
104
|
+
return Number.isFinite(seconds) && seconds > 0 ? Math.min(seconds * 1000, MAX_COOLDOWN_MS) : DEFAULT_COOLDOWN_MS;
|
|
105
|
+
}
|
|
106
|
+
function failureKind(err) {
|
|
107
|
+
if (APICallError.isInstance(err)) {
|
|
108
|
+
const status = err.statusCode;
|
|
109
|
+
if (status === 429)
|
|
110
|
+
return 'rate-limited';
|
|
111
|
+
if (status !== undefined && status >= 400 && status < 500 && ![408, 409, 425].includes(status))
|
|
112
|
+
return 'permanent';
|
|
113
|
+
}
|
|
114
|
+
return 'transient';
|
|
115
|
+
}
|
|
116
|
+
function classify(err) {
|
|
117
|
+
if (APICallError.isInstance(err)) {
|
|
118
|
+
if (err.statusCode === 429)
|
|
119
|
+
return 'rate limited (429)';
|
|
120
|
+
return err.statusCode ? `HTTP ${err.statusCode}` : 'network error';
|
|
121
|
+
}
|
|
122
|
+
if (err instanceof Error && (err.name === 'TimeoutError' || err.name === 'AbortError'))
|
|
123
|
+
return 'timed out';
|
|
124
|
+
return 'unusable response';
|
|
125
|
+
}
|
|
126
|
+
function recordFailure(name, kind, err) {
|
|
127
|
+
const now = Date.now();
|
|
128
|
+
if (kind === 'rate-limited') {
|
|
129
|
+
cooldownUntil.set(name, now + retryAfterMs(err));
|
|
130
|
+
return '';
|
|
131
|
+
}
|
|
132
|
+
if (kind === 'permanent') {
|
|
133
|
+
// Retrying a refused request only repeats the refusal (and, for a bad key, can lock the account): rest the provider.
|
|
134
|
+
cooldownUntil.set(name, now + MAX_COOLDOWN_MS);
|
|
135
|
+
return ', not retried for a while';
|
|
136
|
+
}
|
|
137
|
+
const failures = (transientFailures.get(name) ?? 0) + 1;
|
|
138
|
+
transientFailures.set(name, failures);
|
|
139
|
+
if (failures < BACKOFF_AFTER_FAILURES)
|
|
140
|
+
return '';
|
|
141
|
+
const wait = backoffMs(failures);
|
|
142
|
+
cooldownUntil.set(name, now + wait);
|
|
143
|
+
return `, resting for ${Math.ceil(wait / 1000)} s after ${failures} failures in a row`;
|
|
144
|
+
}
|
|
145
|
+
async function run(rawSystem, rawPrompt, strict, signal) {
|
|
146
|
+
signal?.throwIfAborted();
|
|
147
|
+
const chain = providers();
|
|
148
|
+
if (chain.length === 0)
|
|
149
|
+
return null;
|
|
150
|
+
const { system, prompt, redactions } = prepareOutbound(rawSystem, rawPrompt, { strict });
|
|
151
|
+
// Providers that are resting are skipped before the rate budget is spent: a call that cannot reach any provider
|
|
152
|
+
// must not use up the 20-per-minute allowance of the calls that can.
|
|
153
|
+
const available = chain.filter((p) => (cooldownUntil.get(p.name) ?? 0) <= Date.now());
|
|
154
|
+
if (available.length === 0)
|
|
155
|
+
return null;
|
|
156
|
+
if (!withinRateLimit()) {
|
|
157
|
+
log.warn('LLM call limit reached (20 per minute), using the local result instead');
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
const timeout = perProviderTimeout();
|
|
161
|
+
const deadline = Date.now() + timeout * chain.length;
|
|
162
|
+
for (const [index, provider] of available.entries()) {
|
|
163
|
+
const remaining = deadline - Date.now();
|
|
164
|
+
if (remaining <= 0)
|
|
165
|
+
break;
|
|
166
|
+
try {
|
|
167
|
+
const { text } = await withDeadline(Math.min(timeout, remaining), (abortSignal) => generateText({ model: provider.model, system, prompt, maxOutputTokens: 2000, maxRetries: 0, abortSignal }), signal);
|
|
168
|
+
if (text.trim()) {
|
|
169
|
+
transientFailures.delete(provider.name);
|
|
170
|
+
return { text: text.trim(), provider: provider.name, redactions };
|
|
171
|
+
}
|
|
172
|
+
log.warn(`${provider.name} returned an empty answer${recordFailure(provider.name, 'transient', undefined)}`);
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
// The caller gave up: that is not a provider fault, so it is neither counted nor swallowed.
|
|
176
|
+
if (signal?.aborted)
|
|
177
|
+
throw signal.reason;
|
|
178
|
+
const next = available[index + 1];
|
|
179
|
+
const outcome = recordFailure(provider.name, failureKind(err), err);
|
|
180
|
+
log.warn(`${provider.name} ${classify(err)}${next ? `, falling back to ${next.name}` : ''}${outcome}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
export async function complete(system, prompt, options = {}) {
|
|
186
|
+
try {
|
|
187
|
+
return await run(system, prompt, options.strict ?? false, options.signal);
|
|
188
|
+
}
|
|
189
|
+
catch (err) {
|
|
190
|
+
// A blocked payload is a deliberate refusal the caller must be able to report, not a provider failure.
|
|
191
|
+
if (err instanceof OutboundBlocked)
|
|
192
|
+
throw err;
|
|
193
|
+
// A cancelled request is reported as cancelled, not turned into "no answer".
|
|
194
|
+
if (options.signal?.aborted)
|
|
195
|
+
throw err;
|
|
196
|
+
log.warn(`LLM step failed unexpectedly: ${err instanceof Error ? err.message : String(err)}`);
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
}
|