@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.
- package/README.md +48 -48
- package/index.ts +7 -1
- package/package.json +2 -9
- package/src/agent-identity.ts +3 -3
- package/src/agents-contract.ts +10 -14
- package/src/agents-discovery.ts +34 -54
- package/src/agents-focus.ts +23 -35
- package/src/agents-spawn.ts +24 -23
- package/src/agents-tool.ts +4 -19
- package/src/agents-work.ts +4 -3
- package/src/attachment-notes.ts +8 -7
- package/src/attachment-protocol.ts +3 -2
- package/src/attachment-routing.ts +1 -1
- package/src/attachment-target.ts +10 -18
- package/src/attachment.ts +14 -9
- package/src/board/discovery.ts +67 -0
- package/src/board/host.ts +93 -63
- package/src/board/membership.ts +16 -11
- package/src/board/records.ts +185 -0
- package/src/close-idle.ts +2 -9
- package/src/context-briefing.ts +230 -0
- package/src/context-routing.ts +1 -1
- package/src/control-panel/index.ts +2 -4
- package/src/control-panel/items.ts +1 -11
- package/src/control-panel/view.ts +3 -9
- package/src/controller.ts +50 -63
- package/src/core/AGENTS.md +7 -4
- package/src/core/admission-broker.mjs +235 -0
- package/src/core/admission-endpoint.ts +180 -0
- package/src/core/admission-owner.mjs +52 -0
- package/src/core/admission-startup.ts +130 -0
- package/src/core/admission.ts +181 -0
- package/src/core/agents.ts +102 -0
- package/src/core/backend.ts +110 -84
- package/src/core/client.ts +16 -0
- package/src/core/focus-channel-helper.mjs +6 -27
- package/src/core/focus-channel-wire.mjs +9 -6
- package/src/core/focus-channel.ts +5 -3
- package/src/core/focus-window.ts +15 -4
- package/src/core/helper-bootstrap.mjs +70 -0
- package/src/core/helper-context.mjs +98 -0
- package/src/core/helper-core.mjs +121 -0
- package/src/core/machines.ts +30 -0
- package/src/core/registration.ts +7 -2
- package/src/core/remote-endpoint.ts +80 -74
- package/src/core/remote-helper.ts +330 -0
- package/src/core/remote-reader.ts +3 -42
- package/src/core/remote-service.mjs +239 -0
- package/src/core/remote-transport.ts +290 -0
- package/src/core/spawn.ts +20 -20
- package/src/core/terminal-peer.mjs +57 -0
- package/src/core/terminal.ts +4 -2
- package/src/delivery.ts +39 -0
- package/src/fleet/catalog.ts +16 -58
- package/src/fleet/connection.ts +9 -18
- package/src/fleet/index.ts +38 -76
- package/src/fleet/inspection.ts +9 -22
- package/src/fleet/machine.ts +28 -12
- package/src/fleet/monitor.ts +8 -29
- package/src/fleet/persistence.ts +2 -2
- package/src/fleet/reconnect.ts +5 -3
- package/src/fleet/setup.ts +6 -16
- package/src/launch-contract.ts +0 -3
- package/src/launch-location.ts +4 -132
- package/src/launch.ts +18 -144
- package/src/messages/attachments.ts +1 -1
- package/src/messages/attribution.ts +9 -14
- package/src/messages/events.ts +5 -9
- package/src/messages/focus.ts +2 -7
- package/src/messages/guidance.ts +2 -43
- package/src/messages/peer-sharing.ts +133 -0
- package/src/monitor-events.ts +14 -43
- package/src/monitor-extension-messages.ts +5 -6
- package/src/monitor-record.ts +4 -0
- package/src/monitor-state.ts +1 -101
- package/src/monitor.ts +38 -66
- package/src/notes-discovery.ts +67 -0
- package/src/peer-inbox.ts +21 -27
- package/src/remote/context-relay.ts +25 -10
- package/src/remote/notes-reader.mjs +67 -0
- package/src/remote/shepherdr-channel.mjs +1 -1
- package/src/remote/shepherdr-checkpoints.mjs +49 -102
- package/src/remote/shepherdr-context.mjs +1 -1
- package/src/remote/shepherdr-peer.mjs +19 -24
- package/src/remote/shepherdr-session.mjs +18 -7
- package/src/saved-context.ts +62 -0
- package/src/settlement/collector.ts +29 -9
- package/src/settlement/index.ts +35 -6
- package/src/shared-context-host.ts +27 -33
- package/src/shared-context.ts +44 -15
- package/src/spawn-context.ts +11 -3
- package/src/spawn-ownership.ts +1 -1
- package/src/ssh-setup.ts +40 -73
- package/src/types.ts +4 -8
- package/src/voice-handoff.ts +1 -1
- package/src/widget.ts +7 -2
- package/src/worker-ownership.ts +2 -8
- package/src/worker-service.ts +6 -14
- package/src/core/context-relay.mjs +0 -111
- package/src/core/remote-exec.ts +0 -122
- package/src/core/remote-helper.mjs +0 -244
- package/src/core/remote-relay.ts +0 -191
- package/src/core/request.ts +0 -124
- package/src/herdr-client.ts +0 -364
- package/src/herdr.ts +0 -268
- package/src/machine-catalog.ts +0 -83
- package/src/monitor-event.ts +0 -84
- package/src/remote/client.ts +0 -127
- package/src/remote/deployment.ts +0 -179
- package/src/remote/focus-channels.ts +0 -89
- package/src/remote/shepherdr.mjs +0 -504
- package/src/remote/transport.ts +0 -369
package/src/types.ts
CHANGED
|
@@ -18,7 +18,7 @@ export interface PeerCommand {
|
|
|
18
18
|
description: string;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
interface MachineIdentity {
|
|
22
22
|
host: string;
|
|
23
23
|
session: string;
|
|
24
24
|
}
|
|
@@ -71,8 +71,8 @@ interface AgentSessionInfo {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
export interface PaneInfo {
|
|
74
|
-
backend?: "core";
|
|
75
74
|
agent?: string | null;
|
|
75
|
+
worker?: true;
|
|
76
76
|
agent_session?: AgentSessionInfo | null;
|
|
77
77
|
agent_status: AgentStatus;
|
|
78
78
|
cwd?: string | null;
|
|
@@ -94,7 +94,7 @@ interface TabInfo {
|
|
|
94
94
|
workspace_id: string;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
|
|
97
|
+
interface WorkspaceInfo {
|
|
98
98
|
label: string;
|
|
99
99
|
workspace_id: string;
|
|
100
100
|
}
|
|
@@ -107,6 +107,7 @@ export interface SessionSnapshot {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
export interface MonitoredAgent {
|
|
110
|
+
readonly worker?: true;
|
|
110
111
|
readonly instructions?: string;
|
|
111
112
|
readonly latestInstruction?: string;
|
|
112
113
|
readonly scope: "task" | "persistent";
|
|
@@ -175,8 +176,3 @@ export interface SessionView {
|
|
|
175
176
|
assistantAfterInput?: boolean;
|
|
176
177
|
input?: LatestInput;
|
|
177
178
|
}
|
|
178
|
-
|
|
179
|
-
export interface HerdrEvent {
|
|
180
|
-
data: Record<string, unknown>;
|
|
181
|
-
event: string;
|
|
182
|
-
}
|
package/src/voice-handoff.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type {
|
|
|
3
3
|
ExtensionAPI,
|
|
4
4
|
ExtensionContext,
|
|
5
5
|
} from "@earendil-works/pi-coding-agent";
|
|
6
|
-
import { recordContextBriefing } from "
|
|
6
|
+
import { recordContextBriefing } from "./context-briefing.js";
|
|
7
7
|
import { voiceFocusContinuity, voiceFocusReport } from "./messages/focus.js";
|
|
8
8
|
import { registerFocusChannelReceiver } from "./peer-channel.js";
|
|
9
9
|
import type { FocusRequest, PaneInfo, PeerChannel } from "./types.js";
|
package/src/widget.ts
CHANGED
|
@@ -64,7 +64,12 @@ export function renderAgentWidget(
|
|
|
64
64
|
machines: MachineStatus[],
|
|
65
65
|
): void {
|
|
66
66
|
if (!ctx?.hasUI) return;
|
|
67
|
-
|
|
67
|
+
// Role comes from the live session, not watch scope or Core ancestry.
|
|
68
|
+
agents = agents.filter((agent) => !agent.worker);
|
|
69
|
+
if (
|
|
70
|
+
agents.length === 0 &&
|
|
71
|
+
!machines.some((machine) => machine.monitoringIssue)
|
|
72
|
+
) {
|
|
68
73
|
ctx.ui.setWidget(WIDGET_ID, undefined);
|
|
69
74
|
return;
|
|
70
75
|
}
|
|
@@ -79,7 +84,7 @@ export function renderAgentWidget(
|
|
|
79
84
|
machines.map((machine) => [machine.id, machine]),
|
|
80
85
|
);
|
|
81
86
|
const lines = [
|
|
82
|
-
`${theme.fg("accent", "╭─ herdr
|
|
87
|
+
`${theme.fg("accent", "╭─ herdr peers")} ${theme.fg("dim", `${agents.length} · ${machines.filter((machine) => machine.status === "connected").length}/${machines.length} machines`)}`,
|
|
83
88
|
...ordered
|
|
84
89
|
.slice(0, MAX_VISIBLE_AGENTS)
|
|
85
90
|
.map((agent) => agentLine(ctx, agent, machinesByName)),
|
package/src/worker-ownership.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import {
|
|
2
|
+
import { getSnapshot, sessionPath } from "./core/agents.js";
|
|
3
3
|
import type { AgentFleet } from "./fleet/index.js";
|
|
4
|
-
import { getSnapshot, sessionPath } from "./herdr.js";
|
|
5
4
|
import type { PaneInfo } from "./types.js";
|
|
6
5
|
|
|
7
6
|
export const ENTRY = "herdr-owned-worker-v1";
|
|
@@ -69,12 +68,7 @@ export async function panelFor(
|
|
|
69
68
|
);
|
|
70
69
|
if (panel) {
|
|
71
70
|
if (
|
|
72
|
-
panel.pane_id ===
|
|
73
|
-
process.env[
|
|
74
|
-
fleet.connected().client instanceof CoreConnection
|
|
75
|
-
? "HC_TERMINAL_ID"
|
|
76
|
-
: "HERDR_PANE_ID"
|
|
77
|
-
] ||
|
|
71
|
+
panel.pane_id === process.env["HC_TERMINAL_ID"] ||
|
|
78
72
|
panel.terminal_id !== worker.terminal ||
|
|
79
73
|
panel.agent !== "pi" ||
|
|
80
74
|
sessionPath(panel) !== worker.sessionPath
|
package/src/worker-service.ts
CHANGED
|
@@ -6,9 +6,8 @@ import type {
|
|
|
6
6
|
import { activityTask } from "./activity.js";
|
|
7
7
|
import { dispatchAgentWork, reserveAgentName } from "./agents-work.js";
|
|
8
8
|
import type { AgentBoard } from "./board/host.js";
|
|
9
|
-
import {
|
|
9
|
+
import { sessionPath } from "./core/agents.js";
|
|
10
10
|
import type { AgentFleet } from "./fleet/index.js";
|
|
11
|
-
import { sessionPath } from "./herdr.js";
|
|
12
11
|
import {
|
|
13
12
|
RolledBackAgentLaunchError,
|
|
14
13
|
rollbackStartedAgent,
|
|
@@ -66,7 +65,7 @@ export function registerWorkerService(
|
|
|
66
65
|
if (!worker) throw new Error("Worker is not owned by this session branch");
|
|
67
66
|
const identity = await fleet.connected().client.machineIdentity();
|
|
68
67
|
if (identity.host !== worker.host || identity.session !== worker.session)
|
|
69
|
-
throw new Error("Worker belongs to another host or
|
|
68
|
+
throw new Error("Worker belongs to another host or Core");
|
|
70
69
|
return worker;
|
|
71
70
|
}
|
|
72
71
|
|
|
@@ -196,9 +195,6 @@ export function registerWorkerService(
|
|
|
196
195
|
const boardBinding = worker
|
|
197
196
|
? undefined
|
|
198
197
|
: await board.prepare(ctx, runtime, name, args);
|
|
199
|
-
const workspace = runtime.local
|
|
200
|
-
? process.env["HERDR_WORKSPACE_ID"]
|
|
201
|
-
: undefined;
|
|
202
198
|
stage = "launch";
|
|
203
199
|
const started = await startAgent(
|
|
204
200
|
runtime.client,
|
|
@@ -206,13 +202,7 @@ export function registerWorkerService(
|
|
|
206
202
|
name,
|
|
207
203
|
label: input.title,
|
|
208
204
|
cwd: input.cwd,
|
|
209
|
-
placement:
|
|
210
|
-
runtime.client instanceof CoreConnection
|
|
211
|
-
? "terminal"
|
|
212
|
-
: workspace
|
|
213
|
-
? "new_tab"
|
|
214
|
-
: "new_workspace",
|
|
215
|
-
...(workspace ? { workspace } : {}),
|
|
205
|
+
placement: "terminal",
|
|
216
206
|
},
|
|
217
207
|
runtime.fallbackCwd,
|
|
218
208
|
runtime.resolveDirectory,
|
|
@@ -222,10 +212,11 @@ export function registerWorkerService(
|
|
|
222
212
|
const relationshipResult = await relationship?.accept(
|
|
223
213
|
started.agent,
|
|
224
214
|
);
|
|
225
|
-
await boardBinding?.accept(started.agent);
|
|
215
|
+
const boardResult = await boardBinding?.accept(started.agent);
|
|
226
216
|
const sharingResult = await sharing?.accept(started.agent);
|
|
227
217
|
for (const warning of [
|
|
228
218
|
relationshipResult?.warning,
|
|
219
|
+
boardResult?.warning,
|
|
229
220
|
sharingResult?.warning,
|
|
230
221
|
])
|
|
231
222
|
if (warning) ctx.ui.notify(warning, "warning");
|
|
@@ -265,6 +256,7 @@ export function registerWorkerService(
|
|
|
265
256
|
input.prompt,
|
|
266
257
|
"task",
|
|
267
258
|
);
|
|
259
|
+
attributed.context += `\n${await shared.peerReplyContext(ctx, runtime, panel)}`;
|
|
268
260
|
assertBranch();
|
|
269
261
|
const dispatch = await dispatchAgentWork(
|
|
270
262
|
runtime,
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
// Bounded machine.exec cannot stream. This leased mailbox retains requests only
|
|
2
|
-
// until their authenticated caller disconnects; a lost poll is never replayed.
|
|
3
|
-
// A 1,000,000-byte Notes file can expand sixfold in JSON (e.g. NULs).
|
|
4
|
-
// Leave room for Notes envelopes; context frames and raw exec stdin/stdout
|
|
5
|
-
// must remain below 8 MiB, and base64 Core frames below 16 MiB.
|
|
6
|
-
const RELAY_PAYLOAD_BYTES = 6_250_000;
|
|
7
|
-
const BATCH_BYTES = 7 * 1024 * 1024;
|
|
8
|
-
|
|
9
|
-
export async function serveRelay(input) {
|
|
10
|
-
const { listenContext, relayContextPath } = await import(
|
|
11
|
-
input.modules.context
|
|
12
|
-
);
|
|
13
|
-
const path = relayContextPath(input.threadId, input.routeId);
|
|
14
|
-
const pending = new Map();
|
|
15
|
-
let sequence = 0;
|
|
16
|
-
let wake;
|
|
17
|
-
let lastPoll = Date.now();
|
|
18
|
-
let closed = false;
|
|
19
|
-
const close = await listenContext(path, async (request, signal) => {
|
|
20
|
-
signal.throwIfAborted();
|
|
21
|
-
if (request?.relayToken === input.relayToken) {
|
|
22
|
-
if (request.action === "close") {
|
|
23
|
-
setImmediate(shutdown);
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
if (request.action === "reply") {
|
|
27
|
-
const entry = pending.get(request.id);
|
|
28
|
-
if (entry) {
|
|
29
|
-
pending.delete(request.id);
|
|
30
|
-
if (request.ok) entry.resolve(request.result);
|
|
31
|
-
else entry.reject(new Error(request.error));
|
|
32
|
-
}
|
|
33
|
-
return true;
|
|
34
|
-
}
|
|
35
|
-
if (request.action !== "poll") throw new Error("Invalid relay action");
|
|
36
|
-
lastPoll = Date.now();
|
|
37
|
-
if (![...pending.values()].some((entry) => !entry.sent))
|
|
38
|
-
await new Promise((resolve) => {
|
|
39
|
-
const timer = setTimeout(done, 8_000);
|
|
40
|
-
function done() {
|
|
41
|
-
clearTimeout(timer);
|
|
42
|
-
signal.removeEventListener("abort", done);
|
|
43
|
-
wake = undefined;
|
|
44
|
-
resolve();
|
|
45
|
-
}
|
|
46
|
-
wake = done;
|
|
47
|
-
signal.addEventListener("abort", done, { once: true });
|
|
48
|
-
});
|
|
49
|
-
const requests = [];
|
|
50
|
-
let bytes = 1024; // active IDs and RPC/receipt envelopes
|
|
51
|
-
for (const [id, entry] of pending) {
|
|
52
|
-
if (entry.sent) continue;
|
|
53
|
-
const value = { id, request: entry.request };
|
|
54
|
-
const size = Buffer.byteLength(JSON.stringify(value));
|
|
55
|
-
if (bytes + size > BATCH_BYTES) break;
|
|
56
|
-
requests.push(value);
|
|
57
|
-
bytes += size;
|
|
58
|
-
entry.sent = true;
|
|
59
|
-
}
|
|
60
|
-
return { requests, active: [...pending.keys()] };
|
|
61
|
-
}
|
|
62
|
-
if (closed) throw new Error("Shared context controller disconnected");
|
|
63
|
-
if (
|
|
64
|
-
pending.size >= 8 ||
|
|
65
|
-
Buffer.byteLength(JSON.stringify(request)) > RELAY_PAYLOAD_BYTES
|
|
66
|
-
)
|
|
67
|
-
throw new Error(
|
|
68
|
-
"Shared context relay is busy or request is too large; narrow the request and retry",
|
|
69
|
-
);
|
|
70
|
-
return new Promise((resolve, reject) => {
|
|
71
|
-
const id = String(++sequence);
|
|
72
|
-
const abort = () => {
|
|
73
|
-
pending.delete(id);
|
|
74
|
-
reject(new Error("Shared context caller disconnected"));
|
|
75
|
-
wake?.();
|
|
76
|
-
};
|
|
77
|
-
pending.set(id, {
|
|
78
|
-
request,
|
|
79
|
-
sent: false,
|
|
80
|
-
resolve: (value) => {
|
|
81
|
-
signal.removeEventListener("abort", abort);
|
|
82
|
-
resolve(value);
|
|
83
|
-
},
|
|
84
|
-
reject: (error) => {
|
|
85
|
-
signal.removeEventListener("abort", abort);
|
|
86
|
-
reject(error);
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
90
|
-
wake?.();
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
async function shutdown() {
|
|
94
|
-
if (closed) return;
|
|
95
|
-
closed = true;
|
|
96
|
-
clearInterval(lease);
|
|
97
|
-
wake?.();
|
|
98
|
-
for (const entry of pending.values())
|
|
99
|
-
entry.reject(
|
|
100
|
-
new Error(
|
|
101
|
-
"Shared context controller disconnected; reconnect its machine",
|
|
102
|
-
),
|
|
103
|
-
);
|
|
104
|
-
pending.clear();
|
|
105
|
-
await close();
|
|
106
|
-
}
|
|
107
|
-
const lease = setInterval(() => {
|
|
108
|
-
if (Date.now() - lastPoll > 30_000) void shutdown();
|
|
109
|
-
}, 1_000);
|
|
110
|
-
process.once("SIGTERM", () => void shutdown());
|
|
111
|
-
}
|
package/src/core/remote-exec.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises";
|
|
2
|
-
import type { CoreClient, CoreRequestOptions } from "./client.js";
|
|
3
|
-
import { type CoreObject, object } from "./protocol.js";
|
|
4
|
-
|
|
5
|
-
export interface CoreMachine {
|
|
6
|
-
id: string;
|
|
7
|
-
name: string;
|
|
8
|
-
host: string;
|
|
9
|
-
session: string;
|
|
10
|
-
state: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export async function coreMachines(client: CoreClient): Promise<CoreMachine[]> {
|
|
14
|
-
const machines = object(await client.request("machine.list"))["machines"];
|
|
15
|
-
if (!Array.isArray(machines))
|
|
16
|
-
throw new Error("Core returned no machine catalog");
|
|
17
|
-
return machines.map((value) => {
|
|
18
|
-
const machine = object(value);
|
|
19
|
-
for (const field of ["id", "name", "host", "state"])
|
|
20
|
-
if (typeof machine[field] !== "string" || !machine[field])
|
|
21
|
-
throw new Error("Core returned an invalid machine identity");
|
|
22
|
-
return {
|
|
23
|
-
id: machine["id"] as string,
|
|
24
|
-
name: machine["name"] as string,
|
|
25
|
-
host: machine["host"] as string,
|
|
26
|
-
state: machine["state"] as string,
|
|
27
|
-
session:
|
|
28
|
-
typeof machine["session"] === "string" ? machine["session"] : "default",
|
|
29
|
-
};
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const moduleUrl = (source: string) =>
|
|
34
|
-
`data:text/javascript;base64,${Buffer.from(source).toString("base64")}`;
|
|
35
|
-
let bundle:
|
|
36
|
-
| Promise<{ source: string; modules: Record<string, string> }>
|
|
37
|
-
| undefined;
|
|
38
|
-
async function sources() {
|
|
39
|
-
const read = (path: string) =>
|
|
40
|
-
readFile(new URL(path, import.meta.url), "utf8");
|
|
41
|
-
const [source, peer, session, watch, checkpoints, context, relay, pi] =
|
|
42
|
-
await Promise.all([
|
|
43
|
-
readFile(new URL("./remote-helper.mjs", import.meta.url), "utf8"),
|
|
44
|
-
read("../remote/shepherdr-peer.mjs"),
|
|
45
|
-
read("../remote/shepherdr-session.mjs"),
|
|
46
|
-
read("../remote/shepherdr-session-watch.mjs"),
|
|
47
|
-
read("../remote/shepherdr-checkpoints.mjs"),
|
|
48
|
-
read("../remote/shepherdr-context.mjs"),
|
|
49
|
-
read("./context-relay.mjs"),
|
|
50
|
-
read("./pi-executable.mjs"),
|
|
51
|
-
]);
|
|
52
|
-
const peerUrl = moduleUrl(peer);
|
|
53
|
-
return {
|
|
54
|
-
source,
|
|
55
|
-
modules: {
|
|
56
|
-
pi: moduleUrl(pi),
|
|
57
|
-
relay: moduleUrl(relay),
|
|
58
|
-
peer: peerUrl,
|
|
59
|
-
session: moduleUrl(session),
|
|
60
|
-
watch: moduleUrl(watch),
|
|
61
|
-
context: moduleUrl(
|
|
62
|
-
context
|
|
63
|
-
.replace('"./shepherdr-peer.mjs"', JSON.stringify(peerUrl))
|
|
64
|
-
.replace(
|
|
65
|
-
'"./shepherdr-checkpoints.mjs"',
|
|
66
|
-
JSON.stringify(moduleUrl(checkpoints)),
|
|
67
|
-
),
|
|
68
|
-
),
|
|
69
|
-
},
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** One bounded remote process; never retries a possibly delivered operation. */
|
|
74
|
-
export async function remoteExec(
|
|
75
|
-
client: CoreClient,
|
|
76
|
-
machine: CoreMachine,
|
|
77
|
-
input: CoreObject,
|
|
78
|
-
options: CoreRequestOptions = {},
|
|
79
|
-
): Promise<unknown> {
|
|
80
|
-
const { source, modules } = await (bundle ??= sources());
|
|
81
|
-
const result = object(
|
|
82
|
-
await client.request(
|
|
83
|
-
"machine.exec",
|
|
84
|
-
{
|
|
85
|
-
on: machine.id,
|
|
86
|
-
argv: ["node", "--input-type=module", "-e", source],
|
|
87
|
-
timeout: input["op"] === "watch" ? 20 : 90,
|
|
88
|
-
input: Buffer.from(
|
|
89
|
-
JSON.stringify({
|
|
90
|
-
...input,
|
|
91
|
-
coreId: machine.id,
|
|
92
|
-
session: machine.session,
|
|
93
|
-
modules,
|
|
94
|
-
}),
|
|
95
|
-
).toString("base64"),
|
|
96
|
-
},
|
|
97
|
-
{ timeoutMs: 100_000, ...options },
|
|
98
|
-
),
|
|
99
|
-
);
|
|
100
|
-
if (
|
|
101
|
-
result["timed_out"] ||
|
|
102
|
-
result["truncated"] ||
|
|
103
|
-
result["code"] !== 0 ||
|
|
104
|
-
typeof result["stdout"] !== "string"
|
|
105
|
-
)
|
|
106
|
-
throw new Error(
|
|
107
|
-
"Remote operation did not finish; inspect the target before retrying and verify Node.js and hc are available in the selected Core's PATH",
|
|
108
|
-
);
|
|
109
|
-
const receipt = object(
|
|
110
|
-
JSON.parse(Buffer.from(result["stdout"], "base64").toString()),
|
|
111
|
-
);
|
|
112
|
-
if (receipt["ok"] !== true)
|
|
113
|
-
throw Object.assign(
|
|
114
|
-
new Error(
|
|
115
|
-
typeof receipt["error"] === "string"
|
|
116
|
-
? receipt["error"]
|
|
117
|
-
: "Remote operation failed",
|
|
118
|
-
),
|
|
119
|
-
{ code: receipt["code"] },
|
|
120
|
-
);
|
|
121
|
-
return receipt["result"];
|
|
122
|
-
}
|
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
-
import { homedir, hostname, tmpdir } from "node:os";
|
|
4
|
-
import { join, resolve } from "node:path";
|
|
5
|
-
import { createInterface } from "node:readline";
|
|
6
|
-
|
|
7
|
-
const chunks = [];
|
|
8
|
-
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
9
|
-
const input = JSON.parse(Buffer.concat(chunks).toString());
|
|
10
|
-
const child = spawn(
|
|
11
|
-
"hc",
|
|
12
|
-
["connect", "--session", input.session || "default"],
|
|
13
|
-
{ stdio: ["pipe", "pipe", "pipe"] },
|
|
14
|
-
);
|
|
15
|
-
const pending = new Map();
|
|
16
|
-
let id = 0;
|
|
17
|
-
let diagnostic = "";
|
|
18
|
-
child.stderr.on("data", (chunk) => {
|
|
19
|
-
diagnostic = (diagnostic + chunk).slice(-4096);
|
|
20
|
-
});
|
|
21
|
-
function failed(error) {
|
|
22
|
-
for (const entry of pending.values()) entry.reject(error);
|
|
23
|
-
pending.clear();
|
|
24
|
-
}
|
|
25
|
-
child.on("error", failed);
|
|
26
|
-
child.stdin.on("error", failed);
|
|
27
|
-
child.on("exit", () =>
|
|
28
|
-
failed(new Error(diagnostic || "Remote Core connection closed")),
|
|
29
|
-
);
|
|
30
|
-
createInterface({ input: child.stdout }).on("line", (line) => {
|
|
31
|
-
try {
|
|
32
|
-
const reply = JSON.parse(line);
|
|
33
|
-
const entry = pending.get(reply.id);
|
|
34
|
-
if (!entry) return;
|
|
35
|
-
pending.delete(reply.id);
|
|
36
|
-
if (reply.error) entry.reject(new Error(reply.error.message));
|
|
37
|
-
else entry.resolve(reply.result);
|
|
38
|
-
} catch (error) {
|
|
39
|
-
failed(error);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
function rpc(method, params = {}) {
|
|
43
|
-
return new Promise((resolve, reject) => {
|
|
44
|
-
pending.set(++id, { resolve, reject });
|
|
45
|
-
child.stdin.write(
|
|
46
|
-
JSON.stringify({ jsonrpc: "2.0", id, method, params }) + "\n",
|
|
47
|
-
);
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
function rejected(message) {
|
|
51
|
-
return Object.assign(new Error(message), {
|
|
52
|
-
code: "SHEPHERDR_DELIVERY_REJECTED",
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
async function nativeRequest(method, params) {
|
|
56
|
-
const terminal = params.target ?? params.pane_id ?? params.caller_pane_id;
|
|
57
|
-
if (typeof terminal !== "string" || !/^t[0-9a-z]+$/.test(terminal))
|
|
58
|
-
throw rejected("Use the exact remote terminal address");
|
|
59
|
-
const record = await rpc("terminal.get", { terminal });
|
|
60
|
-
if (record.address !== terminal)
|
|
61
|
-
throw rejected("Remote terminal changed; resolve it again");
|
|
62
|
-
if (method === "pane.close") {
|
|
63
|
-
if (record.state === "live") await rpc("terminal.kill", { terminal });
|
|
64
|
-
return rpc("terminal.rm", { terminal });
|
|
65
|
-
}
|
|
66
|
-
const path = record.labels["pi.session_file"];
|
|
67
|
-
const agent = {
|
|
68
|
-
pane_id: terminal,
|
|
69
|
-
terminal_id: terminal,
|
|
70
|
-
agent: path ? "pi" : null,
|
|
71
|
-
...(path ? { agent_session: { kind: "path", value: path } } : {}),
|
|
72
|
-
};
|
|
73
|
-
if (method === "agent.get") return { agent };
|
|
74
|
-
if (method === "pane.current") return { pane: agent };
|
|
75
|
-
throw rejected("Remote peer action is unavailable");
|
|
76
|
-
}
|
|
77
|
-
async function run() {
|
|
78
|
-
const hello = await rpc("hello");
|
|
79
|
-
if (hello.core.id !== input.coreId)
|
|
80
|
-
throw rejected(
|
|
81
|
-
"Remote Core identity changed; refresh machines before retrying",
|
|
82
|
-
);
|
|
83
|
-
const peer = await import(input.modules.peer);
|
|
84
|
-
switch (input.op) {
|
|
85
|
-
case "list": {
|
|
86
|
-
const result = await rpc("terminal.list");
|
|
87
|
-
return {
|
|
88
|
-
terminals: result.terminals.filter(
|
|
89
|
-
(terminal) => !terminal.address.includes("/"),
|
|
90
|
-
),
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
case "identity":
|
|
94
|
-
return { host: hostname(), session: hello.core.id };
|
|
95
|
-
case "pi-executable":
|
|
96
|
-
return (await import(input.modules.pi)).piExecutable(input.cwd);
|
|
97
|
-
case "message":
|
|
98
|
-
return peer.sendPeerMessage(nativeRequest, input.agent, input.message);
|
|
99
|
-
case "focus":
|
|
100
|
-
return peer.sendPeerFocus(nativeRequest, input.agent, input.focus);
|
|
101
|
-
case "commands":
|
|
102
|
-
return peer.sendPeerCommands(nativeRequest, input.agent);
|
|
103
|
-
case "reply":
|
|
104
|
-
return peer.sendPeerExtensionReply(
|
|
105
|
-
nativeRequest,
|
|
106
|
-
input.agent,
|
|
107
|
-
input.reply,
|
|
108
|
-
);
|
|
109
|
-
case "context":
|
|
110
|
-
return (await import(input.modules.context)).requestContext(
|
|
111
|
-
input.path,
|
|
112
|
-
input.request,
|
|
113
|
-
);
|
|
114
|
-
case "relay-close": {
|
|
115
|
-
await (await import(input.modules.context)).requestContext(input.path, {
|
|
116
|
-
relayToken: input.relayToken,
|
|
117
|
-
action: "close",
|
|
118
|
-
});
|
|
119
|
-
for (let attempt = 0; attempt < 100; attempt++) {
|
|
120
|
-
try {
|
|
121
|
-
await stat(input.path);
|
|
122
|
-
} catch (error) {
|
|
123
|
-
if (error.code === "ENOENT") return true;
|
|
124
|
-
throw error;
|
|
125
|
-
}
|
|
126
|
-
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
127
|
-
}
|
|
128
|
-
throw new Error(
|
|
129
|
-
"Context sharing is still stopping; wait a moment before reconnecting",
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
case "relay-start": {
|
|
133
|
-
// machine.exec ends all descendants, even detached ones. Give the
|
|
134
|
-
// leased relay to Core explicitly; rm removes its terminal on exit.
|
|
135
|
-
const directory = await mkdtemp(join(tmpdir(), "shepherdr-relay-"));
|
|
136
|
-
const script = join(directory, "relay.mjs");
|
|
137
|
-
let terminal;
|
|
138
|
-
try {
|
|
139
|
-
await writeFile(
|
|
140
|
-
script,
|
|
141
|
-
`import {rm} from "node:fs/promises"; await rm(${JSON.stringify(directory)}, {recursive:true,force:true}); const input=${JSON.stringify(input)}; await (await import(input.modules.relay)).serveRelay(input);`,
|
|
142
|
-
{ mode: 0o600, flag: "wx" },
|
|
143
|
-
);
|
|
144
|
-
terminal = await rpc("terminal.run", {
|
|
145
|
-
argv: ["node", script],
|
|
146
|
-
no_parent: true,
|
|
147
|
-
no_notify: true,
|
|
148
|
-
outlive_parent: true,
|
|
149
|
-
rm: true,
|
|
150
|
-
labels: { "shepherdr.role": "context-relay" },
|
|
151
|
-
});
|
|
152
|
-
const context = await import(input.modules.context);
|
|
153
|
-
const path = context.relayContextPath(input.threadId, input.routeId);
|
|
154
|
-
for (let attempt = 0; attempt < 100; attempt++) {
|
|
155
|
-
const record = await rpc("terminal.get", {
|
|
156
|
-
terminal: terminal.address,
|
|
157
|
-
});
|
|
158
|
-
if (record.state !== "live")
|
|
159
|
-
throw new Error(
|
|
160
|
-
"Context sharing could not start; wait 30 seconds and reconnect this machine",
|
|
161
|
-
);
|
|
162
|
-
try {
|
|
163
|
-
const descriptor = JSON.parse(await readFile(path, "utf8"));
|
|
164
|
-
if (descriptor.pid === record.pid) return path;
|
|
165
|
-
} catch (error) {
|
|
166
|
-
if (error.code !== "ENOENT") throw error;
|
|
167
|
-
}
|
|
168
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
169
|
-
}
|
|
170
|
-
throw new Error(
|
|
171
|
-
"Shared context relay did not start; inspect its Core terminal output",
|
|
172
|
-
);
|
|
173
|
-
} catch (error) {
|
|
174
|
-
if (terminal)
|
|
175
|
-
await rpc("terminal.kill", { terminal: terminal.address }).catch(
|
|
176
|
-
() => {},
|
|
177
|
-
);
|
|
178
|
-
throw error;
|
|
179
|
-
} finally {
|
|
180
|
-
await rm(directory, { recursive: true, force: true });
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
case "view":
|
|
184
|
-
return (await import(input.modules.session)).readSessionView(input.path);
|
|
185
|
-
case "directory": {
|
|
186
|
-
const expand = (value) =>
|
|
187
|
-
value === "~"
|
|
188
|
-
? homedir()
|
|
189
|
-
: value.startsWith("~/")
|
|
190
|
-
? resolve(homedir(), value.slice(2))
|
|
191
|
-
: value;
|
|
192
|
-
const path = resolve(
|
|
193
|
-
expand(input.fallback),
|
|
194
|
-
expand(input.value?.trim() || "."),
|
|
195
|
-
);
|
|
196
|
-
if (!(await stat(path)).isDirectory())
|
|
197
|
-
throw new Error("Remote working directory is not a directory");
|
|
198
|
-
return path;
|
|
199
|
-
}
|
|
200
|
-
case "watch": {
|
|
201
|
-
const watcher = await import(input.modules.watch);
|
|
202
|
-
return new Promise((resolve, reject) => {
|
|
203
|
-
let close = () => {};
|
|
204
|
-
const timer = setTimeout(() => {
|
|
205
|
-
close();
|
|
206
|
-
resolve([]);
|
|
207
|
-
}, 10_000);
|
|
208
|
-
try {
|
|
209
|
-
close = watcher.watchSessions(
|
|
210
|
-
input.paths,
|
|
211
|
-
(path) => {
|
|
212
|
-
clearTimeout(timer);
|
|
213
|
-
close();
|
|
214
|
-
resolve([path]);
|
|
215
|
-
},
|
|
216
|
-
(error) => {
|
|
217
|
-
clearTimeout(timer);
|
|
218
|
-
close();
|
|
219
|
-
reject(error);
|
|
220
|
-
},
|
|
221
|
-
);
|
|
222
|
-
} catch (error) {
|
|
223
|
-
clearTimeout(timer);
|
|
224
|
-
reject(error);
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
default:
|
|
229
|
-
throw rejected("Unknown remote operation");
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
try {
|
|
233
|
-
process.stdout.write(
|
|
234
|
-
JSON.stringify({ ok: true, result: await run() }) + "\n",
|
|
235
|
-
);
|
|
236
|
-
} catch (error) {
|
|
237
|
-
process.stdout.write(
|
|
238
|
-
JSON.stringify({ ok: false, error: error.message, code: error.code }) +
|
|
239
|
-
"\n",
|
|
240
|
-
);
|
|
241
|
-
} finally {
|
|
242
|
-
child.stdin.end();
|
|
243
|
-
child.kill();
|
|
244
|
-
}
|