rollbridge 0.1.34 → 0.1.35

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.
@@ -1,8 +1,9 @@
1
1
  ### Fixed
2
2
 
3
- - Carry an exact recovered guardian process key when a ready replacement commits
4
- after the incumbent control listener has already retired, while retaining the
5
- replacement transaction, authority, and registered-process fences.
3
+ - Select retired-owner commit proof from the guardian-published committed owner
4
+ snapshot rather than candidate-local process order, and independently require
5
+ that proof to remain registered while retaining the replacement transaction,
6
+ authority, and control-path fences.
6
7
  - Fail closed when an older retained guardian cannot commit that replacement
7
8
  atomically after the incumbent control socket disappears, preserving the
8
9
  incumbent owner, retained connections, and guardian-managed release processes.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollbridge",
3
- "version": "0.1.34",
3
+ "version": "0.1.35",
4
4
  "description": "Zero-downtime process supervisor and local traffic switcher for deploy-managed apps.",
5
5
  "keywords": [
6
6
  "deploy",
package/src/daemon.js CHANGED
@@ -416,9 +416,11 @@ export default class RollbridgeDaemon {
416
416
 
417
417
  if (!staged.committed) {
418
418
  if (retiredIncumbentControl) {
419
- const processKey = this.guardian.processes.keys().next().value
419
+ const recoveredProcesses = this.guardian.processes
420
+ const processKey = ownerSnapshotProcessKeys(transfer.snapshot, transfer.singletonReleaseIds)
421
+ .find((key) => recoveredProcesses.has(key))
420
422
 
421
- if (!processKey) throw new Error("Retired owner replacement requires an exact recovered guardian process registration")
423
+ if (!processKey) throw new Error("Retired owner replacement requires an exact recovered process from committed owner state")
422
424
  try {
423
425
  await this.guardian.commitRetiredOwnerReplacement(prepared.replacementId, processKey)
424
426
  } catch (error) {
@@ -544,7 +546,7 @@ export default class RollbridgeDaemon {
544
546
  */
545
547
  async prepareLegacyOwnerReplacement({error, persisted, persistedAuthority}) {
546
548
  const diagnostic = error instanceof Error ? error.message : String(error)
547
- const legacyProcessKey = legacyGuardianKeys(persisted)[0]
549
+ const legacyProcessKey = ownerSnapshotProcessKeys(persisted, persisted.singletonReleaseIds)[0]
548
550
 
549
551
  if (!isLegacyGuardianPrepareDiagnostic(diagnostic)) throw error
550
552
  if (!legacyProcessKey) throw new Error("Legacy disruptive owner replacement requires an exact guardian-owned process registration in durable state", {cause: error})
@@ -1996,18 +1998,21 @@ export function isLegacyGuardianPrepareDiagnostic(diagnostic) {
1996
1998
  }
1997
1999
 
1998
2000
  /**
1999
- * @param {OwnerRecoverySnapshot} snapshot - Durable pre-split snapshot.
2001
+ * @param {OwnerRecoverySnapshot} snapshot - Durable committed owner snapshot.
2002
+ * @param {Record<string, string>} [singletonReleaseIds] - Exact singleton owner releases.
2000
2003
  * @returns {string[]} Exact guardian registration keys present in the snapshot.
2001
2004
  */
2002
- function legacyGuardianKeys(snapshot) {
2005
+ function ownerSnapshotProcessKeys(snapshot, singletonReleaseIds = {}) {
2003
2006
  const keys = []
2004
2007
 
2005
2008
  for (const release of snapshot.releases) {
2006
2009
  for (const processStatus of release.processes) keys.push(`release:${release.releaseId}:${processStatus.id}`)
2007
2010
  }
2008
2011
  for (const service of snapshot.services) keys.push(`service:${service.id}`)
2009
- if (snapshot.activeReleaseId) {
2010
- for (const singleton of snapshot.singletons) keys.push(`singleton:${snapshot.activeReleaseId}:${singleton.id}`)
2012
+ for (const singleton of snapshot.singletons) {
2013
+ const releaseId = singletonReleaseIds[singleton.id] || snapshot.activeReleaseId
2014
+
2015
+ if (releaseId) keys.push(`singleton:${releaseId}:${singleton.id}`)
2011
2016
  }
2012
2017
  return keys
2013
2018
  }
@@ -68,7 +68,7 @@ let ownerState = bootstrap.ownerState
68
68
  let ownerRevision = ownerState === undefined ? 0 : 1
69
69
  /** @type {number | undefined} */
70
70
  let replacementRevision
71
- const legacyKeys = legacyGuardian ? legacyOwnerKeys(ownerState) : new Set()
71
+ const legacyKeys = legacyGuardian ? committedOwnerProcessKeys(ownerState) : new Set()
72
72
  /** @type {net.Socket | undefined} */
73
73
  let ownerMutationClient
74
74
  /** @type {string | undefined} */
@@ -301,6 +301,9 @@ async function execute(request, socket) {
301
301
  if (request.command === "commit-retired-owner-replacement") {
302
302
  requireReplacement(socket, request)
303
303
  requireProcess(request)
304
+ if (!committedOwnerProcessKeys(ownerState).has(/** @type {string} */ (request.key))) {
305
+ throw new Error(`Guardian process ${request.key} does not belong to the committed owner`)
306
+ }
304
307
  if (!replacementOwnerState) throw new Error("Retired owner replacement transaction is not staged")
305
308
  if (!isDeepStrictEqual(ownerAuthority(ownerState), replacementAuthority)) throw new Error("Retired owner replacement requires unchanged owner authority")
306
309
  const controlPath = ownerControlPath(ownerState)
@@ -622,11 +625,11 @@ function errorMessage(error) {
622
625
  }
623
626
 
624
627
  /**
625
- * Derives only exact process keys present in a committed pre-split durable snapshot.
626
- * @param {import("./json.js").JsonValue | undefined} state - Seeded owner state.
627
- * @returns {Set<string>} Exact legacy registrations eligible for recovery.
628
+ * Derives only exact process keys serialized by committed private owner state.
629
+ * @param {import("./json.js").JsonValue | undefined} state - Committed owner state.
630
+ * @returns {Set<string>} Exact committed registrations eligible as owner proof.
628
631
  */
629
- function legacyOwnerKeys(state) {
632
+ function committedOwnerProcessKeys(state) {
630
633
  const keys = new Set()
631
634
 
632
635
  if (!state || typeof state !== "object" || Array.isArray(state) || !("snapshot" in state)) return keys
@@ -646,9 +649,18 @@ function legacyOwnerKeys(state) {
646
649
  if (service && typeof service === "object" && !Array.isArray(service) && typeof service.id === "string") keys.add(`service:${service.id}`)
647
650
  }
648
651
  }
649
- if (typeof snapshot.activeReleaseId === "string" && Array.isArray(snapshot.singletons)) {
652
+ const singletonReleaseIds = "singletonReleaseIds" in state && state.singletonReleaseIds && typeof state.singletonReleaseIds === "object" && !Array.isArray(state.singletonReleaseIds)
653
+ ? state.singletonReleaseIds
654
+ : {}
655
+
656
+ if (Array.isArray(snapshot.singletons)) {
650
657
  for (const singleton of snapshot.singletons) {
651
- if (singleton && typeof singleton === "object" && !Array.isArray(singleton) && typeof singleton.id === "string") keys.add(`singleton:${snapshot.activeReleaseId}:${singleton.id}`)
658
+ if (!singleton || typeof singleton !== "object" || Array.isArray(singleton) || typeof singleton.id !== "string") continue
659
+ const releaseId = singleton.id in singletonReleaseIds && typeof singletonReleaseIds[singleton.id] === "string"
660
+ ? singletonReleaseIds[singleton.id]
661
+ : snapshot.activeReleaseId
662
+
663
+ if (typeof releaseId === "string") keys.add(`singleton:${releaseId}:${singleton.id}`)
652
664
  }
653
665
  }
654
666
  return keys
@@ -224,6 +224,42 @@ test("retired owner replacement commit carries its exact recovered process key",
224
224
  await client.commitRetiredOwnerReplacement(replacementId, processKey)
225
225
  })
226
226
 
227
+ test("retired owner replacement rejects a registered process absent from committed owner state", async () => {
228
+ const fixture = await createGuardian()
229
+ const candidate = new GuardianClient({socketPath: fixture.client.socketPath, token: fixture.token})
230
+ const committedProcessKey = "release:v1:worker"
231
+ const candidateProcessKey = "release:candidate:worker"
232
+ const authority = {configDigest: "owner", runtime: null}
233
+ const snapshot = {
234
+ activeReleaseId: "v1",
235
+ control: {path: path.join(fixture.root, "rollbridge.sock")},
236
+ releases: [{processes: [{id: "worker"}], releaseId: "v1"}],
237
+ services: [],
238
+ singletons: []
239
+ }
240
+ const candidateSnapshot = {
241
+ ...snapshot,
242
+ releases: [...snapshot.releases, {processes: [{id: "worker"}], releaseId: "candidate"}]
243
+ }
244
+
245
+ try {
246
+ await fixture.client.process(committedProcessKey, definition("worker")).recover()
247
+ await fixture.client.process(candidateProcessKey, definition("candidate-worker")).recover()
248
+ await fixture.client.publishOwnerState({authority, snapshot})
249
+ await candidate.connect()
250
+ const prepared = await candidate.prepareOwnerReplacement(authority, authority)
251
+
252
+ await candidate.stageOwnerReplacement(prepared.replacementId, {authority, snapshot: candidateSnapshot})
253
+ await assert.rejects(
254
+ () => candidate.commitRetiredOwnerReplacement(prepared.replacementId, candidateProcessKey),
255
+ /process .* does not belong to the committed owner/
256
+ )
257
+ } finally {
258
+ candidate.disconnect()
259
+ await cleanupGuardian(fixture)
260
+ }
261
+ })
262
+
227
263
  test("retired owner replacement requires unchanged authority and the exact control path absent", async () => {
228
264
  const fixture = await createGuardian()
229
265
  const candidate = new GuardianClient({socketPath: fixture.client.socketPath, token: fixture.token})
@@ -232,7 +268,13 @@ test("retired owner replacement requires unchanged authority and the exact contr
232
268
  const processKey = "release:v1:worker"
233
269
  const authority = {configDigest: "incumbent", runtime: null}
234
270
  const nextAuthority = {configDigest: "candidate", runtime: null}
235
- const snapshot = {activeReleaseId: "v1", control: {path: controlPath}}
271
+ const snapshot = {
272
+ activeReleaseId: "v1",
273
+ control: {path: controlPath},
274
+ releases: [{processes: [{id: "worker"}], releaseId: "v1"}],
275
+ services: [],
276
+ singletons: []
277
+ }
236
278
 
237
279
  try {
238
280
  await fixture.client.process(processKey, definition("worker")).recover()
@@ -260,6 +302,10 @@ test("retired owner replacement requires unchanged authority and the exact contr
260
302
  () => contender.request({command: "commit-retired-owner-replacement", key: processKey, replacementId: ready.replacementId}),
261
303
  /not the prepared candidate/
262
304
  )
305
+ await assert.rejects(
306
+ () => candidate.commitRetiredOwnerReplacement("stale-replacement", processKey),
307
+ /not the prepared candidate/
308
+ )
263
309
  await assert.rejects(
264
310
  () => candidate.commitRetiredOwnerReplacement(ready.replacementId, "release:v1:wrong"),
265
311
  /process .* is not registered/
@@ -259,6 +259,7 @@ test("cross-version replacement fails closed without dropping a retained WebSock
259
259
  const compatibilitySockets = new Set()
260
260
  let compatibilityGuardian
261
261
  let committedProcessKey
262
+ let retainedGuardianSocketPath = /** @type {string | undefined} */ (undefined)
262
263
  let owner = /** @type {import("node:child_process").ChildProcess | undefined} */ (undefined)
263
264
  let replacement = /** @type {RollbridgeDaemon | undefined} */ (undefined)
264
265
  let retainedConnection
@@ -287,6 +288,9 @@ test("cross-version replacement fails closed without dropping a retained WebSock
287
288
  const guardianSocketPath = state.recovery.guardian.socketPath
288
289
  const expectedProcessKey = "release:v1:worker"
289
290
 
291
+ if (typeof guardianSocketPath !== "string") throw new Error("Retained guardian state is missing its socket path")
292
+ retainedGuardianSocketPath = guardianSocketPath
293
+
290
294
  compatibilityGuardian = net.createServer((candidateSocket) => {
291
295
  const guardianSocket = net.createConnection(guardianSocketPath)
292
296
  let buffer = ""
@@ -378,6 +382,87 @@ test("cross-version replacement fails closed without dropping a retained WebSock
378
382
  replacement?.guardian?.disconnect()
379
383
  for (const socket of compatibilitySockets) socket.destroy()
380
384
  if (compatibilityGuardian?.listening) await closeServer(compatibilityGuardian)
385
+ if (retainedGuardianSocketPath) {
386
+ const cleanupState = JSON.parse(await fs.readFile(statePath, "utf8"))
387
+
388
+ cleanupState.recovery.guardian.socketPath = retainedGuardianSocketPath
389
+ await fs.writeFile(statePath, `${JSON.stringify(cleanupState)}\n`)
390
+ }
391
+ await stopGuardian(statePath)
392
+ await fs.rm(root, {force: true, recursive: true})
393
+ }
394
+ })
395
+
396
+ test("same-authority replacement commits with committed-owner proof after the incumbent control socket is removed", async () => {
397
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), "rollbridge-owner-replacement-committed-proof-"))
398
+ const socketPath = path.join(root, "rollbridge.sock")
399
+ const statePath = path.join(root, "state.json")
400
+ const releasePath = path.join(root, "v1")
401
+ const daemonConfig = normalizeConfig(config({controlPath: socketPath, extraCompanion: false, statePath}))
402
+ const owner = new RollbridgeDaemon({config: daemonConfig, logger: () => {}})
403
+ const candidateProcessKey = "release:candidate:worker"
404
+ const committedOwnerProcessKey = "release:v1:worker"
405
+ let committedProcessKey
406
+ /** @type {RollbridgeDaemon | undefined} */
407
+ let replacement
408
+
409
+ try {
410
+ await fs.mkdir(releasePath)
411
+ await makeFifo(path.join(releasePath, "worker.fifo"))
412
+ await owner.start()
413
+ await owner.deploy({releaseId: "v1", releasePath, revision: "v1"})
414
+ const ownerProcess = owner.guardian?.processes.values().next().value
415
+
416
+ assert.ok(owner.guardian)
417
+ assert.ok(ownerProcess)
418
+ await owner.guardian.request({
419
+ command: "register",
420
+ definition: ownerProcess.definition,
421
+ key: candidateProcessKey,
422
+ provenance: ownerProcess.provenance
423
+ })
424
+ await Promise.all([...owner.releases.values()].map((release) => release.quiesce()))
425
+ await owner.closeServer(owner.controlServer)
426
+ await owner.removeControlSocket()
427
+ await owner.closeServer(owner.proxyServer)
428
+ const retired = owner.status()
429
+ const processState = retired.releases[0]?.processes.map(({id, pid, state}) => ({id, pid, state}))
430
+
431
+ assert.deepEqual(processState?.map(({state}) => state), ["quiesced", "quiesced"])
432
+ assert.equal((await owner.guardian?.replacementStatus())?.ownerClaimed, true)
433
+ await assert.rejects(fs.access(socketPath), {code: "ENOENT"})
434
+
435
+ replacement = new RollbridgeDaemon({
436
+ config: daemonConfig,
437
+ logger: (message) => {
438
+ if (message !== "owner replacement candidate prepared" || !replacement?.guardian) return
439
+ const guardian = replacement.guardian
440
+ const recoveredProcess = guardian.processes.values().next().value
441
+
442
+ if (!recoveredProcess) throw new Error("Replacement did not reconstruct a guardian process")
443
+ guardian.processes = new Map([[candidateProcessKey, recoveredProcess], ...guardian.processes])
444
+ const commitRetiredOwnerReplacement = guardian.commitRetiredOwnerReplacement.bind(guardian)
445
+
446
+ guardian.commitRetiredOwnerReplacement = async (replacementId, processKey) => {
447
+ committedProcessKey = processKey
448
+ await commitRetiredOwnerReplacement(replacementId, processKey)
449
+ }
450
+ }
451
+ })
452
+ await replacement.replaceIncompatibleOwner()
453
+ const recovered = await sendControlCommand({command: {command: "status"}, path: socketPath})
454
+ const recoveredReleases = /** @type {{processes: {id: string, pid?: number, state: string}[]}[]} */ (recovered.releases)
455
+
456
+ assert.equal(recovered.activeReleaseId, "v1")
457
+ assert.equal(committedProcessKey, committedOwnerProcessKey)
458
+ assert.deepEqual(recovered.releaseReferences, [{releaseId: "v1", releasePath}])
459
+ assert.deepEqual(recoveredReleases[0]?.processes.map(({id, pid, state}) => ({id, pid, state})), processState)
460
+ } finally {
461
+ const shutdown = replacement?.controlCommandsReady ? replacement.shutdown().catch(() => {}) : owner.shutdown().catch(() => {})
462
+
463
+ await Promise.all([fs.writeFile(path.join(releasePath, "worker.fifo"), "drained\n").catch(() => {}), shutdown])
464
+ owner.guardian?.disconnect()
465
+ replacement?.guardian?.disconnect()
381
466
  await stopGuardian(statePath)
382
467
  await fs.rm(root, {force: true, recursive: true})
383
468
  }