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.
- package/CHANGELOG.md +217 -0
- package/LICENSE +21 -0
- package/README.md +636 -2
- package/bin/agent-harness.js +3182 -0
- package/contrib/global-plugins/README.md +49 -0
- package/contrib/global-plugins/harness-freshness.ts +355 -0
- package/package.json +86 -4
- package/templates/.agents/AGENTS.md +148 -0
- package/templates/.agents/features/INDEX.md +10 -0
- package/templates/.agents/handoffs/README.md +87 -0
- package/templates/.agents/memory/dev-loop.md +59 -0
- package/templates/.agents/memory/domain-map.md +29 -0
- package/templates/.agents/memory/environments.md +35 -0
- package/templates/.agents/memory/flow-map.md +23 -0
- package/templates/.agents/memory/history.md +7 -0
- package/templates/.agents/memory/locations.md +14 -0
- package/templates/.agents/memory/model-routing.md +261 -0
- package/templates/.agents/memory/orchestration.md +183 -0
- package/templates/.agents/memory/owned-files.md +19 -0
- package/templates/.agents/memory/product-principles.md +37 -0
- package/templates/.agents/memory/quality-parameters.md +37 -0
- package/templates/.agents/memory/stack-versions.md +35 -0
- package/templates/.agents/memory/test-design-parameters.md +36 -0
- package/templates/.agents/nightly.json +17 -0
- package/templates/.agents/qa-plans/README.md +70 -0
- package/templates/.agents/qa-runs/README.md +21 -0
- package/templates/.agents/reviews/README.md +47 -0
- package/templates/.agents/rules/00-operating.md +199 -0
- package/templates/.agents/skills/data-engineer/SKILL.md +31 -0
- package/templates/.agents/skills/devops-engineer/SKILL.md +50 -0
- package/templates/.agents/skills/diagnostics-expert/SKILL.md +44 -0
- package/templates/.agents/skills/docs/SKILL.md +140 -0
- package/templates/.agents/skills/features/SKILL.md +187 -0
- package/templates/.agents/skills/frontend-engineer/SKILL.md +81 -0
- package/templates/.agents/skills/handoff/SKILL.md +85 -0
- package/templates/.agents/skills/mobile-engineer/SKILL.md +101 -0
- package/templates/.agents/skills/planner/SKILL.md +112 -0
- package/templates/.agents/skills/product-manager/SKILL.md +115 -0
- package/templates/.agents/skills/qa-architect/SKILL.md +224 -0
- package/templates/.agents/skills/qa-runner/SKILL.md +64 -0
- package/templates/.agents/skills/security-engineer/SKILL.md +53 -0
- package/templates/.agents/skills/system-architect/SKILL.md +42 -0
- package/templates/.agents/skills/ui-designer/SKILL.md +71 -0
- package/templates/.github/workflows/ci.yml +148 -0
- package/templates/.github/workflows/deploy-uat.yml +95 -0
- package/templates/.github/workflows/nightly.yml +83 -0
- package/templates/.github/workflows/uat-health.yml +41 -0
- package/templates/.husky/pre-push +23 -0
- package/templates/.ignore +28 -0
- package/templates/.opencode/agents/data-engineer.md +37 -0
- package/templates/.opencode/agents/devops-engineer.md +37 -0
- package/templates/.opencode/agents/diagnostics-expert.md +37 -0
- package/templates/.opencode/agents/docs.md +37 -0
- package/templates/.opencode/agents/features.md +21 -0
- package/templates/.opencode/agents/frontend-engineer.md +37 -0
- package/templates/.opencode/agents/handoff.md +21 -0
- package/templates/.opencode/agents/mobile-engineer.md +37 -0
- package/templates/.opencode/agents/planner.md +34 -0
- package/templates/.opencode/agents/product-manager.md +29 -0
- package/templates/.opencode/agents/qa-architect.md +27 -0
- package/templates/.opencode/agents/qa-runner.md +17 -0
- package/templates/.opencode/agents/security-engineer.md +37 -0
- package/templates/.opencode/agents/system-architect.md +37 -0
- package/templates/.opencode/agents/ui-designer.md +37 -0
- package/templates/.opencode/commands/board.md +15 -0
- package/templates/.opencode/commands/checkpoint.md +25 -0
- package/templates/.opencode/commands/feature.md +56 -0
- package/templates/.opencode/commands/finish.md +36 -0
- package/templates/.opencode/commands/next.md +49 -0
- package/templates/.opencode/commands/report.md +55 -0
- package/templates/.opencode/commands/resume.md +39 -0
- package/templates/.opencode/commands/switch-model.md +34 -0
- package/templates/.opencode/commands/tidy.md +49 -0
- package/templates/.opencode/plugins/backlog-notify.ts +57 -0
- package/templates/.opencode/plugins/backlog.ts +272 -0
- package/templates/.opencode/plugins/checkpoint.ts +160 -0
- package/templates/.opencode/plugins/context-guard.ts +1164 -0
- package/templates/.opencode/plugins/diff.ts +85 -0
- package/templates/.opencode/plugins/gates.ts +71 -0
- package/templates/.opencode/plugins/job-notify.ts +241 -0
- package/templates/.opencode/plugins/orient.ts +67 -0
- package/templates/.opencode/plugins/session.ts +89 -0
- package/templates/.opencode/plugins/wait.ts +101 -0
- package/templates/.opencode/plugins/where.ts +70 -0
- package/templates/.opencode/skills/bounded-explore/SKILL.md +38 -0
- package/templates/.opencode/skills/dev-loop/SKILL.md +87 -0
- package/templates/.vercelignore +16 -0
- package/templates/AGENTS.md +27 -0
- package/templates/BACKLOG.md +95 -0
- package/templates/docs/ci-cd-pipeline.md +81 -0
- package/templates/docs/gates.md +128 -0
- package/templates/docs/mcp-integrations.md +97 -0
- package/templates/docs/mobile-testing.md +555 -0
- package/templates/docs/nightly-testing.md +106 -0
- package/templates/docs/wsl-optimizations.md +290 -0
- package/templates/opencode.json +188 -0
- package/templates/scripts/agent-report.js +541 -0
- package/templates/scripts/agent-search.sh +117 -0
- package/templates/scripts/backlog.js +1924 -0
- package/templates/scripts/board-service.sh +214 -0
- package/templates/scripts/checkpoint.js +290 -0
- package/templates/scripts/finalize.js +416 -0
- package/templates/scripts/nightly.js +913 -0
- package/templates/scripts/orient.js +514 -0
- package/templates/scripts/qa/board-client.js +323 -0
- package/templates/scripts/qa/board-lib.js +96 -0
- package/templates/scripts/qa/board-sync.js +499 -0
- package/templates/scripts/qa/capacity-lib.js +916 -0
- package/templates/scripts/qa/check-backlog.js +434 -0
- package/templates/scripts/qa/check-capacity.js +85 -0
- package/templates/scripts/qa/check-ci-policy.js +164 -0
- package/templates/scripts/qa/check-concurrency.js +344 -0
- package/templates/scripts/qa/check-dispatch-config.js +296 -0
- package/templates/scripts/qa/check-docs.js +288 -0
- package/templates/scripts/qa/check-integrate.js +98 -0
- package/templates/scripts/qa/check-linger.js +160 -0
- package/templates/scripts/qa/check-prefix-stability.js +187 -0
- package/templates/scripts/qa/check-product.js +177 -0
- package/templates/scripts/qa/check-qa-plan.js +378 -0
- package/templates/scripts/qa/check-qa-scripts.js +85 -0
- package/templates/scripts/qa/check-quality.js +249 -0
- package/templates/scripts/qa/check-test-wiring.js +253 -0
- package/templates/scripts/qa/check-worktrees.js +93 -0
- package/templates/scripts/qa/ci-triage.js +346 -0
- package/templates/scripts/qa/ci-usage-lib.js +185 -0
- package/templates/scripts/qa/ci-wait-lib.js +294 -0
- package/templates/scripts/qa/command-classifier.js +552 -0
- package/templates/scripts/qa/context-guard-core.js +1618 -0
- package/templates/scripts/qa/diff.js +174 -0
- package/templates/scripts/qa/feature-scaffold.js +224 -0
- package/templates/scripts/qa/fixtures/backlog.md +104 -0
- package/templates/scripts/qa/governance.js +174 -0
- package/templates/scripts/qa/hash-file.js +32 -0
- package/templates/scripts/qa/lease-lib.js +263 -0
- package/templates/scripts/qa/merge-backlog.js +324 -0
- package/templates/scripts/qa/mobile-e2e/analyze-timings.js +300 -0
- package/templates/scripts/qa/mobile-e2e/apk-cache.js +420 -0
- package/templates/scripts/qa/mobile-e2e/backend-build-cache.js +203 -0
- package/templates/scripts/qa/mobile-e2e/boot-emulator.sh +432 -0
- package/templates/scripts/qa/mobile-e2e/build-apk.sh +210 -0
- package/templates/scripts/qa/mobile-e2e/check-flows.js +568 -0
- package/templates/scripts/qa/mobile-e2e/config.yaml +18 -0
- package/templates/scripts/qa/mobile-e2e/device-down.sh +88 -0
- package/templates/scripts/qa/mobile-e2e/device-up.sh +258 -0
- package/templates/scripts/qa/mobile-e2e/ios-simulator-down.sh +72 -0
- package/templates/scripts/qa/mobile-e2e/ios-simulator-up.sh +153 -0
- package/templates/scripts/qa/mobile-e2e/job-events.js +181 -0
- package/templates/scripts/qa/mobile-e2e/prewarm.sh +56 -0
- package/templates/scripts/qa/mobile-e2e/run-detached.sh +222 -0
- package/templates/scripts/qa/mobile-e2e/run-maestro.js +1543 -0
- package/templates/scripts/qa/mobile-e2e/ui-dump.js +327 -0
- package/templates/scripts/qa/models.allowlist.txt +32 -0
- package/templates/scripts/qa/provider-stats.js +878 -0
- package/templates/scripts/qa/run-clean.js +83 -0
- package/templates/scripts/qa/run-gates.js +213 -0
- package/templates/scripts/qa/run-loop.js +220 -0
- package/templates/scripts/qa/session-recon.js +512 -0
- package/templates/scripts/qa/store-lib.js +324 -0
- package/templates/scripts/qa/tool-lib.js +134 -0
- package/templates/scripts/qa/where.js +214 -0
- package/templates/scripts/qa/worktree-hygiene.js +217 -0
- package/templates/scripts/qa/wslconfig-merge.js +157 -0
- package/templates/scripts/serve.js +1328 -0
- package/templates/scripts/setup-nightly.sh +185 -0
- package/templates/scripts/setup-tmpfs.sh +374 -0
- package/templates/scripts/ship.js +586 -0
- package/templates/scripts/switch-reasoning-model.ps1 +101 -0
- package/templates/scripts/switch-reasoning-model.sh +104 -0
- package/templates/scripts/wait-for.js +342 -0
- package/templates/scripts/wsl-gpu.sh +180 -0
- package/templates/scripts/wsl-tune.sh +319 -0
|
@@ -0,0 +1,1328 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
/*
|
|
4
|
+
* serve.js — start/stop a long-running dev server WITHOUT freezing the agent shell.
|
|
5
|
+
*
|
|
6
|
+
* Why: the agent tool on Windows waits for a launched process tree to release the
|
|
7
|
+
* tool's console/output handles. A foreground server — or a Node `spawn(..., {
|
|
8
|
+
* detached: true, shell: true })`, which only sets CREATE_NEW_PROCESS_GROUP, not
|
|
9
|
+
* DETACHED_PROCESS/CREATE_NEW_CONSOLE — keeps the whole `cmd → dev server` tree
|
|
10
|
+
* attached to that console, so the tool call stays "running" for the server's
|
|
11
|
+
* whole lifetime and only a manual interrupt ends it. The detach that works on
|
|
12
|
+
* this stack is to give the server its OWN hidden console.
|
|
13
|
+
*
|
|
14
|
+
* How (win32): serve.js writes a small `<name>.cmd` launcher (it runs the dev
|
|
15
|
+
* command and redirects stdout/stderr to the log files), then starts it via
|
|
16
|
+
* PowerShell `Start-Process -WindowStyle Hidden` (CREATE_NEW_CONSOLE — the server
|
|
17
|
+
* gets a hidden console of its own and cannot hold this tool call's console).
|
|
18
|
+
* The real PID is captured from `-PassThru` and recorded; the process env + cwd
|
|
19
|
+
* are inherited, so `next dev`/`npm` resolve exactly as they do in the agent
|
|
20
|
+
* shell. Non-win32 keeps the plain detached `spawn` (setsid), which is sufficient.
|
|
21
|
+
*
|
|
22
|
+
* serve.js then polls --url (HTTP GET, any response) or --port (TCP connect)
|
|
23
|
+
* until ready or --timeout (default 60000ms), printing a heartbeat every few
|
|
24
|
+
* seconds so the tool shows live output instead of looking frozen. It prints the
|
|
25
|
+
* PID + log path and exits 0; exits 1 (after printing the log tail) if the
|
|
26
|
+
* server never became ready.
|
|
27
|
+
*
|
|
28
|
+
* Usage:
|
|
29
|
+
* node scripts/serve.js start --cmd "<dev command>" [--url <health url>] [--port <n>] [--name <label>] [--group <tier>] [--timeout <ms>] [--heavy] [--force] [--no-cap] [--mem-max 4G] [--cpu-max 250%]
|
|
30
|
+
* node scripts/serve.js run --cmd "<one-shot command>" [--name <label>] [--group <tier>] [--force] [--no-cap] [--mem-max 4G] [--cpu-max 250%]
|
|
31
|
+
* node scripts/serve.js stop [--name <label>] [--port <n>] [--session <ses_...>]
|
|
32
|
+
* node scripts/serve.js stop-all
|
|
33
|
+
* node scripts/serve.js list
|
|
34
|
+
* node scripts/serve.js prune [--worktrees]
|
|
35
|
+
* node scripts/serve.js reap [--json] [--quiet]
|
|
36
|
+
*
|
|
37
|
+
* - start: runs <cmd> detached (own hidden console on win32); polls --url/--port
|
|
38
|
+
* until ready or --timeout. Exits 0 on ready; exits 1 (after printing the log
|
|
39
|
+
* tail) on timeout. Always prints a "launching" line first + heartbeats while
|
|
40
|
+
* waiting, so an in-progress start is never silent.
|
|
41
|
+
* - A heavy command (emulator, Metro/Next dev, live tier) is capped BY DEFAULT:
|
|
42
|
+
* it runs as a transient `systemd-run --user` unit (under `user.slice`) with
|
|
43
|
+
* MemoryMax from the envelope (`heavy_mem_max_mb`, default 5 GB — above the
|
|
44
|
+
* observed peak, so normal E2E is never throttled), so a runaway is
|
|
45
|
+
* throttled/OOM-killed *inside its cgroup* instead of taking the whole WSL VM
|
|
46
|
+
* into swap-thrash (the 2026-09-29 freeze). `--no-cap` opts out, `--mem-max`
|
|
47
|
+
* overrides. Graceful no-op where systemd is absent; `stop` tears the unit down.
|
|
48
|
+
* - --mem-max / --cpu-max (systemd user bus available): MemoryMax/CPUQuota on
|
|
49
|
+
* that transient unit. Passes the caller's cwd + env through.
|
|
50
|
+
* - stop: kills the recorded PID and its process tree, removes the PID file.
|
|
51
|
+
* - prune: removes `.serve/*.pid` records whose process is gone (`--worktrees`
|
|
52
|
+
* also clears sibling agent worktrees), so a stale record cannot misreport a
|
|
53
|
+
* live heavy tier. Live servers are never touched.
|
|
54
|
+
* - reap: auto-close a serve "after use" — STOP one whose owning session lease is
|
|
55
|
+
* gone/stale (or an untagged one older than `idle_reap_minutes` with no recent
|
|
56
|
+
* log activity), and reconcile an untracked `agent-serve-*` unit left by a
|
|
57
|
+
* killed session. A live owner's serve is never touched. Runs automatically at
|
|
58
|
+
* `start` and when the context-guard plugin sees `session.deleted`, so a
|
|
59
|
+
* dev server/emulator is not kept alive and orphaned.
|
|
60
|
+
* - PID files + logs + the generated launcher live in <project>/.serve/
|
|
61
|
+
* (excluded clone-locally by the harness).
|
|
62
|
+
* - A child's stdout is buffered while it runs; the log flushes on exit, so a
|
|
63
|
+
* startup crash is fully visible in the log after the fact.
|
|
64
|
+
*
|
|
65
|
+
* Always pair a start with a stop once the tier that needed the server is done.
|
|
66
|
+
*/
|
|
67
|
+
const fs = require("fs");
|
|
68
|
+
const path = require("path");
|
|
69
|
+
const http = require("http");
|
|
70
|
+
const https = require("https");
|
|
71
|
+
const net = require("net");
|
|
72
|
+
const { spawn, spawnSync } = require("child_process");
|
|
73
|
+
const capacity = require("./qa/capacity-lib.js");
|
|
74
|
+
const leaseLib = require("./qa/lease-lib.js");
|
|
75
|
+
|
|
76
|
+
const SERVE_DIR = path.join(process.cwd(), ".serve");
|
|
77
|
+
const READY_INTERVAL = 500;
|
|
78
|
+
const HEARTBEAT_INTERVAL = 5000;
|
|
79
|
+
const DEFAULT_TIMEOUT = 60000;
|
|
80
|
+
/* The opencode session id env var handed to tools that invoke scripts (the
|
|
81
|
+
* backlog tools set it). serve.js records it as the serve's owner so a serve can
|
|
82
|
+
* be reaped when the session that started it is gone. */
|
|
83
|
+
const SESSION_ENV = "HARNESS_SESSION_ID";
|
|
84
|
+
|
|
85
|
+
function powershellExe() {
|
|
86
|
+
const known = process.env.SystemRoot
|
|
87
|
+
? path.join(process.env.SystemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe")
|
|
88
|
+
: "";
|
|
89
|
+
return known && fs.existsSync(known) ? known : "powershell.exe";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function parseArgs(argv) {
|
|
93
|
+
const flags = { _: [] };
|
|
94
|
+
for (let i = 0; i < argv.length; i++) {
|
|
95
|
+
const a = argv[i];
|
|
96
|
+
if (
|
|
97
|
+
a === "--cmd" ||
|
|
98
|
+
a === "--url" ||
|
|
99
|
+
a === "--port" ||
|
|
100
|
+
a === "--name" ||
|
|
101
|
+
a === "--group" ||
|
|
102
|
+
a === "--timeout" ||
|
|
103
|
+
a === "--mem-max" ||
|
|
104
|
+
a === "--cpu-max" ||
|
|
105
|
+
a === "--session"
|
|
106
|
+
) {
|
|
107
|
+
flags[a] = argv[++i];
|
|
108
|
+
} else if (
|
|
109
|
+
a === "--heavy" ||
|
|
110
|
+
a === "--force" ||
|
|
111
|
+
a === "--no-cap" ||
|
|
112
|
+
a === "--worktrees" ||
|
|
113
|
+
a === "--json" ||
|
|
114
|
+
a === "--quiet"
|
|
115
|
+
) {
|
|
116
|
+
flags[a] = true;
|
|
117
|
+
} else {
|
|
118
|
+
flags._.push(a);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return flags;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function deriveName(flags) {
|
|
125
|
+
if (flags["--name"]) return flags["--name"];
|
|
126
|
+
if (flags["--port"]) return "port-" + flags["--port"];
|
|
127
|
+
return "default";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function pidFile(name, dir = SERVE_DIR) {
|
|
131
|
+
return path.join(dir, name + ".pid");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function logPath(name, stream, dir = SERVE_DIR) {
|
|
135
|
+
return path.join(dir, name + "." + stream + ".log");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function sleep(ms) {
|
|
139
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function checkUrl(url) {
|
|
143
|
+
const lib = String(url).startsWith("https://") ? https : http;
|
|
144
|
+
return new Promise((resolve) => {
|
|
145
|
+
const req = lib.get(url, (res) => {
|
|
146
|
+
res.resume();
|
|
147
|
+
resolve(true);
|
|
148
|
+
});
|
|
149
|
+
req.on("error", () => resolve(false));
|
|
150
|
+
req.setTimeout(2000, () => {
|
|
151
|
+
req.destroy();
|
|
152
|
+
resolve(false);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function checkPort(port) {
|
|
158
|
+
return new Promise((resolve) => {
|
|
159
|
+
const s = net.connect({ host: "127.0.0.1", port: Number(port) }, () => {
|
|
160
|
+
s.destroy();
|
|
161
|
+
resolve(true);
|
|
162
|
+
});
|
|
163
|
+
s.on("error", () => resolve(false));
|
|
164
|
+
s.setTimeout(2000, () => {
|
|
165
|
+
s.destroy();
|
|
166
|
+
resolve(false);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function killTree(pid) {
|
|
172
|
+
if (process.platform === "win32") {
|
|
173
|
+
const r = spawnSync("taskkill", ["/PID", String(pid), "/T", "/F"], { encoding: "utf8" });
|
|
174
|
+
return r.status === 0;
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
process.kill(-pid, "SIGTERM");
|
|
178
|
+
return true;
|
|
179
|
+
} catch (_) {
|
|
180
|
+
try {
|
|
181
|
+
process.kill(pid, "SIGTERM");
|
|
182
|
+
return true;
|
|
183
|
+
} catch (_) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/* Bounded synchronous pause (ms) — used between SIGTERM and SIGKILL. */
|
|
190
|
+
function sleepSync(ms) {
|
|
191
|
+
try {
|
|
192
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
193
|
+
} catch (_) {
|
|
194
|
+
const end = Date.now() + ms;
|
|
195
|
+
while (Date.now() < end) {
|
|
196
|
+
/* fallback busy-wait */
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/* Tear a process tree down and VERIFY it is gone: SIGTERM the group, wait, then
|
|
202
|
+
* SIGKILL, then confirm the pid no longer exists. Returns true only when the
|
|
203
|
+
* process is actually gone, so a caller never records a false teardown. Guards
|
|
204
|
+
* pid<=1 so it can never issue `kill(-1)` (which signals every process). */
|
|
205
|
+
function terminateTree(pid) {
|
|
206
|
+
const n = Number(pid);
|
|
207
|
+
if (!Number.isFinite(n) || n <= 0) return true; // nothing to kill
|
|
208
|
+
// Never issue kill(-1) (which would signal every process): a pid <= 1 is not
|
|
209
|
+
// ours to teardown — report "not gone" so its record is retained, not orphaned.
|
|
210
|
+
if (n <= 1) return false;
|
|
211
|
+
if (process.platform === "win32") {
|
|
212
|
+
spawnSync("taskkill", ["/PID", String(n), "/T", "/F"], { encoding: "utf8" });
|
|
213
|
+
return !isAlive(n);
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
process.kill(-n, "SIGTERM");
|
|
217
|
+
} catch (_) {
|
|
218
|
+
try {
|
|
219
|
+
process.kill(n, "SIGTERM");
|
|
220
|
+
} catch (_) {
|
|
221
|
+
/* already gone */
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
for (let i = 0; i < 20 && isAlive(n); i++) sleepSync(100);
|
|
225
|
+
if (isAlive(n)) {
|
|
226
|
+
try {
|
|
227
|
+
process.kill(-n, "SIGKILL");
|
|
228
|
+
} catch (_) {
|
|
229
|
+
try {
|
|
230
|
+
process.kill(n, "SIGKILL");
|
|
231
|
+
} catch (_) {
|
|
232
|
+
/* already gone */
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
for (let i = 0; i < 10 && isAlive(n); i++) sleepSync(100);
|
|
236
|
+
}
|
|
237
|
+
return !isAlive(n);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/* The opencode session that owns a serve started from this checkout: the
|
|
241
|
+
* explicit `HARNESS_SESSION_ID` when the caller set it, else the nearest
|
|
242
|
+
* ancestor pid whose process wrote a `.serve/sessions/<id>.json` lease (the
|
|
243
|
+
* context-guard plugin records the opencode server pid). Linux-only; null when
|
|
244
|
+
* untagged (an older harness, a hand launch, or a non-Linux host). */
|
|
245
|
+
function ppidOf(pid) {
|
|
246
|
+
try {
|
|
247
|
+
const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
248
|
+
const close = stat.lastIndexOf(")");
|
|
249
|
+
if (close === -1) return null;
|
|
250
|
+
const ppid = parseInt(stat.slice(close + 2).split(" ")[1], 10);
|
|
251
|
+
return Number.isFinite(ppid) ? ppid : null;
|
|
252
|
+
} catch (_) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function ownerSessionID() {
|
|
258
|
+
const explicit = process.env[SESSION_ENV];
|
|
259
|
+
if (explicit) return String(explicit);
|
|
260
|
+
if (process.platform !== "linux") return null;
|
|
261
|
+
const byPid = new Map();
|
|
262
|
+
for (const root of allServeDirs()) {
|
|
263
|
+
for (const rec of safeReadLeases(root)) {
|
|
264
|
+
if (rec.pid && rec.sessionID) byPid.set(Number(rec.pid), rec.sessionID);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (byPid.size === 0) return null;
|
|
268
|
+
let pid = process.pid;
|
|
269
|
+
for (let i = 0; i < 64 && pid && pid !== 1; i++) {
|
|
270
|
+
pid = ppidOf(pid);
|
|
271
|
+
if (pid && byPid.has(pid)) return byPid.get(pid);
|
|
272
|
+
}
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/* All leases recorded directly under a `.serve/` dir's `sessions/` (best-effort).
|
|
277
|
+
* `serveDir` is the `.serve` directory itself (as returned by allServeDirs). */
|
|
278
|
+
function safeReadLeases(serveDir) {
|
|
279
|
+
const sdir = path.join(serveDir, "sessions");
|
|
280
|
+
const out = [];
|
|
281
|
+
let names;
|
|
282
|
+
try {
|
|
283
|
+
names = fs.readdirSync(sdir);
|
|
284
|
+
} catch (_) {
|
|
285
|
+
return out;
|
|
286
|
+
}
|
|
287
|
+
for (const name of names) {
|
|
288
|
+
if (!name.endsWith(".json")) continue;
|
|
289
|
+
try {
|
|
290
|
+
out.push(JSON.parse(fs.readFileSync(path.join(sdir, name), "utf8")));
|
|
291
|
+
} catch (_) {
|
|
292
|
+
/* unreadable lease */
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return out;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/* Active `agent-serve-*` transient units, with the `.out.log` path parsed out of
|
|
299
|
+
* the unit's ExecStart so an untracked legacy orphan can be judged idle. */
|
|
300
|
+
function activeServeUnits() {
|
|
301
|
+
if (!systemdUserAvailable()) return [];
|
|
302
|
+
const r = spawnSync(
|
|
303
|
+
"systemctl",
|
|
304
|
+
["--user", "list-units", "agent-serve-*", "--all", "--no-legend", "--no-pager"],
|
|
305
|
+
{ encoding: "utf8", timeout: 8000, stdio: ["ignore", "pipe", "ignore"] }
|
|
306
|
+
);
|
|
307
|
+
const units = [];
|
|
308
|
+
for (const raw of (r.stdout || "").split(/\r?\n/)) {
|
|
309
|
+
const line = raw.trim();
|
|
310
|
+
const m = line.match(/^(agent-serve-\S+?\.service)\s+loaded\s+active\s+running\s+(.*)$/);
|
|
311
|
+
if (!m) continue;
|
|
312
|
+
const logM = m[2].match(/([^\s"]+\.out\.log)/);
|
|
313
|
+
units.push({ unit: m[1], outLog: logM ? logM[1] : null });
|
|
314
|
+
}
|
|
315
|
+
return units;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function unitStartedAt(unit) {
|
|
319
|
+
const m = String(unit).match(/-(\d{10,})\.service$/);
|
|
320
|
+
return m ? Number(m[1]) : null;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function unitGone(unit) {
|
|
324
|
+
if (!unit) return true;
|
|
325
|
+
try {
|
|
326
|
+
const r = spawnSync("systemctl", ["--user", "is-active", unit], {
|
|
327
|
+
encoding: "utf8",
|
|
328
|
+
timeout: 5000,
|
|
329
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
330
|
+
});
|
|
331
|
+
const state = (r.stdout || "").trim();
|
|
332
|
+
return state === "" || state === "inactive" || state === "failed" || state === "unknown";
|
|
333
|
+
} catch (_) {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/* Stop a unit and verify it is inactive (a stop that "succeeds" but leaves the
|
|
339
|
+
* unit active must NOT be treated as a teardown). */
|
|
340
|
+
function terminateUnit(unit) {
|
|
341
|
+
if (!unit) return true;
|
|
342
|
+
spawnSync("systemctl", ["--user", "stop", unit], { encoding: "utf8", timeout: 15000 });
|
|
343
|
+
for (let i = 0; i < 20 && !unitGone(unit); i++) sleepSync(100);
|
|
344
|
+
return unitGone(unit);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/* Newest mtime among a record's log files (its activity signal). The record
|
|
348
|
+
* stores absolute out/err log paths; fall back to `<serveDir>/<name>.*.log`. */
|
|
349
|
+
function lastActivityMs(info, dir) {
|
|
350
|
+
const paths = [info.outLog, info.errLog];
|
|
351
|
+
const name = info.name;
|
|
352
|
+
if (name) {
|
|
353
|
+
paths.push(logPath(name, "out", dir), logPath(name, "err", dir));
|
|
354
|
+
}
|
|
355
|
+
let newest = null;
|
|
356
|
+
for (const p of paths) {
|
|
357
|
+
if (!p) continue;
|
|
358
|
+
try {
|
|
359
|
+
const m = fs.statSync(p).mtimeMs;
|
|
360
|
+
if (newest == null || m > newest) newest = m;
|
|
361
|
+
} catch (_) {
|
|
362
|
+
/* no log yet */
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return newest;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/* Whether the systemd *user* manager can host a transient unit (Linux only).
|
|
369
|
+
* `--mem-max`/`--cpu-max` use it to cap a heavy process tree in its own cgroup;
|
|
370
|
+
* everywhere else the flags are ignored with a warning. */
|
|
371
|
+
function systemdUserAvailable() {
|
|
372
|
+
if (process.platform !== "linux") return false;
|
|
373
|
+
if (process.env.AGENT_SERVE_NO_SYSTEMD === "1") return false;
|
|
374
|
+
try {
|
|
375
|
+
const r = spawnSync("systemctl", ["--user", "is-system-running"], {
|
|
376
|
+
encoding: "utf8",
|
|
377
|
+
timeout: 5000,
|
|
378
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
379
|
+
});
|
|
380
|
+
if (r.error) return false;
|
|
381
|
+
return r.status === 0 || /running|degraded|starting|maintenance/.test(r.stdout || "");
|
|
382
|
+
} catch (_) {
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/* MainPID of a transient unit, once systemd has forked it. Bounded retry because
|
|
388
|
+
* there is a small race between `systemd-run` returning and the exec landing. */
|
|
389
|
+
function mainPidOf(unit) {
|
|
390
|
+
for (let i = 0; i < 20; i++) {
|
|
391
|
+
const r = spawnSync("systemctl", ["--user", "show", "-p", "MainPID", "--value", unit], {
|
|
392
|
+
encoding: "utf8",
|
|
393
|
+
timeout: 5000,
|
|
394
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
395
|
+
});
|
|
396
|
+
const pid = parseInt((r.stdout || "").trim(), 10);
|
|
397
|
+
if (pid) return pid;
|
|
398
|
+
const end = Date.now() + 150;
|
|
399
|
+
while (Date.now() < end) {
|
|
400
|
+
/* brief busy-wait; the pid is needed before the pid file is written */
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function stopUnit(unit) {
|
|
407
|
+
if (!unit) return false;
|
|
408
|
+
try {
|
|
409
|
+
const r = spawnSync("systemctl", ["--user", "stop", unit], { encoding: "utf8", timeout: 15000 });
|
|
410
|
+
if (r.status === 0) return true;
|
|
411
|
+
// `--collect` may have already GC'd a unit whose command exited: stopping a
|
|
412
|
+
// gone unit is success, not failure.
|
|
413
|
+
const s = spawnSync("systemctl", ["--user", "is-active", unit], { encoding: "utf8", timeout: 5000 });
|
|
414
|
+
const state = (s.stdout || "").trim();
|
|
415
|
+
return (
|
|
416
|
+
state === "" ||
|
|
417
|
+
state === "inactive" ||
|
|
418
|
+
state === "failed" ||
|
|
419
|
+
state === "unknown" ||
|
|
420
|
+
/not-found/.test(s.stderr || "")
|
|
421
|
+
);
|
|
422
|
+
} catch (_) {
|
|
423
|
+
return false;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function logTail(p) {
|
|
428
|
+
try {
|
|
429
|
+
const lines = fs.readFileSync(p, "utf8").split(/\r?\n/).filter(Boolean);
|
|
430
|
+
return lines.slice(-15).join("\n");
|
|
431
|
+
} catch (_) {
|
|
432
|
+
return "";
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function launcherPath(name, dir = SERVE_DIR) {
|
|
437
|
+
return path.join(dir, name + ".cmd");
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// win32 detach: give the server its OWN hidden console so its process tree can
|
|
441
|
+
// never hold this tool call's console/output handles. Node's
|
|
442
|
+
// spawn(..., { detached: true, shell: true }) only sets CREATE_NEW_PROCESS_GROUP
|
|
443
|
+
// (the tree stays in the tool's console and the call blocks for its whole
|
|
444
|
+
// lifetime); a ShellExecute-launched process gets a fresh console instead. cmd.exe
|
|
445
|
+
// parsing is sidestepped by generating a .cmd launcher that runs the command and
|
|
446
|
+
// redirects stdout/stderr to the log files. The real PID comes back from -PassThru.
|
|
447
|
+
function launchDetachedWin32(fullCmd, name) {
|
|
448
|
+
const launcher = launcherPath(name);
|
|
449
|
+
fs.writeFileSync(launcher, "@echo off\r\n" + fullCmd + "\r\n", "utf8");
|
|
450
|
+
const script =
|
|
451
|
+
"$psi = New-Object System.Diagnostics.ProcessStartInfo;" +
|
|
452
|
+
"$psi.FileName = $env:ComSpec;" +
|
|
453
|
+
"$psi.Arguments = $env:SERVE_ARGS;" +
|
|
454
|
+
"$psi.UseShellExecute = $true;" +
|
|
455
|
+
"$psi.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden;" +
|
|
456
|
+
"$psi.WorkingDirectory = $env:SERVE_CWD;" +
|
|
457
|
+
"$p = [System.Diagnostics.Process]::Start($psi);" +
|
|
458
|
+
"Write-Output $p.Id";
|
|
459
|
+
const r = spawnSync(
|
|
460
|
+
powershellExe(),
|
|
461
|
+
["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", script],
|
|
462
|
+
{
|
|
463
|
+
cwd: process.cwd(),
|
|
464
|
+
encoding: "utf8",
|
|
465
|
+
windowsHide: true,
|
|
466
|
+
env: Object.assign({}, process.env, {
|
|
467
|
+
SERVE_ARGS: `/d /c ""${launcher}""`,
|
|
468
|
+
SERVE_CWD: process.cwd(),
|
|
469
|
+
}),
|
|
470
|
+
}
|
|
471
|
+
);
|
|
472
|
+
if (r.error) throw r.error;
|
|
473
|
+
if (r.status !== 0) {
|
|
474
|
+
throw new Error(`serve launch failed (powershell exit ${r.status}): ${(r.stderr || r.stdout || "").trim()}`);
|
|
475
|
+
}
|
|
476
|
+
const pid = parseInt((r.stdout || "").trim(), 10);
|
|
477
|
+
if (!pid || Number.isNaN(pid)) {
|
|
478
|
+
throw new Error(`serve launch returned no PID: ${JSON.stringify(r.stdout || "")}`);
|
|
479
|
+
}
|
|
480
|
+
return pid;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
async function start(flags) {
|
|
484
|
+
if (!flags["--cmd"]) {
|
|
485
|
+
console.error("serve start: --cmd \"<command>\" is required");
|
|
486
|
+
process.exit(1);
|
|
487
|
+
}
|
|
488
|
+
const name = deriveName(flags);
|
|
489
|
+
const timeout = Number(flags["--timeout"]) || DEFAULT_TIMEOUT;
|
|
490
|
+
const tierGroup = flags["--group"] || null;
|
|
491
|
+
const outLog = logPath(name, "out");
|
|
492
|
+
const errLog = logPath(name, "err");
|
|
493
|
+
|
|
494
|
+
fs.mkdirSync(SERVE_DIR, { recursive: true });
|
|
495
|
+
// Auto-close orphaned/idle serves first: a serve left by a killed session
|
|
496
|
+
// otherwise pins RAM and blocks this very launch (an untracked emulator refuses
|
|
497
|
+
// a same-AVD boot). A live owner's serve is never touched (see `reap`).
|
|
498
|
+
const _exit = process.exitCode;
|
|
499
|
+
try {
|
|
500
|
+
reap({ "--quiet": true });
|
|
501
|
+
} catch (_) {
|
|
502
|
+
/* auto-reap is best-effort and must never break a launch */
|
|
503
|
+
}
|
|
504
|
+
process.exitCode = _exit;
|
|
505
|
+
// Reap records whose process is gone before doing anything else: a stale
|
|
506
|
+
// entry must never make a fresh start (or the capacity preflight) act on a
|
|
507
|
+
// phantom server.
|
|
508
|
+
removeDeadRecords(SERVE_DIR);
|
|
509
|
+
|
|
510
|
+
// Capacity preflight: a heavy long-lived process (emulator, Metro/Next dev,
|
|
511
|
+
// live tier) is what froze the box on 2026-09-29. Check the project envelope
|
|
512
|
+
// and the machine-wide heavy lock BEFORE spawning, and refuse (naming what is
|
|
513
|
+
// already up) rather than silently oversubscribing RAM/CPU. `--force`
|
|
514
|
+
// overrides; a trivial process is never gated.
|
|
515
|
+
const heavyLabel = flags["--heavy"]
|
|
516
|
+
? capacity.classifyHeavy(flags["--cmd"]) || "heavy process"
|
|
517
|
+
: capacity.classifyHeavy(flags["--cmd"]);
|
|
518
|
+
const capacityDisabled = process.env.AGENT_CAPACITY_DISABLE === "1";
|
|
519
|
+
const holdLock = !!heavyLabel && !flags["--force"] && !capacityDisabled;
|
|
520
|
+
if (holdLock) {
|
|
521
|
+
const sample = capacity.sample(process.cwd());
|
|
522
|
+
const ev = capacity.evaluate(sample, { pendingHeavy: 1, pendingLive: 1, pendingGroup: tierGroup });
|
|
523
|
+
if (!ev.ok) {
|
|
524
|
+
console.error(capacity.formatSummary(sample));
|
|
525
|
+
console.error(capacity.heavyBlockText(heavyLabel, ev, sample));
|
|
526
|
+
process.exit(2);
|
|
527
|
+
}
|
|
528
|
+
// A sibling member of the same tier group (the emulator's dev server) joins
|
|
529
|
+
// the already-held group lock instead of being refused: the pair is ONE
|
|
530
|
+
// budgeted mobile tier, not two competing heavy tiers.
|
|
531
|
+
const existing = capacity.readLock();
|
|
532
|
+
const joinsGroup =
|
|
533
|
+
!!tierGroup &&
|
|
534
|
+
!!existing &&
|
|
535
|
+
existing.group === tierGroup &&
|
|
536
|
+
!capacity.lockIsStale(existing, sample.envelope.heavyLockTtlMinutes);
|
|
537
|
+
if (!joinsGroup) {
|
|
538
|
+
const lock = capacity.acquireHeavyLock(name, sample.envelope, tierGroup);
|
|
539
|
+
if (!lock.ok && lock.held) {
|
|
540
|
+
const held = lock.lock || {};
|
|
541
|
+
const members = capacity.lockMembers(held);
|
|
542
|
+
const who = members.length ? members.map((m) => m.name).join(", ") : held.name || "?";
|
|
543
|
+
console.error(
|
|
544
|
+
`refused: the machine-wide heavy lock is held by "${who}"` +
|
|
545
|
+
`${held.group ? ` [group ${held.group}]` : ""} ` +
|
|
546
|
+
`(since ${held.since || "?"}) — only one emulator/live tier ` +
|
|
547
|
+
`runs at a time. Stop it (node scripts/serve.js stop --name ${held.name || "<name>"}) ` +
|
|
548
|
+
`or re-run with --force.`
|
|
549
|
+
);
|
|
550
|
+
process.exit(2);
|
|
551
|
+
}
|
|
552
|
+
if (!lock.ok) {
|
|
553
|
+
console.warn(`serve start: heavy lock unavailable (${lock.error || "unknown"}) — continuing`);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
const fullCmd = `${flags["--cmd"]} > "${outLog}" 2> "${errLog}"`;
|
|
559
|
+
|
|
560
|
+
// Resource caps: heavy tiers are capped BY DEFAULT and run as a transient
|
|
561
|
+
// systemd user unit (under `user.slice`), so a runaway is throttled/OOM-killed
|
|
562
|
+
// inside its cgroup instead of dragging the whole VM into swap-thrash. This is
|
|
563
|
+
// also what makes the `user.slice` ceiling installed by `wsl-tune.sh` actually
|
|
564
|
+
// bind heavy work: WSL's `wsl.exe` session lives in `init.scope`, which that
|
|
565
|
+
// ceiling does NOT cover, so a heavy tree launched inline would escape it.
|
|
566
|
+
// Non-heavy processes are left alone; `--no-cap` opts a heavy tier out;
|
|
567
|
+
// `--mem-max` overrides the default envelope cap. Without a systemd user bus
|
|
568
|
+
// the cap degrades to a warning (Linux+systemd only).
|
|
569
|
+
const capDisabled = process.env.AGENT_SERVE_NO_CAP === "1";
|
|
570
|
+
const shouldCap =
|
|
571
|
+
!flags["--no-cap"] &&
|
|
572
|
+
!capDisabled &&
|
|
573
|
+
(!!flags["--mem-max"] || !!flags["--cpu-max"] || !!heavyLabel);
|
|
574
|
+
let memMax = flags["--mem-max"] || null;
|
|
575
|
+
if (!memMax && heavyLabel && shouldCap) {
|
|
576
|
+
const envelope = capacity.readEnvelope(process.cwd());
|
|
577
|
+
if (envelope.heavyMemMaxMb > 0) memMax = `${envelope.heavyMemMaxMb}M`;
|
|
578
|
+
}
|
|
579
|
+
// Combined tier-group budget. Each member's per-process cap is generous (a
|
|
580
|
+
// 2048 MB AVD peaks ~4.4 GB RSS), so an emulator + dev server could each take
|
|
581
|
+
// 5 GB and fill a 10 GB VM all by themselves (the 2026-09-30 freeze). Tighten
|
|
582
|
+
// this member's cap to the group's remaining budget so the pair stays inside
|
|
583
|
+
// ONE budgeted tier; a near-exhausted budget refuses rather than launching a
|
|
584
|
+
// member that will be OOM-killed on arrival. An explicit `--mem-max` wins.
|
|
585
|
+
if (shouldCap && heavyLabel && tierGroup && !flags["--mem-max"]) {
|
|
586
|
+
const envelope = capacity.readEnvelope(process.cwd());
|
|
587
|
+
if (envelope.groupMemMaxMb > 0) {
|
|
588
|
+
const budget = capacity.groupBudget(capacity.readServes(process.cwd()), tierGroup, envelope);
|
|
589
|
+
const desired = capacity.parseMemMb(memMax) || envelope.heavyMemMaxMb;
|
|
590
|
+
if (budget.remainingMb != null && desired > budget.remainingMb) {
|
|
591
|
+
if (budget.remainingMb < 512 && !flags["--force"]) {
|
|
592
|
+
console.error(
|
|
593
|
+
`refused: tier group "${tierGroup}" has only ${budget.remainingMb} MB left of its ` +
|
|
594
|
+
`${budget.totalMb} MB budget (group_mem_max_mb) — stop a member ` +
|
|
595
|
+
`(node scripts/serve.js stop --name <name>) or re-run with --force`
|
|
596
|
+
);
|
|
597
|
+
process.exit(2);
|
|
598
|
+
}
|
|
599
|
+
memMax = `${Math.max(512, budget.remainingMb)}M`;
|
|
600
|
+
console.log(
|
|
601
|
+
`serve start: tier group "${tierGroup}" has ${budget.remainingMb} MB left — capping ${name} at ${memMax}`
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
let unit = null;
|
|
607
|
+
let pid = null;
|
|
608
|
+
if (shouldCap) {
|
|
609
|
+
if (systemdUserAvailable()) {
|
|
610
|
+
const safe = String(name).replace(/[^a-zA-Z0-9_.-]/g, "_");
|
|
611
|
+
unit = `agent-serve-${safe}-${Date.now()}`;
|
|
612
|
+
const args = ["--user", "--unit", unit, "--collect"];
|
|
613
|
+
if (memMax) args.push("-p", `MemoryMax=${memMax}`);
|
|
614
|
+
if (flags["--cpu-max"]) args.push("-p", `CPUQuota=${flags["--cpu-max"]}`);
|
|
615
|
+
// A transient unit gets the user manager's env + cwd, not the caller's.
|
|
616
|
+
// Pass both through so the command resolves and behaves exactly as it
|
|
617
|
+
// would in the agent shell (PATH, node managers, exported project vars).
|
|
618
|
+
args.push("-p", `WorkingDirectory=${process.cwd()}`);
|
|
619
|
+
for (const [k, v] of Object.entries(process.env)) {
|
|
620
|
+
if (typeof v !== "string" || v.includes("\n")) continue;
|
|
621
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(k)) continue;
|
|
622
|
+
args.push(`--setenv=${k}=${v}`);
|
|
623
|
+
}
|
|
624
|
+
args.push("--", "sh", "-c", fullCmd);
|
|
625
|
+
const r = spawnSync("systemd-run", args, {
|
|
626
|
+
cwd: process.cwd(),
|
|
627
|
+
encoding: "utf8",
|
|
628
|
+
timeout: 15000,
|
|
629
|
+
});
|
|
630
|
+
if (r.status !== 0) {
|
|
631
|
+
console.warn(
|
|
632
|
+
`serve start: systemd-run failed (${((r.stderr || r.stdout || "") + "").trim()}) — ` +
|
|
633
|
+
`launching without resource caps`
|
|
634
|
+
);
|
|
635
|
+
unit = null;
|
|
636
|
+
} else {
|
|
637
|
+
pid = mainPidOf(unit);
|
|
638
|
+
}
|
|
639
|
+
} else {
|
|
640
|
+
console.warn(
|
|
641
|
+
"serve start: resource caps ignored — no systemd user bus (Linux+systemd only)"
|
|
642
|
+
);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
if (!unit && !pid) {
|
|
647
|
+
if (process.platform === "win32") {
|
|
648
|
+
pid = launchDetachedWin32(fullCmd, name);
|
|
649
|
+
} else {
|
|
650
|
+
// POSIX: a detached spawn (setsid) is enough to leave the tool's terminal.
|
|
651
|
+
const child = spawn(fullCmd, {
|
|
652
|
+
cwd: process.cwd(),
|
|
653
|
+
shell: true,
|
|
654
|
+
detached: true,
|
|
655
|
+
windowsHide: true,
|
|
656
|
+
stdio: "ignore",
|
|
657
|
+
});
|
|
658
|
+
pid = child.pid;
|
|
659
|
+
child.unref();
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// Record the cap only when it was actually applied (a transient unit). An
|
|
664
|
+
// uncapped fallback (no systemd, systemd-run failed) must not claim mem≤N.
|
|
665
|
+
const appliedMemMax = unit ? memMax : null;
|
|
666
|
+
const appliedCpuMax = unit ? flags["--cpu-max"] || null : null;
|
|
667
|
+
|
|
668
|
+
// Point the heavy lock at the REAL server pid (the `start` CLI exits now, so
|
|
669
|
+
// its pid would read as stale at once). A same-group sibling is appended, not
|
|
670
|
+
// clobbered. Best-effort: a lock write must never break a launch.
|
|
671
|
+
if (holdLock) {
|
|
672
|
+
try {
|
|
673
|
+
capacity.updateHeavyMember({ name, group: tierGroup, pid, unit: unit || null });
|
|
674
|
+
} catch (_) {
|
|
675
|
+
/* lock bookkeeping is best-effort */
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
fs.writeFileSync(
|
|
680
|
+
pidFile(name),
|
|
681
|
+
JSON.stringify(
|
|
682
|
+
{
|
|
683
|
+
pid,
|
|
684
|
+
name,
|
|
685
|
+
group: tierGroup,
|
|
686
|
+
cmd: flags["--cmd"],
|
|
687
|
+
port: flags["--port"] || null,
|
|
688
|
+
url: flags["--url"] || null,
|
|
689
|
+
heavy: !!heavyLabel,
|
|
690
|
+
unit: unit || null,
|
|
691
|
+
memMax: appliedMemMax,
|
|
692
|
+
cpuMax: appliedCpuMax,
|
|
693
|
+
sessionID: ownerSessionID(),
|
|
694
|
+
outLog,
|
|
695
|
+
errLog,
|
|
696
|
+
startedAt: new Date().toISOString(),
|
|
697
|
+
},
|
|
698
|
+
null,
|
|
699
|
+
2
|
|
700
|
+
),
|
|
701
|
+
"utf8"
|
|
702
|
+
);
|
|
703
|
+
|
|
704
|
+
console.log(
|
|
705
|
+
`serve start: launching ${name} (pid ${pid || "?"}${unit ? `, unit ${unit}` : ""}` +
|
|
706
|
+
`${appliedMemMax ? `, mem≤${appliedMemMax}` : ""}) → ${outLog}`
|
|
707
|
+
);
|
|
708
|
+
if (process.platform === "win32") console.log(` launcher: ${launcherPath(name)}`);
|
|
709
|
+
|
|
710
|
+
const readyCheck = flags["--url"]
|
|
711
|
+
? () => checkUrl(flags["--url"])
|
|
712
|
+
: flags["--port"]
|
|
713
|
+
? () => checkPort(flags["--port"])
|
|
714
|
+
: null;
|
|
715
|
+
|
|
716
|
+
if (!readyCheck) {
|
|
717
|
+
console.log(`started ${name} (pid ${pid}) — no readiness check; logs: ${outLog}`);
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
const target = flags["--url"] || "port " + flags["--port"];
|
|
722
|
+
const deadline = Date.now() + timeout;
|
|
723
|
+
let lastBeat = 0;
|
|
724
|
+
while (Date.now() < deadline) {
|
|
725
|
+
if (await readyCheck()) {
|
|
726
|
+
console.log(`started ${name} (pid ${pid}) — ready on ${target}`);
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
const elapsed = timeout - (deadline - Date.now());
|
|
730
|
+
if (elapsed - lastBeat >= HEARTBEAT_INTERVAL) {
|
|
731
|
+
console.log(` …waiting for ${target} (${Math.round(elapsed / 1000)}s elapsed)`);
|
|
732
|
+
lastBeat = elapsed;
|
|
733
|
+
}
|
|
734
|
+
await sleep(READY_INTERVAL);
|
|
735
|
+
}
|
|
736
|
+
const tail = logTail(outLog) || logTail(errLog);
|
|
737
|
+
console.error(`started ${name} (pid ${pid}) but NOT ready on ${target} within ${timeout}ms.`);
|
|
738
|
+
if (tail) console.error(`\n--- last log lines (${outLog}) ---\n${tail}\n---`);
|
|
739
|
+
else console.error(`(no output captured yet — see ${outLog} / ${errLog})`);
|
|
740
|
+
process.exit(1);
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
function stop(flags, dir) {
|
|
744
|
+
const name = deriveName(flags);
|
|
745
|
+
// Resolve which `.serve/` holds this record: the caller's dir, else the
|
|
746
|
+
// current checkout, else any other serve dir on the machine (a server started
|
|
747
|
+
// in the MAIN checkout before a mid-session worktree reroot lives outside the
|
|
748
|
+
// worktree's `.serve/`).
|
|
749
|
+
if (!dir) {
|
|
750
|
+
dir = SERVE_DIR;
|
|
751
|
+
if (!fs.existsSync(pidFile(name, dir))) {
|
|
752
|
+
const found = allServeDirs().find((d) => fs.existsSync(pidFile(name, d)));
|
|
753
|
+
if (found) dir = found;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
const p = pidFile(name, dir);
|
|
757
|
+
if (!fs.existsSync(p)) {
|
|
758
|
+
// No record: release this name's own lock (covers a lock left after a crash).
|
|
759
|
+
capacity.releaseHeavyLock(name);
|
|
760
|
+
console.log(`${name}: not running`);
|
|
761
|
+
return true;
|
|
762
|
+
}
|
|
763
|
+
let info;
|
|
764
|
+
try {
|
|
765
|
+
info = JSON.parse(fs.readFileSync(p, "utf8"));
|
|
766
|
+
} catch (_) {
|
|
767
|
+
info = { pid: fs.readFileSync(p, "utf8").trim() };
|
|
768
|
+
}
|
|
769
|
+
const pid = info.pid;
|
|
770
|
+
const group = info.group || null;
|
|
771
|
+
// Teardown, then VERIFY. A capped launch lives in a transient systemd unit
|
|
772
|
+
// (the unit, not the MainPID, tears down the whole cgroup); an uncapped one is
|
|
773
|
+
// a detached process tree. A stop that does not actually remove the process
|
|
774
|
+
// MUST NOT delete the record — deleting it is exactly how a live unit became
|
|
775
|
+
// an untracked orphan (invisible to list/stop-all/prune).
|
|
776
|
+
const gone = info.unit ? terminateUnit(info.unit) : terminateTree(pid);
|
|
777
|
+
if (!gone) {
|
|
778
|
+
console.error(
|
|
779
|
+
`stop: ${name} (${info.unit ? `unit ${info.unit}` : `pid ${pid}`}) is STILL running after teardown — ` +
|
|
780
|
+
`keeping the record so it is not orphaned. Retry, or inspect: ` +
|
|
781
|
+
`${info.unit ? `systemctl --user status ${info.unit}` : `ps -p ${pid}`}`
|
|
782
|
+
);
|
|
783
|
+
process.exitCode = 1;
|
|
784
|
+
return false;
|
|
785
|
+
}
|
|
786
|
+
console.log(info.unit ? `stopped ${name} (unit ${info.unit})` : `stopped ${name} (pid ${pid})`);
|
|
787
|
+
try {
|
|
788
|
+
fs.unlinkSync(p);
|
|
789
|
+
} catch (_) {}
|
|
790
|
+
try {
|
|
791
|
+
fs.unlinkSync(launcherPath(name, dir));
|
|
792
|
+
} catch (_) {}
|
|
793
|
+
// Remove this member from the heavy lock; the lock file itself is deleted only
|
|
794
|
+
// when the last member is gone, so a shared group (emulator + dev server)
|
|
795
|
+
// releases exactly once.
|
|
796
|
+
capacity.releaseHeavyLock(name, group);
|
|
797
|
+
return true;
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
/* Stop every recorded serve owned by a session (used when that session ends). */
|
|
801
|
+
function stopSession(sessionID) {
|
|
802
|
+
let stopped = 0;
|
|
803
|
+
for (const dir of allServeDirs()) {
|
|
804
|
+
for (const name of listNames(dir)) {
|
|
805
|
+
let info = {};
|
|
806
|
+
try {
|
|
807
|
+
info = JSON.parse(fs.readFileSync(pidFile(name, dir), "utf8"));
|
|
808
|
+
} catch (_) {
|
|
809
|
+
continue;
|
|
810
|
+
}
|
|
811
|
+
if (info.sessionID && info.sessionID === sessionID) {
|
|
812
|
+
if (stop({ "--name": name }, dir)) stopped++;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
console.log(`stop --session ${sessionID}: ${stopped} serve(s) stopped`);
|
|
817
|
+
return stopped;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
function listNames(dir = SERVE_DIR) {
|
|
821
|
+
if (!fs.existsSync(dir)) return [];
|
|
822
|
+
return fs.readdirSync(dir).filter((f) => f.endsWith(".pid")).map((f) => f.slice(0, -4));
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/* The MAIN worktree root (where a device tier started before a mid-session
|
|
826
|
+
* worktree reroot left its `.serve/`), or null outside a git repo. */
|
|
827
|
+
function mainWorktreeRoot() {
|
|
828
|
+
try {
|
|
829
|
+
const r = spawnSync("git", ["rev-parse", "--path-format=absolute", "--git-common-dir"], {
|
|
830
|
+
cwd: process.cwd(),
|
|
831
|
+
encoding: "utf8",
|
|
832
|
+
});
|
|
833
|
+
if (r.status === 0 && r.stdout && r.stdout.trim()) {
|
|
834
|
+
const common = r.stdout.trim();
|
|
835
|
+
if (path.basename(common) === ".git") return path.dirname(common);
|
|
836
|
+
}
|
|
837
|
+
const r2 = spawnSync("git", ["rev-parse", "--show-toplevel"], { cwd: process.cwd(), encoding: "utf8" });
|
|
838
|
+
if (r2.status === 0 && r2.stdout && r2.stdout.trim()) return r2.stdout.trim();
|
|
839
|
+
} catch (_) {
|
|
840
|
+
/* not a git checkout */
|
|
841
|
+
}
|
|
842
|
+
return null;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/* Every `.serve/` dir this machine's sessions may have written: the current
|
|
846
|
+
* checkout's, the MAIN worktree's, and sibling feature worktrees'. `list` and
|
|
847
|
+
* `stop-all` must span all of them — a server started in the MAIN checkout is
|
|
848
|
+
* invisible to a session that re-rooted into its worktree, so a worktree-scoped
|
|
849
|
+
* `stop-all` reported "no servers running" while the dev server survived and
|
|
850
|
+
* pinned RAM for hours (the F-302 leak that starved F-304/F-305 device runs). */
|
|
851
|
+
function allServeDirs() {
|
|
852
|
+
const dirs = new Set();
|
|
853
|
+
const add = (d) => {
|
|
854
|
+
if (d && fs.existsSync(d)) dirs.add(path.resolve(d));
|
|
855
|
+
};
|
|
856
|
+
add(SERVE_DIR);
|
|
857
|
+
const main = mainWorktreeRoot();
|
|
858
|
+
if (main) add(path.join(main, ".serve"));
|
|
859
|
+
for (const d of siblingServeDirs()) add(d);
|
|
860
|
+
return [...dirs];
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/* Stop every recorded server across every serve dir, then reconcile the
|
|
864
|
+
* machine-wide heavy lock (an orphaned member whose server record was lost in
|
|
865
|
+
* another tree would otherwise wedge the capacity guard). */
|
|
866
|
+
function stopAll() {
|
|
867
|
+
const entries = [];
|
|
868
|
+
for (const dir of allServeDirs()) for (const name of listNames(dir)) entries.push({ dir, name });
|
|
869
|
+
if (entries.length === 0) console.log("no servers running");
|
|
870
|
+
else for (const { dir, name } of entries) stop({ "--name": name }, dir);
|
|
871
|
+
|
|
872
|
+
const lock = capacity.readLock();
|
|
873
|
+
if (lock) {
|
|
874
|
+
const members = capacity.lockMembers(lock);
|
|
875
|
+
let killed = 0;
|
|
876
|
+
for (const m of members) {
|
|
877
|
+
if (!m.pid || !capacity.isAlive(m.pid)) continue;
|
|
878
|
+
if (m.unit ? stopUnit(m.unit) : killTree(m.pid)) killed++;
|
|
879
|
+
}
|
|
880
|
+
capacity.clearHeavyLock();
|
|
881
|
+
console.log(
|
|
882
|
+
`stop-all: cleared heavy lock "${lock.name}" (${members.length} member(s)${killed ? `, ${killed} stopped` : ""})`
|
|
883
|
+
);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/* Sibling worktree `.serve/` dirs, when this checkout lives under the harness
|
|
888
|
+
* layout `<repo>/../.worktrees/<repo>/<id>`. Dead pid files there are invisible
|
|
889
|
+
* to a `prune` run in the main checkout, yet they misreport live serves and hide
|
|
890
|
+
* which heavy tiers were left behind — so `prune --worktrees` clears them. */
|
|
891
|
+
function siblingServeDirs() {
|
|
892
|
+
const m = process.cwd().match(/^(.*[\\/])\.worktrees[\\/]([^\\/]+)[\\/][^\\/]+/);
|
|
893
|
+
if (!m) return [];
|
|
894
|
+
const base = path.join(m[1], ".worktrees", m[2]);
|
|
895
|
+
const out = [];
|
|
896
|
+
try {
|
|
897
|
+
for (const id of fs.readdirSync(base)) {
|
|
898
|
+
const dir = path.join(base, id, ".serve");
|
|
899
|
+
if (fs.existsSync(dir)) out.push(dir);
|
|
900
|
+
}
|
|
901
|
+
} catch (_) {
|
|
902
|
+
/* no worktrees — nothing to add */
|
|
903
|
+
}
|
|
904
|
+
return out;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
/* Remove the dead `.serve/*.pid` records in one dir. Shared by the explicit
|
|
908
|
+
* `prune` command and the silent auto-prune that `list`/`start` perform, so a
|
|
909
|
+
* stale record (e.g. an adb-server/emulator left over from a previous session)
|
|
910
|
+
* is reaped the moment anyone looks at or launches a server instead of lingering
|
|
911
|
+
* to misreport the machine. Dead records cost no RAM, but they hide a stopped
|
|
912
|
+
* emulator and let a stale `list` mislead an operator. Live servers are never
|
|
913
|
+
* touched. `onRemove` (optional) receives each pruned pid-file path. */
|
|
914
|
+
function removeDeadRecords(dir, onRemove) {
|
|
915
|
+
let files;
|
|
916
|
+
try {
|
|
917
|
+
files = fs.readdirSync(dir).filter((f) => f.endsWith(".pid"));
|
|
918
|
+
} catch (_) {
|
|
919
|
+
return { removed: 0, kept: 0 };
|
|
920
|
+
}
|
|
921
|
+
let removed = 0;
|
|
922
|
+
let kept = 0;
|
|
923
|
+
for (const f of files) {
|
|
924
|
+
const p = path.join(dir, f);
|
|
925
|
+
const name = f.slice(0, -4);
|
|
926
|
+
let info = {};
|
|
927
|
+
try {
|
|
928
|
+
info = JSON.parse(fs.readFileSync(p, "utf8"));
|
|
929
|
+
} catch (_) {
|
|
930
|
+
info = {};
|
|
931
|
+
}
|
|
932
|
+
// A record is dead only when BOTH its pid is gone AND (if capped) its unit is
|
|
933
|
+
// inactive. A failed stop or a MainPID that changed must never be pruned into
|
|
934
|
+
// an untracked orphan while the unit is still running.
|
|
935
|
+
if (isAlive(info.pid) || (info.unit && !unitGone(info.unit))) {
|
|
936
|
+
kept++;
|
|
937
|
+
continue;
|
|
938
|
+
}
|
|
939
|
+
try {
|
|
940
|
+
fs.unlinkSync(p);
|
|
941
|
+
fs.unlinkSync(path.join(dir, name + ".cmd"));
|
|
942
|
+
} catch (_) {
|
|
943
|
+
/* already gone */
|
|
944
|
+
}
|
|
945
|
+
removed++;
|
|
946
|
+
if (onRemove) onRemove(p);
|
|
947
|
+
}
|
|
948
|
+
return { removed, kept };
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
function prune(flags) {
|
|
952
|
+
const dirs = [SERVE_DIR];
|
|
953
|
+
if (flags["--worktrees"]) dirs.push(...siblingServeDirs());
|
|
954
|
+
let removed = 0;
|
|
955
|
+
let kept = 0;
|
|
956
|
+
for (const dir of dirs) {
|
|
957
|
+
const result = removeDeadRecords(dir, (p) => console.log(`pruned stale ${p}`));
|
|
958
|
+
removed += result.removed;
|
|
959
|
+
kept += result.kept;
|
|
960
|
+
}
|
|
961
|
+
console.log(`prune: removed ${removed} stale record(s), kept ${kept} live`);
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
/* reap — the mechanical auto-close for long-lived serves.
|
|
965
|
+
*
|
|
966
|
+
* A serve is STOPPED when it is provably no longer managed:
|
|
967
|
+
* - its owning session's lease is gone or stale (heartbeat older than the idle
|
|
968
|
+
* window) — the session ended without stopping it; or
|
|
969
|
+
* - it is untagged (no owner) AND older than `idle_reap_minutes` AND its log
|
|
970
|
+
* shows no recent activity.
|
|
971
|
+
* A serve is KEPT when its owner is a live session, or its log was written
|
|
972
|
+
* within the idle window. This is what closes a dev server/emulator "after use"
|
|
973
|
+
* without killing a live session's long-running tier — the leak where a
|
|
974
|
+
* crashed/killed session left `agent-serve-*` units running for hours (an
|
|
975
|
+
* untracked unit blocks the next boot: "multiple emulators with the same AVD").
|
|
976
|
+
* It also reconciles a legacy untracked unit (a record lost by the old
|
|
977
|
+
* unverified-stop bug) when that unit is old and idle. */
|
|
978
|
+
function reap(flags = {}) {
|
|
979
|
+
const envelope = capacity.readEnvelope(process.cwd());
|
|
980
|
+
const idleMs = (envelope.idleReapMinutes || 90) * 60000;
|
|
981
|
+
const now = Date.now();
|
|
982
|
+
|
|
983
|
+
const units = activeServeUnits();
|
|
984
|
+
// Scan records in every known serve dir PLUS the dir behind each active unit's
|
|
985
|
+
// log, so a worktree unit's record (and its owner lease) is seen even when reap
|
|
986
|
+
// runs from the main checkout.
|
|
987
|
+
const dirs = new Set(allServeDirs());
|
|
988
|
+
for (const u of units) if (u.outLog) dirs.add(path.dirname(u.outLog));
|
|
989
|
+
|
|
990
|
+
const leasesBySession = new Map();
|
|
991
|
+
for (const dir of dirs) {
|
|
992
|
+
for (const rec of safeReadLeases(dir)) {
|
|
993
|
+
if (rec.sessionID) leasesBySession.set(rec.sessionID, rec);
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
const ownerLive = (sessionID) => {
|
|
997
|
+
if (!sessionID) return false;
|
|
998
|
+
const rec = leasesBySession.get(sessionID);
|
|
999
|
+
if (!rec) return false;
|
|
1000
|
+
try {
|
|
1001
|
+
return leaseLib.leaseStatus(rec, { now, maxAgeMs: idleMs }).live;
|
|
1002
|
+
} catch (_) {
|
|
1003
|
+
return false;
|
|
1004
|
+
}
|
|
1005
|
+
};
|
|
1006
|
+
|
|
1007
|
+
const reaped = [];
|
|
1008
|
+
const kept = [];
|
|
1009
|
+
const trackedUnits = new Set();
|
|
1010
|
+
for (const dir of dirs) {
|
|
1011
|
+
for (const name of listNames(dir)) {
|
|
1012
|
+
let info = {};
|
|
1013
|
+
try {
|
|
1014
|
+
info = JSON.parse(fs.readFileSync(pidFile(name, dir), "utf8"));
|
|
1015
|
+
} catch (_) {
|
|
1016
|
+
continue;
|
|
1017
|
+
}
|
|
1018
|
+
if (info.unit) trackedUnits.add(info.unit);
|
|
1019
|
+
const alive = isAlive(info.pid) || (info.unit ? !unitGone(info.unit) : false);
|
|
1020
|
+
if (!alive) continue; // dead record — `prune` owns that
|
|
1021
|
+
if (ownerLive(info.sessionID)) {
|
|
1022
|
+
kept.push(`${name} (owner live)`);
|
|
1023
|
+
continue;
|
|
1024
|
+
}
|
|
1025
|
+
const activity = lastActivityMs(info, dir);
|
|
1026
|
+
if (activity != null && now - activity < idleMs) {
|
|
1027
|
+
kept.push(`${name} (active ${Math.round((now - activity) / 1000)}s ago)`);
|
|
1028
|
+
continue;
|
|
1029
|
+
}
|
|
1030
|
+
const started = info.startedAt ? Date.parse(info.startedAt) : null;
|
|
1031
|
+
const ageMs = started ? now - started : null;
|
|
1032
|
+
if (!info.sessionID && ageMs != null && ageMs <= idleMs) {
|
|
1033
|
+
kept.push(`${name} (untagged, ${Math.round(ageMs / 60000)}m — under idle ${envelope.idleReapMinutes}m)`);
|
|
1034
|
+
continue;
|
|
1035
|
+
}
|
|
1036
|
+
const why = info.sessionID ? "owner gone" : `idle ${ageMs == null ? "?" : Math.round(ageMs / 60000) + "m"}`;
|
|
1037
|
+
if (stop({ "--name": name }, dir)) reaped.push(`${name} (${why})`);
|
|
1038
|
+
else kept.push(`${name} (stop failed)`);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
// Untracked legacy units: a `.pid` record lost to the old unverified stop.
|
|
1043
|
+
for (const u of units) {
|
|
1044
|
+
if (trackedUnits.has(u.unit)) continue;
|
|
1045
|
+
const startedAt = unitStartedAt(u.unit);
|
|
1046
|
+
const ageMs = startedAt ? now - startedAt : null;
|
|
1047
|
+
let activity = null;
|
|
1048
|
+
if (u.outLog) {
|
|
1049
|
+
try {
|
|
1050
|
+
activity = fs.statSync(u.outLog).mtimeMs;
|
|
1051
|
+
} catch (_) {
|
|
1052
|
+
/* no log */
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
if (activity != null && now - activity < idleMs) {
|
|
1056
|
+
kept.push(`${u.unit} (untracked, active)`);
|
|
1057
|
+
continue;
|
|
1058
|
+
}
|
|
1059
|
+
if (ageMs != null && ageMs <= idleMs) {
|
|
1060
|
+
kept.push(`${u.unit} (untracked, ${Math.round(ageMs / 60000)}m)`);
|
|
1061
|
+
continue;
|
|
1062
|
+
}
|
|
1063
|
+
if (terminateUnit(u.unit)) reaped.push(`${u.unit} (untracked orphan)`);
|
|
1064
|
+
else kept.push(`${u.unit} (stop failed)`);
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
if (flags["--json"]) {
|
|
1068
|
+
console.log(JSON.stringify({ reaped, kept }));
|
|
1069
|
+
} else if (!flags["--quiet"] || reaped.length) {
|
|
1070
|
+
for (const r of reaped) console.log(`reaped ${r}`);
|
|
1071
|
+
if (!flags["--quiet"]) for (const k of kept) console.log(`kept ${k}`);
|
|
1072
|
+
console.log(`reap: ${reaped.length} reaped, ${kept.length} kept (idle ${envelope.idleReapMinutes}m)`);
|
|
1073
|
+
}
|
|
1074
|
+
return reaped.length;
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
function isAlive(pid) {
|
|
1078
|
+
if (!pid || pid === "?") return false;
|
|
1079
|
+
try {
|
|
1080
|
+
process.kill(pid, 0);
|
|
1081
|
+
} catch (_) {
|
|
1082
|
+
return false;
|
|
1083
|
+
}
|
|
1084
|
+
// A zombie still answers kill(0) but is not running. Treat it as gone, or a
|
|
1085
|
+
// confirmed teardown reads as "still running" and its record is never removed.
|
|
1086
|
+
if (process.platform === "linux") {
|
|
1087
|
+
try {
|
|
1088
|
+
const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
1089
|
+
const close = stat.lastIndexOf(")");
|
|
1090
|
+
const state = close === -1 ? "" : stat.slice(close + 2).split(" ")[0];
|
|
1091
|
+
if (state === "Z") return false;
|
|
1092
|
+
} catch (_) {
|
|
1093
|
+
/* /proc entry gone → not alive */
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
return true;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
function list() {
|
|
1100
|
+
// Auto-prune first, so `list` never reports a record whose process is gone
|
|
1101
|
+
// (e.g. an adb-server/emulator left over from a killed session). Prune every
|
|
1102
|
+
// serve dir the machine may have written, not just the current checkout's.
|
|
1103
|
+
const dirs = allServeDirs();
|
|
1104
|
+
for (const dir of dirs) removeDeadRecords(dir);
|
|
1105
|
+
// Capacity first: free RAM/swap/load + live-serve count, so `list` is the
|
|
1106
|
+
// one place an operator sees the whole machine state and can spot an idle
|
|
1107
|
+
// emulator/dev server to reap before it freezes the box.
|
|
1108
|
+
try {
|
|
1109
|
+
const sample = capacity.sample(process.cwd());
|
|
1110
|
+
console.log(capacity.formatSummary(sample));
|
|
1111
|
+
} catch (_) {
|
|
1112
|
+
/* capacity is best-effort */
|
|
1113
|
+
}
|
|
1114
|
+
const lock = capacity.readLock();
|
|
1115
|
+
if (lock) {
|
|
1116
|
+
const members = capacity.lockMembers(lock);
|
|
1117
|
+
const who = members.map((m) => `${m.name}(pid ${m.pid})`).join(", ") || "none";
|
|
1118
|
+
const stale = capacity.lockIsStale(lock) ? " [stale]" : "";
|
|
1119
|
+
console.log(`heavy lock: "${lock.name}" [${who}] since ${lock.since}${stale}`);
|
|
1120
|
+
}
|
|
1121
|
+
const entries = [];
|
|
1122
|
+
for (const dir of dirs) for (const name of listNames(dir)) entries.push({ dir, name });
|
|
1123
|
+
const recordedPids = new Set();
|
|
1124
|
+
if (entries.length === 0) {
|
|
1125
|
+
console.log("no servers running");
|
|
1126
|
+
}
|
|
1127
|
+
for (const { dir, name } of entries) {
|
|
1128
|
+
let info;
|
|
1129
|
+
try {
|
|
1130
|
+
info = JSON.parse(fs.readFileSync(pidFile(name, dir), "utf8"));
|
|
1131
|
+
} catch (_) {
|
|
1132
|
+
info = { pid: "?", name };
|
|
1133
|
+
}
|
|
1134
|
+
if (info.pid && info.pid !== "?") recordedPids.add(String(info.pid));
|
|
1135
|
+
const started = info.startedAt ? Date.parse(info.startedAt) : NaN;
|
|
1136
|
+
const age = Number.isFinite(started) ? ` · up ${Math.round((Date.now() - started) / 60000)}m` : "";
|
|
1137
|
+
const heavy = info.heavy ? " [heavy]" : "";
|
|
1138
|
+
const group = info.group ? ` [group ${info.group}]` : "";
|
|
1139
|
+
const caps = [info.memMax ? `mem≤${info.memMax}` : null, info.cpuMax ? `cpu≤${info.cpuMax}` : null]
|
|
1140
|
+
.filter(Boolean)
|
|
1141
|
+
.join(" ");
|
|
1142
|
+
const capped = caps ? ` {${caps}${info.unit ? `, ${info.unit}` : ""}}` : "";
|
|
1143
|
+
const where = path.resolve(dir) === path.resolve(SERVE_DIR) ? "" : ` @ ${dir}`;
|
|
1144
|
+
console.log(
|
|
1145
|
+
`${name}: pid ${info.pid} (${isAlive(info.pid) ? "running" : "stale"})${heavy}${group}${capped}${age}${where} — ${info.cmd || "?"}`
|
|
1146
|
+
);
|
|
1147
|
+
}
|
|
1148
|
+
// Warn about a lock member with no backing serve record anywhere — the leaked
|
|
1149
|
+
// tier that a worktree-scoped stop-all used to miss. `stop-all` clears it.
|
|
1150
|
+
if (lock) {
|
|
1151
|
+
const orphans = capacity
|
|
1152
|
+
.lockMembers(lock)
|
|
1153
|
+
.filter((m) => m.pid && capacity.isAlive(m.pid) && !recordedPids.has(String(m.pid)));
|
|
1154
|
+
if (orphans.length) {
|
|
1155
|
+
console.log(
|
|
1156
|
+
`heavy lock: ${orphans.length} orphan member(s) with no serve record (${orphans
|
|
1157
|
+
.map((m) => `${m.name}(pid ${m.pid})`)
|
|
1158
|
+
.join(", ")}) — reaping with \`node scripts/serve.js stop-all\``
|
|
1159
|
+
);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
/* Run an UNCAPPED heavy command extractor through a stable name. */
|
|
1165
|
+
function runDirect(cmd) {
|
|
1166
|
+
const r = spawnSync(cmd, { cwd: process.cwd(), shell: true, stdio: "inherit" });
|
|
1167
|
+
if (r.error) {
|
|
1168
|
+
console.error(`serve run: ${r.error.message}`);
|
|
1169
|
+
return 1;
|
|
1170
|
+
}
|
|
1171
|
+
return r.status == null ? 1 : r.status;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
/* run — execute a SYNCHRONOUS one-shot heavy command inside a capped cgroup.
|
|
1175
|
+
*
|
|
1176
|
+
* `start` handles long-lived servers; a build or a heavy test tier is a
|
|
1177
|
+
* one-shot that must still be contained. This is the 2026-09-30 fix: the APK
|
|
1178
|
+
* build ran inline as `bash .../build-dev-apk.sh` (no serve.js), so its Gradle
|
|
1179
|
+
* daemon + Kotlin daemon + R8/D8 job had no MemoryMax and dragged the whole VM
|
|
1180
|
+
* into swap-thrash. `run` executes the command in a transient systemd scope with
|
|
1181
|
+
* `MemoryMax` from the envelope (or `--mem-max`), streams its output, and returns
|
|
1182
|
+
* its exit code — so a runaway is OOM-killed inside its own cgroup instead of
|
|
1183
|
+
* freezing every terminal. `--no-cap` opts out; a non-systemd host degrades to a
|
|
1184
|
+
* plain run with a warning. A heavy ONE-SHOT is not a long-lived tier, but it
|
|
1185
|
+
* still needs several GB: sharing the box with a live emulator+dev group OOMs
|
|
1186
|
+
* the build AND gets the emulator killed on a small VM (F-296, 2026-09-30). So
|
|
1187
|
+
* `run` joins the live heavy group (or an explicit `--group`), caps the one-shot
|
|
1188
|
+
* to the group's remaining budget, and refuses when the remainder is below the
|
|
1189
|
+
* one-shot floor — a build must run BEFORE the group boots, or not at all.
|
|
1190
|
+
*/
|
|
1191
|
+
function runCommand(flags) {
|
|
1192
|
+
const cmd = flags["--cmd"];
|
|
1193
|
+
if (!cmd) {
|
|
1194
|
+
console.error('serve run: --cmd "<command>" is required');
|
|
1195
|
+
process.exit(1);
|
|
1196
|
+
}
|
|
1197
|
+
const name = flags["--name"] || "run";
|
|
1198
|
+
const label = capacity.classifyHeavy(cmd);
|
|
1199
|
+
const capacityDisabled = process.env.AGENT_CAPACITY_DISABLE === "1";
|
|
1200
|
+
|
|
1201
|
+
const noCap = !!flags["--no-cap"] || process.env.AGENT_SERVE_NO_CAP === "1";
|
|
1202
|
+
const envelope = capacity.readEnvelope(process.cwd());
|
|
1203
|
+
let memMax = flags["--mem-max"] || null;
|
|
1204
|
+
if (!memMax && !noCap && envelope.heavyMemMaxMb > 0) memMax = `${envelope.heavyMemMaxMb}M`;
|
|
1205
|
+
|
|
1206
|
+
if (label && !flags["--force"] && !capacityDisabled) {
|
|
1207
|
+
const sample = capacity.sample(process.cwd());
|
|
1208
|
+
const longLived = capacity.isLongLivedHeavy(cmd);
|
|
1209
|
+
// If exactly one heavy group is live, the one-shot joins it (one budgeted
|
|
1210
|
+
// tier) so `evaluate` does not wrongly count two live members as two tiers.
|
|
1211
|
+
const liveGroups = [
|
|
1212
|
+
...new Set(sample.serves.filter((x) => x.alive && x.heavy && x.group).map((x) => x.group)),
|
|
1213
|
+
];
|
|
1214
|
+
const joinGroup = flags["--group"] || (liveGroups.length === 1 ? liveGroups[0] : null);
|
|
1215
|
+
const ev = capacity.evaluate(sample, {
|
|
1216
|
+
pendingHeavy: longLived ? 1 : 0,
|
|
1217
|
+
pendingLive: longLived ? 1 : 0,
|
|
1218
|
+
pendingGroup: joinGroup,
|
|
1219
|
+
});
|
|
1220
|
+
if (!ev.ok) {
|
|
1221
|
+
console.error(capacity.formatSummary(sample));
|
|
1222
|
+
console.error(capacity.heavyBlockText(label, ev, sample));
|
|
1223
|
+
process.exit(2);
|
|
1224
|
+
}
|
|
1225
|
+
for (const w of ev.warnings) console.error(`capacity warning: ${w}`);
|
|
1226
|
+
|
|
1227
|
+
// A heavy one-shot must also fit the live tier group's remaining budget. The
|
|
1228
|
+
// free-RAM floor above only catches an already-empty box; a live emulator+dev
|
|
1229
|
+
// group (each capped generously) plus a 5-6 GB build exceeds a 10 GB VM even
|
|
1230
|
+
// while free RAM still looks healthy. Join the one live heavy group (or an
|
|
1231
|
+
// explicit --group), cap to the remainder, and refuse when it is too small.
|
|
1232
|
+
if (!longLived && envelope.groupMemMaxMb > 0 && joinGroup) {
|
|
1233
|
+
const budget = capacity.groupBudget(sample.serves, joinGroup, envelope);
|
|
1234
|
+
const minOneShotMb = Number(process.env.AGENT_ONESHOT_MIN_MB) || 2048;
|
|
1235
|
+
const requested = capacity.parseMemMb(memMax) || envelope.heavyMemMaxMb || minOneShotMb;
|
|
1236
|
+
if (budget.remainingMb != null && budget.remainingMb < minOneShotMb) {
|
|
1237
|
+
const members = sample.serves
|
|
1238
|
+
.filter((x) => x.alive && x.group === joinGroup)
|
|
1239
|
+
.map((x) => x.name)
|
|
1240
|
+
.join(", ");
|
|
1241
|
+
console.error(capacity.formatSummary(sample));
|
|
1242
|
+
console.error(
|
|
1243
|
+
`refused: one-shot "${label}" needs ≥ ${minOneShotMb} MB, but tier group "${joinGroup}" ` +
|
|
1244
|
+
`has only ${budget.remainingMb} MB left of ${budget.totalMb} MB (${members}). ` +
|
|
1245
|
+
`Build BEFORE booting the group, or stop one member ` +
|
|
1246
|
+
`(node scripts/serve.js stop --name <name>).`
|
|
1247
|
+
);
|
|
1248
|
+
process.exit(2);
|
|
1249
|
+
}
|
|
1250
|
+
if (budget.remainingMb != null && requested > budget.remainingMb) {
|
|
1251
|
+
console.error(
|
|
1252
|
+
`capacity warning: tightening one-shot "${name}" to the "${joinGroup}" group remainder (${budget.remainingMb} MB)`
|
|
1253
|
+
);
|
|
1254
|
+
memMax = `${budget.remainingMb}M`;
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
const started = Date.now();
|
|
1260
|
+
let status;
|
|
1261
|
+
if (!noCap && systemdUserAvailable()) {
|
|
1262
|
+
const args = [
|
|
1263
|
+
"--user",
|
|
1264
|
+
"--scope",
|
|
1265
|
+
"--quiet",
|
|
1266
|
+
"--expand-environment=no",
|
|
1267
|
+
`--working-directory=${process.cwd()}`,
|
|
1268
|
+
];
|
|
1269
|
+
if (memMax) args.push("-p", `MemoryMax=${memMax}`);
|
|
1270
|
+
if (flags["--cpu-max"]) args.push("-p", `CPUQuota=${flags["--cpu-max"]}`);
|
|
1271
|
+
args.push("--", "sh", "-c", cmd);
|
|
1272
|
+
console.log(`serve run: ${name}${memMax ? ` (mem≤${memMax})` : ""} — capped one-shot, streaming output`);
|
|
1273
|
+
const r = spawnSync("systemd-run", args, { cwd: process.cwd(), stdio: "inherit" });
|
|
1274
|
+
if (r.error) {
|
|
1275
|
+
console.warn(`serve run: systemd-run failed (${r.error.message}) — running without caps`);
|
|
1276
|
+
status = runDirect(cmd);
|
|
1277
|
+
} else {
|
|
1278
|
+
status = r.status;
|
|
1279
|
+
}
|
|
1280
|
+
} else {
|
|
1281
|
+
if (!noCap) console.warn("serve run: resource caps ignored — no systemd user bus (Linux+systemd only)");
|
|
1282
|
+
status = runDirect(cmd);
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
const secs = Math.round((Date.now() - started) / 1000);
|
|
1286
|
+
console.log(`serve run: ${name} exited ${status == null ? "?" : status} after ${secs}s`);
|
|
1287
|
+
process.exit(status == null ? 1 : status);
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
function main() {
|
|
1291
|
+
const argv = process.argv.slice(2);
|
|
1292
|
+
const sub = argv[0];
|
|
1293
|
+
const flags = parseArgs(argv.slice(1));
|
|
1294
|
+
switch (sub) {
|
|
1295
|
+
case "start":
|
|
1296
|
+
start(flags).catch((e) => {
|
|
1297
|
+
console.error(e);
|
|
1298
|
+
process.exit(1);
|
|
1299
|
+
});
|
|
1300
|
+
break;
|
|
1301
|
+
case "run":
|
|
1302
|
+
runCommand(flags);
|
|
1303
|
+
break;
|
|
1304
|
+
case "stop":
|
|
1305
|
+
if (flags["--session"]) stopSession(flags["--session"]);
|
|
1306
|
+
else stop(flags);
|
|
1307
|
+
break;
|
|
1308
|
+
case "stop-all":
|
|
1309
|
+
stopAll();
|
|
1310
|
+
break;
|
|
1311
|
+
case "list":
|
|
1312
|
+
list();
|
|
1313
|
+
break;
|
|
1314
|
+
case "prune":
|
|
1315
|
+
prune(flags);
|
|
1316
|
+
break;
|
|
1317
|
+
case "reap":
|
|
1318
|
+
reap(flags);
|
|
1319
|
+
break;
|
|
1320
|
+
default:
|
|
1321
|
+
console.error(
|
|
1322
|
+
'usage: node scripts/serve.js <start|run|stop|stop-all|list|prune|reap> [--cmd "..."] [--url <url>] [--port <n>] [--name <label>] [--group <tier>] [--timeout <ms>] [--heavy] [--force] [--no-cap] [--worktrees] [--mem-max 4G] [--cpu-max 250%] [--session <ses_...>] [--json] [--quiet]'
|
|
1323
|
+
);
|
|
1324
|
+
process.exitCode = 1;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
main();
|