@zieglar/pi-chat-exchange 0.3.0 → 0.3.2

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 (41) hide show
  1. package/README.md +10 -6
  2. package/dist/bin/mcp-stdio.js +113 -62
  3. package/dist/control/mcp/endpoint-discovery.js +60 -0
  4. package/dist/control/mcp/messaging-tools.js +12 -2
  5. package/dist/control/mcp/server.js +10 -2
  6. package/dist/host/bound-endpoint.js +19 -6
  7. package/dist/host/endpoint-host.js +78 -4
  8. package/dist/host/mcp-binding-config.js +27 -4
  9. package/dist/runtime/native-readiness.js +8 -5
  10. package/dist/storage/creation.js +16 -0
  11. package/dist/storage/ownership.js +45 -1
  12. package/dist/tunnel/background-authorization.js +343 -0
  13. package/dist/tunnel/background-manager.js +104 -8
  14. package/dist/tunnel/background-scope.js +69 -0
  15. package/dist/tunnel/codes.js +1 -1
  16. package/dist/tunnel/hooks-management-ui.js +225 -0
  17. package/dist/tunnel/hooks-management.js +431 -0
  18. package/dist/tunnel/live-adoption-observation.js +136 -0
  19. package/dist/tunnel/live-adoption-probe.js +240 -0
  20. package/dist/tunnel/live-connection-status.js +136 -0
  21. package/dist/tunnel/live-mcp-composition.js +143 -0
  22. package/dist/tunnel/live-process-observation.js +98 -0
  23. package/dist/tunnel/live-session-migration.js +169 -0
  24. package/dist/tunnel/live-session-policy.js +578 -0
  25. package/dist/tunnel/manager-panel.js +4 -2
  26. package/dist/tunnel/manager-ui.js +71 -15
  27. package/dist/tunnel/multi-session-admission.js +1 -1
  28. package/dist/tunnel/setup-ui.js +2 -4
  29. package/dist/tunnel/status-ui.js +34 -0
  30. package/package.json +1 -1
  31. package/pi-extension/desktop-target.ts +157 -0
  32. package/pi-extension/index.ts +1060 -139
  33. package/pi-extension/live-session-profile.ts +49 -0
  34. package/pi-extension/live-session-runtime.ts +59 -0
  35. package/pi-extension/multi-session-commands.ts +114 -3
  36. package/pi-extension/multi-session-runtime.ts +63 -11
  37. package/pi-extension/native-inbound.ts +235 -0
  38. package/pi-extension/native-read-protocol.ts +44 -0
  39. package/pi-extension/ownership-feedback.ts +100 -0
  40. package/pi-extension/session-onboarding.ts +154 -0
  41. package/pi-extension/session-recovery.ts +214 -0
@@ -0,0 +1,169 @@
1
+ /**
2
+ * HANDOFF-CE-SESSION-ONBOARDING-01 — explicit OFFLINE migration of a legacy
3
+ * `ce-multi-session-v1` profile into the live policy chain.
4
+ *
5
+ * Contract:
6
+ * - The migration runs ONLY under the manager's verified STOPPED-only profile
7
+ * configuration lock. A running/pending/uncertain transport refuses it.
8
+ * - The operator must present the exact current Profile revision and confirm.
9
+ * - The live policy root is a NEW private 0700 directory under `.ce-bindings`;
10
+ * it holds the single writable live chain, never a copied manifest.
11
+ * - The published launch document is a closed REFERENCE to that chain
12
+ * (`ce-live-launch-v1`), so the launch identity stays immutable and the
13
+ * manifest is not duplicated into a second editable file.
14
+ * - The previous generation is retained. Nothing is stopped or started here:
15
+ * transport lifecycle remains an explicit operator action.
16
+ * - The Profile is repointed to the new immutable binding with an O_EXCL temp
17
+ * file, fsync, a final lock/revision re-check and one rename. An uncertain
18
+ * rename is reported as uncertain and never retried or rolled back.
19
+ *
20
+ * This module never reads a credential, mutates a Store, an Owner or a
21
+ * capability, and never performs a network operation.
22
+ */
23
+ import fs from "node:fs";
24
+ import { createHash, randomUUID } from "node:crypto";
25
+ import { dirname, isAbsolute, join, normalize } from "node:path";
26
+ import { parseMcpBindingConfig } from "../host/mcp-binding-config.js";
27
+ import { TunnelRefusal } from "./codes.js";
28
+ import { parseTunnelProfile } from "./background-manager.js";
29
+ import { replaceTunnelMcpBindingPath } from "./profile.js";
30
+ import { publishMessagingGeneration, readPrivateMessagingFile } from "./messaging-config.js";
31
+ import { initializeLivePolicy } from "./live-session-policy.js";
32
+ import { readSelectedMultiSessionPolicy } from "./multi-session-policy.js";
33
+ const LAUNCH_DOCUMENT_KIND = "ce-live-launch-v1";
34
+ const invalid = (field) => new TunnelRefusal("CE_TUNNEL_PROFILE_INVALID", field);
35
+ const conflict = () => new TunnelRefusal("CE_TUNNEL_CONFIG_CONFLICT", "messaging.liveMigration");
36
+ const MAX_PROFILE_BYTES = 65_536;
37
+ const sha256 = (text) => createHash("sha256").update(text, "utf8").digest("hex");
38
+ function readProfileText(profilePath) {
39
+ if (!isAbsolute(profilePath) || normalize(profilePath) !== profilePath)
40
+ throw invalid("profile.path");
41
+ const selected = readPrivateMessagingFile(profilePath);
42
+ if (selected === undefined)
43
+ throw invalid("profile.read");
44
+ return selected.bytes.toString("utf8");
45
+ }
46
+ /** The private configuration directory that holds every generation. */
47
+ function bindingsDirectory(profilePath) {
48
+ const directory = join(dirname(profilePath), ".ce-bindings");
49
+ let stat;
50
+ try {
51
+ stat = fs.lstatSync(directory);
52
+ }
53
+ catch {
54
+ throw invalid("messaging.directory");
55
+ }
56
+ if (!stat.isDirectory() || stat.isSymbolicLink() || stat.uid !== process.geteuid?.() || (stat.mode & 0o7777) !== 0o700) {
57
+ throw invalid("messaging.directory");
58
+ }
59
+ return directory;
60
+ }
61
+ /** Create the NEW empty private live root. It is never reused or recycled. */
62
+ function createLiveRoot(profilePath) {
63
+ const root = join(bindingsDirectory(profilePath), `live-policy-${randomUUID()}`);
64
+ fs.mkdirSync(root, { mode: 0o700 });
65
+ fs.chmodSync(root, 0o700);
66
+ const stat = fs.lstatSync(root);
67
+ if (!stat.isDirectory() || stat.isSymbolicLink() || stat.uid !== process.geteuid?.() || (stat.mode & 0o7777) !== 0o700) {
68
+ throw invalid("messaging.livePolicy.root");
69
+ }
70
+ if (fs.realpathSync(root) !== root)
71
+ throw invalid("messaging.livePolicy.root");
72
+ return root;
73
+ }
74
+ export function createLiveSessionMigration(options) {
75
+ const { profilePath } = options;
76
+ const io = { openSync: fs.openSync, closeSync: fs.closeSync, writeFileSync: fs.writeFileSync, fsyncSync: fs.fsyncSync,
77
+ linkSync: fs.linkSync, renameSync: fs.renameSync, ...options.io };
78
+ const revision = () => sha256(readProfileText(profilePath));
79
+ const migrate = (input) => options.manager.withProfileConfigurationLock((guard) => {
80
+ try {
81
+ if (guard.profilePath !== profilePath)
82
+ throw invalid("profile.path");
83
+ // Every refusal below happens BEFORE the first write.
84
+ if (input.confirm !== true)
85
+ throw invalid("messaging.liveMigration.confirm");
86
+ const text = readProfileText(profilePath);
87
+ const currentRevision = sha256(text);
88
+ if (currentRevision !== input.expectedRevision)
89
+ throw conflict();
90
+ const facts = parseTunnelProfile(text);
91
+ if (facts.mcpCommand === undefined)
92
+ throw invalid("messaging.liveMigration.command");
93
+ // The legacy generation must still be the selected, verified one.
94
+ const snapshot = readSelectedMultiSessionPolicy(profilePath);
95
+ if (snapshot.revision !== currentRevision)
96
+ throw conflict();
97
+ // OneConnect requires both MCP-wide opt-ins. Refuse before preparing
98
+ // any live files; migration must never widen the operator's ceilings.
99
+ if (!snapshot.manifest.mcp.payloadRead || !snapshot.manifest.mcp.ack) {
100
+ throw invalid("messaging.liveMigration.globalCeiling");
101
+ }
102
+ const root = createLiveRoot(profilePath);
103
+ const ref = initializeLivePolicy({ root, manifest: snapshot.manifest, credentialFile: facts.keyFile });
104
+ const config = parseMcpBindingConfig({ version: 1, ...snapshot.config, admissionPolicy: ref });
105
+ // The launch document references the chain; it never copies the manifest.
106
+ const document = { kind: LAUNCH_DOCUMENT_KIND, version: 1, ref };
107
+ const pair = publishMessagingGeneration(profilePath, config, document, { io, credentialFile: facts.keyFile });
108
+ if (pair.durability !== "synced")
109
+ throw invalid("messaging.liveMigration.publicationUncertain");
110
+ const command = replaceTunnelMcpBindingPath(facts.mcpCommand, pair.bindingPath);
111
+ const quoted = `"${facts.mcpCommand}"`;
112
+ if (text.split(quoted).length !== 2)
113
+ throw invalid("messaging.liveMigration.command");
114
+ const nextText = text.replace(quoted, `"${command}"`);
115
+ if (Buffer.byteLength(nextText) > MAX_PROFILE_BYTES)
116
+ throw invalid("messaging.liveMigration.profile");
117
+ if (parseTunnelProfile(nextText).mcpCommand !== command)
118
+ throw invalid("messaging.liveMigration.command");
119
+ const temporary = `${profilePath}.${randomUUID()}.tmp`;
120
+ const fd = io.openSync(temporary, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_NOFOLLOW, 0o600);
121
+ try {
122
+ const stat = fs.fstatSync(fd);
123
+ if (!stat.isFile() || stat.uid !== process.geteuid?.() || (stat.mode & 0o7777) !== 0o600)
124
+ throw invalid("profile.permissions");
125
+ io.writeFileSync(fd, nextText, "utf8");
126
+ io.fsyncSync(fd);
127
+ }
128
+ finally {
129
+ io.closeSync(fd);
130
+ }
131
+ const directory = io.openSync(dirname(profilePath), fs.constants.O_RDONLY | fs.constants.O_DIRECTORY | fs.constants.O_NOFOLLOW);
132
+ try {
133
+ io.fsyncSync(directory);
134
+ guard.assertIntact();
135
+ if (sha256(readProfileText(profilePath)) !== input.expectedRevision)
136
+ throw conflict();
137
+ // The sole activation commit. A throwing rename may have taken effect:
138
+ // it is reported as uncertain, never retried and never rolled back.
139
+ try {
140
+ io.renameSync(temporary, profilePath);
141
+ }
142
+ catch {
143
+ throw new TunnelRefusal("CE_TUNNEL_PROFILE_INVALID", "messaging.liveMigration.commitUncertain");
144
+ }
145
+ let durability = "synced";
146
+ try {
147
+ io.fsyncSync(directory);
148
+ }
149
+ catch {
150
+ durability = "uncertain";
151
+ }
152
+ return { kind: "saved", revision: sha256(nextText), bindingPath: pair.bindingPath, durability, ref };
153
+ }
154
+ finally {
155
+ try {
156
+ io.closeSync(directory);
157
+ }
158
+ catch { /* no post-commit rollback */ }
159
+ }
160
+ }
161
+ catch (error) {
162
+ if (error instanceof TunnelRefusal)
163
+ throw error;
164
+ throw invalid("messaging.liveMigration");
165
+ }
166
+ });
167
+ return { revision, migrate };
168
+ }
169
+ //# sourceMappingURL=live-session-migration.js.map