@howaboua/pi-shepherdr 0.1.5 → 0.2.0
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 +24 -5
- package/package.json +3 -3
- package/profiles/general/prompt.md +1 -1
- package/src/agents-contract.ts +19 -3
- package/src/agents-operations.ts +54 -15
- package/src/agents-tool.ts +35 -19
- package/src/controller.ts +4 -8
- package/src/delivery.ts +24 -0
- package/src/fleet.ts +154 -79
- package/src/herdr-client.ts +24 -7
- package/src/herdr.ts +17 -1
- package/src/launch.ts +0 -25
- package/src/machine-catalog.ts +60 -0
- package/src/messages.ts +66 -11
- package/src/monitor-events.ts +173 -83
- package/src/monitor-record.ts +6 -19
- package/src/monitor-state.ts +50 -6
- package/src/monitor.ts +27 -15
- package/src/remote/shepherdr.mjs +70 -30
- package/src/remote-client.ts +32 -25
- package/src/session-reader.ts +44 -13
- package/src/settlement.ts +214 -73
- package/src/types.ts +15 -4
- package/src/widget.ts +26 -5
- package/src/machine-menu.ts +0 -161
- package/src/machines-config.ts +0 -148
package/src/messages.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { hostname } from "node:os";
|
|
1
2
|
import {
|
|
2
3
|
type ExtensionAPI,
|
|
3
4
|
type ExtensionContext,
|
|
@@ -6,6 +7,8 @@ import {
|
|
|
6
7
|
import { Box, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
|
|
7
8
|
import { activityTask } from "./activity.js";
|
|
8
9
|
import { sendPolicyMessage } from "./delivery.js";
|
|
10
|
+
import { getCurrentPane, getSnapshot } from "./herdr.js";
|
|
11
|
+
import type { HerdrConnection } from "./herdr-client.js";
|
|
9
12
|
import type {
|
|
10
13
|
LatestAssistant,
|
|
11
14
|
MonitoredAgent,
|
|
@@ -33,6 +36,54 @@ interface AgentEventLabels {
|
|
|
33
36
|
workspace?: string;
|
|
34
37
|
}
|
|
35
38
|
|
|
39
|
+
export function agentSource(agent: PaneInfo, labels: AgentEventLabels) {
|
|
40
|
+
const paneName = agent.label || agent.title;
|
|
41
|
+
return {
|
|
42
|
+
pane: agent.pane_id,
|
|
43
|
+
workspace: agent.workspace_id,
|
|
44
|
+
tab: agent.tab_id,
|
|
45
|
+
...(agent.name ? { name: agent.name } : {}),
|
|
46
|
+
...(paneName ? { pane_name: paneName } : {}),
|
|
47
|
+
...(labels.workspace ? { workspace_name: labels.workspace } : {}),
|
|
48
|
+
...(labels.tab ? { tab_name: labels.tab } : {}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function sourceAttributes(source: Record<string, string | undefined>): string {
|
|
53
|
+
return Object.entries(source)
|
|
54
|
+
.filter((entry): entry is [string, string] => entry[1] !== undefined)
|
|
55
|
+
.map(
|
|
56
|
+
([key, value]) =>
|
|
57
|
+
`${key}="${xml(value).replaceAll("\n", " ").replaceAll("\r", " ")}"`,
|
|
58
|
+
)
|
|
59
|
+
.join(" ");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function attributeAgentPrompt(
|
|
63
|
+
client: HerdrConnection,
|
|
64
|
+
message: string,
|
|
65
|
+
kind: "message" | "task",
|
|
66
|
+
): Promise<string> {
|
|
67
|
+
const [pane, snapshot] = await Promise.all([
|
|
68
|
+
getCurrentPane(client),
|
|
69
|
+
getSnapshot(client),
|
|
70
|
+
]);
|
|
71
|
+
const workspace = snapshot.workspaces.find(
|
|
72
|
+
(workspace) => workspace.workspace_id === pane.workspace_id,
|
|
73
|
+
)?.label;
|
|
74
|
+
const tab = snapshot.tabs.find((tab) => tab.tab_id === pane.tab_id)?.label;
|
|
75
|
+
const source = {
|
|
76
|
+
kind,
|
|
77
|
+
host: hostname(),
|
|
78
|
+
session: process.env["HERDR_SESSION"] || "default",
|
|
79
|
+
...agentSource(pane, {
|
|
80
|
+
...(workspace ? { workspace } : {}),
|
|
81
|
+
...(tab ? { tab } : {}),
|
|
82
|
+
}),
|
|
83
|
+
};
|
|
84
|
+
return `<herdr_sender ${sourceAttributes(source)} />\n${message}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
36
87
|
interface AgentEventOptions {
|
|
37
88
|
agent: PaneInfo;
|
|
38
89
|
agentToolName: string;
|
|
@@ -40,6 +91,7 @@ interface AgentEventOptions {
|
|
|
40
91
|
blockedMessage?: string;
|
|
41
92
|
labels: AgentEventLabels;
|
|
42
93
|
machine: string;
|
|
94
|
+
machineLabel: string;
|
|
43
95
|
operatorPrefix: string;
|
|
44
96
|
record: MonitoredAgent;
|
|
45
97
|
reply?: LatestAssistant;
|
|
@@ -51,6 +103,7 @@ interface AgentEventDetails {
|
|
|
51
103
|
blockedOn?: string;
|
|
52
104
|
cwd?: string;
|
|
53
105
|
machine: string;
|
|
106
|
+
machineLabel?: string;
|
|
54
107
|
name?: string;
|
|
55
108
|
paneId: string;
|
|
56
109
|
response?: string;
|
|
@@ -128,7 +181,7 @@ function boundedVoicePrompt(prompt: string, truncationNotice: string): string {
|
|
|
128
181
|
|
|
129
182
|
function agentVoicePrompt(details: AgentEventDetails): string {
|
|
130
183
|
const lines = [
|
|
131
|
-
`Worker: ${details.machine} / ${details.name?.trim() || "unnamed"}`,
|
|
184
|
+
`Worker: ${details.machineLabel ?? details.machine} / ${details.name?.trim() || "unnamed"}`,
|
|
132
185
|
`State: ${details.state}`,
|
|
133
186
|
];
|
|
134
187
|
if (details.task?.trim()) lines.push(`Task:\n${details.task.trim()}`);
|
|
@@ -224,6 +277,7 @@ function eventDetails(value: unknown): AgentEventDetails | undefined {
|
|
|
224
277
|
...(ask ? { ask } : {}),
|
|
225
278
|
...optional("blockedOn"),
|
|
226
279
|
...optional("cwd"),
|
|
280
|
+
...optional("machineLabel"),
|
|
227
281
|
...optional("name"),
|
|
228
282
|
...optional("response"),
|
|
229
283
|
...optional("tab"),
|
|
@@ -243,6 +297,7 @@ function agentEvent(options: AgentEventOptions): {
|
|
|
243
297
|
blockedMessage,
|
|
244
298
|
labels,
|
|
245
299
|
machine,
|
|
300
|
+
machineLabel,
|
|
246
301
|
operatorPrefix,
|
|
247
302
|
record,
|
|
248
303
|
reply,
|
|
@@ -257,14 +312,13 @@ function agentEvent(options: AgentEventOptions): {
|
|
|
257
312
|
: failed
|
|
258
313
|
? "herdr_agent_failed"
|
|
259
314
|
: "herdr_agent_result";
|
|
260
|
-
const attributes =
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
.join(" ");
|
|
315
|
+
const attributes = sourceAttributes({
|
|
316
|
+
machine,
|
|
317
|
+
machine_name: machineLabel !== machine ? machineLabel : undefined,
|
|
318
|
+
...agentSource(agent, labels),
|
|
319
|
+
name: agent.name ?? record.name,
|
|
320
|
+
directory: cwd,
|
|
321
|
+
});
|
|
268
322
|
const lines = [`<${tag} ${attributes}>`];
|
|
269
323
|
if (task) lines.push(`<task>${xml(task)}</task>`);
|
|
270
324
|
if (blockedMessage) {
|
|
@@ -292,10 +346,11 @@ function agentEvent(options: AgentEventOptions): {
|
|
|
292
346
|
content: lines.join("\n"),
|
|
293
347
|
details: {
|
|
294
348
|
machine,
|
|
349
|
+
machineLabel,
|
|
295
350
|
paneId: agent.pane_id,
|
|
296
351
|
state: blocked ? "blocked" : failed ? "failed" : "finished",
|
|
297
352
|
...(blocked && ask ? { ask } : {}),
|
|
298
|
-
...(record.name ? { name: record.name } : {}),
|
|
353
|
+
...(agent.name || record.name ? { name: agent.name || record.name } : {}),
|
|
299
354
|
...(cwd ? { cwd } : {}),
|
|
300
355
|
...(labels.workspace ? { workspace: labels.workspace } : {}),
|
|
301
356
|
...(labels.tab ? { tab: labels.tab } : {}),
|
|
@@ -353,7 +408,7 @@ export function registerAgentEventRenderer(pi: ExtensionAPI): void {
|
|
|
353
408
|
const agentIdentity = details?.name
|
|
354
409
|
? `${details.name} (${details.paneId})`
|
|
355
410
|
: (details?.paneId ?? "unknown");
|
|
356
|
-
const identity = `${details.machine} / ${agentIdentity}`;
|
|
411
|
+
const identity = `${details.machineLabel ?? details.machine} / ${agentIdentity}`;
|
|
357
412
|
const title = theme.fg(
|
|
358
413
|
blocked || failed ? "error" : "success",
|
|
359
414
|
`Herdr agent ${identity} · ${blocked ? "blocked" : failed ? "failed" : "finished"}`,
|
package/src/monitor-events.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import type { HerdrConnection } from "./herdr-client.js";
|
|
2
|
-
import type { HerdrEvent } from "./types.js";
|
|
2
|
+
import type { HerdrEvent, MonitoringIssue } from "./types.js";
|
|
3
|
+
|
|
4
|
+
interface SubscriptionAttempt {
|
|
5
|
+
key: string;
|
|
6
|
+
controller: AbortController;
|
|
7
|
+
}
|
|
3
8
|
|
|
4
9
|
interface Subscription {
|
|
5
|
-
|
|
10
|
+
attempt: SubscriptionAttempt;
|
|
6
11
|
unsubscribe: () => void;
|
|
7
12
|
}
|
|
8
13
|
|
|
@@ -10,19 +15,23 @@ interface MonitorEventsOptions {
|
|
|
10
15
|
client: HerdrConnection;
|
|
11
16
|
reconnect?: boolean;
|
|
12
17
|
onEvent: (event: HerdrEvent) => void;
|
|
13
|
-
|
|
14
|
-
onWarning: (
|
|
18
|
+
reconcile: () => Promise<void>;
|
|
19
|
+
onWarning: (issue: MonitoringIssue) => void;
|
|
20
|
+
onRecovered: () => void;
|
|
15
21
|
targets: () => string[];
|
|
16
22
|
}
|
|
17
23
|
|
|
18
24
|
export class MonitorEvents {
|
|
19
25
|
private readonly options: MonitorEventsOptions;
|
|
20
26
|
private active = false;
|
|
21
|
-
private
|
|
22
|
-
private
|
|
27
|
+
private epoch = 0;
|
|
28
|
+
private dirty = false;
|
|
29
|
+
private resync = false;
|
|
30
|
+
private refreshTask: Promise<void> | undefined;
|
|
31
|
+
private pending: SubscriptionAttempt | undefined;
|
|
23
32
|
private reconnectTimer: NodeJS.Timeout | undefined;
|
|
24
33
|
private subscription: Subscription | undefined;
|
|
25
|
-
private
|
|
34
|
+
private warning: "degraded" | "unavailable" | undefined;
|
|
26
35
|
|
|
27
36
|
constructor(options: MonitorEventsOptions) {
|
|
28
37
|
this.options = options;
|
|
@@ -35,102 +44,183 @@ export class MonitorEvents {
|
|
|
35
44
|
|
|
36
45
|
stop(): void {
|
|
37
46
|
this.active = false;
|
|
38
|
-
this.
|
|
39
|
-
this.
|
|
40
|
-
this.
|
|
41
|
-
this.
|
|
42
|
-
this.
|
|
43
|
-
|
|
44
|
-
this.
|
|
47
|
+
this.epoch += 1;
|
|
48
|
+
this.dirty = false;
|
|
49
|
+
this.resync = false;
|
|
50
|
+
this.cancelPending();
|
|
51
|
+
this.closeSubscription();
|
|
52
|
+
this.warning = undefined;
|
|
53
|
+
this.clearReconnect();
|
|
45
54
|
}
|
|
46
55
|
|
|
47
56
|
async refresh(): Promise<void> {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
this.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this.
|
|
55
|
-
this.
|
|
56
|
-
|
|
57
|
+
this.dirty = this.active;
|
|
58
|
+
this.clearReconnect();
|
|
59
|
+
const key = this.targetKey();
|
|
60
|
+
if (this.pending && this.pending.key !== key) this.cancelPending();
|
|
61
|
+
if (!this.active || key === "[]") {
|
|
62
|
+
this.cancelPending();
|
|
63
|
+
this.closeSubscription();
|
|
64
|
+
if (this.active) this.recovered();
|
|
65
|
+
}
|
|
66
|
+
while (this.dirty || this.refreshTask) {
|
|
67
|
+
this.refreshTask ??= Promise.resolve().then(async () => {
|
|
68
|
+
while (this.active && this.dirty) {
|
|
69
|
+
this.dirty = false;
|
|
70
|
+
await this.update();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
const task = this.refreshTask;
|
|
74
|
+
try {
|
|
75
|
+
await task;
|
|
76
|
+
} finally {
|
|
77
|
+
if (this.refreshTask === task) this.refreshTask = undefined;
|
|
78
|
+
}
|
|
57
79
|
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
retry(): Promise<void> {
|
|
83
|
+
this.resync = true;
|
|
84
|
+
return this.refresh();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private targetKey(): string {
|
|
88
|
+
return JSON.stringify([...new Set(this.options.targets())].sort());
|
|
89
|
+
}
|
|
58
90
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
pane_id: paneId,
|
|
63
|
-
})),
|
|
64
|
-
{ type: "pane.moved" },
|
|
65
|
-
{ type: "pane.closed" },
|
|
66
|
-
];
|
|
67
|
-
this.live.add(generation);
|
|
91
|
+
private async update(): Promise<void> {
|
|
92
|
+
const epoch = this.epoch;
|
|
93
|
+
let attempt: SubscriptionAttempt | undefined;
|
|
68
94
|
try {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
this.warn(`Herdr monitoring disconnected: ${error.message}`);
|
|
80
|
-
this.scheduleReconnect(generation, true);
|
|
81
|
-
},
|
|
82
|
-
);
|
|
83
|
-
if (
|
|
84
|
-
!this.active ||
|
|
85
|
-
generation !== this.generation ||
|
|
86
|
-
!this.live.has(generation)
|
|
87
|
-
) {
|
|
88
|
-
this.live.delete(generation);
|
|
89
|
-
unsubscribe();
|
|
95
|
+
if (this.resync) {
|
|
96
|
+
this.resync = false;
|
|
97
|
+
// Remap panes moved during the outage before requesting new coverage.
|
|
98
|
+
await this.options.reconcile();
|
|
99
|
+
if (!this.active || epoch !== this.epoch) return;
|
|
100
|
+
}
|
|
101
|
+
const key = this.targetKey();
|
|
102
|
+
if (key === "[]") {
|
|
103
|
+
this.closeSubscription();
|
|
104
|
+
this.recovered();
|
|
90
105
|
return;
|
|
91
106
|
}
|
|
92
|
-
this.
|
|
93
|
-
|
|
94
|
-
|
|
107
|
+
if (this.subscription?.attempt.key !== key) {
|
|
108
|
+
const candidate = { key, controller: new AbortController() };
|
|
109
|
+
attempt = candidate;
|
|
110
|
+
this.pending = candidate;
|
|
111
|
+
const subscriptions = [
|
|
112
|
+
...this.options.targets().map((paneId) => ({
|
|
113
|
+
type: "pane.agent_status_changed",
|
|
114
|
+
pane_id: paneId,
|
|
115
|
+
})),
|
|
116
|
+
{ type: "pane.moved" },
|
|
117
|
+
{ type: "pane.closed" },
|
|
118
|
+
];
|
|
119
|
+
const unsubscribe = await this.options.client.subscribe(
|
|
120
|
+
subscriptions,
|
|
121
|
+
(event) => {
|
|
122
|
+
if (this.isLive(candidate)) this.options.onEvent(event);
|
|
123
|
+
},
|
|
124
|
+
(error) => this.disconnected(candidate, error),
|
|
125
|
+
candidate.controller.signal,
|
|
126
|
+
);
|
|
127
|
+
if (!this.isLive(candidate) || key !== this.targetKey()) {
|
|
128
|
+
unsubscribe();
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
this.closeSubscription();
|
|
132
|
+
this.subscription = { attempt: candidate, unsubscribe };
|
|
133
|
+
this.pending = undefined;
|
|
134
|
+
}
|
|
135
|
+
// Herdr baselines status before ACK; reconcile afterward to cover setup transitions.
|
|
136
|
+
await this.options.reconcile();
|
|
137
|
+
if (!this.active || epoch !== this.epoch) return;
|
|
138
|
+
if (key !== this.targetKey()) {
|
|
139
|
+
this.dirty = true;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (this.subscription) this.recovered();
|
|
95
143
|
} catch (error) {
|
|
96
|
-
|
|
97
|
-
|
|
144
|
+
if (
|
|
145
|
+
!this.active ||
|
|
146
|
+
epoch !== this.epoch ||
|
|
147
|
+
attempt?.controller.signal.aborted
|
|
148
|
+
)
|
|
149
|
+
return;
|
|
98
150
|
const failure = error instanceof Error ? error : new Error(String(error));
|
|
99
|
-
this.warn(
|
|
151
|
+
this.warn(failure.message);
|
|
100
152
|
if (this.options.reconnect === false) throw failure;
|
|
101
|
-
this.scheduleReconnect(
|
|
153
|
+
this.scheduleReconnect();
|
|
154
|
+
} finally {
|
|
155
|
+
if (attempt && this.pending === attempt) this.cancelPending();
|
|
102
156
|
}
|
|
103
157
|
}
|
|
104
158
|
|
|
105
|
-
private
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
159
|
+
private isLive(attempt: SubscriptionAttempt): boolean {
|
|
160
|
+
return (
|
|
161
|
+
this.active &&
|
|
162
|
+
!attempt.controller.signal.aborted &&
|
|
163
|
+
(this.pending === attempt || this.subscription?.attempt === attempt)
|
|
164
|
+
);
|
|
109
165
|
}
|
|
110
166
|
|
|
111
|
-
private
|
|
112
|
-
|
|
113
|
-
this.
|
|
114
|
-
|
|
167
|
+
private cancelPending(): void {
|
|
168
|
+
const pending = this.pending;
|
|
169
|
+
this.pending = undefined;
|
|
170
|
+
pending?.controller.abort();
|
|
115
171
|
}
|
|
116
172
|
|
|
117
|
-
private
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
173
|
+
private closeSubscription(): void {
|
|
174
|
+
const subscription = this.subscription;
|
|
175
|
+
this.subscription = undefined;
|
|
176
|
+
subscription?.attempt.controller.abort();
|
|
177
|
+
subscription?.unsubscribe();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private disconnected(attempt: SubscriptionAttempt, error?: Error): void {
|
|
181
|
+
if (!this.isLive(attempt)) return;
|
|
182
|
+
if (this.subscription?.attempt === attempt) this.closeSubscription();
|
|
183
|
+
if (this.pending === attempt) this.cancelPending();
|
|
184
|
+
this.warn(error?.message ?? "event stream closed");
|
|
185
|
+
this.scheduleReconnect();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private warn(detail: string): void {
|
|
189
|
+
const state = this.subscription ? "degraded" : "unavailable";
|
|
190
|
+
if (this.warning === state) return;
|
|
191
|
+
this.warning = state;
|
|
192
|
+
const message =
|
|
193
|
+
state === "degraded"
|
|
194
|
+
? "Herdr monitor update failed. Existing stream retained, coverage may be incomplete"
|
|
195
|
+
: "Herdr monitoring unavailable";
|
|
196
|
+
this.options.onWarning({
|
|
197
|
+
state,
|
|
198
|
+
message: `${message}: ${detail}${this.options.reconnect === false ? "" : ". Retrying"}`,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private recovered(): void {
|
|
203
|
+
if (!this.warning) return;
|
|
204
|
+
this.warning = undefined;
|
|
205
|
+
this.options.onRecovered();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private clearReconnect(): void {
|
|
209
|
+
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
210
|
+
this.reconnectTimer = undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private scheduleReconnect(): void {
|
|
214
|
+
if (!this.active || this.options.reconnect === false || this.reconnectTimer)
|
|
215
|
+
return;
|
|
216
|
+
const epoch = this.epoch;
|
|
123
217
|
this.reconnectTimer = setTimeout(() => {
|
|
124
218
|
this.reconnectTimer = undefined;
|
|
125
|
-
void this.
|
|
126
|
-
.
|
|
127
|
-
.
|
|
128
|
-
.
|
|
129
|
-
|
|
130
|
-
`Herdr monitoring reconnect failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
131
|
-
);
|
|
132
|
-
this.scheduleReconnect(generation, false);
|
|
133
|
-
});
|
|
219
|
+
void this.retry().catch((error) => {
|
|
220
|
+
if (!this.active || epoch !== this.epoch) return;
|
|
221
|
+
this.warn(error instanceof Error ? error.message : String(error));
|
|
222
|
+
this.scheduleReconnect();
|
|
223
|
+
});
|
|
134
224
|
}, 1_000);
|
|
135
225
|
this.reconnectTimer.unref();
|
|
136
226
|
}
|
package/src/monitor-record.ts
CHANGED
|
@@ -88,30 +88,13 @@ function parsedActivity(value: unknown): AgentActivity | undefined {
|
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
function legacyActivity(
|
|
92
|
-
record: Record<string, unknown>,
|
|
93
|
-
): AgentActivity | undefined {
|
|
94
|
-
if (!isAgentStatus(record["lastStatus"])) return undefined;
|
|
95
|
-
const task = optionalString(record, "task");
|
|
96
|
-
if (task === false) return undefined;
|
|
97
|
-
if (record["lastStatus"] === "working") {
|
|
98
|
-
return { phase: "working", ...(task ? { task } : {}) };
|
|
99
|
-
}
|
|
100
|
-
if (!task) return { phase: "settled", status: record["lastStatus"] };
|
|
101
|
-
return {
|
|
102
|
-
attemptId: `restored:${String(record["terminalId"])}`,
|
|
103
|
-
phase: "submitting",
|
|
104
|
-
previous: { phase: "settled", status: record["lastStatus"] },
|
|
105
|
-
task,
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
91
|
export function parseMonitoredAgent(
|
|
110
92
|
value: unknown,
|
|
111
93
|
): MonitoredAgent | undefined {
|
|
112
94
|
if (typeof value !== "object" || value === null) return undefined;
|
|
113
95
|
const record = value as Record<string, unknown>;
|
|
114
96
|
if (
|
|
97
|
+
(record["scope"] !== "task" && record["scope"] !== "persistent") ||
|
|
115
98
|
typeof record["paneId"] !== "string" ||
|
|
116
99
|
typeof record["terminalId"] !== "string" ||
|
|
117
100
|
typeof record["workspaceId"] !== "string" ||
|
|
@@ -122,7 +105,7 @@ export function parseMonitoredAgent(
|
|
|
122
105
|
const cwd = optionalString(record, "cwd");
|
|
123
106
|
const lastAssistantId = optionalString(record, "lastAssistantId");
|
|
124
107
|
const name = optionalString(record, "name");
|
|
125
|
-
const activity = parsedActivity(record["activity"])
|
|
108
|
+
const activity = parsedActivity(record["activity"]);
|
|
126
109
|
if (
|
|
127
110
|
cwd === false ||
|
|
128
111
|
lastAssistantId === false ||
|
|
@@ -132,6 +115,7 @@ export function parseMonitoredAgent(
|
|
|
132
115
|
return undefined;
|
|
133
116
|
}
|
|
134
117
|
return {
|
|
118
|
+
scope: record["scope"],
|
|
135
119
|
activity,
|
|
136
120
|
paneId: record["paneId"],
|
|
137
121
|
terminalId: record["terminalId"],
|
|
@@ -146,10 +130,12 @@ export function parseMonitoredAgent(
|
|
|
146
130
|
export function recordForPanel(
|
|
147
131
|
panel: PaneInfo,
|
|
148
132
|
activity: AgentActivity,
|
|
133
|
+
scope: MonitoredAgent["scope"],
|
|
149
134
|
lastAssistantId?: string,
|
|
150
135
|
): MonitoredAgent {
|
|
151
136
|
const cwd = panel.foreground_cwd ?? panel.cwd;
|
|
152
137
|
return {
|
|
138
|
+
scope,
|
|
153
139
|
activity,
|
|
154
140
|
paneId: panel.pane_id,
|
|
155
141
|
terminalId: panel.terminal_id,
|
|
@@ -166,6 +152,7 @@ export function sameMonitorRecord(
|
|
|
166
152
|
right: MonitoredAgent,
|
|
167
153
|
): boolean {
|
|
168
154
|
return (
|
|
155
|
+
left.scope === right.scope &&
|
|
169
156
|
left.paneId === right.paneId &&
|
|
170
157
|
left.terminalId === right.terminalId &&
|
|
171
158
|
left.workspaceId === right.workspaceId &&
|
package/src/monitor-state.ts
CHANGED
|
@@ -79,10 +79,6 @@ export class MonitorState {
|
|
|
79
79
|
return this.agents.get(terminalId);
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
isMonitored(paneId: string): boolean {
|
|
83
|
-
return this.byPane(paneId) !== undefined;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
82
|
watch(
|
|
87
83
|
panel: PaneInfo,
|
|
88
84
|
lastAssistantId?: string,
|
|
@@ -95,11 +91,12 @@ export class MonitorState {
|
|
|
95
91
|
!existing &&
|
|
96
92
|
(panel.agent_status === "done" || panel.agent_status === "blocked");
|
|
97
93
|
const activity = reportCurrent
|
|
98
|
-
? ({ phase: "working" } as const)
|
|
94
|
+
? ({ attemptId: randomUUID(), phase: "working" } as const)
|
|
99
95
|
: (existing?.activity ?? activityForPanel(panel));
|
|
100
96
|
const record = recordForPanel(
|
|
101
97
|
panel,
|
|
102
98
|
activity,
|
|
99
|
+
reportSettled ? "persistent" : (existing?.scope ?? "task"),
|
|
103
100
|
reportCurrent
|
|
104
101
|
? undefined
|
|
105
102
|
: (existing?.lastAssistantId ?? lastAssistantId),
|
|
@@ -129,6 +126,9 @@ export class MonitorState {
|
|
|
129
126
|
`prompt submission to ${record.name ?? paneId} is still unresolved`,
|
|
130
127
|
);
|
|
131
128
|
}
|
|
129
|
+
if (record.activity.phase === "working" && record.activity.task) {
|
|
130
|
+
throw new Error("Agent already has delegated work; use send for updates");
|
|
131
|
+
}
|
|
132
132
|
const attempt = {
|
|
133
133
|
attemptId: randomUUID(),
|
|
134
134
|
terminalId: record.terminalId,
|
|
@@ -208,6 +208,28 @@ export class MonitorState {
|
|
|
208
208
|
this.agents.set(record.terminalId, { ...record, activity: next });
|
|
209
209
|
return { changed };
|
|
210
210
|
}
|
|
211
|
+
if (
|
|
212
|
+
status === "blocked" &&
|
|
213
|
+
record.activity.phase === "settled" &&
|
|
214
|
+
record.activity.status !== "blocked"
|
|
215
|
+
) {
|
|
216
|
+
const observed = {
|
|
217
|
+
...record,
|
|
218
|
+
activity: {
|
|
219
|
+
attemptId: randomUUID(),
|
|
220
|
+
phase: "working",
|
|
221
|
+
} as const,
|
|
222
|
+
};
|
|
223
|
+
this.agents.set(record.terminalId, observed);
|
|
224
|
+
return {
|
|
225
|
+
changed: true,
|
|
226
|
+
completion: {
|
|
227
|
+
record: observed,
|
|
228
|
+
requireNewReply: false,
|
|
229
|
+
status,
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
}
|
|
211
233
|
if (isSettledStatus(status) && record.activity.phase !== "settled") {
|
|
212
234
|
const task = activityTask(record.activity);
|
|
213
235
|
return {
|
|
@@ -273,6 +295,19 @@ export class MonitorState {
|
|
|
273
295
|
...(expectedUserAfter !== undefined ? { expectedUserAfter } : {}),
|
|
274
296
|
...(task ? { task } : {}),
|
|
275
297
|
};
|
|
298
|
+
} else if (
|
|
299
|
+
panel.agent_status === "blocked" &&
|
|
300
|
+
activity.phase === "settled" &&
|
|
301
|
+
activity.status !== "blocked"
|
|
302
|
+
) {
|
|
303
|
+
activity = {
|
|
304
|
+
attemptId: randomUUID(),
|
|
305
|
+
phase: "working",
|
|
306
|
+
};
|
|
307
|
+
completion = {
|
|
308
|
+
requireNewReply: false,
|
|
309
|
+
status: panel.agent_status,
|
|
310
|
+
};
|
|
276
311
|
} else if (
|
|
277
312
|
isSettledStatus(panel.agent_status) &&
|
|
278
313
|
activity.phase !== "settled"
|
|
@@ -287,7 +322,12 @@ export class MonitorState {
|
|
|
287
322
|
activity = { phase: "settled", status: panel.agent_status };
|
|
288
323
|
}
|
|
289
324
|
|
|
290
|
-
const updated = recordForPanel(
|
|
325
|
+
const updated = recordForPanel(
|
|
326
|
+
panel,
|
|
327
|
+
activity,
|
|
328
|
+
record.scope,
|
|
329
|
+
record.lastAssistantId,
|
|
330
|
+
);
|
|
291
331
|
if (!sameMonitorRecord(record, updated)) changed = true;
|
|
292
332
|
if (record.terminalId !== updated.terminalId) {
|
|
293
333
|
this.agents.delete(record.terminalId);
|
|
@@ -321,6 +361,10 @@ export class MonitorState {
|
|
|
321
361
|
): boolean {
|
|
322
362
|
const record = this.byTerminal(terminalId);
|
|
323
363
|
if (!record || activityTask(record.activity) !== task) return false;
|
|
364
|
+
if (record.scope === "task" && status !== "blocked") {
|
|
365
|
+
this.agents.delete(terminalId);
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
324
368
|
this.agents.set(terminalId, {
|
|
325
369
|
...record,
|
|
326
370
|
activity: { phase: "settled", status },
|