@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,217 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync, statfsSync } from 'node:fs';
|
|
3
|
+
import { availableParallelism, cpus, freemem, loadavg, platform, tmpdir, totalmem } from 'node:os';
|
|
4
|
+
import { deviceReport } from './device-lease.js';
|
|
5
|
+
import { deviceLockDir } from './locks.js';
|
|
6
|
+
import { commandOnPath, runPath } from './machine-commands.js';
|
|
7
|
+
import { machineProfile } from './machine-profile.js';
|
|
8
|
+
import { workDir, workDirFailure } from './work-dir.js';
|
|
9
|
+
export function heavyRunBudget(setting, memBytes = totalmem()) {
|
|
10
|
+
if (typeof setting === 'number' && Number.isInteger(setting) && setting >= 1) {
|
|
11
|
+
return Math.min(16, setting);
|
|
12
|
+
}
|
|
13
|
+
return Math.max(1, Math.floor(memBytes / (8 * 1024 ** 3)));
|
|
14
|
+
}
|
|
15
|
+
export function processorReading(os = platform(), read = {}) {
|
|
16
|
+
let n = 0;
|
|
17
|
+
try {
|
|
18
|
+
n = read.cores ? read.cores() : availableParallelism?.() || cpus().length;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
n = 0;
|
|
22
|
+
}
|
|
23
|
+
const cores = Number.isInteger(n) && n > 0 ? n : 1;
|
|
24
|
+
if (os === 'win32')
|
|
25
|
+
return { cpus: cores, load1PerCore: null };
|
|
26
|
+
const one = (read.load ?? loadavg)()[0];
|
|
27
|
+
return {
|
|
28
|
+
cpus: cores,
|
|
29
|
+
load1PerCore: typeof one === 'number' && Number.isFinite(one) && one >= 0
|
|
30
|
+
? Math.round((one / cores) * 100) / 100
|
|
31
|
+
: null,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export const DEFAULT_MIN_FREE_DISK_GB = 5;
|
|
35
|
+
export const DEFAULT_MIN_FREE_MEMORY_GB = 1;
|
|
36
|
+
export const DEFAULT_JOB_TIMEOUT_MINUTES = 20;
|
|
37
|
+
const GB = 1e9;
|
|
38
|
+
export function floorBytes(gb, fallbackGb) {
|
|
39
|
+
return typeof gb === 'number' && Number.isFinite(gb) && gb >= 0
|
|
40
|
+
? Math.round(gb * GB)
|
|
41
|
+
: fallbackGb * GB;
|
|
42
|
+
}
|
|
43
|
+
export function diskOf(dir) {
|
|
44
|
+
try {
|
|
45
|
+
if (!existsSync(dir))
|
|
46
|
+
return null;
|
|
47
|
+
const s = statfsSync(dir);
|
|
48
|
+
return { freeBytes: s.bavail * s.bsize, totalBytes: s.blocks * s.bsize };
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function leastFreeDisk(dirs, read = diskOf) {
|
|
55
|
+
let least = null;
|
|
56
|
+
for (const d of new Set(dirs)) {
|
|
57
|
+
const r = read(d);
|
|
58
|
+
if (r && (!least || r.freeBytes < least.freeBytes))
|
|
59
|
+
least = r;
|
|
60
|
+
}
|
|
61
|
+
return least;
|
|
62
|
+
}
|
|
63
|
+
export function macAvailableFromVmStat(out) {
|
|
64
|
+
const page = Number(/page size of (\d+) bytes/.exec(out)?.[1]);
|
|
65
|
+
if (!Number.isFinite(page) || page <= 0)
|
|
66
|
+
return null;
|
|
67
|
+
const pages = (name) => {
|
|
68
|
+
const m = new RegExp(`Pages ${name}:\\s+(\\d+)`).exec(out);
|
|
69
|
+
return m ? Number(m[1]) : 0;
|
|
70
|
+
};
|
|
71
|
+
const free = /Pages free:\s+\d+/.test(out) ? pages('free') : null;
|
|
72
|
+
if (free === null)
|
|
73
|
+
return null;
|
|
74
|
+
return (free + pages('inactive') + pages('speculative')) * page;
|
|
75
|
+
}
|
|
76
|
+
export function linuxAvailableFromMeminfo(text) {
|
|
77
|
+
const kb = /^MemAvailable:\s+(\d+)\s+kB/m.exec(text)?.[1];
|
|
78
|
+
return kb ? Number(kb) * 1024 : null;
|
|
79
|
+
}
|
|
80
|
+
export function availableMemory(os = platform()) {
|
|
81
|
+
try {
|
|
82
|
+
if (os === 'darwin') {
|
|
83
|
+
const out = execFileSync('vm_stat', [], { encoding: 'utf8', timeout: 2_000 });
|
|
84
|
+
const v = macAvailableFromVmStat(out);
|
|
85
|
+
if (v !== null)
|
|
86
|
+
return v;
|
|
87
|
+
}
|
|
88
|
+
else if (os === 'linux') {
|
|
89
|
+
const v = linuxAvailableFromMeminfo(readFileSync('/proc/meminfo', 'utf8'));
|
|
90
|
+
if (v !== null)
|
|
91
|
+
return v;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
}
|
|
96
|
+
return freemem();
|
|
97
|
+
}
|
|
98
|
+
export function hasDeviceTools(cfg, has = (n, p) => commandOnPath(n, p)) {
|
|
99
|
+
if (Array.isArray(cfg.devices) && cfg.devices.length > 0)
|
|
100
|
+
return true;
|
|
101
|
+
return has('adb', runPath(cfg));
|
|
102
|
+
}
|
|
103
|
+
export function readResources(cfg, runnerDir, deps = {}) {
|
|
104
|
+
const dirs = [
|
|
105
|
+
tmpdir(),
|
|
106
|
+
...(cfg.workDir ? [cfg.workDir] : []),
|
|
107
|
+
runnerDir,
|
|
108
|
+
...Object.values(cfg.repos ?? {}),
|
|
109
|
+
];
|
|
110
|
+
const disk = (deps.disk ?? ((d) => leastFreeDisk(d)))(dirs);
|
|
111
|
+
let mem = null;
|
|
112
|
+
try {
|
|
113
|
+
mem = (deps.memory ?? availableMemory)();
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
mem = null;
|
|
117
|
+
}
|
|
118
|
+
const limit = typeof cfg.jobTimeoutMinutes === 'number' && cfg.jobTimeoutMinutes > 0
|
|
119
|
+
? cfg.jobTimeoutMinutes
|
|
120
|
+
: DEFAULT_JOB_TIMEOUT_MINUTES;
|
|
121
|
+
const total = (deps.totalMemory ?? totalmem)();
|
|
122
|
+
const cpu = (() => {
|
|
123
|
+
try {
|
|
124
|
+
return (deps.processor ?? (() => processorReading()))();
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
})();
|
|
130
|
+
return {
|
|
131
|
+
disk: disk
|
|
132
|
+
? {
|
|
133
|
+
freeBytes: Math.max(0, Math.floor(disk.freeBytes)),
|
|
134
|
+
totalBytes: Math.floor(disk.totalBytes),
|
|
135
|
+
floorBytes: floorBytes(cfg.minFreeDiskGB, DEFAULT_MIN_FREE_DISK_GB),
|
|
136
|
+
}
|
|
137
|
+
: null,
|
|
138
|
+
memory: mem !== null && Number.isFinite(mem)
|
|
139
|
+
? {
|
|
140
|
+
freeBytes: Math.max(0, Math.floor(mem)),
|
|
141
|
+
totalBytes: total,
|
|
142
|
+
floorBytes: floorBytes(cfg.minFreeMemoryGB, DEFAULT_MIN_FREE_MEMORY_GB),
|
|
143
|
+
}
|
|
144
|
+
: null,
|
|
145
|
+
timeLimitMinutes: Math.round(limit),
|
|
146
|
+
workDir: deps.workDir ? deps.workDir() : workDirReport(),
|
|
147
|
+
devices: (() => {
|
|
148
|
+
try {
|
|
149
|
+
if (deps.devices)
|
|
150
|
+
return deps.devices();
|
|
151
|
+
return hasDeviceTools(cfg) ? deviceReport(deviceLockDir(runnerDir), cfg) : undefined;
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
})(),
|
|
157
|
+
...(cpu ? { cpus: cpu.cpus } : {}),
|
|
158
|
+
...(cpu && cpu.load1PerCore !== null ? { load1PerCore: cpu.load1PerCore } : {}),
|
|
159
|
+
maxHeavyRuns: heavyRunBudget(cfg.maxHeavyRuns, total),
|
|
160
|
+
...(() => {
|
|
161
|
+
try {
|
|
162
|
+
const p = deps.profile ? deps.profile() : machineProfile(runPath(cfg));
|
|
163
|
+
return p ? { profile: p } : {};
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return {};
|
|
167
|
+
}
|
|
168
|
+
})(),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
export function workDirReport() {
|
|
172
|
+
const set = workDir() !== null;
|
|
173
|
+
return { set, usable: !set || workDirFailure() === null };
|
|
174
|
+
}
|
|
175
|
+
export function shortOf(r) {
|
|
176
|
+
const out = [];
|
|
177
|
+
if (r.disk && r.disk.floorBytes > 0 && r.disk.freeBytes < r.disk.floorBytes)
|
|
178
|
+
out.push('disk');
|
|
179
|
+
if (r.memory && r.memory.floorBytes > 0 && r.memory.freeBytes < r.memory.floorBytes)
|
|
180
|
+
out.push('memory');
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
const gb = (b) => `${(b / GB).toFixed(1)} GB`;
|
|
184
|
+
export function resourceWatch(deps) {
|
|
185
|
+
const every = deps.everyMs ?? 15_000;
|
|
186
|
+
const now = deps.now ?? (() => Date.now());
|
|
187
|
+
let last = null;
|
|
188
|
+
let at = -Infinity;
|
|
189
|
+
let wasShort = [];
|
|
190
|
+
const report = () => {
|
|
191
|
+
if (!last || now() - at >= every) {
|
|
192
|
+
last = deps.read();
|
|
193
|
+
at = now();
|
|
194
|
+
}
|
|
195
|
+
return last;
|
|
196
|
+
};
|
|
197
|
+
const short = () => {
|
|
198
|
+
const r = report();
|
|
199
|
+
const s = shortOf(r);
|
|
200
|
+
const key = s.join(',');
|
|
201
|
+
if (key !== wasShort.join(',')) {
|
|
202
|
+
if (s.length) {
|
|
203
|
+
const words = s.map((k) => k === 'disk'
|
|
204
|
+
? `free disk ${gb(r.disk.freeBytes)} is under the ${gb(r.disk.floorBytes)} floor (minFreeDiskGB)`
|
|
205
|
+
: `available memory ${gb(r.memory.freeBytes)} is under the ${gb(r.memory.floorBytes)} floor (minFreeMemoryGB)`);
|
|
206
|
+
deps.log(`${words.join('; ')} — claiming no new work until there is room; runs already going carry on`);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
deps.log('there is room again — claiming work');
|
|
210
|
+
}
|
|
211
|
+
wasShort = s;
|
|
212
|
+
}
|
|
213
|
+
return s.length > 0;
|
|
214
|
+
};
|
|
215
|
+
return { report, short };
|
|
216
|
+
}
|
|
217
|
+
//# sourceMappingURL=resources.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type RunnerConfig } from './config.js';
|
|
2
|
+
export type RestartIdleDeps = {
|
|
3
|
+
load: () => RunnerConfig | null;
|
|
4
|
+
save: (cfg: RunnerConfig) => void;
|
|
5
|
+
held: () => string[];
|
|
6
|
+
restart: () => Promise<{
|
|
7
|
+
ok: boolean;
|
|
8
|
+
message: string;
|
|
9
|
+
installed: boolean;
|
|
10
|
+
}>;
|
|
11
|
+
sleep: (ms: number) => Promise<void>;
|
|
12
|
+
now: () => number;
|
|
13
|
+
log: (line: string) => void;
|
|
14
|
+
settleMs?: number;
|
|
15
|
+
pollMs?: number;
|
|
16
|
+
maxWaitMs?: number | null;
|
|
17
|
+
};
|
|
18
|
+
export type RestartIdleResult = {
|
|
19
|
+
ok: boolean;
|
|
20
|
+
message: string;
|
|
21
|
+
};
|
|
22
|
+
export declare const DRAIN_SETTLE_MS = 70000;
|
|
23
|
+
export declare function restartWhenIdle(d: RestartIdleDeps): Promise<RestartIdleResult>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { drainSetting, isDraining } from './config.js';
|
|
2
|
+
export const DRAIN_SETTLE_MS = 70_000;
|
|
3
|
+
export async function restartWhenIdle(d) {
|
|
4
|
+
const cfg = d.load();
|
|
5
|
+
if (!cfg)
|
|
6
|
+
return { ok: false, message: 'Not connected. Run "firefunc-runner connect <token>".' };
|
|
7
|
+
const drainedBefore = isDraining(cfg);
|
|
8
|
+
if (!drainedBefore)
|
|
9
|
+
d.save(drainSetting(cfg, true));
|
|
10
|
+
const lift = () => {
|
|
11
|
+
if (drainedBefore)
|
|
12
|
+
return;
|
|
13
|
+
const now = d.load();
|
|
14
|
+
if (now)
|
|
15
|
+
d.save(drainSetting(now, false));
|
|
16
|
+
};
|
|
17
|
+
const settle = d.settleMs ?? DRAIN_SETTLE_MS;
|
|
18
|
+
const poll = d.pollMs ?? 10_000;
|
|
19
|
+
const started = d.now();
|
|
20
|
+
d.log('Draining: the runner finishes the runs it holds and takes nothing new; it restarts once none is left.');
|
|
21
|
+
let told = -1;
|
|
22
|
+
for (;;) {
|
|
23
|
+
const held = d.held();
|
|
24
|
+
const waited = d.now() - started;
|
|
25
|
+
if (held.length === 0 && waited >= settle)
|
|
26
|
+
break;
|
|
27
|
+
if (d.maxWaitMs != null && waited >= d.maxWaitMs) {
|
|
28
|
+
lift();
|
|
29
|
+
return {
|
|
30
|
+
ok: false,
|
|
31
|
+
message: `Not restarted: ${held.length} run${held.length === 1 ? ' is' : 's are'} still running after ${Math.round(waited / 60_000)} min. ` +
|
|
32
|
+
(drainedBefore
|
|
33
|
+
? 'The runner stays drained, as it was. '
|
|
34
|
+
: 'The drain was lifted, so it takes work again. ') +
|
|
35
|
+
'Run "firefunc-runner restart --when-idle" again without --max-wait to wait for them.',
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (held.length !== told) {
|
|
39
|
+
told = held.length;
|
|
40
|
+
d.log(held.length > 0
|
|
41
|
+
? `waiting for ${held.length} run${held.length === 1 ? '' : 's'} to finish: ${held.join(', ')}`
|
|
42
|
+
: 'no run held — waiting a moment for the runner to read the drain');
|
|
43
|
+
}
|
|
44
|
+
await d.sleep(poll);
|
|
45
|
+
}
|
|
46
|
+
const res = await d.restart();
|
|
47
|
+
if (!res.installed) {
|
|
48
|
+
return {
|
|
49
|
+
ok: false,
|
|
50
|
+
message: 'Idle: the runner holds no run, and stays drained. No service is installed, so restart it yourself — stop it with Ctrl-C where it runs and start it again with "firefunc-runner start" — then run "firefunc-runner resume" so it takes work again.',
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (!res.ok) {
|
|
54
|
+
return {
|
|
55
|
+
ok: false,
|
|
56
|
+
message: `${res.message}\nThe runner holds no run and stays drained; restart the service yourself, then run "firefunc-runner resume".`,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
lift();
|
|
60
|
+
return {
|
|
61
|
+
ok: true,
|
|
62
|
+
message: drainedBefore
|
|
63
|
+
? 'Restarted while idle. It was drained before, so it stays drained: run "firefunc-runner resume" to take work again.'
|
|
64
|
+
: 'Restarted while idle, and the drain is lifted: it takes work again within a minute.',
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=restart-idle.js.map
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { WrittenSkills } from './skills.js';
|
|
2
|
+
export declare const ATTACH_FOLDER = ".firefunc/attach";
|
|
3
|
+
export declare const MAX_ATTACHMENTS = 12;
|
|
4
|
+
export declare const MAX_ATTACHMENT_BYTES: number;
|
|
5
|
+
export declare const MAX_ATTACHMENTS_TOTAL_BYTES: number;
|
|
6
|
+
export declare const ATTACHMENT_TYPES: Readonly<Record<string, string>>;
|
|
7
|
+
export declare const ATTACH_RULE = "- To show the founder what you built, save screenshots (png/jpg/webp) or one short screen recording (mp4) in `.firefunc/attach/`; FireFunc attaches them to the ticket. Never capture a screen that shows a key, a token or a password.";
|
|
8
|
+
export type AttachLimits = {
|
|
9
|
+
maxFiles: number;
|
|
10
|
+
maxFileBytes: number;
|
|
11
|
+
maxTotalBytes: number;
|
|
12
|
+
};
|
|
13
|
+
export declare function attachLimitsOf(offer: {
|
|
14
|
+
maxFiles?: number;
|
|
15
|
+
maxFileBytes?: number;
|
|
16
|
+
maxTotalBytes?: number;
|
|
17
|
+
}): AttachLimits;
|
|
18
|
+
export type PreparedAttach = WrittenSkills & {
|
|
19
|
+
dir: string | null;
|
|
20
|
+
};
|
|
21
|
+
export declare function prepareAttachFolder(worktree: string, log: (m: string) => void): PreparedAttach;
|
|
22
|
+
export declare function prepareFireFuncFolder(worktree: string, folder: {
|
|
23
|
+
sub: string;
|
|
24
|
+
gitignore: string;
|
|
25
|
+
what: string;
|
|
26
|
+
}, log: (m: string) => void): PreparedAttach;
|
|
27
|
+
export type AttachCandidate = {
|
|
28
|
+
name: string;
|
|
29
|
+
path: string;
|
|
30
|
+
size: number;
|
|
31
|
+
contentType: string;
|
|
32
|
+
};
|
|
33
|
+
export declare function scanAttachFolder(dir: string, limits?: AttachLimits): {
|
|
34
|
+
files: AttachCandidate[];
|
|
35
|
+
skipped: Array<{
|
|
36
|
+
name: string;
|
|
37
|
+
why: string;
|
|
38
|
+
}>;
|
|
39
|
+
};
|
|
40
|
+
export type AttachApi = {
|
|
41
|
+
offerAttachment(runId: string, file: {
|
|
42
|
+
name: string;
|
|
43
|
+
size: number;
|
|
44
|
+
sha256: string;
|
|
45
|
+
contentType: string;
|
|
46
|
+
}): Promise<{
|
|
47
|
+
id: string;
|
|
48
|
+
upload: {
|
|
49
|
+
url: string;
|
|
50
|
+
headers: Record<string, string>;
|
|
51
|
+
};
|
|
52
|
+
} | {
|
|
53
|
+
refused: {
|
|
54
|
+
reason: string;
|
|
55
|
+
fix: string;
|
|
56
|
+
status: number;
|
|
57
|
+
};
|
|
58
|
+
}>;
|
|
59
|
+
completeAttachment(runId: string, attachmentId: string): Promise<{
|
|
60
|
+
ok: true;
|
|
61
|
+
} | {
|
|
62
|
+
refused: {
|
|
63
|
+
reason: string;
|
|
64
|
+
fix: string;
|
|
65
|
+
status: number;
|
|
66
|
+
};
|
|
67
|
+
}>;
|
|
68
|
+
probe(): Promise<boolean>;
|
|
69
|
+
};
|
|
70
|
+
export declare function uploadRunAttachments(opts: {
|
|
71
|
+
runId: string;
|
|
72
|
+
dir: string;
|
|
73
|
+
api: AttachApi;
|
|
74
|
+
log: (m: string) => void;
|
|
75
|
+
limits?: AttachLimits;
|
|
76
|
+
maxWaitMs?: number;
|
|
77
|
+
fetcher?: typeof fetch;
|
|
78
|
+
}): Promise<{
|
|
79
|
+
attached: string[];
|
|
80
|
+
skipped: Array<{
|
|
81
|
+
name: string;
|
|
82
|
+
why: string;
|
|
83
|
+
}>;
|
|
84
|
+
}>;
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, statSync, writeFileSync, } from 'node:fs';
|
|
4
|
+
import { isAbsolute, join, relative, sep } from 'node:path';
|
|
5
|
+
import { isNetworkError, withNetworkRetry } from './network.js';
|
|
6
|
+
export const ATTACH_FOLDER = '.firefunc/attach';
|
|
7
|
+
export const MAX_ATTACHMENTS = 12;
|
|
8
|
+
export const MAX_ATTACHMENT_BYTES = 20 * 1024 * 1024;
|
|
9
|
+
export const MAX_ATTACHMENTS_TOTAL_BYTES = 60 * 1024 * 1024;
|
|
10
|
+
export const ATTACHMENT_TYPES = {
|
|
11
|
+
png: 'image/png',
|
|
12
|
+
jpg: 'image/jpeg',
|
|
13
|
+
jpeg: 'image/jpeg',
|
|
14
|
+
webp: 'image/webp',
|
|
15
|
+
gif: 'image/gif',
|
|
16
|
+
mp4: 'video/mp4',
|
|
17
|
+
};
|
|
18
|
+
const GITIGNORE = '# FireFunc: pictures a run attaches to its ticket, never committed\n*\n';
|
|
19
|
+
export const ATTACH_RULE = `- To show the founder what you built, save screenshots (png/jpg/webp) or one short screen recording (mp4) in \`${ATTACH_FOLDER}/\`; FireFunc attaches them to the ticket. Never capture a screen that shows a key, a token or a password.`;
|
|
20
|
+
export function attachLimitsOf(offer) {
|
|
21
|
+
const within = (v, cap) => typeof v === 'number' && Number.isFinite(v) && v > 0 ? Math.min(Math.floor(v), cap) : cap;
|
|
22
|
+
return {
|
|
23
|
+
maxFiles: within(offer.maxFiles, MAX_ATTACHMENTS),
|
|
24
|
+
maxFileBytes: within(offer.maxFileBytes, MAX_ATTACHMENT_BYTES),
|
|
25
|
+
maxTotalBytes: within(offer.maxTotalBytes, MAX_ATTACHMENTS_TOTAL_BYTES),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function git(cwd, args) {
|
|
29
|
+
try {
|
|
30
|
+
return execFileSync('git', ['-C', cwd, ...args], {
|
|
31
|
+
encoding: 'utf8',
|
|
32
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
33
|
+
}).trim();
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function insideTop(top, real) {
|
|
40
|
+
const rel = relative(top, real);
|
|
41
|
+
if (!rel || rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel))
|
|
42
|
+
return null;
|
|
43
|
+
return rel.split(sep).join('/');
|
|
44
|
+
}
|
|
45
|
+
function lstatExists(p) {
|
|
46
|
+
try {
|
|
47
|
+
lstatSync(p);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const NONE = { written: [], pathspecs: [], dir: null };
|
|
55
|
+
export function prepareAttachFolder(worktree, log) {
|
|
56
|
+
return prepareFireFuncFolder(worktree, { sub: 'attach', gitignore: GITIGNORE, what: 'pictures' }, log);
|
|
57
|
+
}
|
|
58
|
+
export function prepareFireFuncFolder(worktree, folder, log) {
|
|
59
|
+
const { sub, what } = folder;
|
|
60
|
+
const GITIGNORE = folder.gitignore;
|
|
61
|
+
let top;
|
|
62
|
+
try {
|
|
63
|
+
top = realpathSync(worktree);
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
log(`no folder for ${what}: ${e.message}`);
|
|
67
|
+
return NONE;
|
|
68
|
+
}
|
|
69
|
+
const outer = join(top, '.firefunc');
|
|
70
|
+
if (!lstatExists(outer)) {
|
|
71
|
+
try {
|
|
72
|
+
mkdirSync(outer);
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
log(`no folder for ${what}: ${e.message}`);
|
|
76
|
+
return NONE;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else if (!existsSync(outer)) {
|
|
80
|
+
log(`no folder for ${what}: .firefunc is a link to a place that does not exist in this checkout. ` +
|
|
81
|
+
'Fix: point it at a folder inside the repository, or remove the link.');
|
|
82
|
+
return NONE;
|
|
83
|
+
}
|
|
84
|
+
let outerReal;
|
|
85
|
+
try {
|
|
86
|
+
outerReal = realpathSync(outer);
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
log(`no folder for ${what}: ${e.message}`);
|
|
90
|
+
return NONE;
|
|
91
|
+
}
|
|
92
|
+
if (insideTop(top, outerReal) === null || !statSync(outerReal).isDirectory()) {
|
|
93
|
+
log(`no folder for ${what}: .firefunc leads to ${outerReal}, which is not a folder inside this checkout. ` +
|
|
94
|
+
'Fix: make .firefunc a folder inside the repository.');
|
|
95
|
+
return NONE;
|
|
96
|
+
}
|
|
97
|
+
const dir = join(outerReal, sub);
|
|
98
|
+
const rel = insideTop(top, dir);
|
|
99
|
+
const gitTop = git(top, ['rev-parse', '--show-toplevel']);
|
|
100
|
+
let isRepo = false;
|
|
101
|
+
if (gitTop) {
|
|
102
|
+
try {
|
|
103
|
+
isRepo = realpathSync(gitTop) === top;
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
isRepo = false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const ignoreCase = isRepo && git(top, ['config', '--bool', 'core.ignorecase']) === 'true';
|
|
110
|
+
const ours = () => ({
|
|
111
|
+
written: [rel],
|
|
112
|
+
pathspecs: [`:(exclude,literal${ignoreCase ? ',icase' : ''})${rel}`],
|
|
113
|
+
...(ignoreCase ? { ignoreCase } : {}),
|
|
114
|
+
dir,
|
|
115
|
+
});
|
|
116
|
+
if (lstatExists(dir)) {
|
|
117
|
+
let mine = false;
|
|
118
|
+
try {
|
|
119
|
+
mine =
|
|
120
|
+
lstatSync(dir).isDirectory() && readFileSync(join(dir, '.gitignore'), 'utf8') === GITIGNORE;
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
mine = false;
|
|
124
|
+
}
|
|
125
|
+
if (mine)
|
|
126
|
+
return ours();
|
|
127
|
+
log(`no folder for ${what}: ${rel} is already in the checkout, and FireFunc never writes into the ` +
|
|
128
|
+
`repository's own files. Fix: rename or remove ${rel} in the repository.`);
|
|
129
|
+
return NONE;
|
|
130
|
+
}
|
|
131
|
+
if (isRepo) {
|
|
132
|
+
const tracked = git(top, [
|
|
133
|
+
'ls-files',
|
|
134
|
+
'-z',
|
|
135
|
+
'--',
|
|
136
|
+
`:(literal${ignoreCase ? ',icase' : ''})${rel}`,
|
|
137
|
+
]);
|
|
138
|
+
if (tracked) {
|
|
139
|
+
log(`no folder for ${what}: the repository tracks files under ${rel}. Fix: move them out of ${rel}.`);
|
|
140
|
+
return NONE;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
mkdirSync(dir);
|
|
145
|
+
writeFileSync(join(dir, '.gitignore'), GITIGNORE);
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
log(`no folder for ${what}: ${e.message}`);
|
|
149
|
+
return NONE;
|
|
150
|
+
}
|
|
151
|
+
return ours();
|
|
152
|
+
}
|
|
153
|
+
export function scanAttachFolder(dir, limits = attachLimitsOf({})) {
|
|
154
|
+
const files = [];
|
|
155
|
+
const skipped = [];
|
|
156
|
+
let names;
|
|
157
|
+
try {
|
|
158
|
+
names = readdirSync(dir).sort((a, b) => a.localeCompare(b));
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return { files, skipped };
|
|
162
|
+
}
|
|
163
|
+
let total = 0;
|
|
164
|
+
for (const name of names) {
|
|
165
|
+
if (name === '.gitignore')
|
|
166
|
+
continue;
|
|
167
|
+
const path = join(dir, name);
|
|
168
|
+
let st;
|
|
169
|
+
try {
|
|
170
|
+
st = lstatSync(path);
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (!st.isFile()) {
|
|
176
|
+
skipped.push({
|
|
177
|
+
name,
|
|
178
|
+
why: st.isSymbolicLink() ? 'it is a link, not a file' : 'it is not a file',
|
|
179
|
+
});
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (name.length > 120 || /[\u0000-\u001f\u007f\\]/.test(name) || name.trim() !== name) {
|
|
183
|
+
skipped.push({ name, why: 'its name is not a plain file name' });
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const ext = /\.([A-Za-z0-9]+)$/.exec(name)?.[1]?.toLowerCase();
|
|
187
|
+
const contentType = ext ? ATTACHMENT_TYPES[ext] : undefined;
|
|
188
|
+
if (!contentType) {
|
|
189
|
+
skipped.push({ name, why: 'only .png, .jpg, .jpeg, .webp, .gif and .mp4 are attached' });
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (st.size === 0) {
|
|
193
|
+
skipped.push({ name, why: 'it is empty' });
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (st.size > limits.maxFileBytes) {
|
|
197
|
+
skipped.push({ name, why: `it is larger than ${limits.maxFileBytes} bytes` });
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (files.length >= limits.maxFiles) {
|
|
201
|
+
skipped.push({ name, why: `a run attaches at most ${limits.maxFiles} files` });
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (total + st.size > limits.maxTotalBytes) {
|
|
205
|
+
skipped.push({
|
|
206
|
+
name,
|
|
207
|
+
why: `a run attaches at most ${limits.maxTotalBytes} bytes in all`,
|
|
208
|
+
});
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
total += st.size;
|
|
212
|
+
files.push({ name, path, size: st.size, contentType });
|
|
213
|
+
}
|
|
214
|
+
return { files, skipped };
|
|
215
|
+
}
|
|
216
|
+
export async function uploadRunAttachments(opts) {
|
|
217
|
+
const { runId, api, log } = opts;
|
|
218
|
+
const fetcher = opts.fetcher ?? fetch;
|
|
219
|
+
const scan = scanAttachFolder(opts.dir, opts.limits);
|
|
220
|
+
const attached = [];
|
|
221
|
+
const skipped = [...scan.skipped];
|
|
222
|
+
for (const s of scan.skipped)
|
|
223
|
+
log(`picture ${s.name} not attached: ${s.why}`);
|
|
224
|
+
const retry = (label, fn) => withNetworkRetry(fn, {
|
|
225
|
+
label,
|
|
226
|
+
maxWaitMs: opts.maxWaitMs ?? 2 * 60_000,
|
|
227
|
+
probe: () => api.probe(),
|
|
228
|
+
log,
|
|
229
|
+
});
|
|
230
|
+
for (const f of scan.files) {
|
|
231
|
+
const skip = (why) => {
|
|
232
|
+
skipped.push({ name: f.name, why });
|
|
233
|
+
log(`picture ${f.name} not attached: ${why}`);
|
|
234
|
+
};
|
|
235
|
+
try {
|
|
236
|
+
const bytes = readFileSync(f.path);
|
|
237
|
+
if (bytes.byteLength !== f.size) {
|
|
238
|
+
skip('it changed while it was being read');
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
const sha256 = createHash('sha256').update(bytes).digest('hex');
|
|
242
|
+
const offered = await retry(`offer of ${f.name}`, () => api.offerAttachment(runId, {
|
|
243
|
+
name: f.name,
|
|
244
|
+
size: f.size,
|
|
245
|
+
sha256,
|
|
246
|
+
contentType: f.contentType,
|
|
247
|
+
}));
|
|
248
|
+
if ('refused' in offered) {
|
|
249
|
+
skip(`${offered.refused.reason} Fix: ${offered.refused.fix}`);
|
|
250
|
+
if (offered.refused.status === 404 || offered.refused.status === 503)
|
|
251
|
+
break;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const put = await retry(`upload of ${f.name}`, () => fetcher(offered.upload.url, {
|
|
255
|
+
method: 'PUT',
|
|
256
|
+
headers: offered.upload.headers,
|
|
257
|
+
body: bytes,
|
|
258
|
+
}));
|
|
259
|
+
if (!put.ok) {
|
|
260
|
+
skip(`the upload answered ${put.status}`);
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const done = await retry(`check of ${f.name}`, () => api.completeAttachment(runId, offered.id));
|
|
264
|
+
if ('refused' in done) {
|
|
265
|
+
skip(`${done.refused.reason} Fix: ${done.refused.fix}`);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
attached.push(f.name);
|
|
269
|
+
}
|
|
270
|
+
catch (e) {
|
|
271
|
+
skip(isNetworkError(e)
|
|
272
|
+
? 'FireFunc could not be reached'
|
|
273
|
+
: `it could not be uploaded (${e.name})`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (attached.length) {
|
|
277
|
+
log(`uploaded ${attached.length} picture(s) for the ticket: ${attached.join(', ')}`);
|
|
278
|
+
}
|
|
279
|
+
return { attached, skipped };
|
|
280
|
+
}
|
|
281
|
+
//# sourceMappingURL=run-attachments.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const STOP_GRACE_MS = 3000;
|
|
2
|
+
export declare function trackRunGroup(runId: string, pgid: number): void;
|
|
3
|
+
export declare function untrackRunGroup(runId: string, pgid: number): void;
|
|
4
|
+
export declare function isStopped(runId: string): boolean;
|
|
5
|
+
export declare function forgetRun(runId: string): void;
|
|
6
|
+
export declare function readStopList(body: unknown): string[];
|
|
7
|
+
export type StopDeps = {
|
|
8
|
+
signal?: (pgid: number, sig: NodeJS.Signals) => void;
|
|
9
|
+
later?: (fn: () => void, ms: number) => void;
|
|
10
|
+
log?: (msg: string) => void;
|
|
11
|
+
};
|
|
12
|
+
export declare function windowsStopArgs(pid: number): string[];
|
|
13
|
+
export declare function stopRun(runId: string, deps?: StopDeps): number;
|
|
14
|
+
export declare function stoppedByPersonSentence(engineName: string, at: 'engine' | 'push' | 'pr'): string;
|
|
15
|
+
export declare function resetRunStops(): void;
|