@substrat-run/adapter-cloudflare 0.235.0 → 0.236.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.
@@ -1105,8 +1105,8 @@ export class ControlPlaneDO extends DurableObject {
1105
1105
  kernelSql;
1106
1106
  /** Settles once the constructor's #1764 backfill check has run (awaited by the tests). */
1107
1107
  backfillArmed;
1108
- /** The pass recovers a handoff interrupted after the directory commit. */
1109
- armTenantDueWork(tenantId, items) {
1108
+ /** After the directory commits, the pass can recover a failed handoff. */
1109
+ async handOverTenantDueWork(tenantId, items) {
1110
1110
  const env = this.env;
1111
1111
  const ns = env.TENANT_DUE_WORK;
1112
1112
  if (!ns || items.length === 0)
@@ -1117,10 +1117,16 @@ export class ControlPlaneDO extends DurableObject {
1117
1117
  }
1118
1118
  else if (env.REAP_OWNER !== 'tenant')
1119
1119
  return;
1120
- const owner = ns.get(ns.idFromName(tenantDueWorkName(tenantId)));
1121
- this.ctx.waitUntil(owner.ensure(tenantId, items).catch((err) => {
1120
+ try {
1121
+ const owner = ns.get(ns.idFromName(tenantDueWorkName(tenantId)));
1122
+ await owner.ensure(tenantId, items);
1123
+ }
1124
+ catch (err) {
1122
1125
  console.error('directory could not arm tenant due work', { tenantId, err });
1123
- }));
1126
+ }
1127
+ }
1128
+ armTenantDueWork(tenantId, items) {
1129
+ this.ctx.waitUntil(this.handOverTenantDueWork(tenantId, items));
1124
1130
  }
1125
1131
  autoReapAt(setting, at) {
1126
1132
  const raw = Reflect.get(this.env, setting);
@@ -1320,10 +1326,10 @@ export class ControlPlaneDO extends DurableObject {
1320
1326
  routeFlushing = null;
1321
1327
  routeFlushAgain = false;
1322
1328
  /**
1323
- * How many of this object's methods are running (`kickRouteReadModelAfterEveryCall`). A flush
1324
- * computes entries synchronously from the directory, so it only starts, and only reads a group
1325
- * of keys, while this is 0: a method still running may hold a transaction that has not committed,
1326
- * and a snapshot read inside it could be published and then rolled back.
1329
+ * How many calls are still in their directory work (see `ROUTE_KICK_EXEMPT`). A flush computes
1330
+ * entries synchronously from the directory, so it only starts, and only reads a group of keys,
1331
+ * while this is 0. Otherwise it could read a transaction before it commits and publish a snapshot
1332
+ * of a write that later rolls back.
1327
1333
  */
1328
1334
  routeCallDepth = 0;
1329
1335
  /**
@@ -4288,7 +4294,7 @@ export class ControlPlaneDO extends DurableObject {
4288
4294
  * claimed, and on its copy move still owning a live lease. The move's confirmation commits in
4289
4295
  * the same transaction as the row. False when any condition failed and nothing was written.
4290
4296
  */
4291
- moveScopeRow(set, setArgs, scopeId, cond, audit,
4297
+ async moveScopeRow(set, setArgs, scopeId, cond, audit,
4292
4298
  /** Judged first, in the write's own transaction: a throw refuses the move with nothing written. */
4293
4299
  judge) {
4294
4300
  const now = new Date().toISOString();
@@ -4312,7 +4318,9 @@ export class ControlPlaneDO extends DurableObject {
4312
4318
  return true;
4313
4319
  }, audit);
4314
4320
  if (confirmedTenant !== undefined) {
4315
- this.armTenantDueWork(confirmedTenant, [{ kind: 'copies', unit: confirmedTenant, dueAt: Date.parse(now) }]);
4321
+ // Keep the copy handoff in this RPC. A detached handoff can be lost once the caller's
4322
+ // RPC ends, leaving the copy in the directory until the next reserve pass.
4323
+ await this.handOverTenantDueWork(confirmedTenant, [{ kind: 'copies', unit: confirmedTenant, dueAt: Date.parse(now) }]);
4316
4324
  }
4317
4325
  return written;
4318
4326
  }
@@ -4437,16 +4445,12 @@ export class ControlPlaneDO extends DurableObject {
4437
4445
  claimSubjectErasure(tenantId, scopeId, fence) {
4438
4446
  return this.sql.exec(SUBJECT_ERASURE_CLAIM_SQL, ...subjectErasureClaimParams(tenantId, scopeId, fence)).rowsWritten > 0;
4439
4447
  }
4440
- recordScopeScriptCopy(tenantId, scopeId, scriptRef, moveId, role = null, loadStamp = null, leaseMs = SCOPE_COPY_LEASE_MS) {
4448
+ async recordScopeScriptCopy(tenantId, scopeId, scriptRef, moveId, role = null, loadStamp = null, leaseMs = SCOPE_COPY_LEASE_MS) {
4441
4449
  if (!scriptRef || !moveId)
4442
4450
  return 'invalid';
4443
4451
  const dueAt = Date.now() + leaseMs;
4444
- const written = this.sql.exec(`INSERT INTO scope_script_copies (tenant_id, scope_id, script_ref, move_id, state, role, load_stamp, lease_until)
4445
- SELECT tenant_id, scope_id, ?, ?, 'pending', ?, ?, ? FROM scopes
4446
- WHERE tenant_id = ? AND scope_id = ? AND reap_claimed_at IS NULL
4447
- ON CONFLICT (tenant_id, scope_id, script_ref, move_id) DO NOTHING`, scriptRef, moveId, role, loadStamp, new Date(dueAt).toISOString(), tenantId, scopeId);
4448
- if (written.rowsWritten > 0) {
4449
- this.armTenantDueWork(tenantId, [{ kind: 'copies', unit: tenantId, dueAt }]);
4452
+ if (this.insertPendingScopeScriptCopy(tenantId, scopeId, scriptRef, moveId, role, loadStamp, dueAt)) {
4453
+ await this.handOverTenantDueWork(tenantId, [{ kind: 'copies', unit: tenantId, dueAt }]);
4450
4454
  return 'recorded';
4451
4455
  }
4452
4456
  const scope = this.sql.exec('SELECT reap_claimed_at FROM scopes WHERE tenant_id = ? AND scope_id = ?', tenantId, scopeId)
@@ -4457,6 +4461,12 @@ export class ControlPlaneDO extends DurableObject {
4457
4461
  return 'reaping';
4458
4462
  return 'recorded'; // idempotent retry of this move
4459
4463
  }
4464
+ insertPendingScopeScriptCopy(tenantId, scopeId, scriptRef, moveId, role, loadStamp, dueAt) {
4465
+ return this.sql.exec(`INSERT INTO scope_script_copies (tenant_id, scope_id, script_ref, move_id, state, role, load_stamp, lease_until)
4466
+ SELECT tenant_id, scope_id, ?, ?, 'pending', ?, ?, ? FROM scopes
4467
+ WHERE tenant_id = ? AND scope_id = ? AND reap_claimed_at IS NULL
4468
+ ON CONFLICT (tenant_id, scope_id, script_ref, move_id) DO NOTHING`, scriptRef, moveId, role, loadStamp, new Date(dueAt).toISOString(), tenantId, scopeId).rowsWritten > 0;
4469
+ }
4460
4470
  /**
4461
4471
  * Record a pre-ledger copy, and its `backfillScopeCopy` audit row in the same unit (#1722): the row
4462
4472
  * carries the erasure epoch read here, which the backfill orders erasures by, so an entry with no
@@ -4545,8 +4555,15 @@ export class ControlPlaneDO extends DurableObject {
4545
4555
  return 'not-due';
4546
4556
  return 'pending';
4547
4557
  }
4548
- settleScopeScriptCopy(tenantId, scopeId, scriptRef, moveId, state, loadStamp, revision, claimedBy) {
4549
- const written = this.ctx.storage.transactionSync(() => {
4558
+ async settleScopeScriptCopy(tenantId, scopeId, scriptRef, moveId, state, loadStamp, revision, claimedBy) {
4559
+ const written = this.settleScopeScriptCopyRow(tenantId, scopeId, scriptRef, moveId, state, loadStamp, revision, claimedBy);
4560
+ if (written && state === 'eligible') {
4561
+ await this.handOverTenantDueWork(tenantId, [{ kind: 'copies', unit: tenantId, dueAt: Date.now() }]);
4562
+ }
4563
+ return written;
4564
+ }
4565
+ settleScopeScriptCopyRow(tenantId, scopeId, scriptRef, moveId, state, loadStamp, revision, claimedBy) {
4566
+ return this.ctx.storage.transactionSync(() => {
4550
4567
  const settled = this.sql.exec(`UPDATE scope_script_copies SET state = ?, load_stamp = ?, revision = ?,
4551
4568
  last_attempt_at = strftime('%Y-%m-%dT%H:%M:%fZ','now')
4552
4569
  WHERE tenant_id = ? AND scope_id = ? AND script_ref = ? AND move_id = ?
@@ -4556,10 +4573,6 @@ export class ControlPlaneDO extends DurableObject {
4556
4573
  this.sql.exec(COPY_BACKFILL_SUPERSEDE_SQL, tenantId, scopeId);
4557
4574
  return settled;
4558
4575
  });
4559
- if (written && state === 'eligible') {
4560
- this.armTenantDueWork(tenantId, [{ kind: 'copies', unit: tenantId, dueAt: Date.now() }]);
4561
- }
4562
- return written;
4563
4576
  }
4564
4577
  touchScopeScriptCopy(tenantId, scopeId, scriptRef, moveId) {
4565
4578
  this.sql.exec(`UPDATE scope_script_copies SET last_attempt_at = strftime('%Y-%m-%dT%H:%M:%fZ','now')
@@ -6554,7 +6567,7 @@ export class ControlPlaneDO extends DurableObject {
6554
6567
  }
6555
6568
  }
6556
6569
  /**
6557
- * #3395: when the last running method of the directory returns, publish what its writes queued.
6570
+ * #3395: when the last counted directory call returns, publish what its writes queued.
6558
6571
  *
6559
6572
  * The capture is the triggers (route-read-model-publisher.ts), which miss no write path; this is
6560
6573
  * the matching half for the publish, so it cannot be forgotten either: a method added later is
@@ -6566,6 +6579,10 @@ export class ControlPlaneDO extends DurableObject {
6566
6579
  const ROUTE_KICK_EXEMPT = new Set([
6567
6580
  'constructor', 'readRoute', 'kickRouteReadModel', 'settleRouteReadModel', 'publishRouteReadModel',
6568
6581
  'reconcileRouteReadModel', 'routeReadModelStatus', 'ownRouteFlush', 'routeQuiet',
6582
+ // The copy RPCs wait for the owner after their writes commit. The synchronous write helpers
6583
+ // take the guard, so this tenant's acknowledgment cannot hold up routes or mirror writes.
6584
+ 'setScopeServingRef', 'bindScopeVersion', 'bindScopeVersionJudged', 'moveScopeRow',
6585
+ 'recordScopeScriptCopy', 'settleScopeScriptCopy', 'handOverTenantDueWork',
6569
6586
  // The retry path: its own return must not start a flush, or a failure is retried at once rather than
6570
6587
  // after `ROUTE_PUBLISH_RETRY_MS`; the alarm starts it (`retryRouteReadModel`).
6571
6588
  'armRouteReadModelRetry', 'retryRouteReadModel',