@fantastic.dev/better-stripe 0.88.0 → 0.89.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 (38) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAggregate/readMaterial.d.ts.map +1 -1
  4. package/dist/client/billing/renewalAggregate/readMaterial.js +4 -3
  5. package/dist/client/billing/renewalAggregate/readMaterial.js.map +1 -1
  6. package/dist/client/billing/renewalAttach/internal/originalOwner.d.ts +27 -0
  7. package/dist/client/billing/renewalAttach/internal/originalOwner.d.ts.map +1 -0
  8. package/dist/client/billing/renewalAttach/internal/originalOwner.js +236 -0
  9. package/dist/client/billing/renewalAttach/internal/originalOwner.js.map +1 -0
  10. package/dist/client/billing/renewalAttach/internal/sourceBindings.d.ts +5 -0
  11. package/dist/client/billing/renewalAttach/internal/sourceBindings.d.ts.map +1 -0
  12. package/dist/client/billing/renewalAttach/internal/sourceBindings.js +9 -0
  13. package/dist/client/billing/renewalAttach/internal/sourceBindings.js.map +1 -0
  14. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.d.ts +6 -0
  15. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.d.ts.map +1 -0
  16. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.js +35 -0
  17. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.js.map +1 -0
  18. package/dist/client/billing/renewalPriceReplacement.d.ts.map +1 -1
  19. package/dist/client/billing/renewalPriceReplacement.js +7 -3
  20. package/dist/client/billing/renewalPriceReplacement.js.map +1 -1
  21. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  22. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
  23. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  24. package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
  25. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  26. package/dist/component/usageCredits/accounting.fixtures.d.ts +16 -16
  27. package/docs/renewal-context-reads.md +25 -0
  28. package/docs/renewal-phase-assembly.md +7 -0
  29. package/package.json +1 -1
  30. package/src/client/billing/renewalAggregate/readMaterial.ts +5 -3
  31. package/src/client/billing/renewalAttach/internal/originalOwner.test.ts +258 -0
  32. package/src/client/billing/renewalAttach/internal/originalOwner.ts +280 -0
  33. package/src/client/billing/renewalAttach/internal/sourceBindings.test.ts +13 -0
  34. package/src/client/billing/renewalAttach/internal/sourceBindings.ts +8 -0
  35. package/src/client/billing/renewalAttach/internal/stockRawDelegate.test.ts +156 -0
  36. package/src/client/billing/renewalAttach/internal/stockRawDelegate.ts +43 -0
  37. package/src/client/billing/renewalPriceReplacement.ts +7 -3
  38. package/src/client/billing/renewalV2/rawRetention.test.ts +293 -0
@@ -2351,6 +2351,14 @@ export declare function large(count?: number, amount?: number): Promise<{
2351
2351
  preparationId: import("convex/values").GenericId<"balanceFundingPreparations">;
2352
2352
  requestKey: string;
2353
2353
  result?: {
2354
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
2355
+ retryable: false;
2356
+ status: "conflict";
2357
+ } | {
2358
+ reason: "policy_invalid";
2359
+ retryable: false;
2360
+ status: "rejected";
2361
+ } | {
2354
2362
  operation: {
2355
2363
  accountId: string;
2356
2364
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -2361,14 +2369,6 @@ export declare function large(count?: number, amount?: number): Promise<{
2361
2369
  } | {
2362
2370
  enabled: boolean;
2363
2371
  status: "unchanged";
2364
- } | {
2365
- reason: "identity_changed" | "revision_changed" | "scope_busy";
2366
- retryable: false;
2367
- status: "conflict";
2368
- } | {
2369
- reason: "policy_invalid";
2370
- retryable: false;
2371
- status: "rejected";
2372
2372
  } | {
2373
2373
  reason: "provider_pending";
2374
2374
  retryable: true;
@@ -2379,6 +2379,14 @@ export declare function large(count?: number, amount?: number): Promise<{
2379
2379
  payload: import("convex/values").VString<string, "required">;
2380
2380
  preparationId: import("convex/values").VId<import("convex/values").GenericId<"balanceFundingPreparations">, "required">;
2381
2381
  result: import("convex/values").VUnion<{
2382
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
2383
+ retryable: false;
2384
+ status: "conflict";
2385
+ } | {
2386
+ reason: "policy_invalid";
2387
+ retryable: false;
2388
+ status: "rejected";
2389
+ } | {
2382
2390
  operation: {
2383
2391
  accountId: string;
2384
2392
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -2389,14 +2397,6 @@ export declare function large(count?: number, amount?: number): Promise<{
2389
2397
  } | {
2390
2398
  enabled: boolean;
2391
2399
  status: "unchanged";
2392
- } | {
2393
- reason: "identity_changed" | "revision_changed" | "scope_busy";
2394
- retryable: false;
2395
- status: "conflict";
2396
- } | {
2397
- reason: "policy_invalid";
2398
- retryable: false;
2399
- status: "rejected";
2400
2400
  } | {
2401
2401
  reason: "provider_pending";
2402
2402
  retryable: true;
@@ -7207,6 +7207,14 @@ export declare function withHistory(count?: number, amount?: number): Promise<{
7207
7207
  preparationId: import("convex/values").GenericId<"balanceFundingPreparations">;
7208
7208
  requestKey: string;
7209
7209
  result?: {
7210
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
7211
+ retryable: false;
7212
+ status: "conflict";
7213
+ } | {
7214
+ reason: "policy_invalid";
7215
+ retryable: false;
7216
+ status: "rejected";
7217
+ } | {
7210
7218
  operation: {
7211
7219
  accountId: string;
7212
7220
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -7217,14 +7225,6 @@ export declare function withHistory(count?: number, amount?: number): Promise<{
7217
7225
  } | {
7218
7226
  enabled: boolean;
7219
7227
  status: "unchanged";
7220
- } | {
7221
- reason: "identity_changed" | "revision_changed" | "scope_busy";
7222
- retryable: false;
7223
- status: "conflict";
7224
- } | {
7225
- reason: "policy_invalid";
7226
- retryable: false;
7227
- status: "rejected";
7228
7228
  } | {
7229
7229
  reason: "provider_pending";
7230
7230
  retryable: true;
@@ -7235,6 +7235,14 @@ export declare function withHistory(count?: number, amount?: number): Promise<{
7235
7235
  payload: import("convex/values").VString<string, "required">;
7236
7236
  preparationId: import("convex/values").VId<import("convex/values").GenericId<"balanceFundingPreparations">, "required">;
7237
7237
  result: import("convex/values").VUnion<{
7238
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
7239
+ retryable: false;
7240
+ status: "conflict";
7241
+ } | {
7242
+ reason: "policy_invalid";
7243
+ retryable: false;
7244
+ status: "rejected";
7245
+ } | {
7238
7246
  operation: {
7239
7247
  accountId: string;
7240
7248
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -7245,14 +7253,6 @@ export declare function withHistory(count?: number, amount?: number): Promise<{
7245
7253
  } | {
7246
7254
  enabled: boolean;
7247
7255
  status: "unchanged";
7248
- } | {
7249
- reason: "identity_changed" | "revision_changed" | "scope_busy";
7250
- retryable: false;
7251
- status: "conflict";
7252
- } | {
7253
- reason: "policy_invalid";
7254
- retryable: false;
7255
- status: "rejected";
7256
7256
  } | {
7257
7257
  reason: "provider_pending";
7258
7258
  retryable: true;
@@ -9,6 +9,17 @@ export declare const bridge: import("convex/server").RegisteredAction<"internal"
9
9
  stripeSubscriptionId: string;
10
10
  version: number;
11
11
  }, Promise<{
12
+ status: "ready";
13
+ value: {
14
+ customerAccount: string;
15
+ enabled?: boolean | undefined;
16
+ paymentMethodId: string | null;
17
+ paymentMethodTypes?: string[] | null | undefined;
18
+ phase: "apply" | "attach" | "create";
19
+ providerApiVersion: string;
20
+ stripeSubscriptionId: string;
21
+ };
22
+ } | {
12
23
  reason: "history_incomplete" | "provider_outcome_unknown";
13
24
  retryable: false;
14
25
  status: "unknown";
@@ -24,16 +35,5 @@ export declare const bridge: import("convex/server").RegisteredAction<"internal"
24
35
  reason: "provider_pending";
25
36
  retryable: true;
26
37
  status: "not_ready";
27
- } | {
28
- status: "ready";
29
- value: {
30
- customerAccount: string;
31
- enabled?: boolean | undefined;
32
- paymentMethodId: string | null;
33
- paymentMethodTypes?: string[] | null | undefined;
34
- phase: "apply" | "attach" | "create";
35
- providerApiVersion: string;
36
- stripeSubscriptionId: string;
37
- };
38
38
  }>>;
39
39
  //# sourceMappingURL=actions.d.ts.map
@@ -9,9 +9,9 @@ export declare function legOwnership(ctx: MutationCtx, bucketKey: string, prior:
9
9
  orgId: string | undefined;
10
10
  ownershipStatus: "ambiguous";
11
11
  } | {
12
- registrationId?: undefined;
13
12
  userId?: undefined;
14
13
  orgId?: undefined;
14
+ registrationId?: undefined;
15
15
  ownershipStatus: "ambiguous";
16
16
  } | {
17
17
  registrationId: import("convex/values").GenericId<"financialOwnership">;
@@ -19,9 +19,9 @@ export declare function legOwnership(ctx: MutationCtx, bucketKey: string, prior:
19
19
  orgId: string | undefined;
20
20
  ownershipStatus: "unknown";
21
21
  } | {
22
- registrationId?: undefined;
23
22
  userId?: undefined;
24
23
  orgId?: undefined;
24
+ registrationId?: undefined;
25
25
  ownershipStatus: "unknown";
26
26
  } | {
27
27
  registrationId: import("convex/values").GenericId<"financialOwnership">;
@@ -29,9 +29,9 @@ export declare function legOwnership(ctx: MutationCtx, bucketKey: string, prior:
29
29
  orgId: string | undefined;
30
30
  ownershipStatus: "contradictory";
31
31
  } | {
32
- registrationId?: undefined;
33
32
  userId?: undefined;
34
33
  orgId?: undefined;
34
+ registrationId?: undefined;
35
35
  ownershipStatus: "contradictory";
36
36
  } | {
37
37
  registrationId: import("convex/values").GenericId<"financialOwnership">;
@@ -2351,14 +2351,6 @@ export declare function setupAllowance(): Promise<{
2351
2351
  preparationId: import("convex/values").GenericId<"balanceFundingPreparations">;
2352
2352
  requestKey: string;
2353
2353
  result?: {
2354
- reason: "identity_changed" | "revision_changed" | "scope_busy";
2355
- retryable: false;
2356
- status: "conflict";
2357
- } | {
2358
- reason: "policy_invalid";
2359
- retryable: false;
2360
- status: "rejected";
2361
- } | {
2362
2354
  operation: {
2363
2355
  accountId: string;
2364
2356
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -2369,6 +2361,14 @@ export declare function setupAllowance(): Promise<{
2369
2361
  } | {
2370
2362
  enabled: boolean;
2371
2363
  status: "unchanged";
2364
+ } | {
2365
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
2366
+ retryable: false;
2367
+ status: "conflict";
2368
+ } | {
2369
+ reason: "policy_invalid";
2370
+ retryable: false;
2371
+ status: "rejected";
2372
2372
  } | {
2373
2373
  reason: "provider_pending";
2374
2374
  retryable: true;
@@ -2379,14 +2379,6 @@ export declare function setupAllowance(): Promise<{
2379
2379
  payload: import("convex/values").VString<string, "required">;
2380
2380
  preparationId: import("convex/values").VId<import("convex/values").GenericId<"balanceFundingPreparations">, "required">;
2381
2381
  result: import("convex/values").VUnion<{
2382
- reason: "identity_changed" | "revision_changed" | "scope_busy";
2383
- retryable: false;
2384
- status: "conflict";
2385
- } | {
2386
- reason: "policy_invalid";
2387
- retryable: false;
2388
- status: "rejected";
2389
- } | {
2390
2382
  operation: {
2391
2383
  accountId: string;
2392
2384
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -2397,6 +2389,14 @@ export declare function setupAllowance(): Promise<{
2397
2389
  } | {
2398
2390
  enabled: boolean;
2399
2391
  status: "unchanged";
2392
+ } | {
2393
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
2394
+ retryable: false;
2395
+ status: "conflict";
2396
+ } | {
2397
+ reason: "policy_invalid";
2398
+ retryable: false;
2399
+ status: "rejected";
2400
2400
  } | {
2401
2401
  reason: "provider_pending";
2402
2402
  retryable: true;
@@ -2351,6 +2351,14 @@ export declare function setupCredits(): Promise<{
2351
2351
  preparationId: import("convex/values").GenericId<"balanceFundingPreparations">;
2352
2352
  requestKey: string;
2353
2353
  result?: {
2354
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
2355
+ retryable: false;
2356
+ status: "conflict";
2357
+ } | {
2358
+ reason: "policy_invalid";
2359
+ retryable: false;
2360
+ status: "rejected";
2361
+ } | {
2354
2362
  operation: {
2355
2363
  accountId: string;
2356
2364
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -2361,14 +2369,6 @@ export declare function setupCredits(): Promise<{
2361
2369
  } | {
2362
2370
  enabled: boolean;
2363
2371
  status: "unchanged";
2364
- } | {
2365
- reason: "identity_changed" | "revision_changed" | "scope_busy";
2366
- retryable: false;
2367
- status: "conflict";
2368
- } | {
2369
- reason: "policy_invalid";
2370
- retryable: false;
2371
- status: "rejected";
2372
2372
  } | {
2373
2373
  reason: "provider_pending";
2374
2374
  retryable: true;
@@ -2379,6 +2379,14 @@ export declare function setupCredits(): Promise<{
2379
2379
  payload: import("convex/values").VString<string, "required">;
2380
2380
  preparationId: import("convex/values").VId<import("convex/values").GenericId<"balanceFundingPreparations">, "required">;
2381
2381
  result: import("convex/values").VUnion<{
2382
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
2383
+ retryable: false;
2384
+ status: "conflict";
2385
+ } | {
2386
+ reason: "policy_invalid";
2387
+ retryable: false;
2388
+ status: "rejected";
2389
+ } | {
2382
2390
  operation: {
2383
2391
  accountId: string;
2384
2392
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -2389,14 +2397,6 @@ export declare function setupCredits(): Promise<{
2389
2397
  } | {
2390
2398
  enabled: boolean;
2391
2399
  status: "unchanged";
2392
- } | {
2393
- reason: "identity_changed" | "revision_changed" | "scope_busy";
2394
- retryable: false;
2395
- status: "conflict";
2396
- } | {
2397
- reason: "policy_invalid";
2398
- retryable: false;
2399
- status: "rejected";
2400
2400
  } | {
2401
2401
  reason: "provider_pending";
2402
2402
  retryable: true;
@@ -31,6 +31,31 @@ and complete records under immutable authorization. An explicitly chosen attach
31
31
  key may equal the logical key; no key is silently reused, derived or generated.
32
32
  A future configure key must differ from the attachment key.
33
33
 
34
+ Preparation validates the original Subscription, target Price and optional
35
+ billing Customer, then retains their complete captured JSON bodies. Eligible
36
+ unknown fields, including own JSON keys such as `__proto__`, remain in the
37
+ snapshot and binding; validation does not replace the retained body with a
38
+ selected-field view. Unsupported fields in strict schemas still reject.
39
+
40
+ Older snapshots that already omitted a field cannot recover it or establish
41
+ retroactive full-record provenance. Reconstructing a changed full record does
42
+ not migrate its old binding or authorize a retry.
43
+
44
+ ## Internal ownership and transport groundwork
45
+
46
+ The internal `renewalAttach/internal` modules keep one original owner identity
47
+ and adapt the stock Stripe client's inherited `rawRequest` method without
48
+ changing its receiver, arguments, result or thrown value. The owner checks the
49
+ same original wall and monotonic time bounds and closes permanently on expiry,
50
+ regression or retirement. These modules do not read a clock at import time.
51
+
52
+ The compiled entry registry remains empty, and the original Source and stock
53
+ client bindings remain unbound. There is no package export or operational
54
+ attachment actor in this increment. It does not authorize reads, dispatch a
55
+ request, enable recovery or establish provider custody or native qualification.
56
+ The internal Node-only mechanics are not a caller-supplied clock, client or
57
+ qualification API. The public preparation and receipt constructors are unchanged.
58
+
34
59
  ## Fixed acquisition profile
35
60
 
36
61
  The initial profile supports platform requests using the configured
@@ -60,6 +60,13 @@ full-baseline and default-preservation checks apply. Actual subscription
60
60
  association is required; the constructor does not replace it with null to pass
61
61
  the original preparation checks.
62
62
 
63
+ Fresh Subscription, target Price and billing Customer comparisons use their
64
+ complete captured JSON bodies, including eligible unknown fields. Only the
65
+ Subscription's root Schedule association is handled separately by the existing
66
+ ownership checks. Parsed semantic views do not permit unknown-field drift or
67
+ replace the retained raw baseline. Unsupported strict-schema fields still
68
+ reject, and snapshot equality remains an unsigned data comparison.
69
+
63
70
  Success is `projected_configure_request`, with version `2`, the complete
64
71
  normalized two-phase payload and `providerQualification: "required"`. C4
65
72
  (`renewal-baseline`) binds the entire original Schedule body. C5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fantastic.dev/better-stripe",
3
- "version": "0.88.0",
3
+ "version": "0.89.0",
4
4
  "description": "Convex component for Stripe Accounts v2, subscriptions, metered billing and Connect marketplaces, with React workflows.",
5
5
  "keywords": [
6
6
  "stripe",
@@ -68,14 +68,16 @@ export function validateReadMaterial(
68
68
  }
69
69
  const { schedule: originalSchedule, ...original } =
70
70
  prepared.providerSnapshot.subscription;
71
- const { schedule: freshSchedule, ...fresh } = source;
71
+ // Compare complete captured bodies, not parser outputs that may omit own keys.
72
+ const { schedule: freshSchedule, ...fresh } =
73
+ args.freshSubscription as typeof source;
72
74
  if (scheduleId ? originalSchedule !== null : freshSchedule !== null)
73
75
  conflict("schedule_association_mismatch");
74
76
  if (!equal(original, fresh)) conflict("original_source_drift");
75
77
  const target = renewalPrice.safeParse(args.targetPrice);
76
78
  if (!target.success || !target.data.active)
77
79
  unsupported("unsupported_target_price");
78
- if (!equal(target.data, prepared.providerSnapshot.targetPrice))
80
+ if (!equal(args.targetPrice, prepared.providerSnapshot.targetPrice))
79
81
  conflict("target_price_drift");
80
82
  if (
81
83
  prepared.providerSnapshot.billingCustomer === undefined ||
@@ -84,7 +86,7 @@ export function validateReadMaterial(
84
86
  unsupported("missing_retained_billing_customer");
85
87
  const customer = renewalBillingCustomer.safeParse(args.billingCustomer);
86
88
  if (!customer.success) unsupported("unsupported_billing_customer");
87
- if (!equal(customer.data, prepared.providerSnapshot.billingCustomer))
89
+ if (!equal(args.billingCustomer, prepared.providerSnapshot.billingCustomer))
88
90
  conflict("billing_customer_drift");
89
91
  const referenceContext = {
90
92
  customerAccountId: prepared.expected.customerAccountId,
@@ -0,0 +1,258 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+
3
+ afterEach(() => {
4
+ vi.doUnmock("./sourceBindings.js");
5
+ vi.resetModules();
6
+ });
7
+
8
+ function fixture() {
9
+ const basis = {
10
+ startWallMs: 1_000_000,
11
+ startNs: 1_000_000_000n,
12
+ workEndWallMs: 1_180_000,
13
+ workEndNs: 1_180_000_000n,
14
+ endWallMs: 1_210_000,
15
+ endNs: 1_210_000_000n,
16
+ };
17
+ const policy = { firstDispatchedAt: 1000, periodStart: 900, periodEnd: 2000 };
18
+ const sample = { wallMs: basis.startWallMs, ns: basis.startNs };
19
+ let samples = 0;
20
+ let loads = 0;
21
+ let detached = 0;
22
+ let listener = () => {};
23
+ const source = {
24
+ clock: {
25
+ basis,
26
+ sample() {
27
+ samples++;
28
+ return { ...sample };
29
+ },
30
+ subscribeRetirement(fn: () => void) {
31
+ listener = fn;
32
+ return () => {
33
+ detached++;
34
+ };
35
+ },
36
+ },
37
+ originalPolicy: policy,
38
+ originAssociation: Object.freeze({}),
39
+ };
40
+ return {
41
+ basis,
42
+ policy,
43
+ sample,
44
+ source,
45
+ load() {
46
+ loads++;
47
+ return source;
48
+ },
49
+ retire() {
50
+ listener();
51
+ },
52
+ counts() {
53
+ return { samples, loads, detached };
54
+ },
55
+ };
56
+ }
57
+
58
+ async function owner(f = fixture()) {
59
+ vi.resetModules();
60
+ vi.doMock("./sourceBindings.js", () => ({
61
+ loadOwnedOriginalSource: () => f.load(),
62
+ }));
63
+ return { f, OWNER: (await import("./originalOwner.js")).OWNER };
64
+ }
65
+
66
+ it("valid original owner is live with exactly one accepted sample", async () => {
67
+ const { OWNER, f } = await owner();
68
+ let actual: unknown;
69
+ try {
70
+ actual = OWNER.check();
71
+ } catch {
72
+ actual = "stopped";
73
+ }
74
+ expect(actual).toEqual({
75
+ wallMs: 1_000_000,
76
+ ns: 1_000_000_000n,
77
+ nowSeconds: 1000,
78
+ retired: false,
79
+ });
80
+ expect(f.counts()).toEqual({ loads: 1, samples: 1, detached: 0 });
81
+ expect(OWNER.sample).toBe(OWNER.check);
82
+ expect(Object.isFrozen(OWNER)).toBe(true);
83
+ });
84
+
85
+ it("loads lazily once, copies the original basis, and does not sample on basis access", async () => {
86
+ const { OWNER, f } = await owner();
87
+ expect(f.counts().loads).toBe(0);
88
+ const captured = OWNER.basis;
89
+ f.basis.workEndWallMs = 9_000_000;
90
+ expect(OWNER.basis).toBe(captured);
91
+ expect(captured.workEndWallMs).toBe(1_180_000);
92
+ expect(Object.isFrozen(captured)).toBe(true);
93
+ expect(f.counts()).toEqual({ loads: 1, samples: 0, detached: 0 });
94
+ });
95
+
96
+ it.each(["wall", "ns"] as const)(
97
+ "original %s work end is exclusive and irreversibly closes",
98
+ async (clock) => {
99
+ const { OWNER, f } = await owner();
100
+ OWNER.check();
101
+ if (clock === "wall") f.sample.wallMs = f.basis.workEndWallMs;
102
+ else f.sample.ns = f.basis.workEndNs;
103
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
104
+ f.sample.wallMs = f.basis.startWallMs;
105
+ f.sample.ns = f.basis.startNs;
106
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
107
+ expect(f.counts().samples).toBe(2);
108
+ },
109
+ );
110
+
111
+ it.each(["wall", "ns"] as const)(
112
+ "%s regression cannot renew the owner",
113
+ async (clock) => {
114
+ const { OWNER, f } = await owner();
115
+ f.sample.wallMs++;
116
+ f.sample.ns++;
117
+ OWNER.check();
118
+ if (clock === "wall") f.sample.wallMs--;
119
+ else f.sample.ns--;
120
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
121
+ },
122
+ );
123
+
124
+ it("derives floor seconds from the same sample and enforces future first dispatch", async () => {
125
+ const f = fixture();
126
+ f.policy.firstDispatchedAt = 1001;
127
+ const { OWNER } = await owner(f);
128
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
129
+ expect(f.counts().samples).toBe(1);
130
+ });
131
+
132
+ it("age 86400 is refused independently under a declared long synthetic basis", async () => {
133
+ const f = fixture();
134
+ f.basis.workEndWallMs = 100_000_000;
135
+ f.basis.endWallMs = 100_001_000;
136
+ f.basis.workEndNs = 100_000_000_000n;
137
+ f.basis.endNs = 100_001_000_000n;
138
+ f.policy.periodEnd = 200_000;
139
+ const { OWNER } = await owner(f);
140
+ f.sample.wallMs = (1000 + 86399) * 1000;
141
+ expect(OWNER.check().nowSeconds).toBe(87399);
142
+ f.sample.wallMs += 1000;
143
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
144
+ });
145
+
146
+ it("paid period start is inclusive and end exclusive", async () => {
147
+ const f = fixture();
148
+ f.policy.periodStart = 1000;
149
+ f.policy.periodEnd = 1001;
150
+ const { OWNER } = await owner(f);
151
+ expect(OWNER.check().nowSeconds).toBe(1000);
152
+ f.sample.wallMs = 1_001_000;
153
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
154
+ });
155
+
156
+ it("rejects a Source Proxy before any reflective traps or samples", async () => {
157
+ const f = fixture();
158
+ let traps = 0;
159
+ const value = new Proxy(f.source, {
160
+ getPrototypeOf() {
161
+ traps++;
162
+ return Object.prototype;
163
+ },
164
+ ownKeys() {
165
+ traps++;
166
+ return [];
167
+ },
168
+ get() {
169
+ traps++;
170
+ return null;
171
+ },
172
+ });
173
+ f.load = () => value;
174
+ const { OWNER } = await owner(f);
175
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
176
+ expect(traps).toBe(0);
177
+ expect(f.counts().samples).toBe(0);
178
+ });
179
+
180
+ it("rejects an own accessor without invoking it", async () => {
181
+ const f = fixture();
182
+ let gets = 0;
183
+ Object.defineProperty(f.source, "clock", {
184
+ get() {
185
+ gets++;
186
+ return null;
187
+ },
188
+ enumerable: true,
189
+ });
190
+ const { OWNER } = await owner(f);
191
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
192
+ expect(gets).toBe(0);
193
+ });
194
+
195
+ it("retirement notifies active waiters once and never samples again", async () => {
196
+ const { OWNER, f } = await owner();
197
+ let active = 0;
198
+ let removed = 0;
199
+ OWNER.check();
200
+ OWNER.subscribe(() => {
201
+ active++;
202
+ });
203
+ const unsubscribe = OWNER.subscribe(() => {
204
+ removed++;
205
+ });
206
+ unsubscribe();
207
+ unsubscribe();
208
+ f.retire();
209
+ OWNER.retire();
210
+ expect(active).toBe(1);
211
+ expect(removed).toBe(0);
212
+ expect(f.counts().detached).toBe(1);
213
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
214
+ expect(f.counts().samples).toBe(1);
215
+ });
216
+
217
+ it("cannot revive after a Source loader catches a reentrant check", async () => {
218
+ const f = fixture();
219
+ let nested = () => {};
220
+ const ordinaryLoad = f.load;
221
+ f.load = () => {
222
+ try {
223
+ nested();
224
+ } catch {
225
+ /* The outer load must stay closed. */
226
+ }
227
+ return ordinaryLoad();
228
+ };
229
+ const { OWNER } = await owner(f);
230
+ nested = () => {
231
+ OWNER.check();
232
+ };
233
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
234
+ expect(f.counts().samples).toBe(0);
235
+ });
236
+
237
+ it("cannot revive during listener registration and detaches its returned listener", async () => {
238
+ const f = fixture();
239
+ let nested = () => {};
240
+ let detached = 0;
241
+ f.source.clock.subscribeRetirement = () => {
242
+ try {
243
+ nested();
244
+ } catch {
245
+ /* The registration cannot publish live. */
246
+ }
247
+ return () => {
248
+ detached++;
249
+ };
250
+ };
251
+ const { OWNER } = await owner(f);
252
+ nested = () => {
253
+ OWNER.check();
254
+ };
255
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
256
+ expect(detached).toBe(1);
257
+ expect(f.counts().samples).toBe(0);
258
+ });