@fantastic.dev/better-stripe 0.97.1 → 0.99.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 (111) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.d.ts +11 -0
  4. package/dist/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.d.ts.map +1 -1
  5. package/dist/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.js +39 -1
  6. package/dist/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.js.map +1 -1
  7. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.d.ts +22 -0
  8. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.d.ts.map +1 -1
  9. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.js +13 -5
  10. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.js.map +1 -1
  11. package/dist/client/billing/renewalAttach/internal/operations/actorFailureControl.d.ts +17 -0
  12. package/dist/client/billing/renewalAttach/internal/operations/actorFailureControl.d.ts.map +1 -0
  13. package/dist/client/billing/renewalAttach/internal/operations/actorFailureControl.js +140 -0
  14. package/dist/client/billing/renewalAttach/internal/operations/actorFailureControl.js.map +1 -0
  15. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.d.ts +61 -9
  16. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.d.ts.map +1 -1
  17. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.js +41 -411
  18. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.js.map +1 -1
  19. package/dist/client/billing/renewalAttach/internal/operations/initialActorData.d.ts +21 -0
  20. package/dist/client/billing/renewalAttach/internal/operations/initialActorData.d.ts.map +1 -0
  21. package/dist/client/billing/renewalAttach/internal/operations/initialActorData.js +36 -0
  22. package/dist/client/billing/renewalAttach/internal/operations/initialActorData.js.map +1 -0
  23. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.d.ts +141 -0
  24. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.d.ts.map +1 -1
  25. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.js +132 -182
  26. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.js.map +1 -1
  27. package/dist/client/billing/renewalAttach/internal/operations/ownedAwait.d.ts +1 -0
  28. package/dist/client/billing/renewalAttach/internal/operations/ownedAwait.d.ts.map +1 -1
  29. package/dist/client/billing/renewalAttach/internal/operations/ownedAwait.js +17 -5
  30. package/dist/client/billing/renewalAttach/internal/operations/ownedAwait.js.map +1 -1
  31. package/dist/client/billing/renewalAttach/internal/operations/transportCapture.d.ts +35 -0
  32. package/dist/client/billing/renewalAttach/internal/operations/transportCapture.d.ts.map +1 -0
  33. package/dist/client/billing/renewalAttach/internal/operations/transportCapture.js +46 -0
  34. package/dist/client/billing/renewalAttach/internal/operations/transportCapture.js.map +1 -0
  35. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.d.ts +63 -0
  36. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.d.ts.map +1 -0
  37. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.js +380 -0
  38. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.js.map +1 -0
  39. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts +8 -0
  40. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts.map +1 -1
  41. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js +25 -0
  42. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js.map +1 -1
  43. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.d.ts +56 -0
  44. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.d.ts.map +1 -0
  45. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.js +49 -0
  46. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.js.map +1 -0
  47. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts +1 -0
  48. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts.map +1 -1
  49. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js +3 -0
  50. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js.map +1 -1
  51. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.d.ts +65 -0
  52. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.d.ts.map +1 -0
  53. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.js +162 -0
  54. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.js.map +1 -0
  55. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.d.ts +4 -0
  56. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.d.ts.map +1 -0
  57. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.js +87 -0
  58. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.js.map +1 -0
  59. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.d.ts +368 -0
  60. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.d.ts.map +1 -0
  61. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.js +106 -0
  62. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.js.map +1 -0
  63. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.d.ts +4 -0
  64. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.d.ts.map +1 -1
  65. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.js +24 -3
  66. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.js.map +1 -1
  67. package/dist/client/billing/renewalAttach/internal/recovery/output.d.ts +1 -0
  68. package/dist/client/billing/renewalAttach/internal/recovery/output.d.ts.map +1 -1
  69. package/dist/client/billing/renewalAttach/internal/recovery/output.js +22 -8
  70. package/dist/client/billing/renewalAttach/internal/recovery/output.js.map +1 -1
  71. package/dist/client/billing/renewalAttach/internal/recovery/record.d.ts +1 -0
  72. package/dist/client/billing/renewalAttach/internal/recovery/record.d.ts.map +1 -1
  73. package/dist/client/billing/renewalAttach/internal/recovery/record.js +10 -2
  74. package/dist/client/billing/renewalAttach/internal/recovery/record.js.map +1 -1
  75. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.d.ts +3 -0
  76. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.d.ts.map +1 -1
  77. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.js +69 -156
  78. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.js.map +1 -1
  79. package/dist/client/billing/renewalAttach/internal/recovery/relation.d.ts +18 -0
  80. package/dist/client/billing/renewalAttach/internal/recovery/relation.d.ts.map +1 -1
  81. package/dist/client/billing/renewalAttach/internal/recovery/relation.js +15 -1
  82. package/dist/client/billing/renewalAttach/internal/recovery/relation.js.map +1 -1
  83. package/docs/renewal-context-reads.md +69 -8
  84. package/package.json +1 -1
  85. package/src/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.ts +45 -3
  86. package/src/client/billing/renewalAttach/internal/c10/attachEvents.ts +17 -5
  87. package/src/client/billing/renewalAttach/internal/operations/actorFailureControl.ts +165 -0
  88. package/src/client/billing/renewalAttach/internal/operations/fixedTransport.ts +48 -535
  89. package/src/client/billing/renewalAttach/internal/operations/initialActorData.ts +41 -0
  90. package/src/client/billing/renewalAttach/internal/operations/initialAttach.ts +168 -209
  91. package/src/client/billing/renewalAttach/internal/operations/ownedAwait.ts +23 -5
  92. package/src/client/billing/renewalAttach/internal/operations/transportCapture.ts +60 -0
  93. package/src/client/billing/renewalAttach/internal/operations/transportKernel.ts +500 -0
  94. package/src/client/billing/renewalAttach/internal/producer/invocationProducer.ts +28 -0
  95. package/src/client/billing/renewalAttach/internal/producer/journalWorkFactory.ts +54 -0
  96. package/src/client/billing/renewalAttach/internal/producer/lineageAuthority.ts +3 -0
  97. package/src/client/billing/renewalAttach/internal/producer/operationFactory.ts +176 -0
  98. package/src/client/billing/renewalAttach/internal/producer/producerAcquisition.integration.test.ts +240 -0
  99. package/src/client/billing/renewalAttach/internal/producer/producerActor.integration.test.ts +526 -0
  100. package/src/client/billing/renewalAttach/internal/producer/producerRecovery.integration.test.ts +394 -0
  101. package/src/client/billing/renewalAttach/internal/producer/producerRecoveryControls.integration.test.ts +437 -0
  102. package/src/client/billing/renewalAttach/internal/producer/producerRecoveryIsolation.integration.test.ts +332 -0
  103. package/src/client/billing/renewalAttach/internal/producer/producerTransport.integration.test.ts +440 -0
  104. package/src/client/billing/renewalAttach/internal/producer/recoveryEntry.ts +109 -0
  105. package/src/client/billing/renewalAttach/internal/producer/recoveryFactory.ts +135 -0
  106. package/src/client/billing/renewalAttach/internal/producer/stockDelegateFactory.ts +29 -5
  107. package/src/client/billing/renewalAttach/internal/recovery/output.ts +34 -8
  108. package/src/client/billing/renewalAttach/internal/recovery/record.ts +16 -2
  109. package/src/client/billing/renewalAttach/internal/recovery/recoveryActor.ts +107 -188
  110. package/src/client/billing/renewalAttach/internal/recovery/relation.ts +17 -1
  111. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +82 -11
@@ -1,6 +1,6 @@
1
1
  import { admitInitialOutput } from "../recovery/material.js";
2
2
  import { types } from "node:util";
3
- import { OWNER } from "../originalOwner.js";
3
+ import { OWNER as LEGACY_OWNER } from "../originalOwner.js";
4
4
  import {
5
5
  prepareRenewalPriceReplacementV2 as c1,
6
6
  prepareRenewalScheduleAttachmentV2 as c2,
@@ -18,8 +18,8 @@ import {
18
18
  type FailureControl,
19
19
  } from "./types.js";
20
20
  import { equal } from "../../../renewalAggregate/shared.js";
21
- import { owned } from "./ownedAwait.js";
22
- import type { Diagnostics } from "./providerFailure.js";
21
+ import { owned as legacyOwned, ownedForInitialLineage } from "./ownedAwait.js";
22
+ import { captureActorFailureControl } from "./actorFailureControl.js";
23
23
  import { bindRawAcquisitionFromSource } from "../acquisition/ownedAcquisition.js";
24
24
  import { createAttachEventCompiler } from "../c10/attachEvents.js";
25
25
  import {
@@ -29,46 +29,62 @@ import {
29
29
  } from "./sourceOperations.js";
30
30
  import { createInitialOwnedTransportWithAttemptCapability as createPair } from "./fixedTransport.js";
31
31
 
32
+ import { sourceMethod, stop, need, seven, refuse } from "./initialActorData.js";
33
+ import { ownerForInitialLineage } from "../producer/ownerFactory.js";
34
+ import { readInitialLineage } from "../producer/lineageAuthority.js";
35
+ import {
36
+ operationForInitialLineage,
37
+ retainInitialReceiptForLineage,
38
+ } from "../producer/operationFactory.js";
39
+ import { createInitialTransportForLineage } from "./fixedTransport.js";
40
+ import { createInitialMaterialClassifiers } from "../material/classifiers.js";
41
+ import { acquisitionForInitialLineage } from "../acquisition/ownedAcquisition.js";
42
+ import { createInitialAttachEventCompiler } from "../c10/attachEvents.js";
43
+ import { journalWorkForInitialLineage } from "../producer/journalWorkFactory.js";
32
44
  type Pair = ReturnType<typeof createPair>;
33
45
 
34
- function sourceMethod(object: unknown, name: string): SourceMethod {
35
- need(object !== null && typeof object === "object" && !types.isProxy(object));
36
- const d = Object.getOwnPropertyDescriptor(object, name);
46
+ type AttemptCustody = {
47
+ entry: unknown;
48
+ retainedPredicate: SourceMethod;
49
+ freshPredicate: SourceMethod;
50
+ originalRecord: unknown;
51
+ raw: ReturnType<typeof captureRetainedInput>["attachRequest"];
52
+ prepared: Awaited<ReturnType<typeof c1>>;
53
+ request: AttachRequest;
54
+ pair: Pair;
55
+ response: Readonly<{ body: unknown; status: number }> | null;
56
+ settled: boolean;
57
+ returnedACK: boolean;
58
+ reserved: boolean;
59
+ };
60
+ const attempts = new WeakMap<object, AttemptCustody>();
61
+ /** Consumes only actual settled attempt custody; stored DATA cannot register it. */
62
+ export function takeInitialRecoveryRecord(token: object) {
63
+ need(arguments.length === 1);
64
+ const owner = ownerForInitialLineage(token);
65
+ owner.check();
66
+ const attempt = attempts.get(token);
67
+ need(attempt && attempt.settled && !attempt.returnedACK && !attempt.reserved);
68
+ need(journalWorkForInitialLineage(token).idle());
69
+ const disposition = attempt.pair.transport.postDisposition();
37
70
  need(
38
- d &&
39
- d.enumerable &&
40
- "value" in d &&
41
- typeof d.value === "function" &&
42
- !types.isProxy(d.value),
71
+ disposition === "REJECTED" ||
72
+ disposition === "FULFILLED_SAFE" ||
73
+ disposition === "FULFILLED_UNVERIFIED",
43
74
  );
44
- return d.value as SourceMethod;
45
- }
46
- const stop = (): never => {
47
- throw Error("INITIAL_ACTOR_STOP");
48
- };
49
- function need(value: unknown): asserts value {
50
- if (!value) stop();
51
- }
52
- function seven(request: AttachRequest) {
53
- const { scope, apiVersion, method, path, payload, key, firstDispatchedAt } =
54
- request;
75
+ attempt.reserved = true;
55
76
  return Object.freeze({
56
- scope,
57
- apiVersion,
58
- method,
59
- path,
60
- payload,
61
- key,
62
- firstDispatchedAt,
77
+ entry: attempt.entry,
78
+ retainedPredicate: attempt.retainedPredicate,
79
+ freshPredicate: attempt.freshPredicate,
80
+ originalRecord: attempt.originalRecord,
81
+ raw: attempt.raw,
82
+ prepared: attempt.prepared,
83
+ request: attempt.request,
84
+ response: attempt.response,
63
85
  });
64
86
  }
65
87
 
66
- const refuse = (reason: string) =>
67
- Object.freeze({
68
- status: "not_dispatched",
69
- reason,
70
- operationalAuthority: false,
71
- });
72
88
  /** Internal single-use actor. Empty class registry precedes every Source side effect. */
73
89
  export function captureInitialAttach() {
74
90
  need(arguments.length === 0);
@@ -76,6 +92,20 @@ export function captureInitialAttach() {
76
92
  }
77
93
  export function captureOwnedInitialAttachV2() {
78
94
  need(arguments.length === 0);
95
+ return initialKernel(null);
96
+ }
97
+ const actors = new WeakMap<object, ReturnType<typeof initialKernel>>();
98
+ export function captureInitialAttachForLineage(token: object) {
99
+ need(arguments.length === 1);
100
+ readInitialLineage(token);
101
+ let actor = actors.get(token);
102
+ if (!actor) {
103
+ actor = initialKernel(token);
104
+ actors.set(token, actor);
105
+ }
106
+ return actor;
107
+ }
108
+ function initialKernel(token: object | null) {
79
109
  let consumed = false;
80
110
  return Object.freeze({
81
111
  async invoke() {
@@ -84,6 +114,32 @@ export function captureOwnedInitialAttachV2() {
84
114
  consumed = true;
85
115
  need(!types.isProxy(ENTRIES) && Array.isArray(ENTRIES));
86
116
  if (ENTRIES.length === 0) return refuse("no_qualified_native_class");
117
+
118
+ const OWNER =
119
+ token === null ? LEGACY_OWNER : ownerForInitialLineage(token);
120
+ const owned =
121
+ token === null ? legacyOwned : ownedForInitialLineage(token);
122
+ const classifyRetained =
123
+ token === null
124
+ ? classifyOwnedRetainedInitialAttachment
125
+ : (_raw: unknown) =>
126
+ createInitialMaterialClassifiers(token).retained();
127
+ const classifyFresh =
128
+ token === null
129
+ ? classifyOwnedInitialAttachment
130
+ : (raw: unknown) => {
131
+ const input = closed(raw, [
132
+ "attachRequest",
133
+ "originalBundle",
134
+ "currentBundle",
135
+ "observedAt",
136
+ ]);
137
+ return createInitialMaterialClassifiers(token).fresh(
138
+ input.currentBundle,
139
+ input.observedAt,
140
+ );
141
+ };
142
+ let attempt: AttemptCustody | undefined;
87
143
  let pair: Pair | null = null,
88
144
  failureControl: FailureControl | null = null,
89
145
  postEligible = false;
@@ -105,7 +161,10 @@ export function captureOwnedInitialAttachV2() {
105
161
  };
106
162
  try {
107
163
  OWNER.check();
108
- const live = loadFixedInitialOperation();
164
+ const live =
165
+ token === null
166
+ ? loadFixedInitialOperation()
167
+ : (await owned(() => operationForInitialLineage(token))).operation;
109
168
  OWNER.check();
110
169
  if (live === null) return refuse("original_slot_unbound");
111
170
  const op = closed(live, [
@@ -121,7 +180,10 @@ export function captureOwnedInitialAttachV2() {
121
180
  op.originAssociation !== null &&
122
181
  typeof op.originAssociation === "object" &&
123
182
  !types.isProxy(op.originAssociation) &&
124
- op.originAssociation === ORIGINAL_ORIGIN_ASSOCIATION,
183
+ op.originAssociation ===
184
+ (token === null
185
+ ? ORIGINAL_ORIGIN_ASSOCIATION
186
+ : readInitialLineage(token).originAssociation),
125
187
  );
126
188
  const takeOnce = sourceMethod(op.slot, "takeOnce");
127
189
  OWNER.check();
@@ -146,13 +208,15 @@ export function captureOwnedInitialAttachV2() {
146
208
  }),
147
209
  );
148
210
  OWNER.check();
149
- const retained = await owned(() =>
150
- classifyOwnedRetainedInitialAttachment(original),
151
- );
211
+ const retained = await owned(() => classifyRetained(original));
152
212
  OWNER.check();
153
213
  if (retained.status !== "classified")
154
214
  return refuse("retained_material_unsupported");
155
- const matches: { raw: unknown; fresh: SourceMethod }[] = [];
215
+ const matches: {
216
+ raw: unknown;
217
+ fresh: SourceMethod;
218
+ retained: SourceMethod;
219
+ }[] = [];
156
220
  for (const entry of ENTRIES) {
157
221
  OWNER.check();
158
222
  const retainedPredicate = sourceMethod(entry, "coversRetained"),
@@ -163,13 +227,20 @@ export function captureOwnedInitialAttachV2() {
163
227
  original.originalBundle,
164
228
  ]) === true
165
229
  )
166
- matches.push({ raw: entry, fresh: freshPredicate });
230
+ matches.push({
231
+ raw: entry,
232
+ fresh: freshPredicate,
233
+ retained: retainedPredicate,
234
+ });
167
235
  OWNER.check();
168
236
  }
169
237
  if (matches.length !== 1) return refuse("retained_class_not_unique");
170
238
  const entry = matches[0];
171
239
  OWNER.check();
172
- pair = createPair();
240
+ pair =
241
+ token === null
242
+ ? createPair()
243
+ : await owned(() => createInitialTransportForLineage(token));
173
244
  OWNER.check();
174
245
  need(
175
246
  pair.capability.assertSourceOperationBinding(
@@ -183,169 +254,23 @@ export function captureOwnedInitialAttachV2() {
183
254
 
184
255
  const activePair = pair;
185
256
  // Only this invocation's pair and actual locally rebuilt C2 enter this capsule.
186
- const control = (() => {
187
- const capability = activePair.capability,
188
- takeQuarantine = capability.takeFulfilledQuarantine;
189
- Object.freeze(request.payload);
190
- Object.freeze(request.requestIdentity);
191
- Object.freeze(request);
192
- OWNER.check();
193
- type Token = {
194
- invocation: object;
195
- phase: "read" | "post";
196
- ordinal: number;
197
- request: AttachRequest;
198
- diagnostics: Diagnostics;
199
- };
200
- const tokens = new WeakMap<object, Token>(),
201
- invocation = Object.freeze({});
202
- let phase = "unused";
203
- const tokenValue = (error: unknown) =>
204
- error !== null &&
205
- (typeof error === "object" || typeof error === "function")
206
- ? tokens.get(error)
207
- : undefined;
208
- function consume(error: unknown, kind: "read" | "post") {
209
- OWNER.check();
210
- const value = tokenValue(error);
211
- if (
212
- error !== null &&
213
- (typeof error === "object" || typeof error === "function")
214
- )
215
- tokens.delete(error);
216
- if (
217
- !value ||
218
- value.invocation !== invocation ||
219
- value.phase !== kind ||
220
- phase !== (kind === "read" ? "failed" : "failed_post")
221
- )
222
- return null;
223
- phase = kind === "read" ? "consumed" : "consumed_post";
224
- const failure = Object.freeze({
225
- category: "provider_rejected",
226
- phase: kind,
227
- ordinal: value.ordinal,
228
- request: value.request,
229
- diagnostics: value.diagnostics,
230
- outcome: "unknown",
231
- operationalAuthority: false,
232
- });
233
- const out = Error("INITIAL_PROVIDER_REJECTED");
234
- Object.defineProperty(out, "failure", {
235
- value: failure,
236
- enumerable: false,
237
- writable: false,
238
- configurable: false,
239
- });
240
- return out;
241
- }
242
- return Object.freeze({
243
- takePostQuarantine(...args: unknown[]) {
244
- if (args.length !== 0) return null;
245
- return Reflect.apply(takeQuarantine, capability, []) as
246
- object | null;
247
- },
248
- begin() {
249
- OWNER.check();
250
- if (phase !== "unused") stop();
251
- if (capability.assertVirginAcquisition() !== true) {
252
- phase = "closed";
253
- stop();
254
- }
255
- OWNER.check();
256
- phase = "acquiring";
257
- },
258
- complete() {
259
- OWNER.check();
260
- if (phase !== "acquiring") stop();
261
- phase = "complete";
262
- },
263
- takeReadFailure() {
264
- OWNER.check();
265
- if (phase !== "acquiring") stop();
266
- const record = capability.consumeReadCatch();
267
- OWNER.check();
268
- if (record === null) {
269
- phase = "unsupported";
270
- return null;
271
- }
272
- need(
273
- record.phase === "read" &&
274
- Number.isSafeInteger(record.ordinal) &&
275
- record.ordinal >= 1 &&
276
- record.ordinal <= 43,
277
- );
278
- const token = Object.freeze(Object.create(null)) as object;
279
- tokens.set(
280
- token,
281
- Object.freeze({
282
- invocation,
283
- phase: "read",
284
- ordinal: record.ordinal,
285
- request,
286
- diagnostics: record.diagnostics,
287
- }),
288
- );
289
- phase = "failed";
290
- return token;
291
- },
292
- beginPost() {
293
- OWNER.check();
294
- if (phase !== "complete") stop();
295
- phase = "posting";
296
- },
297
- takePostFailure(actualReply: unknown) {
298
- OWNER.check();
299
- if (phase !== "posting") stop();
300
- const record = capability.consumePostCatch(actualReply);
301
- OWNER.check();
302
- if (record === null) {
303
- phase = "post_without_token";
304
- return null;
305
- }
306
- need(
307
- record.phase === "post" &&
308
- Number.isSafeInteger(record.ordinal) &&
309
- record.ordinal >= 1 &&
310
- record.ordinal <= 44,
311
- );
312
- const token = Object.freeze(Object.create(null)) as object;
313
- tokens.set(
314
- token,
315
- Object.freeze({
316
- invocation,
317
- phase: "post",
318
- ordinal: record.ordinal,
319
- request,
320
- diagnostics: record.diagnostics,
321
- }),
322
- );
323
- phase = "failed_post";
324
- return token;
325
- },
326
- forward(error: unknown) {
327
- OWNER.check();
328
- const value = tokenValue(error);
329
- return value &&
330
- value.invocation === invocation &&
331
- (phase === "failed" || phase === "failed_post")
332
- ? (error as object)
333
- : null;
334
- },
335
- consume(error: unknown) {
336
- return consume(error, "read");
337
- },
338
- consumePost(error: unknown) {
339
- return consume(error, "post");
340
- },
341
- });
342
- })();
257
+ const control = captureActorFailureControl(OWNER, activePair, request);
343
258
  OWNER.check();
344
259
  failureControl = control;
345
- const reader = bindRawAcquisitionFromSource(
346
- OWNER,
347
- activePair.transport.readTransport,
348
- );
260
+ const reader =
261
+ token === null
262
+ ? bindRawAcquisitionFromSource(
263
+ OWNER,
264
+ activePair.transport.readTransport,
265
+ )
266
+ : {
267
+ async acquire(_expected: unknown) {
268
+ const reader = await owned(() =>
269
+ acquisitionForInitialLineage(token),
270
+ );
271
+ return reader.acquire();
272
+ },
273
+ };
349
274
  OWNER.check();
350
275
  let acquiredOnce = false;
351
276
  const acquire = async () => {
@@ -370,7 +295,7 @@ export function captureOwnedInitialAttachV2() {
370
295
  if (!acquired.ok) return refuse("current_read_unsupported");
371
296
 
372
297
  const fresh = await owned(() =>
373
- classifyOwnedInitialAttachment({
298
+ classifyFresh({
374
299
  attachRequest: raw,
375
300
  originalBundle: original.originalBundle,
376
301
  currentBundle: acquired.bundle,
@@ -441,16 +366,41 @@ export function captureOwnedInitialAttachV2() {
441
366
  OWNER.check();
442
367
  return Reflect.apply(send, op.sink, [record, ack]);
443
368
  };
444
- const compiler = createAttachEventCompiler(raw, {
445
- send: sourceSend,
446
- timeoutMs: 5000,
447
- });
369
+ const compiler =
370
+ token === null
371
+ ? createAttachEventCompiler(raw, {
372
+ send: sourceSend,
373
+ timeoutMs: 5000,
374
+ })
375
+ : createInitialAttachEventCompiler(token);
448
376
  OWNER.check();
449
377
  await owned(() =>
450
378
  compiler.dispatch({ event: "dispatch", stage: "attach", request }),
451
379
  );
452
380
  OWNER.check();
453
381
  postEligible = true;
382
+ if (token !== null) {
383
+ attempt = {
384
+ entry: entry.raw,
385
+ retainedPredicate: entry.retained,
386
+ freshPredicate: entry.fresh,
387
+ originalRecord: {
388
+ originalRaw: original,
389
+ originalPrepared: prepared,
390
+ completeRequest: request,
391
+ retainedMaterial: retained.material,
392
+ },
393
+ raw,
394
+ prepared,
395
+ request,
396
+ pair: activePair,
397
+ response: null,
398
+ settled: false,
399
+ returnedACK: false,
400
+ reserved: false,
401
+ };
402
+ attempts.set(token, attempt);
403
+ }
454
404
  let postedOnce = false;
455
405
  const post = async () => {
456
406
  OWNER.check();
@@ -471,6 +421,8 @@ export function captureOwnedInitialAttachV2() {
471
421
  if (reply.outcome === "provider_rejected") stop();
472
422
  if (reply.outcome !== "fulfilled_safe") return uncertain();
473
423
  const body = reply.body;
424
+ if (attempt)
425
+ attempt.response = Object.freeze({ body, status: reply.statusCode });
474
426
  await owned(() =>
475
427
  compiler.response({
476
428
  event: "response",
@@ -489,6 +441,10 @@ export function captureOwnedInitialAttachV2() {
489
441
  }),
490
442
  );
491
443
  OWNER.check();
444
+ if (token !== null) {
445
+ await owned(() => retainInitialReceiptForLineage(token, receipt));
446
+ OWNER.check();
447
+ }
492
448
  await owned(() =>
493
449
  compiler.receipt({
494
450
  event: "receipt",
@@ -501,6 +457,7 @@ export function captureOwnedInitialAttachV2() {
501
457
  const result = admitInitialOutput(receipt, prepared, request);
502
458
  OWNER.check();
503
459
  control.takePostQuarantine();
460
+ if (attempt) attempt.returnedACK = true;
504
461
  return result;
505
462
  } catch (error) {
506
463
  let live = false;
@@ -529,6 +486,8 @@ export function captureOwnedInitialAttachV2() {
529
486
  if (state === "NOT_STARTED") return refuse("post_not_started");
530
487
  }
531
488
  return stop();
489
+ } finally {
490
+ if (attempt) attempt.settled = true;
532
491
  }
533
492
  },
534
493
  });
@@ -1,4 +1,5 @@
1
1
  import { OWNER } from "../originalOwner.js";
2
+ import { ownerForInitialLineage } from "../producer/ownerFactory.js";
2
3
 
3
4
  export interface FailureForwarder {
4
5
  forward(error: unknown): object | null;
@@ -8,7 +9,24 @@ export function owned<T>(
8
9
  work: () => T | PromiseLike<T>,
9
10
  failureControl: FailureForwarder | null = null,
10
11
  ): Promise<T> {
11
- OWNER.check();
12
+ return runOwned(OWNER, work, failureControl);
13
+ }
14
+ export function ownedForInitialLineage(token: object) {
15
+ if (arguments.length !== 1) throw Error("INITIAL_ACTOR_STOP");
16
+ const owner = ownerForInitialLineage(token);
17
+ return function owned<T>(
18
+ work: () => T | PromiseLike<T>,
19
+ control: FailureForwarder | null = null,
20
+ ): Promise<T> {
21
+ return runOwned(owner, work, control);
22
+ };
23
+ }
24
+ function runOwned<T>(
25
+ owner: Pick<typeof OWNER, "check" | "subscribe">,
26
+ work: () => T | PromiseLike<T>,
27
+ failureControl: FailureForwarder | null,
28
+ ): Promise<T> {
29
+ owner.check();
12
30
  return new Promise((resolve, reject) => {
13
31
  let done = false,
14
32
  remove: (() => void) | null = null;
@@ -20,14 +38,14 @@ export function owned<T>(
20
38
  else reject(value);
21
39
  };
22
40
  const retire = () => finish(false, Error("INITIAL_ACTOR_STOP"));
23
- remove = OWNER.subscribe(retire);
41
+ remove = owner.subscribe(retire);
24
42
  if (done) {
25
43
  remove();
26
44
  return;
27
45
  }
28
46
  let pending: T | PromiseLike<T>;
29
47
  try {
30
- OWNER.check();
48
+ owner.check();
31
49
  pending = work();
32
50
  } catch {
33
51
  finish(false, Error("INITIAL_ACTOR_STOP"));
@@ -37,7 +55,7 @@ export function owned<T>(
37
55
  (value) => {
38
56
  if (done) return;
39
57
  try {
40
- OWNER.check();
58
+ owner.check();
41
59
  finish(true, value);
42
60
  } catch {
43
61
  retire();
@@ -46,7 +64,7 @@ export function owned<T>(
46
64
  (error: unknown) => {
47
65
  if (done) return;
48
66
  try {
49
- OWNER.check();
67
+ owner.check();
50
68
  const forwarded = failureControl
51
69
  ? failureControl.forward(error)
52
70
  : null;
@@ -0,0 +1,60 @@
1
+ import { types } from "node:util";
2
+ import { OWNER } from "../originalOwner.js";
3
+ import { ownerForInitialLineage } from "../producer/ownerFactory.js";
4
+ import {
5
+ closed,
6
+ captureRequestAndReads,
7
+ need,
8
+ type SourceMethod,
9
+ } from "./types.js";
10
+
11
+ function sourceFunction(
12
+ service: unknown,
13
+ name: string,
14
+ ): { receiver: object; method: SourceMethod } {
15
+ need(service && typeof service === "object" && !types.isProxy(service));
16
+ const d = Object.getOwnPropertyDescriptor(service, name);
17
+ need(d && "value" in d && typeof d.value === "function");
18
+ // The function is selected once from validated own data, preserving its receiver.
19
+ return { receiver: service, method: d.value as SourceMethod };
20
+ }
21
+
22
+ /** Internal fixed attach profile: 43 reads and one POST, never a 44-GET profile. */
23
+ function captureSource(raw: unknown, expectedOwner: typeof OWNER) {
24
+ const b = closed(raw, ["owner", "request", "readPlan", "delegate"]);
25
+ need(b.owner === expectedOwner);
26
+ const owner = sourceFunction(b.owner, "check"),
27
+ delegate = sourceFunction(b.delegate, "rawRequest");
28
+ const { request, plan } = captureRequestAndReads(b.request, b.readPlan);
29
+ return Object.freeze({
30
+ owner: owner.receiver,
31
+ check: owner.method,
32
+ delegate: delegate.receiver,
33
+ send: delegate.method,
34
+ request,
35
+ plan: Object.freeze(plan),
36
+ });
37
+ }
38
+
39
+ export function captureLegacyTransportSource(raw: unknown) {
40
+ return captureSource(raw, OWNER);
41
+ }
42
+ export function captureInitialTransportSource(token: object, raw: unknown) {
43
+ return captureSource(raw, ownerForInitialLineage(token));
44
+ }
45
+
46
+ export function checkTransportOwnerResult(value: unknown) {
47
+ const x = closed(value, ["wallMs", "ns", "nowSeconds", "retired"]);
48
+ need(
49
+ typeof x.wallMs === "number" &&
50
+ Number.isSafeInteger(x.wallMs) &&
51
+ x.wallMs >= 0 &&
52
+ typeof x.ns === "bigint" &&
53
+ x.ns >= 0n &&
54
+ x.ns <= 18446744073709551615n &&
55
+ typeof x.nowSeconds === "number" &&
56
+ Number.isSafeInteger(x.nowSeconds) &&
57
+ x.nowSeconds === Math.floor(x.wallMs / 1000) &&
58
+ x.retired === false,
59
+ );
60
+ }