@fantastic.dev/better-stripe 0.110.0 → 0.112.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 (134) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/observation/freshActor.d.ts +564 -0
  4. package/dist/client/billing/renewalAttach/internal/observation/freshActor.d.ts.map +1 -1
  5. package/dist/client/billing/renewalAttach/internal/observation/freshActor.js +103 -21
  6. package/dist/client/billing/renewalAttach/internal/observation/freshActor.js.map +1 -1
  7. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.d.ts +18 -1
  8. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.d.ts.map +1 -1
  9. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.js +54 -1
  10. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.js.map +1 -1
  11. package/dist/client/billing/renewalAttach/internal/observation/invocation.d.ts +4 -0
  12. package/dist/client/billing/renewalAttach/internal/observation/invocation.d.ts.map +1 -1
  13. package/dist/client/billing/renewalAttach/internal/observation/invocation.js +1 -0
  14. package/dist/client/billing/renewalAttach/internal/observation/invocation.js.map +1 -1
  15. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts +4 -0
  16. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts.map +1 -1
  17. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js +6 -1
  18. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js.map +1 -1
  19. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.d.ts +38 -0
  20. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.d.ts.map +1 -0
  21. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.js +172 -0
  22. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.js.map +1 -0
  23. package/dist/client/billing/renewalAttach/internal/observation/witnessData.d.ts +135 -0
  24. package/dist/client/billing/renewalAttach/internal/observation/witnessData.d.ts.map +1 -0
  25. package/dist/client/billing/renewalAttach/internal/observation/witnessData.js +143 -0
  26. package/dist/client/billing/renewalAttach/internal/observation/witnessData.js.map +1 -0
  27. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.d.ts +85 -0
  28. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.d.ts.map +1 -0
  29. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.js +121 -0
  30. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.js.map +1 -0
  31. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.d.ts +44 -0
  32. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.d.ts.map +1 -0
  33. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.js +4 -0
  34. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.js.map +1 -0
  35. package/dist/client/billing/renewalAttach/internal/producer/configureJournalWork.d.ts +1 -1
  36. package/dist/client/billing/renewalAttach/internal/producer/configureLineage.d.ts +21 -21
  37. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.d.ts +3 -3
  38. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.d.ts +1 -1
  39. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +84 -32
  40. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  41. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +26 -0
  42. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  43. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +84 -32
  44. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  45. package/dist/client/connect/payoutSyncTestHelpers.d.ts +26 -0
  46. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  47. package/dist/component/_generated/api.d.ts +24 -0
  48. package/dist/component/_generated/api.d.ts.map +1 -1
  49. package/dist/component/_generated/api.js.map +1 -1
  50. package/dist/component/_generated/component.d.ts +94 -10
  51. package/dist/component/_generated/component.d.ts.map +1 -1
  52. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  53. package/dist/component/balanceFunding/mutations.d.ts +16 -16
  54. package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
  55. package/dist/component/balanceFunding/queries.d.ts +1 -1
  56. package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
  57. package/dist/component/earnings/triggers.d.ts +44 -0
  58. package/dist/component/earnings/triggers.d.ts.map +1 -1
  59. package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
  60. package/dist/component/renewalWitnesses/eligibility.d.ts +36 -0
  61. package/dist/component/renewalWitnesses/eligibility.d.ts.map +1 -0
  62. package/dist/component/renewalWitnesses/eligibility.js +60 -0
  63. package/dist/component/renewalWitnesses/eligibility.js.map +1 -0
  64. package/dist/component/renewalWitnesses/mutations.d.ts +45 -0
  65. package/dist/component/renewalWitnesses/mutations.d.ts.map +1 -0
  66. package/dist/component/renewalWitnesses/mutations.js +14 -0
  67. package/dist/component/renewalWitnesses/mutations.js.map +1 -0
  68. package/dist/component/renewalWitnesses/protocol.d.ts +75 -0
  69. package/dist/component/renewalWitnesses/protocol.d.ts.map +1 -0
  70. package/dist/component/renewalWitnesses/protocol.js +82 -0
  71. package/dist/component/renewalWitnesses/protocol.js.map +1 -0
  72. package/dist/component/renewalWitnesses/queries.d.ts +39 -0
  73. package/dist/component/renewalWitnesses/queries.d.ts.map +1 -0
  74. package/dist/component/renewalWitnesses/queries.js +11 -0
  75. package/dist/component/renewalWitnesses/queries.js.map +1 -0
  76. package/dist/component/renewalWitnesses/schema.d.ts +99 -0
  77. package/dist/component/renewalWitnesses/schema.d.ts.map +1 -0
  78. package/dist/component/renewalWitnesses/schema.js +36 -0
  79. package/dist/component/renewalWitnesses/schema.js.map +1 -0
  80. package/dist/component/renewalWitnesses/source.d.ts +22 -0
  81. package/dist/component/renewalWitnesses/source.d.ts.map +1 -0
  82. package/dist/component/renewalWitnesses/source.js +108 -0
  83. package/dist/component/renewalWitnesses/source.js.map +1 -0
  84. package/dist/component/schema.d.ts +42 -16
  85. package/dist/component/schema.js +2 -0
  86. package/dist/component/schema.js.map +1 -1
  87. package/dist/component/usage/allowance.fixtures.d.ts +42 -16
  88. package/dist/component/usage/allowance.fixtures.d.ts.map +1 -1
  89. package/dist/component/usageCredits/accounting.fixtures.d.ts +26 -0
  90. package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -1
  91. package/dist/shared/renewalWitness.d.ts +51 -0
  92. package/dist/shared/renewalWitness.d.ts.map +1 -0
  93. package/dist/shared/renewalWitness.js +127 -0
  94. package/dist/shared/renewalWitness.js.map +1 -0
  95. package/docs/renewal-witness-protocol.md +139 -0
  96. package/package.json +1 -1
  97. package/src/client/billing/renewalAttach/internal/observation/freshActor.ts +142 -19
  98. package/src/client/billing/renewalAttach/internal/observation/freshTransport.ts +90 -1
  99. package/src/client/billing/renewalAttach/internal/observation/invocation.ts +2 -0
  100. package/src/client/billing/renewalAttach/internal/observation/readerAuthorization.ts +6 -1
  101. package/src/client/billing/renewalAttach/internal/observation/witnessAcquisition.integration.test.ts +348 -0
  102. package/src/client/billing/renewalAttach/internal/observation/witnessActor.integration.test.ts +204 -0
  103. package/src/client/billing/renewalAttach/internal/observation/witnessAuthorization.ts +239 -0
  104. package/src/client/billing/renewalAttach/internal/observation/witnessCapabilities.integration.test.ts +231 -0
  105. package/src/client/billing/renewalAttach/internal/observation/witnessData.ts +189 -0
  106. package/src/client/billing/renewalAttach/internal/observation/witnessHistorical.integration.test.ts +98 -0
  107. package/src/client/billing/renewalAttach/internal/observation/witnessImmutable.integration.test.ts +78 -0
  108. package/src/client/billing/renewalAttach/internal/observation/witnessInvoice.integration.test.ts +85 -0
  109. package/src/client/billing/renewalAttach/internal/observation/witnessLifecycle.integration.test.ts +264 -0
  110. package/src/client/billing/renewalAttach/internal/observation/witnessOperation.ts +205 -0
  111. package/src/client/billing/renewalAttach/internal/observation/witnessProfile.integration.test.ts +93 -0
  112. package/src/client/billing/renewalAttach/internal/observation/witnessRPCControls.integration.test.ts +256 -0
  113. package/src/client/billing/renewalAttach/internal/operations/witnessProfiles.ts +58 -0
  114. package/src/client/billing/renewalAttach/internal/test-fixtures/invoiceObservation.ts +3 -1
  115. package/src/client/billing/renewalAttach/internal/test-fixtures/producerConfigureAdmission.ts +8 -0
  116. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +18 -7
  117. package/src/client/billing/renewalAttach/internal/test-fixtures/witnessHistory.ts +70 -0
  118. package/src/client/billing/renewalAttach/internal/test-fixtures/witnessObservation.ts +353 -0
  119. package/src/component/_generated/api.ts +24 -0
  120. package/src/component/_generated/component.ts +98 -10
  121. package/src/component/renewalOperations/configureJournal.fixtures.ts +25 -3
  122. package/src/component/renewalWitnesses/bounds.test.ts +204 -0
  123. package/src/component/renewalWitnesses/codec.test.ts +84 -0
  124. package/src/component/renewalWitnesses/controls.test.ts +250 -0
  125. package/src/component/renewalWitnesses/eligibility.ts +78 -0
  126. package/src/component/renewalWitnesses/mutations.ts +17 -0
  127. package/src/component/renewalWitnesses/protocol.test.ts +63 -0
  128. package/src/component/renewalWitnesses/protocol.ts +103 -0
  129. package/src/component/renewalWitnesses/queries.ts +11 -0
  130. package/src/component/renewalWitnesses/routes.test.ts +154 -0
  131. package/src/component/renewalWitnesses/schema.ts +36 -0
  132. package/src/component/renewalWitnesses/source.ts +157 -0
  133. package/src/component/schema.ts +2 -0
  134. package/src/shared/renewalWitness.ts +179 -0
@@ -0,0 +1,264 @@
1
+ import {
2
+ witnessControls,
3
+ prepareWitnessObservation,
4
+ invokeWitnessCapture,
5
+ invokeWitnessRead,
6
+ readStoredWitness,
7
+ witnessFixtureOptions,
8
+ witnessActorInput,
9
+ } from "../test-fixtures/witnessObservation.js";
10
+ import {
11
+ configureAdmissionCase,
12
+ admissionControls,
13
+ } from "../test-fixtures/producerConfigureAdmission.js";
14
+ import { captureFreshWitnessRenewalObservation } from "./freshActor.js";
15
+ import { getFunctionAddress } from "convex/server";
16
+ import { expect, vi } from "vitest";
17
+ import assert from "node:assert/strict";
18
+
19
+ function latch() {
20
+ let release!: () => void;
21
+ const promise = new Promise<void>((resolve) => {
22
+ release = resolve;
23
+ });
24
+ return { promise, release };
25
+ }
26
+ const isRetain = (ref: Parameters<typeof getFunctionAddress>[0]) =>
27
+ getFunctionAddress(ref).reference?.endsWith("/retainAuthorizedWitness");
28
+ const isRead = (ref: Parameters<typeof getFunctionAddress>[0]) =>
29
+ getFunctionAddress(ref).reference?.endsWith("/readAuthorizedWitness");
30
+
31
+ for (const phase of ["reply", "readback"] as const)
32
+ configureAdmissionCase(
33
+ "an unresolved original " + phase + " cannot release retention DATA",
34
+ prepareWitnessObservation,
35
+ async (h, state) => {
36
+ const entered = latch(),
37
+ resume = latch();
38
+ let settled = false,
39
+ readbacks = 0;
40
+ const pending = invokeWitnessCapture(h, state, {
41
+ before(ctx) {
42
+ const mutate = ctx.runMutation,
43
+ query = ctx.runQuery;
44
+ Object.defineProperty(ctx, "runMutation", {
45
+ value: async (...args: Parameters<typeof mutate>) => {
46
+ const result = await mutate.apply(ctx, args);
47
+ if (phase === "reply" && isRetain(args[0])) {
48
+ entered.release();
49
+ await resume.promise;
50
+ }
51
+ return result;
52
+ },
53
+ });
54
+ Object.defineProperty(ctx, "runQuery", {
55
+ value: async (...args: Parameters<typeof query>) => {
56
+ const result = await query.apply(ctx, args);
57
+ if (isRead(args[0])) {
58
+ readbacks++;
59
+ if (phase === "readback") {
60
+ entered.release();
61
+ await resume.promise;
62
+ }
63
+ }
64
+ return result;
65
+ },
66
+ });
67
+ },
68
+ });
69
+ const observed = pending.then((value) => {
70
+ settled = true;
71
+ return value;
72
+ });
73
+ try {
74
+ await entered.promise;
75
+ expect(await readStoredWitness(h, state)).not.toBeNull();
76
+ await vi.advanceTimersByTimeAsync(0);
77
+ expect(settled).toBe(false);
78
+ expect(readbacks).toBe(phase === "reply" ? 0 : 1);
79
+ expect(witnessControls.retainQueries).toBe(1);
80
+ } finally {
81
+ resume.release();
82
+ }
83
+ const output = await observed;
84
+ assert("witnessRetention" in output);
85
+ expect(output.witnessRetention?.status).toBe("retained");
86
+ expect(witnessControls.retainQueries).toBe(2);
87
+ },
88
+ witnessFixtureOptions,
89
+ );
90
+
91
+ for (const phase of ["reply", "readback"] as const)
92
+ configureAdmissionCase(
93
+ "expiry during original " +
94
+ phase +
95
+ " leaves durable storage without returned ACK",
96
+ prepareWitnessObservation,
97
+ async (h, state) => {
98
+ const entered = latch(),
99
+ resume = latch();
100
+ let returned = false;
101
+ const pending = invokeWitnessCapture(h, state, {
102
+ before(ctx) {
103
+ if (phase === "reply") {
104
+ const original = ctx.runMutation;
105
+ Object.defineProperty(ctx, "runMutation", {
106
+ value: async (...args: Parameters<typeof original>) => {
107
+ const result = await original.apply(ctx, args);
108
+ if (isRetain(args[0])) {
109
+ entered.release();
110
+ await resume.promise;
111
+ }
112
+ return result;
113
+ },
114
+ });
115
+ } else {
116
+ const original = ctx.runQuery;
117
+ Object.defineProperty(ctx, "runQuery", {
118
+ value: async (...args: Parameters<typeof original>) => {
119
+ const result = await original.apply(ctx, args);
120
+ if (isRead(args[0])) {
121
+ entered.release();
122
+ await resume.promise;
123
+ }
124
+ return result;
125
+ },
126
+ });
127
+ }
128
+ },
129
+ });
130
+ const outcome = pending.then(
131
+ () => {
132
+ returned = true;
133
+ return null;
134
+ },
135
+ (error: unknown) => error,
136
+ );
137
+ try {
138
+ await entered.promise;
139
+ expect(await readStoredWitness(h, state)).not.toBeNull();
140
+ await vi.advanceTimersByTimeAsync(30000);
141
+ expect(await outcome).toEqual(
142
+ Error("OBSERVATION_HISTORY_READER_EXPIRED"),
143
+ );
144
+ } finally {
145
+ resume.release();
146
+ }
147
+ await vi.advanceTimersByTimeAsync(0);
148
+ expect(returned).toBe(false);
149
+ expect(witnessControls.retainQueries).toBe(1);
150
+ expect(await readStoredWitness(h, state)).not.toBeNull();
151
+ },
152
+ witnessFixtureOptions,
153
+ );
154
+
155
+ for (const change of [
156
+ "lost-reply",
157
+ "retention-revoked",
158
+ "profile-removed",
159
+ "query-address",
160
+ "selected-sdk-drift",
161
+ ] as const)
162
+ configureAdmissionCase(
163
+ "committed retention produces no ACK after " + change,
164
+ prepareWitnessObservation,
165
+ async (h, state) => {
166
+ let writes = 0;
167
+ const selected: { client: object | null } = { client: null };
168
+ const oldAddress = Reflect.get(
169
+ state.captureProfile.authorizationQuery,
170
+ Symbol.for("functionName"),
171
+ );
172
+ admissionControls.onSelect = (client) => {
173
+ selected.client = client;
174
+ };
175
+ try {
176
+ await expect(
177
+ invokeWitnessCapture(h, state, {
178
+ before(ctx) {
179
+ const original = ctx.runMutation;
180
+ Object.defineProperty(ctx, "runMutation", {
181
+ value: async (...args: Parameters<typeof original>) => {
182
+ const result = await original.apply(ctx, args);
183
+ if (isRetain(args[0])) {
184
+ writes++;
185
+ if (change === "lost-reply")
186
+ throw Error("CONTROLLED_LOST_RETENTION_REPLY");
187
+ if (change === "retention-revoked")
188
+ witnessControls.denyRetain = true;
189
+ if (change === "profile-removed")
190
+ witnessControls.captures.splice(0);
191
+ if (change === "query-address")
192
+ Reflect.set(
193
+ state.captureProfile.authorizationQuery,
194
+ Symbol.for("functionName"),
195
+ "initialOwner:authorizeWitnessRead",
196
+ );
197
+ if (change === "selected-sdk-drift") {
198
+ assert(selected.client);
199
+ Object.defineProperty(selected.client, "rawRequest", {
200
+ configurable: true,
201
+ value() {
202
+ throw Error("UNEXPECTED_DRIFTED_SDK_CALL");
203
+ },
204
+ });
205
+ }
206
+ }
207
+ return result;
208
+ },
209
+ });
210
+ },
211
+ }),
212
+ ).rejects.toThrow();
213
+ } finally {
214
+ if (selected.client && change === "selected-sdk-drift")
215
+ Reflect.deleteProperty(selected.client, "rawRequest");
216
+ Reflect.set(
217
+ state.captureProfile.authorizationQuery,
218
+ Symbol.for("functionName"),
219
+ oldAddress,
220
+ );
221
+ }
222
+ expect(writes).toBe(1);
223
+ expect(await readStoredWitness(h, state)).not.toBeNull();
224
+ },
225
+ witnessFixtureOptions,
226
+ );
227
+
228
+ configureAdmissionCase(
229
+ "an idle captured factory expires within its original 30 seconds",
230
+ prepareWitnessObservation,
231
+ async (h, state) => {
232
+ const before = h.calls.length;
233
+ await h.inAnotherAction(async (ctx) => {
234
+ const actor = await captureFreshWitnessRenewalObservation(
235
+ h.sdk,
236
+ witnessActorInput(state, ctx),
237
+ );
238
+ await vi.advanceTimersByTimeAsync(30000);
239
+ await expect(actor.invoke()).rejects.toThrow(
240
+ "OBSERVATION_HISTORY_READER_EXPIRED",
241
+ );
242
+ await expect(actor.invoke()).rejects.toThrow();
243
+ });
244
+ expect(h.calls).toHaveLength(before);
245
+ expect(await readStoredWitness(h, state)).toBeNull();
246
+ },
247
+ witnessFixtureOptions,
248
+ );
249
+
250
+ configureAdmissionCase(
251
+ "revocation of the distinct read capability before release cannot return historical DATA",
252
+ prepareWitnessObservation,
253
+ async (h, state) => {
254
+ await invokeWitnessCapture(h, state);
255
+ const stored = await readStoredWitness(h, state);
256
+ witnessControls.onReadGrant = (_grant, count) => {
257
+ if (count === 1) witnessControls.denyRead = true;
258
+ };
259
+ await expect(invokeWitnessRead(h, state)).rejects.toThrow();
260
+ expect(witnessControls.readQueries).toBe(2);
261
+ expect(await readStoredWitness(h, state)).toEqual(stored);
262
+ },
263
+ witnessFixtureOptions,
264
+ );
@@ -0,0 +1,205 @@
1
+ import { closed, freeze } from "../c10/ownData.js";
2
+ import { matchConfigureDATA } from "../producer/configureJournalData.js";
3
+ import { readCheckedJournalActionRPC } from "../producer/classOwnedActionRoot.js";
4
+ import { readObservationInvocation } from "./invocation.js";
5
+ import {
6
+ readFreshObservationTransport,
7
+ consumeFreshObservationAcquisition,
8
+ } from "./freshTransport.js";
9
+ import {
10
+ captureReaderQueryDATA,
11
+ readerText as text,
12
+ } from "./readerAuthorization.js";
13
+ import {
14
+ createWitnessAuthorization,
15
+ witnessRPC,
16
+ witnessAddress,
17
+ witnessProfileGuard,
18
+ type WitnessMode,
19
+ type WitnessProfile,
20
+ } from "./witnessAuthorization.js";
21
+ import {
22
+ captureWitnessPayload,
23
+ inspectWitnessPayload,
24
+ checkedWitnessDescriptor,
25
+ checkWitnessC7,
26
+ type InspectedWitness,
27
+ } from "./witnessData.js";
28
+ import { inspectHistoricalMaterialEvidenceV1 } from "../../../renewalObservation/evidence-material.js";
29
+ import {
30
+ witnessNeed as need,
31
+ type WitnessIdentity,
32
+ } from "../../../../../shared/renewalWitness.js";
33
+
34
+ /** Actor-private protocol. Caller DATA cannot register a token, grant or returned ACK. */
35
+ export function createWitnessOperation(
36
+ token: object,
37
+ profile: WitnessProfile,
38
+ mode: WitnessMode,
39
+ identity: WitnessIdentity,
40
+ ) {
41
+ need(arguments.length === 4);
42
+ const state = readObservationInvocation(token),
43
+ rpc = readCheckedJournalActionRPC(state.binding);
44
+ const transport = readFreshObservationTransport(
45
+ token,
46
+ profile.freshProfile,
47
+ profile.invoiceProfile,
48
+ );
49
+ const refs = rpc.component.renewalWitnesses;
50
+ const retain = refs.mutations.retainAuthorizedWitness,
51
+ read = refs.queries.readAuthorizedWitness;
52
+ const addresses = [witnessAddress(retain), witnessAddress(read)];
53
+ let authorization: ReturnType<typeof createWitnessAuthorization> | null =
54
+ null;
55
+ let stored: InspectedWitness | null = null;
56
+ let descriptor: ReturnType<typeof checkedWitnessDescriptor> | null = null;
57
+ let used = false;
58
+ function guard() {
59
+ state.guard();
60
+ transport.guard();
61
+ witnessProfileGuard(profile, mode);
62
+ need(
63
+ readCheckedJournalActionRPC(state.binding) === rpc &&
64
+ profile.freshProfile.historyEntry === state.historyEntry,
65
+ );
66
+ const current = rpc.component.renewalWitnesses;
67
+ need(
68
+ witnessAddress(current.mutations.retainAuthorizedWitness) ===
69
+ addresses[0] &&
70
+ witnessAddress(current.queries.readAuthorizedWitness) === addresses[1],
71
+ );
72
+ authorization?.guard();
73
+ }
74
+ async function fullRead() {
75
+ const raw: unknown = await witnessRPC(
76
+ state,
77
+ () => rpc.runQuery.call(rpc.receiver, read, { identity }),
78
+ guard,
79
+ );
80
+ const reply = closed(captureReaderQueryDATA(raw), [
81
+ "descriptor",
82
+ "payloadJSON",
83
+ ]);
84
+ return {
85
+ descriptor: reply.descriptor,
86
+ payloadJSON: text(reply.payloadJSON, 500000),
87
+ };
88
+ }
89
+ return Object.freeze({
90
+ guard,
91
+ async retain() {
92
+ need(arguments.length === 0 && mode === "retain" && !used);
93
+ used = true;
94
+ guard();
95
+ const { reads, invoiceContext } = consumeFreshObservationAcquisition(
96
+ token,
97
+ profile.freshProfile,
98
+ profile.invoiceProfile,
99
+ );
100
+ const payloadJSON = captureWitnessPayload(
101
+ state,
102
+ identity,
103
+ reads,
104
+ invoiceContext,
105
+ );
106
+ const admitted = await state.owned(
107
+ () => inspectWitnessPayload(payloadJSON),
108
+ guard,
109
+ );
110
+ checkWitnessC7(admitted.payload, state);
111
+ authorization = createWitnessAuthorization(
112
+ state,
113
+ profile,
114
+ mode,
115
+ identity,
116
+ admitted.scope,
117
+ );
118
+ await authorization.authorize();
119
+ guard();
120
+ const raw: unknown = await witnessRPC(
121
+ state,
122
+ () =>
123
+ rpc.runMutation.call(rpc.receiver, retain, { identity, payloadJSON }),
124
+ guard,
125
+ );
126
+ const reply = captureReaderQueryDATA(raw);
127
+ if (
128
+ reply &&
129
+ typeof reply === "object" &&
130
+ Reflect.get(reply, "status") === "conflict"
131
+ ) {
132
+ matchConfigureDATA(reply, {
133
+ status: "conflict",
134
+ reason: "identity_changed",
135
+ });
136
+ return freeze({
137
+ status: "conflict" as const,
138
+ reason: "identity_changed" as const,
139
+ });
140
+ }
141
+ const result = closed(reply, ["status", "descriptor"]);
142
+ need(result.status === "retained");
143
+ const retained = checkedWitnessDescriptor(result.descriptor, admitted);
144
+ const readback = await fullRead();
145
+ need(readback.payloadJSON === payloadJSON);
146
+ matchConfigureDATA(
147
+ checkedWitnessDescriptor(readback.descriptor, admitted),
148
+ retained,
149
+ );
150
+ guard();
151
+ return freeze({ status: "retained" as const, descriptor: retained });
152
+ },
153
+ async load() {
154
+ need(mode === "read" && !used);
155
+ used = true;
156
+ guard();
157
+ authorization = createWitnessAuthorization(
158
+ state,
159
+ profile,
160
+ mode,
161
+ identity,
162
+ null,
163
+ );
164
+ const grant = await authorization.authorize();
165
+ guard();
166
+ const reply = await fullRead();
167
+ const admitted = await state.owned(
168
+ () => inspectWitnessPayload(reply.payloadJSON),
169
+ guard,
170
+ );
171
+ matchConfigureDATA(admitted.payload.identity, identity);
172
+ checkWitnessC7(admitted.payload, state);
173
+ matchConfigureDATA(admitted.scope, grant.scope);
174
+ const checked = checkedWitnessDescriptor(reply.descriptor, admitted);
175
+ need(
176
+ checked.witnessRowId === grant.witnessRowId &&
177
+ checked.payloadBinding === grant.payloadBinding,
178
+ );
179
+ stored = admitted;
180
+ descriptor = checked;
181
+ guard();
182
+ },
183
+ async historicalEvidence() {
184
+ need(mode === "read" && stored && descriptor);
185
+ guard();
186
+ const input = stored.historicalInput;
187
+ const material = await state.owned(
188
+ () => inspectHistoricalMaterialEvidenceV1(input),
189
+ guard,
190
+ );
191
+ return { identity: stored.identity, material };
192
+ },
193
+ descriptor() {
194
+ need(mode === "read" && descriptor);
195
+ guard();
196
+ return descriptor;
197
+ },
198
+ async finalize() {
199
+ need(authorization);
200
+ guard();
201
+ await authorization.authorize();
202
+ guard();
203
+ },
204
+ });
205
+ }
@@ -0,0 +1,93 @@
1
+ import {
2
+ witnessControls,
3
+ prepareWitnessObservation,
4
+ invokeWitnessCapture,
5
+ readStoredWitness,
6
+ witnessFixtureOptions,
7
+ } from "../test-fixtures/witnessObservation.js";
8
+ import {
9
+ configureAdmissionCase,
10
+ admissionControls,
11
+ } from "../test-fixtures/producerConfigureAdmission.js";
12
+ import { INITIAL_NS } from "../test-fixtures/producerInitialDeclarations.js";
13
+ import { expect, vi } from "vitest";
14
+ import assert from "node:assert/strict";
15
+
16
+ for (const mode of ["duplicate", "foreign-fresh", "foreign-invoice"] as const)
17
+ configureAdmissionCase(
18
+ "witness Source refuses " + mode + " profile before provider I/O",
19
+ prepareWitnessObservation,
20
+ async (h, state) => {
21
+ const before = h.calls.length,
22
+ selectors = admissionControls.selectors;
23
+ if (mode === "duplicate")
24
+ witnessControls.captures.push(state.captureProfile);
25
+ else
26
+ witnessControls.captures.splice(
27
+ 0,
28
+ witnessControls.captures.length,
29
+ Object.freeze({
30
+ ...state.captureProfile,
31
+ ...(mode === "foreign-fresh"
32
+ ? { freshProfile: Object.freeze({ ...state.profile }) }
33
+ : {
34
+ invoiceProfile: Object.freeze({
35
+ freshProfile: state.profile,
36
+ readProfile: "foreign",
37
+ authorizationQuery: state.captureProfile.authorizationQuery,
38
+ }),
39
+ }),
40
+ }),
41
+ );
42
+ await expect(invokeWitnessCapture(h, state)).rejects.toThrow();
43
+ expect(h.calls).toHaveLength(before);
44
+ expect(admissionControls.selectors).toBe(selectors);
45
+ expect(await readStoredWitness(h, state)).toBeNull();
46
+ },
47
+ witnessFixtureOptions,
48
+ );
49
+
50
+ let track = false,
51
+ ticks = 0;
52
+ configureAdmissionCase(
53
+ "captured acquisition metadata measures the original fresh wall and monotonic lifetime",
54
+ async (h) => {
55
+ track = false;
56
+ ticks = 0;
57
+ return prepareWitnessObservation(h);
58
+ },
59
+ async (h, state) => {
60
+ const start = Date.now();
61
+ track = true;
62
+ try {
63
+ await invokeWitnessCapture(h, state);
64
+ } finally {
65
+ track = false;
66
+ }
67
+ const stored = await readStoredWitness(h, state);
68
+ assert(stored);
69
+ const payload = JSON.parse(stored.payloadJSON);
70
+ expect(ticks).toBe(6);
71
+ expect(payload.acquisition).toEqual({
72
+ observedAtMs: start + 12,
73
+ elapsedWallMs: 12,
74
+ elapsedNsDecimal: "12000000",
75
+ apiVersion: "2026-09-30.endive",
76
+ baseReadProfile: "endive-schedule-first-44-v1",
77
+ invoiceReadProfile: null,
78
+ });
79
+ expect(stored.descriptor.observedAtMs).toBe(start + 12);
80
+ },
81
+ {
82
+ ...witnessFixtureOptions,
83
+ beforeFetch: async (_path, method) => {
84
+ if (track && method === "GET") {
85
+ ticks++;
86
+ vi.setSystemTime(Date.now() + 2);
87
+ vi.mocked(process.hrtime.bigint).mockReturnValue(
88
+ INITIAL_NS + BigInt(ticks * 2000000),
89
+ );
90
+ }
91
+ },
92
+ },
93
+ );