session-peer 0.3.6 → 0.5.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/CONTRIBUTING.md +6 -4
- package/PARITY.md +431 -6
- package/README.ja.md +14 -8
- package/README.ko.md +14 -8
- package/README.md +14 -8
- package/README.zh-CN.md +14 -8
- package/RELEASING.md +109 -0
- package/VALIDATION.md +806 -1
- package/dist/app-server.d.ts +207 -0
- package/dist/app-server.js +1044 -0
- package/dist/cli.js +229 -39
- package/dist/handoff-ledger.d.ts +103 -0
- package/dist/handoff-ledger.js +791 -0
- package/dist/handoff-observer-probe.d.ts +1 -0
- package/dist/handoff-observer-probe.js +170 -0
- package/dist/handoff-operations.d.ts +29 -0
- package/dist/handoff-operations.js +496 -0
- package/dist/handoff-writer-probe.d.ts +1 -0
- package/dist/handoff-writer-probe.js +146 -0
- package/dist/handoff.d.ts +112 -0
- package/dist/handoff.js +261 -0
- package/dist/help.js +18 -0
- package/dist/linux-native-probe.d.ts +1 -0
- package/dist/linux-native-probe.js +51 -0
- package/dist/linux-proc.d.ts +55 -0
- package/dist/linux-proc.js +150 -0
- package/dist/metadata-observation.d.ts +49 -0
- package/dist/metadata-observation.js +250 -0
- package/dist/native-profiles.d.ts +25 -0
- package/dist/native-profiles.js +62 -0
- package/dist/output.js +7 -1
- package/dist/process.d.ts +28 -0
- package/dist/process.js +284 -2
- package/dist/protocol.d.ts +2 -2
- package/dist/protocol.js +1 -1
- package/dist/send.d.ts +18 -1
- package/dist/send.js +91 -6
- package/dist/skill-metadata.js +7 -4
- package/dist/ssh-response.d.ts +11 -0
- package/dist/ssh-response.js +78 -2
- package/dist/ssh.d.ts +2 -1
- package/dist/ssh.js +37 -2
- package/dist/windows.d.ts +28 -0
- package/dist/windows.js +149 -1
- package/docs/api.md +255 -4
- package/docs/design/typescript-skill-compatibility.md +55 -2
- package/docs/guide.ja.md +36 -16
- package/docs/guide.ko.md +39 -19
- package/docs/guide.md +46 -20
- package/docs/guide.zh-CN.md +36 -16
- package/docs/shorthand.md +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
export declare const HANDOFF_LIMITS: Readonly<{
|
|
2
|
+
privateFrame: 4096;
|
|
3
|
+
publicFrame: 8192;
|
|
4
|
+
outerFrame: 1048576;
|
|
5
|
+
fanout: 32;
|
|
6
|
+
aggregateFrame: 34603008;
|
|
7
|
+
intents: 10000;
|
|
8
|
+
journal: 33554432;
|
|
9
|
+
waits: 64;
|
|
10
|
+
detailMs: number;
|
|
11
|
+
capabilityMs: 86400000;
|
|
12
|
+
cleanupMs: 5000;
|
|
13
|
+
}>;
|
|
14
|
+
export type WaitFor = 'none' | 'delivered' | 'acknowledged';
|
|
15
|
+
export type WaitStatus = 'not_requested' | 'pending' | 'satisfied' | 'timed_out_unknown' | 'stopped' | 'unsupported' | 'failed';
|
|
16
|
+
export type WaitReason = 'insufficient_budget' | 'deadline_before_effect' | 'evidence_unsupported' | 'evidence_failed' | 'history_unavailable' | 'stopped_by_operator' | 'invalid_handoff';
|
|
17
|
+
export type Handoff = {
|
|
18
|
+
schemaVersion: 1;
|
|
19
|
+
correlationId: string;
|
|
20
|
+
ledgerEpoch: string;
|
|
21
|
+
state: 'validated' | 'refused' | 'submitted' | 'delivered' | 'acknowledged' | 'unknown' | 'timed_out_unknown';
|
|
22
|
+
submission: {
|
|
23
|
+
status: 'not_attempted' | 'submitted' | 'refused' | 'unknown';
|
|
24
|
+
};
|
|
25
|
+
observation: {
|
|
26
|
+
status: 'not_requested' | 'pending' | 'observed' | 'unsupported' | 'failed';
|
|
27
|
+
injectionObserved: boolean;
|
|
28
|
+
clientUserMessageId?: string;
|
|
29
|
+
turn?: {
|
|
30
|
+
id: string;
|
|
31
|
+
status: 'running' | 'completed' | 'failed' | 'interrupted' | 'unknown';
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
ack: {
|
|
35
|
+
status: 'not_requested' | 'pending' | 'acknowledged' | 'unsupported';
|
|
36
|
+
assurance?: 'token_possession' | 'operator_confirmed';
|
|
37
|
+
receivedAtUtcMs?: number;
|
|
38
|
+
late?: boolean;
|
|
39
|
+
};
|
|
40
|
+
wait: {
|
|
41
|
+
for: WaitFor;
|
|
42
|
+
status: WaitStatus;
|
|
43
|
+
operationId?: string;
|
|
44
|
+
deadlineAtUtcMs?: number;
|
|
45
|
+
reason?: WaitReason;
|
|
46
|
+
};
|
|
47
|
+
targetGeneration: string | null;
|
|
48
|
+
decisionOwner: 'sender_operator';
|
|
49
|
+
retry: {
|
|
50
|
+
allowed: false;
|
|
51
|
+
reason: 'receiver_dedup_unavailable';
|
|
52
|
+
};
|
|
53
|
+
nextActions: ('keep_waiting' | 'reconcile' | 'stop_waiting')[];
|
|
54
|
+
};
|
|
55
|
+
export type PublicHandoff = Handoff;
|
|
56
|
+
export type Clock = {
|
|
57
|
+
monotonic: () => number;
|
|
58
|
+
utc: () => number;
|
|
59
|
+
};
|
|
60
|
+
export declare const systemClock: Clock;
|
|
61
|
+
export declare function fail(code?: string, exitCode?: number): never;
|
|
62
|
+
export declare function canonicalId(value: unknown): value is string;
|
|
63
|
+
export declare function safeInteger(value: unknown): value is number;
|
|
64
|
+
export declare function safeId(value: unknown, bytes?: number): value is string;
|
|
65
|
+
export declare function parseWaitTimeout(value: string): number;
|
|
66
|
+
export declare class HandoffBudget {
|
|
67
|
+
readonly seconds: number;
|
|
68
|
+
readonly clock: Clock;
|
|
69
|
+
readonly startedAt: number;
|
|
70
|
+
readonly cutoff: number;
|
|
71
|
+
readonly deadline: number;
|
|
72
|
+
readonly deadlineAtUtcMs: number;
|
|
73
|
+
constructor(seconds?: number, clock?: Clock);
|
|
74
|
+
static fromRemainingMs(remainingMs: number, clock?: Clock, startedAt?: number): HandoffBudget;
|
|
75
|
+
remainingMs(): number;
|
|
76
|
+
observationRemainingMs(): number;
|
|
77
|
+
beforeEffect(): void;
|
|
78
|
+
}
|
|
79
|
+
export declare function parseStrictJson(raw: string | Uint8Array, options?: {
|
|
80
|
+
scope?: 'private' | 'handoff' | 'query' | 'outer';
|
|
81
|
+
maxBytes?: number;
|
|
82
|
+
}): unknown;
|
|
83
|
+
export declare function closed(value: unknown, required: string[], optional?: string[]): Record<string, unknown>;
|
|
84
|
+
export declare function validateHandoff(value: unknown): Handoff;
|
|
85
|
+
export type Receipt = {
|
|
86
|
+
schemaVersion: 1;
|
|
87
|
+
kind: 'receipt';
|
|
88
|
+
ledgerEpoch: string;
|
|
89
|
+
correlationId: string;
|
|
90
|
+
targetGeneration: string;
|
|
91
|
+
receiptId: string;
|
|
92
|
+
capability: string;
|
|
93
|
+
};
|
|
94
|
+
export type Confirmation = {
|
|
95
|
+
schemaVersion: 1;
|
|
96
|
+
ledgerEpoch: string;
|
|
97
|
+
correlationId: string;
|
|
98
|
+
targetGeneration: string;
|
|
99
|
+
confirmed: true;
|
|
100
|
+
};
|
|
101
|
+
export declare function validateHandoffQuery(value: unknown): {
|
|
102
|
+
schemaVersion: 1;
|
|
103
|
+
correlationId: string;
|
|
104
|
+
status: 'unknown';
|
|
105
|
+
context: 'ledger_missing' | 'ledger_corrupt' | 'id_unknown';
|
|
106
|
+
retry: {
|
|
107
|
+
allowed: false;
|
|
108
|
+
reason: 'history_unavailable';
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
export declare function validateReceipt(raw: string | Uint8Array): Receipt;
|
|
112
|
+
export declare function validateConfirmation(raw: string | Uint8Array): Confirmation;
|
package/dist/handoff.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// Pure opt-in protocol helpers. Importing this module performs no filesystem IO.
|
|
2
|
+
import { Refusal } from './discovery.js';
|
|
3
|
+
export const HANDOFF_LIMITS = Object.freeze({ privateFrame: 4096, publicFrame: 8192, outerFrame: 1048576,
|
|
4
|
+
fanout: 32, aggregateFrame: 34603008, intents: 10000, journal: 33554432, waits: 64,
|
|
5
|
+
detailMs: 30 * 86400000, capabilityMs: 86400000, cleanupMs: 5000 });
|
|
6
|
+
export const systemClock = { monotonic: () => performance.now(), utc: () => Date.now() };
|
|
7
|
+
export function fail(code = 'invalid_handoff', exitCode = 1) { throw new Refusal(code, exitCode); }
|
|
8
|
+
export function canonicalId(value) {
|
|
9
|
+
return typeof value === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value);
|
|
10
|
+
}
|
|
11
|
+
export function safeInteger(value) { return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0; }
|
|
12
|
+
export function safeId(value, bytes = 128) {
|
|
13
|
+
return typeof value === 'string' && value.length > 0 && Buffer.byteLength(value, 'utf8') <= bytes &&
|
|
14
|
+
!/[\x00-\x1f\x7f-\x9f]/.test(value) && !/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/u.test(value);
|
|
15
|
+
}
|
|
16
|
+
export function parseWaitTimeout(value) {
|
|
17
|
+
if (!/^(?:[1-9]|[1-5][0-9]|60)$/.test(value))
|
|
18
|
+
fail('invalid_wait_timeout', 2);
|
|
19
|
+
return Number(value);
|
|
20
|
+
}
|
|
21
|
+
export class HandoffBudget {
|
|
22
|
+
seconds;
|
|
23
|
+
clock;
|
|
24
|
+
startedAt;
|
|
25
|
+
cutoff;
|
|
26
|
+
deadline;
|
|
27
|
+
deadlineAtUtcMs;
|
|
28
|
+
constructor(seconds = 30, clock = systemClock) {
|
|
29
|
+
this.seconds = seconds;
|
|
30
|
+
this.clock = clock;
|
|
31
|
+
parseWaitTimeout(String(seconds));
|
|
32
|
+
this.startedAt = clock.monotonic();
|
|
33
|
+
this.deadline = this.startedAt + seconds * 1000;
|
|
34
|
+
this.cutoff = this.deadline - HANDOFF_LIMITS.cleanupMs;
|
|
35
|
+
this.deadlineAtUtcMs = Math.max(0, Math.floor(clock.utc() + Math.max(0, seconds * 1000 - HANDOFF_LIMITS.cleanupMs)));
|
|
36
|
+
}
|
|
37
|
+
// Private SSH frames carry a remaining duration, never a renewed CLI budget
|
|
38
|
+
// or a UTC authorization deadline. Charge frame-reading time to that duration.
|
|
39
|
+
static fromRemainingMs(remainingMs, clock = systemClock, startedAt = clock.monotonic()) {
|
|
40
|
+
if (!safeInteger(remainingMs) || remainingMs < 1 || remainingMs > 60000 || !Number.isFinite(startedAt) || startedAt > clock.monotonic())
|
|
41
|
+
fail('invalid_handoff_budget', 2);
|
|
42
|
+
const budget = new HandoffBudget(Math.ceil(remainingMs / 1000), clock);
|
|
43
|
+
const deadline = startedAt + remainingMs, cutoff = deadline - HANDOFF_LIMITS.cleanupMs;
|
|
44
|
+
Object.assign(budget, { startedAt, deadline, cutoff,
|
|
45
|
+
deadlineAtUtcMs: Math.max(0, Math.floor(clock.utc() + Math.max(0, cutoff - clock.monotonic()))) });
|
|
46
|
+
return budget;
|
|
47
|
+
}
|
|
48
|
+
remainingMs() { return Math.max(0, this.deadline - this.clock.monotonic()); }
|
|
49
|
+
observationRemainingMs() { return Math.max(0, this.cutoff - this.clock.monotonic()); }
|
|
50
|
+
beforeEffect() {
|
|
51
|
+
if (this.seconds <= 5)
|
|
52
|
+
fail('insufficient_budget');
|
|
53
|
+
if (this.clock.monotonic() >= this.cutoff)
|
|
54
|
+
fail('deadline_before_effect');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Recursive token parser keeps integer lexemes before JavaScript can round them.
|
|
58
|
+
// Outer mode restricts new-schema subtrees only; native numeric siblings retain
|
|
59
|
+
// their existing grammar. Duplicate keys/nonfinite numbers always fail closed.
|
|
60
|
+
export function parseStrictJson(raw, options = {}) {
|
|
61
|
+
if (typeof raw === 'string' && /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/u.test(raw))
|
|
62
|
+
fail('invalid_handoff_json');
|
|
63
|
+
const scope = options.scope ?? 'private', bytes = typeof raw === 'string' ? Buffer.from(raw, 'utf8') : raw;
|
|
64
|
+
const maximum = options.maxBytes ?? (scope === 'outer' ? HANDOFF_LIMITS.outerFrame : scope === 'handoff' ? HANDOFF_LIMITS.publicFrame : HANDOFF_LIMITS.privateFrame);
|
|
65
|
+
if (bytes.byteLength > maximum)
|
|
66
|
+
fail('handoff_frame_too_large');
|
|
67
|
+
let text;
|
|
68
|
+
try {
|
|
69
|
+
text = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
fail('invalid_handoff_json');
|
|
73
|
+
}
|
|
74
|
+
let position = 0;
|
|
75
|
+
const space = () => { while (/[\x20\t\r\n]/.test(text[position] ?? 'x'))
|
|
76
|
+
position++; };
|
|
77
|
+
const string = () => {
|
|
78
|
+
const start = position++;
|
|
79
|
+
while (position < text.length) {
|
|
80
|
+
const c = text[position++];
|
|
81
|
+
if (c === '"') {
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(text.slice(start, position));
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
fail('invalid_handoff_json');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (c === '\\')
|
|
90
|
+
position++;
|
|
91
|
+
}
|
|
92
|
+
fail('invalid_handoff_json');
|
|
93
|
+
};
|
|
94
|
+
const value = (strict, depth) => {
|
|
95
|
+
if (depth > 64)
|
|
96
|
+
fail('invalid_handoff_json');
|
|
97
|
+
space();
|
|
98
|
+
const c = text[position];
|
|
99
|
+
if (c === '"')
|
|
100
|
+
return string();
|
|
101
|
+
if (c === '{') {
|
|
102
|
+
position++;
|
|
103
|
+
space();
|
|
104
|
+
const result = Object.create(null), keys = new Set();
|
|
105
|
+
if (text[position] === '}') {
|
|
106
|
+
position++;
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
while (true) {
|
|
110
|
+
space();
|
|
111
|
+
if (text[position] !== '"')
|
|
112
|
+
fail('invalid_handoff_json');
|
|
113
|
+
const key = string();
|
|
114
|
+
if (keys.has(key))
|
|
115
|
+
fail('invalid_handoff_json');
|
|
116
|
+
keys.add(key);
|
|
117
|
+
space();
|
|
118
|
+
if (text[position++] !== ':')
|
|
119
|
+
fail('invalid_handoff_json');
|
|
120
|
+
result[key] = value(strict || (scope === 'outer' && depth === 0 && ['handoff', 'handoffQuery'].includes(key)), depth + 1);
|
|
121
|
+
space();
|
|
122
|
+
const separator = text[position++];
|
|
123
|
+
if (separator === '}')
|
|
124
|
+
break;
|
|
125
|
+
if (separator !== ',')
|
|
126
|
+
fail('invalid_handoff_json');
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
if (c === '[') {
|
|
131
|
+
position++;
|
|
132
|
+
space();
|
|
133
|
+
const result = [];
|
|
134
|
+
if (text[position] === ']') {
|
|
135
|
+
position++;
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
while (true) {
|
|
139
|
+
result.push(value(strict, depth + 1));
|
|
140
|
+
space();
|
|
141
|
+
const separator = text[position++];
|
|
142
|
+
if (separator === ']')
|
|
143
|
+
break;
|
|
144
|
+
if (separator !== ',')
|
|
145
|
+
fail('invalid_handoff_json');
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
for (const [literal, item] of [['true', true], ['false', false], ['null', null]]) {
|
|
150
|
+
if (text.startsWith(literal, position)) {
|
|
151
|
+
position += literal.length;
|
|
152
|
+
return item;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const token = /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/.exec(text.slice(position))?.[0];
|
|
156
|
+
if (!token)
|
|
157
|
+
fail('invalid_handoff_json');
|
|
158
|
+
position += token.length;
|
|
159
|
+
const number = Number(token);
|
|
160
|
+
if (!Number.isFinite(number) || (strict && (!/^(?:0|[1-9][0-9]*)$/.test(token) || !safeInteger(number))))
|
|
161
|
+
fail('invalid_handoff_json');
|
|
162
|
+
return number;
|
|
163
|
+
};
|
|
164
|
+
const result = value(scope !== 'outer', 0);
|
|
165
|
+
space();
|
|
166
|
+
if (position !== text.length)
|
|
167
|
+
fail('invalid_handoff_json');
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
export function closed(value, required, optional = []) {
|
|
171
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
172
|
+
fail();
|
|
173
|
+
const row = value;
|
|
174
|
+
if (required.some(key => !Object.hasOwn(row, key)) || Object.keys(row).some(key => !required.includes(key) && !optional.includes(key)))
|
|
175
|
+
fail();
|
|
176
|
+
return row;
|
|
177
|
+
}
|
|
178
|
+
const member = (value, values) => typeof value === 'string' && values.includes(value);
|
|
179
|
+
export function validateHandoff(value) {
|
|
180
|
+
const row = closed(value, ['schemaVersion', 'correlationId', 'ledgerEpoch', 'state', 'submission', 'observation', 'ack', 'wait', 'targetGeneration', 'decisionOwner', 'retry', 'nextActions']);
|
|
181
|
+
const sub = closed(row.submission, ['status']), obs = closed(row.observation, ['status', 'injectionObserved'], ['clientUserMessageId', 'turn']);
|
|
182
|
+
const ack = closed(row.ack, ['status'], ['assurance', 'receivedAtUtcMs', 'late']);
|
|
183
|
+
const wait = closed(row.wait, ['for', 'status'], ['operationId', 'deadlineAtUtcMs', 'reason']);
|
|
184
|
+
const retry = closed(row.retry, ['allowed', 'reason']);
|
|
185
|
+
if (row.schemaVersion !== 1 || !canonicalId(row.correlationId) || !canonicalId(row.ledgerEpoch) ||
|
|
186
|
+
!member(row.state, ['validated', 'refused', 'submitted', 'delivered', 'acknowledged', 'unknown', 'timed_out_unknown']) ||
|
|
187
|
+
!member(sub.status, ['not_attempted', 'submitted', 'refused', 'unknown']) ||
|
|
188
|
+
!member(obs.status, ['not_requested', 'pending', 'observed', 'unsupported', 'failed']) || typeof obs.injectionObserved !== 'boolean' ||
|
|
189
|
+
!member(ack.status, ['not_requested', 'pending', 'acknowledged', 'unsupported']) ||
|
|
190
|
+
!member(wait.for, ['none', 'delivered', 'acknowledged']) || !member(wait.status, ['not_requested', 'pending', 'satisfied', 'timed_out_unknown', 'stopped', 'unsupported', 'failed']) ||
|
|
191
|
+
(row.targetGeneration !== null && !safeId(row.targetGeneration, 256)) || row.decisionOwner !== 'sender_operator' ||
|
|
192
|
+
retry.allowed !== false || retry.reason !== 'receiver_dedup_unavailable' || !Array.isArray(row.nextActions) ||
|
|
193
|
+
row.nextActions.some(item => !member(item, ['keep_waiting', 'reconcile', 'stop_waiting'])) || new Set(row.nextActions).size !== row.nextActions.length)
|
|
194
|
+
fail();
|
|
195
|
+
if (Object.hasOwn(obs, 'clientUserMessageId') && !safeId(obs.clientUserMessageId))
|
|
196
|
+
fail();
|
|
197
|
+
if (obs.injectionObserved !== (obs.status === 'observed') ||
|
|
198
|
+
(obs.injectionObserved && (!safeId(obs.clientUserMessageId) || row.targetGeneration === null || sub.status !== 'submitted')) ||
|
|
199
|
+
(!obs.injectionObserved && Object.hasOwn(obs, 'clientUserMessageId')))
|
|
200
|
+
fail();
|
|
201
|
+
if (Object.hasOwn(obs, 'turn')) {
|
|
202
|
+
const turn = closed(obs.turn, ['id', 'status']);
|
|
203
|
+
if (!obs.injectionObserved || !safeId(turn.id) || !member(turn.status, ['running', 'completed', 'failed', 'interrupted', 'unknown']))
|
|
204
|
+
fail();
|
|
205
|
+
}
|
|
206
|
+
if (ack.status === 'acknowledged') {
|
|
207
|
+
if (!member(ack.assurance, ['token_possession', 'operator_confirmed']) || !safeInteger(ack.receivedAtUtcMs) || typeof ack.late !== 'boolean' || row.state !== 'acknowledged' || sub.status !== 'submitted' || row.targetGeneration === null)
|
|
208
|
+
fail();
|
|
209
|
+
}
|
|
210
|
+
else if (['assurance', 'receivedAtUtcMs', 'late'].some(key => Object.hasOwn(ack, key)))
|
|
211
|
+
fail();
|
|
212
|
+
if (row.targetGeneration === null && (ack.status === 'pending' || ack.status === 'acknowledged' || wait.status === 'pending' || wait.status === 'satisfied'))
|
|
213
|
+
fail();
|
|
214
|
+
const states = { validated: 'not_attempted', refused: 'refused', submitted: 'submitted', delivered: 'submitted', acknowledged: 'submitted', unknown: 'unknown' };
|
|
215
|
+
if ((row.state !== 'timed_out_unknown' && sub.status !== states[row.state]) ||
|
|
216
|
+
(row.state === 'timed_out_unknown' && !['submitted', 'unknown'].includes(sub.status)) ||
|
|
217
|
+
(row.state === 'delivered' && !obs.injectionObserved) || (row.state === 'acknowledged' && ack.status !== 'acknowledged'))
|
|
218
|
+
fail();
|
|
219
|
+
if (wait.for === 'none') {
|
|
220
|
+
if (wait.status !== 'not_requested' || Object.keys(wait).length !== 2)
|
|
221
|
+
fail();
|
|
222
|
+
}
|
|
223
|
+
else if (wait.status === 'not_requested' || !canonicalId(wait.operationId) || !safeInteger(wait.deadlineAtUtcMs))
|
|
224
|
+
fail();
|
|
225
|
+
if (Object.hasOwn(wait, 'reason') && !member(wait.reason, ['insufficient_budget', 'deadline_before_effect', 'evidence_unsupported', 'evidence_failed', 'history_unavailable', 'stopped_by_operator', 'invalid_handoff']))
|
|
226
|
+
fail();
|
|
227
|
+
if (wait.status === 'stopped' && wait.reason !== 'stopped_by_operator')
|
|
228
|
+
fail();
|
|
229
|
+
if (wait.status === 'satisfied' && (wait.for === 'delivered' ? !obs.injectionObserved : ack.status !== 'acknowledged'))
|
|
230
|
+
fail();
|
|
231
|
+
if (row.nextActions.includes('keep_waiting') && (row.targetGeneration === null || wait.status === 'unsupported' ||
|
|
232
|
+
(wait.for === 'delivered' ? obs.status !== 'pending' : wait.for !== 'acknowledged' || ack.status !== 'pending') ||
|
|
233
|
+
!['submitted', 'unknown'].includes(sub.status)))
|
|
234
|
+
fail();
|
|
235
|
+
if (row.nextActions.includes('stop_waiting') && wait.status !== 'pending')
|
|
236
|
+
fail();
|
|
237
|
+
if (Buffer.byteLength(JSON.stringify(row), 'utf8') > HANDOFF_LIMITS.publicFrame)
|
|
238
|
+
fail('handoff_frame_too_large');
|
|
239
|
+
return structuredClone(row);
|
|
240
|
+
}
|
|
241
|
+
export function validateHandoffQuery(value) {
|
|
242
|
+
const row = closed(value, ['schemaVersion', 'correlationId', 'status', 'context', 'retry']);
|
|
243
|
+
const retry = closed(row.retry, ['allowed', 'reason']);
|
|
244
|
+
if (row.schemaVersion !== 1 || !canonicalId(row.correlationId) || row.status !== 'unknown' ||
|
|
245
|
+
!member(row.context, ['ledger_missing', 'ledger_corrupt', 'id_unknown']) || retry.allowed !== false || retry.reason !== 'history_unavailable')
|
|
246
|
+
fail();
|
|
247
|
+
return structuredClone(row);
|
|
248
|
+
}
|
|
249
|
+
export function validateReceipt(raw) {
|
|
250
|
+
const row = closed(parseStrictJson(raw), ['schemaVersion', 'kind', 'ledgerEpoch', 'correlationId', 'targetGeneration', 'receiptId', 'capability']);
|
|
251
|
+
if (row.schemaVersion !== 1 || row.kind !== 'receipt' || !canonicalId(row.ledgerEpoch) || !canonicalId(row.correlationId) || !canonicalId(row.receiptId) || !safeId(row.targetGeneration, 256) ||
|
|
252
|
+
typeof row.capability !== 'string' || !/^[A-Za-z0-9_-]{43}$/.test(row.capability) || Buffer.from(row.capability, 'base64url').length !== 32 || Buffer.from(row.capability, 'base64url').toString('base64url') !== row.capability)
|
|
253
|
+
fail('invalid_receipt');
|
|
254
|
+
return row;
|
|
255
|
+
}
|
|
256
|
+
export function validateConfirmation(raw) {
|
|
257
|
+
const row = closed(parseStrictJson(raw), ['schemaVersion', 'ledgerEpoch', 'correlationId', 'targetGeneration', 'confirmed']);
|
|
258
|
+
if (row.schemaVersion !== 1 || !canonicalId(row.ledgerEpoch) || !canonicalId(row.correlationId) || !safeId(row.targetGeneration, 256) || row.confirmed !== true)
|
|
259
|
+
fail('invalid_confirmation');
|
|
260
|
+
return row;
|
|
261
|
+
}
|
package/dist/help.js
CHANGED
|
@@ -17,6 +17,17 @@ Transport:
|
|
|
17
17
|
known online peers use their MagicDNS name, known offline peers are refused.
|
|
18
18
|
--no-update-notice Suppress the opt-in cached update notice
|
|
19
19
|
-h, --help Show command help`;
|
|
20
|
+
if (command === 'handoff' || command === 'ack')
|
|
21
|
+
return `Usage: session-peer handoff init|prepare|status|wait|confirm --json
|
|
22
|
+
init Initialize private POSIX sender ledger explicitly
|
|
23
|
+
prepare --to TARGET --message-file FILE Reserve a fresh ID without submitting (FILE mode 0600)
|
|
24
|
+
status --correlation-id UUID Inspect retained evidence; never submit again
|
|
25
|
+
wait --correlation-id UUID --wait-for delivered|acknowledged --wait-timeout SECONDS
|
|
26
|
+
confirm --receipt - Read private operator-confirmation JSON from stdin
|
|
27
|
+
ack --receipt - Record a qualified private receipt; never prompt an agent
|
|
28
|
+
SESSION_PEER_HANDOFF_HOME selects an absolute private ledger directory.
|
|
29
|
+
Production automated receipt bootstrap and Windows ledger are unsupported.
|
|
30
|
+
Missing history never authorizes retry. Secrets must never be passed through argv.`;
|
|
20
31
|
if (command === 'list')
|
|
21
32
|
return `Usage: session-peer list [options]
|
|
22
33
|
List reachable Claude and saved Codex sessions. Listing is not send authorization.
|
|
@@ -51,6 +62,12 @@ Notices on other commands are opt-in: SESSION_PEER_UPDATE_NOTICE=1.`;
|
|
|
51
62
|
TARGET: Claude name/PID, claude:NAME/PID, codex:UUID, or session-peer reply URI.
|
|
52
63
|
Omit the body or use - for UTF-8 stdin. Only one body source is permitted.
|
|
53
64
|
Unicode names use exact Unicode 14.0.0 default casefold; collisions require PID.
|
|
65
|
+
--correlation-id UUID Use an already-prepared private handoff ID (opt-in)
|
|
66
|
+
--request-ack Add handoff metadata; automated ACK currently unsupported
|
|
67
|
+
--observe-delivery Request optional body-free observation (qualified routes only)
|
|
68
|
+
--wait-for delivered|acknowledged Refuse before submission if required evidence is unsupported
|
|
69
|
+
--wait-timeout SECONDS Total budget 1..60 (default 30, includes 5 s cleanup)
|
|
70
|
+
--message-file FILE Private UTF-8 input file (POSIX mode 0600)
|
|
54
71
|
--dry-run Validate without submission
|
|
55
72
|
--codex-home HOME Explicit Codex home; otherwise select unique live writer
|
|
56
73
|
--codex-bin PATH Native Codex executable
|
|
@@ -70,6 +87,7 @@ Usage: session-peer <command> [options]
|
|
|
70
87
|
send Submit one guarded message
|
|
71
88
|
doctor Inspect read-only metadata and capabilities
|
|
72
89
|
update --check Check npm for a newer version (no self-update)
|
|
90
|
+
handoff / ack Opt-in private evidence operations (see handoff --help)
|
|
73
91
|
--version Print exact runtime version
|
|
74
92
|
--help, -h Show help; <command> --help for details
|
|
75
93
|
${common}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Owned one-shot worker for synchronous native-profile I/O: the complete
|
|
2
|
+
// executable digest and the Linux owned-child procfs/canonical-path binding.
|
|
3
|
+
// The caller runs it with process.run() under the ORIGINAL remaining budget;
|
|
4
|
+
// at that deadline it requests termination of the worker's process group and
|
|
5
|
+
// the caller refuses the effect. Actual reaping of a process blocked in
|
|
6
|
+
// uninterruptible kernel I/O may occur later, when the kernel returns; the
|
|
7
|
+
// caller's effect decision never waits on it beyond its own deadline check. Output is closed JSON returned only to the
|
|
8
|
+
// caller (opened paths stay in memory, never in CLI output or logs).
|
|
9
|
+
import { createHash } from 'node:crypto';
|
|
10
|
+
import { closeSync, openSync, readSync, realpathSync, statSync } from 'node:fs';
|
|
11
|
+
import { isAbsolute } from 'node:path';
|
|
12
|
+
import { nativeExecutable, procOwnedFiles } from './linux-proc.js';
|
|
13
|
+
const integer = (value) => /^[1-9]\d{0,9}$/.test(value ?? '') ? Number(value) : NaN;
|
|
14
|
+
function digest(path, platform, arch, maxBytes) {
|
|
15
|
+
const fd = openSync(path, 'r');
|
|
16
|
+
try {
|
|
17
|
+
const header = Buffer.alloc(20);
|
|
18
|
+
if (readSync(fd, header, 0, 20, 0) !== 20 || !nativeExecutable(header, platform, arch))
|
|
19
|
+
throw new Error('format');
|
|
20
|
+
const hash = createHash('sha256'), chunk = Buffer.alloc(1024 * 1024);
|
|
21
|
+
let total = 0;
|
|
22
|
+
for (let read; (read = readSync(fd, chunk, 0, chunk.length, null)) > 0;) {
|
|
23
|
+
total += read;
|
|
24
|
+
if (total > maxBytes)
|
|
25
|
+
throw new Error('size');
|
|
26
|
+
hash.update(chunk.subarray(0, read));
|
|
27
|
+
}
|
|
28
|
+
return hash.digest('hex');
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
closeSync(fd);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function main(args) {
|
|
35
|
+
try {
|
|
36
|
+
if (args[0] === 'digest' && args.length === 5 && isAbsolute(args[1])) {
|
|
37
|
+
return { ok: true, sha256: digest(args[1], args[2], args[3], integer(args[4])) };
|
|
38
|
+
}
|
|
39
|
+
if (args[0] === 'bind' && args.length === 5 && isAbsolute(args[4])) {
|
|
40
|
+
const [pid, parent, uid] = [integer(args[1]), integer(args[2]), Number(args[3])];
|
|
41
|
+
if (!Number.isSafeInteger(uid) || uid < 0 || uid !== process.getuid?.())
|
|
42
|
+
throw new Error('uid');
|
|
43
|
+
const exe = statSync(realpathSync(args[4]), { bigint: true });
|
|
44
|
+
const files = procOwnedFiles(pid, parent, uid, { dev: exe.dev, ino: exe.ino });
|
|
45
|
+
return { ok: true, files: files.map(file => ({ name: file.name, dev: file.dev.toString(), ino: file.ino.toString(), deleted: file.deleted })) };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch { /* Closed refusal below; no path or native diagnostic is returned. */ }
|
|
49
|
+
return { ok: false };
|
|
50
|
+
}
|
|
51
|
+
process.stdout.write(JSON.stringify(main(process.argv.slice(2))) + '\n');
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export type ProcStat = {
|
|
2
|
+
dev: bigint;
|
|
3
|
+
ino: bigint;
|
|
4
|
+
};
|
|
5
|
+
export type ProcReader = {
|
|
6
|
+
readFile(path: string): string;
|
|
7
|
+
readdir(path: string): string[];
|
|
8
|
+
readlink(path: string): string;
|
|
9
|
+
stat(path: string): ProcStat;
|
|
10
|
+
};
|
|
11
|
+
export declare const procfs: ProcReader;
|
|
12
|
+
export declare class ProcRefusal extends Error {
|
|
13
|
+
}
|
|
14
|
+
export declare function parseProcStat(text: string): {
|
|
15
|
+
state: string;
|
|
16
|
+
ppid: number;
|
|
17
|
+
pgrp: number;
|
|
18
|
+
starttime: string;
|
|
19
|
+
};
|
|
20
|
+
export declare function parseProcStatusUid(text: string): number;
|
|
21
|
+
export declare function devParts(dev: bigint): {
|
|
22
|
+
major: bigint;
|
|
23
|
+
minor: bigint;
|
|
24
|
+
};
|
|
25
|
+
export type ProcLock = {
|
|
26
|
+
blocked: boolean;
|
|
27
|
+
kind: string;
|
|
28
|
+
access: string;
|
|
29
|
+
pid: number;
|
|
30
|
+
major: bigint;
|
|
31
|
+
minor: bigint;
|
|
32
|
+
ino: bigint;
|
|
33
|
+
};
|
|
34
|
+
export declare function procLockHolders(text: string, target: ProcStat): ProcLock[];
|
|
35
|
+
export declare function procFlockOwner(path: string, reader?: ProcReader): number;
|
|
36
|
+
export type ProcOwner = {
|
|
37
|
+
pid: number;
|
|
38
|
+
ppid: number;
|
|
39
|
+
pgrp: number;
|
|
40
|
+
state: string;
|
|
41
|
+
starttime: string;
|
|
42
|
+
uid: number;
|
|
43
|
+
exe?: ProcStat;
|
|
44
|
+
};
|
|
45
|
+
export declare function procOwner(pid: number, reader?: ProcReader, executable?: boolean): ProcOwner;
|
|
46
|
+
export declare const sameOwner: (left: ProcOwner, right: ProcOwner) => boolean;
|
|
47
|
+
export type OpenedFile = {
|
|
48
|
+
name: string;
|
|
49
|
+
dev: bigint;
|
|
50
|
+
ino: bigint;
|
|
51
|
+
deleted: boolean;
|
|
52
|
+
};
|
|
53
|
+
export declare function procOpenedFiles(pid: number, reader?: ProcReader, limit?: number): OpenedFile[];
|
|
54
|
+
export declare function procOwnedFiles(pid: number, parentPid: number, uid: number, executable: ProcStat, reader?: ProcReader): OpenedFile[];
|
|
55
|
+
export declare function nativeExecutable(header: Buffer, platform: string, arch: string): boolean;
|