opencode-agent-harness 0.0.0-stage → 0.2.0

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 (169) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/LICENSE +21 -0
  3. package/README.md +590 -2
  4. package/bin/agent-harness.js +3059 -0
  5. package/contrib/global-plugins/README.md +49 -0
  6. package/contrib/global-plugins/harness-freshness.ts +355 -0
  7. package/package.json +48 -4
  8. package/templates/.agents/AGENTS.md +204 -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 +1864 -0
  100. package/templates/scripts/board-service.sh +207 -0
  101. package/templates/scripts/checkpoint.js +290 -0
  102. package/templates/scripts/finalize.js +277 -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 +260 -0
  106. package/templates/scripts/qa/board-lib.js +96 -0
  107. package/templates/scripts/qa/board-sync.js +469 -0
  108. package/templates/scripts/qa/capacity-lib.js +916 -0
  109. package/templates/scripts/qa/check-backlog.js +427 -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 +284 -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 +183 -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/ci-triage.js +346 -0
  124. package/templates/scripts/qa/ci-usage-lib.js +185 -0
  125. package/templates/scripts/qa/ci-wait-lib.js +294 -0
  126. package/templates/scripts/qa/command-classifier.js +552 -0
  127. package/templates/scripts/qa/context-guard-core.js +1618 -0
  128. package/templates/scripts/qa/diff.js +174 -0
  129. package/templates/scripts/qa/feature-scaffold.js +224 -0
  130. package/templates/scripts/qa/fixtures/backlog.md +104 -0
  131. package/templates/scripts/qa/governance.js +174 -0
  132. package/templates/scripts/qa/hash-file.js +32 -0
  133. package/templates/scripts/qa/lease-lib.js +263 -0
  134. package/templates/scripts/qa/merge-backlog.js +324 -0
  135. package/templates/scripts/qa/mobile-e2e/analyze-timings.js +300 -0
  136. package/templates/scripts/qa/mobile-e2e/apk-cache.js +420 -0
  137. package/templates/scripts/qa/mobile-e2e/backend-build-cache.js +203 -0
  138. package/templates/scripts/qa/mobile-e2e/boot-emulator.sh +432 -0
  139. package/templates/scripts/qa/mobile-e2e/build-apk.sh +210 -0
  140. package/templates/scripts/qa/mobile-e2e/check-flows.js +453 -0
  141. package/templates/scripts/qa/mobile-e2e/config.yaml +18 -0
  142. package/templates/scripts/qa/mobile-e2e/device-down.sh +88 -0
  143. package/templates/scripts/qa/mobile-e2e/device-up.sh +258 -0
  144. package/templates/scripts/qa/mobile-e2e/ios-simulator-down.sh +72 -0
  145. package/templates/scripts/qa/mobile-e2e/ios-simulator-up.sh +153 -0
  146. package/templates/scripts/qa/mobile-e2e/job-events.js +181 -0
  147. package/templates/scripts/qa/mobile-e2e/prewarm.sh +56 -0
  148. package/templates/scripts/qa/mobile-e2e/run-detached.sh +222 -0
  149. package/templates/scripts/qa/mobile-e2e/run-maestro.js +1543 -0
  150. package/templates/scripts/qa/mobile-e2e/ui-dump.js +327 -0
  151. package/templates/scripts/qa/models.allowlist.txt +32 -0
  152. package/templates/scripts/qa/provider-stats.js +878 -0
  153. package/templates/scripts/qa/run-clean.js +83 -0
  154. package/templates/scripts/qa/run-gates.js +212 -0
  155. package/templates/scripts/qa/run-loop.js +220 -0
  156. package/templates/scripts/qa/session-recon.js +512 -0
  157. package/templates/scripts/qa/store-lib.js +324 -0
  158. package/templates/scripts/qa/tool-lib.js +134 -0
  159. package/templates/scripts/qa/where.js +214 -0
  160. package/templates/scripts/qa/wslconfig-merge.js +157 -0
  161. package/templates/scripts/serve.js +1328 -0
  162. package/templates/scripts/setup-nightly.sh +185 -0
  163. package/templates/scripts/setup-tmpfs.sh +374 -0
  164. package/templates/scripts/ship.js +586 -0
  165. package/templates/scripts/switch-reasoning-model.ps1 +101 -0
  166. package/templates/scripts/switch-reasoning-model.sh +104 -0
  167. package/templates/scripts/wait-for.js +342 -0
  168. package/templates/scripts/wsl-gpu.sh +180 -0
  169. package/templates/scripts/wsl-tune.sh +261 -0
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env bash
2
+ # POSIX twin of scripts/switch-reasoning-model.ps1 (WSL/Linux/macOS).
3
+ # Switches the reasoning-tier model: rewrites the `model` field in opencode.json
4
+ # and the `model:` frontmatter of the six reasoning-tier agents to one model.
5
+ #
6
+ # Usage: bash scripts/switch-reasoning-model.sh <alias|model-id>
7
+ # Aliases: go|go-flash|go-deepseek, go-pro, deepseek|flash|direct,
8
+ # deepseek-pro|pro, nvidia|kimi|kimi-k3, nemotron, openrouter|or|m3, or a
9
+ # bare model id (auto-prefixed with deepseek/ when it has no provider).
10
+ set -euo pipefail
11
+
12
+ TARGET="${1:-}"
13
+ if [ -z "$TARGET" ]; then
14
+ echo "usage: bash scripts/switch-reasoning-model.sh <alias|model-id>" >&2
15
+ exit 2
16
+ fi
17
+
18
+ root="$(cd "$(dirname "$0")/.." && pwd)"
19
+
20
+ target_lc="$(printf '%s' "$TARGET" | tr '[:upper:]' '[:lower:]')"
21
+ case "$target_lc" in
22
+ go|go-flash|go-deepseek) target_model="opencode-go/deepseek-v4.1-flash" ;;
23
+ go-pro) target_model="opencode-go/deepseek-v4-pro" ;;
24
+ deepseek|flash|deepseek-flash|direct) target_model="deepseek/deepseek-flash" ;;
25
+ deepseek-pro|deepseek-v4-pro|pro) target_model="deepseek/deepseek-v4-pro" ;;
26
+ nvidia|kimi|kimi-k3) target_model="nvidia/moonshotai/kimi-k3" ;;
27
+ nemotron|nemotron-ultra) target_model="nvidia/nemotron-3-ultra-550b-a55b:free" ;;
28
+ openrouter|or|minimax-m3|m3) target_model="openrouter/minimax/minimax-m3" ;;
29
+ *) target_model="$TARGET" ;;
30
+ esac
31
+ case "$target_model" in
32
+ */*) ;;
33
+ *) target_model="deepseek/$target_model" ;;
34
+ esac
35
+
36
+ # Every alias value is also a "find" value (anything the script can switch TO it
37
+ # can also switch FROM) — see the note in the PowerShell twin.
38
+ reasoning_models="
39
+ opencode-go/deepseek-v4.1-flash
40
+ opencode-go/deepseek-v4-pro
41
+ deepseek/deepseek-flash
42
+ deepseek/deepseek-v4-pro
43
+ nvidia/moonshotai/kimi-k3
44
+ nvidia/nemotron-3-ultra-550b-a55b:free
45
+ openrouter/minimax/minimax-m3
46
+ "
47
+
48
+ files="
49
+ opencode.json
50
+ .opencode/agents/planner.md
51
+ .opencode/agents/system-architect.md
52
+ .opencode/agents/security-engineer.md
53
+ .opencode/agents/diagnostics-expert.md
54
+ .opencode/agents/product-manager.md
55
+ .opencode/agents/qa-architect.md
56
+ "
57
+
58
+ esc_bre() { printf '%s' "$1" | sed -e 's/[][\\.^$*#]/\\&/g'; }
59
+ esc_repl() { printf '%s' "$1" | sed -e 's/[\\&#]/\\&/g'; }
60
+
61
+ changed=""
62
+ skipped=""
63
+ for rel in $files; do
64
+ path="$root/$rel"
65
+ if [ ! -f "$path" ]; then
66
+ printf 'warning: missing: %s\n' "$rel" >&2
67
+ continue
68
+ fi
69
+ raw="$(cat "$path"; printf x)"; raw="${raw%x}"
70
+ new="$raw"
71
+ find_hits=0
72
+ for m in $reasoning_models; do
73
+ [ "$m" = "$target_model" ] && continue
74
+ case "$new" in *"$m"*) find_hits=1 ;; esac
75
+ new="$(printf '%s' "$new" | sed "s#$(esc_bre "$m")#$(esc_repl "$target_model")#g"; printf x)"; new="${new%x}"
76
+ done
77
+ if [ "$new" != "$raw" ]; then
78
+ printf '%s' "$new" > "$path"
79
+ changed="$changed $rel"
80
+ elif [ "$find_hits" = 0 ]; then
81
+ case "$raw" in
82
+ *"$target_model"*) ;;
83
+ *)
84
+ current="$(printf '%s' "$raw" | grep -oE 'model:[[:space:]]*"?[A-Za-z0-9._:/-]+"?' | head -1 | sed -E 's/^model:[[:space:]]*"?//; s/"$//')"
85
+ skipped="${skipped}${rel} (current value \"${current}\" not in replace list; edit by hand or add an alias)\n"
86
+ ;;
87
+ esac
88
+ fi
89
+ done
90
+
91
+ printf 'Switched reasoning-tier model -> %s\n' "$target_model"
92
+ if [ -n "$changed" ]; then
93
+ printf 'Updated:\n'
94
+ for rel in $changed; do printf ' - %s\n' "$rel"; done
95
+ fi
96
+ if [ -n "$skipped" ]; then
97
+ printf 'Skipped (current value not in replace list):\n'
98
+ printf '%b' "$skipped"
99
+ printf 'Add an alias to scripts/switch-reasoning-model.sh (or edit the file by hand).\n'
100
+ fi
101
+ if [ -z "$changed" ] && [ -z "$skipped" ]; then
102
+ printf 'No files changed (already set).\n'
103
+ fi
104
+ printf 'Restart opencode for the change to take effect.\n'
@@ -0,0 +1,342 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ /*
4
+ * wait-for — block (bounded) for a job/CI/log/health to reach a terminal state,
5
+ * instead of the agent's `sleep N; grep log` (or `gh run watch`) polling loop.
6
+ *
7
+ * The 2026-09 LibraryOS recon found 98 `sleep` commands + 81 `gh` polls in
8
+ * 30 days, rising on the device-heavy days (09-30: 26 + 20). A blind `sleep`
9
+ * either wastes minutes (too long) or forces another poll (too short); a real
10
+ * blocking waiter returns the moment the condition is met and a terminal status
11
+ * when it is not. `run-detached.sh --wait` and the `job-notify` plugin already
12
+ * cover the mobile-E2E matrix; this covers the general cases — a recorded job,
13
+ * a CI run, a log line, or a URL/port.
14
+ *
15
+ * Modes (exactly one):
16
+ * --job <record.json> block until the job's events reach `matrix_done`
17
+ * or its pid dies (reuses the mobile-e2e job files)
18
+ * --events <events.jsonl> same, from an events file directly
19
+ * --ci [--commit <sha>] block until the CI run on <sha> (or HEAD) finishes
20
+ * --health <url> block until an HTTP request succeeds
21
+ * --port <n> block until a TCP port accepts a connection
22
+ * --file <log> --grep <re> block until <log> contains a line matching <re>
23
+ *
24
+ * Options: [--timeout <sec>] [--poll <sec>] [--root <dir>] [--quiet]
25
+ * Exit: 0 = condition met (job pass / CI green / ready), 1 = timeout/failure.
26
+ *
27
+ * Deterministic and dependency-free. The CI half reuses
28
+ * `scripts/qa/ci-wait-lib.js` — the same implementation `ship.js` uses — so a
29
+ * behavior change lands once.
30
+ */
31
+
32
+ const fs = require("fs");
33
+ const path = require("path");
34
+ const net = require("net");
35
+ const { spawnSync } = require("child_process");
36
+ const jobEvents = require("./qa/mobile-e2e/job-events.js");
37
+ const ciWait = require("./qa/ci-wait-lib.js");
38
+
39
+ const DEFAULT_TIMEOUT_S = 20 * 60;
40
+ const DEFAULT_POLL_S = 5;
41
+
42
+ function log(msg, quiet) {
43
+ if (!quiet) process.stderr.write(`[wait] ${msg}\n`);
44
+ }
45
+
46
+ function sleepSync(ms) {
47
+ if (!(ms > 0)) return;
48
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
49
+ }
50
+
51
+ function pidAlive(pid) {
52
+ const n = Number(pid);
53
+ if (!Number.isFinite(n) || n <= 0) return true; // unknown -> assume alive
54
+ try {
55
+ process.kill(n, 0);
56
+ return true;
57
+ } catch (err) {
58
+ return err && err.code === "EPERM";
59
+ }
60
+ }
61
+
62
+ /* Wait on a mobile-e2e-style events stream (reuses job-events.js). Terminal
63
+ * when a `matrix_done` event appears or the job's pid (from --job's record) is
64
+ * gone. Returns 0 on a clean pass (0 failed) or a recorded done-with-failures
65
+ * also returns 0 but prints the summary (the caller decides); a dead runner
66
+ * before `matrix_done` returns 1. */
67
+ function waitEvents(eventsFile, opts) {
68
+ const { timeoutMs, pollMs, record, quiet } = opts;
69
+ const stallWarnMs = Number(opts.stallWarnMs) || 0;
70
+ const maxStallMs = Number(opts.maxStallMs) || 0;
71
+ const deadline = Date.now() + timeoutMs;
72
+ let lastCount = 0;
73
+ let lastEventAt = Date.now();
74
+ let stallWarned = false;
75
+ for (;;) {
76
+ const events = jobEvents.readEvents(eventsFile);
77
+ if (events.length > lastCount) {
78
+ for (const e of events.slice(lastCount)) {
79
+ const line = jobEvents.progressLine(e);
80
+ if (line) log(line, quiet);
81
+ }
82
+ lastCount = events.length;
83
+ const last = events[events.length - 1];
84
+ lastEventAt = last && last.at && !Number.isNaN(Date.parse(last.at)) ? Date.parse(last.at) : Date.now();
85
+ stallWarned = false;
86
+ }
87
+ const summary = jobEvents.summarize(events);
88
+ // An operator stop writes `matrix_aborted` (run-detached.sh --stop): terminal.
89
+ if (summary.aborted) {
90
+ process.stdout.write(jobEvents.notificationText(summary, { eventsPath: eventsFile }) + "\n");
91
+ log("job was aborted by an operator", quiet);
92
+ return 1;
93
+ }
94
+ if (summary.done) {
95
+ process.stdout.write(jobEvents.notificationText(summary, { eventsPath: eventsFile }) + "\n");
96
+ return summary.failed === 0 ? 0 : 0; // done is terminal; failures are in the text
97
+ }
98
+ if (record && !pidAlive(record.pid)) {
99
+ process.stdout.write(
100
+ jobEvents.notificationText(summary, { eventsPath: eventsFile }) + "\nRunner exited without matrix_done.\n"
101
+ );
102
+ return 1;
103
+ }
104
+ // Liveness is event-driven: the runner emits a heartbeat while an invocation
105
+ // runs, so a quiet stream means the run really is stalled (or the machine is
106
+ // starved) — no longer a guess. `--max-stall` stops the wait on that; the
107
+ // warning alone is always printed past the warn threshold.
108
+ const stallMs = Date.now() - lastEventAt;
109
+ if (maxStallMs > 0 && stallMs >= maxStallMs) {
110
+ process.stdout.write(jobEvents.notificationText(summary, { eventsPath: eventsFile }) + "\n");
111
+ log(
112
+ `stalled: no job event for ${Math.round(stallMs / 1000)}s (--max-stall ${Math.round(
113
+ maxStallMs / 1000
114
+ )}s) — stopping the wait; the run may still be alive (check the log)`,
115
+ quiet
116
+ );
117
+ return 1;
118
+ }
119
+ if (!stallWarned && stallWarnMs > 0 && stallMs >= stallWarnMs) {
120
+ stallWarned = true;
121
+ log(
122
+ `no job event for ${Math.round(stallMs / 1000)}s — the run may be stalled. ` +
123
+ `Stop it deterministically: scripts/qa/mobile-e2e/run-detached.sh --stop <job-id>`,
124
+ quiet
125
+ );
126
+ }
127
+ if (Date.now() >= deadline) {
128
+ process.stdout.write(jobEvents.notificationText(summary, { eventsPath: eventsFile }) + "\n");
129
+ log(`timeout after ${Math.round(timeoutMs / 60000)}m`, quiet);
130
+ return 1;
131
+ }
132
+ sleepSync(pollMs);
133
+ }
134
+ }
135
+
136
+ function httpOk(url) {
137
+ // Node 18+ global fetch; a bounded request that never throws out.
138
+ try {
139
+ const ctl = new AbortController();
140
+ const t = setTimeout(() => ctl.abort(), 4000);
141
+ return fetch(url, { signal: ctl.signal })
142
+ .then((r) => {
143
+ clearTimeout(t);
144
+ return !!r && r.status < 500;
145
+ })
146
+ .catch(() => false);
147
+ } catch (_) {
148
+ return Promise.resolve(false);
149
+ }
150
+ }
151
+
152
+ function tcpOpen(port, host = "127.0.0.1") {
153
+ return new Promise((resolve) => {
154
+ const s = net.connect({ port: Number(port), host });
155
+ const done = (ok) => {
156
+ s.destroy();
157
+ resolve(ok);
158
+ };
159
+ s.setTimeout(3000);
160
+ s.once("connect", () => done(true));
161
+ s.once("timeout", () => done(false));
162
+ s.once("error", () => done(false));
163
+ });
164
+ }
165
+
166
+ async function pollUntil(check, opts, readyText, failText) {
167
+ const { timeoutMs, pollMs, quiet } = opts;
168
+ const deadline = Date.now() + timeoutMs;
169
+ let announced = false;
170
+ for (;;) {
171
+ if (await check()) {
172
+ process.stdout.write(`${readyText}\n`);
173
+ return 0;
174
+ }
175
+ if (Date.now() >= deadline) {
176
+ process.stderr.write(`${failText} (timeout ${Math.round(timeoutMs / 60000)}m)\n`);
177
+ return 1;
178
+ }
179
+ if (!announced) {
180
+ log("waiting...", quiet);
181
+ announced = true;
182
+ }
183
+ sleepSync(pollMs);
184
+ }
185
+ }
186
+
187
+ function fileMatches(file, re) {
188
+ try {
189
+ return re.test(fs.readFileSync(file, "utf8"));
190
+ } catch (_) {
191
+ return false;
192
+ }
193
+ }
194
+
195
+ function parseArgs(argv) {
196
+ const out = { quiet: false };
197
+ for (let i = 0; i < argv.length; i++) {
198
+ const a = argv[i];
199
+ if (a === "--job") out.job = argv[++i];
200
+ else if (a === "--events") out.events = argv[++i];
201
+ else if (a === "--ci") out.ci = true;
202
+ else if (a === "--commit") out.commit = argv[++i];
203
+ else if (a === "--workflow") out.workflow = argv[++i];
204
+ else if (a === "--health") out.health = argv[++i];
205
+ else if (a === "--port") out.port = argv[++i];
206
+ else if (a === "--file") out.file = argv[++i];
207
+ else if (a === "--grep") out.grep = argv[++i];
208
+ else if (a === "--timeout") out.timeout = argv[++i];
209
+ else if (a === "--poll") out.poll = argv[++i];
210
+ else if (a === "--max-stall") out.maxStall = argv[++i];
211
+ else if (a === "--stall-warn") out.stallWarn = argv[++i];
212
+ else if (a === "--root") out.root = argv[++i];
213
+ else if (a === "--quiet") out.quiet = true;
214
+ else if (a === "--help" || a === "-h") out.help = true;
215
+ else out._.push(a);
216
+ }
217
+ return out;
218
+ }
219
+
220
+ function usage() {
221
+ console.log(
222
+ "wait-for — block (bounded) for a job/CI/log/health to finish, instead of polling.\n\n" +
223
+ " node scripts/wait-for.js --job <record.json> # mobile-e2e/detached job\n" +
224
+ " node scripts/wait-for.js --events <events.jsonl> # job events directly\n" +
225
+ " node scripts/wait-for.js --ci [--commit <sha>] # CI run on a SHA\n" +
226
+ " node scripts/wait-for.js --health <url> # HTTP ready\n" +
227
+ " node scripts/wait-for.js --port <n> # TCP port ready\n" +
228
+ " node scripts/wait-for.js --file <log> --grep <re> # a log line appears\n\n" +
229
+ "Options: [--timeout <sec>] [--poll <sec>] [--max-stall <sec>] [--stall-warn <sec>] [--root <dir>] [--quiet]\n" +
230
+ " --max-stall <sec> stop the wait when the job emits no event for <sec> (0 = off)\n" +
231
+ " --stall-warn <sec> print a stall warning after <sec> of no events (default 120)\n" +
232
+ "Exit 0 when the condition is met, 1 on timeout / abort / stall."
233
+ );
234
+ }
235
+
236
+ async function main() {
237
+ const args = parseArgs(process.argv.slice(2));
238
+ if (args.help) return usage();
239
+ const root = path.resolve(args.root || process.cwd());
240
+ const timeoutMs = (Number(args.timeout) || DEFAULT_TIMEOUT_S) * 1000;
241
+ const pollMs = (Number(args.poll) || DEFAULT_POLL_S) * 1000;
242
+ const opts = {
243
+ timeoutMs,
244
+ pollMs,
245
+ quiet: args.quiet,
246
+ maxStallMs: (Number(args.maxStall) || 0) * 1000,
247
+ stallWarnMs: (Number(args.stallWarn) || 120) * 1000,
248
+ };
249
+
250
+ const modes = [args.job, args.events, args.ci, args.health, args.port, args.file].filter(Boolean);
251
+ if (modes.length !== 1) {
252
+ console.error("wait-for: specify exactly one of --job/--events/--ci/--health/--port/--file");
253
+ process.exitCode = 2;
254
+ return;
255
+ }
256
+
257
+ if (args.job) {
258
+ let record = null;
259
+ try {
260
+ record = JSON.parse(fs.readFileSync(args.job, "utf8"));
261
+ } catch (e) {
262
+ console.error(`wait-for: cannot read job record ${args.job}: ${e.message}`);
263
+ process.exitCode = 2;
264
+ return;
265
+ }
266
+ if (!record.events) {
267
+ console.error("wait-for: job record has no `events` path");
268
+ process.exitCode = 2;
269
+ return;
270
+ }
271
+ process.exitCode = waitEvents(record.events, { ...opts, record });
272
+ return;
273
+ }
274
+ if (args.events) {
275
+ process.exitCode = waitEvents(args.events, opts);
276
+ return;
277
+ }
278
+ if (args.ci) {
279
+ const sha = args.commit || spawnSync("git", ["-C", root, "rev-parse", "HEAD"], { encoding: "utf8" }).stdout.trim();
280
+ if (!sha) {
281
+ console.error("wait-for: could not resolve a commit SHA (pass --commit)");
282
+ process.exitCode = 2;
283
+ return;
284
+ }
285
+ const workflow = args.workflow || ciWait.ciWorkflow(root);
286
+ const cfg = ciWait.ciConfig(root, {
287
+ "ci-timeout": timeoutMs / 60000,
288
+ "ci-poll": pollMs / 1000,
289
+ });
290
+ process.exitCode = ciWait.waitCi(root, sha, { ...cfg, workflow, cwd: root });
291
+ return;
292
+ }
293
+ if (args.health) {
294
+ process.exitCode = await pollUntil(
295
+ () => httpOk(args.health),
296
+ opts,
297
+ `ready: ${args.health}`,
298
+ `not ready: ${args.health}`
299
+ );
300
+ return;
301
+ }
302
+ if (args.port) {
303
+ process.exitCode = await pollUntil(
304
+ () => tcpOpen(args.port),
305
+ opts,
306
+ `port open: ${args.port}`,
307
+ `port never opened: ${args.port}`
308
+ );
309
+ return;
310
+ }
311
+ if (args.file) {
312
+ if (!args.grep) {
313
+ console.error("wait-for: --file requires --grep <regex>");
314
+ process.exitCode = 2;
315
+ return;
316
+ }
317
+ let re;
318
+ try {
319
+ re = new RegExp(args.grep);
320
+ } catch (e) {
321
+ console.error(`wait-for: bad --grep regex: ${e.message}`);
322
+ process.exitCode = 2;
323
+ return;
324
+ }
325
+ process.exitCode = await pollUntil(
326
+ () => fileMatches(args.file, re),
327
+ opts,
328
+ `matched ${args.grep} in ${args.file}`,
329
+ `no match for ${args.grep} in ${args.file}`
330
+ );
331
+ return;
332
+ }
333
+ }
334
+
335
+ if (require.main === module) {
336
+ main().catch((e) => {
337
+ console.error("wait-for: " + (e && e.message ? e.message : e));
338
+ process.exitCode = 1;
339
+ });
340
+ }
341
+
342
+ module.exports = { parseArgs, waitEvents, httpOk, tcpOpen, fileMatches, pidAlive, DEFAULT_TIMEOUT_S, DEFAULT_POLL_S };
@@ -0,0 +1,180 @@
1
+ #!/usr/bin/env bash
2
+ # wsl-gpu.sh — enable/verify real GPU acceleration for WSL2 (OpenGL over D3D12).
3
+ #
4
+ # Why: the Android device tier is the slowest part of the mobile E2E run, and on
5
+ # a software renderer it is CPU-bound (~2.5 s per Maestro command; F-291/F-296).
6
+ # WSL2 can pass the Windows GPU through `/dev/dxg`, but two commonly-missing
7
+ # pieces are needed and this script fixes/diagnoses both:
8
+ #
9
+ # 1. the `vgem` DRM module must be loaded. Modern WSL kernels (>= 2.5) ship it
10
+ # as a loadable module and do NOT auto-load it, so `/dev/dri` is absent and
11
+ # every GL app — including the emulator's `-gpu host` — silently falls back
12
+ # to the llvmpipe software renderer (WSL issue #12702, #11837).
13
+ # 2. Mesa must select the `d3d12` gallium driver. It does NOT by default, so
14
+ # even with `/dev/dri` present, `glxinfo` still reports llvmpipe. Forcing
15
+ # `GALLIUM_DRIVER=d3d12` (with `/usr/lib/wsl/lib` on the loader path) makes
16
+ # `glxinfo -B` report `D3D12 (<Windows GPU>)` and `Accelerated: yes`.
17
+ #
18
+ # `status` shows exactly which of those two is missing; `apply` loads + persists
19
+ # vgem and verifies d3d12; `env` prints the exports to use for a GL process.
20
+ # `boot-emulator.sh` probes the same path and uses it automatically.
21
+ #
22
+ # Usage:
23
+ # bash scripts/wsl-gpu.sh status # diagnose (default)
24
+ # bash scripts/wsl-gpu.sh apply # load vgem (persist) + verify d3d12 (auto-sudo)
25
+ # bash scripts/wsl-gpu.sh env # print the GL env exports (eval-able)
26
+ #
27
+ # No-op outside WSL (no /dev/dxg; override with AGENT_WSL_GPU_FORCE=1).
28
+ # Idempotent. Env overrides:
29
+ # AGENT_WSL_GPU_DRIVER (d3d12), AGENT_WSL_GPU_MODULE (vgem),
30
+ # AGENT_WSL_GPU_MODULES_FILE (/etc/modules-load.d/vgem.conf),
31
+ # AGENT_WSL_GPU_GLXINFO (glxinfo), AGENT_WSL_GPU_FORCE
32
+ set -euo pipefail
33
+
34
+ MODULE="${AGENT_WSL_GPU_MODULE:-vgem}"
35
+ DRIVER="${AGENT_WSL_GPU_DRIVER:-d3d12}"
36
+ MODULES_FILE="${AGENT_WSL_GPU_MODULES_FILE:-/etc/modules-load.d/${MODULE}.conf}"
37
+ GLXINFO="${AGENT_WSL_GPU_GLXINFO:-glxinfo}"
38
+
39
+ log() { printf '%s\n' "$*"; }
40
+ warn() { printf '%s\n' "$*" >&2; }
41
+
42
+ # WSL GPU passthrough exposes the Windows adapter at /dev/dxg. Absent => not WSL
43
+ # or no GPU-PV, and everything here is a no-op.
44
+ has_dxg() {
45
+ [ "${AGENT_WSL_GPU_FORCE:-0}" = "1" ] && return 0
46
+ [ -e /dev/dxg ]
47
+ }
48
+
49
+ module_loaded() {
50
+ if command -v lsmod >/dev/null 2>&1; then
51
+ lsmod | awk '{print $1}' | grep -qx "$MODULE" && return 0
52
+ fi
53
+ ls /dev/dri/renderD* >/dev/null 2>&1
54
+ }
55
+
56
+ # The usable GL renderer for the D3D12 path (empty if glxinfo is unavailable or
57
+ # the probe fails). Only meaningful once vgem is loaded.
58
+ d3d12_renderer() {
59
+ command -v "$GLXINFO" >/dev/null 2>&1 || return 0
60
+ MESA_LOADER_DRIVER_OVERRIDE="$DRIVER" GALLIUM_DRIVER="$DRIVER" \
61
+ "$GLXINFO" -B 2>/dev/null \
62
+ | grep -i 'OpenGL renderer string' | head -1 \
63
+ | sed 's/.*: //' || true
64
+ }
65
+
66
+ d3d12_accelerated() {
67
+ local dev
68
+ dev="$(d3d12_renderer)"
69
+ [ -n "$dev" ] || return 1
70
+ case "$(printf '%s' "$dev" | tr 'A-Z' 'a-z')" in
71
+ *llvmpipe*|*softpipe*|*swrast*|*"software rasterizer"*) return 1 ;;
72
+ esac
73
+ return 0
74
+ }
75
+
76
+ do_status() {
77
+ log "wsl-gpu status"
78
+ if ! has_dxg; then
79
+ log " not WSL GPU passthrough (/dev/dxg absent) — nothing to do here"
80
+ return 0
81
+ fi
82
+ log " /dev/dxg: present (Windows GPU exposed)"
83
+ if module_loaded; then
84
+ log " $MODULE module: loaded (/dev/dri render node present)"
85
+ else
86
+ log " $MODULE module: NOT loaded — /dev/dri is missing; the emulator will use llvmpipe"
87
+ log " fix: bash scripts/wsl-gpu.sh apply (or: sudo modprobe $MODULE)"
88
+ fi
89
+ if ! command -v "$GLXINFO" >/dev/null 2>&1; then
90
+ log " glxinfo: not installed (apt install mesa-utils) — cannot verify acceleration"
91
+ return 0
92
+ fi
93
+ local dev
94
+ dev="$(d3d12_renderer)"
95
+ if d3d12_accelerated; then
96
+ log " d3d12 renderer: $dev (Accelerated: yes)"
97
+ log ""
98
+ log " Apply for a GL process: $(shell_env)"
99
+ else
100
+ log " d3d12 renderer: ${dev:-<probe failed>} — not accelerated"
101
+ [ "$(module_loaded && echo 1 || echo 0)" = "1" ] \
102
+ && log " $MODULE is loaded but Mesa did not pick d3d12" \
103
+ || log " load $MODULE first: bash scripts/wsl-gpu.sh apply"
104
+ fi
105
+ }
106
+
107
+ shell_env() {
108
+ printf 'export GALLIUM_DRIVER=%s MESA_LOADER_DRIVER_OVERRIDE=%s\n' "$DRIVER" "$DRIVER"
109
+ }
110
+
111
+ do_env() {
112
+ if has_dxg && d3d12_accelerated; then
113
+ shell_env
114
+ fi
115
+ }
116
+
117
+ do_apply() {
118
+ if ! has_dxg; then
119
+ log "wsl-gpu: not WSL GPU passthrough — no-op."
120
+ return 0
121
+ fi
122
+ local needs_root=0
123
+ module_loaded || needs_root=1
124
+ [ -w "$(dirname "$MODULES_FILE")" ] || needs_root=1
125
+ if [ "$needs_root" = "1" ] && [ "$(id -u)" -ne 0 ]; then
126
+ if command -v sudo >/dev/null 2>&1 && sudo -n true 2>/dev/null; then
127
+ exec sudo -E bash "$0" apply "$@"
128
+ fi
129
+ warn "wsl-gpu: loading $MODULE and writing $MODULES_FILE need root — re-run: sudo bash scripts/wsl-gpu.sh apply"
130
+ return 1
131
+ fi
132
+
133
+ if module_loaded; then
134
+ log "wsl-gpu: $MODULE already loaded"
135
+ else
136
+ if command -v modprobe >/dev/null 2>&1; then
137
+ modprobe "$MODULE" && log "wsl-gpu: loaded $MODULE" || warn "wsl-gpu: modprobe $MODULE failed"
138
+ else
139
+ warn "wsl-gpu: modprobe not found — cannot load $MODULE"
140
+ fi
141
+ fi
142
+
143
+ # Persist across WSL restarts. modules-load.d is the modern, supported path.
144
+ if [ ! -f "$MODULES_FILE" ] || ! grep -qx "$MODULE" "$MODULES_FILE" 2>/dev/null; then
145
+ mkdir -p "$(dirname "$MODULES_FILE")"
146
+ printf '%s\n' "$MODULE" > "$MODULES_FILE"
147
+ log "wsl-gpu: wrote $MODULES_FILE (loads $MODULE on boot)"
148
+ else
149
+ log "wsl-gpu: $MODULES_FILE already lists $MODULE"
150
+ fi
151
+
152
+ log ""
153
+ if d3d12_accelerated; then
154
+ local dev; dev="$(d3d12_renderer)"
155
+ log "wsl-gpu: GPU acceleration is ACTIVE — $dev"
156
+ log " For a GL process, export: $(shell_env)"
157
+ log " (boot-emulator.sh probes this automatically and uses -gpu host.)"
158
+ else
159
+ warn "wsl-gpu: $MODULE loaded but d3d12 acceleration not detected."
160
+ warn " Ensure the Windows GPU driver supports WSL (Intel/AMD/NVIDIA WSL driver),"
161
+ warn " then 'wsl --shutdown' from Windows PowerShell and retry."
162
+ fi
163
+ }
164
+
165
+ main() {
166
+ local sub="status"
167
+ for a in "$@"; do
168
+ case "$a" in
169
+ status|apply|env) sub="$a" ;;
170
+ *) warn "wsl-gpu: unknown argument: $a"; exit 2 ;;
171
+ esac
172
+ done
173
+ case "$sub" in
174
+ status) do_status ;;
175
+ apply) do_apply "$@" ;;
176
+ env) do_env ;;
177
+ esac
178
+ }
179
+
180
+ main "$@"