@howaboua/pi-shepherdr 0.2.24 → 0.2.25
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 +13 -0
- package/README.md +17 -9
- package/index.ts +2 -0
- package/package.json +5 -5
- package/src/agents-contract.ts +9 -6
- package/src/agents-discovery.ts +41 -15
- package/src/agents-focus.ts +15 -9
- package/src/agents-spawn.ts +22 -6
- package/src/agents-tool.ts +24 -6
- package/src/agents-work.ts +2 -2
- package/src/attachment.ts +2 -0
- package/src/board/host.ts +3 -1
- package/src/close-idle.ts +89 -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 +151 -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/monitor-record.ts +9 -6
- 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-context.mjs +1 -1
- 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 -3
- package/src/shared-context.ts +34 -2
- package/src/spawn-context.ts +4 -0
- package/src/spawn-ownership.ts +91 -0
- package/src/types.ts +9 -5
- package/src/worker-ownership.ts +92 -0
- package/src/worker-service.ts +12 -81
|
@@ -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
|
+
}
|
package/src/fleet/index.ts
CHANGED
|
@@ -2,6 +2,9 @@ import type {
|
|
|
2
2
|
ExtensionAPI,
|
|
3
3
|
ExtensionContext,
|
|
4
4
|
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { CoreConnection } from "../core/backend.js";
|
|
6
|
+
import { CoreClient } from "../core/client.js";
|
|
7
|
+
import { type CoreMachine, coreMachines } from "../core/remote-exec.js";
|
|
5
8
|
import { HerdrClient, type HerdrConnection } from "../herdr-client.js";
|
|
6
9
|
import { LOCAL_MACHINE, readMachineCatalog } from "../machine-catalog.js";
|
|
7
10
|
import { AgentMonitor } from "../monitor.js";
|
|
@@ -12,6 +15,8 @@ import type {
|
|
|
12
15
|
ScopedMonitoredAgent,
|
|
13
16
|
} from "../types.js";
|
|
14
17
|
import { renderAgentWidget } from "../widget.js";
|
|
18
|
+
import { reloadCoreCatalog, reloadSshCatalog } from "./catalog.js";
|
|
19
|
+
import { connectMachine } from "./connection.js";
|
|
15
20
|
import {
|
|
16
21
|
connectedMachine,
|
|
17
22
|
listAgents,
|
|
@@ -40,19 +45,43 @@ export class AgentFleet {
|
|
|
40
45
|
};
|
|
41
46
|
}
|
|
42
47
|
private readonly setup = new FleetSetup(() => this.refresh());
|
|
48
|
+
private needsContextRelay: (
|
|
49
|
+
ctx: ExtensionContext,
|
|
50
|
+
machine: string,
|
|
51
|
+
) => boolean = () => false;
|
|
43
52
|
setContextRelay(
|
|
44
53
|
handler: (
|
|
45
54
|
ctx: ExtensionContext,
|
|
46
55
|
request: unknown,
|
|
47
56
|
signal: AbortSignal,
|
|
48
57
|
) => Promise<unknown>,
|
|
58
|
+
needsRelay: (ctx: ExtensionContext, machine: string) => boolean,
|
|
49
59
|
): void {
|
|
50
60
|
this.setup.setContextRelay(handler);
|
|
61
|
+
this.needsContextRelay = needsRelay;
|
|
62
|
+
}
|
|
63
|
+
async restoreSharing(ctx: ExtensionContext): Promise<void> {
|
|
64
|
+
if (this.context !== ctx) return;
|
|
65
|
+
await Promise.all(
|
|
66
|
+
this.statuses()
|
|
67
|
+
.filter(
|
|
68
|
+
(machine) =>
|
|
69
|
+
!machine.local &&
|
|
70
|
+
machine.status === "connected" &&
|
|
71
|
+
this.needsContextRelay(ctx, machine.id),
|
|
72
|
+
)
|
|
73
|
+
.map((machine) =>
|
|
74
|
+
this.connected(machine.id)
|
|
75
|
+
.prepareSharing()
|
|
76
|
+
.catch(() => {}),
|
|
77
|
+
),
|
|
78
|
+
);
|
|
51
79
|
}
|
|
52
80
|
private context: ExtensionContext | undefined;
|
|
53
81
|
private generation = 0;
|
|
54
82
|
private readonly pi: ExtensionAPI;
|
|
55
83
|
private readonly runtimes = new Map<string, MachineRuntime>();
|
|
84
|
+
private localCoreId: string | undefined;
|
|
56
85
|
|
|
57
86
|
constructor(pi: ExtensionAPI) {
|
|
58
87
|
this.pi = pi;
|
|
@@ -62,13 +91,32 @@ export class AgentFleet {
|
|
|
62
91
|
this.deactivate();
|
|
63
92
|
const generation = this.generation;
|
|
64
93
|
this.context = ctx;
|
|
65
|
-
const catalog =
|
|
94
|
+
const catalog = process.env["HC_TERMINAL_SOCKET"]
|
|
95
|
+
? {}
|
|
96
|
+
: await readMachineCatalog();
|
|
66
97
|
if (!this.isCurrent(generation, ctx)) return;
|
|
67
98
|
const machines = Object.entries(catalog).filter(
|
|
68
99
|
([, machine]) => machine.enabled,
|
|
69
100
|
);
|
|
70
|
-
|
|
71
|
-
|
|
101
|
+
if (process.env["HC_TERMINAL_SOCKET"]) {
|
|
102
|
+
const identityClient = new CoreConnection();
|
|
103
|
+
let coreId: string;
|
|
104
|
+
try {
|
|
105
|
+
coreId = (await identityClient.machineIdentity()).session;
|
|
106
|
+
} finally {
|
|
107
|
+
identityClient.close();
|
|
108
|
+
}
|
|
109
|
+
if (!this.isCurrent(generation, ctx)) return;
|
|
110
|
+
this.localCoreId = coreId;
|
|
111
|
+
}
|
|
112
|
+
const grouped = groupSaved(savedAgents(ctx, this.localCoreId));
|
|
113
|
+
const coreCatalog = await this.coreCatalog();
|
|
114
|
+
if (!this.isCurrent(generation, ctx)) return;
|
|
115
|
+
const known = new Set([
|
|
116
|
+
LOCAL_MACHINE,
|
|
117
|
+
...machines.map(([id]) => id),
|
|
118
|
+
...coreCatalog.map((machine) => machine.id),
|
|
119
|
+
]);
|
|
72
120
|
const orphaned = [...grouped.entries()]
|
|
73
121
|
.filter(([machine]) => !known.has(machine))
|
|
74
122
|
.reduce((count, [, records]) => count + records.length, 0);
|
|
@@ -93,10 +141,22 @@ export class AgentFleet {
|
|
|
93
141
|
status: "connecting",
|
|
94
142
|
});
|
|
95
143
|
}
|
|
144
|
+
for (const machine of coreCatalog)
|
|
145
|
+
this.runtimes.set(machine.id, {
|
|
146
|
+
coreMachine: machine,
|
|
147
|
+
local: false,
|
|
148
|
+
pending: grouped.get(machine.id) ?? [],
|
|
149
|
+
status: "connecting",
|
|
150
|
+
});
|
|
96
151
|
|
|
97
|
-
const localClient =
|
|
152
|
+
const localClient = process.env["HC_TERMINAL_SOCKET"]
|
|
153
|
+
? new CoreConnection()
|
|
154
|
+
: new HerdrClient();
|
|
98
155
|
await localClient.request("ping");
|
|
99
|
-
if (!this.isCurrent(generation, ctx))
|
|
156
|
+
if (!this.isCurrent(generation, ctx)) {
|
|
157
|
+
if (localClient instanceof CoreConnection) localClient.close();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
100
160
|
const localMonitor = this.createMonitor(
|
|
101
161
|
LOCAL_MACHINE,
|
|
102
162
|
localRuntime,
|
|
@@ -121,13 +181,20 @@ export class AgentFleet {
|
|
|
121
181
|
for (const [name] of machines) {
|
|
122
182
|
void this.connectMachine(name, generation, true);
|
|
123
183
|
}
|
|
184
|
+
for (const machine of coreCatalog)
|
|
185
|
+
void this.connectMachine(machine.id, generation, true);
|
|
124
186
|
}
|
|
125
187
|
|
|
126
188
|
deactivate(): void {
|
|
127
189
|
this.generation += 1;
|
|
190
|
+
this.localCoreId = undefined;
|
|
128
191
|
for (const runtime of this.runtimes.values()) {
|
|
129
192
|
runtime.monitor?.deactivate();
|
|
130
|
-
if (
|
|
193
|
+
if (
|
|
194
|
+
runtime.client instanceof RemoteHerdrClient ||
|
|
195
|
+
runtime.client instanceof CoreConnection
|
|
196
|
+
)
|
|
197
|
+
runtime.client.close();
|
|
131
198
|
}
|
|
132
199
|
this.runtimes.clear();
|
|
133
200
|
renderAgentWidget(this.context, [], []);
|
|
@@ -143,38 +210,20 @@ export class AgentFleet {
|
|
|
143
210
|
const ctx = this.context;
|
|
144
211
|
if (!ctx) return;
|
|
145
212
|
const generation = this.generation;
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (runtime.local || catalog[name]?.enabled) continue;
|
|
152
|
-
runtime.monitor?.deactivate();
|
|
153
|
-
if (runtime.client instanceof RemoteHerdrClient) runtime.client.close();
|
|
154
|
-
this.runtimes.delete(name);
|
|
155
|
-
}
|
|
156
|
-
for (const [name, machineConfig] of Object.entries(catalog)) {
|
|
157
|
-
if (!machineConfig.enabled) continue;
|
|
158
|
-
const runtime = this.runtimes.get(name);
|
|
159
|
-
if (
|
|
160
|
-
runtime?.config &&
|
|
161
|
-
runtime.config.target === machineConfig.target &&
|
|
162
|
-
runtime.config.session === machineConfig.session
|
|
163
|
-
) {
|
|
164
|
-
runtime.config = machineConfig;
|
|
165
|
-
continue;
|
|
166
|
-
}
|
|
167
|
-
const pending = runtime?.monitor?.list() ?? runtime?.pending ?? [];
|
|
168
|
-
runtime?.monitor?.deactivate();
|
|
169
|
-
if (runtime?.client instanceof RemoteHerdrClient) runtime.client.close();
|
|
170
|
-
this.runtimes.set(name, {
|
|
171
|
-
config: machineConfig,
|
|
172
|
-
local: false,
|
|
173
|
-
pending,
|
|
174
|
-
status: "connecting",
|
|
213
|
+
if (process.env["HC_TERMINAL_SOCKET"]) {
|
|
214
|
+
const catalog = await this.coreCatalog();
|
|
215
|
+
if (!this.isCurrent(generation, ctx)) return;
|
|
216
|
+
reloadCoreCatalog(this.runtimes, catalog, (id) => {
|
|
217
|
+
void this.connectMachine(id, generation, true);
|
|
175
218
|
});
|
|
176
|
-
|
|
219
|
+
this.changed();
|
|
220
|
+
return;
|
|
177
221
|
}
|
|
222
|
+
const catalog = process.env["HC_TERMINAL_SOCKET"]
|
|
223
|
+
? {}
|
|
224
|
+
: await readMachineCatalog();
|
|
225
|
+
if (!this.isCurrent(generation, ctx)) return;
|
|
226
|
+
const reconnect = reloadSshCatalog(this.runtimes, catalog);
|
|
178
227
|
this.changed();
|
|
179
228
|
for (const name of reconnect) {
|
|
180
229
|
void this.connectMachine(name, generation, true);
|
|
@@ -191,7 +240,41 @@ export class AgentFleet {
|
|
|
191
240
|
return listAgents(this.runtimes);
|
|
192
241
|
}
|
|
193
242
|
connected(machine?: string): ConnectedMachine {
|
|
194
|
-
|
|
243
|
+
const ctx = this.context;
|
|
244
|
+
return connectedMachine(
|
|
245
|
+
this.runtimes,
|
|
246
|
+
ctx,
|
|
247
|
+
async (name, runtime, client) => {
|
|
248
|
+
if (
|
|
249
|
+
!ctx ||
|
|
250
|
+
this.context !== ctx ||
|
|
251
|
+
this.runtimes.get(name) !== runtime ||
|
|
252
|
+
runtime.client !== client
|
|
253
|
+
)
|
|
254
|
+
throw new Error(
|
|
255
|
+
"Session changed; retry sharing from the current session",
|
|
256
|
+
);
|
|
257
|
+
if (runtime.local) return;
|
|
258
|
+
if (
|
|
259
|
+
!(
|
|
260
|
+
client instanceof RemoteHerdrClient ||
|
|
261
|
+
client instanceof CoreConnection
|
|
262
|
+
)
|
|
263
|
+
)
|
|
264
|
+
throw new Error("Context sharing is unavailable on this machine");
|
|
265
|
+
await this.prepareContextRelay(name, runtime, client, ctx);
|
|
266
|
+
if (
|
|
267
|
+
this.context !== ctx ||
|
|
268
|
+
this.runtimes.get(name) !== runtime ||
|
|
269
|
+
runtime.client !== client
|
|
270
|
+
)
|
|
271
|
+
throw new Error(
|
|
272
|
+
"Session changed; retry sharing from the current session",
|
|
273
|
+
);
|
|
274
|
+
client.contextRelayPath();
|
|
275
|
+
},
|
|
276
|
+
machine,
|
|
277
|
+
);
|
|
195
278
|
}
|
|
196
279
|
private isCurrent(generation: number, ctx: ExtensionContext): boolean {
|
|
197
280
|
return generation === this.generation && ctx === this.context;
|
|
@@ -224,7 +307,7 @@ export class AgentFleet {
|
|
|
224
307
|
private async prepareContextRelay(
|
|
225
308
|
name: string,
|
|
226
309
|
runtime: MachineRuntime,
|
|
227
|
-
client: RemoteHerdrClient,
|
|
310
|
+
client: RemoteHerdrClient | CoreConnection,
|
|
228
311
|
ctx: ExtensionContext,
|
|
229
312
|
): Promise<void> {
|
|
230
313
|
return this.setup.prepareContextRelay(
|
|
@@ -257,88 +340,16 @@ export class AgentFleet {
|
|
|
257
340
|
): Promise<void> {
|
|
258
341
|
const runtime = this.runtimes.get(name);
|
|
259
342
|
const ctx = this.context;
|
|
260
|
-
if (
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
}
|
|
270
|
-
runtime.attempting = true;
|
|
271
|
-
runtime.status = "connecting";
|
|
272
|
-
delete runtime.reason;
|
|
273
|
-
this.refresh();
|
|
274
|
-
let attached = false;
|
|
275
|
-
let client: RemoteHerdrClient | undefined;
|
|
276
|
-
try {
|
|
277
|
-
client = await RemoteHerdrClient.connect(runtime.config, (error) => {
|
|
278
|
-
if (runtime.client === client)
|
|
279
|
-
this.disconnected(name, runtime, error.message);
|
|
280
|
-
});
|
|
281
|
-
await this.prepareContextRelay(name, runtime, client, ctx);
|
|
282
|
-
if (
|
|
283
|
-
!this.isCurrent(generation, ctx) ||
|
|
284
|
-
this.runtimes.get(name) !== runtime
|
|
285
|
-
) {
|
|
286
|
-
client.close();
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
await client.request("ping");
|
|
290
|
-
if (
|
|
291
|
-
!this.isCurrent(generation, ctx) ||
|
|
292
|
-
this.runtimes.get(name) !== runtime
|
|
293
|
-
) {
|
|
294
|
-
client.close();
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
const pending = runtime.monitor?.list() ?? runtime.pending;
|
|
298
|
-
const monitor = this.createMonitor(name, runtime, client);
|
|
299
|
-
runtime.client = client;
|
|
300
|
-
runtime.monitor = monitor;
|
|
301
|
-
attached = true;
|
|
302
|
-
await monitor.activate(ctx, pending);
|
|
303
|
-
if (
|
|
304
|
-
!this.isCurrent(generation, ctx) ||
|
|
305
|
-
this.runtimes.get(name) !== runtime ||
|
|
306
|
-
runtime.client !== client ||
|
|
307
|
-
runtime.monitor !== monitor
|
|
308
|
-
) {
|
|
309
|
-
monitor.deactivate();
|
|
310
|
-
client.close();
|
|
311
|
-
return;
|
|
312
|
-
}
|
|
313
|
-
runtime.pending = [];
|
|
314
|
-
runtime.status = "connected";
|
|
315
|
-
delete runtime.attempting;
|
|
316
|
-
this.refresh();
|
|
317
|
-
if (!initial) ctx.ui.notify(`Connected to ${name}`, "info");
|
|
318
|
-
} catch (error) {
|
|
319
|
-
delete runtime.attempting;
|
|
320
|
-
if (
|
|
321
|
-
this.isCurrent(generation, ctx) &&
|
|
322
|
-
this.runtimes.get(name) === runtime &&
|
|
323
|
-
runtime.client === client &&
|
|
324
|
-
runtime.monitor &&
|
|
325
|
-
runtime.monitoringIssue?.state === "degraded"
|
|
326
|
-
) {
|
|
327
|
-
runtime.pending = [];
|
|
328
|
-
runtime.status = "connected";
|
|
329
|
-
this.refresh();
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
client?.close();
|
|
333
|
-
if (generation !== this.generation || ctx !== this.context) return;
|
|
334
|
-
if (
|
|
335
|
-
this.runtimes.get(name) !== runtime ||
|
|
336
|
-
(attached && runtime.client !== client)
|
|
337
|
-
) {
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
this.disconnected(name, runtime, errorMessage(error));
|
|
341
|
-
}
|
|
343
|
+
if (!runtime || !ctx) return;
|
|
344
|
+
return connectMachine(this.runtimes, name, runtime, ctx, initial, {
|
|
345
|
+
isCurrent: () => this.isCurrent(generation, ctx),
|
|
346
|
+
refresh: () => this.refresh(),
|
|
347
|
+
createMonitor: (client) => this.createMonitor(name, runtime, client),
|
|
348
|
+
needsContextRelay: () => this.needsContextRelay(ctx, name),
|
|
349
|
+
prepareContextRelay: (client) =>
|
|
350
|
+
this.prepareContextRelay(name, runtime, client, ctx),
|
|
351
|
+
disconnected: (reason) => this.disconnected(name, runtime, reason),
|
|
352
|
+
});
|
|
342
353
|
}
|
|
343
354
|
|
|
344
355
|
private disconnected(
|
|
@@ -351,7 +362,11 @@ export class AgentFleet {
|
|
|
351
362
|
// Keep the stopped owner until reconnect so in-flight dispatch cleanup
|
|
352
363
|
// updates the state we persist, rather than a detached snapshot.
|
|
353
364
|
runtime.monitor?.deactivate();
|
|
354
|
-
if (
|
|
365
|
+
if (
|
|
366
|
+
runtime.client instanceof RemoteHerdrClient ||
|
|
367
|
+
runtime.client instanceof CoreConnection
|
|
368
|
+
)
|
|
369
|
+
runtime.client.close();
|
|
355
370
|
delete runtime.client;
|
|
356
371
|
delete runtime.contextRelayError;
|
|
357
372
|
delete runtime.monitoringIssue;
|
|
@@ -368,9 +383,23 @@ export class AgentFleet {
|
|
|
368
383
|
private changed(): void {
|
|
369
384
|
if (!this.context) return;
|
|
370
385
|
const agents = this.persistedAgents();
|
|
371
|
-
this.pi.appendEntry(MONITOR_STATE_TYPE, {
|
|
386
|
+
this.pi.appendEntry(MONITOR_STATE_TYPE, {
|
|
387
|
+
version: 2,
|
|
388
|
+
agents,
|
|
389
|
+
...(this.localCoreId ? { localCoreId: this.localCoreId } : {}),
|
|
390
|
+
});
|
|
372
391
|
this.refresh();
|
|
373
392
|
}
|
|
393
|
+
private async coreCatalog(): Promise<CoreMachine[]> {
|
|
394
|
+
const socket = process.env["HC_TERMINAL_SOCKET"];
|
|
395
|
+
if (!socket) return [];
|
|
396
|
+
const client = new CoreClient(socket);
|
|
397
|
+
try {
|
|
398
|
+
return await coreMachines(client);
|
|
399
|
+
} finally {
|
|
400
|
+
client.close();
|
|
401
|
+
}
|
|
402
|
+
}
|
|
374
403
|
|
|
375
404
|
private refresh(): void {
|
|
376
405
|
if (!this.context) return;
|
package/src/fleet/inspection.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { CoreConnection } from "../core/backend.js";
|
|
2
3
|
import { getSnapshot } from "../herdr.js";
|
|
3
4
|
import { LOCAL_MACHINE } from "../machine-catalog.js";
|
|
4
5
|
import { parseMonitoredAgent } from "../monitor-record.js";
|
|
@@ -30,6 +31,11 @@ export function listAgents(
|
|
|
30
31
|
export function connectedMachine(
|
|
31
32
|
runtimes: ReadonlyMap<string, MachineRuntime>,
|
|
32
33
|
context: ExtensionContext | undefined,
|
|
34
|
+
prepareSharing: (
|
|
35
|
+
machine: string,
|
|
36
|
+
runtime: MachineRuntime,
|
|
37
|
+
client: NonNullable<MachineRuntime["client"]>,
|
|
38
|
+
) => Promise<void>,
|
|
33
39
|
machine?: string,
|
|
34
40
|
): ConnectedMachine {
|
|
35
41
|
const name = machine?.trim() || LOCAL_MACHINE;
|
|
@@ -45,8 +51,13 @@ export function connectedMachine(
|
|
|
45
51
|
);
|
|
46
52
|
}
|
|
47
53
|
const remote =
|
|
48
|
-
runtime.client instanceof RemoteHerdrClient
|
|
54
|
+
runtime.client instanceof RemoteHerdrClient ||
|
|
55
|
+
(runtime.client instanceof CoreConnection && runtime.client.remote)
|
|
56
|
+
? runtime.client
|
|
57
|
+
: undefined;
|
|
58
|
+
const client = runtime.client;
|
|
49
59
|
return {
|
|
60
|
+
prepareSharing: () => prepareSharing(name, runtime, client),
|
|
50
61
|
machine: name,
|
|
51
62
|
local: runtime.local,
|
|
52
63
|
client: runtime.client,
|
|
@@ -128,6 +139,13 @@ export function statusFor(
|
|
|
128
139
|
session: runtime.config.session,
|
|
129
140
|
}
|
|
130
141
|
: {}),
|
|
142
|
+
...(runtime.coreMachine
|
|
143
|
+
? {
|
|
144
|
+
label: runtime.coreMachine.name,
|
|
145
|
+
target: runtime.coreMachine.host,
|
|
146
|
+
session: runtime.coreMachine.id,
|
|
147
|
+
}
|
|
148
|
+
: {}),
|
|
131
149
|
status: runtime.status,
|
|
132
150
|
...(runtime.monitoringIssue
|
|
133
151
|
? { monitoringIssue: runtime.monitoringIssue }
|
package/src/fleet/machine.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CoreMachine } from "../core/remote-exec.js";
|
|
1
2
|
import type { HerdrConnection } from "../herdr-client.js";
|
|
2
3
|
import type { SshMachine } from "../machine-catalog.js";
|
|
3
4
|
import type { AgentMonitor } from "../monitor.js";
|
|
@@ -11,6 +12,7 @@ export interface MachineRuntime {
|
|
|
11
12
|
attempting?: boolean;
|
|
12
13
|
client?: HerdrConnection;
|
|
13
14
|
config?: SshMachine;
|
|
15
|
+
coreMachine?: CoreMachine;
|
|
14
16
|
contextRelayError?: string;
|
|
15
17
|
local: boolean;
|
|
16
18
|
monitor?: AgentMonitor;
|
|
@@ -21,6 +23,7 @@ export interface MachineRuntime {
|
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
export interface ConnectedMachine {
|
|
26
|
+
prepareSharing: () => Promise<void>;
|
|
24
27
|
client: HerdrConnection;
|
|
25
28
|
fallbackCwd: string;
|
|
26
29
|
local: boolean;
|
package/src/fleet/monitor.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
ExtensionAPI,
|
|
3
3
|
ExtensionContext,
|
|
4
4
|
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { CoreConnection } from "../core/backend.js";
|
|
5
6
|
import type { HerdrConnection } from "../herdr-client.js";
|
|
6
7
|
import { LOCAL_MACHINE } from "../machine-catalog.js";
|
|
7
8
|
import { AgentMonitor } from "../monitor.js";
|
|
@@ -28,13 +29,25 @@ export function createMachineMonitor(
|
|
|
28
29
|
pi: ExtensionAPI,
|
|
29
30
|
events: FleetMonitorEvents,
|
|
30
31
|
): AgentMonitor {
|
|
32
|
+
const selfPaneId = runtime.local
|
|
33
|
+
? process.env[
|
|
34
|
+
client instanceof CoreConnection ? "HC_TERMINAL_ID" : "HERDR_PANE_ID"
|
|
35
|
+
]
|
|
36
|
+
: undefined;
|
|
31
37
|
return new AgentMonitor(pi, {
|
|
32
38
|
client,
|
|
33
39
|
machine,
|
|
34
|
-
machineLabel: () =>
|
|
40
|
+
machineLabel: () =>
|
|
41
|
+
runtime.coreMachine?.name ?? runtime.config?.label ?? LOCAL_MACHINE,
|
|
35
42
|
onChange: () => events.changed(),
|
|
36
43
|
onRefresh: () => events.refresh(),
|
|
37
|
-
operatorPrefix: runtime.
|
|
44
|
+
operatorPrefix: runtime.coreMachine
|
|
45
|
+
? "hc"
|
|
46
|
+
: runtime.local
|
|
47
|
+
? client instanceof CoreConnection
|
|
48
|
+
? "hc"
|
|
49
|
+
: "herdr"
|
|
50
|
+
: operatorPrefix(runtime.config!),
|
|
38
51
|
onWarning: (issue) => {
|
|
39
52
|
if (!events.isAttached()) return;
|
|
40
53
|
if (!runtime.local && issue.state === "unavailable") {
|
|
@@ -64,8 +77,9 @@ export function createMachineMonitor(
|
|
|
64
77
|
},
|
|
65
78
|
reconnect: runtime.local,
|
|
66
79
|
...(client instanceof RemoteHerdrClient ? { reader: client } : {}),
|
|
67
|
-
...(
|
|
68
|
-
? {
|
|
80
|
+
...(client instanceof CoreConnection && client.reader
|
|
81
|
+
? { reader: client.reader }
|
|
69
82
|
: {}),
|
|
83
|
+
...(selfPaneId ? { selfPaneId } : {}),
|
|
70
84
|
});
|
|
71
85
|
}
|