@howaboua/pi-shepherdr 0.2.24 → 0.2.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/README.md +20 -12
- package/index.ts +2 -2
- package/package.json +10 -11
- package/src/agent-identity.ts +75 -0
- package/src/agents-contract.ts +20 -12
- package/src/agents-discovery.ts +79 -34
- package/src/agents-focus.ts +15 -9
- package/src/agents-spawn.ts +25 -7
- package/src/agents-tool.ts +35 -7
- package/src/agents-work.ts +2 -2
- package/src/attachment-notes.ts +25 -24
- package/src/attachment-protocol.ts +35 -15
- package/src/attachment-routing.ts +28 -24
- package/src/attachment-target.ts +58 -19
- package/src/attachment.ts +13 -17
- package/src/board/host.ts +19 -20
- package/src/board/membership.ts +49 -30
- package/src/close-idle.ts +89 -0
- package/src/context-routing.ts +196 -0
- package/src/control-panel/index.ts +3 -1
- package/src/control-panel/items.ts +14 -2
- package/src/control-panel/view.ts +6 -2
- package/src/controller.ts +11 -2
- package/src/core/AGENTS.md +5 -0
- package/src/core/backend.ts +266 -0
- package/src/core/client.ts +249 -0
- package/src/core/context-relay.mjs +111 -0
- package/src/core/focus-channel-helper.mjs +165 -0
- package/src/core/focus-channel-stream.ts +140 -0
- package/src/core/focus-channel-wire.mjs +160 -0
- package/src/core/focus-channel.ts +213 -0
- package/src/core/focus-window.ts +192 -0
- package/src/core/pi-executable.mjs +34 -0
- package/src/core/protocol.ts +116 -0
- package/src/core/registration.ts +51 -0
- package/src/core/remote-endpoint.ts +119 -0
- package/src/core/remote-exec.ts +122 -0
- package/src/core/remote-helper.mjs +244 -0
- package/src/core/remote-reader.ts +67 -0
- package/src/core/remote-relay.ts +191 -0
- package/src/core/request.ts +124 -0
- package/src/core/spawn.ts +177 -0
- package/src/core/status.ts +68 -0
- package/src/core/terminal.ts +103 -0
- package/src/fleet/catalog.ts +83 -0
- package/src/fleet/connection.ts +111 -0
- package/src/fleet/index.ts +161 -122
- package/src/fleet/inspection.ts +19 -1
- package/src/fleet/machine.ts +3 -0
- package/src/fleet/monitor.ts +18 -4
- package/src/fleet/persistence.ts +26 -3
- package/src/fleet/setup.ts +54 -13
- package/src/herdr.ts +25 -5
- package/src/launch-contract.ts +48 -0
- package/src/launch-location.ts +151 -0
- package/src/launch.ts +30 -183
- package/src/messages/attribution.ts +15 -6
- package/src/messages/events.ts +10 -4
- package/src/messages/focus.ts +11 -2
- package/src/messages/guidance.ts +22 -16
- package/src/monitor-extension-messages.ts +9 -2
- package/src/monitor-record.ts +13 -6
- package/src/monitor-state.ts +18 -4
- package/src/monitor.ts +31 -0
- package/src/peer-commands.ts +11 -0
- package/src/peer-inbox-delivery.ts +171 -0
- package/src/peer-inbox-focus.ts +113 -0
- package/src/peer-inbox.ts +126 -208
- package/src/remote/deployment.ts +1 -1
- package/src/remote/shepherdr-checkpoints.mjs +20 -2
- package/src/remote/shepherdr-context.mjs +15 -3
- package/src/remote/shepherdr-peer.mjs +18 -5
- package/src/remote/shepherdr-session.mjs +9 -2
- package/src/remote/shepherdr.mjs +1 -1
- package/src/settlement/collector.ts +2 -2
- package/src/settlement/index.ts +12 -0
- package/src/shared-context-host.ts +7 -18
- package/src/shared-context.ts +139 -12
- package/src/spawn-context.ts +10 -5
- package/src/spawn-ownership.ts +91 -0
- package/src/types.ts +10 -5
- package/src/worker-ownership.ts +92 -0
- package/src/worker-service.ts +13 -82
- package/changelog.ts +0 -337
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
RolledBackAgentLaunchError,
|
|
4
|
+
type StartAgentParams,
|
|
5
|
+
type StartedAgent,
|
|
6
|
+
UnstartedAgentLaunchError,
|
|
7
|
+
} from "../launch-contract.js";
|
|
8
|
+
import type { CoreConnection } from "./backend.js";
|
|
9
|
+
import { piExecutable } from "./pi-executable.mjs";
|
|
10
|
+
import { object } from "./protocol.js";
|
|
11
|
+
import { RemoteCoreEndpoint } from "./remote-endpoint.js";
|
|
12
|
+
import { remoteExec } from "./remote-exec.js";
|
|
13
|
+
import { terminalPane, terminalSnapshot } from "./terminal.js";
|
|
14
|
+
|
|
15
|
+
export async function startCoreAgent(
|
|
16
|
+
client: CoreConnection,
|
|
17
|
+
params: StartAgentParams,
|
|
18
|
+
cwd: string,
|
|
19
|
+
_label: string,
|
|
20
|
+
args: string[],
|
|
21
|
+
): Promise<StartedAgent> {
|
|
22
|
+
if (params.placement === "pane")
|
|
23
|
+
throw new Error(
|
|
24
|
+
"Core cannot launch a program inside an existing terminal without terminal input. Use terminal placement for a background worker or new_tab/new_workspace for a Herdr window location.",
|
|
25
|
+
);
|
|
26
|
+
const labels: Record<string, string> = {};
|
|
27
|
+
if (params.placement === "new_tab") {
|
|
28
|
+
const snapshot = terminalSnapshot(
|
|
29
|
+
await client.core.request("terminal.list"),
|
|
30
|
+
);
|
|
31
|
+
if (
|
|
32
|
+
!params.workspace ||
|
|
33
|
+
!snapshot.workspaces.some(
|
|
34
|
+
(space) => space.workspace_id === params.workspace,
|
|
35
|
+
)
|
|
36
|
+
)
|
|
37
|
+
throw new UnstartedAgentLaunchError(
|
|
38
|
+
"new_tab requires a space ID advertised by a live terminal on this Core; use list to find it, or choose new_workspace/terminal placement",
|
|
39
|
+
);
|
|
40
|
+
labels["herdr.space"] = params.workspace;
|
|
41
|
+
labels["herdr.tab"] = `tb${randomUUID().replaceAll("-", "")}`;
|
|
42
|
+
} else if (params.placement === "new_workspace") {
|
|
43
|
+
labels["herdr.space"] = `sp${randomUUID().replaceAll("-", "")}`;
|
|
44
|
+
labels["herdr.tab"] = `tb${randomUUID().replaceAll("-", "")}`;
|
|
45
|
+
} else if (params.placement !== "terminal") {
|
|
46
|
+
throw new UnstartedAgentLaunchError(
|
|
47
|
+
"Choose terminal, new_tab or new_workspace placement for Core",
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
const executable =
|
|
51
|
+
client.core instanceof RemoteCoreEndpoint
|
|
52
|
+
? await remoteExec(client.core.host, client.core.machine, {
|
|
53
|
+
op: "pi-executable",
|
|
54
|
+
cwd,
|
|
55
|
+
})
|
|
56
|
+
: await piExecutable(cwd);
|
|
57
|
+
if (typeof executable !== "string" || !executable)
|
|
58
|
+
throw new Error("Could not resolve Pi on the target machine");
|
|
59
|
+
const result = object(
|
|
60
|
+
await client.core.request("terminal.run", {
|
|
61
|
+
argv: [executable, ...args],
|
|
62
|
+
name: params.name,
|
|
63
|
+
cwd,
|
|
64
|
+
parent: process.env["HC_TERMINAL_ID"],
|
|
65
|
+
outlive_parent: true,
|
|
66
|
+
no_notify: true,
|
|
67
|
+
// Labels request adoption only by a window connected to this Core.
|
|
68
|
+
labels,
|
|
69
|
+
env: process.env,
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
const terminal = result["address"];
|
|
73
|
+
if (typeof terminal !== "string" || !/^t[0-9a-z]+$/.test(terminal))
|
|
74
|
+
throw new Error(
|
|
75
|
+
"Core launch returned no local terminal address; inspect Core terminals before retrying",
|
|
76
|
+
);
|
|
77
|
+
const cleanup = { method: "terminal.close" as const, id: terminal };
|
|
78
|
+
try {
|
|
79
|
+
await client.core.request("terminal.label", {
|
|
80
|
+
terminal,
|
|
81
|
+
labels: { "pi.shepherdr.name": params.name },
|
|
82
|
+
});
|
|
83
|
+
const agent = await waitReady(client, terminal);
|
|
84
|
+
return { agent, id: terminal, cleanup };
|
|
85
|
+
} catch (error) {
|
|
86
|
+
try {
|
|
87
|
+
await client.request(cleanup.method, { id: cleanup.id });
|
|
88
|
+
} catch (cleanupError) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`${String(error)}; cleanup failed: ${String(cleanupError)}`,
|
|
91
|
+
{ cause: error },
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
throw new RolledBackAgentLaunchError(error);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function waitReady(client: CoreConnection, terminal: string) {
|
|
99
|
+
const abort = new AbortController();
|
|
100
|
+
let chain = Promise.resolve();
|
|
101
|
+
let resolve!: (agent: ReturnType<typeof terminalPane>) => void;
|
|
102
|
+
let reject!: (error: unknown) => void;
|
|
103
|
+
const ready = new Promise<ReturnType<typeof terminalPane>>((yes, no) => {
|
|
104
|
+
resolve = yes;
|
|
105
|
+
reject = no;
|
|
106
|
+
});
|
|
107
|
+
const inspect = (value: unknown) => {
|
|
108
|
+
const record = object(value);
|
|
109
|
+
if (record["state"] !== "live")
|
|
110
|
+
throw new Error(
|
|
111
|
+
`Core terminal ${terminal} exited before publishing its Pi session; inspect its output`,
|
|
112
|
+
);
|
|
113
|
+
const agent = terminalPane(record);
|
|
114
|
+
if (agent.agent_status === "blocked")
|
|
115
|
+
throw new Error(
|
|
116
|
+
`Core terminal ${terminal} is blocked during startup; inspect its output`,
|
|
117
|
+
);
|
|
118
|
+
if (agent.interactive_ready) resolve(agent);
|
|
119
|
+
};
|
|
120
|
+
const timer = setTimeout(
|
|
121
|
+
() =>
|
|
122
|
+
reject(
|
|
123
|
+
new Error(
|
|
124
|
+
`Timed out waiting for ${terminal} to publish its Pi session; inspect its output`,
|
|
125
|
+
),
|
|
126
|
+
),
|
|
127
|
+
30_000,
|
|
128
|
+
);
|
|
129
|
+
try {
|
|
130
|
+
const subscribed = client.core
|
|
131
|
+
.subscribe(
|
|
132
|
+
{ terminal },
|
|
133
|
+
(message) => {
|
|
134
|
+
if (message.method === "snapshot" || message.method === "missed") {
|
|
135
|
+
// Remote mirrors may not contain an acknowledged launch yet.
|
|
136
|
+
if (message.terminals.length === 0)
|
|
137
|
+
chain = chain
|
|
138
|
+
.then(async () =>
|
|
139
|
+
inspect(
|
|
140
|
+
await client.core.request(
|
|
141
|
+
"terminal.get",
|
|
142
|
+
{ terminal },
|
|
143
|
+
{ signal: abort.signal },
|
|
144
|
+
),
|
|
145
|
+
),
|
|
146
|
+
)
|
|
147
|
+
.catch(reject);
|
|
148
|
+
for (const record of message.terminals) inspect(record);
|
|
149
|
+
} else if (message.method === "event") {
|
|
150
|
+
chain = chain
|
|
151
|
+
.then(async () =>
|
|
152
|
+
inspect(
|
|
153
|
+
await client.core.request(
|
|
154
|
+
"terminal.get",
|
|
155
|
+
{ terminal },
|
|
156
|
+
{ signal: abort.signal },
|
|
157
|
+
),
|
|
158
|
+
),
|
|
159
|
+
)
|
|
160
|
+
.catch(reject);
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
{ signal: abort.signal },
|
|
164
|
+
)
|
|
165
|
+
.then((sub) => {
|
|
166
|
+
void sub.closed.then((error) => {
|
|
167
|
+
if (error) reject(error);
|
|
168
|
+
});
|
|
169
|
+
return sub;
|
|
170
|
+
});
|
|
171
|
+
const [, agent] = await Promise.all([subscribed, ready]);
|
|
172
|
+
return agent;
|
|
173
|
+
} finally {
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
abort.abort();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
/** Core observes only status reports written by the program in its terminal. */
|
|
4
|
+
export function registerCoreStatus(pi: ExtensionAPI): void {
|
|
5
|
+
if (
|
|
6
|
+
!process.env["HC_TERMINAL_ID"] ||
|
|
7
|
+
!process.env["HC_TERMINAL_SOCKET"] ||
|
|
8
|
+
!process.stdout.isTTY
|
|
9
|
+
)
|
|
10
|
+
return;
|
|
11
|
+
let active = false;
|
|
12
|
+
let running = false;
|
|
13
|
+
let failed = false;
|
|
14
|
+
let blocked = 0;
|
|
15
|
+
let last = "";
|
|
16
|
+
const publish = (state: string) => {
|
|
17
|
+
if (!active || state === last) return;
|
|
18
|
+
last = state;
|
|
19
|
+
process.stdout.write(`\x1b]7501;state=${state}:app=pi\x1b\\`);
|
|
20
|
+
};
|
|
21
|
+
const current = () =>
|
|
22
|
+
publish(blocked ? "blocked" : running ? "working" : "idle");
|
|
23
|
+
pi.events.on("herdr:blocked", (value: unknown) => {
|
|
24
|
+
if (
|
|
25
|
+
!active ||
|
|
26
|
+
typeof value !== "object" ||
|
|
27
|
+
value === null ||
|
|
28
|
+
!("active" in value) ||
|
|
29
|
+
typeof value.active !== "boolean"
|
|
30
|
+
)
|
|
31
|
+
return;
|
|
32
|
+
blocked = value.active ? blocked + 1 : Math.max(0, blocked - 1);
|
|
33
|
+
current();
|
|
34
|
+
});
|
|
35
|
+
pi.on("session_start", (_event, ctx) => {
|
|
36
|
+
active = ctx.mode === "tui";
|
|
37
|
+
blocked = 0;
|
|
38
|
+
running = !ctx.isIdle();
|
|
39
|
+
failed = false;
|
|
40
|
+
last = "";
|
|
41
|
+
current();
|
|
42
|
+
});
|
|
43
|
+
pi.on("before_agent_start", () => {
|
|
44
|
+
running = true;
|
|
45
|
+
failed = false;
|
|
46
|
+
current();
|
|
47
|
+
});
|
|
48
|
+
pi.on("agent_start", () => {
|
|
49
|
+
running = true;
|
|
50
|
+
current();
|
|
51
|
+
});
|
|
52
|
+
pi.on("agent_end", (event) => {
|
|
53
|
+
const reply = event.messages.findLast(
|
|
54
|
+
(message) => message.role === "assistant",
|
|
55
|
+
);
|
|
56
|
+
failed = reply?.role === "assistant" && reply.stopReason === "error";
|
|
57
|
+
});
|
|
58
|
+
pi.on("agent_settled", (event) => {
|
|
59
|
+
running = false;
|
|
60
|
+
publish(
|
|
61
|
+
blocked ? "blocked" : event.aborted ? "idle" : failed ? "error" : "done",
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
pi.on("session_shutdown", () => {
|
|
65
|
+
publish("clear");
|
|
66
|
+
active = false;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { AgentStatus, PaneInfo, SessionSnapshot } from "../types.js";
|
|
2
|
+
import { object } from "./protocol.js";
|
|
3
|
+
import { CORE_SESSION_LABEL } from "./registration.js";
|
|
4
|
+
|
|
5
|
+
export function terminalPane(value: unknown): PaneInfo {
|
|
6
|
+
const terminal = object(value);
|
|
7
|
+
const address = terminal["address"];
|
|
8
|
+
if (typeof address !== "string" || !address || address.includes("/"))
|
|
9
|
+
throw new Error("Expected a local Core terminal address");
|
|
10
|
+
const labels = object(terminal["labels"]);
|
|
11
|
+
const status = terminal["status"] == null ? {} : object(terminal["status"]);
|
|
12
|
+
const state = status["state"];
|
|
13
|
+
const agentStatus: AgentStatus =
|
|
14
|
+
state === "error"
|
|
15
|
+
? "done"
|
|
16
|
+
: state === "idle" ||
|
|
17
|
+
state === "working" ||
|
|
18
|
+
state === "blocked" ||
|
|
19
|
+
state === "done"
|
|
20
|
+
? state
|
|
21
|
+
: "unknown";
|
|
22
|
+
const session = labels[CORE_SESSION_LABEL];
|
|
23
|
+
const live = terminal["state"] === "live";
|
|
24
|
+
return {
|
|
25
|
+
backend: "core",
|
|
26
|
+
// The shared legacy key is an address, not a fabricated app pane ID.
|
|
27
|
+
pane_id: address,
|
|
28
|
+
terminal_id: address,
|
|
29
|
+
agent: typeof session === "string" && session ? "pi" : null,
|
|
30
|
+
agent_status: agentStatus,
|
|
31
|
+
interactive_ready: live && typeof session === "string" && !!session,
|
|
32
|
+
launch_pending: live && !session,
|
|
33
|
+
...(typeof terminal["cwd"] === "string" ? { cwd: terminal["cwd"] } : {}),
|
|
34
|
+
...(typeof terminal["title"] === "string"
|
|
35
|
+
? { title: terminal["title"] }
|
|
36
|
+
: {}),
|
|
37
|
+
...(typeof labels["pi.shepherdr.name"] === "string"
|
|
38
|
+
? { name: labels["pi.shepherdr.name"] }
|
|
39
|
+
: typeof terminal["name"] === "string"
|
|
40
|
+
? { name: terminal["name"] }
|
|
41
|
+
: {}),
|
|
42
|
+
...(typeof labels["herdr.space"] === "string" && labels["herdr.space"]
|
|
43
|
+
? { workspace_id: labels["herdr.space"] }
|
|
44
|
+
: {}),
|
|
45
|
+
...(typeof labels["herdr.tab"] === "string" && labels["herdr.tab"]
|
|
46
|
+
? { tab_id: labels["herdr.tab"] }
|
|
47
|
+
: {}),
|
|
48
|
+
...(typeof session === "string" && session
|
|
49
|
+
? {
|
|
50
|
+
agent_session: {
|
|
51
|
+
agent: "pi",
|
|
52
|
+
kind: "path",
|
|
53
|
+
source: CORE_SESSION_LABEL,
|
|
54
|
+
value: session,
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
: {}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function terminalSnapshot(value: unknown): SessionSnapshot {
|
|
62
|
+
const terminals = object(value)["terminals"];
|
|
63
|
+
if (!Array.isArray(terminals)) throw new Error("Core returned no terminals");
|
|
64
|
+
const panes = terminals
|
|
65
|
+
.filter((value) => {
|
|
66
|
+
const terminal = object(value);
|
|
67
|
+
return (
|
|
68
|
+
terminal["state"] === "live" &&
|
|
69
|
+
typeof terminal["address"] === "string" &&
|
|
70
|
+
!terminal["address"].includes("/")
|
|
71
|
+
);
|
|
72
|
+
})
|
|
73
|
+
.map(terminalPane);
|
|
74
|
+
const workspaces = [
|
|
75
|
+
...new Set(
|
|
76
|
+
panes.flatMap((pane) => (pane.workspace_id ? [pane.workspace_id] : [])),
|
|
77
|
+
),
|
|
78
|
+
].map((id) => ({ workspace_id: id, label: id }));
|
|
79
|
+
const tabs = [
|
|
80
|
+
...new Map(
|
|
81
|
+
panes.flatMap((pane) =>
|
|
82
|
+
pane.tab_id && pane.workspace_id
|
|
83
|
+
? [
|
|
84
|
+
[
|
|
85
|
+
pane.tab_id,
|
|
86
|
+
{
|
|
87
|
+
tab_id: pane.tab_id,
|
|
88
|
+
workspace_id: pane.workspace_id,
|
|
89
|
+
label: pane.tab_id,
|
|
90
|
+
},
|
|
91
|
+
] as const,
|
|
92
|
+
]
|
|
93
|
+
: [],
|
|
94
|
+
),
|
|
95
|
+
).values(),
|
|
96
|
+
];
|
|
97
|
+
return {
|
|
98
|
+
panes,
|
|
99
|
+
agents: panes.filter((pane) => pane.agent === "pi"),
|
|
100
|
+
tabs,
|
|
101
|
+
workspaces,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { CoreConnection } from "../core/backend.js";
|
|
2
|
+
import type { CoreMachine } from "../core/remote-exec.js";
|
|
3
|
+
import type { SshMachine } from "../machine-catalog.js";
|
|
4
|
+
import { RemoteHerdrClient } from "../remote/client.js";
|
|
5
|
+
import type { MachineRuntime } from "./machine.js";
|
|
6
|
+
|
|
7
|
+
/** Reconcile catalog changes without changing fleet identity or scheduling retries. */
|
|
8
|
+
export function reloadCoreCatalog(
|
|
9
|
+
runtimes: Map<string, MachineRuntime>,
|
|
10
|
+
catalog: CoreMachine[],
|
|
11
|
+
connect: (id: string) => void,
|
|
12
|
+
): void {
|
|
13
|
+
for (const [id, runtime] of runtimes) {
|
|
14
|
+
if (
|
|
15
|
+
runtime.local ||
|
|
16
|
+
catalog.some(
|
|
17
|
+
(machine) =>
|
|
18
|
+
machine.id === id && machine.name === runtime.coreMachine?.name,
|
|
19
|
+
)
|
|
20
|
+
)
|
|
21
|
+
continue;
|
|
22
|
+
runtime.monitor?.deactivate();
|
|
23
|
+
if (runtime.client instanceof CoreConnection) runtime.client.close();
|
|
24
|
+
if (!catalog.some((machine) => machine.id === id)) runtimes.delete(id);
|
|
25
|
+
else {
|
|
26
|
+
runtime.pending = runtime.monitor?.list() ?? runtime.pending;
|
|
27
|
+
delete runtime.client;
|
|
28
|
+
delete runtime.monitor;
|
|
29
|
+
runtime.status = "unavailable";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
for (const machine of catalog) {
|
|
33
|
+
let runtime = runtimes.get(machine.id);
|
|
34
|
+
if (runtime) runtime.coreMachine = machine;
|
|
35
|
+
else {
|
|
36
|
+
runtime = {
|
|
37
|
+
coreMachine: machine,
|
|
38
|
+
local: false,
|
|
39
|
+
pending: [],
|
|
40
|
+
status: "connecting",
|
|
41
|
+
};
|
|
42
|
+
runtimes.set(machine.id, runtime);
|
|
43
|
+
}
|
|
44
|
+
if (!runtime.client) connect(machine.id);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function reloadSshCatalog(
|
|
49
|
+
runtimes: Map<string, MachineRuntime>,
|
|
50
|
+
catalog: Record<string, SshMachine>,
|
|
51
|
+
): string[] {
|
|
52
|
+
const reconnect: string[] = [];
|
|
53
|
+
|
|
54
|
+
for (const [name, runtime] of runtimes) {
|
|
55
|
+
if (runtime.local || catalog[name]?.enabled) continue;
|
|
56
|
+
runtime.monitor?.deactivate();
|
|
57
|
+
if (runtime.client instanceof RemoteHerdrClient) runtime.client.close();
|
|
58
|
+
runtimes.delete(name);
|
|
59
|
+
}
|
|
60
|
+
for (const [name, machineConfig] of Object.entries(catalog)) {
|
|
61
|
+
if (!machineConfig.enabled) continue;
|
|
62
|
+
const runtime = runtimes.get(name);
|
|
63
|
+
if (
|
|
64
|
+
runtime?.config &&
|
|
65
|
+
runtime.config.target === machineConfig.target &&
|
|
66
|
+
runtime.config.session === machineConfig.session
|
|
67
|
+
) {
|
|
68
|
+
runtime.config = machineConfig;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const pending = runtime?.monitor?.list() ?? runtime?.pending ?? [];
|
|
72
|
+
runtime?.monitor?.deactivate();
|
|
73
|
+
if (runtime?.client instanceof RemoteHerdrClient) runtime.client.close();
|
|
74
|
+
runtimes.set(name, {
|
|
75
|
+
config: machineConfig,
|
|
76
|
+
local: false,
|
|
77
|
+
pending,
|
|
78
|
+
status: "connecting",
|
|
79
|
+
});
|
|
80
|
+
reconnect.push(name);
|
|
81
|
+
}
|
|
82
|
+
return reconnect;
|
|
83
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { CoreConnection } from "../core/backend.js";
|
|
3
|
+
import type { AgentMonitor } from "../monitor.js";
|
|
4
|
+
import { RemoteHerdrClient } from "../remote/client.js";
|
|
5
|
+
import { errorMessage, type MachineRuntime } from "./machine.js";
|
|
6
|
+
|
|
7
|
+
interface ConnectionEvents {
|
|
8
|
+
isCurrent(): boolean;
|
|
9
|
+
refresh(): void;
|
|
10
|
+
createMonitor(client: RemoteHerdrClient | CoreConnection): AgentMonitor;
|
|
11
|
+
needsContextRelay(): boolean;
|
|
12
|
+
prepareContextRelay(
|
|
13
|
+
client: RemoteHerdrClient | CoreConnection,
|
|
14
|
+
): Promise<void>;
|
|
15
|
+
disconnected(reason: string): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Owns remote connection attachment, stale-attempt cleanup and degraded-monitor acceptance. */
|
|
19
|
+
export async function connectMachine(
|
|
20
|
+
runtimes: ReadonlyMap<string, MachineRuntime>,
|
|
21
|
+
name: string,
|
|
22
|
+
runtime: MachineRuntime,
|
|
23
|
+
ctx: ExtensionContext,
|
|
24
|
+
initial: boolean,
|
|
25
|
+
events: ConnectionEvents,
|
|
26
|
+
): Promise<void> {
|
|
27
|
+
if (
|
|
28
|
+
(!runtime.config && !runtime.coreMachine) ||
|
|
29
|
+
runtime.local ||
|
|
30
|
+
runtime.status === "connected" ||
|
|
31
|
+
runtime.attempting ||
|
|
32
|
+
!events.isCurrent()
|
|
33
|
+
) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
runtime.attempting = true;
|
|
37
|
+
runtime.status = "connecting";
|
|
38
|
+
delete runtime.reason;
|
|
39
|
+
events.refresh();
|
|
40
|
+
let attached = false;
|
|
41
|
+
let client: RemoteHerdrClient | CoreConnection | undefined;
|
|
42
|
+
try {
|
|
43
|
+
if (runtime.coreMachine) {
|
|
44
|
+
client = new CoreConnection(
|
|
45
|
+
process.env["HC_TERMINAL_SOCKET"],
|
|
46
|
+
runtime.coreMachine,
|
|
47
|
+
);
|
|
48
|
+
} else {
|
|
49
|
+
client = await RemoteHerdrClient.connect(runtime.config!, (error) => {
|
|
50
|
+
if (runtime.client === client) events.disconnected(error.message);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (!events.isCurrent() || runtimes.get(name) !== runtime) {
|
|
54
|
+
client.close();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
await client.request("ping");
|
|
58
|
+
if (!events.isCurrent() || runtimes.get(name) !== runtime) {
|
|
59
|
+
client.close();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const pending = runtime.monitor?.list() ?? runtime.pending;
|
|
63
|
+
const monitor = events.createMonitor(client);
|
|
64
|
+
runtime.client = client;
|
|
65
|
+
runtime.monitor = monitor;
|
|
66
|
+
attached = true;
|
|
67
|
+
await monitor.activate(ctx, pending);
|
|
68
|
+
if (
|
|
69
|
+
!events.isCurrent() ||
|
|
70
|
+
runtimes.get(name) !== runtime ||
|
|
71
|
+
runtime.client !== client ||
|
|
72
|
+
runtime.monitor !== monitor
|
|
73
|
+
) {
|
|
74
|
+
monitor.deactivate();
|
|
75
|
+
client.close();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
runtime.pending = [];
|
|
79
|
+
runtime.status = "connected";
|
|
80
|
+
delete runtime.attempting;
|
|
81
|
+
// Late connections must see optional sharing owners that finished startup.
|
|
82
|
+
if (events.needsContextRelay())
|
|
83
|
+
await events.prepareContextRelay(client).catch(() => {});
|
|
84
|
+
if (!events.isCurrent() || runtime.client !== client) return;
|
|
85
|
+
events.refresh();
|
|
86
|
+
if (!initial) ctx.ui.notify(`Connected to ${name}`, "info");
|
|
87
|
+
} catch (error) {
|
|
88
|
+
delete runtime.attempting;
|
|
89
|
+
if (
|
|
90
|
+
events.isCurrent() &&
|
|
91
|
+
runtimes.get(name) === runtime &&
|
|
92
|
+
runtime.client === client &&
|
|
93
|
+
runtime.monitor &&
|
|
94
|
+
runtime.monitoringIssue?.state === "degraded"
|
|
95
|
+
) {
|
|
96
|
+
runtime.pending = [];
|
|
97
|
+
runtime.status = "connected";
|
|
98
|
+
events.refresh();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
client?.close();
|
|
102
|
+
if (!events.isCurrent()) return;
|
|
103
|
+
if (
|
|
104
|
+
runtimes.get(name) !== runtime ||
|
|
105
|
+
(attached && runtime.client !== client)
|
|
106
|
+
) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
events.disconnected(errorMessage(error));
|
|
110
|
+
}
|
|
111
|
+
}
|