@howaboua/pi-shepherdr 0.2.22 → 0.2.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -18
- package/README.md +79 -58
- package/biome.json +1 -1
- package/index.ts +7 -3
- package/package.json +14 -9
- package/src/agents-discovery.ts +42 -27
- package/src/agents-focus.ts +2 -2
- package/src/agents-reply.ts +1 -1
- package/src/agents-spawn.ts +143 -137
- package/src/agents-tool.ts +3 -3
- package/src/agents-work.ts +33 -9
- package/src/attachment-notes.ts +14 -2
- package/src/attachment-protocol.ts +21 -4
- package/src/attachment-routing.ts +1 -1
- package/src/attachment-target.ts +5 -2
- package/src/attachment.ts +2 -3
- package/src/board/host.ts +2 -2
- package/src/board/membership.ts +1 -1
- package/src/{control-panel-actions.ts → control-panel/actions.ts} +2 -2
- package/src/{control-panel.ts → control-panel/index.ts} +4 -4
- package/src/{control-panel-items.ts → control-panel/items.ts} +3 -3
- package/src/{control-panel-view.ts → control-panel/view.ts} +3 -3
- package/src/controller.ts +6 -3
- package/src/fleet/index.ts +384 -0
- package/src/fleet/inspection.ts +141 -0
- package/src/fleet/machine.ts +52 -0
- package/src/fleet/monitor.ts +71 -0
- package/src/fleet/persistence.ts +42 -0
- package/src/fleet/reconnect.ts +75 -0
- package/src/fleet/setup.ts +92 -0
- package/src/launch.ts +14 -2
- package/src/messages/attachments.ts +39 -0
- package/src/messages/attribution.ts +75 -0
- package/src/messages/event-details.ts +69 -0
- package/src/messages/events.ts +189 -0
- package/src/messages/focus.ts +38 -0
- package/src/messages/guidance.ts +83 -0
- package/src/messages/render.ts +89 -0
- package/src/messages/voice-announcements.ts +79 -0
- package/src/monitor-extension-messages.ts +14 -1
- package/src/monitor.ts +4 -2
- package/src/peer-inbox.ts +2 -2
- package/src/profile-prompt.ts +32 -0
- package/src/profiles.ts +5 -25
- package/src/remote/client.ts +127 -0
- package/src/remote/context-relay.ts +84 -0
- package/src/remote/deployment.ts +179 -0
- package/src/remote/focus-channels.ts +89 -0
- package/src/remote/transport.ts +369 -0
- package/src/settlement/collector.ts +122 -0
- package/src/settlement/delivery.ts +144 -0
- package/src/{settlement.ts → settlement/index.ts} +23 -233
- package/src/shared-context-host.ts +33 -11
- package/src/shared-context.ts +10 -4
- package/src/spawn-context.ts +20 -17
- package/src/voice-handoff.ts +1 -1
- package/src/worker-role.ts +83 -0
- package/src/worker-service.ts +74 -89
- package/src/fleet.ts +0 -702
- package/src/messages.ts +0 -629
- package/src/remote-client.ts +0 -751
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { isSettledStatus } from "../activity.js";
|
|
2
|
+
import { getAgent, getSnapshot, sessionPath } from "../herdr.js";
|
|
3
|
+
import type { HerdrConnection } from "../herdr-client.js";
|
|
4
|
+
import { type AssistantReader, SessionReader } from "../session-reader.js";
|
|
5
|
+
import type {
|
|
6
|
+
ExtensionMessage,
|
|
7
|
+
LatestAssistant,
|
|
8
|
+
MonitoredAgent,
|
|
9
|
+
PaneInfo,
|
|
10
|
+
SessionView,
|
|
11
|
+
SettledAgentStatus,
|
|
12
|
+
} from "../types.js";
|
|
13
|
+
|
|
14
|
+
interface SettlementLabels {
|
|
15
|
+
tab?: string;
|
|
16
|
+
workspace?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface CollectedSettlement {
|
|
20
|
+
agent: PaneInfo;
|
|
21
|
+
extensionMessages?: ExtensionMessage[];
|
|
22
|
+
labels: SettlementLabels;
|
|
23
|
+
reply?: LatestAssistant;
|
|
24
|
+
status: SettledAgentStatus;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface CollectedSettlementWithSession extends CollectedSettlement {
|
|
28
|
+
session: SessionView;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function blockedSettlement(
|
|
32
|
+
record: MonitoredAgent,
|
|
33
|
+
): CollectedSettlementWithSession {
|
|
34
|
+
return {
|
|
35
|
+
agent: {
|
|
36
|
+
agent: "pi",
|
|
37
|
+
agent_status: "blocked",
|
|
38
|
+
...(record.cwd ? { cwd: record.cwd } : {}),
|
|
39
|
+
...(record.name ? { name: record.name } : {}),
|
|
40
|
+
pane_id: record.paneId,
|
|
41
|
+
tab_id: record.tabId,
|
|
42
|
+
terminal_id: record.terminalId,
|
|
43
|
+
workspace_id: record.workspaceId,
|
|
44
|
+
},
|
|
45
|
+
labels: {},
|
|
46
|
+
session: {},
|
|
47
|
+
status: "blocked",
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class SettlementCollector {
|
|
52
|
+
private readonly client: HerdrConnection;
|
|
53
|
+
private readonly reader: AssistantReader;
|
|
54
|
+
|
|
55
|
+
constructor(
|
|
56
|
+
client: HerdrConnection,
|
|
57
|
+
reader: AssistantReader = new SessionReader(),
|
|
58
|
+
) {
|
|
59
|
+
this.client = client;
|
|
60
|
+
this.reader = reader;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async latest(panel: PaneInfo): Promise<LatestAssistant | undefined> {
|
|
64
|
+
try {
|
|
65
|
+
return await this.reader.latest(sessionPath(panel));
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (panel.agent_status !== "blocked") throw error;
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
view(panel: PaneInfo): Promise<SessionView> {
|
|
73
|
+
return this.reader.view(sessionPath(panel));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async collect(
|
|
77
|
+
record: MonitoredAgent,
|
|
78
|
+
reportedStatus?: "blocked",
|
|
79
|
+
): Promise<CollectedSettlementWithSession | undefined> {
|
|
80
|
+
const [current, snapshot] = await Promise.all([
|
|
81
|
+
getAgent(this.client, record.paneId),
|
|
82
|
+
getSnapshot(this.client),
|
|
83
|
+
]);
|
|
84
|
+
if (current.terminal_id !== record.terminalId || current.agent !== "pi") {
|
|
85
|
+
return reportedStatus === "blocked"
|
|
86
|
+
? blockedSettlement(record)
|
|
87
|
+
: undefined;
|
|
88
|
+
}
|
|
89
|
+
const status =
|
|
90
|
+
reportedStatus ??
|
|
91
|
+
(isSettledStatus(current.agent_status)
|
|
92
|
+
? current.agent_status
|
|
93
|
+
: undefined);
|
|
94
|
+
if (!status) return undefined;
|
|
95
|
+
let view: SessionView;
|
|
96
|
+
try {
|
|
97
|
+
view = await this.reader.view(sessionPath(current));
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (status !== "blocked") throw error;
|
|
100
|
+
view = {};
|
|
101
|
+
}
|
|
102
|
+
const workspace = snapshot.workspaces.find(
|
|
103
|
+
(candidate) => candidate.workspace_id === current.workspace_id,
|
|
104
|
+
)?.label;
|
|
105
|
+
const tab = snapshot.tabs.find(
|
|
106
|
+
(candidate) => candidate.tab_id === current.tab_id,
|
|
107
|
+
)?.label;
|
|
108
|
+
return {
|
|
109
|
+
agent: current,
|
|
110
|
+
labels: {
|
|
111
|
+
...(workspace ? { workspace } : {}),
|
|
112
|
+
...(tab ? { tab } : {}),
|
|
113
|
+
},
|
|
114
|
+
...(view.assistant ? { reply: view.assistant } : {}),
|
|
115
|
+
...(view.extensionMessages
|
|
116
|
+
? { extensionMessages: view.extensionMessages }
|
|
117
|
+
: {}),
|
|
118
|
+
session: view,
|
|
119
|
+
status,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { activityAttemptId } from "../activity.js";
|
|
3
|
+
import { injectAgentEvent } from "../messages/events.js";
|
|
4
|
+
import type { WorkAttempt } from "../monitor-state.js";
|
|
5
|
+
import type {
|
|
6
|
+
ExtensionReply,
|
|
7
|
+
LatestAssistant,
|
|
8
|
+
MonitoredAgent,
|
|
9
|
+
} from "../types.js";
|
|
10
|
+
import type {
|
|
11
|
+
CollectedSettlement,
|
|
12
|
+
CollectedSettlementWithSession,
|
|
13
|
+
} from "./collector.js";
|
|
14
|
+
import type { SettlementLifecycle } from "./index.js";
|
|
15
|
+
|
|
16
|
+
export interface ClaimedSettlement extends CollectedSettlement {
|
|
17
|
+
blockedMessage?: string;
|
|
18
|
+
record: MonitoredAgent;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface SettlementClaim {
|
|
22
|
+
replyingTo?: ExtensionReply;
|
|
23
|
+
cleanup(): void;
|
|
24
|
+
reject(error: Error): void;
|
|
25
|
+
resolve(settlement: ClaimedSettlement): void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Claims are per-attempt capabilities; delivery chooses exactly one waiter or controller. */
|
|
29
|
+
export class SettlementDelivery {
|
|
30
|
+
private readonly claims = new Map<string, SettlementClaim>();
|
|
31
|
+
private readonly pi: ExtensionAPI;
|
|
32
|
+
private readonly origin: {
|
|
33
|
+
machine: string;
|
|
34
|
+
label: () => string;
|
|
35
|
+
operatorPrefix: string;
|
|
36
|
+
};
|
|
37
|
+
constructor(
|
|
38
|
+
pi: ExtensionAPI,
|
|
39
|
+
origin: { machine: string; label: () => string; operatorPrefix: string },
|
|
40
|
+
) {
|
|
41
|
+
this.pi = pi;
|
|
42
|
+
this.origin = origin;
|
|
43
|
+
}
|
|
44
|
+
replyingTo(attemptId: string) {
|
|
45
|
+
return this.claims.get(attemptId)?.replyingTo;
|
|
46
|
+
}
|
|
47
|
+
stop() {
|
|
48
|
+
for (const claim of this.claims.values()) {
|
|
49
|
+
claim.cleanup();
|
|
50
|
+
claim.reject(new Error("Shepherdr monitor stopped"));
|
|
51
|
+
}
|
|
52
|
+
this.claims.clear();
|
|
53
|
+
}
|
|
54
|
+
claim(
|
|
55
|
+
attempt: WorkAttempt,
|
|
56
|
+
signal: AbortSignal,
|
|
57
|
+
replyingTo?: ExtensionReply,
|
|
58
|
+
): Promise<ClaimedSettlement> | undefined {
|
|
59
|
+
signal.throwIfAborted();
|
|
60
|
+
// Updates share the worker's current settlement, without stealing its waiter.
|
|
61
|
+
if (this.claims.has(attempt.attemptId)) return;
|
|
62
|
+
return new Promise<ClaimedSettlement>((resolve, reject) => {
|
|
63
|
+
const abort = () => {
|
|
64
|
+
const claim = this.claims.get(attempt.attemptId);
|
|
65
|
+
if (!claim) return;
|
|
66
|
+
this.claims.delete(attempt.attemptId);
|
|
67
|
+
claim.cleanup();
|
|
68
|
+
reject(
|
|
69
|
+
signal.reason instanceof Error
|
|
70
|
+
? signal.reason
|
|
71
|
+
: new Error("Shepherdr wait aborted"),
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
const claim: SettlementClaim = {
|
|
75
|
+
...(replyingTo ? { replyingTo } : {}),
|
|
76
|
+
cleanup: () => signal.removeEventListener("abort", abort),
|
|
77
|
+
reject,
|
|
78
|
+
resolve,
|
|
79
|
+
};
|
|
80
|
+
this.claims.set(attempt.attemptId, claim);
|
|
81
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
releaseClaim(attempt: WorkAttempt | undefined, error: unknown): void {
|
|
86
|
+
if (!attempt) return;
|
|
87
|
+
this.releaseAttempt(attempt.attemptId, error);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
releaseAgentClaim(record: MonitoredAgent, error: unknown): void {
|
|
91
|
+
const attemptId = activityAttemptId(record.activity);
|
|
92
|
+
if (attemptId) this.releaseAttempt(attemptId, error);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
releaseAttempt(attemptId: string, error: unknown): void {
|
|
96
|
+
const claim = this.claims.get(attemptId);
|
|
97
|
+
if (!claim) return;
|
|
98
|
+
this.claims.delete(attemptId);
|
|
99
|
+
claim.cleanup();
|
|
100
|
+
claim.reject(error instanceof Error ? error : new Error(String(error)));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
deliverSettlement(
|
|
104
|
+
lifecycle: SettlementLifecycle,
|
|
105
|
+
record: MonitoredAgent,
|
|
106
|
+
settlement: CollectedSettlementWithSession,
|
|
107
|
+
attemptId: string | undefined,
|
|
108
|
+
blockedMessage: string | undefined,
|
|
109
|
+
reply: LatestAssistant | undefined,
|
|
110
|
+
): void {
|
|
111
|
+
const claim = attemptId ? this.claims.get(attemptId) : undefined;
|
|
112
|
+
if (claim && attemptId) {
|
|
113
|
+
this.claims.delete(attemptId);
|
|
114
|
+
claim.cleanup();
|
|
115
|
+
const {
|
|
116
|
+
reply: _latestReply,
|
|
117
|
+
session: _session,
|
|
118
|
+
...claimedSettlement
|
|
119
|
+
} = settlement;
|
|
120
|
+
claim.resolve({
|
|
121
|
+
...claimedSettlement,
|
|
122
|
+
record,
|
|
123
|
+
...(blockedMessage ? { blockedMessage } : {}),
|
|
124
|
+
...(reply ? { reply } : {}),
|
|
125
|
+
});
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
injectAgentEvent(this.pi, lifecycle.context, {
|
|
129
|
+
agent: settlement.agent,
|
|
130
|
+
agentToolName: "agents",
|
|
131
|
+
...(settlement.extensionMessages
|
|
132
|
+
? { extensionMessages: settlement.extensionMessages }
|
|
133
|
+
: {}),
|
|
134
|
+
machine: this.origin.machine,
|
|
135
|
+
machineLabel: this.origin.label(),
|
|
136
|
+
operatorPrefix: this.origin.operatorPrefix,
|
|
137
|
+
...(blockedMessage ? { blockedMessage } : {}),
|
|
138
|
+
labels: settlement.labels,
|
|
139
|
+
record,
|
|
140
|
+
...(reply ? { reply } : {}),
|
|
141
|
+
status: settlement.status,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -6,51 +6,23 @@ import {
|
|
|
6
6
|
activityAttemptId,
|
|
7
7
|
activityExpectedUser,
|
|
8
8
|
activityTask,
|
|
9
|
-
|
|
10
|
-
} from "
|
|
11
|
-
import {
|
|
12
|
-
import type {
|
|
13
|
-
import { injectAgentEvent } from "./messages.js";
|
|
14
|
-
import type { MonitorState, WorkAttempt } from "./monitor-state.js";
|
|
15
|
-
import { type AssistantReader, SessionReader } from "./session-reader.js";
|
|
9
|
+
} from "../activity.js";
|
|
10
|
+
import type { HerdrConnection } from "../herdr-client.js";
|
|
11
|
+
import type { MonitorState, WorkAttempt } from "../monitor-state.js";
|
|
12
|
+
import type { AssistantReader } from "../session-reader.js";
|
|
16
13
|
import type {
|
|
17
|
-
ExtensionMessage,
|
|
18
14
|
ExtensionReply,
|
|
19
15
|
LatestAssistant,
|
|
20
16
|
MonitoredAgent,
|
|
21
17
|
PaneInfo,
|
|
22
18
|
SessionView,
|
|
23
19
|
SettledAgentStatus,
|
|
24
|
-
} from "
|
|
20
|
+
} from "../types.js";
|
|
21
|
+
import { blockedSettlement, SettlementCollector } from "./collector.js";
|
|
22
|
+
import { SettlementDelivery } from "./delivery.js";
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
workspace?: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface CollectedSettlement {
|
|
32
|
-
agent: PaneInfo;
|
|
33
|
-
extensionMessages?: ExtensionMessage[];
|
|
34
|
-
labels: SettlementLabels;
|
|
35
|
-
reply?: LatestAssistant;
|
|
36
|
-
status: SettledAgentStatus;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface CollectedSettlementWithSession extends CollectedSettlement {
|
|
40
|
-
session: SessionView;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export interface ClaimedSettlement extends CollectedSettlement {
|
|
44
|
-
blockedMessage?: string;
|
|
45
|
-
record: MonitoredAgent;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
interface SettlementClaim {
|
|
49
|
-
replyingTo?: ExtensionReply;
|
|
50
|
-
cleanup(): void;
|
|
51
|
-
reject(error: Error): void;
|
|
52
|
-
resolve(settlement: ClaimedSettlement): void;
|
|
53
|
-
}
|
|
24
|
+
export type { ClaimedSettlement } from "./delivery.js";
|
|
25
|
+
export { SettlementCollector };
|
|
54
26
|
|
|
55
27
|
interface SettlementRetry {
|
|
56
28
|
attemptId: string | undefined;
|
|
@@ -66,26 +38,6 @@ function attemptKey(terminalId: string, attemptId: string | undefined): string {
|
|
|
66
38
|
return JSON.stringify([terminalId, attemptId ?? null]);
|
|
67
39
|
}
|
|
68
40
|
|
|
69
|
-
function blockedSettlement(
|
|
70
|
-
record: MonitoredAgent,
|
|
71
|
-
): CollectedSettlementWithSession {
|
|
72
|
-
return {
|
|
73
|
-
agent: {
|
|
74
|
-
agent: "pi",
|
|
75
|
-
agent_status: "blocked",
|
|
76
|
-
...(record.cwd ? { cwd: record.cwd } : {}),
|
|
77
|
-
...(record.name ? { name: record.name } : {}),
|
|
78
|
-
pane_id: record.paneId,
|
|
79
|
-
tab_id: record.tabId,
|
|
80
|
-
terminal_id: record.terminalId,
|
|
81
|
-
workspace_id: record.workspaceId,
|
|
82
|
-
},
|
|
83
|
-
labels: {},
|
|
84
|
-
session: {},
|
|
85
|
-
status: "blocked",
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
41
|
export interface SettlementRequest {
|
|
90
42
|
blockedMessage?: string;
|
|
91
43
|
record: MonitoredAgent;
|
|
@@ -106,91 +58,12 @@ interface PendingBlockedSettlement {
|
|
|
106
58
|
retry?: SettlementRetry;
|
|
107
59
|
}
|
|
108
60
|
|
|
109
|
-
export class SettlementCollector {
|
|
110
|
-
private readonly client: HerdrConnection;
|
|
111
|
-
private readonly reader: AssistantReader;
|
|
112
|
-
|
|
113
|
-
constructor(
|
|
114
|
-
client: HerdrConnection,
|
|
115
|
-
reader: AssistantReader = new SessionReader(),
|
|
116
|
-
) {
|
|
117
|
-
this.client = client;
|
|
118
|
-
this.reader = reader;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async latest(panel: PaneInfo): Promise<LatestAssistant | undefined> {
|
|
122
|
-
try {
|
|
123
|
-
return await this.reader.latest(sessionPath(panel));
|
|
124
|
-
} catch (error) {
|
|
125
|
-
if (panel.agent_status !== "blocked") throw error;
|
|
126
|
-
return undefined;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
view(panel: PaneInfo): Promise<SessionView> {
|
|
131
|
-
return this.reader.view(sessionPath(panel));
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async collect(
|
|
135
|
-
record: MonitoredAgent,
|
|
136
|
-
reportedStatus?: "blocked",
|
|
137
|
-
): Promise<CollectedSettlementWithSession | undefined> {
|
|
138
|
-
const [current, snapshot] = await Promise.all([
|
|
139
|
-
getAgent(this.client, record.paneId),
|
|
140
|
-
getSnapshot(this.client),
|
|
141
|
-
]);
|
|
142
|
-
if (current.terminal_id !== record.terminalId || current.agent !== "pi") {
|
|
143
|
-
return reportedStatus === "blocked"
|
|
144
|
-
? blockedSettlement(record)
|
|
145
|
-
: undefined;
|
|
146
|
-
}
|
|
147
|
-
const status =
|
|
148
|
-
reportedStatus ??
|
|
149
|
-
(isSettledStatus(current.agent_status)
|
|
150
|
-
? current.agent_status
|
|
151
|
-
: undefined);
|
|
152
|
-
if (!status) return undefined;
|
|
153
|
-
let view: SessionView;
|
|
154
|
-
try {
|
|
155
|
-
view = await this.reader.view(sessionPath(current));
|
|
156
|
-
} catch (error) {
|
|
157
|
-
if (status !== "blocked") throw error;
|
|
158
|
-
view = {};
|
|
159
|
-
}
|
|
160
|
-
const workspace = snapshot.workspaces.find(
|
|
161
|
-
(candidate) => candidate.workspace_id === current.workspace_id,
|
|
162
|
-
)?.label;
|
|
163
|
-
const tab = snapshot.tabs.find(
|
|
164
|
-
(candidate) => candidate.tab_id === current.tab_id,
|
|
165
|
-
)?.label;
|
|
166
|
-
return {
|
|
167
|
-
agent: current,
|
|
168
|
-
labels: {
|
|
169
|
-
...(workspace ? { workspace } : {}),
|
|
170
|
-
...(tab ? { tab } : {}),
|
|
171
|
-
},
|
|
172
|
-
...(view.assistant ? { reply: view.assistant } : {}),
|
|
173
|
-
...(view.extensionMessages
|
|
174
|
-
? { extensionMessages: view.extensionMessages }
|
|
175
|
-
: {}),
|
|
176
|
-
session: view,
|
|
177
|
-
status,
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
61
|
export class SettlementReporter {
|
|
183
62
|
private readonly collector: SettlementCollector;
|
|
184
63
|
private readonly persist: () => void;
|
|
185
|
-
private readonly pi: ExtensionAPI;
|
|
186
|
-
private readonly origin: {
|
|
187
|
-
machine: string;
|
|
188
|
-
label: () => string;
|
|
189
|
-
operatorPrefix: string;
|
|
190
|
-
};
|
|
191
64
|
private readonly reporting = new Map<string, SettledAgentStatus>();
|
|
192
65
|
private readonly pendingBlocked = new Map<string, PendingBlockedSettlement>();
|
|
193
|
-
private readonly
|
|
66
|
+
private readonly delivery: SettlementDelivery;
|
|
194
67
|
private readonly retries = new Map<string, NodeJS.Timeout>();
|
|
195
68
|
private readonly state: MonitorState;
|
|
196
69
|
|
|
@@ -202,11 +75,10 @@ export class SettlementReporter {
|
|
|
202
75
|
reader: AssistantReader | undefined,
|
|
203
76
|
origin: { machine: string; label: () => string; operatorPrefix: string },
|
|
204
77
|
) {
|
|
205
|
-
this.
|
|
78
|
+
this.delivery = new SettlementDelivery(pi, origin);
|
|
206
79
|
this.collector = new SettlementCollector(client, reader);
|
|
207
80
|
this.state = state;
|
|
208
81
|
this.persist = persist;
|
|
209
|
-
this.origin = origin;
|
|
210
82
|
}
|
|
211
83
|
|
|
212
84
|
latest(panel: PaneInfo): Promise<LatestAssistant | undefined> {
|
|
@@ -220,11 +92,7 @@ export class SettlementReporter {
|
|
|
220
92
|
stop(): void {
|
|
221
93
|
this.reporting.clear();
|
|
222
94
|
this.pendingBlocked.clear();
|
|
223
|
-
|
|
224
|
-
claim.cleanup();
|
|
225
|
-
claim.reject(new Error("Shepherdr monitor stopped"));
|
|
226
|
-
}
|
|
227
|
-
this.claims.clear();
|
|
95
|
+
this.delivery.stop();
|
|
228
96
|
for (const timer of this.retries.values()) clearTimeout(timer);
|
|
229
97
|
this.retries.clear();
|
|
230
98
|
}
|
|
@@ -233,51 +101,15 @@ export class SettlementReporter {
|
|
|
233
101
|
attempt: WorkAttempt,
|
|
234
102
|
signal: AbortSignal,
|
|
235
103
|
replyingTo?: ExtensionReply,
|
|
236
|
-
)
|
|
237
|
-
|
|
238
|
-
// Updates share the worker's current settlement, without stealing its waiter.
|
|
239
|
-
if (this.claims.has(attempt.attemptId)) return;
|
|
240
|
-
return new Promise<ClaimedSettlement>((resolve, reject) => {
|
|
241
|
-
const abort = () => {
|
|
242
|
-
const claim = this.claims.get(attempt.attemptId);
|
|
243
|
-
if (!claim) return;
|
|
244
|
-
this.claims.delete(attempt.attemptId);
|
|
245
|
-
claim.cleanup();
|
|
246
|
-
reject(
|
|
247
|
-
signal.reason instanceof Error
|
|
248
|
-
? signal.reason
|
|
249
|
-
: new Error("Shepherdr wait aborted"),
|
|
250
|
-
);
|
|
251
|
-
};
|
|
252
|
-
const claim: SettlementClaim = {
|
|
253
|
-
...(replyingTo ? { replyingTo } : {}),
|
|
254
|
-
cleanup: () => signal.removeEventListener("abort", abort),
|
|
255
|
-
reject,
|
|
256
|
-
resolve,
|
|
257
|
-
};
|
|
258
|
-
this.claims.set(attempt.attemptId, claim);
|
|
259
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
releaseClaim(attempt: WorkAttempt | undefined, error: unknown): void {
|
|
264
|
-
if (!attempt) return;
|
|
265
|
-
this.releaseAttempt(attempt.attemptId, error);
|
|
104
|
+
) {
|
|
105
|
+
return this.delivery.claim(attempt, signal, replyingTo);
|
|
266
106
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const attemptId = activityAttemptId(record.activity);
|
|
270
|
-
if (attemptId) this.releaseAttempt(attemptId, error);
|
|
107
|
+
releaseClaim(attempt: WorkAttempt | undefined, error: unknown) {
|
|
108
|
+
this.delivery.releaseClaim(attempt, error);
|
|
271
109
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const claim = this.claims.get(attemptId);
|
|
275
|
-
if (!claim) return;
|
|
276
|
-
this.claims.delete(attemptId);
|
|
277
|
-
claim.cleanup();
|
|
278
|
-
claim.reject(error instanceof Error ? error : new Error(String(error)));
|
|
110
|
+
releaseAgentClaim(record: MonitoredAgent, error: unknown) {
|
|
111
|
+
this.delivery.releaseAgentClaim(record, error);
|
|
279
112
|
}
|
|
280
|
-
|
|
281
113
|
async report(
|
|
282
114
|
lifecycle: SettlementLifecycle,
|
|
283
115
|
request: SettlementRequest,
|
|
@@ -306,7 +138,7 @@ export class SettlementReporter {
|
|
|
306
138
|
this.reporting.set(key, request.status);
|
|
307
139
|
try {
|
|
308
140
|
const replyingTo = retryState.attemptId
|
|
309
|
-
? this.
|
|
141
|
+
? this.delivery.replyingTo(retryState.attemptId)
|
|
310
142
|
: undefined;
|
|
311
143
|
const settlement = await this.collector.collect(
|
|
312
144
|
request.record,
|
|
@@ -322,7 +154,7 @@ export class SettlementReporter {
|
|
|
322
154
|
) {
|
|
323
155
|
if (request.status !== "blocked") return;
|
|
324
156
|
this.clearRetry(request.record.terminalId, retryState.attemptId);
|
|
325
|
-
this.deliverSettlement(
|
|
157
|
+
this.delivery.deliverSettlement(
|
|
326
158
|
lifecycle,
|
|
327
159
|
request.record,
|
|
328
160
|
settlement,
|
|
@@ -405,7 +237,7 @@ export class SettlementReporter {
|
|
|
405
237
|
) {
|
|
406
238
|
this.clearRetry(current.terminalId, retryState.attemptId);
|
|
407
239
|
this.persist();
|
|
408
|
-
this.deliverSettlement(
|
|
240
|
+
this.delivery.deliverSettlement(
|
|
409
241
|
lifecycle,
|
|
410
242
|
current,
|
|
411
243
|
settlement,
|
|
@@ -430,7 +262,7 @@ export class SettlementReporter {
|
|
|
430
262
|
) {
|
|
431
263
|
this.persist();
|
|
432
264
|
}
|
|
433
|
-
this.deliverSettlement(
|
|
265
|
+
this.delivery.deliverSettlement(
|
|
434
266
|
lifecycle,
|
|
435
267
|
currentOwnsAttempt ? current : request.record,
|
|
436
268
|
blockedSettlement(current),
|
|
@@ -473,48 +305,6 @@ export class SettlementReporter {
|
|
|
473
305
|
}
|
|
474
306
|
}
|
|
475
307
|
|
|
476
|
-
private deliverSettlement(
|
|
477
|
-
lifecycle: SettlementLifecycle,
|
|
478
|
-
record: MonitoredAgent,
|
|
479
|
-
settlement: CollectedSettlementWithSession,
|
|
480
|
-
attemptId: string | undefined,
|
|
481
|
-
blockedMessage: string | undefined,
|
|
482
|
-
reply: LatestAssistant | undefined,
|
|
483
|
-
): void {
|
|
484
|
-
const claim = attemptId ? this.claims.get(attemptId) : undefined;
|
|
485
|
-
if (claim && attemptId) {
|
|
486
|
-
this.claims.delete(attemptId);
|
|
487
|
-
claim.cleanup();
|
|
488
|
-
const {
|
|
489
|
-
reply: _latestReply,
|
|
490
|
-
session: _session,
|
|
491
|
-
...claimedSettlement
|
|
492
|
-
} = settlement;
|
|
493
|
-
claim.resolve({
|
|
494
|
-
...claimedSettlement,
|
|
495
|
-
record,
|
|
496
|
-
...(blockedMessage ? { blockedMessage } : {}),
|
|
497
|
-
...(reply ? { reply } : {}),
|
|
498
|
-
});
|
|
499
|
-
return;
|
|
500
|
-
}
|
|
501
|
-
injectAgentEvent(this.pi, lifecycle.context, {
|
|
502
|
-
agent: settlement.agent,
|
|
503
|
-
agentToolName: "agents",
|
|
504
|
-
...(settlement.extensionMessages
|
|
505
|
-
? { extensionMessages: settlement.extensionMessages }
|
|
506
|
-
: {}),
|
|
507
|
-
machine: this.origin.machine,
|
|
508
|
-
machineLabel: this.origin.label(),
|
|
509
|
-
operatorPrefix: this.origin.operatorPrefix,
|
|
510
|
-
...(blockedMessage ? { blockedMessage } : {}),
|
|
511
|
-
labels: settlement.labels,
|
|
512
|
-
record,
|
|
513
|
-
...(reply ? { reply } : {}),
|
|
514
|
-
status: settlement.status,
|
|
515
|
-
});
|
|
516
|
-
}
|
|
517
|
-
|
|
518
308
|
private clearRetry(terminalId: string, attemptId: string | undefined): void {
|
|
519
309
|
const key = attemptKey(terminalId, attemptId);
|
|
520
310
|
const retry = this.retries.get(key);
|
|
@@ -580,7 +370,7 @@ export class SettlementReporter {
|
|
|
580
370
|
" after " +
|
|
581
371
|
SETTLEMENT_RETRY_WINDOW_SECONDS +
|
|
582
372
|
" seconds";
|
|
583
|
-
if (attemptId) this.releaseAttempt(attemptId, new Error(message));
|
|
373
|
+
if (attemptId) this.delivery.releaseAttempt(attemptId, new Error(message));
|
|
584
374
|
lifecycle.context.ui.notify(message, "warning");
|
|
585
375
|
}
|
|
586
376
|
}
|