vouchington-tooling 0.60.0 → 0.61.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -0
- package/dist/cli/commands/sandbox-command-audit.d.mts +7 -0
- package/dist/cli/commands/sandbox-command-audit.mjs +27 -0
- package/dist/cli/index.mjs +2 -0
- package/dist/cli/parse-forwarded-commands.d.mts +3 -0
- package/dist/cli/parse-forwarded-commands.mjs +2 -0
- package/dist/cli/parse.d.mts +3 -0
- package/dist/cli/usage-text.d.mts +1 -1
- package/dist/cli/usage-text.mjs +9 -0
- package/dist/sandbox-command-audit/args.d.mts +20 -0
- package/dist/sandbox-command-audit/args.mjs +64 -0
- package/dist/sandbox-command-audit/classify-prefixes.d.mts +23 -0
- package/dist/sandbox-command-audit/classify-prefixes.mjs +50 -0
- package/dist/sandbox-command-audit/classify.d.mts +46 -0
- package/dist/sandbox-command-audit/classify.mjs +74 -0
- package/dist/sandbox-command-audit/claude-extract.d.mts +23 -0
- package/dist/sandbox-command-audit/claude-extract.mjs +76 -0
- package/dist/sandbox-command-audit/claude-tool-uses.d.mts +15 -0
- package/dist/sandbox-command-audit/claude-tool-uses.mjs +53 -0
- package/dist/sandbox-command-audit/codex-extract.d.mts +21 -0
- package/dist/sandbox-command-audit/codex-extract.mjs +77 -0
- package/dist/sandbox-command-audit/codex-scan.d.mts +11 -0
- package/dist/sandbox-command-audit/codex-scan.mjs +98 -0
- package/dist/sandbox-command-audit/codex-spool.d.mts +14 -0
- package/dist/sandbox-command-audit/codex-spool.mjs +46 -0
- package/dist/sandbox-command-audit/discover-files.d.mts +44 -0
- package/dist/sandbox-command-audit/discover-files.mjs +85 -0
- package/dist/sandbox-command-audit/failure-text.d.mts +7 -0
- package/dist/sandbox-command-audit/failure-text.mjs +34 -0
- package/dist/sandbox-command-audit/fixtures.test-helpers.d.mts +34 -0
- package/dist/sandbox-command-audit/fixtures.test-helpers.mjs +41 -0
- package/dist/sandbox-command-audit/index.d.mts +26 -0
- package/dist/sandbox-command-audit/index.mjs +15 -0
- package/dist/sandbox-command-audit/json-record.d.mts +6 -0
- package/dist/sandbox-command-audit/json-record.mjs +16 -0
- package/dist/sandbox-command-audit/limits.d.mts +30 -0
- package/dist/sandbox-command-audit/limits.mjs +66 -0
- package/dist/sandbox-command-audit/repo-scope.d.mts +25 -0
- package/dist/sandbox-command-audit/repo-scope.mjs +79 -0
- package/dist/sandbox-command-audit/report-raw.d.mts +16 -0
- package/dist/sandbox-command-audit/report-raw.mjs +55 -0
- package/dist/sandbox-command-audit/report.d.mts +10 -0
- package/dist/sandbox-command-audit/report.mjs +61 -0
- package/dist/sandbox-command-audit/run.d.mts +36 -0
- package/dist/sandbox-command-audit/run.mjs +70 -0
- package/dist/sandbox-command-audit/scan.d.mts +10 -0
- package/dist/sandbox-command-audit/scan.mjs +79 -0
- package/dist/sandbox-command-audit/transcript-cwd.d.mts +14 -0
- package/dist/sandbox-command-audit/transcript-cwd.mjs +153 -0
- package/dist/sandbox-command-audit/types.d.mts +59 -0
- package/dist/sandbox-command-audit/types.mjs +23 -0
- package/package.json +6 -1
- package/scripts/__pycache__/run_bounded_process.cpython-312.pyc +0 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { asRecord } from "../retrospective-transcript/index.mjs";
|
|
2
|
+
import { appendBounded, truncateRawText } from "./limits.mjs";
|
|
3
|
+
function evict(toolUses, bytes, retention, pendingBytes, limits) {
|
|
4
|
+
while (toolUses.size >= limits.maxPendingToolUses ||
|
|
5
|
+
pendingBytes.value + bytes > limits.maxPendingToolUseBytes) {
|
|
6
|
+
const oldest = toolUses.keys().next().value;
|
|
7
|
+
if (oldest === undefined)
|
|
8
|
+
break;
|
|
9
|
+
const evicted = toolUses.get(oldest);
|
|
10
|
+
toolUses.delete(oldest);
|
|
11
|
+
pendingBytes.value -= evicted.bytes;
|
|
12
|
+
retention.pendingToolUsesDropped++;
|
|
13
|
+
retention.pendingToolUseBytesDropped += evicted.bytes;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Remembers a Claude tool_use so its later tool_result can be correlated to the command, and
|
|
18
|
+
* records an escalation when a Bash call asked to leave the sandbox. Pending state is bounded by
|
|
19
|
+
* `limits`; a block too large to hold is dropped (its escalation is still recorded).
|
|
20
|
+
*/
|
|
21
|
+
export function recordClaudeToolUse(block, toolUses, escalations, retention, pendingBytes, limits) {
|
|
22
|
+
const value = asRecord(block);
|
|
23
|
+
if (!value || (value.type !== 'tool_use' && value.type !== 'server_tool_use'))
|
|
24
|
+
return;
|
|
25
|
+
const id = typeof value.id === 'string' ? value.id : undefined;
|
|
26
|
+
const name = typeof value.name === 'string' ? value.name : undefined;
|
|
27
|
+
if (!id || !name)
|
|
28
|
+
return;
|
|
29
|
+
const input = asRecord(value.input);
|
|
30
|
+
const command = typeof input?.command === 'string' ? input.command : undefined;
|
|
31
|
+
const retainedCommand = command
|
|
32
|
+
? truncateRawText(command, retention, 'command', limits)
|
|
33
|
+
: undefined;
|
|
34
|
+
const bytes = Buffer.byteLength(id) + Buffer.byteLength(name) + Buffer.byteLength(retainedCommand ?? '');
|
|
35
|
+
const existing = toolUses.get(id);
|
|
36
|
+
if (existing) {
|
|
37
|
+
toolUses.delete(id);
|
|
38
|
+
pendingBytes.value -= existing.bytes;
|
|
39
|
+
}
|
|
40
|
+
const escalated = name === 'Bash' && command && input?.dangerouslyDisableSandbox === true;
|
|
41
|
+
if (bytes > limits.maxPendingToolUseBytes) {
|
|
42
|
+
retention.pendingToolUsesDropped++;
|
|
43
|
+
retention.pendingToolUseBytesDropped += bytes;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
evict(toolUses, bytes, retention, pendingBytes, limits);
|
|
47
|
+
toolUses.set(id, { name, command: retainedCommand, bytes });
|
|
48
|
+
pendingBytes.value += bytes;
|
|
49
|
+
}
|
|
50
|
+
if (escalated) {
|
|
51
|
+
appendBounded(escalations, { source: 'claude', command: retainedCommand }, retention, limits);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type AuditLimits, type RawRetention } from './limits.mts';
|
|
2
|
+
import type { ClassifyFailure, CodexSandboxFailureRecord } from './types.mts';
|
|
3
|
+
export type CodexExtraction = {
|
|
4
|
+
sandboxFailures: CodexSandboxFailureRecord[];
|
|
5
|
+
rawRetention: RawRetention;
|
|
6
|
+
};
|
|
7
|
+
export type CodexRecordExtractor = {
|
|
8
|
+
pushLine: (line: string) => void;
|
|
9
|
+
result: () => CodexExtraction;
|
|
10
|
+
};
|
|
11
|
+
export type CodexExtractOptions = {
|
|
12
|
+
classifyFailure?: ClassifyFailure | undefined;
|
|
13
|
+
limits: AuditLimits;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Streams Codex transcript lines. Only completed CommandExecution items that the caller's
|
|
17
|
+
* `classifyFailure` rule claims are recorded: the generic permission signatures are not applied to
|
|
18
|
+
* Codex output.
|
|
19
|
+
*/
|
|
20
|
+
export declare function createCodexRecordExtractor(options: CodexExtractOptions): CodexRecordExtractor;
|
|
21
|
+
export declare function extractCodexRecords(lines: string[], options: CodexExtractOptions): CodexExtraction;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { asRecord } from "../retrospective-transcript/index.mjs";
|
|
2
|
+
import { parseJsonRecord } from "./json-record.mjs";
|
|
3
|
+
import { appendBounded, emptyRawRetention, truncateRawText, } from "./limits.mjs";
|
|
4
|
+
// A CommandExecution item carries `command` as a string or as an argv array such as
|
|
5
|
+
// `["bash", "-lc", "<script>"]`; the command text is the last string.
|
|
6
|
+
function commandFromArguments(command) {
|
|
7
|
+
if (typeof command === 'string')
|
|
8
|
+
return command;
|
|
9
|
+
if (!Array.isArray(command))
|
|
10
|
+
return undefined;
|
|
11
|
+
return command.findLast((entry) => typeof entry === 'string');
|
|
12
|
+
}
|
|
13
|
+
function commandFromExecution(item) {
|
|
14
|
+
const direct = commandFromArguments(item.command);
|
|
15
|
+
if (direct !== undefined || !Array.isArray(item.parsed_cmd))
|
|
16
|
+
return direct;
|
|
17
|
+
for (const parsed of item.parsed_cmd) {
|
|
18
|
+
const record = asRecord(parsed);
|
|
19
|
+
if (typeof record?.cmd === 'string')
|
|
20
|
+
return record.cmd;
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
function outputFromExecution(item) {
|
|
25
|
+
if (typeof item.aggregated_output === 'string' && item.aggregated_output.length > 0) {
|
|
26
|
+
return item.aggregated_output;
|
|
27
|
+
}
|
|
28
|
+
return [item.stdout, item.stderr]
|
|
29
|
+
.filter((value) => typeof value === 'string' && value.length > 0)
|
|
30
|
+
.join('\n');
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Streams Codex transcript lines. Only completed CommandExecution items that the caller's
|
|
34
|
+
* `classifyFailure` rule claims are recorded: the generic permission signatures are not applied to
|
|
35
|
+
* Codex output.
|
|
36
|
+
*/
|
|
37
|
+
export function createCodexRecordExtractor(options) {
|
|
38
|
+
const rawRetention = emptyRawRetention();
|
|
39
|
+
const sandboxFailures = [];
|
|
40
|
+
const { classifyFailure, limits } = options;
|
|
41
|
+
return {
|
|
42
|
+
pushLine(line) {
|
|
43
|
+
if (!classifyFailure)
|
|
44
|
+
return;
|
|
45
|
+
const record = parseJsonRecord(line);
|
|
46
|
+
const payload = asRecord(record?.payload);
|
|
47
|
+
const item = asRecord(payload?.item);
|
|
48
|
+
if (record?.type !== 'event_msg' || payload?.type !== 'item_completed')
|
|
49
|
+
return;
|
|
50
|
+
if (item?.type !== 'CommandExecution')
|
|
51
|
+
return;
|
|
52
|
+
const command = commandFromExecution(item);
|
|
53
|
+
const errorText = outputFromExecution(item);
|
|
54
|
+
const kind = classifyFailure(command, errorText, 'codex');
|
|
55
|
+
if (kind === undefined)
|
|
56
|
+
return;
|
|
57
|
+
const failure = {
|
|
58
|
+
source: 'codex',
|
|
59
|
+
kind,
|
|
60
|
+
command: command ? truncateRawText(command, rawRetention, 'command', limits) : undefined,
|
|
61
|
+
errorText: truncateRawText(errorText, rawRetention, 'error', limits),
|
|
62
|
+
};
|
|
63
|
+
appendBounded(sandboxFailures, failure, rawRetention, limits);
|
|
64
|
+
},
|
|
65
|
+
result: () => {
|
|
66
|
+
const result = { sandboxFailures, rawRetention };
|
|
67
|
+
Object.defineProperty(result, 'rawRetention', { enumerable: false, value: rawRetention });
|
|
68
|
+
return result;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
export function extractCodexRecords(lines, options) {
|
|
73
|
+
const extractor = createCodexRecordExtractor(options);
|
|
74
|
+
for (const line of lines)
|
|
75
|
+
extractor.pushLine(line);
|
|
76
|
+
return extractor.result();
|
|
77
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type CodexExtractOptions, type CodexExtraction } from './codex-extract.mts';
|
|
2
|
+
export type CodexFileResult = CodexExtraction | {
|
|
3
|
+
error: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Extracts records from one Codex transcript. A forked child repeats its parent's history before
|
|
7
|
+
* its first owned `task_started`; that prefix is spooled under `tempRoot` and dropped once the
|
|
8
|
+
* owned task starts. When no owned task is ever found the whole spool is replayed, deliberately
|
|
9
|
+
* over-counting rather than losing activity.
|
|
10
|
+
*/
|
|
11
|
+
export declare function scanCodexFile(file: string, tempRoot: string, options: CodexExtractOptions): Promise<CodexFileResult>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { asRecord, openTranscriptLines } from "../retrospective-transcript/index.mjs";
|
|
2
|
+
import { createCodexRecordExtractor, } from "./codex-extract.mjs";
|
|
3
|
+
import { createSpool } from "./codex-spool.mjs";
|
|
4
|
+
function inheritedSession(record) {
|
|
5
|
+
const payload = asRecord(record.payload);
|
|
6
|
+
return (asRecord(asRecord(payload?.source)?.subagent) !== undefined ||
|
|
7
|
+
typeof payload?.forked_from_id === 'string' ||
|
|
8
|
+
typeof payload?.parent_thread_id === 'string' ||
|
|
9
|
+
(typeof payload?.id === 'string' &&
|
|
10
|
+
typeof payload.session_id === 'string' &&
|
|
11
|
+
payload.id !== payload.session_id));
|
|
12
|
+
}
|
|
13
|
+
// started_at is epoch seconds or milliseconds. A millisecond value is compared against the exact
|
|
14
|
+
// session creation time, so an inherited task from earlier in the same second is not mistaken for
|
|
15
|
+
// an owned one; a seconds value can only be compared at second precision.
|
|
16
|
+
function ownedTaskStarted(line, sessionMs) {
|
|
17
|
+
try {
|
|
18
|
+
const record = asRecord(JSON.parse(line));
|
|
19
|
+
const payload = asRecord(record?.payload);
|
|
20
|
+
if (record?.type !== 'event_msg' || payload?.type !== 'task_started')
|
|
21
|
+
return false;
|
|
22
|
+
if (typeof payload.started_at !== 'number')
|
|
23
|
+
return false;
|
|
24
|
+
const inMillis = payload.started_at >= 1_000_000_000_000;
|
|
25
|
+
if (inMillis)
|
|
26
|
+
return payload.started_at >= sessionMs;
|
|
27
|
+
return payload.started_at >= Math.floor(sessionMs / 1000);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// The creation time of a forked child's session when its first record marks it inherited, else
|
|
34
|
+
// undefined (including for malformed metadata, which falls back to scanning the whole file).
|
|
35
|
+
function inheritedSessionMs(line) {
|
|
36
|
+
try {
|
|
37
|
+
const record = asRecord(JSON.parse(line));
|
|
38
|
+
if (record?.type !== 'session_meta' || !inheritedSession(record))
|
|
39
|
+
return undefined;
|
|
40
|
+
const timestamp = asRecord(record.payload)?.timestamp;
|
|
41
|
+
const timestampMs = typeof timestamp === 'string' ? Date.parse(timestamp) : Number.NaN;
|
|
42
|
+
return Number.isFinite(timestampMs) ? timestampMs : undefined;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Extracts records from one Codex transcript. A forked child repeats its parent's history before
|
|
50
|
+
* its first owned `task_started`; that prefix is spooled under `tempRoot` and dropped once the
|
|
51
|
+
* owned task starts. When no owned task is ever found the whole spool is replayed, deliberately
|
|
52
|
+
* over-counting rather than losing activity.
|
|
53
|
+
*/
|
|
54
|
+
export async function scanCodexFile(file, tempRoot, options) {
|
|
55
|
+
const opened = await openTranscriptLines(file);
|
|
56
|
+
if ('error' in opened)
|
|
57
|
+
return { error: opened.error };
|
|
58
|
+
const extractor = createCodexRecordExtractor(options);
|
|
59
|
+
let spool;
|
|
60
|
+
let sessionMs;
|
|
61
|
+
let first = true;
|
|
62
|
+
try {
|
|
63
|
+
for await (const line of opened.lines) {
|
|
64
|
+
if (first && line.trim()) {
|
|
65
|
+
first = false;
|
|
66
|
+
sessionMs = inheritedSessionMs(line);
|
|
67
|
+
if (sessionMs !== undefined) {
|
|
68
|
+
spool = await createSpool(tempRoot, options.limits.maxInheritedCodexSpoolBytes);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (spool === undefined || sessionMs === undefined) {
|
|
72
|
+
extractor.pushLine(line);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
await spool.write(line);
|
|
76
|
+
if (!ownedTaskStarted(line, sessionMs))
|
|
77
|
+
continue;
|
|
78
|
+
await spool.discard();
|
|
79
|
+
spool = undefined;
|
|
80
|
+
extractor.pushLine(line);
|
|
81
|
+
}
|
|
82
|
+
if (spool) {
|
|
83
|
+
await spool.close();
|
|
84
|
+
const replay = await openTranscriptLines(spool.path);
|
|
85
|
+
if ('error' in replay)
|
|
86
|
+
return { error: replay.error };
|
|
87
|
+
for await (const line of replay.lines)
|
|
88
|
+
extractor.pushLine(line);
|
|
89
|
+
}
|
|
90
|
+
return extractor.result();
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
return { error: err.message };
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
await spool?.discard();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type Spool = {
|
|
2
|
+
path: string;
|
|
3
|
+
/** Appends one line; rejects once the byte limit is passed or the stream has failed. */
|
|
4
|
+
write: (line: string) => Promise<void>;
|
|
5
|
+
/** Flushes and closes the file, keeping it on disk for a replay. */
|
|
6
|
+
close: () => Promise<void>;
|
|
7
|
+
/** Closes the stream if open and deletes the spool directory. */
|
|
8
|
+
discard: () => Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A temporary line spool for the history a forked Codex child inherits before its first owned
|
|
12
|
+
* task. It lives under `tempRoot` so a large inherited prefix never sits in memory.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createSpool(tempRoot: string, limitBytes: number): Promise<Spool>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createWriteStream } from 'node:fs';
|
|
2
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
3
|
+
import { once } from 'node:events';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { finished } from 'node:stream/promises';
|
|
6
|
+
function assertHealthy(writer) {
|
|
7
|
+
if (writer.errored)
|
|
8
|
+
throw writer.errored;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A temporary line spool for the history a forked Codex child inherits before its first owned
|
|
12
|
+
* task. It lives under `tempRoot` so a large inherited prefix never sits in memory.
|
|
13
|
+
*/
|
|
14
|
+
export async function createSpool(tempRoot, limitBytes) {
|
|
15
|
+
const dir = await mkdtemp(join(tempRoot, 'sandbox-audit-codex-'));
|
|
16
|
+
const path = join(dir, 'inherited.jsonl');
|
|
17
|
+
const writer = createWriteStream(path, { encoding: 'utf8' });
|
|
18
|
+
let bytes = 0;
|
|
19
|
+
let closed = false;
|
|
20
|
+
// An open failure surfaces from the first write or from close, through the stream's error.
|
|
21
|
+
writer.on('error', () => undefined);
|
|
22
|
+
return {
|
|
23
|
+
path,
|
|
24
|
+
async write(line) {
|
|
25
|
+
assertHealthy(writer);
|
|
26
|
+
bytes += Buffer.byteLength(line) + 1;
|
|
27
|
+
if (bytes > limitBytes) {
|
|
28
|
+
throw new Error(`inherited Codex spool exceeds ${limitBytes} byte limit`);
|
|
29
|
+
}
|
|
30
|
+
if (!writer.write(`${line}\n`))
|
|
31
|
+
await once(writer, 'drain');
|
|
32
|
+
},
|
|
33
|
+
async close() {
|
|
34
|
+
assertHealthy(writer);
|
|
35
|
+
writer.end();
|
|
36
|
+
await finished(writer);
|
|
37
|
+
closed = true;
|
|
38
|
+
assertHealthy(writer);
|
|
39
|
+
},
|
|
40
|
+
async discard() {
|
|
41
|
+
if (!closed)
|
|
42
|
+
writer.destroy();
|
|
43
|
+
await rm(dir, { recursive: true, force: true });
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { SkipCounts } from './types.mts';
|
|
2
|
+
export type DiscoverOptions = {
|
|
3
|
+
/** Root holding Claude project transcripts (`<root>/<project>/<session>.jsonl`). */
|
|
4
|
+
projectsDir: string;
|
|
5
|
+
/** Root holding Codex rollouts (`<root>/**\/rollout-*-<session>.jsonl`). */
|
|
6
|
+
codexSessionsDir: string;
|
|
7
|
+
/** Most-recent files kept per root, applied after repo scoping. */
|
|
8
|
+
maxFilesPerRoot: number;
|
|
9
|
+
/** Drops files last modified before `now - sinceDays`, before the cap. */
|
|
10
|
+
sinceDays?: number;
|
|
11
|
+
/** Clock for `sinceDays`; injected so tests use a fixed one. */
|
|
12
|
+
now?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Narrows discovery to one session's transcripts. An explicit session is already an exact
|
|
15
|
+
* target, so it bypasses repo scoping: it must never be lost to an unresolved cwd.
|
|
16
|
+
*/
|
|
17
|
+
sessionId?: string;
|
|
18
|
+
/** Roots to scope discovery to. Empty means unscoped (purely mtime-ordered). */
|
|
19
|
+
repoRoots: string[];
|
|
20
|
+
};
|
|
21
|
+
export type DiscoveredFiles = {
|
|
22
|
+
claudeFiles: string[];
|
|
23
|
+
codexFiles: string[];
|
|
24
|
+
skippedOtherRepo: SkipCounts;
|
|
25
|
+
skippedUnknownCwd: SkipCounts;
|
|
26
|
+
};
|
|
27
|
+
type StatEntry = {
|
|
28
|
+
path: string;
|
|
29
|
+
mtimeMs: number;
|
|
30
|
+
};
|
|
31
|
+
type ResolveCwd = (path: string) => Promise<string | undefined>;
|
|
32
|
+
type SelectResult = {
|
|
33
|
+
files: string[];
|
|
34
|
+
skippedOtherRepo: number;
|
|
35
|
+
skippedUnknownCwd: number;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Walks the newest-first list resolving each file's cwd and keeping in-repo files until `limit`.
|
|
39
|
+
* The skip counts are lower bounds, because the walk stops at `limit`. A file whose cwd cannot be
|
|
40
|
+
* resolved is excluded (fail closed) but always counted.
|
|
41
|
+
*/
|
|
42
|
+
export declare function selectInRepo(entries: StatEntry[], limit: number, repoRoots: string[], resolveCwd: ResolveCwd): Promise<SelectResult>;
|
|
43
|
+
export declare function discoverTranscriptFiles(options: DiscoverOptions): Promise<DiscoveredFiles>;
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { statSync } from 'node:fs';
|
|
2
|
+
import { globFrom } from "../retrospective-transcript/resolve-transcript-file.mjs";
|
|
3
|
+
import { isWithinRepoRoots } from "./repo-scope.mjs";
|
|
4
|
+
import { claudeSessionCwdResolver, readTranscriptCwd } from "./transcript-cwd.mjs";
|
|
5
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
6
|
+
// Stats every candidate, drops anything outside the window, and sorts newest first. The cap is
|
|
7
|
+
// applied later, after repo scoping: capping first would fill a small limit with other
|
|
8
|
+
// repositories' files.
|
|
9
|
+
function statAndSort(paths, cutoffMs) {
|
|
10
|
+
const entries = [];
|
|
11
|
+
for (const path of paths) {
|
|
12
|
+
try {
|
|
13
|
+
entries.push({ path, mtimeMs: statSync(path).mtimeMs });
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
// A file removed between glob and stat, or unreadable, is skipped: discovery is best-effort.
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const inWindow = entries.filter((entry) => cutoffMs === undefined || entry.mtimeMs >= cutoffMs);
|
|
20
|
+
return inWindow.toSorted((a, b) => b.mtimeMs - a.mtimeMs);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Walks the newest-first list resolving each file's cwd and keeping in-repo files until `limit`.
|
|
24
|
+
* The skip counts are lower bounds, because the walk stops at `limit`. A file whose cwd cannot be
|
|
25
|
+
* resolved is excluded (fail closed) but always counted.
|
|
26
|
+
*/
|
|
27
|
+
export async function selectInRepo(entries, limit, repoRoots, resolveCwd) {
|
|
28
|
+
const files = [];
|
|
29
|
+
let skippedOtherRepo = 0;
|
|
30
|
+
let skippedUnknownCwd = 0;
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
if (files.length >= limit)
|
|
33
|
+
break;
|
|
34
|
+
const cwd = await resolveCwd(entry.path).catch(() => undefined);
|
|
35
|
+
if (cwd === undefined) {
|
|
36
|
+
skippedUnknownCwd++;
|
|
37
|
+
}
|
|
38
|
+
else if (await isWithinRepoRoots(cwd, repoRoots)) {
|
|
39
|
+
files.push(entry.path);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
skippedOtherRepo++;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { files, skippedOtherRepo, skippedUnknownCwd };
|
|
46
|
+
}
|
|
47
|
+
async function discoverForRoot(paths, options, resolveCwd) {
|
|
48
|
+
const cutoffMs = options.sinceDays === undefined
|
|
49
|
+
? undefined
|
|
50
|
+
: (options.now ?? Date.now()) - options.sinceDays * DAY_MS;
|
|
51
|
+
const sorted = statAndSort(paths, cutoffMs);
|
|
52
|
+
if (options.sessionId !== undefined || options.repoRoots.length === 0) {
|
|
53
|
+
const files = sorted.slice(0, options.maxFilesPerRoot).map((entry) => entry.path);
|
|
54
|
+
return { files, skippedOtherRepo: 0, skippedUnknownCwd: 0 };
|
|
55
|
+
}
|
|
56
|
+
return selectInRepo(sorted, options.maxFilesPerRoot, options.repoRoots, resolveCwd);
|
|
57
|
+
}
|
|
58
|
+
// Top-level session files and subagent transcripts share one JSONL schema, so one recursive glob
|
|
59
|
+
// finds both. A session id needs two precise patterns: `**/<id>*.jsonl` would also match
|
|
60
|
+
// unrelated sessions that share a prefix.
|
|
61
|
+
function claudePaths(options) {
|
|
62
|
+
const { projectsDir, sessionId } = options;
|
|
63
|
+
if (sessionId === undefined)
|
|
64
|
+
return globFrom(projectsDir, '**/*.jsonl');
|
|
65
|
+
return [
|
|
66
|
+
...globFrom(projectsDir, `**/${sessionId}.jsonl`),
|
|
67
|
+
...globFrom(projectsDir, `**/${sessionId}/subagents/*.jsonl`),
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
function codexPaths(options) {
|
|
71
|
+
const { codexSessionsDir, sessionId } = options;
|
|
72
|
+
return globFrom(codexSessionsDir, sessionId === undefined ? '**/*.jsonl' : `**/rollout-*-${sessionId}.jsonl`);
|
|
73
|
+
}
|
|
74
|
+
export async function discoverTranscriptFiles(options) {
|
|
75
|
+
const [claude, codex] = await Promise.all([
|
|
76
|
+
discoverForRoot(claudePaths(options), options, claudeSessionCwdResolver(options.projectsDir)),
|
|
77
|
+
discoverForRoot(codexPaths(options), options, readTranscriptCwd),
|
|
78
|
+
]);
|
|
79
|
+
return {
|
|
80
|
+
claudeFiles: claude.files,
|
|
81
|
+
codexFiles: codex.files,
|
|
82
|
+
skippedOtherRepo: { claude: claude.skippedOtherRepo, codex: codex.skippedOtherRepo },
|
|
83
|
+
skippedUnknownCwd: { claude: claude.skippedUnknownCwd, codex: codex.skippedUnknownCwd },
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ClassifyFailure, FailureKind } from './types.mts';
|
|
2
|
+
/**
|
|
3
|
+
* Sorts a failed Bash result into `e2big`, a caller-defined kind, `genuine`, or nothing. The
|
|
4
|
+
* caller's rule sits between the E2BIG check and the generic signatures, so it can claim a failure
|
|
5
|
+
* that would otherwise count as a genuine bypass candidate.
|
|
6
|
+
*/
|
|
7
|
+
export declare function classifyFailureText(command: string | undefined, text: string, source: 'claude' | 'codex', classify?: ClassifyFailure): FailureKind | undefined;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const E2BIG_PATTERN = /\bE2BIG\b/;
|
|
2
|
+
// Deliberately conservative: `is_error: true` on a Bash tool_result also covers ordinary
|
|
3
|
+
// application failures (a failed test run, a grep with no matches), so only known
|
|
4
|
+
// permission/access signatures count. ECONNREFUSED is excluded on purpose: it is the generic
|
|
5
|
+
// "nothing is listening" error and carries no permission semantics.
|
|
6
|
+
const GENUINE_FAILURE_PATTERNS = [
|
|
7
|
+
/operation not permitted/i,
|
|
8
|
+
/permission denied/i,
|
|
9
|
+
/\bEACCES\b/,
|
|
10
|
+
/\bEPERM\b/,
|
|
11
|
+
/could not start .*sandbox/i,
|
|
12
|
+
];
|
|
13
|
+
// A timeout against a loopback target is a sandbox's signature for a silently dropped
|
|
14
|
+
// connection. Requiring both tokens keeps it narrow: a bare "timed out" (a slow test run) or a
|
|
15
|
+
// bare loopback mention carries no sandbox signal on its own.
|
|
16
|
+
const TIMEOUT_PATTERN = /\bETIMEDOUT\b|\btimed out\b/i;
|
|
17
|
+
const LOOPBACK_PATTERN = /\blocalhost\b|\b127\.0\.0\.1\b|(?<![\w:])::1(?!\w)/i;
|
|
18
|
+
/**
|
|
19
|
+
* Sorts a failed Bash result into `e2big`, a caller-defined kind, `genuine`, or nothing. The
|
|
20
|
+
* caller's rule sits between the E2BIG check and the generic signatures, so it can claim a failure
|
|
21
|
+
* that would otherwise count as a genuine bypass candidate.
|
|
22
|
+
*/
|
|
23
|
+
export function classifyFailureText(command, text, source, classify) {
|
|
24
|
+
if (E2BIG_PATTERN.test(text))
|
|
25
|
+
return 'e2big';
|
|
26
|
+
const custom = classify?.(command, text, source);
|
|
27
|
+
if (custom !== undefined)
|
|
28
|
+
return custom;
|
|
29
|
+
if (GENUINE_FAILURE_PATTERNS.some((pattern) => pattern.test(text)))
|
|
30
|
+
return 'genuine';
|
|
31
|
+
if (TIMEOUT_PATTERN.test(text) && LOOPBACK_PATTERN.test(text))
|
|
32
|
+
return 'genuine';
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type AuditLimits } from './limits.mts';
|
|
2
|
+
export declare const limits: (overrides?: Partial<AuditLimits>) => AuditLimits;
|
|
3
|
+
export declare const jsonl: (...records: unknown[]) => string[];
|
|
4
|
+
export declare function bashUse(id: string, command: string, input?: Record<string, unknown>): {
|
|
5
|
+
type: string;
|
|
6
|
+
message: {
|
|
7
|
+
content: {
|
|
8
|
+
type: string;
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
input: {
|
|
12
|
+
command: string;
|
|
13
|
+
};
|
|
14
|
+
}[];
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export declare function toolResult(id: string, content: unknown, extra?: {
|
|
18
|
+
isError?: boolean;
|
|
19
|
+
toolDenialKind?: string;
|
|
20
|
+
}): {
|
|
21
|
+
message: {
|
|
22
|
+
content: {
|
|
23
|
+
type: string;
|
|
24
|
+
tool_use_id: string;
|
|
25
|
+
is_error: boolean;
|
|
26
|
+
content: unknown;
|
|
27
|
+
}[];
|
|
28
|
+
};
|
|
29
|
+
toolDenialKind?: string;
|
|
30
|
+
type: string;
|
|
31
|
+
};
|
|
32
|
+
/** A temporary directory removed after the test. */
|
|
33
|
+
export declare function tempDir(): string;
|
|
34
|
+
export declare function writeLines(path: string, lines: string[]): string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { afterEach } from 'vitest';
|
|
5
|
+
import { resolveLimits } from "./limits.mjs";
|
|
6
|
+
export const limits = (overrides = {}) => resolveLimits(overrides);
|
|
7
|
+
export const jsonl = (...records) => records.map((record) => JSON.stringify(record));
|
|
8
|
+
export function bashUse(id, command, input = {}) {
|
|
9
|
+
return {
|
|
10
|
+
type: 'assistant',
|
|
11
|
+
message: {
|
|
12
|
+
content: [{ type: 'tool_use', id, name: 'Bash', input: { command, ...input } }],
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function toolResult(id, content, extra = {}) {
|
|
17
|
+
const { isError = true, toolDenialKind } = extra;
|
|
18
|
+
return {
|
|
19
|
+
type: 'user',
|
|
20
|
+
...(toolDenialKind === undefined ? {} : { toolDenialKind }),
|
|
21
|
+
message: {
|
|
22
|
+
content: [{ type: 'tool_result', tool_use_id: id, is_error: isError, content }],
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const roots = [];
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
for (const root of roots.splice(0))
|
|
29
|
+
rmSync(root, { recursive: true, force: true });
|
|
30
|
+
});
|
|
31
|
+
/** A temporary directory removed after the test. */
|
|
32
|
+
export function tempDir() {
|
|
33
|
+
const root = mkdtempSync(join(tmpdir(), 'sandbox-audit-test-'));
|
|
34
|
+
roots.push(root);
|
|
35
|
+
return root;
|
|
36
|
+
}
|
|
37
|
+
export function writeLines(path, lines) {
|
|
38
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
39
|
+
writeFileSync(path, `${lines.join('\n')}\n`);
|
|
40
|
+
return path;
|
|
41
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export { parseAuditArgs } from './args.mts';
|
|
2
|
+
export type { AuditArgs } from './args.mts';
|
|
3
|
+
export { classifyAudit } from './classify.mts';
|
|
4
|
+
export type { BlockCandidate, CategorizedAudit, CoverageList, CoveragePolicy, EscalationPressure, GenuineBypassCandidate, KindCount, PrefixCount, } from './classify.mts';
|
|
5
|
+
export { createClaudeRecordExtractor, extractClaudeRecords } from './claude-extract.mts';
|
|
6
|
+
export type { ClaudeExtraction, ClaudeRecordExtractor } from './claude-extract.mts';
|
|
7
|
+
export { scanCodexFile } from './codex-scan.mts';
|
|
8
|
+
export { createCodexRecordExtractor, extractCodexRecords } from './codex-extract.mts';
|
|
9
|
+
export type { CodexExtraction, CodexRecordExtractor } from './codex-extract.mts';
|
|
10
|
+
export { discoverTranscriptFiles } from './discover-files.mts';
|
|
11
|
+
export type { DiscoverOptions, DiscoveredFiles } from './discover-files.mts';
|
|
12
|
+
export { classifyFailureText } from './failure-text.mts';
|
|
13
|
+
export { DEFAULT_LIMITS, resolveLimits } from './limits.mts';
|
|
14
|
+
export type { AuditLimits, RawRetention } from './limits.mts';
|
|
15
|
+
export { HEADER, formatMarkdown, formatReport } from './report.mts';
|
|
16
|
+
export type { ReportOptions } from './report.mts';
|
|
17
|
+
export { formatRaw } from './report-raw.mts';
|
|
18
|
+
export { isWithinRepoRoots, resolveRepoRoots } from './repo-scope.mts';
|
|
19
|
+
export type { RepoScopeDeps, RepoScopeOptions } from './repo-scope.mts';
|
|
20
|
+
export { loadAuditRules, runAudit } from './run.mts';
|
|
21
|
+
export type { AuditRules, PolicyContext } from './run.mts';
|
|
22
|
+
export { scanTranscripts } from './scan.mts';
|
|
23
|
+
export type { ScanOptions } from './scan.mts';
|
|
24
|
+
export { claudeSessionCwdResolver, readTranscriptCwd } from './transcript-cwd.mts';
|
|
25
|
+
export { emptyScanResult } from './types.mts';
|
|
26
|
+
export type { ClassifyFailure, ClaudeDenialRecord, ClaudeEscalationRecord, ClaudeSandboxFailureRecord, ClaudeToolDenialKind, CodexSandboxFailureRecord, FailureKind, ScanResult, ScanRules, SkipCounts, } from './types.mts';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { parseAuditArgs } from "./args.mjs";
|
|
2
|
+
export { classifyAudit } from "./classify.mjs";
|
|
3
|
+
export { createClaudeRecordExtractor, extractClaudeRecords } from "./claude-extract.mjs";
|
|
4
|
+
export { scanCodexFile } from "./codex-scan.mjs";
|
|
5
|
+
export { createCodexRecordExtractor, extractCodexRecords } from "./codex-extract.mjs";
|
|
6
|
+
export { discoverTranscriptFiles } from "./discover-files.mjs";
|
|
7
|
+
export { classifyFailureText } from "./failure-text.mjs";
|
|
8
|
+
export { DEFAULT_LIMITS, resolveLimits } from "./limits.mjs";
|
|
9
|
+
export { HEADER, formatMarkdown, formatReport } from "./report.mjs";
|
|
10
|
+
export { formatRaw } from "./report-raw.mjs";
|
|
11
|
+
export { isWithinRepoRoots, resolveRepoRoots } from "./repo-scope.mjs";
|
|
12
|
+
export { loadAuditRules, runAudit } from "./run.mjs";
|
|
13
|
+
export { scanTranscripts } from "./scan.mjs";
|
|
14
|
+
export { claudeSessionCwdResolver, readTranscriptCwd } from "./transcript-cwd.mjs";
|
|
15
|
+
export { emptyScanResult } from "./types.mjs";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses one transcript line into an object, or undefined for a blank, malformed, partially
|
|
3
|
+
* written or non-object line. Only parsing is guarded: callers run their own logic outside it, so
|
|
4
|
+
* an error from an injected rule is never mistaken for a malformed line.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseJsonRecord(line: string): Record<string, unknown> | undefined;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { asRecord } from "../retrospective-transcript/index.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Parses one transcript line into an object, or undefined for a blank, malformed, partially
|
|
4
|
+
* written or non-object line. Only parsing is guarded: callers run their own logic outside it, so
|
|
5
|
+
* an error from an injected rule is never mistaken for a malformed line.
|
|
6
|
+
*/
|
|
7
|
+
export function parseJsonRecord(line) {
|
|
8
|
+
if (!line.trim())
|
|
9
|
+
return undefined;
|
|
10
|
+
try {
|
|
11
|
+
return asRecord(JSON.parse(line));
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
}
|