@serfab/cadre-core 1.19.1 → 1.21.0

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 (103) hide show
  1. package/dist/cadre-node.d.ts +53 -14
  2. package/dist/cadre-node.js +97 -30
  3. package/dist/cadre-node.js.map +1 -1
  4. package/dist/control-database.d.ts +2 -1
  5. package/dist/control-database.js +146 -135
  6. package/dist/control-database.js.map +1 -1
  7. package/dist/control-protocol-guard.js +3 -2
  8. package/dist/control-protocol-guard.js.map +1 -1
  9. package/dist/control-stream.js +2 -1
  10. package/dist/control-stream.js.map +1 -1
  11. package/dist/describe-thrown.d.ts +12 -0
  12. package/dist/describe-thrown.js +59 -0
  13. package/dist/describe-thrown.js.map +1 -0
  14. package/dist/edge-node/dissolve.d.ts +1 -1
  15. package/dist/edge-node/dissolve.js +1 -1
  16. package/dist/edge-node/dissolve.js.map +1 -1
  17. package/dist/edge-node/edge-join.js +2 -1
  18. package/dist/edge-node/edge-join.js.map +1 -1
  19. package/dist/edge-node/edge-node.js +2 -1
  20. package/dist/edge-node/edge-node.js.map +1 -1
  21. package/dist/edge-node/strand-move.d.ts +0 -2
  22. package/dist/edge-node/strand-move.js +1 -4
  23. package/dist/edge-node/strand-move.js.map +1 -1
  24. package/dist/edge-node/strands.js +2 -1
  25. package/dist/edge-node/strands.js.map +1 -1
  26. package/dist/enrollment.js +2 -1
  27. package/dist/enrollment.js.map +1 -1
  28. package/dist/index.d.ts +3 -1
  29. package/dist/index.js +3 -0
  30. package/dist/index.js.map +1 -1
  31. package/dist/member-redemption.js +3 -9
  32. package/dist/member-redemption.js.map +1 -1
  33. package/dist/node-local-snapshot.js +2 -1
  34. package/dist/node-local-snapshot.js.map +1 -1
  35. package/dist/peer-dial.js +3 -2
  36. package/dist/peer-dial.js.map +1 -1
  37. package/dist/pending-join-runner.d.ts +29 -2
  38. package/dist/pending-join-runner.js +60 -20
  39. package/dist/pending-join-runner.js.map +1 -1
  40. package/dist/push-notifier-apns.js +8 -7
  41. package/dist/push-notifier-apns.js.map +1 -1
  42. package/dist/push-notifier-fcm.js +6 -5
  43. package/dist/push-notifier-fcm.js.map +1 -1
  44. package/dist/push-notifier-shared.d.ts +0 -2
  45. package/dist/push-notifier-shared.js +4 -4
  46. package/dist/push-notifier-shared.js.map +1 -1
  47. package/dist/relay-reservation.js +24 -4
  48. package/dist/relay-reservation.js.map +1 -1
  49. package/dist/strand-addr-protocol.js +2 -1
  50. package/dist/strand-addr-protocol.js.map +1 -1
  51. package/dist/strand-app-schema-report.d.ts +56 -0
  52. package/dist/strand-app-schema-report.js +83 -0
  53. package/dist/strand-app-schema-report.js.map +1 -0
  54. package/dist/strand-database.d.ts +9 -5
  55. package/dist/strand-database.js +12 -6
  56. package/dist/strand-database.js.map +1 -1
  57. package/dist/strand-first-sync-gate.js +4 -3
  58. package/dist/strand-first-sync-gate.js.map +1 -1
  59. package/dist/strand-formation-protocol.js +4 -3
  60. package/dist/strand-formation-protocol.js.map +1 -1
  61. package/dist/strand-instance-manager.d.ts +8 -2
  62. package/dist/strand-instance-manager.js +8 -6
  63. package/dist/strand-instance-manager.js.map +1 -1
  64. package/dist/strand-membership-reconciler.js +9 -11
  65. package/dist/strand-membership-reconciler.js.map +1 -1
  66. package/dist/strand-move.js +4 -6
  67. package/dist/strand-move.js.map +1 -1
  68. package/dist/strand-offline.d.ts +10 -39
  69. package/dist/strand-offline.js +9 -54
  70. package/dist/strand-offline.js.map +1 -1
  71. package/dist/types.d.ts +8 -5
  72. package/dist/types.js.map +1 -1
  73. package/package.json +8 -8
  74. package/src/cadre-node.ts +11528 -11456
  75. package/src/control-database.ts +5603 -5592
  76. package/src/control-protocol-guard.ts +3 -2
  77. package/src/control-stream.ts +2 -1
  78. package/src/describe-thrown.ts +60 -0
  79. package/src/edge-node/dissolve.ts +2 -1
  80. package/src/edge-node/edge-join.ts +1 -1
  81. package/src/edge-node/edge-node.ts +2 -1
  82. package/src/edge-node/strand-move.ts +1 -5
  83. package/src/edge-node/strands.ts +74 -73
  84. package/src/enrollment.ts +2 -1
  85. package/src/index.ts +924 -917
  86. package/src/member-redemption.ts +3 -9
  87. package/src/node-local-snapshot.ts +2 -1
  88. package/src/peer-dial.ts +605 -604
  89. package/src/pending-join-runner.ts +68 -22
  90. package/src/push-notifier-apns.ts +8 -7
  91. package/src/push-notifier-fcm.ts +6 -5
  92. package/src/push-notifier-shared.ts +5 -5
  93. package/src/relay-reservation.ts +1239 -1211
  94. package/src/strand-addr-protocol.ts +2 -1
  95. package/src/strand-app-schema-report.ts +122 -0
  96. package/src/strand-database.ts +474 -466
  97. package/src/strand-first-sync-gate.ts +600 -599
  98. package/src/strand-formation-protocol.ts +4 -3
  99. package/src/strand-instance-manager.ts +2219 -2210
  100. package/src/strand-membership-reconciler.ts +942 -945
  101. package/src/strand-move.ts +594 -597
  102. package/src/strand-offline.ts +11 -80
  103. package/src/types.ts +2963 -2960
@@ -1702,7 +1702,8 @@ export declare class ControlDatabase {
1702
1702
  * when a row was actually removed.
1703
1703
  *
1704
1704
  * Opted into {@link LOST_UPDATE_RETRY_POLICY}: the row's machine may publish its own record
1705
- * at the moment of the removal (its first publish lands about a second after it starts), and
1705
+ * at the moment of the removal (its first publish lands about a second after it starts, or once
1706
+ * it reaches a sibling), and
1706
1707
  * the removal that loses is refused as a lost update. {@link deleteGuardedRow} reads the stamp
1707
1708
  * and signs inside the body, and a self-publish never changes the stamp the `'remove'` digest
1708
1709
  * binds, so the re-run reads the published row and removes it.
@@ -17,6 +17,7 @@ import { isCohortUnreachableRead, retryControlRead } from './control-read-retry.
17
17
  import { chainMessages } from './control-retry.js';
18
18
  import { TableWatchRegistry } from './table-change-watch.js';
19
19
  import { MembershipView } from './membership-view.js';
20
+ import { describeThrown } from './describe-thrown.js';
20
21
  const log = debug('sereus:cadre:control-db');
21
22
  const timing = debug('sereus:cadre:timing');
22
23
  /**
@@ -728,32 +729,32 @@ function formationInviteRowOf(row) {
728
729
  strandId: row.StrandId ?? null,
729
730
  };
730
731
  }
731
- const JOIN_REQUEST_INSERT_SQL = `
732
- insert into CadreControl.JoinRequest (Id, Invitation, Disclosure, RequestedAt, ExpiresAt, StampId)
733
- with context OwnerKey = ?, Signature = ?
734
- values (?, ?, ?, ?, ?, ?)
732
+ const JOIN_REQUEST_INSERT_SQL = `
733
+ insert into CadreControl.JoinRequest (Id, Invitation, Disclosure, RequestedAt, ExpiresAt, StampId)
734
+ with context OwnerKey = ?, Signature = ?
735
+ values (?, ?, ?, ?, ?, ?)
735
736
  `;
736
- const JOIN_SUCCESS_INSERT_SQL = `
737
- insert into CadreControl.JoinSuccess (RequestStampId, RecordedAt, StrandId, MembershipInvite)
738
- with context OwnerKey = ?, Signature = ?
739
- values (?, ?, ?, ?)
737
+ const JOIN_SUCCESS_INSERT_SQL = `
738
+ insert into CadreControl.JoinSuccess (RequestStampId, RecordedAt, StrandId, MembershipInvite)
739
+ with context OwnerKey = ?, Signature = ?
740
+ values (?, ?, ?, ?)
740
741
  `;
741
- const JOIN_FAILURE_INSERT_SQL = `
742
- insert into CadreControl.JoinFailure (RequestStampId, RecordedAt, Code, Reason)
743
- with context OwnerKey = ?, Signature = ?
744
- values (?, ?, ?, ?)
742
+ const JOIN_FAILURE_INSERT_SQL = `
743
+ insert into CadreControl.JoinFailure (RequestStampId, RecordedAt, Code, Reason)
744
+ with context OwnerKey = ?, Signature = ?
745
+ values (?, ?, ?, ?)
745
746
  `;
746
747
  /**
747
748
  * A request with its outcome in one statement: the outcome tables are keyed by the request's
748
749
  * stamp, so the join finds exactly this incarnation's outcome.
749
750
  */
750
- const PENDING_JOIN_SELECT_SQL = `
751
- select R.Id, R.Invitation, R.Disclosure, R.RequestedAt, R.ExpiresAt, R.StampId,
752
- S.RecordedAt as JoinedAt, S.StrandId, S.MembershipInvite,
753
- F.RecordedAt as FailedAt, F.Code, F.Reason
754
- from CadreControl.JoinRequest R
755
- left join CadreControl.JoinSuccess S on S.RequestStampId = R.StampId
756
- left join CadreControl.JoinFailure F on F.RequestStampId = R.StampId
751
+ const PENDING_JOIN_SELECT_SQL = `
752
+ select R.Id, R.Invitation, R.Disclosure, R.RequestedAt, R.ExpiresAt, R.StampId,
753
+ S.RecordedAt as JoinedAt, S.StrandId, S.MembershipInvite,
754
+ F.RecordedAt as FailedAt, F.Code, F.Reason
755
+ from CadreControl.JoinRequest R
756
+ left join CadreControl.JoinSuccess S on S.RequestStampId = R.StampId
757
+ left join CadreControl.JoinFailure F on F.RequestStampId = R.StampId
757
758
  `;
758
759
  /** A {@link PENDING_JOIN_SELECT_SQL} row as read. */
759
760
  function pendingJoinOf(row) {
@@ -818,7 +819,7 @@ async function readSchemaFile(schemaPath) {
818
819
  return await fs.readFile(schemaPath, 'utf-8');
819
820
  }
820
821
  catch (error) {
821
- throw new Error(`Failed to load schema from ${schemaPath}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
822
+ throw new Error(`Failed to load schema from ${schemaPath}: ${describeThrown(error)}`, { cause: error });
822
823
  }
823
824
  }
824
825
  /**
@@ -907,6 +908,10 @@ export class ControlDatabase {
907
908
  default_key_network: 'libp2p',
908
909
  default_network_name: networkName,
909
910
  enable_cache: true,
911
+ // Every member holds every control block (`CONTROL_REPLICATION_BREADTH`), so a control read
912
+ // answers from this machine's own replica. Left unset, the `network` transactor would price
913
+ // these tables as `distributed`.
914
+ cost_profile: 'localDisk',
910
915
  });
911
916
  // Register vtables and functions manually since we need access to collectionFactory
912
917
  for (const vtable of pluginResult.vtables) {
@@ -1069,15 +1074,20 @@ export class ControlDatabase {
1069
1074
  // `catalog-only` kinds, from Optimystic 1.16.0): the plugin's `alterTable` arms (add column,
1070
1075
  // drop column, add and drop CHECK) and the tag change write the table's catalog record under
1071
1076
  // the batch's per-statement checkpoint, so a step that throws withdraws its own write, and a
1072
- // batch whose commit fails is discarded whole and the tables it wrote re-persist on their next
1073
- // touch. A change the record already holds — another machine migrated first — is adopted with
1074
- // nothing written, and an undo of an adopted change touches this machine's catalog only. The
1075
- // one arm Quereus marks irreversible is `drop column`: once it has run, a later failure
1076
- // unwinds nothing (the apply reports the schema as partially migrated), the plugin's
1077
- // `endSchemaBatch` commits the steps that landed — or, when that commit fails, marks their
1078
- // tables to re-persist on next touch — and the retry's diff runs only the steps still
1079
- // missing. No row is lost either way: rows keep a dropped column's field until they are
1080
- // rewritten, and nothing reads it.
1077
+ // batch whose commit fails is discarded whole and undone in a second batch (Quereus 4.25.0), so
1078
+ // the retry plans the whole change again. A change the record already holds — another machine
1079
+ // migrated first — is adopted with nothing written, and an undo of an adopted change touches
1080
+ // this machine's catalog only. The one arm Quereus marks irreversible is `drop column`: once it
1081
+ // has run, a later failure unwinds nothing (the apply reports the schema as partially
1082
+ // migrated), the plugin's `endSchemaBatch` commits the steps that landed — or, when that commit
1083
+ // fails, marks their tables to re-persist on next touch — and the retry's diff runs only the
1084
+ // steps still missing. No row is lost either way: rows keep a dropped column's field until they
1085
+ // are rewritten, and nothing reads it.
1086
+ //
1087
+ // NOTE: a cohort that keeps refusing the commit now ends the retries with a thrown error, so a
1088
+ // machine migrating while its siblings are offline fails its start. No release has migrated
1089
+ // yet (`CONTROL_SCHEMA_VERSION` is the baseline). The first version that does should decide
1090
+ // whether that start waits, as the `App` apply does (`applyAppSchemaOrDefer`).
1081
1091
  //
1082
1092
  // Only the "cohort did not answer, nothing committed" class is retried; the classifier
1083
1093
  // vetoes indeterminate commits and does not match `Missing block`, which is a durable
@@ -1911,11 +1921,11 @@ export class ControlDatabase {
1911
1921
  }
1912
1922
  const record = sign(current);
1913
1923
  // Bare `exec` — inside the non-re-entrant write lock (see insertCadrePeer).
1914
- await this.db.exec(`
1915
- update CadreControl.CadrePeer
1916
- with context OwnerKey = null, Signature = ?
1917
- set Multiaddr = ?, UpdatedAt = ?, Profile = ?, Sig = ?
1918
- where PeerId = ?
1924
+ await this.db.exec(`
1925
+ update CadreControl.CadrePeer
1926
+ with context OwnerKey = null, Signature = ?
1927
+ set Multiaddr = ?, UpdatedAt = ?, Profile = ?, Sig = ?
1928
+ where PeerId = ?
1919
1929
  `, [record.sig, record.addrs.join(','), record.updatedAt, record.profile, record.sig, peerId]);
1920
1930
  return record;
1921
1931
  }, LOST_UPDATE_RETRY_POLICY, 'self-record-update');
@@ -1967,11 +1977,11 @@ export class ControlDatabase {
1967
1977
  */
1968
1978
  async updateSelfDeviceToken(record) {
1969
1979
  this.ensureInitialized();
1970
- await this.execWrite(`
1971
- update CadreControl.DeviceToken
1972
- with context OwnerKey = null, Signature = ?
1973
- set Platform = ?, Token = ?, UpdatedAt = ?, Sig = ?
1974
- where PeerId = ?
1980
+ await this.execWrite(`
1981
+ update CadreControl.DeviceToken
1982
+ with context OwnerKey = null, Signature = ?
1983
+ set Platform = ?, Token = ?, UpdatedAt = ?, Sig = ?
1984
+ where PeerId = ?
1975
1985
  `, [record.sig, record.platform, record.token, record.updatedAt, record.sig, record.peerId], 'device-token-update');
1976
1986
  log('Self device token updated: %s (platform=%s, updatedAt=%d)', record.peerId, record.platform, record.updatedAt);
1977
1987
  }
@@ -1994,10 +2004,10 @@ export class ControlDatabase {
1994
2004
  // not-null/unique anti-replay constraint — the StampId is a real column value, not the
1995
2005
  // optimystic `StampId()` SQL function.
1996
2006
  const stampId = generateStampId(this.config.libp2pNode.peerId.toString());
1997
- await this.execWrite(`
1998
- insert into CadreControl.OwnerKey (Key, Label, StampId)
1999
- with context OwnerKey = null, Signature = null
2000
- values (?, ?, ?)
2007
+ await this.execWrite(`
2008
+ insert into CadreControl.OwnerKey (Key, Label, StampId)
2009
+ with context OwnerKey = null, Signature = null
2010
+ values (?, ?, ?)
2001
2011
  `, [key, storedLabel, stampId], 'owner-key-insert');
2002
2012
  log('Owner key inserted');
2003
2013
  }
@@ -2064,10 +2074,10 @@ export class ControlDatabase {
2064
2074
  * NOT re-entrant.
2065
2075
  */
2066
2076
  async execOwnerKeyVouchedInsert(row, ownerKey, signature) {
2067
- await this.db.exec(`
2068
- insert into CadreControl.OwnerKey (Key, Label, StampId, VouchOwner, VouchSig)
2069
- with context OwnerKey = ?, Signature = ?
2070
- values (?, ?, ?, ?, ?)
2077
+ await this.db.exec(`
2078
+ insert into CadreControl.OwnerKey (Key, Label, StampId, VouchOwner, VouchSig)
2079
+ with context OwnerKey = ?, Signature = ?
2080
+ values (?, ?, ?, ?, ?)
2071
2081
  `, [ownerKey, signature, row.key, row.label, row.stampId, ownerKey, signature]);
2072
2082
  }
2073
2083
  /**
@@ -2266,10 +2276,10 @@ export class ControlDatabase {
2266
2276
  // StampId is a real, unique column (single-use anti-replay), no longer a context value.
2267
2277
  // FounderOwnerKey must equal the signing owner or the constraint rejects the row.
2268
2278
  return {
2269
- sql: `
2270
- insert into CadreControl.Strand (Id, Type, MemberPrivateKey, StampId, FounderOwnerKey, FounderPeerId)
2271
- with context OwnerKey = ?, Signature = ?
2272
- values (?, ?, ?, ?, ?, ?)
2279
+ sql: `
2280
+ insert into CadreControl.Strand (Id, Type, MemberPrivateKey, StampId, FounderOwnerKey, FounderPeerId)
2281
+ with context OwnerKey = ?, Signature = ?
2282
+ values (?, ?, ?, ?, ?, ?)
2273
2283
  `,
2274
2284
  params: [ownerKey, signature, strandId, type, memberPrivateKey ?? null, stampId, ownerKey, peerId],
2275
2285
  };
@@ -2412,10 +2422,10 @@ export class ControlDatabase {
2412
2422
  const message = buildAuthorizationMessage('CadreControl.StrandPartyKey', 'add', this.config.partyId, [strandId, privateKey, stampId]);
2413
2423
  const signature = await signMessage(message);
2414
2424
  return {
2415
- sql: `
2416
- insert into CadreControl.StrandPartyKey (Id, PrivateKey, StampId)
2417
- with context OwnerKey = ?, Signature = ?
2418
- values (?, ?, ?)
2425
+ sql: `
2426
+ insert into CadreControl.StrandPartyKey (Id, PrivateKey, StampId)
2427
+ with context OwnerKey = ?, Signature = ?
2428
+ values (?, ?, ?)
2419
2429
  `,
2420
2430
  params: [ownerKey, signature, strandId, privateKey, stampId],
2421
2431
  };
@@ -2455,10 +2465,10 @@ export class ControlDatabase {
2455
2465
  // Id, Type, MemberPrivateKey ('' when null), StampId.
2456
2466
  const message = buildAuthorizationMessage('CadreControl.JoinedStrand', 'add', this.config.partyId, [row.Id, row.Type, row.MemberPrivateKey ?? '', stampId]);
2457
2467
  const signature = await signMessage(message);
2458
- await this.execWrite(`
2459
- insert into CadreControl.JoinedStrand (Id, Type, MemberPrivateKey, StampId)
2460
- with context OwnerKey = ?, Signature = ?
2461
- values (?, ?, ?, ?)
2468
+ await this.execWrite(`
2469
+ insert into CadreControl.JoinedStrand (Id, Type, MemberPrivateKey, StampId)
2470
+ with context OwnerKey = ?, Signature = ?
2471
+ values (?, ?, ?, ?)
2462
2472
  `, [ownerKey, signature, row.Id, row.Type, row.MemberPrivateKey, stampId], 'joined-strand-insert');
2463
2473
  log('Joined strand inserted: %s', row.Id);
2464
2474
  }
@@ -2585,10 +2595,10 @@ export class ControlDatabase {
2585
2595
  }
2586
2596
  /** One unsigned outcome delete (see {@link deleteJoinOutcomes}; a failure's is also authorized by the join superseding it). */
2587
2597
  async deleteJoinOutcome(table, requestStampId) {
2588
- await this.db.exec(`
2589
- delete from CadreControl.${table}
2590
- with context OwnerKey = null, Signature = null
2591
- where RequestStampId = ?
2598
+ await this.db.exec(`
2599
+ delete from CadreControl.${table}
2600
+ with context OwnerKey = null, Signature = null
2601
+ where RequestStampId = ?
2592
2602
  `, [requestStampId]);
2593
2603
  }
2594
2604
  /**
@@ -2612,10 +2622,10 @@ export class ControlDatabase {
2612
2622
  // Field order MUST match the schema's ValidationKey `AuthorizedInsert` verify: Key, StampId.
2613
2623
  const message = buildAuthorizationMessage('CadreControl.ValidationKey', 'add', this.config.partyId, [key, stampId]);
2614
2624
  const signature = await signMessage(message);
2615
- await this.execWrite(`
2616
- insert into CadreControl.ValidationKey (Key, StampId)
2617
- with context OwnerKey = ?, Signature = ?
2618
- values (?, ?)
2625
+ await this.execWrite(`
2626
+ insert into CadreControl.ValidationKey (Key, StampId)
2627
+ with context OwnerKey = ?, Signature = ?
2628
+ values (?, ?)
2619
2629
  `, [ownerKey, signature, key, stampId], 'validation-key-insert');
2620
2630
  log('Validation key inserted: %s', key);
2621
2631
  }
@@ -2693,10 +2703,10 @@ export class ControlDatabase {
2693
2703
  // Persist the vouching (owner, signature) onto the row (VouchOwner/VouchSig) —
2694
2704
  // identical to the context pair, which the AuthorizedInsert constraint binds — so a
2695
2705
  // reader can later re-check the voucher against its node-local trusted-owner anchor.
2696
- await this.db.exec(`
2697
- insert into CadreControl.CadrePeer (PeerId, PublicKey, Multiaddr, UpdatedAt, Sig, Profile, StampId, VouchOwner, VouchSig)
2698
- with context OwnerKey = ?, Signature = ?
2699
- values (?, ?, ?, ?, ?, ?, ?, ?, ?)
2706
+ await this.db.exec(`
2707
+ insert into CadreControl.CadrePeer (PeerId, PublicKey, Multiaddr, UpdatedAt, Sig, Profile, StampId, VouchOwner, VouchSig)
2708
+ with context OwnerKey = ?, Signature = ?
2709
+ values (?, ?, ?, ?, ?, ?, ?, ?, ?)
2700
2710
  `, [ownerKey, signature, row.peerId, row.publicKey, row.multiaddr, row.updatedAt, row.sig, row.profile, stampId, ownerKey, signature]);
2701
2711
  return true;
2702
2712
  });
@@ -2785,19 +2795,19 @@ export class ControlDatabase {
2785
2795
  const signature = await signMessage(buildAuthorizationMessage('CadreControl.CadrePeer', 'vouch', this.config.partyId, [peerId, target.stampId]));
2786
2796
  // Bare `exec` — inside the non-re-entrant write lock (see insertCadrePeer).
2787
2797
  if (mode === 'rebroadcast') {
2788
- await this.db.exec(`
2789
- update CadreControl.CadrePeer
2790
- with context OwnerKey = ?, Signature = ?
2791
- set UpdatedAt = ?, VouchOwner = ?, VouchSig = ?, VouchUsage = null
2792
- where PeerId = ?
2798
+ await this.db.exec(`
2799
+ update CadreControl.CadrePeer
2800
+ with context OwnerKey = ?, Signature = ?
2801
+ set UpdatedAt = ?, VouchOwner = ?, VouchSig = ?, VouchUsage = null
2802
+ where PeerId = ?
2793
2803
  `, [ownerKey, signature, Math.max(Date.now(), target.updatedAt + 1), ownerKey, signature, peerId]);
2794
2804
  }
2795
2805
  else {
2796
- await this.db.exec(`
2797
- update CadreControl.CadrePeer
2798
- with context OwnerKey = ?, Signature = ?
2799
- set VouchOwner = ?, VouchSig = ?, VouchUsage = null
2800
- where PeerId = ?
2806
+ await this.db.exec(`
2807
+ update CadreControl.CadrePeer
2808
+ with context OwnerKey = ?, Signature = ?
2809
+ set VouchOwner = ?, VouchSig = ?, VouchUsage = null
2810
+ where PeerId = ?
2801
2811
  `, [ownerKey, signature, ownerKey, signature, peerId]);
2802
2812
  }
2803
2813
  return 'retouched';
@@ -2824,7 +2834,8 @@ export class ControlDatabase {
2824
2834
  * when a row was actually removed.
2825
2835
  *
2826
2836
  * Opted into {@link LOST_UPDATE_RETRY_POLICY}: the row's machine may publish its own record
2827
- * at the moment of the removal (its first publish lands about a second after it starts), and
2837
+ * at the moment of the removal (its first publish lands about a second after it starts, or once
2838
+ * it reaches a sibling), and
2828
2839
  * the removal that loses is refused as a lost update. {@link deleteGuardedRow} reads the stamp
2829
2840
  * and signs inside the body, and a self-publish never changes the stamp the `'remove'` digest
2830
2841
  * binds, so the re-run reads the published row and removes it.
@@ -2907,10 +2918,10 @@ export class ControlDatabase {
2907
2918
  */
2908
2919
  async execGuardedRemoval(removal, ownerKey) {
2909
2920
  const { tableName, rowKey, stampId } = removal.ref;
2910
- await this.db.exec(`
2911
- delete from CadreControl.${tableName}
2912
- with context OwnerKey = ?, Signature = ?
2913
- where ${GUARDED_KEY_COLUMN[tableName]} = ? and StampId = ?
2921
+ await this.db.exec(`
2922
+ delete from CadreControl.${tableName}
2923
+ with context OwnerKey = ?, Signature = ?
2924
+ where ${GUARDED_KEY_COLUMN[tableName]} = ? and StampId = ?
2914
2925
  `, [ownerKey, removal.signature, rowKey, stampId]);
2915
2926
  await this.execTombstone(removal.ref, removal.revocationSignature, ownerKey);
2916
2927
  }
@@ -2928,10 +2939,10 @@ export class ControlDatabase {
2928
2939
  * equal), so a node that receives the row by replication can tell which owner filed it.
2929
2940
  */
2930
2941
  async execTombstone(ref, revocationSignature, ownerKey) {
2931
- await this.db.exec(`
2932
- insert into CadreControl.Revocation (TableName, RowKey, StampId, ReissuedAt, SignerKey, SignerSig)
2933
- with context OwnerKey = ?, Signature = ?
2934
- values (?, ?, ?, 0, ?, ?)
2942
+ await this.db.exec(`
2943
+ insert into CadreControl.Revocation (TableName, RowKey, StampId, ReissuedAt, SignerKey, SignerSig)
2944
+ with context OwnerKey = ?, Signature = ?
2945
+ values (?, ?, ?, 0, ?, ?)
2935
2946
  `, [ownerKey, revocationSignature, ref.tableName, ref.rowKey, ref.stampId, ownerKey, revocationSignature]);
2936
2947
  }
2937
2948
  /**
@@ -3015,10 +3026,10 @@ export class ControlDatabase {
3015
3026
  log('reap %s: %s holds a fresh incarnation (live stamp differs from the tombstoned one); leaving it', table, rowKey);
3016
3027
  return false;
3017
3028
  }
3018
- const sql = `
3019
- delete from CadreControl.${table}
3020
- with context OwnerKey = null, Signature = null
3021
- where ${GUARDED_KEY_COLUMN[table]} = ? and StampId = ?
3029
+ const sql = `
3030
+ delete from CadreControl.${table}
3031
+ with context OwnerKey = null, Signature = null
3032
+ where ${GUARDED_KEY_COLUMN[table]} = ? and StampId = ?
3022
3033
  `;
3023
3034
  if (table === 'CadrePeer') {
3024
3035
  // Through mutateCadrePeer so the "EVERY CadrePeer writer goes through here"
@@ -3193,11 +3204,11 @@ export class ControlDatabase {
3193
3204
  // output minted per row incarnation (generateStampId), so it identifies one
3194
3205
  // row across all TableName values. Do NOT "fix" this by adding TableName to
3195
3206
  // the where clause.
3196
- await this.db.exec(`
3197
- update CadreControl.Revocation
3198
- with context OwnerKey = ?, Signature = ?
3199
- set ReissuedAt = ?
3200
- where StampId = ?
3207
+ await this.db.exec(`
3208
+ update CadreControl.Revocation
3209
+ with context OwnerKey = ?, Signature = ?
3210
+ set ReissuedAt = ?
3211
+ where StampId = ?
3201
3212
  `, [ownerKey, signature, reissuedAt, row.stampId]);
3202
3213
  executed++;
3203
3214
  }
@@ -3598,10 +3609,10 @@ export class ControlDatabase {
3598
3609
  // signed source-of-truth and the stored value are produced once. StampId is a real,
3599
3610
  // unique column (single-use anti-replay), no longer a context value.
3600
3611
  return {
3601
- sql: `
3602
- insert into CadreControl.FormationInvite (Token, sAppId, ExpiresAt, TotalUses, ValidationUrl, StrandId, StampId)
3603
- with context OwnerKey = ?, Signature = ?
3604
- values (?, ?, ?, ?, ?, ?, ?)
3612
+ sql: `
3613
+ insert into CadreControl.FormationInvite (Token, sAppId, ExpiresAt, TotalUses, ValidationUrl, StrandId, StampId)
3614
+ with context OwnerKey = ?, Signature = ?
3615
+ values (?, ?, ?, ?, ?, ?, ?)
3605
3616
  `,
3606
3617
  params: [
3607
3618
  ownerKey, signature,
@@ -3671,10 +3682,10 @@ export class ControlDatabase {
3671
3682
  // Hard-coded open + keyless + explicit null founder columns: the
3672
3683
  // consent branch admits no other shape (no signature means no
3673
3684
  // trustworthy founder machine to record — see the schema's comment).
3674
- await this.db.exec(`
3675
- insert into CadreControl.Strand (Id, Type, MemberPrivateKey, StampId, FounderOwnerKey, FounderPeerId)
3676
- with context OwnerKey = null, Signature = null
3677
- values (?, 'o', null, ?, null, null)
3685
+ await this.db.exec(`
3686
+ insert into CadreControl.Strand (Id, Type, MemberPrivateKey, StampId, FounderOwnerKey, FounderPeerId)
3687
+ with context OwnerKey = null, Signature = null
3688
+ values (?, 'o', null, ?, null, null)
3678
3689
  `, [strandId, strandStampId]);
3679
3690
  // 2. FormationUsage row — authorised by the matching FormationInvite, and
3680
3691
  // carrying the strand's stamp so it authorizes THIS row and no other.
@@ -3793,10 +3804,10 @@ export class ControlDatabase {
3793
3804
  // robustness/consistency change, matching the strand layer's `consumeInvite`,
3794
3805
  // not a fix for an observable mis-ordering.
3795
3806
  const nowCanonical = await canonicalDatetime(this.db, opts.nowMs);
3796
- await this.db.exec(`
3797
- insert into CadreControl.FormationUsage (Token, UsageStampId, PeerKey, PeerSig, Disclosure, StrandId, StrandStampId)
3798
- with context Now = ?, ValidationKey = ?, ValidationSignature = ?
3799
- values (?, ?, ?, ?, ?, ?, ?)
3807
+ await this.db.exec(`
3808
+ insert into CadreControl.FormationUsage (Token, UsageStampId, PeerKey, PeerSig, Disclosure, StrandId, StrandStampId)
3809
+ with context Now = ?, ValidationKey = ?, ValidationSignature = ?
3810
+ values (?, ?, ?, ?, ?, ?, ?)
3800
3811
  `, [
3801
3812
  nowCanonical, opts.validationKey, opts.validationSignature,
3802
3813
  opts.token, opts.usageStampId, opts.peerKey,
@@ -4045,10 +4056,10 @@ export class ControlDatabase {
4045
4056
  }
4046
4057
  /** Bare `CadreInvite` insert shared by {@link insertCadreInvite} and {@link seatCadreInvite}; the caller holds the lock. */
4047
4058
  async execCadreInviteInsert(row) {
4048
- await this.db.exec(`
4049
- insert into CadreControl.CadreInvite (Key, PeerId, ExpiresAt, TotalUses, IssuerKey, IssuerSig, StampId)
4050
- with context OwnerKey = ?, Signature = ?
4051
- values (?, ?, ?, ?, ?, ?, ?)
4059
+ await this.db.exec(`
4060
+ insert into CadreControl.CadreInvite (Key, PeerId, ExpiresAt, TotalUses, IssuerKey, IssuerSig, StampId)
4061
+ with context OwnerKey = ?, Signature = ?
4062
+ values (?, ?, ?, ?, ?, ?, ?)
4052
4063
  `, [
4053
4064
  row.issuerKey, row.issuerSig,
4054
4065
  row.key, row.peerId, row.expiresAt, row.totalUses,
@@ -4349,16 +4360,16 @@ export class ControlDatabase {
4349
4360
  const peerStampId = generateStampId(peerId);
4350
4361
  const nowCanonical = await canonicalDatetime(this.db, nowMs);
4351
4362
  await this.inTransaction('cadre-invite-redeem', async () => {
4352
- await this.db.exec(`
4353
- insert into CadreControl.CadrePeer (PeerId, PublicKey, Multiaddr, UpdatedAt, Sig, Profile, StampId, VouchOwner, VouchSig, VouchUsage)
4354
- with context OwnerKey = null, Signature = null
4355
- values (?, ?, ?, ?, ?, ?, ?, ?, null, ?)
4363
+ await this.db.exec(`
4364
+ insert into CadreControl.CadrePeer (PeerId, PublicKey, Multiaddr, UpdatedAt, Sig, Profile, StampId, VouchOwner, VouchSig, VouchUsage)
4365
+ with context OwnerKey = null, Signature = null
4366
+ values (?, ?, ?, ?, ?, ?, ?, ?, null, ?)
4356
4367
  `, [peerId, peerKey, multiaddr, record?.updatedAt ?? nowMs, record?.sig ?? null, record?.profile ?? null,
4357
4368
  peerStampId, invite.issuerKey, usageStampId]);
4358
- await this.db.exec(`
4359
- insert into CadreControl.CadreInviteUsage (UsageStampId, InviteKey, PeerId, PeerKey, PeerStampId, InviteSig, PeerSig)
4360
- with context Now = ?
4361
- values (?, ?, ?, ?, ?, ?, ?)
4369
+ await this.db.exec(`
4370
+ insert into CadreControl.CadreInviteUsage (UsageStampId, InviteKey, PeerId, PeerKey, PeerStampId, InviteSig, PeerSig)
4371
+ with context Now = ?
4372
+ values (?, ?, ?, ?, ?, ?, ?)
4362
4373
  `, [nowCanonical, usageStampId, inviteKey, peerId, peerKey, peerStampId, inviteSig, peerSig]);
4363
4374
  });
4364
4375
  log('Redeemed cadre invitation %s: %s admitted (usage %s)', inviteKey, peerId, usageStampId);
@@ -4439,10 +4450,10 @@ export class ControlDatabase {
4439
4450
  }
4440
4451
  /** Bare `OwnerGrant` insert shared by {@link insertOwnerGrant} and {@link seatOwnerGrant}; the caller holds the lock. */
4441
4452
  async execOwnerGrantInsert(row) {
4442
- await this.db.exec(`
4443
- insert into CadreControl.OwnerGrant (Key, Label, ExpiresAt, TotalUses, IssuerKey, IssuerSig, StampId)
4444
- with context OwnerKey = ?, Signature = ?
4445
- values (?, ?, ?, ?, ?, ?, ?)
4453
+ await this.db.exec(`
4454
+ insert into CadreControl.OwnerGrant (Key, Label, ExpiresAt, TotalUses, IssuerKey, IssuerSig, StampId)
4455
+ with context OwnerKey = ?, Signature = ?
4456
+ values (?, ?, ?, ?, ?, ?, ?)
4446
4457
  `, [
4447
4458
  row.issuerKey, row.issuerSig,
4448
4459
  row.key, row.label, row.expiresAt, row.totalUses,
@@ -4572,15 +4583,15 @@ export class ControlDatabase {
4572
4583
  const seatedStampId = generateStampId(this.config.libp2pNode.peerId.toString());
4573
4584
  const nowCanonical = await canonicalDatetime(this.db, nowMs);
4574
4585
  await this.inTransaction('owner-grant-redeem', async () => {
4575
- await this.db.exec(`
4576
- insert into CadreControl.OwnerKey (Key, Label, StampId, VouchOwner, VouchSig, VouchUsage)
4577
- with context OwnerKey = null, Signature = null
4578
- values (?, ?, ?, ?, null, ?)
4586
+ await this.db.exec(`
4587
+ insert into CadreControl.OwnerKey (Key, Label, StampId, VouchOwner, VouchSig, VouchUsage)
4588
+ with context OwnerKey = null, Signature = null
4589
+ values (?, ?, ?, ?, null, ?)
4579
4590
  `, [seatedKey, grant.label, seatedStampId, grant.issuerKey, usageStampId]);
4580
- await this.db.exec(`
4581
- insert into CadreControl.OwnerGrantUsage (UsageStampId, GrantKey, SeatedKey, SeatedStampId, GrantSig, OwnerSig)
4582
- with context Now = ?
4583
- values (?, ?, ?, ?, ?, ?)
4591
+ await this.db.exec(`
4592
+ insert into CadreControl.OwnerGrantUsage (UsageStampId, GrantKey, SeatedKey, SeatedStampId, GrantSig, OwnerSig)
4593
+ with context Now = ?
4594
+ values (?, ?, ?, ?, ?, ?)
4584
4595
  `, [nowCanonical, usageStampId, grantKey, seatedKey, seatedStampId, grantSig, ownerSig]);
4585
4596
  });
4586
4597
  log('Redeemed owner grant %s: %s seated as an owner (usage %s)', grantKey, seatedKey, usageStampId);