@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
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { getSnapshot } from "../herdr.js";
|
|
3
|
+
import { LOCAL_MACHINE } from "../machine-catalog.js";
|
|
4
|
+
import { parseMonitoredAgent } from "../monitor-record.js";
|
|
5
|
+
import { RemoteHerdrClient } from "../remote/client.js";
|
|
6
|
+
import type {
|
|
7
|
+
MachineStatus,
|
|
8
|
+
MonitoredAgent,
|
|
9
|
+
ScopedMonitoredAgent,
|
|
10
|
+
} from "../types.js";
|
|
11
|
+
import {
|
|
12
|
+
type ConnectedMachine,
|
|
13
|
+
errorMessage,
|
|
14
|
+
type MachineRuntime,
|
|
15
|
+
type MachineSnapshot,
|
|
16
|
+
} from "./machine.js";
|
|
17
|
+
|
|
18
|
+
export function listAgents(
|
|
19
|
+
runtimes: ReadonlyMap<string, MachineRuntime>,
|
|
20
|
+
): ScopedMonitoredAgent[] {
|
|
21
|
+
return [...runtimes.entries()].flatMap(([machine, runtime]) => {
|
|
22
|
+
const values = runtime.monitor?.list() ?? runtime.pending;
|
|
23
|
+
return values
|
|
24
|
+
.map((value) => parseMonitoredAgent(value))
|
|
25
|
+
.filter((value): value is MonitoredAgent => value !== undefined)
|
|
26
|
+
.map((agent) => ({ ...agent, machine }));
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function connectedMachine(
|
|
31
|
+
runtimes: ReadonlyMap<string, MachineRuntime>,
|
|
32
|
+
context: ExtensionContext | undefined,
|
|
33
|
+
machine?: string,
|
|
34
|
+
): ConnectedMachine {
|
|
35
|
+
const name = machine?.trim() || LOCAL_MACHINE;
|
|
36
|
+
const runtime = runtimes.get(name);
|
|
37
|
+
if (!runtime) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
`unknown Herdr machine ${JSON.stringify(name)}; use list to see configured machines`,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
if (runtime.status !== "connected" || !runtime.client || !runtime.monitor) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`Herdr machine ${JSON.stringify(name)} is ${runtime.status}${runtime.reason ? `: ${runtime.reason}` : ""}; open /herdr → Status to reconnect`,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
const remote =
|
|
48
|
+
runtime.client instanceof RemoteHerdrClient ? runtime.client : undefined;
|
|
49
|
+
return {
|
|
50
|
+
machine: name,
|
|
51
|
+
local: runtime.local,
|
|
52
|
+
client: runtime.client,
|
|
53
|
+
monitor: runtime.monitor,
|
|
54
|
+
fallbackCwd: runtime.local ? (context?.cwd ?? ".") : "~",
|
|
55
|
+
...(remote
|
|
56
|
+
? {
|
|
57
|
+
resolveDirectory: (value: string | undefined, fallback: string) =>
|
|
58
|
+
remote.directory(value, fallback),
|
|
59
|
+
}
|
|
60
|
+
: {}),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function machineSnapshots(
|
|
65
|
+
runtimes: ReadonlyMap<string, MachineRuntime>,
|
|
66
|
+
machine?: string,
|
|
67
|
+
): Promise<MachineSnapshot[]> {
|
|
68
|
+
const selected = machine?.trim()
|
|
69
|
+
? [statusFor(runtimes, machine.trim())]
|
|
70
|
+
: [...runtimes.keys()].map((name) => statusFor(runtimes, name));
|
|
71
|
+
return Promise.all(
|
|
72
|
+
selected.map(async (status) => {
|
|
73
|
+
const runtime = runtimes.get(status.id);
|
|
74
|
+
if (status.status !== "connected" || !runtime?.client || !runtime.monitor)
|
|
75
|
+
return status;
|
|
76
|
+
const client = runtime.client;
|
|
77
|
+
const monitor = runtime.monitor;
|
|
78
|
+
try {
|
|
79
|
+
const snapshot = await getSnapshot(client);
|
|
80
|
+
if (
|
|
81
|
+
runtime.status !== "connected" ||
|
|
82
|
+
runtime.client !== client ||
|
|
83
|
+
runtime.monitor !== monitor ||
|
|
84
|
+
runtimes.get(status.id) !== runtime
|
|
85
|
+
) {
|
|
86
|
+
return runtimes.has(status.id)
|
|
87
|
+
? statusFor(runtimes, status.id)
|
|
88
|
+
: status;
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
...status,
|
|
92
|
+
monitoredPaneIds: new Set(
|
|
93
|
+
monitor.list().map((record) => record.paneId),
|
|
94
|
+
),
|
|
95
|
+
snapshot,
|
|
96
|
+
};
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if (
|
|
99
|
+
runtime.status !== "connected" ||
|
|
100
|
+
runtime.client !== client ||
|
|
101
|
+
runtime.monitor !== monitor ||
|
|
102
|
+
runtimes.get(status.id) !== runtime
|
|
103
|
+
) {
|
|
104
|
+
return runtimes.has(status.id)
|
|
105
|
+
? statusFor(runtimes, status.id)
|
|
106
|
+
: status;
|
|
107
|
+
}
|
|
108
|
+
return { ...status, snapshotError: errorMessage(error) };
|
|
109
|
+
}
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function statusFor(
|
|
115
|
+
runtimes: ReadonlyMap<string, MachineRuntime>,
|
|
116
|
+
name: string,
|
|
117
|
+
): MachineStatus {
|
|
118
|
+
const runtime = runtimes.get(name);
|
|
119
|
+
if (!runtime)
|
|
120
|
+
throw new Error(`unknown Herdr machine ${JSON.stringify(name)}`);
|
|
121
|
+
return {
|
|
122
|
+
id: name,
|
|
123
|
+
local: runtime.local,
|
|
124
|
+
...(runtime.config
|
|
125
|
+
? {
|
|
126
|
+
label: runtime.config.label,
|
|
127
|
+
target: runtime.config.target,
|
|
128
|
+
session: runtime.config.session,
|
|
129
|
+
}
|
|
130
|
+
: {}),
|
|
131
|
+
status: runtime.status,
|
|
132
|
+
...(runtime.monitoringIssue
|
|
133
|
+
? { monitoringIssue: runtime.monitoringIssue }
|
|
134
|
+
: {}),
|
|
135
|
+
...(runtime.reason ? { reason: runtime.reason } : {}),
|
|
136
|
+
...(runtime.contextRelayError
|
|
137
|
+
? { contextRelayError: runtime.contextRelayError }
|
|
138
|
+
: {}),
|
|
139
|
+
...(runtime.attempting ? { attempting: true } : {}),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { HerdrConnection } from "../herdr-client.js";
|
|
2
|
+
import type { SshMachine } from "../machine-catalog.js";
|
|
3
|
+
import type { AgentMonitor } from "../monitor.js";
|
|
4
|
+
import type {
|
|
5
|
+
MachineStatus,
|
|
6
|
+
MonitoringIssue,
|
|
7
|
+
SessionSnapshot,
|
|
8
|
+
} from "../types.js";
|
|
9
|
+
|
|
10
|
+
export interface MachineRuntime {
|
|
11
|
+
attempting?: boolean;
|
|
12
|
+
client?: HerdrConnection;
|
|
13
|
+
config?: SshMachine;
|
|
14
|
+
contextRelayError?: string;
|
|
15
|
+
local: boolean;
|
|
16
|
+
monitor?: AgentMonitor;
|
|
17
|
+
monitoringIssue?: MonitoringIssue;
|
|
18
|
+
pending: unknown[];
|
|
19
|
+
reason?: string;
|
|
20
|
+
status: MachineStatus["status"];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ConnectedMachine {
|
|
24
|
+
client: HerdrConnection;
|
|
25
|
+
fallbackCwd: string;
|
|
26
|
+
local: boolean;
|
|
27
|
+
machine: string;
|
|
28
|
+
monitor: AgentMonitor;
|
|
29
|
+
resolveDirectory?: (
|
|
30
|
+
value: string | undefined,
|
|
31
|
+
fallback: string,
|
|
32
|
+
) => Promise<string>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface MachineSnapshot extends MachineStatus {
|
|
36
|
+
monitoredPaneIds?: ReadonlySet<string>;
|
|
37
|
+
snapshot?: SessionSnapshot;
|
|
38
|
+
snapshotError?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function errorMessage(error: unknown): string {
|
|
42
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
43
|
+
return message.length <= 500 ? message : `${message.slice(0, 499)}…`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function shellQuote(value: string): string {
|
|
47
|
+
return `'${value.replaceAll("'", `'"'"'`)}'`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function operatorPrefix(config: SshMachine): string {
|
|
51
|
+
return `ssh -T -o BatchMode=yes -- ${shellQuote(config.target)} herdr --session ${shellQuote(config.session)}`;
|
|
52
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionAPI,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import type { HerdrConnection } from "../herdr-client.js";
|
|
6
|
+
import { LOCAL_MACHINE } from "../machine-catalog.js";
|
|
7
|
+
import { AgentMonitor } from "../monitor.js";
|
|
8
|
+
import { RemoteHerdrClient } from "../remote/client.js";
|
|
9
|
+
import {
|
|
10
|
+
errorMessage,
|
|
11
|
+
type MachineRuntime,
|
|
12
|
+
operatorPrefix,
|
|
13
|
+
} from "./machine.js";
|
|
14
|
+
|
|
15
|
+
interface FleetMonitorEvents {
|
|
16
|
+
isAttached(): boolean;
|
|
17
|
+
context(): ExtensionContext | undefined;
|
|
18
|
+
changed(): void;
|
|
19
|
+
refresh(): void;
|
|
20
|
+
disconnected(reason: string): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Owns the local/remote monitoring adapter and its warning/recovery policy. */
|
|
24
|
+
export function createMachineMonitor(
|
|
25
|
+
machine: string,
|
|
26
|
+
runtime: MachineRuntime,
|
|
27
|
+
client: HerdrConnection,
|
|
28
|
+
pi: ExtensionAPI,
|
|
29
|
+
events: FleetMonitorEvents,
|
|
30
|
+
): AgentMonitor {
|
|
31
|
+
return new AgentMonitor(pi, {
|
|
32
|
+
client,
|
|
33
|
+
machine,
|
|
34
|
+
machineLabel: () => runtime.config?.label ?? LOCAL_MACHINE,
|
|
35
|
+
onChange: () => events.changed(),
|
|
36
|
+
onRefresh: () => events.refresh(),
|
|
37
|
+
operatorPrefix: runtime.local ? "herdr" : operatorPrefix(runtime.config!),
|
|
38
|
+
onWarning: (issue) => {
|
|
39
|
+
if (!events.isAttached()) return;
|
|
40
|
+
if (!runtime.local && issue.state === "unavailable") {
|
|
41
|
+
events.disconnected(issue.message);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
runtime.monitoringIssue = {
|
|
45
|
+
...issue,
|
|
46
|
+
message: errorMessage(issue.message),
|
|
47
|
+
};
|
|
48
|
+
events.refresh();
|
|
49
|
+
events
|
|
50
|
+
.context()
|
|
51
|
+
?.ui.notify(
|
|
52
|
+
`${machine}: ${issue.message}${runtime.local ? "" : "\nOpen /herdr → Status to reconnect"}`,
|
|
53
|
+
"warning",
|
|
54
|
+
);
|
|
55
|
+
},
|
|
56
|
+
onRecovered: () => {
|
|
57
|
+
if (!events.isAttached() || !runtime.monitoringIssue) return;
|
|
58
|
+
delete runtime.monitoringIssue;
|
|
59
|
+
events.refresh();
|
|
60
|
+
if (runtime.monitor?.list().length)
|
|
61
|
+
events
|
|
62
|
+
.context()
|
|
63
|
+
?.ui.notify(`Herdr monitoring restored: ${machine}`, "info");
|
|
64
|
+
},
|
|
65
|
+
reconnect: runtime.local,
|
|
66
|
+
...(client instanceof RemoteHerdrClient ? { reader: client } : {}),
|
|
67
|
+
...(runtime.local && process.env["HERDR_PANE_ID"]
|
|
68
|
+
? { selfPaneId: process.env["HERDR_PANE_ID"] }
|
|
69
|
+
: {}),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { LOCAL_MACHINE } from "../machine-catalog.js";
|
|
3
|
+
export const MONITOR_STATE_TYPE = "herdr-agents-monitor-state";
|
|
4
|
+
|
|
5
|
+
export function savedAgents(ctx: ExtensionContext): unknown[] {
|
|
6
|
+
let latest: unknown[] = [];
|
|
7
|
+
let version: unknown;
|
|
8
|
+
for (const entry of ctx.sessionManager.getBranch()) {
|
|
9
|
+
if (entry.type !== "custom" || entry.customType !== MONITOR_STATE_TYPE)
|
|
10
|
+
continue;
|
|
11
|
+
const data = entry.data as { version?: unknown; agents?: unknown };
|
|
12
|
+
if (Array.isArray(data?.agents)) {
|
|
13
|
+
latest = data.agents;
|
|
14
|
+
version = data.version;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (latest.length > 0 && version !== 2) {
|
|
18
|
+
ctx.ui.notify(
|
|
19
|
+
"Cleared legacy Shepherdr watches; use watch to subscribe explicitly. Remote agents are unchanged.",
|
|
20
|
+
"warning",
|
|
21
|
+
);
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
return latest;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function groupSaved(values: unknown[]): Map<string, unknown[]> {
|
|
28
|
+
const grouped = new Map<string, unknown[]>();
|
|
29
|
+
for (const value of values) {
|
|
30
|
+
const machine =
|
|
31
|
+
typeof value === "object" &&
|
|
32
|
+
value !== null &&
|
|
33
|
+
"machine" in value &&
|
|
34
|
+
typeof value.machine === "string"
|
|
35
|
+
? value.machine
|
|
36
|
+
: LOCAL_MACHINE;
|
|
37
|
+
const records = grouped.get(machine) ?? [];
|
|
38
|
+
records.push(value);
|
|
39
|
+
grouped.set(machine, records);
|
|
40
|
+
}
|
|
41
|
+
return grouped;
|
|
42
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { MachineRuntime } from "./machine.js";
|
|
2
|
+
|
|
3
|
+
/** Explicit reconnect selection and user receipts; never schedules autonomous reconnects. */
|
|
4
|
+
export function connectMachines(
|
|
5
|
+
runtimes: ReadonlyMap<string, MachineRuntime>,
|
|
6
|
+
machine: string | undefined,
|
|
7
|
+
retrySetup: (name: string, runtime: MachineRuntime) => Promise<void>,
|
|
8
|
+
connectMachine: (name: string) => Promise<void>,
|
|
9
|
+
): string {
|
|
10
|
+
const target = machine?.trim();
|
|
11
|
+
if (target) {
|
|
12
|
+
const runtime = runtimes.get(target);
|
|
13
|
+
if (!runtime)
|
|
14
|
+
throw new Error(`unknown Herdr machine ${JSON.stringify(target)}`);
|
|
15
|
+
if (
|
|
16
|
+
runtime.status === "connected" &&
|
|
17
|
+
(runtime.monitoringIssue || runtime.contextRelayError)
|
|
18
|
+
) {
|
|
19
|
+
if (runtime.attempting)
|
|
20
|
+
return `Already retrying connection setup on ${target}`;
|
|
21
|
+
void retrySetup(target, runtime);
|
|
22
|
+
return `Retrying connection setup on ${target}`;
|
|
23
|
+
}
|
|
24
|
+
if (runtime.local) {
|
|
25
|
+
return `${target} is the local machine and is already connected`;
|
|
26
|
+
}
|
|
27
|
+
if (runtime.status === "connected") {
|
|
28
|
+
return `${target} is already connected`;
|
|
29
|
+
}
|
|
30
|
+
if (runtime.attempting || runtime.status === "connecting") {
|
|
31
|
+
return `${target} is already connecting`;
|
|
32
|
+
}
|
|
33
|
+
void connectMachine(target);
|
|
34
|
+
return `Connecting to ${target}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const retries = [...runtimes.entries()].filter(
|
|
38
|
+
([, runtime]) =>
|
|
39
|
+
runtime.status === "connected" &&
|
|
40
|
+
(runtime.monitoringIssue || runtime.contextRelayError) &&
|
|
41
|
+
!runtime.attempting,
|
|
42
|
+
);
|
|
43
|
+
for (const [name, runtime] of retries) void retrySetup(name, runtime);
|
|
44
|
+
const names = [...runtimes.entries()]
|
|
45
|
+
.filter(
|
|
46
|
+
([, runtime]) =>
|
|
47
|
+
!runtime.local && runtime.status !== "connected" && !runtime.attempting,
|
|
48
|
+
)
|
|
49
|
+
.map(([name]) => name);
|
|
50
|
+
for (const name of names) {
|
|
51
|
+
void connectMachine(name);
|
|
52
|
+
}
|
|
53
|
+
if (names.length > 0 || retries.length > 0) {
|
|
54
|
+
return [
|
|
55
|
+
retries.length > 0
|
|
56
|
+
? `Retrying connection setup on ${retries.map(([name]) => name).join(", ")}`
|
|
57
|
+
: "",
|
|
58
|
+
names.length > 0 ? `Connecting to ${names.join(", ")}` : "",
|
|
59
|
+
]
|
|
60
|
+
.filter(Boolean)
|
|
61
|
+
.join(". ");
|
|
62
|
+
}
|
|
63
|
+
const remotes = [...runtimes.entries()].filter(
|
|
64
|
+
([, runtime]) => !runtime.local,
|
|
65
|
+
);
|
|
66
|
+
if (remotes.length === 0) {
|
|
67
|
+
return "No remote machines are configured";
|
|
68
|
+
}
|
|
69
|
+
const connecting = remotes
|
|
70
|
+
.filter(([, runtime]) => runtime.attempting)
|
|
71
|
+
.map(([name]) => name);
|
|
72
|
+
return connecting.length > 0
|
|
73
|
+
? `Already connecting to ${connecting.join(", ")}`
|
|
74
|
+
: "All configured machines are connected";
|
|
75
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { RemoteHerdrClient } from "../remote/client.js";
|
|
3
|
+
import { errorMessage, type MachineRuntime } from "./machine.js";
|
|
4
|
+
|
|
5
|
+
/** Explicitly requested sharing/monitor readiness; fleet supplies current-selection leases. */
|
|
6
|
+
export class FleetSetup {
|
|
7
|
+
private readonly refresh: () => void;
|
|
8
|
+
constructor(refresh: () => void) {
|
|
9
|
+
this.refresh = refresh;
|
|
10
|
+
}
|
|
11
|
+
private contextRelay:
|
|
12
|
+
| ((
|
|
13
|
+
ctx: ExtensionContext,
|
|
14
|
+
request: unknown,
|
|
15
|
+
signal: AbortSignal,
|
|
16
|
+
) => Promise<unknown>)
|
|
17
|
+
| undefined;
|
|
18
|
+
setContextRelay(
|
|
19
|
+
handler: (
|
|
20
|
+
ctx: ExtensionContext,
|
|
21
|
+
request: unknown,
|
|
22
|
+
signal: AbortSignal,
|
|
23
|
+
) => Promise<unknown>,
|
|
24
|
+
): void {
|
|
25
|
+
this.contextRelay = handler;
|
|
26
|
+
}
|
|
27
|
+
async prepareContextRelay(
|
|
28
|
+
name: string,
|
|
29
|
+
runtime: MachineRuntime,
|
|
30
|
+
client: RemoteHerdrClient,
|
|
31
|
+
ctx: ExtensionContext,
|
|
32
|
+
isSelected: () => boolean,
|
|
33
|
+
): Promise<void> {
|
|
34
|
+
if (!this.contextRelay) return;
|
|
35
|
+
try {
|
|
36
|
+
await client.startContextRelay(
|
|
37
|
+
ctx.sessionManager.getSessionId(),
|
|
38
|
+
name,
|
|
39
|
+
(request, signal) => this.contextRelay!(ctx, request, signal),
|
|
40
|
+
);
|
|
41
|
+
if (!isSelected()) return;
|
|
42
|
+
delete runtime.contextRelayError;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (!isSelected()) return;
|
|
45
|
+
runtime.contextRelayError = errorMessage(error);
|
|
46
|
+
ctx.ui.notify(
|
|
47
|
+
`Context sharing unavailable on ${name}: ${runtime.contextRelayError}\nOpen /herdr → Status to retry sharing.`,
|
|
48
|
+
"warning",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async retrySetup(
|
|
54
|
+
name: string,
|
|
55
|
+
runtime: MachineRuntime,
|
|
56
|
+
ctx: ExtensionContext | undefined,
|
|
57
|
+
isCurrent: () => boolean,
|
|
58
|
+
isRegistered: () => boolean,
|
|
59
|
+
isSelected: () => boolean,
|
|
60
|
+
): Promise<void> {
|
|
61
|
+
const monitor = runtime.monitor;
|
|
62
|
+
if (!monitor || !ctx || runtime.attempting) return;
|
|
63
|
+
runtime.attempting = true;
|
|
64
|
+
this.refresh();
|
|
65
|
+
try {
|
|
66
|
+
if (
|
|
67
|
+
runtime.contextRelayError &&
|
|
68
|
+
runtime.client instanceof RemoteHerdrClient
|
|
69
|
+
)
|
|
70
|
+
await this.prepareContextRelay(
|
|
71
|
+
name,
|
|
72
|
+
runtime,
|
|
73
|
+
runtime.client,
|
|
74
|
+
ctx,
|
|
75
|
+
isSelected,
|
|
76
|
+
);
|
|
77
|
+
if (isCurrent() && isRegistered() && runtime.monitoringIssue)
|
|
78
|
+
await monitor.retryMonitoring();
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (isCurrent() && isRegistered() && runtime.monitor === monitor) {
|
|
81
|
+
ctx.ui.notify(
|
|
82
|
+
`Monitoring retry failed on ${name}: ${errorMessage(error)}`,
|
|
83
|
+
"warning",
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
} finally {
|
|
87
|
+
if (isRegistered() && runtime.monitor === monitor)
|
|
88
|
+
delete runtime.attempting;
|
|
89
|
+
this.refresh();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { BoardBinding } from "@howaboua/pi-agent-board/integration";
|
|
2
|
+
|
|
3
|
+
export function attachmentMessage(
|
|
4
|
+
controllerContextAgent?: string,
|
|
5
|
+
boardAgent?: string,
|
|
6
|
+
) {
|
|
7
|
+
return {
|
|
8
|
+
customType: "herdr-agent-attachment",
|
|
9
|
+
content: [
|
|
10
|
+
...(controllerContextAgent
|
|
11
|
+
? [
|
|
12
|
+
`Controller notes/history: ${controllerContextAgent}; own context unchanged`,
|
|
13
|
+
]
|
|
14
|
+
: []),
|
|
15
|
+
...(boardAgent ? [`Shared board: ${boardAgent}`] : []),
|
|
16
|
+
].join("\n"),
|
|
17
|
+
display: true,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function detachmentMessage(
|
|
22
|
+
controllerContextAgent?: string,
|
|
23
|
+
board?: BoardBinding,
|
|
24
|
+
) {
|
|
25
|
+
return {
|
|
26
|
+
customType: "herdr-agent-detachment",
|
|
27
|
+
content: [
|
|
28
|
+
...(controllerContextAgent
|
|
29
|
+
? [
|
|
30
|
+
`Detached controller: ${controllerContextAgent}; checkpoints read-only, history unavailable; own context unchanged`,
|
|
31
|
+
]
|
|
32
|
+
: []),
|
|
33
|
+
...(board
|
|
34
|
+
? [`Board restored: ${board.boardId} as ${board.agentName}`]
|
|
35
|
+
: []),
|
|
36
|
+
].join("\n"),
|
|
37
|
+
display: true,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { hostname } from "node:os";
|
|
2
|
+
import { getCurrentPane, getSnapshot } from "../herdr.js";
|
|
3
|
+
import type { HerdrConnection } from "../herdr-client.js";
|
|
4
|
+
import type { PaneInfo, PeerMessage } from "../types.js";
|
|
5
|
+
|
|
6
|
+
const DELEGATED_WORKER_GUIDANCE =
|
|
7
|
+
"If blocked mid-run, use a question-asking tool, not peer messages. Without one, end with the blocker. Finish with an assistant reply, not a separate send report; never watch your parent.";
|
|
8
|
+
|
|
9
|
+
export function xml(value: string): string {
|
|
10
|
+
return value
|
|
11
|
+
.replaceAll("&", "&")
|
|
12
|
+
.replaceAll("<", "<")
|
|
13
|
+
.replaceAll(">", ">")
|
|
14
|
+
.replaceAll('"', """)
|
|
15
|
+
.replaceAll("'", "'");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface AgentEventLabels {
|
|
19
|
+
tab?: string;
|
|
20
|
+
workspace?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function agentSource(agent: PaneInfo, labels: AgentEventLabels) {
|
|
24
|
+
const paneName = agent.label || agent.title;
|
|
25
|
+
return {
|
|
26
|
+
pane: agent.pane_id,
|
|
27
|
+
workspace: agent.workspace_id,
|
|
28
|
+
tab: agent.tab_id,
|
|
29
|
+
...(agent.name ? { name: agent.name } : {}),
|
|
30
|
+
...(paneName ? { pane_name: paneName } : {}),
|
|
31
|
+
...(labels.workspace ? { workspace_name: labels.workspace } : {}),
|
|
32
|
+
...(labels.tab ? { tab_name: labels.tab } : {}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function sourceAttributes(
|
|
37
|
+
source: Record<string, string | undefined>,
|
|
38
|
+
): string {
|
|
39
|
+
return Object.entries(source)
|
|
40
|
+
.filter((entry): entry is [string, string] => entry[1] !== undefined)
|
|
41
|
+
.map(
|
|
42
|
+
([key, value]) =>
|
|
43
|
+
`${key}="${xml(value).replaceAll("\n", " ").replaceAll("\r", " ")}"`,
|
|
44
|
+
)
|
|
45
|
+
.join(" ");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function attributeAgentPrompt(
|
|
49
|
+
client: HerdrConnection,
|
|
50
|
+
message: string,
|
|
51
|
+
kind: "message" | "task",
|
|
52
|
+
): Promise<PeerMessage> {
|
|
53
|
+
const [pane, snapshot] = await Promise.all([
|
|
54
|
+
getCurrentPane(client),
|
|
55
|
+
getSnapshot(client),
|
|
56
|
+
]);
|
|
57
|
+
const workspace = snapshot.workspaces.find(
|
|
58
|
+
(workspace) => workspace.workspace_id === pane.workspace_id,
|
|
59
|
+
)?.label;
|
|
60
|
+
const tab = snapshot.tabs.find((tab) => tab.tab_id === pane.tab_id)?.label;
|
|
61
|
+
const source = {
|
|
62
|
+
kind,
|
|
63
|
+
host: hostname(),
|
|
64
|
+
session: process.env["HERDR_SESSION"] || "default",
|
|
65
|
+
...agentSource(pane, {
|
|
66
|
+
...(workspace ? { workspace } : {}),
|
|
67
|
+
...(tab ? { tab } : {}),
|
|
68
|
+
}),
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
sender: `<herdr_sender ${sourceAttributes(source)} />`,
|
|
72
|
+
text: message,
|
|
73
|
+
...(kind === "task" ? { context: DELEGATED_WORKER_GUIDANCE } : {}),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { readExtensionMessage } from "../remote/shepherdr-session.mjs";
|
|
2
|
+
import type { ExtensionMessage } from "../types.js";
|
|
3
|
+
export const AGENT_EVENT_MESSAGE_TYPE = "herdr-agent-event";
|
|
4
|
+
|
|
5
|
+
export interface AgentEventDetails {
|
|
6
|
+
extensionMessages?: ExtensionMessage[];
|
|
7
|
+
blockedOn?: string;
|
|
8
|
+
cwd?: string;
|
|
9
|
+
machine: string;
|
|
10
|
+
machineLabel?: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
paneId: string;
|
|
13
|
+
response?: string;
|
|
14
|
+
state: "blocked" | "failed" | "finished" | "extension-message";
|
|
15
|
+
tab?: string;
|
|
16
|
+
task?: string;
|
|
17
|
+
workspace?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function eventExtensionMessages(
|
|
21
|
+
value: unknown,
|
|
22
|
+
): ExtensionMessage[] | undefined {
|
|
23
|
+
if (!Array.isArray(value)) return undefined;
|
|
24
|
+
const messages = value.map((message) => {
|
|
25
|
+
if (typeof message !== "object" || message === null) return undefined;
|
|
26
|
+
return readExtensionMessage({ ...message, version: 1, state: "pending" });
|
|
27
|
+
});
|
|
28
|
+
return messages.every((message) => message !== undefined)
|
|
29
|
+
? messages
|
|
30
|
+
: undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function eventDetails(value: unknown): AgentEventDetails | undefined {
|
|
34
|
+
if (typeof value !== "object" || value === null) return undefined;
|
|
35
|
+
const details = value as Record<string, unknown>;
|
|
36
|
+
if (
|
|
37
|
+
typeof details["paneId"] !== "string" ||
|
|
38
|
+
(details["machine"] !== undefined &&
|
|
39
|
+
typeof details["machine"] !== "string") ||
|
|
40
|
+
(details["state"] !== "blocked" &&
|
|
41
|
+
details["state"] !== "extension-message" &&
|
|
42
|
+
details["state"] !== "failed" &&
|
|
43
|
+
details["state"] !== "finished")
|
|
44
|
+
) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
const optional = (field: keyof AgentEventDetails) =>
|
|
48
|
+
typeof details[field] === "string"
|
|
49
|
+
? { [field]: details[field] as string }
|
|
50
|
+
: {};
|
|
51
|
+
const extensionMessages = eventExtensionMessages(
|
|
52
|
+
details["extensionMessages"],
|
|
53
|
+
);
|
|
54
|
+
return {
|
|
55
|
+
machine:
|
|
56
|
+
typeof details["machine"] === "string" ? details["machine"] : "local",
|
|
57
|
+
paneId: details["paneId"],
|
|
58
|
+
state: details["state"],
|
|
59
|
+
...(extensionMessages ? { extensionMessages } : {}),
|
|
60
|
+
...optional("blockedOn"),
|
|
61
|
+
...optional("cwd"),
|
|
62
|
+
...optional("machineLabel"),
|
|
63
|
+
...optional("name"),
|
|
64
|
+
...optional("response"),
|
|
65
|
+
...optional("tab"),
|
|
66
|
+
...optional("task"),
|
|
67
|
+
...optional("workspace"),
|
|
68
|
+
};
|
|
69
|
+
}
|