@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
|
@@ -1,32 +1,60 @@
|
|
|
1
|
+
import { CoreAdmission } from "./admission.js";
|
|
1
2
|
import {
|
|
2
3
|
CoreClient,
|
|
3
4
|
type CoreEventFilter,
|
|
4
5
|
type CoreRequestOptions,
|
|
5
6
|
type CoreSubscribeOptions,
|
|
6
7
|
} from "./client.js";
|
|
8
|
+
import type { CoreMachine } from "./machines.js";
|
|
7
9
|
import { type CoreNotification, type CoreObject, object } from "./protocol.js";
|
|
8
|
-
import {
|
|
10
|
+
import { CoreRemoteTransport } from "./remote-transport.js";
|
|
9
11
|
|
|
10
|
-
/**
|
|
12
|
+
/** A verified remote Core, addressed by ID; native terminal addresses stay local. */
|
|
11
13
|
export class RemoteCoreEndpoint {
|
|
12
14
|
readonly host: CoreClient;
|
|
15
|
+
private transport: CoreRemoteTransport | undefined;
|
|
16
|
+
private ended = false;
|
|
17
|
+
private readonly socketPath: string;
|
|
13
18
|
readonly machine: CoreMachine;
|
|
14
|
-
|
|
15
|
-
|
|
19
|
+
private readonly admission: CoreAdmission;
|
|
20
|
+
constructor(
|
|
21
|
+
socketPath: string,
|
|
22
|
+
machine: CoreMachine,
|
|
23
|
+
admission = new CoreAdmission(socketPath, machine.id),
|
|
24
|
+
) {
|
|
25
|
+
this.socketPath = socketPath;
|
|
16
26
|
this.machine = machine;
|
|
27
|
+
this.admission = admission;
|
|
28
|
+
this.host = new CoreClient(socketPath);
|
|
29
|
+
}
|
|
30
|
+
get bridge(): CoreRemoteTransport {
|
|
31
|
+
if (this.ended) throw new Error("Remote Core connection closed");
|
|
32
|
+
return (this.transport ??= new CoreRemoteTransport(
|
|
33
|
+
this.socketPath,
|
|
34
|
+
this.machine,
|
|
35
|
+
));
|
|
17
36
|
}
|
|
18
37
|
close(): void {
|
|
38
|
+
this.ended = true;
|
|
39
|
+
this.admission.close();
|
|
40
|
+
void this.transport?.close();
|
|
19
41
|
this.host.close();
|
|
20
42
|
}
|
|
21
|
-
|
|
22
|
-
const terminal = object(value);
|
|
23
|
-
const prefix = `${this.machine.name}/`;
|
|
43
|
+
call(input: CoreObject, options: CoreRequestOptions = {}): Promise<unknown> {
|
|
24
44
|
if (
|
|
25
|
-
|
|
26
|
-
|
|
45
|
+
["saved_session", "find_saved_sessions", "view"].includes(
|
|
46
|
+
String(input["op"]),
|
|
47
|
+
)
|
|
27
48
|
)
|
|
28
|
-
|
|
29
|
-
|
|
49
|
+
return this.admission.run(
|
|
50
|
+
"bulk",
|
|
51
|
+
() => this.bridge.call(input, options),
|
|
52
|
+
options.signal,
|
|
53
|
+
);
|
|
54
|
+
return this.bridge.call(input, options);
|
|
55
|
+
}
|
|
56
|
+
localSnapshot(): Promise<unknown> {
|
|
57
|
+
return this.call({ op: "core", method: "terminal.list", params: {} });
|
|
30
58
|
}
|
|
31
59
|
async request(
|
|
32
60
|
method: string,
|
|
@@ -34,84 +62,62 @@ export class RemoteCoreEndpoint {
|
|
|
34
62
|
options: CoreRequestOptions = {},
|
|
35
63
|
): Promise<unknown> {
|
|
36
64
|
if (method === "hello") {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
);
|
|
40
|
-
return { core: { id: identity["session"], name: identity["host"] } };
|
|
41
|
-
}
|
|
42
|
-
if (method === "terminal.list") {
|
|
43
|
-
const result = object(
|
|
65
|
+
// Catalogue connection checks do not launch idle filesystem helpers.
|
|
66
|
+
const catalog = object(
|
|
44
67
|
await this.host.request("machine.list", {}, options),
|
|
45
|
-
);
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
? machines.map(object).find((entry) => entry["id"] === this.machine.id)
|
|
68
|
+
)["machines"];
|
|
69
|
+
const selected = Array.isArray(catalog)
|
|
70
|
+
? catalog.map(object).find((entry) => entry["id"] === this.machine.id)
|
|
49
71
|
: undefined;
|
|
50
|
-
if (
|
|
51
|
-
!machine ||
|
|
52
|
-
machine["state"] !== "connected" ||
|
|
53
|
-
machine["name"] !== this.machine.name
|
|
54
|
-
)
|
|
72
|
+
if (selected?.["state"] !== "connected")
|
|
55
73
|
throw new Error(
|
|
56
|
-
"Remote Core is disconnected
|
|
74
|
+
"Remote Core is disconnected; open /herdr → Status to reconnect",
|
|
57
75
|
);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
76
|
+
return {
|
|
77
|
+
version: selected["version"],
|
|
78
|
+
core: { id: this.machine.id, name: this.machine.host },
|
|
79
|
+
};
|
|
61
80
|
}
|
|
62
|
-
if (method === "terminal.run")
|
|
81
|
+
if (method === "terminal.run") {
|
|
82
|
+
// The originating Core qualifies caller ancestry across the route.
|
|
63
83
|
return this.local(
|
|
64
84
|
await this.host.request(
|
|
65
85
|
method,
|
|
66
|
-
{
|
|
67
|
-
...params,
|
|
68
|
-
on: this.machine.id,
|
|
69
|
-
env: {},
|
|
70
|
-
},
|
|
86
|
+
{ ...params, on: this.machine.id, env: {} },
|
|
71
87
|
options,
|
|
72
88
|
),
|
|
73
89
|
);
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
!/^t[0-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
90
|
+
}
|
|
91
|
+
if (typeof params["terminal"] === "string") {
|
|
92
|
+
if (!/^t[1-9][0-9bcdfghjkmnpqrstvwxyz]*$/.test(params["terminal"]))
|
|
93
|
+
throw new Error("Use an exact terminal address from this machine");
|
|
94
|
+
const result = await this.host.request(
|
|
95
|
+
method,
|
|
96
|
+
{ ...params, terminal: `${this.machine.id}/${params["terminal"]}` },
|
|
97
|
+
options,
|
|
98
|
+
);
|
|
99
|
+
return ["terminal.get", "terminal.kill", "terminal.label"].includes(
|
|
100
|
+
method,
|
|
101
|
+
)
|
|
102
|
+
? this.local(result)
|
|
103
|
+
: result;
|
|
104
|
+
}
|
|
105
|
+
return this.call({ op: "core", method, params }, options);
|
|
106
|
+
}
|
|
107
|
+
private local(value: unknown): CoreObject {
|
|
108
|
+
const record = object(value);
|
|
109
|
+
const address = record["address"];
|
|
110
|
+
const terminal =
|
|
111
|
+
typeof address === "string" ? address.split("/").at(-1) : undefined;
|
|
112
|
+
if (!terminal || !/^t[1-9][0-9bcdfghjkmnpqrstvwxyz]*$/.test(terminal))
|
|
113
|
+
throw new Error("Remote Core returned an invalid terminal address");
|
|
114
|
+
return { ...record, address: terminal };
|
|
87
115
|
}
|
|
88
116
|
subscribe(
|
|
89
|
-
|
|
117
|
+
filter: CoreEventFilter,
|
|
90
118
|
onNotification: (message: CoreNotification) => void,
|
|
91
119
|
options: CoreSubscribeOptions = {},
|
|
92
120
|
) {
|
|
93
|
-
|
|
94
|
-
// Treat events only as invalidations; get/list remain authoritative by ID.
|
|
95
|
-
return this.host.subscribe(
|
|
96
|
-
{},
|
|
97
|
-
(message) => {
|
|
98
|
-
if (message.method === "snapshot" || message.method === "missed")
|
|
99
|
-
onNotification({
|
|
100
|
-
...message,
|
|
101
|
-
terminals: [],
|
|
102
|
-
});
|
|
103
|
-
else if (message.method === "heartbeat") onNotification(message);
|
|
104
|
-
else if (message.method === "event")
|
|
105
|
-
onNotification({
|
|
106
|
-
...message,
|
|
107
|
-
event: {
|
|
108
|
-
seq: message.event.seq,
|
|
109
|
-
type: "reconcile",
|
|
110
|
-
data: {},
|
|
111
|
-
},
|
|
112
|
-
});
|
|
113
|
-
},
|
|
114
|
-
options,
|
|
115
|
-
);
|
|
121
|
+
return this.bridge.subscribe({ ...filter }, onNotification, options);
|
|
116
122
|
}
|
|
117
123
|
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import {
|
|
4
|
+
NOTES_READER_UNAVAILABLE,
|
|
5
|
+
notesModule,
|
|
6
|
+
} from "../remote/notes-reader.mjs";
|
|
7
|
+
import { CoreClient } from "./client.js";
|
|
8
|
+
import { attachCoreTerminal } from "./focus-channel-stream.js";
|
|
9
|
+
import { focusWire } from "./focus-channel-wire.mjs";
|
|
10
|
+
import type { CoreMachine } from "./machines.js";
|
|
11
|
+
import { type CoreObject, object } from "./protocol.js";
|
|
12
|
+
|
|
13
|
+
const moduleUrl = (source: string) =>
|
|
14
|
+
`data:text/javascript;base64,${Buffer.from(source).toString("base64")}`;
|
|
15
|
+
let bundle: ReturnType<typeof sources> | undefined;
|
|
16
|
+
async function savedSessionSource(): Promise<string | undefined> {
|
|
17
|
+
const url = await notesModule("saved-session");
|
|
18
|
+
if (!url) return undefined;
|
|
19
|
+
const [source, manifest] = await Promise.all([
|
|
20
|
+
readFile(url, "utf8"),
|
|
21
|
+
readFile(new URL("./tree-archive-manifest.mjs", url), "utf8"),
|
|
22
|
+
]);
|
|
23
|
+
return moduleUrl(
|
|
24
|
+
source.replace(
|
|
25
|
+
'"./tree-archive-manifest.mjs"',
|
|
26
|
+
JSON.stringify(moduleUrl(manifest)),
|
|
27
|
+
),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
async function savedSessionIndexSource(): Promise<string | undefined> {
|
|
31
|
+
const url = await notesModule("saved-session-index");
|
|
32
|
+
return url ? moduleUrl(await readFile(url, "utf8")) : undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A broken optional package must fail its reads, never the base Core connection. */
|
|
36
|
+
async function optionalNotesSource(
|
|
37
|
+
load: () => Promise<string | undefined>,
|
|
38
|
+
operation: "readSavedNotesSession" | "findSavedNotesSessions",
|
|
39
|
+
): Promise<string | undefined> {
|
|
40
|
+
try {
|
|
41
|
+
return await load();
|
|
42
|
+
} catch (error) {
|
|
43
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
44
|
+
const message = reason.startsWith("Saved Notes")
|
|
45
|
+
? reason
|
|
46
|
+
: "Saved Notes could not be loaded on the controller. Update their extension and reload Pi. Other agent operations remain available.";
|
|
47
|
+
return moduleUrl(
|
|
48
|
+
`export function ${operation}() { throw new Error(${JSON.stringify(message)}, { cause: new Error(${JSON.stringify(reason)}) }); }`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function sources() {
|
|
53
|
+
const read = (path: string) =>
|
|
54
|
+
readFile(new URL(path, import.meta.url), "utf8");
|
|
55
|
+
const [
|
|
56
|
+
bootstrap,
|
|
57
|
+
wire,
|
|
58
|
+
helper,
|
|
59
|
+
core,
|
|
60
|
+
contextHost,
|
|
61
|
+
peer,
|
|
62
|
+
session,
|
|
63
|
+
watch,
|
|
64
|
+
context,
|
|
65
|
+
checkpoints,
|
|
66
|
+
pi,
|
|
67
|
+
terminal,
|
|
68
|
+
] = await Promise.all([
|
|
69
|
+
read("./helper-bootstrap.mjs"),
|
|
70
|
+
read("./focus-channel-wire.mjs"),
|
|
71
|
+
read("./remote-service.mjs"),
|
|
72
|
+
read("./helper-core.mjs"),
|
|
73
|
+
read("./helper-context.mjs"),
|
|
74
|
+
read("../remote/shepherdr-peer.mjs"),
|
|
75
|
+
read("../remote/shepherdr-session.mjs"),
|
|
76
|
+
read("../remote/shepherdr-session-watch.mjs"),
|
|
77
|
+
read("../remote/shepherdr-context.mjs"),
|
|
78
|
+
read("../remote/shepherdr-checkpoints.mjs"),
|
|
79
|
+
read("./pi-executable.mjs"),
|
|
80
|
+
read("./terminal-peer.mjs"),
|
|
81
|
+
]);
|
|
82
|
+
const peerUrl = moduleUrl(peer);
|
|
83
|
+
const savedSession = await optionalNotesSource(
|
|
84
|
+
savedSessionSource,
|
|
85
|
+
"readSavedNotesSession",
|
|
86
|
+
);
|
|
87
|
+
const savedSessionIndex = await optionalNotesSource(
|
|
88
|
+
savedSessionIndexSource,
|
|
89
|
+
"findSavedNotesSessions",
|
|
90
|
+
);
|
|
91
|
+
const checkpointsUrl = moduleUrl(
|
|
92
|
+
checkpoints.replace(
|
|
93
|
+
'"./notes-reader.mjs"',
|
|
94
|
+
JSON.stringify(
|
|
95
|
+
savedSession ??
|
|
96
|
+
moduleUrl(
|
|
97
|
+
`export function readSavedNotesSession() { throw new Error(${JSON.stringify(NOTES_READER_UNAVAILABLE)}); }`,
|
|
98
|
+
),
|
|
99
|
+
),
|
|
100
|
+
),
|
|
101
|
+
);
|
|
102
|
+
return {
|
|
103
|
+
bootstrap,
|
|
104
|
+
wire: moduleUrl(wire),
|
|
105
|
+
modules: {
|
|
106
|
+
...(savedSession ? { savedSession } : {}),
|
|
107
|
+
...(savedSessionIndex ? { savedSessionIndex } : {}),
|
|
108
|
+
helper: moduleUrl(helper),
|
|
109
|
+
core: moduleUrl(core),
|
|
110
|
+
contextHost: moduleUrl(contextHost),
|
|
111
|
+
peer: peerUrl,
|
|
112
|
+
session: moduleUrl(session),
|
|
113
|
+
watch: moduleUrl(watch),
|
|
114
|
+
pi: moduleUrl(pi),
|
|
115
|
+
terminal: moduleUrl(terminal),
|
|
116
|
+
context: moduleUrl(
|
|
117
|
+
context
|
|
118
|
+
.replace('"./shepherdr-peer.mjs"', JSON.stringify(peerUrl))
|
|
119
|
+
.replace(
|
|
120
|
+
'"./shepherdr-checkpoints.mjs"',
|
|
121
|
+
JSON.stringify(checkpointsUrl),
|
|
122
|
+
),
|
|
123
|
+
),
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface RemoteHelper {
|
|
129
|
+
send(message: CoreObject): void;
|
|
130
|
+
close(): Promise<void>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** One private Core-owned process; credited attachment carries all control traffic. */
|
|
134
|
+
export async function openRemoteHelper(
|
|
135
|
+
socketPath: string,
|
|
136
|
+
machine: CoreMachine,
|
|
137
|
+
onMessage: (message: CoreObject) => void,
|
|
138
|
+
onClose: (error: Error) => void,
|
|
139
|
+
signal: AbortSignal,
|
|
140
|
+
): Promise<RemoteHelper> {
|
|
141
|
+
signal.throwIfAborted();
|
|
142
|
+
const source = await (bundle ??= sources());
|
|
143
|
+
signal.throwIfAborted();
|
|
144
|
+
const client = new CoreClient(socketPath);
|
|
145
|
+
const nonce = randomBytes(8).toString("hex");
|
|
146
|
+
const argv = [
|
|
147
|
+
"node",
|
|
148
|
+
"--input-type=module",
|
|
149
|
+
"-e",
|
|
150
|
+
source.bootstrap,
|
|
151
|
+
Buffer.from(JSON.stringify({ nonce, wire: source.wire })).toString(
|
|
152
|
+
"base64",
|
|
153
|
+
),
|
|
154
|
+
];
|
|
155
|
+
if (argv.reduce((size, arg) => size + arg.length * 2 + 3, 0) > 30_000)
|
|
156
|
+
throw new Error(
|
|
157
|
+
"Remote launcher exceeds the supported command size; update Shepherdr",
|
|
158
|
+
);
|
|
159
|
+
let terminal: string | undefined;
|
|
160
|
+
let launch: Promise<void> | undefined;
|
|
161
|
+
let stream: ReturnType<typeof attachCoreTerminal> | undefined;
|
|
162
|
+
let wire: ReturnType<typeof focusWire> | undefined;
|
|
163
|
+
let ended = false;
|
|
164
|
+
let started = false;
|
|
165
|
+
let admitted = false;
|
|
166
|
+
let resolveReady: () => void = () => {};
|
|
167
|
+
let rejectReady: (error: Error) => void = () => {};
|
|
168
|
+
const ready = new Promise<void>((resolve, reject) => {
|
|
169
|
+
resolveReady = resolve;
|
|
170
|
+
rejectReady = reject;
|
|
171
|
+
});
|
|
172
|
+
void ready.catch(() => {});
|
|
173
|
+
let resolveClosed: () => void = () => {};
|
|
174
|
+
const closed = new Promise<void>((resolve) => {
|
|
175
|
+
resolveClosed = resolve;
|
|
176
|
+
});
|
|
177
|
+
let closing: Promise<void> | undefined;
|
|
178
|
+
let cleaning: Promise<void> | undefined;
|
|
179
|
+
const cleanup = () =>
|
|
180
|
+
(cleaning ??= (async () => {
|
|
181
|
+
// Keep the response socket until accepted startup reveals its address.
|
|
182
|
+
await launch?.catch(() => {});
|
|
183
|
+
wire?.close();
|
|
184
|
+
stream?.close();
|
|
185
|
+
if (terminal)
|
|
186
|
+
await client
|
|
187
|
+
.request("terminal.kill", { terminal }, { timeoutMs: 5000 })
|
|
188
|
+
.catch(() => {});
|
|
189
|
+
client.close();
|
|
190
|
+
})());
|
|
191
|
+
const finish = (error?: Error): Promise<void> => {
|
|
192
|
+
if (closing) return closing;
|
|
193
|
+
ended = true;
|
|
194
|
+
clearTimeout(timer);
|
|
195
|
+
signal.removeEventListener("abort", abort);
|
|
196
|
+
rejectReady(error ?? new Error("Remote connection closed"));
|
|
197
|
+
closing = (async () => {
|
|
198
|
+
let timeout: NodeJS.Timeout | undefined;
|
|
199
|
+
try {
|
|
200
|
+
if (wire && admitted) {
|
|
201
|
+
try {
|
|
202
|
+
wire.send({ type: "close" });
|
|
203
|
+
} catch {}
|
|
204
|
+
// The helper closes listeners before acknowledging. Its 10s wire
|
|
205
|
+
// lease also bounds cleanup after loss of the Core route.
|
|
206
|
+
await Promise.race([
|
|
207
|
+
closed,
|
|
208
|
+
new Promise<void>((resolve) => {
|
|
209
|
+
timeout = setTimeout(resolve, 12_000);
|
|
210
|
+
}),
|
|
211
|
+
]);
|
|
212
|
+
}
|
|
213
|
+
} finally {
|
|
214
|
+
clearTimeout(timeout);
|
|
215
|
+
await cleanup();
|
|
216
|
+
}
|
|
217
|
+
})();
|
|
218
|
+
if (error) onClose(error);
|
|
219
|
+
return closing;
|
|
220
|
+
};
|
|
221
|
+
const failed = (error: Error) => {
|
|
222
|
+
void finish(error);
|
|
223
|
+
};
|
|
224
|
+
const abort = () =>
|
|
225
|
+
failed(new Error("Remote connection cancelled", { cause: signal.reason }));
|
|
226
|
+
const timer = setTimeout(
|
|
227
|
+
() =>
|
|
228
|
+
failed(
|
|
229
|
+
new Error(
|
|
230
|
+
"Remote connection did not become ready; check Node.js on the target machine",
|
|
231
|
+
),
|
|
232
|
+
),
|
|
233
|
+
30_000,
|
|
234
|
+
);
|
|
235
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
236
|
+
if (signal.aborted) abort();
|
|
237
|
+
try {
|
|
238
|
+
if (ended) throw new Error("Remote connection cancelled during startup");
|
|
239
|
+
launch = client
|
|
240
|
+
.request(
|
|
241
|
+
"terminal.run",
|
|
242
|
+
{
|
|
243
|
+
on: machine.id,
|
|
244
|
+
argv,
|
|
245
|
+
name: `shepherdr-${nonce}`,
|
|
246
|
+
no_parent: true,
|
|
247
|
+
no_notify: true,
|
|
248
|
+
rm: true,
|
|
249
|
+
labels: { "shepherdr.role": "connection" },
|
|
250
|
+
},
|
|
251
|
+
{ timeoutMs: 20_000 },
|
|
252
|
+
)
|
|
253
|
+
.then((value) => {
|
|
254
|
+
const address = object(value)["address"];
|
|
255
|
+
const local =
|
|
256
|
+
typeof address === "string" ? address.split("/").at(-1) : undefined;
|
|
257
|
+
if (!local || !/^t[1-9][0-9bcdfghjkmnpqrstvwxyz]*$/.test(local))
|
|
258
|
+
throw new Error("Core returned an invalid connection terminal");
|
|
259
|
+
terminal = `${machine.id}/${local}`;
|
|
260
|
+
});
|
|
261
|
+
await launch;
|
|
262
|
+
if (!terminal) throw new Error("Core returned no connection terminal");
|
|
263
|
+
if (ended) {
|
|
264
|
+
await cleanup();
|
|
265
|
+
throw new Error("Remote connection cancelled during startup");
|
|
266
|
+
}
|
|
267
|
+
wire = focusWire(
|
|
268
|
+
nonce,
|
|
269
|
+
(bytes) => {
|
|
270
|
+
if (!stream) throw new Error("Remote connection is not attached");
|
|
271
|
+
stream.write(bytes);
|
|
272
|
+
},
|
|
273
|
+
(value) => {
|
|
274
|
+
const message = object(value);
|
|
275
|
+
if (message["type"] === "closed") {
|
|
276
|
+
resolveClosed();
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (ended) return;
|
|
280
|
+
if (message["type"] === "ready" && !admitted) {
|
|
281
|
+
admitted = true;
|
|
282
|
+
clearTimeout(timer);
|
|
283
|
+
resolveReady();
|
|
284
|
+
} else if (message["type"] === "error") {
|
|
285
|
+
throw new Error(
|
|
286
|
+
typeof message["message"] === "string"
|
|
287
|
+
? message["message"]
|
|
288
|
+
: "Remote connection failed",
|
|
289
|
+
);
|
|
290
|
+
} else if (admitted) onMessage(message);
|
|
291
|
+
else throw new Error("Remote connection sent data before admission");
|
|
292
|
+
},
|
|
293
|
+
failed,
|
|
294
|
+
() => {
|
|
295
|
+
if (started || ended) return;
|
|
296
|
+
started = true;
|
|
297
|
+
wire?.send({
|
|
298
|
+
type: "start",
|
|
299
|
+
input: {
|
|
300
|
+
coreId: machine.id,
|
|
301
|
+
session: machine.session,
|
|
302
|
+
modules: source.modules,
|
|
303
|
+
},
|
|
304
|
+
});
|
|
305
|
+
},
|
|
306
|
+
{ maxMessageBytes: 8 * 1024 * 1024, maxQueuedBytes: 24 * 1024 * 1024 },
|
|
307
|
+
);
|
|
308
|
+
stream = attachCoreTerminal(
|
|
309
|
+
socketPath,
|
|
310
|
+
terminal,
|
|
311
|
+
(bytes) => wire?.push(bytes),
|
|
312
|
+
(error) => {
|
|
313
|
+
resolveClosed();
|
|
314
|
+
if (!ended) failed(error);
|
|
315
|
+
},
|
|
316
|
+
);
|
|
317
|
+
await stream.ready;
|
|
318
|
+
await ready;
|
|
319
|
+
return {
|
|
320
|
+
send(message) {
|
|
321
|
+
if (ended || !wire) throw new Error("Remote connection is closed");
|
|
322
|
+
wire.send(message);
|
|
323
|
+
},
|
|
324
|
+
close: () => finish(),
|
|
325
|
+
};
|
|
326
|
+
} catch (error) {
|
|
327
|
+
await finish(error instanceof Error ? error : new Error(String(error)));
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
@@ -2,7 +2,6 @@ import type { AssistantReader } from "../session-reader.js";
|
|
|
2
2
|
import type { SessionView } from "../types.js";
|
|
3
3
|
import { object } from "./protocol.js";
|
|
4
4
|
import type { RemoteCoreEndpoint } from "./remote-endpoint.js";
|
|
5
|
-
import { remoteExec } from "./remote-exec.js";
|
|
6
5
|
|
|
7
6
|
export class CoreRemoteReader implements AssistantReader {
|
|
8
7
|
private readonly endpoint: RemoteCoreEndpoint;
|
|
@@ -12,56 +11,18 @@ export class CoreRemoteReader implements AssistantReader {
|
|
|
12
11
|
async view(path?: string): Promise<SessionView> {
|
|
13
12
|
if (!path) return {};
|
|
14
13
|
return object(
|
|
15
|
-
await
|
|
16
|
-
op: "view",
|
|
17
|
-
path,
|
|
18
|
-
}),
|
|
14
|
+
await this.endpoint.call({ op: "view", path }),
|
|
19
15
|
) as SessionView;
|
|
20
16
|
}
|
|
21
17
|
async latest(path?: string) {
|
|
22
18
|
return (await this.view(path)).assistant;
|
|
23
19
|
}
|
|
24
|
-
|
|
20
|
+
watch(
|
|
25
21
|
paths: string[],
|
|
26
22
|
onChange: (path: string) => void,
|
|
27
23
|
onError: (error: Error) => void,
|
|
28
24
|
signal: AbortSignal,
|
|
29
25
|
): Promise<() => void> {
|
|
30
|
-
|
|
31
|
-
const abort = () => lifetime.abort();
|
|
32
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
33
|
-
if (signal.aborted) abort();
|
|
34
|
-
const close = () => {
|
|
35
|
-
signal.removeEventListener("abort", abort);
|
|
36
|
-
lifetime.abort();
|
|
37
|
-
};
|
|
38
|
-
void (async () => {
|
|
39
|
-
try {
|
|
40
|
-
while (!lifetime.signal.aborted) {
|
|
41
|
-
const changed = await remoteExec(
|
|
42
|
-
this.endpoint.host,
|
|
43
|
-
this.endpoint.machine,
|
|
44
|
-
{ op: "watch", paths },
|
|
45
|
-
{ signal: lifetime.signal },
|
|
46
|
-
);
|
|
47
|
-
if (lifetime.signal.aborted) break;
|
|
48
|
-
if (
|
|
49
|
-
!Array.isArray(changed) ||
|
|
50
|
-
!changed.every(
|
|
51
|
-
(path) => typeof path === "string" && paths.includes(path),
|
|
52
|
-
)
|
|
53
|
-
)
|
|
54
|
-
throw new Error("Remote session watch returned invalid paths");
|
|
55
|
-
// A bounded long-poll cannot promise continuity between processes.
|
|
56
|
-
for (const path of changed.length ? changed : paths) onChange(path);
|
|
57
|
-
}
|
|
58
|
-
} catch (error) {
|
|
59
|
-
if (!lifetime.signal.aborted)
|
|
60
|
-
onError(error instanceof Error ? error : new Error(String(error)));
|
|
61
|
-
} finally {
|
|
62
|
-
close();
|
|
63
|
-
}
|
|
64
|
-
})();
|
|
65
|
-
return close;
|
|
26
|
+
return this.endpoint.bridge.watch(paths, onChange, onError, signal);
|
|
66
27
|
}
|
|
67
28
|
}
|