@zieglar/pi-chat-exchange 0.2.2 → 0.3.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/README.md +11 -7
- package/dist/bin/mcp-stdio.js +124 -56
- package/dist/control/mcp/endpoint-discovery.js +60 -0
- package/dist/control/mcp/messaging-tools.js +12 -2
- package/dist/control/mcp/server.js +10 -2
- package/dist/hooks/config.js +106 -0
- package/dist/hooks/events.js +19 -0
- package/dist/hooks/runner.js +187 -0
- package/dist/host/bound-endpoint.js +19 -6
- package/dist/host/endpoint-host.js +92 -4
- package/dist/host/mcp-binding-config.js +41 -5
- package/dist/runtime/native-readiness.js +8 -5
- package/dist/storage/creation.js +16 -0
- package/dist/storage/ownership.js +45 -1
- package/dist/tunnel/background-authorization.js +343 -0
- package/dist/tunnel/background-manager.js +104 -8
- package/dist/tunnel/background-scope.js +69 -0
- package/dist/tunnel/codes.js +1 -1
- package/dist/tunnel/hooks-management-ui.js +225 -0
- package/dist/tunnel/hooks-management.js +431 -0
- package/dist/tunnel/live-adoption-observation.js +136 -0
- package/dist/tunnel/live-adoption-probe.js +240 -0
- package/dist/tunnel/live-connection-status.js +136 -0
- package/dist/tunnel/live-mcp-composition.js +143 -0
- package/dist/tunnel/live-process-observation.js +98 -0
- package/dist/tunnel/live-session-migration.js +169 -0
- package/dist/tunnel/live-session-policy.js +578 -0
- package/dist/tunnel/manager-panel.js +4 -2
- package/dist/tunnel/manager-ui.js +71 -15
- package/dist/tunnel/multi-session-admission.js +1 -1
- package/dist/tunnel/multi-session-policy.js +45 -13
- package/dist/tunnel/setup-ui.js +2 -4
- package/dist/tunnel/status-ui.js +34 -0
- package/package.json +4 -1
- package/pi-extension/allowlist.example.json +12 -1
- package/pi-extension/desktop-target.ts +157 -0
- package/pi-extension/index.ts +1128 -132
- package/pi-extension/live-session-profile.ts +49 -0
- package/pi-extension/live-session-runtime.ts +59 -0
- package/pi-extension/multi-session-commands.ts +114 -3
- package/pi-extension/multi-session-runtime.ts +63 -11
- package/pi-extension/native-inbound.ts +235 -0
- package/pi-extension/ownership-feedback.ts +100 -0
- package/pi-extension/session-onboarding.ts +154 -0
- package/pi-extension/session-recovery.ts +214 -0
- package/skills/ce-hook-script/SKILL.md +62 -0
- package/skills/ce-hook-script/examples/file-notification.py +79 -0
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
/** Private offline/live configuration history. No Store, Owner or transport authority. */
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
4
|
+
import { execFileSync } from "node:child_process";
|
|
5
|
+
import { dirname, isAbsolute, join, normalize } from "node:path";
|
|
6
|
+
import { isDeepStrictEqual } from "node:util";
|
|
7
|
+
import { parseMultiSessionManifest } from "./multi-session-policy.js";
|
|
8
|
+
import { readPrivateMessagingFile } from "./messaging-config.js";
|
|
9
|
+
import { TunnelRefusal } from "./codes.js";
|
|
10
|
+
import { parseHookRules } from "../hooks/config.js";
|
|
11
|
+
const KIND = "ce-live-session-v1";
|
|
12
|
+
const fail = () => { throw new TunnelRefusal("CE_TUNNEL_PROFILE_INVALID", "messaging.livePolicy"); };
|
|
13
|
+
const conflict = () => { throw new TunnelRefusal("CE_TUNNEL_CONFIG_CONFLICT", "messaging.livePolicy"); };
|
|
14
|
+
const hash = (s) => createHash("sha256").update(s).digest("hex");
|
|
15
|
+
const json = (v) => JSON.stringify(v);
|
|
16
|
+
const digest = (v) => hash(json(v));
|
|
17
|
+
function closed(v, keys) {
|
|
18
|
+
if (v === null || typeof v !== "object" || Array.isArray(v) || ![Object.prototype, null].includes(Object.getPrototypeOf(v)))
|
|
19
|
+
return fail();
|
|
20
|
+
const own = Reflect.ownKeys(v);
|
|
21
|
+
if (own.length !== keys.length || own.some(k => typeof k !== "string" || !keys.includes(k)))
|
|
22
|
+
return fail();
|
|
23
|
+
for (const k of keys) {
|
|
24
|
+
const d = Object.getOwnPropertyDescriptor(v, k);
|
|
25
|
+
if (!d || !("value" in d) || !d.enumerable)
|
|
26
|
+
return fail();
|
|
27
|
+
}
|
|
28
|
+
return v;
|
|
29
|
+
}
|
|
30
|
+
function label(v) { if (typeof v !== "string" || !v.length || v.length > 256 || /[\u0000-\u001f\u007f]|cap_[0-9a-f]{16,}/i.test(v))
|
|
31
|
+
return fail(); return v; }
|
|
32
|
+
function sha(v) { if (typeof v !== "string" || !/^[a-f0-9]{64}$/.test(v))
|
|
33
|
+
return fail(); return v; }
|
|
34
|
+
function integer(v) { if (typeof v !== "number" || !Number.isSafeInteger(v) || v < 0 || v > 65536)
|
|
35
|
+
return fail(); return v; }
|
|
36
|
+
function acl(path) {
|
|
37
|
+
if (process.platform !== "darwin")
|
|
38
|
+
return;
|
|
39
|
+
const output = execFileSync("/bin/ls", ["-lde", path], { encoding: "utf8", timeout: 5000, maxBuffer: 16384, env: { PATH: "/usr/bin:/bin", LC_ALL: "C" }, stdio: ["ignore", "pipe", "pipe"] });
|
|
40
|
+
const [header = "", ...lines] = output.trimEnd().split("\n");
|
|
41
|
+
if (!/^[bcdlps-][rwxStTs-]{9}[+@ ]/.test(header) || lines.some(l => !/^\s*\d+: \S+ deny [a-z_,]+$/.test(l)) || header[10] === "+" && !lines.length)
|
|
42
|
+
fail();
|
|
43
|
+
}
|
|
44
|
+
function directory(root) {
|
|
45
|
+
if (!isAbsolute(root) || normalize(root) !== root || root === "/" || fs.realpathSync(root) !== root)
|
|
46
|
+
fail();
|
|
47
|
+
for (let p = root;; p = dirname(p)) {
|
|
48
|
+
const s = fs.lstatSync(p);
|
|
49
|
+
const sticky = s.uid === 0 && (s.mode & 0o1000) !== 0;
|
|
50
|
+
if (!s.isDirectory() || s.isSymbolicLink() || s.uid !== 0 && s.uid !== process.geteuid?.() || !sticky && (s.mode & 0o022) !== 0)
|
|
51
|
+
fail();
|
|
52
|
+
if (p === root && (s.uid !== process.geteuid?.() || (s.mode & 0o7777) !== 0o700))
|
|
53
|
+
fail();
|
|
54
|
+
acl(p);
|
|
55
|
+
if (dirname(p) === p)
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function reference(raw) {
|
|
60
|
+
const v2 = raw.kind === "ce-live-session-v2";
|
|
61
|
+
const r = closed(raw, ["kind", "root", "policyId", "genesisDigest", ...(v2 ? ["profilePath", "upgradeSequence"] : [])]);
|
|
62
|
+
if (r.kind !== KIND && !v2 || typeof r.root !== "string")
|
|
63
|
+
return fail();
|
|
64
|
+
if (v2 && (integer(r.upgradeSequence) < 1 || typeof r.profilePath !== "string" || !isAbsolute(r.profilePath) || normalize(r.profilePath) !== r.profilePath))
|
|
65
|
+
return fail();
|
|
66
|
+
directory(r.root);
|
|
67
|
+
label(r.policyId);
|
|
68
|
+
sha(r.genesisDigest);
|
|
69
|
+
return Object.freeze({ ...raw });
|
|
70
|
+
}
|
|
71
|
+
function read(path, credentialFile) {
|
|
72
|
+
const data = readPrivateMessagingFile(path, credentialFile);
|
|
73
|
+
if (data)
|
|
74
|
+
acl(path);
|
|
75
|
+
return data?.bytes;
|
|
76
|
+
}
|
|
77
|
+
function parseManifest(raw, sequence, v2 = false) {
|
|
78
|
+
if (raw === null || typeof raw !== "object")
|
|
79
|
+
return fail();
|
|
80
|
+
const r = raw;
|
|
81
|
+
if (!Array.isArray(r.registrations))
|
|
82
|
+
return fail();
|
|
83
|
+
const extras = r.registrations.map((entry) => {
|
|
84
|
+
if (entry === null || typeof entry !== "object")
|
|
85
|
+
return fail();
|
|
86
|
+
const e = entry;
|
|
87
|
+
closed(e, ["endpointId", "runtimeSessionId", "epoch", "status", "payloadRead", "ack", "payloadReadGeneration", "ackGeneration", "presenceDisclosure", ...(Object.hasOwn(e, "hooks") ? ["hooks"] : []), ...(v2 ? ["hookGeneration"] : [])]);
|
|
88
|
+
if (typeof e.presenceDisclosure !== "boolean" || integer(e.payloadReadGeneration) > sequence || integer(e.ackGeneration) > sequence)
|
|
89
|
+
return fail();
|
|
90
|
+
const { payloadReadGeneration, ackGeneration, presenceDisclosure, hookGeneration, ...base } = e;
|
|
91
|
+
if (v2 && integer(hookGeneration) > sequence)
|
|
92
|
+
return fail();
|
|
93
|
+
return { base, payloadReadGeneration: payloadReadGeneration, ackGeneration: ackGeneration, presenceDisclosure, ...(v2 ? { hookGeneration: hookGeneration } : {}) };
|
|
94
|
+
});
|
|
95
|
+
const { mcpHookConsent, ...baseRaw } = r;
|
|
96
|
+
if (!v2 && Object.hasOwn(r, "mcpHookConsent"))
|
|
97
|
+
return fail();
|
|
98
|
+
const consent = v2 ? parseConsent(mcpHookConsent, sequence) : undefined;
|
|
99
|
+
const base = parseMultiSessionManifest({ ...baseRaw, registrations: extras.map(e => e.base) });
|
|
100
|
+
return Object.freeze({ ...base, ...(consent ? { mcpHookConsent: consent } : {}), registrations: Object.freeze(base.registrations.map((e, i) => Object.freeze({ ...e,
|
|
101
|
+
...(v2 ? { hookGeneration: extras[i].hookGeneration } : {}),
|
|
102
|
+
payloadReadGeneration: extras[i].payloadReadGeneration, ackGeneration: extras[i].ackGeneration, presenceDisclosure: extras[i].presenceDisclosure }))) });
|
|
103
|
+
}
|
|
104
|
+
function registration(raw) {
|
|
105
|
+
const r = closed(raw, ["endpointId", "runtimeSessionId", "payloadRead", "ack", "presenceDisclosure"]);
|
|
106
|
+
if (typeof r.payloadRead !== "boolean" || typeof r.ack !== "boolean" || typeof r.presenceDisclosure !== "boolean")
|
|
107
|
+
return fail();
|
|
108
|
+
return Object.freeze({ endpointId: label(r.endpointId), runtimeSessionId: label(r.runtimeSessionId), payloadRead: r.payloadRead, ack: r.ack, presenceDisclosure: r.presenceDisclosure });
|
|
109
|
+
}
|
|
110
|
+
function targets(raw) {
|
|
111
|
+
if (!Array.isArray(raw) || raw.length > 64)
|
|
112
|
+
return fail();
|
|
113
|
+
const ids = new Set();
|
|
114
|
+
return Object.freeze(raw.map(value => {
|
|
115
|
+
const r = closed(value, ["ruleId", "endpointId", "runtimeSessionId", "epoch"]);
|
|
116
|
+
// Rule IDs follow the Hook grammar; identity fields retain the credential guard.
|
|
117
|
+
const ruleId = r.ruleId;
|
|
118
|
+
if (typeof ruleId !== "string" || ruleId.length > 64 || !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(ruleId) || ids.has(ruleId) || integer(r.epoch) < 1)
|
|
119
|
+
return fail();
|
|
120
|
+
ids.add(ruleId);
|
|
121
|
+
return Object.freeze({ ruleId, endpointId: label(r.endpointId), runtimeSessionId: label(r.runtimeSessionId), epoch: r.epoch });
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
function parseConsent(raw, seq) {
|
|
125
|
+
const r = closed(raw, ["generation", "bindingGeneration", "targets"]);
|
|
126
|
+
const t = targets(r.targets);
|
|
127
|
+
if (integer(r.generation) > seq || r.bindingGeneration === null && t.length)
|
|
128
|
+
return fail();
|
|
129
|
+
return Object.freeze({ generation: r.generation, bindingGeneration: r.bindingGeneration === null ? null : sha(r.bindingGeneration), targets: t });
|
|
130
|
+
}
|
|
131
|
+
function intent(raw, v2 = false) {
|
|
132
|
+
const k = raw?.kind;
|
|
133
|
+
if (k === "genesis") {
|
|
134
|
+
closed(raw, ["kind"]);
|
|
135
|
+
return { kind: k };
|
|
136
|
+
}
|
|
137
|
+
if (k === "register" || k === "reauthorize") {
|
|
138
|
+
const r = closed(raw, ["kind", "registration"]);
|
|
139
|
+
return { kind: k, registration: registration(r.registration) };
|
|
140
|
+
}
|
|
141
|
+
if (k === "revoke") {
|
|
142
|
+
const r = closed(raw, ["kind", "endpointId"]);
|
|
143
|
+
return { kind: k, endpointId: label(r.endpointId) };
|
|
144
|
+
}
|
|
145
|
+
if (v2 && k === "upgrade") {
|
|
146
|
+
const r = closed(raw, ["kind", "actorRuntimeSessionId", "profileRevision", "bindingGeneration"]);
|
|
147
|
+
return { kind: k, actorRuntimeSessionId: label(r.actorRuntimeSessionId), profileRevision: sha(r.profileRevision), bindingGeneration: sha(r.bindingGeneration) };
|
|
148
|
+
}
|
|
149
|
+
if (v2 && k === "set-pi-hooks") {
|
|
150
|
+
const r = closed(raw, ["kind", "runtimeSessionId", "endpointId", "epoch", "hooks"]);
|
|
151
|
+
if (integer(r.epoch) < 1 || !Array.isArray(r.hooks))
|
|
152
|
+
return fail();
|
|
153
|
+
let hooks;
|
|
154
|
+
try {
|
|
155
|
+
hooks = parseHookRules(r.hooks);
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
return fail();
|
|
159
|
+
}
|
|
160
|
+
return { kind: k, runtimeSessionId: label(r.runtimeSessionId), endpointId: label(r.endpointId), epoch: r.epoch, hooks };
|
|
161
|
+
}
|
|
162
|
+
if (v2 && k === "set-mcp-hooks") {
|
|
163
|
+
const r = closed(raw, ["kind", "actorRuntimeSessionId", "bindingGeneration", "targets"]);
|
|
164
|
+
return { kind: k, actorRuntimeSessionId: label(r.actorRuntimeSessionId), bindingGeneration: sha(r.bindingGeneration), targets: targets(r.targets) };
|
|
165
|
+
}
|
|
166
|
+
return fail();
|
|
167
|
+
}
|
|
168
|
+
function fixed(m) { const { registrations: _, ...rest } = m; return rest; }
|
|
169
|
+
function transform(m, i, seq) {
|
|
170
|
+
if (i.kind === "genesis")
|
|
171
|
+
return fail();
|
|
172
|
+
const v2 = m.mcpHookConsent !== undefined;
|
|
173
|
+
if (i.kind === "upgrade") {
|
|
174
|
+
if (v2)
|
|
175
|
+
return fail();
|
|
176
|
+
return parseManifest({ ...m, registrations: m.registrations.map(r => ({ ...r, hookGeneration: seq })), mcpHookConsent: { generation: seq, bindingGeneration: null, targets: [] } }, seq, true);
|
|
177
|
+
}
|
|
178
|
+
if (i.kind === "set-pi-hooks") {
|
|
179
|
+
if (!v2 || !m.registrations.some(r => r.status === "active" && r.endpointId === i.endpointId && r.runtimeSessionId === i.runtimeSessionId && r.epoch === i.epoch))
|
|
180
|
+
return fail();
|
|
181
|
+
return parseManifest({ ...m, registrations: m.registrations.map(r => r.endpointId === i.endpointId ? { ...r, hooks: i.hooks, hookGeneration: seq } : r) }, seq, true);
|
|
182
|
+
}
|
|
183
|
+
if (i.kind === "set-mcp-hooks") {
|
|
184
|
+
if (!v2 || i.targets.some(t => !m.registrations.some(r => r.status === "active" && r.endpointId === t.endpointId && r.runtimeSessionId === t.runtimeSessionId && r.epoch === t.epoch)))
|
|
185
|
+
return fail();
|
|
186
|
+
return parseManifest({ ...m, mcpHookConsent: { generation: seq, bindingGeneration: i.bindingGeneration, targets: i.targets } }, seq, true);
|
|
187
|
+
}
|
|
188
|
+
const old = m.registrations.find(r => r.endpointId === (i.kind !== "revoke" ? i.registration.endpointId : i.endpointId));
|
|
189
|
+
let next;
|
|
190
|
+
if (i.kind === "revoke") {
|
|
191
|
+
if (!old || old.status !== "active")
|
|
192
|
+
return fail();
|
|
193
|
+
next = { ...old, status: "revoked" };
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
const r = i.registration;
|
|
197
|
+
const restoring = i.kind === "reauthorize";
|
|
198
|
+
if (restoring && (!old || old.status !== "revoked") || old && (old.runtimeSessionId !== r.runtimeSessionId || !restoring && old.status !== "active") || m.registrations.some(e => e.runtimeSessionId === r.runtimeSessionId && e.endpointId !== r.endpointId))
|
|
199
|
+
return fail();
|
|
200
|
+
// An explicit new incarnation invalidates both loaded permission generations,
|
|
201
|
+
// even when their booleans remain unchanged. Existing Hooks are retained.
|
|
202
|
+
next = { ...old, ...r, ...(v2 && !old ? { hooks: [], hookGeneration: seq } : {}), epoch: restoring ? old.epoch + 1 : old?.epoch ?? 1, status: "active",
|
|
203
|
+
payloadReadGeneration: restoring || !old || old.payloadRead !== r.payloadRead ? seq : old.payloadReadGeneration,
|
|
204
|
+
ackGeneration: restoring || !old || old.ack !== r.ack ? seq : old.ackGeneration };
|
|
205
|
+
}
|
|
206
|
+
return parseManifest({ ...m, registrations: old ? m.registrations.map(r => r === old ? next : r) : [...m.registrations, next] }, seq, v2);
|
|
207
|
+
}
|
|
208
|
+
const slot = (root, seq) => join(root, `${String(seq).padStart(6, "0")}.json`);
|
|
209
|
+
function envelope(bytes, v2 = false) {
|
|
210
|
+
let raw;
|
|
211
|
+
try {
|
|
212
|
+
raw = JSON.parse(bytes.toString("utf8"));
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
return fail();
|
|
216
|
+
}
|
|
217
|
+
const r = closed(raw, ["kind", "policyId", "sequence", "parentDigest", "identityDigest", "operationId", "intent", "manifestDigest", "manifest"]);
|
|
218
|
+
if (r.kind !== (v2 ? "ce-live-session-v2" : KIND))
|
|
219
|
+
return fail();
|
|
220
|
+
const seq = integer(r.sequence);
|
|
221
|
+
const m = parseManifest(r.manifest, seq, v2);
|
|
222
|
+
if (r.parentDigest !== null)
|
|
223
|
+
sha(r.parentDigest);
|
|
224
|
+
if (sha(r.identityDigest) !== digest(fixedIdentity(m, v2)) || sha(r.manifestDigest) !== digest(m))
|
|
225
|
+
return fail();
|
|
226
|
+
return { kind: v2 ? "ce-live-session-v2" : KIND, policyId: label(r.policyId), sequence: seq, parentDigest: r.parentDigest,
|
|
227
|
+
identityDigest: sha(r.identityDigest), operationId: label(r.operationId), intent: intent(r.intent, v2), manifestDigest: sha(r.manifestDigest), manifest: m };
|
|
228
|
+
}
|
|
229
|
+
function maxSlotIndex(root) {
|
|
230
|
+
let max = -1;
|
|
231
|
+
for (const name of fs.readdirSync(root))
|
|
232
|
+
if (/^\d{6}\.json$/.test(name))
|
|
233
|
+
max = Math.max(max, Number(name.slice(0, 6)));
|
|
234
|
+
return max;
|
|
235
|
+
}
|
|
236
|
+
function history(ref, credentialFile, seam) {
|
|
237
|
+
const snapshots = [];
|
|
238
|
+
const envelopes = [];
|
|
239
|
+
let total = 0;
|
|
240
|
+
let seq = 0;
|
|
241
|
+
let bytes = read(slot(ref.root, 0), credentialFile);
|
|
242
|
+
if (!bytes)
|
|
243
|
+
return fail();
|
|
244
|
+
for (;;) {
|
|
245
|
+
total += bytes.length;
|
|
246
|
+
if (total > 64 * 1024 * 1024)
|
|
247
|
+
return fail();
|
|
248
|
+
const v2 = ref.kind === "ce-live-session-v2" && seq >= ref.upgradeSequence;
|
|
249
|
+
const e = envelope(bytes, v2);
|
|
250
|
+
const d = hash(bytes);
|
|
251
|
+
const prev = snapshots.at(-1);
|
|
252
|
+
if (e.policyId !== ref.policyId || e.sequence !== seq || e.parentDigest !== (prev?.digest ?? null))
|
|
253
|
+
return fail();
|
|
254
|
+
if (!prev) {
|
|
255
|
+
if (d !== ref.genesisDigest || e.intent.kind !== "genesis" || e.manifest.registrations.some(r => r.payloadReadGeneration !== 0 || r.ackGeneration !== 0))
|
|
256
|
+
return fail();
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
if (v2 && (seq === ref.upgradeSequence ? e.intent.kind !== "upgrade" : e.intent.kind === "upgrade"))
|
|
260
|
+
return fail();
|
|
261
|
+
if (!isDeepStrictEqual(fixedIdentity(prev.manifest, v2), fixedIdentity(e.manifest, v2)) || !isDeepStrictEqual(transform(prev.manifest, e.intent, seq), e.manifest))
|
|
262
|
+
return fail();
|
|
263
|
+
}
|
|
264
|
+
if (envelopes.some(p => p.operationId === e.operationId))
|
|
265
|
+
return fail();
|
|
266
|
+
envelopes.push(e);
|
|
267
|
+
snapshots.push(Object.freeze({ ref, sequence: seq, digest: d, manifest: e.manifest }));
|
|
268
|
+
const nextSeq = seq + 1;
|
|
269
|
+
if (nextSeq > 65536)
|
|
270
|
+
break;
|
|
271
|
+
let next = read(slot(ref.root, nextSeq), credentialFile);
|
|
272
|
+
if (!next) {
|
|
273
|
+
seam?.afterMissingSlot?.(nextSeq);
|
|
274
|
+
next = read(slot(ref.root, nextSeq), credentialFile);
|
|
275
|
+
}
|
|
276
|
+
if (!next) {
|
|
277
|
+
const maxIdx = maxSlotIndex(ref.root);
|
|
278
|
+
// Enumeration may see several concurrent appends after the absent read.
|
|
279
|
+
// Verify the next contiguous slot; a real hole still refuses. Publication
|
|
280
|
+
// separately compares expectedDigest and never rebases consent.
|
|
281
|
+
if (maxIdx >= nextSeq) {
|
|
282
|
+
next = read(slot(ref.root, nextSeq), credentialFile);
|
|
283
|
+
if (!next)
|
|
284
|
+
return fail();
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (!next)
|
|
288
|
+
break;
|
|
289
|
+
seq = nextSeq;
|
|
290
|
+
bytes = next;
|
|
291
|
+
}
|
|
292
|
+
const upgraded = ref.kind !== "ce-live-session-v2" || seq >= ref.upgradeSequence;
|
|
293
|
+
return { snapshots, envelopes, bytes: total, upgraded };
|
|
294
|
+
}
|
|
295
|
+
export function readLivePolicy(raw, options = {}) {
|
|
296
|
+
const ref = reference(raw);
|
|
297
|
+
const h = history(ref, options.credentialFile, options.seam);
|
|
298
|
+
// A pinned v2 reference only reads a chain whose upgrade marker was published.
|
|
299
|
+
if (!h.upgraded)
|
|
300
|
+
return fail();
|
|
301
|
+
const tip = h.snapshots.at(-1);
|
|
302
|
+
const w = options.watermark;
|
|
303
|
+
if (w) {
|
|
304
|
+
if (w.sequence !== undefined && (w.policyId !== ref.policyId || !Number.isSafeInteger(w.sequence) || w.sequence < 0 || h.snapshots[w.sequence]?.digest !== w.digest))
|
|
305
|
+
fail();
|
|
306
|
+
Object.assign(w, { sequence: tip.sequence, digest: tip.digest, policyId: ref.policyId });
|
|
307
|
+
}
|
|
308
|
+
return tip;
|
|
309
|
+
}
|
|
310
|
+
function syncDir(root, io) {
|
|
311
|
+
const fd = io.openSync(root, fs.constants.O_RDONLY | fs.constants.O_DIRECTORY | fs.constants.O_NOFOLLOW);
|
|
312
|
+
try {
|
|
313
|
+
io.fsyncSync(fd);
|
|
314
|
+
}
|
|
315
|
+
finally {
|
|
316
|
+
io.closeSync(fd);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function stage(root, bytes, io, credentialFile) {
|
|
320
|
+
if (bytes.length > 65536)
|
|
321
|
+
return fail();
|
|
322
|
+
const path = join(root, `.live-stage-${randomUUID()}`);
|
|
323
|
+
const fd = io.openSync(path, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_NOFOLLOW, 0o600);
|
|
324
|
+
try {
|
|
325
|
+
io.writeFileSync(fd, bytes);
|
|
326
|
+
io.fsyncSync(fd);
|
|
327
|
+
}
|
|
328
|
+
finally {
|
|
329
|
+
io.closeSync(fd);
|
|
330
|
+
}
|
|
331
|
+
const checked = read(path, credentialFile);
|
|
332
|
+
if (!checked?.equals(bytes))
|
|
333
|
+
return fail();
|
|
334
|
+
return path;
|
|
335
|
+
}
|
|
336
|
+
function fixedIdentity(m, v2) { if (!v2)
|
|
337
|
+
return fixed(m); const { mcpHookConsent: _, ...rest } = fixed(m); return rest; }
|
|
338
|
+
function makeEnvelope(policyId, sequence, parentDigest, operationId, i, manifest) {
|
|
339
|
+
const v2 = manifest.mcpHookConsent !== undefined;
|
|
340
|
+
return { kind: v2 ? "ce-live-session-v2" : KIND, policyId, sequence, parentDigest, identityDigest: digest(fixedIdentity(manifest, v2)), operationId, intent: i, manifestDigest: digest(manifest), manifest };
|
|
341
|
+
}
|
|
342
|
+
const defaultIo = () => ({ openSync: fs.openSync, closeSync: fs.closeSync, writeFileSync: fs.writeFileSync, fsyncSync: fs.fsyncSync, linkSync: fs.linkSync });
|
|
343
|
+
/** Isolated offline preparation only. The caller supplies an existing empty private root. */
|
|
344
|
+
export function initializeLivePolicy(input) {
|
|
345
|
+
directory(input.root);
|
|
346
|
+
if (fs.readdirSync(input.root).length)
|
|
347
|
+
return fail();
|
|
348
|
+
const base = parseMultiSessionManifest(input.manifest);
|
|
349
|
+
const manifest = parseManifest({ ...base, registrations: base.registrations.map(r => ({ ...r, payloadReadGeneration: 0, ackGeneration: 0, presenceDisclosure: false })) }, 0);
|
|
350
|
+
const policyId = randomUUID();
|
|
351
|
+
const bytes = Buffer.from(json(makeEnvelope(policyId, 0, null, randomUUID(), { kind: "genesis" }, manifest)));
|
|
352
|
+
const io = defaultIo();
|
|
353
|
+
const path = stage(input.root, bytes, io, input.credentialFile);
|
|
354
|
+
io.linkSync(path, slot(input.root, 0));
|
|
355
|
+
syncDir(input.root, io);
|
|
356
|
+
return Object.freeze({ kind: KIND, root: input.root, policyId, genesisDigest: hash(bytes) });
|
|
357
|
+
}
|
|
358
|
+
const locatorPrefix = (runtime) => `.live-intent-${hash(runtime)}-`;
|
|
359
|
+
function parseLocator(bytes) {
|
|
360
|
+
let raw;
|
|
361
|
+
try {
|
|
362
|
+
raw = JSON.parse(bytes.toString("utf8"));
|
|
363
|
+
}
|
|
364
|
+
catch {
|
|
365
|
+
return fail();
|
|
366
|
+
}
|
|
367
|
+
const r = closed(raw, ["kind", "policyId", "runtimeSessionId", "parentSequence", "parentDigest", "sequence", "operationId", "intentDigest", "candidateDigest"]);
|
|
368
|
+
if (r.kind !== "ce-live-locator-v1" || integer(r.sequence) !== integer(r.parentSequence) + 1)
|
|
369
|
+
return fail();
|
|
370
|
+
return { kind: "ce-live-locator-v1", policyId: label(r.policyId), runtimeSessionId: label(r.runtimeSessionId),
|
|
371
|
+
parentSequence: integer(r.parentSequence), parentDigest: sha(r.parentDigest), sequence: integer(r.sequence),
|
|
372
|
+
operationId: label(r.operationId), intentDigest: sha(r.intentDigest), candidateDigest: sha(r.candidateDigest) };
|
|
373
|
+
}
|
|
374
|
+
/** Offline preparation of the pinned v2 reference for the same immutable history.
|
|
375
|
+
* No publication happens here and no genesis is reset. */
|
|
376
|
+
export function prepareLivePolicyUpgrade(v1, profilePath) {
|
|
377
|
+
const ref = reference(v1);
|
|
378
|
+
if (ref.kind !== KIND)
|
|
379
|
+
return fail();
|
|
380
|
+
if (!isAbsolute(profilePath) || normalize(profilePath) !== profilePath)
|
|
381
|
+
return fail();
|
|
382
|
+
const tip = readLivePolicy(ref);
|
|
383
|
+
if (tip.sequence >= 65536)
|
|
384
|
+
return fail();
|
|
385
|
+
return Object.freeze({ kind: "ce-live-session-v2", root: ref.root, policyId: ref.policyId, genesisDigest: ref.genesisDigest, profilePath, upgradeSequence: tip.sequence + 1 });
|
|
386
|
+
}
|
|
387
|
+
function ownerOf(e) {
|
|
388
|
+
switch (e.intent.kind) {
|
|
389
|
+
case "genesis": return undefined;
|
|
390
|
+
case "register":
|
|
391
|
+
case "reauthorize": return e.intent.registration.runtimeSessionId;
|
|
392
|
+
case "revoke": {
|
|
393
|
+
const endpointId = e.intent.endpointId;
|
|
394
|
+
return e.manifest.registrations.find(r => r.endpointId === endpointId)?.runtimeSessionId;
|
|
395
|
+
}
|
|
396
|
+
// New intents carry their trusted actor in the closed intent record. The
|
|
397
|
+
// actor is publication attribution only, never an Owner or capability.
|
|
398
|
+
case "upgrade": return e.intent.actorRuntimeSessionId;
|
|
399
|
+
case "set-pi-hooks": return e.intent.runtimeSessionId;
|
|
400
|
+
case "set-mcp-hooks": return e.intent.actorRuntimeSessionId;
|
|
401
|
+
default: return fail();
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
/** Read-only proof for an explicitly identified configuration publication.
|
|
405
|
+
* Locator attribution is evidence, never ownership or execution authority. */
|
|
406
|
+
export function readLiveHookPublication(input) {
|
|
407
|
+
const ref = reference(input.ref);
|
|
408
|
+
if (ref.kind !== "ce-live-session-v2")
|
|
409
|
+
return fail();
|
|
410
|
+
const runtime = label(input.runtimeSessionId);
|
|
411
|
+
const operation = label(input.operationId);
|
|
412
|
+
const reconciled = createLivePolicyRegistration({ ref, credentialFile: input.credentialFile }).reconcile({ runtimeSessionId: runtime });
|
|
413
|
+
const record = reconciled.find(r => r.operationId === operation);
|
|
414
|
+
if (!record || record.outcome !== "PUBLISHED")
|
|
415
|
+
return fail();
|
|
416
|
+
const h = history(ref, input.credentialFile);
|
|
417
|
+
if (!h.upgraded)
|
|
418
|
+
return fail();
|
|
419
|
+
const e = h.envelopes[record.sequence];
|
|
420
|
+
if (!e || e.operationId !== operation || ownerOf(e) !== runtime || (e.intent.kind !== "upgrade" && e.intent.kind !== "set-mcp-hooks"))
|
|
421
|
+
return fail();
|
|
422
|
+
return Object.freeze({ sequence: record.sequence, intent: Object.freeze(e.intent), snapshot: h.snapshots[record.sequence] });
|
|
423
|
+
}
|
|
424
|
+
export function createLivePolicyRegistration(options) {
|
|
425
|
+
const ref = reference(options.ref);
|
|
426
|
+
const io = { ...defaultIo(), ...options.io };
|
|
427
|
+
const watermark = {};
|
|
428
|
+
const load = () => readLivePolicy(ref, { ...(options.credentialFile === undefined ? {} : { credentialFile: options.credentialFile }), watermark });
|
|
429
|
+
function evidenceHistory() {
|
|
430
|
+
if (ref.kind === "ce-live-session-v2") {
|
|
431
|
+
const marker = read(slot(ref.root, ref.upgradeSequence), options.credentialFile);
|
|
432
|
+
if (marker) {
|
|
433
|
+
let raw;
|
|
434
|
+
try {
|
|
435
|
+
raw = JSON.parse(marker.toString("utf8"));
|
|
436
|
+
}
|
|
437
|
+
catch {
|
|
438
|
+
return fail();
|
|
439
|
+
}
|
|
440
|
+
if (raw?.kind === KIND) {
|
|
441
|
+
// Only an explicitly v1 marker-slot envelope selects the original
|
|
442
|
+
// reader. Validate the full original chain before classifying a lost
|
|
443
|
+
// upgrade race; malformed v2 never falls back to v1.
|
|
444
|
+
envelope(marker);
|
|
445
|
+
return history({ kind: KIND, root: ref.root, policyId: ref.policyId, genesisDigest: ref.genesisDigest }, options.credentialFile);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return history(ref, options.credentialFile);
|
|
450
|
+
}
|
|
451
|
+
function reconcile(input) {
|
|
452
|
+
const runtime = label(input.runtimeSessionId);
|
|
453
|
+
reference(ref);
|
|
454
|
+
const names = fs.readdirSync(ref.root).filter(n => n.startsWith(locatorPrefix(runtime)));
|
|
455
|
+
if (names.length > 1024)
|
|
456
|
+
return fail();
|
|
457
|
+
const h = evidenceHistory();
|
|
458
|
+
for (const e of h.envelopes.slice(1)) {
|
|
459
|
+
const owner = ownerOf(e);
|
|
460
|
+
if (owner === undefined)
|
|
461
|
+
return fail();
|
|
462
|
+
if (owner === runtime && !names.includes(`${locatorPrefix(runtime)}${hash(e.operationId)}.json`))
|
|
463
|
+
fail();
|
|
464
|
+
}
|
|
465
|
+
return Object.freeze(names.sort().map(name => {
|
|
466
|
+
const l = parseLocator(read(join(ref.root, name), options.credentialFile) ?? fail());
|
|
467
|
+
if (l.policyId !== ref.policyId || l.runtimeSessionId !== runtime || name !== `${locatorPrefix(runtime)}${hash(l.operationId)}.json`)
|
|
468
|
+
return fail();
|
|
469
|
+
const parent = h.snapshots[l.parentSequence];
|
|
470
|
+
const candidate = h.snapshots[l.sequence];
|
|
471
|
+
const e = h.envelopes[l.sequence];
|
|
472
|
+
if (parent?.digest !== l.parentDigest)
|
|
473
|
+
return fail();
|
|
474
|
+
const outcome = !candidate ? "NOT_OBSERVED" : candidate.digest === l.candidateDigest && e?.operationId === l.operationId && digest(e.intent) === l.intentDigest ? "PUBLISHED" : "COMPETING_COMMIT";
|
|
475
|
+
return Object.freeze({ operationId: l.operationId, sequence: l.sequence, outcome });
|
|
476
|
+
}));
|
|
477
|
+
}
|
|
478
|
+
function publish(expectedDigest, i, operationId) {
|
|
479
|
+
sha(expectedDigest);
|
|
480
|
+
const current = load();
|
|
481
|
+
if (current.digest !== expectedDigest)
|
|
482
|
+
return conflict();
|
|
483
|
+
return publishFrom(current, i, operationId);
|
|
484
|
+
}
|
|
485
|
+
function publishFrom(current, i, operationId, verify) {
|
|
486
|
+
if (current.sequence >= 65536)
|
|
487
|
+
return fail();
|
|
488
|
+
i = intent(i, ref.kind === "ce-live-session-v2");
|
|
489
|
+
const next = transform(current.manifest, i, current.sequence + 1);
|
|
490
|
+
const runtime = ownerOf({ kind: KIND, policyId: ref.policyId, sequence: current.sequence + 1, parentDigest: current.digest,
|
|
491
|
+
identityDigest: "0".repeat(64), operationId: "pending", intent: i, manifestDigest: "0".repeat(64), manifest: current.manifest });
|
|
492
|
+
if (!runtime)
|
|
493
|
+
return fail();
|
|
494
|
+
const prior = reconcile({ runtimeSessionId: runtime });
|
|
495
|
+
// Unresolved attempts never authorize replay or a new speculative publication.
|
|
496
|
+
if (prior.some(r => r.outcome === "NOT_OBSERVED" || r.outcome === "UNKNOWN") || prior.length >= 1024)
|
|
497
|
+
return fail();
|
|
498
|
+
const op = label(operationId ?? randomUUID());
|
|
499
|
+
if (prior.some(r => r.operationId === op))
|
|
500
|
+
return conflict();
|
|
501
|
+
const e = makeEnvelope(ref.policyId, current.sequence + 1, current.digest, op, i, next);
|
|
502
|
+
const bytes = Buffer.from(json(e));
|
|
503
|
+
const h = history(ref, options.credentialFile);
|
|
504
|
+
if (h.bytes + bytes.length > 64 * 1024 * 1024)
|
|
505
|
+
return fail();
|
|
506
|
+
const prepared = stage(ref.root, bytes, io, options.credentialFile);
|
|
507
|
+
const l = { kind: "ce-live-locator-v1", policyId: ref.policyId, runtimeSessionId: runtime, parentSequence: current.sequence, parentDigest: current.digest,
|
|
508
|
+
sequence: e.sequence, operationId: op, intentDigest: digest(i), candidateDigest: hash(bytes) };
|
|
509
|
+
const locator = stage(ref.root, Buffer.from(json(l)), io, options.credentialFile);
|
|
510
|
+
io.linkSync(locator, join(ref.root, `${locatorPrefix(runtime)}${hash(op)}.json`));
|
|
511
|
+
syncDir(ref.root, io);
|
|
512
|
+
if ((verify ?? load)().digest !== current.digest)
|
|
513
|
+
return conflict();
|
|
514
|
+
try {
|
|
515
|
+
io.linkSync(prepared, slot(ref.root, e.sequence));
|
|
516
|
+
}
|
|
517
|
+
catch (error) {
|
|
518
|
+
if (error.code === "EEXIST") {
|
|
519
|
+
const observed = evidenceHistory();
|
|
520
|
+
if (observed.snapshots[current.sequence]?.digest !== current.digest || !observed.snapshots[e.sequence])
|
|
521
|
+
return fail();
|
|
522
|
+
return conflict();
|
|
523
|
+
}
|
|
524
|
+
throw new TunnelRefusal("CE_TUNNEL_PROFILE_INVALID", "messaging.livePolicy.publicationUnknown");
|
|
525
|
+
}
|
|
526
|
+
let durability = "synced";
|
|
527
|
+
try {
|
|
528
|
+
syncDir(ref.root, io);
|
|
529
|
+
}
|
|
530
|
+
catch {
|
|
531
|
+
durability = "uncertain";
|
|
532
|
+
}
|
|
533
|
+
return Object.freeze({ snapshot: load(), durability, operationId: op });
|
|
534
|
+
}
|
|
535
|
+
/** Publishes the unique v2 upgrade marker into the prepared next slot.
|
|
536
|
+
* The original v1 tip is read before the marker and the pinned v2 chain is
|
|
537
|
+
* read afterwards. There is no rebase and no second attempt. */
|
|
538
|
+
function upgrade(input) {
|
|
539
|
+
if (ref.kind !== "ce-live-session-v2")
|
|
540
|
+
return fail();
|
|
541
|
+
const v1ref = { kind: KIND, root: ref.root, policyId: ref.policyId, genesisDigest: ref.genesisDigest };
|
|
542
|
+
let tip;
|
|
543
|
+
try {
|
|
544
|
+
tip = readLivePolicy(v1ref, { ...(options.credentialFile === undefined ? {} : { credentialFile: options.credentialFile }) });
|
|
545
|
+
}
|
|
546
|
+
catch {
|
|
547
|
+
return fail();
|
|
548
|
+
}
|
|
549
|
+
sha(input.expectedDigest);
|
|
550
|
+
if (tip.digest !== input.expectedDigest)
|
|
551
|
+
return conflict();
|
|
552
|
+
if (tip.sequence + 1 !== ref.upgradeSequence)
|
|
553
|
+
return fail();
|
|
554
|
+
// Upgrade must account for every old writer's durable requests, including
|
|
555
|
+
// actors with no committed registration yet. Locators are bounded per actor.
|
|
556
|
+
const actors = new Set(tip.manifest.registrations.map(r => r.runtimeSessionId));
|
|
557
|
+
const names = fs.readdirSync(ref.root).filter(n => n.startsWith(".live-intent-"));
|
|
558
|
+
for (const name of names) {
|
|
559
|
+
const l = parseLocator(read(join(ref.root, name), options.credentialFile) ?? fail());
|
|
560
|
+
actors.add(l.runtimeSessionId);
|
|
561
|
+
}
|
|
562
|
+
for (const actor of actors)
|
|
563
|
+
if (reconcile({ runtimeSessionId: actor }).some(r => r.outcome === "NOT_OBSERVED" || r.outcome === "UNKNOWN"))
|
|
564
|
+
return fail();
|
|
565
|
+
const intent = { kind: "upgrade", actorRuntimeSessionId: label(input.actorRuntimeSessionId), profileRevision: sha(input.profileRevision), bindingGeneration: sha(input.bindingGeneration) };
|
|
566
|
+
return publishFrom(tip, intent, input.operationId, () => readLivePolicy(v1ref, { ...(options.credentialFile === undefined ? {} : { credentialFile: options.credentialFile }) }));
|
|
567
|
+
}
|
|
568
|
+
return {
|
|
569
|
+
register: (input) => publish(input.expectedDigest, { kind: "register", registration: registration(input.registration) }, input.operationId),
|
|
570
|
+
reauthorize: (input) => publish(input.expectedDigest, { kind: "reauthorize", registration: registration(input.registration) }, input.operationId),
|
|
571
|
+
revoke: (input) => publish(input.expectedDigest, { kind: "revoke", endpointId: label(input.endpointId) }, input.operationId),
|
|
572
|
+
upgrade,
|
|
573
|
+
setPiHooks: (input) => publish(input.expectedDigest, { kind: "set-pi-hooks", runtimeSessionId: label(input.runtimeSessionId), endpointId: label(input.endpointId), epoch: integer(input.epoch), hooks: input.hooks }, input.operationId),
|
|
574
|
+
setMcpHooks: (input) => publish(input.expectedDigest, { kind: "set-mcp-hooks", actorRuntimeSessionId: label(input.actorRuntimeSessionId), bindingGeneration: sha(input.bindingGeneration), targets: input.targets }, input.operationId),
|
|
575
|
+
reconcile,
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
//# sourceMappingURL=live-session-policy.js.map
|
|
@@ -90,7 +90,9 @@ export function tunnelPanelModel(projection, view = "manager") {
|
|
|
90
90
|
{ label: "Credential", value: read.credential, tone: configuredTone(read.credential) },
|
|
91
91
|
];
|
|
92
92
|
const runtime = [
|
|
93
|
-
{ label: "
|
|
93
|
+
{ label: "Shared Tunnel", value: read.state, tone: primaryState(read).tone },
|
|
94
|
+
{ label: "Current Session", value: projection.sessionBinding === "attached" ? "Attached" : projection.sessionBinding === "detached" ? "Detached" : "Unknown",
|
|
95
|
+
tone: projection.sessionBinding === "attached" ? "ok" : projection.sessionBinding === "detached" ? "neutral" : "warn" },
|
|
94
96
|
...(view === "details" ? [{ label: "Evidence", value: read.evidence, tone: "neutral" }] : []),
|
|
95
97
|
...(view === "details" && read.errorCode !== undefined
|
|
96
98
|
? [{ label: "Error", value: read.errorCode, tone: "bad" }] : []),
|
|
@@ -107,7 +109,7 @@ export function tunnelPanelModel(projection, view = "manager") {
|
|
|
107
109
|
: [];
|
|
108
110
|
return Object.freeze({
|
|
109
111
|
title: view === "details" ? "Tunnel Details" : "Chat Exchange Tunnel",
|
|
110
|
-
primary: primaryState(read),
|
|
112
|
+
primary: { ...primaryState(read), text: primaryState(read).text.replace("● ", "● Shared Tunnel · ") },
|
|
111
113
|
sections: Object.freeze([
|
|
112
114
|
Object.freeze({ title: "Configuration", fields: Object.freeze(identity) }),
|
|
113
115
|
...(view === "details" && messaging.length > 0 ? [Object.freeze({ title: "Messaging (configuration)", fields: Object.freeze(messaging) })] : []),
|