@howaboua/pi-shepherdr 0.2.23 → 0.2.24
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 +7 -18
- package/README.md +63 -60
- package/biome.json +1 -1
- package/index.ts +2 -2
- package/package.json +14 -9
- package/src/agents-discovery.ts +41 -26
- package/src/agents-focus.ts +2 -2
- package/src/agents-reply.ts +1 -1
- package/src/agents-spawn.ts +2 -2
- package/src/agents-tool.ts +2 -2
- package/src/agents-work.ts +3 -3
- package/src/attachment-notes.ts +14 -2
- package/src/attachment-protocol.ts +21 -4
- package/src/attachment-routing.ts +1 -1
- package/src/attachment-target.ts +5 -2
- package/src/attachment.ts +2 -3
- package/src/board/host.ts +2 -2
- package/src/board/membership.ts +1 -1
- package/src/{control-panel-actions.ts → control-panel/actions.ts} +2 -2
- package/src/{control-panel.ts → control-panel/index.ts} +4 -4
- package/src/{control-panel-items.ts → control-panel/items.ts} +3 -3
- package/src/{control-panel-view.ts → control-panel/view.ts} +3 -3
- package/src/controller.ts +6 -3
- package/src/fleet/index.ts +384 -0
- package/src/fleet/inspection.ts +141 -0
- package/src/fleet/machine.ts +52 -0
- package/src/fleet/monitor.ts +71 -0
- package/src/fleet/persistence.ts +42 -0
- package/src/fleet/reconnect.ts +75 -0
- package/src/fleet/setup.ts +92 -0
- package/src/messages/attachments.ts +39 -0
- package/src/messages/attribution.ts +75 -0
- package/src/messages/event-details.ts +69 -0
- package/src/messages/events.ts +189 -0
- package/src/messages/focus.ts +38 -0
- package/src/messages/guidance.ts +83 -0
- package/src/messages/render.ts +89 -0
- package/src/messages/voice-announcements.ts +79 -0
- package/src/monitor.ts +2 -2
- package/src/peer-inbox.ts +2 -2
- package/src/remote/client.ts +127 -0
- package/src/remote/context-relay.ts +84 -0
- package/src/remote/deployment.ts +179 -0
- package/src/remote/focus-channels.ts +89 -0
- package/src/remote/transport.ts +369 -0
- package/src/settlement/collector.ts +122 -0
- package/src/settlement/delivery.ts +144 -0
- package/src/{settlement.ts → settlement/index.ts} +23 -233
- package/src/shared-context-host.ts +33 -11
- package/src/shared-context.ts +10 -4
- package/src/spawn-context.ts +20 -17
- package/src/voice-handoff.ts +1 -1
- package/src/worker-service.ts +2 -2
- package/src/fleet.ts +0 -702
- package/src/messages.ts +0 -637
- package/src/remote-client.ts +0 -761
package/src/fleet.ts
DELETED
|
@@ -1,702 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ExtensionAPI,
|
|
3
|
-
ExtensionContext,
|
|
4
|
-
} from "@earendil-works/pi-coding-agent";
|
|
5
|
-
import { getSnapshot } from "./herdr.js";
|
|
6
|
-
import { HerdrClient, type HerdrConnection } from "./herdr-client.js";
|
|
7
|
-
import {
|
|
8
|
-
LOCAL_MACHINE,
|
|
9
|
-
readMachineCatalog,
|
|
10
|
-
type SshMachine,
|
|
11
|
-
} from "./machine-catalog.js";
|
|
12
|
-
import { AgentMonitor } from "./monitor.js";
|
|
13
|
-
import { parseMonitoredAgent } from "./monitor-record.js";
|
|
14
|
-
import { RemoteHerdrClient } from "./remote-client.js";
|
|
15
|
-
import type {
|
|
16
|
-
MachineStatus,
|
|
17
|
-
MonitoredAgent,
|
|
18
|
-
MonitoringIssue,
|
|
19
|
-
ScopedMonitoredAgent,
|
|
20
|
-
SessionSnapshot,
|
|
21
|
-
} from "./types.js";
|
|
22
|
-
import { renderAgentWidget } from "./widget.js";
|
|
23
|
-
|
|
24
|
-
const MONITOR_STATE_TYPE = "herdr-agents-monitor-state";
|
|
25
|
-
|
|
26
|
-
interface Runtime {
|
|
27
|
-
attempting?: boolean;
|
|
28
|
-
client?: HerdrConnection;
|
|
29
|
-
config?: SshMachine;
|
|
30
|
-
contextRelayError?: string;
|
|
31
|
-
local: boolean;
|
|
32
|
-
monitor?: AgentMonitor;
|
|
33
|
-
monitoringIssue?: MonitoringIssue;
|
|
34
|
-
pending: unknown[];
|
|
35
|
-
reason?: string;
|
|
36
|
-
status: MachineStatus["status"];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface ConnectedMachine {
|
|
40
|
-
client: HerdrConnection;
|
|
41
|
-
fallbackCwd: string;
|
|
42
|
-
local: boolean;
|
|
43
|
-
machine: string;
|
|
44
|
-
monitor: AgentMonitor;
|
|
45
|
-
resolveDirectory?: (
|
|
46
|
-
value: string | undefined,
|
|
47
|
-
fallback: string,
|
|
48
|
-
) => Promise<string>;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface MachineSnapshot extends MachineStatus {
|
|
52
|
-
monitoredPaneIds?: ReadonlySet<string>;
|
|
53
|
-
snapshot?: SessionSnapshot;
|
|
54
|
-
snapshotError?: string;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function errorMessage(error: unknown): string {
|
|
58
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
59
|
-
return message.length <= 500 ? message : `${message.slice(0, 499)}…`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function shellQuote(value: string): string {
|
|
63
|
-
return `'${value.replaceAll("'", `'"'"'`)}'`;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function operatorPrefix(config: SshMachine): string {
|
|
67
|
-
return `ssh -T -o BatchMode=yes -- ${shellQuote(config.target)} herdr --session ${shellQuote(config.session)}`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function savedAgents(ctx: ExtensionContext): unknown[] {
|
|
71
|
-
let latest: unknown[] = [];
|
|
72
|
-
let version: unknown;
|
|
73
|
-
for (const entry of ctx.sessionManager.getBranch()) {
|
|
74
|
-
if (entry.type !== "custom" || entry.customType !== MONITOR_STATE_TYPE)
|
|
75
|
-
continue;
|
|
76
|
-
const data = entry.data as { version?: unknown; agents?: unknown };
|
|
77
|
-
if (Array.isArray(data?.agents)) {
|
|
78
|
-
latest = data.agents;
|
|
79
|
-
version = data.version;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (latest.length > 0 && version !== 2) {
|
|
83
|
-
ctx.ui.notify(
|
|
84
|
-
"Cleared legacy Shepherdr watches; use watch to subscribe explicitly. Remote agents are unchanged.",
|
|
85
|
-
"warning",
|
|
86
|
-
);
|
|
87
|
-
return [];
|
|
88
|
-
}
|
|
89
|
-
return latest;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function groupSaved(values: unknown[]): Map<string, unknown[]> {
|
|
93
|
-
const grouped = new Map<string, unknown[]>();
|
|
94
|
-
for (const value of values) {
|
|
95
|
-
const machine =
|
|
96
|
-
typeof value === "object" &&
|
|
97
|
-
value !== null &&
|
|
98
|
-
"machine" in value &&
|
|
99
|
-
typeof value.machine === "string"
|
|
100
|
-
? value.machine
|
|
101
|
-
: LOCAL_MACHINE;
|
|
102
|
-
const records = grouped.get(machine) ?? [];
|
|
103
|
-
records.push(value);
|
|
104
|
-
grouped.set(machine, records);
|
|
105
|
-
}
|
|
106
|
-
return grouped;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export class AgentFleet {
|
|
110
|
-
private readonly listeners = new Set<() => void>();
|
|
111
|
-
subscribe(listener: () => void): () => void {
|
|
112
|
-
this.listeners.add(listener);
|
|
113
|
-
return () => {
|
|
114
|
-
this.listeners.delete(listener);
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
private contextRelay:
|
|
118
|
-
| ((
|
|
119
|
-
ctx: ExtensionContext,
|
|
120
|
-
request: unknown,
|
|
121
|
-
signal: AbortSignal,
|
|
122
|
-
) => Promise<unknown>)
|
|
123
|
-
| undefined;
|
|
124
|
-
setContextRelay(
|
|
125
|
-
handler: (
|
|
126
|
-
ctx: ExtensionContext,
|
|
127
|
-
request: unknown,
|
|
128
|
-
signal: AbortSignal,
|
|
129
|
-
) => Promise<unknown>,
|
|
130
|
-
): void {
|
|
131
|
-
this.contextRelay = handler;
|
|
132
|
-
}
|
|
133
|
-
private context: ExtensionContext | undefined;
|
|
134
|
-
private generation = 0;
|
|
135
|
-
private readonly pi: ExtensionAPI;
|
|
136
|
-
private readonly runtimes = new Map<string, Runtime>();
|
|
137
|
-
|
|
138
|
-
constructor(pi: ExtensionAPI) {
|
|
139
|
-
this.pi = pi;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async activate(ctx: ExtensionContext): Promise<void> {
|
|
143
|
-
this.deactivate();
|
|
144
|
-
const generation = this.generation;
|
|
145
|
-
this.context = ctx;
|
|
146
|
-
const catalog = await readMachineCatalog();
|
|
147
|
-
if (!this.isCurrent(generation, ctx)) return;
|
|
148
|
-
const machines = Object.entries(catalog).filter(
|
|
149
|
-
([, machine]) => machine.enabled,
|
|
150
|
-
);
|
|
151
|
-
const grouped = groupSaved(savedAgents(ctx));
|
|
152
|
-
const known = new Set([LOCAL_MACHINE, ...machines.map(([id]) => id)]);
|
|
153
|
-
const orphaned = [...grouped.entries()]
|
|
154
|
-
.filter(([machine]) => !known.has(machine))
|
|
155
|
-
.reduce((count, [, records]) => count + records.length, 0);
|
|
156
|
-
if (orphaned > 0) {
|
|
157
|
-
ctx.ui.notify(
|
|
158
|
-
`Ignored ${orphaned} saved Shepherdr ${orphaned === 1 ? "agent" : "agents"} from removed machines`,
|
|
159
|
-
"warning",
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const localRuntime: Runtime = {
|
|
164
|
-
local: true,
|
|
165
|
-
pending: grouped.get(LOCAL_MACHINE) ?? [],
|
|
166
|
-
status: "connecting",
|
|
167
|
-
};
|
|
168
|
-
this.runtimes.set(LOCAL_MACHINE, localRuntime);
|
|
169
|
-
for (const [name, config] of machines) {
|
|
170
|
-
this.runtimes.set(name, {
|
|
171
|
-
config,
|
|
172
|
-
local: false,
|
|
173
|
-
pending: grouped.get(name) ?? [],
|
|
174
|
-
status: "connecting",
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const localClient = new HerdrClient();
|
|
179
|
-
await localClient.request("ping");
|
|
180
|
-
if (!this.isCurrent(generation, ctx)) return;
|
|
181
|
-
const localMonitor = this.createMonitor(
|
|
182
|
-
LOCAL_MACHINE,
|
|
183
|
-
localRuntime,
|
|
184
|
-
localClient,
|
|
185
|
-
);
|
|
186
|
-
localRuntime.client = localClient;
|
|
187
|
-
localRuntime.monitor = localMonitor;
|
|
188
|
-
await localMonitor.activate(ctx, localRuntime.pending);
|
|
189
|
-
if (
|
|
190
|
-
!this.isCurrent(generation, ctx) ||
|
|
191
|
-
this.runtimes.get(LOCAL_MACHINE) !== localRuntime ||
|
|
192
|
-
localRuntime.client !== localClient ||
|
|
193
|
-
localRuntime.monitor !== localMonitor
|
|
194
|
-
) {
|
|
195
|
-
localMonitor.deactivate();
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
localRuntime.pending = [];
|
|
199
|
-
localRuntime.status = "connected";
|
|
200
|
-
this.changed();
|
|
201
|
-
|
|
202
|
-
for (const [name] of machines) {
|
|
203
|
-
void this.connectMachine(name, generation, true);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
deactivate(): void {
|
|
208
|
-
this.generation += 1;
|
|
209
|
-
for (const runtime of this.runtimes.values()) {
|
|
210
|
-
runtime.monitor?.deactivate();
|
|
211
|
-
if (runtime.client instanceof RemoteHerdrClient) runtime.client.close();
|
|
212
|
-
}
|
|
213
|
-
this.runtimes.clear();
|
|
214
|
-
renderAgentWidget(this.context, [], []);
|
|
215
|
-
this.context = undefined;
|
|
216
|
-
for (const listener of this.listeners) listener();
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
isActive(): boolean {
|
|
220
|
-
return this.context !== undefined;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
async reload(): Promise<void> {
|
|
224
|
-
const ctx = this.context;
|
|
225
|
-
if (!ctx) return;
|
|
226
|
-
const generation = this.generation;
|
|
227
|
-
const catalog = await readMachineCatalog();
|
|
228
|
-
if (!this.isCurrent(generation, ctx)) return;
|
|
229
|
-
const reconnect: string[] = [];
|
|
230
|
-
|
|
231
|
-
for (const [name, runtime] of this.runtimes) {
|
|
232
|
-
if (runtime.local || catalog[name]?.enabled) continue;
|
|
233
|
-
runtime.monitor?.deactivate();
|
|
234
|
-
if (runtime.client instanceof RemoteHerdrClient) runtime.client.close();
|
|
235
|
-
this.runtimes.delete(name);
|
|
236
|
-
}
|
|
237
|
-
for (const [name, machineConfig] of Object.entries(catalog)) {
|
|
238
|
-
if (!machineConfig.enabled) continue;
|
|
239
|
-
const runtime = this.runtimes.get(name);
|
|
240
|
-
if (
|
|
241
|
-
runtime?.config &&
|
|
242
|
-
runtime.config.target === machineConfig.target &&
|
|
243
|
-
runtime.config.session === machineConfig.session
|
|
244
|
-
) {
|
|
245
|
-
runtime.config = machineConfig;
|
|
246
|
-
continue;
|
|
247
|
-
}
|
|
248
|
-
const pending = runtime?.monitor?.list() ?? runtime?.pending ?? [];
|
|
249
|
-
runtime?.monitor?.deactivate();
|
|
250
|
-
if (runtime?.client instanceof RemoteHerdrClient) runtime.client.close();
|
|
251
|
-
this.runtimes.set(name, {
|
|
252
|
-
config: machineConfig,
|
|
253
|
-
local: false,
|
|
254
|
-
pending,
|
|
255
|
-
status: "connecting",
|
|
256
|
-
});
|
|
257
|
-
reconnect.push(name);
|
|
258
|
-
}
|
|
259
|
-
this.changed();
|
|
260
|
-
for (const name of reconnect) {
|
|
261
|
-
void this.connectMachine(name, generation, true);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
statuses(): MachineStatus[] {
|
|
266
|
-
return [...this.runtimes.keys()].map((name) => this.statusFor(name));
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
list(): ScopedMonitoredAgent[] {
|
|
270
|
-
return [...this.runtimes.entries()].flatMap(([machine, runtime]) => {
|
|
271
|
-
const values = runtime.monitor?.list() ?? runtime.pending;
|
|
272
|
-
return values
|
|
273
|
-
.map((value) => parseMonitoredAgent(value))
|
|
274
|
-
.filter((value): value is MonitoredAgent => value !== undefined)
|
|
275
|
-
.map((agent) => ({ ...agent, machine }));
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
connected(machine?: string): ConnectedMachine {
|
|
280
|
-
const name = machine?.trim() || LOCAL_MACHINE;
|
|
281
|
-
const runtime = this.runtimes.get(name);
|
|
282
|
-
if (!runtime) {
|
|
283
|
-
throw new Error(
|
|
284
|
-
`unknown Herdr machine ${JSON.stringify(name)}; use list to see configured machines`,
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
if (runtime.status !== "connected" || !runtime.client || !runtime.monitor) {
|
|
288
|
-
throw new Error(
|
|
289
|
-
`Herdr machine ${JSON.stringify(name)} is ${runtime.status}${runtime.reason ? `: ${runtime.reason}` : ""}; open /herdr → Status to reconnect`,
|
|
290
|
-
);
|
|
291
|
-
}
|
|
292
|
-
const remote =
|
|
293
|
-
runtime.client instanceof RemoteHerdrClient ? runtime.client : undefined;
|
|
294
|
-
return {
|
|
295
|
-
machine: name,
|
|
296
|
-
local: runtime.local,
|
|
297
|
-
client: runtime.client,
|
|
298
|
-
monitor: runtime.monitor,
|
|
299
|
-
fallbackCwd: runtime.local ? (this.context?.cwd ?? ".") : "~",
|
|
300
|
-
...(remote
|
|
301
|
-
? {
|
|
302
|
-
resolveDirectory: (value: string | undefined, fallback: string) =>
|
|
303
|
-
remote.directory(value, fallback),
|
|
304
|
-
}
|
|
305
|
-
: {}),
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
private isCurrent(generation: number, ctx: ExtensionContext): boolean {
|
|
310
|
-
return generation === this.generation && ctx === this.context;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
async snapshots(machine?: string): Promise<MachineSnapshot[]> {
|
|
314
|
-
const selected = machine?.trim()
|
|
315
|
-
? [this.statusFor(machine.trim())]
|
|
316
|
-
: this.statuses();
|
|
317
|
-
return Promise.all(
|
|
318
|
-
selected.map(async (status) => {
|
|
319
|
-
const runtime = this.runtimes.get(status.id);
|
|
320
|
-
if (
|
|
321
|
-
status.status !== "connected" ||
|
|
322
|
-
!runtime?.client ||
|
|
323
|
-
!runtime.monitor
|
|
324
|
-
)
|
|
325
|
-
return status;
|
|
326
|
-
const client = runtime.client;
|
|
327
|
-
const monitor = runtime.monitor;
|
|
328
|
-
try {
|
|
329
|
-
const snapshot = await getSnapshot(client);
|
|
330
|
-
if (
|
|
331
|
-
runtime.status !== "connected" ||
|
|
332
|
-
runtime.client !== client ||
|
|
333
|
-
runtime.monitor !== monitor ||
|
|
334
|
-
this.runtimes.get(status.id) !== runtime
|
|
335
|
-
) {
|
|
336
|
-
return this.runtimes.has(status.id)
|
|
337
|
-
? this.statusFor(status.id)
|
|
338
|
-
: status;
|
|
339
|
-
}
|
|
340
|
-
return {
|
|
341
|
-
...status,
|
|
342
|
-
monitoredPaneIds: new Set(
|
|
343
|
-
monitor.list().map((record) => record.paneId),
|
|
344
|
-
),
|
|
345
|
-
snapshot,
|
|
346
|
-
};
|
|
347
|
-
} catch (error) {
|
|
348
|
-
if (
|
|
349
|
-
runtime.status !== "connected" ||
|
|
350
|
-
runtime.client !== client ||
|
|
351
|
-
runtime.monitor !== monitor ||
|
|
352
|
-
this.runtimes.get(status.id) !== runtime
|
|
353
|
-
) {
|
|
354
|
-
return this.runtimes.has(status.id)
|
|
355
|
-
? this.statusFor(status.id)
|
|
356
|
-
: status;
|
|
357
|
-
}
|
|
358
|
-
return { ...status, snapshotError: errorMessage(error) };
|
|
359
|
-
}
|
|
360
|
-
}),
|
|
361
|
-
);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
connect(machine?: string): string {
|
|
365
|
-
const target = machine?.trim();
|
|
366
|
-
if (target) {
|
|
367
|
-
const runtime = this.runtimes.get(target);
|
|
368
|
-
if (!runtime)
|
|
369
|
-
throw new Error(`unknown Herdr machine ${JSON.stringify(target)}`);
|
|
370
|
-
if (
|
|
371
|
-
runtime.status === "connected" &&
|
|
372
|
-
(runtime.monitoringIssue || runtime.contextRelayError)
|
|
373
|
-
) {
|
|
374
|
-
if (runtime.attempting)
|
|
375
|
-
return `Already retrying connection setup on ${target}`;
|
|
376
|
-
void this.retrySetup(target, runtime);
|
|
377
|
-
return `Retrying connection setup on ${target}`;
|
|
378
|
-
}
|
|
379
|
-
if (runtime.local) {
|
|
380
|
-
return `${target} is the local machine and is already connected`;
|
|
381
|
-
}
|
|
382
|
-
if (runtime.status === "connected") {
|
|
383
|
-
return `${target} is already connected`;
|
|
384
|
-
}
|
|
385
|
-
if (runtime.attempting || runtime.status === "connecting") {
|
|
386
|
-
return `${target} is already connecting`;
|
|
387
|
-
}
|
|
388
|
-
void this.connectMachine(target, this.generation, false);
|
|
389
|
-
return `Connecting to ${target}`;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
const retries = [...this.runtimes.entries()].filter(
|
|
393
|
-
([, runtime]) =>
|
|
394
|
-
runtime.status === "connected" &&
|
|
395
|
-
(runtime.monitoringIssue || runtime.contextRelayError) &&
|
|
396
|
-
!runtime.attempting,
|
|
397
|
-
);
|
|
398
|
-
for (const [name, runtime] of retries) void this.retrySetup(name, runtime);
|
|
399
|
-
const names = [...this.runtimes.entries()]
|
|
400
|
-
.filter(
|
|
401
|
-
([, runtime]) =>
|
|
402
|
-
!runtime.local &&
|
|
403
|
-
runtime.status !== "connected" &&
|
|
404
|
-
!runtime.attempting,
|
|
405
|
-
)
|
|
406
|
-
.map(([name]) => name);
|
|
407
|
-
for (const name of names) {
|
|
408
|
-
void this.connectMachine(name, this.generation, false);
|
|
409
|
-
}
|
|
410
|
-
if (names.length > 0 || retries.length > 0) {
|
|
411
|
-
return [
|
|
412
|
-
retries.length > 0
|
|
413
|
-
? `Retrying connection setup on ${retries.map(([name]) => name).join(", ")}`
|
|
414
|
-
: "",
|
|
415
|
-
names.length > 0 ? `Connecting to ${names.join(", ")}` : "",
|
|
416
|
-
]
|
|
417
|
-
.filter(Boolean)
|
|
418
|
-
.join(". ");
|
|
419
|
-
}
|
|
420
|
-
const remotes = [...this.runtimes.entries()].filter(
|
|
421
|
-
([, runtime]) => !runtime.local,
|
|
422
|
-
);
|
|
423
|
-
if (remotes.length === 0) {
|
|
424
|
-
return "No remote machines are configured";
|
|
425
|
-
}
|
|
426
|
-
const connecting = remotes
|
|
427
|
-
.filter(([, runtime]) => runtime.attempting)
|
|
428
|
-
.map(([name]) => name);
|
|
429
|
-
return connecting.length > 0
|
|
430
|
-
? `Already connecting to ${connecting.join(", ")}`
|
|
431
|
-
: "All configured machines are connected";
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
private statusFor(name: string): MachineStatus {
|
|
435
|
-
const runtime = this.runtimes.get(name);
|
|
436
|
-
if (!runtime)
|
|
437
|
-
throw new Error(`unknown Herdr machine ${JSON.stringify(name)}`);
|
|
438
|
-
return {
|
|
439
|
-
id: name,
|
|
440
|
-
local: runtime.local,
|
|
441
|
-
...(runtime.config
|
|
442
|
-
? {
|
|
443
|
-
label: runtime.config.label,
|
|
444
|
-
target: runtime.config.target,
|
|
445
|
-
session: runtime.config.session,
|
|
446
|
-
}
|
|
447
|
-
: {}),
|
|
448
|
-
status: runtime.status,
|
|
449
|
-
...(runtime.monitoringIssue
|
|
450
|
-
? { monitoringIssue: runtime.monitoringIssue }
|
|
451
|
-
: {}),
|
|
452
|
-
...(runtime.reason ? { reason: runtime.reason } : {}),
|
|
453
|
-
...(runtime.contextRelayError
|
|
454
|
-
? { contextRelayError: runtime.contextRelayError }
|
|
455
|
-
: {}),
|
|
456
|
-
...(runtime.attempting ? { attempting: true } : {}),
|
|
457
|
-
};
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
private createMonitor(
|
|
461
|
-
machine: string,
|
|
462
|
-
runtime: Runtime,
|
|
463
|
-
client: HerdrConnection,
|
|
464
|
-
): AgentMonitor {
|
|
465
|
-
return new AgentMonitor(this.pi, {
|
|
466
|
-
client,
|
|
467
|
-
machine,
|
|
468
|
-
machineLabel: () => runtime.config?.label ?? LOCAL_MACHINE,
|
|
469
|
-
onChange: () => this.changed(),
|
|
470
|
-
onRefresh: () => this.refresh(),
|
|
471
|
-
operatorPrefix: runtime.local ? "herdr" : operatorPrefix(runtime.config!),
|
|
472
|
-
onWarning: (issue) => {
|
|
473
|
-
if (this.runtimes.get(machine) !== runtime) return;
|
|
474
|
-
if (!runtime.local && issue.state === "unavailable") {
|
|
475
|
-
this.disconnected(machine, runtime, issue.message);
|
|
476
|
-
return;
|
|
477
|
-
}
|
|
478
|
-
runtime.monitoringIssue = {
|
|
479
|
-
...issue,
|
|
480
|
-
message: errorMessage(issue.message),
|
|
481
|
-
};
|
|
482
|
-
this.refresh();
|
|
483
|
-
this.context?.ui.notify(
|
|
484
|
-
`${machine}: ${issue.message}${runtime.local ? "" : "\nOpen /herdr → Status to reconnect"}`,
|
|
485
|
-
"warning",
|
|
486
|
-
);
|
|
487
|
-
},
|
|
488
|
-
onRecovered: () => {
|
|
489
|
-
if (this.runtimes.get(machine) !== runtime || !runtime.monitoringIssue)
|
|
490
|
-
return;
|
|
491
|
-
delete runtime.monitoringIssue;
|
|
492
|
-
this.refresh();
|
|
493
|
-
if (runtime.monitor?.list().length)
|
|
494
|
-
this.context?.ui.notify(
|
|
495
|
-
`Herdr monitoring restored: ${machine}`,
|
|
496
|
-
"info",
|
|
497
|
-
);
|
|
498
|
-
},
|
|
499
|
-
reconnect: runtime.local,
|
|
500
|
-
...(client instanceof RemoteHerdrClient ? { reader: client } : {}),
|
|
501
|
-
...(runtime.local && process.env["HERDR_PANE_ID"]
|
|
502
|
-
? { selfPaneId: process.env["HERDR_PANE_ID"] }
|
|
503
|
-
: {}),
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
private async prepareContextRelay(
|
|
508
|
-
name: string,
|
|
509
|
-
runtime: Runtime,
|
|
510
|
-
client: RemoteHerdrClient,
|
|
511
|
-
ctx: ExtensionContext,
|
|
512
|
-
): Promise<void> {
|
|
513
|
-
if (!this.contextRelay) return;
|
|
514
|
-
try {
|
|
515
|
-
await client.startContextRelay(
|
|
516
|
-
ctx.sessionManager.getSessionId(),
|
|
517
|
-
name,
|
|
518
|
-
(request, signal) => this.contextRelay!(ctx, request, signal),
|
|
519
|
-
);
|
|
520
|
-
if (this.context !== ctx || this.runtimes.get(name) !== runtime) return;
|
|
521
|
-
delete runtime.contextRelayError;
|
|
522
|
-
} catch (error) {
|
|
523
|
-
if (this.context !== ctx || this.runtimes.get(name) !== runtime) return;
|
|
524
|
-
runtime.contextRelayError = errorMessage(error);
|
|
525
|
-
ctx.ui.notify(
|
|
526
|
-
`Context sharing unavailable on ${name}: ${runtime.contextRelayError}\nOpen /herdr → Status to retry sharing.`,
|
|
527
|
-
"warning",
|
|
528
|
-
);
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
private async retrySetup(name: string, runtime: Runtime): Promise<void> {
|
|
533
|
-
const monitor = runtime.monitor;
|
|
534
|
-
const ctx = this.context;
|
|
535
|
-
const generation = this.generation;
|
|
536
|
-
if (!monitor || !ctx || runtime.attempting) return;
|
|
537
|
-
runtime.attempting = true;
|
|
538
|
-
this.refresh();
|
|
539
|
-
try {
|
|
540
|
-
if (
|
|
541
|
-
runtime.contextRelayError &&
|
|
542
|
-
runtime.client instanceof RemoteHerdrClient
|
|
543
|
-
)
|
|
544
|
-
await this.prepareContextRelay(name, runtime, runtime.client, ctx);
|
|
545
|
-
if (
|
|
546
|
-
this.isCurrent(generation, ctx) &&
|
|
547
|
-
this.runtimes.get(name) === runtime &&
|
|
548
|
-
runtime.monitoringIssue
|
|
549
|
-
)
|
|
550
|
-
await monitor.retryMonitoring();
|
|
551
|
-
} catch (error) {
|
|
552
|
-
if (
|
|
553
|
-
this.isCurrent(generation, ctx) &&
|
|
554
|
-
this.runtimes.get(name) === runtime &&
|
|
555
|
-
runtime.monitor === monitor
|
|
556
|
-
) {
|
|
557
|
-
ctx.ui.notify(
|
|
558
|
-
`Monitoring retry failed on ${name}: ${errorMessage(error)}`,
|
|
559
|
-
"warning",
|
|
560
|
-
);
|
|
561
|
-
}
|
|
562
|
-
} finally {
|
|
563
|
-
if (this.runtimes.get(name) === runtime && runtime.monitor === monitor)
|
|
564
|
-
delete runtime.attempting;
|
|
565
|
-
this.refresh();
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
private async connectMachine(
|
|
570
|
-
name: string,
|
|
571
|
-
generation: number,
|
|
572
|
-
initial: boolean,
|
|
573
|
-
): Promise<void> {
|
|
574
|
-
const runtime = this.runtimes.get(name);
|
|
575
|
-
const ctx = this.context;
|
|
576
|
-
if (
|
|
577
|
-
!runtime?.config ||
|
|
578
|
-
runtime.local ||
|
|
579
|
-
runtime.status === "connected" ||
|
|
580
|
-
runtime.attempting ||
|
|
581
|
-
!ctx ||
|
|
582
|
-
generation !== this.generation
|
|
583
|
-
) {
|
|
584
|
-
return;
|
|
585
|
-
}
|
|
586
|
-
runtime.attempting = true;
|
|
587
|
-
runtime.status = "connecting";
|
|
588
|
-
delete runtime.reason;
|
|
589
|
-
this.refresh();
|
|
590
|
-
let attached = false;
|
|
591
|
-
let client: RemoteHerdrClient | undefined;
|
|
592
|
-
try {
|
|
593
|
-
client = await RemoteHerdrClient.connect(runtime.config, (error) => {
|
|
594
|
-
if (runtime.client === client)
|
|
595
|
-
this.disconnected(name, runtime, error.message);
|
|
596
|
-
});
|
|
597
|
-
await this.prepareContextRelay(name, runtime, client, ctx);
|
|
598
|
-
if (
|
|
599
|
-
!this.isCurrent(generation, ctx) ||
|
|
600
|
-
this.runtimes.get(name) !== runtime
|
|
601
|
-
) {
|
|
602
|
-
client.close();
|
|
603
|
-
return;
|
|
604
|
-
}
|
|
605
|
-
await client.request("ping");
|
|
606
|
-
if (
|
|
607
|
-
!this.isCurrent(generation, ctx) ||
|
|
608
|
-
this.runtimes.get(name) !== runtime
|
|
609
|
-
) {
|
|
610
|
-
client.close();
|
|
611
|
-
return;
|
|
612
|
-
}
|
|
613
|
-
const pending = runtime.monitor?.list() ?? runtime.pending;
|
|
614
|
-
const monitor = this.createMonitor(name, runtime, client);
|
|
615
|
-
runtime.client = client;
|
|
616
|
-
runtime.monitor = monitor;
|
|
617
|
-
attached = true;
|
|
618
|
-
await monitor.activate(ctx, pending);
|
|
619
|
-
if (
|
|
620
|
-
!this.isCurrent(generation, ctx) ||
|
|
621
|
-
this.runtimes.get(name) !== runtime ||
|
|
622
|
-
runtime.client !== client ||
|
|
623
|
-
runtime.monitor !== monitor
|
|
624
|
-
) {
|
|
625
|
-
monitor.deactivate();
|
|
626
|
-
client.close();
|
|
627
|
-
return;
|
|
628
|
-
}
|
|
629
|
-
runtime.pending = [];
|
|
630
|
-
runtime.status = "connected";
|
|
631
|
-
delete runtime.attempting;
|
|
632
|
-
this.refresh();
|
|
633
|
-
if (!initial) ctx.ui.notify(`Connected to ${name}`, "info");
|
|
634
|
-
} catch (error) {
|
|
635
|
-
delete runtime.attempting;
|
|
636
|
-
if (
|
|
637
|
-
this.isCurrent(generation, ctx) &&
|
|
638
|
-
this.runtimes.get(name) === runtime &&
|
|
639
|
-
runtime.client === client &&
|
|
640
|
-
runtime.monitor &&
|
|
641
|
-
runtime.monitoringIssue?.state === "degraded"
|
|
642
|
-
) {
|
|
643
|
-
runtime.pending = [];
|
|
644
|
-
runtime.status = "connected";
|
|
645
|
-
this.refresh();
|
|
646
|
-
return;
|
|
647
|
-
}
|
|
648
|
-
client?.close();
|
|
649
|
-
if (generation !== this.generation || ctx !== this.context) return;
|
|
650
|
-
if (
|
|
651
|
-
this.runtimes.get(name) !== runtime ||
|
|
652
|
-
(attached && runtime.client !== client)
|
|
653
|
-
) {
|
|
654
|
-
return;
|
|
655
|
-
}
|
|
656
|
-
this.disconnected(name, runtime, errorMessage(error));
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
private disconnected(name: string, runtime: Runtime, reason: string): void {
|
|
661
|
-
reason = errorMessage(reason);
|
|
662
|
-
if (runtime.status === "unavailable" && runtime.reason === reason) return;
|
|
663
|
-
// Keep the stopped owner until reconnect so in-flight dispatch cleanup
|
|
664
|
-
// updates the state we persist, rather than a detached snapshot.
|
|
665
|
-
runtime.monitor?.deactivate();
|
|
666
|
-
if (runtime.client instanceof RemoteHerdrClient) runtime.client.close();
|
|
667
|
-
delete runtime.client;
|
|
668
|
-
delete runtime.contextRelayError;
|
|
669
|
-
delete runtime.monitoringIssue;
|
|
670
|
-
runtime.status = "unavailable";
|
|
671
|
-
delete runtime.attempting;
|
|
672
|
-
runtime.reason = reason;
|
|
673
|
-
this.changed();
|
|
674
|
-
this.context?.ui.notify(
|
|
675
|
-
`Shepherdr unavailable on ${name}: ${reason}\nOpen /herdr → Status to reconnect`,
|
|
676
|
-
"warning",
|
|
677
|
-
);
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
private changed(): void {
|
|
681
|
-
if (!this.context) return;
|
|
682
|
-
const agents = this.persistedAgents();
|
|
683
|
-
this.pi.appendEntry(MONITOR_STATE_TYPE, { version: 2, agents });
|
|
684
|
-
this.refresh();
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
private refresh(): void {
|
|
688
|
-
if (!this.context) return;
|
|
689
|
-
renderAgentWidget(this.context, this.list(), this.statuses());
|
|
690
|
-
for (const listener of this.listeners) listener();
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
private persistedAgents(): Array<MonitoredAgent & { machine: string }> {
|
|
694
|
-
return [...this.runtimes.entries()].flatMap(([machine, runtime]) => {
|
|
695
|
-
const values = runtime.monitor?.list() ?? runtime.pending;
|
|
696
|
-
return values
|
|
697
|
-
.map((value) => parseMonitoredAgent(value))
|
|
698
|
-
.filter((value): value is MonitoredAgent => value !== undefined)
|
|
699
|
-
.map((value) => ({ ...value, machine }));
|
|
700
|
-
});
|
|
701
|
-
}
|
|
702
|
-
}
|