rollbridge 0.1.77 → 0.1.79

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.
@@ -0,0 +1,5 @@
1
+ The version-drift owner replacement bridge now fences the committed legacy guardian
2
+ inventory against its own committed owner snapshot. Process registrations restored by
3
+ the replacement are allowed and carried into the staged owner state, while missing or
4
+ changed legacy registrations produce a classified refusal naming the expected and
5
+ observed keys and leave the incumbent owner in place.
@@ -0,0 +1 @@
1
+ Stale guardian socket paths are now reclaimed before startup, while live listeners are rejected with their owning PID.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollbridge",
3
- "version": "0.1.77",
3
+ "version": "0.1.79",
4
4
  "description": "Zero-downtime process supervisor and local traffic switcher for deploy-managed apps.",
5
5
  "keywords": [
6
6
  "deploy",
@@ -2,8 +2,6 @@
2
2
 
3
3
  import net from "node:net"
4
4
 
5
- const DEFAULT_CONTROL_COMMAND_TIMEOUT_MS = 30000
6
-
7
5
  /**
8
6
  * @typedef {import("./json.js").JsonValue} JsonValue
9
7
  */
@@ -13,15 +11,15 @@ const DEFAULT_CONTROL_COMMAND_TIMEOUT_MS = 30000
13
11
  * @param {object} args - Options.
14
12
  * @param {Record<string, JsonValue>} args.command - Command payload.
15
13
  * @param {string} args.path - Control socket path.
16
- * @param {number} [args.timeoutMs] - Maximum time to wait for a response.
14
+ * @param {number} [args.timeoutMs] - Optional maximum time to wait for a response. When omitted, wait for the daemon's terminal response.
17
15
  * @returns {Promise<Record<string, JsonValue>>} Response payload.
18
16
  */
19
- export async function sendControlCommand({command, path, timeoutMs = DEFAULT_CONTROL_COMMAND_TIMEOUT_MS}) {
17
+ export async function sendControlCommand({command, path, timeoutMs}) {
20
18
  return await new Promise((resolve, reject) => {
21
19
  const socket = net.createConnection(path)
22
20
  let buffer = ""
23
21
  let settled = false
24
- const timeout = setTimeout(() => {
22
+ const timeout = timeoutMs === undefined ? undefined : setTimeout(() => {
25
23
  socket.destroy()
26
24
  settleReject(new Error(`Rollbridge control command ${String(command.command)} on ${path} timed out after ${timeoutMs}ms`))
27
25
  }, timeoutMs)
@@ -30,7 +28,7 @@ export async function sendControlCommand({command, path, timeoutMs = DEFAULT_CON
30
28
  const settleReject = (error) => {
31
29
  if (settled) return
32
30
  settled = true
33
- clearTimeout(timeout)
31
+ if (timeout) clearTimeout(timeout)
34
32
  reject(error)
35
33
  }
36
34
 
@@ -38,7 +36,7 @@ export async function sendControlCommand({command, path, timeoutMs = DEFAULT_CON
38
36
  const settleResolve = (response) => {
39
37
  if (settled) return
40
38
  settled = true
41
- clearTimeout(timeout)
39
+ if (timeout) clearTimeout(timeout)
42
40
  resolve(response)
43
41
  }
44
42
 
package/src/daemon.js CHANGED
@@ -38,7 +38,7 @@ const STATE_PERSIST_INTERVAL_MS = 5000
38
38
  * @typedef {{configDigest: string, format: number, guardian: {pid?: number, socketPath: string, token: string}, reconnectGraceMs: number}} OwnerRecoveryMetadata
39
39
  * @typedef {DaemonStatus & {recovery: OwnerRecoveryMetadata, serviceReleaseIds?: Record<string, string>, singletonReleaseIds?: Record<string, string>}} OwnerRecoverySnapshot
40
40
  * @typedef {{authority: JsonValue, config: import("./config.js").RollbridgeConfig, listenerConnectionSources?: Record<string, Record<string, {http: number, websocket: number}>>, listenerSourceId?: string, recovery?: {command: JsonValue, reconnectGraceMs: number, startupTimeoutMs: number}, releaseConfigs?: Record<string, import("./config.js").RollbridgeConfig>, retiredListenerHandoff?: number, serviceReleaseIds?: Record<string, string>, singletonReleaseIds?: Record<string, string>, snapshot: OwnerRecoverySnapshot}} PrivateOwnerState
41
- * @typedef {{boundaryCrossed: boolean, guardianVersion?: string, incumbentControl?: Awaited<ReturnType<typeof openControlSession>>, incumbentStartTime: string, legacyGuardian: GuardianClient, legacyInventory: {key: string, provenance: string}[], legacyPrepared?: {ownerState: JsonValue, replacementId: string}, legacySnapshot: OwnerRecoverySnapshot, prepared: {ownerState: JsonValue, replacementId: string}, protocol: GuardianHandoffProtocol, recoverySnapshot: OwnerRecoverySnapshot}} LegacyOwnerBridge
41
+ * @typedef {{boundaryCrossed: boolean, guardianVersion?: string, incumbentControl?: Awaited<ReturnType<typeof openControlSession>>, incumbentStartTime: string, legacyGuardian: GuardianClient, legacyInventory: {key: string, provenance: string, status: import("./managed-process.js").ManagedProcessStatus}[], legacyPrepared?: {ownerState: JsonValue, replacementId: string}, legacySnapshot: OwnerRecoverySnapshot, prepared: {ownerState: JsonValue, replacementId: string}, protocol: GuardianHandoffProtocol, recoverySnapshot: OwnerRecoverySnapshot}} LegacyOwnerBridge
42
42
  */
43
43
 
44
44
  export default class RollbridgeDaemon {
@@ -1134,8 +1134,12 @@ export default class RollbridgeDaemon {
1134
1134
  }
1135
1135
  const currentInventory = normalizeLegacyGuardianInventory(await bridge.legacyGuardian.inventory())
1136
1136
 
1137
- if (!isDeepStrictEqual(currentInventory, bridge.legacyInventory)) {
1138
- throw new Error("Legacy guardian process inventory changed before the disruptive boundary")
1137
+ try {
1138
+ assertLegacyGuardianInventoryCarriedForward(currentInventory, bridge.legacyInventory)
1139
+ } catch (error) {
1140
+ const diagnostic = error instanceof Error ? error.message : String(error)
1141
+
1142
+ throw new Error(`Legacy owner replacement refused at the disruptive boundary: ${diagnostic}; incumbent owner and connections were preserved`, {cause: error})
1139
1143
  }
1140
1144
  const candidateOwnerState = {
1141
1145
  authority: this.ownerAuthority(),
@@ -1146,7 +1150,20 @@ export default class RollbridgeDaemon {
1146
1150
  snapshot: this.status()
1147
1151
  }
1148
1152
 
1149
- assertLegacyGuardianInventoryMembership(currentInventory, candidateOwnerState)
1153
+ // A legacy guardian keeps stopped registrations until its owner reconciles them. Validate
1154
+ // the registrations carried over from the committed legacy owner, not the replacement's
1155
+ // freshly reconstructed process set. The replacement may legitimately add registrations
1156
+ // while restoring a changed generation; stageOwnerReplacement carries that set forward.
1157
+ try {
1158
+ assertLegacyGuardianInventoryMembership(bridge.legacyInventory, {
1159
+ snapshot: bridge.legacySnapshot,
1160
+ singletonReleaseIds: bridge.legacySnapshot.singletonReleaseIds
1161
+ })
1162
+ } catch (error) {
1163
+ const diagnostic = error instanceof Error ? error.message : String(error)
1164
+
1165
+ throw new Error(`Legacy owner replacement refused at the disruptive boundary: ${diagnostic}; incumbent owner and connections were preserved`, {cause: error})
1166
+ }
1150
1167
  let legacyCommitted
1151
1168
 
1152
1169
  if (bridge.legacyPrepared) {
@@ -3540,15 +3557,15 @@ function reconstructableOwnerSnapshotProcessKeys(snapshot, singletonReleaseIds =
3540
3557
 
3541
3558
  /**
3542
3559
  * Canonicalizes the authenticated guardian inventory without relying on map insertion order.
3543
- * @param {{key: string, provenance: string}[]} inventory - Guardian inventory response.
3544
- * @returns {{key: string, provenance: string}[]} Exact key/provenance fence.
3560
+ * @param {{key: string, provenance: string, status: import("./managed-process.js").ManagedProcessStatus}[]} inventory - Guardian inventory response.
3561
+ * @returns {{key: string, provenance: string, status: import("./managed-process.js").ManagedProcessStatus}[]} Exact key/provenance/status fence.
3545
3562
  */
3546
3563
  function normalizeLegacyGuardianInventory(inventory) {
3547
3564
  const normalized = inventory.map((entry) => {
3548
3565
  if (!entry || typeof entry.key !== "string" || !entry.key || typeof entry.provenance !== "string" || !entry.provenance) {
3549
3566
  throw new Error("Legacy guardian returned an invalid process inventory")
3550
3567
  }
3551
- return {key: entry.key, provenance: entry.provenance}
3568
+ return {key: entry.key, provenance: entry.provenance, status: entry.status}
3552
3569
  }).sort((left, right) => left.key.localeCompare(right.key))
3553
3570
 
3554
3571
  if (new Set(normalized.map(({key}) => key)).size !== normalized.length) {
@@ -3559,19 +3576,58 @@ function normalizeLegacyGuardianInventory(inventory) {
3559
3576
 
3560
3577
  /**
3561
3578
  * Requires every and only the process registrations serialized by committed private owner state.
3562
- * @param {{key: string, provenance: string}[]} inventory - Canonical guardian inventory.
3579
+ * @param {{key: string, provenance: string, status: import("./managed-process.js").ManagedProcessStatus}[]} inventory - Canonical guardian inventory.
3563
3580
  * @param {{snapshot: DaemonStatus, singletonReleaseIds?: Record<string, string>}} ownerState - Authenticated owner process snapshot.
3581
+ * @param {{allowStopped?: boolean}} [options] - Whether known stopped registrations may remain during legacy handoff.
3564
3582
  */
3565
- function assertLegacyGuardianInventoryMembership(inventory, ownerState) {
3583
+ export function assertLegacyGuardianInventoryMembership(inventory, ownerState, {allowStopped = false} = {}) {
3566
3584
  if (!ownerState?.snapshot) throw new Error("Legacy guardian owner state is missing its committed process snapshot")
3567
- const inventoryKeys = inventory.map(({key}) => key)
3585
+ const inventoryKeys = inventory.filter(({status}) => !allowStopped || status.state !== "stopped").map(({key}) => key)
3568
3586
  const snapshotKeys = ownerSnapshotProcessKeys(ownerState.snapshot, ownerState.singletonReleaseIds).sort((left, right) => left.localeCompare(right))
3569
3587
 
3570
3588
  if (!isDeepStrictEqual(inventoryKeys, snapshotKeys)) {
3571
- throw new Error("Legacy guardian process inventory does not match committed owner state")
3589
+ const expected = new Set(snapshotKeys)
3590
+ const actual = new Set(inventoryKeys)
3591
+ const added = [...actual].filter((key) => !expected.has(key)).sort((left, right) => left.localeCompare(right))
3592
+ const removed = [...expected].filter((key) => !actual.has(key)).sort((left, right) => left.localeCompare(right))
3593
+
3594
+ throw new Error(`Legacy guardian process inventory does not match committed owner state (expected: ${formatLegacyKeys(snapshotKeys)}; actual: ${formatLegacyKeys(inventoryKeys)}; added: ${formatLegacyKeys(added)}; removed: ${formatLegacyKeys(removed)})`)
3572
3595
  }
3573
3596
  }
3574
3597
 
3598
+ /**
3599
+ * Ensures the registrations carried by the legacy guardian survive coordinator upgrade.
3600
+ * New registrations belong to the replacement and are intentionally allowed.
3601
+ * @param {{key: string, provenance: string, status: import("./managed-process.js").ManagedProcessStatus}[]} inventory - Current coordinator inventory.
3602
+ * @param {{key: string, provenance: string}[]} legacyInventory - Committed legacy inventory.
3603
+ */
3604
+ function assertLegacyGuardianInventoryCarriedForward(inventory, legacyInventory) {
3605
+ const current = new Map(inventory.map(({key, provenance}) => [key, provenance]))
3606
+ const expected = new Map(legacyInventory.map(({key, provenance}) => [key, provenance]))
3607
+ const missing = [...expected.keys()].filter((key) => !current.has(key)).sort((left, right) => left.localeCompare(right))
3608
+ const changed = [...expected.keys()].filter((key) => current.get(key) !== expected.get(key)).sort((left, right) => left.localeCompare(right))
3609
+
3610
+ if (missing.length > 0 || changed.length > 0) {
3611
+ throw new Error(`Legacy guardian process inventory changed before the disruptive boundary (expected: ${formatLegacyInventoryIdentity(legacyInventory)}; actual: ${formatLegacyInventoryIdentity(inventory)}; missing: ${formatLegacyKeys(missing)}; changed: ${formatLegacyKeys(changed)})`)
3612
+ }
3613
+ }
3614
+
3615
+ /**
3616
+ * @param {string[]} keys - Registration keys.
3617
+ * @returns {string} Diagnostic representation.
3618
+ */
3619
+ function formatLegacyKeys(keys) {
3620
+ return keys.length > 0 ? keys.join(", ") : "(none)"
3621
+ }
3622
+
3623
+ /**
3624
+ * @param {{key: string, provenance: string}[]} entries - Inventory identity entries.
3625
+ * @returns {string} Diagnostic representation.
3626
+ */
3627
+ function formatLegacyInventoryIdentity(entries) {
3628
+ return entries.length > 0 ? entries.map(({key, provenance}) => `${key} [${provenance}]`).join(", ") : "(none)"
3629
+ }
3630
+
3575
3631
  /**
3576
3632
  * Verifies the exact authenticated guardian process and socket without scanning other PIDs.
3577
3633
  * @param {number} pid - Persisted guardian PID.
@@ -1,5 +1,6 @@
1
1
  // @ts-check
2
2
 
3
+ import fs from "node:fs"
3
4
  import net from "node:net"
4
5
 
5
6
  /**
@@ -37,9 +38,10 @@ async function tryPort({host, port}) {
37
38
  * @param {string} args.host - Bind host.
38
39
  * @param {PortRange | undefined} args.range - Candidate range.
39
40
  * @param {Set<number>} args.usedPorts - Ports already allocated by this deploy.
41
+ * @param {string} [args.procPath] - Procfs root used to explain occupied ports.
40
42
  * @returns {Promise<number>} Available port.
41
43
  */
42
- export async function findAvailablePort({host, range, usedPorts}) {
44
+ export async function findAvailablePort({host, range, usedPorts, procPath = "/proc"}) {
43
45
  if (!range || range.from === 0 || range.to === 0) {
44
46
  const result = await tryPort({host, port: 0})
45
47
 
@@ -50,6 +52,8 @@ export async function findAvailablePort({host, range, usedPorts}) {
50
52
  }
51
53
 
52
54
  let reserved = 0
55
+ /** @type {{port: number, holder: string}[]} */
56
+ const holders = []
53
57
  let inUse = 0
54
58
  let unavailable = 0
55
59
  /** @type {string | undefined} */
@@ -70,6 +74,8 @@ export async function findAvailablePort({host, range, usedPorts}) {
70
74
 
71
75
  if (result.code === "EADDRINUSE") {
72
76
  inUse += 1
77
+ const holder = describePortHolder(port, procPath)
78
+ if (holder) holders.push({holder, port})
73
79
  } else {
74
80
  unavailable += 1
75
81
  lastErrorCode = result.code
@@ -79,6 +85,11 @@ export async function findAvailablePort({host, range, usedPorts}) {
79
85
  const total = range.to - range.from + 1
80
86
  const details = [`${reserved} reserved by this deploy`, `${inUse} already in use`]
81
87
 
88
+ if (holders.length > 0) {
89
+ details.push("holders: " + holders.map(({holder, port}) => port + " " + holder).join("; "))
90
+ details.push("reclaim Rollbridge-owned holders with `rollbridge predeploy-cleanup`")
91
+ }
92
+
82
93
  if (unavailable > 0) details.push(`${unavailable} could not be bound (e.g. ${lastErrorCode})`)
83
94
 
84
95
  throw new Error(
@@ -86,3 +97,72 @@ export async function findAvailablePort({host, range, usedPorts}) {
86
97
  `${details.join(", ")}. Widen the port range, free a port, or check bind permissions.`
87
98
  )
88
99
  }
100
+
101
+ /**
102
+ * Explains a listening socket using a procfs inode join. This is best-effort: lack of procfs
103
+ * access must not turn a normal EADDRINUSE into a different allocation failure.
104
+ * @param {number} port - Occupied TCP port.
105
+ * @param {string} procPath - Procfs root.
106
+ * @returns {string | undefined} Holder description.
107
+ */
108
+ function describePortHolder(port, procPath) {
109
+ try {
110
+ const inodes = new Set()
111
+ for (const table of ["tcp", "tcp6"]) {
112
+ let contents
113
+ try { contents = fs.readFileSync(procPath + "/net/" + table, "utf8") } catch { continue }
114
+ for (const line of contents.split("\n").slice(1)) {
115
+ const fields = line.trim().split(/\s+/)
116
+ if (fields.length < 10 || fields[3] !== "0A") continue
117
+ const localPort = Number.parseInt(fields[1].split(":")[1] || "", 16)
118
+ if (localPort === port) inodes.add(fields[9])
119
+ }
120
+ }
121
+ if (inodes.size === 0) return undefined
122
+
123
+ for (const entry of fs.readdirSync(procPath, {withFileTypes: true})) {
124
+ if (!entry.isDirectory() || !/^\d+$/.test(entry.name)) continue
125
+ const fdPath = procPath + "/" + entry.name + "/fd"
126
+ let descriptors
127
+ try { descriptors = fs.readdirSync(fdPath) } catch { continue }
128
+ const ownsSocket = descriptors.some((descriptor) => {
129
+ try {
130
+ const target = fs.readlinkSync(fdPath + "/" + descriptor)
131
+ return /^socket:\[\d+\]$/.test(target) && inodes.has(target.slice(8, -1))
132
+ } catch { return false }
133
+ })
134
+ if (!ownsSocket) continue
135
+
136
+ const command = readProcText(procPath + "/" + entry.name + "/cmdline").replaceAll("\u0000", " ").trim() || readProcText(procPath + "/" + entry.name + "/comm").trim() || "unknown command"
137
+ const environment = readProcEnvironment(procPath + "/" + entry.name + "/environ")
138
+ const provenance = [
139
+ environment.ROLLBRIDGE_APPLICATION && "application=" + environment.ROLLBRIDGE_APPLICATION,
140
+ environment.ROLLBRIDGE_RELEASE_ID && "release=" + environment.ROLLBRIDGE_RELEASE_ID,
141
+ environment.ROLLBRIDGE_PROCESS_ID && "process=" + environment.ROLLBRIDGE_PROCESS_ID
142
+ ].filter(Boolean).join(", ")
143
+
144
+ return "pid " + Number(entry.name) + " (" + command + (provenance ? "; " + provenance : "; provenance=unmanaged/unknown") + ")"
145
+ }
146
+ } catch { return undefined }
147
+
148
+ return undefined
149
+ }
150
+
151
+ /**
152
+ * @param {string} filePath - Procfs text file.
153
+ * @returns {string} Contents or empty text.
154
+ */
155
+ function readProcText(filePath) {
156
+ try { return fs.readFileSync(filePath, "utf8") } catch { return "" }
157
+ }
158
+
159
+ /**
160
+ * @param {string} filePath - Procfs environment file.
161
+ * @returns {Record<string, string>} Environment.
162
+ */
163
+ function readProcEnvironment(filePath) {
164
+ return Object.fromEntries(readProcText(filePath).split("\u0000").flatMap((entry) => {
165
+ const separator = entry.indexOf("=")
166
+ return separator > 0 ? [[entry.slice(0, separator), entry.slice(separator + 1)]] : []
167
+ }))
168
+ }
@@ -238,6 +238,13 @@ server.on("error", (error) => {
238
238
  else throw error
239
239
  })
240
240
 
241
+ try {
242
+ await prepareSocketPath(socketPath)
243
+ } catch (error) {
244
+ if (process.send) process.send({error: error instanceof Error ? error.message : String(error)})
245
+ throw error
246
+ }
247
+
241
248
  server.listen(socketPath, async () => {
242
249
  await fs.chmod(socketPath, 0o600)
243
250
  if (process.send) {
@@ -246,6 +253,103 @@ server.listen(socketPath, async () => {
246
253
  }
247
254
  })
248
255
 
256
+ /**
257
+ * Ensures the guardian socket path is either unused or a stale filesystem entry.
258
+ * @param {string} candidatePath - Exact guardian socket path.
259
+ * @returns {Promise<void>} Resolves once the path is ready for bind.
260
+ */
261
+ async function prepareSocketPath(candidatePath) {
262
+ try {
263
+ await fs.lstat(candidatePath)
264
+ } catch (error) {
265
+ if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return
266
+ throw error
267
+ }
268
+
269
+ const listener = await probeUnixListener(candidatePath)
270
+
271
+ if (listener === true) {
272
+ const pid = await unixSocketOwnerPid(candidatePath)
273
+ throw new Error(`Guardian socket ${candidatePath} is already owned by a live listener${pid === undefined ? "" : ` (pid ${pid})`}`)
274
+ }
275
+ if (listener === undefined) throw new Error(`Could not determine whether guardian socket ${candidatePath} is live; refusing to remove it`)
276
+
277
+ try {
278
+ await fs.unlink(candidatePath)
279
+ } catch (error) {
280
+ if (!error || typeof error !== "object" || !("code" in error) || error.code !== "ENOENT") throw error
281
+ }
282
+ }
283
+
284
+ /**
285
+ * Probes whether anything is listening on a Unix socket path.
286
+ * @param {string} candidatePath - Exact socket path.
287
+ * @returns {Promise<boolean | undefined>} True for a live listener, false for a stale path, undefined when inconclusive.
288
+ */
289
+ function probeUnixListener(candidatePath) {
290
+ return new Promise((resolve) => {
291
+ const socket = net.createConnection(candidatePath)
292
+ let settled = false
293
+
294
+ /** @param {boolean | undefined} result - Probe result. */
295
+ const finish = (result) => {
296
+ if (settled) return
297
+ settled = true
298
+ clearTimeout(timer)
299
+ socket.destroy()
300
+ resolve(result)
301
+ }
302
+
303
+ const timer = setTimeout(() => finish(undefined), 1000)
304
+
305
+ socket.once("connect", () => finish(true))
306
+ socket.once("error", (error) => {
307
+ const code = error && typeof error === "object" && "code" in error ? error.code : undefined
308
+ finish(code === "ECONNREFUSED" || code === "ENOENT" ? false : undefined)
309
+ })
310
+ })
311
+ }
312
+
313
+ /**
314
+ * Finds the process holding the exact Unix socket inode.
315
+ * @param {string} candidatePath - Exact socket path.
316
+ * @returns {Promise<number | undefined>} Owning PID when discoverable.
317
+ */
318
+ async function unixSocketOwnerPid(candidatePath) {
319
+ const rows = (await fs.readFile("/proc/net/unix", "utf8")).split("\n").map((line) => line.trim().split(/\s+/))
320
+ const row = rows.find((columns) => columns[7] === candidatePath)
321
+ const inode = row?.[6]
322
+
323
+ if (!inode) return undefined
324
+
325
+ let entries
326
+ try {
327
+ entries = await fs.readdir("/proc")
328
+ } catch {
329
+ return undefined
330
+ }
331
+
332
+ for (const entry of entries) {
333
+ if (!/^\d+$/.test(entry)) continue
334
+
335
+ let descriptors
336
+ try {
337
+ descriptors = await fs.readdir(`/proc/${entry}/fd`)
338
+ } catch {
339
+ continue
340
+ }
341
+ for (const descriptor of descriptors) {
342
+ try {
343
+ if (await fs.readlink(`/proc/${entry}/fd/${descriptor}`) === `socket:[${inode}]`) return Number(entry)
344
+ } catch {
345
+ // Processes can exit between reading their fd directory and its entry.
346
+ }
347
+ }
348
+ }
349
+
350
+ return undefined
351
+ }
352
+
249
353
  /**
250
354
  * @param {net.Socket} socket - Client.
251
355
  * @param {string} line - JSON request.
@@ -123,6 +123,29 @@ test("a control connection closed during owner handoff rejects the command", asy
123
123
  }
124
124
  })
125
125
 
126
+ test("control commands wait for the daemon terminal response when no timeout is configured", async () => {
127
+ const delayedSocketPath = path.join(root, "delayed.sock")
128
+ const delayedServer = net.createServer((socket) => {
129
+ socket.on("data", () => {
130
+ setTimeout(() => {
131
+ socket.end('{"status":"success","message":"terminal"}\n')
132
+ }, 50)
133
+ })
134
+ })
135
+
136
+ await new Promise((resolve, reject) => {
137
+ delayedServer.once("error", reject)
138
+ delayedServer.listen(delayedSocketPath, () => resolve(undefined))
139
+ })
140
+
141
+ try {
142
+ await expect(sendControlCommand({command: {command: "deploy"}, path: delayedSocketPath})).resolves.toEqual({status: "success", message: "terminal"})
143
+ } finally {
144
+ await new Promise((resolve) => delayedServer.close(resolve))
145
+ await fs.rm(delayedSocketPath, {force: true})
146
+ }
147
+ })
148
+
126
149
  test("status can omit process logs without changing the default status payload", async () => {
127
150
  if (!daemon) throw new Error("Missing required fixture: daemon")
128
151
  await daemon.deploy({releaseId: "v1", releasePath: root, revision: "v1"})
@@ -3,6 +3,7 @@
3
3
  import {spawn} from "node:child_process"
4
4
  import {once} from "node:events"
5
5
  import fs from "node:fs/promises"
6
+ import net from "node:net"
6
7
  import os from "node:os"
7
8
  import path from "node:path"
8
9
  import {describe, expect, test} from "@velocious/testing"
@@ -34,6 +35,36 @@ test("guardian maps an overlong configured socket path to a stable short path",
34
35
  }
35
36
  })
36
37
 
38
+ test("guardian reclaims a stale socket path before binding", async () => {
39
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), "rollbridge-stale-guardian-"))
40
+ const socketPath = path.join(root, "guardian.sock")
41
+ const client = new GuardianClient({socketPath, token: "stale-socket-capability"})
42
+
43
+ try {
44
+ await fs.writeFile(socketPath, "stale guardian path")
45
+ await client.launch()
46
+ expect(await client.capabilities()).toEqual({daemonRecovery: 1, generationReactivation: 1, guardianVersion: packageVersion})
47
+ } finally {
48
+ await cleanupGuardian({client, root})
49
+ }
50
+ })
51
+
52
+ test("guardian refuses a live socket path and names its owning PID", async () => {
53
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), "rollbridge-live-guardian-"))
54
+ const socketPath = path.join(root, "guardian.sock")
55
+ const incumbent = net.createServer()
56
+
57
+ try {
58
+ await once(incumbent.listen(socketPath), "listening")
59
+ const client = new GuardianClient({socketPath, token: "live-socket-capability"})
60
+
61
+ await expect(client.launch()).rejects.toThrow(new RegExp(`Guardian socket .* is already owned by a live listener \\(pid ${process.pid}\\)`))
62
+ } finally {
63
+ await new Promise((resolve) => incumbent.close(resolve))
64
+ await fs.rm(root, {force: true, recursive: true})
65
+ }
66
+ })
67
+
37
68
  test("guardian bootstrap capability is absent from process argv", async () => {
38
69
  const fixture = await createGuardian()
39
70
  const packageVersion = JSON.parse(await fs.readFile(path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "package.json"), "utf8")).version
@@ -10,13 +10,48 @@ import {describe, expect, test} from "@velocious/testing"
10
10
  import {fileURLToPath} from "node:url"
11
11
  import {normalizeConfig} from "../src/config.js"
12
12
  import {openControlSession, sendControlCommand} from "../src/control-client.js"
13
- import RollbridgeDaemon, {isLegacyGuardianPrepareDiagnostic, ownerConfigDigest, ownerReplacementTransitionAuthorityMatches} from "../src/daemon.js"
13
+ import RollbridgeDaemon, {assertLegacyGuardianInventoryMembership, isLegacyGuardianPrepareDiagnostic, ownerConfigDigest, ownerReplacementTransitionAuthorityMatches} from "../src/daemon.js"
14
14
  import GuardianClient from "../src/guardian-client.js"
15
15
  import {findAvailablePort} from "../src/port-allocator.js"
16
16
  import {isProcessRunning, waitForProcessExit} from "./support/process.js"
17
17
 
18
18
  describe("owner-replacement", () => {
19
19
 
20
+ test("legacy owner proof ignores only stopped registrations omitted by replacement reconstruction", () => {
21
+ const ownerState = /** @type {Parameters<typeof assertLegacyGuardianInventoryMembership>[1]} */ (/** @type {import("../src/json.js").JsonValue} */ ({
22
+ snapshot: {
23
+ activeReleaseId: "active",
24
+ releases: [
25
+ {releaseId: "stopped", processes: [{id: "web"}], state: "stopped"},
26
+ {releaseId: "active", processes: [{id: "web"}], state: "active"}
27
+ ],
28
+ services: [],
29
+ singletons: []
30
+ }
31
+ }))
32
+ const inventory = /** @type {Parameters<typeof assertLegacyGuardianInventoryMembership>[0]} */ (/** @type {import("../src/json.js").JsonValue} */ ([
33
+ {key: "release:active:web", provenance: "active-provenance", status: {state: "running"}},
34
+ {key: "release:stopped:web", provenance: "stopped-provenance", status: {state: "stopped"}}
35
+ ]))
36
+ const unexpectedStopped = /** @type {Parameters<typeof assertLegacyGuardianInventoryMembership>[0]} */ (/** @type {import("../src/json.js").JsonValue} */ ([
37
+ {key: "release:unexpected:web", provenance: "unexpected-provenance", status: {state: "stopped"}},
38
+ inventory[0]
39
+ ]))
40
+ const unexpectedLive = /** @type {Parameters<typeof assertLegacyGuardianInventoryMembership>[0]} */ (/** @type {import("../src/json.js").JsonValue} */ ([
41
+ {key: "release:unexpected:web", provenance: "unexpected-provenance", status: {state: "running"}},
42
+ inventory[1]
43
+ ]))
44
+
45
+ expect(() => assertLegacyGuardianInventoryMembership(inventory, ownerState)).not.toThrow()
46
+ expect(() => assertLegacyGuardianInventoryMembership(unexpectedStopped, ownerState, {allowStopped: true})).toThrow(/inventory does not match.*expected: release:active:web.*actual: release:active:web.*removed: release:stopped:web/)
47
+ expect(() => assertLegacyGuardianInventoryMembership(inventory, {
48
+ snapshot: {...ownerState.snapshot, releases: [ownerState.snapshot.releases[1]]}
49
+ }, {allowStopped: true})).not.toThrow()
50
+ expect(() => assertLegacyGuardianInventoryMembership(unexpectedLive, {
51
+ snapshot: {...ownerState.snapshot, releases: [ownerState.snapshot.releases[1]]}
52
+ }, {allowStopped: true})).toThrow(/inventory does not match.*removed: release:active:web/)
53
+ })
54
+
20
55
  const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..")
21
56
  const binPath = path.join(repoRoot, "bin", "rollbridge")
22
57
  const dummyAppPath = path.join(repoRoot, "test", "fixtures", "dummy-app.js")
@@ -2171,7 +2206,7 @@ async function prepareCandidatePackage(destination, options = {}) {
2171
2206
  if (dropCommitResponse) {
2172
2207
  const clientPath = path.join(destination, "src", "control-client.js")
2173
2208
  const source = await fs.readFile(clientPath, "utf8")
2174
- const marker = "export async function sendControlCommand({command, path, timeoutMs = DEFAULT_CONTROL_COMMAND_TIMEOUT_MS}) {\n"
2209
+ const marker = "export async function sendControlCommand({command, path, timeoutMs}) {\n"
2175
2210
  const injected = `${marker} if (command.command === "commit-owner-replacement") {\n return await new Promise((resolve, reject) => {\n const socket = net.createConnection(path)\n socket.once("error", reject)\n socket.once("data", () => { socket.destroy(); reject(new Error("injected lost commit response")) })\n socket.once("connect", () => socket.write(\`${"${JSON.stringify(command)}"}\\n\`))\n })\n }\n\n`
2176
2211
  const sessionMarker = " this.socket.write(`${JSON.stringify(command)}\\n`)\n return await response\n"
2177
2212
  const sessionInjection = " this.socket.write(`${JSON.stringify(command)}\\n`)\n const result = await response\n if (command.command === \"commit-owner-replacement\") throw new Error(\"injected lost commit response\")\n return result\n"
@@ -1,6 +1,9 @@
1
1
  // @ts-check
2
2
 
3
3
  import net from "node:net"
4
+ import fs from "node:fs/promises"
5
+ import os from "node:os"
6
+ import path from "node:path"
4
7
  import {describe, expect, test} from "@velocious/testing"
5
8
  import {findAvailablePort} from "../src/port-allocator.js"
6
9
 
@@ -70,4 +73,42 @@ test("findAvailablePort skips the occupied port and records the allocated one",
70
73
  await closeServer(server)
71
74
  }
72
75
  })
76
+
77
+ test("findAvailablePort attributes an occupied port to its procfs holder and provenance", async () => {
78
+ const {port, server} = await occupyPort()
79
+ const procPath = await fs.mkdtemp(path.join(os.tmpdir(), "rollbridge-port-proc-"))
80
+ const pid = "1234"
81
+ const nul = String.fromCharCode(0)
82
+
83
+ try {
84
+ await fs.mkdir(path.join(procPath, "net"))
85
+ await fs.mkdir(path.join(procPath, pid, "fd"), {recursive: true})
86
+ const portHex = port.toString(16).toUpperCase().padStart(4, "0")
87
+ await fs.writeFile(path.join(procPath, "net", "tcp"), " sl local_address rem_address st tx_queue tr tm->when retrnsmt uid timeout inode\n 0: 0100007F:" + portHex + " 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 98765 1\n")
88
+ await fs.symlink("socket:[98765]", path.join(procPath, pid, "fd", "3"))
89
+ await fs.writeFile(path.join(procPath, pid, "cmdline"), "velocious" + nul + "beacon" + nul)
90
+ await fs.writeFile(path.join(procPath, pid, "comm"), "velocious beacon\n")
91
+ await fs.writeFile(path.join(procPath, pid, "environ"), "ROLLBRIDGE_APPLICATION=demo" + nul + "ROLLBRIDGE_RELEASE_ID=old" + nul + "ROLLBRIDGE_PROCESS_ID=beacon" + nul)
92
+
93
+ const allocation = findAvailablePort({host, procPath, range: {from: port, to: port}, usedPorts: new Set()})
94
+
95
+ await expect(allocation).rejects.toMatchObject({message: expect.stringMatching(/pid 1234 .*application=demo, release=old, process=beacon/)})
96
+ await expect(allocation).rejects.toMatchObject({message: expect.stringMatching(/reclaim Rollbridge-owned holders/)})
97
+ } finally {
98
+ await closeServer(server)
99
+ await fs.rm(procPath, {force: true, recursive: true})
100
+ }
101
+ })
102
+
103
+ test("findAvailablePort keeps the normal error when procfs cannot explain a holder", async () => {
104
+ const {port, server} = await occupyPort()
105
+
106
+ try {
107
+ const allocation = findAvailablePort({host, procPath: "/does/not/exist", range: {from: port, to: port}, usedPorts: new Set()})
108
+
109
+ await expect(allocation).rejects.toMatchObject({message: expect.stringMatching(/1 already in use/)})
110
+ } finally {
111
+ await closeServer(server)
112
+ }
113
+ })
73
114
  })