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.
Files changed (44) hide show
  1. package/PARITY.md +20 -5
  2. package/README.ja.md +8 -8
  3. package/README.ko.md +8 -8
  4. package/README.md +8 -8
  5. package/README.zh-CN.md +8 -8
  6. package/RELEASING.md +73 -0
  7. package/VALIDATION.md +102 -1
  8. package/dist/cli.js +54 -4
  9. package/dist/diagnostics.js +17 -98
  10. package/dist/discovery.d.ts +32 -0
  11. package/dist/discovery.js +405 -68
  12. package/dist/handoff-operations.js +23 -3
  13. package/dist/help.js +18 -2
  14. package/dist/macos-acl.d.ts +10 -0
  15. package/dist/macos-acl.js +75 -0
  16. package/dist/output.js +6 -1
  17. package/dist/protocol.d.ts +2 -2
  18. package/dist/protocol.js +1 -1
  19. package/dist/receipt-collector.d.ts +12 -0
  20. package/dist/receipt-collector.js +283 -0
  21. package/dist/receipt-command.d.ts +10 -0
  22. package/dist/receipt-command.js +246 -0
  23. package/dist/receipt-producer.d.ts +7 -0
  24. package/dist/receipt-producer.js +107 -0
  25. package/dist/receipt-protocol.d.ts +219 -0
  26. package/dist/receipt-protocol.js +374 -0
  27. package/dist/receipt-send.d.ts +26 -0
  28. package/dist/receipt-send.js +375 -0
  29. package/dist/receipt-state.d.ts +89 -0
  30. package/dist/receipt-state.js +503 -0
  31. package/dist/receipt-store.d.ts +32 -0
  32. package/dist/receipt-store.js +286 -0
  33. package/dist/replies.js +3 -1
  34. package/dist/send.d.ts +5 -2
  35. package/dist/send.js +34 -49
  36. package/dist/skill-metadata.js +4 -3
  37. package/docs/api.md +32 -7
  38. package/docs/design/typescript-skill-compatibility.md +29 -0
  39. package/docs/guide.ja.md +41 -15
  40. package/docs/guide.ko.md +43 -17
  41. package/docs/guide.md +42 -16
  42. package/docs/guide.zh-CN.md +41 -15
  43. package/docs/shorthand.md +1 -1
  44. package/package.json +3 -3
@@ -0,0 +1,7 @@
1
+ import { type ProducerEvidence, type ReceiptPair } from './receipt-protocol.js';
2
+ export declare function validateReceiptProducer(pair: ReceiptPair, options?: {
3
+ timeoutMs?: number;
4
+ }): Promise<{
5
+ evidence: ProducerEvidence;
6
+ revalidate(): void;
7
+ }>;
@@ -0,0 +1,107 @@
1
+ // Local artifact continuity and a private nonce challenge; this is not code authentication.
2
+ import { createHash, randomUUID } from 'node:crypto';
3
+ import { closeSync, constants, fstatSync, lstatSync, openSync, opendirSync, readSync, realpathSync } from 'node:fs';
4
+ import { dirname, join } from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+ import { Refusal } from './discovery.js';
7
+ import { inspectMacosAcls } from './macos-acl.js';
8
+ import { run } from './process.js';
9
+ import { VERSION } from './protocol.js';
10
+ import { parseProducerProbeResponse } from './receipt-protocol.js';
11
+ const fail = () => { throw new Refusal('receipt_producer_mismatch', 1); };
12
+ const identity = (s) => [s.dev, s.ino, s.uid, s.mode, s.size, s.mtimeMs, s.ctimeMs, s.birthtimeMs].join(':');
13
+ export async function validateReceiptProducer(pair, options = {}) {
14
+ const entry = fileURLToPath(new URL('./cli.js', import.meta.url)), directory = dirname(entry), node = realpathSync(process.execPath);
15
+ const pinned = new Map(), hash = createHash('sha256');
16
+ const inspect = (path, directory = false) => {
17
+ const stat = lstatSync(path);
18
+ if (stat.isSymbolicLink() || (directory ? !stat.isDirectory() : !stat.isFile()) ||
19
+ ![0, process.getuid?.()].includes(stat.uid) || (stat.mode & 0o022) !== 0)
20
+ fail();
21
+ return stat;
22
+ };
23
+ try {
24
+ pinned.set(directory, identity(inspect(directory, true)));
25
+ pinned.set(node, identity(inspect(node)));
26
+ const names = [], listing = opendirSync(directory);
27
+ try {
28
+ let count = 0;
29
+ for (;;) {
30
+ const item = listing.readSync();
31
+ if (!item)
32
+ break;
33
+ if (++count > 128)
34
+ fail();
35
+ if (item.name.endsWith('.js'))
36
+ names.push(item.name);
37
+ }
38
+ }
39
+ finally {
40
+ listing.closeSync();
41
+ }
42
+ names.sort();
43
+ if (!names.length || !names.includes('cli.js'))
44
+ fail();
45
+ let total = 0;
46
+ for (const name of names) {
47
+ const path = join(directory, name), stat = inspect(path);
48
+ if (stat.size > 4 * 1024 * 1024 || total + stat.size > 16 * 1024 * 1024)
49
+ fail();
50
+ const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
51
+ try {
52
+ if (identity(fstatSync(fd)) !== identity(stat))
53
+ fail();
54
+ const buffer = Buffer.alloc(stat.size + 1);
55
+ let length = 0;
56
+ while (length < buffer.length) {
57
+ const got = readSync(fd, buffer, length, buffer.length - length, null);
58
+ if (!got)
59
+ break;
60
+ length += got;
61
+ }
62
+ total += length;
63
+ if (length !== stat.size || total > 16 * 1024 * 1024 || identity(fstatSync(fd)) !== identity(stat) || identity(inspect(path)) !== identity(stat))
64
+ fail();
65
+ const bytes = buffer.subarray(0, length);
66
+ hash.update(name).update('\0').update(bytes);
67
+ pinned.set(path, identity(stat));
68
+ }
69
+ finally {
70
+ closeSync(fd);
71
+ }
72
+ }
73
+ const revalidate = () => {
74
+ try {
75
+ for (const [path, value] of pinned)
76
+ if (identity(inspect(path, path === directory)) !== value)
77
+ fail();
78
+ if (process.platform === 'darwin' && inspectMacosAcls([...pinned.keys()]) !== 'clear')
79
+ fail();
80
+ for (const [path, value] of pinned)
81
+ if (identity(inspect(path, path === directory)) !== value)
82
+ fail();
83
+ }
84
+ catch {
85
+ fail();
86
+ }
87
+ };
88
+ revalidate();
89
+ const challenge = { producerProtocolVersion: 1, challengeId: randomUUID(), receiptSchemaVersion: pair.receiptSchemaVersion, receiptScope: pair.receiptScope };
90
+ const timeout = Math.min(2000, Math.floor(options.timeoutMs ?? 2000));
91
+ if (timeout < 1)
92
+ fail();
93
+ const result = await run(node, [entry, 'receipt', 'probe', '--json'], { input: JSON.stringify(challenge) + '\n', timeout, limit: 4096, rawStdout: true,
94
+ env: { PATH: '/usr/bin:/bin', LANG: 'C', LC_ALL: 'C' } });
95
+ if (result.interrupted || result.code !== 0 || !result.spawned || result.stderr.length)
96
+ fail();
97
+ const response = parseProducerProbeResponse(result.stdoutBytes ?? Buffer.from(result.stdout), challenge);
98
+ if (response.runtimeVersion !== VERSION || response.implementation !== 'typescript')
99
+ fail();
100
+ revalidate();
101
+ return { evidence: { protocolVersion: 1, artifactSha256: hash.digest('hex'), challengeId: challenge.challengeId,
102
+ receiptSchemaVersion: pair.receiptSchemaVersion, receiptScope: pair.receiptScope }, revalidate };
103
+ }
104
+ catch {
105
+ return fail();
106
+ }
107
+ }
@@ -0,0 +1,219 @@
1
+ import { type Handoff, type Receipt } from './handoff.js';
2
+ export declare const RECEIPT_LIMITS: Readonly<{
3
+ frame: 16384;
4
+ privateFrame: 4096;
5
+ depth: 16;
6
+ nodes: 1024;
7
+ connections: 16;
8
+ readMs: 2000;
9
+ writeMs: 2000;
10
+ exchangeMs: 5000;
11
+ activeWaits: 128;
12
+ }>;
13
+ export type ReceiptScope = 'original_generation' | 'delegated_context';
14
+ export type ReceiptPair = {
15
+ agent: 'codex';
16
+ receiptSchemaVersion: 1;
17
+ receiptScope: 'original_generation';
18
+ } | {
19
+ agent: 'claude';
20
+ receiptSchemaVersion: 2;
21
+ receiptScope: 'delegated_context';
22
+ };
23
+ export declare const RECEIPT_PAIRS: readonly ReceiptPair[];
24
+ export type ReceiptBinding = {
25
+ agent: 'codex';
26
+ destination: string;
27
+ target: string;
28
+ home: string;
29
+ writerIdentity: string;
30
+ generation: string;
31
+ payloadDigest: string;
32
+ } | {
33
+ agent: 'claude';
34
+ destination: string;
35
+ target: string;
36
+ registryDirectory: string;
37
+ registryRecordPath: string;
38
+ registryIdentity: string;
39
+ socketPath: string;
40
+ routeIdentity: string;
41
+ generation: null;
42
+ payloadDigest: string;
43
+ };
44
+ export type ProducerEvidence = {
45
+ protocolVersion: 1;
46
+ artifactSha256: string;
47
+ challengeId: string;
48
+ receiptSchemaVersion: 1 | 2;
49
+ receiptScope: ReceiptScope;
50
+ };
51
+ export type ReceiptV2 = {
52
+ schemaVersion: 2;
53
+ kind: 'receipt';
54
+ receiptScope: 'delegated_context';
55
+ ledgerEpoch: string;
56
+ correlationId: string;
57
+ receiptContextId: string;
58
+ receiptId: string;
59
+ capability: string;
60
+ };
61
+ export type ManagedReceipt = Receipt | ReceiptV2;
62
+ export type HandoffV2 = Omit<Handoff, 'schemaVersion' | 'targetGeneration'> & {
63
+ schemaVersion: 2;
64
+ targetGeneration: null;
65
+ receiptContext: {
66
+ kind: 'delegated_context';
67
+ id: string;
68
+ };
69
+ };
70
+ export type ManagedHandoff = Handoff | HandoffV2;
71
+ export type ReceiptHandle = {
72
+ ledgerEpoch: string;
73
+ correlationId: string;
74
+ receiptContextId: string | null;
75
+ };
76
+ export type ReceiptRequestBody = {
77
+ hello: Record<string, never>;
78
+ prepare: {
79
+ binding: ReceiptBinding;
80
+ receiptSchemaVersion: 1 | 2;
81
+ receiptScope: ReceiptScope;
82
+ producerEvidence: ProducerEvidence;
83
+ };
84
+ mint: ReceiptHandle & {
85
+ producerProtocolVersion: 1;
86
+ };
87
+ fence: ReceiptHandle & {
88
+ binding: ReceiptBinding;
89
+ };
90
+ submission: ReceiptHandle & {
91
+ status: 'submitted' | 'refused' | 'unknown';
92
+ };
93
+ status: {
94
+ ledgerEpoch: string;
95
+ correlationId: string;
96
+ };
97
+ 'wait-open': {
98
+ ledgerEpoch: string;
99
+ correlationId: string;
100
+ goal: 'acknowledged';
101
+ timeoutSeconds: number;
102
+ clockReferenceMs: number;
103
+ remainingMs: number;
104
+ };
105
+ 'wait-status': {
106
+ ledgerEpoch: string;
107
+ correlationId: string;
108
+ operationId: string;
109
+ };
110
+ 'wait-stop': {
111
+ ledgerEpoch: string;
112
+ correlationId: string;
113
+ operationId: string;
114
+ };
115
+ revoke: {
116
+ ledgerEpoch: string;
117
+ correlationId: string;
118
+ };
119
+ receipt: {
120
+ receipt: string;
121
+ };
122
+ };
123
+ export type ReceiptOperation = keyof ReceiptRequestBody;
124
+ export type ReceiptRequest = {
125
+ [K in ReceiptOperation]: {
126
+ ipcVersion: 2;
127
+ requestId: string;
128
+ collectorContextId: K extends 'hello' ? null : string;
129
+ op: K;
130
+ body: ReceiptRequestBody[K];
131
+ };
132
+ }[ReceiptOperation];
133
+ export type ReceiptResponseBody = {
134
+ hello: {
135
+ clockReferenceMs: number;
136
+ receiptSchemas: ReceiptPair[];
137
+ managedStoreVersion: 2;
138
+ endpointOwner: {
139
+ uid: number;
140
+ pid: number;
141
+ };
142
+ };
143
+ prepare: ReceiptHandle & {
144
+ handoff: ManagedHandoff;
145
+ };
146
+ mint: {
147
+ capability: string;
148
+ };
149
+ fence: {
150
+ handoff: ManagedHandoff;
151
+ };
152
+ submission: {
153
+ handoff: ManagedHandoff;
154
+ };
155
+ status: {
156
+ handoff: ManagedHandoff;
157
+ };
158
+ 'wait-open': {
159
+ handoff: ManagedHandoff;
160
+ };
161
+ 'wait-status': {
162
+ handoff: ManagedHandoff;
163
+ };
164
+ 'wait-stop': {
165
+ handoff: ManagedHandoff;
166
+ };
167
+ revoke: {
168
+ handoff: ManagedHandoff;
169
+ };
170
+ receipt: {
171
+ status: 'awaiting_submission' | 'acknowledged';
172
+ ledgerEpoch: string;
173
+ correlationId: string;
174
+ receiptId: string;
175
+ handoff: ManagedHandoff;
176
+ };
177
+ };
178
+ export type ReceiptResponse = {
179
+ ipcVersion: 2;
180
+ requestId: string;
181
+ collectorContextId: string;
182
+ } & ({
183
+ ok: true;
184
+ body: ReceiptResponseBody[ReceiptOperation];
185
+ } | {
186
+ ok: false;
187
+ error: ReceiptErrorCode;
188
+ });
189
+ export declare const RECEIPT_ERROR_CODES: readonly ["invalid_receipt_protocol", "invalid_handoff_json", "handoff_frame_too_large", "invalid_receipt", "invalid_handoff", "receipt_ipc_version_unsupported", "receipt_schema_unsupported", "receipt_scope_unsupported", "receipt_storage_unsupported", "receipt_storage_untrusted", "receipt_history_unavailable", "receipt_history_changed", "receipt_history_corrupt", "receipt_state_exists", "receipt_collector_busy", "receipt_collector_closed", "receipt_collector_error", "receipt_collector_unavailable", "receipt_ledger_full", "receipt_id_unknown", "receipt_epoch_mismatch", "receipt_binding_conflict", "receipt_scope_mismatch", "receipt_authority_unavailable", "receipt_authority_expired", "receipt_authentication_failed", "receipt_already_committed", "receipt_candidate_conflict", "receipt_already_attempted", "receipt_outcome_not_authorized", "receipt_wait_quota", "receipt_wait_unknown", "receipt_wait_terminal", "receipt_context_mismatch", "receipt_producer_mismatch", "receipt_history_quarantined", "receipt_bootstrap_unsupported", "receipt_managed_context_required", "receipt_endpoint_untrusted", "receipt_endpoint_changed", "receipt_endpoint_exists", "receipt_endpoint_busy", "receipt_transport_unknown", "receipt_frame_too_large", "receipt_transport_timeout", "insufficient_budget", "deadline_before_effect"];
190
+ export type ReceiptErrorCode = typeof RECEIPT_ERROR_CODES[number];
191
+ export declare function isReceiptErrorCode(value: unknown): value is ReceiptErrorCode;
192
+ export declare function publicReceiptError(error: unknown): ReceiptErrorCode;
193
+ export declare function validateReceiptPair(agent: unknown, version: unknown, scope: unknown): ReceiptPair;
194
+ export declare function validateBinding(value: unknown): ReceiptBinding;
195
+ export declare const validateReceiptBinding: typeof validateBinding;
196
+ export declare function validateProducerEvidence(value: unknown): ProducerEvidence;
197
+ export declare function validateReceiptV2(raw: string | Uint8Array): ReceiptV2;
198
+ export declare function validateManagedReceipt(raw: string | Uint8Array): ManagedReceipt;
199
+ export declare function validateHandoffV2(value: unknown): HandoffV2;
200
+ export declare function validateManagedHandoff(value: unknown): ManagedHandoff;
201
+ export declare function parseManagedHandoff(raw: string | Uint8Array): ManagedHandoff;
202
+ export declare function validateReceiptRequest(value: unknown): ReceiptRequest;
203
+ export declare function parseReceiptRequest(raw: string | Uint8Array): ReceiptRequest;
204
+ export declare function validateReceiptResponse(value: unknown, request: ReceiptRequest): ReceiptResponse;
205
+ export declare function parseReceiptResponse(raw: string | Uint8Array, request: ReceiptRequest): ReceiptResponse;
206
+ export type ProducerProbeRequest = {
207
+ producerProtocolVersion: 1;
208
+ challengeId: string;
209
+ receiptSchemaVersion: 1 | 2;
210
+ receiptScope: ReceiptScope;
211
+ };
212
+ export type ProducerProbeResponse = ProducerProbeRequest & {
213
+ implementation: 'typescript';
214
+ runtimeVersion: string;
215
+ };
216
+ export declare function validateProducerProbeRequest(value: unknown): ProducerProbeRequest;
217
+ export declare function validateProducerProbeResponse(value: unknown, request: ProducerProbeRequest): ProducerProbeResponse;
218
+ export declare function parseProducerProbeRequest(raw: string | Uint8Array): ProducerProbeRequest;
219
+ export declare function parseProducerProbeResponse(raw: string | Uint8Array, request: ProducerProbeRequest): ProducerProbeResponse;