@codeyam-editor/codeyam-editor 0.1.0-staging.01efa1a

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 (56) hide show
  1. package/npm/cli.js +19 -0
  2. package/npm/cloud-branch-switch.js +442 -0
  3. package/npm/cloud.js +2184 -0
  4. package/npm/container.js +54 -0
  5. package/npm/editor-dev.js +459 -0
  6. package/npm/editor.js +272 -0
  7. package/npm/empty-project-dir.js +33 -0
  8. package/npm/fleet-auth.js +111 -0
  9. package/npm/fleet-branch.js +49 -0
  10. package/npm/fleet-broker-death.js +44 -0
  11. package/npm/fleet-broker-debounce.js +63 -0
  12. package/npm/fleet-broker-status.js +45 -0
  13. package/npm/fleet-build-data.js +81 -0
  14. package/npm/fleet-cleanup.js +297 -0
  15. package/npm/fleet-effect-status.js +179 -0
  16. package/npm/fleet-env.js +125 -0
  17. package/npm/fleet-grouping.js +56 -0
  18. package/npm/fleet-job-badge.js +83 -0
  19. package/npm/fleet-memory.js +157 -0
  20. package/npm/fleet-plans.js +96 -0
  21. package/npm/fleet-pool-policy.js +78 -0
  22. package/npm/fleet-pool.js +401 -0
  23. package/npm/fleet-queue.js +40 -0
  24. package/npm/fleet-reconcile.js +93 -0
  25. package/npm/fleet-reset.js +222 -0
  26. package/npm/fleet-roster.js +81 -0
  27. package/npm/launch-config.js +717 -0
  28. package/npm/open.js +15 -0
  29. package/npm/preflight-guidance.js +77 -0
  30. package/npm/scenario-check.js +604 -0
  31. package/npm/scenario-handlers.js +82 -0
  32. package/npm/scenario-interactivity.js +245 -0
  33. package/npm/scenario-issues.js +35 -0
  34. package/npm/scenario-metrics.js +108 -0
  35. package/npm/scenario-mocks.js +107 -0
  36. package/npm/scenario-playwright.js +534 -0
  37. package/npm/utils.js +823 -0
  38. package/npm/vm-urls.js +117 -0
  39. package/package.json +32 -0
  40. package/ui/dist/assets/ContainerInspector-C8EkH4PP.js +2 -0
  41. package/ui/dist/assets/EditorShell-By-2rl7D.js +187 -0
  42. package/ui/dist/assets/Markdown-BHBqz-Mj.js +1 -0
  43. package/ui/dist/assets/ScenarioDataPanel-Cg8zOFNZ.js +5 -0
  44. package/ui/dist/assets/ScenarioDataPanel-DYP7pi_n.css +32 -0
  45. package/ui/dist/assets/ScenarioDataPanelFullPage-WOUYbba7.js +1 -0
  46. package/ui/dist/assets/ScenarioRenderer-CBTlIVPs.js +1 -0
  47. package/ui/dist/assets/StepsSimulator-CRtmlyNZ.js +1 -0
  48. package/ui/dist/assets/index-DzoF9RKw.js +27 -0
  49. package/ui/dist/assets/index-ckz94rKI.css +1 -0
  50. package/ui/dist/assets/markdown-v0F0UEt3.js +37 -0
  51. package/ui/dist/assets/react-CSS0HapR.js +28 -0
  52. package/ui/dist/assets/useEvents-DggWrtHe.js +1 -0
  53. package/ui/dist/assets/xterm--24IGk-x.js +9 -0
  54. package/ui/dist/favicon.ico +0 -0
  55. package/ui/dist/favicon.png +0 -0
  56. package/ui/dist/index.html +16 -0
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ // Assemble the per-VM wire object the dashboard's /data response carries to the
4
+ // browser. This is the single source of truth for a card's transport shape: the
5
+ // card UI (scripts/operator/fleet-dashboard/public/index.html) reads `v.<field>`
6
+ // off each entry, so every field the card renders MUST be emitted here.
7
+ //
8
+ // It was split out of server.js's buildData() so the shape is unit-testable.
9
+ // The regression that motivated the split: `currentProvider` (the VM's
10
+ // configured build agent) was merged onto the poll state AND read by the card to
11
+ // render the 🤖 agent badge, but it was silently missing from this envelope — so
12
+ // it lived in server memory and never reached the browser, and the badge never
13
+ // appeared. A hand-maintained object literal inside a 197KB monolith has no
14
+ // guard against that drift; a pure function does.
15
+ //
16
+ // `v` is the merged poll state (state.vms[n]). `ctx` carries the derived,
17
+ // cross-VM, and side-effecting values buildData computes per VM (status,
18
+ // debounced broker tri-state, reconciled job, queue position, launch cfg, …) —
19
+ // the things this pure function can't know on its own.
20
+ function buildVmEnvelope(v, ctx) {
21
+ return {
22
+ n: ctx.n,
23
+ url: v.url || null,
24
+ reachable: !!v.reachable,
25
+ hasSession: !!v.hasSession,
26
+ // Three-state: true = broker confirmed up, false = confirmed down, null =
27
+ // editor unreachable so broker state is unknown. Never conflate "down" with
28
+ // "unreachable" — that mismatch is what made the card show "broker down"
29
+ // while a direct pgrep proved the broker was up.
30
+ brokerLive: v.reachable ? !!v.agentBrokerLive : null,
31
+ // Broker tri-state: 'down' | 'idle' | 'agent-live' | 'unknown' (derived
32
+ // upstream, debounced). Richer than brokerLive's is-an-agent-live boolean.
33
+ brokerStatus: ctx.brokerStatus,
34
+ awaitingInput: !!v.awaitingUserInput,
35
+ feature: v.feature || null,
36
+ step: v.step != null ? v.step : null,
37
+ mode: v.mode || null,
38
+ label: v.label || null,
39
+ status: ctx.status,
40
+ orphan: ctx.orphan || null,
41
+ degradedReasons: ctx.degradedReasons || [],
42
+ isHead: !!ctx.isHead,
43
+ // True when this VM holds the queue head but is unreachable (operator
44
+ // decides manually whether to release it).
45
+ headHolderUnreachable: !!ctx.headHolderUnreachable,
46
+ queuePos: ctx.queuePos != null ? ctx.queuePos : null,
47
+ queueLen: ctx.queueLen,
48
+ job: ctx.job || null,
49
+ config: ctx.config || null,
50
+ project: ctx.project != null ? ctx.project : null,
51
+ variant: ctx.variant != null ? ctx.variant : null,
52
+ cardIdentity: ctx.cardIdentity,
53
+ brokerHistory: Array.isArray(v.brokerHistory) ? v.brokerHistory : [],
54
+ brokerDeath: ctx.brokerDeath,
55
+ // Auth state + reason + token expiry. Only surfaced when the editor is
56
+ // reachable (an unreachable VM's cached auth is stale and misleading).
57
+ providerAuth: v.reachable && v.providerAuth ? v.providerAuth : null,
58
+ effects: v.effects || null,
59
+ // The VM's live-configured build agent (lowercase provider), reported by
60
+ // /api/session-info and merged (sticky) onto the poll state. Drives the
61
+ // card's 🤖 agent badge. null for an older VM whose editor predates the
62
+ // field (the card omits the badge rather than rendering a blank chip).
63
+ currentProvider: v.currentProvider || null,
64
+ };
65
+ }
66
+
67
+ // The fields the card UI depends on being present in every /data VM entry. This
68
+ // is the contract `buildVmEnvelope` must satisfy: the regression that prompted
69
+ // this module was a field the card read but the envelope didn't emit, so the
70
+ // test asserts the envelope keys are a superset of this list. Adding a field the
71
+ // card renders means adding it here AND in buildVmEnvelope — the test fails loud
72
+ // if you do one without the other.
73
+ const REQUIRED_CARD_FIELDS = [
74
+ 'n', 'url', 'reachable', 'hasSession', 'brokerLive', 'brokerStatus',
75
+ 'awaitingInput', 'feature', 'step', 'mode', 'label', 'status', 'orphan',
76
+ 'degradedReasons', 'isHead', 'headHolderUnreachable', 'queuePos', 'queueLen',
77
+ 'job', 'config', 'project', 'variant', 'cardIdentity', 'brokerHistory',
78
+ 'brokerDeath', 'providerAuth', 'effects', 'currentProvider',
79
+ ];
80
+
81
+ module.exports = { buildVmEnvelope, REQUIRED_CARD_FIELDS };
@@ -0,0 +1,297 @@
1
+ 'use strict';
2
+ // Pure-ish helpers for the fleet dashboard's pending-job sidecar feature
3
+ // (scripts/operator/fleet-dashboard/server.js requires this). Sidecar
4
+ // serialization, regex parsing of filenames, gcloud-describe error
5
+ // classification, and the reap decision matrix all live here so the
6
+ // `server.js` orchestration code can be left thin and these pieces can be
7
+ // exercised in isolation (npm/fleet-cleanup.test.js).
8
+ //
9
+ // File-touching helpers take the directory as a PARAMETER so tests pass a
10
+ // throwaway tmpdir; the dashboard passes its fixed `PENDING_JOBS_DIR` constant.
11
+
12
+ const fs = require('fs');
13
+ const path = require('path');
14
+
15
+ // Sidecar filename grammar: `<action>-<n>.json` where action ∈ {add, destroy}.
16
+ // Returns { action, n } on a valid match, null otherwise. Hoisted out of the
17
+ // inline regex so the parser is unit-testable on its own (junk filenames, tmp
18
+ // turds, missing numbers).
19
+ function parsePendingJobFilename(name) {
20
+ const m = String(name == null ? '' : name).match(/^(add|destroy)-(\d+)\.json$/);
21
+ if (!m) return null;
22
+ return { action: m[1], n: Number(m[2]) };
23
+ }
24
+
25
+ // Atomically write a sidecar marking VM-n's <action> in flight. Best-effort:
26
+ // any I/O failure is logged, never thrown (the dashboard mustn't crash because
27
+ // .codeyam/logs is missing or read-only).
28
+ function persistPendingJob(dir, n, action, meta) {
29
+ try {
30
+ fs.mkdirSync(dir, { recursive: true });
31
+ const f = path.join(dir, `${action}-${n}.json`);
32
+ const tmp = f + '.tmp';
33
+ fs.writeFileSync(tmp, JSON.stringify({ n, action, startedAt: new Date().toISOString(), ...(meta || {}) }));
34
+ fs.renameSync(tmp, f);
35
+ } catch (e) { console.error('persistPendingJob:', e && e.message); }
36
+ }
37
+
38
+ // Remove a sidecar; a missing file is the already-cleared path, not an error.
39
+ function clearPendingJob(dir, n, action) {
40
+ try { fs.unlinkSync(path.join(dir, `${action}-${n}.json`)); }
41
+ catch (_) { /* missing is fine */ }
42
+ }
43
+
44
+ // Enumerate sidecars under `dir`. Returns one entry per RECOGNISED file —
45
+ // junk filenames are dropped silently so an operator's debug touch under
46
+ // pending-jobs/ can't crash the reap. Unparseable JSON meta degrades to `{}`
47
+ // (the sidecar's existence is what matters; meta is best-effort context).
48
+ function listPendingJobs(dir) {
49
+ let entries;
50
+ try { entries = fs.readdirSync(dir); } catch { return []; }
51
+ const out = [];
52
+ for (const name of entries) {
53
+ const parsed = parsePendingJobFilename(name);
54
+ if (!parsed) continue;
55
+ let meta = {};
56
+ try { meta = JSON.parse(fs.readFileSync(path.join(dir, name), 'utf8')) || {}; }
57
+ catch { /* unparseable — treat as bare {action, n} */ }
58
+ out.push({ n: parsed.n, action: parsed.action, meta });
59
+ }
60
+ return out;
61
+ }
62
+
63
+ // Classify the outcome of a `gcloud compute instances describe` exec callback.
64
+ // - err is null/undefined → instance exists (`true`)
65
+ // - stderr/err.message names "NOT_FOUND" / "was not found" / "not found"
66
+ // → instance is gone (`false`)
67
+ // - anything else (auth glitch, network blip, timeout) → uncertain (`null`)
68
+ // The `null` case is what makes the reap fail-soft: an uncertain probe leaves
69
+ // the sidecar in place so the next dashboard boot retries instead of clearing
70
+ // state on a flaky gcloud.
71
+ function classifyDescribeError(err, stderr) {
72
+ if (!err) return true;
73
+ const out = String(stderr == null ? '' : stderr) + ' ' + String((err && err.message) || '');
74
+ if (/NOT_FOUND|was not found|not found/i.test(out)) return false;
75
+ return null;
76
+ }
77
+
78
+ // Decide what the startup reap should do for one sidecar. Pure: takes the
79
+ // decoded sidecar + GCP-existence verdict + (for adds) container-up verdict +
80
+ // (for up adds) project-wiring verdict, returns a tag the caller dispatches on.
81
+ // verdict when dispatch
82
+ // 'cleanup' destroy gone, OR add gone runDestroyCleanup / runAddCleanup
83
+ // 'reconcile-add' add up, container up, project wired (or unknown) ensure roster + recordLaunch, then clear
84
+ // 'half-provisioned' add up, container up, but project NOT wired surface needs-recovery card (finish-wire from cfg)
85
+ // 'recover-bootstrap' add instance up but container missing/down re-run cloud:up to finish the half-done bootstrap
86
+ // 'clear-only' destroy sidecar but instance still exists just clear the sidecar
87
+ // 'leave' gcloud uncertain (exists === null) leave for the next boot
88
+ // The (exists, containerUp) split distinguishes a finished add from a container-
89
+ // down half-provisioned one: a dashboard SIGKILL'd mid-`cloud:up` leaves a GCP
90
+ // instance with no docker/container, which the old (exists-only) matrix wrongly
91
+ // classified 'reconcile-add' — adding an unreachable VM to the roster. The
92
+ // `projectWired` axis closes the remaining gap (improve39 VM-7): an instance +
93
+ // container that ARE up but whose `/workspace` is unwired (no git remote, empty
94
+ // manifest) is NOT a finished add — it's half-provisioned and must surface a
95
+ // recoverable card, not be silently reconciled "done". Only a POSITIVE unwired
96
+ // signal (`projectWired === false`) downgrades to 'half-provisioned'; an
97
+ // unknown/unprobed wiring (`null`/`undefined`) stays 'reconcile-add' so a flaky
98
+ // probe can't strand a healthy VM. `containerUp`/`projectWired` are only consulted
99
+ // for an add whose instance exists; destroy and the instance-gone add path ignore
100
+ // them. Splitting the matrix out lets the table be exhaustively tested without any
101
+ // of the side-effects in server.js.
102
+ function reapVerdict({ action, exists, containerUp, projectWired }) {
103
+ if (exists === null) return 'leave';
104
+ if (action === 'destroy') return exists ? 'clear-only' : 'cleanup';
105
+ if (action === 'add') {
106
+ if (!exists) return 'cleanup';
107
+ if (!containerUp) return 'recover-bootstrap';
108
+ return projectWired === false ? 'half-provisioned' : 'reconcile-add';
109
+ }
110
+ return 'leave';
111
+ }
112
+
113
+ // Classify whether a VM's editor `/workspace` is actually wired to a client
114
+ // project, from a best-effort probe of two signals: the git remote URL and
115
+ // whether `package.json` carries a real `name`. Pure: server.js runs the SSH +
116
+ // `docker exec` probe and hands the raw stdout/err here so the decision is
117
+ // unit-testable without a live VM.
118
+ // - err present (ssh/docker blip, timeout) → null (unknown — do NOT
119
+ // strand a healthy VM on a flaky probe; reapVerdict treats null as wired)
120
+ // - a git remote OR a non-empty package.json name → true (wired)
121
+ // - neither (empty / no-git workspace) → false (half-provisioned —
122
+ // the VM-7 signature: `/workspace` with no remote and an empty manifest)
123
+ // The probe emits the two signals separated by the `---CY---` sentinel so this
124
+ // parser is independent of locale/line-ending noise in the ssh transport.
125
+ function classifyProjectWiring({ stdout, err } = {}) {
126
+ if (err) return null;
127
+ const parts = String(stdout == null ? '' : stdout).split('---CY---').map((s) => s.trim());
128
+ const remote = parts[0] || '';
129
+ const nameCount = Number((parts[1] || '0').trim()) || 0;
130
+ if (remote || nameCount > 0) return true;
131
+ return false;
132
+ }
133
+
134
+ // Durable exit marker for a detached `add` provision (improve40). The add's
135
+ // `cloud:up` shell is spawned in its OWN session (detached) so a dashboard
136
+ // SIGKILL — the 2GB launchd hard cap, exactly the kill that aborted the VM-7
137
+ // add mid-flight — no longer reaches it; the provision keeps running. When that
138
+ // detached shell finally exits it writes `add-<n>.exit` (its trailing exit code
139
+ // + an ISO timestamp) into PENDING_JOBS_DIR, alongside the `add-<n>.json`
140
+ // sidecar. The marker is the GROUND-TRUTH completion signal the next boot reads
141
+ // instead of inferring `in-flight → error` from the lost child: a marker means
142
+ // "the provision finished while the dashboard was away," its code says whether
143
+ // it finished OK. The name is derived here so the shell `trap` (server.js) and
144
+ // the JS reader build the exact same path.
145
+ function exitMarkerName(n) {
146
+ return `add-${n}.exit`;
147
+ }
148
+
149
+ // Parse an exit-marker file body into { exitCode, finishedAt }, or null when the
150
+ // body is missing/blank/malformed. Format is two lines — the numeric exit code
151
+ // then an ISO timestamp — written by the shell trap. A non-integer first line is
152
+ // treated as no-marker (null) so a half-written / corrupt marker falls back to
153
+ // the gcloud reconcile path rather than being read as a bogus exit code. Pure:
154
+ // takes the text so it's unit-testable without touching disk.
155
+ function parseExitMarker(text) {
156
+ if (text == null) return null;
157
+ const lines = String(text).split('\n').map((s) => s.trim()).filter(Boolean);
158
+ if (!lines.length) return null;
159
+ const code = Number(lines[0]);
160
+ if (!Number.isInteger(code)) return null;
161
+ return { exitCode: code, finishedAt: lines[1] || null };
162
+ }
163
+
164
+ // Read VM-n's exit marker from `dir`, or null when it's absent/unreadable/
165
+ // malformed (the still-running and never-written cases both surface as null).
166
+ function readExitMarker(dir, n) {
167
+ try { return parseExitMarker(fs.readFileSync(path.join(dir, exitMarkerName(n)), 'utf8')); }
168
+ catch { return null; }
169
+ }
170
+
171
+ // Remove a consumed exit marker; a missing file is the already-cleared path, not
172
+ // an error (mirrors clearPendingJob).
173
+ function clearExitMarker(dir, n) {
174
+ try { fs.unlinkSync(path.join(dir, exitMarkerName(n))); }
175
+ catch (_) { /* missing is fine */ }
176
+ }
177
+
178
+ // Decide how a snapshot-restored `add` should be re-attached after a dashboard
179
+ // restart, BEFORE any gcloud probe. Pure: takes the durable signals the boot
180
+ // path gathers — whether the exit marker exists, its recorded code, and whether
181
+ // the detached provision child is still alive (recorded pid, `kill -0`).
182
+ // verdict when dispatch
183
+ // 'done' marker present, exit code 0 roster + recordLaunch, clear sidecar/marker
184
+ // 'error' marker present, non-zero exit code surface the real failure, clear sidecar/marker
185
+ // 'running' no marker yet, child still alive RE-ATTACH: keep polling, do NOT mark error
186
+ // 'reconcile' child gone, no usable marker fall through to fc.reapVerdict (gcloud probe)
187
+ // This is the durable fix's brain: a provision that survived the kill (still
188
+ // running, or finished while we were away) is classified from GROUND TRUTH, so
189
+ // it is never blanket-converted `in-flight → error` the way the snapshot-only
190
+ // path did (the improve39 VM-7 false `add-failed`). Only when the child is gone
191
+ // AND left no parseable marker do we fall back to inferring state from gcloud —
192
+ // `reconcile` hands that exact (exists, containerUp, projectWired) decision to
193
+ // fc.reapVerdict. A marker whose first line isn't an integer is treated as
194
+ // absent (parseExitMarker → null) so a corrupt marker reconciles rather than
195
+ // asserting a bogus code. The matrix is exhaustively tested without a live VM.
196
+ function reattachVerdict({ markerPresent, exitCode, childAlive } = {}) {
197
+ if (markerPresent) {
198
+ // Guard the null/undefined coercion BEFORE Number() — `Number(null)` is 0,
199
+ // which would masquerade as a clean exit. A marker with no parseable code
200
+ // (half-written / corrupt) must reconcile, not assert success.
201
+ const code = exitCode == null ? NaN : Number(exitCode);
202
+ if (Number.isInteger(code)) return code === 0 ? 'done' : 'error';
203
+ return 'reconcile'; // marker present but no parseable code — let gcloud decide
204
+ }
205
+ if (childAlive) return 'running';
206
+ return 'reconcile';
207
+ }
208
+
209
+ // Anti-loop budget for the `recover-bootstrap` path. Pure: given the count of
210
+ // recoveries ALREADY persisted on the sidecar, return the next attempt number,
211
+ // whether the reap should proceed (re-run cloud:up) or treat the VM as
212
+ // exhausted (leave the sidecar + surface a needs-manual-recovery card), and the
213
+ // cap. Keeps the off-by-one (prior 3 → attempt 4 → stop) out of server.js so it
214
+ // can be unit-tested without the side-effecting re-run.
215
+ function recoverBootstrapDecision({ recoverAttempts, max = 3 }) {
216
+ const prior = Number(recoverAttempts) || 0;
217
+ const attempt = prior + 1;
218
+ return { attempt, proceed: attempt <= max, max };
219
+ }
220
+
221
+ // improve40 — select the orphaned `add` jobs that must be reconciled from the
222
+ // VM's REAL provision state even though their per-job sidecar is GONE. The
223
+ // durable re-attach path (sidecar + exit marker + recorded pid) cannot fire for
224
+ // these — a restart left only the snapshot record — so the snapshot job's
225
+ // stashed `config` is the sole cfg source and a live gcloud probe is the only
226
+ // ground truth. Pure: takes the restored jobs map + the set of `<action>-<n>`
227
+ // sidecar keys the sidecar reap already owns; returns the [{ n, cfg }] the boot
228
+ // path hands to reconcileViaGcloud. A job is selected when ALL hold:
229
+ // - it is an `add`,
230
+ // - it is flagged `restartOrphaned` (restoreFromSnapshot downgraded a
231
+ // running add → error because the dashboard restarted before it finished —
232
+ // this is the ONLY error state we re-probe; an add that failed IN-PROCESS
233
+ // carries a plain terminal `error` with no flag and stays surfaced), AND
234
+ // - NO `add-<n>` sidecar exists (a sidecar means the sidecar reap already
235
+ // owns it — never double-reconcile).
236
+ // `cfg` rides from the job's `config`; null when the snapshot never carried one
237
+ // (older snapshot) — the caller still probes, but a roster reconcile then can't
238
+ // recordLaunch, exactly mirroring the sidecar path's `meta.cfg`-absent case.
239
+ function orphanedAddsToReconcile({ jobs, sidecarKeys } = {}) {
240
+ const sidecars = sidecarKeys instanceof Set ? sidecarKeys : new Set(sidecarKeys || []);
241
+ const src = (jobs && typeof jobs === 'object') ? jobs : {};
242
+ const out = [];
243
+ for (const [k, j] of Object.entries(src)) {
244
+ if (!j || typeof j !== 'object') continue;
245
+ if (j.action !== 'add') continue;
246
+ if (!j.restartOrphaned) continue;
247
+ if (sidecars.has(`add-${k}`)) continue;
248
+ out.push({ n: Number(k), cfg: j.config || null });
249
+ }
250
+ return out;
251
+ }
252
+
253
+ // improve41 — the destroy-direction mirror of orphanedAddsToReconcile: decide
254
+ // what to do with a VM that is STILL in the roster but whose GCP instance state
255
+ // we have just re-probed. The bug this closes: a Destroy that races a dashboard
256
+ // restart can leave the instance deleted while the startup reap took the
257
+ // `clear-only` path (instance still present at reap time) and dropped the only
258
+ // sidecar — so nothing ever runs roster cleanup and the VM polls forever
259
+ // "unreachable." A sidecar-independent reconcile, keyed on REAL GCP existence
260
+ // rather than on a pending-job sidecar, catches it. Pure: server.js runs the
261
+ // gcloud describe (→ exists, via classifyDescribeError) and reads session /
262
+ // pending-add state, and hands the three facts here so the matrix is testable
263
+ // without a live VM.
264
+ // verdict when dispatch
265
+ // 'cleanup' instance gone AND the VM is idle runDestroyCleanup
266
+ // 'leave' exists uncertain (null), OR the VM is busy do nothing this pass
267
+ // 'keep' instance still exists do nothing (healthy)
268
+ // Fail-safe by construction: cleanup fires ONLY on a DEFINITIVE absence
269
+ // (exists === false) — a flaky/transient describe (null) is always 'leave', so a
270
+ // gcloud blip can never evict a healthy VM (mirroring reapVerdict's `leave`
271
+ // posture). "Busy" (a live session or an in-flight add to this number) also
272
+ // holds the entry for an operator rather than auto-removing — the shouldn't-
273
+ // happen-but-fail-safe case where a vanished instance still reports a session.
274
+ function reconcileRosteredVm({ exists, hasSession, hasPendingAdd } = {}) {
275
+ if (exists === null || exists === undefined) return "leave";
276
+ if (exists) return "keep";
277
+ if (hasSession || hasPendingAdd) return "leave";
278
+ return "cleanup";
279
+ }
280
+
281
+ module.exports = {
282
+ parsePendingJobFilename,
283
+ persistPendingJob,
284
+ clearPendingJob,
285
+ listPendingJobs,
286
+ classifyDescribeError,
287
+ reapVerdict,
288
+ reconcileRosteredVm,
289
+ orphanedAddsToReconcile,
290
+ classifyProjectWiring,
291
+ recoverBootstrapDecision,
292
+ exitMarkerName,
293
+ parseExitMarker,
294
+ readExitMarker,
295
+ clearExitMarker,
296
+ reattachVerdict,
297
+ };
@@ -0,0 +1,179 @@
1
+ 'use strict';
2
+ // Pure predicate module for the Fleet Dashboard's per-button "will clicking
3
+ // this change the VM, or is it already in that state?" indicators
4
+ // (scripts/operator/fleet-dashboard/server.js requires this). Given an
5
+ // already-gathered FACTS object — no I/O, no git/docker exec; server.js does
6
+ // the impure fact-gathering — computeButtonEffects returns a per-button verdict
7
+ // map. Mirrors the pure-module-plus-colocated-vitest-test shape of
8
+ // npm/fleet-reset.js so the whole decision table is unit-testable without
9
+ // spawning a process.
10
+ //
11
+ // Each verdict is { effect, reason }:
12
+ // - 'changes' — clicking does something; `reason` says what.
13
+ // - 'noop' — already in that state; clicking changes nothing.
14
+ // - 'recommended' — soft signal (run-setup only): a reset/update landed after
15
+ // the last successful setup. NEVER a confident green no-op.
16
+ // - 'unknown' — not yet checked, VM unreachable, or a needed fact is
17
+ // missing. We never CLAIM a safe no-op we couldn't verify —
18
+ // silent failure here would be worse than no indicator.
19
+ //
20
+ // The predicate for each button is derived from what that button actually
21
+ // touches (its tooltip in public/index.html is the source of truth): the sync
22
+ // buttons key off a git commit comparison, the reset buttons off
23
+ // working-tree/session/`.codeyam` state, and rebuild off a build-commit
24
+ // comparison.
25
+
26
+ // Short sha for compact reason strings. Tolerates null / short input.
27
+ function short(sha) {
28
+ return sha ? String(sha).slice(0, 7) : '?';
29
+ }
30
+
31
+ // "origin/<branch>" — or a bare "origin" when the branch is unknown.
32
+ function originRef(branch) {
33
+ return branch ? `origin/${branch}` : 'origin';
34
+ }
35
+
36
+ // "N commit(s) behind origin/<branch>" with correct singular/plural.
37
+ function behindReason(behind, branch) {
38
+ return `${behind} commit${behind === 1 ? '' : 's'} behind ${originRef(branch)}`;
39
+ }
40
+
41
+ // Compose a short reason listing which signals make a reset non-trivial, so the
42
+ // operator sees WHAT a Reset would discard rather than a bare "changes". The
43
+ // `agent` (a live build agent in the broker) leads the list — it's the most
44
+ // consequential thing a reset's `pty-broker stop` would silently discard.
45
+ function resetReason({ agent, session, dirty, behind, codeyam }) {
46
+ const parts = [];
47
+ if (agent) parts.push('running build agent');
48
+ if (dirty) parts.push('uncommitted changes');
49
+ if (behind) parts.push('source behind origin');
50
+ if (session) parts.push('active session');
51
+ if (codeyam) parts.push('.codeyam state');
52
+ return parts.length ? `will discard: ${parts.join(', ')}` : 'will reset state';
53
+ }
54
+
55
+ // The action ids governed by an effect indicator — the same ids server.js /
56
+ // index.html use for the buttons. Reconfigure / Env / Destroy are deliberately
57
+ // absent (one-shots, not converge-to-a-state actions, so a no-op predicate is
58
+ // meaningless for them).
59
+ const ACTION_IDS = [
60
+ 'update-client-source',
61
+ 'update-editor-source',
62
+ 'reset-editor-source',
63
+ 'reset-client',
64
+ 'rebuild-binary',
65
+ 'update-reset-everything',
66
+ 'run-setup',
67
+ ];
68
+
69
+ // facts (a plain object gathered by server.js — no I/O):
70
+ // {
71
+ // reachable: boolean,
72
+ // hasSession: boolean,
73
+ // client: { headSha, originSha, behind, dirty } | null, // /workspace
74
+ // editor: { headSha, originSha, behind, dirty, buildCommit } | null,
75
+ // codeyamDirty: boolean,
76
+ // setupRanSince: boolean, // a setup completed after the last reset/update
77
+ // clientBranch: string | null, // for reason text only
78
+ // editorBranch: string | null, // for reason text only
79
+ // }
80
+ function computeButtonEffects(facts) {
81
+ const f = facts || {};
82
+
83
+ // Unreachable VM (or no facts at all): every governed button is unknown. We
84
+ // could not reach the editor to gather facts, so never render a green no-op.
85
+ if (!f.reachable) {
86
+ const out = {};
87
+ for (const id of ACTION_IDS) out[id] = { effect: 'unknown', reason: 'VM unreachable' };
88
+ return out;
89
+ }
90
+
91
+ const client = f.client || null;
92
+ const editor = f.editor || null;
93
+ const hasSession = !!f.hasSession;
94
+ const codeyamDirty = !!f.codeyamDirty;
95
+ // A live build agent in the broker. A reset's first step is `pty-broker stop`,
96
+ // which kills it regardless of whether a workflow session is registered — so
97
+ // this is discardable work even when hasSession is false. Explicit-true only:
98
+ // a null/absent value (older or unreachable VM) must NOT flip a genuine noop.
99
+ const brokerLive = f.brokerLive === true;
100
+
101
+ // (1) Update client — ff-advance /workspace to origin tip. Changes iff behind.
102
+ const updateClient = client == null
103
+ ? { effect: 'unknown', reason: 'client state not checked' }
104
+ : client.behind > 0
105
+ ? { effect: 'changes', reason: behindReason(client.behind, f.clientBranch) }
106
+ : { effect: 'noop', reason: 'up to date with ' + originRef(f.clientBranch) };
107
+
108
+ // (2) Update editor — advance the editor source the binary builds from.
109
+ const updateEditor = editor == null
110
+ ? { effect: 'unknown', reason: 'editor state not checked' }
111
+ : editor.behind > 0
112
+ ? { effect: 'changes', reason: behindReason(editor.behind, f.editorBranch) }
113
+ : { effect: 'noop', reason: 'up to date with ' + originRef(f.editorBranch) };
114
+
115
+ // (5) Rebuild binary — advances source then compiles. Changes iff the running
116
+ // binary's build commit differs from the editor origin tip. Unknown when we
117
+ // don't have both commits to compare.
118
+ const rebuild = (editor == null || !editor.buildCommit || !editor.originSha)
119
+ ? { effect: 'unknown', reason: 'binary/source commit not checked' }
120
+ : editor.buildCommit !== editor.originSha
121
+ ? { effect: 'changes', reason: `binary at ${short(editor.buildCommit)}, ${originRef(f.editorBranch)} at ${short(editor.originSha)}` }
122
+ : { effect: 'noop', reason: 'binary built from ' + originRef(f.editorBranch) + ' tip' };
123
+
124
+ // (3) Reset editor — discards uncommitted + session, advances source, wipes
125
+ // .codeyam. No-op ONLY when clean at origin tip with no session.
126
+ const resetEditor = editor == null
127
+ ? { effect: 'unknown', reason: 'editor state not checked' }
128
+ : (brokerLive || hasSession || editor.dirty || editor.behind > 0 || codeyamDirty)
129
+ ? { effect: 'changes', reason: resetReason({ agent: brokerLive, session: hasSession, dirty: editor.dirty, behind: editor.behind > 0, codeyam: codeyamDirty }) }
130
+ : { effect: 'noop', reason: 'already clean at origin tip, no session' };
131
+
132
+ // (3b) Reset client — hard-clean /workspace + .codeyam, drop the session.
133
+ // No source-advance, so `behind` is NOT part of this predicate.
134
+ const resetClient = client == null
135
+ ? { effect: 'unknown', reason: 'client state not checked' }
136
+ : (brokerLive || client.dirty || codeyamDirty || hasSession)
137
+ ? { effect: 'changes', reason: resetReason({ agent: brokerLive, session: hasSession, dirty: client.dirty, behind: false, codeyam: codeyamDirty }) }
138
+ : { effect: 'noop', reason: 'already clean, no session' };
139
+
140
+ // (4b) Update & Reset Everything — composite. Changes iff ANY component would
141
+ // change: rebuild-binary, reset-editor-source, or the client being
142
+ // behind/dirty. Reason summarizes which parts.
143
+ const everything = (() => {
144
+ if (editor == null || client == null) {
145
+ return { effect: 'unknown', reason: 'state not checked' };
146
+ }
147
+ const parts = [];
148
+ if (rebuild.effect === 'changes') parts.push('rebuild');
149
+ if (resetEditor.effect === 'changes') parts.push('reset editor');
150
+ if (client.behind > 0 || client.dirty) parts.push('reset client');
151
+ if (parts.length > 0) return { effect: 'changes', reason: parts.join(' + ') };
152
+ // No part would change. But if a component was itself unverifiable, don't
153
+ // claim a confident no-op — fall back to unknown.
154
+ if (rebuild.effect === 'unknown' || resetEditor.effect === 'unknown') {
155
+ return { effect: 'unknown', reason: 'state not checked' };
156
+ }
157
+ return { effect: 'noop', reason: 'everything already at origin tip, no session' };
158
+ })();
159
+
160
+ // (run-setup) Honest soft signal — there is no cheap upstream proof that
161
+ // `npm run setup` would change nothing (DB/migration state isn't
162
+ // introspected). `recommended` when a reset/update landed after the last
163
+ // successful setup; otherwise `unknown`. Never a confident green no-op.
164
+ const runSetup = f.setupRanSince
165
+ ? { effect: 'unknown', reason: 'no setup-affecting change since last setup' }
166
+ : { effect: 'recommended', reason: 'a reset/update landed since the last setup' };
167
+
168
+ return {
169
+ 'update-client-source': updateClient,
170
+ 'update-editor-source': updateEditor,
171
+ 'reset-editor-source': resetEditor,
172
+ 'reset-client': resetClient,
173
+ 'rebuild-binary': rebuild,
174
+ 'update-reset-everything': everything,
175
+ 'run-setup': runSetup,
176
+ };
177
+ }
178
+
179
+ module.exports = { computeButtonEffects, ACTION_IDS };