@howaboua/pi-shepherdr 0.2.5 → 0.2.7
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 +20 -0
- package/README.md +14 -2
- package/index.ts +3 -1
- package/package.json +6 -6
- package/src/agents-answer.ts +108 -0
- package/src/agents-contract.ts +18 -1
- package/src/agents-discovery.ts +211 -0
- package/src/agents-spawn.ts +179 -0
- package/src/agents-tool.ts +24 -202
- package/src/agents-work.ts +194 -0
- package/src/ask-answer.ts +54 -2
- package/src/delivery.ts +14 -3
- package/src/fleet.ts +66 -10
- package/src/herdr-client.ts +17 -0
- package/src/launch.ts +19 -8
- package/src/messages.ts +39 -7
- package/src/monitor.ts +2 -1
- package/src/peer-inbox.ts +10 -0
- package/src/remote/shepherdr-context.mjs +260 -0
- package/src/remote/shepherdr-peer.mjs +1 -1
- package/src/remote/shepherdr-session.mjs +294 -0
- package/src/remote/shepherdr.mjs +98 -236
- package/src/remote-client.ts +126 -17
- package/src/session-reader.ts +58 -0
- package/src/settlement.ts +19 -1
- package/src/shared-context.ts +283 -0
- package/src/types.ts +6 -0
- package/src/agents-operations.ts +0 -413
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @howaboua/pi-shepherdr
|
|
2
2
|
|
|
3
|
+
## 0.2.7
|
|
4
|
+
|
|
5
|
+
- Added opt-in shared notes and history for spawned agents with Pi Codex Conversion.
|
|
6
|
+
|
|
7
|
+
- New and nested workers receive distinct, persistent context identities before their first task.
|
|
8
|
+
- Local and Tree context stays in its owning sessions and routes through live controllers and existing SSH connections. Remote storage sharing requires the same Codex account.
|
|
9
|
+
- Sharing follows **Share subagent context** under `/codex context` and affects new spawns only. Existing agents and ordinary delegation remain unchanged.
|
|
10
|
+
- Wakeups of existing agents now say “Continue, unless awaiting for user approval.” Fresh-agent startup is unchanged.
|
|
11
|
+
- Fixed spawned agents dispatching before their persistent Pi session is registered. Profiles using `--no-session` now fail before creating a tab or workspace.
|
|
12
|
+
- Delegated tasks now guide workers to finish or report blockage with an assistant reply, reserving peer messages for useful mid-run updates rather than waiting or taking over orchestration.
|
|
13
|
+
- Peer messages now reach the receiving agent's active voice as brief updates, alongside completion reports. Task dispatches and slash commands are not announced.
|
|
14
|
+
|
|
15
|
+
## 0.2.6
|
|
16
|
+
|
|
17
|
+
- Fixed exact Ask answer confirmation and retry reconciliation without resending input.
|
|
18
|
+
|
|
19
|
+
- Keep answers pending while the original Ask is still open.
|
|
20
|
+
- Preserve accepted receipts when the worker's reply fails.
|
|
21
|
+
- Report dismissed Asks as rejected.
|
|
22
|
+
|
|
3
23
|
## 0.2.5
|
|
4
24
|
|
|
5
25
|
- GPT-6 Sol and Luna now share Astra's Codex support.
|
package/README.md
CHANGED
|
@@ -38,7 +38,7 @@ Shepherdr reads Herdr's existing machine profiles and connects enabled profiles
|
|
|
38
38
|
|
|
39
39
|
Profiles belong to the host running Pi, not the machine displaying its terminal. Omit `machine` for local agent calls. `list` and `find` search all machines unless filtered. Explicit `local` also means the host running Pi. For remote calls, use the opaque profile ID returned by `list`, not its label or hostname. Renaming a profile changes its label, not its routing identity.
|
|
40
40
|
|
|
41
|
-
Remote machines connect over noninteractive SSH. The target needs `node` on its SSH PATH, Herdr 0.9 or newer, the Herdr Pi integration and a running Herdr session. Shepherdr installs
|
|
41
|
+
Remote machines connect over noninteractive SSH. The target needs `node` on its SSH PATH, Herdr 0.9 or newer, the Herdr Pi integration and a running Herdr session. Shepherdr installs managed transport helpers under `~/.pi/agent` on each remote, runs them only for the connection lifetime and leaves no remote daemon behind. Herdr's multi-machine UI does not expose a cross-machine automation socket, so Shepherdr still owns its remote transport and Pi transcript reads.
|
|
42
42
|
|
|
43
43
|
### Migrating from separate Shepherdr machines
|
|
44
44
|
|
|
@@ -61,7 +61,9 @@ Call the `agents` tool with `action: "help"` before first use, then send flat re
|
|
|
61
61
|
| `watch` | Push future settlement from an existing Pi agent |
|
|
62
62
|
| `unwatch` | Stop reporting an agent |
|
|
63
63
|
|
|
64
|
-
`spawn
|
|
64
|
+
`spawn` and `assign` block by default. Set `blocking: false` when the controller should continue other work immediately. Task completion and blockage are then delivered automatically.
|
|
65
|
+
|
|
66
|
+
`answer` requires the pending `ask_id` from `read` or a blocked report. It returns `accepted` only when that exact Ask persisted the supplied responses; an accepted retry sends no input.
|
|
65
67
|
|
|
66
68
|
Questions, status updates and replies use `send`. It returns after submission, does not accept `blocking`, and never creates or changes a watch or task. Use `assign` only to delegate work whose result you need, not to exchange coordination messages.
|
|
67
69
|
|
|
@@ -83,6 +85,16 @@ Idle messages start a prepared user turn. Messages arriving during a run use ste
|
|
|
83
85
|
|
|
84
86
|
For `answer` inside Code or Notebook Mode, update Pi Ask on workers together with Shepherdr on controllers.
|
|
85
87
|
|
|
88
|
+
## Shared notes and history
|
|
89
|
+
|
|
90
|
+
With Pi Codex Conversion 3.0.40 or newer and notes-based continuity, enable **Share subagent context** under `/codex context` in the controller. It is off by default. Shared `spawn` gives each child a unique context identity before its first turn; `contextAgent` in the result identifies its notes and history. Shared nested spawns stay in the same family. Turning sharing off affects new spawns only; existing identity survives resume. `assign` and independently started agents remain unchanged. Older compatible Conversion versions keep ordinary delegation without sharing.
|
|
91
|
+
|
|
92
|
+
Pi creates each worker session normally. The worker records its shared identity before Shepherdr delivers the first task; no pre-created session file or launch override is needed.
|
|
93
|
+
|
|
94
|
+
Remote sharing requires Remote storage and the same Codex account on both ends. Local and Tree route through the owning Pi sessions and existing SSH connections. Those owners and intermediate controllers must be running; unavailable routes fail explicitly. Resume the owner and use `/herdr connect` after a connection loss. No note store is copied or silently substituted.
|
|
95
|
+
|
|
96
|
+
Both extensions work independently. A target without active context support still starts, with a warning that its context is not shared. A conflicting storage mode or account rejects the shared spawn before task delivery. Profiles that select or resume an existing session cannot participate in shared `spawn`; use `assign` instead.
|
|
97
|
+
|
|
86
98
|
## Profiles
|
|
87
99
|
|
|
88
100
|
On first load, Shepherdr installs three editable profiles:
|
package/index.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { AgentFleet } from "./src/fleet.js";
|
|
|
8
8
|
import { registerAgentEventRenderer } from "./src/messages.js";
|
|
9
9
|
import { registerPeerInbox } from "./src/peer-inbox.js";
|
|
10
10
|
import { installAgentProfiles } from "./src/profiles.js";
|
|
11
|
+
import { registerSharedAgentContext } from "./src/shared-context.js";
|
|
11
12
|
|
|
12
13
|
const CODE_MODE_PACKAGE = "@howaboua/pi-codex-conversion";
|
|
13
14
|
const CODE_MODE_MODULE = `${CODE_MODE_PACKAGE}/code-mode`;
|
|
@@ -20,7 +21,8 @@ export default async function shepherdrExtension(
|
|
|
20
21
|
await registerDeveloperDelivery(pi);
|
|
21
22
|
registerPeerInbox(pi);
|
|
22
23
|
const fleet = new AgentFleet(pi);
|
|
23
|
-
const
|
|
24
|
+
const sharedContext = await registerSharedAgentContext(pi, fleet);
|
|
25
|
+
const tool = createAgentsTool(fleet, sharedContext);
|
|
24
26
|
|
|
25
27
|
registerAgentEventRenderer(pi);
|
|
26
28
|
pi.registerTool(tool);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@howaboua/pi-shepherdr",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "Persistent blocking and asynchronous Pi agents through Herdr.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"pack:dry": "npm pack --dry-run"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@howaboua/pi-codex-conversion": ">=3.0.
|
|
46
|
+
"@howaboua/pi-codex-conversion": ">=3.0.40",
|
|
47
47
|
"@earendil-works/pi-ai": ">=0.84.3",
|
|
48
48
|
"@earendil-works/pi-coding-agent": ">=0.84.4",
|
|
49
49
|
"@earendil-works/pi-tui": ">=0.84.3",
|
|
@@ -55,11 +55,11 @@
|
|
|
55
55
|
}
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@howaboua/pi-codex-conversion": "3.0.
|
|
58
|
+
"@howaboua/pi-codex-conversion": "3.0.40",
|
|
59
59
|
"@biomejs/biome": "^2.5.10",
|
|
60
|
-
"@earendil-works/pi-ai": "0.
|
|
61
|
-
"@earendil-works/pi-coding-agent": "0.
|
|
62
|
-
"@earendil-works/pi-tui": "0.
|
|
60
|
+
"@earendil-works/pi-ai": "0.99.1",
|
|
61
|
+
"@earendil-works/pi-coding-agent": "0.99.1",
|
|
62
|
+
"@earendil-works/pi-tui": "0.99.1",
|
|
63
63
|
"@types/node": "^26.2.0",
|
|
64
64
|
"typebox": "1.3.18",
|
|
65
65
|
"typescript": "^7.0.2"
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { AgentToolUpdateCallback } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { AgentsParams } from "./agents-contract.js";
|
|
3
|
+
import {
|
|
4
|
+
dispatchAgentWork,
|
|
5
|
+
settlementResult,
|
|
6
|
+
toolResult,
|
|
7
|
+
} from "./agents-work.js";
|
|
8
|
+
import {
|
|
9
|
+
type AskAnswer,
|
|
10
|
+
answersMatch,
|
|
11
|
+
prepareAskAnswer,
|
|
12
|
+
sameAgentIdentity,
|
|
13
|
+
} from "./ask-answer.js";
|
|
14
|
+
import type { ConnectedMachine } from "./fleet.js";
|
|
15
|
+
import { getAgent } from "./herdr.js";
|
|
16
|
+
import type { PaneInfo } from "./types.js";
|
|
17
|
+
|
|
18
|
+
export async function answerAgent(
|
|
19
|
+
runtime: ConnectedMachine,
|
|
20
|
+
panel: PaneInfo,
|
|
21
|
+
params: AgentsParams,
|
|
22
|
+
signal: AbortSignal,
|
|
23
|
+
onUpdate: AgentToolUpdateCallback<Record<string, unknown>>,
|
|
24
|
+
) {
|
|
25
|
+
const answers = params.answers as AskAnswer[];
|
|
26
|
+
const askId = params.ask_id!.trim();
|
|
27
|
+
const view = await runtime.monitor.view(panel);
|
|
28
|
+
const prior = view.askResults?.[askId];
|
|
29
|
+
if (prior) {
|
|
30
|
+
const accepted =
|
|
31
|
+
prior.status === "accepted" && answersMatch(answers, prior.responses);
|
|
32
|
+
return toolResult({
|
|
33
|
+
answered: accepted,
|
|
34
|
+
ask_id: askId,
|
|
35
|
+
machine: runtime.machine,
|
|
36
|
+
status:
|
|
37
|
+
prior.status === "rejected"
|
|
38
|
+
? "rejected"
|
|
39
|
+
: accepted
|
|
40
|
+
? "accepted"
|
|
41
|
+
: "unknown",
|
|
42
|
+
target: panel.pane_id,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (view.ask?.toolCallId !== askId) {
|
|
46
|
+
return toolResult({
|
|
47
|
+
answered: false,
|
|
48
|
+
ask_id: askId,
|
|
49
|
+
machine: runtime.machine,
|
|
50
|
+
status: "unknown",
|
|
51
|
+
target: panel.pane_id,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const prepared = await prepareAskAnswer(
|
|
55
|
+
runtime.client,
|
|
56
|
+
runtime.monitor,
|
|
57
|
+
panel,
|
|
58
|
+
answers,
|
|
59
|
+
signal,
|
|
60
|
+
askId,
|
|
61
|
+
);
|
|
62
|
+
const task = `Answer: ${prepared.ask.prompts
|
|
63
|
+
.map((prompt) => prompt.title)
|
|
64
|
+
.join(", ")}`;
|
|
65
|
+
const { settlement, warning } = await dispatchAgentWork(
|
|
66
|
+
runtime,
|
|
67
|
+
panel,
|
|
68
|
+
task,
|
|
69
|
+
true,
|
|
70
|
+
signal,
|
|
71
|
+
onUpdate,
|
|
72
|
+
prepared.submit,
|
|
73
|
+
{ answeringAskId: askId },
|
|
74
|
+
);
|
|
75
|
+
const current = await getAgent(runtime.client, panel.pane_id);
|
|
76
|
+
const result = (await runtime.monitor.view(current)).askResults?.[askId];
|
|
77
|
+
const accepted =
|
|
78
|
+
sameAgentIdentity(panel, current) &&
|
|
79
|
+
result?.status === "accepted" &&
|
|
80
|
+
answersMatch(answers, result.responses);
|
|
81
|
+
const {
|
|
82
|
+
machine: _workerMachine,
|
|
83
|
+
status: workerStatus,
|
|
84
|
+
target: _workerTarget,
|
|
85
|
+
...workerResult
|
|
86
|
+
} = settlement
|
|
87
|
+
? settlementResult(runtime.machine, settlement, {
|
|
88
|
+
reportWorkerError: true,
|
|
89
|
+
})
|
|
90
|
+
: {};
|
|
91
|
+
return toolResult(
|
|
92
|
+
{
|
|
93
|
+
...workerResult,
|
|
94
|
+
answered: accepted,
|
|
95
|
+
ask_id: askId,
|
|
96
|
+
machine: runtime.machine,
|
|
97
|
+
status:
|
|
98
|
+
result?.status === "rejected"
|
|
99
|
+
? "rejected"
|
|
100
|
+
: accepted
|
|
101
|
+
? "accepted"
|
|
102
|
+
: "unknown",
|
|
103
|
+
target: panel.pane_id,
|
|
104
|
+
...(workerStatus ? { worker_status: workerStatus } : {}),
|
|
105
|
+
},
|
|
106
|
+
warning,
|
|
107
|
+
);
|
|
108
|
+
}
|
package/src/agents-contract.ts
CHANGED
|
@@ -42,7 +42,7 @@ const ACTION_FIELDS: Record<(typeof ACTIONS)[number], ReadonlySet<string>> = {
|
|
|
42
42
|
send: new Set(["action", "machine", "target", "message"]),
|
|
43
43
|
assign: new Set(["action", "machine", "target", "message", "blocking"]),
|
|
44
44
|
read: new Set(["action", "machine", "target", "source", "lines"]),
|
|
45
|
-
answer: new Set(["action", "machine", "target", "
|
|
45
|
+
answer: new Set(["action", "machine", "target", "ask_id", "answers"]),
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
const AskAnswerParameters = Type.Object(
|
|
@@ -86,6 +86,7 @@ const AgentsRequest = Type.Object(
|
|
|
86
86
|
status: Type.Optional(StringEnum(STATUSES)),
|
|
87
87
|
source: Type.Optional(StringEnum(READ_SOURCES)),
|
|
88
88
|
lines: Type.Optional(Type.Integer({ minimum: 1, maximum: 200 })),
|
|
89
|
+
ask_id: Type.Optional(Type.String()),
|
|
89
90
|
answers: Type.Optional(Type.Array(AskAnswerParameters)),
|
|
90
91
|
},
|
|
91
92
|
{ additionalProperties: false },
|
|
@@ -96,6 +97,14 @@ export const AgentsParameters = Type.Object({ action: StringEnum(ACTIONS) });
|
|
|
96
97
|
export type AgentsParams = Static<typeof AgentsRequest>;
|
|
97
98
|
export type AgentsToolParams = Static<typeof AgentsParameters>;
|
|
98
99
|
|
|
100
|
+
export function requiredAgentField(
|
|
101
|
+
value: string | undefined,
|
|
102
|
+
field: string,
|
|
103
|
+
): string {
|
|
104
|
+
if (!value?.trim()) throw new Error(`${field} is required for this action`);
|
|
105
|
+
return value.trim();
|
|
106
|
+
}
|
|
107
|
+
|
|
99
108
|
export function parseAgentsRequest(input: unknown): AgentsParams {
|
|
100
109
|
let value = input;
|
|
101
110
|
if (typeof input === "string") {
|
|
@@ -123,6 +132,14 @@ export function parseAgentsRequest(input: unknown): AgentsParams {
|
|
|
123
132
|
"send is message-only; omit blocking. Use assign to delegate work",
|
|
124
133
|
);
|
|
125
134
|
}
|
|
135
|
+
const request = value as Record<string, unknown>;
|
|
136
|
+
const askId = request["ask_id"];
|
|
137
|
+
if (action === "answer" && (typeof askId !== "string" || !askId.trim())) {
|
|
138
|
+
throw new Error("ask_id is required for answer");
|
|
139
|
+
}
|
|
140
|
+
if (action === "answer" && !Array.isArray(request["answers"])) {
|
|
141
|
+
throw new Error("answers is required for answer");
|
|
142
|
+
}
|
|
126
143
|
const unknown = Object.keys(value).filter(
|
|
127
144
|
(key) => !ACTION_FIELDS[action as keyof typeof ACTION_FIELDS].has(key),
|
|
128
145
|
);
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import type { AgentsParams, READ_SOURCES } from "./agents-contract.js";
|
|
2
|
+
import type { AgentFleet, ConnectedMachine } from "./fleet.js";
|
|
3
|
+
import { loadAgentProfiles } from "./profiles.js";
|
|
4
|
+
import type { AgentStatus, PaneInfo, SessionSnapshot } from "./types.js";
|
|
5
|
+
|
|
6
|
+
const MAX_LIST_ITEMS = 30;
|
|
7
|
+
const MAX_TERMINAL_READ_CHARS = 36_000;
|
|
8
|
+
|
|
9
|
+
export async function agentsHelp(): Promise<Record<string, unknown>> {
|
|
10
|
+
const profiles = await loadAgentProfiles();
|
|
11
|
+
return {
|
|
12
|
+
actions: {
|
|
13
|
+
help: "",
|
|
14
|
+
list: "machine?",
|
|
15
|
+
find: "query? status? machine?",
|
|
16
|
+
spawn:
|
|
17
|
+
"agent_type label message name? machine? placement? workspace? pane? cwd? base? blocking?",
|
|
18
|
+
watch: "target machine?",
|
|
19
|
+
unwatch: "target machine?",
|
|
20
|
+
send: "target message machine?",
|
|
21
|
+
assign: "target message machine? blocking?",
|
|
22
|
+
read: "target machine? source? lines?",
|
|
23
|
+
answer: "target ask_id answers machine?",
|
|
24
|
+
},
|
|
25
|
+
rules: {
|
|
26
|
+
machine:
|
|
27
|
+
"Omit for local (host running Pi); list/find omit for all machines. Remote: profile ID from list, not label/hostname",
|
|
28
|
+
target: "Use spawn/find target exactly",
|
|
29
|
+
label: "2-3 words; tab/session",
|
|
30
|
+
answers: "[{selections?:string[],other?:string,comment?:string}]",
|
|
31
|
+
ask_id: "Exact pending Ask ID",
|
|
32
|
+
send: "Peer questions, updates, replies; submission only, no wait or watch",
|
|
33
|
+
assign: "Delegate a task to an existing agent",
|
|
34
|
+
blocking:
|
|
35
|
+
"spawn/assign default true; false pushes task settlement; never poll. Reviewer spawns always block; await review before working its scope",
|
|
36
|
+
watch:
|
|
37
|
+
"Explicit watch persists until unwatch; automatic task watches end on finish/failure, not blockage",
|
|
38
|
+
prompt:
|
|
39
|
+
"Only task + inaccessible context; no method/evidence/reporting boilerplate",
|
|
40
|
+
slash:
|
|
41
|
+
"Leading / uses target Pi commands/skills/templates; extension commands are submission-only, even with assign/spawn; TUI-only commands unavailable",
|
|
42
|
+
reuse:
|
|
43
|
+
"Reuse only same investigation; reviews independent; new scope = new agent",
|
|
44
|
+
...(profiles.has("general")
|
|
45
|
+
? {
|
|
46
|
+
general: "Only when requested/orchestrating",
|
|
47
|
+
}
|
|
48
|
+
: {}),
|
|
49
|
+
},
|
|
50
|
+
profiles: Object.fromEntries(
|
|
51
|
+
[...profiles].map(([name, profile]) => [name, profile.description]),
|
|
52
|
+
),
|
|
53
|
+
advanced: "herdr --skill: workspace/tab/pane/process/focus/layout/terminal",
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function labels(snapshot: SessionSnapshot) {
|
|
58
|
+
return {
|
|
59
|
+
panes: new Map(snapshot.panes.map((pane) => [pane.pane_id, pane.label])),
|
|
60
|
+
tabs: new Map(snapshot.tabs.map((tab) => [tab.tab_id, tab.label])),
|
|
61
|
+
workspaces: new Map(
|
|
62
|
+
snapshot.workspaces.map((workspace) => [
|
|
63
|
+
workspace.workspace_id,
|
|
64
|
+
workspace.label,
|
|
65
|
+
]),
|
|
66
|
+
),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function compactAgent(
|
|
71
|
+
agent: PaneInfo,
|
|
72
|
+
snapshot: SessionSnapshot,
|
|
73
|
+
machine: string,
|
|
74
|
+
monitored: boolean,
|
|
75
|
+
): Record<string, unknown> {
|
|
76
|
+
const names = labels(snapshot);
|
|
77
|
+
return {
|
|
78
|
+
machine,
|
|
79
|
+
target: agent.pane_id,
|
|
80
|
+
...(agent.name ? { name: agent.name } : {}),
|
|
81
|
+
...(agent.label || names.panes.get(agent.pane_id)
|
|
82
|
+
? { label: agent.label ?? names.panes.get(agent.pane_id) }
|
|
83
|
+
: {}),
|
|
84
|
+
status: agent.agent_status,
|
|
85
|
+
cwd: agent.foreground_cwd ?? agent.cwd ?? null,
|
|
86
|
+
workspace: names.workspaces.get(agent.workspace_id) ?? agent.workspace_id,
|
|
87
|
+
tab: names.tabs.get(agent.tab_id) ?? agent.tab_id,
|
|
88
|
+
monitored,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function matchesAgent(
|
|
93
|
+
agent: Record<string, unknown>,
|
|
94
|
+
query: string | undefined,
|
|
95
|
+
status: AgentStatus | undefined,
|
|
96
|
+
): boolean {
|
|
97
|
+
if (status && agent["status"] !== status) return false;
|
|
98
|
+
if (!query) return true;
|
|
99
|
+
const haystack = Object.values(agent)
|
|
100
|
+
.filter((value) => typeof value === "string")
|
|
101
|
+
.join("\n")
|
|
102
|
+
.toLowerCase();
|
|
103
|
+
return haystack.includes(query.toLowerCase());
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function listFleetAgents(
|
|
107
|
+
fleet: AgentFleet,
|
|
108
|
+
params: Pick<AgentsParams, "machine" | "query" | "status">,
|
|
109
|
+
): Promise<Record<string, unknown>> {
|
|
110
|
+
const [profiles, machines] = await Promise.all([
|
|
111
|
+
loadAgentProfiles(),
|
|
112
|
+
fleet.snapshots(params.machine),
|
|
113
|
+
]);
|
|
114
|
+
const agents = machines.flatMap((machine) => {
|
|
115
|
+
if (!machine.snapshot) return [];
|
|
116
|
+
return machine.snapshot.agents
|
|
117
|
+
.filter(
|
|
118
|
+
(agent) =>
|
|
119
|
+
agent.agent === "pi" &&
|
|
120
|
+
(!machine.local || agent.pane_id !== process.env["HERDR_PANE_ID"]),
|
|
121
|
+
)
|
|
122
|
+
.map((agent) =>
|
|
123
|
+
compactAgent(
|
|
124
|
+
agent,
|
|
125
|
+
machine.snapshot!,
|
|
126
|
+
machine.id,
|
|
127
|
+
machine.monitoredPaneIds?.has(agent.pane_id) ?? false,
|
|
128
|
+
),
|
|
129
|
+
)
|
|
130
|
+
.filter((agent) => matchesAgent(agent, params.query, params.status));
|
|
131
|
+
});
|
|
132
|
+
const workspaces = machines.flatMap((machine) =>
|
|
133
|
+
(machine.snapshot?.workspaces ?? []).map((workspace) => ({
|
|
134
|
+
machine: machine.id,
|
|
135
|
+
id: workspace.workspace_id,
|
|
136
|
+
label: workspace.label,
|
|
137
|
+
})),
|
|
138
|
+
);
|
|
139
|
+
return {
|
|
140
|
+
profiles: Object.fromEntries(
|
|
141
|
+
[...profiles].map(([name, profile]) => [name, profile.description]),
|
|
142
|
+
),
|
|
143
|
+
machines: machines.map(
|
|
144
|
+
({
|
|
145
|
+
snapshot: _snapshot,
|
|
146
|
+
monitoredPaneIds: _monitoredPaneIds,
|
|
147
|
+
...machine
|
|
148
|
+
}) => machine,
|
|
149
|
+
),
|
|
150
|
+
agents: agents.slice(0, MAX_LIST_ITEMS),
|
|
151
|
+
workspaces: workspaces.slice(0, MAX_LIST_ITEMS),
|
|
152
|
+
...(agents.length > MAX_LIST_ITEMS
|
|
153
|
+
? { moreAgents: agents.length - MAX_LIST_ITEMS }
|
|
154
|
+
: {}),
|
|
155
|
+
...(workspaces.length > MAX_LIST_ITEMS
|
|
156
|
+
? { moreWorkspaces: workspaces.length - MAX_LIST_ITEMS }
|
|
157
|
+
: {}),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export async function findFleetAgents(
|
|
162
|
+
fleet: AgentFleet,
|
|
163
|
+
params: Pick<AgentsParams, "machine" | "query" | "status">,
|
|
164
|
+
): Promise<Record<string, unknown>> {
|
|
165
|
+
const listed = await listFleetAgents(fleet, params);
|
|
166
|
+
return {
|
|
167
|
+
agents: listed["agents"],
|
|
168
|
+
...(listed["moreAgents"] === undefined
|
|
169
|
+
? {}
|
|
170
|
+
: { moreAgents: listed["moreAgents"] }),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export async function readAgentTerminal(
|
|
175
|
+
runtime: ConnectedMachine,
|
|
176
|
+
panel: PaneInfo,
|
|
177
|
+
source: Exclude<(typeof READ_SOURCES)[number], "latest">,
|
|
178
|
+
lines: number,
|
|
179
|
+
): Promise<Record<string, unknown>> {
|
|
180
|
+
const value = await runtime.client.request<unknown>("agent.read", {
|
|
181
|
+
target: panel.pane_id,
|
|
182
|
+
source,
|
|
183
|
+
format: "text",
|
|
184
|
+
lines,
|
|
185
|
+
strip_ansi: true,
|
|
186
|
+
});
|
|
187
|
+
if (
|
|
188
|
+
typeof value !== "object" ||
|
|
189
|
+
value === null ||
|
|
190
|
+
!("read" in value) ||
|
|
191
|
+
typeof value.read !== "object" ||
|
|
192
|
+
value.read === null ||
|
|
193
|
+
!("text" in value.read) ||
|
|
194
|
+
typeof value.read.text !== "string"
|
|
195
|
+
) {
|
|
196
|
+
throw new Error("Herdr agent.read returned no text");
|
|
197
|
+
}
|
|
198
|
+
const truncated = value.read.text.length > MAX_TERMINAL_READ_CHARS;
|
|
199
|
+
return {
|
|
200
|
+
machine: runtime.machine,
|
|
201
|
+
target: panel.pane_id,
|
|
202
|
+
status: panel.agent_status,
|
|
203
|
+
text: truncated
|
|
204
|
+
? `${value.read.text.slice(0, MAX_TERMINAL_READ_CHARS)}\n…`
|
|
205
|
+
: value.read.text,
|
|
206
|
+
...(truncated ||
|
|
207
|
+
("truncated" in value.read && value.read.truncated === true)
|
|
208
|
+
? { truncated: true }
|
|
209
|
+
: {}),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentToolUpdateCallback,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import {
|
|
6
|
+
type AgentsParams,
|
|
7
|
+
requiredAgentField as required,
|
|
8
|
+
shouldBlockAgentSpawn,
|
|
9
|
+
} from "./agents-contract.js";
|
|
10
|
+
import {
|
|
11
|
+
allocateAgentName,
|
|
12
|
+
dispatchAgentWork,
|
|
13
|
+
reportProgress,
|
|
14
|
+
settlementResult,
|
|
15
|
+
toolResult,
|
|
16
|
+
} from "./agents-work.js";
|
|
17
|
+
import type { AgentFleet, ConnectedMachine } from "./fleet.js";
|
|
18
|
+
import { isDispatchRejected } from "./herdr-client.js";
|
|
19
|
+
import {
|
|
20
|
+
resolvePreparationDirectory,
|
|
21
|
+
rollbackStartedAgent,
|
|
22
|
+
startAgent,
|
|
23
|
+
} from "./launch.js";
|
|
24
|
+
import { attributeAgentPrompt } from "./messages.js";
|
|
25
|
+
import {
|
|
26
|
+
loadAgentProfiles,
|
|
27
|
+
prepareProfileMessage,
|
|
28
|
+
profileAgentArgs,
|
|
29
|
+
} from "./profiles.js";
|
|
30
|
+
import type { SharedAgentContext } from "./shared-context.js";
|
|
31
|
+
|
|
32
|
+
function agentLabel(value: string | undefined): string {
|
|
33
|
+
const label = required(value, "label");
|
|
34
|
+
const words = label.split(/\s+/u);
|
|
35
|
+
if (words.length < 2 || words.length > 3) {
|
|
36
|
+
throw new Error("label must contain 2 or 3 words");
|
|
37
|
+
}
|
|
38
|
+
return label;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function spawnAgent(
|
|
42
|
+
fleet: AgentFleet,
|
|
43
|
+
runtime: ConnectedMachine,
|
|
44
|
+
params: AgentsParams,
|
|
45
|
+
signal: AbortSignal,
|
|
46
|
+
onUpdate: AgentToolUpdateCallback<Record<string, unknown>>,
|
|
47
|
+
sharedContext: SharedAgentContext,
|
|
48
|
+
ctx: ExtensionContext,
|
|
49
|
+
) {
|
|
50
|
+
const profiles = await loadAgentProfiles();
|
|
51
|
+
const profileName = required(params.agent_type, "agent_type");
|
|
52
|
+
const profile = profiles.get(profileName);
|
|
53
|
+
if (!profile) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
`unknown agent_type ${JSON.stringify(profileName)}; available: ${[...profiles.keys()].join(", ")}`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
const label = agentLabel(params.label);
|
|
59
|
+
const name = params.name?.trim() || (await allocateAgentName(runtime, label));
|
|
60
|
+
const placement =
|
|
61
|
+
params.placement ??
|
|
62
|
+
(runtime.local && process.env["HERDR_WORKSPACE_ID"]
|
|
63
|
+
? "new_tab"
|
|
64
|
+
: "new_workspace");
|
|
65
|
+
const workspace =
|
|
66
|
+
params.workspace ??
|
|
67
|
+
(placement === "new_tab" && runtime.local
|
|
68
|
+
? process.env["HERDR_WORKSPACE_ID"]
|
|
69
|
+
: undefined);
|
|
70
|
+
const startParams = {
|
|
71
|
+
name,
|
|
72
|
+
label,
|
|
73
|
+
placement,
|
|
74
|
+
...(workspace ? { workspace } : {}),
|
|
75
|
+
...(params.pane ? { pane: params.pane } : {}),
|
|
76
|
+
...(params.cwd ? { cwd: params.cwd } : {}),
|
|
77
|
+
};
|
|
78
|
+
const cwd = await resolvePreparationDirectory(
|
|
79
|
+
runtime.client,
|
|
80
|
+
startParams,
|
|
81
|
+
runtime.fallbackCwd,
|
|
82
|
+
runtime.resolveDirectory,
|
|
83
|
+
);
|
|
84
|
+
const input = required(params.message, "message");
|
|
85
|
+
const message = await prepareProfileMessage(
|
|
86
|
+
profile,
|
|
87
|
+
{
|
|
88
|
+
cwd,
|
|
89
|
+
message: input,
|
|
90
|
+
...(params.base ? { base: params.base } : {}),
|
|
91
|
+
},
|
|
92
|
+
{ targetLocal: runtime.local },
|
|
93
|
+
);
|
|
94
|
+
const attributedMessage = await attributeAgentPrompt(
|
|
95
|
+
fleet.connected().client,
|
|
96
|
+
input.startsWith("/") ? input : message,
|
|
97
|
+
"task",
|
|
98
|
+
);
|
|
99
|
+
if (input.startsWith("/") && message !== input)
|
|
100
|
+
attributedMessage.context = [attributedMessage.context, message]
|
|
101
|
+
.filter(Boolean)
|
|
102
|
+
.join("\n\n");
|
|
103
|
+
reportProgress(onUpdate, `Spawning ${label}`, {
|
|
104
|
+
machine: runtime.machine,
|
|
105
|
+
name,
|
|
106
|
+
profile: profile.name,
|
|
107
|
+
status: "starting",
|
|
108
|
+
});
|
|
109
|
+
const agentArgs = profileAgentArgs(profile, { targetLocal: runtime.local });
|
|
110
|
+
const sharing = await sharedContext.prepare(ctx, runtime, name, agentArgs);
|
|
111
|
+
const started = await startAgent(
|
|
112
|
+
runtime.client,
|
|
113
|
+
startParams,
|
|
114
|
+
runtime.fallbackCwd,
|
|
115
|
+
runtime.resolveDirectory,
|
|
116
|
+
{ agentArgs },
|
|
117
|
+
);
|
|
118
|
+
let promptSubmissionStarted = false;
|
|
119
|
+
let promptAccepted = false;
|
|
120
|
+
let dispatch;
|
|
121
|
+
let shared: { agentName?: string; warning?: string } | undefined;
|
|
122
|
+
const blocking = shouldBlockAgentSpawn(profile.name, params.blocking);
|
|
123
|
+
try {
|
|
124
|
+
shared = await sharing?.accept(started.agent);
|
|
125
|
+
dispatch = await dispatchAgentWork(
|
|
126
|
+
runtime,
|
|
127
|
+
started.agent,
|
|
128
|
+
message,
|
|
129
|
+
blocking,
|
|
130
|
+
signal,
|
|
131
|
+
onUpdate,
|
|
132
|
+
async () => {
|
|
133
|
+
promptSubmissionStarted = true;
|
|
134
|
+
const receipt = await runtime.client.sendMessage(
|
|
135
|
+
started.agent,
|
|
136
|
+
attributedMessage,
|
|
137
|
+
);
|
|
138
|
+
promptAccepted = true;
|
|
139
|
+
return receipt;
|
|
140
|
+
},
|
|
141
|
+
{ expectUserMessage: true },
|
|
142
|
+
);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (
|
|
145
|
+
!promptAccepted &&
|
|
146
|
+
(!promptSubmissionStarted || isDispatchRejected(error))
|
|
147
|
+
) {
|
|
148
|
+
return rollbackStartedAgent(runtime.client, started, error);
|
|
149
|
+
}
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
return toolResult(
|
|
153
|
+
{
|
|
154
|
+
...(shared?.agentName ? { contextAgent: shared.agentName } : {}),
|
|
155
|
+
...(dispatch.command
|
|
156
|
+
? {
|
|
157
|
+
spawned: true,
|
|
158
|
+
commandSubmitted: true,
|
|
159
|
+
machine: runtime.machine,
|
|
160
|
+
target: started.id,
|
|
161
|
+
name,
|
|
162
|
+
}
|
|
163
|
+
: dispatch.settlement
|
|
164
|
+
? {
|
|
165
|
+
spawned: true,
|
|
166
|
+
...settlementResult(runtime.machine, dispatch.settlement),
|
|
167
|
+
}
|
|
168
|
+
: {
|
|
169
|
+
spawned: true,
|
|
170
|
+
machine: runtime.machine,
|
|
171
|
+
target: started.id,
|
|
172
|
+
name,
|
|
173
|
+
status: "working",
|
|
174
|
+
next: "Completion or blockage will be delivered automatically; do not poll",
|
|
175
|
+
}),
|
|
176
|
+
},
|
|
177
|
+
[dispatch.warning, shared?.warning].filter(Boolean).join("\n") || undefined,
|
|
178
|
+
);
|
|
179
|
+
}
|