@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.
Files changed (112) hide show
  1. package/README.md +48 -48
  2. package/index.ts +7 -1
  3. package/package.json +2 -9
  4. package/src/agent-identity.ts +3 -3
  5. package/src/agents-contract.ts +10 -14
  6. package/src/agents-discovery.ts +34 -54
  7. package/src/agents-focus.ts +23 -35
  8. package/src/agents-spawn.ts +24 -23
  9. package/src/agents-tool.ts +4 -19
  10. package/src/agents-work.ts +4 -3
  11. package/src/attachment-notes.ts +8 -7
  12. package/src/attachment-protocol.ts +3 -2
  13. package/src/attachment-routing.ts +1 -1
  14. package/src/attachment-target.ts +10 -18
  15. package/src/attachment.ts +14 -9
  16. package/src/board/discovery.ts +67 -0
  17. package/src/board/host.ts +93 -63
  18. package/src/board/membership.ts +16 -11
  19. package/src/board/records.ts +185 -0
  20. package/src/close-idle.ts +2 -9
  21. package/src/context-briefing.ts +230 -0
  22. package/src/context-routing.ts +1 -1
  23. package/src/control-panel/index.ts +2 -4
  24. package/src/control-panel/items.ts +1 -11
  25. package/src/control-panel/view.ts +3 -9
  26. package/src/controller.ts +50 -63
  27. package/src/core/AGENTS.md +7 -4
  28. package/src/core/admission-broker.mjs +235 -0
  29. package/src/core/admission-endpoint.ts +180 -0
  30. package/src/core/admission-owner.mjs +52 -0
  31. package/src/core/admission-startup.ts +130 -0
  32. package/src/core/admission.ts +181 -0
  33. package/src/core/agents.ts +102 -0
  34. package/src/core/backend.ts +110 -84
  35. package/src/core/client.ts +16 -0
  36. package/src/core/focus-channel-helper.mjs +6 -27
  37. package/src/core/focus-channel-wire.mjs +9 -6
  38. package/src/core/focus-channel.ts +5 -3
  39. package/src/core/focus-window.ts +15 -4
  40. package/src/core/helper-bootstrap.mjs +70 -0
  41. package/src/core/helper-context.mjs +98 -0
  42. package/src/core/helper-core.mjs +121 -0
  43. package/src/core/machines.ts +30 -0
  44. package/src/core/registration.ts +7 -2
  45. package/src/core/remote-endpoint.ts +80 -74
  46. package/src/core/remote-helper.ts +330 -0
  47. package/src/core/remote-reader.ts +3 -42
  48. package/src/core/remote-service.mjs +239 -0
  49. package/src/core/remote-transport.ts +290 -0
  50. package/src/core/spawn.ts +20 -20
  51. package/src/core/terminal-peer.mjs +57 -0
  52. package/src/core/terminal.ts +4 -2
  53. package/src/delivery.ts +39 -0
  54. package/src/fleet/catalog.ts +16 -58
  55. package/src/fleet/connection.ts +9 -18
  56. package/src/fleet/index.ts +38 -76
  57. package/src/fleet/inspection.ts +9 -22
  58. package/src/fleet/machine.ts +28 -12
  59. package/src/fleet/monitor.ts +8 -29
  60. package/src/fleet/persistence.ts +2 -2
  61. package/src/fleet/reconnect.ts +5 -3
  62. package/src/fleet/setup.ts +6 -16
  63. package/src/launch-contract.ts +0 -3
  64. package/src/launch-location.ts +4 -132
  65. package/src/launch.ts +18 -144
  66. package/src/messages/attachments.ts +1 -1
  67. package/src/messages/attribution.ts +9 -14
  68. package/src/messages/events.ts +5 -9
  69. package/src/messages/focus.ts +2 -7
  70. package/src/messages/guidance.ts +2 -43
  71. package/src/messages/peer-sharing.ts +133 -0
  72. package/src/monitor-events.ts +14 -43
  73. package/src/monitor-extension-messages.ts +5 -6
  74. package/src/monitor-record.ts +4 -0
  75. package/src/monitor-state.ts +1 -101
  76. package/src/monitor.ts +38 -66
  77. package/src/notes-discovery.ts +67 -0
  78. package/src/peer-inbox.ts +21 -27
  79. package/src/remote/context-relay.ts +25 -10
  80. package/src/remote/notes-reader.mjs +67 -0
  81. package/src/remote/shepherdr-channel.mjs +1 -1
  82. package/src/remote/shepherdr-checkpoints.mjs +49 -102
  83. package/src/remote/shepherdr-context.mjs +1 -1
  84. package/src/remote/shepherdr-peer.mjs +19 -24
  85. package/src/remote/shepherdr-session.mjs +18 -7
  86. package/src/saved-context.ts +62 -0
  87. package/src/settlement/collector.ts +29 -9
  88. package/src/settlement/index.ts +35 -6
  89. package/src/shared-context-host.ts +27 -33
  90. package/src/shared-context.ts +44 -15
  91. package/src/spawn-context.ts +11 -3
  92. package/src/spawn-ownership.ts +1 -1
  93. package/src/ssh-setup.ts +40 -73
  94. package/src/types.ts +4 -8
  95. package/src/voice-handoff.ts +1 -1
  96. package/src/widget.ts +7 -2
  97. package/src/worker-ownership.ts +2 -8
  98. package/src/worker-service.ts +6 -14
  99. package/src/core/context-relay.mjs +0 -111
  100. package/src/core/remote-exec.ts +0 -122
  101. package/src/core/remote-helper.mjs +0 -244
  102. package/src/core/remote-relay.ts +0 -191
  103. package/src/core/request.ts +0 -124
  104. package/src/herdr-client.ts +0 -364
  105. package/src/herdr.ts +0 -268
  106. package/src/machine-catalog.ts +0 -83
  107. package/src/monitor-event.ts +0 -84
  108. package/src/remote/client.ts +0 -127
  109. package/src/remote/deployment.ts +0 -179
  110. package/src/remote/focus-channels.ts +0 -89
  111. package/src/remote/shepherdr.mjs +0 -504
  112. package/src/remote/transport.ts +0 -369
@@ -1,6 +1,6 @@
1
1
  import type { AgentStatus, PaneInfo, SessionSnapshot } from "../types.js";
2
2
  import { object } from "./protocol.js";
3
- import { CORE_SESSION_LABEL } from "./registration.js";
3
+ import { CORE_SESSION_LABEL, CORE_WORKER_LABEL } from "./registration.js";
4
4
 
5
5
  export function terminalPane(value: unknown): PaneInfo {
6
6
  const terminal = object(value);
@@ -22,12 +22,14 @@ export function terminalPane(value: unknown): PaneInfo {
22
22
  const session = labels[CORE_SESSION_LABEL];
23
23
  const live = terminal["state"] === "live";
24
24
  return {
25
- backend: "core",
26
25
  // The shared legacy key is an address, not a fabricated app pane ID.
27
26
  pane_id: address,
28
27
  terminal_id: address,
29
28
  agent: typeof session === "string" && session ? "pi" : null,
30
29
  agent_status: agentStatus,
30
+ ...(session && labels[CORE_WORKER_LABEL] === session
31
+ ? { worker: true as const }
32
+ : {}),
31
33
  interactive_ready: live && typeof session === "string" && !!session,
32
34
  launch_pending: live && !session,
33
35
  ...(typeof terminal["cwd"] === "string" ? { cwd: terminal["cwd"] } : {}),
package/src/delivery.ts CHANGED
@@ -4,6 +4,7 @@ import type {
4
4
  ExtensionContext,
5
5
  } from "@earendil-works/pi-coding-agent";
6
6
  import type {
7
+ CodexContextBriefingHandler,
7
8
  CodexDeveloperCustomMessage,
8
9
  CodexDeveloperMessageOptions,
9
10
  CodexDeveloperMessageRequest,
@@ -11,6 +12,44 @@ import type {
11
12
 
12
13
  const contexts = new WeakMap<ExtensionAPI, ExtensionContext>();
13
14
  const PENDING_MESSAGE = "herdr-pending-message";
15
+
16
+ /** PCC's public broker also works when its package cannot resolve from this extension. */
17
+ export function registerInferenceBriefing(
18
+ pi: ExtensionAPI,
19
+ handler: CodexContextBriefingHandler,
20
+ ) {
21
+ const channel = "@howaboua/pi-codex-conversion/context-briefings/v1";
22
+ const unregister = pi.events.on(channel, (value) => {
23
+ if (
24
+ !value ||
25
+ typeof value !== "object" ||
26
+ !("protocol" in value) ||
27
+ value.protocol !== 1 ||
28
+ !("action" in value) ||
29
+ value.action !== "record"
30
+ )
31
+ return;
32
+ const request = value as {
33
+ protocol: 1;
34
+ action: "record";
35
+ ctx: ExtensionContext;
36
+ windowId: string | undefined;
37
+ messages?: Parameters<CodexContextBriefingHandler>[2];
38
+ pending: Promise<void>[];
39
+ };
40
+ request.pending.push(
41
+ handler(request.ctx, request.windowId, request.messages),
42
+ );
43
+ });
44
+ pi.on("session_shutdown", unregister);
45
+ return {
46
+ hosted() {
47
+ const request = { protocol: 1, action: "available", available: false };
48
+ pi.events.emit(channel, request);
49
+ return request.available === true;
50
+ },
51
+ };
52
+ }
14
53
  interface PendingMessage {
15
54
  sessionId: string;
16
55
  message: CodexDeveloperCustomMessage & {
@@ -1,39 +1,34 @@
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";
1
+ import type { CoreMachine } from "../core/machines.js";
5
2
  import type { MachineRuntime } from "./machine.js";
6
3
 
7
- /** Reconcile catalog changes without changing fleet identity or scheduling retries. */
4
+ /** Catalog names are metadata: renames must not replace stable connection owners. */
8
5
  export function reloadCoreCatalog(
9
6
  runtimes: Map<string, MachineRuntime>,
10
7
  catalog: CoreMachine[],
11
8
  connect: (id: string) => void,
12
9
  ): void {
10
+ const ids = new Set(catalog.map((machine) => machine.id));
13
11
  for (const [id, runtime] of runtimes) {
12
+ if (runtime.local || ids.has(id)) continue;
13
+ runtime.monitor?.deactivate();
14
+ runtime.client?.close();
15
+ runtimes.delete(id);
16
+ }
17
+ for (const machine of catalog) {
14
18
  if (
15
- runtime.local ||
16
- catalog.some(
17
- (machine) =>
18
- machine.id === id && machine.name === runtime.coreMachine?.name,
19
+ [...runtimes.values()].some(
20
+ (runtime) => runtime.local && runtime.coreId === machine.id,
19
21
  )
20
22
  )
21
23
  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
24
  let runtime = runtimes.get(machine.id);
34
- if (runtime) runtime.coreMachine = machine;
25
+ if (runtime?.coreMachine) {
26
+ // Live Core endpoints retain this object for address qualification.
27
+ Object.assign(runtime.coreMachine, machine);
28
+ } else if (runtime) runtime.coreMachine = machine;
35
29
  else {
36
30
  runtime = {
31
+ coreId: machine.id,
37
32
  coreMachine: machine,
38
33
  local: false,
39
34
  pending: [],
@@ -44,40 +39,3 @@ export function reloadCoreCatalog(
44
39
  if (!runtime.client) connect(machine.id);
45
40
  }
46
41
  }
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
- }
@@ -1,17 +1,14 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
  import { CoreConnection } from "../core/backend.js";
3
3
  import type { AgentMonitor } from "../monitor.js";
4
- import { RemoteHerdrClient } from "../remote/client.js";
5
4
  import { errorMessage, type MachineRuntime } from "./machine.js";
6
5
 
7
6
  interface ConnectionEvents {
8
7
  isCurrent(): boolean;
9
8
  refresh(): void;
10
- createMonitor(client: RemoteHerdrClient | CoreConnection): AgentMonitor;
9
+ createMonitor(client: CoreConnection): AgentMonitor;
11
10
  needsContextRelay(): boolean;
12
- prepareContextRelay(
13
- client: RemoteHerdrClient | CoreConnection,
14
- ): Promise<void>;
11
+ prepareContextRelay(client: CoreConnection): Promise<void>;
15
12
  disconnected(reason: string): void;
16
13
  }
17
14
 
@@ -25,7 +22,7 @@ export async function connectMachine(
25
22
  events: ConnectionEvents,
26
23
  ): Promise<void> {
27
24
  if (
28
- (!runtime.config && !runtime.coreMachine) ||
25
+ !runtime.coreMachine ||
29
26
  runtime.local ||
30
27
  runtime.status === "connected" ||
31
28
  runtime.attempting ||
@@ -38,23 +35,17 @@ export async function connectMachine(
38
35
  delete runtime.reason;
39
36
  events.refresh();
40
37
  let attached = false;
41
- let client: RemoteHerdrClient | CoreConnection | undefined;
38
+ let client: CoreConnection | undefined;
42
39
  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
- }
40
+ client = new CoreConnection(
41
+ process.env["HC_TERMINAL_SOCKET"],
42
+ runtime.coreMachine,
43
+ );
53
44
  if (!events.isCurrent() || runtimes.get(name) !== runtime) {
54
45
  client.close();
55
46
  return;
56
47
  }
57
- await client.request("ping");
48
+ await client.machineIdentity();
58
49
  if (!events.isCurrent() || runtimes.get(name) !== runtime) {
59
50
  client.close();
60
51
  return;
@@ -4,18 +4,15 @@ import type {
4
4
  } from "@earendil-works/pi-coding-agent";
5
5
  import { CoreConnection } from "../core/backend.js";
6
6
  import { CoreClient } from "../core/client.js";
7
- import { type CoreMachine, coreMachines } from "../core/remote-exec.js";
8
- import { HerdrClient, type HerdrConnection } from "../herdr-client.js";
9
- import { LOCAL_MACHINE, readMachineCatalog } from "../machine-catalog.js";
7
+ import { type CoreMachine, coreMachines } from "../core/machines.js";
10
8
  import { AgentMonitor } from "../monitor.js";
11
- import { RemoteHerdrClient } from "../remote/client.js";
12
9
  import type {
13
10
  MachineStatus,
14
11
  MonitoredAgent,
15
12
  ScopedMonitoredAgent,
16
13
  } from "../types.js";
17
14
  import { renderAgentWidget } from "../widget.js";
18
- import { reloadCoreCatalog, reloadSshCatalog } from "./catalog.js";
15
+ import { reloadCoreCatalog } from "./catalog.js";
19
16
  import { connectMachine } from "./connection.js";
20
17
  import {
21
18
  connectedMachine,
@@ -26,6 +23,7 @@ import {
26
23
  import {
27
24
  type ConnectedMachine,
28
25
  errorMessage,
26
+ LOCAL_MACHINE,
29
27
  type MachineRuntime,
30
28
  type MachineSnapshot,
31
29
  } from "./machine.js";
@@ -95,30 +93,25 @@ export class AgentFleet {
95
93
  this.deactivate();
96
94
  const generation = this.generation;
97
95
  this.context = ctx;
98
- const catalog = process.env["HC_TERMINAL_SOCKET"]
99
- ? {}
100
- : await readMachineCatalog();
101
- if (!this.isCurrent(generation, ctx)) return;
102
- const machines = Object.entries(catalog).filter(
103
- ([, machine]) => machine.enabled,
104
- );
105
- if (process.env["HC_TERMINAL_SOCKET"]) {
106
- const identityClient = new CoreConnection();
107
- let coreId: string;
108
- try {
109
- coreId = (await identityClient.machineIdentity()).session;
110
- } finally {
111
- identityClient.close();
112
- }
113
- if (!this.isCurrent(generation, ctx)) return;
114
- this.localCoreId = coreId;
96
+ const identityClient = new CoreConnection();
97
+ let coreId: string;
98
+ try {
99
+ coreId = (await identityClient.machineIdentity()).session;
100
+ } catch (error) {
101
+ throw new Error(
102
+ `Shepherdr could not connect to Core: ${errorMessage(error)}. Start or reconnect Core, then retry from a Core terminal.`,
103
+ { cause: error },
104
+ );
105
+ } finally {
106
+ identityClient.close();
115
107
  }
108
+ if (!this.isCurrent(generation, ctx)) return;
109
+ this.localCoreId = coreId;
116
110
  const grouped = groupSaved(savedAgents(ctx, this.localCoreId));
117
111
  const coreCatalog = await this.coreCatalog();
118
112
  if (!this.isCurrent(generation, ctx)) return;
119
113
  const known = new Set([
120
114
  LOCAL_MACHINE,
121
- ...machines.map(([id]) => id),
122
115
  ...coreCatalog.map((machine) => machine.id),
123
116
  ]);
124
117
  const orphaned = [...grouped.entries()]
@@ -132,33 +125,24 @@ export class AgentFleet {
132
125
  }
133
126
 
134
127
  const localRuntime: MachineRuntime = {
128
+ coreId,
135
129
  local: true,
136
130
  pending: grouped.get(LOCAL_MACHINE) ?? [],
137
131
  status: "connecting",
138
132
  };
139
133
  this.runtimes.set(LOCAL_MACHINE, localRuntime);
140
- for (const [name, config] of machines) {
141
- this.runtimes.set(name, {
142
- config,
143
- local: false,
144
- pending: grouped.get(name) ?? [],
145
- status: "connecting",
146
- });
147
- }
148
134
  for (const machine of coreCatalog)
149
135
  this.runtimes.set(machine.id, {
136
+ coreId: machine.id,
150
137
  coreMachine: machine,
151
138
  local: false,
152
139
  pending: grouped.get(machine.id) ?? [],
153
140
  status: "connecting",
154
141
  });
155
142
 
156
- const localClient = process.env["HC_TERMINAL_SOCKET"]
157
- ? new CoreConnection()
158
- : new HerdrClient();
159
- await localClient.request("ping");
143
+ const localClient = new CoreConnection();
160
144
  if (!this.isCurrent(generation, ctx)) {
161
- if (localClient instanceof CoreConnection) localClient.close();
145
+ localClient.close();
162
146
  return;
163
147
  }
164
148
  const localMonitor = this.createMonitor(
@@ -182,9 +166,6 @@ export class AgentFleet {
182
166
  localRuntime.status = "connected";
183
167
  this.changed();
184
168
 
185
- for (const [name] of machines) {
186
- void this.connectMachine(name, generation, true);
187
- }
188
169
  for (const machine of coreCatalog)
189
170
  void this.connectMachine(machine.id, generation, true);
190
171
  }
@@ -195,11 +176,7 @@ export class AgentFleet {
195
176
  this.lastPersistedState = undefined;
196
177
  for (const runtime of this.runtimes.values()) {
197
178
  runtime.monitor?.deactivate();
198
- if (
199
- runtime.client instanceof RemoteHerdrClient ||
200
- runtime.client instanceof CoreConnection
201
- )
202
- runtime.client.close();
179
+ runtime.client?.close();
203
180
  }
204
181
  this.runtimes.clear();
205
182
  renderAgentWidget(this.context, [], []);
@@ -215,24 +192,12 @@ export class AgentFleet {
215
192
  const ctx = this.context;
216
193
  if (!ctx) return;
217
194
  const generation = this.generation;
218
- if (process.env["HC_TERMINAL_SOCKET"]) {
219
- const catalog = await this.coreCatalog();
220
- if (!this.isCurrent(generation, ctx)) return;
221
- reloadCoreCatalog(this.runtimes, catalog, (id) => {
222
- void this.connectMachine(id, generation, true);
223
- });
224
- this.changed();
225
- return;
226
- }
227
- const catalog = process.env["HC_TERMINAL_SOCKET"]
228
- ? {}
229
- : await readMachineCatalog();
195
+ const catalog = await this.coreCatalog();
230
196
  if (!this.isCurrent(generation, ctx)) return;
231
- const reconnect = reloadSshCatalog(this.runtimes, catalog);
197
+ reloadCoreCatalog(this.runtimes, catalog, (id) => {
198
+ void this.connectMachine(id, generation, true);
199
+ });
232
200
  this.changed();
233
- for (const name of reconnect) {
234
- void this.connectMachine(name, generation, true);
235
- }
236
201
  }
237
202
 
238
203
  statuses(): MachineStatus[] {
@@ -244,6 +209,11 @@ export class AgentFleet {
244
209
  list(): ScopedMonitoredAgent[] {
245
210
  return listAgents(this.runtimes);
246
211
  }
212
+ localMachine(): string {
213
+ if (!this.localCoreId)
214
+ throw new Error("Core is unavailable; reconnect before sharing context");
215
+ return this.localCoreId;
216
+ }
247
217
  connected(machine?: string): ConnectedMachine {
248
218
  const ctx = this.context;
249
219
  return connectedMachine(
@@ -260,13 +230,6 @@ export class AgentFleet {
260
230
  "Session changed; retry sharing from the current session",
261
231
  );
262
232
  if (runtime.local) return;
263
- if (
264
- !(
265
- client instanceof RemoteHerdrClient ||
266
- client instanceof CoreConnection
267
- )
268
- )
269
- throw new Error("Context sharing is unavailable on this machine");
270
233
  await this.prepareContextRelay(name, runtime, client, ctx);
271
234
  if (
272
235
  this.context !== ctx ||
@@ -299,7 +262,7 @@ export class AgentFleet {
299
262
  private createMonitor(
300
263
  machine: string,
301
264
  runtime: MachineRuntime,
302
- client: HerdrConnection,
265
+ client: CoreConnection,
303
266
  ): AgentMonitor {
304
267
  return createMachineMonitor(machine, runtime, client, this.pi, {
305
268
  isAttached: () => this.runtimes.get(machine) === runtime,
@@ -312,7 +275,7 @@ export class AgentFleet {
312
275
  private async prepareContextRelay(
313
276
  name: string,
314
277
  runtime: MachineRuntime,
315
- client: RemoteHerdrClient | CoreConnection,
278
+ client: CoreConnection,
316
279
  ctx: ExtensionContext,
317
280
  ): Promise<void> {
318
281
  return this.setup.prepareContextRelay(
@@ -367,11 +330,7 @@ export class AgentFleet {
367
330
  // Keep the stopped owner until reconnect so in-flight dispatch cleanup
368
331
  // updates the state we persist, rather than a detached snapshot.
369
332
  runtime.monitor?.deactivate();
370
- if (
371
- runtime.client instanceof RemoteHerdrClient ||
372
- runtime.client instanceof CoreConnection
373
- )
374
- runtime.client.close();
333
+ runtime.client?.close();
375
334
  delete runtime.client;
376
335
  delete runtime.contextRelayError;
377
336
  delete runtime.monitoringIssue;
@@ -402,10 +361,13 @@ export class AgentFleet {
402
361
  }
403
362
  private async coreCatalog(): Promise<CoreMachine[]> {
404
363
  const socket = process.env["HC_TERMINAL_SOCKET"];
405
- if (!socket) return [];
364
+ if (!socket)
365
+ throw new Error("Run Pi inside a Core terminal to connect Shepherdr");
406
366
  const client = new CoreClient(socket);
407
367
  try {
408
- return await coreMachines(client);
368
+ return (await coreMachines(client)).filter(
369
+ (machine) => machine.id !== this.localCoreId,
370
+ );
409
371
  } finally {
410
372
  client.close();
411
373
  }
@@ -1,9 +1,6 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
- import { CoreConnection } from "../core/backend.js";
3
- import { getSnapshot } from "../herdr.js";
4
- import { LOCAL_MACHINE } from "../machine-catalog.js";
2
+ import { getSnapshot } from "../core/agents.js";
5
3
  import { parseMonitoredAgent } from "../monitor-record.js";
6
- import { RemoteHerdrClient } from "../remote/client.js";
7
4
  import type {
8
5
  MachineStatus,
9
6
  MonitoredAgent,
@@ -12,8 +9,10 @@ import type {
12
9
  import {
13
10
  type ConnectedMachine,
14
11
  errorMessage,
12
+ LOCAL_MACHINE,
15
13
  type MachineRuntime,
16
14
  type MachineSnapshot,
15
+ resolveMachine,
17
16
  } from "./machine.js";
18
17
 
19
18
  export function listAgents(
@@ -38,23 +37,19 @@ export function connectedMachine(
38
37
  ) => Promise<void>,
39
38
  machine?: string,
40
39
  ): ConnectedMachine {
41
- const name = machine?.trim() || LOCAL_MACHINE;
40
+ const name = resolveMachine(runtimes, machine?.trim() || LOCAL_MACHINE);
42
41
  const runtime = runtimes.get(name);
43
42
  if (!runtime) {
44
43
  throw new Error(
45
- `unknown Herdr machine ${JSON.stringify(name)}; use list to see configured machines`,
44
+ `Unknown Core machine ${JSON.stringify(name)}; use list to see current machines`,
46
45
  );
47
46
  }
48
47
  if (runtime.status !== "connected" || !runtime.client || !runtime.monitor) {
49
48
  throw new Error(
50
- `Herdr machine ${JSON.stringify(name)} is ${runtime.status}${runtime.reason ? `: ${runtime.reason}` : ""}; open /herdr → Status to reconnect`,
49
+ `Core machine ${JSON.stringify(name)} is ${runtime.status}${runtime.reason ? `: ${runtime.reason}` : ""}; open /herdr → Status to reconnect`,
51
50
  );
52
51
  }
53
- const remote =
54
- runtime.client instanceof RemoteHerdrClient ||
55
- (runtime.client instanceof CoreConnection && runtime.client.remote)
56
- ? runtime.client
57
- : undefined;
52
+ const remote = runtime.client.remote ? runtime.client : undefined;
58
53
  const client = runtime.client;
59
54
  return {
60
55
  prepareSharing: () => prepareSharing(name, runtime, client),
@@ -77,7 +72,7 @@ export async function machineSnapshots(
77
72
  machine?: string,
78
73
  ): Promise<MachineSnapshot[]> {
79
74
  const selected = machine?.trim()
80
- ? [statusFor(runtimes, machine.trim())]
75
+ ? [statusFor(runtimes, resolveMachine(runtimes, machine))]
81
76
  : [...runtimes.keys()].map((name) => statusFor(runtimes, name));
82
77
  return Promise.all(
83
78
  selected.map(async (status) => {
@@ -127,18 +122,10 @@ export function statusFor(
127
122
  name: string,
128
123
  ): MachineStatus {
129
124
  const runtime = runtimes.get(name);
130
- if (!runtime)
131
- throw new Error(`unknown Herdr machine ${JSON.stringify(name)}`);
125
+ if (!runtime) throw new Error(`Unknown Core machine ${JSON.stringify(name)}`);
132
126
  return {
133
127
  id: name,
134
128
  local: runtime.local,
135
- ...(runtime.config
136
- ? {
137
- label: runtime.config.label,
138
- target: runtime.config.target,
139
- session: runtime.config.session,
140
- }
141
- : {}),
142
129
  ...(runtime.coreMachine
143
130
  ? {
144
131
  label: runtime.coreMachine.name,
@@ -1,6 +1,7 @@
1
- import type { CoreMachine } from "../core/remote-exec.js";
2
- import type { HerdrConnection } from "../herdr-client.js";
3
- import type { SshMachine } from "../machine-catalog.js";
1
+ import type { CoreConnection } from "../core/backend.js";
2
+ import type { CoreMachine } from "../core/machines.js";
3
+ export const LOCAL_MACHINE = "local";
4
+
4
5
  import type { AgentMonitor } from "../monitor.js";
5
6
  import type {
6
7
  MachineStatus,
@@ -10,8 +11,8 @@ import type {
10
11
 
11
12
  export interface MachineRuntime {
12
13
  attempting?: boolean;
13
- client?: HerdrConnection;
14
- config?: SshMachine;
14
+ client?: CoreConnection;
15
+ coreId: string;
15
16
  coreMachine?: CoreMachine;
16
17
  contextRelayError?: string;
17
18
  local: boolean;
@@ -24,7 +25,7 @@ export interface MachineRuntime {
24
25
 
25
26
  export interface ConnectedMachine {
26
27
  prepareSharing: () => Promise<void>;
27
- client: HerdrConnection;
28
+ client: CoreConnection;
28
29
  fallbackCwd: string;
29
30
  local: boolean;
30
31
  machine: string;
@@ -46,10 +47,25 @@ export function errorMessage(error: unknown): string {
46
47
  return message.length <= 500 ? message : `${message.slice(0, 499)}…`;
47
48
  }
48
49
 
49
- function shellQuote(value: string): string {
50
- return `'${value.replaceAll("'", `'"'"'`)}'`;
51
- }
52
-
53
- export function operatorPrefix(config: SshMachine): string {
54
- return `ssh -T -o BatchMode=yes -- ${shellQuote(config.target)} herdr --session ${shellQuote(config.session)}`;
50
+ /** Local alias and stable IDs win over mutable catalog names; never infer hosts. */
51
+ export function resolveMachine(
52
+ runtimes: ReadonlyMap<string, MachineRuntime>,
53
+ selector: string,
54
+ ): string {
55
+ const target = selector.trim();
56
+ if (runtimes.has(target)) return target;
57
+ const byId = [...runtimes].find(([, runtime]) => runtime.coreId === target);
58
+ if (byId) return byId[0];
59
+ const matches = [...runtimes].filter(
60
+ ([, runtime]) => runtime.coreMachine?.name === target,
61
+ );
62
+ const match = matches[0];
63
+ if (matches.length === 1 && match) return match[0];
64
+ if (matches.length > 1)
65
+ throw new Error(
66
+ `Ambiguous Core machine ${JSON.stringify(target)}; use a stable machine ID: ${matches.map(([id]) => id).join(", ")}`,
67
+ );
68
+ throw new Error(
69
+ `Unknown Core machine ${JSON.stringify(target)}; use list to see current machines or ${LOCAL_MACHINE}`,
70
+ );
55
71
  }
@@ -2,16 +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 type { HerdrConnection } from "../herdr-client.js";
7
- import { LOCAL_MACHINE } from "../machine-catalog.js";
5
+ import type { CoreConnection } from "../core/backend.js";
8
6
  import { AgentMonitor } from "../monitor.js";
9
- import { RemoteHerdrClient } from "../remote/client.js";
10
- import {
11
- errorMessage,
12
- type MachineRuntime,
13
- operatorPrefix,
14
- } from "./machine.js";
7
+ import { errorMessage, LOCAL_MACHINE, type MachineRuntime } from "./machine.js";
15
8
 
16
9
  interface FleetMonitorEvents {
17
10
  isAttached(): boolean;
@@ -25,29 +18,18 @@ interface FleetMonitorEvents {
25
18
  export function createMachineMonitor(
26
19
  machine: string,
27
20
  runtime: MachineRuntime,
28
- client: HerdrConnection,
21
+ client: CoreConnection,
29
22
  pi: ExtensionAPI,
30
23
  events: FleetMonitorEvents,
31
24
  ): AgentMonitor {
32
- const selfPaneId = runtime.local
33
- ? process.env[
34
- client instanceof CoreConnection ? "HC_TERMINAL_ID" : "HERDR_PANE_ID"
35
- ]
36
- : undefined;
25
+ const selfPaneId = runtime.local ? process.env["HC_TERMINAL_ID"] : undefined;
37
26
  return new AgentMonitor(pi, {
38
27
  client,
39
28
  machine,
40
- machineLabel: () =>
41
- runtime.coreMachine?.name ?? runtime.config?.label ?? LOCAL_MACHINE,
29
+ machineLabel: () => runtime.coreMachine?.name ?? LOCAL_MACHINE,
42
30
  onChange: () => events.changed(),
43
31
  onRefresh: () => events.refresh(),
44
- operatorPrefix: runtime.coreMachine
45
- ? "hc"
46
- : runtime.local
47
- ? client instanceof CoreConnection
48
- ? "hc"
49
- : "herdr"
50
- : operatorPrefix(runtime.config!),
32
+ operatorPrefix: "hc",
51
33
  onWarning: (issue) => {
52
34
  if (!events.isAttached()) return;
53
35
  if (!runtime.local && issue.state === "unavailable") {
@@ -73,13 +55,10 @@ export function createMachineMonitor(
73
55
  if (runtime.monitor?.list().length)
74
56
  events
75
57
  .context()
76
- ?.ui.notify(`Herdr monitoring restored: ${machine}`, "info");
58
+ ?.ui.notify(`Core monitoring restored: ${machine}`, "info");
77
59
  },
78
60
  reconnect: runtime.local,
79
- ...(client instanceof RemoteHerdrClient ? { reader: client } : {}),
80
- ...(client instanceof CoreConnection && client.reader
81
- ? { reader: client.reader }
82
- : {}),
61
+ ...(client.reader ? { reader: client.reader } : {}),
83
62
  ...(selfPaneId ? { selfPaneId } : {}),
84
63
  });
85
64
  }