session-peer 0.5.2 → 0.6.1
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.
- package/CONTRIBUTING.md +11 -5
- package/PARITY.md +20 -5
- package/README.ja.md +8 -8
- package/README.ko.md +8 -8
- package/README.md +8 -8
- package/README.zh-CN.md +8 -8
- package/RELEASING.md +116 -0
- package/VALIDATION.md +137 -1
- package/dist/cli.js +54 -4
- package/dist/diagnostics.js +17 -98
- package/dist/discovery.d.ts +32 -0
- package/dist/discovery.js +405 -68
- package/dist/handoff-operations.js +88 -34
- package/dist/help.js +18 -2
- package/dist/macos-acl.d.ts +10 -0
- package/dist/macos-acl.js +75 -0
- package/dist/output.js +6 -1
- package/dist/protocol.d.ts +2 -2
- package/dist/protocol.js +1 -1
- package/dist/receipt-collector.d.ts +12 -0
- package/dist/receipt-collector.js +283 -0
- package/dist/receipt-command.d.ts +10 -0
- package/dist/receipt-command.js +246 -0
- package/dist/receipt-producer.d.ts +7 -0
- package/dist/receipt-producer.js +107 -0
- package/dist/receipt-protocol.d.ts +219 -0
- package/dist/receipt-protocol.js +374 -0
- package/dist/receipt-send.d.ts +26 -0
- package/dist/receipt-send.js +375 -0
- package/dist/receipt-state.d.ts +89 -0
- package/dist/receipt-state.js +503 -0
- package/dist/receipt-store.d.ts +32 -0
- package/dist/receipt-store.js +286 -0
- package/dist/replies.js +3 -1
- package/dist/send.d.ts +5 -2
- package/dist/send.js +34 -49
- package/dist/skill-metadata.js +5 -3
- package/docs/api.md +32 -7
- package/docs/design/typescript-skill-compatibility.md +48 -0
- package/docs/guide.ja.md +41 -15
- package/docs/guide.ko.md +43 -17
- package/docs/guide.md +42 -16
- package/docs/guide.zh-CN.md +41 -15
- package/docs/shorthand.md +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
// Single-owner receipt actor. It only records the original caller's evidence;
|
|
2
|
+
// it never sends native input, reads transcripts or treats a token as identity.
|
|
3
|
+
import { randomBytes, randomUUID, timingSafeEqual } from 'node:crypto';
|
|
4
|
+
import { canonicalId, closed, fail, HANDOFF_LIMITS, safeInteger, systemClock } from './handoff.js';
|
|
5
|
+
import { ReceiptStore, receiptDigest } from './receipt-store.js';
|
|
6
|
+
import { validateBinding, validateProducerEvidence, validateReceiptPair, validateManagedHandoff, validateManagedReceipt } from './receipt-protocol.js';
|
|
7
|
+
const reserveBytes = 65536;
|
|
8
|
+
const hex = (value) => typeof value === 'string' && /^[a-f0-9]{64}$/.test(value);
|
|
9
|
+
const finite = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0;
|
|
10
|
+
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
11
|
+
function normalizeBinding(value) {
|
|
12
|
+
const row = validateBinding(value);
|
|
13
|
+
// Stable keys make comparison independent of sender JSON insertion order.
|
|
14
|
+
return Object.fromEntries(Object.entries(row).sort(([a], [b]) => a.localeCompare(b)));
|
|
15
|
+
}
|
|
16
|
+
function assertSelector(value) {
|
|
17
|
+
closed(value, ['ledgerEpoch', 'correlationId']);
|
|
18
|
+
if (!canonicalId(value.ledgerEpoch) || !canonicalId(value.correlationId))
|
|
19
|
+
fail('receipt_id_unknown');
|
|
20
|
+
}
|
|
21
|
+
export class ReceiptState {
|
|
22
|
+
path;
|
|
23
|
+
collectorContextId = randomUUID();
|
|
24
|
+
clock;
|
|
25
|
+
store;
|
|
26
|
+
data;
|
|
27
|
+
busy = false;
|
|
28
|
+
stopped = false;
|
|
29
|
+
lastMono;
|
|
30
|
+
constructor(path, options = {}) {
|
|
31
|
+
this.path = path;
|
|
32
|
+
this.clock = options.clock ?? systemClock;
|
|
33
|
+
}
|
|
34
|
+
static initialize(path, endpointSelector) {
|
|
35
|
+
return ReceiptStore.initialize(path, endpointSelector);
|
|
36
|
+
}
|
|
37
|
+
get endpointSelector() { if (!this.store)
|
|
38
|
+
fail('receipt_collector_closed'); return this.store.marker.endpointSelector; }
|
|
39
|
+
get ledgerEpoch() { if (!this.data)
|
|
40
|
+
fail('receipt_collector_closed'); return this.data.epoch; }
|
|
41
|
+
assertOpen() {
|
|
42
|
+
if (!this.store || !this.data || this.stopped)
|
|
43
|
+
fail('receipt_collector_closed');
|
|
44
|
+
this.now();
|
|
45
|
+
this.store.assertUnchanged();
|
|
46
|
+
}
|
|
47
|
+
open() {
|
|
48
|
+
if (this.store || this.stopped)
|
|
49
|
+
fail('receipt_collector_closed');
|
|
50
|
+
try {
|
|
51
|
+
this.store = new ReceiptStore(this.path);
|
|
52
|
+
const raw = this.store.read();
|
|
53
|
+
if (!raw.records || typeof raw.records !== 'object' || Array.isArray(raw.records))
|
|
54
|
+
fail('receipt_history_corrupt');
|
|
55
|
+
for (const [id, item] of Object.entries(raw.records)) {
|
|
56
|
+
if (!canonicalId(id) || !item || typeof item !== 'object' || !item.binding)
|
|
57
|
+
fail('receipt_history_corrupt');
|
|
58
|
+
item.binding.payloadDigest = this.store.openDigest(item.binding.payloadDigest, raw.epoch, id);
|
|
59
|
+
}
|
|
60
|
+
this.validate(raw);
|
|
61
|
+
this.data = raw;
|
|
62
|
+
// Restart never adopts a prior owner's live authority or monotonic waits.
|
|
63
|
+
this.transact(data => {
|
|
64
|
+
let changed = false;
|
|
65
|
+
for (const item of Object.values(data.records))
|
|
66
|
+
changed = this.classify(item) || changed;
|
|
67
|
+
return { value: undefined, write: changed };
|
|
68
|
+
});
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
this.store?.close();
|
|
73
|
+
this.store = undefined;
|
|
74
|
+
this.data = undefined;
|
|
75
|
+
this.stopped = true;
|
|
76
|
+
if (error.code === 'ENOENT')
|
|
77
|
+
fail('receipt_history_unavailable');
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
close() { this.stopped = true; this.store?.close(); this.store = undefined; this.data = undefined; }
|
|
82
|
+
now() {
|
|
83
|
+
const now = this.clock.monotonic();
|
|
84
|
+
if (!finite(now) || (this.lastMono !== undefined && now < this.lastMono)) {
|
|
85
|
+
// Observed clock loss permanently closes this owner. Catching up later
|
|
86
|
+
// cannot revive a capability or a deadline from the lost clock context.
|
|
87
|
+
this.stopped = true;
|
|
88
|
+
fail('receipt_authority_expired');
|
|
89
|
+
}
|
|
90
|
+
this.lastMono = now;
|
|
91
|
+
return now;
|
|
92
|
+
}
|
|
93
|
+
clockReference() {
|
|
94
|
+
this.assertOpen();
|
|
95
|
+
return Math.floor(this.now());
|
|
96
|
+
}
|
|
97
|
+
utc() { const now = Math.floor(this.clock.utc()); if (!safeInteger(now))
|
|
98
|
+
fail('receipt_authority_unavailable'); return now; }
|
|
99
|
+
transact(operation) {
|
|
100
|
+
if (this.busy)
|
|
101
|
+
fail('receipt_collector_busy');
|
|
102
|
+
if (!this.store || !this.data || this.stopped)
|
|
103
|
+
fail('receipt_collector_closed');
|
|
104
|
+
this.busy = true;
|
|
105
|
+
try {
|
|
106
|
+
this.store.assertUnchanged();
|
|
107
|
+
const copy = structuredClone(this.data), result = operation(copy);
|
|
108
|
+
if (result.write !== false) {
|
|
109
|
+
this.validate(copy);
|
|
110
|
+
const stored = structuredClone(copy);
|
|
111
|
+
for (const [id, item] of Object.entries(stored.records))
|
|
112
|
+
item.binding.payloadDigest = this.store.sealDigest(item.binding.payloadDigest, stored.epoch, id);
|
|
113
|
+
const text = JSON.stringify({ digest: receiptDigest(JSON.stringify(stored)), data: stored });
|
|
114
|
+
const reserved = Object.values(stored.records).reduce((n, item) => n + Math.max(0, item.reserve - Buffer.byteLength(JSON.stringify(item))), 0);
|
|
115
|
+
if (Buffer.byteLength(text) + reserved > HANDOFF_LIMITS.journal)
|
|
116
|
+
fail('receipt_ledger_full');
|
|
117
|
+
try {
|
|
118
|
+
this.store.write(stored);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
this.stopped = true;
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
this.data = copy;
|
|
125
|
+
}
|
|
126
|
+
return result.value;
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
this.busy = false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
validate(data) {
|
|
133
|
+
try {
|
|
134
|
+
closed(data, ['version', 'epoch', 'keyId', 'quarantined', 'records']);
|
|
135
|
+
if (data.version !== 2 || !canonicalId(data.epoch) || !hex(data.keyId) || typeof data.quarantined !== 'boolean' ||
|
|
136
|
+
!data.records || typeof data.records !== 'object' || Array.isArray(data.records) || Object.keys(data.records).length > HANDOFF_LIMITS.intents)
|
|
137
|
+
throw 0;
|
|
138
|
+
for (const [id, item] of Object.entries(data.records)) {
|
|
139
|
+
closed(item, ['binding', 'producerEvidence', 'receiptSchemaVersion', 'receiptScope', 'receiptContextId', 'originClockId',
|
|
140
|
+
'preparedMono', 'preparedUtc', 'phase', 'handoff', 'waits', 'reserve', 'revoked', 'expired'], ['capabilityHash', 'receiptId', 'pendingReceipt']);
|
|
141
|
+
if (!canonicalId(id) || !canonicalId(item.originClockId) || !finite(item.preparedMono) || !safeInteger(item.preparedUtc) ||
|
|
142
|
+
!['prepared', 'fenced', 'finished'].includes(item.phase) || typeof item.revoked !== 'boolean' || typeof item.expired !== 'boolean' ||
|
|
143
|
+
!safeInteger(item.reserve) || item.reserve > reserveBytes || !Array.isArray(item.waits) || item.waits.length > HANDOFF_LIMITS.waits)
|
|
144
|
+
throw 0;
|
|
145
|
+
item.binding = normalizeBinding(item.binding);
|
|
146
|
+
validateProducerEvidence(item.producerEvidence);
|
|
147
|
+
validateReceiptPair(item.binding.agent, item.receiptSchemaVersion, item.receiptScope);
|
|
148
|
+
if (item.producerEvidence.receiptSchemaVersion !== item.receiptSchemaVersion || item.producerEvidence.receiptScope !== item.receiptScope ||
|
|
149
|
+
(item.binding.agent === 'codex' ? item.receiptContextId !== null : !canonicalId(item.receiptContextId)))
|
|
150
|
+
throw 0;
|
|
151
|
+
const h = validateManagedHandoff(item.handoff);
|
|
152
|
+
if (h.correlationId !== id || h.ledgerEpoch !== data.epoch || h.targetGeneration !== item.binding.generation ||
|
|
153
|
+
h.schemaVersion !== item.receiptSchemaVersion || (h.schemaVersion === 2 && h.receiptContext.id !== item.receiptContextId))
|
|
154
|
+
throw 0;
|
|
155
|
+
if (item.capabilityHash !== undefined && !hex(item.capabilityHash))
|
|
156
|
+
throw 0;
|
|
157
|
+
if (item.receiptId !== undefined && (!canonicalId(item.receiptId) || h.ack.status !== 'acknowledged'))
|
|
158
|
+
throw 0;
|
|
159
|
+
if (h.ack.status === 'acknowledged' && (!item.receiptId || !item.capabilityHash || h.submission.status !== 'submitted'))
|
|
160
|
+
throw 0;
|
|
161
|
+
if (item.phase === 'prepared' && h.submission.status !== 'not_attempted')
|
|
162
|
+
throw 0;
|
|
163
|
+
if (item.phase === 'fenced' && h.submission.status !== 'unknown')
|
|
164
|
+
throw 0;
|
|
165
|
+
if (item.phase === 'finished' && !['submitted', 'refused', 'unknown'].includes(h.submission.status))
|
|
166
|
+
throw 0;
|
|
167
|
+
for (const wait of item.waits) {
|
|
168
|
+
closed(wait, ['public', 'clockId', 'cutoff']);
|
|
169
|
+
if (!canonicalId(wait.clockId) || !finite(wait.cutoff))
|
|
170
|
+
throw 0;
|
|
171
|
+
validateManagedHandoff({ ...h, wait: wait.public, nextActions: ['reconcile'] });
|
|
172
|
+
}
|
|
173
|
+
if (item.pendingReceipt !== undefined) {
|
|
174
|
+
const p = item.pendingReceipt;
|
|
175
|
+
closed(p, ['receiptId', 'receiptSchemaVersion', 'receiptScope', 'receiptContextId', 'arrivalClockContextId', 'arrivalMonotonic', 'arrivalUtcMs']);
|
|
176
|
+
if (!canonicalId(p.receiptId) || p.receiptSchemaVersion !== item.receiptSchemaVersion || p.receiptScope !== item.receiptScope ||
|
|
177
|
+
p.receiptContextId !== item.receiptContextId || !canonicalId(p.arrivalClockContextId) || !finite(p.arrivalMonotonic) || !safeInteger(p.arrivalUtcMs) ||
|
|
178
|
+
!item.capabilityHash || item.phase === 'prepared' || (item.receiptId !== undefined && item.receiptId !== p.receiptId))
|
|
179
|
+
throw 0;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
fail('receipt_history_corrupt');
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
item(data, selector) {
|
|
188
|
+
assertSelector(selector);
|
|
189
|
+
if (data.epoch !== selector.ledgerEpoch)
|
|
190
|
+
fail('receipt_epoch_mismatch');
|
|
191
|
+
if (data.quarantined)
|
|
192
|
+
fail('receipt_history_quarantined');
|
|
193
|
+
const item = data.records[selector.correlationId];
|
|
194
|
+
if (!item)
|
|
195
|
+
fail('receipt_id_unknown');
|
|
196
|
+
if (item.expired || this.utc() - item.preparedUtc >= HANDOFF_LIMITS.detailMs)
|
|
197
|
+
fail('receipt_history_unavailable');
|
|
198
|
+
return item;
|
|
199
|
+
}
|
|
200
|
+
handle(data, value) {
|
|
201
|
+
const item = this.item(data, { ledgerEpoch: value.ledgerEpoch, correlationId: value.correlationId });
|
|
202
|
+
if (value.receiptContextId !== item.receiptContextId)
|
|
203
|
+
fail('receipt_context_mismatch');
|
|
204
|
+
return item;
|
|
205
|
+
}
|
|
206
|
+
authority(item) {
|
|
207
|
+
const elapsed = this.now() - item.preparedMono;
|
|
208
|
+
return !!item.capabilityHash && !item.revoked && !item.expired && item.originClockId === this.collectorContextId &&
|
|
209
|
+
elapsed >= 0 && elapsed < HANDOFF_LIMITS.capabilityMs;
|
|
210
|
+
}
|
|
211
|
+
classify(item) {
|
|
212
|
+
const before = JSON.stringify(item), h = item.handoff, now = this.now();
|
|
213
|
+
const live = this.authority(item);
|
|
214
|
+
if (h.ack.status === 'pending' && !live)
|
|
215
|
+
h.ack = { status: 'unsupported' };
|
|
216
|
+
for (const wait of item.waits) {
|
|
217
|
+
if (wait.public.status !== 'pending')
|
|
218
|
+
continue;
|
|
219
|
+
if (!live || wait.clockId !== this.collectorContextId) {
|
|
220
|
+
wait.public.status = 'unsupported';
|
|
221
|
+
wait.public.reason = 'evidence_unsupported';
|
|
222
|
+
}
|
|
223
|
+
else if (now >= wait.cutoff) {
|
|
224
|
+
if (item.phase === 'prepared') {
|
|
225
|
+
wait.public.status = 'failed';
|
|
226
|
+
wait.public.reason = 'deadline_before_effect';
|
|
227
|
+
item.phase = 'finished';
|
|
228
|
+
item.revoked = true;
|
|
229
|
+
h.state = 'refused';
|
|
230
|
+
h.submission = { status: 'refused' };
|
|
231
|
+
h.ack = { status: 'unsupported' };
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
wait.public.status = 'timed_out_unknown';
|
|
235
|
+
if (h.ack.status !== 'acknowledged')
|
|
236
|
+
h.state = 'timed_out_unknown';
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (item.waits.length)
|
|
241
|
+
h.wait = structuredClone(item.waits.at(-1).public);
|
|
242
|
+
if (h.ack.status !== 'acknowledged' && h.wait.status === 'timed_out_unknown' && ['submitted', 'unknown'].includes(h.submission.status))
|
|
243
|
+
h.state = 'timed_out_unknown';
|
|
244
|
+
h.nextActions = ['reconcile'];
|
|
245
|
+
if (h.wait.status === 'pending') {
|
|
246
|
+
h.nextActions.push('stop_waiting');
|
|
247
|
+
if (h.ack.status === 'pending' && ['submitted', 'unknown'].includes(h.submission.status))
|
|
248
|
+
h.nextActions.unshift('keep_waiting');
|
|
249
|
+
}
|
|
250
|
+
return before !== JSON.stringify(item);
|
|
251
|
+
}
|
|
252
|
+
snapshot(item, wait) {
|
|
253
|
+
const h = structuredClone(item.handoff);
|
|
254
|
+
if (wait) {
|
|
255
|
+
h.wait = structuredClone(wait.public);
|
|
256
|
+
h.nextActions = ['reconcile'];
|
|
257
|
+
if (wait.public.status === 'pending') {
|
|
258
|
+
h.nextActions.push('stop_waiting');
|
|
259
|
+
if (h.ack.status === 'pending' && ['submitted', 'unknown'].includes(h.submission.status))
|
|
260
|
+
h.nextActions.unshift('keep_waiting');
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return validateManagedHandoff(h);
|
|
264
|
+
}
|
|
265
|
+
prepare(input) {
|
|
266
|
+
closed(input, ['binding', 'receiptSchemaVersion', 'receiptScope', 'producerEvidence']);
|
|
267
|
+
const binding = normalizeBinding(input.binding), producer = validateProducerEvidence(input.producerEvidence);
|
|
268
|
+
validateReceiptPair(binding.agent, input.receiptSchemaVersion, input.receiptScope);
|
|
269
|
+
if (producer.receiptSchemaVersion !== input.receiptSchemaVersion || producer.receiptScope !== input.receiptScope)
|
|
270
|
+
fail('receipt_producer_mismatch');
|
|
271
|
+
return this.transact(data => {
|
|
272
|
+
if (data.quarantined)
|
|
273
|
+
fail('receipt_history_quarantined');
|
|
274
|
+
if (Object.keys(data.records).length >= HANDOFF_LIMITS.intents)
|
|
275
|
+
fail('receipt_ledger_full');
|
|
276
|
+
const id = randomUUID(), context = binding.agent === 'claude' ? randomUUID() : null;
|
|
277
|
+
const shared = { correlationId: id, ledgerEpoch: data.epoch, state: 'validated', submission: { status: 'not_attempted' },
|
|
278
|
+
observation: { status: 'not_requested', injectionObserved: false }, ack: { status: 'unsupported' },
|
|
279
|
+
wait: { for: 'none', status: 'not_requested' }, targetGeneration: binding.generation, decisionOwner: 'sender_operator',
|
|
280
|
+
retry: { allowed: false, reason: 'receiver_dedup_unavailable' }, nextActions: [] };
|
|
281
|
+
const handoff = validateManagedHandoff(binding.agent === 'codex' ? { schemaVersion: 1, ...shared } :
|
|
282
|
+
{ schemaVersion: 2, ...shared, receiptContext: { kind: 'delegated_context', id: context } });
|
|
283
|
+
data.records[id] = { binding, producerEvidence: producer, receiptSchemaVersion: input.receiptSchemaVersion, receiptScope: input.receiptScope,
|
|
284
|
+
receiptContextId: context, originClockId: this.collectorContextId, preparedMono: this.now(), preparedUtc: this.utc(), phase: 'prepared',
|
|
285
|
+
handoff, waits: [], reserve: reserveBytes, revoked: false, expired: false };
|
|
286
|
+
return { value: { ledgerEpoch: data.epoch, correlationId: id, receiptContextId: context, handoff: this.snapshot(data.records[id]) } };
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
mint(input) {
|
|
290
|
+
closed(input, ['ledgerEpoch', 'correlationId', 'receiptContextId', 'producerProtocolVersion']);
|
|
291
|
+
return this.transact(data => {
|
|
292
|
+
const item = this.handle(data, input);
|
|
293
|
+
const elapsed = this.now() - item.preparedMono;
|
|
294
|
+
if (item.phase !== 'prepared' || item.capabilityHash || item.originClockId !== this.collectorContextId || item.revoked ||
|
|
295
|
+
elapsed < 0 || elapsed >= HANDOFF_LIMITS.capabilityMs)
|
|
296
|
+
fail('receipt_authority_unavailable');
|
|
297
|
+
if (input.producerProtocolVersion !== item.producerEvidence.protocolVersion)
|
|
298
|
+
fail('receipt_producer_mismatch');
|
|
299
|
+
const capability = randomBytes(32).toString('base64url');
|
|
300
|
+
item.capabilityHash = receiptDigest(capability);
|
|
301
|
+
item.handoff.ack = { status: 'pending' };
|
|
302
|
+
return { value: { ledgerEpoch: data.epoch, correlationId: input.correlationId, receiptContextId: item.receiptContextId,
|
|
303
|
+
receiptSchemaVersion: item.receiptSchemaVersion, receiptScope: item.receiptScope, targetGeneration: item.binding.generation, capability } };
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
fence(input) {
|
|
307
|
+
closed(input, ['ledgerEpoch', 'correlationId', 'receiptContextId', 'binding']);
|
|
308
|
+
const binding = normalizeBinding(input.binding);
|
|
309
|
+
return this.transact(data => {
|
|
310
|
+
const item = this.handle(data, input);
|
|
311
|
+
this.classify(item);
|
|
312
|
+
if (!same(binding, item.binding))
|
|
313
|
+
fail('receipt_binding_conflict');
|
|
314
|
+
if (item.phase !== 'prepared')
|
|
315
|
+
fail('receipt_already_attempted');
|
|
316
|
+
if (!this.authority(item))
|
|
317
|
+
fail('receipt_authority_unavailable');
|
|
318
|
+
item.phase = 'fenced';
|
|
319
|
+
item.handoff.state = 'unknown';
|
|
320
|
+
item.handoff.submission = { status: 'unknown' };
|
|
321
|
+
this.classify(item);
|
|
322
|
+
return { value: this.snapshot(item) };
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
submission(input) {
|
|
326
|
+
closed(input, ['ledgerEpoch', 'correlationId', 'receiptContextId', 'status']);
|
|
327
|
+
if (!['submitted', 'refused', 'unknown'].includes(input.status))
|
|
328
|
+
fail('receipt_outcome_not_authorized');
|
|
329
|
+
return this.transact(data => {
|
|
330
|
+
const item = this.handle(data, input);
|
|
331
|
+
if (item.phase !== 'fenced' || item.originClockId !== this.collectorContextId)
|
|
332
|
+
fail('receipt_outcome_not_authorized');
|
|
333
|
+
this.classify(item);
|
|
334
|
+
item.phase = 'finished';
|
|
335
|
+
item.handoff.submission = { status: input.status };
|
|
336
|
+
item.handoff.state = input.status;
|
|
337
|
+
if (input.status !== 'submitted') {
|
|
338
|
+
item.handoff.ack = { status: 'unsupported' };
|
|
339
|
+
for (const wait of item.waits)
|
|
340
|
+
if (wait.public.status === 'pending') {
|
|
341
|
+
wait.public.status = input.status === 'refused' ? 'failed' : 'unsupported';
|
|
342
|
+
wait.public.reason = input.status === 'refused' ? 'evidence_failed' : 'evidence_unsupported';
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
else if (item.pendingReceipt && this.authority(item))
|
|
346
|
+
this.commitAck(item, item.pendingReceipt.receiptId);
|
|
347
|
+
this.classify(item);
|
|
348
|
+
return { value: this.snapshot(item) };
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
status(input) {
|
|
352
|
+
assertSelector(input);
|
|
353
|
+
return this.transact(data => { const item = this.item(data, input), changed = this.classify(item); return { value: this.snapshot(item), write: changed }; });
|
|
354
|
+
}
|
|
355
|
+
waitOpen(input) {
|
|
356
|
+
closed(input, ['ledgerEpoch', 'correlationId', 'goal', 'timeoutSeconds'], ['clockReferenceMs', 'remainingMs']);
|
|
357
|
+
if (input.goal !== 'acknowledged' || !Number.isInteger(input.timeoutSeconds) || input.timeoutSeconds < 1 || input.timeoutSeconds > 60)
|
|
358
|
+
fail('receipt_wait_unknown');
|
|
359
|
+
return this.transact(data => {
|
|
360
|
+
const item = this.item(data, { ledgerEpoch: input.ledgerEpoch, correlationId: input.correlationId });
|
|
361
|
+
this.classify(item);
|
|
362
|
+
if (item.waits.length >= HANDOFF_LIMITS.waits)
|
|
363
|
+
fail('receipt_wait_quota');
|
|
364
|
+
const now = this.now();
|
|
365
|
+
const active = Object.values(data.records).reduce((n, r) => n + r.waits.filter(w => w.public.status === 'pending' && w.clockId === this.collectorContextId && now < w.cutoff).length, 0);
|
|
366
|
+
const paired = input.clockReferenceMs !== undefined || input.remainingMs !== undefined;
|
|
367
|
+
if (paired && (!safeInteger(input.clockReferenceMs) || input.clockReferenceMs > now ||
|
|
368
|
+
!safeInteger(input.remainingMs) || input.remainingMs < 1 || input.remainingMs > input.timeoutSeconds * 1000))
|
|
369
|
+
fail('receipt_wait_unknown');
|
|
370
|
+
const span = paired ? input.remainingMs : input.timeoutSeconds * 1000;
|
|
371
|
+
const cutoff = Math.max(0, (paired ? input.clockReferenceMs : now) + span - HANDOFF_LIMITS.cleanupMs);
|
|
372
|
+
const wait = { public: { for: 'acknowledged', status: 'pending', operationId: randomUUID(),
|
|
373
|
+
deadlineAtUtcMs: Math.max(0, Math.floor(this.utc() + Math.max(0, cutoff - now))) }, clockId: this.collectorContextId, cutoff };
|
|
374
|
+
if (item.handoff.ack.status === 'acknowledged')
|
|
375
|
+
wait.public.status = 'satisfied';
|
|
376
|
+
else if (input.timeoutSeconds * 1000 <= HANDOFF_LIMITS.cleanupMs) {
|
|
377
|
+
wait.public.status = 'failed';
|
|
378
|
+
wait.public.reason = 'insufficient_budget';
|
|
379
|
+
}
|
|
380
|
+
else if (!this.authority(item) || (item.phase === 'finished' && item.handoff.submission.status !== 'submitted')) {
|
|
381
|
+
wait.public.status = 'unsupported';
|
|
382
|
+
wait.public.reason = 'evidence_unsupported';
|
|
383
|
+
}
|
|
384
|
+
else if (now >= cutoff) {
|
|
385
|
+
wait.public.status = item.phase === 'prepared' ? 'failed' : 'timed_out_unknown';
|
|
386
|
+
if (item.phase === 'prepared')
|
|
387
|
+
wait.public.reason = 'deadline_before_effect';
|
|
388
|
+
}
|
|
389
|
+
if (wait.public.status === 'pending' && active >= 128)
|
|
390
|
+
fail('receipt_wait_quota');
|
|
391
|
+
item.waits.push(wait);
|
|
392
|
+
if (item.phase === 'prepared' && wait.public.status !== 'pending') {
|
|
393
|
+
item.phase = 'finished';
|
|
394
|
+
item.revoked = true;
|
|
395
|
+
item.handoff.state = 'refused';
|
|
396
|
+
item.handoff.submission = { status: 'refused' };
|
|
397
|
+
item.handoff.ack = { status: 'unsupported' };
|
|
398
|
+
}
|
|
399
|
+
this.classify(item);
|
|
400
|
+
return { value: this.snapshot(item) };
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
selectWait(item, id) {
|
|
404
|
+
if (!canonicalId(id))
|
|
405
|
+
fail('receipt_wait_unknown');
|
|
406
|
+
const wait = item.waits.find(w => w.public.operationId === id);
|
|
407
|
+
if (!wait)
|
|
408
|
+
fail('receipt_wait_unknown');
|
|
409
|
+
return wait;
|
|
410
|
+
}
|
|
411
|
+
waitStatus(input) {
|
|
412
|
+
closed(input, ['ledgerEpoch', 'correlationId', 'operationId']);
|
|
413
|
+
return this.transact(data => {
|
|
414
|
+
const item = this.item(data, { ledgerEpoch: input.ledgerEpoch, correlationId: input.correlationId });
|
|
415
|
+
const wait = this.selectWait(item, input.operationId), changed = this.classify(item);
|
|
416
|
+
return { value: this.snapshot(item, wait), write: changed };
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
waitStop(input) {
|
|
420
|
+
closed(input, ['ledgerEpoch', 'correlationId', 'operationId']);
|
|
421
|
+
return this.transact(data => {
|
|
422
|
+
const item = this.item(data, { ledgerEpoch: input.ledgerEpoch, correlationId: input.correlationId });
|
|
423
|
+
this.classify(item);
|
|
424
|
+
const wait = this.selectWait(item, input.operationId);
|
|
425
|
+
if (wait.public.status !== 'pending')
|
|
426
|
+
fail('receipt_wait_terminal');
|
|
427
|
+
wait.public.status = 'stopped';
|
|
428
|
+
wait.public.reason = 'stopped_by_operator';
|
|
429
|
+
this.classify(item);
|
|
430
|
+
return { value: this.snapshot(item, wait) };
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
revoke(input) {
|
|
434
|
+
assertSelector(input);
|
|
435
|
+
return this.transact(data => {
|
|
436
|
+
const item = this.item(data, input);
|
|
437
|
+
item.revoked = true;
|
|
438
|
+
if (item.phase === 'prepared') {
|
|
439
|
+
item.phase = 'finished';
|
|
440
|
+
item.handoff.state = 'refused';
|
|
441
|
+
item.handoff.submission = { status: 'refused' };
|
|
442
|
+
for (const wait of item.waits)
|
|
443
|
+
if (wait.public.status === 'pending') {
|
|
444
|
+
wait.public.status = 'failed';
|
|
445
|
+
wait.public.reason = 'evidence_failed';
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
this.classify(item);
|
|
449
|
+
return { value: this.snapshot(item) };
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
commitAck(item, receiptId) {
|
|
453
|
+
const now = this.now(), origin = item.waits[0];
|
|
454
|
+
if (item.handoff.submission.status !== 'submitted' || !this.authority(item))
|
|
455
|
+
fail('receipt_authority_expired');
|
|
456
|
+
this.classify(item);
|
|
457
|
+
const late = !!origin && (origin.public.status === 'timed_out_unknown' || now >= origin.cutoff);
|
|
458
|
+
item.handoff.state = 'acknowledged';
|
|
459
|
+
item.handoff.ack = { status: 'acknowledged', assurance: 'token_possession', receivedAtUtcMs: this.utc(), late };
|
|
460
|
+
item.receiptId = receiptId;
|
|
461
|
+
for (const wait of item.waits)
|
|
462
|
+
if (wait.public.status === 'pending')
|
|
463
|
+
wait.public.status = now < wait.cutoff ? 'satisfied' : 'timed_out_unknown';
|
|
464
|
+
this.classify(item);
|
|
465
|
+
}
|
|
466
|
+
receipt(raw) {
|
|
467
|
+
const proof = validateManagedReceipt(raw);
|
|
468
|
+
return this.transact(data => {
|
|
469
|
+
// Always compare a fixed-size hash, including unknown IDs; errors never
|
|
470
|
+
// reflect the secret-bearing receipt or raw transport/parser details.
|
|
471
|
+
const item = data.records[proof.correlationId];
|
|
472
|
+
const actual = Buffer.from(receiptDigest(proof.capability), 'hex'), expected = Buffer.from(item?.capabilityHash ?? '0'.repeat(64), 'hex');
|
|
473
|
+
const authenticated = timingSafeEqual(actual, expected);
|
|
474
|
+
if (!authenticated || !item || !item.capabilityHash || item.revoked || item.expired || data.quarantined || data.epoch !== proof.ledgerEpoch ||
|
|
475
|
+
this.utc() - item.preparedUtc >= HANDOFF_LIMITS.detailMs || item.receiptSchemaVersion !== proof.schemaVersion ||
|
|
476
|
+
(proof.schemaVersion === 1 ? item.binding.generation !== proof.targetGeneration : item.receiptContextId !== proof.receiptContextId))
|
|
477
|
+
fail('receipt_authentication_failed');
|
|
478
|
+
if (item.receiptId) {
|
|
479
|
+
if (item.receiptId !== proof.receiptId)
|
|
480
|
+
fail('receipt_already_committed');
|
|
481
|
+
return { value: { status: 'acknowledged', receiptId: proof.receiptId, handoff: this.snapshot(item) }, write: false };
|
|
482
|
+
}
|
|
483
|
+
if (!this.authority(item))
|
|
484
|
+
fail('receipt_authority_expired');
|
|
485
|
+
if (item.phase === 'prepared')
|
|
486
|
+
fail('receipt_authority_unavailable');
|
|
487
|
+
if (item.pendingReceipt && item.pendingReceipt.receiptId !== proof.receiptId)
|
|
488
|
+
fail('receipt_candidate_conflict');
|
|
489
|
+
if (item.handoff.submission.status === 'submitted') {
|
|
490
|
+
this.commitAck(item, proof.receiptId);
|
|
491
|
+
return { value: { status: 'acknowledged', receiptId: proof.receiptId, handoff: this.snapshot(item) } };
|
|
492
|
+
}
|
|
493
|
+
if (item.phase !== 'fenced')
|
|
494
|
+
fail('receipt_authority_unavailable');
|
|
495
|
+
if (item.pendingReceipt)
|
|
496
|
+
return { value: { status: 'awaiting_submission', receiptId: proof.receiptId, handoff: this.snapshot(item) }, write: false };
|
|
497
|
+
item.pendingReceipt = { receiptId: proof.receiptId, receiptSchemaVersion: item.receiptSchemaVersion, receiptScope: item.receiptScope,
|
|
498
|
+
receiptContextId: item.receiptContextId, arrivalClockContextId: this.collectorContextId, arrivalMonotonic: this.now(), arrivalUtcMs: this.utc() };
|
|
499
|
+
this.classify(item);
|
|
500
|
+
return { value: { status: 'awaiting_submission', receiptId: proof.receiptId, handoff: this.snapshot(item) } };
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare const receiptDigest: (value: string | Uint8Array) => string;
|
|
2
|
+
type Marker = {
|
|
3
|
+
version: 2;
|
|
4
|
+
ledgerEpoch: string;
|
|
5
|
+
endpointSelector: string;
|
|
6
|
+
};
|
|
7
|
+
export declare class ReceiptStore {
|
|
8
|
+
readonly path: string;
|
|
9
|
+
readonly marker: Marker;
|
|
10
|
+
private fd;
|
|
11
|
+
private readonly lockEvidence;
|
|
12
|
+
private readonly directoryEvidence;
|
|
13
|
+
private readonly markerEvidence;
|
|
14
|
+
private readonly keyEvidence;
|
|
15
|
+
private readonly key;
|
|
16
|
+
private stateEvidence;
|
|
17
|
+
private poisoned;
|
|
18
|
+
constructor(path: string);
|
|
19
|
+
static initialize(path: string, endpointSelector: string): {
|
|
20
|
+
ledgerEpoch: string;
|
|
21
|
+
storage: 'private_posix';
|
|
22
|
+
managedVersion: 2;
|
|
23
|
+
};
|
|
24
|
+
assertOwner(): void;
|
|
25
|
+
read(): unknown;
|
|
26
|
+
assertUnchanged(): void;
|
|
27
|
+
sealDigest(value: string, epoch: string, id: string): string;
|
|
28
|
+
openDigest(value: unknown, epoch: string, id: string): string;
|
|
29
|
+
write(data: unknown): void;
|
|
30
|
+
close(): void;
|
|
31
|
+
}
|
|
32
|
+
export {};
|