@howaboua/pi-shepherdr 0.2.24 → 0.2.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/README.md +20 -12
- package/index.ts +2 -2
- package/package.json +10 -11
- package/src/agent-identity.ts +75 -0
- package/src/agents-contract.ts +20 -12
- package/src/agents-discovery.ts +79 -34
- package/src/agents-focus.ts +15 -9
- package/src/agents-spawn.ts +25 -7
- package/src/agents-tool.ts +35 -7
- package/src/agents-work.ts +2 -2
- package/src/attachment-notes.ts +25 -24
- package/src/attachment-protocol.ts +35 -15
- package/src/attachment-routing.ts +28 -24
- package/src/attachment-target.ts +58 -19
- package/src/attachment.ts +13 -17
- package/src/board/host.ts +19 -20
- package/src/board/membership.ts +49 -30
- package/src/close-idle.ts +89 -0
- package/src/context-routing.ts +196 -0
- package/src/control-panel/index.ts +3 -1
- package/src/control-panel/items.ts +14 -2
- package/src/control-panel/view.ts +6 -2
- package/src/controller.ts +11 -2
- package/src/core/AGENTS.md +5 -0
- package/src/core/backend.ts +266 -0
- package/src/core/client.ts +249 -0
- package/src/core/context-relay.mjs +111 -0
- package/src/core/focus-channel-helper.mjs +165 -0
- package/src/core/focus-channel-stream.ts +140 -0
- package/src/core/focus-channel-wire.mjs +160 -0
- package/src/core/focus-channel.ts +213 -0
- package/src/core/focus-window.ts +192 -0
- package/src/core/pi-executable.mjs +34 -0
- package/src/core/protocol.ts +116 -0
- package/src/core/registration.ts +51 -0
- package/src/core/remote-endpoint.ts +119 -0
- package/src/core/remote-exec.ts +122 -0
- package/src/core/remote-helper.mjs +244 -0
- package/src/core/remote-reader.ts +67 -0
- package/src/core/remote-relay.ts +191 -0
- package/src/core/request.ts +124 -0
- package/src/core/spawn.ts +177 -0
- package/src/core/status.ts +68 -0
- package/src/core/terminal.ts +103 -0
- package/src/fleet/catalog.ts +83 -0
- package/src/fleet/connection.ts +111 -0
- package/src/fleet/index.ts +161 -122
- package/src/fleet/inspection.ts +19 -1
- package/src/fleet/machine.ts +3 -0
- package/src/fleet/monitor.ts +18 -4
- package/src/fleet/persistence.ts +26 -3
- package/src/fleet/setup.ts +54 -13
- package/src/herdr.ts +25 -5
- package/src/launch-contract.ts +48 -0
- package/src/launch-location.ts +151 -0
- package/src/launch.ts +30 -183
- package/src/messages/attribution.ts +15 -6
- package/src/messages/events.ts +10 -4
- package/src/messages/focus.ts +11 -2
- package/src/messages/guidance.ts +22 -16
- package/src/monitor-extension-messages.ts +9 -2
- package/src/monitor-record.ts +13 -6
- package/src/monitor-state.ts +18 -4
- package/src/monitor.ts +31 -0
- package/src/peer-commands.ts +11 -0
- package/src/peer-inbox-delivery.ts +171 -0
- package/src/peer-inbox-focus.ts +113 -0
- package/src/peer-inbox.ts +126 -208
- package/src/remote/deployment.ts +1 -1
- package/src/remote/shepherdr-checkpoints.mjs +20 -2
- package/src/remote/shepherdr-context.mjs +15 -3
- package/src/remote/shepherdr-peer.mjs +18 -5
- package/src/remote/shepherdr-session.mjs +9 -2
- package/src/remote/shepherdr.mjs +1 -1
- package/src/settlement/collector.ts +2 -2
- package/src/settlement/index.ts +12 -0
- package/src/shared-context-host.ts +7 -18
- package/src/shared-context.ts +139 -12
- package/src/spawn-context.ts +10 -5
- package/src/spawn-ownership.ts +91 -0
- package/src/types.ts +10 -5
- package/src/worker-ownership.ts +92 -0
- package/src/worker-service.ts +13 -82
- package/changelog.ts +0 -337
package/src/herdr.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CoreConnection } from "./core/backend.js";
|
|
1
2
|
import { type HerdrConnection, isHerdrErrorCode } from "./herdr-client.js";
|
|
2
3
|
import type {
|
|
3
4
|
AgentStatus,
|
|
@@ -49,9 +50,14 @@ export async function getPane(
|
|
|
49
50
|
export async function getCurrentPane(
|
|
50
51
|
client: HerdrConnection,
|
|
51
52
|
): Promise<PaneInfo> {
|
|
52
|
-
const callerPaneId =
|
|
53
|
+
const callerPaneId =
|
|
54
|
+
client instanceof CoreConnection
|
|
55
|
+
? process.env["HC_TERMINAL_ID"]
|
|
56
|
+
: process.env["HERDR_PANE_ID"];
|
|
53
57
|
if (!callerPaneId)
|
|
54
|
-
throw new Error(
|
|
58
|
+
throw new Error(
|
|
59
|
+
"Cannot identify this terminal; run Pi inside Herdr and retry",
|
|
60
|
+
);
|
|
55
61
|
const result = record(
|
|
56
62
|
await client.request<unknown>("pane.current", {
|
|
57
63
|
caller_pane_id: callerPaneId,
|
|
@@ -64,7 +70,9 @@ export async function getCurrentPane(
|
|
|
64
70
|
export async function resolvePiAgent(
|
|
65
71
|
client: HerdrConnection,
|
|
66
72
|
target: string,
|
|
67
|
-
controllingPaneId =
|
|
73
|
+
controllingPaneId = client instanceof CoreConnection
|
|
74
|
+
? process.env["HC_TERMINAL_ID"]
|
|
75
|
+
: process.env["HERDR_PANE_ID"],
|
|
68
76
|
): Promise<PaneInfo> {
|
|
69
77
|
const agent = await getAgent(client, target).catch((error: unknown) => {
|
|
70
78
|
if (error instanceof Error && isHerdrErrorCode(error, "agent_not_found")) {
|
|
@@ -108,10 +116,22 @@ export function sessionPath(agent: PaneInfo): string | undefined {
|
|
|
108
116
|
export function parsePaneInfo(value: unknown, path = "pane"): PaneInfo {
|
|
109
117
|
const pane = record(value, path);
|
|
110
118
|
return {
|
|
119
|
+
...(pane["backend"] === "core" ? { backend: "core" as const } : {}),
|
|
111
120
|
pane_id: requiredString(pane, "pane_id", path),
|
|
112
121
|
terminal_id: requiredString(pane, "terminal_id", path),
|
|
113
|
-
|
|
114
|
-
|
|
122
|
+
...(pane["backend"] === "core"
|
|
123
|
+
? {
|
|
124
|
+
...(typeof pane["workspace_id"] === "string"
|
|
125
|
+
? { workspace_id: pane["workspace_id"] }
|
|
126
|
+
: {}),
|
|
127
|
+
...(typeof pane["tab_id"] === "string"
|
|
128
|
+
? { tab_id: pane["tab_id"] }
|
|
129
|
+
: {}),
|
|
130
|
+
}
|
|
131
|
+
: {
|
|
132
|
+
workspace_id: requiredString(pane, "workspace_id", path),
|
|
133
|
+
tab_id: requiredString(pane, "tab_id", path),
|
|
134
|
+
}),
|
|
115
135
|
agent_status: parseAgentStatus(
|
|
116
136
|
pane["agent_status"],
|
|
117
137
|
`${path}.agent_status`,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { PaneInfo } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export const START_PLACEMENTS = [
|
|
4
|
+
"new_workspace",
|
|
5
|
+
"new_tab",
|
|
6
|
+
"pane",
|
|
7
|
+
"terminal",
|
|
8
|
+
] as const;
|
|
9
|
+
|
|
10
|
+
export interface StartAgentParams {
|
|
11
|
+
cwd?: string;
|
|
12
|
+
label?: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
pane?: string;
|
|
15
|
+
placement?: (typeof START_PLACEMENTS)[number];
|
|
16
|
+
workspace?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface CreatedLocationCleanup {
|
|
20
|
+
id: string;
|
|
21
|
+
method: "tab.close" | "workspace.close" | "terminal.close";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface StartedAgent {
|
|
25
|
+
agent: PaneInfo;
|
|
26
|
+
cleanup?: CreatedLocationCleanup;
|
|
27
|
+
id: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class RolledBackAgentLaunchError extends Error {
|
|
31
|
+
constructor(cause: unknown) {
|
|
32
|
+
super(cause instanceof Error ? cause.message : String(cause), { cause });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class UnstartedAgentLaunchError extends Error {
|
|
37
|
+
constructor(cause: unknown) {
|
|
38
|
+
super(cause instanceof Error ? cause.message : String(cause), { cause });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function requiredStartField(
|
|
43
|
+
value: string | undefined,
|
|
44
|
+
field: string,
|
|
45
|
+
): string {
|
|
46
|
+
if (!value?.trim()) throw new Error(`${field} is required for start`);
|
|
47
|
+
return value.trim();
|
|
48
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { getSnapshot, parsePaneInfo, resolveWorkspace } from "./herdr.js";
|
|
2
|
+
import type { HerdrConnection } from "./herdr-client.js";
|
|
3
|
+
import {
|
|
4
|
+
type CreatedLocationCleanup,
|
|
5
|
+
RolledBackAgentLaunchError,
|
|
6
|
+
requiredStartField,
|
|
7
|
+
type StartAgentParams,
|
|
8
|
+
UnstartedAgentLaunchError,
|
|
9
|
+
} from "./launch-contract.js";
|
|
10
|
+
import type { PaneInfo } from "./types.js";
|
|
11
|
+
|
|
12
|
+
export async function createStartPane(
|
|
13
|
+
client: HerdrConnection,
|
|
14
|
+
params: StartAgentParams,
|
|
15
|
+
cwd: string,
|
|
16
|
+
label: string,
|
|
17
|
+
): Promise<{
|
|
18
|
+
cleanup?: CreatedLocationCleanup;
|
|
19
|
+
paneId: string;
|
|
20
|
+
}> {
|
|
21
|
+
const placement = params.placement;
|
|
22
|
+
if (!placement) throw new Error("placement is required for start");
|
|
23
|
+
if (placement === "terminal")
|
|
24
|
+
throw new UnstartedAgentLaunchError(
|
|
25
|
+
"terminal placement requires Core; use new_tab or new_workspace in legacy Herdr",
|
|
26
|
+
);
|
|
27
|
+
if (placement === "pane") {
|
|
28
|
+
return { paneId: requiredStartField(params.pane, "pane") };
|
|
29
|
+
}
|
|
30
|
+
if (placement === "new_tab") {
|
|
31
|
+
const workspaceTarget = requiredStartField(params.workspace, "workspace");
|
|
32
|
+
let workspace;
|
|
33
|
+
try {
|
|
34
|
+
workspace = resolveWorkspace(await getSnapshot(client), workspaceTarget);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
throw new UnstartedAgentLaunchError(error);
|
|
37
|
+
}
|
|
38
|
+
// A failed create response may still have created a location.
|
|
39
|
+
const created = createdLocation(
|
|
40
|
+
await client.request<unknown>("tab.create", {
|
|
41
|
+
workspace_id: workspace.workspace_id,
|
|
42
|
+
cwd,
|
|
43
|
+
label,
|
|
44
|
+
focus: false,
|
|
45
|
+
}),
|
|
46
|
+
"tab.create result",
|
|
47
|
+
);
|
|
48
|
+
return {
|
|
49
|
+
paneId: created.root_pane.pane_id,
|
|
50
|
+
cleanup: { method: "tab.close", id: created.tab.tab_id },
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
const created = createdLocation(
|
|
54
|
+
await client.request<unknown>("workspace.create", {
|
|
55
|
+
cwd,
|
|
56
|
+
focus: false,
|
|
57
|
+
}),
|
|
58
|
+
"workspace.create result",
|
|
59
|
+
true,
|
|
60
|
+
);
|
|
61
|
+
try {
|
|
62
|
+
await client.request("tab.rename", { tab_id: created.tab.tab_id, label });
|
|
63
|
+
} catch (error) {
|
|
64
|
+
return rollbackCreatedLocation(
|
|
65
|
+
client,
|
|
66
|
+
{ method: "workspace.close", id: created.workspace.workspace_id },
|
|
67
|
+
error,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
paneId: created.root_pane.pane_id,
|
|
72
|
+
cleanup: { method: "workspace.close", id: created.workspace.workspace_id },
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function createdLocation(
|
|
77
|
+
value: unknown,
|
|
78
|
+
path: string,
|
|
79
|
+
): { root_pane: PaneInfo; tab: { tab_id: string } };
|
|
80
|
+
function createdLocation(
|
|
81
|
+
value: unknown,
|
|
82
|
+
path: string,
|
|
83
|
+
withWorkspace: true,
|
|
84
|
+
): {
|
|
85
|
+
root_pane: PaneInfo;
|
|
86
|
+
tab: { tab_id: string };
|
|
87
|
+
workspace: { workspace_id: string };
|
|
88
|
+
};
|
|
89
|
+
function createdLocation(
|
|
90
|
+
value: unknown,
|
|
91
|
+
path: string,
|
|
92
|
+
withWorkspace = false,
|
|
93
|
+
): {
|
|
94
|
+
root_pane: PaneInfo;
|
|
95
|
+
tab: { tab_id: string };
|
|
96
|
+
workspace?: { workspace_id: string };
|
|
97
|
+
} {
|
|
98
|
+
if (typeof value !== "object" || value === null) {
|
|
99
|
+
throw new Error(`Herdr ${path} must be an object`);
|
|
100
|
+
}
|
|
101
|
+
const result = value as Record<string, unknown>;
|
|
102
|
+
const tabId = nestedId(result["tab"], "tab_id", `${path}.tab`);
|
|
103
|
+
return {
|
|
104
|
+
root_pane: parsePaneInfo(result["root_pane"], `${path}.root_pane`),
|
|
105
|
+
tab: { tab_id: tabId },
|
|
106
|
+
...(withWorkspace
|
|
107
|
+
? {
|
|
108
|
+
workspace: {
|
|
109
|
+
workspace_id: nestedId(
|
|
110
|
+
result["workspace"],
|
|
111
|
+
"workspace_id",
|
|
112
|
+
`${path}.workspace`,
|
|
113
|
+
),
|
|
114
|
+
},
|
|
115
|
+
}
|
|
116
|
+
: {}),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function nestedId(value: unknown, key: string, path: string): string {
|
|
121
|
+
if (typeof value !== "object" || value === null) {
|
|
122
|
+
throw new Error(`Herdr ${path} must be an object`);
|
|
123
|
+
}
|
|
124
|
+
const id = (value as Record<string, unknown>)[key];
|
|
125
|
+
if (typeof id !== "string")
|
|
126
|
+
throw new Error(`Herdr ${path}.${key} must be string`);
|
|
127
|
+
return id;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export async function rollbackCreatedLocation(
|
|
131
|
+
client: HerdrConnection,
|
|
132
|
+
cleanup: CreatedLocationCleanup | undefined,
|
|
133
|
+
cause: unknown,
|
|
134
|
+
): Promise<never> {
|
|
135
|
+
if (!cleanup) throw cause;
|
|
136
|
+
try {
|
|
137
|
+
await client.request(cleanup.method, {
|
|
138
|
+
[cleanup.method === "tab.close"
|
|
139
|
+
? "tab_id"
|
|
140
|
+
: cleanup.method === "terminal.close"
|
|
141
|
+
? "id"
|
|
142
|
+
: "workspace_id"]: cleanup.id,
|
|
143
|
+
});
|
|
144
|
+
} catch (cleanupError) {
|
|
145
|
+
throw new Error(
|
|
146
|
+
`${cause instanceof Error ? cause.message : String(cause)}; rollback also failed: ${cleanupError instanceof Error ? cleanupError.message : String(cleanupError)}`,
|
|
147
|
+
{ cause },
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
throw new RolledBackAgentLaunchError(cause);
|
|
151
|
+
}
|
package/src/launch.ts
CHANGED
|
@@ -1,34 +1,27 @@
|
|
|
1
1
|
import { stat } from "node:fs/promises";
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import { parseArgs } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
getSnapshot,
|
|
8
|
-
parsePaneInfo,
|
|
9
|
-
resolveWorkspace,
|
|
10
|
-
sessionPath,
|
|
11
|
-
} from "./herdr.js";
|
|
4
|
+
import { CoreConnection } from "./core/backend.js";
|
|
5
|
+
import { startCoreAgent } from "./core/spawn.js";
|
|
6
|
+
import { getAgent, getPane, parsePaneInfo, sessionPath } from "./herdr.js";
|
|
12
7
|
import { type HerdrConnection, isHerdrErrorCode } from "./herdr-client.js";
|
|
8
|
+
import {
|
|
9
|
+
requiredStartField,
|
|
10
|
+
type StartAgentParams,
|
|
11
|
+
type StartedAgent,
|
|
12
|
+
UnstartedAgentLaunchError,
|
|
13
|
+
} from "./launch-contract.js";
|
|
14
|
+
import { createStartPane, rollbackCreatedLocation } from "./launch-location.js";
|
|
13
15
|
import type { PaneInfo } from "./types.js";
|
|
14
16
|
import { WORKER_FLAG } from "./worker-role.js";
|
|
15
17
|
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
+
export {
|
|
19
|
+
RolledBackAgentLaunchError,
|
|
20
|
+
START_PLACEMENTS,
|
|
21
|
+
UnstartedAgentLaunchError,
|
|
22
|
+
} from "./launch-contract.js";
|
|
18
23
|
|
|
19
|
-
|
|
20
|
-
cwd?: string;
|
|
21
|
-
label?: string;
|
|
22
|
-
name?: string;
|
|
23
|
-
pane?: string;
|
|
24
|
-
placement?: (typeof START_PLACEMENTS)[number];
|
|
25
|
-
workspace?: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function required(value: string | undefined, field: string): string {
|
|
29
|
-
if (!value?.trim()) throw new Error(`${field} is required for start`);
|
|
30
|
-
return value.trim();
|
|
31
|
-
}
|
|
24
|
+
const AGENT_NAME = /^[a-z][a-z0-9_-]{0,31}$/;
|
|
32
25
|
|
|
33
26
|
async function directory(
|
|
34
27
|
value: string | undefined,
|
|
@@ -66,10 +59,14 @@ export async function resolvePreparationDirectory(
|
|
|
66
59
|
fallbackCwd: string,
|
|
67
60
|
resolveDirectory: DirectoryResolver = directory,
|
|
68
61
|
): Promise<string> {
|
|
62
|
+
if (client instanceof CoreConnection && params.placement === "pane")
|
|
63
|
+
throw new UnstartedAgentLaunchError(
|
|
64
|
+
"Core cannot start Pi in an existing terminal without terminal input; use terminal, new_tab or new_workspace placement",
|
|
65
|
+
);
|
|
69
66
|
if (params.placement !== "pane") {
|
|
70
67
|
return resolveStartDirectory(params, fallbackCwd, resolveDirectory);
|
|
71
68
|
}
|
|
72
|
-
const pane = await getPane(client,
|
|
69
|
+
const pane = await getPane(client, requiredStartField(params.pane, "pane"));
|
|
73
70
|
return pane.foreground_cwd?.trim() || pane.cwd?.trim() || fallbackCwd;
|
|
74
71
|
}
|
|
75
72
|
|
|
@@ -77,143 +74,6 @@ interface StartAgentOptions {
|
|
|
77
74
|
agentArgs?: string[];
|
|
78
75
|
}
|
|
79
76
|
|
|
80
|
-
interface CreatedLocationCleanup {
|
|
81
|
-
id: string;
|
|
82
|
-
method: "tab.close" | "workspace.close";
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export interface StartedAgent {
|
|
86
|
-
agent: PaneInfo;
|
|
87
|
-
cleanup?: CreatedLocationCleanup;
|
|
88
|
-
id: string;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export class RolledBackAgentLaunchError extends Error {
|
|
92
|
-
constructor(cause: unknown) {
|
|
93
|
-
super(cause instanceof Error ? cause.message : String(cause), { cause });
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export class UnstartedAgentLaunchError extends Error {
|
|
98
|
-
constructor(cause: unknown) {
|
|
99
|
-
super(cause instanceof Error ? cause.message : String(cause), { cause });
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async function createStartPane(
|
|
104
|
-
client: HerdrConnection,
|
|
105
|
-
params: StartAgentParams,
|
|
106
|
-
cwd: string,
|
|
107
|
-
label: string,
|
|
108
|
-
): Promise<{
|
|
109
|
-
cleanup?: CreatedLocationCleanup;
|
|
110
|
-
paneId: string;
|
|
111
|
-
}> {
|
|
112
|
-
const placement = params.placement;
|
|
113
|
-
if (!placement) throw new Error("placement is required for start");
|
|
114
|
-
if (placement === "pane") {
|
|
115
|
-
return { paneId: required(params.pane, "pane") };
|
|
116
|
-
}
|
|
117
|
-
if (placement === "new_tab") {
|
|
118
|
-
const workspaceTarget = required(params.workspace, "workspace");
|
|
119
|
-
let workspace;
|
|
120
|
-
try {
|
|
121
|
-
workspace = resolveWorkspace(await getSnapshot(client), workspaceTarget);
|
|
122
|
-
} catch (error) {
|
|
123
|
-
throw new UnstartedAgentLaunchError(error);
|
|
124
|
-
}
|
|
125
|
-
// A failed create response may still have created a location.
|
|
126
|
-
const created = createdLocation(
|
|
127
|
-
await client.request<unknown>("tab.create", {
|
|
128
|
-
workspace_id: workspace.workspace_id,
|
|
129
|
-
cwd,
|
|
130
|
-
label,
|
|
131
|
-
focus: false,
|
|
132
|
-
}),
|
|
133
|
-
"tab.create result",
|
|
134
|
-
);
|
|
135
|
-
return {
|
|
136
|
-
paneId: created.root_pane.pane_id,
|
|
137
|
-
cleanup: { method: "tab.close", id: created.tab.tab_id },
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
const created = createdLocation(
|
|
141
|
-
await client.request<unknown>("workspace.create", {
|
|
142
|
-
cwd,
|
|
143
|
-
focus: false,
|
|
144
|
-
}),
|
|
145
|
-
"workspace.create result",
|
|
146
|
-
true,
|
|
147
|
-
);
|
|
148
|
-
try {
|
|
149
|
-
await client.request("tab.rename", { tab_id: created.tab.tab_id, label });
|
|
150
|
-
} catch (error) {
|
|
151
|
-
return rollbackCreatedLocation(
|
|
152
|
-
client,
|
|
153
|
-
{ method: "workspace.close", id: created.workspace.workspace_id },
|
|
154
|
-
error,
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
return {
|
|
158
|
-
paneId: created.root_pane.pane_id,
|
|
159
|
-
cleanup: { method: "workspace.close", id: created.workspace.workspace_id },
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function createdLocation(
|
|
164
|
-
value: unknown,
|
|
165
|
-
path: string,
|
|
166
|
-
): { root_pane: PaneInfo; tab: { tab_id: string } };
|
|
167
|
-
function createdLocation(
|
|
168
|
-
value: unknown,
|
|
169
|
-
path: string,
|
|
170
|
-
withWorkspace: true,
|
|
171
|
-
): {
|
|
172
|
-
root_pane: PaneInfo;
|
|
173
|
-
tab: { tab_id: string };
|
|
174
|
-
workspace: { workspace_id: string };
|
|
175
|
-
};
|
|
176
|
-
function createdLocation(
|
|
177
|
-
value: unknown,
|
|
178
|
-
path: string,
|
|
179
|
-
withWorkspace = false,
|
|
180
|
-
): {
|
|
181
|
-
root_pane: PaneInfo;
|
|
182
|
-
tab: { tab_id: string };
|
|
183
|
-
workspace?: { workspace_id: string };
|
|
184
|
-
} {
|
|
185
|
-
if (typeof value !== "object" || value === null) {
|
|
186
|
-
throw new Error(`Herdr ${path} must be an object`);
|
|
187
|
-
}
|
|
188
|
-
const result = value as Record<string, unknown>;
|
|
189
|
-
const tabId = nestedId(result["tab"], "tab_id", `${path}.tab`);
|
|
190
|
-
return {
|
|
191
|
-
root_pane: parsePaneInfo(result["root_pane"], `${path}.root_pane`),
|
|
192
|
-
tab: { tab_id: tabId },
|
|
193
|
-
...(withWorkspace
|
|
194
|
-
? {
|
|
195
|
-
workspace: {
|
|
196
|
-
workspace_id: nestedId(
|
|
197
|
-
result["workspace"],
|
|
198
|
-
"workspace_id",
|
|
199
|
-
`${path}.workspace`,
|
|
200
|
-
),
|
|
201
|
-
},
|
|
202
|
-
}
|
|
203
|
-
: {}),
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function nestedId(value: unknown, key: string, path: string): string {
|
|
208
|
-
if (typeof value !== "object" || value === null) {
|
|
209
|
-
throw new Error(`Herdr ${path} must be an object`);
|
|
210
|
-
}
|
|
211
|
-
const id = (value as Record<string, unknown>)[key];
|
|
212
|
-
if (typeof id !== "string")
|
|
213
|
-
throw new Error(`Herdr ${path}.${key} must be string`);
|
|
214
|
-
return id;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
77
|
export async function startAgent(
|
|
218
78
|
client: HerdrConnection,
|
|
219
79
|
params: StartAgentParams,
|
|
@@ -221,7 +81,7 @@ export async function startAgent(
|
|
|
221
81
|
resolveDirectory: DirectoryResolver = directory,
|
|
222
82
|
options: StartAgentOptions = {},
|
|
223
83
|
): Promise<StartedAgent> {
|
|
224
|
-
const name =
|
|
84
|
+
const name = requiredStartField(params.name, "name");
|
|
225
85
|
if (!AGENT_NAME.test(name)) {
|
|
226
86
|
throw new Error(
|
|
227
87
|
"name must be 1–32 characters, start with a lowercase letter, and contain only lowercase letters, digits, underscores or hyphens. Omit name to generate it from label; label can be longer.",
|
|
@@ -250,12 +110,18 @@ export async function startAgent(
|
|
|
250
110
|
"cwd cannot change an existing pane; prepare the pane through Herdr",
|
|
251
111
|
);
|
|
252
112
|
}
|
|
113
|
+
if (client instanceof CoreConnection && params.placement === "pane")
|
|
114
|
+
throw new UnstartedAgentLaunchError(
|
|
115
|
+
"Core cannot start Pi in an existing terminal without terminal input; use terminal, new_tab or new_workspace placement",
|
|
116
|
+
);
|
|
253
117
|
let cwd: string;
|
|
254
118
|
try {
|
|
255
119
|
cwd = await resolveStartDirectory(params, fallbackCwd, resolveDirectory);
|
|
256
120
|
} catch (error) {
|
|
257
121
|
throw new UnstartedAgentLaunchError(error);
|
|
258
122
|
}
|
|
123
|
+
if (client instanceof CoreConnection)
|
|
124
|
+
return startCoreAgent(client, params, cwd, label, args);
|
|
259
125
|
const created = await createStartPane(client, params, cwd, label);
|
|
260
126
|
let agent: PaneInfo;
|
|
261
127
|
try {
|
|
@@ -284,25 +150,6 @@ export async function rollbackStartedAgent(
|
|
|
284
150
|
return rollbackCreatedLocation(client, started.cleanup, cause);
|
|
285
151
|
}
|
|
286
152
|
|
|
287
|
-
async function rollbackCreatedLocation(
|
|
288
|
-
client: HerdrConnection,
|
|
289
|
-
cleanup: CreatedLocationCleanup | undefined,
|
|
290
|
-
cause: unknown,
|
|
291
|
-
): Promise<never> {
|
|
292
|
-
if (!cleanup) throw cause;
|
|
293
|
-
try {
|
|
294
|
-
await client.request(cleanup.method, {
|
|
295
|
-
[cleanup.method === "tab.close" ? "tab_id" : "workspace_id"]: cleanup.id,
|
|
296
|
-
});
|
|
297
|
-
} catch (cleanupError) {
|
|
298
|
-
throw new Error(
|
|
299
|
-
`${cause instanceof Error ? cause.message : String(cause)}; rollback also failed: ${cleanupError instanceof Error ? cleanupError.message : String(cleanupError)}`,
|
|
300
|
-
{ cause },
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
throw new RolledBackAgentLaunchError(cause);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
153
|
async function startWhenShellReady(
|
|
307
154
|
client: HerdrConnection,
|
|
308
155
|
params: Record<string, unknown>,
|
|
@@ -331,11 +178,11 @@ async function startWhenShellReady(
|
|
|
331
178
|
}
|
|
332
179
|
}
|
|
333
180
|
|
|
334
|
-
const name =
|
|
181
|
+
const name = requiredStartField(
|
|
335
182
|
typeof params["name"] === "string" ? params["name"] : undefined,
|
|
336
183
|
"name",
|
|
337
184
|
);
|
|
338
|
-
const paneId =
|
|
185
|
+
const paneId = requiredStartField(
|
|
339
186
|
typeof params["pane_id"] === "string" ? params["pane_id"] : undefined,
|
|
340
187
|
"pane_id",
|
|
341
188
|
);
|
|
@@ -20,12 +20,17 @@ export interface AgentEventLabels {
|
|
|
20
20
|
workspace?: string;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export function agentSource(
|
|
23
|
+
export function agentSource(
|
|
24
|
+
agent: PaneInfo,
|
|
25
|
+
labels: AgentEventLabels,
|
|
26
|
+
): Record<string, string | undefined> {
|
|
24
27
|
const paneName = agent.label || agent.title;
|
|
25
28
|
return {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
...(agent.backend === "core"
|
|
30
|
+
? { terminal: agent.terminal_id }
|
|
31
|
+
: { pane: agent.pane_id }),
|
|
32
|
+
...(agent.workspace_id ? { workspace: agent.workspace_id } : {}),
|
|
33
|
+
...(agent.tab_id ? { tab: agent.tab_id } : {}),
|
|
29
34
|
...(agent.name ? { name: agent.name } : {}),
|
|
30
35
|
...(paneName ? { pane_name: paneName } : {}),
|
|
31
36
|
...(labels.workspace ? { workspace_name: labels.workspace } : {}),
|
|
@@ -60,8 +65,12 @@ export async function attributeAgentPrompt(
|
|
|
60
65
|
const tab = snapshot.tabs.find((tab) => tab.tab_id === pane.tab_id)?.label;
|
|
61
66
|
const source = {
|
|
62
67
|
kind,
|
|
63
|
-
|
|
64
|
-
|
|
68
|
+
...(pane.backend === "core"
|
|
69
|
+
? await client.machineIdentity()
|
|
70
|
+
: {
|
|
71
|
+
host: hostname(),
|
|
72
|
+
session: process.env["HERDR_SESSION"] || "default",
|
|
73
|
+
}),
|
|
65
74
|
...agentSource(pane, {
|
|
66
75
|
...(workspace ? { workspace } : {}),
|
|
67
76
|
...(tab ? { tab } : {}),
|
package/src/messages/events.ts
CHANGED
|
@@ -47,8 +47,9 @@ function blockedOperatorHint(
|
|
|
47
47
|
machine: string,
|
|
48
48
|
operatorPrefix: string,
|
|
49
49
|
paneId: string,
|
|
50
|
+
core: boolean,
|
|
50
51
|
): string {
|
|
51
|
-
return `Inspect first with ${operatorCommand(operatorPrefix, `agent read ${paneId} --source visible`)}; respond through ${agentToolName} with machine=${JSON.stringify(machine)}, or use ${operatorCommand(operatorPrefix, `agent send-keys ${paneId} <keys>`)} for interactive controls.`;
|
|
52
|
+
return `Inspect first with ${operatorCommand(operatorPrefix, core ? `read ${paneId}` : `agent read ${paneId} --source visible`)}; respond through ${agentToolName} with machine=${JSON.stringify(machine)}, or use ${operatorCommand(operatorPrefix, core ? `keys ${paneId} <keys>` : `agent send-keys ${paneId} <keys>`)} for interactive controls.`;
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
function failedOperatorHint(
|
|
@@ -56,8 +57,9 @@ function failedOperatorHint(
|
|
|
56
57
|
machine: string,
|
|
57
58
|
operatorPrefix: string,
|
|
58
59
|
paneId: string,
|
|
60
|
+
core: boolean,
|
|
59
61
|
): string {
|
|
60
|
-
return `Inspect with ${operatorCommand(operatorPrefix, `agent read ${paneId} --source recent-unwrapped --lines 80`)} and assess the failure. If this task has not already been retried and one simple corrective prompt could recover it, try once through ${agentToolName} with machine=${JSON.stringify(machine)}. If it fails again or the setup looks broken, stop retrying and tell the user.`;
|
|
62
|
+
return `Inspect with ${operatorCommand(operatorPrefix, core ? `read ${paneId} --lines 80` : `agent read ${paneId} --source recent-unwrapped --lines 80`)} and assess the failure. If this task has not already been retried and one simple corrective prompt could recover it, try once through ${agentToolName} with machine=${JSON.stringify(machine)}. If it fails again or the setup looks broken, stop retrying and tell the user.`;
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
function agentEvent(options: AgentEventOptions): {
|
|
@@ -82,6 +84,10 @@ function agentEvent(options: AgentEventOptions): {
|
|
|
82
84
|
const blocked = status === "blocked";
|
|
83
85
|
const notification = status === "extension-message";
|
|
84
86
|
const failed = !blocked && reply?.stopReason === "error";
|
|
87
|
+
const operatorTarget =
|
|
88
|
+
agent.backend === "core" && machine !== "local"
|
|
89
|
+
? `${machineLabel}/${agent.pane_id}`
|
|
90
|
+
: agent.pane_id;
|
|
85
91
|
const cwd = agent.foreground_cwd ?? agent.cwd ?? record.cwd;
|
|
86
92
|
const tag = notification
|
|
87
93
|
? "herdr_agent_extension_message"
|
|
@@ -121,12 +127,12 @@ function agentEvent(options: AgentEventOptions): {
|
|
|
121
127
|
}
|
|
122
128
|
if (blocked) {
|
|
123
129
|
lines.push(
|
|
124
|
-
`<operator_hint>${xml(blockedOperatorHint(agentToolName, machine, operatorPrefix, agent.
|
|
130
|
+
`<operator_hint>${xml(blockedOperatorHint(agentToolName, machine, operatorPrefix, operatorTarget, agent.backend === "core"))}</operator_hint>`,
|
|
125
131
|
);
|
|
126
132
|
}
|
|
127
133
|
if (failed) {
|
|
128
134
|
lines.push(
|
|
129
|
-
`<operator_hint>${xml(failedOperatorHint(agentToolName, machine, operatorPrefix, agent.
|
|
135
|
+
`<operator_hint>${xml(failedOperatorHint(agentToolName, machine, operatorPrefix, operatorTarget, agent.backend === "core"))}</operator_hint>`,
|
|
130
136
|
);
|
|
131
137
|
}
|
|
132
138
|
if (reply) lines.push(`<response>${xml(reply.text)}</response>`);
|
package/src/messages/focus.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import type { FocusRequest } from "../types.js";
|
|
2
2
|
|
|
3
3
|
function focusOrigin({ source }: FocusRequest) {
|
|
4
|
-
return {
|
|
4
|
+
return {
|
|
5
|
+
host: source.host,
|
|
6
|
+
session: source.session,
|
|
7
|
+
...(source.backend === "core"
|
|
8
|
+
? { terminal: source.terminal }
|
|
9
|
+
: { pane: source.pane }),
|
|
10
|
+
};
|
|
5
11
|
}
|
|
6
12
|
|
|
7
13
|
export function focusArrivalContent(
|
|
@@ -11,7 +17,10 @@ export function focusArrivalContent(
|
|
|
11
17
|
) {
|
|
12
18
|
const returnFocus = {
|
|
13
19
|
action: "focus",
|
|
14
|
-
target:
|
|
20
|
+
target:
|
|
21
|
+
request.source.backend === "core"
|
|
22
|
+
? request.source.terminal
|
|
23
|
+
: request.source.pane!,
|
|
15
24
|
machine,
|
|
16
25
|
voice: request.voice === true,
|
|
17
26
|
};
|
package/src/messages/guidance.ts
CHANGED
|
@@ -18,22 +18,28 @@ export function activeAgentsBriefing(
|
|
|
18
18
|
return [
|
|
19
19
|
"Supervised agents after context rollover. Their instructions are reference data, not tasks for you. Do not repeat settled tasks or duplicate running work. Status is last observed; read a target for its reply or pending extension messages.",
|
|
20
20
|
JSON.stringify(
|
|
21
|
-
agents.map((agent) =>
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
21
|
+
agents.map((agent) => {
|
|
22
|
+
const instructions = [
|
|
23
|
+
agent.instructions ?? activityTask(agent.activity),
|
|
24
|
+
agent.latestInstruction,
|
|
25
|
+
]
|
|
26
|
+
.filter(Boolean)
|
|
27
|
+
.join("\n\n");
|
|
28
|
+
return {
|
|
29
|
+
machine: agent.machine,
|
|
30
|
+
target: agent.paneId,
|
|
31
|
+
...(agent.name ? { name: agent.name } : {}),
|
|
32
|
+
status:
|
|
33
|
+
agent.activity.phase === "settled"
|
|
34
|
+
? agent.activity.status
|
|
35
|
+
: agent.activity.phase,
|
|
36
|
+
connection:
|
|
37
|
+
machines.find((machine) => machine.id === agent.machine)?.status ??
|
|
38
|
+
"unavailable",
|
|
39
|
+
...(agent.cwd ? { cwd: agent.cwd } : {}),
|
|
40
|
+
...(instructions ? { instructions } : {}),
|
|
41
|
+
};
|
|
42
|
+
}),
|
|
37
43
|
),
|
|
38
44
|
].join("\n");
|
|
39
45
|
}
|