@substrat-run/adapter-cloudflare 0.233.0 → 0.235.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 (47) hide show
  1. package/dist/control-plane-do.d.ts +4 -2
  2. package/dist/control-plane-do.d.ts.map +1 -1
  3. package/dist/control-plane-do.js +9 -5
  4. package/dist/control-plane-do.js.map +1 -1
  5. package/dist/directory-cutover.d.ts +32 -2
  6. package/dist/directory-cutover.d.ts.map +1 -1
  7. package/dist/directory-cutover.js +52 -5
  8. package/dist/directory-cutover.js.map +1 -1
  9. package/dist/directory-mirror-verify.d.ts +17 -0
  10. package/dist/directory-mirror-verify.d.ts.map +1 -1
  11. package/dist/directory-mirror-verify.js +15 -0
  12. package/dist/directory-mirror-verify.js.map +1 -1
  13. package/dist/index.d.ts +4 -2
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +5 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/scope-do.d.ts.map +1 -1
  18. package/dist/scope-do.js +104 -26
  19. package/dist/scope-do.js.map +1 -1
  20. package/dist/scope-facets.d.ts +43 -0
  21. package/dist/scope-facets.d.ts.map +1 -1
  22. package/dist/scope-facets.js +2 -20
  23. package/dist/scope-facets.js.map +1 -1
  24. package/dist/scope-supervisor-do.d.ts +28 -5
  25. package/dist/scope-supervisor-do.d.ts.map +1 -1
  26. package/dist/scope-supervisor-do.js +122 -9
  27. package/dist/scope-supervisor-do.js.map +1 -1
  28. package/dist/tenant-due-coverage.d.ts +71 -0
  29. package/dist/tenant-due-coverage.d.ts.map +1 -0
  30. package/dist/tenant-due-coverage.js +92 -0
  31. package/dist/tenant-due-coverage.js.map +1 -0
  32. package/dist/tenant-due-evidence.d.ts +105 -0
  33. package/dist/tenant-due-evidence.d.ts.map +1 -0
  34. package/dist/tenant-due-evidence.js +164 -0
  35. package/dist/tenant-due-evidence.js.map +1 -0
  36. package/dist/tenant-due-index-do.d.ts +25 -2
  37. package/dist/tenant-due-index-do.d.ts.map +1 -1
  38. package/dist/tenant-due-index-do.js +22 -2
  39. package/dist/tenant-due-index-do.js.map +1 -1
  40. package/dist/tenant-due-nudge.d.ts +3 -2
  41. package/dist/tenant-due-nudge.d.ts.map +1 -1
  42. package/dist/tenant-due-nudge.js.map +1 -1
  43. package/dist/tenant-due-work-do.d.ts +14 -0
  44. package/dist/tenant-due-work-do.d.ts.map +1 -1
  45. package/dist/tenant-due-work-do.js +10 -1
  46. package/dist/tenant-due-work-do.js.map +1 -1
  47. package/package.json +10 -10
package/dist/scope-do.js CHANGED
@@ -19,7 +19,7 @@ import { actorOf, admitPeer, boundToPeerDeclarations, collectPeers, switchPeer,
19
19
  import { assertSameAttachmentGate, attachmentGateOf, decideAttachmentRemove } from '@substrat-run/kernel';
20
20
  import { COPY_RESTORE_FENCE_LAPSED, copyRestoreFenceLapsed, orphanSweepVersionRefusal } from '@substrat-run/kernel';
21
21
  import { createDoTupleChecker, createLocalControlPlaneReader, scopeTupleReader } from './checker.js';
22
- import { clearSettledEmailIn, settlePlatformRequestIn } from '@substrat-run/kernel';
22
+ import { clearSettledIntentIn, settlePlatformRequestIn } from '@substrat-run/kernel';
23
23
  import { loadDropsRowsOf, SCOPE_COPY_LEASE_MS, ulidTime } from '@substrat-run/kernel';
24
24
  import { handoffOutcome, reportHandoff, traceIdOf } from '@substrat-run/kernel';
25
25
  import { currentTraceparent } from '@substrat-run/kernel/invocation-line';
@@ -1220,6 +1220,13 @@ function scopeClass(modules, bareOps, options, runtime) {
1220
1220
  #facetWakeAt = null;
1221
1221
  /** Track B: the tenant this facet's scope was provisioned for, once read (it never changes). */
1222
1222
  #facetTenant = null;
1223
+ /**
1224
+ * #3466: what each supervised call now inside this facet carries (`ScopePlatformCall`), newest
1225
+ * last. A facet's env is empty, so this is the only way its platform switches and reach arrive:
1226
+ * per call, and gone with it. Every path that reaches out runs inside a call; with none inside,
1227
+ * every switch reads off, as a facet's always did.
1228
+ */
1229
+ #platformCalls = [];
1223
1230
  constructor(ctx, env) {
1224
1231
  super(ctx, env);
1225
1232
  // Track B's rollback fence (`scope-facets.ts`): an object a scope supervisor served keeps the
@@ -2075,7 +2082,7 @@ function scopeClass(modules, bareOps, options, runtime) {
2075
2082
  // K-74: with a broker bound, only what it has acknowledged may go: the scope's own marks,
2076
2083
  // which are only ever at or below what the broker holds, so the prune keeps more, never less.
2077
2084
  // A5: only while this scope hands off at all; off, the broker holds nothing it still owes.
2078
- const handedOffThrough = this.env.SUBSTRAT_DELIVERY_BROKER && (await this.handoffOptions()) !== null
2085
+ const handedOffThrough = this.platformSwitches().broker && (await this.handoffOptions()) !== null
2079
2086
  ? ((await this.ctx.storage.get(DELIVERY_HANDOFF_KEY))?.marks?.event ?? null)
2080
2087
  : undefined;
2081
2088
  return this.queue.enqueue(() => this.revision.transactionSync(() => pruneDrainedOutbox(this.switchSql(), {
@@ -3212,13 +3219,24 @@ function scopeClass(modules, bareOps, options, runtime) {
3212
3219
  * A call that throws carries no wake: the supervisor keeps the alarm it armed before the call,
3213
3220
  * and the wake stays here for the next call to carry.
3214
3221
  */
3215
- async supervisedCall(method, args) {
3222
+ async supervisedCall(method, args, platform) {
3216
3223
  this.assertFacet('supervisedCall');
3217
3224
  const callable = method === 'alarm' || method === 'liveAdmit' || (typeof method === 'string' && !SUPERVISOR_OWNED_METHODS.has(method) && definedHere(this, method));
3218
3225
  const fn = callable ? this[method] : undefined;
3219
3226
  if (typeof fn !== 'function')
3220
3227
  throw new Error(`scope facet: no forwarded method '${String(method)}'`);
3221
- const value = await fn.apply(this, Array.isArray(args) ? args : []);
3228
+ // #3466: the platform this call carries, live for exactly as long as the call is inside.
3229
+ const live = platform && typeof platform === 'object' ? platform : null;
3230
+ if (live)
3231
+ this.#platformCalls.push(live);
3232
+ let value;
3233
+ try {
3234
+ value = await fn.apply(this, Array.isArray(args) ? args : []);
3235
+ }
3236
+ finally {
3237
+ if (live)
3238
+ this.#platformCalls.splice(this.#platformCalls.lastIndexOf(live), 1);
3239
+ }
3222
3240
  const wakeAt = this.#facetWakeAt;
3223
3241
  this.#facetWakeAt = null;
3224
3242
  return { value, wakeAt, tenant: this.facetTenant() };
@@ -3291,6 +3309,44 @@ function scopeClass(modules, bareOps, options, runtime) {
3291
3309
  }
3292
3310
  return { cursors, behind };
3293
3311
  }
3312
+ /**
3313
+ * #3466: the platform switches this scope runs under. As an object, its own env, exactly as it
3314
+ * always read them. As a facet, whose env is empty, what the supervised call inside carries
3315
+ * (`ScopePlatformCall`), and off with none inside. Every read of a platform switch goes here.
3316
+ */
3317
+ platformSwitches() {
3318
+ if (runtime === 'facet') {
3319
+ return this.#platformCalls.at(-1)?.switches ?? { broker: false, sweeper: false, wake: false };
3320
+ }
3321
+ const env = this.env;
3322
+ return {
3323
+ broker: env.SUBSTRAT_DELIVERY_BROKER !== undefined,
3324
+ ...(env.SUBSTRAT_DELIVERY_HANDOFF !== undefined ? { deliveryHandoff: env.SUBSTRAT_DELIVERY_HANDOFF } : {}),
3325
+ ...(env.SUBSTRAT_PURGE_DUE !== undefined ? { purgeDue: env.SUBSTRAT_PURGE_DUE } : {}),
3326
+ sweeper: env.SWEEPER !== undefined,
3327
+ wake: Boolean(env.CONTROL_PLANE_URL && env.PLATFORM_SECRET),
3328
+ };
3329
+ }
3330
+ /** #3466: the supervisor's handle for the call inside, when its switches reach out. A facet's only reach. */
3331
+ platformHandle() {
3332
+ return runtime === 'facet' ? (this.#platformCalls.at(-1)?.handle ?? null) : null;
3333
+ }
3334
+ /**
3335
+ * #3540, #3466: ask the sweeper to run this scope's due work (`runScopeDue`) or purges. As an
3336
+ * object, through its own `SWEEPER`; as a facet, through its supervisor, which holds the binding
3337
+ * and names the pair it pinned.
3338
+ */
3339
+ async sweepVia(tenantId, scopeId, schedules) {
3340
+ if (runtime === 'facet') {
3341
+ const handle = this.platformHandle();
3342
+ if (!handle)
3343
+ throw new Error('scope facet: no sweeper reaches this call');
3344
+ return handle.sweep(tenantId, scopeId, schedules);
3345
+ }
3346
+ const sweeper = this.env.SWEEPER;
3347
+ const stub = sweeper.get(sweeper.idFromName(SCOPE_SWEEPER_NAME));
3348
+ return schedules ? stub.runScopeDue(tenantId, scopeId) : stub.runScopePurges(tenantId, scopeId);
3349
+ }
3294
3350
  /**
3295
3351
  * #3540: whether this scope's purges are due by event: the switch is `event`, a sweeper is bound
3296
3352
  * for the alarm to ask, and a registered module declares a purge horizon.
@@ -3300,7 +3356,8 @@ function scopeClass(modules, bareOps, options, runtime) {
3300
3356
  }
3301
3357
  /** #3540: the switch is `event` and a sweeper is bound for the alarm to ask. */
3302
3358
  dueByEventSwitched() {
3303
- return this.env.SUBSTRAT_PURGE_DUE === PURGE_DUE_BY_EVENT && this.env.SWEEPER !== undefined;
3359
+ const switches = this.platformSwitches();
3360
+ return switches.purgeDue === PURGE_DUE_BY_EVENT && switches.sweeper;
3304
3361
  }
3305
3362
  /**
3306
3363
  * #3540: whether anything here can be due by event: the switch, and a purge horizon or a
@@ -3388,6 +3445,10 @@ function scopeClass(modules, bareOps, options, runtime) {
3388
3445
  * own) or building it throws: the alarm then asks the sweeper, so the work is never stranded.
3389
3446
  */
3390
3447
  dueRunHost() {
3448
+ // #3466: a facet's env is empty, so a host built from it reaches nothing: its supervisor asks
3449
+ // the sweeper instead (`sweepVia`).
3450
+ if (runtime === 'facet')
3451
+ return null;
3391
3452
  const hostFor = globalThis[Symbol.for(SCOPE_DUE_HOST_KEY)];
3392
3453
  if (typeof hostFor !== 'function')
3393
3454
  return null;
@@ -3455,7 +3516,6 @@ function scopeClass(modules, bareOps, options, runtime) {
3455
3516
  await this.armAlarmBy(Math.max(at, notBefore));
3456
3517
  return;
3457
3518
  }
3458
- const sweeper = this.env.SWEEPER;
3459
3519
  let answer;
3460
3520
  this.#purgeAlarmRunning = true;
3461
3521
  try {
@@ -3470,10 +3530,7 @@ function scopeClass(modules, bareOps, options, runtime) {
3470
3530
  });
3471
3531
  }
3472
3532
  else {
3473
- const stub = sweeper.get(sweeper.idFromName(SCOPE_SWEEPER_NAME));
3474
- answer = schedules
3475
- ? await stub.runScopeDue(state.tenantId, state.scopeId)
3476
- : await stub.runScopePurges(state.tenantId, state.scopeId);
3533
+ answer = await this.sweepVia(state.tenantId, state.scopeId, schedules);
3477
3534
  }
3478
3535
  }
3479
3536
  catch (err) {
@@ -3518,11 +3575,22 @@ function scopeClass(modules, bareOps, options, runtime) {
3518
3575
  console.error('substrat: due alarm failed; the sweeper pass re-arms it', err);
3519
3576
  }
3520
3577
  }
3521
- /** K-69: a scope with the control plane's origin and secret asks for its own drains. */
3522
- wakeTransport() {
3578
+ /**
3579
+ * K-69: a scope with the control plane's origin and secret asks for its own drains, or null when
3580
+ * it has neither. #3466: a facet asks through its supervisor, which holds both: the secret never
3581
+ * enters the facet.
3582
+ */
3583
+ wakeKick() {
3584
+ if (runtime === 'facet') {
3585
+ const handle = this.platformSwitches().wake ? this.platformHandle() : null;
3586
+ return handle ? (body, timeoutMs) => handle.kick(body, timeoutMs) : null;
3587
+ }
3523
3588
  const url = this.env.CONTROL_PLANE_URL;
3524
3589
  const secret = this.env.PLATFORM_SECRET;
3525
- return url && secret ? { controlPlaneUrl: url, secret } : null;
3590
+ if (!url || !secret)
3591
+ return null;
3592
+ const transport = { controlPlaneUrl: url, secret };
3593
+ return (body, timeoutMs) => kickControlPlane(transport, body, timeoutMs ? AbortSignal.timeout(timeoutMs) : undefined);
3526
3594
  }
3527
3595
  /**
3528
3596
  * K-69: what a commit left for the platform, read by index (`CommitWork`): the newest intent by
@@ -3549,7 +3617,7 @@ function scopeClass(modules, bareOps, options, runtime) {
3549
3617
  * follows: a wake not armed costs latency, and the pass (or its check) is the backstop.
3550
3618
  */
3551
3619
  async noteScopeWake(tenantId, scopeId) {
3552
- if (!this.wakeTransport())
3620
+ if (!this.wakeKick())
3553
3621
  return;
3554
3622
  try {
3555
3623
  const prior = (await this.ctx.storage.get(SCOPE_WAKE_KEY)) ?? null;
@@ -3567,12 +3635,12 @@ function scopeClass(modules, bareOps, options, runtime) {
3567
3635
  }
3568
3636
  /** K-69: the wake's half of the alarm. Never throws: a thrown alarm is retried by the runtime. */
3569
3637
  async runScopeWake() {
3570
- const transport = this.wakeTransport();
3638
+ const kick = this.wakeKick();
3571
3639
  try {
3572
3640
  const state = (await this.ctx.storage.get(SCOPE_WAKE_KEY)) ?? null;
3573
3641
  if (!state)
3574
3642
  return;
3575
- if (!transport) {
3643
+ if (!kick) {
3576
3644
  // The binding went away (a re-upload without it): nothing can be asked, so nothing is kept.
3577
3645
  await this.ctx.storage.delete(SCOPE_WAKE_KEY);
3578
3646
  return;
@@ -3581,7 +3649,7 @@ function scopeClass(modules, bareOps, options, runtime) {
3581
3649
  let answeredEnvironment;
3582
3650
  let answeredLake = false;
3583
3651
  const ran = await runWake(state, Date.now(), () => this.wakeWork(), async (body) => {
3584
- const answer = await kickControlPlane(transport, body);
3652
+ const answer = await kick(body);
3585
3653
  answered = answer?.broker;
3586
3654
  answeredEnvironment = answer?.brokerEnvironment;
3587
3655
  answeredLake = answer?.brokerLake === true;
@@ -3613,6 +3681,15 @@ function scopeClass(modules, bareOps, options, runtime) {
3613
3681
  * when it names none.
3614
3682
  */
3615
3683
  deliveryBroker(tenantId, environment) {
3684
+ if (runtime === 'facet') {
3685
+ // #3466: through the supervisor, which reaches only the broker of the (tenant, app) it
3686
+ // pinned and refuses a call naming another tenant or scope.
3687
+ const handle = this.platformSwitches().broker ? this.platformHandle() : null;
3688
+ if (!handle)
3689
+ return null;
3690
+ const via = (method) => (...args) => handle.broker(environment, method, args);
3691
+ return { marks: via('marks'), handOff: via('handOff'), eraseSubject: via('eraseSubject') };
3692
+ }
3616
3693
  const ns = this.env.SUBSTRAT_DELIVERY_BROKER;
3617
3694
  if (!ns)
3618
3695
  return null;
@@ -3636,10 +3713,10 @@ function scopeClass(modules, bareOps, options, runtime) {
3636
3713
  * This request reads the mode only. A timeout leaves the ordinary wake and drain backstops.
3637
3714
  */
3638
3715
  async refreshShadowHandoff(tenantId, scopeId, environment) {
3639
- const transport = this.wakeTransport();
3640
- if (!transport)
3716
+ const kick = this.wakeKick();
3717
+ if (!kick)
3641
3718
  return false;
3642
- const answer = await kickControlPlane(transport, { tenantId, scopeId, platformRequests: false, events: false, source: 'scope', brokerEnvironments: true }, AbortSignal.timeout(2_000)).catch(() => null);
3719
+ const answer = await kick({ tenantId, scopeId, platformRequests: false, events: false, source: 'scope', brokerEnvironments: true }, 2_000).catch(() => null);
3643
3720
  if (answer?.broker !== 'live' || answer.brokerEnvironment !== environment)
3644
3721
  return false;
3645
3722
  // A regular wake can update the mode while this lookup is in flight.
@@ -3664,7 +3741,7 @@ function scopeClass(modules, bareOps, options, runtime) {
3664
3741
  async handOffCommitted(tenantId, scopeId,
3665
3742
  /** #3474: the invocation whose commit this follows, and its trace: its own rows carry the trace. */
3666
3743
  commit) {
3667
- if (!this.env.SUBSTRAT_DELIVERY_BROKER)
3744
+ if (!this.platformSwitches().broker)
3668
3745
  return;
3669
3746
  // A5 (#3471): only while the tenant is in shadow or live, and under that mode.
3670
3747
  const options = await this.handoffOptions(tenantId, scopeId);
@@ -3747,7 +3824,7 @@ function scopeClass(modules, bareOps, options, runtime) {
3747
3824
  * `SUBSTRAT_DELIVERY_HANDOFF=always` hands off naming no mode, owing every receiver (A1–A4).
3748
3825
  */
3749
3826
  async handoffOptions(tenantId, scopeId) {
3750
- if (this.env.SUBSTRAT_DELIVERY_HANDOFF === 'always')
3827
+ if (this.platformSwitches().deliveryHandoff === 'always')
3751
3828
  return {};
3752
3829
  const state = (await this.ctx.storage.get(DELIVERY_MODE_KEY)) ?? null;
3753
3830
  if (!state)
@@ -6879,10 +6956,11 @@ function scopeClass(modules, bareOps, options, runtime) {
6879
6956
  SET status = ?, result = COALESCE(?, result), last_error = ?, last_failure = ?,
6880
6957
  attempts = attempts + 1, settled_at = ?
6881
6958
  WHERE id = ? AND status = 'pending'`, status, result, lastError, lastFailure, status === 'pending' ? null : now, id);
6882
- // #3568: a settled row's message is cleared when its sender asked, in this transaction, as
6883
- // the kernel's settle does on the other verb. Idempotent, and never a pending row.
6959
+ // #3568: a settled row's message is cleared when its sender asked, and a mail-in address
6960
+ // request's allowlist always, in this transaction, as the kernel's settle does on the other
6961
+ // verb. Idempotent, and never a pending row.
6884
6962
  if (status !== 'pending')
6885
- clearSettledEmailIn(this.switchSql(), id, now);
6963
+ clearSettledIntentIn(this.switchSql(), id, now);
6886
6964
  });
6887
6965
  }
6888
6966
  /**
@@ -8801,7 +8879,7 @@ function scopeClass(modules, bareOps, options, runtime) {
8801
8879
  * neither refuses: fail closed, before the key.
8802
8880
  */
8803
8881
  async eraseSubjectFromBroker(subjectId, at) {
8804
- if (!this.env.SUBSTRAT_DELIVERY_BROKER)
8882
+ if (!this.platformSwitches().broker)
8805
8883
  return;
8806
8884
  const handed = (await this.ctx.storage.get(DELIVERY_HANDOFF_KEY)) ?? null;
8807
8885
  const tenant = tenantIdOf.safeParse(handed?.tenantId ?? this.provisionedFor() ?? this.soleRoleTenant());