@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
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { Socket } from "node:net";
|
|
3
|
+
import { admissionEndpoint, connectAdmission } from "./admission-endpoint.js";
|
|
4
|
+
|
|
5
|
+
type Lane = "launch" | "bulk";
|
|
6
|
+
interface Waiting {
|
|
7
|
+
resolve(): void;
|
|
8
|
+
reject(error: Error): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Owns one client's queue and connection, not execution or agent identity. */
|
|
12
|
+
export class CoreAdmission {
|
|
13
|
+
private readonly lifetime = new AbortController();
|
|
14
|
+
private readonly waiting = new Map<string, Waiting>();
|
|
15
|
+
private connection: Promise<Socket> | undefined;
|
|
16
|
+
private socket: Socket | undefined;
|
|
17
|
+
private ended: Error | undefined;
|
|
18
|
+
private buffer = "";
|
|
19
|
+
private readonly core: string;
|
|
20
|
+
private readonly destination: string;
|
|
21
|
+
constructor(core: string, destination = "local") {
|
|
22
|
+
this.core = core;
|
|
23
|
+
this.destination = destination;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
close(): void {
|
|
27
|
+
this.fail(new Error("Agent connection closed"));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private fail(error: Error): void {
|
|
31
|
+
if (this.ended) return;
|
|
32
|
+
this.ended = error;
|
|
33
|
+
this.lifetime.abort(error);
|
|
34
|
+
this.socket?.destroy();
|
|
35
|
+
for (const waiter of this.waiting.values()) waiter.reject(error);
|
|
36
|
+
this.waiting.clear();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private disconnected(socket: Socket): void {
|
|
40
|
+
if (this.socket !== socket) return;
|
|
41
|
+
this.socket = undefined;
|
|
42
|
+
this.connection = undefined;
|
|
43
|
+
this.buffer = "";
|
|
44
|
+
socket.destroy();
|
|
45
|
+
// Only callers not yet admitted fail. Accepted work keeps its original
|
|
46
|
+
// execution/cleanup owner and is never replayed after broker loss.
|
|
47
|
+
for (const waiter of this.waiting.values())
|
|
48
|
+
waiter.reject(
|
|
49
|
+
new Error("Agent connection lost before the operation started"),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private async open(): Promise<Socket> {
|
|
54
|
+
const path = await admissionEndpoint(this.core, this.destination);
|
|
55
|
+
const startup = AbortSignal.any([
|
|
56
|
+
this.lifetime.signal,
|
|
57
|
+
AbortSignal.timeout(5000),
|
|
58
|
+
]);
|
|
59
|
+
const socket = await connectAdmission(path, startup);
|
|
60
|
+
if (this.ended) {
|
|
61
|
+
socket.destroy();
|
|
62
|
+
throw this.ended;
|
|
63
|
+
}
|
|
64
|
+
this.socket = socket;
|
|
65
|
+
socket.setEncoding("utf8");
|
|
66
|
+
socket.on("error", () => this.disconnected(socket));
|
|
67
|
+
socket.on("close", () => this.disconnected(socket));
|
|
68
|
+
socket.on("data", (chunk: string) => {
|
|
69
|
+
try {
|
|
70
|
+
this.buffer += chunk;
|
|
71
|
+
for (;;) {
|
|
72
|
+
const end = this.buffer.indexOf("\n");
|
|
73
|
+
if (
|
|
74
|
+
Buffer.byteLength(
|
|
75
|
+
end < 0 ? this.buffer : this.buffer.slice(0, end),
|
|
76
|
+
) > 8192
|
|
77
|
+
)
|
|
78
|
+
throw new Error("Invalid agent connection response");
|
|
79
|
+
if (end < 0) break;
|
|
80
|
+
const value: unknown = JSON.parse(this.buffer.slice(0, end));
|
|
81
|
+
this.buffer = this.buffer.slice(end + 1);
|
|
82
|
+
if (
|
|
83
|
+
!value ||
|
|
84
|
+
typeof value !== "object" ||
|
|
85
|
+
!("id" in value) ||
|
|
86
|
+
typeof value.id !== "string" ||
|
|
87
|
+
!("state" in value)
|
|
88
|
+
)
|
|
89
|
+
throw new Error("Invalid agent connection response");
|
|
90
|
+
const waiter = this.waiting.get(value.id);
|
|
91
|
+
if (value.state === "granted") waiter?.resolve();
|
|
92
|
+
else if (
|
|
93
|
+
value.state === "error" &&
|
|
94
|
+
"message" in value &&
|
|
95
|
+
typeof value.message === "string"
|
|
96
|
+
)
|
|
97
|
+
waiter?.reject(
|
|
98
|
+
new Error(
|
|
99
|
+
"Agent operation could not start: concurrent operation limit reached",
|
|
100
|
+
),
|
|
101
|
+
);
|
|
102
|
+
else throw new Error("Invalid agent connection response");
|
|
103
|
+
}
|
|
104
|
+
} catch (error) {
|
|
105
|
+
this.fail(error instanceof Error ? error : new Error(String(error)));
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return socket;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Deadline is for admission only. Execution owns its existing cancellation/timeout. */
|
|
112
|
+
async run<T>(
|
|
113
|
+
lane: Lane,
|
|
114
|
+
execute: () => Promise<T>,
|
|
115
|
+
signal?: AbortSignal,
|
|
116
|
+
): Promise<T> {
|
|
117
|
+
signal?.throwIfAborted();
|
|
118
|
+
if (this.ended) throw this.ended;
|
|
119
|
+
if (this.waiting.size >= 64)
|
|
120
|
+
throw new Error(
|
|
121
|
+
"Agent operation could not start: concurrent operation limit reached",
|
|
122
|
+
);
|
|
123
|
+
const id = randomUUID();
|
|
124
|
+
const admission = AbortSignal.any([
|
|
125
|
+
this.lifetime.signal,
|
|
126
|
+
AbortSignal.timeout(90_000),
|
|
127
|
+
...(signal ? [signal] : []),
|
|
128
|
+
]);
|
|
129
|
+
let socket: Socket | undefined;
|
|
130
|
+
let resolve!: () => void;
|
|
131
|
+
let reject!: (error: Error) => void;
|
|
132
|
+
const granted = new Promise<void>((yes, no) => {
|
|
133
|
+
resolve = yes;
|
|
134
|
+
reject = no;
|
|
135
|
+
});
|
|
136
|
+
// Register before connection startup: cancellation cannot submit queued work.
|
|
137
|
+
this.waiting.set(id, { resolve, reject });
|
|
138
|
+
const abort = () =>
|
|
139
|
+
reject(
|
|
140
|
+
new Error(
|
|
141
|
+
admission.reason?.name === "TimeoutError"
|
|
142
|
+
? "Agent operation could not start before its deadline"
|
|
143
|
+
: "Agent operation cancelled before starting",
|
|
144
|
+
{ cause: admission.reason },
|
|
145
|
+
),
|
|
146
|
+
);
|
|
147
|
+
admission.addEventListener("abort", abort, { once: true });
|
|
148
|
+
if (admission.aborted) abort();
|
|
149
|
+
const connecting = (this.connection ??= this.open());
|
|
150
|
+
void connecting.then(
|
|
151
|
+
(connection) => {
|
|
152
|
+
socket = connection;
|
|
153
|
+
if (!admission.aborted && this.waiting.has(id))
|
|
154
|
+
socket.write(`${JSON.stringify({ op: "acquire", id, lane })}\n`);
|
|
155
|
+
},
|
|
156
|
+
(error: unknown) => {
|
|
157
|
+
if (this.connection !== connecting) return;
|
|
158
|
+
this.connection = undefined;
|
|
159
|
+
for (const waiter of this.waiting.values())
|
|
160
|
+
waiter.reject(
|
|
161
|
+
new Error(
|
|
162
|
+
"Agent operations could not start. Check Core and local runtime permissions",
|
|
163
|
+
{ cause: error },
|
|
164
|
+
),
|
|
165
|
+
);
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
try {
|
|
169
|
+
await granted;
|
|
170
|
+
admission.throwIfAborted();
|
|
171
|
+
admission.removeEventListener("abort", abort);
|
|
172
|
+
// Broker failure never retries accepted work. Existing operation owns cleanup.
|
|
173
|
+
return await execute();
|
|
174
|
+
} finally {
|
|
175
|
+
admission.removeEventListener("abort", abort);
|
|
176
|
+
this.waiting.delete(id);
|
|
177
|
+
if (socket && !socket.destroyed)
|
|
178
|
+
socket.write(`${JSON.stringify({ op: "release", id })}\n`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { PaneInfo, SessionSnapshot } from "../types.js";
|
|
2
|
+
import type { CoreConnection } from "./backend.js";
|
|
3
|
+
import { object } from "./protocol.js";
|
|
4
|
+
import { terminalPane, terminalSnapshot } from "./terminal.js";
|
|
5
|
+
import { terminalPeer } from "./terminal-peer.mjs";
|
|
6
|
+
|
|
7
|
+
export async function getSnapshot(
|
|
8
|
+
client: CoreConnection,
|
|
9
|
+
): Promise<SessionSnapshot> {
|
|
10
|
+
return terminalSnapshot(await client.core.localSnapshot());
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function getAgent(
|
|
14
|
+
client: CoreConnection,
|
|
15
|
+
target: string,
|
|
16
|
+
): Promise<PaneInfo> {
|
|
17
|
+
if (/^t[1-9][0-9bcdfghjkmnpqrstvwxyz]*$/.test(target)) {
|
|
18
|
+
try {
|
|
19
|
+
return terminalPane(
|
|
20
|
+
await client.core.request("terminal.get", { terminal: target }),
|
|
21
|
+
);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (
|
|
24
|
+
!(
|
|
25
|
+
error instanceof Error &&
|
|
26
|
+
"kind" in error &&
|
|
27
|
+
error.kind === "not_found"
|
|
28
|
+
)
|
|
29
|
+
)
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const snapshot = await getSnapshot(client);
|
|
34
|
+
const matches = snapshot.agents.filter((agent) => agent.name === target);
|
|
35
|
+
if (matches.length > 1)
|
|
36
|
+
throw new Error("Agent name is ambiguous; use its terminal address");
|
|
37
|
+
const agent = matches[0];
|
|
38
|
+
if (!agent)
|
|
39
|
+
throw Object.assign(
|
|
40
|
+
new Error(
|
|
41
|
+
`No Pi agent matches ${JSON.stringify(target)}; use the exact target returned by spawn/find`,
|
|
42
|
+
),
|
|
43
|
+
{ kind: "not_found" },
|
|
44
|
+
);
|
|
45
|
+
return terminalPane(
|
|
46
|
+
await client.core.request("terminal.get", { terminal: agent.terminal_id }),
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function getCurrentPane(
|
|
51
|
+
client: CoreConnection,
|
|
52
|
+
): Promise<PaneInfo> {
|
|
53
|
+
const terminal = process.env["HC_TERMINAL_ID"];
|
|
54
|
+
if (!terminal || client.remote)
|
|
55
|
+
throw new Error(
|
|
56
|
+
"Cannot identify this terminal; run Pi inside a local Core terminal",
|
|
57
|
+
);
|
|
58
|
+
return terminalPane(await client.core.request("terminal.get", { terminal }));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function resolvePiAgent(
|
|
62
|
+
client: CoreConnection,
|
|
63
|
+
target: string,
|
|
64
|
+
controllingPaneId = process.env["HC_TERMINAL_ID"],
|
|
65
|
+
): Promise<PaneInfo> {
|
|
66
|
+
const agent = await getAgent(client, target);
|
|
67
|
+
if (agent.agent !== "pi") throw new Error(`${target} is not a Pi agent`);
|
|
68
|
+
if (controllingPaneId && agent.terminal_id === controllingPaneId)
|
|
69
|
+
throw new Error("refusing to target the controlling Pi session");
|
|
70
|
+
return agent;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function sessionPath(agent: PaneInfo): string | undefined {
|
|
74
|
+
return agent.agent_session?.kind === "path"
|
|
75
|
+
? agent.agent_session.value
|
|
76
|
+
: undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function closeTerminal(
|
|
80
|
+
client: CoreConnection,
|
|
81
|
+
terminal: string,
|
|
82
|
+
): Promise<void> {
|
|
83
|
+
await terminalPeer((method, params) =>
|
|
84
|
+
client.core.request(method, params),
|
|
85
|
+
).closeTerminal(terminal);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function readTerminal(
|
|
89
|
+
client: CoreConnection,
|
|
90
|
+
terminal: string,
|
|
91
|
+
lines: number,
|
|
92
|
+
): Promise<{ text: string; truncated: boolean }> {
|
|
93
|
+
const result = object(
|
|
94
|
+
await client.core.request("terminal.read", { terminal, lines }),
|
|
95
|
+
);
|
|
96
|
+
if (
|
|
97
|
+
typeof result["text"] !== "string" ||
|
|
98
|
+
typeof result["complete"] !== "boolean"
|
|
99
|
+
)
|
|
100
|
+
throw new Error("Core returned no terminal text");
|
|
101
|
+
return { text: result["text"], truncated: !result["complete"] };
|
|
102
|
+
}
|
package/src/core/backend.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { hostname } from "node:os";
|
|
2
|
-
import
|
|
2
|
+
import {
|
|
3
|
+
findSavedNotesSessions,
|
|
4
|
+
readSavedNotesSession,
|
|
5
|
+
} from "../remote/notes-reader.mjs";
|
|
3
6
|
import { openPeerChannel } from "../remote/shepherdr-channel.mjs";
|
|
4
7
|
import { requestContext } from "../remote/shepherdr-context.mjs";
|
|
5
8
|
import {
|
|
@@ -12,28 +15,30 @@ import {
|
|
|
12
15
|
import type {
|
|
13
16
|
ExtensionReply,
|
|
14
17
|
FocusRequest,
|
|
15
|
-
HerdrEvent,
|
|
16
18
|
PaneInfo,
|
|
17
19
|
PeerChannel,
|
|
18
20
|
PeerMessage,
|
|
19
21
|
} from "../types.js";
|
|
22
|
+
import { CoreAdmission } from "./admission.js";
|
|
20
23
|
import { CoreClient } from "./client.js";
|
|
21
24
|
import { openCoreFocusChannel } from "./focus-channel.js";
|
|
25
|
+
import type { CoreMachine } from "./machines.js";
|
|
22
26
|
import { object } from "./protocol.js";
|
|
23
27
|
import { RemoteCoreEndpoint } from "./remote-endpoint.js";
|
|
24
|
-
import { type CoreMachine, remoteExec } from "./remote-exec.js";
|
|
25
28
|
import { CoreRemoteReader } from "./remote-reader.js";
|
|
26
|
-
import {
|
|
27
|
-
import { dispatchCoreRequest } from "./request.js";
|
|
29
|
+
import { terminalPeer } from "./terminal-peer.mjs";
|
|
28
30
|
|
|
29
|
-
export class CoreConnection
|
|
31
|
+
export class CoreConnection {
|
|
32
|
+
private readonly peer = terminalPeer((method, params) =>
|
|
33
|
+
this.core.request(method, params),
|
|
34
|
+
);
|
|
30
35
|
private readonly lifetime = new AbortController();
|
|
31
36
|
private readonly focusChannels = new Set<PeerChannel>();
|
|
32
|
-
private relay: CoreContextRelay | undefined;
|
|
33
37
|
private readonly socketPath: string;
|
|
34
38
|
readonly core: CoreClient | RemoteCoreEndpoint;
|
|
35
39
|
readonly remote: CoreMachine | undefined;
|
|
36
40
|
readonly reader: CoreRemoteReader | undefined;
|
|
41
|
+
readonly admission: CoreAdmission;
|
|
37
42
|
constructor(
|
|
38
43
|
socketPath = process.env["HC_TERMINAL_SOCKET"],
|
|
39
44
|
remote?: CoreMachine,
|
|
@@ -42,8 +47,9 @@ export class CoreConnection implements HerdrConnection {
|
|
|
42
47
|
throw new Error("Run Pi inside a Core terminal to connect Shepherdr");
|
|
43
48
|
this.remote = remote;
|
|
44
49
|
this.socketPath = socketPath;
|
|
50
|
+
this.admission = new CoreAdmission(socketPath, remote?.id);
|
|
45
51
|
this.core = remote
|
|
46
|
-
? new RemoteCoreEndpoint(socketPath, remote)
|
|
52
|
+
? new RemoteCoreEndpoint(socketPath, remote, this.admission)
|
|
47
53
|
: new CoreClient(socketPath);
|
|
48
54
|
this.reader =
|
|
49
55
|
this.core instanceof RemoteCoreEndpoint
|
|
@@ -52,13 +58,28 @@ export class CoreConnection implements HerdrConnection {
|
|
|
52
58
|
}
|
|
53
59
|
close(): void {
|
|
54
60
|
this.lifetime.abort();
|
|
61
|
+
this.admission.close();
|
|
55
62
|
for (const channel of this.focusChannels) channel.close();
|
|
56
63
|
this.focusChannels.clear();
|
|
57
|
-
void this.relay?.close();
|
|
58
64
|
this.core.close();
|
|
59
65
|
}
|
|
60
66
|
async machineIdentity() {
|
|
61
67
|
const hello = object(await this.core.request("hello"));
|
|
68
|
+
const version =
|
|
69
|
+
typeof hello["version"] === "string"
|
|
70
|
+
? /^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/.exec(hello["version"])
|
|
71
|
+
: null;
|
|
72
|
+
if (
|
|
73
|
+
!version ||
|
|
74
|
+
!(
|
|
75
|
+
Number(version[1]) > 0 ||
|
|
76
|
+
Number(version[2]) > 1 ||
|
|
77
|
+
(Number(version[2]) === 1 && Number(version[3]) >= 18)
|
|
78
|
+
)
|
|
79
|
+
)
|
|
80
|
+
throw new Error(
|
|
81
|
+
"Shepherdr requires Herdr Core 0.1.18 or newer; update Core on this machine and reconnect",
|
|
82
|
+
);
|
|
62
83
|
const core = object(hello["core"]);
|
|
63
84
|
if (typeof core["id"] !== "string" || !core["id"])
|
|
64
85
|
throw new Error("Core returned no stable identity");
|
|
@@ -72,40 +93,61 @@ export class CoreConnection implements HerdrConnection {
|
|
|
72
93
|
}
|
|
73
94
|
requestContext(path: string, request: unknown, signal?: AbortSignal) {
|
|
74
95
|
if (this.core instanceof RemoteCoreEndpoint)
|
|
75
|
-
return
|
|
76
|
-
this.core.host,
|
|
77
|
-
this.core.machine,
|
|
96
|
+
return this.core.call(
|
|
78
97
|
{ op: "context", path, request },
|
|
79
|
-
signal ? { signal } : {},
|
|
98
|
+
{ timeoutMs: 36_000, ...(signal ? { signal } : {}) },
|
|
80
99
|
);
|
|
81
100
|
return requestContext(path, request, signal);
|
|
82
101
|
}
|
|
102
|
+
async readSavedSession(
|
|
103
|
+
path: string,
|
|
104
|
+
history: boolean,
|
|
105
|
+
signal?: AbortSignal,
|
|
106
|
+
): Promise<unknown> {
|
|
107
|
+
if (this.core instanceof RemoteCoreEndpoint)
|
|
108
|
+
return this.core.call(
|
|
109
|
+
{ op: "saved_session", path, history },
|
|
110
|
+
{ ...(signal ? { signal } : {}) },
|
|
111
|
+
);
|
|
112
|
+
return this.admission.run(
|
|
113
|
+
"bulk",
|
|
114
|
+
() => readSavedNotesSession(path, signal, history),
|
|
115
|
+
signal,
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
async findSavedSessions(
|
|
119
|
+
threadId: string,
|
|
120
|
+
signal?: AbortSignal,
|
|
121
|
+
): Promise<unknown> {
|
|
122
|
+
signal?.throwIfAborted();
|
|
123
|
+
if (this.core instanceof RemoteCoreEndpoint)
|
|
124
|
+
return this.core.call(
|
|
125
|
+
{ op: "find_saved_sessions", threadId },
|
|
126
|
+
{ ...(signal ? { signal } : {}) },
|
|
127
|
+
);
|
|
128
|
+
return this.admission.run(
|
|
129
|
+
"bulk",
|
|
130
|
+
() => findSavedNotesSessions(threadId, signal),
|
|
131
|
+
signal,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
83
134
|
async startContextRelay(
|
|
84
135
|
threadId: string,
|
|
85
136
|
_routeId: string,
|
|
86
137
|
handler: (request: unknown, signal: AbortSignal) => Promise<unknown>,
|
|
87
138
|
onFailure: (error: Error) => void = () => {},
|
|
88
139
|
): Promise<void> {
|
|
89
|
-
if (!this.
|
|
140
|
+
if (!(this.core instanceof RemoteCoreEndpoint))
|
|
90
141
|
throw new Error("Local context routes use the controller session");
|
|
91
142
|
this.lifetime.signal.throwIfAborted();
|
|
92
|
-
await this.
|
|
93
|
-
this.lifetime.signal.throwIfAborted();
|
|
94
|
-
const relay = new CoreContextRelay(this.socketPath, this.remote);
|
|
95
|
-
this.relay = relay;
|
|
96
|
-
try {
|
|
97
|
-
await relay.start(threadId, handler, onFailure);
|
|
98
|
-
} catch (error) {
|
|
99
|
-
await relay.close();
|
|
100
|
-
throw error;
|
|
101
|
-
}
|
|
143
|
+
await this.core.bridge.startContextRelay(threadId, handler, onFailure);
|
|
102
144
|
}
|
|
103
145
|
contextRelayPath(): string {
|
|
104
|
-
if (!this.
|
|
146
|
+
if (!(this.core instanceof RemoteCoreEndpoint))
|
|
105
147
|
throw new Error(
|
|
106
148
|
"Shared context relay is unavailable; open /herdr → Status to retry sharing",
|
|
107
149
|
);
|
|
108
|
-
return this.
|
|
150
|
+
return this.core.bridge.contextRelayPath();
|
|
109
151
|
}
|
|
110
152
|
async directory(
|
|
111
153
|
value: string | undefined,
|
|
@@ -113,7 +155,7 @@ export class CoreConnection implements HerdrConnection {
|
|
|
113
155
|
): Promise<string> {
|
|
114
156
|
if (!(this.core instanceof RemoteCoreEndpoint))
|
|
115
157
|
throw new Error("Use local directory resolution on the local Core");
|
|
116
|
-
const result = await
|
|
158
|
+
const result = await this.core.call({
|
|
117
159
|
op: "directory",
|
|
118
160
|
value,
|
|
119
161
|
fallback,
|
|
@@ -125,11 +167,14 @@ export class CoreConnection implements HerdrConnection {
|
|
|
125
167
|
async sendMessage(agent: PaneInfo, message: PeerMessage) {
|
|
126
168
|
if (this.core instanceof RemoteCoreEndpoint) {
|
|
127
169
|
const result = object(
|
|
128
|
-
await
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
170
|
+
await this.core.call(
|
|
171
|
+
{
|
|
172
|
+
op: "message",
|
|
173
|
+
agent,
|
|
174
|
+
message,
|
|
175
|
+
},
|
|
176
|
+
{ timeoutMs: message.text.trim() === "/quit" ? 0 : 36_000 },
|
|
177
|
+
),
|
|
133
178
|
);
|
|
134
179
|
if (
|
|
135
180
|
typeof result["command"] !== "boolean" ||
|
|
@@ -148,56 +193,47 @@ export class CoreConnection implements HerdrConnection {
|
|
|
148
193
|
: {}),
|
|
149
194
|
};
|
|
150
195
|
}
|
|
151
|
-
return sendPeerMessage(
|
|
152
|
-
(method, params) => this.request(method, params),
|
|
153
|
-
agent,
|
|
154
|
-
message,
|
|
155
|
-
);
|
|
196
|
+
return sendPeerMessage(this.peer, agent, message);
|
|
156
197
|
}
|
|
157
198
|
async commands(agent: PaneInfo) {
|
|
158
199
|
if (this.core instanceof RemoteCoreEndpoint)
|
|
159
200
|
return readPeerCommands(
|
|
160
|
-
await
|
|
201
|
+
await this.core.call({
|
|
161
202
|
op: "commands",
|
|
162
203
|
agent,
|
|
163
204
|
}),
|
|
164
205
|
);
|
|
165
|
-
return sendPeerCommands(
|
|
166
|
-
(method, params) => this.request(method, params),
|
|
167
|
-
agent,
|
|
168
|
-
);
|
|
206
|
+
return sendPeerCommands(this.peer, agent);
|
|
169
207
|
}
|
|
170
208
|
async replyExtension(agent: PaneInfo, reply: ExtensionReply) {
|
|
171
209
|
if (this.core instanceof RemoteCoreEndpoint) {
|
|
172
|
-
const result = await
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
210
|
+
const result = await this.core.call(
|
|
211
|
+
{
|
|
212
|
+
op: "reply",
|
|
213
|
+
agent,
|
|
214
|
+
reply,
|
|
215
|
+
},
|
|
216
|
+
{ timeoutMs: 60_000 },
|
|
217
|
+
);
|
|
177
218
|
if (typeof result !== "string")
|
|
178
219
|
throw new Error(
|
|
179
220
|
"Remote extension reply was not acknowledged; inspect the target before retrying",
|
|
180
221
|
);
|
|
181
222
|
return result;
|
|
182
223
|
}
|
|
183
|
-
return sendPeerExtensionReply(
|
|
184
|
-
(method, params) => this.request(method, params),
|
|
185
|
-
agent,
|
|
186
|
-
reply,
|
|
187
|
-
);
|
|
224
|
+
return sendPeerExtensionReply(this.peer, agent, reply);
|
|
188
225
|
}
|
|
189
226
|
focus(agent: PaneInfo, request: FocusRequest): Promise<unknown> {
|
|
190
227
|
if (this.core instanceof RemoteCoreEndpoint)
|
|
191
|
-
return
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
);
|
|
228
|
+
return this.core.call(
|
|
229
|
+
{
|
|
230
|
+
op: "focus",
|
|
231
|
+
agent,
|
|
232
|
+
focus: request,
|
|
233
|
+
},
|
|
234
|
+
{ timeoutMs: 60_000 },
|
|
235
|
+
);
|
|
236
|
+
return sendPeerFocus(this.peer, agent, request);
|
|
201
237
|
}
|
|
202
238
|
async openFocusChannel(
|
|
203
239
|
agent: PaneInfo,
|
|
@@ -214,11 +250,7 @@ export class CoreConnection implements HerdrConnection {
|
|
|
214
250
|
request,
|
|
215
251
|
{ signal: this.lifetime.signal },
|
|
216
252
|
)
|
|
217
|
-
: await openPeerChannel(
|
|
218
|
-
(method, params) => this.request(method, params),
|
|
219
|
-
agent,
|
|
220
|
-
request,
|
|
221
|
-
);
|
|
253
|
+
: await openPeerChannel(this.peer, agent, request);
|
|
222
254
|
if (this.lifetime.signal.aborted) {
|
|
223
255
|
channel.close();
|
|
224
256
|
this.lifetime.signal.throwIfAborted();
|
|
@@ -227,31 +259,25 @@ export class CoreConnection implements HerdrConnection {
|
|
|
227
259
|
channel.onClose(() => this.focusChannels.delete(channel));
|
|
228
260
|
return channel;
|
|
229
261
|
}
|
|
230
|
-
async request<T>(
|
|
231
|
-
method: string,
|
|
232
|
-
params: object = {},
|
|
233
|
-
timeoutMs = 30_000,
|
|
234
|
-
): Promise<T> {
|
|
235
|
-
return (await dispatchCoreRequest(
|
|
236
|
-
this.core,
|
|
237
|
-
method,
|
|
238
|
-
object(params),
|
|
239
|
-
timeoutMs,
|
|
240
|
-
)) as T;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
262
|
async subscribe(
|
|
244
|
-
|
|
245
|
-
|
|
263
|
+
targets: string[],
|
|
264
|
+
onChange: () => void,
|
|
246
265
|
onDisconnect: (error?: Error) => void,
|
|
247
266
|
signal?: AbortSignal,
|
|
248
267
|
): Promise<() => void> {
|
|
249
268
|
let closed = false;
|
|
269
|
+
const watched = new Set(targets);
|
|
250
270
|
const subscription = await this.core.subscribe(
|
|
251
271
|
{},
|
|
252
272
|
(notification) => {
|
|
253
|
-
if (
|
|
254
|
-
|
|
273
|
+
if (
|
|
274
|
+
notification.method === "snapshot" ||
|
|
275
|
+
notification.method === "missed" ||
|
|
276
|
+
(notification.method === "event" &&
|
|
277
|
+
notification.event.terminal &&
|
|
278
|
+
watched.has(notification.event.terminal))
|
|
279
|
+
)
|
|
280
|
+
onChange();
|
|
255
281
|
},
|
|
256
282
|
{ ...(signal ? { signal } : {}) },
|
|
257
283
|
);
|
package/src/core/client.ts
CHANGED
|
@@ -228,6 +228,22 @@ export class CoreClient {
|
|
|
228
228
|
for (const connection of this.connections) connection.close();
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
+
/** Local snapshot avoids aggregate terminal.list's remote timeout/omission policy. */
|
|
232
|
+
async localSnapshot(): Promise<{ terminals: CoreObject[] }> {
|
|
233
|
+
let terminals: CoreObject[] | undefined;
|
|
234
|
+
const subscription = await this.subscribe({}, (event) => {
|
|
235
|
+
if (event.method === "snapshot")
|
|
236
|
+
terminals = event.terminals.filter(
|
|
237
|
+
(terminal) =>
|
|
238
|
+
typeof terminal["address"] === "string" &&
|
|
239
|
+
!terminal["address"].includes("/"),
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
subscription.close();
|
|
243
|
+
if (!terminals) throw new Error("Core returned no local terminal snapshot");
|
|
244
|
+
return { terminals };
|
|
245
|
+
}
|
|
246
|
+
|
|
231
247
|
private open(
|
|
232
248
|
method: string,
|
|
233
249
|
params: CoreObject,
|
|
@@ -78,36 +78,12 @@ function object(value) {
|
|
|
78
78
|
throw new Error("Invalid remote Core response");
|
|
79
79
|
return /** @type {Record<string,unknown>} */ (value);
|
|
80
80
|
}
|
|
81
|
-
/** @param {string
|
|
82
|
-
async function nativeRequest(method, params) {
|
|
83
|
-
const target = object(params)["target"];
|
|
84
|
-
if (
|
|
85
|
-
method !== "agent.get" ||
|
|
86
|
-
typeof target !== "string" ||
|
|
87
|
-
!/^t[0-9a-z]+$/.test(target)
|
|
88
|
-
)
|
|
89
|
-
throw new Error("Use an exact remote terminal identity");
|
|
90
|
-
const record = object(await rpc("terminal.get", { terminal: target }));
|
|
91
|
-
if (record["address"] !== target || record["state"] !== "live")
|
|
92
|
-
throw new Error("Remote target changed or exited");
|
|
93
|
-
const path = object(record["labels"])["pi.session_file"];
|
|
94
|
-
if (typeof path !== "string" || !path)
|
|
95
|
-
throw new Error("Remote target has no saved Pi session");
|
|
96
|
-
return {
|
|
97
|
-
agent: {
|
|
98
|
-
pane_id: target,
|
|
99
|
-
terminal_id: target,
|
|
100
|
-
agent: path ? "pi" : null,
|
|
101
|
-
agent_session: { kind: "path", value: path },
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
/** @param {{coreId:string, session:string, modules:{channel:string}, expected:import('../types.js').PaneInfo, focus:import('../types.js').FocusRequest}} input */
|
|
81
|
+
/** @param {{coreId:string, session:string, modules:{channel:string,terminal:string}, expected:import('../types.js').PaneInfo, focus:import('../types.js').FocusRequest}} input */
|
|
106
82
|
async function admit(input) {
|
|
107
83
|
const hello = object(await rpc("hello"));
|
|
108
84
|
if (
|
|
109
85
|
object(hello["core"])["id"] !== input.coreId ||
|
|
110
|
-
process.env["HC_SESSION"] !== input.session
|
|
86
|
+
(process.env["HC_SESSION"] ?? "default") !== input.session
|
|
111
87
|
)
|
|
112
88
|
throw new Error(
|
|
113
89
|
"Remote Core identity/session changed; refresh machines before retrying",
|
|
@@ -116,8 +92,11 @@ async function admit(input) {
|
|
|
116
92
|
/** @type {typeof import('../remote/shepherdr-channel.mjs')} */ (
|
|
117
93
|
await import(input.modules.channel)
|
|
118
94
|
);
|
|
95
|
+
const { terminalPeer } = /** @type {typeof import('./terminal-peer.mjs')} */ (
|
|
96
|
+
await import(input.modules.terminal)
|
|
97
|
+
);
|
|
119
98
|
const channel = await openPeerChannel(
|
|
120
|
-
|
|
99
|
+
terminalPeer(rpc),
|
|
121
100
|
input.expected,
|
|
122
101
|
input.focus,
|
|
123
102
|
);
|