@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
|
@@ -9,8 +9,11 @@ const MAX_MESSAGE = 512 * 1024 + 128;
|
|
|
9
9
|
* @param {(message:unknown)=>void} receive
|
|
10
10
|
* @param {(error:Error)=>void} fail
|
|
11
11
|
* @param {()=>void} [onLive]
|
|
12
|
+
* @param {{maxMessageBytes?:number,maxQueuedBytes?:number}} [limits]
|
|
12
13
|
*/
|
|
13
|
-
export function focusWire(nonce, write, receive, fail, onLive) {
|
|
14
|
+
export function focusWire(nonce, write, receive, fail, onLive, limits = {}) {
|
|
15
|
+
const maxMessage = limits.maxMessageBytes ?? MAX_MESSAGE;
|
|
16
|
+
const maxQueued = limits.maxQueuedBytes ?? LIMIT;
|
|
14
17
|
const prefix = `!${nonce}!`;
|
|
15
18
|
let buffer = "";
|
|
16
19
|
let ended = false;
|
|
@@ -78,9 +81,9 @@ export function focusWire(nonce, write, receive, fail, onLive) {
|
|
|
78
81
|
const json = JSON.stringify(message);
|
|
79
82
|
if (json === undefined) throw new Error("Focus stream requires JSON");
|
|
80
83
|
const bytes = Buffer.from(json);
|
|
81
|
-
if (bytes.length >
|
|
82
|
-
throw new Error("
|
|
83
|
-
if (queuedBytes + Math.ceil(bytes.length / 3) * 4 >
|
|
84
|
+
if (bytes.length > maxMessage)
|
|
85
|
+
throw new Error("Remote message exceeds its size limit");
|
|
86
|
+
if (queuedBytes + Math.ceil(bytes.length / 3) * 4 > maxQueued)
|
|
84
87
|
throw new Error("Focus stream output exceeded its buffer");
|
|
85
88
|
for (let at = 0; at < bytes.length; at += CHUNK) {
|
|
86
89
|
const data = bytes.subarray(at, at + CHUNK).toString("base64");
|
|
@@ -140,8 +143,8 @@ export function focusWire(nonce, write, receive, fail, onLive) {
|
|
|
140
143
|
throw new Error("Focus stream data is out of order or invalid");
|
|
141
144
|
const chunk = Buffer.from(frame.data, "base64");
|
|
142
145
|
chunkBytes += chunk.length;
|
|
143
|
-
if (chunkBytes >
|
|
144
|
-
throw new Error("
|
|
146
|
+
if (chunkBytes > maxMessage)
|
|
147
|
+
throw new Error("Remote message exceeds its size limit");
|
|
145
148
|
chunks.push(chunk);
|
|
146
149
|
received++;
|
|
147
150
|
if (frame.end) {
|
|
@@ -5,23 +5,25 @@ import type { FocusRequest, PaneInfo, PeerChannel } from "../types.js";
|
|
|
5
5
|
import type { CoreClient } from "./client.js";
|
|
6
6
|
import { attachCoreTerminal } from "./focus-channel-stream.js";
|
|
7
7
|
import { focusWire } from "./focus-channel-wire.mjs";
|
|
8
|
+
import type { CoreMachine } from "./machines.js";
|
|
8
9
|
import { object } from "./protocol.js";
|
|
9
|
-
import type { CoreMachine } from "./remote-exec.js";
|
|
10
10
|
|
|
11
11
|
const moduleUrl = (text: string) =>
|
|
12
12
|
`data:text/javascript;base64,${Buffer.from(text).toString("base64")}`;
|
|
13
13
|
async function helperSources() {
|
|
14
14
|
const read = (name: string) =>
|
|
15
15
|
readFile(new URL(name, import.meta.url), "utf8");
|
|
16
|
-
const [helper, wire, channel, peer] = await Promise.all([
|
|
16
|
+
const [helper, wire, channel, peer, terminal] = await Promise.all([
|
|
17
17
|
read("./focus-channel-helper.mjs"),
|
|
18
18
|
read("./focus-channel-wire.mjs"),
|
|
19
19
|
read("../remote/shepherdr-channel.mjs"),
|
|
20
20
|
read("../remote/shepherdr-peer.mjs"),
|
|
21
|
+
read("./terminal-peer.mjs"),
|
|
21
22
|
]);
|
|
22
23
|
return {
|
|
23
24
|
helper,
|
|
24
25
|
modules: {
|
|
26
|
+
terminal: moduleUrl(terminal),
|
|
25
27
|
wire: moduleUrl(wire),
|
|
26
28
|
channel: moduleUrl(
|
|
27
29
|
channel.replace(
|
|
@@ -190,7 +192,7 @@ export async function openCoreFocusChannel(
|
|
|
190
192
|
session: machine.session,
|
|
191
193
|
expected,
|
|
192
194
|
focus,
|
|
193
|
-
modules: { channel: modules.channel },
|
|
195
|
+
modules: { channel: modules.channel, terminal: modules.terminal },
|
|
194
196
|
},
|
|
195
197
|
});
|
|
196
198
|
}
|
package/src/core/focus-window.ts
CHANGED
|
@@ -11,6 +11,13 @@ const exec = promisify(execFile);
|
|
|
11
11
|
const delay = (ms: number) =>
|
|
12
12
|
new Promise<void>((resolve) => setTimeout(resolve, ms));
|
|
13
13
|
|
|
14
|
+
function rejected(cause: unknown): Error {
|
|
15
|
+
return Object.assign(
|
|
16
|
+
cause instanceof Error ? cause : new Error(String(cause)),
|
|
17
|
+
{ code: "SHEPHERDR_DELIVERY_REJECTED" },
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
14
21
|
function appSocket(): string {
|
|
15
22
|
return join(
|
|
16
23
|
process.env["HERDR_APP_HOME"] ||
|
|
@@ -79,10 +86,10 @@ export async function focusCoreWindow(
|
|
|
79
86
|
target: string,
|
|
80
87
|
): Promise<void> {
|
|
81
88
|
if (!/^t[0-9a-z]+$/.test(target))
|
|
82
|
-
throw
|
|
89
|
+
throw rejected("Use the exact local terminal address");
|
|
83
90
|
const socket = appSocket();
|
|
84
91
|
const original = await stat(socket).catch(() => {
|
|
85
|
-
throw
|
|
92
|
+
throw rejected("Open the Herdr window on this machine and retry focus");
|
|
86
93
|
});
|
|
87
94
|
const token = randomBytes(12).toString("hex");
|
|
88
95
|
const name = `probe-${token}`;
|
|
@@ -90,6 +97,7 @@ export async function focusCoreWindow(
|
|
|
90
97
|
const tab = `tb${token}`;
|
|
91
98
|
let probe: string | undefined;
|
|
92
99
|
let focused = false;
|
|
100
|
+
let dispatched = false;
|
|
93
101
|
let failed = false;
|
|
94
102
|
let failure: unknown;
|
|
95
103
|
try {
|
|
@@ -148,6 +156,7 @@ export async function focusCoreWindow(
|
|
|
148
156
|
};
|
|
149
157
|
await checkWindow();
|
|
150
158
|
if (!world.some((entry) => entry.terminal === target)) {
|
|
159
|
+
dispatched = true;
|
|
151
160
|
await app(["do", "space.select", JSON.stringify({ id: space })]);
|
|
152
161
|
await checkWindow();
|
|
153
162
|
await app(["do", "terminal.keep", JSON.stringify({ terminal: target })]);
|
|
@@ -163,11 +172,12 @@ export async function focusCoreWindow(
|
|
|
163
172
|
);
|
|
164
173
|
}
|
|
165
174
|
await checkWindow();
|
|
175
|
+
dispatched = true;
|
|
166
176
|
await app(["do", "pane.focus", JSON.stringify({ terminal: target })]);
|
|
167
177
|
focused = true;
|
|
168
178
|
} catch (error) {
|
|
169
179
|
failed = true;
|
|
170
|
-
failure = error;
|
|
180
|
+
failure = dispatched ? error : rejected(error);
|
|
171
181
|
} finally {
|
|
172
182
|
if (probe) {
|
|
173
183
|
try {
|
|
@@ -181,10 +191,11 @@ export async function focusCoreWindow(
|
|
|
181
191
|
if (failed) throw failure;
|
|
182
192
|
return;
|
|
183
193
|
}
|
|
184
|
-
|
|
194
|
+
const cleanupFailure = new Error(
|
|
185
195
|
`${focused ? "Focus succeeded" : `Focus failed: ${String(failure)}`} but its temporary terminal could not be closed: ${String(error)}`,
|
|
186
196
|
{ cause: failure ?? error },
|
|
187
197
|
);
|
|
198
|
+
throw dispatched ? cleanupFailure : rejected(cleanupFailure);
|
|
188
199
|
}
|
|
189
200
|
}
|
|
190
201
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Core owns this private terminal. Nothing touches stdout before attachment.
|
|
2
|
+
const bootstrap = JSON.parse(Buffer.from(process.argv[1], "base64").toString());
|
|
3
|
+
const { focusWire } = await import(bootstrap.wire);
|
|
4
|
+
let runtime;
|
|
5
|
+
let starting = false;
|
|
6
|
+
let stopped = false;
|
|
7
|
+
async function stop(error) {
|
|
8
|
+
if (stopped) return;
|
|
9
|
+
stopped = true;
|
|
10
|
+
if (error) {
|
|
11
|
+
try {
|
|
12
|
+
wire.send({ type: "error", message: error.message });
|
|
13
|
+
} catch {}
|
|
14
|
+
}
|
|
15
|
+
// Cleanup must finish before a replacement publishes the same context path.
|
|
16
|
+
const deadline = setTimeout(() => process.exit(1), 2000);
|
|
17
|
+
deadline.unref();
|
|
18
|
+
try {
|
|
19
|
+
await runtime?.close();
|
|
20
|
+
try {
|
|
21
|
+
wire.send({ type: "closed" });
|
|
22
|
+
} catch {}
|
|
23
|
+
} finally {
|
|
24
|
+
wire.close();
|
|
25
|
+
process.stdin.destroy();
|
|
26
|
+
process.stdout.end(() => process.exit(error ? 1 : 0));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const wire = focusWire(
|
|
30
|
+
bootstrap.nonce,
|
|
31
|
+
(bytes) => {
|
|
32
|
+
if (process.stdout.writableLength + bytes.length > 1024 * 1024)
|
|
33
|
+
throw new Error("Remote connection output exceeded its buffer");
|
|
34
|
+
process.stdout.write(bytes);
|
|
35
|
+
},
|
|
36
|
+
(value) => {
|
|
37
|
+
if (value?.type === "close") {
|
|
38
|
+
void stop();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (value?.type === "start" && !starting) {
|
|
42
|
+
starting = true;
|
|
43
|
+
void import(value.input.modules.helper)
|
|
44
|
+
.then(async ({ serve }) => {
|
|
45
|
+
runtime = await serve(
|
|
46
|
+
value.input,
|
|
47
|
+
(message) => wire.send(message),
|
|
48
|
+
stop,
|
|
49
|
+
);
|
|
50
|
+
if (stopped) {
|
|
51
|
+
await runtime.close();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
wire.send({ type: "ready" });
|
|
55
|
+
})
|
|
56
|
+
.catch(stop);
|
|
57
|
+
} else if (runtime) runtime.receive(value);
|
|
58
|
+
else throw new Error("Remote connection is not ready");
|
|
59
|
+
},
|
|
60
|
+
stop,
|
|
61
|
+
undefined,
|
|
62
|
+
{ maxMessageBytes: 8 * 1024 * 1024, maxQueuedBytes: 24 * 1024 * 1024 },
|
|
63
|
+
);
|
|
64
|
+
process.stdin.setRawMode(true);
|
|
65
|
+
process.stdin.on("data", (bytes) => wire.push(bytes));
|
|
66
|
+
process.stdin.on("error", stop);
|
|
67
|
+
process.stdin.on("end", () => void stop());
|
|
68
|
+
process.stdout.on("error", stop);
|
|
69
|
+
process.on("SIGTERM", () => void stop());
|
|
70
|
+
process.on("SIGHUP", () => void stop());
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
/** Reverse context traffic shares the live Core transport, never exec polls. */
|
|
4
|
+
export function helperContext(context, send) {
|
|
5
|
+
const incoming = new Map();
|
|
6
|
+
const outgoing = new Map();
|
|
7
|
+
const relays = new Map();
|
|
8
|
+
let closed = false;
|
|
9
|
+
function requestController(request, signal) {
|
|
10
|
+
signal.throwIfAborted();
|
|
11
|
+
if (closed) throw new Error("Shared context controller disconnected");
|
|
12
|
+
const id = randomUUID();
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const finish = (error, result) => {
|
|
15
|
+
if (!outgoing.delete(id)) return;
|
|
16
|
+
clearTimeout(timer);
|
|
17
|
+
signal.removeEventListener("abort", abort);
|
|
18
|
+
if (error) reject(error);
|
|
19
|
+
else resolve(result);
|
|
20
|
+
};
|
|
21
|
+
const abort = () => {
|
|
22
|
+
try {
|
|
23
|
+
if (!closed) send({ type: "context_cancel", id });
|
|
24
|
+
} catch (error) {
|
|
25
|
+
finish(error);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
finish(
|
|
29
|
+
new Error(
|
|
30
|
+
"Shared context request cancelled; a write may have completed. Reread before retrying",
|
|
31
|
+
),
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
const timer = setTimeout(abort, 35_000);
|
|
35
|
+
outgoing.set(id, { finish });
|
|
36
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
37
|
+
try {
|
|
38
|
+
send({ type: "context_request", id, request });
|
|
39
|
+
} catch (error) {
|
|
40
|
+
finish(error);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
async request(id, execute) {
|
|
46
|
+
if (closed) throw new Error("Shared context connection closed");
|
|
47
|
+
const controller = new AbortController();
|
|
48
|
+
incoming.set(id, controller);
|
|
49
|
+
try {
|
|
50
|
+
return await execute(controller.signal);
|
|
51
|
+
} finally {
|
|
52
|
+
incoming.delete(id);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
async start(threadId, routeId) {
|
|
56
|
+
if (closed) throw new Error("Shared context connection closed");
|
|
57
|
+
const path = context.relayContextPath(threadId, routeId);
|
|
58
|
+
let ready = relays.get(path);
|
|
59
|
+
if (!ready) {
|
|
60
|
+
ready = context.listenContext(path, requestController);
|
|
61
|
+
relays.set(path, ready);
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
await ready;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
relays.delete(path);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
return path;
|
|
70
|
+
},
|
|
71
|
+
consume(message) {
|
|
72
|
+
if (message.op === "context_cancel") {
|
|
73
|
+
incoming.get(message.id)?.abort();
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
if (message.op === "context_reply") {
|
|
77
|
+
outgoing
|
|
78
|
+
.get(message.id)
|
|
79
|
+
?.finish(
|
|
80
|
+
message.ok ? undefined : new Error(message.error),
|
|
81
|
+
message.result,
|
|
82
|
+
);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
},
|
|
87
|
+
async close() {
|
|
88
|
+
closed = true;
|
|
89
|
+
for (const controller of incoming.values()) controller.abort();
|
|
90
|
+
for (const entry of outgoing.values())
|
|
91
|
+
entry.finish(new Error("Shared context controller disconnected"));
|
|
92
|
+
await Promise.allSettled(
|
|
93
|
+
[...relays.values()].map(async (ready) => (await ready)()),
|
|
94
|
+
);
|
|
95
|
+
relays.clear();
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { createConnection } from "node:net";
|
|
2
|
+
|
|
3
|
+
// The helper talks to its own Core, never to a guessed socket or another host.
|
|
4
|
+
export function localCore(socketPath) {
|
|
5
|
+
const connections = new Set();
|
|
6
|
+
let closed = false;
|
|
7
|
+
function open(method, params, receive, fail) {
|
|
8
|
+
if (closed) throw new Error("Core connection closed");
|
|
9
|
+
const socket = createConnection(socketPath);
|
|
10
|
+
connections.add(socket);
|
|
11
|
+
let buffer = "";
|
|
12
|
+
let ended = false;
|
|
13
|
+
const finish = () => {
|
|
14
|
+
if (ended) return;
|
|
15
|
+
ended = true;
|
|
16
|
+
clearTimeout(timer);
|
|
17
|
+
connections.delete(socket);
|
|
18
|
+
socket.destroy();
|
|
19
|
+
};
|
|
20
|
+
const failed = (error = new Error("Core connection closed")) => {
|
|
21
|
+
if (ended) return;
|
|
22
|
+
finish();
|
|
23
|
+
fail(error);
|
|
24
|
+
};
|
|
25
|
+
const timer = setTimeout(
|
|
26
|
+
() => failed(new Error("Core connection timed out")),
|
|
27
|
+
45_000,
|
|
28
|
+
);
|
|
29
|
+
socket.setEncoding("utf8");
|
|
30
|
+
socket.on("connect", () =>
|
|
31
|
+
socket.write(
|
|
32
|
+
JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }) + "\n",
|
|
33
|
+
),
|
|
34
|
+
);
|
|
35
|
+
socket.on("error", failed);
|
|
36
|
+
socket.on("close", () => failed());
|
|
37
|
+
socket.on("data", (chunk) => {
|
|
38
|
+
try {
|
|
39
|
+
timer.refresh();
|
|
40
|
+
buffer += chunk;
|
|
41
|
+
for (;;) {
|
|
42
|
+
const end = buffer.indexOf("\n");
|
|
43
|
+
if (
|
|
44
|
+
Buffer.byteLength(end < 0 ? buffer : buffer.slice(0, end)) >
|
|
45
|
+
32 * 1024 * 1024
|
|
46
|
+
)
|
|
47
|
+
throw new Error("Core response exceeded its size limit");
|
|
48
|
+
if (end < 0 || ended) break;
|
|
49
|
+
const value = JSON.parse(buffer.slice(0, end));
|
|
50
|
+
buffer = buffer.slice(end + 1);
|
|
51
|
+
if (value.jsonrpc !== "2.0") throw new Error("Invalid Core response");
|
|
52
|
+
if (value.error)
|
|
53
|
+
throw Object.assign(
|
|
54
|
+
new Error(value.error.message),
|
|
55
|
+
value.error.data,
|
|
56
|
+
);
|
|
57
|
+
receive(value, finish);
|
|
58
|
+
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
failed(error);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
return finish;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
request(method, params = {}) {
|
|
67
|
+
return new Promise((resolve, reject) =>
|
|
68
|
+
open(
|
|
69
|
+
method,
|
|
70
|
+
params,
|
|
71
|
+
(value, finish) => {
|
|
72
|
+
if (value.id !== 1 || !("result" in value))
|
|
73
|
+
throw new Error("Invalid Core response");
|
|
74
|
+
finish();
|
|
75
|
+
resolve(value.result);
|
|
76
|
+
},
|
|
77
|
+
reject,
|
|
78
|
+
),
|
|
79
|
+
);
|
|
80
|
+
},
|
|
81
|
+
subscribe(filter, receive, fail) {
|
|
82
|
+
let initial = true;
|
|
83
|
+
let stop;
|
|
84
|
+
let floor = 0;
|
|
85
|
+
const ready = new Promise((resolve, reject) => {
|
|
86
|
+
stop = open(
|
|
87
|
+
"events.subscribe",
|
|
88
|
+
filter,
|
|
89
|
+
(value) => {
|
|
90
|
+
if (initial) {
|
|
91
|
+
if (value.id !== 1 || !Array.isArray(value.result?.terminals))
|
|
92
|
+
throw new Error("Invalid Core snapshot");
|
|
93
|
+
initial = false;
|
|
94
|
+
floor = value.result.seq;
|
|
95
|
+
receive({ method: "snapshot", ...value.result });
|
|
96
|
+
resolve();
|
|
97
|
+
} else if (value.method === "event") {
|
|
98
|
+
if (value.params.seq > floor)
|
|
99
|
+
receive({ method: "event", event: value.params });
|
|
100
|
+
} else if (value.method === "missed") {
|
|
101
|
+
floor = value.params.seq;
|
|
102
|
+
receive({ method: "missed", ...value.params });
|
|
103
|
+
} else if (value.method === "heartbeat")
|
|
104
|
+
receive({ method: "heartbeat", seq: value.params.seq });
|
|
105
|
+
else throw new Error("Invalid Core event");
|
|
106
|
+
},
|
|
107
|
+
(error) => {
|
|
108
|
+
reject(error);
|
|
109
|
+
fail(error);
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
return { ready, close: () => stop?.() };
|
|
114
|
+
},
|
|
115
|
+
close() {
|
|
116
|
+
closed = true;
|
|
117
|
+
for (const socket of connections) socket.destroy();
|
|
118
|
+
connections.clear();
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { CoreClient } from "./client.js";
|
|
2
|
+
import { object } from "./protocol.js";
|
|
3
|
+
|
|
4
|
+
export interface CoreMachine {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
host: string;
|
|
8
|
+
session: string;
|
|
9
|
+
state: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function coreMachines(client: CoreClient): Promise<CoreMachine[]> {
|
|
13
|
+
const machines = object(await client.request("machine.list"))["machines"];
|
|
14
|
+
if (!Array.isArray(machines))
|
|
15
|
+
throw new Error("Core returned no machine catalog");
|
|
16
|
+
return machines.map((value) => {
|
|
17
|
+
const machine = object(value);
|
|
18
|
+
for (const field of ["id", "name", "host", "state"])
|
|
19
|
+
if (typeof machine[field] !== "string" || !machine[field])
|
|
20
|
+
throw new Error("Core returned an invalid machine identity");
|
|
21
|
+
return {
|
|
22
|
+
id: machine["id"] as string,
|
|
23
|
+
name: machine["name"] as string,
|
|
24
|
+
host: machine["host"] as string,
|
|
25
|
+
state: machine["state"] as string,
|
|
26
|
+
session:
|
|
27
|
+
typeof machine["session"] === "string" ? machine["session"] : "default",
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
}
|
package/src/core/registration.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { CoreClient } from "./client.js";
|
|
|
3
3
|
import { object } from "./protocol.js";
|
|
4
4
|
|
|
5
5
|
export const CORE_SESSION_LABEL = "pi.session_file";
|
|
6
|
+
export const CORE_WORKER_LABEL = "pi.shepherdr.worker_session";
|
|
6
7
|
|
|
7
8
|
export interface CoreIdentity {
|
|
8
9
|
terminalId: string;
|
|
@@ -21,13 +22,17 @@ export function coreIdentity(ctx: ExtensionContext): CoreIdentity | undefined {
|
|
|
21
22
|
export async function publishCoreSession(
|
|
22
23
|
identity: CoreIdentity,
|
|
23
24
|
sessionFile: string,
|
|
25
|
+
worker: boolean,
|
|
24
26
|
): Promise<() => Promise<void>> {
|
|
25
27
|
const client = new CoreClient(identity.socketPath);
|
|
26
28
|
const terminal = identity.terminalId;
|
|
27
29
|
try {
|
|
28
30
|
await client.request("terminal.label", {
|
|
29
31
|
terminal,
|
|
30
|
-
labels: {
|
|
32
|
+
labels: {
|
|
33
|
+
[CORE_SESSION_LABEL]: sessionFile,
|
|
34
|
+
[CORE_WORKER_LABEL]: worker ? sessionFile : "",
|
|
35
|
+
},
|
|
31
36
|
});
|
|
32
37
|
} catch (error) {
|
|
33
38
|
client.close();
|
|
@@ -42,7 +47,7 @@ export async function publishCoreSession(
|
|
|
42
47
|
if (labels[CORE_SESSION_LABEL] === sessionFile)
|
|
43
48
|
await client.request("terminal.label", {
|
|
44
49
|
terminal,
|
|
45
|
-
labels: { [CORE_SESSION_LABEL]: "" },
|
|
50
|
+
labels: { [CORE_SESSION_LABEL]: "", [CORE_WORKER_LABEL]: "" },
|
|
46
51
|
});
|
|
47
52
|
} finally {
|
|
48
53
|
client.close();
|