session-peer 0.5.2 → 0.6.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.
- 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 +73 -0
- package/VALIDATION.md +102 -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 +23 -3
- 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 +4 -3
- package/docs/api.md +32 -7
- package/docs/design/typescript-skill-compatibility.md +29 -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,374 @@
|
|
|
1
|
+
// Pure local receipt protocol. No filesystem, transport, process or native input.
|
|
2
|
+
import { posix } from 'node:path';
|
|
3
|
+
import { Refusal } from './discovery.js';
|
|
4
|
+
import { canonicalId, closed, HANDOFF_LIMITS, parseStrictJson, safeId, safeInteger, validateHandoff, validateReceipt } from './handoff.js';
|
|
5
|
+
export const RECEIPT_LIMITS = Object.freeze({ frame: 16384, privateFrame: 4096, depth: 16, nodes: 1024,
|
|
6
|
+
connections: 16, readMs: 2000, writeMs: 2000, exchangeMs: 5000, activeWaits: 128 });
|
|
7
|
+
export const RECEIPT_PAIRS = Object.freeze([
|
|
8
|
+
Object.freeze({ agent: 'codex', receiptSchemaVersion: 1, receiptScope: 'original_generation' }),
|
|
9
|
+
Object.freeze({ agent: 'claude', receiptSchemaVersion: 2, receiptScope: 'delegated_context' })
|
|
10
|
+
]);
|
|
11
|
+
export const RECEIPT_ERROR_CODES = Object.freeze([
|
|
12
|
+
'invalid_receipt_protocol', 'invalid_handoff_json', 'handoff_frame_too_large', 'invalid_receipt', 'invalid_handoff', 'receipt_ipc_version_unsupported', 'receipt_schema_unsupported', 'receipt_scope_unsupported',
|
|
13
|
+
'receipt_storage_unsupported', 'receipt_storage_untrusted', 'receipt_history_unavailable', 'receipt_history_changed',
|
|
14
|
+
'receipt_history_corrupt', 'receipt_state_exists', 'receipt_collector_busy', 'receipt_collector_closed',
|
|
15
|
+
'receipt_collector_error', 'receipt_collector_unavailable', 'receipt_ledger_full', 'receipt_id_unknown',
|
|
16
|
+
'receipt_epoch_mismatch', 'receipt_binding_conflict', 'receipt_scope_mismatch', 'receipt_authority_unavailable',
|
|
17
|
+
'receipt_authority_expired', 'receipt_authentication_failed', 'receipt_already_committed', 'receipt_candidate_conflict',
|
|
18
|
+
'receipt_already_attempted', 'receipt_outcome_not_authorized', 'receipt_wait_quota', 'receipt_wait_unknown',
|
|
19
|
+
'receipt_wait_terminal', 'receipt_context_mismatch', 'receipt_producer_mismatch', 'receipt_history_quarantined',
|
|
20
|
+
'receipt_bootstrap_unsupported', 'receipt_managed_context_required', 'receipt_endpoint_untrusted',
|
|
21
|
+
'receipt_endpoint_changed', 'receipt_endpoint_exists', 'receipt_endpoint_busy', 'receipt_transport_unknown',
|
|
22
|
+
'receipt_frame_too_large', 'receipt_transport_timeout', 'insufficient_budget', 'deadline_before_effect'
|
|
23
|
+
]);
|
|
24
|
+
export function isReceiptErrorCode(value) {
|
|
25
|
+
return typeof value === 'string' && RECEIPT_ERROR_CODES.includes(value);
|
|
26
|
+
}
|
|
27
|
+
export function publicReceiptError(error) {
|
|
28
|
+
return error instanceof Refusal && isReceiptErrorCode(error.code) ? error.code : 'receipt_collector_error';
|
|
29
|
+
}
|
|
30
|
+
function invalid(code = 'invalid_receipt_protocol') { throw new Refusal(code, 1); }
|
|
31
|
+
const digest = (v) => typeof v === 'string' && /^[0-9a-f]{64}$/.test(v);
|
|
32
|
+
const nativeUuid = (v) => typeof v === 'string' && /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(v);
|
|
33
|
+
const member = (v, choices) => typeof v === 'string' && choices.includes(v);
|
|
34
|
+
const capability = (v) => typeof v === 'string' && /^[A-Za-z0-9_-]{43}$/.test(v) &&
|
|
35
|
+
Buffer.from(v, 'base64url').length === 32 && Buffer.from(v, 'base64url').toString('base64url') === v;
|
|
36
|
+
const path = (v) => safeId(v, 4096) && posix.isAbsolute(v) && v.startsWith('/') && posix.normalize(v) === v;
|
|
37
|
+
function keys(value, required) {
|
|
38
|
+
try {
|
|
39
|
+
return closed(value, required);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
invalid();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function bounded(value, max = RECEIPT_LIMITS.frame) {
|
|
46
|
+
let nodes = 0;
|
|
47
|
+
const seen = new Set();
|
|
48
|
+
function visit(item, depth) {
|
|
49
|
+
if (++nodes > RECEIPT_LIMITS.nodes || depth > RECEIPT_LIMITS.depth)
|
|
50
|
+
invalid();
|
|
51
|
+
if (item === null || typeof item === 'boolean')
|
|
52
|
+
return;
|
|
53
|
+
if (typeof item === 'string') {
|
|
54
|
+
if (Buffer.byteLength(item, 'utf8') > max || /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/u.test(item))
|
|
55
|
+
invalid();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (typeof item === 'number') {
|
|
59
|
+
if (!safeInteger(item))
|
|
60
|
+
invalid();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (!item || typeof item !== 'object' || seen.has(item))
|
|
64
|
+
invalid();
|
|
65
|
+
seen.add(item);
|
|
66
|
+
if (Array.isArray(item))
|
|
67
|
+
for (const child of item)
|
|
68
|
+
visit(child, depth + 1);
|
|
69
|
+
else
|
|
70
|
+
for (const [key, child] of Object.entries(item)) {
|
|
71
|
+
if (++nodes > RECEIPT_LIMITS.nodes || !safeId(key, max))
|
|
72
|
+
invalid();
|
|
73
|
+
visit(child, depth + 1);
|
|
74
|
+
}
|
|
75
|
+
seen.delete(item);
|
|
76
|
+
}
|
|
77
|
+
visit(value, 0);
|
|
78
|
+
try {
|
|
79
|
+
if (Buffer.byteLength(JSON.stringify(value), 'utf8') + 1 > max)
|
|
80
|
+
invalid('receipt_frame_too_large');
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (error instanceof Refusal)
|
|
84
|
+
throw error;
|
|
85
|
+
invalid();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export function validateReceiptPair(agent, version, scope) {
|
|
89
|
+
const pair = RECEIPT_PAIRS.find(item => item.agent === agent && item.receiptSchemaVersion === version && item.receiptScope === scope);
|
|
90
|
+
if (!pair)
|
|
91
|
+
invalid('receipt_scope_unsupported');
|
|
92
|
+
return { ...pair };
|
|
93
|
+
}
|
|
94
|
+
export function validateBinding(value) {
|
|
95
|
+
const agent = value && typeof value === 'object' ? value.agent : undefined;
|
|
96
|
+
const row = keys(value, agent === 'codex' ? ['agent', 'destination', 'target', 'home', 'writerIdentity', 'generation', 'payloadDigest'] :
|
|
97
|
+
['agent', 'destination', 'target', 'registryDirectory', 'registryRecordPath', 'registryIdentity', 'socketPath', 'routeIdentity', 'generation', 'payloadDigest']);
|
|
98
|
+
if (!safeId(row.destination, 512) || !digest(row.payloadDigest))
|
|
99
|
+
invalid();
|
|
100
|
+
if (agent === 'codex') {
|
|
101
|
+
if (!nativeUuid(row.target) || !path(row.home) || !safeId(row.generation, 256) || !safeId(row.writerIdentity, 256))
|
|
102
|
+
invalid();
|
|
103
|
+
}
|
|
104
|
+
else if (agent === 'claude') {
|
|
105
|
+
if (typeof row.target !== 'string' || row.target.length > 10 || !/^[1-9][0-9]*$/.test(row.target) || Number(row.target) <= 1 || Number(row.target) > 2147483647 ||
|
|
106
|
+
!path(row.registryDirectory) || !path(row.registryRecordPath) || !digest(row.registryIdentity) ||
|
|
107
|
+
!safeId(row.socketPath, 4096) || !digest(row.routeIdentity) || row.generation !== null)
|
|
108
|
+
invalid();
|
|
109
|
+
}
|
|
110
|
+
else
|
|
111
|
+
invalid();
|
|
112
|
+
return structuredClone(row);
|
|
113
|
+
}
|
|
114
|
+
export const validateReceiptBinding = validateBinding;
|
|
115
|
+
export function validateProducerEvidence(value) {
|
|
116
|
+
const row = keys(value, ['protocolVersion', 'artifactSha256', 'challengeId', 'receiptSchemaVersion', 'receiptScope']);
|
|
117
|
+
if (row.protocolVersion !== 1 || !digest(row.artifactSha256) || !canonicalId(row.challengeId))
|
|
118
|
+
invalid('receipt_producer_mismatch');
|
|
119
|
+
validateReceiptPair(row.receiptSchemaVersion === 1 ? 'codex' : 'claude', row.receiptSchemaVersion, row.receiptScope);
|
|
120
|
+
return structuredClone(row);
|
|
121
|
+
}
|
|
122
|
+
export function validateReceiptV2(raw) {
|
|
123
|
+
const row = keys(parseStrictJson(raw), ['schemaVersion', 'kind', 'receiptScope', 'ledgerEpoch', 'correlationId', 'receiptContextId', 'receiptId', 'capability']);
|
|
124
|
+
if (row.schemaVersion !== 2 || row.kind !== 'receipt' || row.receiptScope !== 'delegated_context' ||
|
|
125
|
+
!canonicalId(row.ledgerEpoch) || !canonicalId(row.correlationId) || !canonicalId(row.receiptContextId) ||
|
|
126
|
+
!canonicalId(row.receiptId) || !capability(row.capability))
|
|
127
|
+
invalid();
|
|
128
|
+
return structuredClone(row);
|
|
129
|
+
}
|
|
130
|
+
export function validateManagedReceipt(raw) {
|
|
131
|
+
const value = parseStrictJson(raw);
|
|
132
|
+
if (value && value.schemaVersion === 1)
|
|
133
|
+
return validateReceipt(raw);
|
|
134
|
+
if (value && value.schemaVersion === 2)
|
|
135
|
+
return validateReceiptV2(raw);
|
|
136
|
+
invalid('receipt_schema_unsupported');
|
|
137
|
+
}
|
|
138
|
+
export function validateHandoffV2(value) {
|
|
139
|
+
const row = keys(value, ['schemaVersion', 'correlationId', 'ledgerEpoch', 'state', 'submission', 'observation', 'ack', 'wait',
|
|
140
|
+
'targetGeneration', 'receiptContext', 'decisionOwner', 'retry', 'nextActions']);
|
|
141
|
+
const context = keys(row.receiptContext, ['kind', 'id']);
|
|
142
|
+
if (row.schemaVersion !== 2 || row.targetGeneration !== null || context.kind !== 'delegated_context' || !canonicalId(context.id))
|
|
143
|
+
invalid();
|
|
144
|
+
const obs = keys(row.observation, ['status', 'injectionObserved']);
|
|
145
|
+
if (!member(obs.status, ['not_requested', 'unsupported']) || obs.injectionObserved !== false || row.state === 'delivered')
|
|
146
|
+
invalid();
|
|
147
|
+
const ack = row.ack, wait = row.wait;
|
|
148
|
+
if (!ack || !wait || (ack.status === 'acknowledged' && ack.assurance !== 'token_possession') ||
|
|
149
|
+
(wait.for === 'delivered' && !member(wait.status, ['unsupported', 'failed'])) ||
|
|
150
|
+
(Array.isArray(row.nextActions) && row.nextActions.includes('keep_waiting') && (wait.for !== 'acknowledged' || wait.status !== 'pending')))
|
|
151
|
+
invalid();
|
|
152
|
+
// Reuse all unchanged v1 predicates without teaching its strict validator a
|
|
153
|
+
// permissive extension. The synthetic generation is local validation-only;
|
|
154
|
+
// it is never returned, persisted, sent or used as identity evidence.
|
|
155
|
+
const legacy = { ...row, schemaVersion: 1, targetGeneration: 'validation-only-not-native-generation' };
|
|
156
|
+
delete legacy.receiptContext;
|
|
157
|
+
validateHandoff(legacy);
|
|
158
|
+
try {
|
|
159
|
+
if (Buffer.byteLength(JSON.stringify(row), 'utf8') > HANDOFF_LIMITS.publicFrame)
|
|
160
|
+
invalid('receipt_frame_too_large');
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
if (error instanceof Refusal)
|
|
164
|
+
throw error;
|
|
165
|
+
invalid();
|
|
166
|
+
}
|
|
167
|
+
return structuredClone(row);
|
|
168
|
+
}
|
|
169
|
+
export function validateManagedHandoff(value) {
|
|
170
|
+
if (value && typeof value === 'object' && value.schemaVersion === 2)
|
|
171
|
+
return validateHandoffV2(value);
|
|
172
|
+
return validateHandoff(value);
|
|
173
|
+
}
|
|
174
|
+
export function parseManagedHandoff(raw) {
|
|
175
|
+
return validateManagedHandoff(parseStrictJson(raw, { scope: 'handoff' }));
|
|
176
|
+
}
|
|
177
|
+
function handle(value) {
|
|
178
|
+
if (!canonicalId(value.ledgerEpoch) || !canonicalId(value.correlationId) ||
|
|
179
|
+
(value.receiptContextId !== null && !canonicalId(value.receiptContextId)))
|
|
180
|
+
invalid();
|
|
181
|
+
}
|
|
182
|
+
const operations = ['hello', 'prepare', 'mint', 'fence', 'submission', 'status', 'wait-open', 'wait-status', 'wait-stop', 'revoke', 'receipt'];
|
|
183
|
+
export function validateReceiptRequest(value) {
|
|
184
|
+
const row = keys(value, ['ipcVersion', 'requestId', 'collectorContextId', 'op', 'body']);
|
|
185
|
+
if (row.ipcVersion !== 2)
|
|
186
|
+
invalid('receipt_ipc_version_unsupported');
|
|
187
|
+
if (!canonicalId(row.requestId) || !operations.includes(row.op) ||
|
|
188
|
+
(row.op === 'hello' ? row.collectorContextId !== null : !canonicalId(row.collectorContextId)))
|
|
189
|
+
invalid();
|
|
190
|
+
let body;
|
|
191
|
+
switch (row.op) {
|
|
192
|
+
case 'hello':
|
|
193
|
+
keys(row.body, []);
|
|
194
|
+
break;
|
|
195
|
+
case 'prepare': {
|
|
196
|
+
body = keys(row.body, ['binding', 'receiptSchemaVersion', 'receiptScope', 'producerEvidence']);
|
|
197
|
+
const binding = validateBinding(body.binding), producer = validateProducerEvidence(body.producerEvidence);
|
|
198
|
+
validateReceiptPair(binding.agent, body.receiptSchemaVersion, body.receiptScope);
|
|
199
|
+
if (producer.receiptSchemaVersion !== body.receiptSchemaVersion || producer.receiptScope !== body.receiptScope)
|
|
200
|
+
invalid('receipt_producer_mismatch');
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
case 'mint':
|
|
204
|
+
body = keys(row.body, ['ledgerEpoch', 'correlationId', 'receiptContextId', 'producerProtocolVersion']);
|
|
205
|
+
handle(body);
|
|
206
|
+
if (body.producerProtocolVersion !== 1)
|
|
207
|
+
invalid('receipt_producer_mismatch');
|
|
208
|
+
break;
|
|
209
|
+
case 'fence':
|
|
210
|
+
body = keys(row.body, ['ledgerEpoch', 'correlationId', 'receiptContextId', 'binding']);
|
|
211
|
+
handle(body);
|
|
212
|
+
{
|
|
213
|
+
const binding = validateBinding(body.binding);
|
|
214
|
+
if ((binding.agent === 'codex') !== (body.receiptContextId === null))
|
|
215
|
+
invalid();
|
|
216
|
+
}
|
|
217
|
+
break;
|
|
218
|
+
case 'submission':
|
|
219
|
+
body = keys(row.body, ['ledgerEpoch', 'correlationId', 'receiptContextId', 'status']);
|
|
220
|
+
handle(body);
|
|
221
|
+
if (!member(body.status, ['submitted', 'refused', 'unknown']))
|
|
222
|
+
invalid();
|
|
223
|
+
break;
|
|
224
|
+
case 'status':
|
|
225
|
+
case 'revoke':
|
|
226
|
+
body = keys(row.body, ['ledgerEpoch', 'correlationId']);
|
|
227
|
+
if (!canonicalId(body.ledgerEpoch) || !canonicalId(body.correlationId))
|
|
228
|
+
invalid();
|
|
229
|
+
break;
|
|
230
|
+
case 'wait-open':
|
|
231
|
+
body = keys(row.body, ['ledgerEpoch', 'correlationId', 'goal', 'timeoutSeconds', 'clockReferenceMs', 'remainingMs']);
|
|
232
|
+
if (!canonicalId(body.ledgerEpoch) || !canonicalId(body.correlationId) || body.goal !== 'acknowledged' || !safeInteger(body.timeoutSeconds) || body.timeoutSeconds < 1 || body.timeoutSeconds > 60 || !safeInteger(body.clockReferenceMs) || !safeInteger(body.remainingMs) || body.remainingMs < 1 || body.remainingMs > body.timeoutSeconds * 1000)
|
|
233
|
+
invalid();
|
|
234
|
+
break;
|
|
235
|
+
case 'wait-status':
|
|
236
|
+
case 'wait-stop':
|
|
237
|
+
body = keys(row.body, ['ledgerEpoch', 'correlationId', 'operationId']);
|
|
238
|
+
if (!canonicalId(body.ledgerEpoch) || !canonicalId(body.correlationId) || !canonicalId(body.operationId))
|
|
239
|
+
invalid();
|
|
240
|
+
break;
|
|
241
|
+
case 'receipt':
|
|
242
|
+
body = keys(row.body, ['receipt']);
|
|
243
|
+
if (typeof body.receipt !== 'string')
|
|
244
|
+
invalid();
|
|
245
|
+
validateManagedReceipt(body.receipt);
|
|
246
|
+
break;
|
|
247
|
+
default: invalid();
|
|
248
|
+
}
|
|
249
|
+
bounded(row);
|
|
250
|
+
return structuredClone(row);
|
|
251
|
+
}
|
|
252
|
+
function parseFrame(raw, maximum = RECEIPT_LIMITS.frame) {
|
|
253
|
+
const bytes = typeof raw === 'string' ? Buffer.from(raw, 'utf8') : raw;
|
|
254
|
+
if (bytes.byteLength > maximum)
|
|
255
|
+
invalid('receipt_frame_too_large');
|
|
256
|
+
if (!bytes.byteLength || bytes[bytes.byteLength - 1] !== 10 || bytes.subarray(0, -1).includes(10) || bytes.includes(13))
|
|
257
|
+
invalid();
|
|
258
|
+
return parseStrictJson(typeof raw === 'string' ? raw : bytes, { maxBytes: maximum });
|
|
259
|
+
}
|
|
260
|
+
export function parseReceiptRequest(raw) { return validateReceiptRequest(parseFrame(raw)); }
|
|
261
|
+
function matchingHandoff(value, request) {
|
|
262
|
+
const result = validateManagedHandoff(value), body = request.body;
|
|
263
|
+
if ('ledgerEpoch' in body && (result.ledgerEpoch !== body.ledgerEpoch || result.correlationId !== body.correlationId))
|
|
264
|
+
invalid();
|
|
265
|
+
// Handles carry an immutable scope discriminator: null is Codex v1; an
|
|
266
|
+
// original context UUID is Claude v2. IDs alone cannot authorize a cross-scope
|
|
267
|
+
// response, even when both variants are independently well-formed.
|
|
268
|
+
if (Object.hasOwn(body, 'receiptContextId')) {
|
|
269
|
+
if (body.receiptContextId === null) {
|
|
270
|
+
if (result.schemaVersion !== 1)
|
|
271
|
+
invalid();
|
|
272
|
+
}
|
|
273
|
+
else if (result.schemaVersion !== 2 || result.receiptContext.id !== body.receiptContextId)
|
|
274
|
+
invalid();
|
|
275
|
+
}
|
|
276
|
+
if (request.op === 'fence' && result.targetGeneration !== request.body.binding.generation)
|
|
277
|
+
invalid();
|
|
278
|
+
if ((request.op === 'wait-status' || request.op === 'wait-stop') && result.wait.operationId !== body.operationId)
|
|
279
|
+
invalid();
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
export function validateReceiptResponse(value, request) {
|
|
283
|
+
const row = value && typeof value === 'object' ? value : {};
|
|
284
|
+
keys(row, row.ok === true ? ['ipcVersion', 'requestId', 'collectorContextId', 'ok', 'body'] : ['ipcVersion', 'requestId', 'collectorContextId', 'ok', 'error']);
|
|
285
|
+
if (row.ipcVersion !== 2 || row.requestId !== request.requestId || !canonicalId(row.collectorContextId))
|
|
286
|
+
invalid();
|
|
287
|
+
if (request.collectorContextId !== null && row.collectorContextId !== request.collectorContextId)
|
|
288
|
+
invalid('receipt_context_mismatch');
|
|
289
|
+
if (row.ok === false) {
|
|
290
|
+
if (!isReceiptErrorCode(row.error))
|
|
291
|
+
invalid();
|
|
292
|
+
bounded(row);
|
|
293
|
+
return structuredClone(row);
|
|
294
|
+
}
|
|
295
|
+
if (row.ok !== true)
|
|
296
|
+
invalid();
|
|
297
|
+
let body;
|
|
298
|
+
switch (request.op) {
|
|
299
|
+
case 'hello': {
|
|
300
|
+
body = keys(row.body, ['receiptSchemas', 'managedStoreVersion', 'endpointOwner', 'clockReferenceMs']);
|
|
301
|
+
const owner = keys(body.endpointOwner, ['uid', 'pid']);
|
|
302
|
+
if (!safeInteger(body.clockReferenceMs) || body.managedStoreVersion !== 2 || !safeInteger(owner.uid) || !safeInteger(owner.pid) || owner.pid <= 1 || owner.pid > 2147483647 ||
|
|
303
|
+
!Array.isArray(body.receiptSchemas) || body.receiptSchemas.length < 1 || body.receiptSchemas.length > 2)
|
|
304
|
+
invalid();
|
|
305
|
+
const scopes = new Set();
|
|
306
|
+
for (const item of body.receiptSchemas) {
|
|
307
|
+
const pair = keys(item, ['agent', 'receiptSchemaVersion', 'receiptScope']);
|
|
308
|
+
validateReceiptPair(pair.agent, pair.receiptSchemaVersion, pair.receiptScope);
|
|
309
|
+
if (scopes.has(String(pair.receiptScope)))
|
|
310
|
+
invalid();
|
|
311
|
+
scopes.add(String(pair.receiptScope));
|
|
312
|
+
}
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
case 'prepare': {
|
|
316
|
+
body = keys(row.body, ['ledgerEpoch', 'correlationId', 'receiptContextId', 'handoff']);
|
|
317
|
+
handle(body);
|
|
318
|
+
const handoff = validateManagedHandoff(body.handoff), binding = request.body.binding;
|
|
319
|
+
if (handoff.ledgerEpoch !== body.ledgerEpoch || handoff.correlationId !== body.correlationId || handoff.targetGeneration !== binding.generation ||
|
|
320
|
+
(binding.agent === 'codex' ? body.receiptContextId !== null || handoff.schemaVersion !== 1 :
|
|
321
|
+
handoff.schemaVersion !== 2 || handoff.receiptContext.id !== body.receiptContextId))
|
|
322
|
+
invalid();
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
case 'mint':
|
|
326
|
+
body = keys(row.body, ['capability']);
|
|
327
|
+
if (!capability(body.capability))
|
|
328
|
+
invalid();
|
|
329
|
+
break;
|
|
330
|
+
case 'receipt': {
|
|
331
|
+
body = keys(row.body, ['status', 'ledgerEpoch', 'correlationId', 'receiptId', 'handoff']);
|
|
332
|
+
const receipt = validateManagedReceipt(request.body.receipt), handoff = validateManagedHandoff(body.handoff);
|
|
333
|
+
if (!member(body.status, ['awaiting_submission', 'acknowledged']) || body.ledgerEpoch !== receipt.ledgerEpoch ||
|
|
334
|
+
body.correlationId !== receipt.correlationId || body.receiptId !== receipt.receiptId || handoff.ledgerEpoch !== receipt.ledgerEpoch ||
|
|
335
|
+
handoff.correlationId !== receipt.correlationId || (body.status === 'acknowledged') !== (handoff.ack.status === 'acknowledged') ||
|
|
336
|
+
(receipt.schemaVersion === 1 ? handoff.schemaVersion !== 1 || handoff.targetGeneration !== receipt.targetGeneration :
|
|
337
|
+
handoff.schemaVersion !== 2 || handoff.receiptContext.id !== receipt.receiptContextId))
|
|
338
|
+
invalid();
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
default:
|
|
342
|
+
body = keys(row.body, ['handoff']);
|
|
343
|
+
matchingHandoff(body.handoff, request);
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
bounded(row);
|
|
347
|
+
return structuredClone(row);
|
|
348
|
+
}
|
|
349
|
+
export function parseReceiptResponse(raw, request) {
|
|
350
|
+
return validateReceiptResponse(parseFrame(raw), request);
|
|
351
|
+
}
|
|
352
|
+
export function validateProducerProbeRequest(value) {
|
|
353
|
+
const row = keys(value, ['producerProtocolVersion', 'challengeId', 'receiptSchemaVersion', 'receiptScope']);
|
|
354
|
+
if (row.producerProtocolVersion !== 1 || !canonicalId(row.challengeId))
|
|
355
|
+
invalid('receipt_producer_mismatch');
|
|
356
|
+
validateReceiptPair(row.receiptSchemaVersion === 1 ? 'codex' : 'claude', row.receiptSchemaVersion, row.receiptScope);
|
|
357
|
+
bounded(row, RECEIPT_LIMITS.privateFrame);
|
|
358
|
+
return structuredClone(row);
|
|
359
|
+
}
|
|
360
|
+
export function validateProducerProbeResponse(value, request) {
|
|
361
|
+
const row = keys(value, ['producerProtocolVersion', 'challengeId', 'receiptSchemaVersion', 'receiptScope', 'implementation', 'runtimeVersion']);
|
|
362
|
+
if (row.producerProtocolVersion !== request.producerProtocolVersion || row.challengeId !== request.challengeId ||
|
|
363
|
+
row.receiptSchemaVersion !== request.receiptSchemaVersion || row.receiptScope !== request.receiptScope || row.implementation !== 'typescript' ||
|
|
364
|
+
typeof row.runtimeVersion !== 'string' || !/^(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)$/.test(row.runtimeVersion) || row.runtimeVersion.length > 64)
|
|
365
|
+
invalid('receipt_producer_mismatch');
|
|
366
|
+
bounded(row, RECEIPT_LIMITS.privateFrame);
|
|
367
|
+
return structuredClone(row);
|
|
368
|
+
}
|
|
369
|
+
export function parseProducerProbeRequest(raw) {
|
|
370
|
+
return validateProducerProbeRequest(parseFrame(raw, RECEIPT_LIMITS.privateFrame));
|
|
371
|
+
}
|
|
372
|
+
export function parseProducerProbeResponse(raw, request) {
|
|
373
|
+
return validateProducerProbeResponse(parseFrame(raw, RECEIPT_LIMITS.privateFrame), request);
|
|
374
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { nativeQueueOnce } from './app-server.js';
|
|
2
|
+
import { HandoffBudget } from './handoff.js';
|
|
3
|
+
import { run } from './process.js';
|
|
4
|
+
import { receiptEndpoint, receiptExchange } from './receipt-collector.js';
|
|
5
|
+
import { validateReceiptProducer } from './receipt-producer.js';
|
|
6
|
+
import { send, type SendOptions } from './send.js';
|
|
7
|
+
export type ReceiptSendConfig = {
|
|
8
|
+
collector: string;
|
|
9
|
+
requestAck?: boolean;
|
|
10
|
+
waitFor?: 'acknowledged';
|
|
11
|
+
seconds?: number;
|
|
12
|
+
payload?: string;
|
|
13
|
+
budget?: HandoffBudget;
|
|
14
|
+
};
|
|
15
|
+
export type ReceiptSendDependencies = {
|
|
16
|
+
send?: typeof send;
|
|
17
|
+
exchange?: typeof receiptExchange;
|
|
18
|
+
producer?: typeof validateReceiptProducer;
|
|
19
|
+
endpoint?: typeof receiptEndpoint;
|
|
20
|
+
nativeQueue?: typeof nativeQueueOnce;
|
|
21
|
+
run?: typeof run;
|
|
22
|
+
};
|
|
23
|
+
export declare function managedReceiptSend(options: SendOptions, config: ReceiptSendConfig, deps?: ReceiptSendDependencies): Promise<{
|
|
24
|
+
value: Record<string, unknown>;
|
|
25
|
+
exitCode: number;
|
|
26
|
+
}>;
|