@bridge_gpt/mcp-server 0.2.71 → 0.2.73

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 (198) hide show
  1. package/README.md +55 -17
  2. package/build/agent-launchers/claude-executor-adapter.js +22 -3
  3. package/build/agent-notes.js +171 -95
  4. package/build/agents.generated.js +7 -7
  5. package/build/bridge-permissions.js +31 -0
  6. package/build/bridge-worktree-inspection.js +237 -0
  7. package/build/check-evidence.js +6 -0
  8. package/build/ci-system-detection.js +50 -0
  9. package/build/ci-trigger-adapters.js +631 -0
  10. package/build/ci-trigger-install.js +106 -0
  11. package/build/claude-review-workflow.js +1 -1
  12. package/build/cli-help.js +6 -0
  13. package/build/codex-auto-recovery.js +126 -0
  14. package/build/codex-launcher-pin.js +33 -2
  15. package/build/codex-skill-adapter.js +5 -1
  16. package/build/command-host.js +33 -0
  17. package/build/commands.generated.js +7 -6
  18. package/build/conductor/bridge-api-client.js +1352 -50
  19. package/build/conductor/bring-up-facts.js +18 -5
  20. package/build/conductor/command-drift-production.js +42 -17
  21. package/build/conductor/command-drift.js +47 -0
  22. package/build/conductor/declare-blocked.js +554 -0
  23. package/build/conductor/doctor.js +187 -28
  24. package/build/conductor/driver-commands.js +141 -0
  25. package/build/conductor/driver-guard.js +277 -0
  26. package/build/conductor/driver-lapse.js +321 -0
  27. package/build/conductor/driver-register.js +176 -0
  28. package/build/conductor/driver-release.js +174 -0
  29. package/build/conductor/driver-session-state.js +315 -0
  30. package/build/conductor/epic-branch-ci-inspection.js +222 -29
  31. package/build/conductor/epic-implementer-cli.js +213 -13
  32. package/build/conductor/epic-server-database.js +162 -0
  33. package/build/conductor/epic-server-guidance.js +278 -0
  34. package/build/conductor/epic-server-journal.js +239 -0
  35. package/build/conductor/epic-server-processes.js +324 -0
  36. package/build/conductor/epic-server-recipe.js +397 -0
  37. package/build/conductor/epic-server-runtime.js +1207 -0
  38. package/build/conductor/epic-server-sfcc.js +107 -0
  39. package/build/conductor/epic-server-state.js +427 -0
  40. package/build/conductor/epic-server.js +594 -0
  41. package/build/conductor/gate-discovery.js +281 -10
  42. package/build/conductor/gate-settings-review.js +525 -5
  43. package/build/conductor/git-hooks-uninstall.js +112 -0
  44. package/build/conductor/git-hooks.js +81 -15
  45. package/build/conductor/guided-repair.js +10 -1
  46. package/build/conductor/install-doctor.js +2 -4
  47. package/build/conductor/launch-need-probes.js +6 -53
  48. package/build/conductor/launch-plan.js +318 -121
  49. package/build/conductor/local-merge.js +12 -1
  50. package/build/conductor/merge-execution-contract.js +1 -1
  51. package/build/conductor/merged-child-check.js +2 -1
  52. package/build/conductor/prerequisite-inputs.js +1213 -0
  53. package/build/conductor/readiness-assessment.js +93 -0
  54. package/build/conductor/readiness-cli.js +30 -9
  55. package/build/conductor/readiness-prerequisites.js +77 -5
  56. package/build/conductor/readiness-sections.js +146 -126
  57. package/build/conductor/readiness.js +21 -3
  58. package/build/conductor/recovery-cli.js +236 -15
  59. package/build/conductor/recovery-operations.js +34 -4
  60. package/build/conductor/repair-contract.js +33 -0
  61. package/build/conductor/tools.js +36 -4
  62. package/build/conductor/wait-run-attention.js +115 -0
  63. package/build/conductor/wait-run-cli.js +300 -21
  64. package/build/conductor/worker-database-doctor-inspector.js +34 -2
  65. package/build/conductor/worker-setup-tally.js +27 -0
  66. package/build/credential-store.js +96 -21
  67. package/build/dependency-install-declarations.js +212 -0
  68. package/build/docs.generated.js +4 -4
  69. package/build/doctor.js +91 -8
  70. package/build/drive-epic-help.js +23 -4
  71. package/build/drive-epic.js +1841 -340
  72. package/build/engine-paths.js +20 -0
  73. package/build/epic-branch-ci-summary.js +140 -0
  74. package/build/epic-capability-statement.js +107 -0
  75. package/build/epic-ci-workflow-drift.js +30 -15
  76. package/build/epic-ci-workflow-install.js +110 -42
  77. package/build/epic-ci-workflow-v2.js +274 -0
  78. package/build/epic-ci-workflow.js +38 -12
  79. package/build/epic-implementer-bin.js +2184 -52
  80. package/build/epic-launch-context.js +607 -0
  81. package/build/epic-launch-profile.js +1 -1
  82. package/build/epic-server-record.js +273 -0
  83. package/build/epic-shared/bridge-client.js +11 -3
  84. package/build/escalation-reachability.js +43 -0
  85. package/build/executor/ci-fix-test-runs.js +334 -0
  86. package/build/executor/cli.js +2 -0
  87. package/build/executor/completion-contract.js +24 -1
  88. package/build/executor/credentials.js +1 -1
  89. package/build/executor/delivery-observations.js +113 -0
  90. package/build/executor/delivery-outcome.js +30 -0
  91. package/build/executor/dependency-refresh-runner.js +132 -0
  92. package/build/executor/dependency-refresh.js +530 -0
  93. package/build/executor/deps.js +4 -0
  94. package/build/executor/env.js +119 -17
  95. package/build/executor/epic-server-context.js +141 -0
  96. package/build/executor/gate-qualification-job.js +127 -26
  97. package/build/executor/harness-coverage.js +19 -0
  98. package/build/executor/http-client.js +87 -6
  99. package/build/executor/index.js +2 -1
  100. package/build/executor/install-preflight.js +1 -1
  101. package/build/executor/job-runner.js +659 -87
  102. package/build/executor/likely-tests.js +94 -97
  103. package/build/executor/observation.js +8 -0
  104. package/build/executor/pre-pr-checks.js +363 -0
  105. package/build/executor/prompt-spec.js +127 -5
  106. package/build/executor/reconcile-overlap-job.js +5 -3
  107. package/build/executor/required-verification.js +28 -0
  108. package/build/executor/required-work-diagnostics.js +32 -0
  109. package/build/executor/required-work-ledger.js +955 -39
  110. package/build/executor/results.js +100 -0
  111. package/build/executor/service-discovery.js +42 -0
  112. package/build/executor/service-lifecycle.js +109 -2
  113. package/build/executor/service-uninstall.js +205 -0
  114. package/build/executor/service-unit.js +5 -3
  115. package/build/executor/terminal-mutation.js +8 -0
  116. package/build/executor/test-clock.js +3 -0
  117. package/build/executor/verification-hints.js +179 -0
  118. package/build/executor/worker-command.js +126 -5
  119. package/build/executor/worker-database-config.js +281 -0
  120. package/build/executor/worker-database-target.js +16 -12
  121. package/build/executor/worker-database.js +575 -116
  122. package/build/executor/worker-finalization.js +71 -10
  123. package/build/executor/worker-guard-hook.js +10 -2
  124. package/build/executor/worker-resources.js +25 -3
  125. package/build/executor/worker-verification-guidance.js +55 -5
  126. package/build/executor/worktree-inspection.js +42 -0
  127. package/build/executor/worktree.js +20 -1
  128. package/build/executor-launch.js +54 -2
  129. package/build/generated-assets-catalog.js +2 -2
  130. package/build/github-sign-in.js +1 -1
  131. package/build/index.js +1111 -381
  132. package/build/init.js +29 -14
  133. package/build/install-bridge-conductor-answers.js +465 -0
  134. package/build/install-bridge-conductor-apply.js +259 -0
  135. package/build/install-bridge-conductor-assessment.js +895 -0
  136. package/build/install-bridge-conductor-compact.js +161 -0
  137. package/build/install-bridge-conductor-contracts.js +63 -0
  138. package/build/install-bridge-conductor-gate.js +1383 -188
  139. package/build/install-bridge-conductor-setup.js +637 -92
  140. package/build/install-bridge-conductor.js +3163 -740
  141. package/build/install-bridge.js +601 -128
  142. package/build/install-maintenance.js +127 -0
  143. package/build/install-reexec.js +1 -1
  144. package/build/launcher-config-inspection.js +162 -1
  145. package/build/local-cleanup.js +59 -0
  146. package/build/mcp-grant-candidates.js +767 -0
  147. package/build/mcp-grant-doctor.js +63 -0
  148. package/build/mcp-grant.js +288 -0
  149. package/build/mcp-host-targets.js +8 -0
  150. package/build/mcp-identity.js +23 -0
  151. package/build/mcp-install-state.js +68 -2
  152. package/build/mcp-invoke.js +126 -13
  153. package/build/mcp-presets.js +107 -0
  154. package/build/mcp-provisioning.js +134 -2
  155. package/build/mcp-registration-doctor.js +31 -2
  156. package/build/mcp-server-invocation.js +6 -1
  157. package/build/merge-workflow-install.js +236 -111
  158. package/build/package-script-semantics.js +10 -0
  159. package/build/pipeline-utils.js +1 -1
  160. package/build/pipelines.generated.js +6 -6
  161. package/build/plan-discovery.js +97 -4
  162. package/build/plan-execution-ledger.js +248 -9
  163. package/build/plan-generation-io.js +262 -0
  164. package/build/plan-generation.js +424 -0
  165. package/build/plane/build-freshness.js +6 -1
  166. package/build/plane/cli.js +75 -9
  167. package/build/plane/plane-freshness.js +23 -7
  168. package/build/plane/preflight.js +50 -10
  169. package/build/plane/shutdown.js +19 -0
  170. package/build/plane/terminal-run-cleanup.js +23 -5
  171. package/build/plane/terminal-run-status.js +4 -0
  172. package/build/printed-remedy-commands.js +229 -15
  173. package/build/readiness-check.js +1 -1
  174. package/build/readiness-prerequisites.generated.js +311 -4
  175. package/build/readme.generated.js +1 -1
  176. package/build/repository-state-paths.js +38 -0
  177. package/build/repository-toolchains.js +50 -8
  178. package/build/run-unit-tests-launcher.js +8 -1
  179. package/build/setup-change-policy.js +32 -0
  180. package/build/setup-epic.js +1192 -198
  181. package/build/setup-progress.js +44 -0
  182. package/build/setup-question.js +180 -0
  183. package/build/start-tickets.js +4 -0
  184. package/build/test-command-detection.js +32 -88
  185. package/build/test-support/notes-fs.js +40 -0
  186. package/build/third-party-mcp-targets.js +30 -0
  187. package/build/ticket-delivery-enrollment.js +30 -8
  188. package/build/ticket-delivery-run-scope.js +128 -0
  189. package/build/upgrade-cli.js +85 -2
  190. package/build/v2-readiness-requirements.js +28 -1
  191. package/build/version.generated.js +3 -3
  192. package/build/worker-guard-hook-bin.js +2 -2
  193. package/build/worktrees.js +105 -0
  194. package/docs/EPIC-IMPLEMENTER-INSTALL.md +9 -1
  195. package/docs/EPIC-IMPLEMENTER.md +5 -0
  196. package/docs/epic-implementer/getting-started.md +42 -15
  197. package/docs/epic-implementer/self-hosting.md +14 -1
  198. package/package.json +2 -2
@@ -0,0 +1,324 @@
1
+ /**
2
+ * Process ownership for the epic-server helper (BAPI-1546).
3
+ *
4
+ * The helper and every recipe descendant carry the helper's OWN instance tag
5
+ * (`BRIDGE_EPIC_SERVER_INSTANCE_TAG`), never the executor's job tag, so the
6
+ * executor's containment sweep never claims them and the helper never claims an
7
+ * executor job's process.
8
+ *
9
+ * A signal is sent only to a process whose pid, normalized `ps lstart` start
10
+ * time, AND instance tag all match what the helper recorded, re-checked
11
+ * immediately before each signal. A mismatch, or a check that cannot be read,
12
+ * refuses: nothing is ever signalled by port, by name, or by pid alone, and
13
+ * nothing listening on `:8000` is ever touched. A platform that cannot inspect a
14
+ * process's environment makes the epic server unavailable rather than weakening
15
+ * the check.
16
+ *
17
+ * Every recipe subprocess runs with a REPLACING environment built here: the
18
+ * minimal runtime inputs, the recipe's named literal inputs, its resolved
19
+ * credential references, `PORT`, the ownership tag, and the helper-owned
20
+ * per-turn database settings. Ambient `DATABASE_URL`, `PG*`, `POSTGRES_*`, the
21
+ * owner's dotenv values, and unrelated credentials never pass through.
22
+ */
23
+ import { spawn as nodeSpawn, execFile } from "node:child_process";
24
+ import { readFile } from "node:fs/promises";
25
+ import { EPIC_SERVER_INSTANCE_TAG_ENV, isInstanceNonce } from "./epic-server-state.js";
26
+ const PS_TIMEOUT_MS = 5_000;
27
+ const LSTART_RE = /^[A-Z][a-z]{2}\s+[A-Z][a-z]{2}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}\s+\d{4}$/;
28
+ const MAX_ENV_LISTING_CHARS = 1024 * 1024;
29
+ /** Captured stdout per recipe command (only the seed's is ever parsed). */
30
+ export const EPIC_SERVER_MAX_CAPTURED_OUTPUT = 64 * 1024;
31
+ /** The runtime keys copied from the helper's own environment, and nothing else. */
32
+ export const EPIC_SERVER_RUNTIME_ENV_KEYS = [
33
+ "PATH",
34
+ "HOME",
35
+ "USER",
36
+ "LOGNAME",
37
+ "TMPDIR",
38
+ "LANG",
39
+ "LC_ALL",
40
+ ];
41
+ /** The production `ps` runner: bounded, shell-free. */
42
+ export const defaultCommandRunner = (file, args, options) => new Promise((resolve) => {
43
+ execFile(file, args, { timeout: options.timeoutMs, maxBuffer: MAX_ENV_LISTING_CHARS }, (err, stdout) => {
44
+ const code = err && typeof err.code === "number" ? (err.code) : err ? null : 0;
45
+ resolve({ exitCode: code, stdout: typeof stdout === "string" ? stdout : String(stdout ?? "") });
46
+ });
47
+ });
48
+ export function createDefaultEpicServerProcessDeps() {
49
+ return {
50
+ platform: process.platform,
51
+ selfPid: process.pid,
52
+ runCommand: defaultCommandRunner,
53
+ readProcEnviron: (pid) => readFile(`/proc/${pid}/environ`, "latin1"),
54
+ killGroup: (pgid, sig) => {
55
+ if (!Number.isSafeInteger(pgid) || pgid <= 1)
56
+ return "denied";
57
+ try {
58
+ process.kill(-pgid, sig);
59
+ return "sent";
60
+ }
61
+ catch (err) {
62
+ return err.code === "ESRCH" ? "gone" : "denied";
63
+ }
64
+ },
65
+ sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
66
+ };
67
+ }
68
+ /** True when this platform can read both a start time and an environment tag. */
69
+ export function isOwnershipInspectionSupported(platform) {
70
+ return platform === "darwin" || platform === "linux";
71
+ }
72
+ function normalizeLstart(raw) {
73
+ return raw.trim().replace(/\s+/g, " ");
74
+ }
75
+ /** The live, normalized start time of `pid`, or null when gone or unreadable. */
76
+ export async function readProcessStartTime(pid, deps) {
77
+ if (!Number.isSafeInteger(pid) || pid <= 1)
78
+ return null;
79
+ if (!isOwnershipInspectionSupported(deps.platform))
80
+ return null;
81
+ try {
82
+ const res = await deps.runCommand("ps", ["-o", "lstart=", "-p", String(pid)], { timeoutMs: PS_TIMEOUT_MS });
83
+ if (res.exitCode !== 0)
84
+ return null;
85
+ const startedAt = normalizeLstart(res.stdout);
86
+ return LSTART_RE.test(startedAt) ? startedAt : null;
87
+ }
88
+ catch {
89
+ return null;
90
+ }
91
+ }
92
+ const TAG_RE = new RegExp(`(?:^|\\s|\\0)${EPIC_SERVER_INSTANCE_TAG_ENV}=([0-9a-f]{64})(?=\\s|\\0|$)`, "g");
93
+ /** The LAST well-formed instance tag in an environment listing, or null. */
94
+ export function extractInstanceTag(listing) {
95
+ let last = null;
96
+ TAG_RE.lastIndex = 0;
97
+ for (let m = TAG_RE.exec(listing); m !== null; m = TAG_RE.exec(listing))
98
+ last = m[1];
99
+ return last;
100
+ }
101
+ /** The instance tag `pid` carries, or null when it carries none or cannot be read. */
102
+ export async function readProcessInstanceTag(pid, deps) {
103
+ if (!Number.isSafeInteger(pid) || pid <= 1)
104
+ return null;
105
+ try {
106
+ if (deps.platform === "darwin") {
107
+ const res = await deps.runCommand("ps", ["-E", "-ww", "-o", "command=", "-p", String(pid)], { timeoutMs: PS_TIMEOUT_MS });
108
+ if (res.exitCode !== 0 || res.stdout.length > MAX_ENV_LISTING_CHARS)
109
+ return null;
110
+ return extractInstanceTag(res.stdout);
111
+ }
112
+ if (deps.platform === "linux") {
113
+ const environ = await deps.readProcEnviron(pid);
114
+ if (environ.length > MAX_ENV_LISTING_CHARS)
115
+ return null;
116
+ return extractInstanceTag(environ);
117
+ }
118
+ }
119
+ catch {
120
+ return null;
121
+ }
122
+ return null;
123
+ }
124
+ /**
125
+ * Verify that `expected` is still the process the helper started: same pid, same
126
+ * start time, and the instance tag equal to `nonce`. `gone` means the pid no
127
+ * longer exists. A pid reused by a process with another start time is
128
+ * `mismatch`: it is never signalled, and a caller that only needs to know the
129
+ * owned process is gone (recovery) treats it as gone itself.
130
+ */
131
+ export async function verifyOwnedProcess(expected, nonce, deps) {
132
+ if (!isInstanceNonce(nonce))
133
+ return "unreadable";
134
+ if (!isOwnershipInspectionSupported(deps.platform))
135
+ return "unreadable";
136
+ if (expected.pid === deps.selfPid)
137
+ return "mismatch";
138
+ const startedAt = await readProcessStartTime(expected.pid, deps);
139
+ if (startedAt === null)
140
+ return "gone";
141
+ if (startedAt !== expected.started_at)
142
+ return "mismatch";
143
+ const tag = await readProcessInstanceTag(expected.pid, deps);
144
+ if (tag === null)
145
+ return "unreadable";
146
+ return tag === nonce ? "match" : "mismatch";
147
+ }
148
+ /**
149
+ * Stop an owned process GROUP (the leader's pid is the pgid): verify, SIGTERM,
150
+ * wait up to `graceMs`, re-verify, SIGKILL. Every signal is preceded by a fresh
151
+ * ownership check; any mismatch or unreadable check refuses without signalling.
152
+ */
153
+ export async function stopOwnedProcessGroup(expected, nonce, graceMs, deps) {
154
+ const first = await verifyOwnedProcess(expected, nonce, deps);
155
+ if (first === "gone")
156
+ return "already_gone";
157
+ if (first !== "match")
158
+ return "refused";
159
+ const sent = deps.killGroup(expected.pid, "SIGTERM");
160
+ if (sent === "gone")
161
+ return "already_gone";
162
+ if (sent === "denied")
163
+ return "refused";
164
+ const deadline = Date.now() + Math.max(0, graceMs);
165
+ while (Date.now() < deadline) {
166
+ await deps.sleep(Math.min(250, Math.max(1, deadline - Date.now())));
167
+ const now = await readProcessStartTime(expected.pid, deps);
168
+ if (now === null || now !== expected.started_at)
169
+ return "stopped";
170
+ }
171
+ const again = await verifyOwnedProcess(expected, nonce, deps);
172
+ if (again === "gone")
173
+ return "stopped";
174
+ if (again !== "match")
175
+ return "refused";
176
+ const killed = deps.killGroup(expected.pid, "SIGKILL");
177
+ return killed === "denied" ? "refused" : "stopped";
178
+ }
179
+ /**
180
+ * Build the replacing environment for one recipe subprocess. Nothing from the
181
+ * helper's environment passes except the minimal runtime keys; the database
182
+ * settings, when present, name only the helper's current per-turn database.
183
+ */
184
+ export function buildEpicServerCommandEnv(input) {
185
+ const env = {};
186
+ for (const key of EPIC_SERVER_RUNTIME_ENV_KEYS) {
187
+ const value = input.helperEnv[key];
188
+ if (typeof value === "string" && value.length > 0)
189
+ env[key] = value;
190
+ }
191
+ for (const [name, value] of Object.entries(input.inputs))
192
+ env[name] = value;
193
+ for (const [name, value] of Object.entries(input.credentials))
194
+ env[name] = value;
195
+ env.PORT = String(input.port);
196
+ env[EPIC_SERVER_INSTANCE_TAG_ENV] = input.nonce;
197
+ if (input.database !== null) {
198
+ const db = input.database;
199
+ env.DATABASE_URL = `postgresql://${encodeURIComponent(db.role)}@${db.host}:${db.port}/${encodeURIComponent(db.name)}`;
200
+ env.PGHOST = db.host;
201
+ env.PGPORT = db.port;
202
+ env.PGUSER = db.role;
203
+ env.PGDATABASE = db.name;
204
+ env.POSTGRES_HOST = db.host;
205
+ env.POSTGRES_PORT = db.port;
206
+ env.POSTGRES_USER = db.role;
207
+ env.POSTGRES_DB = db.name;
208
+ env.POSTGRES_DATABASE = db.name;
209
+ }
210
+ return env;
211
+ }
212
+ /**
213
+ * Run one bounded recipe command in its own process group, with the replacing
214
+ * environment. On timeout or cancellation the whole group is killed (the helper
215
+ * holds the live child handle, so this group is its own by construction).
216
+ * Stderr is discarded; stdout is captured only up to the bound.
217
+ */
218
+ export function runRecipeCommand(argv, options, spawnFn = nodeSpawn) {
219
+ return new Promise((resolve) => {
220
+ // An already-aborted signal never fires `abort`: refuse before spawning.
221
+ if (options.signal?.aborted) {
222
+ resolve({ kind: "cancelled", exitCode: null, stdout: "" });
223
+ return;
224
+ }
225
+ let child;
226
+ try {
227
+ child = spawnFn(argv[0], argv.slice(1), {
228
+ cwd: options.cwd,
229
+ env: options.env,
230
+ detached: true,
231
+ stdio: ["ignore", options.captureStdout ? "pipe" : "ignore", "ignore"],
232
+ });
233
+ }
234
+ catch {
235
+ resolve({ kind: "spawn_failed", exitCode: null, stdout: "" });
236
+ return;
237
+ }
238
+ let stdout = "";
239
+ let settled = false;
240
+ let timedOut = false;
241
+ let cancelled = false;
242
+ const killGroup = () => {
243
+ if (typeof child.pid === "number" && child.pid > 1) {
244
+ try {
245
+ process.kill(-child.pid, "SIGKILL");
246
+ }
247
+ catch {
248
+ /* already gone */
249
+ }
250
+ }
251
+ };
252
+ const timer = setTimeout(() => {
253
+ timedOut = true;
254
+ killGroup();
255
+ }, Math.max(1, options.timeoutMs));
256
+ const onAbort = () => {
257
+ cancelled = true;
258
+ killGroup();
259
+ };
260
+ options.signal?.addEventListener("abort", onAbort, { once: true });
261
+ child.stdout?.on("data", (chunk) => {
262
+ if (stdout.length < EPIC_SERVER_MAX_CAPTURED_OUTPUT)
263
+ stdout += chunk.toString("utf8").slice(0, EPIC_SERVER_MAX_CAPTURED_OUTPUT - stdout.length);
264
+ });
265
+ const finish = (outcome) => {
266
+ if (settled)
267
+ return;
268
+ settled = true;
269
+ clearTimeout(timer);
270
+ options.signal?.removeEventListener("abort", onAbort);
271
+ resolve(outcome);
272
+ };
273
+ child.on("error", () => finish({ kind: "spawn_failed", exitCode: null, stdout: "" }));
274
+ child.on("close", (code) => {
275
+ if (cancelled)
276
+ finish({ kind: "cancelled", exitCode: code, stdout: "" });
277
+ else if (timedOut)
278
+ finish({ kind: "timeout", exitCode: code, stdout: "" });
279
+ else
280
+ finish({ kind: code === 0 ? "ok" : "failed", exitCode: code, stdout });
281
+ });
282
+ });
283
+ }
284
+ /** Start the app detached in its own process group. Output is discarded (it may carry secrets). */
285
+ export function startAppProcess(argv, options, spawnFn = nodeSpawn) {
286
+ let child;
287
+ try {
288
+ child = spawnFn(argv[0], argv.slice(1), { cwd: options.cwd, env: options.env, detached: true, stdio: ["ignore", "ignore", "ignore"] });
289
+ }
290
+ catch {
291
+ return null;
292
+ }
293
+ if (typeof child.pid !== "number" || child.pid <= 1)
294
+ return null;
295
+ child.on("error", () => undefined);
296
+ return { pid: child.pid, child };
297
+ }
298
+ /**
299
+ * Parse bounded seed output into the declared `KEY=VALUE` pairs. Lines that are
300
+ * not a declared key are ignored; every declared key must appear exactly once
301
+ * with a non-empty single-line value. Null when the output does not satisfy that.
302
+ */
303
+ export function parseSeedOutput(stdout, outputs) {
304
+ const found = {};
305
+ for (const raw of stdout.split(/\r?\n/)) {
306
+ const eq = raw.indexOf("=");
307
+ if (eq <= 0)
308
+ continue;
309
+ const key = raw.slice(0, eq).trim();
310
+ if (!outputs.includes(key))
311
+ continue;
312
+ const value = raw.slice(eq + 1);
313
+ if (value.length === 0 || value.length > 1024 || /[\0\r\n]/.test(value))
314
+ return null;
315
+ if (Object.prototype.hasOwnProperty.call(found, key))
316
+ return null;
317
+ found[key] = value;
318
+ }
319
+ return outputs.every((k) => Object.prototype.hasOwnProperty.call(found, k)) ? found : null;
320
+ }
321
+ /** Serialize a login file's content (`KEY=VALUE` lines, declared order). */
322
+ export function formatLoginFile(values, outputs) {
323
+ return `${outputs.map((k) => `${k}=${values[k]}`).join("\n")}\n`;
324
+ }