@firefunc-agent/runner 0.9.7 → 0.9.34
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 +169 -10
- package/dist/activity.d.ts +1 -0
- package/dist/activity.js +1 -1
- package/dist/alive.d.ts +40 -0
- package/dist/alive.js +138 -0
- package/dist/api-fallback.d.ts +11 -0
- package/dist/api-fallback.js +80 -0
- package/dist/api.d.ts +186 -3
- package/dist/api.js +311 -6
- package/dist/ask/index.d.ts +20 -0
- package/dist/ask/index.js +64 -0
- package/dist/ask-first.d.ts +8 -0
- package/dist/brief-file.d.ts +15 -0
- package/dist/brief-file.js +36 -0
- package/dist/brief.d.ts +5 -0
- package/dist/brief.js +19 -3
- package/dist/build-caches.d.ts +7 -0
- package/dist/build-caches.js +63 -0
- package/dist/build-info.json +2 -2
- package/dist/checkpoint.d.ts +40 -0
- package/dist/checkpoint.js +98 -0
- package/dist/cli.js +184 -18
- package/dist/cloud-boot.d.ts +41 -0
- package/dist/cloud-boot.js +239 -0
- package/dist/command-activity.d.ts +23 -0
- package/dist/command-activity.js +89 -0
- package/dist/command-step.d.ts +58 -0
- package/dist/command-step.js +248 -0
- package/dist/config.d.ts +40 -0
- package/dist/config.js +23 -0
- package/dist/cut.d.ts +2 -1
- package/dist/cut.js +19 -5
- package/dist/daemon.js +375 -14
- package/dist/device-lease.d.ts +15 -0
- package/dist/device-lease.js +608 -0
- package/dist/editor/fs-server.d.ts +31 -0
- package/dist/editor/fs-server.js +159 -0
- package/dist/editor/relay-crypto.d.ts +68 -0
- package/dist/editor/relay-crypto.js +191 -0
- package/dist/editor/sessions.d.ts +50 -0
- package/dist/editor/sessions.js +279 -0
- package/dist/editor/worktree-of.d.ts +1 -0
- package/dist/editor/worktree-of.js +23 -0
- package/dist/editor/ws-client.d.ts +26 -0
- package/dist/editor/ws-client.js +227 -0
- package/dist/engine-account.d.ts +24 -0
- package/dist/engine-account.js +118 -0
- package/dist/engine-login.d.ts +12 -0
- package/dist/engine-login.js +88 -0
- package/dist/engine-models.d.ts +13 -0
- package/dist/engine-models.js +71 -0
- package/dist/engine-watch.d.ts +1 -0
- package/dist/engine-watch.js +22 -6
- package/dist/engines/antigravity.js +19 -4
- package/dist/engines/claude.js +15 -8
- package/dist/engines/codex.js +16 -2
- package/dist/engines/copilot.js +12 -0
- package/dist/engines/crush.d.ts +2 -0
- package/dist/engines/crush.js +58 -0
- package/dist/engines/cursor.js +12 -0
- package/dist/engines/gemini.js +14 -9
- package/dist/engines/index.d.ts +8 -0
- package/dist/engines/index.js +73 -1
- package/dist/engines/kilo.d.ts +2 -0
- package/dist/engines/kilo.js +63 -0
- package/dist/engines/opencode.d.ts +9 -0
- package/dist/engines/opencode.js +234 -0
- package/dist/engines/openhands.d.ts +10 -0
- package/dist/engines/openhands.js +271 -0
- package/dist/engines/pi.d.ts +10 -0
- package/dist/engines/pi.js +248 -0
- package/dist/engines/qwen.d.ts +8 -0
- package/dist/engines/qwen.js +153 -0
- package/dist/engines/sessions.d.ts +6 -0
- package/dist/engines/sessions.js +68 -0
- package/dist/engines/types.d.ts +3 -3
- package/dist/exec.d.ts +1 -0
- package/dist/exec.js +10 -4
- package/dist/firefunc/agents.d.ts +8 -1
- package/dist/firefunc/agents.js +53 -3
- package/dist/firefunc/auth.js +72 -19
- package/dist/firefunc/cli.js +25 -0
- package/dist/firefunc/client.d.ts +5 -1
- package/dist/firefunc/client.js +12 -2
- package/dist/firefunc/config.d.ts +11 -1
- package/dist/firefunc/config.js +6 -4
- package/dist/firefunc/io.d.ts +1 -0
- package/dist/firefunc/main.js +8 -1
- package/dist/firefunc/oauth-login.d.ts +23 -0
- package/dist/firefunc/oauth-login.js +304 -0
- package/dist/firefunc/tasks.d.ts +4 -0
- package/dist/firefunc/tasks.js +89 -2
- package/dist/fleet-words.d.ts +2 -0
- package/dist/fleet-words.js +7 -0
- package/dist/git-auth.d.ts +16 -0
- package/dist/git-auth.js +48 -2
- package/dist/held.d.ts +3 -0
- package/dist/job.d.ts +26 -3
- package/dist/job.js +1109 -132
- package/dist/keep-awake.d.ts +17 -0
- package/dist/keep-awake.js +90 -0
- package/dist/keep-local.d.ts +2 -1
- package/dist/keep-local.js +8 -3
- package/dist/limit.d.ts +10 -1
- package/dist/limit.js +264 -4
- package/dist/local-checks.d.ts +19 -0
- package/dist/local-checks.js +79 -0
- package/dist/locks.d.ts +21 -1
- package/dist/locks.js +279 -66
- package/dist/machine-commands.d.ts +21 -0
- package/dist/machine-commands.js +107 -0
- package/dist/machine-profile.d.ts +18 -0
- package/dist/machine-profile.js +69 -0
- package/dist/mcp-init.d.ts +2 -0
- package/dist/mcp-init.js +28 -0
- package/dist/mcp.d.ts +13 -1
- package/dist/mcp.js +23 -2
- package/dist/network.js +3 -2
- package/dist/orphans.d.ts +4 -0
- package/dist/orphans.js +96 -4
- package/dist/plan-phase/index.d.ts +11 -0
- package/dist/plan-phase/index.js +46 -0
- package/dist/pool.d.ts +1 -0
- package/dist/pool.js +29 -4
- package/dist/pr-title.d.ts +15 -0
- package/dist/pr-title.js +33 -0
- package/dist/recover.d.ts +3 -1
- package/dist/recover.js +27 -7
- package/dist/reference-files.d.ts +27 -0
- package/dist/reference-files.js +202 -0
- package/dist/release-draft.d.ts +79 -0
- package/dist/release-draft.js +301 -0
- package/dist/resources.d.ts +86 -0
- package/dist/resources.js +217 -0
- package/dist/restart-idle.d.ts +23 -0
- package/dist/restart-idle.js +67 -0
- package/dist/run-attachments.d.ts +84 -0
- package/dist/run-attachments.js +281 -0
- package/dist/run-stops.d.ts +15 -0
- package/dist/run-stops.js +90 -0
- package/dist/run-temp.d.ts +12 -0
- package/dist/run-temp.js +57 -0
- package/dist/secret-scan.d.ts +6 -1
- package/dist/secret-scan.js +33 -0
- package/dist/secret-values.d.ts +17 -0
- package/dist/secret-values.js +81 -0
- package/dist/self-upgrade.d.ts +36 -0
- package/dist/self-upgrade.js +78 -0
- package/dist/service.d.ts +7 -1
- package/dist/service.js +50 -7
- package/dist/sign-in.d.ts +41 -0
- package/dist/sign-in.js +239 -0
- package/dist/tool-call.d.ts +59 -0
- package/dist/tool-call.js +375 -0
- package/dist/trail/chain.d.ts +12 -0
- package/dist/trail/chain.js +89 -0
- package/dist/trail/deltas.d.ts +34 -0
- package/dist/trail/deltas.js +206 -0
- package/dist/trail/index.d.ts +27 -0
- package/dist/trail/index.js +128 -0
- package/dist/trail/keys.d.ts +14 -0
- package/dist/trail/keys.js +61 -0
- package/dist/trail/recorder.d.ts +118 -0
- package/dist/trail/recorder.js +697 -0
- package/dist/trail/rules.d.ts +30 -0
- package/dist/trail/rules.js +158 -0
- package/dist/trail/stream.d.ts +41 -0
- package/dist/trail/stream.js +86 -0
- package/dist/transport.d.ts +26 -0
- package/dist/transport.js +118 -0
- package/dist/work-dir.d.ts +11 -0
- package/dist/work-dir.js +69 -0
- package/dist/worktree-report.d.ts +10 -0
- package/dist/worktree-report.js +17 -0
- package/dist/worktrees.d.ts +16 -1
- package/dist/worktrees.js +62 -17
- package/package.json +4 -4
- package/skill/firefunc/SKILL.md +4 -3
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { createHash, createHmac, randomBytes, sign as edSign } from 'node:crypto';
|
|
2
|
+
export const TRAIL_SCHEMA_VERSION = 1;
|
|
3
|
+
const RECEIPT_FIELDS = new Set([
|
|
4
|
+
'event_hash',
|
|
5
|
+
'sig',
|
|
6
|
+
'key_id',
|
|
7
|
+
'received_at',
|
|
8
|
+
'clock_skew_ms',
|
|
9
|
+
'late',
|
|
10
|
+
'workspace_id',
|
|
11
|
+
'organization_id',
|
|
12
|
+
'project_id',
|
|
13
|
+
'altered_at_receipt',
|
|
14
|
+
]);
|
|
15
|
+
export function canonicalJson(value) {
|
|
16
|
+
if (value === null || typeof value !== 'object') {
|
|
17
|
+
if (typeof value === 'number' && !Number.isFinite(value))
|
|
18
|
+
return 'null';
|
|
19
|
+
return JSON.stringify(value) ?? 'null';
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
return `[${value.map((v) => (v === undefined ? 'null' : canonicalJson(v))).join(',')}]`;
|
|
23
|
+
}
|
|
24
|
+
const obj = value;
|
|
25
|
+
const keys = Object.keys(obj)
|
|
26
|
+
.filter((k) => obj[k] !== undefined)
|
|
27
|
+
.sort();
|
|
28
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalJson(obj[k])}`).join(',')}}`;
|
|
29
|
+
}
|
|
30
|
+
export function sha256Hex(text) {
|
|
31
|
+
return createHash('sha256').update(text).digest('hex');
|
|
32
|
+
}
|
|
33
|
+
export function genesisHash(runId, source) {
|
|
34
|
+
return sha256Hex(`firefunc-trail-v${TRAIL_SCHEMA_VERSION}:${runId}:${source}`);
|
|
35
|
+
}
|
|
36
|
+
export function eventHashOf(ev) {
|
|
37
|
+
const part = {};
|
|
38
|
+
for (const [k, v] of Object.entries(ev))
|
|
39
|
+
if (!RECEIPT_FIELDS.has(k) && v !== undefined)
|
|
40
|
+
part[k] = v;
|
|
41
|
+
return sha256Hex(`${String(part.prev_hash)}\n${canonicalJson(part)}`);
|
|
42
|
+
}
|
|
43
|
+
export function merkleRoot(hashes) {
|
|
44
|
+
if (hashes.length === 0)
|
|
45
|
+
return sha256Hex('');
|
|
46
|
+
let level = [...hashes];
|
|
47
|
+
while (level.length > 1) {
|
|
48
|
+
const next = [];
|
|
49
|
+
for (let i = 0; i < level.length; i += 2)
|
|
50
|
+
next.push(sha256Hex(level[i] + (level[i + 1] ?? level[i])));
|
|
51
|
+
level = next;
|
|
52
|
+
}
|
|
53
|
+
return level[0];
|
|
54
|
+
}
|
|
55
|
+
export function keyIdOf(publicKeyB64) {
|
|
56
|
+
return sha256Hex(Buffer.from(publicKeyB64, 'base64')).slice(0, 32);
|
|
57
|
+
}
|
|
58
|
+
export function signText(privateKey, text) {
|
|
59
|
+
return edSign(null, Buffer.from(text), privateKey).toString('base64');
|
|
60
|
+
}
|
|
61
|
+
export function digestText(d) {
|
|
62
|
+
const { sig: _s, key_id: _k, ...rest } = d;
|
|
63
|
+
return canonicalJson(rest);
|
|
64
|
+
}
|
|
65
|
+
export function hmacHex(key, bytes) {
|
|
66
|
+
return createHmac('sha256', key).update(bytes).digest('hex');
|
|
67
|
+
}
|
|
68
|
+
const CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
|
69
|
+
export function ulid(nowMs, random = randomBytes(10)) {
|
|
70
|
+
let t = Math.max(0, Math.floor(nowMs));
|
|
71
|
+
let time = '';
|
|
72
|
+
for (let i = 0; i < 10; i++) {
|
|
73
|
+
time = CROCKFORD[t % 32] + time;
|
|
74
|
+
t = Math.floor(t / 32);
|
|
75
|
+
}
|
|
76
|
+
let bits = 0;
|
|
77
|
+
let acc = 0;
|
|
78
|
+
let rand = '';
|
|
79
|
+
for (let i = 0; i < 10 && rand.length < 16; i++) {
|
|
80
|
+
acc = (acc << 8) | (random[i] ?? 0);
|
|
81
|
+
bits += 8;
|
|
82
|
+
while (bits >= 5 && rand.length < 16) {
|
|
83
|
+
rand += CROCKFORD[(acc >> (bits - 5)) & 31];
|
|
84
|
+
bits -= 5;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return time + rand.padEnd(16, '0');
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=chain.js.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type ExecResult } from '../exec.js';
|
|
2
|
+
export type GitRun = (args: string[], opts: {
|
|
3
|
+
cwd: string;
|
|
4
|
+
input?: string;
|
|
5
|
+
}) => Promise<ExecResult>;
|
|
6
|
+
export type FileDelta = {
|
|
7
|
+
path: string;
|
|
8
|
+
created: boolean;
|
|
9
|
+
deleted: boolean;
|
|
10
|
+
isBinary: boolean;
|
|
11
|
+
linesAdded: number;
|
|
12
|
+
linesRemoved: number;
|
|
13
|
+
hunks: number;
|
|
14
|
+
hunkRanges: Array<[number, number, number, number]>;
|
|
15
|
+
hashBefore?: string;
|
|
16
|
+
hashAfter?: string;
|
|
17
|
+
};
|
|
18
|
+
export declare function countDiff(diff: string): Pick<FileDelta, 'isBinary' | 'linesAdded' | 'linesRemoved' | 'hunks' | 'hunkRanges'>;
|
|
19
|
+
export declare function statusPaths(out: string): string[];
|
|
20
|
+
export declare function createDeltaTracker(opts: {
|
|
21
|
+
worktree: () => string | null;
|
|
22
|
+
contentKey: Buffer;
|
|
23
|
+
forensic: boolean;
|
|
24
|
+
git?: GitRun;
|
|
25
|
+
maxFiles?: number;
|
|
26
|
+
}): {
|
|
27
|
+
prime: () => Promise<boolean>;
|
|
28
|
+
snapshot: () => Promise<{
|
|
29
|
+
deltas: FileDelta[];
|
|
30
|
+
truncated: number;
|
|
31
|
+
} | null>;
|
|
32
|
+
head: () => Promise<string | null>;
|
|
33
|
+
readonly usable: boolean | null;
|
|
34
|
+
};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { existsSync, lstatSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { exec } from '../exec.js';
|
|
4
|
+
import { hmacHex } from './chain.js';
|
|
5
|
+
const defaultGit = (args, opts) => exec('git', args, {
|
|
6
|
+
cwd: opts.cwd,
|
|
7
|
+
timeoutMs: 30_000,
|
|
8
|
+
...(opts.input !== undefined ? { input: opts.input } : {}),
|
|
9
|
+
});
|
|
10
|
+
const EMPTY_BLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391';
|
|
11
|
+
const HUNK = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
|
|
12
|
+
export function countDiff(diff) {
|
|
13
|
+
let added = 0;
|
|
14
|
+
let removed = 0;
|
|
15
|
+
const ranges = [];
|
|
16
|
+
let binary = false;
|
|
17
|
+
let inBody = false;
|
|
18
|
+
for (const line of diff.split('\n')) {
|
|
19
|
+
if (/^Binary files .* differ$/.test(line) || line.startsWith('GIT binary patch'))
|
|
20
|
+
binary = true;
|
|
21
|
+
const h = HUNK.exec(line);
|
|
22
|
+
if (h) {
|
|
23
|
+
inBody = true;
|
|
24
|
+
ranges.push([
|
|
25
|
+
Number(h[1]),
|
|
26
|
+
h[2] === undefined ? 1 : Number(h[2]),
|
|
27
|
+
Number(h[3]),
|
|
28
|
+
h[4] === undefined ? 1 : Number(h[4]),
|
|
29
|
+
]);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (!inBody)
|
|
33
|
+
continue;
|
|
34
|
+
if (line.startsWith('+'))
|
|
35
|
+
added += 1;
|
|
36
|
+
else if (line.startsWith('-'))
|
|
37
|
+
removed += 1;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
isBinary: binary,
|
|
41
|
+
linesAdded: added,
|
|
42
|
+
linesRemoved: removed,
|
|
43
|
+
hunks: ranges.length,
|
|
44
|
+
hunkRanges: ranges,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function statusPaths(out) {
|
|
48
|
+
const paths = [];
|
|
49
|
+
for (const entry of out.split('\0')) {
|
|
50
|
+
if (entry.length < 4)
|
|
51
|
+
continue;
|
|
52
|
+
paths.push(entry.slice(3));
|
|
53
|
+
}
|
|
54
|
+
return paths;
|
|
55
|
+
}
|
|
56
|
+
export function createDeltaTracker(opts) {
|
|
57
|
+
const git = opts.git ?? defaultGit;
|
|
58
|
+
const maxFiles = opts.maxFiles ?? 200;
|
|
59
|
+
const known = new Map();
|
|
60
|
+
let baseline;
|
|
61
|
+
let lastHead = null;
|
|
62
|
+
let usable = null;
|
|
63
|
+
let emptyWritten = false;
|
|
64
|
+
const run = async (args, input) => {
|
|
65
|
+
const wt = opts.worktree();
|
|
66
|
+
if (!wt)
|
|
67
|
+
return null;
|
|
68
|
+
try {
|
|
69
|
+
return await git(args, { cwd: wt, ...(input !== undefined ? { input } : {}) });
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const head = async () => {
|
|
76
|
+
const r = await run(['rev-parse', '--verify', '-q', 'HEAD']);
|
|
77
|
+
const sha = r?.code === 0 ? r.stdout.trim() : '';
|
|
78
|
+
return /^[0-9a-f]{40,64}$/.test(sha) ? sha : null;
|
|
79
|
+
};
|
|
80
|
+
const prime = async () => {
|
|
81
|
+
if (usable !== null)
|
|
82
|
+
return usable;
|
|
83
|
+
const wt = opts.worktree();
|
|
84
|
+
if (!wt || !existsSync(join(wt, '.git'))) {
|
|
85
|
+
usable = false;
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
baseline = await head();
|
|
89
|
+
lastHead = baseline;
|
|
90
|
+
usable = true;
|
|
91
|
+
return true;
|
|
92
|
+
};
|
|
93
|
+
const keyed = (blob) => opts.forensic && blob ? hmacHex(opts.contentKey, `blob:${blob}`) : undefined;
|
|
94
|
+
const snapshot = async () => {
|
|
95
|
+
if (!(await prime()))
|
|
96
|
+
return null;
|
|
97
|
+
const wt = opts.worktree();
|
|
98
|
+
if (!wt)
|
|
99
|
+
return null;
|
|
100
|
+
const st = await run([
|
|
101
|
+
'--no-optional-locks',
|
|
102
|
+
'status',
|
|
103
|
+
'--porcelain=v1',
|
|
104
|
+
'-z',
|
|
105
|
+
'--untracked-files=all',
|
|
106
|
+
'--no-renames',
|
|
107
|
+
]);
|
|
108
|
+
if (!st || st.code !== 0)
|
|
109
|
+
return null;
|
|
110
|
+
const candidates = new Set([...statusPaths(st.stdout), ...known.keys()]);
|
|
111
|
+
const now = await head();
|
|
112
|
+
if (now && lastHead && now !== lastHead) {
|
|
113
|
+
const moved = await run(['diff', '--name-only', '-z', '--no-renames', lastHead, now]);
|
|
114
|
+
if (moved?.code === 0)
|
|
115
|
+
for (const p of moved.stdout.split('\0'))
|
|
116
|
+
if (p)
|
|
117
|
+
candidates.add(p);
|
|
118
|
+
}
|
|
119
|
+
lastHead = now;
|
|
120
|
+
const all = [...candidates].sort();
|
|
121
|
+
const paths = all.slice(0, maxFiles);
|
|
122
|
+
const truncated = all.length - paths.length;
|
|
123
|
+
if (paths.length === 0)
|
|
124
|
+
return { deltas: [], truncated };
|
|
125
|
+
const present = paths.filter((p) => {
|
|
126
|
+
try {
|
|
127
|
+
const s = lstatSync(join(wt, p));
|
|
128
|
+
return s.isFile() || s.isSymbolicLink();
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
const current = new Map(paths.map((p) => [p, null]));
|
|
135
|
+
if (present.length > 0) {
|
|
136
|
+
const h = await run(['hash-object', '-w', '--stdin-paths'], `${present.join('\n')}\n`);
|
|
137
|
+
if (!h || h.code !== 0)
|
|
138
|
+
return null;
|
|
139
|
+
const ids = h.stdout.trim().split('\n');
|
|
140
|
+
present.forEach((p, i) => current.set(p, ids[i] ?? null));
|
|
141
|
+
}
|
|
142
|
+
const before = new Map();
|
|
143
|
+
const fromBaseline = paths.filter((p) => !known.has(p));
|
|
144
|
+
for (const p of paths)
|
|
145
|
+
if (known.has(p))
|
|
146
|
+
before.set(p, known.get(p) ?? null);
|
|
147
|
+
if (fromBaseline.length > 0) {
|
|
148
|
+
for (const p of fromBaseline)
|
|
149
|
+
before.set(p, null);
|
|
150
|
+
if (baseline) {
|
|
151
|
+
const ls = await run(['ls-tree', '-z', baseline, '--', ...fromBaseline]);
|
|
152
|
+
if (ls?.code === 0) {
|
|
153
|
+
for (const entry of ls.stdout.split('\0')) {
|
|
154
|
+
const m = /^\d+ blob ([0-9a-f]+)\t(.+)$/s.exec(entry);
|
|
155
|
+
if (m)
|
|
156
|
+
before.set(m[2], m[1]);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const deltas = [];
|
|
162
|
+
for (const p of paths) {
|
|
163
|
+
const was = before.get(p) ?? null;
|
|
164
|
+
const is = current.get(p) ?? null;
|
|
165
|
+
known.set(p, is);
|
|
166
|
+
if (was === is)
|
|
167
|
+
continue;
|
|
168
|
+
if ((!was || !is) && !emptyWritten) {
|
|
169
|
+
await run(['hash-object', '-w', '--stdin'], '');
|
|
170
|
+
emptyWritten = true;
|
|
171
|
+
}
|
|
172
|
+
const d = await run([
|
|
173
|
+
'diff',
|
|
174
|
+
'--no-color',
|
|
175
|
+
'--no-ext-diff',
|
|
176
|
+
'--no-textconv',
|
|
177
|
+
'-U0',
|
|
178
|
+
was ?? EMPTY_BLOB,
|
|
179
|
+
is ?? EMPTY_BLOB,
|
|
180
|
+
]);
|
|
181
|
+
const counts = d && d.code === 0
|
|
182
|
+
? countDiff(d.stdout)
|
|
183
|
+
: { isBinary: false, linesAdded: 0, linesRemoved: 0, hunks: 0, hunkRanges: [] };
|
|
184
|
+
const hashBefore = keyed(was);
|
|
185
|
+
const hashAfter = keyed(is);
|
|
186
|
+
deltas.push({
|
|
187
|
+
path: p,
|
|
188
|
+
created: was === null,
|
|
189
|
+
deleted: is === null,
|
|
190
|
+
...counts,
|
|
191
|
+
...(hashBefore ? { hashBefore } : {}),
|
|
192
|
+
...(hashAfter ? { hashAfter } : {}),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
return { deltas, truncated };
|
|
196
|
+
};
|
|
197
|
+
return {
|
|
198
|
+
prime,
|
|
199
|
+
snapshot,
|
|
200
|
+
head,
|
|
201
|
+
get usable() {
|
|
202
|
+
return usable;
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
//# sourceMappingURL=deltas.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type DetailLevel, type PostTrail, type RunnerTrailInput, type TrailBatchOut, type TrailRecorder } from './recorder.js';
|
|
2
|
+
export type { RunnerTrailInput, TrailRecorder, DetailLevel, TrailBatchOut } from './recorder.js';
|
|
3
|
+
export declare function detailOf(job: {
|
|
4
|
+
trail?: unknown;
|
|
5
|
+
}): DetailLevel;
|
|
6
|
+
export declare function trailOff(job: {
|
|
7
|
+
trail?: unknown;
|
|
8
|
+
}): boolean;
|
|
9
|
+
export declare function emitTrailEvent(runId: string, input: RunnerTrailInput): void;
|
|
10
|
+
export declare function trailFor(runId: string): TrailRecorder | undefined;
|
|
11
|
+
export declare function trailPendingDir(runnerDir: string): string;
|
|
12
|
+
export declare function trailHeadsDir(runnerDir: string): string;
|
|
13
|
+
export declare function startRunTrail(opts: {
|
|
14
|
+
runId: string;
|
|
15
|
+
runnerDir: string;
|
|
16
|
+
detail: DetailLevel;
|
|
17
|
+
post: PostTrail;
|
|
18
|
+
worktree: () => string | null;
|
|
19
|
+
blank?: <T>(v: T) => T;
|
|
20
|
+
who?: Record<string, unknown>;
|
|
21
|
+
writeTools?: ReadonlySet<string>;
|
|
22
|
+
now?: () => Date;
|
|
23
|
+
}): TrailRecorder;
|
|
24
|
+
export declare function flushPendingTrails(runnerDir: string, post: (runId: string, batch: TrailBatchOut) => Promise<'ok' | 'gone' | 'refused'>): Promise<{
|
|
25
|
+
delivered: number;
|
|
26
|
+
kept: number;
|
|
27
|
+
}>;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, unlinkSync, writeFileSync, } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { machineTrailKeys } from './keys.js';
|
|
4
|
+
import { createTrailRecorder, DETAIL_LEVELS, } from './recorder.js';
|
|
5
|
+
const active = new Map();
|
|
6
|
+
export function detailOf(job) {
|
|
7
|
+
const d = job.trail?.detail;
|
|
8
|
+
return typeof d === 'string' && DETAIL_LEVELS.includes(d)
|
|
9
|
+
? d
|
|
10
|
+
: 'standard';
|
|
11
|
+
}
|
|
12
|
+
export function trailOff(job) {
|
|
13
|
+
return job.trail?.off === true;
|
|
14
|
+
}
|
|
15
|
+
export function emitTrailEvent(runId, input) {
|
|
16
|
+
const r = active.get(runId);
|
|
17
|
+
if (r)
|
|
18
|
+
void r.emit(input);
|
|
19
|
+
}
|
|
20
|
+
export function trailFor(runId) {
|
|
21
|
+
return active.get(runId);
|
|
22
|
+
}
|
|
23
|
+
export function trailPendingDir(runnerDir) {
|
|
24
|
+
return join(runnerDir, 'trail-pending');
|
|
25
|
+
}
|
|
26
|
+
export function trailHeadsDir(runnerDir) {
|
|
27
|
+
return join(runnerDir, 'trail-heads');
|
|
28
|
+
}
|
|
29
|
+
const HEAD_KEEP_MS = 8 * 24 * 60 * 60_000;
|
|
30
|
+
function readHead(runnerDir, runId) {
|
|
31
|
+
const file = join(trailHeadsDir(runnerDir), `${runId}.json`);
|
|
32
|
+
if (!existsSync(file))
|
|
33
|
+
return null;
|
|
34
|
+
try {
|
|
35
|
+
const h = JSON.parse(readFileSync(file, 'utf8'));
|
|
36
|
+
return typeof h.seq === 'number' && typeof h.prev === 'string' && Array.isArray(h.hashes)
|
|
37
|
+
? h
|
|
38
|
+
: null;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function keepHead(runnerDir, runId, head) {
|
|
45
|
+
const dir = trailHeadsDir(runnerDir);
|
|
46
|
+
mkdirSync(dir, { recursive: true });
|
|
47
|
+
writeFileSync(join(dir, `${runId}.json`), JSON.stringify(head), { mode: 0o600 });
|
|
48
|
+
try {
|
|
49
|
+
const cutoff = Date.now() - HEAD_KEEP_MS;
|
|
50
|
+
for (const name of readdirSync(dir)) {
|
|
51
|
+
const f = join(dir, name);
|
|
52
|
+
if (statSync(f).mtimeMs < cutoff)
|
|
53
|
+
unlinkSync(f);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function startRunTrail(opts) {
|
|
60
|
+
const keys = machineTrailKeys(opts.runnerDir);
|
|
61
|
+
const resume = readHead(opts.runnerDir, opts.runId);
|
|
62
|
+
const rec = createTrailRecorder({
|
|
63
|
+
resume,
|
|
64
|
+
runId: opts.runId,
|
|
65
|
+
detail: opts.detail,
|
|
66
|
+
keys,
|
|
67
|
+
post: opts.post,
|
|
68
|
+
worktree: opts.worktree,
|
|
69
|
+
...(opts.blank ? { blank: opts.blank } : {}),
|
|
70
|
+
...(opts.who ? { who: opts.who } : {}),
|
|
71
|
+
...(opts.writeTools ? { writeTools: opts.writeTools } : {}),
|
|
72
|
+
...(opts.now ? { now: opts.now } : {}),
|
|
73
|
+
keep: (batches) => {
|
|
74
|
+
const dir = trailPendingDir(opts.runnerDir);
|
|
75
|
+
mkdirSync(dir, { recursive: true });
|
|
76
|
+
writeFileSync(join(dir, `${opts.runId}.json`), JSON.stringify({ runId: opts.runId, batches }), { mode: 0o600 });
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
active.set(opts.runId, rec);
|
|
80
|
+
const finish = rec.finish.bind(rec);
|
|
81
|
+
rec.finish = async (info) => {
|
|
82
|
+
try {
|
|
83
|
+
await finish(info);
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
active.delete(opts.runId);
|
|
87
|
+
try {
|
|
88
|
+
keepHead(opts.runnerDir, opts.runId, rec.head());
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
return rec;
|
|
95
|
+
}
|
|
96
|
+
export async function flushPendingTrails(runnerDir, post) {
|
|
97
|
+
const dir = trailPendingDir(runnerDir);
|
|
98
|
+
const out = { delivered: 0, kept: 0 };
|
|
99
|
+
if (!existsSync(dir))
|
|
100
|
+
return out;
|
|
101
|
+
for (const name of readdirSync(dir)) {
|
|
102
|
+
if (!name.endsWith('.json'))
|
|
103
|
+
continue;
|
|
104
|
+
const file = join(dir, name);
|
|
105
|
+
let rec;
|
|
106
|
+
try {
|
|
107
|
+
rec = JSON.parse(readFileSync(file, 'utf8'));
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
let left = [...(rec.batches ?? [])];
|
|
113
|
+
try {
|
|
114
|
+
while (left.length > 0) {
|
|
115
|
+
await post(rec.runId, left[0]);
|
|
116
|
+
left = left.slice(1);
|
|
117
|
+
}
|
|
118
|
+
unlinkSync(file);
|
|
119
|
+
out.delivered += 1;
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
writeFileSync(file, JSON.stringify({ runId: rec.runId, batches: left }), { mode: 0o600 });
|
|
123
|
+
out.kept += 1;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type KeyObject } from 'node:crypto';
|
|
2
|
+
export type MachineTrailKeys = {
|
|
3
|
+
signing: {
|
|
4
|
+
keyId: string;
|
|
5
|
+
publicKey: string;
|
|
6
|
+
privateKey: KeyObject;
|
|
7
|
+
};
|
|
8
|
+
content: {
|
|
9
|
+
keyId: string;
|
|
10
|
+
key: Buffer;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export declare function trailKeysPath(runnerDir: string): string;
|
|
14
|
+
export declare function machineTrailKeys(runnerDir: string): MachineTrailKeys;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createPrivateKey, createPublicKey, generateKeyPairSync, randomBytes, } from 'node:crypto';
|
|
2
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { hmacHex, keyIdOf } from './chain.js';
|
|
5
|
+
export function trailKeysPath(runnerDir) {
|
|
6
|
+
return join(runnerDir, 'trail-keys.json');
|
|
7
|
+
}
|
|
8
|
+
function fromStored(s) {
|
|
9
|
+
const privateKey = createPrivateKey({
|
|
10
|
+
key: Buffer.from(s.signingPkcs8, 'base64'),
|
|
11
|
+
format: 'der',
|
|
12
|
+
type: 'pkcs8',
|
|
13
|
+
});
|
|
14
|
+
const publicKey = createPublicKey(privateKey)
|
|
15
|
+
.export({ format: 'der', type: 'spki' })
|
|
16
|
+
.toString('base64');
|
|
17
|
+
const content = Buffer.from(s.contentKey, 'base64');
|
|
18
|
+
return {
|
|
19
|
+
signing: { keyId: keyIdOf(publicKey), publicKey, privateKey },
|
|
20
|
+
content: {
|
|
21
|
+
keyId: hmacHex(content, 'firefunc-trail-content-key-id').slice(0, 32),
|
|
22
|
+
key: content,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
let cached = null;
|
|
27
|
+
export function machineTrailKeys(runnerDir) {
|
|
28
|
+
if (cached?.dir === runnerDir)
|
|
29
|
+
return cached.keys;
|
|
30
|
+
const file = trailKeysPath(runnerDir);
|
|
31
|
+
let keys = null;
|
|
32
|
+
if (existsSync(file)) {
|
|
33
|
+
try {
|
|
34
|
+
const s = JSON.parse(readFileSync(file, 'utf8'));
|
|
35
|
+
if (s.v === 1)
|
|
36
|
+
keys = fromStored(s);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
keys = null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (!keys) {
|
|
43
|
+
const { privateKey } = generateKeyPairSync('ed25519');
|
|
44
|
+
const s = {
|
|
45
|
+
v: 1,
|
|
46
|
+
signingPkcs8: privateKey.export({ format: 'der', type: 'pkcs8' }).toString('base64'),
|
|
47
|
+
contentKey: randomBytes(32).toString('base64'),
|
|
48
|
+
};
|
|
49
|
+
mkdirSync(runnerDir, { recursive: true });
|
|
50
|
+
writeFileSync(file, JSON.stringify(s), { mode: 0o600 });
|
|
51
|
+
try {
|
|
52
|
+
chmodSync(file, 0o600);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
}
|
|
56
|
+
keys = fromStored(s);
|
|
57
|
+
}
|
|
58
|
+
cached = { dir: runnerDir, keys };
|
|
59
|
+
return keys;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=keys.js.map
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { type GitRun } from './deltas.js';
|
|
2
|
+
import type { MachineTrailKeys } from './keys.js';
|
|
3
|
+
import { type Risk } from './rules.js';
|
|
4
|
+
export type DetailLevel = 'summary' | 'standard' | 'forensic';
|
|
5
|
+
export declare const DETAIL_LEVELS: readonly DetailLevel[];
|
|
6
|
+
export type TrailEventOut = Record<string, unknown> & {
|
|
7
|
+
event_id: string;
|
|
8
|
+
seq: number;
|
|
9
|
+
event_hash: string;
|
|
10
|
+
type: string;
|
|
11
|
+
category: string;
|
|
12
|
+
};
|
|
13
|
+
export type TrailDigestOut = {
|
|
14
|
+
run_id: string;
|
|
15
|
+
source: 'machine';
|
|
16
|
+
first_seq: number;
|
|
17
|
+
last_seq: number;
|
|
18
|
+
count: number;
|
|
19
|
+
last_hash: string;
|
|
20
|
+
root: string;
|
|
21
|
+
counts_by_category: Record<string, number>;
|
|
22
|
+
gaps: number;
|
|
23
|
+
detail: DetailLevel;
|
|
24
|
+
sealed_at: string;
|
|
25
|
+
key_id?: string;
|
|
26
|
+
sig?: string;
|
|
27
|
+
};
|
|
28
|
+
export type TrailBatchOut = {
|
|
29
|
+
machine_key: {
|
|
30
|
+
key_id: string;
|
|
31
|
+
public_key: string;
|
|
32
|
+
};
|
|
33
|
+
hmac_key_id: string;
|
|
34
|
+
detail: DetailLevel;
|
|
35
|
+
events: TrailEventOut[];
|
|
36
|
+
digest?: TrailDigestOut;
|
|
37
|
+
};
|
|
38
|
+
export type RunnerTrailInput = {
|
|
39
|
+
type: string;
|
|
40
|
+
readOnly?: boolean | null;
|
|
41
|
+
risk?: Risk;
|
|
42
|
+
action: Record<string, unknown> & {
|
|
43
|
+
tool: string;
|
|
44
|
+
action: string;
|
|
45
|
+
};
|
|
46
|
+
result?: Record<string, unknown>;
|
|
47
|
+
decision?: Record<string, unknown>;
|
|
48
|
+
gap?: Record<string, unknown>;
|
|
49
|
+
where?: Record<string, unknown>;
|
|
50
|
+
addendum?: {
|
|
51
|
+
reason: string;
|
|
52
|
+
original_event_id?: string;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
export type TrailHead = {
|
|
56
|
+
seq: number;
|
|
57
|
+
prev: string;
|
|
58
|
+
hashes: string[];
|
|
59
|
+
counts: Record<string, number>;
|
|
60
|
+
gaps: number;
|
|
61
|
+
};
|
|
62
|
+
export type PostTrail = (batch: TrailBatchOut) => Promise<'ok' | 'gone' | 'refused'>;
|
|
63
|
+
export declare function createTrailRecorder(opts: {
|
|
64
|
+
runId: string;
|
|
65
|
+
detail: DetailLevel;
|
|
66
|
+
keys: MachineTrailKeys;
|
|
67
|
+
post: PostTrail;
|
|
68
|
+
worktree: () => string | null;
|
|
69
|
+
blank?: <T>(v: T) => T;
|
|
70
|
+
keep?: (batches: TrailBatchOut[]) => void;
|
|
71
|
+
who?: Record<string, unknown>;
|
|
72
|
+
now?: () => Date;
|
|
73
|
+
git?: GitRun;
|
|
74
|
+
intervalMs?: number;
|
|
75
|
+
maxBatch?: number;
|
|
76
|
+
cap?: number;
|
|
77
|
+
writeTools?: ReadonlySet<string>;
|
|
78
|
+
resume?: TrailHead | null;
|
|
79
|
+
}): {
|
|
80
|
+
readonly runId: string;
|
|
81
|
+
readonly stats: {
|
|
82
|
+
recorded: number;
|
|
83
|
+
filtered: number;
|
|
84
|
+
dropped: number;
|
|
85
|
+
gaps: number;
|
|
86
|
+
gone: boolean;
|
|
87
|
+
};
|
|
88
|
+
head(): TrailHead;
|
|
89
|
+
feed(chunk: string): void;
|
|
90
|
+
setWho(extra: Record<string, unknown>): void;
|
|
91
|
+
emit(input: RunnerTrailInput): Promise<void>;
|
|
92
|
+
runStarted(where?: Record<string, unknown>): Promise<void>;
|
|
93
|
+
setupRan(info: {
|
|
94
|
+
command: string;
|
|
95
|
+
exitCode: number;
|
|
96
|
+
durationMs: number;
|
|
97
|
+
}): Promise<void>;
|
|
98
|
+
engineStarted(info: {
|
|
99
|
+
engine?: string;
|
|
100
|
+
model?: string | null;
|
|
101
|
+
where?: Record<string, unknown>;
|
|
102
|
+
}): Promise<void>;
|
|
103
|
+
engineEnded(info: {
|
|
104
|
+
exitCode: number;
|
|
105
|
+
durationMs: number;
|
|
106
|
+
}): Promise<void>;
|
|
107
|
+
pushed(info: {
|
|
108
|
+
branch: string;
|
|
109
|
+
}): Promise<void>;
|
|
110
|
+
finish(info: {
|
|
111
|
+
status: string;
|
|
112
|
+
pr?: {
|
|
113
|
+
number?: number;
|
|
114
|
+
url?: string;
|
|
115
|
+
};
|
|
116
|
+
}): Promise<void>;
|
|
117
|
+
};
|
|
118
|
+
export type TrailRecorder = ReturnType<typeof createTrailRecorder>;
|