@codeyam-editor/codeyam-editor 0.1.5 → 0.1.6

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 (76) hide show
  1. package/npm/cli.js +5 -1
  2. package/npm/cloud.js +1884 -210
  3. package/npm/codesign-plan.js +68 -0
  4. package/npm/empty-project-dir.js +92 -7
  5. package/npm/fleet-add-failure.js +252 -0
  6. package/npm/fleet-asset-version.js +37 -0
  7. package/npm/fleet-auth-health.js +143 -5
  8. package/npm/fleet-build-data.js +60 -5
  9. package/npm/fleet-cleanup.js +114 -9
  10. package/npm/fleet-destroy-safety.js +230 -13
  11. package/npm/fleet-early-roster.js +87 -0
  12. package/npm/fleet-effect-poll.js +110 -0
  13. package/npm/fleet-env.js +86 -0
  14. package/npm/fleet-ghcr-secret.js +129 -0
  15. package/npm/fleet-git-maintenance.js +142 -0
  16. package/npm/fleet-host-op-limiter.js +68 -0
  17. package/npm/fleet-iap-token.js +165 -0
  18. package/npm/fleet-live-status.js +40 -8
  19. package/npm/fleet-memory.js +245 -9
  20. package/npm/fleet-plans.js +98 -4
  21. package/npm/fleet-pool.js +75 -16
  22. package/npm/fleet-prewarm.js +87 -11
  23. package/npm/fleet-project-drift.js +58 -0
  24. package/npm/fleet-public-url.js +234 -0
  25. package/npm/fleet-queue.js +58 -1
  26. package/npm/fleet-reachability-debounce.js +69 -0
  27. package/npm/fleet-reachability.js +80 -0
  28. package/npm/fleet-reset.js +32 -0
  29. package/npm/fleet-running-plan.js +85 -0
  30. package/npm/fleet-session-token-cache.js +65 -0
  31. package/npm/fleet-setup-steps.js +19 -12
  32. package/npm/fleet-status-refs.js +96 -0
  33. package/npm/fleet-strand.js +39 -8
  34. package/npm/fleet-stuck-status.js +13 -7
  35. package/npm/fleet-vm-access.js +170 -0
  36. package/npm/fleet-vm-probe-target.js +282 -0
  37. package/npm/fleet-vm-readiness.js +136 -3
  38. package/npm/google-ingress.js +15 -0
  39. package/npm/ingress-invoke.js +478 -0
  40. package/npm/launch-config.js +182 -6
  41. package/npm/scenario-check.js +282 -10
  42. package/npm/scenario-handlers.js +85 -27
  43. package/npm/scenario-interactivity.js +145 -6
  44. package/npm/scenario-issues.js +13 -1
  45. package/npm/scenario-mocks.js +129 -26
  46. package/npm/scenario-playwright.js +286 -12
  47. package/npm/script-drift.js +72 -1
  48. package/npm/utils.js +37 -3
  49. package/package.json +5 -5
  50. package/ui/dist/assets/{ContainerInspector-D7VZkkaj.js → ContainerInspector-DgM0pTAx.js} +1 -1
  51. package/ui/dist/assets/EditorShell-B7Q06uJ_.js +252 -0
  52. package/ui/dist/assets/EditorShell-DP5UY20G.css +1 -0
  53. package/ui/dist/assets/{Markdown-BoLHHFXH.js → Markdown-Chgdf3PW.js} +1 -1
  54. package/ui/dist/assets/ProjectLauncherScreen-BRrEr8Ng.js +1 -0
  55. package/ui/dist/assets/ScenarioDataPanel-BeQfa28g.js +9 -0
  56. package/ui/dist/assets/{ScenarioDataPanel-DYP7pi_n.css → ScenarioDataPanel-C1LukYI6.css} +1 -1
  57. package/ui/dist/assets/{ScenarioDataPanelFullPage-CBLTGC4-.js → ScenarioDataPanelFullPage-BLqRCxQX.js} +1 -1
  58. package/ui/dist/assets/ScenarioRenderer-CIAxfHPh.js +21 -0
  59. package/ui/dist/assets/{StepsSimulator-BFldWyoX.js → StepsSimulator-CymGgfbu.js} +1 -1
  60. package/ui/dist/assets/index-BygCiJSb.css +1 -0
  61. package/ui/dist/assets/{index-QsOiOG9x.js → index-DCHqRrwO.js} +4 -4
  62. package/ui/dist/assets/{markdown-v0F0UEt3.js → markdown-C9tbsVHX.js} +1 -1
  63. package/ui/dist/assets/react-nrLBr15I.js +28 -0
  64. package/ui/dist/assets/{useEvents-DHkLJZR4.js → useEvents-S3Qk1Y_d.js} +1 -1
  65. package/ui/dist/assets/useServerIdentity-B7rI0u3O.js +1 -0
  66. package/ui/dist/index.html +4 -4
  67. package/ui/dist/open-source/codeyam-counter.png +0 -0
  68. package/ui/dist/open-source/el-carot.png +0 -0
  69. package/ui/dist/open-source/tabcommand.png +0 -0
  70. package/ui/dist/assets/EditorShell-DHhIaotd.css +0 -1
  71. package/ui/dist/assets/EditorShell-r-796N_W.js +0 -191
  72. package/ui/dist/assets/ProjectLauncherScreen-Ck-XWkQN.js +0 -1
  73. package/ui/dist/assets/ScenarioDataPanel-B9iDSUpA.js +0 -5
  74. package/ui/dist/assets/ScenarioRenderer-BhLfT-nX.js +0 -21
  75. package/ui/dist/assets/index-CIXZkwfB.css +0 -1
  76. package/ui/dist/assets/react-CSS0HapR.js +0 -28
@@ -25,6 +25,14 @@ const MARKER_PUBLISH_SKIPPED = 'CY_BINARY_PUBLISH_SKIPPED'; // pacer will NOT pu
25
25
  const MARKER_PATH_PULL = 'CY_PROVISION_PATH pull'; // this VM pulled a prebuilt image (~2 min fast path)
26
26
  const MARKER_PATH_COMPILE = 'CY_PROVISION_PATH compile'; // this VM compiled from source (~10 min slow path)
27
27
 
28
+ // Markers scripts/cloud-refresh-binary.sh emits into the between-session
29
+ // refresh log (/tmp/dash-refresh-binary-vm-<n>.log, or the systemd journal when
30
+ // the timer runs it unattended). Same stable-contract rule as the four above.
31
+ const MARKER_REFRESHED = 'CY_BINARY_REFRESHED'; // pulled + swapped a newer published binary, container restarted
32
+ const MARKER_REFRESH_CURRENT = 'CY_BINARY_CURRENT'; // already on the newest published binary; nothing to do
33
+ const MARKER_REFRESH_DEFERRED = 'CY_BINARY_REFRESH_DEFERRED'; // newer binary exists but a session is live (or state unknown)
34
+ const MARKER_REFRESH_DEGRADED = 'CY_DEGRADED binary-refresh'; // the refresh could NOT complete — this VM may be running stale
35
+
28
36
  // Mirror scripts/gcp-bootstrap.sh's BRANCH_SLUG: '/' → '-', lowercased, capped
29
37
  // at 128 chars. The dashboard and the VM MUST agree on the slug or the probe
30
38
  // checks different tags than the bootstrap pulls. Empty / missing branch → the
@@ -53,7 +61,7 @@ function usableImageTags({ registry, branch } = {}) {
53
61
  return tags;
54
62
  }
55
63
 
56
- // The core verdict: should a batch add pace one VM ahead of the rest?
64
+ // The core verdict: should a cold add pace one VM to compile+publish first?
57
65
  //
58
66
  // count how many VMs the operator asked to add
59
67
  // hasUsableImage the probe result — true (image present), false (definitely
@@ -61,24 +69,46 @@ function usableImageTags({ registry, branch } = {}) {
61
69
  // hasPushToken a write:packages token is present, so the pacer CAN publish
62
70
  //
63
71
  // Pace ONLY when it strictly pays AND can work:
64
- // - count > 1 — a single add is never serialized (never slower)
65
- // - hasUsableImage === false — known-absent; an existing image means all N can
66
- // pull concurrently, and `null` (unknown) falls
67
- // back to today's fan-out rather than risk a
68
- // 10-min serialization that buys nothing
72
+ // - count >= 1 — a batch (k>1) serializes ONE ahead so the rest pull;
73
+ // a single cold add (k==1) still paces so it PUBLISHES
74
+ // the branch image, making every FUTURE add (single or
75
+ // batch) pull instead of source-building. Gating the
76
+ // publish behind k>1 was a bug: adding VMs one at a time
77
+ // bypassed the pacer entirely, so no image was ever
78
+ // produced and every single add recompiled.
79
+ // - hasUsableImage !== true — a usable image lets all N pull concurrently, so
80
+ // only pace when one is NOT known to exist. Both
81
+ // `false` (known-absent) AND `null` (undetermined)
82
+ // pace: publishing is cheap + idempotent, and pacing
83
+ // is the ONLY path that bootstraps self-feeding.
84
+ // Fanning out on `null` is exactly what permanently
85
+ // wedges a docker-less operator host whose probe can
86
+ // never confirm absence (see
87
+ // npm/cloud.js::probeUsableEditorImage) — republishing
88
+ // an image that already existed is the harmless worst
89
+ // case.
69
90
  // - hasPushToken — without it the pacer can't publish, so pacing would
70
91
  // just serialize N source compiles for no gain
71
- // Any miss → fan out all N immediately (today's behavior, strictly additive).
92
+ // Fan out immediately only when a usable image is KNOWN to exist, or there is no
93
+ // push token (today's behavior for those two cases, strictly additive).
72
94
  function decideBatchPrewarm({ count, hasUsableImage, hasPushToken } = {}) {
73
95
  const k = Math.round(Number(count) || 0);
74
- if (k <= 1) return { pace: false, reason: 'single add — never serialized' };
96
+ if (k < 1) return { pace: false, reason: 'no VMs requested' };
75
97
  if (hasUsableImage === true)
76
98
  return { pace: false, reason: 'usable prebuilt image exists — all pull concurrently' };
77
- if (hasUsableImage !== false)
78
- return { pace: false, reason: 'image presence undetermined — fan out (no worse than today)' };
79
99
  if (!hasPushToken)
80
100
  return { pace: false, reason: 'no push token — pacer could not publish, fan out' };
81
- return { pace: true, reason: `pacing 1 of ${k} — branch image absent, will publish for the rest` };
101
+ // hasUsableImage is `false` (known-absent) or `null` (undetermined); with a push
102
+ // token, pace either way to bootstrap self-feeding.
103
+ const basis = hasUsableImage === false
104
+ ? 'branch image absent'
105
+ : 'image presence undetermined — publishing to bootstrap self-feeding';
106
+ return {
107
+ pace: true,
108
+ reason: k > 1
109
+ ? `pacing 1 of ${k} — ${basis}, will publish for the rest`
110
+ : `single cold add — ${basis}, will compile and publish so future adds pull`,
111
+ };
82
112
  }
83
113
 
84
114
  // Has the pacer's add log settled to a definite publish outcome the held batch
@@ -149,11 +179,57 @@ function pushTokenDiagnostic({ hasPushToken } = {}) {
149
179
  };
150
180
  }
151
181
 
182
+ // What did the last between-session binary refresh do? Parsed from the refresh
183
+ // log for the VM card, so an operator can tell "up to date" from "could not
184
+ // update" without opening a log.
185
+ //
186
+ // Returns 'refreshed' | 'current' | 'deferred' | 'degraded' | null (no refresh
187
+ // has reported yet).
188
+ //
189
+ // The LAST marker wins, for the same reason provisionPath() takes the last one:
190
+ // a tick that defers and a later tick that refreshes must read as refreshed,
191
+ // not stay stuck on the older verdict. Ordering — not precedence between marker
192
+ // kinds — is what makes this correct across repeated timer runs.
193
+ //
194
+ // `degraded` is deliberately a distinct state from `deferred`. Deferring is the
195
+ // mechanism working as designed (a session is live, so we wait). Degraded means
196
+ // the VM wanted a newer binary and could NOT get one — the silent-staleness
197
+ // case this whole mechanism exists to make visible, and the only one that
198
+ // warrants an operator's attention.
199
+ function binaryRefreshState(logText) {
200
+ if (!logText || typeof logText !== 'string') return null;
201
+ const at = (marker) => logText.lastIndexOf(marker);
202
+ const candidates = [
203
+ ['refreshed', at(MARKER_REFRESHED)],
204
+ ['current', at(MARKER_REFRESH_CURRENT)],
205
+ ['deferred', at(MARKER_REFRESH_DEFERRED)],
206
+ ['degraded', at(MARKER_REFRESH_DEGRADED)],
207
+ ].filter(([, idx]) => idx >= 0);
208
+ if (!candidates.length) return null;
209
+ return candidates.reduce((best, cur) => (cur[1] > best[1] ? cur : best))[0];
210
+ }
211
+
212
+ // Operator-facing one-liner for a refresh state, for the VM card. null state →
213
+ // null (render nothing — a VM that has never run a refresh is not a problem).
214
+ function binaryRefreshLabel(state) {
215
+ if (state === 'refreshed') return 'editor binary refreshed to the newest published build';
216
+ if (state === 'current') return 'editor binary up to date';
217
+ if (state === 'deferred') return 'newer editor binary available — waiting for the session to end';
218
+ if (state === 'degraded') return 'could NOT refresh the editor binary — may be running stale';
219
+ return null;
220
+ }
221
+
152
222
  module.exports = {
153
223
  MARKER_PUBLISHED,
154
224
  MARKER_PUBLISH_SKIPPED,
155
225
  MARKER_PATH_PULL,
156
226
  MARKER_PATH_COMPILE,
227
+ MARKER_REFRESHED,
228
+ MARKER_REFRESH_CURRENT,
229
+ MARKER_REFRESH_DEFERRED,
230
+ MARKER_REFRESH_DEGRADED,
231
+ binaryRefreshState,
232
+ binaryRefreshLabel,
157
233
  branchSlug,
158
234
  usableImageTags,
159
235
  decideBatchPrewarm,
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ // npm/fleet-project-drift.js — pure classifier for the fleet dashboard's
4
+ // "project-dir-drift" surface: does a VM's LIVE project identity conflict with
5
+ // the identity the operator asked for at Add time?
6
+ //
7
+ // The drift surface exists to catch a genuine misprovision — a VM whose
8
+ // /workspace checked out the WRONG repo, so its live project name no longer
9
+ // matches the expected one. But a brand-new GREENFIELD project (added with
10
+ // --new-project / newProjectName, no clientRepo) has no origin remote and, on a
11
+ // cloud VM, is always bind-mounted at /workspace. Before the editor seeds its
12
+ // projectTitle, the live identity falls back to the bare mount basename
13
+ // "workspace", which never equals the expected newProjectName — a PERMANENT
14
+ // FALSE drift that made every healthy greenfield add read amber and then get
15
+ // misclassified as "Add failed". This module suppresses drift for exactly that
16
+ // un-onboarded greenfield shape while leaving every true-positive intact.
17
+ //
18
+ // Pure like fleet-add-failure.js: server.js is the thin I/O shell that gathers
19
+ // the poll state and renders the returned reasons; the decision is unit-tested
20
+ // here without a live VM.
21
+
22
+ // The bare basename of the cloud-VM bind mount. Every fleet VM mounts the
23
+ // client project at /workspace (see server.js's in-container gather and the
24
+ // `-w /workspace` exec target), so an un-onboarded greenfield project with no
25
+ // origin resolves its live name to this literal string until the editor seeds a
26
+ // real projectTitle.
27
+ const MOUNT_BASENAME = 'workspace';
28
+
29
+ // Decide the project-dir-drift reasons for one VM.
30
+ //
31
+ // expectedName the identity the operator asked for: repoBasename(clientRepo)
32
+ // for a clone, else newProjectName for greenfield, else null
33
+ // liveName the VM's live clientProject.name (null when unreachable)
34
+ // liveRepo the VM's live clientProject.repo — null for a project with no
35
+ // origin remote (always the case for a fresh greenfield add)
36
+ // hasClientRepo whether the Add was configured with a clientRepo (a clone);
37
+ // false for a --new-project greenfield add
38
+ //
39
+ // Returns { drifted, reasons }. `reasons` is [] when not drifted so the caller
40
+ // can spread it into the card's reason list unconditionally.
41
+ function classifyProjectDrift({ expectedName, liveName, liveRepo, hasClientRepo } = {}) {
42
+ // Un-onboarded greenfield: no configured clientRepo, no live origin remote,
43
+ // and the live name is still the bare mount basename. This is a healthy fresh
44
+ // project that simply has not been wired to a remote yet — never real drift.
45
+ // Note the live-origin check: once a greenfield project DOES gain an origin
46
+ // whose basename conflicts with expected, `liveRepo` is non-null and this
47
+ // suppression no longer applies, so a genuine later misprovision still drifts.
48
+ const greenfieldUnonboarded =
49
+ !hasClientRepo && !liveRepo && liveName === MOUNT_BASENAME;
50
+ const drifted =
51
+ !greenfieldUnonboarded && !!(expectedName && liveName && expectedName !== liveName);
52
+ const reasons = drifted
53
+ ? [`project-dir-drift (expected: ${expectedName}, running: ${liveName})`]
54
+ : [];
55
+ return { drifted, reasons };
56
+ }
57
+
58
+ module.exports = { classifyProjectDrift, MOUNT_BASENAME };
@@ -24,6 +24,12 @@
24
24
  // as the scripts it backstops. A project that never joins the fleet never sets
25
25
  // FLEET_DOMAIN and never reaches this probe.
26
26
 
27
+ // The IAP members probe parses the same `value(bindings.members)` gcloud shape
28
+ // the per-VM access sidecar already reads, so it reuses that parser rather than
29
+ // growing a second one. fleet-vm-access requires only utils + vm-urls, so this
30
+ // edge introduces no cycle.
31
+ const { parseProjectMembers } = require('./fleet-vm-access');
32
+
27
33
  // The Google HTTPS-LB "no backend answered" body signature: GFE emits this when
28
34
  // a host rule routes nowhere (or to a backendless default). A single source so
29
35
  // the classifier and any future log line agree on the string.
@@ -59,6 +65,202 @@ function classifyPublicUrlHealth({ statusCode, body, error } = {}) {
59
65
  return 'unknown';
60
66
  }
61
67
 
68
+ /** Classify a public-URL WebSocket-UPGRADE probe result into the same ingress-
69
+ * wiring vocabulary classifyPublicUrlHealth uses (vm: Readiness Verifies Real
70
+ * Usability). The plain GET proves the host rule ROUTES, but says NOTHING about
71
+ * whether the ingress carries a WebSocket UPGRADE — the exact VM5-class
72
+ * blindness: the editor answered HTTP 200 (its `101` even succeeded) while the
73
+ * LB severed the socket every 30s, so the Build tab looped "Reconnecting…" on a
74
+ * VM every readiness signal called healthy. server.js gathers a `101`-asserting
75
+ * upgrade against `<publicUrl>/ws/health` (the auth-free liveness endpoint that
76
+ * completes the upgrade, sends one frame, and closes — no broker, no PTY) and
77
+ * hands the result here. Follows the same absence-of-evidence contract:
78
+ *
79
+ * 'unknown' — a network / TLS / timeout error (no statusCode), OR a 3xx
80
+ * auth/IAP login redirect: the probe couldn't reach a verdict.
81
+ * NEVER a fault, exactly like classifyPublicUrlHealth's 'unknown'
82
+ * — a transient blip, or an IAP that bounced an un-authenticated
83
+ * probe to a login page, must never yank a healthy VM out of the
84
+ * roster. (An IAP-fronted VM whose browser WS works fine returns a
85
+ * 302 to a probe that carries no IAP credential — proof of nothing
86
+ * about the editor, so it cannot be 'unwired'.)
87
+ * 'wired' — the upgrade completed (HTTP 101). The WS path carries frames.
88
+ * 'unwired' — the LB/editor answered PAST auth and REFUSED the upgrade (a
89
+ * non-101, non-3xx code): a host/LB rule that doesn't carry
90
+ * WebSocket at all (the OTHER WS failure mode, distinct from the
91
+ * timeout guillotine below). This is the only definitive fault,
92
+ * so 'ws-dead' fires on it but never on the ambiguous 'unknown'.
93
+ */
94
+ function classifyPublicWsHealth({ statusCode, error } = {}) {
95
+ if (error || statusCode == null) return 'unknown';
96
+ const code = Number(statusCode);
97
+ if (code === 101) return 'wired';
98
+ // A 3xx is an auth/IAP login redirect — the probe carries no IAP credential,
99
+ // so it can never complete the handshake. Absence of evidence, never a fault
100
+ // (mirrors classifyPublicUrlHealth's 3xx-is-'wired'/never-unwired treatment).
101
+ if (code >= 300 && code < 400) return 'unknown';
102
+ // A non-101, non-redirect response is the editor answering past auth and
103
+ // refusing the upgrade — the genuine WS-unwired signal.
104
+ return 'unwired';
105
+ }
106
+
107
+ /** The IAP role that actually authorizes opening a VM's public URL. A policy can
108
+ * carry any number of other bindings; only this one decides the verdict, and
109
+ * only its members are real IAP principals. */
110
+ const IAP_ACCESSOR_ROLE = 'roles/iap.httpsResourceAccessor';
111
+
112
+ /** Classify a gcloud IAP get-iam-policy result for a backend service. PURE —
113
+ * takes `{ error, stdout }` and returns `{ verdict, members }`.
114
+ *
115
+ * `verdict` is the unchanged three-value vocabulary the ingress rendering and
116
+ * fleet-vm-readiness already depend on:
117
+ * 'unknown' — the gcloud read itself errored (absence of evidence, never
118
+ * manufactured as a fault; mirrors the gather probes).
119
+ * 'granted' — at least one binding carries roles/iap.httpsResourceAccessor,
120
+ * so someone can actually open the VM's public URL.
121
+ * 'no-accessor' — the policy read cleanly but names no accessor role, i.e. the
122
+ * VM is wired but nobody is granted (the public URL 51s in the
123
+ * browser).
124
+ *
125
+ * `members` is the list of principals holding the accessor role — the fact the
126
+ * old bare-verdict shape was structurally incapable of reporting. Because
127
+ * `domain:codeyam.com` always holds the role, "is anyone granted?" was always
128
+ * true, so a specific collaborator's MISSING binding could never be detected;
129
+ * that blindness is what let the dashboard render a failed grant as access for
130
+ * hours (VM-10, 2026-07-30). `members` is `[]` on a read error and on
131
+ * 'no-accessor' — an empty list is never evidence of a gap by itself, the verdict
132
+ * carries that.
133
+ *
134
+ * Input is `--format=value(bindings.role,bindings.members)`: one line per
135
+ * binding, fields TAB-separated, members within a binding `;`-separated. Only
136
+ * the accessor binding's members are returned — a roles/owner binding does not
137
+ * grant IAP web access, so listing its members as IAP principals would be a
138
+ * different flavor of the same lie.
139
+ */
140
+ function classifyIapAccess({ error, stdout } = {}) {
141
+ if (error) return { verdict: 'unknown', members: [] };
142
+ const lines = String(stdout || '')
143
+ .split('\n')
144
+ .map((l) => l.trim())
145
+ .filter(Boolean);
146
+ const accessorLines = lines.filter((l) => l.split('\t')[0].trim() === IAP_ACCESSOR_ROLE);
147
+ if (accessorLines.length === 0) return { verdict: 'no-accessor', members: [] };
148
+ // Reuse the members parser written for exactly this gcloud output shape
149
+ // (`;`/newline split, trim, dedupe preserving first appearance, drop `deleted:`
150
+ // tombstones) rather than growing a second one that would drift from it.
151
+ const members = parseProjectMembers(
152
+ accessorLines.map((l) => l.split('\t').slice(1).join('\t')).join('\n'),
153
+ );
154
+ return { verdict: 'granted', members };
155
+ }
156
+
157
+ /** Aggregate the per-surface IAP verdicts (editor + preview) into a single fleet
158
+ * verdict. PURE — takes `{ editor, preview }`, each a `classifyIapAccess`
159
+ * result, and returns:
160
+ * 'unknown' — either surface's read errored (absence of evidence,
161
+ * never manufactured as a fault).
162
+ * 'no-accessor' — the EDITOR backend has no accessor: the URL the
163
+ * operator opens 51s for everyone. The primary gap
164
+ * (dominates a preview gap — the editor is unusable).
165
+ * 'preview-no-accessor' — the editor grants access but the PREVIEW backend has
166
+ * no accessor. The exact shape that hid this bug: the
167
+ * editor loads, but the framed Live Preview (served
168
+ * from the separate preview host) is blank for every
169
+ * authorized user because nobody is granted there.
170
+ * 'granted' — BOTH surfaces authorize someone.
171
+ *
172
+ * The preview verdict is optional: an older dashboard that only gathered the
173
+ * editor surface passes `preview === undefined`, and the result collapses to the
174
+ * single-surface classification (never a phantom preview gap).
175
+ *
176
+ * Accepts each surface as either a bare verdict string or a full
177
+ * `classifyIapAccess` result `{ verdict, members }`, and RETURNS A STRING either
178
+ * way. The string return is load-bearing, not laziness: `deriveVmReadiness`
179
+ * compares `v.iapAccess` against 'no-accessor' / 'preview-no-accessor' directly
180
+ * (npm/fleet-vm-readiness.js), and the card's readiness-badge CSS keys off those
181
+ * same values. Members are aggregated separately by `aggregateIapMembers` so the
182
+ * verdict contract stays byte-identical. */
183
+ function aggregateIapAccess({ editor, preview } = {}) {
184
+ // Unwrap the `{ verdict, members }` shape; a bare string passes through, and
185
+ // undefined/null stay undefined/null so the single-surface fallback below and
186
+ // the 'unknown' guard both keep working.
187
+ const v = (s) => (s && typeof s === 'object' ? s.verdict : s);
188
+ editor = v(editor);
189
+ preview = v(preview);
190
+ // Absence of evidence on either surface is never a fault.
191
+ if (editor === 'unknown' || preview === 'unknown') return 'unknown';
192
+ // The editor is the URL the operator opens; if IT lacks an accessor, that is
193
+ // the primary gap and dominates any preview verdict.
194
+ if (editor !== 'granted') return 'no-accessor';
195
+ // Editor granted. With no preview verdict gathered, fall back to the
196
+ // editor-only 'granted' (backward-compatible with single-surface dashboards).
197
+ if (preview === undefined || preview === null) return 'granted';
198
+ // Editor granted but the preview host authorizes nobody — the framed preview
199
+ // is blank for authorized users even though the editor works.
200
+ if (preview !== 'granted') return 'preview-no-accessor';
201
+ return 'granted';
202
+ }
203
+
204
+ /** Aggregate the per-surface IAP MEMBER lists into the card's display truth.
205
+ * PURE — takes the same `{ editor, preview }` map of `classifyIapAccess`
206
+ * results `aggregateIapAccess` consumes, and returns
207
+ * `{ editor, preview, all, partial }`:
208
+ *
209
+ * editor / preview — that surface's accessor members ([] when the surface
210
+ * reported no accessor, or was not gathered).
211
+ * all — the union across surfaces, first-appearance order. This
212
+ * is what the card renders as "who actually has access".
213
+ * partial — true when a principal holds the role on SOME but not all
214
+ * gathered surfaces. That is the editor-works /
215
+ * preview-blank shape, visible per-principal rather than
216
+ * only as the coarse 'preview-no-accessor' verdict.
217
+ *
218
+ * Returns `null` when either gathered surface errored ('unknown'): a partial
219
+ * read would render a live principal as missing, which is the same class of lie
220
+ * in the opposite direction. Absence of evidence is not a member list. */
221
+ function aggregateIapMembers({ editor, preview } = {}) {
222
+ const surfaces = [editor, preview].filter((s) => s !== undefined && s !== null);
223
+ if (surfaces.some((s) => (typeof s === 'object' ? s.verdict : s) === 'unknown')) return null;
224
+ const listOf = (s) => (s && typeof s === 'object' && Array.isArray(s.members) ? s.members : []);
225
+ const e = listOf(editor);
226
+ const p = listOf(preview);
227
+ const all = [];
228
+ for (const m of [...e, ...p]) if (!all.includes(m)) all.push(m);
229
+ // A principal is "partial" when it is absent from at least one surface that was
230
+ // actually gathered. With only one surface gathered nothing can be partial.
231
+ const gathered = [];
232
+ if (editor !== undefined && editor !== null) gathered.push(e);
233
+ if (preview !== undefined && preview !== null) gathered.push(p);
234
+ const partial = gathered.length > 1
235
+ && all.some((m) => gathered.some((list) => !list.includes(m)));
236
+ return { editor: e, preview: p, all, partial };
237
+ }
238
+
239
+ // The fleet minimum backend-service `timeoutSec` a WebSocket needs to survive
240
+ // behind the External HTTPS LB. The LB severs a backend connection at
241
+ // `timeoutSec` regardless of liveness, so the default 30s guillotines the Build
242
+ // tab's terminal every 30s (the VM5 symptom). scripts/gcp/vm-ingress.sh sets —
243
+ // and always converges existing BES to — 86400 (24h); readiness asserts the
244
+ // same floor so a BES that drifted below it reads `ws-dead` instead of healthy.
245
+ // A single source so the classifier and any future log line agree on the number.
246
+ const FLEET_WS_TIMEOUT_MIN_SEC = 86400;
247
+
248
+ /** Classify a backend-service `timeoutSec` against the fleet WS minimum. PURE —
249
+ * server.js reads the value via `gcloud backend-services describe` and hands
250
+ * the number in, so the threshold logic is unit-testable without gcloud.
251
+ * Absence of evidence is never a fault (mirrors classifyBackendHealth):
252
+ *
253
+ * 'unknown' — not a usable positive number (describe errored /
254
+ * empty): no verdict, never a problem state.
255
+ * 'ws-timeout-too-low' — a real value BELOW the minimum: the LB WILL sever
256
+ * the socket (the VM5 guillotine). The only fault.
257
+ * 'ok' — timeoutSec ≥ the fleet minimum: long enough for WS. */
258
+ function classifyWsTimeout(backendTimeoutSec, minSec = FLEET_WS_TIMEOUT_MIN_SEC) {
259
+ const t = Number(backendTimeoutSec);
260
+ if (!Number.isFinite(t) || t <= 0) return 'unknown';
261
+ return t >= Number(minSec) ? 'ok' : 'ws-timeout-too-low';
262
+ }
263
+
62
264
  /** The sticky `ingress-unwired` degraded-reason string — the single source for
63
265
  * both the dashboard card reason and any log line. Embeds the advertised URL
64
266
  * and the EXACT re-wire command an operator runs to fix it, so the amber card
@@ -152,9 +354,41 @@ function classifyBackendAttachment(err, stdout) {
152
354
  return String(stdout == null ? '' : stdout).trim() === '' ? 'backendless' : 'attached';
153
355
  }
154
356
 
357
+ /** Collapse a VM's raw wiring signals into the single URL-state vocabulary the
358
+ * card renders (vm--add-time-url-wiring-converges-reliably), so "the link is
359
+ * converging" is DISTINCT from "the link is dead" — the operator is never shown
360
+ * a "ready" card over an unwired URL, nor left to infer state from an absent
361
+ * link. PURE — the dashboard reads the live facts (the public-URL probe verdict,
362
+ * the inflight marker, the failed marker: all I/O at the edge) and passes them
363
+ * in. Precedence:
364
+ * - `publicUrlHealth === 'wired'` → 'wired'. A real 2xx/3xx wins over any
365
+ * stale marker; the URL genuinely serves.
366
+ * - else `inflight` → 'wiring'. The add-tail converge loop is still running;
367
+ * the card reads a calm "wiring public URL…", not a false failure.
368
+ * - else `failed` → 'wiring-failed'. The converge loop exhausted its deadline
369
+ * without reaching `wired` — a real attention reason with a re-run command,
370
+ * never a silent `partial`.
371
+ * - else `null`. No wiring signal (legacy quick-tunnel, or a transient probe
372
+ * miss with no markers) — the caller falls back to its existing
373
+ * unwired/unknown handling rather than inventing a state. */
374
+ function classifyUrlWiringState({ publicUrlHealth, inflight, failed } = {}) {
375
+ if (publicUrlHealth === 'wired') return 'wired';
376
+ if (inflight) return 'wiring';
377
+ if (failed) return 'wiring-failed';
378
+ return null;
379
+ }
380
+
155
381
  module.exports = {
156
382
  classifyPublicUrlHealth,
383
+ classifyPublicWsHealth,
384
+ classifyIapAccess,
385
+ aggregateIapAccess,
386
+ aggregateIapMembers,
387
+ IAP_ACCESSOR_ROLE,
388
+ classifyWsTimeout,
389
+ FLEET_WS_TIMEOUT_MIN_SEC,
157
390
  ingressUnwiredReason,
158
391
  shouldAutoRewireIngress,
159
392
  classifyBackendAttachment,
393
+ classifyUrlWiringState,
160
394
  };
@@ -74,4 +74,61 @@ function computeReorderTargets({ order, current }) {
74
74
  return { ok: true, targets };
75
75
  }
76
76
 
77
- module.exports = { decideAutoReleaseGoneHead, computeReorderTargets };
77
+ /** Move one uuid within a desired order array.
78
+ *
79
+ * The staged-draft panel shuffles a working uuid order before it is saved.
80
+ * `delta > 0` raises priority (moves the entry TOWARD the head / index 0);
81
+ * `delta < 0` lowers it (toward the tail). The target index is clamped to the
82
+ * array bounds, so a nudge past an end is a no-op and a full-span negative
83
+ * delta lands the entry at the very tail — which is exactly how Demote
84
+ * (head → back of the line) is expressed: `nudgeQueueOrder(order, head, -(N-1))`.
85
+ * Mirrors the editor UI's `useBranchQueueDraft.nudge` (arrayMove(from, from-delta)).
86
+ *
87
+ * `order` is a uuid array; a uuid not present is returned unchanged. Pure — the
88
+ * input array is never mutated.
89
+ */
90
+ function nudgeQueueOrder(order, uuid, delta) {
91
+ const arr = (order || []).slice();
92
+ const from = arr.findIndex((u) => u === uuid);
93
+ if (from < 0 || !delta) return arr;
94
+ let to = from - delta; // +delta → toward head; -delta → toward tail
95
+ if (to < 0) to = 0;
96
+ if (to > arr.length - 1) to = arr.length - 1;
97
+ if (to === from) return arr;
98
+ const [moved] = arr.splice(from, 1);
99
+ arr.splice(to, 0, moved);
100
+ return arr;
101
+ }
102
+
103
+ /** Merge a fresh poll's membership into a staged draft order without discarding
104
+ * the operator's in-progress reorder.
105
+ *
106
+ * - uuids still present live keep their DRAFT position (the working order wins);
107
+ * - draft uuids that vanished upstream are dropped;
108
+ * - live uuids not yet in the draft are appended at the tail so a sibling
109
+ * session's newly-enqueued entry doesn't silently disappear.
110
+ *
111
+ * Pure port of the editor UI's `reconcileDraft` order rule (uuid-only, no meta).
112
+ * Both arguments are uuid arrays; returns the merged uuid array. Never mutates
113
+ * its inputs.
114
+ */
115
+ function reconcileQueueDraft(draftOrder, liveOrder) {
116
+ const live = liveOrder || [];
117
+ const liveSet = new Set(live);
118
+ const seen = new Set();
119
+ const out = [];
120
+ for (const u of draftOrder || []) {
121
+ if (liveSet.has(u) && !seen.has(u)) { out.push(u); seen.add(u); }
122
+ }
123
+ for (const u of live) {
124
+ if (!seen.has(u)) { out.push(u); seen.add(u); }
125
+ }
126
+ return out;
127
+ }
128
+
129
+ module.exports = {
130
+ decideAutoReleaseGoneHead,
131
+ computeReorderTargets,
132
+ nudgeQueueOrder,
133
+ reconcileQueueDraft,
134
+ };
@@ -0,0 +1,69 @@
1
+ 'use strict';
2
+ // Pure consecutive-failure debounce for the Fleet Dashboard's per-VM top-level
3
+ // reachability. Mirrors npm/fleet-broker-debounce.js exactly: server.js does the
4
+ // polling and threads the state; this decides the reachability the card renders.
5
+ //
6
+ // The bug this fixes (observed live 2026-07-01, VM6): a healthy VM briefly
7
+ // flashed 'unreachable' and surfaced a reset-client-error even though it was up
8
+ // the whole time. The dashboard polls each VM's editor over a laptop-side
9
+ // SSH/cloudflared forward with NO fast respawner, so a single one-poll hiccup
10
+ // returned reachable:false and the card instantly read 'unreachable' — which
11
+ // then invited the destructive "Reset client" action on a perfectly healthy VM.
12
+ // The single probe is left as-is on the poller side; the right seam for
13
+ // absorbing a one-poll blip is here at the aggregation layer, just like broker.
14
+ //
15
+ // Asymmetric by design: ONLY the flip to 'unreachable' is debounced (it requires
16
+ // `threshold` consecutive failed polls). A reachable poll takes effect
17
+ // immediately — we never DELAY clearing a false alarm, only delay raising one.
18
+ //
19
+ // Three display states the card renders:
20
+ // 'reachable' — the editor answered this poll.
21
+ // 'reconnecting' — a sub-threshold failure streak (1..N-1), OR the GCP side
22
+ // confirms the editor answers over IAP while the local port
23
+ // is dark (a laptop tunnel fault, never a dead VM). Calm; the
24
+ // watchdog respawns the forward. A one-poll hiccup lives here.
25
+ // 'unreachable' — N consecutive failures AND no GCP-side confirmation. The
26
+ // honest sustained-outage state.
27
+ //
28
+ // State carried across polls: { display, failStreak, lastConfirmed }.
29
+ // display — the debounced value the card renders this poll.
30
+ // failStreak — how many consecutive raw failed polls we've seen.
31
+ // lastConfirmed — the last raw 'reachable' we proved (informational; the card
32
+ // holds 'reconnecting' during a pending sub-threshold streak).
33
+
34
+ // ~9s at the dashboard's 3s poll cadence, matching the broker debounce: enough
35
+ // to absorb a transient forward hiccup while still surfacing a real outage
36
+ // quickly. Named so retuning is a one-line edit.
37
+ const UNREACHABLE_DEBOUNCE_THRESHOLD = 3;
38
+
39
+ function debounceReachability(prev, rawReachable, opts) {
40
+ const threshold = (opts && opts.threshold) || UNREACHABLE_DEBOUNCE_THRESHOLD;
41
+ const gcpConfirmedReachable = !!(opts && opts.gcpConfirmedReachable);
42
+ const lastConfirmed = prev ? prev.lastConfirmed : undefined;
43
+
44
+ // Confirmed reachable: the editor answered now. Render immediately, reset the
45
+ // streak, remember this as the last-confirmed value.
46
+ if (rawReachable) {
47
+ return { display: 'reachable', failStreak: 0, lastConfirmed: 'reachable' };
48
+ }
49
+
50
+ // Raw failure: count the streak.
51
+ const failStreak = (prev ? prev.failStreak : 0) + 1;
52
+
53
+ // GCP-side confirmation: the editor answers over IAP while the local port is
54
+ // dark. That's a laptop tunnel fault, NOT a dead VM — never escalate to
55
+ // 'unreachable' no matter how long the local forward has been down; the
56
+ // watchdog respawns the forward. Hold the calm 'reconnecting' state.
57
+ if (gcpConfirmedReachable) {
58
+ return { display: 'reconnecting', failStreak, lastConfirmed };
59
+ }
60
+
61
+ // Sub-threshold failure: stay calm ('reconnecting'). Only a SUSTAINED failure
62
+ // past the threshold escalates to the honest 'unreachable'.
63
+ if (failStreak >= threshold) {
64
+ return { display: 'unreachable', failStreak, lastConfirmed };
65
+ }
66
+ return { display: 'reconnecting', failStreak, lastConfirmed };
67
+ }
68
+
69
+ module.exports = { debounceReachability, UNREACHABLE_DEBOUNCE_THRESHOLD };