@howaboua/pi-shepherdr 0.2.13 → 0.2.14
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 +10 -0
- package/README.md +11 -5
- package/package.json +6 -6
- package/src/agents-answer.ts +22 -0
- package/src/agents-discovery.ts +2 -2
- package/src/agents-tool.ts +2 -1
- package/src/ask-answer.ts +32 -18
- package/src/board/awareness.ts +115 -0
- package/src/board/host.ts +46 -6
- package/src/board/tool.ts +0 -3
- package/src/context-briefing.ts +226 -0
- package/src/controller.ts +81 -14
- package/src/delivery.ts +33 -0
- package/src/messages.ts +86 -15
- package/src/monitor-questions.ts +249 -0
- package/src/monitor-record.ts +6 -0
- package/src/monitor-state.ts +10 -4
- package/src/monitor.ts +40 -2
- package/src/remote/shepherdr-session-watch.mjs +72 -0
- package/src/remote/shepherdr-session.mjs +32 -2
- package/src/remote/shepherdr.mjs +21 -1
- package/src/remote-client.ts +66 -24
- package/src/session-reader.ts +49 -3
- package/src/types.ts +2 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { sameAgentIdentity } from "./ask-answer.js";
|
|
3
|
+
import { getAgent, sessionPath } from "./herdr.js";
|
|
4
|
+
import type { HerdrConnection } from "./herdr-client.js";
|
|
5
|
+
import type { MonitorState } from "./monitor-state.js";
|
|
6
|
+
import type { AssistantReader } from "./session-reader.js";
|
|
7
|
+
import type {
|
|
8
|
+
AskResult,
|
|
9
|
+
MonitoredAgent,
|
|
10
|
+
PaneInfo,
|
|
11
|
+
PendingAsk,
|
|
12
|
+
} from "./types.js";
|
|
13
|
+
|
|
14
|
+
const ASK_RESULT_TIMEOUT_MS = 10_000;
|
|
15
|
+
|
|
16
|
+
interface MonitorQuestionsOptions {
|
|
17
|
+
client: HerdrConnection;
|
|
18
|
+
reader: AssistantReader;
|
|
19
|
+
state: MonitorState;
|
|
20
|
+
notify: (
|
|
21
|
+
ctx: ExtensionContext,
|
|
22
|
+
record: MonitoredAgent,
|
|
23
|
+
panel: PaneInfo,
|
|
24
|
+
ask: PendingAsk,
|
|
25
|
+
) => void;
|
|
26
|
+
persist: () => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Questions are attention events, not settlements of delegated work.
|
|
30
|
+
export class MonitorQuestions {
|
|
31
|
+
private context: ExtensionContext | undefined;
|
|
32
|
+
private generation = 0;
|
|
33
|
+
private panels: PaneInfo[] = [];
|
|
34
|
+
private key = "";
|
|
35
|
+
private controller: AbortController | undefined;
|
|
36
|
+
private unsubscribe: (() => void) | undefined;
|
|
37
|
+
private refreshTail = Promise.resolve();
|
|
38
|
+
private readonly checks = new Map<string, { dirty: boolean }>();
|
|
39
|
+
private readonly answers = new Set<AbortController>();
|
|
40
|
+
private warned = false;
|
|
41
|
+
private readonly options: MonitorQuestionsOptions;
|
|
42
|
+
|
|
43
|
+
constructor(options: MonitorQuestionsOptions) {
|
|
44
|
+
this.options = options;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
start(ctx: ExtensionContext): void {
|
|
48
|
+
this.stop();
|
|
49
|
+
this.context = ctx;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
stop(): void {
|
|
53
|
+
this.generation++;
|
|
54
|
+
for (const controller of this.answers) controller.abort();
|
|
55
|
+
this.context = undefined;
|
|
56
|
+
this.panels = [];
|
|
57
|
+
this.key = "";
|
|
58
|
+
this.controller?.abort();
|
|
59
|
+
this.unsubscribe?.();
|
|
60
|
+
this.unsubscribe = undefined;
|
|
61
|
+
this.controller = undefined;
|
|
62
|
+
this.checks.clear();
|
|
63
|
+
this.warned = false;
|
|
64
|
+
this.refreshTail = Promise.resolve();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
submitAnswer(
|
|
68
|
+
panel: PaneInfo,
|
|
69
|
+
askId: string,
|
|
70
|
+
submit: (signal: AbortSignal) => Promise<void>,
|
|
71
|
+
signal: AbortSignal,
|
|
72
|
+
): Promise<AskResult | undefined> {
|
|
73
|
+
signal.throwIfAborted();
|
|
74
|
+
const path = sessionPath(panel);
|
|
75
|
+
if (!path) return Promise.resolve(undefined);
|
|
76
|
+
const controller = new AbortController();
|
|
77
|
+
this.answers.add(controller);
|
|
78
|
+
return new Promise((resolve, reject) => {
|
|
79
|
+
let closed = false;
|
|
80
|
+
let unsubscribe: (() => void) | undefined;
|
|
81
|
+
let submitted = false;
|
|
82
|
+
let checking = false;
|
|
83
|
+
let dirty = false;
|
|
84
|
+
const finish = (result?: AskResult, error?: unknown) => {
|
|
85
|
+
if (closed) return;
|
|
86
|
+
closed = true;
|
|
87
|
+
clearTimeout(timer);
|
|
88
|
+
signal.removeEventListener("abort", cancel);
|
|
89
|
+
controller.signal.removeEventListener("abort", stopped);
|
|
90
|
+
controller.abort();
|
|
91
|
+
unsubscribe?.();
|
|
92
|
+
this.answers.delete(controller);
|
|
93
|
+
if (error !== undefined) reject(error);
|
|
94
|
+
else resolve(result);
|
|
95
|
+
};
|
|
96
|
+
const cancel = () => finish(undefined, signal.reason);
|
|
97
|
+
const stopped = () => finish(undefined, controller.signal.reason);
|
|
98
|
+
const timer = setTimeout(() => finish(), ASK_RESULT_TIMEOUT_MS);
|
|
99
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
100
|
+
controller.signal.addEventListener("abort", stopped, { once: true });
|
|
101
|
+
const check = () => {
|
|
102
|
+
dirty = true;
|
|
103
|
+
if (closed || !submitted || checking) return;
|
|
104
|
+
checking = true;
|
|
105
|
+
void (async () => {
|
|
106
|
+
while (dirty && !closed) {
|
|
107
|
+
dirty = false;
|
|
108
|
+
const current = await getAgent(this.options.client, panel.pane_id);
|
|
109
|
+
if (closed) return;
|
|
110
|
+
if (!sameAgentIdentity(panel, current)) return finish();
|
|
111
|
+
const view = await this.options.reader.view(path);
|
|
112
|
+
if (closed) return;
|
|
113
|
+
// Identity may change while the session read is in flight.
|
|
114
|
+
const after = await getAgent(this.options.client, panel.pane_id);
|
|
115
|
+
if (closed) return;
|
|
116
|
+
if (!sameAgentIdentity(panel, after)) return finish();
|
|
117
|
+
// Ask can leave the UI before its close record is persisted.
|
|
118
|
+
const result = view.askResults?.[askId];
|
|
119
|
+
if (result) return finish(result);
|
|
120
|
+
}
|
|
121
|
+
})()
|
|
122
|
+
.catch((error) => finish(undefined, error))
|
|
123
|
+
.finally(() => {
|
|
124
|
+
checking = false;
|
|
125
|
+
if (dirty && !closed) check();
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
void (async () => {
|
|
129
|
+
// The reader belongs to this machine. Cover persistence before Enter.
|
|
130
|
+
const close = await this.options.reader.watch(
|
|
131
|
+
[path],
|
|
132
|
+
check,
|
|
133
|
+
(error) => finish(undefined, error),
|
|
134
|
+
controller.signal,
|
|
135
|
+
);
|
|
136
|
+
if (closed) return close();
|
|
137
|
+
unsubscribe = close;
|
|
138
|
+
await submit(controller.signal);
|
|
139
|
+
if (closed) return;
|
|
140
|
+
submitted = true;
|
|
141
|
+
// Read after subscription and submit ACK even if no event was observed.
|
|
142
|
+
check();
|
|
143
|
+
})().catch((error) => finish(undefined, error));
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
refresh(panels: PaneInfo[]): Promise<void> {
|
|
148
|
+
this.panels = panels.filter(
|
|
149
|
+
(panel) =>
|
|
150
|
+
this.options.state.byTerminal(panel.terminal_id) && sessionPath(panel),
|
|
151
|
+
);
|
|
152
|
+
const generation = this.generation;
|
|
153
|
+
const update = async () => {
|
|
154
|
+
if (!this.context || generation !== this.generation) return;
|
|
155
|
+
const paths = [
|
|
156
|
+
...new Set(this.panels.map((panel) => sessionPath(panel)!)),
|
|
157
|
+
].sort();
|
|
158
|
+
const key = JSON.stringify(paths);
|
|
159
|
+
if (key !== this.key) {
|
|
160
|
+
this.controller?.abort();
|
|
161
|
+
this.unsubscribe?.();
|
|
162
|
+
this.unsubscribe = undefined;
|
|
163
|
+
this.key = "";
|
|
164
|
+
const controller = new AbortController();
|
|
165
|
+
this.controller = controller;
|
|
166
|
+
if (paths.length) {
|
|
167
|
+
const close = await this.options.reader.watch(
|
|
168
|
+
paths,
|
|
169
|
+
(path) => {
|
|
170
|
+
if (!controller.signal.aborted) this.check(path);
|
|
171
|
+
},
|
|
172
|
+
(error) => {
|
|
173
|
+
if (controller.signal.aborted) return;
|
|
174
|
+
controller.abort();
|
|
175
|
+
this.key = "";
|
|
176
|
+
this.warning(error);
|
|
177
|
+
},
|
|
178
|
+
controller.signal,
|
|
179
|
+
);
|
|
180
|
+
if (generation !== this.generation || controller.signal.aborted) {
|
|
181
|
+
close();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
this.unsubscribe = close;
|
|
185
|
+
}
|
|
186
|
+
this.key = key;
|
|
187
|
+
}
|
|
188
|
+
// Subscribe before reading to cover changes during setup and restoration.
|
|
189
|
+
for (const path of paths) this.check(path);
|
|
190
|
+
};
|
|
191
|
+
this.refreshTail = this.refreshTail.then(update, update).catch((error) => {
|
|
192
|
+
if (generation === this.generation) this.warning(error);
|
|
193
|
+
});
|
|
194
|
+
return this.refreshTail;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private check(path: string): void {
|
|
198
|
+
const existing = this.checks.get(path);
|
|
199
|
+
if (existing) {
|
|
200
|
+
existing.dirty = true;
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const check = { dirty: true };
|
|
204
|
+
this.checks.set(path, check);
|
|
205
|
+
const generation = this.generation;
|
|
206
|
+
void (async () => {
|
|
207
|
+
while (check.dirty && generation === this.generation && this.context) {
|
|
208
|
+
check.dirty = false;
|
|
209
|
+
for (const panel of this.panels.filter(
|
|
210
|
+
(panel) => sessionPath(panel) === path,
|
|
211
|
+
)) {
|
|
212
|
+
const current = await getAgent(this.options.client, panel.pane_id);
|
|
213
|
+
if (!sameAgentIdentity(panel, current)) continue;
|
|
214
|
+
const view = await this.options.reader.view(path);
|
|
215
|
+
if (generation !== this.generation || !this.context) return;
|
|
216
|
+
const record = this.options.state.byTerminal(current.terminal_id);
|
|
217
|
+
const ask = view.ask;
|
|
218
|
+
if (
|
|
219
|
+
!record ||
|
|
220
|
+
record.paneId !== current.pane_id ||
|
|
221
|
+
ask?.delivery !== "steer" ||
|
|
222
|
+
record.lastQuestionId === ask.toolCallId
|
|
223
|
+
)
|
|
224
|
+
continue;
|
|
225
|
+
this.options.notify(this.context, record, current, ask);
|
|
226
|
+
if (
|
|
227
|
+
this.options.state.markQuestion(record.terminalId, ask.toolCallId)
|
|
228
|
+
)
|
|
229
|
+
this.options.persist();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
})()
|
|
233
|
+
.catch((error) => {
|
|
234
|
+
if (generation === this.generation) this.warning(error);
|
|
235
|
+
})
|
|
236
|
+
.finally(() => {
|
|
237
|
+
if (this.checks.get(path) === check) this.checks.delete(path);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private warning(error: unknown): void {
|
|
242
|
+
if (this.warned) return;
|
|
243
|
+
this.warned = true;
|
|
244
|
+
this.context?.ui.notify(
|
|
245
|
+
`Async question monitoring unavailable: ${error instanceof Error ? error.message : String(error)}. Use agents read to inspect workers; reconnect through /herdr`,
|
|
246
|
+
"warning",
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
}
|
package/src/monitor-record.ts
CHANGED
|
@@ -104,11 +104,13 @@ export function parseMonitoredAgent(
|
|
|
104
104
|
}
|
|
105
105
|
const cwd = optionalString(record, "cwd");
|
|
106
106
|
const lastAssistantId = optionalString(record, "lastAssistantId");
|
|
107
|
+
const lastQuestionId = optionalString(record, "lastQuestionId");
|
|
107
108
|
const name = optionalString(record, "name");
|
|
108
109
|
const activity = parsedActivity(record["activity"]);
|
|
109
110
|
if (
|
|
110
111
|
cwd === false ||
|
|
111
112
|
lastAssistantId === false ||
|
|
113
|
+
lastQuestionId === false ||
|
|
112
114
|
name === false ||
|
|
113
115
|
!activity
|
|
114
116
|
) {
|
|
@@ -123,6 +125,7 @@ export function parseMonitoredAgent(
|
|
|
123
125
|
tabId: record["tabId"],
|
|
124
126
|
...(cwd ? { cwd } : {}),
|
|
125
127
|
...(lastAssistantId ? { lastAssistantId } : {}),
|
|
128
|
+
...(lastQuestionId ? { lastQuestionId } : {}),
|
|
126
129
|
...(name ? { name } : {}),
|
|
127
130
|
};
|
|
128
131
|
}
|
|
@@ -132,6 +135,7 @@ export function recordForPanel(
|
|
|
132
135
|
activity: AgentActivity,
|
|
133
136
|
scope: MonitoredAgent["scope"],
|
|
134
137
|
lastAssistantId?: string,
|
|
138
|
+
lastQuestionId?: string,
|
|
135
139
|
): MonitoredAgent {
|
|
136
140
|
const cwd = panel.foreground_cwd ?? panel.cwd;
|
|
137
141
|
return {
|
|
@@ -144,6 +148,7 @@ export function recordForPanel(
|
|
|
144
148
|
...(panel.name ? { name: panel.name } : {}),
|
|
145
149
|
...(cwd ? { cwd } : {}),
|
|
146
150
|
...(lastAssistantId ? { lastAssistantId } : {}),
|
|
151
|
+
...(lastQuestionId ? { lastQuestionId } : {}),
|
|
147
152
|
};
|
|
148
153
|
}
|
|
149
154
|
|
|
@@ -160,6 +165,7 @@ export function sameMonitorRecord(
|
|
|
160
165
|
left.cwd === right.cwd &&
|
|
161
166
|
left.name === right.name &&
|
|
162
167
|
left.lastAssistantId === right.lastAssistantId &&
|
|
168
|
+
left.lastQuestionId === right.lastQuestionId &&
|
|
163
169
|
sameAgentActivity(left.activity, right.activity)
|
|
164
170
|
);
|
|
165
171
|
}
|
package/src/monitor-state.ts
CHANGED
|
@@ -81,6 +81,13 @@ export class MonitorState {
|
|
|
81
81
|
return this.agents.get(terminalId);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
markQuestion(terminalId: string, id: string): boolean {
|
|
85
|
+
const record = this.byTerminal(terminalId);
|
|
86
|
+
if (!record || record.lastQuestionId === id) return false;
|
|
87
|
+
this.agents.set(terminalId, { ...record, lastQuestionId: id });
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
84
91
|
watch(
|
|
85
92
|
panel: PaneInfo,
|
|
86
93
|
lastAssistantId?: string,
|
|
@@ -102,6 +109,7 @@ export class MonitorState {
|
|
|
102
109
|
reportCurrent
|
|
103
110
|
? undefined
|
|
104
111
|
: (existing?.lastAssistantId ?? lastAssistantId),
|
|
112
|
+
existing?.lastQuestionId,
|
|
105
113
|
);
|
|
106
114
|
if (existing && existing.terminalId !== record.terminalId) {
|
|
107
115
|
this.agents.delete(existing.terminalId);
|
|
@@ -348,6 +356,7 @@ export class MonitorState {
|
|
|
348
356
|
activity,
|
|
349
357
|
record.scope,
|
|
350
358
|
record.lastAssistantId,
|
|
359
|
+
record.lastQuestionId,
|
|
351
360
|
);
|
|
352
361
|
if (!sameMonitorRecord(record, updated)) changed = true;
|
|
353
362
|
if (record.terminalId !== updated.terminalId) {
|
|
@@ -382,12 +391,9 @@ export class MonitorState {
|
|
|
382
391
|
): boolean {
|
|
383
392
|
const record = this.byTerminal(terminalId);
|
|
384
393
|
if (!record || activityTask(record.activity) !== task) return false;
|
|
385
|
-
if (record.scope === "task" && status !== "blocked") {
|
|
386
|
-
this.agents.delete(terminalId);
|
|
387
|
-
return true;
|
|
388
|
-
}
|
|
389
394
|
this.agents.set(terminalId, {
|
|
390
395
|
...record,
|
|
396
|
+
scope: "persistent",
|
|
391
397
|
activity: { phase: "settled", status },
|
|
392
398
|
...(lastAssistantId ? { lastAssistantId } : {}),
|
|
393
399
|
});
|
package/src/monitor.ts
CHANGED
|
@@ -5,10 +5,12 @@ import type {
|
|
|
5
5
|
import { isSettledStatus } from "./activity.js";
|
|
6
6
|
import { getSnapshot } from "./herdr.js";
|
|
7
7
|
import { type HerdrConnection, isDispatchRejected } from "./herdr-client.js";
|
|
8
|
+
import { injectAgentEvent } from "./messages.js";
|
|
8
9
|
import { parseMonitorEvent } from "./monitor-event.js";
|
|
9
10
|
import { MonitorEvents } from "./monitor-events.js";
|
|
11
|
+
import { MonitorQuestions } from "./monitor-questions.js";
|
|
10
12
|
import { MonitorState, type WorkAttempt } from "./monitor-state.js";
|
|
11
|
-
import type
|
|
13
|
+
import { type AssistantReader, SessionReader } from "./session-reader.js";
|
|
12
14
|
import {
|
|
13
15
|
type ClaimedSettlement,
|
|
14
16
|
SettlementReporter,
|
|
@@ -45,6 +47,7 @@ export class AgentMonitor {
|
|
|
45
47
|
private readonly state = new MonitorState();
|
|
46
48
|
private readonly events: MonitorEvents;
|
|
47
49
|
private readonly settlements: SettlementReporter;
|
|
50
|
+
private readonly questions: MonitorQuestions;
|
|
48
51
|
private context: ExtensionContext | undefined;
|
|
49
52
|
private activationGeneration = 0;
|
|
50
53
|
|
|
@@ -54,6 +57,26 @@ export class AgentMonitor {
|
|
|
54
57
|
this.onChange = options.onChange;
|
|
55
58
|
this.onRefresh = options.onRefresh;
|
|
56
59
|
this.selfPaneId = options.selfPaneId;
|
|
60
|
+
const reader = options.reader ?? new SessionReader();
|
|
61
|
+
this.questions = new MonitorQuestions({
|
|
62
|
+
client: this.client,
|
|
63
|
+
reader,
|
|
64
|
+
state: this.state,
|
|
65
|
+
notify: (ctx, record, agent, ask) => {
|
|
66
|
+
injectAgentEvent(pi, ctx, {
|
|
67
|
+
agent,
|
|
68
|
+
ask,
|
|
69
|
+
record,
|
|
70
|
+
status: "question",
|
|
71
|
+
labels: {},
|
|
72
|
+
agentToolName: "agents",
|
|
73
|
+
machine: this.machine,
|
|
74
|
+
machineLabel: options.machineLabel(),
|
|
75
|
+
operatorPrefix: options.operatorPrefix,
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
persist: () => this.persist(),
|
|
79
|
+
});
|
|
57
80
|
this.settlements = new SettlementReporter(
|
|
58
81
|
pi,
|
|
59
82
|
this.client,
|
|
@@ -62,7 +85,7 @@ export class AgentMonitor {
|
|
|
62
85
|
this.persist();
|
|
63
86
|
this.refreshAfterEvent();
|
|
64
87
|
},
|
|
65
|
-
|
|
88
|
+
reader,
|
|
66
89
|
{
|
|
67
90
|
machine: this.machine,
|
|
68
91
|
label: options.machineLabel,
|
|
@@ -87,6 +110,7 @@ export class AgentMonitor {
|
|
|
87
110
|
this.deactivate();
|
|
88
111
|
const generation = this.activationGeneration;
|
|
89
112
|
this.context = ctx;
|
|
113
|
+
this.questions.start(ctx);
|
|
90
114
|
const dropped = this.state.restore(restored, this.selfPaneId);
|
|
91
115
|
if (dropped > 0) {
|
|
92
116
|
ctx.ui.notify(
|
|
@@ -105,6 +129,7 @@ export class AgentMonitor {
|
|
|
105
129
|
this.activationGeneration += 1;
|
|
106
130
|
this.context = undefined;
|
|
107
131
|
this.events.stop();
|
|
132
|
+
this.questions.stop();
|
|
108
133
|
this.settlements.stop();
|
|
109
134
|
}
|
|
110
135
|
|
|
@@ -149,6 +174,7 @@ export class AgentMonitor {
|
|
|
149
174
|
new Error(`${paneId} was unwatched before its work settled`),
|
|
150
175
|
);
|
|
151
176
|
this.persist();
|
|
177
|
+
if (this.list().length === 0) await this.questions.refresh([]);
|
|
152
178
|
await this.events.refresh();
|
|
153
179
|
return true;
|
|
154
180
|
}
|
|
@@ -179,6 +205,15 @@ export class AgentMonitor {
|
|
|
179
205
|
return this.settlements.view(panel);
|
|
180
206
|
}
|
|
181
207
|
|
|
208
|
+
submitAskAnswer(
|
|
209
|
+
panel: PaneInfo,
|
|
210
|
+
askId: string,
|
|
211
|
+
submit: (signal: AbortSignal) => Promise<void>,
|
|
212
|
+
signal: AbortSignal,
|
|
213
|
+
) {
|
|
214
|
+
return this.questions.submitAnswer(panel, askId, submit, signal);
|
|
215
|
+
}
|
|
216
|
+
|
|
182
217
|
acceptWork(attempt: WorkAttempt | undefined): void {
|
|
183
218
|
if (this.state.acceptWork(attempt)) this.persist();
|
|
184
219
|
}
|
|
@@ -232,6 +267,7 @@ export class AgentMonitor {
|
|
|
232
267
|
return;
|
|
233
268
|
}
|
|
234
269
|
if (this.list().length === 0) {
|
|
270
|
+
await this.questions.refresh([]);
|
|
235
271
|
if (persistRestoration) this.persist();
|
|
236
272
|
else this.onRefresh();
|
|
237
273
|
return;
|
|
@@ -251,6 +287,7 @@ export class AgentMonitor {
|
|
|
251
287
|
}
|
|
252
288
|
if (changed || persistRestoration) this.persist();
|
|
253
289
|
else this.onRefresh();
|
|
290
|
+
await this.questions.refresh(snapshot.agents);
|
|
254
291
|
for (const completion of completions) {
|
|
255
292
|
void this.report(completion);
|
|
256
293
|
}
|
|
@@ -290,6 +327,7 @@ export class AgentMonitor {
|
|
|
290
327
|
}
|
|
291
328
|
|
|
292
329
|
private refreshAfterEvent(): void {
|
|
330
|
+
if (this.list().length === 0) void this.questions.refresh([]);
|
|
293
331
|
// MonitorEvents reports failures through onWarning before rejecting awaited updates.
|
|
294
332
|
void this.events.refresh().catch(() => undefined);
|
|
295
333
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// @howaboua/pi-shepherdr managed bridge
|
|
2
|
+
import { watch } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { basename, dirname, join } from "node:path";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Watch only named sessions' directories, including file replacement.
|
|
8
|
+
* @param {string[]} paths
|
|
9
|
+
* @param {(path: string) => void} onChange
|
|
10
|
+
* @param {(error: Error) => void} onError
|
|
11
|
+
* @param {AbortSignal} [signal]
|
|
12
|
+
*/
|
|
13
|
+
export function watchSessions(paths, onChange, onError, signal) {
|
|
14
|
+
/** @type {import('node:fs').FSWatcher[]} */
|
|
15
|
+
const watchers = [];
|
|
16
|
+
/** @type {Map<string, NodeJS.Timeout>} */
|
|
17
|
+
const timers = new Map();
|
|
18
|
+
/** @type {Map<string, Map<string, string>>} */
|
|
19
|
+
const directories = new Map();
|
|
20
|
+
let closed = false;
|
|
21
|
+
const close = () => {
|
|
22
|
+
if (closed) return;
|
|
23
|
+
closed = true;
|
|
24
|
+
signal?.removeEventListener("abort", close);
|
|
25
|
+
for (const watcher of watchers) watcher.close();
|
|
26
|
+
for (const timer of timers.values()) clearTimeout(timer);
|
|
27
|
+
timers.clear();
|
|
28
|
+
};
|
|
29
|
+
try {
|
|
30
|
+
for (const path of new Set(paths)) {
|
|
31
|
+
const expanded =
|
|
32
|
+
path === "~"
|
|
33
|
+
? homedir()
|
|
34
|
+
: path.startsWith("~/")
|
|
35
|
+
? join(homedir(), path.slice(2))
|
|
36
|
+
: path;
|
|
37
|
+
const directory = dirname(expanded);
|
|
38
|
+
const files = directories.get(directory) ?? new Map();
|
|
39
|
+
files.set(basename(expanded), path);
|
|
40
|
+
directories.set(directory, files);
|
|
41
|
+
}
|
|
42
|
+
for (const [directory, files] of directories) {
|
|
43
|
+
const watcher = watch(
|
|
44
|
+
directory,
|
|
45
|
+
{ persistent: false },
|
|
46
|
+
(_event, filename) => {
|
|
47
|
+
for (const [name, path] of files) {
|
|
48
|
+
if (filename !== null && filename.toString() !== name) continue;
|
|
49
|
+
if (timers.has(path) || closed) continue;
|
|
50
|
+
const timer = setTimeout(() => {
|
|
51
|
+
timers.delete(path);
|
|
52
|
+
if (!closed) onChange(path);
|
|
53
|
+
}, 25);
|
|
54
|
+
timer.unref();
|
|
55
|
+
timers.set(path, timer);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
watcher.on("error", (error) => {
|
|
60
|
+
close();
|
|
61
|
+
onError(error);
|
|
62
|
+
});
|
|
63
|
+
watchers.push(watcher);
|
|
64
|
+
}
|
|
65
|
+
signal?.addEventListener("abort", close, { once: true });
|
|
66
|
+
if (signal?.aborted) close();
|
|
67
|
+
return close;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
close();
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// @howaboua/pi-shepherdr managed bridge
|
|
2
|
+
|
|
2
3
|
import { open, stat } from "node:fs/promises";
|
|
3
4
|
import { homedir } from "node:os";
|
|
4
5
|
import { join } from "node:path";
|
|
@@ -105,6 +106,7 @@ function askFromInput(toolCallId, input) {
|
|
|
105
106
|
return {
|
|
106
107
|
toolCallId,
|
|
107
108
|
handoff: input.handoff === true,
|
|
109
|
+
...(input.delivery === "steer" ? { delivery: "steer" } : {}),
|
|
108
110
|
prompts,
|
|
109
111
|
};
|
|
110
112
|
}
|
|
@@ -114,6 +116,7 @@ function askResult(message) {
|
|
|
114
116
|
return undefined;
|
|
115
117
|
const id = message.toolCallId ?? message.tool_call_id;
|
|
116
118
|
if (typeof id !== "string") return undefined;
|
|
119
|
+
if (message.details?.pending === true) return undefined;
|
|
117
120
|
if (message.isError === true) return [id, { status: "rejected" }];
|
|
118
121
|
if (message.isError !== false) return [id, { status: "unknown" }];
|
|
119
122
|
if (message.details?.dismissed === true) return [id, { status: "rejected" }];
|
|
@@ -147,7 +150,12 @@ function askResult(message) {
|
|
|
147
150
|
return [
|
|
148
151
|
id,
|
|
149
152
|
{
|
|
150
|
-
status:
|
|
153
|
+
status:
|
|
154
|
+
responses &&
|
|
155
|
+
responses.length > 0 &&
|
|
156
|
+
responses.length === rawResponseCount
|
|
157
|
+
? "accepted"
|
|
158
|
+
: "unknown",
|
|
151
159
|
...(responses && responses.length === rawResponseCount
|
|
152
160
|
? { responses }
|
|
153
161
|
: {}),
|
|
@@ -203,6 +211,24 @@ async function sessionView(path, size) {
|
|
|
203
211
|
}
|
|
204
212
|
}
|
|
205
213
|
}
|
|
214
|
+
if (entry.type === "custom" && entry.customType === "pi-ask-pending") {
|
|
215
|
+
const update = entry.data;
|
|
216
|
+
if (
|
|
217
|
+
update?.version === 1 &&
|
|
218
|
+
update.state === "closed" &&
|
|
219
|
+
typeof update.id === "string"
|
|
220
|
+
) {
|
|
221
|
+
resolved.add(update.id);
|
|
222
|
+
const result = askResult({
|
|
223
|
+
role: "toolResult",
|
|
224
|
+
toolName: "ask",
|
|
225
|
+
toolCallId: update.id,
|
|
226
|
+
isError: false,
|
|
227
|
+
details: update,
|
|
228
|
+
});
|
|
229
|
+
if (result && !askResults.has(result[0])) askResults.set(...result);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
206
232
|
if (
|
|
207
233
|
!input &&
|
|
208
234
|
entry.type === "custom_message" &&
|
|
@@ -219,6 +245,7 @@ async function sessionView(path, size) {
|
|
|
219
245
|
if (result && !askResults.has(result[0])) askResults.set(...result);
|
|
220
246
|
if (
|
|
221
247
|
(message.role === "toolResult" || message.role === "tool") &&
|
|
248
|
+
message.details?.pending !== true &&
|
|
222
249
|
typeof (message.toolCallId ?? message.tool_call_id) === "string"
|
|
223
250
|
)
|
|
224
251
|
resolved.add(message.toolCallId ?? message.tool_call_id);
|
|
@@ -239,7 +266,10 @@ async function sessionView(path, size) {
|
|
|
239
266
|
.reverse()
|
|
240
267
|
.map(askCall)
|
|
241
268
|
.find(
|
|
242
|
-
(candidate) =>
|
|
269
|
+
(candidate) =>
|
|
270
|
+
candidate &&
|
|
271
|
+
candidate.delivery !== "steer" &&
|
|
272
|
+
!resolved.has(candidate.toolCallId),
|
|
243
273
|
);
|
|
244
274
|
}
|
|
245
275
|
if (typeof entry.parentId !== "string") return true;
|
package/src/remote/shepherdr.mjs
CHANGED
|
@@ -10,8 +10,9 @@ import {
|
|
|
10
10
|
} from "./shepherdr-context.mjs";
|
|
11
11
|
import { sendPeerMessage } from "./shepherdr-peer.mjs";
|
|
12
12
|
import { readSessionView } from "./shepherdr-session.mjs";
|
|
13
|
+
import { watchSessions } from "./shepherdr-session-watch.mjs";
|
|
13
14
|
|
|
14
|
-
const BRIDGE_VERSION =
|
|
15
|
+
const BRIDGE_VERSION = 14;
|
|
15
16
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
16
17
|
const subscriptions = new Map();
|
|
17
18
|
const contextRelays = new Map();
|
|
@@ -344,6 +345,25 @@ async function handle(message) {
|
|
|
344
345
|
await subscribe(message.id, message.subscriptions ?? []);
|
|
345
346
|
return { subscribed: true };
|
|
346
347
|
}
|
|
348
|
+
if (message.op === "session_subscribe") {
|
|
349
|
+
const close = watchSessions(
|
|
350
|
+
message.paths ?? [],
|
|
351
|
+
(path) =>
|
|
352
|
+
send({
|
|
353
|
+
subscription: message.id,
|
|
354
|
+
event: { event: "session.changed", data: { path } },
|
|
355
|
+
}),
|
|
356
|
+
(error) => {
|
|
357
|
+
process.stderr.write(
|
|
358
|
+
`Session monitoring disconnected: ${error.message}\n`,
|
|
359
|
+
);
|
|
360
|
+
process.exitCode = 1;
|
|
361
|
+
setTimeout(() => process.exit(), 0);
|
|
362
|
+
},
|
|
363
|
+
);
|
|
364
|
+
subscriptions.set(message.id, close);
|
|
365
|
+
return { subscribed: true };
|
|
366
|
+
}
|
|
347
367
|
if (message.op === "unsubscribe") {
|
|
348
368
|
subscriptions.get(message.subscription)?.();
|
|
349
369
|
subscriptions.delete(message.subscription);
|