@firefunc-agent/runner 0.9.14 → 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.
Files changed (153) hide show
  1. package/README.md +167 -10
  2. package/dist/alive.d.ts +40 -0
  3. package/dist/alive.js +138 -0
  4. package/dist/api-fallback.d.ts +4 -2
  5. package/dist/api-fallback.js +26 -1
  6. package/dist/api.d.ts +65 -3
  7. package/dist/api.js +164 -3
  8. package/dist/brief-file.d.ts +15 -0
  9. package/dist/brief-file.js +36 -0
  10. package/dist/brief.d.ts +3 -0
  11. package/dist/brief.js +14 -4
  12. package/dist/build-caches.d.ts +7 -0
  13. package/dist/build-caches.js +63 -0
  14. package/dist/build-info.json +2 -2
  15. package/dist/checkpoint.d.ts +40 -0
  16. package/dist/checkpoint.js +98 -0
  17. package/dist/cli.js +184 -18
  18. package/dist/cloud-boot.d.ts +41 -0
  19. package/dist/cloud-boot.js +239 -0
  20. package/dist/command-activity.d.ts +23 -0
  21. package/dist/command-activity.js +89 -0
  22. package/dist/command-step.d.ts +58 -0
  23. package/dist/command-step.js +248 -0
  24. package/dist/config.d.ts +38 -0
  25. package/dist/config.js +23 -0
  26. package/dist/cut.d.ts +1 -1
  27. package/dist/cut.js +13 -3
  28. package/dist/daemon.js +364 -15
  29. package/dist/device-lease.d.ts +15 -0
  30. package/dist/device-lease.js +608 -0
  31. package/dist/editor/fs-server.d.ts +31 -0
  32. package/dist/editor/fs-server.js +159 -0
  33. package/dist/editor/relay-crypto.d.ts +68 -0
  34. package/dist/editor/relay-crypto.js +191 -0
  35. package/dist/editor/sessions.d.ts +50 -0
  36. package/dist/editor/sessions.js +279 -0
  37. package/dist/editor/worktree-of.d.ts +1 -0
  38. package/dist/editor/worktree-of.js +23 -0
  39. package/dist/editor/ws-client.d.ts +26 -0
  40. package/dist/editor/ws-client.js +227 -0
  41. package/dist/engine-account.d.ts +24 -0
  42. package/dist/engine-account.js +118 -0
  43. package/dist/engine-login.d.ts +12 -0
  44. package/dist/engine-login.js +88 -0
  45. package/dist/engine-models.d.ts +13 -0
  46. package/dist/engine-models.js +71 -0
  47. package/dist/engine-watch.d.ts +1 -0
  48. package/dist/engine-watch.js +22 -6
  49. package/dist/engines/antigravity.js +19 -4
  50. package/dist/engines/claude.js +10 -9
  51. package/dist/engines/codex.js +16 -2
  52. package/dist/engines/copilot.js +12 -0
  53. package/dist/engines/crush.d.ts +2 -0
  54. package/dist/engines/crush.js +58 -0
  55. package/dist/engines/cursor.js +12 -0
  56. package/dist/engines/gemini.js +14 -9
  57. package/dist/engines/index.d.ts +8 -0
  58. package/dist/engines/index.js +73 -1
  59. package/dist/engines/kilo.d.ts +2 -0
  60. package/dist/engines/kilo.js +63 -0
  61. package/dist/engines/opencode.d.ts +9 -0
  62. package/dist/engines/opencode.js +234 -0
  63. package/dist/engines/openhands.d.ts +10 -0
  64. package/dist/engines/openhands.js +271 -0
  65. package/dist/engines/pi.d.ts +10 -0
  66. package/dist/engines/pi.js +248 -0
  67. package/dist/engines/qwen.d.ts +8 -0
  68. package/dist/engines/qwen.js +153 -0
  69. package/dist/engines/sessions.d.ts +6 -0
  70. package/dist/engines/sessions.js +68 -0
  71. package/dist/engines/types.d.ts +3 -3
  72. package/dist/exec.d.ts +1 -0
  73. package/dist/exec.js +10 -4
  74. package/dist/firefunc/agents.d.ts +8 -1
  75. package/dist/firefunc/agents.js +53 -3
  76. package/dist/firefunc/auth.js +72 -19
  77. package/dist/firefunc/cli.js +25 -0
  78. package/dist/firefunc/client.d.ts +5 -1
  79. package/dist/firefunc/client.js +12 -2
  80. package/dist/firefunc/config.d.ts +11 -1
  81. package/dist/firefunc/config.js +6 -4
  82. package/dist/firefunc/io.d.ts +1 -0
  83. package/dist/firefunc/main.js +8 -1
  84. package/dist/firefunc/oauth-login.d.ts +23 -0
  85. package/dist/firefunc/oauth-login.js +304 -0
  86. package/dist/firefunc/tasks.d.ts +4 -0
  87. package/dist/firefunc/tasks.js +89 -2
  88. package/dist/fleet-words.d.ts +2 -0
  89. package/dist/fleet-words.js +7 -0
  90. package/dist/git-auth.d.ts +15 -0
  91. package/dist/git-auth.js +39 -1
  92. package/dist/held.d.ts +3 -0
  93. package/dist/job.d.ts +26 -3
  94. package/dist/job.js +752 -152
  95. package/dist/keep-awake.d.ts +17 -0
  96. package/dist/keep-awake.js +90 -0
  97. package/dist/keep-local.d.ts +1 -1
  98. package/dist/keep-local.js +5 -3
  99. package/dist/limit.d.ts +9 -1
  100. package/dist/limit.js +260 -3
  101. package/dist/local-checks.d.ts +19 -0
  102. package/dist/local-checks.js +79 -0
  103. package/dist/locks.d.ts +18 -1
  104. package/dist/locks.js +249 -69
  105. package/dist/machine-commands.d.ts +21 -0
  106. package/dist/machine-commands.js +107 -0
  107. package/dist/machine-profile.d.ts +18 -0
  108. package/dist/machine-profile.js +69 -0
  109. package/dist/mcp-init.d.ts +2 -0
  110. package/dist/mcp-init.js +28 -0
  111. package/dist/mcp.d.ts +13 -1
  112. package/dist/mcp.js +23 -2
  113. package/dist/network.js +3 -2
  114. package/dist/orphans.d.ts +4 -0
  115. package/dist/orphans.js +96 -4
  116. package/dist/pool.d.ts +1 -0
  117. package/dist/pool.js +25 -2
  118. package/dist/pr-title.d.ts +10 -0
  119. package/dist/pr-title.js +15 -0
  120. package/dist/recover.d.ts +3 -1
  121. package/dist/recover.js +27 -7
  122. package/dist/release-draft.d.ts +79 -0
  123. package/dist/release-draft.js +301 -0
  124. package/dist/resources.d.ts +86 -0
  125. package/dist/resources.js +217 -0
  126. package/dist/restart-idle.d.ts +23 -0
  127. package/dist/restart-idle.js +67 -0
  128. package/dist/run-attachments.d.ts +5 -0
  129. package/dist/run-attachments.js +14 -9
  130. package/dist/run-stops.d.ts +15 -0
  131. package/dist/run-stops.js +90 -0
  132. package/dist/run-temp.d.ts +12 -0
  133. package/dist/run-temp.js +57 -0
  134. package/dist/secret-scan.d.ts +5 -0
  135. package/dist/secret-scan.js +30 -0
  136. package/dist/self-upgrade.d.ts +36 -0
  137. package/dist/self-upgrade.js +78 -0
  138. package/dist/service.d.ts +7 -1
  139. package/dist/service.js +50 -7
  140. package/dist/sign-in.d.ts +41 -0
  141. package/dist/sign-in.js +239 -0
  142. package/dist/tool-call.d.ts +59 -0
  143. package/dist/tool-call.js +375 -0
  144. package/dist/transport.d.ts +26 -0
  145. package/dist/transport.js +118 -0
  146. package/dist/work-dir.d.ts +11 -0
  147. package/dist/work-dir.js +69 -0
  148. package/dist/worktree-report.d.ts +10 -0
  149. package/dist/worktree-report.js +17 -0
  150. package/dist/worktrees.d.ts +16 -1
  151. package/dist/worktrees.js +62 -17
  152. package/package.json +4 -4
  153. package/skill/firefunc/SKILL.md +4 -3
package/dist/locks.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { createHash } from 'node:crypto';
2
- import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, } from 'node:fs';
3
- import { hostname } from 'node:os';
2
+ import { chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, } from 'node:fs';
3
+ import { cpus, hostname, totalmem } from 'node:os';
4
4
  import { dirname, join } from 'node:path';
5
+ import { deviceLeaseScript } from './device-lease.js';
5
6
  const noop = () => { };
6
7
  export function createKeyedMutex() {
7
8
  const tails = new Map();
@@ -47,9 +48,16 @@ function holderOf(lockDir) {
47
48
  return null;
48
49
  }
49
50
  }
51
+ const ownTickets = new Set();
52
+ const ownSlots = new Set();
53
+ function aliveFor(pid, own, thing) {
54
+ if (pid === process.pid)
55
+ return own.has(thing);
56
+ return isAlive(pid);
57
+ }
50
58
  function breakIfStale(lockDir, now) {
51
59
  const holder = holderOf(lockDir);
52
- let stale = holder !== null && !isAlive(holder);
60
+ let stale = holder !== null && !aliveFor(holder, ownSlots, lockDir);
53
61
  if (holder === null) {
54
62
  try {
55
63
  stale = now() - statSync(lockDir).mtimeMs > 60_000;
@@ -70,88 +78,268 @@ function breakIfStale(lockDir, now) {
70
78
  rmSync(away, { recursive: true, force: true });
71
79
  return true;
72
80
  }
81
+ export function heavySlotsFor(machine = {
82
+ cores: cpus().length,
83
+ memBytes: totalmem(),
84
+ }) {
85
+ const byCores = Math.floor(machine.cores / 3);
86
+ const byMem = Math.floor(machine.memBytes / (5 * 1024 ** 3));
87
+ return Math.max(1, Math.min(8, byCores, byMem));
88
+ }
89
+ export function heavySlots(setting) {
90
+ if (typeof setting === 'number' && Number.isInteger(setting) && setting >= 1) {
91
+ return Math.min(16, setting);
92
+ }
93
+ return heavySlotsFor();
94
+ }
95
+ export function heavySlotDir(lockDir, i) {
96
+ return i === 0 ? lockDir : `${lockDir}.slot-${i}`;
97
+ }
98
+ export function heavyQueueDir(lockDir) {
99
+ return `${lockDir}.queue`;
100
+ }
101
+ const TICKET = /^(\d{13})-(\d{10})-[A-Za-z0-9]+$/;
102
+ const TAG = /^[a-z0-9]{12}$/;
103
+ export function heavyTag(runId) {
104
+ return runId
105
+ .toLowerCase()
106
+ .replace(/[^a-z0-9]/g, '')
107
+ .slice(0, 12);
108
+ }
109
+ export function heavyWaits(runnerDir) {
110
+ const locks = join(runnerDir, 'locks');
111
+ let names;
112
+ try {
113
+ names = readdirSync(locks);
114
+ }
115
+ catch {
116
+ return [];
117
+ }
118
+ const out = [];
119
+ for (const q of names) {
120
+ if (!q.startsWith('heavy-') || !q.endsWith('.lock.queue'))
121
+ continue;
122
+ const lockDir = join(locks, q.slice(0, -'.queue'.length));
123
+ const holders = new Map();
124
+ for (const slot of names) {
125
+ const base = lockDir.slice(locks.length + 1);
126
+ if (slot !== base && !slot.startsWith(`${base}.slot-`))
127
+ continue;
128
+ const pid = holderOf(join(locks, slot));
129
+ if (pid && isAlive(pid))
130
+ holders.set(pid, (holders.get(pid) ?? 0) + 1);
131
+ }
132
+ const running = [...holders.values()].reduce((a, b) => a + b, 0);
133
+ let place = 0;
134
+ for (const t of liveTickets(join(locks, q))) {
135
+ const m = TICKET.exec(t);
136
+ if (!m)
137
+ continue;
138
+ const pid = Number(m[2]);
139
+ const left = holders.get(pid) ?? 0;
140
+ if (left > 0) {
141
+ holders.set(pid, left - 1);
142
+ continue;
143
+ }
144
+ place += 1;
145
+ const tag = /-(?:rt|st)([a-z0-9]{12})/.exec(t)?.[1];
146
+ if (tag)
147
+ out.push({ tag, place, running, since: Number(m[1]) });
148
+ }
149
+ }
150
+ return out;
151
+ }
152
+ function liveTickets(queueDir) {
153
+ let names;
154
+ try {
155
+ names = readdirSync(queueDir);
156
+ }
157
+ catch {
158
+ return [];
159
+ }
160
+ const out = [];
161
+ for (const name of names.sort()) {
162
+ const m = TICKET.exec(name);
163
+ if (!m)
164
+ continue;
165
+ if (!aliveFor(Number(m[2]), ownTickets, join(queueDir, name))) {
166
+ rmSync(join(queueDir, name), { force: true });
167
+ continue;
168
+ }
169
+ out.push(name);
170
+ }
171
+ return out;
172
+ }
73
173
  export async function acquireHeavyLock(lockDir, opts = {}) {
74
174
  const now = opts.now ?? (() => Date.now());
75
175
  const sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
176
+ const slots = Math.max(1, Math.floor(opts.slots ?? 1));
76
177
  const started = now();
77
178
  let told = false;
78
- mkdirSync(dirname(lockDir), { recursive: true, mode: 0o700 });
79
- for (;;) {
179
+ const queue = heavyQueueDir(lockDir);
180
+ let ticket = null;
181
+ const dropTicket = () => {
182
+ if (ticket) {
183
+ rmSync(join(queue, ticket), { force: true });
184
+ ownTickets.delete(join(queue, ticket));
185
+ }
186
+ ticket = null;
187
+ };
188
+ try {
189
+ mkdirSync(dirname(lockDir), { recursive: true, mode: 0o700 });
190
+ mkdirSync(queue, { recursive: true, mode: 0o700 });
191
+ }
192
+ catch {
193
+ return noop;
194
+ }
195
+ const writeTicket = () => {
196
+ const tag = opts.tag && TAG.test(opts.tag) ? `rt${opts.tag}` : '';
197
+ const name = `${String(Date.now()).padStart(13, '0')}-${String(process.pid).padStart(10, '0')}-${tag}${Math.random().toString(36).slice(2, 10) || 'x'}`;
80
198
  try {
81
- mkdirSync(lockDir);
82
- writeFileSync(join(lockDir, 'owner'), `${process.pid}\n${hostname()}\n${new Date(now()).toISOString()}\n${opts.label ?? 'runner'}\n`);
83
- let held = true;
84
- return () => {
85
- if (!held)
86
- return;
87
- held = false;
88
- if (holderOf(lockDir) === process.pid)
89
- rmSync(lockDir, { recursive: true, force: true });
90
- };
199
+ ownTickets.add(join(queue, name));
200
+ writeFileSync(join(queue, name), '');
201
+ ticket = name;
91
202
  }
92
- catch (e) {
93
- if (e.code !== 'EEXIST')
94
- return noop;
203
+ catch {
204
+ ticket = null;
205
+ }
206
+ };
207
+ writeTicket();
208
+ for (;;) {
209
+ if (ticket && !existsSync(join(queue, ticket)))
210
+ writeTicket();
211
+ const order = liveTickets(queue);
212
+ const ahead = ticket ? order.indexOf(ticket) : 0;
213
+ let holder = null;
214
+ if (ahead >= 0 && ahead < slots) {
215
+ for (let i = 0; i < slots; i++) {
216
+ const slot = heavySlotDir(lockDir, i);
217
+ try {
218
+ mkdirSync(slot);
219
+ }
220
+ catch (e) {
221
+ if (e.code !== 'EEXIST') {
222
+ dropTicket();
223
+ return noop;
224
+ }
225
+ if (breakIfStale(slot, now)) {
226
+ i -= 1;
227
+ continue;
228
+ }
229
+ holder ??= holderOf(slot);
230
+ continue;
231
+ }
232
+ ownSlots.add(slot);
233
+ writeFileSync(join(slot, 'owner'), `${process.pid}\n${hostname()}\n${new Date(now()).toISOString()}\n${opts.label ?? 'runner'}\n`);
234
+ let held = true;
235
+ return () => {
236
+ if (!held)
237
+ return;
238
+ held = false;
239
+ if (holderOf(slot) === process.pid)
240
+ rmSync(slot, { recursive: true, force: true });
241
+ ownSlots.delete(slot);
242
+ dropTicket();
243
+ };
244
+ }
245
+ }
246
+ else {
247
+ holder = holderOf(lockDir);
95
248
  }
96
- if (breakIfStale(lockDir, now))
97
- continue;
98
249
  if (!told) {
99
250
  told = true;
100
- opts.onWait?.(holderOf(lockDir));
251
+ opts.onWait?.(holder, Math.max(0, ahead));
101
252
  }
102
- if (opts.maxWaitMs !== undefined && now() - started >= opts.maxWaitMs)
253
+ if (opts.maxWaitMs !== undefined && now() - started >= opts.maxWaitMs) {
254
+ dropTicket();
103
255
  return noop;
256
+ }
104
257
  await sleep(opts.pollMs ?? 1000);
105
258
  }
106
259
  }
107
- const heavyMutex = createKeyedMutex();
108
260
  export async function withHeavyLock(lockDir, fn, opts = {}) {
109
- return heavyMutex.run(lockDir, async () => {
110
- const release = await acquireHeavyLock(lockDir, opts);
111
- try {
112
- return await fn();
113
- }
114
- finally {
115
- release();
116
- }
117
- });
261
+ const release = await acquireHeavyLock(lockDir, opts);
262
+ try {
263
+ return await fn();
264
+ }
265
+ finally {
266
+ release();
267
+ }
118
268
  }
119
- const HEAVY_SCRIPT_VERSION = 1;
120
- const DEVICE_SCRIPT_VERSION = 1;
269
+ const HEAVY_SCRIPT_VERSION = 3;
121
270
  function helperScript(h) {
271
+ const slots = h.slotsVar ? `\${${h.slotsVar}:-1}` : '1';
122
272
  return `#!/bin/sh
123
273
  # ${h.name} v${h.version} — written by the FireFunc runner (locks.ts).
124
274
  # ${h.header}
125
275
  # ${h.usage}
126
- # It waits for ${h.waitsFor}, then runs yours.
276
+ # It waits its turn — in the order commands arrived — for ${h.waitsFor}, then runs yours.
127
277
  lock="\${${h.lockVar}:-}"
128
278
  if [ -z "$lock" ] || [ "\${${h.heldVar}:-}" = "$lock" ]; then
129
279
  exec "$@"
130
280
  fi
131
- mkdir -p "$(dirname "$lock")" 2>/dev/null
132
- told=0
133
- while ! mkdir "$lock" 2>/dev/null; do
134
- holder=$(sed -n 1p "$lock/owner" 2>/dev/null)
135
- if [ -n "$holder" ] && ! kill -0 "$holder" 2>/dev/null; then
136
- away="$lock.stale-$$"
137
- mv "$lock" "$away" 2>/dev/null && rm -rf "$away"
138
- continue
139
- fi
140
- if [ -z "$holder" ] && [ -n "$(find "$lock" -maxdepth 0 -mmin +1 2>/dev/null)" ]; then
141
- away="$lock.stale-$$"
142
- mv "$lock" "$away" 2>/dev/null && rm -rf "$away"
143
- continue
281
+ slots="${slots}"
282
+ case "$slots" in ''|*[!0-9]*) slots=1 ;; esac
283
+ [ "$slots" -lt 1 ] && slots=1
284
+ q="$lock.queue"
285
+ mkdir -p "$q" 2>/dev/null
286
+ tag=$(printf '%s' "\${FIREFUNC_HEAVY_TAG:-}" | tr -cd 'a-z0-9' | cut -c1-12)
287
+ if [ \${#tag} -eq 12 ]; then ticket="$(date +%s)000-$(printf '%010d' $$)-st$tag"; else ticket="$(date +%s)000-$(printf '%010d' $$)-sh"; fi
288
+ held=""
289
+ cleanup() {
290
+ rm -f "$q/$ticket"
291
+ if [ -n "$held" ]; then rm -rf "$held"; fi
292
+ }
293
+ trap cleanup EXIT
294
+ trap 'cleanup; exit 130' INT
295
+ trap 'cleanup; exit 143' TERM
296
+ : > "$q/$ticket"
297
+ told=""
298
+ while [ -z "$held" ]; do
299
+ [ -e "$q/$ticket" ] || : > "$q/$ticket"
300
+ ahead=0
301
+ for t in $(ls "$q" 2>/dev/null | LC_ALL=C sort); do
302
+ case "$t" in [0-9]*-[0-9]*-*) ;; *) continue ;; esac
303
+ tp=$(echo "$t" | cut -d- -f2 | sed 's/^0*//')
304
+ if [ -n "$tp" ] && ! kill -0 "$tp" 2>/dev/null; then
305
+ rm -f "$q/$t"
306
+ continue
307
+ fi
308
+ [ "$t" = "$ticket" ] && break
309
+ ahead=$((ahead + 1))
310
+ done
311
+ holder=""
312
+ if [ "$ahead" -lt "$slots" ]; then
313
+ i=0
314
+ while [ "$i" -lt "$slots" ]; do
315
+ if [ "$i" = 0 ]; then s="$lock"; else s="$lock.slot-$i"; fi
316
+ if mkdir "$s" 2>/dev/null; then
317
+ held="$s"
318
+ break
319
+ fi
320
+ h=$(sed -n 1p "$s/owner" 2>/dev/null)
321
+ if [ -n "$h" ] && ! kill -0 "$h" 2>/dev/null; then
322
+ away="$s.stale-$$"
323
+ mv "$s" "$away" 2>/dev/null && rm -rf "$away"
324
+ continue
325
+ fi
326
+ if [ -z "$h" ] && [ -n "$(find "$s" -maxdepth 0 -mmin +1 2>/dev/null)" ]; then
327
+ away="$s.stale-$$"
328
+ mv "$s" "$away" 2>/dev/null && rm -rf "$away"
329
+ continue
330
+ fi
331
+ [ -z "$holder" ] && holder="$h"
332
+ i=$((i + 1))
333
+ done
144
334
  fi
145
- if [ "$told" = 0 ]; then
146
- echo "${h.name}: ${h.waiting} (pid \${holder:-?}) — waiting for it to finish first" >&2
147
- told=1
335
+ [ -n "$held" ] && break
336
+ if [ "$told" != "$ahead" ]; then
337
+ echo "${h.name}: ${h.waiting} (pid \${holder:-?}; $ahead waiting ahead of this one, $slots at once) — waiting for its turn" >&2
338
+ told="$ahead"
148
339
  fi
149
- sleep 2
340
+ sleep 1
150
341
  done
151
- printf '%s\\n%s\\n%s\\n%s\\n' "$$" "$(hostname)" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "engine" > "$lock/owner"
152
- trap 'rm -rf "$lock"' EXIT
153
- trap 'rm -rf "$lock"; exit 130' INT
154
- trap 'rm -rf "$lock"; exit 143' TERM
342
+ printf '%s\\n%s\\n%s\\n%s\\n' "$$" "$(hostname)" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "engine" > "$held/owner"
155
343
  ${h.heldVar}="$lock"
156
344
  export ${h.heldVar}
157
345
  "$@"
@@ -161,28 +349,20 @@ export function heavyScript() {
161
349
  return helperScript({
162
350
  name: 'firefunc-heavy',
163
351
  version: HEAVY_SCRIPT_VERSION,
164
- header: 'Runs ONE heavy command at a time per repository on this machine:',
352
+ header: 'Runs heavy commands per repository on this machine a few at a time ($FIREFUNC_HEAVY_SLOTS), in arrival order:',
165
353
  usage: '"$FIREFUNC_HEAVY" pnpm test',
166
354
  lockVar: 'FIREFUNC_HEAVY_LOCK',
167
355
  heldVar: 'FIREFUNC_HEAVY_HELD',
168
- waitsFor: 'any other heavy command in the same repository',
169
- waiting: 'another heavy command is running in this repository',
356
+ slotsVar: 'FIREFUNC_HEAVY_SLOTS',
357
+ waitsFor: 'a free heavy slot in the same repository',
358
+ waiting: 'every heavy slot in this repository is in use',
170
359
  });
171
360
  }
172
361
  export function deviceLockDir(runnerDir) {
173
362
  return join(runnerDir, 'locks', 'device.lock');
174
363
  }
175
364
  export function deviceScript() {
176
- return helperScript({
177
- name: 'firefunc-device',
178
- version: DEVICE_SCRIPT_VERSION,
179
- header: 'Gives ONE session at a time the device (the emulator) on this machine:',
180
- usage: '"$FIREFUNC_DEVICE" maestro test e2e/',
181
- lockVar: 'FIREFUNC_DEVICE_LOCK',
182
- heldVar: 'FIREFUNC_DEVICE_HELD',
183
- waitsFor: 'any other session using the device',
184
- waiting: 'another session is using the device',
185
- });
365
+ return deviceLeaseScript();
186
366
  }
187
367
  function ensureScript(runnerDir, name, text) {
188
368
  if (process.platform === 'win32')
@@ -0,0 +1,21 @@
1
+ import type { RunnerConfig } from './config.js';
2
+ export type CommandsReport = {
3
+ checked: string[];
4
+ missing: string[];
5
+ };
6
+ export declare const MACHINE_PROBES: Readonly<Record<string, string>>;
7
+ export declare function probePasses(name: string, pathVar: string): boolean;
8
+ export declare function cachedProbe(run?: (name: string, pathVar: string) => boolean, now?: () => number): (name: string, pathVar: string) => boolean;
9
+ export declare function commandOnPath(name: string, pathVar: string, platform?: NodeJS.Platform, pathExt?: string): boolean;
10
+ export declare function runPath(cfg: Pick<RunnerConfig, 'claudeEnv'>, env?: NodeJS.ProcessEnv): string;
11
+ export declare function commandsReport(required: readonly string[], pathVar: string, has?: (name: string, pathVar: string) => boolean): CommandsReport;
12
+ export declare function commandWatch(deps: {
13
+ path: () => string;
14
+ has?: (name: string, pathVar: string) => boolean;
15
+ now?: () => number;
16
+ everyMs?: number;
17
+ log?: (msg: string) => void;
18
+ }): {
19
+ update(list: string[] | null): void;
20
+ report(): CommandsReport | undefined;
21
+ };
@@ -0,0 +1,107 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ import { existsSync, statSync } from 'node:fs';
3
+ import { delimiter, join } from 'node:path';
4
+ const COMMAND = /^[A-Za-z0-9][A-Za-z0-9._+-]{0,63}$/;
5
+ export const MACHINE_PROBES = {
6
+ '@node-sqlite-fts5': "const { DatabaseSync } = require('node:sqlite'); new DatabaseSync(':memory:').exec('CREATE VIRTUAL TABLE t USING fts5(x)');",
7
+ };
8
+ const PROBE_TIMEOUT_MS = 15_000;
9
+ const PROBE_KEEP_MS = 10 * 60_000;
10
+ export function probePasses(name, pathVar) {
11
+ const program = MACHINE_PROBES[name];
12
+ if (!program)
13
+ return false;
14
+ const env = {};
15
+ for (const [k, v] of Object.entries(process.env))
16
+ if (k.toUpperCase() !== 'PATH')
17
+ env[k] = v;
18
+ try {
19
+ execFileSync('node', ['-e', program], {
20
+ env: { ...env, PATH: pathVar, NODE_NO_WARNINGS: '1' },
21
+ stdio: 'ignore',
22
+ timeout: PROBE_TIMEOUT_MS,
23
+ windowsHide: true,
24
+ });
25
+ return true;
26
+ }
27
+ catch {
28
+ return false;
29
+ }
30
+ }
31
+ export function cachedProbe(run = probePasses, now = () => Date.now()) {
32
+ const kept = new Map();
33
+ return (name, pathVar) => {
34
+ const key = `${name}\u0000${pathVar}`;
35
+ const k = kept.get(key);
36
+ if (k && now() - k.at < PROBE_KEEP_MS)
37
+ return k.ok;
38
+ const ok = run(name, pathVar);
39
+ kept.set(key, { ok, at: now() });
40
+ return ok;
41
+ };
42
+ }
43
+ const defaultProbe = cachedProbe();
44
+ const isRequirement = (c) => COMMAND.test(c) || Object.hasOwn(MACHINE_PROBES, c);
45
+ export function commandOnPath(name, pathVar, platform = process.platform, pathExt = process.env.PATHEXT ?? '.EXE;.CMD;.BAT;.COM') {
46
+ const exts = platform === 'win32' ? ['', ...pathExt.split(';').filter(Boolean)] : [''];
47
+ const sep = platform === 'win32' ? ';' : delimiter;
48
+ for (const dir of pathVar.split(sep).filter(Boolean)) {
49
+ for (const ext of exts) {
50
+ try {
51
+ const p = join(dir, name + ext);
52
+ if (existsSync(p) && statSync(p).isFile())
53
+ return true;
54
+ }
55
+ catch {
56
+ }
57
+ }
58
+ }
59
+ return false;
60
+ }
61
+ export function runPath(cfg, env = process.env) {
62
+ return cfg.claudeEnv?.PATH ?? env.PATH ?? env.Path ?? '';
63
+ }
64
+ export function commandsReport(required, pathVar, has = (n, p) => Object.hasOwn(MACHINE_PROBES, n) ? defaultProbe(n, p) : commandOnPath(n, p)) {
65
+ const checked = [...new Set(required.filter(isRequirement))].slice(0, 100);
66
+ return { checked, missing: checked.filter((c) => !has(c, pathVar)) };
67
+ }
68
+ export function commandWatch(deps) {
69
+ const now = deps.now ?? (() => Date.now());
70
+ const everyMs = deps.everyMs ?? 60_000;
71
+ let required = null;
72
+ let last = null;
73
+ let readAt = 0;
74
+ const read = () => {
75
+ if (!required)
76
+ return null;
77
+ const r = commandsReport(required, deps.path(), deps.has);
78
+ const was = last?.missing.join(',') ?? '';
79
+ const is = r.missing.join(',');
80
+ if (is && is !== was) {
81
+ deps.log?.(`this machine lacks ${r.missing.join(', ')}, which a project it serves needs — FireFunc hands it none of that project's runs until the runner finds ${r.missing.length === 1 ? 'it' : 'them'} on its PATH`);
82
+ }
83
+ else if (!is && was) {
84
+ deps.log?.(`found ${was.split(',').join(', ')} — taking that project's runs again`);
85
+ }
86
+ last = r;
87
+ readAt = now();
88
+ return r;
89
+ };
90
+ return {
91
+ update(list) {
92
+ const next = list ? [...list].sort() : null;
93
+ const changed = (next?.join(',') ?? null) !== (required?.join(',') ?? null);
94
+ required = next;
95
+ if (changed)
96
+ read();
97
+ },
98
+ report() {
99
+ if (!required)
100
+ return undefined;
101
+ if (!last || now() - readAt >= everyMs)
102
+ read();
103
+ return last ?? undefined;
104
+ },
105
+ };
106
+ }
107
+ //# sourceMappingURL=machine-commands.js.map
@@ -0,0 +1,18 @@
1
+ export declare const PROFILE_TOOLS: readonly string[];
2
+ export type CpuClass = 'apple_silicon' | 'arm64' | 'x64' | 'other';
3
+ export type MachineProfile = {
4
+ cpuClass: CpuClass;
5
+ cpuModel: string | null;
6
+ tools: string[];
7
+ };
8
+ export declare function cpuClassOf(os: string, architecture: string): CpuClass;
9
+ export declare function cpuModelOf(read?: () => Array<{
10
+ model?: string;
11
+ }>): string | null;
12
+ export declare function readMachineProfile(pathVar: string, deps?: {
13
+ os?: string;
14
+ arch?: string;
15
+ model?: () => string | null;
16
+ has?: (name: string, pathVar: string) => boolean;
17
+ }): MachineProfile;
18
+ export declare function machineProfile(pathVar: string, now?: number): MachineProfile | null;
@@ -0,0 +1,69 @@
1
+ import { arch, cpus, platform } from 'node:os';
2
+ import { commandOnPath } from './machine-commands.js';
3
+ export const PROFILE_TOOLS = [
4
+ 'adb',
5
+ 'emulator',
6
+ 'xcodebuild',
7
+ 'xcrun',
8
+ 'maestro',
9
+ 'java',
10
+ 'gradle',
11
+ 'docker',
12
+ 'node',
13
+ 'python3',
14
+ 'cargo',
15
+ 'go',
16
+ 'flutter',
17
+ 'pod',
18
+ ];
19
+ export function cpuClassOf(os, architecture) {
20
+ if (architecture === 'arm64')
21
+ return os === 'darwin' ? 'apple_silicon' : 'arm64';
22
+ if (architecture === 'x64')
23
+ return 'x64';
24
+ return 'other';
25
+ }
26
+ export function cpuModelOf(read = cpus) {
27
+ try {
28
+ const m = read()[0]?.model;
29
+ if (typeof m !== 'string')
30
+ return null;
31
+ const t = m.replace(/\s+/g, ' ').trim();
32
+ return t ? t.slice(0, 60) : null;
33
+ }
34
+ catch {
35
+ return null;
36
+ }
37
+ }
38
+ export function readMachineProfile(pathVar, deps = {}) {
39
+ const has = deps.has ?? ((n, p) => commandOnPath(n, p));
40
+ const tools = [];
41
+ for (const t of PROFILE_TOOLS) {
42
+ try {
43
+ if (has(t, pathVar))
44
+ tools.push(t);
45
+ }
46
+ catch {
47
+ }
48
+ }
49
+ return {
50
+ cpuClass: cpuClassOf(deps.os ?? platform(), deps.arch ?? arch()),
51
+ cpuModel: (deps.model ?? (() => cpuModelOf()))(),
52
+ tools,
53
+ };
54
+ }
55
+ const EVERY_MS = 5 * 60_000;
56
+ let cached = null;
57
+ export function machineProfile(pathVar, now = Date.now()) {
58
+ try {
59
+ if (cached && cached.path === pathVar && now - cached.at < EVERY_MS)
60
+ return cached.profile;
61
+ const profile = readMachineProfile(pathVar);
62
+ cached = { at: now, path: pathVar, profile };
63
+ return profile;
64
+ }
65
+ catch {
66
+ return null;
67
+ }
68
+ }
69
+ //# sourceMappingURL=machine-profile.js.map
@@ -0,0 +1,2 @@
1
+ export declare function mcpServersFailedAtStart(streamJson: string): string[];
2
+ export declare function firefuncReconnectedBrief(): string;
@@ -0,0 +1,28 @@
1
+ export function mcpServersFailedAtStart(streamJson) {
2
+ const failed = new Set();
3
+ for (const line of streamJson.split('\n')) {
4
+ const t = line.trim();
5
+ if (!t.startsWith('{') || !t.includes('"init"') || !t.includes('"mcp_servers"'))
6
+ continue;
7
+ try {
8
+ const ev = JSON.parse(t);
9
+ if (ev.type !== 'system' || ev.subtype !== 'init' || !Array.isArray(ev.mcp_servers))
10
+ continue;
11
+ failed.clear();
12
+ for (const s of ev.mcp_servers) {
13
+ const o = s;
14
+ if (typeof o?.name === 'string' && o.status === 'failed')
15
+ failed.add(o.name);
16
+ }
17
+ }
18
+ catch {
19
+ }
20
+ }
21
+ return [...failed];
22
+ }
23
+ export function firefuncReconnectedBrief() {
24
+ return ("FireFunc's own tools (the `firefunc` MCP server) could not connect when your session started, " +
25
+ 'so nothing you were asked to report through them was recorded. They are connected now. ' +
26
+ 'Check what you already did, then finish the task — and report through those tools what the task asks you to report.');
27
+ }
28
+ //# sourceMappingURL=mcp-init.js.map
package/dist/mcp.d.ts CHANGED
@@ -20,7 +20,7 @@ export type McpRunConfig = {
20
20
  servers: McpServerSpec[];
21
21
  callBudget: number | null;
22
22
  };
23
- export type McpFormat = 'claude' | 'cursor' | 'gemini' | 'codex' | 'copilot';
23
+ export type McpFormat = 'claude' | 'cursor' | 'gemini' | 'codex' | 'copilot' | 'opencode';
24
24
  export type McpCredentialClient = {
25
25
  mcpCredential(runId: string, server: string): Promise<{
26
26
  token: string;
@@ -50,6 +50,11 @@ export type PrepareMcpOptions = {
50
50
  relay?: boolean;
51
51
  };
52
52
  export declare function mcpFormatForEngine(engine: string | null | undefined): McpFormat | null;
53
+ declare function parseServer(raw: unknown): {
54
+ spec: McpServerSpec;
55
+ } | {
56
+ reject: string;
57
+ };
53
58
  export type CachedMcpLogin = {
54
59
  server: string;
55
60
  tokenType: string;
@@ -58,5 +63,12 @@ export type CachedMcpLogin = {
58
63
  accountLabel?: string | null;
59
64
  };
60
65
  export declare function mcpLoginPath(server: string): string;
66
+ type LoginLookup = {
67
+ login: CachedMcpLogin;
68
+ } | {
69
+ missing: 'none' | 'unreadable' | 'expired';
70
+ };
71
+ declare function readCachedLogin(server: string, now: number): LoginLookup;
61
72
  export declare function prepareMcp(opts: PrepareMcpOptions): Promise<PreparedMcp | null>;
62
73
  export declare function mcpFromContext(ctx: InvocationContext, format: McpFormat): PreparedMcp | null;
74
+ export { parseServer as parseMcpServerSpec, readCachedLogin as cachedMcpLogin };