@narumitw/pi-herdr 0.2.0 → 0.3.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/README.md +8 -0
- package/package.json +51 -52
- package/skills/herdr/SKILL.md +12 -0
- package/src/herdr-agent-state.ts +404 -411
- package/src/herdr-client.ts +236 -238
- package/src/herdr-menu.ts +49 -50
- package/src/herdr-metadata.ts +46 -52
- package/src/herdr-observer.ts +334 -372
- package/src/herdr-protocol.ts +185 -187
- package/src/herdr-settings-controller.ts +118 -124
- package/src/herdr-widget.ts +204 -217
- package/src/settings.ts +131 -135
package/src/herdr-observer.ts
CHANGED
|
@@ -1,54 +1,40 @@
|
|
|
1
1
|
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type HerdrRequest, type HerdrSubscription, openHerdrSubscription, requestHerdr } from "./herdr-client.js";
|
|
2
3
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
herdrWidgetSubscriptions,
|
|
13
|
-
parseAgentListResult,
|
|
14
|
-
parseHerdrPaneEvent,
|
|
15
|
-
parsePaneCurrentResult,
|
|
16
|
-
parsePaneListResult,
|
|
17
|
-
parseWorkspaceGetResult,
|
|
4
|
+
type HerdrPane,
|
|
5
|
+
type HerdrPaneEvent,
|
|
6
|
+
type HerdrWorkspace,
|
|
7
|
+
herdrWidgetSubscriptions,
|
|
8
|
+
parseAgentListResult,
|
|
9
|
+
parseHerdrPaneEvent,
|
|
10
|
+
parsePaneCurrentResult,
|
|
11
|
+
parsePaneListResult,
|
|
12
|
+
parseWorkspaceGetResult,
|
|
18
13
|
} from "./herdr-protocol.js";
|
|
19
|
-
import {
|
|
20
|
-
createHerdrWidget,
|
|
21
|
-
HERDR_WIDGET_KEY,
|
|
22
|
-
HerdrWidgetModel,
|
|
23
|
-
herdrWidgetFingerprint,
|
|
24
|
-
} from "./herdr-widget.js";
|
|
14
|
+
import { createHerdrWidget, HERDR_WIDGET_KEY, HerdrWidgetModel, herdrWidgetFingerprint } from "./herdr-widget.js";
|
|
25
15
|
|
|
26
16
|
export interface HerdrObserverEnvironment {
|
|
27
|
-
|
|
28
|
-
|
|
17
|
+
paneId: string;
|
|
18
|
+
socketEndpoint: string;
|
|
29
19
|
}
|
|
30
20
|
|
|
31
21
|
export interface HerdrObserverOptions {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
signal: AbortSignal,
|
|
44
|
-
onEvent: (frame: unknown) => void,
|
|
45
|
-
) => Promise<HerdrSubscription>;
|
|
46
|
-
retryDelayMs?: number;
|
|
22
|
+
environment: HerdrObserverEnvironment;
|
|
23
|
+
now?: () => number;
|
|
24
|
+
random?: () => number;
|
|
25
|
+
request?: (socketEndpoint: string, request: HerdrRequest, signal: AbortSignal) => Promise<unknown>;
|
|
26
|
+
subscribe?: (
|
|
27
|
+
socketEndpoint: string,
|
|
28
|
+
request: HerdrRequest,
|
|
29
|
+
signal: AbortSignal,
|
|
30
|
+
onEvent: (frame: unknown) => void,
|
|
31
|
+
) => Promise<HerdrSubscription>;
|
|
32
|
+
retryDelayMs?: number;
|
|
47
33
|
}
|
|
48
34
|
|
|
49
35
|
export interface HerdrWidgetObserver {
|
|
50
|
-
|
|
51
|
-
|
|
36
|
+
start(ctx: ExtensionContext): void;
|
|
37
|
+
shutdown(ctx: ExtensionContext): Promise<void>;
|
|
52
38
|
}
|
|
53
39
|
|
|
54
40
|
const MAX_PENDING_EVENTS = 1024;
|
|
@@ -56,360 +42,336 @@ const MAX_PENDING_EVENTS = 1024;
|
|
|
56
42
|
class HerdrTopologyChangedError extends Error {}
|
|
57
43
|
|
|
58
44
|
function requiresResubscribe(event: HerdrPaneEvent): boolean {
|
|
59
|
-
|
|
45
|
+
return event.type === "created" || event.type === "moved" || event.type === "agent_detected";
|
|
60
46
|
}
|
|
61
47
|
|
|
62
48
|
function isRelevantEvent(model: HerdrWidgetModel, event: HerdrPaneEvent): boolean {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
49
|
+
if (event.type === "created") return model.isCurrentWorkspace(event.pane.workspaceId);
|
|
50
|
+
if (event.type === "moved") {
|
|
51
|
+
return (
|
|
52
|
+
model.isCurrentPane(event.previousPaneId) ||
|
|
53
|
+
model.isCurrentWorkspace(event.previousWorkspaceId) ||
|
|
54
|
+
model.isCurrentWorkspace(event.pane.workspaceId)
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return model.isCurrentWorkspace(event.workspaceId);
|
|
72
58
|
}
|
|
73
59
|
|
|
74
60
|
function subscriptionFingerprint(currentPane: HerdrPane, panes: readonly HerdrPane[]): string {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
61
|
+
const agentPaneIds = panes
|
|
62
|
+
.filter((pane) => pane.agent !== undefined)
|
|
63
|
+
.map((pane) => pane.paneId)
|
|
64
|
+
.sort();
|
|
65
|
+
return JSON.stringify([currentPane.paneId, currentPane.workspaceId, agentPaneIds]);
|
|
80
66
|
}
|
|
81
67
|
|
|
82
68
|
function waitForRetry(delayMs: number, signal: AbortSignal): Promise<void> {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
69
|
+
if (signal.aborted) return Promise.reject(signal.reason);
|
|
70
|
+
if (delayMs <= 0) return Promise.resolve();
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
const timer = setTimeout(() => {
|
|
73
|
+
signal.removeEventListener("abort", abort);
|
|
74
|
+
resolve();
|
|
75
|
+
}, delayMs);
|
|
76
|
+
timer.unref?.();
|
|
77
|
+
const abort = () => {
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
reject(signal.reason);
|
|
80
|
+
};
|
|
81
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
82
|
+
});
|
|
97
83
|
}
|
|
98
84
|
|
|
99
85
|
export function createHerdrWidgetObserver(options: HerdrObserverOptions): HerdrWidgetObserver {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
86
|
+
const now = options.now ?? Date.now;
|
|
87
|
+
const random = options.random ?? Math.random;
|
|
88
|
+
const request = options.request ?? requestHerdr;
|
|
89
|
+
const subscribe = options.subscribe ?? openHerdrSubscription;
|
|
90
|
+
const retryDelayMs = options.retryDelayMs ?? 200;
|
|
91
|
+
let generation = 0;
|
|
92
|
+
let activeContext: ExtensionContext | undefined;
|
|
93
|
+
let activeSession: ExtensionContext["sessionManager"] | undefined;
|
|
94
|
+
let controller: AbortController | undefined;
|
|
95
|
+
const ownedTasks = new Set<Promise<void>>();
|
|
96
|
+
let publishedFingerprint: string | undefined;
|
|
97
|
+
let hasPublished = false;
|
|
112
98
|
|
|
113
|
-
|
|
114
|
-
|
|
99
|
+
const owns = (ctx: ExtensionContext, expectedGeneration = generation) =>
|
|
100
|
+
ctx.sessionManager === activeSession && expectedGeneration === generation;
|
|
115
101
|
|
|
116
|
-
|
|
117
|
-
`herdr:pi:widget:${kind}:${now()}:${random().toString(36).slice(2)}`;
|
|
102
|
+
const requestId = (kind: string) => `herdr:pi:widget:${kind}:${now()}:${random().toString(36).slice(2)}`;
|
|
118
103
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
104
|
+
const clearWidget = (ctx: ExtensionContext) => {
|
|
105
|
+
if (ctx.mode === "tui") ctx.ui.setWidget(HERDR_WIDGET_KEY, undefined);
|
|
106
|
+
publishedFingerprint = undefined;
|
|
107
|
+
hasPublished = false;
|
|
108
|
+
};
|
|
124
109
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
hasPublished = true;
|
|
141
|
-
};
|
|
110
|
+
const publish = (ctx: ExtensionContext, model: HerdrWidgetModel, expectedGeneration: number) => {
|
|
111
|
+
if (!owns(ctx, expectedGeneration) || ctx.mode !== "tui") return;
|
|
112
|
+
const snapshot = model.snapshot();
|
|
113
|
+
const fingerprint = herdrWidgetFingerprint(snapshot);
|
|
114
|
+
if (hasPublished && fingerprint === publishedFingerprint) return;
|
|
115
|
+
if (!snapshot) {
|
|
116
|
+
ctx.ui.setWidget(HERDR_WIDGET_KEY, undefined);
|
|
117
|
+
} else {
|
|
118
|
+
ctx.ui.setWidget(HERDR_WIDGET_KEY, (_tui: unknown, theme: Theme) => createHerdrWidget(snapshot, theme), {
|
|
119
|
+
placement: "aboveEditor",
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
publishedFingerprint = fingerprint;
|
|
123
|
+
hasPublished = true;
|
|
124
|
+
};
|
|
142
125
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
panes: parsePaneListResult(listResult).map(withAgentName),
|
|
201
|
-
workspace,
|
|
202
|
-
};
|
|
203
|
-
};
|
|
126
|
+
const loadPaneSet = async (
|
|
127
|
+
ctx: ExtensionContext,
|
|
128
|
+
expectedGeneration: number,
|
|
129
|
+
signal: AbortSignal,
|
|
130
|
+
): Promise<{ currentPane: HerdrPane; panes: HerdrPane[]; workspace: HerdrWorkspace } | undefined> => {
|
|
131
|
+
const currentResult = await request(
|
|
132
|
+
options.environment.socketEndpoint,
|
|
133
|
+
{
|
|
134
|
+
id: requestId("current"),
|
|
135
|
+
method: "pane.current",
|
|
136
|
+
params: { caller_pane_id: options.environment.paneId },
|
|
137
|
+
},
|
|
138
|
+
signal,
|
|
139
|
+
);
|
|
140
|
+
if (!owns(ctx, expectedGeneration) || signal.aborted) return undefined;
|
|
141
|
+
const currentPane = parsePaneCurrentResult(currentResult);
|
|
142
|
+
const [listResult, agentResult, workspaceResult] = await Promise.all([
|
|
143
|
+
request(
|
|
144
|
+
options.environment.socketEndpoint,
|
|
145
|
+
{
|
|
146
|
+
id: requestId("list"),
|
|
147
|
+
method: "pane.list",
|
|
148
|
+
params: { workspace_id: currentPane.workspaceId },
|
|
149
|
+
},
|
|
150
|
+
signal,
|
|
151
|
+
),
|
|
152
|
+
request(
|
|
153
|
+
options.environment.socketEndpoint,
|
|
154
|
+
{ id: requestId("agents"), method: "agent.list", params: {} },
|
|
155
|
+
signal,
|
|
156
|
+
),
|
|
157
|
+
request(
|
|
158
|
+
options.environment.socketEndpoint,
|
|
159
|
+
{
|
|
160
|
+
id: requestId("workspace"),
|
|
161
|
+
method: "workspace.get",
|
|
162
|
+
params: { workspace_id: currentPane.workspaceId },
|
|
163
|
+
},
|
|
164
|
+
signal,
|
|
165
|
+
),
|
|
166
|
+
]);
|
|
167
|
+
if (!owns(ctx, expectedGeneration) || signal.aborted) return undefined;
|
|
168
|
+
const namesByPane = new Map(parseAgentListResult(agentResult).map((pane) => [pane.paneId, pane.agentName]));
|
|
169
|
+
const withAgentName = (pane: HerdrPane): HerdrPane => ({
|
|
170
|
+
...pane,
|
|
171
|
+
agentName: namesByPane.get(pane.paneId) ?? pane.agentName,
|
|
172
|
+
});
|
|
173
|
+
const workspace = parseWorkspaceGetResult(workspaceResult);
|
|
174
|
+
if (workspace.workspaceId !== currentPane.workspaceId) {
|
|
175
|
+
throw new Error("Herdr returned a mismatched workspace");
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
currentPane: withAgentName(currentPane),
|
|
179
|
+
panes: parsePaneListResult(listResult).map(withAgentName),
|
|
180
|
+
workspace,
|
|
181
|
+
};
|
|
182
|
+
};
|
|
204
183
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
})
|
|
274
|
-
.finally(() => {
|
|
275
|
-
refreshingTopology = false;
|
|
276
|
-
refreshTask = undefined;
|
|
277
|
-
});
|
|
278
|
-
};
|
|
184
|
+
const runAttempt = async (
|
|
185
|
+
ctx: ExtensionContext,
|
|
186
|
+
expectedGeneration: number,
|
|
187
|
+
sessionSignal: AbortSignal,
|
|
188
|
+
): Promise<void> => {
|
|
189
|
+
const attemptController = new AbortController();
|
|
190
|
+
const abortAttempt = () => attemptController.abort(sessionSignal.reason);
|
|
191
|
+
if (sessionSignal.aborted) abortAttempt();
|
|
192
|
+
else sessionSignal.addEventListener("abort", abortAttempt, { once: true });
|
|
193
|
+
const signal = attemptController.signal;
|
|
194
|
+
let attemptActive = true;
|
|
195
|
+
let subscription: HerdrSubscription | undefined;
|
|
196
|
+
let refreshTask: Promise<void> | undefined;
|
|
197
|
+
try {
|
|
198
|
+
const discovered = await loadPaneSet(ctx, expectedGeneration, signal);
|
|
199
|
+
if (!discovered) return;
|
|
200
|
+
const subscribedFingerprint = subscriptionFingerprint(discovered.currentPane, discovered.panes);
|
|
201
|
+
const model = new HerdrWidgetModel();
|
|
202
|
+
model.reset(discovered.currentPane, discovered.panes, discovered.workspace);
|
|
203
|
+
const pendingEvents: HerdrPaneEvent[] = [];
|
|
204
|
+
const pendingStatusEvents: HerdrPaneEvent[] = [];
|
|
205
|
+
let initialized = false;
|
|
206
|
+
let topologyPending = false;
|
|
207
|
+
let refreshingTopology = false;
|
|
208
|
+
let refreshAgain = false;
|
|
209
|
+
let rejectReconfigure!: (error: Error) => void;
|
|
210
|
+
let reconfigureSettled = false;
|
|
211
|
+
const reconfigure = new Promise<never>((_resolve, reject) => {
|
|
212
|
+
rejectReconfigure = reject;
|
|
213
|
+
});
|
|
214
|
+
void reconfigure.catch(() => undefined);
|
|
215
|
+
const failAttempt = (error: Error) => {
|
|
216
|
+
if (reconfigureSettled) return;
|
|
217
|
+
reconfigureSettled = true;
|
|
218
|
+
rejectReconfigure(error);
|
|
219
|
+
};
|
|
220
|
+
const scheduleTopologyRefresh = () => {
|
|
221
|
+
if (!initialized || !attemptActive || signal.aborted) return;
|
|
222
|
+
if (refreshTask) {
|
|
223
|
+
refreshAgain = true;
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
refreshTask = (async () => {
|
|
227
|
+
do {
|
|
228
|
+
refreshAgain = false;
|
|
229
|
+
refreshingTopology = true;
|
|
230
|
+
const refreshed = await loadPaneSet(ctx, expectedGeneration, signal);
|
|
231
|
+
if (!refreshed || !attemptActive || signal.aborted) return;
|
|
232
|
+
model.reset(refreshed.currentPane, refreshed.panes, refreshed.workspace);
|
|
233
|
+
for (const event of pendingStatusEvents.splice(0)) {
|
|
234
|
+
if (isRelevantEvent(model, event)) model.apply(event);
|
|
235
|
+
}
|
|
236
|
+
refreshingTopology = false;
|
|
237
|
+
publish(ctx, model, expectedGeneration);
|
|
238
|
+
if (subscriptionFingerprint(refreshed.currentPane, refreshed.panes) !== subscribedFingerprint) {
|
|
239
|
+
failAttempt(new HerdrTopologyChangedError());
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
} while (refreshAgain && attemptActive && !signal.aborted);
|
|
243
|
+
})()
|
|
244
|
+
.catch((error) => {
|
|
245
|
+
failAttempt(error instanceof Error ? error : new Error(String(error)));
|
|
246
|
+
})
|
|
247
|
+
.finally(() => {
|
|
248
|
+
refreshingTopology = false;
|
|
249
|
+
refreshTask = undefined;
|
|
250
|
+
});
|
|
251
|
+
};
|
|
279
252
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
253
|
+
subscription = await subscribe(
|
|
254
|
+
options.environment.socketEndpoint,
|
|
255
|
+
{
|
|
256
|
+
id: requestId("subscribe"),
|
|
257
|
+
method: "events.subscribe",
|
|
258
|
+
params: { subscriptions: herdrWidgetSubscriptions(discovered.panes) },
|
|
259
|
+
},
|
|
260
|
+
signal,
|
|
261
|
+
(frame) => {
|
|
262
|
+
const event = parseHerdrPaneEvent(frame);
|
|
263
|
+
if (!initialized) {
|
|
264
|
+
if (!event || !isRelevantEvent(model, event)) return;
|
|
265
|
+
if (requiresResubscribe(event)) {
|
|
266
|
+
topologyPending = true;
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (pendingEvents.length >= MAX_PENDING_EVENTS) {
|
|
270
|
+
throw new Error("Too many Herdr events arrived during initialization");
|
|
271
|
+
}
|
|
272
|
+
pendingEvents.push(event);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
302
275
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
model.apply(event);
|
|
323
|
-
publish(ctx, model, expectedGeneration);
|
|
324
|
-
},
|
|
325
|
-
);
|
|
326
|
-
if (!owns(ctx, expectedGeneration) || signal.aborted) return;
|
|
276
|
+
if (!event || !owns(ctx, expectedGeneration) || signal.aborted || !isRelevantEvent(model, event)) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (requiresResubscribe(event)) {
|
|
280
|
+
scheduleTopologyRefresh();
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (refreshingTopology) {
|
|
284
|
+
if (pendingStatusEvents.length >= MAX_PENDING_EVENTS) {
|
|
285
|
+
throw new Error("Too many Herdr status events arrived during refresh");
|
|
286
|
+
}
|
|
287
|
+
pendingStatusEvents.push(event);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
model.apply(event);
|
|
291
|
+
publish(ctx, model, expectedGeneration);
|
|
292
|
+
},
|
|
293
|
+
);
|
|
294
|
+
if (!owns(ctx, expectedGeneration) || signal.aborted) return;
|
|
327
295
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
}
|
|
355
|
-
};
|
|
296
|
+
const reconciled = await loadPaneSet(ctx, expectedGeneration, signal);
|
|
297
|
+
if (!reconciled) return;
|
|
298
|
+
model.reset(reconciled.currentPane, reconciled.panes, reconciled.workspace);
|
|
299
|
+
for (const event of pendingEvents.splice(0)) {
|
|
300
|
+
if (isRelevantEvent(model, event)) model.apply(event);
|
|
301
|
+
}
|
|
302
|
+
if (subscriptionFingerprint(reconciled.currentPane, reconciled.panes) !== subscribedFingerprint) {
|
|
303
|
+
throw new HerdrTopologyChangedError();
|
|
304
|
+
}
|
|
305
|
+
initialized = true;
|
|
306
|
+
publish(ctx, model, expectedGeneration);
|
|
307
|
+
if (topologyPending) scheduleTopologyRefresh();
|
|
308
|
+
await Promise.race([
|
|
309
|
+
subscription.closed.then(() => {
|
|
310
|
+
if (!signal.aborted) throw new Error("Herdr subscription ended");
|
|
311
|
+
}),
|
|
312
|
+
reconfigure,
|
|
313
|
+
]);
|
|
314
|
+
} finally {
|
|
315
|
+
attemptActive = false;
|
|
316
|
+
attemptController.abort(new DOMException("Herdr widget attempt ended", "AbortError"));
|
|
317
|
+
sessionSignal.removeEventListener("abort", abortAttempt);
|
|
318
|
+
subscription?.close();
|
|
319
|
+
await Promise.allSettled(refreshTask ? [refreshTask] : []);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
356
322
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
if (!owns(ctx, expectedGeneration) || signal.aborted) return;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
};
|
|
323
|
+
const run = async (ctx: ExtensionContext, expectedGeneration: number, signal: AbortSignal): Promise<void> => {
|
|
324
|
+
let failures = 0;
|
|
325
|
+
while (owns(ctx, expectedGeneration) && !signal.aborted) {
|
|
326
|
+
try {
|
|
327
|
+
await runAttempt(ctx, expectedGeneration, signal);
|
|
328
|
+
return;
|
|
329
|
+
} catch (error) {
|
|
330
|
+
if (!owns(ctx, expectedGeneration) || signal.aborted) return;
|
|
331
|
+
clearWidget(ctx);
|
|
332
|
+
if (error instanceof HerdrTopologyChangedError) continue;
|
|
333
|
+
failures += 1;
|
|
334
|
+
if (failures >= 2) return;
|
|
335
|
+
try {
|
|
336
|
+
await waitForRetry(retryDelayMs, signal);
|
|
337
|
+
} catch {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (!owns(ctx, expectedGeneration) || signal.aborted) return;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
};
|
|
382
344
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
345
|
+
return {
|
|
346
|
+
start(ctx) {
|
|
347
|
+
generation += 1;
|
|
348
|
+
controller?.abort(new DOMException("Herdr widget session replaced", "AbortError"));
|
|
349
|
+
if (activeContext) clearWidget(activeContext);
|
|
350
|
+
activeContext = undefined;
|
|
351
|
+
activeSession = undefined;
|
|
352
|
+
controller = undefined;
|
|
353
|
+
if (ctx.mode !== "tui") return;
|
|
392
354
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
355
|
+
activeContext = ctx;
|
|
356
|
+
activeSession = ctx.sessionManager;
|
|
357
|
+
controller = new AbortController();
|
|
358
|
+
const expectedGeneration = generation;
|
|
359
|
+
clearWidget(ctx);
|
|
360
|
+
const task = run(ctx, expectedGeneration, controller.signal).finally(() => {
|
|
361
|
+
ownedTasks.delete(task);
|
|
362
|
+
});
|
|
363
|
+
ownedTasks.add(task);
|
|
364
|
+
},
|
|
365
|
+
async shutdown(ctx) {
|
|
366
|
+
if (!owns(ctx)) return;
|
|
367
|
+
const shutdownGeneration = ++generation;
|
|
368
|
+
controller?.abort(new DOMException("Herdr widget session shut down", "AbortError"));
|
|
369
|
+
clearWidget(ctx);
|
|
370
|
+
await Promise.allSettled([...ownedTasks]);
|
|
371
|
+
if (generation !== shutdownGeneration) return;
|
|
372
|
+
activeContext = undefined;
|
|
373
|
+
activeSession = undefined;
|
|
374
|
+
controller = undefined;
|
|
375
|
+
},
|
|
376
|
+
};
|
|
415
377
|
}
|