@howaboua/pi-shepherdr 0.2.23 → 0.2.24
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/CHANGELOG.md +7 -18
- package/README.md +63 -60
- package/biome.json +1 -1
- package/index.ts +2 -2
- package/package.json +14 -9
- package/src/agents-discovery.ts +41 -26
- package/src/agents-focus.ts +2 -2
- package/src/agents-reply.ts +1 -1
- package/src/agents-spawn.ts +2 -2
- package/src/agents-tool.ts +2 -2
- package/src/agents-work.ts +3 -3
- package/src/attachment-notes.ts +14 -2
- package/src/attachment-protocol.ts +21 -4
- package/src/attachment-routing.ts +1 -1
- package/src/attachment-target.ts +5 -2
- package/src/attachment.ts +2 -3
- package/src/board/host.ts +2 -2
- package/src/board/membership.ts +1 -1
- package/src/{control-panel-actions.ts → control-panel/actions.ts} +2 -2
- package/src/{control-panel.ts → control-panel/index.ts} +4 -4
- package/src/{control-panel-items.ts → control-panel/items.ts} +3 -3
- package/src/{control-panel-view.ts → control-panel/view.ts} +3 -3
- package/src/controller.ts +6 -3
- package/src/fleet/index.ts +384 -0
- package/src/fleet/inspection.ts +141 -0
- package/src/fleet/machine.ts +52 -0
- package/src/fleet/monitor.ts +71 -0
- package/src/fleet/persistence.ts +42 -0
- package/src/fleet/reconnect.ts +75 -0
- package/src/fleet/setup.ts +92 -0
- package/src/messages/attachments.ts +39 -0
- package/src/messages/attribution.ts +75 -0
- package/src/messages/event-details.ts +69 -0
- package/src/messages/events.ts +189 -0
- package/src/messages/focus.ts +38 -0
- package/src/messages/guidance.ts +83 -0
- package/src/messages/render.ts +89 -0
- package/src/messages/voice-announcements.ts +79 -0
- package/src/monitor.ts +2 -2
- package/src/peer-inbox.ts +2 -2
- package/src/remote/client.ts +127 -0
- package/src/remote/context-relay.ts +84 -0
- package/src/remote/deployment.ts +179 -0
- package/src/remote/focus-channels.ts +89 -0
- package/src/remote/transport.ts +369 -0
- package/src/settlement/collector.ts +122 -0
- package/src/settlement/delivery.ts +144 -0
- package/src/{settlement.ts → settlement/index.ts} +23 -233
- package/src/shared-context-host.ts +33 -11
- package/src/shared-context.ts +10 -4
- package/src/spawn-context.ts +20 -17
- package/src/voice-handoff.ts +1 -1
- package/src/worker-service.ts +2 -2
- package/src/fleet.ts +0 -702
- package/src/messages.ts +0 -637
- package/src/remote-client.ts +0 -761
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
/** Owns relay handlers and incoming cancellation; transport shutdown aborts the whole relay. */
|
|
4
|
+
export class ContextRelay {
|
|
5
|
+
private contextHandler:
|
|
6
|
+
| ((request: unknown, signal: AbortSignal) => Promise<unknown>)
|
|
7
|
+
| undefined;
|
|
8
|
+
private readonly incomingContext = new Map<string, AbortController>();
|
|
9
|
+
private relayPath: string | undefined;
|
|
10
|
+
private readonly child: ChildProcessWithoutNullStreams;
|
|
11
|
+
private readonly closed: () => boolean;
|
|
12
|
+
private readonly maxFrameBytes: number;
|
|
13
|
+
constructor(
|
|
14
|
+
child: ChildProcessWithoutNullStreams,
|
|
15
|
+
closed: () => boolean,
|
|
16
|
+
maxFrameBytes: number,
|
|
17
|
+
) {
|
|
18
|
+
this.child = child;
|
|
19
|
+
this.closed = closed;
|
|
20
|
+
this.maxFrameBytes = maxFrameBytes;
|
|
21
|
+
}
|
|
22
|
+
async start(
|
|
23
|
+
threadId: string,
|
|
24
|
+
routeId: string,
|
|
25
|
+
handler: (request: unknown, signal: AbortSignal) => Promise<unknown>,
|
|
26
|
+
call: (message: Record<string, unknown>) => Promise<unknown>,
|
|
27
|
+
): Promise<void> {
|
|
28
|
+
this.contextHandler = handler;
|
|
29
|
+
const path = await call({ op: "context_relay", threadId, routeId });
|
|
30
|
+
if (typeof path !== "string")
|
|
31
|
+
throw new Error("Invalid shared context relay path");
|
|
32
|
+
this.relayPath = path;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
path(): string {
|
|
36
|
+
if (!this.relayPath)
|
|
37
|
+
throw new Error(
|
|
38
|
+
"Shared context relay is unavailable; open /herdr → Status to retry",
|
|
39
|
+
);
|
|
40
|
+
return this.relayPath;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
consume(id: string, value: Record<string, unknown>): boolean {
|
|
44
|
+
if (value["type"] === "context_cancel") {
|
|
45
|
+
this.incomingContext.get(id)?.abort();
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
if (value["type"] === "context_request") {
|
|
49
|
+
const controller = new AbortController();
|
|
50
|
+
this.incomingContext.set(id, controller);
|
|
51
|
+
const reply = (data: object) => {
|
|
52
|
+
if (this.closed() || controller.signal.aborted) return;
|
|
53
|
+
let frame = `${JSON.stringify({ op: "context_reply", id, ...data })}\n`;
|
|
54
|
+
if (Buffer.byteLength(frame) > this.maxFrameBytes)
|
|
55
|
+
frame = `${JSON.stringify({ op: "context_reply", id, ok: false, error: "Shared context response is too large" })}\n`;
|
|
56
|
+
this.child.stdin.write(frame);
|
|
57
|
+
};
|
|
58
|
+
void Promise.resolve()
|
|
59
|
+
.then(() => {
|
|
60
|
+
if (!this.contextHandler)
|
|
61
|
+
throw new Error("Shared context controller is unavailable");
|
|
62
|
+
controller.signal.throwIfAborted();
|
|
63
|
+
return this.contextHandler(value["request"], controller.signal);
|
|
64
|
+
})
|
|
65
|
+
.then(
|
|
66
|
+
(result) => reply({ ok: true, result }),
|
|
67
|
+
(error: unknown) =>
|
|
68
|
+
reply({
|
|
69
|
+
ok: false,
|
|
70
|
+
error: error instanceof Error ? error.message : String(error),
|
|
71
|
+
}),
|
|
72
|
+
)
|
|
73
|
+
.finally(() => {
|
|
74
|
+
this.incomingContext.delete(id);
|
|
75
|
+
});
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
abortAll() {
|
|
81
|
+
for (const controller of this.incomingContext.values()) controller.abort();
|
|
82
|
+
this.incomingContext.clear();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
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 = 22;
|
|
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
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
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
|
+
}
|