@howaboua/pi-shepherdr 0.2.27 → 0.2.28-core.12.1.gda85fa2e8a0f

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 (112) hide show
  1. package/README.md +48 -48
  2. package/index.ts +7 -1
  3. package/package.json +2 -9
  4. package/src/agent-identity.ts +3 -3
  5. package/src/agents-contract.ts +10 -14
  6. package/src/agents-discovery.ts +34 -54
  7. package/src/agents-focus.ts +23 -35
  8. package/src/agents-spawn.ts +24 -23
  9. package/src/agents-tool.ts +4 -19
  10. package/src/agents-work.ts +4 -3
  11. package/src/attachment-notes.ts +8 -7
  12. package/src/attachment-protocol.ts +3 -2
  13. package/src/attachment-routing.ts +1 -1
  14. package/src/attachment-target.ts +10 -18
  15. package/src/attachment.ts +14 -9
  16. package/src/board/discovery.ts +67 -0
  17. package/src/board/host.ts +93 -63
  18. package/src/board/membership.ts +16 -11
  19. package/src/board/records.ts +185 -0
  20. package/src/close-idle.ts +2 -9
  21. package/src/context-briefing.ts +230 -0
  22. package/src/context-routing.ts +1 -1
  23. package/src/control-panel/index.ts +2 -4
  24. package/src/control-panel/items.ts +1 -11
  25. package/src/control-panel/view.ts +3 -9
  26. package/src/controller.ts +50 -63
  27. package/src/core/AGENTS.md +7 -4
  28. package/src/core/admission-broker.mjs +235 -0
  29. package/src/core/admission-endpoint.ts +180 -0
  30. package/src/core/admission-owner.mjs +52 -0
  31. package/src/core/admission-startup.ts +130 -0
  32. package/src/core/admission.ts +181 -0
  33. package/src/core/agents.ts +102 -0
  34. package/src/core/backend.ts +110 -84
  35. package/src/core/client.ts +16 -0
  36. package/src/core/focus-channel-helper.mjs +6 -27
  37. package/src/core/focus-channel-wire.mjs +9 -6
  38. package/src/core/focus-channel.ts +5 -3
  39. package/src/core/focus-window.ts +15 -4
  40. package/src/core/helper-bootstrap.mjs +70 -0
  41. package/src/core/helper-context.mjs +98 -0
  42. package/src/core/helper-core.mjs +121 -0
  43. package/src/core/machines.ts +30 -0
  44. package/src/core/registration.ts +7 -2
  45. package/src/core/remote-endpoint.ts +80 -74
  46. package/src/core/remote-helper.ts +330 -0
  47. package/src/core/remote-reader.ts +3 -42
  48. package/src/core/remote-service.mjs +239 -0
  49. package/src/core/remote-transport.ts +290 -0
  50. package/src/core/spawn.ts +20 -20
  51. package/src/core/terminal-peer.mjs +57 -0
  52. package/src/core/terminal.ts +4 -2
  53. package/src/delivery.ts +39 -0
  54. package/src/fleet/catalog.ts +16 -58
  55. package/src/fleet/connection.ts +9 -18
  56. package/src/fleet/index.ts +38 -76
  57. package/src/fleet/inspection.ts +9 -22
  58. package/src/fleet/machine.ts +28 -12
  59. package/src/fleet/monitor.ts +8 -29
  60. package/src/fleet/persistence.ts +2 -2
  61. package/src/fleet/reconnect.ts +5 -3
  62. package/src/fleet/setup.ts +6 -16
  63. package/src/launch-contract.ts +0 -3
  64. package/src/launch-location.ts +4 -132
  65. package/src/launch.ts +18 -144
  66. package/src/messages/attachments.ts +1 -1
  67. package/src/messages/attribution.ts +9 -14
  68. package/src/messages/events.ts +5 -9
  69. package/src/messages/focus.ts +2 -7
  70. package/src/messages/guidance.ts +2 -43
  71. package/src/messages/peer-sharing.ts +133 -0
  72. package/src/monitor-events.ts +14 -43
  73. package/src/monitor-extension-messages.ts +5 -6
  74. package/src/monitor-record.ts +4 -0
  75. package/src/monitor-state.ts +1 -101
  76. package/src/monitor.ts +38 -66
  77. package/src/notes-discovery.ts +67 -0
  78. package/src/peer-inbox.ts +21 -27
  79. package/src/remote/context-relay.ts +25 -10
  80. package/src/remote/notes-reader.mjs +67 -0
  81. package/src/remote/shepherdr-channel.mjs +1 -1
  82. package/src/remote/shepherdr-checkpoints.mjs +49 -102
  83. package/src/remote/shepherdr-context.mjs +1 -1
  84. package/src/remote/shepherdr-peer.mjs +19 -24
  85. package/src/remote/shepherdr-session.mjs +18 -7
  86. package/src/saved-context.ts +62 -0
  87. package/src/settlement/collector.ts +29 -9
  88. package/src/settlement/index.ts +35 -6
  89. package/src/shared-context-host.ts +27 -33
  90. package/src/shared-context.ts +44 -15
  91. package/src/spawn-context.ts +11 -3
  92. package/src/spawn-ownership.ts +1 -1
  93. package/src/ssh-setup.ts +40 -73
  94. package/src/types.ts +4 -8
  95. package/src/voice-handoff.ts +1 -1
  96. package/src/widget.ts +7 -2
  97. package/src/worker-ownership.ts +2 -8
  98. package/src/worker-service.ts +6 -14
  99. package/src/core/context-relay.mjs +0 -111
  100. package/src/core/remote-exec.ts +0 -122
  101. package/src/core/remote-helper.mjs +0 -244
  102. package/src/core/remote-relay.ts +0 -191
  103. package/src/core/request.ts +0 -124
  104. package/src/herdr-client.ts +0 -364
  105. package/src/herdr.ts +0 -268
  106. package/src/machine-catalog.ts +0 -83
  107. package/src/monitor-event.ts +0 -84
  108. package/src/remote/client.ts +0 -127
  109. package/src/remote/deployment.ts +0 -179
  110. package/src/remote/focus-channels.ts +0 -89
  111. package/src/remote/shepherdr.mjs +0 -504
  112. package/src/remote/transport.ts +0 -369
@@ -1,179 +0,0 @@
1
- import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process";
2
- import { readFile } from "node:fs/promises";
3
- import { fileURLToPath } from "node:url";
4
- import type { SshMachine } from "../machine-catalog.js";
5
-
6
- export const BRIDGE_VERSION = 25;
7
- const REMOTE_HELPER = "~/.pi/agent/shepherdr.mjs";
8
- const REMOTE_PEER_HELPER = "~/.pi/agent/shepherdr-peer.mjs";
9
- const REMOTE_CHANNEL_HELPER = "~/.pi/agent/shepherdr-channel.mjs";
10
- const REMOTE_SESSION_HELPER = "~/.pi/agent/shepherdr-session.mjs";
11
- const REMOTE_SESSION_WATCH_HELPER = "~/.pi/agent/shepherdr-session-watch.mjs";
12
- const REMOTE_CHECKPOINT_HELPER = "~/.pi/agent/shepherdr-checkpoints.mjs";
13
- const REMOTE_CONTEXT_HELPER = "~/.pi/agent/shepherdr-context.mjs";
14
- const MAX_DIAGNOSTIC_BYTES = 8 * 1024;
15
- const DEPLOY_TIMEOUT_MS = 20_000;
16
-
17
- const DEPLOY_SOURCE = String.raw`
18
- const { randomUUID } = require("node:crypto");
19
- const { mkdir, readFile, rename, unlink, writeFile } = require("node:fs/promises");
20
- const { homedir } = require("node:os");
21
- const { dirname, join } = require("node:path");
22
- const targetValue = process.argv[1];
23
- const marker = "// @howaboua/pi-shepherdr managed bridge";
24
- const target = targetValue === "~" ? homedir() : targetValue.startsWith("~/") ? join(homedir(), targetValue.slice(2)) : targetValue;
25
- const chunks = [];
26
- let size = 0;
27
- process.stdin.on("data", chunk => { size += chunk.length; if (size > 1024 * 1024) { process.stderr.write("bridge source is too large\n"); process.exit(1); } chunks.push(chunk); });
28
- process.stdin.on("end", () => { void (async () => {
29
- const source = Buffer.concat(chunks);
30
- let existing;
31
- try { existing = await readFile(target); } catch (error) { if (error.code !== "ENOENT") throw error; }
32
- if (existing) {
33
- if (existing.equals(source)) { process.stdout.write(JSON.stringify({ updated: false, path: target }) + "\n"); return; }
34
- if (!existing.toString("utf8", 0, marker.length + 32).startsWith(marker)) throw new Error(target + " is not owned by Shepherdr");
35
- }
36
- await mkdir(dirname(target), { recursive: true, mode: 0o700 });
37
- const temporary = target + "." + process.pid + "." + randomUUID() + ".tmp";
38
- try {
39
- await writeFile(temporary, source, { mode: 0o600, flag: "wx" });
40
- await rename(temporary, target);
41
- } catch (error) {
42
- await unlink(temporary).catch(() => undefined);
43
- throw error;
44
- }
45
- process.stdout.write(JSON.stringify({ updated: true, path: target }) + "\n");
46
- })().catch(error => { process.stderr.write(error.message + "\n"); process.exit(1); }); });
47
- `;
48
-
49
- function shellQuote(value: string): string {
50
- return `'${value.replaceAll("'", `'"'"'`)}'`;
51
- }
52
-
53
- function executablePath(path: string): string {
54
- return path.startsWith("~/")
55
- ? `"$HOME"/${shellQuote(path.slice(2))}`
56
- : shellQuote(path);
57
- }
58
-
59
- function remoteCommand(config: SshMachine): string {
60
- const args = [
61
- "exec",
62
- "node",
63
- executablePath(REMOTE_HELPER),
64
- "--session",
65
- shellQuote(config.session),
66
- ];
67
- return args.join(" ");
68
- }
69
-
70
- function spawnConnector(
71
- config: SshMachine,
72
- command: string,
73
- ): ChildProcessWithoutNullStreams {
74
- return spawn(
75
- "ssh",
76
- ["-T", "-o", "BatchMode=yes", "--", config.target, command],
77
- {
78
- stdio: ["pipe", "pipe", "pipe"],
79
- },
80
- );
81
- }
82
-
83
- export function appendBounded(current: string, chunk: Buffer): string {
84
- const next = current + chunk.toString("utf8");
85
- return next.length <= MAX_DIAGNOSTIC_BYTES
86
- ? next
87
- : next.slice(next.length - MAX_DIAGNOSTIC_BYTES);
88
- }
89
-
90
- async function deploy(
91
- config: SshMachine,
92
- source: Buffer,
93
- target: string,
94
- ): Promise<void> {
95
- const command = [
96
- "node",
97
- "-e",
98
- shellQuote(DEPLOY_SOURCE),
99
- shellQuote(target),
100
- ].join(" ");
101
- const child = spawnConnector(config, command);
102
- let stdout = "";
103
- let stderr = "";
104
- child.stdout.on("data", (chunk: Buffer) => {
105
- stdout = appendBounded(stdout, chunk);
106
- });
107
- child.stderr.on("data", (chunk: Buffer) => {
108
- stderr = appendBounded(stderr, chunk);
109
- });
110
- await new Promise<void>((resolve, reject) => {
111
- const timer = setTimeout(() => {
112
- child.kill();
113
- reject(new Error("remote bridge deployment timed out"));
114
- }, DEPLOY_TIMEOUT_MS);
115
- timer.unref();
116
- child.stdin.on("error", (error) => {
117
- clearTimeout(timer);
118
- child.kill();
119
- reject(error);
120
- });
121
- child.on("error", (error) => {
122
- clearTimeout(timer);
123
- reject(error);
124
- });
125
- child.on("exit", (code) => {
126
- clearTimeout(timer);
127
- if (code === 0) resolve();
128
- else
129
- reject(
130
- new Error(
131
- stderr.trim() ||
132
- stdout.trim() ||
133
- `remote bridge deployment exited ${code ?? "without status"}`,
134
- ),
135
- );
136
- });
137
- child.stdin.end(source);
138
- });
139
- }
140
-
141
- export async function connectRemoteBridge(
142
- config: SshMachine,
143
- ): Promise<ChildProcessWithoutNullStreams> {
144
- const [
145
- source,
146
- peerSource,
147
- channelSource,
148
- sessionSource,
149
- contextSource,
150
- checkpointSource,
151
- sessionWatchSource,
152
- ] = await Promise.all([
153
- readFile(fileURLToPath(new URL("./shepherdr.mjs", import.meta.url))),
154
- readFile(fileURLToPath(new URL("./shepherdr-peer.mjs", import.meta.url))),
155
- readFile(
156
- fileURLToPath(new URL("./shepherdr-channel.mjs", import.meta.url)),
157
- ),
158
- readFile(
159
- fileURLToPath(new URL("./shepherdr-session.mjs", import.meta.url)),
160
- ),
161
- readFile(
162
- fileURLToPath(new URL("./shepherdr-context.mjs", import.meta.url)),
163
- ),
164
- readFile(
165
- fileURLToPath(new URL("./shepherdr-checkpoints.mjs", import.meta.url)),
166
- ),
167
- readFile(
168
- fileURLToPath(new URL("./shepherdr-session-watch.mjs", import.meta.url)),
169
- ),
170
- ]);
171
- await deploy(config, peerSource, REMOTE_PEER_HELPER);
172
- await deploy(config, channelSource, REMOTE_CHANNEL_HELPER);
173
- await deploy(config, sessionSource, REMOTE_SESSION_HELPER);
174
- await deploy(config, sessionWatchSource, REMOTE_SESSION_WATCH_HELPER);
175
- await deploy(config, checkpointSource, REMOTE_CHECKPOINT_HELPER);
176
- await deploy(config, contextSource, REMOTE_CONTEXT_HELPER);
177
- await deploy(config, source, REMOTE_HELPER);
178
- return spawnConnector(config, remoteCommand(config));
179
- }
@@ -1,89 +0,0 @@
1
- import type { ChildProcessWithoutNullStreams } from "node:child_process";
2
- import { randomUUID } from "node:crypto";
3
- import type { PaneInfo, PeerChannel } from "../types.js";
4
- import { createBufferedChannel } from "./shepherdr-channel.mjs";
5
-
6
- /** Owns admitted duplex channels; the transport closes all on its single teardown path. */
7
- export class FocusChannels {
8
- private readonly channels = new Map<
9
- string,
10
- ReturnType<typeof createBufferedChannel>
11
- >();
12
- private readonly child: ChildProcessWithoutNullStreams;
13
- private readonly closed: () => boolean;
14
- constructor(child: ChildProcessWithoutNullStreams, closed: () => boolean) {
15
- this.child = child;
16
- this.closed = closed;
17
- }
18
- async open(
19
- agent: PaneInfo,
20
- request: import("../types.js").FocusRequest,
21
- call: (
22
- message: Record<string, unknown>,
23
- timeoutMs: number,
24
- ) => Promise<unknown>,
25
- ): Promise<PeerChannel> {
26
- const id = randomUUID();
27
- const state = createBufferedChannel(
28
- (message) => {
29
- const frame = `${JSON.stringify({ op: "channel_send", id, message })}\n`;
30
- if (
31
- this.closed() ||
32
- this.child.stdin.writableLength + Buffer.byteLength(frame) >
33
- 1024 * 1024
34
- )
35
- throw new Error(
36
- "Focus channel output is unavailable or exceeded its buffer",
37
- );
38
- this.child.stdin.write(frame);
39
- },
40
- () => {
41
- this.channels.delete(id);
42
- if (!this.closed())
43
- this.child.stdin.write(
44
- `${JSON.stringify({ op: "channel_close", id })}\n`,
45
- );
46
- },
47
- );
48
- this.channels.set(id, state);
49
- try {
50
- const result = await call(
51
- { op: "channel_open", id, agent, request },
52
- 60_000,
53
- );
54
- if (
55
- !result ||
56
- typeof result !== "object" ||
57
- !("admitted" in result) ||
58
- result.admitted !== true
59
- )
60
- throw new Error("Focus channel was not admitted");
61
- return state.channel;
62
- } catch (error) {
63
- state.fail(error instanceof Error ? error : new Error(String(error)));
64
- throw error;
65
- }
66
- }
67
-
68
- consume(value: Record<string, unknown>): boolean {
69
- if (value["type"] === "channel" && typeof value["channel"] === "string") {
70
- const state = this.channels.get(value["channel"]);
71
- if (value["closed"] === true)
72
- state?.fail(
73
- new Error(
74
- typeof value["error"] === "string"
75
- ? value["error"]
76
- : "Focus channel disconnected",
77
- ),
78
- );
79
- else if ("message" in value) state?.receive(value["message"]);
80
- else state?.fail(new Error("Invalid focus channel frame"));
81
- return true;
82
- }
83
- return false;
84
- }
85
- failAll(error: Error) {
86
- for (const state of this.channels.values()) state.fail(error);
87
- this.channels.clear();
88
- }
89
- }