@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.
- package/README.md +167 -10
- package/dist/alive.d.ts +40 -0
- package/dist/alive.js +138 -0
- package/dist/api-fallback.d.ts +4 -2
- package/dist/api-fallback.js +26 -1
- package/dist/api.d.ts +65 -3
- package/dist/api.js +164 -3
- package/dist/brief-file.d.ts +15 -0
- package/dist/brief-file.js +36 -0
- package/dist/brief.d.ts +3 -0
- package/dist/brief.js +14 -4
- 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 +38 -0
- package/dist/config.js +23 -0
- package/dist/cut.d.ts +1 -1
- package/dist/cut.js +13 -3
- package/dist/daemon.js +364 -15
- 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 +10 -9
- 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 +15 -0
- package/dist/git-auth.js +39 -1
- package/dist/held.d.ts +3 -0
- package/dist/job.d.ts +26 -3
- package/dist/job.js +752 -152
- package/dist/keep-awake.d.ts +17 -0
- package/dist/keep-awake.js +90 -0
- package/dist/keep-local.d.ts +1 -1
- package/dist/keep-local.js +5 -3
- package/dist/limit.d.ts +9 -1
- package/dist/limit.js +260 -3
- package/dist/local-checks.d.ts +19 -0
- package/dist/local-checks.js +79 -0
- package/dist/locks.d.ts +18 -1
- package/dist/locks.js +249 -69
- 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/pool.d.ts +1 -0
- package/dist/pool.js +25 -2
- package/dist/pr-title.d.ts +10 -0
- package/dist/pr-title.js +15 -0
- package/dist/recover.d.ts +3 -1
- package/dist/recover.js +27 -7
- 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 +5 -0
- package/dist/run-attachments.js +14 -9
- 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 +5 -0
- package/dist/secret-scan.js +30 -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/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,608 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, rmSync } from 'node:fs';
|
|
2
|
+
import { totalmem } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
const DEVICE_SCRIPT_VERSION = 7;
|
|
5
|
+
export function deviceLeaseScript() {
|
|
6
|
+
return `#!/bin/sh
|
|
7
|
+
# firefunc-device v${DEVICE_SCRIPT_VERSION} — written by the FireFunc runner (device-lease.ts).
|
|
8
|
+
# Gives each device on this machine (the emulator, or one of a pool) to ONE
|
|
9
|
+
# session at a time, and gives it back as it was found:
|
|
10
|
+
# "$FIREFUNC_DEVICE" maestro test e2e/ run one command on the device
|
|
11
|
+
# "$FIREFUNC_DEVICE" --hold keep the device for this session's next commands
|
|
12
|
+
# "$FIREFUNC_DEVICE" --release let a held device go
|
|
13
|
+
# "$FIREFUNC_DEVICE" --boot <avd> start an emulator as the machine's own, and wait for it
|
|
14
|
+
# "$FIREFUNC_DEVICE" --status who holds each device, and since when
|
|
15
|
+
# A command (or a hold) keeps the device at most $FIREFUNC_DEVICE_TIMEOUT
|
|
16
|
+
# seconds (default 1800), and is stopped when no device has answered adb for
|
|
17
|
+
# $FIREFUNC_DEVICE_GONE seconds (default 300) — so a dead emulator never holds
|
|
18
|
+
# every other session up.
|
|
19
|
+
lock="\${FIREFUNC_DEVICE_LOCK:-}"
|
|
20
|
+
session="\${FIREFUNC_DEVICE_SESSION:-pid-$$}"
|
|
21
|
+
timeout="\${FIREFUNC_DEVICE_TIMEOUT:-1800}"
|
|
22
|
+
gone="\${FIREFUNC_DEVICE_GONE:-300}"
|
|
23
|
+
boot_wait="\${FIREFUNC_DEVICE_BOOT:-300}"
|
|
24
|
+
case "$timeout" in ''|*[!0-9]*) timeout=1800 ;; esac
|
|
25
|
+
case "$gone" in ''|*[!0-9]*) gone=300 ;; esac
|
|
26
|
+
case "$boot_wait" in ''|*[!0-9]*) boot_wait=300 ;; esac
|
|
27
|
+
# How many emulators this machine's memory carries (the runner sizes it; empty: no cap).
|
|
28
|
+
max_emu="\${FIREFUNC_DEVICE_EMULATORS:-}"
|
|
29
|
+
case "$max_emu" in ''|0|*[!0-9]*) max_emu="" ;; esac
|
|
30
|
+
me="firefunc-device"
|
|
31
|
+
say() { echo "$me: $*" >&2; }
|
|
32
|
+
now() { date +%s; }
|
|
33
|
+
|
|
34
|
+
# The machine's devices: one lock for "the device", or one per listed serial.
|
|
35
|
+
serials="\${FIREFUNC_DEVICE_SERIALS:-}"
|
|
36
|
+
if [ -n "\${ANDROID_SERIAL:-}" ] && [ -n "$serials" ]; then
|
|
37
|
+
case " $serials " in *" $ANDROID_SERIAL "*) serials="$ANDROID_SERIAL" ;; esac
|
|
38
|
+
fi
|
|
39
|
+
slot_dir() { if [ -z "$1" ]; then echo "$lock"; else echo "$lock-$1"; fi; }
|
|
40
|
+
slots() { if [ -z "$serials" ]; then echo "-"; else echo "$serials"; fi; }
|
|
41
|
+
slot_of() { [ "$1" = "-" ] && echo "" || echo "$1"; }
|
|
42
|
+
label() { [ "$1" = "-" ] && echo "the device" || echo "$1"; }
|
|
43
|
+
|
|
44
|
+
# adb, with a time limit (a wedged adb server must not stall the watch).
|
|
45
|
+
have_adb() { command -v adb >/dev/null 2>&1; }
|
|
46
|
+
adb_t() {
|
|
47
|
+
out="$(mktemp "\${TMPDIR:-/tmp}/ffdev.XXXXXX")" || return 1
|
|
48
|
+
adb "$@" > "$out" 2>/dev/null &
|
|
49
|
+
ap=$!
|
|
50
|
+
n=0
|
|
51
|
+
while kill -0 "$ap" 2>/dev/null && [ "$n" -lt 100 ]; do sleep 0.1; n=$((n + 1)); done
|
|
52
|
+
kill "$ap" 2>/dev/null
|
|
53
|
+
wait "$ap" 2>/dev/null
|
|
54
|
+
tr -d '\\r' < "$out"
|
|
55
|
+
rm -f "$out"
|
|
56
|
+
}
|
|
57
|
+
# The serials answering adb as "device" (the given one only, when given).
|
|
58
|
+
live_devices() {
|
|
59
|
+
have_adb || return 0
|
|
60
|
+
adb_t devices | awk 'NR > 1 && $2 == "device" { print $1 }' | {
|
|
61
|
+
if [ -n "$1" ]; then grep -x -F "$1"; else cat; fi
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
# A device's settings a test may change: put back as they were found (gap 55).
|
|
66
|
+
# A "system." key is Android's system namespace; the rest are global.
|
|
67
|
+
SETTINGS="airplane_mode_on wifi_on mobile_data http_proxy system.sound_effects_enabled"
|
|
68
|
+
ns_of() { case "$1" in system.*) echo system ;; *) echo global ;; esac; }
|
|
69
|
+
key_of() { echo "\${1#system.}"; }
|
|
70
|
+
snapshot() { # <held dir> <serial or "">
|
|
71
|
+
have_adb || return 0
|
|
72
|
+
for s in $(live_devices "$2"); do
|
|
73
|
+
for k in $SETTINGS; do
|
|
74
|
+
v="$(adb_t -s "$s" shell settings get "$(ns_of "$k")" "$(key_of "$k")" | head -n 1)"
|
|
75
|
+
echo "$s $k \${v:-null}"
|
|
76
|
+
done
|
|
77
|
+
done > "$1/state" 2>/dev/null
|
|
78
|
+
}
|
|
79
|
+
# The audio a recording needs (gaps 68, 69): an emulator's host microphone on,
|
|
80
|
+
# touch sounds off — the emulator's microphone dies when its own sound output
|
|
81
|
+
# stops. Touch sounds are put back by restore; the microphone switch stays on.
|
|
82
|
+
prepare_audio() { # <serial or "">
|
|
83
|
+
have_adb || return 0
|
|
84
|
+
[ "\${FIREFUNC_DEVICE_AUDIO:-}" = keep ] && return 0
|
|
85
|
+
for s in $(live_devices "$1"); do
|
|
86
|
+
case "$s" in emulator-*) adb_t -s "$s" emu avd hostmicon >/dev/null ;; esac
|
|
87
|
+
adb_t -s "$s" shell settings put system sound_effects_enabled 0 >/dev/null
|
|
88
|
+
done
|
|
89
|
+
}
|
|
90
|
+
restore() { # <held dir>
|
|
91
|
+
[ -s "$1/state" ] && have_adb || return 0
|
|
92
|
+
while read -r s k v; do
|
|
93
|
+
[ -n "$k" ] || continue
|
|
94
|
+
cur="$(adb_t -s "$s" shell settings get "$(ns_of "$k")" "$(key_of "$k")" | head -n 1)"
|
|
95
|
+
cur="\${cur:-null}"
|
|
96
|
+
[ "$cur" = "$v" ] && continue
|
|
97
|
+
case "$k" in
|
|
98
|
+
airplane_mode_on)
|
|
99
|
+
if [ "$v" = 1 ]; then w=enable; else w=disable; fi
|
|
100
|
+
adb_t -s "$s" shell cmd connectivity airplane-mode "$w" >/dev/null
|
|
101
|
+
say "put back airplane mode ($w) on $s" ;;
|
|
102
|
+
wifi_on)
|
|
103
|
+
if [ "$v" = 0 ]; then w=disable; else w=enable; fi
|
|
104
|
+
adb_t -s "$s" shell svc wifi "$w" >/dev/null
|
|
105
|
+
say "put back wifi ($w) on $s" ;;
|
|
106
|
+
mobile_data)
|
|
107
|
+
if [ "$v" = 0 ]; then w=disable; else w=enable; fi
|
|
108
|
+
adb_t -s "$s" shell svc data "$w" >/dev/null
|
|
109
|
+
say "put back mobile data ($w) on $s" ;;
|
|
110
|
+
http_proxy)
|
|
111
|
+
if [ "$v" = null ] || [ -z "$v" ] || [ "$v" = ":0" ]; then
|
|
112
|
+
adb_t -s "$s" shell settings put global http_proxy :0 >/dev/null
|
|
113
|
+
adb_t -s "$s" shell settings delete global http_proxy >/dev/null
|
|
114
|
+
say "cleared the proxy a command left on $s"
|
|
115
|
+
else
|
|
116
|
+
adb_t -s "$s" shell settings put global http_proxy "$v" >/dev/null
|
|
117
|
+
say "put back the proxy on $s"
|
|
118
|
+
fi ;;
|
|
119
|
+
system.sound_effects_enabled)
|
|
120
|
+
if [ "$v" != null ] && [ -n "$v" ]; then
|
|
121
|
+
adb_t -s "$s" shell settings put system sound_effects_enabled "$v" >/dev/null
|
|
122
|
+
fi ;;
|
|
123
|
+
esac
|
|
124
|
+
done < "$1/state"
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
holder_pid() { sed -n 1p "$1/owner" 2>/dev/null; }
|
|
128
|
+
holder_session() { sed -n 4p "$1/owner" 2>/dev/null; }
|
|
129
|
+
# Break a slot whose holder is gone (renamed away first: one breaker wins).
|
|
130
|
+
break_stale() {
|
|
131
|
+
h="$(holder_pid "$1")"
|
|
132
|
+
if [ -n "$h" ] && kill -0 "$h" 2>/dev/null; then return 1; fi
|
|
133
|
+
if [ -z "$h" ] && [ -z "$(find "$1" -maxdepth 0 -mmin +1 2>/dev/null)" ]; then return 1; fi
|
|
134
|
+
away="$1.stale-$$"
|
|
135
|
+
mv "$1" "$away" 2>/dev/null && rm -rf "$away"
|
|
136
|
+
return 0
|
|
137
|
+
}
|
|
138
|
+
# The slot this session holds already, if any.
|
|
139
|
+
mine() {
|
|
140
|
+
for x in $(slots); do
|
|
141
|
+
d="$(slot_dir "$(slot_of "$x")")"
|
|
142
|
+
if [ -d "$d" ] && [ "$(holder_session "$d")" = "$session" ] && ! break_stale "$d"; then
|
|
143
|
+
echo "$x"
|
|
144
|
+
return 0
|
|
145
|
+
fi
|
|
146
|
+
done
|
|
147
|
+
return 1
|
|
148
|
+
}
|
|
149
|
+
write_owner() { # <dir> <pid> — written whole, then moved into place
|
|
150
|
+
printf '%s\\n%s\\n%s\\n%s\\n%s\\n' "$2" "$(hostname)" "$(now)" "$session" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$1/owner.$$"
|
|
151
|
+
mv -f "$1/owner.$$" "$1/owner"
|
|
152
|
+
}
|
|
153
|
+
# Take a free slot, waiting for one; sets $held and $serial.
|
|
154
|
+
take() {
|
|
155
|
+
mkdir -p "$(dirname "$lock")" 2>/dev/null
|
|
156
|
+
told=""
|
|
157
|
+
while :; do
|
|
158
|
+
for x in $(slots); do
|
|
159
|
+
d="$(slot_dir "$(slot_of "$x")")"
|
|
160
|
+
if mkdir "$d" 2>/dev/null; then
|
|
161
|
+
held="$d"
|
|
162
|
+
serial="$(slot_of "$x")"
|
|
163
|
+
write_owner "$d" "$$"
|
|
164
|
+
return 0
|
|
165
|
+
fi
|
|
166
|
+
break_stale "$d" && continue
|
|
167
|
+
done
|
|
168
|
+
if [ -z "$told" ]; then
|
|
169
|
+
for x in $(slots); do
|
|
170
|
+
d="$(slot_dir "$(slot_of "$x")")"
|
|
171
|
+
since="$(sed -n 3p "$d/owner" 2>/dev/null)"
|
|
172
|
+
if [ -n "$since" ]; then
|
|
173
|
+
say "$(label "$x") is held by session $(holder_session "$d") for $(( $(now) - since ))s — waiting for it to finish first"
|
|
174
|
+
break
|
|
175
|
+
fi
|
|
176
|
+
done
|
|
177
|
+
told=1
|
|
178
|
+
fi
|
|
179
|
+
sleep 1
|
|
180
|
+
done
|
|
181
|
+
}
|
|
182
|
+
# Let a slot go: put the device back as it was found, then remove the lock.
|
|
183
|
+
let_go() {
|
|
184
|
+
[ -n "\${held:-}" ] && [ -d "$held" ] || return 0
|
|
185
|
+
restore "$held"
|
|
186
|
+
rm -rf "$held"
|
|
187
|
+
held=""
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
# Stop a command and everything it started (never the device: --boot detaches it).
|
|
191
|
+
# Every descendant, however deep (a script's \`adb wait-for-device\` is a
|
|
192
|
+
# grandchild), less a device program and what it runs: the emulator is the
|
|
193
|
+
# machine's even when a command started it (orphans.ts).
|
|
194
|
+
is_device() {
|
|
195
|
+
dc="$(ps -o comm= -p "$1" 2>/dev/null)"
|
|
196
|
+
case "\${dc##*/}" in
|
|
197
|
+
emulator|emulator64-*|qemu-system-*|netsimd|launchd_sim) return 0 ;;
|
|
198
|
+
esac
|
|
199
|
+
return 1
|
|
200
|
+
}
|
|
201
|
+
descendants() {
|
|
202
|
+
for c in $(pgrep -P "$1" 2>/dev/null); do
|
|
203
|
+
is_device "$c" && continue
|
|
204
|
+
echo "$c"
|
|
205
|
+
descendants "$c"
|
|
206
|
+
done
|
|
207
|
+
}
|
|
208
|
+
stop_tree() {
|
|
209
|
+
kids="$(descendants "$1")"
|
|
210
|
+
# shellcheck disable=SC2086
|
|
211
|
+
kill -TERM "$1" $kids 2>/dev/null
|
|
212
|
+
n=0
|
|
213
|
+
while kill -0 "$1" 2>/dev/null && [ "$n" -lt 10 ]; do sleep 0.5; n=$((n + 1)); done
|
|
214
|
+
# shellcheck disable=SC2086
|
|
215
|
+
kill -KILL "$1" $kids $(descendants "$1") 2>/dev/null
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
# Run a command on the device, watching the time it keeps it and that a device still answers.
|
|
219
|
+
watch_run() {
|
|
220
|
+
"$@" &
|
|
221
|
+
cmd=$!
|
|
222
|
+
trap 'stop_tree "$cmd"; let_go; exit 143' TERM
|
|
223
|
+
trap 'stop_tree "$cmd"; let_go; exit 130' INT
|
|
224
|
+
start="$(now)"
|
|
225
|
+
seen=""
|
|
226
|
+
[ -n "$(live_devices "$serial")" ] && seen=1
|
|
227
|
+
if [ -z "$seen" ] && have_adb; then
|
|
228
|
+
say "no device answers adb on this machine right now — start one with \\"\\$FIREFUNC_DEVICE\\" --boot <avd> (emulator -list-avds names them)"
|
|
229
|
+
fi
|
|
230
|
+
missing=""
|
|
231
|
+
last=0
|
|
232
|
+
why=""
|
|
233
|
+
every=$((gone / 4))
|
|
234
|
+
[ "$every" -lt 1 ] && every=1
|
|
235
|
+
[ "$every" -gt 15 ] && every=15
|
|
236
|
+
while kill -0 "$cmd" 2>/dev/null; do
|
|
237
|
+
sleep 1
|
|
238
|
+
t="$(now)"
|
|
239
|
+
if [ $((t - start)) -ge "$timeout" ]; then
|
|
240
|
+
why="it kept the device for \${timeout}s, the most one command may (FIREFUNC_DEVICE_TIMEOUT). Split the work into shorter steps, or use \\"\\$FIREFUNC_DEVICE\\" --hold for a sequence"
|
|
241
|
+
break
|
|
242
|
+
fi
|
|
243
|
+
if have_adb && [ $((t - last)) -ge "$every" ]; then
|
|
244
|
+
last="$t"
|
|
245
|
+
if [ -n "$(live_devices "$serial")" ]; then
|
|
246
|
+
seen=1
|
|
247
|
+
missing=""
|
|
248
|
+
elif [ -n "$seen" ]; then
|
|
249
|
+
missing="\${missing:-$t}"
|
|
250
|
+
if [ $((t - missing)) -ge "$gone" ]; then
|
|
251
|
+
why="no device has answered adb for \${gone}s — the emulator is down. Start it again with \\"\\$FIREFUNC_DEVICE\\" --boot <avd>"
|
|
252
|
+
break
|
|
253
|
+
fi
|
|
254
|
+
fi
|
|
255
|
+
fi
|
|
256
|
+
done
|
|
257
|
+
if [ -n "$why" ]; then
|
|
258
|
+
stop_tree "$cmd"
|
|
259
|
+
wait "$cmd" 2>/dev/null
|
|
260
|
+
say "stopped \\"$*\\" and let the device go: $why"
|
|
261
|
+
rc=124
|
|
262
|
+
else
|
|
263
|
+
wait "$cmd"
|
|
264
|
+
rc=$?
|
|
265
|
+
fi
|
|
266
|
+
trap - TERM INT
|
|
267
|
+
return "$rc"
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
# The emulator, found where the SDK puts it.
|
|
271
|
+
emulator_bin() {
|
|
272
|
+
for e in "$(command -v emulator 2>/dev/null)" \\
|
|
273
|
+
"\${ANDROID_HOME:-}/emulator/emulator" "\${ANDROID_SDK_ROOT:-}/emulator/emulator" \\
|
|
274
|
+
"$HOME/Library/Android/sdk/emulator/emulator" "$HOME/Android/Sdk/emulator/emulator"; do
|
|
275
|
+
if [ -n "$e" ] && [ -x "$e" ]; then echo "$e"; return 0; fi
|
|
276
|
+
done
|
|
277
|
+
return 1
|
|
278
|
+
}
|
|
279
|
+
# Start a program in a session of its own, so no run's end or stop reaches it.
|
|
280
|
+
detach() {
|
|
281
|
+
if command -v setsid >/dev/null 2>&1; then
|
|
282
|
+
setsid "$@" &
|
|
283
|
+
elif command -v perl >/dev/null 2>&1; then
|
|
284
|
+
perl -MPOSIX -e 'POSIX::setsid(); exec @ARGV or die "$!"' "$@" &
|
|
285
|
+
else
|
|
286
|
+
nohup "$@" &
|
|
287
|
+
fi
|
|
288
|
+
}
|
|
289
|
+
# Boot an emulator as the machine's own and wait until it has booted (gaps 96, 98).
|
|
290
|
+
boot() { # <avd> [emulator arguments…]
|
|
291
|
+
avd="\${1:-}"
|
|
292
|
+
if [ -z "$avd" ]; then say "--boot needs the AVD's name: \\"\\$FIREFUNC_DEVICE\\" --boot <avd> (emulator -list-avds names them)"; return 2; fi
|
|
293
|
+
shift
|
|
294
|
+
have_adb || { say "adb is not on this machine's PATH, so the emulator cannot be started or checked — install the Android SDK platform-tools"; return 1; }
|
|
295
|
+
for s in $(live_devices "$serial"); do
|
|
296
|
+
case "$s" in emulator-*)
|
|
297
|
+
if [ "$(adb_t -s "$s" emu avd name | head -n 1)" = "$avd" ]; then
|
|
298
|
+
say "$avd is already running as $s — using it as it is"
|
|
299
|
+
return 0
|
|
300
|
+
fi ;;
|
|
301
|
+
esac
|
|
302
|
+
done
|
|
303
|
+
# A further emulator only while the machine's memory carries it (gap 133): on
|
|
304
|
+
# 16 Oct a 16 GB Mac running seven sessions crashed for want of memory. The
|
|
305
|
+
# count and the start are one step machine-wide (the boot lock, held until
|
|
306
|
+
# the boot ends), and an emulator still booting — adb lists it offline — is
|
|
307
|
+
# counted, so two sessions of a pool booting at once cannot both pass.
|
|
308
|
+
if [ -n "$max_emu" ]; then
|
|
309
|
+
boot_lock="$(dirname "\${lock:-\${TMPDIR:-/tmp}/x}")/emulator-boot.lock"
|
|
310
|
+
take_boot_lock "$boot_lock"
|
|
311
|
+
running="$(emulators_listed)"
|
|
312
|
+
if [ "$running" -ge "$max_emu" ]; then
|
|
313
|
+
rm -rf "$boot_lock"
|
|
314
|
+
say "not starting $avd: $running emulator(s) already run on this machine, and its memory carries $max_emu (about 4 GB each, with half the memory kept for builds and engines). Fix: run your command through \"\$FIREFUNC_DEVICE\" to take a running one when it is free, or the machine's operator sets maxEmulators in the runner's config"
|
|
315
|
+
return 1
|
|
316
|
+
fi
|
|
317
|
+
start_emulator "$avd" "$@"
|
|
318
|
+
rc=$?
|
|
319
|
+
rm -rf "$boot_lock"
|
|
320
|
+
return $rc
|
|
321
|
+
fi
|
|
322
|
+
start_emulator "$avd" "$@"
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
# Every emulator adb lists, booting (offline) ones included.
|
|
326
|
+
emulators_listed() {
|
|
327
|
+
have_adb || { echo 0; return 0; }
|
|
328
|
+
adb_t devices | awk 'NR > 1 && $1 ~ /^emulator-/ { n++ } END { print n + 0 }'
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
# One emulator boot at a time on this machine while a cap applies; a lock whose
|
|
332
|
+
# holder is gone (or never wrote its pid) is broken.
|
|
333
|
+
take_boot_lock() { # <dir>
|
|
334
|
+
told=""
|
|
335
|
+
blank=0
|
|
336
|
+
while ! mkdir "$1" 2>/dev/null; do
|
|
337
|
+
bp="$(cat "$1/pid" 2>/dev/null)"
|
|
338
|
+
if [ -n "$bp" ]; then
|
|
339
|
+
blank=0
|
|
340
|
+
if ! kill -0 "$bp" 2>/dev/null; then rm -rf "$1"; continue; fi
|
|
341
|
+
else
|
|
342
|
+
blank=$((blank + 1))
|
|
343
|
+
if [ "$blank" -ge 10 ]; then rm -rf "$1"; blank=0; continue; fi
|
|
344
|
+
fi
|
|
345
|
+
if [ -z "$told" ]; then say "another session is starting an emulator on this machine — waiting for its boot to end first"; told=1; fi
|
|
346
|
+
sleep 1
|
|
347
|
+
done
|
|
348
|
+
echo $$ > "$1/pid"
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
start_emulator() { # <avd> [emulator arguments…]
|
|
352
|
+
avd="$1"
|
|
353
|
+
shift
|
|
354
|
+
e="$(emulator_bin)" || { say "no emulator program was found (PATH, ANDROID_HOME, ~/Library/Android/sdk) — install the Android SDK's emulator"; return 1; }
|
|
355
|
+
port=""
|
|
356
|
+
case "$serial" in emulator-[0-9]*) port="-port \${serial#emulator-}" ;; esac
|
|
357
|
+
logf="$(dirname "\${lock:-\${TMPDIR:-/tmp}/x}")/emulator-$avd.log"
|
|
358
|
+
# No crash-report dialog: one waiting on the screen hung every later boot (20 Oct).
|
|
359
|
+
# shellcheck disable=SC2086
|
|
360
|
+
detach "$e" -avd "$avd" $port -crash-report-mode disabled "$@" > "$logf" 2>&1 < /dev/null
|
|
361
|
+
ep=$!
|
|
362
|
+
start="$(now)"
|
|
363
|
+
while :; do
|
|
364
|
+
if ! kill -0 "$ep" 2>/dev/null; then
|
|
365
|
+
say "the emulator $avd exited while booting: $(tail -n 3 "$logf" 2>/dev/null | tr '\\n' ' ') (log: $logf)"
|
|
366
|
+
return 1
|
|
367
|
+
fi
|
|
368
|
+
for s in $(live_devices "$serial"); do
|
|
369
|
+
case "$s" in emulator-*)
|
|
370
|
+
if [ "$(adb_t -s "$s" shell getprop sys.boot_completed | head -n 1)" = 1 ] &&
|
|
371
|
+
[ "$(adb_t -s "$s" emu avd name | head -n 1)" = "$avd" ]; then
|
|
372
|
+
say "$avd has booted as $s ($(( $(now) - start ))s); it belongs to the machine — leave it running"
|
|
373
|
+
# Booted after the lease began (inside a --hold), so the lease's own
|
|
374
|
+
# audio step never saw it: its host microphone on now (gap 68).
|
|
375
|
+
[ "\${FIREFUNC_DEVICE_AUDIO:-}" = keep ] || adb_t -s "$s" emu avd hostmicon >/dev/null
|
|
376
|
+
return 0
|
|
377
|
+
fi ;;
|
|
378
|
+
esac
|
|
379
|
+
done
|
|
380
|
+
if [ $(( $(now) - start )) -ge "$boot_wait" ]; then
|
|
381
|
+
kill -TERM "$ep" 2>/dev/null
|
|
382
|
+
say "the emulator $avd did not finish booting within \${boot_wait}s, so it was stopped: a dialog may be waiting on the machine's screen, or the machine is short of memory (log: $logf). Raise FIREFUNC_DEVICE_BOOT for a slow machine"
|
|
383
|
+
return 1
|
|
384
|
+
fi
|
|
385
|
+
sleep 3
|
|
386
|
+
done
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
case "\${1:-}" in
|
|
390
|
+
--status)
|
|
391
|
+
[ -n "$lock" ] || { echo "no device lock on this machine"; exit 0; }
|
|
392
|
+
for x in $(slots); do
|
|
393
|
+
d="$(slot_dir "$(slot_of "$x")")"
|
|
394
|
+
name="$(label "$x")"
|
|
395
|
+
if [ -d "$d" ] && ! break_stale "$d"; then
|
|
396
|
+
since="$(sed -n 3p "$d/owner" 2>/dev/null)"
|
|
397
|
+
echo "$name: held by session $(holder_session "$d") for $(( $(now) - \${since:-$(now)} ))s"
|
|
398
|
+
else
|
|
399
|
+
echo "$name: free"
|
|
400
|
+
fi
|
|
401
|
+
done
|
|
402
|
+
exit 0 ;;
|
|
403
|
+
--release)
|
|
404
|
+
x="$(mine)" || { say "this session holds no device"; exit 0; }
|
|
405
|
+
d="$(slot_dir "$(slot_of "$x")")"
|
|
406
|
+
k="$(holder_pid "$d")"
|
|
407
|
+
[ -n "$k" ] && kill -TERM "$k" 2>/dev/null
|
|
408
|
+
n=0
|
|
409
|
+
while [ -d "$d" ] && [ "$n" -lt 60 ]; do sleep 0.5; n=$((n + 1)); done
|
|
410
|
+
if [ -d "$d" ]; then held="$d"; let_go; fi
|
|
411
|
+
say "let go of $(label "$x")"
|
|
412
|
+
exit 0 ;;
|
|
413
|
+
--keeper)
|
|
414
|
+
# A hold's keeper: owns the slot until --release, the timeout or a stop.
|
|
415
|
+
held="$2"
|
|
416
|
+
session="$3"
|
|
417
|
+
trap 'let_go; exit 0' TERM INT HUP
|
|
418
|
+
# It lets go once no device has answered adb for $gone seconds after one
|
|
419
|
+
# had, like a command (gaps 96, 98): a dead emulator holds nobody up.
|
|
420
|
+
serial="\${4:-}"
|
|
421
|
+
start="$(now)"
|
|
422
|
+
seen=""
|
|
423
|
+
missing=""
|
|
424
|
+
sleep 1
|
|
425
|
+
while [ -d "$held" ] && [ "$(holder_session "$held")" = "$session" ] &&
|
|
426
|
+
[ $(( $(now) - start )) -lt "$timeout" ]; do
|
|
427
|
+
if have_adb; then
|
|
428
|
+
if [ -n "$(live_devices "$serial")" ]; then
|
|
429
|
+
seen=1
|
|
430
|
+
missing=""
|
|
431
|
+
elif [ -n "$seen" ]; then
|
|
432
|
+
missing="\${missing:-$(now)}"
|
|
433
|
+
[ $(( $(now) - missing )) -ge "$gone" ] && break
|
|
434
|
+
fi
|
|
435
|
+
fi
|
|
436
|
+
sleep 2
|
|
437
|
+
done
|
|
438
|
+
[ "$(holder_session "$held")" = "$session" ] && let_go
|
|
439
|
+
exit 0 ;;
|
|
440
|
+
esac
|
|
441
|
+
|
|
442
|
+
# No device lock offered: the command runs as it would without the helper.
|
|
443
|
+
if [ -z "$lock" ]; then
|
|
444
|
+
case "\${1:-}" in
|
|
445
|
+
--hold|--release) exit 0 ;;
|
|
446
|
+
--boot) shift; boot "$@"; exit $? ;;
|
|
447
|
+
*) exec "$@" ;;
|
|
448
|
+
esac
|
|
449
|
+
fi
|
|
450
|
+
|
|
451
|
+
# Already inside a device command of this session: straight through.
|
|
452
|
+
if [ "\${FIREFUNC_DEVICE_HELD:-}" = "$lock" ]; then
|
|
453
|
+
[ "\${1:-}" = "--boot" ] && { shift; boot "$@"; exit $?; }
|
|
454
|
+
[ "\${1:-}" = "--hold" ] && exit 0
|
|
455
|
+
exec "$@"
|
|
456
|
+
fi
|
|
457
|
+
FIREFUNC_DEVICE_HELD="$lock"
|
|
458
|
+
export FIREFUNC_DEVICE_HELD
|
|
459
|
+
|
|
460
|
+
# This session holds a device already (--hold): use it.
|
|
461
|
+
if x="$(mine)"; then
|
|
462
|
+
serial="$(slot_of "$x")"
|
|
463
|
+
[ -n "$serial" ] && { ANDROID_SERIAL="$serial"; export ANDROID_SERIAL; }
|
|
464
|
+
[ "\${1:-}" = "--hold" ] && { say "this session already holds $(label "$x")"; exit 0; }
|
|
465
|
+
[ "\${1:-}" = "--boot" ] && { shift; boot "$@"; exit $?; }
|
|
466
|
+
# Watched like any command: its time, and that the device still answers.
|
|
467
|
+
held=""
|
|
468
|
+
watch_run "$@"
|
|
469
|
+
exit $?
|
|
470
|
+
fi
|
|
471
|
+
|
|
472
|
+
held=""
|
|
473
|
+
serial=""
|
|
474
|
+
trap 'let_go' EXIT
|
|
475
|
+
trap 'let_go; exit 130' INT
|
|
476
|
+
trap 'let_go; exit 143' TERM
|
|
477
|
+
take
|
|
478
|
+
[ -n "$serial" ] && { ANDROID_SERIAL="$serial"; export ANDROID_SERIAL; }
|
|
479
|
+
snapshot "$held" "$serial"
|
|
480
|
+
prepare_audio "$serial"
|
|
481
|
+
|
|
482
|
+
if [ "\${1:-}" = "--hold" ]; then
|
|
483
|
+
if [ -z "\${FIREFUNC_DEVICE_SESSION:-}" ]; then
|
|
484
|
+
say "--hold needs the session's name (FIREFUNC_DEVICE_SESSION, set by the runner); run the steps in one \\"\\$FIREFUNC_DEVICE\\" sh -c '…' instead"
|
|
485
|
+
exit 2
|
|
486
|
+
fi
|
|
487
|
+
# The keeper owns the slot from here; this process leaves without letting go.
|
|
488
|
+
sh "$0" --keeper "$held" "$session" "$serial" > /dev/null 2>&1 < /dev/null &
|
|
489
|
+
write_owner "$held" "$!"
|
|
490
|
+
trap - EXIT INT TERM
|
|
491
|
+
say "holding the device\${serial:+ $serial} for this session — until \\"\\$FIREFUNC_DEVICE\\" --release, the run's end, or \${timeout}s"
|
|
492
|
+
exit 0
|
|
493
|
+
fi
|
|
494
|
+
|
|
495
|
+
if [ "\${1:-}" = "--boot" ]; then
|
|
496
|
+
shift
|
|
497
|
+
boot "$@"
|
|
498
|
+
exit $?
|
|
499
|
+
fi
|
|
500
|
+
|
|
501
|
+
watch_run "$@"
|
|
502
|
+
rc=$?
|
|
503
|
+
let_go
|
|
504
|
+
exit "$rc"
|
|
505
|
+
`;
|
|
506
|
+
}
|
|
507
|
+
export function deviceSerials(devices) {
|
|
508
|
+
if (!Array.isArray(devices))
|
|
509
|
+
return null;
|
|
510
|
+
const ok = [
|
|
511
|
+
...new Set(devices.filter((d) => typeof d === 'string' && /^[\w.:-]{1,64}$/.test(d))),
|
|
512
|
+
];
|
|
513
|
+
return ok.length >= 2 ? ok.join(' ') : null;
|
|
514
|
+
}
|
|
515
|
+
export function emulatorBudget(setting, memBytes = totalmem()) {
|
|
516
|
+
if (typeof setting === 'number' && Number.isInteger(setting) && setting >= 1) {
|
|
517
|
+
return Math.min(16, setting);
|
|
518
|
+
}
|
|
519
|
+
return Math.max(1, Math.floor(memBytes / (8 * 1024 ** 3)));
|
|
520
|
+
}
|
|
521
|
+
function isAlive(pid) {
|
|
522
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
523
|
+
return false;
|
|
524
|
+
try {
|
|
525
|
+
process.kill(pid, 0);
|
|
526
|
+
return true;
|
|
527
|
+
}
|
|
528
|
+
catch (e) {
|
|
529
|
+
return e.code === 'EPERM';
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
export async function releaseDeviceHolds(lockDir, session, opts = {}) {
|
|
533
|
+
const out = [];
|
|
534
|
+
const parent = join(lockDir, '..');
|
|
535
|
+
const base = lockDir.split('/').pop() ?? '';
|
|
536
|
+
let names;
|
|
537
|
+
try {
|
|
538
|
+
names = readdirSync(parent);
|
|
539
|
+
}
|
|
540
|
+
catch {
|
|
541
|
+
return out;
|
|
542
|
+
}
|
|
543
|
+
for (const n of names) {
|
|
544
|
+
if (n !== base && !(n.startsWith(`${base}-`) && !n.includes('.stale-')))
|
|
545
|
+
continue;
|
|
546
|
+
const dir = join(parent, n);
|
|
547
|
+
let owner;
|
|
548
|
+
try {
|
|
549
|
+
owner = readFileSync(join(dir, 'owner'), 'utf8').split('\n');
|
|
550
|
+
}
|
|
551
|
+
catch {
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
if ((owner[3] ?? '') !== session)
|
|
555
|
+
continue;
|
|
556
|
+
const keeper = Number(owner[0]);
|
|
557
|
+
if (isAlive(keeper)) {
|
|
558
|
+
try {
|
|
559
|
+
process.kill(keeper, 'SIGTERM');
|
|
560
|
+
}
|
|
561
|
+
catch {
|
|
562
|
+
}
|
|
563
|
+
const until = Date.now() + (opts.waitMs ?? 15_000);
|
|
564
|
+
while (Date.now() < until) {
|
|
565
|
+
try {
|
|
566
|
+
readFileSync(join(dir, 'owner'));
|
|
567
|
+
}
|
|
568
|
+
catch {
|
|
569
|
+
break;
|
|
570
|
+
}
|
|
571
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
rmSync(dir, { recursive: true, force: true });
|
|
575
|
+
out.push(n);
|
|
576
|
+
}
|
|
577
|
+
return out;
|
|
578
|
+
}
|
|
579
|
+
export function deviceReport(lockDir, cfg, memBytes = totalmem()) {
|
|
580
|
+
const serials = deviceSerials(cfg.devices);
|
|
581
|
+
const parent = join(lockDir, '..');
|
|
582
|
+
const base = lockDir.split(/[\\/]/).pop() ?? '';
|
|
583
|
+
let held = 0;
|
|
584
|
+
let names = [];
|
|
585
|
+
try {
|
|
586
|
+
names = readdirSync(parent);
|
|
587
|
+
}
|
|
588
|
+
catch {
|
|
589
|
+
names = [];
|
|
590
|
+
}
|
|
591
|
+
for (const n of names) {
|
|
592
|
+
if (n !== base && !(n.startsWith(`${base}-`) && !n.includes('.stale-')))
|
|
593
|
+
continue;
|
|
594
|
+
try {
|
|
595
|
+
const pid = Number(readFileSync(join(parent, n, 'owner'), 'utf8').split('\n')[0]);
|
|
596
|
+
if (isAlive(pid))
|
|
597
|
+
held += 1;
|
|
598
|
+
}
|
|
599
|
+
catch {
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return {
|
|
603
|
+
pool: serials ? serials.split(' ').length : null,
|
|
604
|
+
held,
|
|
605
|
+
emulators: emulatorBudget(cfg.maxEmulators, memBytes),
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
//# sourceMappingURL=device-lease.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const CHUNK = 12288;
|
|
2
|
+
export type FsErrorCode = 'FileNotFound' | 'NoPermissions' | 'Unavailable';
|
|
3
|
+
export type FsRequest = {
|
|
4
|
+
op: string;
|
|
5
|
+
path: unknown;
|
|
6
|
+
offset?: unknown;
|
|
7
|
+
length?: unknown;
|
|
8
|
+
};
|
|
9
|
+
export type FsResult = {
|
|
10
|
+
ok: true;
|
|
11
|
+
result: unknown;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
error: {
|
|
15
|
+
code: FsErrorCode;
|
|
16
|
+
message: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export declare function pathParts(raw: unknown): string[] | null;
|
|
20
|
+
export declare class FolderServer {
|
|
21
|
+
readonly root: string;
|
|
22
|
+
private readonly rootReal;
|
|
23
|
+
constructor(root: string);
|
|
24
|
+
resolve(raw: unknown): {
|
|
25
|
+
file: string;
|
|
26
|
+
} | {
|
|
27
|
+
refuse: FsResult;
|
|
28
|
+
};
|
|
29
|
+
handle(req: FsRequest): FsResult;
|
|
30
|
+
watch(onChanged: (paths: string[]) => void, debounceMs?: number): () => void;
|
|
31
|
+
}
|