opencode-agent-harness 0.0.0-stage → 0.3.14

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 (171) hide show
  1. package/CHANGELOG.md +217 -0
  2. package/LICENSE +21 -0
  3. package/README.md +636 -2
  4. package/bin/agent-harness.js +3182 -0
  5. package/contrib/global-plugins/README.md +49 -0
  6. package/contrib/global-plugins/harness-freshness.ts +355 -0
  7. package/package.json +86 -4
  8. package/templates/.agents/AGENTS.md +148 -0
  9. package/templates/.agents/features/INDEX.md +10 -0
  10. package/templates/.agents/handoffs/README.md +87 -0
  11. package/templates/.agents/memory/dev-loop.md +59 -0
  12. package/templates/.agents/memory/domain-map.md +29 -0
  13. package/templates/.agents/memory/environments.md +35 -0
  14. package/templates/.agents/memory/flow-map.md +23 -0
  15. package/templates/.agents/memory/history.md +7 -0
  16. package/templates/.agents/memory/locations.md +14 -0
  17. package/templates/.agents/memory/model-routing.md +261 -0
  18. package/templates/.agents/memory/orchestration.md +183 -0
  19. package/templates/.agents/memory/owned-files.md +19 -0
  20. package/templates/.agents/memory/product-principles.md +37 -0
  21. package/templates/.agents/memory/quality-parameters.md +37 -0
  22. package/templates/.agents/memory/stack-versions.md +35 -0
  23. package/templates/.agents/memory/test-design-parameters.md +36 -0
  24. package/templates/.agents/nightly.json +17 -0
  25. package/templates/.agents/qa-plans/README.md +70 -0
  26. package/templates/.agents/qa-runs/README.md +21 -0
  27. package/templates/.agents/reviews/README.md +47 -0
  28. package/templates/.agents/rules/00-operating.md +199 -0
  29. package/templates/.agents/skills/data-engineer/SKILL.md +31 -0
  30. package/templates/.agents/skills/devops-engineer/SKILL.md +50 -0
  31. package/templates/.agents/skills/diagnostics-expert/SKILL.md +44 -0
  32. package/templates/.agents/skills/docs/SKILL.md +140 -0
  33. package/templates/.agents/skills/features/SKILL.md +187 -0
  34. package/templates/.agents/skills/frontend-engineer/SKILL.md +81 -0
  35. package/templates/.agents/skills/handoff/SKILL.md +85 -0
  36. package/templates/.agents/skills/mobile-engineer/SKILL.md +101 -0
  37. package/templates/.agents/skills/planner/SKILL.md +112 -0
  38. package/templates/.agents/skills/product-manager/SKILL.md +115 -0
  39. package/templates/.agents/skills/qa-architect/SKILL.md +224 -0
  40. package/templates/.agents/skills/qa-runner/SKILL.md +64 -0
  41. package/templates/.agents/skills/security-engineer/SKILL.md +53 -0
  42. package/templates/.agents/skills/system-architect/SKILL.md +42 -0
  43. package/templates/.agents/skills/ui-designer/SKILL.md +71 -0
  44. package/templates/.github/workflows/ci.yml +148 -0
  45. package/templates/.github/workflows/deploy-uat.yml +95 -0
  46. package/templates/.github/workflows/nightly.yml +83 -0
  47. package/templates/.github/workflows/uat-health.yml +41 -0
  48. package/templates/.husky/pre-push +23 -0
  49. package/templates/.ignore +28 -0
  50. package/templates/.opencode/agents/data-engineer.md +37 -0
  51. package/templates/.opencode/agents/devops-engineer.md +37 -0
  52. package/templates/.opencode/agents/diagnostics-expert.md +37 -0
  53. package/templates/.opencode/agents/docs.md +37 -0
  54. package/templates/.opencode/agents/features.md +21 -0
  55. package/templates/.opencode/agents/frontend-engineer.md +37 -0
  56. package/templates/.opencode/agents/handoff.md +21 -0
  57. package/templates/.opencode/agents/mobile-engineer.md +37 -0
  58. package/templates/.opencode/agents/planner.md +34 -0
  59. package/templates/.opencode/agents/product-manager.md +29 -0
  60. package/templates/.opencode/agents/qa-architect.md +27 -0
  61. package/templates/.opencode/agents/qa-runner.md +17 -0
  62. package/templates/.opencode/agents/security-engineer.md +37 -0
  63. package/templates/.opencode/agents/system-architect.md +37 -0
  64. package/templates/.opencode/agents/ui-designer.md +37 -0
  65. package/templates/.opencode/commands/board.md +15 -0
  66. package/templates/.opencode/commands/checkpoint.md +25 -0
  67. package/templates/.opencode/commands/feature.md +56 -0
  68. package/templates/.opencode/commands/finish.md +36 -0
  69. package/templates/.opencode/commands/next.md +49 -0
  70. package/templates/.opencode/commands/report.md +55 -0
  71. package/templates/.opencode/commands/resume.md +39 -0
  72. package/templates/.opencode/commands/switch-model.md +34 -0
  73. package/templates/.opencode/commands/tidy.md +49 -0
  74. package/templates/.opencode/plugins/backlog-notify.ts +57 -0
  75. package/templates/.opencode/plugins/backlog.ts +272 -0
  76. package/templates/.opencode/plugins/checkpoint.ts +160 -0
  77. package/templates/.opencode/plugins/context-guard.ts +1164 -0
  78. package/templates/.opencode/plugins/diff.ts +85 -0
  79. package/templates/.opencode/plugins/gates.ts +71 -0
  80. package/templates/.opencode/plugins/job-notify.ts +241 -0
  81. package/templates/.opencode/plugins/orient.ts +67 -0
  82. package/templates/.opencode/plugins/session.ts +89 -0
  83. package/templates/.opencode/plugins/wait.ts +101 -0
  84. package/templates/.opencode/plugins/where.ts +70 -0
  85. package/templates/.opencode/skills/bounded-explore/SKILL.md +38 -0
  86. package/templates/.opencode/skills/dev-loop/SKILL.md +87 -0
  87. package/templates/.vercelignore +16 -0
  88. package/templates/AGENTS.md +27 -0
  89. package/templates/BACKLOG.md +95 -0
  90. package/templates/docs/ci-cd-pipeline.md +81 -0
  91. package/templates/docs/gates.md +128 -0
  92. package/templates/docs/mcp-integrations.md +97 -0
  93. package/templates/docs/mobile-testing.md +555 -0
  94. package/templates/docs/nightly-testing.md +106 -0
  95. package/templates/docs/wsl-optimizations.md +290 -0
  96. package/templates/opencode.json +188 -0
  97. package/templates/scripts/agent-report.js +541 -0
  98. package/templates/scripts/agent-search.sh +117 -0
  99. package/templates/scripts/backlog.js +1924 -0
  100. package/templates/scripts/board-service.sh +214 -0
  101. package/templates/scripts/checkpoint.js +290 -0
  102. package/templates/scripts/finalize.js +416 -0
  103. package/templates/scripts/nightly.js +913 -0
  104. package/templates/scripts/orient.js +514 -0
  105. package/templates/scripts/qa/board-client.js +323 -0
  106. package/templates/scripts/qa/board-lib.js +96 -0
  107. package/templates/scripts/qa/board-sync.js +499 -0
  108. package/templates/scripts/qa/capacity-lib.js +916 -0
  109. package/templates/scripts/qa/check-backlog.js +434 -0
  110. package/templates/scripts/qa/check-capacity.js +85 -0
  111. package/templates/scripts/qa/check-ci-policy.js +164 -0
  112. package/templates/scripts/qa/check-concurrency.js +344 -0
  113. package/templates/scripts/qa/check-dispatch-config.js +296 -0
  114. package/templates/scripts/qa/check-docs.js +288 -0
  115. package/templates/scripts/qa/check-integrate.js +98 -0
  116. package/templates/scripts/qa/check-linger.js +160 -0
  117. package/templates/scripts/qa/check-prefix-stability.js +187 -0
  118. package/templates/scripts/qa/check-product.js +177 -0
  119. package/templates/scripts/qa/check-qa-plan.js +378 -0
  120. package/templates/scripts/qa/check-qa-scripts.js +85 -0
  121. package/templates/scripts/qa/check-quality.js +249 -0
  122. package/templates/scripts/qa/check-test-wiring.js +253 -0
  123. package/templates/scripts/qa/check-worktrees.js +93 -0
  124. package/templates/scripts/qa/ci-triage.js +346 -0
  125. package/templates/scripts/qa/ci-usage-lib.js +185 -0
  126. package/templates/scripts/qa/ci-wait-lib.js +294 -0
  127. package/templates/scripts/qa/command-classifier.js +552 -0
  128. package/templates/scripts/qa/context-guard-core.js +1618 -0
  129. package/templates/scripts/qa/diff.js +174 -0
  130. package/templates/scripts/qa/feature-scaffold.js +224 -0
  131. package/templates/scripts/qa/fixtures/backlog.md +104 -0
  132. package/templates/scripts/qa/governance.js +174 -0
  133. package/templates/scripts/qa/hash-file.js +32 -0
  134. package/templates/scripts/qa/lease-lib.js +263 -0
  135. package/templates/scripts/qa/merge-backlog.js +324 -0
  136. package/templates/scripts/qa/mobile-e2e/analyze-timings.js +300 -0
  137. package/templates/scripts/qa/mobile-e2e/apk-cache.js +420 -0
  138. package/templates/scripts/qa/mobile-e2e/backend-build-cache.js +203 -0
  139. package/templates/scripts/qa/mobile-e2e/boot-emulator.sh +432 -0
  140. package/templates/scripts/qa/mobile-e2e/build-apk.sh +210 -0
  141. package/templates/scripts/qa/mobile-e2e/check-flows.js +568 -0
  142. package/templates/scripts/qa/mobile-e2e/config.yaml +18 -0
  143. package/templates/scripts/qa/mobile-e2e/device-down.sh +88 -0
  144. package/templates/scripts/qa/mobile-e2e/device-up.sh +258 -0
  145. package/templates/scripts/qa/mobile-e2e/ios-simulator-down.sh +72 -0
  146. package/templates/scripts/qa/mobile-e2e/ios-simulator-up.sh +153 -0
  147. package/templates/scripts/qa/mobile-e2e/job-events.js +181 -0
  148. package/templates/scripts/qa/mobile-e2e/prewarm.sh +56 -0
  149. package/templates/scripts/qa/mobile-e2e/run-detached.sh +222 -0
  150. package/templates/scripts/qa/mobile-e2e/run-maestro.js +1543 -0
  151. package/templates/scripts/qa/mobile-e2e/ui-dump.js +327 -0
  152. package/templates/scripts/qa/models.allowlist.txt +32 -0
  153. package/templates/scripts/qa/provider-stats.js +878 -0
  154. package/templates/scripts/qa/run-clean.js +83 -0
  155. package/templates/scripts/qa/run-gates.js +213 -0
  156. package/templates/scripts/qa/run-loop.js +220 -0
  157. package/templates/scripts/qa/session-recon.js +512 -0
  158. package/templates/scripts/qa/store-lib.js +324 -0
  159. package/templates/scripts/qa/tool-lib.js +134 -0
  160. package/templates/scripts/qa/where.js +214 -0
  161. package/templates/scripts/qa/worktree-hygiene.js +217 -0
  162. package/templates/scripts/qa/wslconfig-merge.js +157 -0
  163. package/templates/scripts/serve.js +1328 -0
  164. package/templates/scripts/setup-nightly.sh +185 -0
  165. package/templates/scripts/setup-tmpfs.sh +374 -0
  166. package/templates/scripts/ship.js +586 -0
  167. package/templates/scripts/switch-reasoning-model.ps1 +101 -0
  168. package/templates/scripts/switch-reasoning-model.sh +104 -0
  169. package/templates/scripts/wait-for.js +342 -0
  170. package/templates/scripts/wsl-gpu.sh +180 -0
  171. package/templates/scripts/wsl-tune.sh +319 -0
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env bash
2
+ #
3
+ # mobile E2E — warm the device-tier APK cache OFF the critical path.
4
+ #
5
+ # A cold `assembleRelease` is 7-11 min; run inside `device-up.sh` it blocks the
6
+ # whole tier. This script pays that cost up front (e.g. right after a worktree is
7
+ # created or the harness is updated) so the next `device-up.sh` is a fast
8
+ # cache hit/overlay. It is idempotent and safe to run at any time:
9
+ #
10
+ # * a full-key cache hit exits immediately (nothing to do);
11
+ # * else the native-base + fresh-bundle overlay is tried (seconds);
12
+ # * else the capped Gradle build runs.
13
+ #
14
+ # `build-apk.sh` routes the Gradle build through `serve.js run`, so prewarm obeys
15
+ # the machine-wide capacity envelope: it refuses (non-zero, no build) when a
16
+ # heavy tier is live rather than thrashing the VM. That makes it safe to invoke
17
+ # speculatively.
18
+ #
19
+ # Usage:
20
+ # scripts/qa/mobile-e2e/prewarm.sh # warm, in the foreground
21
+ # scripts/qa/mobile-e2e/prewarm.sh --detach # warm in the background, return
22
+ set -euo pipefail
23
+
24
+ ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
25
+ cd "$ROOT"
26
+ CACHE_JS="$ROOT/scripts/qa/mobile-e2e/apk-cache.js"
27
+
28
+ if [ "${1:-}" = "--detach" ]; then
29
+ LOG="${MOBILE_E2E_PREWARM_LOG:-$ROOT/.serve/prewarm.log}"
30
+ mkdir -p "$(dirname "$LOG")"
31
+ setsid bash "$ROOT/scripts/qa/mobile-e2e/prewarm.sh" >"$LOG" 2>&1 </dev/null &
32
+ echo "prewarm: started detached (log: $LOG, pid $!)"
33
+ exit 0
34
+ fi
35
+
36
+ if [ ! -f "$CACHE_JS" ]; then
37
+ echo "prewarm: no apk-cache.js — nothing to do" >&2
38
+ exit 0
39
+ fi
40
+
41
+ if node "$CACHE_JS" get >/dev/null 2>&1; then
42
+ echo "prewarm: full cache hit — device-up is ready"
43
+ exit 0
44
+ fi
45
+
46
+ if [ "${MOBILE_E2E_APK_OVERLAY:-1}" != "0" ] && node "$CACHE_JS" overlay >/dev/null 2>&1; then
47
+ echo "prewarm: native base + fresh bundle overlaid — device-up is ready"
48
+ exit 0
49
+ fi
50
+
51
+ if [ -x "$ROOT/scripts/qa/mobile-e2e/build-apk.sh" ]; then
52
+ echo "prewarm: building the native base (capped)…"
53
+ bash "$ROOT/scripts/qa/mobile-e2e/build-apk.sh"
54
+ else
55
+ echo "prewarm: no build-apk.sh — nothing to do" >&2
56
+ fi
@@ -0,0 +1,222 @@
1
+ #!/usr/bin/env bash
2
+ #
3
+ # F-291 — run the device matrix DETACHED, so a 15–40 min run never blocks the
4
+ # tool and its progress is observable by tailing the log.
5
+ #
6
+ # Why a script: the device tier is a long-lived process. Launching it inline (or
7
+ # piping it through a buffering filter) makes a healthy run look frozen, which is
8
+ # how a full matrix gets aborted mid-flight. This wraps run-maestro.js with its
9
+ # own session and stdio — the launching shell sees EOF immediately and returns —
10
+ # and points you at the log file for live progress. run-maestro.js already prints
11
+ # a `Running flow (i/n): …` line per invocation, so the tail is meaningful.
12
+ #
13
+ # It also records a machine-readable JOB so the run is NOTIFYABLE instead of
14
+ # polled: `.serve/jobs/`-style dir under MOBILE_E2E_JOB_DIR holds
15
+ # <id>.events.jsonl one JSON line per scenario (run-maestro.js writes it)
16
+ # <id>.json { pid, log, events, cmd, startedAt, sessionID }
17
+ # The `job-notify` opencode plugin watches the events file and pushes per-scenario
18
+ # updates + a completion (and first-failure) notification. With `--wait` the
19
+ # launch blocks here until the run finishes, streaming each scenario line —
20
+ # use it when no plugin is watching (returns the run's exit code).
21
+ #
22
+ # Usage:
23
+ # scripts/qa/mobile-e2e/run-detached.sh --flows all --reuse-session
24
+ # scripts/qa/mobile-e2e/run-detached.sh --flows all --wait # block + stream
25
+ # scripts/qa/mobile-e2e/run-detached.sh --list # live/recent jobs
26
+ # scripts/qa/mobile-e2e/run-detached.sh --stop <job-id|record.json> # kill + matrix_aborted
27
+ # MOBILE_E2E_LOG=/tmp/opencode/run6.log scripts/qa/mobile-e2e/run-detached.sh --flows all
28
+ #
29
+ # `--list` and `--stop` are the deterministic "what is running / stop it" verbs:
30
+ # --list prints each job's pid, running/dead state, age and seconds since its
31
+ # last event; --stop kills the job's process group (the setsid run + its Maestro
32
+ # JVM), force-stops the app, and appends a `matrix_aborted` event so every
33
+ # `wait-for --job` returns immediately instead of being guessed at.
34
+ #
35
+ # Env:
36
+ # MOBILE_E2E_LOG log path (default <jobdir>/<id>.log)
37
+ # MOBILE_E2E_JOB_DIR job dir (default /tmp/opencode/mobile-e2e-jobs)
38
+ # MOBILE_E2E_JOB job id (default timestamp)
39
+ # MOBILE_E2E_JOB_SESSION owning opencode session id (recorded for the plugin)
40
+ # MOBILE_E2E_WAIT_HEARTBEAT --wait progress interval seconds (default 20)
41
+ set -euo pipefail
42
+
43
+ ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
44
+ cd "$ROOT"
45
+
46
+ JOB_DIR="${MOBILE_E2E_JOB_DIR:-/tmp/opencode/mobile-e2e-jobs}"
47
+ JOB_ID="${MOBILE_E2E_JOB:-mobile-e2e-$(date +%Y%m%d-%H%M%S)}"
48
+ SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
49
+
50
+ # Harness-side verbs: `--wait` (block + stream), `--list` (show jobs),
51
+ # `--stop <id>` (kill a run deterministically + write `matrix_aborted`).
52
+ WAIT=0; LIST=0; STOP=""; HELP=0; ARGS=()
53
+ while [ $# -gt 0 ]; do
54
+ case "$1" in
55
+ --wait) WAIT=1; shift;;
56
+ --list) LIST=1; shift;;
57
+ --stop)
58
+ STOP="${2:-}"
59
+ if [ -z "$STOP" ]; then echo "run-detached: --stop needs a job id or record path" >&2; exit 2; fi
60
+ shift 2;;
61
+ -h|--help) HELP=1; shift;;
62
+ *) ARGS+=("$1"); shift;;
63
+ esac
64
+ done
65
+
66
+ if [ "$HELP" = "1" ]; then
67
+ cat <<'EOF'
68
+ run-detached.sh — launch/observe/stop a detached mobile-E2E matrix run.
69
+ run-detached.sh --flows all --reuse-session # launch (returns immediately)
70
+ run-detached.sh --flows all --wait # launch + block, streaming progress
71
+ run-detached.sh --list # live/recent jobs (pid, state, last event)
72
+ run-detached.sh --stop <job-id|record.json> # kill a run + write matrix_aborted
73
+ EOF
74
+ exit 0
75
+ fi
76
+
77
+ if [ "$LIST" = "1" ]; then
78
+ node - "$JOB_DIR" <<'NODE'
79
+ const fs = require("fs"), path = require("path");
80
+ const dir = process.argv[2];
81
+ let files = [];
82
+ try { files = fs.readdirSync(dir).filter((f) => f.endsWith(".json")); } catch {}
83
+ const rows = files
84
+ .map((f) => { try { return JSON.parse(fs.readFileSync(path.join(dir, f), "utf8")); } catch { return null; } })
85
+ .filter((r) => r && (r.id || r.events))
86
+ .sort((a, b) => String(b.startedAt || "").localeCompare(String(a.startedAt || "")));
87
+ if (!rows.length) { console.log("no jobs in " + dir); process.exit(0); }
88
+ const alive = (p) => { try { process.kill(Number(p), 0); return true; } catch (e) { return e && e.code === "EPERM"; } };
89
+ console.log("job".padEnd(30) + "pid".padEnd(9) + "state".padEnd(9) + "age".padEnd(9) + "last-event");
90
+ for (const r of rows) {
91
+ let evAge = "-";
92
+ try { evAge = Math.round((Date.now() - fs.statSync(r.events).mtimeMs) / 1000) + "s"; } catch {}
93
+ const age = r.startedAt ? Math.round((Date.now() - Date.parse(r.startedAt)) / 1000) + "s" : "-";
94
+ console.log(
95
+ String(r.id || "?").padEnd(30) + String(r.pid || "-").padEnd(9) +
96
+ (alive(r.pid) ? "running" : "dead").padEnd(9) + age.padEnd(9) + evAge
97
+ );
98
+ }
99
+ NODE
100
+ exit 0
101
+ fi
102
+
103
+ if [ -n "$STOP" ]; then
104
+ RECORD="$STOP"
105
+ case "$STOP" in */* | *.json) ;; *) RECORD="$JOB_DIR/$STOP.json";; esac
106
+ if [ ! -f "$RECORD" ]; then echo "run-detached: no such job record: $RECORD" >&2; exit 2; fi
107
+ node - "$RECORD" "$SELF_DIR" <<'NODE'
108
+ const fs = require("fs"), path = require("path");
109
+ const { spawnSync } = require("child_process");
110
+ const rec = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
111
+ const selfDir = process.argv[3];
112
+ const pid = Number(rec.pid);
113
+ const alive = (p) => { try { process.kill(Number(p), 0); return true; } catch (e) { return e && e.code === "EPERM"; } };
114
+ let signalled = false;
115
+ if (pid && alive(pid)) {
116
+ try { process.kill(-pid, "SIGTERM"); signalled = true; } catch {}
117
+ if (!signalled) { try { process.kill(pid, "SIGTERM"); signalled = true; } catch {} }
118
+ const until = Date.now() + 3000;
119
+ while (Date.now() < until && alive(pid)) {
120
+ try { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 200); } catch {}
121
+ }
122
+ if (alive(pid)) {
123
+ try { process.kill(-pid, "SIGKILL"); } catch { try { process.kill(pid, "SIGKILL"); } catch {} }
124
+ }
125
+ }
126
+ let appId = process.env.MOBILE_E2E_APP_ID || "com.example.app";
127
+ let localCfg = {};
128
+ try { localCfg = JSON.parse(fs.readFileSync(path.join(selfDir, "local.json"), "utf8")); } catch {}
129
+ if (localCfg.appId) appId = localCfg.appId;
130
+ // Platform: env MOBILE_E2E_PLATFORM > local.json `platform` > android. On iOS
131
+ // stop the app via `xcrun simctl terminate <bundle> <udid>`; on Android the
132
+ // historical `adb shell am force-stop` is unchanged.
133
+ const platform = String(process.env.MOBILE_E2E_PLATFORM || localCfg.platform || "android").toLowerCase();
134
+ if (platform === "ios") {
135
+ const bundleId = process.env.MOBILE_E2E_IOS_BUNDLE_ID || (localCfg.ios && localCfg.ios.bundleId) || appId;
136
+ const udid = process.env.MOBILE_E2E_IOS_UDID || "";
137
+ try { spawnSync("xcrun", ["simctl", "terminate", bundleId].concat(udid ? [udid] : []), { timeout: 5000, stdio: "ignore" }); } catch {}
138
+ } else {
139
+ try { spawnSync("adb", ["shell", "am", "force-stop", appId], { timeout: 5000, stdio: "ignore" }); } catch {}
140
+ }
141
+ if (rec.events) {
142
+ try {
143
+ fs.appendFileSync(rec.events, JSON.stringify({
144
+ type: "matrix_aborted", at: new Date().toISOString(),
145
+ by: "run-detached --stop", reason: "operator stop",
146
+ }) + "\n");
147
+ } catch {}
148
+ }
149
+ console.log("stop: pid " + (pid || "?") + (signalled ? " signalled" : " (already exited)") +
150
+ " · matrix_aborted written" + (rec.events ? " -> " + rec.events : ""));
151
+ NODE
152
+ exit 0
153
+ fi
154
+
155
+ mkdir -p "$JOB_DIR"
156
+ EVENTS="$JOB_DIR/$JOB_ID.events.jsonl"
157
+ RECORD="$JOB_DIR/$JOB_ID.json"
158
+ LOG="${MOBILE_E2E_LOG:-$JOB_DIR/$JOB_ID.log}"
159
+ : >"$EVENTS"
160
+
161
+ # `setsid` gives the run its own session; the inner redirection is on the actual
162
+ # command, so the wrapper's own fds stay closed and the tool returns at once.
163
+ # This is the correct form of the anti-pattern called out in docs/mobile-testing.md.
164
+ MOBILE_E2E_EVENTS="$EVENTS" setsid node scripts/qa/mobile-e2e/run-maestro.js "${ARGS[@]}" >"$LOG" 2>&1 </dev/null &
165
+ PID=$!
166
+
167
+ node -e "
168
+ const fs=require('fs');
169
+ fs.writeFileSync(process.argv[1], JSON.stringify({
170
+ id: process.argv[2], pid: Number(process.argv[3]), log: process.argv[4], events: process.argv[5],
171
+ cmd: 'run-maestro.js ' + process.argv.slice(6).join(' '),
172
+ sessionID: process.env.MOBILE_E2E_JOB_SESSION || null,
173
+ startedAt: new Date().toISOString()
174
+ }, null, 2));
175
+ " "$RECORD" "$JOB_ID" "$PID" "$LOG" "$EVENTS" "${ARGS[@]}"
176
+
177
+ echo "started run-maestro (pid $PID)"
178
+ echo "job: $JOB_ID"
179
+ echo "record: $RECORD"
180
+ echo "events: $EVENTS"
181
+ echo "log: $LOG"
182
+ echo "tail: tail -f $LOG"
183
+ echo "exit: wait $PID; or check \`kill -0 $PID\`"
184
+
185
+ if [ "$WAIT" != "1" ]; then
186
+ exit 0
187
+ fi
188
+
189
+ # --wait blocks THIS shell for the whole run. From an agent, prefer detached +
190
+ # the job-notify plugin (or the async `wait` tool): a blocking bash call leaves
191
+ # the tool "running" and the transcript blind for the entire matrix (F-296).
192
+ echo "run-detached: --wait blocks this shell until the run finishes." >&2
193
+ echo " In an agent session prefer detached + job-notify, or the async \`wait\` tool;" >&2
194
+ echo " use --wait only headless. Progress every ${MOBILE_E2E_WAIT_HEARTBEAT:-20}s below." >&2
195
+
196
+ # --wait: block here, streaming each new scenario line, then return the run's
197
+ # exit code. A zombie (exited, not yet reaped) must not read as "running".
198
+ is_running() {
199
+ [ -d "/proc/$PID" ] || return 1
200
+ local state
201
+ state="$(awk '{print $3}' "/proc/$PID/stat" 2>/dev/null || true)"
202
+ [ "$state" != "Z" ]
203
+ }
204
+ printed=0
205
+ emit_new() {
206
+ local total
207
+ total="$(wc -l <"$EVENTS" 2>/dev/null | tr -d ' ' || echo 0)"
208
+ [ -z "$total" ] && total=0
209
+ if [ "$total" -gt "$printed" ]; then
210
+ node scripts/qa/mobile-e2e/job-events.js --render "$EVENTS" --skip "$printed" || true
211
+ printed="$total"
212
+ fi
213
+ }
214
+ HB="${MOBILE_E2E_WAIT_HEARTBEAT:-20}"
215
+ while is_running; do
216
+ sleep "$HB"
217
+ emit_new
218
+ done
219
+ wait "$PID" && code=0 || code=$?
220
+ emit_new
221
+ node scripts/qa/mobile-e2e/job-events.js --summary "$EVENTS" || true
222
+ exit "$code"