@firefunc-agent/runner 0.7.1 → 0.9.3
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/LICENSE +7 -3
- package/README.md +296 -4
- package/dist/activity.d.ts +32 -0
- package/dist/activity.js +295 -0
- package/dist/api.d.ts +69 -1
- package/dist/api.js +114 -3
- package/dist/brief.d.ts +11 -0
- package/dist/brief.js +278 -0
- package/dist/build-info.json +4 -0
- package/dist/cli.js +59 -2
- package/dist/config.d.ts +7 -0
- package/dist/config.js +3 -0
- package/dist/cut.d.ts +45 -0
- package/dist/cut.js +105 -0
- package/dist/daemon.d.ts +16 -0
- package/dist/daemon.js +196 -7
- package/dist/declined.d.ts +11 -0
- package/dist/declined.js +27 -0
- package/dist/engines/antigravity.d.ts +2 -0
- package/dist/engines/antigravity.js +102 -0
- package/dist/engines/claude-budget.d.ts +1 -1
- package/dist/engines/claude-budget.js +2 -72
- package/dist/engines/claude.js +139 -2
- package/dist/engines/codex.js +10 -3
- package/dist/engines/copilot.d.ts +2 -0
- package/dist/engines/copilot.js +117 -0
- package/dist/engines/cursor.js +4 -0
- package/dist/engines/gemini.js +6 -1
- package/dist/engines/index.d.ts +2 -0
- package/dist/engines/index.js +22 -4
- package/dist/engines/types.d.ts +12 -1
- package/dist/exec.d.ts +3 -0
- package/dist/exec.js +48 -5
- package/dist/firefunc/agents.d.ts +2 -0
- package/dist/firefunc/agents.js +343 -0
- package/dist/firefunc/args.d.ts +18 -0
- package/dist/firefunc/args.js +82 -0
- package/dist/firefunc/auth.d.ts +21 -0
- package/dist/firefunc/auth.js +129 -0
- package/dist/firefunc/catalog.d.ts +10 -0
- package/dist/firefunc/catalog.js +201 -0
- package/dist/firefunc/cli.d.ts +2 -0
- package/dist/firefunc/cli.js +24 -0
- package/dist/firefunc/client.d.ts +36 -0
- package/dist/firefunc/client.js +177 -0
- package/dist/firefunc/command.d.ts +28 -0
- package/dist/firefunc/command.js +35 -0
- package/dist/firefunc/config.d.ts +29 -0
- package/dist/firefunc/config.js +111 -0
- package/dist/firefunc/configure.d.ts +6 -0
- package/dist/firefunc/configure.js +409 -0
- package/dist/firefunc/io.d.ts +25 -0
- package/dist/firefunc/io.js +14 -0
- package/dist/firefunc/main.d.ts +4 -0
- package/dist/firefunc/main.js +259 -0
- package/dist/firefunc/output.d.ts +29 -0
- package/dist/firefunc/output.js +131 -0
- package/dist/firefunc/prompt.d.ts +14 -0
- package/dist/firefunc/prompt.js +58 -0
- package/dist/firefunc/skill.d.ts +3 -0
- package/dist/firefunc/skill.js +80 -0
- package/dist/firefunc/tasks.d.ts +2 -0
- package/dist/firefunc/tasks.js +279 -0
- package/dist/firefunc/version.d.ts +2 -0
- package/dist/firefunc/version.js +8 -0
- package/dist/git-auth.d.ts +4 -1
- package/dist/git-auth.js +6 -2
- package/dist/held.d.ts +18 -0
- package/dist/held.js +95 -0
- package/dist/job.d.ts +3 -0
- package/dist/job.js +818 -102
- package/dist/keep-local.d.ts +12 -0
- package/dist/keep-local.js +651 -0
- package/dist/limit.d.ts +30 -0
- package/dist/limit.js +258 -0
- package/dist/locks.d.ts +14 -0
- package/dist/locks.js +164 -0
- package/dist/machine-logins.d.ts +19 -0
- package/dist/machine-logins.js +187 -0
- package/dist/mcp-login.d.ts +24 -0
- package/dist/mcp-login.js +993 -0
- package/dist/mcp-proxy.d.ts +21 -0
- package/dist/mcp-proxy.js +180 -0
- package/dist/mcp.d.ts +62 -0
- package/dist/mcp.js +611 -0
- package/dist/network.d.ts +20 -0
- package/dist/network.js +76 -0
- package/dist/orphans.d.ts +40 -0
- package/dist/orphans.js +235 -0
- package/dist/pending.d.ts +14 -0
- package/dist/pending.js +56 -0
- package/dist/pool.d.ts +7 -0
- package/dist/pool.js +57 -9
- package/dist/pr-title.d.ts +11 -0
- package/dist/pr-title.js +63 -0
- package/dist/recover.d.ts +10 -0
- package/dist/recover.js +104 -0
- package/dist/secret-scan.d.ts +19 -0
- package/dist/secret-scan.js +161 -0
- package/dist/session-stream.d.ts +9 -1
- package/dist/session-stream.js +20 -2
- package/dist/skills.d.ts +11 -0
- package/dist/skills.js +164 -0
- package/dist/stopped.d.ts +3 -0
- package/dist/stopped.js +10 -0
- package/dist/usage.d.ts +49 -0
- package/dist/usage.js +389 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +17 -0
- package/dist/viewer.js +7 -1
- package/dist/worktrees.d.ts +15 -0
- package/dist/worktrees.js +96 -0
- package/package.json +8 -5
- package/skill/firefunc/SKILL.md +222 -0
package/dist/network.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const NETWORK_SIGNS = [
|
|
2
|
+
/\bECONN(?:RESET|REFUSED|ABORTED)\b/i,
|
|
3
|
+
/\bENOTFOUND\b/i,
|
|
4
|
+
/\bEAI_AGAIN\b/i,
|
|
5
|
+
/\bETIMEDOUT\b/i,
|
|
6
|
+
/\bE(?:HOST|NET)UNREACH\b/i,
|
|
7
|
+
/\bEPIPE\b/i,
|
|
8
|
+
/\bfetch failed\b/i,
|
|
9
|
+
/\bsocket hang up\b/i,
|
|
10
|
+
/\bTLS handshake\b/i,
|
|
11
|
+
/\bgetaddrinfo\b/i,
|
|
12
|
+
/\bcould not resolve host\b/i,
|
|
13
|
+
/\bfailed to connect\b/i,
|
|
14
|
+
/\bconnection (?:refused|reset|timed out|error)\b/i,
|
|
15
|
+
/\bnetwork (?:error|is unreachable|unreachable)\b/i,
|
|
16
|
+
/\bunable to (?:access|connect)\b/i,
|
|
17
|
+
/\bthe remote end hung up\b/i,
|
|
18
|
+
/\bunexpected disconnect\b/i,
|
|
19
|
+
/\bHTTP\/?[12](?:\.[01])? 50[234]\b/i,
|
|
20
|
+
/\b(?:^|[^0-9])50[234]\b.*\b(?:gateway|unavailable|timeout)\b/i,
|
|
21
|
+
/\bgateway time-?out\b/i,
|
|
22
|
+
/\bservice unavailable\b/i,
|
|
23
|
+
/\bbad gateway\b/i,
|
|
24
|
+
];
|
|
25
|
+
export function isNetworkError(input) {
|
|
26
|
+
const text = input instanceof Error
|
|
27
|
+
? `${input.message} ${input.cause?.message ?? ''} ${input.cause?.code ?? ''}`
|
|
28
|
+
: String(input ?? '');
|
|
29
|
+
return NETWORK_SIGNS.some((re) => re.test(text));
|
|
30
|
+
}
|
|
31
|
+
export function networkFailureHint(text) {
|
|
32
|
+
if (!isNetworkError(text))
|
|
33
|
+
return null;
|
|
34
|
+
const m = /(fetch failed|ECONNRESET|ENOTFOUND|EAI_AGAIN|ETIMEDOUT|socket hang up|connection (?:refused|reset|timed out|error)|network (?:error|is unreachable)|TLS handshake|could not resolve host|50[234])/i.exec(text);
|
|
35
|
+
return `the network went away (${m?.[1] ?? 'transport error'})`;
|
|
36
|
+
}
|
|
37
|
+
export const netDeps = {
|
|
38
|
+
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
39
|
+
now: () => Date.now(),
|
|
40
|
+
};
|
|
41
|
+
export async function waitForNetwork(opts) {
|
|
42
|
+
const started = netDeps.now();
|
|
43
|
+
let delay = opts.initialDelayMs ?? 5_000;
|
|
44
|
+
const cap = opts.maxDelayMs ?? 60_000;
|
|
45
|
+
for (;;) {
|
|
46
|
+
if (await opts.probe())
|
|
47
|
+
return true;
|
|
48
|
+
const waited = netDeps.now() - started;
|
|
49
|
+
if (waited + delay > opts.maxWaitMs)
|
|
50
|
+
return false;
|
|
51
|
+
opts.log?.(`waiting for the network — retrying in ${Math.round(delay / 1000)}s (${Math.round(waited / 60_000)} min so far, giving up after ${Math.round(opts.maxWaitMs / 60_000)})`);
|
|
52
|
+
await netDeps.sleep(delay);
|
|
53
|
+
delay = Math.min(delay * 2, cap);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export async function withNetworkRetry(fn, opts) {
|
|
57
|
+
const started = netDeps.now();
|
|
58
|
+
for (;;) {
|
|
59
|
+
try {
|
|
60
|
+
return await fn();
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
if (!isNetworkError(err))
|
|
64
|
+
throw err;
|
|
65
|
+
const left = opts.maxWaitMs - (netDeps.now() - started);
|
|
66
|
+
opts.log?.(`${opts.label}: ${err.message} — the network seems to be away`);
|
|
67
|
+
if (left <= 0)
|
|
68
|
+
throw err;
|
|
69
|
+
const back = await waitForNetwork({ ...opts, maxWaitMs: left });
|
|
70
|
+
if (!back)
|
|
71
|
+
throw err;
|
|
72
|
+
opts.log?.(`${opts.label}: the network is back — trying again`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=network.js.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export type ProcessRow = {
|
|
2
|
+
pid: number;
|
|
3
|
+
ppid: number;
|
|
4
|
+
pgid: number;
|
|
5
|
+
command: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function listProcesses(): Promise<ProcessRow[]>;
|
|
8
|
+
export declare function workingDirs(pids: number[]): Promise<Map<number, string>>;
|
|
9
|
+
export type EngineGroup = {
|
|
10
|
+
pgid: number;
|
|
11
|
+
startedAt: number;
|
|
12
|
+
leaderExited: boolean;
|
|
13
|
+
};
|
|
14
|
+
export type OrphanScope = {
|
|
15
|
+
roots: string[];
|
|
16
|
+
reviewCopiesOf?: string;
|
|
17
|
+
groups?: EngineGroup[];
|
|
18
|
+
self?: number;
|
|
19
|
+
};
|
|
20
|
+
export type SelectScope = Omit<OrphanScope, 'groups'> & {
|
|
21
|
+
groups?: number[];
|
|
22
|
+
};
|
|
23
|
+
export declare function groupsStillOurs(groups: EngineGroup[], rows: ProcessRow[]): Promise<number[]>;
|
|
24
|
+
export declare function selectOrphans(rows: ProcessRow[], cwds: Map<number, string>, scope: SelectScope): number[];
|
|
25
|
+
export declare function isAlive(pid: number): boolean;
|
|
26
|
+
export type OrphanSweep = {
|
|
27
|
+
stopped: Array<{
|
|
28
|
+
pid: number;
|
|
29
|
+
program: string;
|
|
30
|
+
}>;
|
|
31
|
+
survivors: number[];
|
|
32
|
+
};
|
|
33
|
+
export declare function killOrphans(scope: OrphanScope, opts?: {
|
|
34
|
+
graceMs?: number;
|
|
35
|
+
log?: (line: string) => void;
|
|
36
|
+
}): Promise<OrphanSweep>;
|
|
37
|
+
export declare function reviewCopiesOf(worktree: string): string[];
|
|
38
|
+
export declare function trackEngineGroup(pgid: number): void;
|
|
39
|
+
export declare function untrackEngineGroup(pgid: number): void;
|
|
40
|
+
export declare function signalEngineGroups(sig: NodeJS.Signals): number;
|
package/dist/orphans.js
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { readlinkSync, readdirSync, realpathSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { basename, join } from 'node:path';
|
|
4
|
+
import { exec } from './exec.js';
|
|
5
|
+
export async function listProcesses() {
|
|
6
|
+
if (process.platform === 'win32')
|
|
7
|
+
return [];
|
|
8
|
+
const ps = await exec('ps', ['-A', '-ww', '-o', 'pid=,ppid=,pgid=,command='], {
|
|
9
|
+
timeoutMs: 20_000,
|
|
10
|
+
}).catch(() => null);
|
|
11
|
+
if (!ps || ps.code !== 0)
|
|
12
|
+
return [];
|
|
13
|
+
const rows = [];
|
|
14
|
+
for (const line of ps.stdout.split('\n')) {
|
|
15
|
+
const m = /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(.*)$/.exec(line);
|
|
16
|
+
if (!m)
|
|
17
|
+
continue;
|
|
18
|
+
rows.push({ pid: Number(m[1]), ppid: Number(m[2]), pgid: Number(m[3]), command: m[4].trim() });
|
|
19
|
+
}
|
|
20
|
+
return rows;
|
|
21
|
+
}
|
|
22
|
+
export async function workingDirs(pids) {
|
|
23
|
+
const out = new Map();
|
|
24
|
+
if (process.platform === 'win32')
|
|
25
|
+
return out;
|
|
26
|
+
if (process.platform === 'linux') {
|
|
27
|
+
for (const pid of pids) {
|
|
28
|
+
try {
|
|
29
|
+
out.set(pid, readlinkSync(`/proc/${pid}/cwd`).replace(/ \(deleted\)$/, ''));
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
const uid = process.getuid?.();
|
|
37
|
+
const r = await exec('lsof', ['-a', '-d', 'cwd', ...(uid === undefined ? [] : ['-u', String(uid)]), '-Fpn'], { timeoutMs: 30_000 }).catch(() => null);
|
|
38
|
+
if (!r?.stdout)
|
|
39
|
+
return out;
|
|
40
|
+
let pid = 0;
|
|
41
|
+
for (const line of r.stdout.split('\n')) {
|
|
42
|
+
if (line.startsWith('p'))
|
|
43
|
+
pid = Number(line.slice(1));
|
|
44
|
+
else if (line.startsWith('n') && pid > 0)
|
|
45
|
+
out.set(pid, line.slice(1));
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
function spellings(path) {
|
|
50
|
+
const clean = path.replace(/\/+$/, '');
|
|
51
|
+
const out = new Set([clean]);
|
|
52
|
+
try {
|
|
53
|
+
out.add(realpathSync(clean));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
}
|
|
57
|
+
for (const p of [...out]) {
|
|
58
|
+
if (p.startsWith('/private/'))
|
|
59
|
+
out.add(p.slice('/private'.length));
|
|
60
|
+
else if (p.startsWith('/var/') || p.startsWith('/tmp/'))
|
|
61
|
+
out.add(`/private${p}`);
|
|
62
|
+
}
|
|
63
|
+
return [...out].filter((p) => p.length > 1);
|
|
64
|
+
}
|
|
65
|
+
const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
66
|
+
async function startedAtOf(pid) {
|
|
67
|
+
const r = await exec('ps', ['-o', 'etime=', '-p', String(pid)], { timeoutMs: 10_000 }).catch(() => null);
|
|
68
|
+
const m = /^\s*(?:(\d+)-)?(?:(\d+):)?(\d+):(\d+)\s*$/.exec(r?.stdout ?? '');
|
|
69
|
+
if (!r || r.code !== 0 || !m)
|
|
70
|
+
return null;
|
|
71
|
+
const secs = Number(m[1] ?? 0) * 86_400 + Number(m[2] ?? 0) * 3_600 + Number(m[3]) * 60 + Number(m[4]);
|
|
72
|
+
return Date.now() - secs * 1000;
|
|
73
|
+
}
|
|
74
|
+
export async function groupsStillOurs(groups, rows) {
|
|
75
|
+
const out = [];
|
|
76
|
+
for (const g of groups) {
|
|
77
|
+
if (!(g.pgid > 1))
|
|
78
|
+
continue;
|
|
79
|
+
const leader = rows.some((r) => r.pid === g.pgid);
|
|
80
|
+
if (!leader) {
|
|
81
|
+
out.push(g.pgid);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (g.leaderExited)
|
|
85
|
+
continue;
|
|
86
|
+
const started = await startedAtOf(g.pgid);
|
|
87
|
+
if (started !== null && Math.abs(started - g.startedAt) < 15_000)
|
|
88
|
+
out.push(g.pgid);
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
export function selectOrphans(rows, cwds, scope) {
|
|
93
|
+
const self = scope.self ?? process.pid;
|
|
94
|
+
const byPid = new Map(rows.map((r) => [r.pid, r]));
|
|
95
|
+
const protectedPids = new Set([0, 1, self]);
|
|
96
|
+
for (let p = byPid.get(self)?.ppid; p && !protectedPids.has(p); p = byPid.get(p)?.ppid) {
|
|
97
|
+
protectedPids.add(p);
|
|
98
|
+
}
|
|
99
|
+
const roots = scope.roots.flatMap(spellings);
|
|
100
|
+
const inRoot = roots.map((r) => new RegExp(`${escapeRe(r)}(?:/|\\s|$|['"])`));
|
|
101
|
+
const review = scope.reviewCopiesOf
|
|
102
|
+
? new RegExp(`/ff-review-[^/\\s]*-${escapeRe(scope.reviewCopiesOf)}(?:/|\\s|$|['"])`)
|
|
103
|
+
: null;
|
|
104
|
+
const groups = new Set((scope.groups ?? []).filter((g) => g > 1));
|
|
105
|
+
const matches = (text) => inRoot.some((re) => re.test(text)) || !!review?.test(text);
|
|
106
|
+
const hit = new Set();
|
|
107
|
+
for (const r of rows) {
|
|
108
|
+
if (protectedPids.has(r.pid))
|
|
109
|
+
continue;
|
|
110
|
+
const cwd = cwds.get(r.pid);
|
|
111
|
+
if (groups.has(r.pgid) || (cwd && matches(`${cwd}/`)) || matches(r.command))
|
|
112
|
+
hit.add(r.pid);
|
|
113
|
+
}
|
|
114
|
+
const children = new Map();
|
|
115
|
+
for (const r of rows) {
|
|
116
|
+
const list = children.get(r.ppid) ?? [];
|
|
117
|
+
list.push(r.pid);
|
|
118
|
+
children.set(r.ppid, list);
|
|
119
|
+
}
|
|
120
|
+
const queue = [...hit];
|
|
121
|
+
while (queue.length) {
|
|
122
|
+
const pid = queue.shift();
|
|
123
|
+
for (const child of children.get(pid) ?? []) {
|
|
124
|
+
if (hit.has(child) || protectedPids.has(child))
|
|
125
|
+
continue;
|
|
126
|
+
hit.add(child);
|
|
127
|
+
queue.push(child);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return [...hit].sort((a, b) => a - b);
|
|
131
|
+
}
|
|
132
|
+
export function isAlive(pid) {
|
|
133
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
134
|
+
return false;
|
|
135
|
+
try {
|
|
136
|
+
process.kill(pid, 0);
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
catch (e) {
|
|
140
|
+
return e.code === 'EPERM';
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function signal(pid, sig) {
|
|
144
|
+
try {
|
|
145
|
+
process.kill(pid, sig);
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function programOf(command) {
|
|
151
|
+
const first = command.split(/\s+/)[0] ?? '';
|
|
152
|
+
return basename(first) || 'a process';
|
|
153
|
+
}
|
|
154
|
+
export async function killOrphans(scope, opts = {}) {
|
|
155
|
+
const result = { stopped: [], survivors: [] };
|
|
156
|
+
try {
|
|
157
|
+
if (process.platform === 'win32')
|
|
158
|
+
return result;
|
|
159
|
+
const rows = await listProcesses();
|
|
160
|
+
if (rows.length === 0)
|
|
161
|
+
return result;
|
|
162
|
+
const groups = await groupsStillOurs(scope.groups ?? [], rows);
|
|
163
|
+
const select = { ...scope, groups };
|
|
164
|
+
for (const g of groups)
|
|
165
|
+
signal(-g, 'SIGTERM');
|
|
166
|
+
const cwds = await workingDirs(rows.map((r) => r.pid));
|
|
167
|
+
const pids = selectOrphans(rows, cwds, select);
|
|
168
|
+
const byPid = new Map(rows.map((r) => [r.pid, r]));
|
|
169
|
+
for (const pid of pids) {
|
|
170
|
+
signal(pid, 'SIGTERM');
|
|
171
|
+
result.stopped.push({ pid, program: programOf(byPid.get(pid)?.command ?? '') });
|
|
172
|
+
}
|
|
173
|
+
if (pids.length === 0)
|
|
174
|
+
return result;
|
|
175
|
+
await new Promise((r) => setTimeout(r, opts.graceMs ?? 1500));
|
|
176
|
+
for (const pid of pids)
|
|
177
|
+
if (isAlive(pid))
|
|
178
|
+
signal(pid, 'SIGKILL');
|
|
179
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
180
|
+
const again = await listProcesses();
|
|
181
|
+
const againCwds = await workingDirs(again.map((r) => r.pid));
|
|
182
|
+
result.survivors = selectOrphans(again, againCwds, { ...select, groups: [] });
|
|
183
|
+
for (const pid of result.survivors)
|
|
184
|
+
signal(pid, 'SIGKILL');
|
|
185
|
+
if (result.stopped.length > 0) {
|
|
186
|
+
opts.log?.(`stopped ${result.stopped.length} process(es) the session left running in its checkout: ` +
|
|
187
|
+
result.stopped
|
|
188
|
+
.slice(0, 8)
|
|
189
|
+
.map((p) => `${p.program} (${p.pid})`)
|
|
190
|
+
.join(', ') +
|
|
191
|
+
(result.stopped.length > 8 ? `, and ${result.stopped.length - 8} more` : ''));
|
|
192
|
+
}
|
|
193
|
+
if (result.survivors.length > 0) {
|
|
194
|
+
opts.log?.(`still running in the checkout after SIGKILL: ${result.survivors.join(', ')} — the directory is removed anyway`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch (e) {
|
|
198
|
+
opts.log?.(`could not look for processes left in the checkout: ${e.message}`);
|
|
199
|
+
}
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
export function reviewCopiesOf(worktree) {
|
|
203
|
+
const base = basename(worktree);
|
|
204
|
+
const out = new Set();
|
|
205
|
+
for (const dir of new Set([tmpdir(), '/tmp'])) {
|
|
206
|
+
let names;
|
|
207
|
+
try {
|
|
208
|
+
names = readdirSync(dir);
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
for (const n of names) {
|
|
214
|
+
if (n.startsWith('ff-review-') && n.endsWith(`-${base}`))
|
|
215
|
+
out.add(join(dir, n));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return [...out];
|
|
219
|
+
}
|
|
220
|
+
const engineGroups = new Set();
|
|
221
|
+
export function trackEngineGroup(pgid) {
|
|
222
|
+
if (pgid > 1)
|
|
223
|
+
engineGroups.add(pgid);
|
|
224
|
+
}
|
|
225
|
+
export function untrackEngineGroup(pgid) {
|
|
226
|
+
engineGroups.delete(pgid);
|
|
227
|
+
}
|
|
228
|
+
export function signalEngineGroups(sig) {
|
|
229
|
+
if (process.platform === 'win32')
|
|
230
|
+
return 0;
|
|
231
|
+
for (const g of engineGroups)
|
|
232
|
+
signal(-g, sig);
|
|
233
|
+
return engineGroups.size;
|
|
234
|
+
}
|
|
235
|
+
//# sourceMappingURL=orphans.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ResultPayload, RunnerApi } from './api.js';
|
|
2
|
+
export type PendingResult = {
|
|
3
|
+
runId: string;
|
|
4
|
+
payload: ResultPayload;
|
|
5
|
+
savedAt: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function pendingDir(runnerDir: string): string;
|
|
8
|
+
export declare function savePendingResult(runnerDir: string, runId: string, payload: ResultPayload): string;
|
|
9
|
+
export declare function listPendingResults(runnerDir: string): PendingResult[];
|
|
10
|
+
export declare function flushPendingResults(runnerDir: string, api: Pick<RunnerApi, 'result'>, log?: (line: string) => void): Promise<{
|
|
11
|
+
delivered: number;
|
|
12
|
+
refused: number;
|
|
13
|
+
kept: number;
|
|
14
|
+
}>;
|
package/dist/pending.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync, } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { isNetworkError } from './network.js';
|
|
4
|
+
export function pendingDir(runnerDir) {
|
|
5
|
+
return join(runnerDir, 'pending');
|
|
6
|
+
}
|
|
7
|
+
export function savePendingResult(runnerDir, runId, payload) {
|
|
8
|
+
const dir = pendingDir(runnerDir);
|
|
9
|
+
mkdirSync(dir, { recursive: true });
|
|
10
|
+
const file = join(dir, `${runId}.json`);
|
|
11
|
+
const rec = { runId, payload, savedAt: new Date().toISOString() };
|
|
12
|
+
writeFileSync(file, JSON.stringify(rec, null, 2));
|
|
13
|
+
return file;
|
|
14
|
+
}
|
|
15
|
+
export function listPendingResults(runnerDir) {
|
|
16
|
+
const dir = pendingDir(runnerDir);
|
|
17
|
+
if (!existsSync(dir))
|
|
18
|
+
return [];
|
|
19
|
+
const out = [];
|
|
20
|
+
for (const name of readdirSync(dir)) {
|
|
21
|
+
if (!name.endsWith('.json'))
|
|
22
|
+
continue;
|
|
23
|
+
try {
|
|
24
|
+
const rec = JSON.parse(readFileSync(join(dir, name), 'utf8'));
|
|
25
|
+
if (rec && typeof rec.runId === 'string' && rec.payload)
|
|
26
|
+
out.push(rec);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return out.sort((a, b) => a.savedAt.localeCompare(b.savedAt));
|
|
32
|
+
}
|
|
33
|
+
export async function flushPendingResults(runnerDir, api, log) {
|
|
34
|
+
const summary = { delivered: 0, refused: 0, kept: 0 };
|
|
35
|
+
for (const rec of listPendingResults(runnerDir)) {
|
|
36
|
+
const file = join(pendingDir(runnerDir), `${rec.runId}.json`);
|
|
37
|
+
try {
|
|
38
|
+
await api.result(rec.runId, rec.payload);
|
|
39
|
+
unlinkSync(file);
|
|
40
|
+
summary.delivered += 1;
|
|
41
|
+
log?.(`delivered the result kept for ${rec.runId} since ${rec.savedAt}`);
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
if (isNetworkError(err)) {
|
|
45
|
+
summary.kept += 1;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
unlinkSync(file);
|
|
49
|
+
summary.refused += 1;
|
|
50
|
+
log?.(`FireFunc refused the result kept for ${rec.runId}: ${err.message}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return summary;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=pending.js.map
|
package/dist/pool.d.ts
CHANGED
|
@@ -8,5 +8,12 @@ export type JobPoolDeps<J> = {
|
|
|
8
8
|
idleDelayMs?: number;
|
|
9
9
|
maxIdleDelayMs?: number;
|
|
10
10
|
sleep?: (ms: number) => Promise<void>;
|
|
11
|
+
paused?: () => boolean;
|
|
12
|
+
heartbeat?: (state: {
|
|
13
|
+
draining: boolean;
|
|
14
|
+
paused: boolean;
|
|
15
|
+
}) => Promise<J | null>;
|
|
16
|
+
heartbeatMs?: number;
|
|
17
|
+
now?: () => number;
|
|
11
18
|
};
|
|
12
19
|
export declare function runJobPool<J>(deps: JobPoolDeps<J>): Promise<void>;
|
package/dist/pool.js
CHANGED
|
@@ -7,13 +7,67 @@ export async function runJobPool(deps) {
|
|
|
7
7
|
const idleDelay = () => Math.min(maxIdleMs, baseIdleMs * 2 ** Math.min(emptyClaims, 10));
|
|
8
8
|
const cap = () => Math.max(1, Math.floor(typeof deps.maxParallel === 'function' ? deps.maxParallel() : deps.maxParallel));
|
|
9
9
|
const active = new Set();
|
|
10
|
-
|
|
10
|
+
const now = deps.now ?? (() => Date.now());
|
|
11
|
+
const heartbeatMs = deps.heartbeatMs ?? 30_000;
|
|
12
|
+
let lastContact = now();
|
|
13
|
+
const spawn = (job) => {
|
|
14
|
+
deps.onStart?.(job, active.size + 1);
|
|
15
|
+
const p = deps.run(job).finally(() => {
|
|
16
|
+
active.delete(p);
|
|
17
|
+
});
|
|
18
|
+
active.add(p);
|
|
19
|
+
};
|
|
20
|
+
const beat = async (state) => {
|
|
21
|
+
if (!deps.heartbeat || now() - lastContact < heartbeatMs)
|
|
22
|
+
return;
|
|
23
|
+
lastContact = now();
|
|
24
|
+
const job = await deps.heartbeat(state).catch(() => null);
|
|
25
|
+
if (job != null)
|
|
26
|
+
spawn(job);
|
|
27
|
+
};
|
|
28
|
+
const napUntilBeat = async () => {
|
|
29
|
+
const wait = Math.max(50, heartbeatMs - (now() - lastContact));
|
|
30
|
+
if (active.size > 0)
|
|
31
|
+
await Promise.race([...active, sleep(wait)]);
|
|
32
|
+
else
|
|
33
|
+
await sleep(wait);
|
|
34
|
+
};
|
|
35
|
+
for (;;) {
|
|
36
|
+
if (deps.stopping()) {
|
|
37
|
+
if (active.size === 0)
|
|
38
|
+
break;
|
|
39
|
+
if (!deps.heartbeat) {
|
|
40
|
+
await Promise.race(active);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
await beat({ draining: true, paused: false });
|
|
44
|
+
if (active.size > 0)
|
|
45
|
+
await napUntilBeat();
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (deps.paused?.()) {
|
|
49
|
+
if (deps.heartbeat) {
|
|
50
|
+
await beat({ draining: false, paused: true });
|
|
51
|
+
await napUntilBeat();
|
|
52
|
+
}
|
|
53
|
+
else if (active.size > 0) {
|
|
54
|
+
await Promise.race([...active, sleep(idleDelay())]);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
await sleep(idleDelay());
|
|
58
|
+
}
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
11
61
|
if (active.size >= cap()) {
|
|
12
|
-
|
|
62
|
+
if (deps.heartbeat)
|
|
63
|
+
await Promise.race([...active, sleep(heartbeatMs)]);
|
|
64
|
+
else
|
|
65
|
+
await Promise.race(active);
|
|
13
66
|
continue;
|
|
14
67
|
}
|
|
15
68
|
let job;
|
|
16
69
|
try {
|
|
70
|
+
lastContact = now();
|
|
17
71
|
job = await deps.claim();
|
|
18
72
|
}
|
|
19
73
|
catch (err) {
|
|
@@ -33,13 +87,7 @@ export async function runJobPool(deps) {
|
|
|
33
87
|
continue;
|
|
34
88
|
}
|
|
35
89
|
emptyClaims = 0;
|
|
36
|
-
|
|
37
|
-
break;
|
|
38
|
-
deps.onStart?.(job, active.size + 1);
|
|
39
|
-
const p = deps.run(job).finally(() => {
|
|
40
|
-
active.delete(p);
|
|
41
|
-
});
|
|
42
|
-
active.add(p);
|
|
90
|
+
spawn(job);
|
|
43
91
|
}
|
|
44
92
|
if (active.size > 0)
|
|
45
93
|
await Promise.allSettled(active);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type ChangeKind = 'fix' | 'test';
|
|
2
|
+
export declare function withoutConventionalPrefixes(title: string): string;
|
|
3
|
+
export declare function isTestPath(path: string): boolean;
|
|
4
|
+
export declare function changeKind(paths: string[] | null): ChangeKind;
|
|
5
|
+
export declare function pullRequestTitle(job: {
|
|
6
|
+
title: string | null;
|
|
7
|
+
externalId: string | null;
|
|
8
|
+
}, kind: ChangeKind): string;
|
|
9
|
+
export declare function pullRequestBody(kind: ChangeKind, engineName: string, externalId: string | null, opts?: {
|
|
10
|
+
continuesBranch?: boolean;
|
|
11
|
+
}): string;
|
package/dist/pr-title.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const CONVENTIONAL_PREFIXES = /^(?:\s*(?:fix|feat|chore|refactor|docs|test)(?:\([^)]*\))?!?:\s*)+/i;
|
|
2
|
+
export function withoutConventionalPrefixes(title) {
|
|
3
|
+
return title.replace(CONVENTIONAL_PREFIXES, '').trim();
|
|
4
|
+
}
|
|
5
|
+
const TEST_DIRS = new Set([
|
|
6
|
+
'test',
|
|
7
|
+
'tests',
|
|
8
|
+
'__tests__',
|
|
9
|
+
'__test__',
|
|
10
|
+
'e2e',
|
|
11
|
+
'cypress',
|
|
12
|
+
'testdata',
|
|
13
|
+
'test-data',
|
|
14
|
+
'test_data',
|
|
15
|
+
'testfixtures',
|
|
16
|
+
'__snapshots__',
|
|
17
|
+
'__mocks__',
|
|
18
|
+
'__fixtures__',
|
|
19
|
+
'androidtest',
|
|
20
|
+
'integrationtest',
|
|
21
|
+
'integration-tests',
|
|
22
|
+
'integration_tests',
|
|
23
|
+
]);
|
|
24
|
+
const SPEC_DIRS = new Set(['spec', 'specs']);
|
|
25
|
+
const CODE_FILE = /\.(?:[cm]?[jt]sx?|mts|cts|vue|svelte|py|rb|php|go|rs|java|kt|kts|groovy|scala|cs|fs|swift|dart|exs?|m|mm|c|cc|cpp|h|hpp|lua)$/i;
|
|
26
|
+
const TEST_FILES = [
|
|
27
|
+
/\.(?:test|spec|e2e|e2e-spec|cy)\.(?:[cm]?[jt]sx?|mts|cts|vue|svelte|py|rb|php|dart)$/i,
|
|
28
|
+
/_(?:unit)?test\.(?:go|py|rb|exs?|dart|c|cc|cpp|rs|[cm]?[jt]sx?)$/,
|
|
29
|
+
/^test_[^/]*\.py$|^tests?\.py$|_spec\.rb$/,
|
|
30
|
+
/[a-z0-9](?:Test|Tests|IT|Spec|Specs)\.(?:java|kt|kts|groovy|scala|cs|fs|swift|php|m|mm)$/,
|
|
31
|
+
/\.snap$/,
|
|
32
|
+
];
|
|
33
|
+
const TEST_CONFIG = /^(?:(?:vitest|jest|playwright|cypress|ava|wdio)\.(?:config|setup|workspace)\.[cm]?[jt]s|karma\.conf\.[cm]?[jt]s|setupTests\.[cm]?[jt]sx?|conftest\.py|pytest\.ini|\.mocharc(?:\.[a-z]+)?)$/;
|
|
34
|
+
export function isTestPath(path) {
|
|
35
|
+
const parts = path.split('/').filter(Boolean);
|
|
36
|
+
const file = parts.pop();
|
|
37
|
+
if (!file)
|
|
38
|
+
return false;
|
|
39
|
+
if (parts.some((dir) => TEST_DIRS.has(dir.toLowerCase())))
|
|
40
|
+
return true;
|
|
41
|
+
if (parts.some((dir) => SPEC_DIRS.has(dir.toLowerCase())) && CODE_FILE.test(file))
|
|
42
|
+
return true;
|
|
43
|
+
return TEST_CONFIG.test(file) || TEST_FILES.some((re) => re.test(file));
|
|
44
|
+
}
|
|
45
|
+
export function changeKind(paths) {
|
|
46
|
+
return paths && paths.length > 0 && paths.every(isTestPath) ? 'test' : 'fix';
|
|
47
|
+
}
|
|
48
|
+
export function pullRequestTitle(job, kind) {
|
|
49
|
+
const words = withoutConventionalPrefixes(job.title ?? '') ||
|
|
50
|
+
job.externalId ||
|
|
51
|
+
(kind === 'test' ? 'tests by FireFunc' : 'FireFunc auto-fix');
|
|
52
|
+
return `${kind}: ${words}`;
|
|
53
|
+
}
|
|
54
|
+
export function pullRequestBody(kind, engineName, externalId, opts = {}) {
|
|
55
|
+
const tests = `Tests written by FireFunc (self-hosted ${engineName} runner) for ${externalId ?? 'this task'}.`;
|
|
56
|
+
const what = kind === 'test'
|
|
57
|
+
? opts.continuesBranch
|
|
58
|
+
? tests
|
|
59
|
+
: `${tests} When this pull request was opened, its change touched test files only.`
|
|
60
|
+
: `Automated fix by FireFunc (self-hosted ${engineName} runner) for ${externalId ?? 'a reported bug'}.`;
|
|
61
|
+
return `${what}\n\nReview before merging.`;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=pr-title.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RunnerApi } from './api.js';
|
|
2
|
+
export declare function lostSentence(machine: string): string;
|
|
3
|
+
export type RecoverDeps = {
|
|
4
|
+
runnerDir: string;
|
|
5
|
+
api: Pick<RunnerApi, 'result' | 'probe'>;
|
|
6
|
+
machine: string;
|
|
7
|
+
log: (line: string) => void;
|
|
8
|
+
maxWaitMs?: number;
|
|
9
|
+
};
|
|
10
|
+
export declare function recoverPreviousProcess(deps: RecoverDeps): Promise<string[]>;
|