@leitwerk-dev/watcher-utils 0.2.0 → 0.2.1
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/dist/{chunk-HG5TEBHZ.js → chunk-BO52GPAV.js} +5 -7
- package/dist/chunk-BO52GPAV.js.map +1 -0
- package/dist/{chunk-IU3L35PC.js → chunk-D57H46H7.js} +1 -1
- package/dist/chunk-D57H46H7.js.map +1 -0
- package/dist/{chunk-3KGAUQIG.js → chunk-EWA6E2ZM.js} +1 -1
- package/dist/chunk-EWA6E2ZM.js.map +1 -0
- package/dist/{chunk-USTHXEZJ.js → chunk-ZW3LXDBX.js} +6 -4
- package/dist/chunk-ZW3LXDBX.js.map +1 -0
- package/dist/duration-parse.d.ts +2 -0
- package/dist/duration-parse.d.ts.map +1 -1
- package/dist/duration-parse.js +1 -1
- package/dist/index.d.ts +3 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -33
- package/dist/index.js.map +1 -1
- package/dist/poll-loop.d.ts +22 -2
- package/dist/poll-loop.d.ts.map +1 -1
- package/dist/poll-loop.js +2 -2
- package/dist/polling-coordinator.d.ts +17 -1
- package/dist/polling-coordinator.d.ts.map +1 -1
- package/dist/polling-coordinator.js +3 -3
- package/dist/process-helpers.d.ts +15 -0
- package/dist/process-helpers.d.ts.map +1 -1
- package/dist/process-helpers.js +1 -1
- package/dist/trigger-files.d.ts +7 -0
- package/dist/trigger-files.d.ts.map +1 -1
- package/dist/watcher-coordinator.d.ts +77 -2
- package/dist/watcher-coordinator.d.ts.map +1 -1
- package/dist/watcher-coordinator.js +147 -10
- package/dist/watcher-coordinator.js.map +1 -1
- package/package.json +4 -4
- package/src/duration-parse.ts +5 -1
- package/src/index.ts +1 -38
- package/src/poll-loop.ts +25 -7
- package/src/polling-coordinator.ts +23 -3
- package/src/process-helpers.ts +30 -3
- package/src/trigger-files.ts +15 -1
- package/src/watcher-coordinator.ts +99 -11
- package/dist/chunk-3KGAUQIG.js.map +0 -1
- package/dist/chunk-HG5TEBHZ.js.map +0 -1
- package/dist/chunk-IU3L35PC.js.map +0 -1
- package/dist/chunk-USTHXEZJ.js.map +0 -1
- package/dist/chunk-XLVMFO6W.js +0 -160
- package/dist/chunk-XLVMFO6W.js.map +0 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { ProcessInstance, TurnId } from "@leitwerk-dev/domain";
|
|
2
|
+
import type { WriteIdentity } from "@leitwerk-dev/external-writes";
|
|
2
3
|
import {
|
|
3
4
|
type ExternalWriteLogRepoLike,
|
|
4
5
|
recordWriteIfMissing,
|
|
5
|
-
|
|
6
|
-
} from "@leitwerk-dev/external-writes";
|
|
6
|
+
} from "@leitwerk-dev/external-writes/internal";
|
|
7
7
|
import type { KnownDurableWsFrameType, WsPayloadByType } from "@leitwerk-dev/protocol";
|
|
8
8
|
import type { PollResult } from "./poll-loop.js";
|
|
9
9
|
import {
|
|
@@ -14,11 +14,15 @@ import {
|
|
|
14
14
|
type WorkerSupervisorLike,
|
|
15
15
|
} from "./process-helpers.js";
|
|
16
16
|
|
|
17
|
+
/** @internal */
|
|
17
18
|
export interface AgentRepoAccessorLike {
|
|
19
|
+
/** @internal */
|
|
18
20
|
getById(id: string): ProcessInstance | null;
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
/** @internal */
|
|
21
24
|
export interface BroadcasterLike {
|
|
25
|
+
/** @internal */
|
|
22
26
|
sendDurable<T extends KnownDurableWsFrameType>(
|
|
23
27
|
type: T,
|
|
24
28
|
payload: WsPayloadByType[T],
|
|
@@ -26,39 +30,66 @@ export interface BroadcasterLike {
|
|
|
26
30
|
): void;
|
|
27
31
|
}
|
|
28
32
|
|
|
33
|
+
/** @internal */
|
|
29
34
|
export interface WatcherCommandResult {
|
|
35
|
+
/** @internal */
|
|
30
36
|
ok: boolean;
|
|
37
|
+
/** @internal */
|
|
31
38
|
code?: string;
|
|
39
|
+
/** @internal */
|
|
32
40
|
message?: string;
|
|
41
|
+
/** @internal */
|
|
33
42
|
process?: ProcessInstance | null;
|
|
34
43
|
}
|
|
35
44
|
|
|
45
|
+
/** @internal */
|
|
36
46
|
export interface WatcherCommandServiceLike {
|
|
47
|
+
/** @internal */
|
|
37
48
|
startProcess(instanceId: string, startTurnId: TurnId): Promise<WatcherCommandResult>;
|
|
49
|
+
/** @internal */
|
|
38
50
|
abortProcess(instanceId: string): Promise<WatcherCommandResult>;
|
|
39
51
|
}
|
|
40
52
|
|
|
53
|
+
/** @internal */
|
|
41
54
|
export interface WatcherStartupDeps {
|
|
55
|
+
/** @internal */
|
|
42
56
|
processes: AgentRepoAccessorLike;
|
|
57
|
+
/** @internal */
|
|
43
58
|
events: ProcessEventRepoLike;
|
|
59
|
+
/** @internal */
|
|
44
60
|
broadcaster: BroadcasterLike;
|
|
61
|
+
/** @internal */
|
|
45
62
|
commands: WatcherCommandServiceLike;
|
|
63
|
+
/** @internal */
|
|
46
64
|
supervisor: WorkerSupervisorLike;
|
|
47
65
|
}
|
|
48
66
|
|
|
67
|
+
/** @internal */
|
|
49
68
|
export interface ContinueWatcherAgentStartupOptions {
|
|
69
|
+
/** @internal */
|
|
50
70
|
process: ProcessInstance;
|
|
71
|
+
/** @internal */
|
|
51
72
|
startTurnId: TurnId;
|
|
73
|
+
/** @internal */
|
|
52
74
|
createdEventData: Record<string, unknown>;
|
|
75
|
+
/** @internal */
|
|
53
76
|
createdBroadcastData: Record<string, unknown>;
|
|
54
|
-
/** Defaults to true. Set false when the shared launch executor already emitted process.created. */
|
|
77
|
+
/** Defaults to true. Set false when the shared launch executor already emitted process.created. @internal */
|
|
55
78
|
broadcastCreated?: boolean;
|
|
79
|
+
/** @internal */
|
|
56
80
|
afterCreatedEvent?: (process: ProcessInstance) => Promise<void> | void;
|
|
81
|
+
/** @internal */
|
|
57
82
|
beforeStart?: (process: ProcessInstance) => Promise<void>;
|
|
58
83
|
}
|
|
59
84
|
|
|
85
|
+
/** @internal */
|
|
60
86
|
export function requireWatcherStartTurnId(
|
|
61
|
-
launchPlan: {
|
|
87
|
+
launchPlan: {
|
|
88
|
+
/** @internal */
|
|
89
|
+
launcherId: string;
|
|
90
|
+
/** @internal */
|
|
91
|
+
startTurnId: TurnId | null;
|
|
92
|
+
},
|
|
62
93
|
targetLabel: string,
|
|
63
94
|
): TurnId {
|
|
64
95
|
if (launchPlan.startTurnId) {
|
|
@@ -69,6 +100,7 @@ export function requireWatcherStartTurnId(
|
|
|
69
100
|
);
|
|
70
101
|
}
|
|
71
102
|
|
|
103
|
+
/** @internal */
|
|
72
104
|
export async function continueWatcherAgentStartup(
|
|
73
105
|
deps: WatcherStartupDeps,
|
|
74
106
|
opts: ContinueWatcherAgentStartupOptions,
|
|
@@ -111,26 +143,49 @@ export async function continueWatcherAgentStartup(
|
|
|
111
143
|
return currentProcess.id;
|
|
112
144
|
}
|
|
113
145
|
|
|
146
|
+
/** @internal */
|
|
114
147
|
export type DiscoveryResumeResult = "handled" | "skipped" | undefined;
|
|
115
148
|
|
|
149
|
+
/** @internal */
|
|
116
150
|
export type DiscoveryCreateResult =
|
|
117
|
-
| {
|
|
118
|
-
|
|
119
|
-
|
|
151
|
+
| {
|
|
152
|
+
/** @internal */
|
|
153
|
+
outcome: "created";
|
|
154
|
+
/** @internal */
|
|
155
|
+
instanceId: string;
|
|
156
|
+
}
|
|
157
|
+
| {
|
|
158
|
+
/** @internal */
|
|
159
|
+
outcome: "skipped";
|
|
160
|
+
}
|
|
161
|
+
| {
|
|
162
|
+
/** @internal */
|
|
163
|
+
outcome: "handled";
|
|
164
|
+
}
|
|
120
165
|
| null
|
|
121
166
|
| undefined;
|
|
122
167
|
|
|
168
|
+
/** @internal */
|
|
123
169
|
export interface DiscoveryPassOptions<TItem> {
|
|
170
|
+
/** @internal */
|
|
124
171
|
items: readonly TItem[];
|
|
172
|
+
/** @internal */
|
|
125
173
|
result: PollResult;
|
|
174
|
+
/** @internal */
|
|
126
175
|
findExistingAgent(item: TItem): ProcessInstance | null;
|
|
176
|
+
/** @internal */
|
|
127
177
|
getResultLabel(item: TItem): string;
|
|
178
|
+
/** @internal */
|
|
128
179
|
resumeDiscoveredAgent(process: ProcessInstance, item: TItem): Promise<DiscoveryResumeResult>;
|
|
180
|
+
/** @internal */
|
|
129
181
|
createAgent(item: TItem): Promise<DiscoveryCreateResult>;
|
|
182
|
+
/** @internal */
|
|
130
183
|
ensureWorkerForExistingAgent?(process: ProcessInstance, item: TItem): Promise<boolean>;
|
|
184
|
+
/** @internal */
|
|
131
185
|
formatError(item: TItem, error: unknown): string;
|
|
132
186
|
}
|
|
133
187
|
|
|
188
|
+
/** @internal */
|
|
134
189
|
export async function runWatcherDiscoveryPass<TItem>(
|
|
135
190
|
opts: DiscoveryPassOptions<TItem>,
|
|
136
191
|
): Promise<void> {
|
|
@@ -169,17 +224,27 @@ export async function runWatcherDiscoveryPass<TItem>(
|
|
|
169
224
|
}
|
|
170
225
|
}
|
|
171
226
|
|
|
227
|
+
/** @internal */
|
|
172
228
|
export interface AbortReconciliationOptions {
|
|
229
|
+
/** @internal */
|
|
173
230
|
candidates: readonly ProcessInstance[];
|
|
231
|
+
/** @internal */
|
|
174
232
|
result: PollResult;
|
|
233
|
+
/** @internal */
|
|
175
234
|
events: ProcessEventRepoLike;
|
|
235
|
+
/** @internal */
|
|
176
236
|
commands: WatcherCommandServiceLike;
|
|
237
|
+
/** @internal */
|
|
177
238
|
shouldAbort(process: ProcessInstance): Promise<boolean>;
|
|
239
|
+
/** @internal */
|
|
178
240
|
getResultLabel(process: ProcessInstance): string;
|
|
241
|
+
/** @internal */
|
|
179
242
|
getErrorLabel(process: ProcessInstance): string;
|
|
243
|
+
/** @internal */
|
|
180
244
|
getAbortedEventData(process: ProcessInstance): Record<string, unknown>;
|
|
181
245
|
}
|
|
182
246
|
|
|
247
|
+
/** @internal */
|
|
183
248
|
export async function runWatcherAbortReconciliation(
|
|
184
249
|
opts: AbortReconciliationOptions,
|
|
185
250
|
): Promise<void> {
|
|
@@ -215,21 +280,33 @@ export async function runWatcherAbortReconciliation(
|
|
|
215
280
|
}
|
|
216
281
|
}
|
|
217
282
|
|
|
283
|
+
/** @internal */
|
|
218
284
|
export interface CompletionReconciliationResult {
|
|
285
|
+
/** @internal */
|
|
219
286
|
changed: boolean;
|
|
287
|
+
/** @internal */
|
|
220
288
|
writeIdentity?: WriteIdentity;
|
|
289
|
+
/** @internal */
|
|
221
290
|
metadata?: Record<string, unknown>;
|
|
222
291
|
}
|
|
223
292
|
|
|
293
|
+
/** @internal */
|
|
224
294
|
export interface CompletionReconciliationOptions {
|
|
295
|
+
/** @internal */
|
|
225
296
|
candidates: readonly ProcessInstance[];
|
|
297
|
+
/** @internal */
|
|
226
298
|
result: PollResult;
|
|
299
|
+
/** @internal */
|
|
227
300
|
externalWrites: ExternalWriteLogRepoLike;
|
|
301
|
+
/** @internal */
|
|
228
302
|
reconcile(process: ProcessInstance): Promise<CompletionReconciliationResult>;
|
|
303
|
+
/** @internal */
|
|
229
304
|
getResultLabel(process: ProcessInstance): string;
|
|
305
|
+
/** @internal */
|
|
230
306
|
getErrorLabel(process: ProcessInstance): string;
|
|
231
307
|
}
|
|
232
308
|
|
|
309
|
+
/** @internal */
|
|
233
310
|
export async function runWatcherCompletionReconciliation(
|
|
234
311
|
opts: CompletionReconciliationOptions,
|
|
235
312
|
): Promise<void> {
|
|
@@ -257,21 +334,32 @@ export async function runWatcherCompletionReconciliation(
|
|
|
257
334
|
}
|
|
258
335
|
}
|
|
259
336
|
|
|
337
|
+
/** @internal */
|
|
260
338
|
export function shouldEnsureWorkerForExistingActiveAgent(
|
|
261
|
-
deps: {
|
|
339
|
+
deps: {
|
|
340
|
+
/** @internal */
|
|
341
|
+
supervisor: WorkerSupervisorLike;
|
|
342
|
+
},
|
|
262
343
|
process: ProcessInstance,
|
|
263
344
|
): boolean {
|
|
264
345
|
return processNeedsWorker(process) && !deps.supervisor.getWorker(process.id);
|
|
265
346
|
}
|
|
266
347
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
348
|
+
/** @internal */
|
|
349
|
+
export function deferWatcherLaunchStart<
|
|
350
|
+
TLaunchPlan extends {
|
|
351
|
+
/** @internal */
|
|
352
|
+
startTurnId: unknown;
|
|
353
|
+
},
|
|
354
|
+
>(launchPlan: TLaunchPlan): TLaunchPlan {
|
|
270
355
|
return launchPlan.startTurnId === null ? launchPlan : { ...launchPlan, startTurnId: null };
|
|
271
356
|
}
|
|
272
357
|
|
|
358
|
+
/** @internal */
|
|
273
359
|
export function launchFailureMessage(result: {
|
|
360
|
+
/** @internal */
|
|
274
361
|
status: number;
|
|
362
|
+
/** @internal */
|
|
275
363
|
body: Record<string, unknown>;
|
|
276
364
|
}): string {
|
|
277
365
|
return typeof result.body.error === "string" && result.body.error.trim().length > 0
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/process-helpers.ts"],"sourcesContent":["import type { ProcessInstance } from \"@leitwerk-dev/domain\";\n\nexport interface ProcessEventRepoLike {\n\tcreate(input: { instanceId: string; eventType: string; data?: Record<string, unknown> }): unknown;\n\tlistByInstance(instanceId: string, limit?: number): Array<{ eventType: string }>;\n}\n\nexport interface WorkerSupervisorLike {\n\tspawnWorker(instanceId: string): Promise<unknown>;\n\tgetWorker(instanceId: string): unknown | undefined;\n}\n\n/**\n * Check whether a process already has an event of the given type.\n */\nexport function hasProcessEvent(\n\tevents: ProcessEventRepoLike,\n\tinstanceId: string,\n\teventType: string,\n): boolean {\n\treturn events.listByInstance(instanceId, 200).some((event) => event.eventType === eventType);\n}\n\nexport function processNeedsWorker(\n\tprocess: Pick<ProcessInstance, \"selectedTurnId\" | \"lifecycleStatus\">,\n): boolean {\n\treturn process.selectedTurnId !== null && process.lifecycleStatus === \"active\";\n}\n\n/**\n * Spawn a worker for a process whose selected turn is runnable but currently has\n * no running worker. No-ops when the process is not worker-runnable or already\n * has a worker.\n */\nexport async function ensureWorkerForActiveAgent(\n\tdeps: { events: ProcessEventRepoLike; supervisor: WorkerSupervisorLike },\n\tprocess: ProcessInstance,\n): Promise<void> {\n\tif (!processNeedsWorker(process) || deps.supervisor.getWorker(process.id)) {\n\t\treturn;\n\t}\n\ttry {\n\t\tawait deps.supervisor.spawnWorker(process.id);\n\t} catch (error) {\n\t\tdeps.events.create({\n\t\t\tinstanceId: process.id,\n\t\t\teventType: \"worker_spawn_failed\",\n\t\t\tdata: {\n\t\t\t\treason: error instanceof Error ? error.message : \"Failed to spawn worker\",\n\t\t\t},\n\t\t});\n\t\tthrow error;\n\t}\n}\n"],"mappings":";AAeO,SAAS,gBACf,QACA,YACA,WACU;AACV,SAAO,OAAO,eAAe,YAAY,GAAG,EAAE,KAAK,CAAC,UAAU,MAAM,cAAc,SAAS;AAC5F;AAEO,SAAS,mBACf,SACU;AACV,SAAO,QAAQ,mBAAmB,QAAQ,QAAQ,oBAAoB;AACvE;AAOA,eAAsB,2BACrB,MACA,SACgB;AAChB,MAAI,CAAC,mBAAmB,OAAO,KAAK,KAAK,WAAW,UAAU,QAAQ,EAAE,GAAG;AAC1E;AAAA,EACD;AACA,MAAI;AACH,UAAM,KAAK,WAAW,YAAY,QAAQ,EAAE;AAAA,EAC7C,SAAS,OAAO;AACf,SAAK,OAAO,OAAO;AAAA,MAClB,YAAY,QAAQ;AAAA,MACpB,WAAW;AAAA,MACX,MAAM;AAAA,QACL,QAAQ,iBAAiB,QAAQ,MAAM,UAAU;AAAA,MAClD;AAAA,IACD,CAAC;AACD,UAAM;AAAA,EACP;AACD;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/poll-loop.ts"],"sourcesContent":["import { parseDurationMs } from \"./duration-parse.js\";\n\n/** Reserve the next poll time for a caller-owned key, before starting its work. */\nexport function createPollSchedule(now: () => number = () => Date.now()) {\n\tconst dueAt = new Map<string, number>();\n\treturn (key: string, interval = \"30s\", timestamp = now()): boolean => {\n\t\tif ((dueAt.get(key) ?? 0) > timestamp) return false;\n\t\tdueAt.set(key, timestamp + parseDurationMs(interval, 30_000));\n\t\treturn true;\n\t};\n}\n\nexport interface PollResult {\n\tcreated: string[];\n\taborted: string[];\n\tlabelExchanged: string[];\n\tskipped: string[];\n\terrors: string[];\n}\n\nexport function emptyPollResult(): PollResult {\n\treturn {\n\t\tcreated: [],\n\t\taborted: [],\n\t\tlabelExchanged: [],\n\t\tskipped: [],\n\t\terrors: [],\n\t};\n}\n\nexport interface PollResultWithErrors {\n\treadonly errors: readonly string[];\n}\n\nexport interface PollLoop<T extends PollResultWithErrors = PollResult> {\n\tpoll(): Promise<T>;\n\tstart(): void;\n\tstop(): void;\n}\n\n/**\n * Create a non-overlapping polling loop. `pollOnce` is the actual work;\n * concurrent calls to `poll()` coalesce into the in-flight promise.\n */\nexport function createPollLoop<T extends PollResultWithErrors>(opts: {\n\tpollOnce: () => Promise<T>;\n\tisEnabled: () => boolean;\n\tpollInterval: () => string;\n\tdefaultIntervalMs?: number;\n\tonScheduledResult: (result: T, durationMs: number) => void;\n\tonScheduledError: (error: unknown, durationMs: number) => void;\n}): PollLoop<T> {\n\tlet timer: ReturnType<typeof setInterval> | null = null;\n\tlet pollInFlight: Promise<T> | null = null;\n\tconst defaultMs = opts.defaultIntervalMs ?? 60_000;\n\n\tasync function poll(): Promise<T> {\n\t\tif (pollInFlight) {\n\t\t\treturn pollInFlight;\n\t\t}\n\t\tconst running = (async () => {\n\t\t\ttry {\n\t\t\t\treturn await opts.pollOnce();\n\t\t\t} finally {\n\t\t\t\tpollInFlight = null;\n\t\t\t}\n\t\t})();\n\t\tpollInFlight = running;\n\t\treturn running;\n\t}\n\n\tfunction pollScheduled(): void {\n\t\tconst startedAt = Date.now();\n\t\tvoid poll().then(\n\t\t\t(result) => opts.onScheduledResult(result, Date.now() - startedAt),\n\t\t\t(error: unknown) => opts.onScheduledError(error, Date.now() - startedAt),\n\t\t);\n\t}\n\n\treturn {\n\t\tpoll,\n\t\tstart() {\n\t\t\tif (timer) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (!opts.isEnabled()) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst ms = parseDurationMs(opts.pollInterval(), defaultMs);\n\t\t\ttimer = setInterval(pollScheduled, ms);\n\t\t\tpollScheduled();\n\t\t},\n\t\tstop() {\n\t\t\tif (!timer) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tclearInterval(timer);\n\t\t\ttimer = null;\n\t\t},\n\t};\n}\n"],"mappings":";;;;;AAGO,SAAS,mBAAmB,MAAoB,MAAM,KAAK,IAAI,GAAG;AACxE,QAAM,QAAQ,oBAAI,IAAoB;AACtC,SAAO,CAAC,KAAa,WAAW,OAAO,YAAY,IAAI,MAAe;AACrE,SAAK,MAAM,IAAI,GAAG,KAAK,KAAK,UAAW,QAAO;AAC9C,UAAM,IAAI,KAAK,YAAY,gBAAgB,UAAU,GAAM,CAAC;AAC5D,WAAO;AAAA,EACR;AACD;AAUO,SAAS,kBAA8B;AAC7C,SAAO;AAAA,IACN,SAAS,CAAC;AAAA,IACV,SAAS,CAAC;AAAA,IACV,gBAAgB,CAAC;AAAA,IACjB,SAAS,CAAC;AAAA,IACV,QAAQ,CAAC;AAAA,EACV;AACD;AAgBO,SAAS,eAA+C,MAO/C;AACf,MAAI,QAA+C;AACnD,MAAI,eAAkC;AACtC,QAAM,YAAY,KAAK,qBAAqB;AAE5C,iBAAe,OAAmB;AACjC,QAAI,cAAc;AACjB,aAAO;AAAA,IACR;AACA,UAAM,WAAW,YAAY;AAC5B,UAAI;AACH,eAAO,MAAM,KAAK,SAAS;AAAA,MAC5B,UAAE;AACD,uBAAe;AAAA,MAChB;AAAA,IACD,GAAG;AACH,mBAAe;AACf,WAAO;AAAA,EACR;AAEA,WAAS,gBAAsB;AAC9B,UAAM,YAAY,KAAK,IAAI;AAC3B,SAAK,KAAK,EAAE;AAAA,MACX,CAAC,WAAW,KAAK,kBAAkB,QAAQ,KAAK,IAAI,IAAI,SAAS;AAAA,MACjE,CAAC,UAAmB,KAAK,iBAAiB,OAAO,KAAK,IAAI,IAAI,SAAS;AAAA,IACxE;AAAA,EACD;AAEA,SAAO;AAAA,IACN;AAAA,IACA,QAAQ;AACP,UAAI,OAAO;AACV;AAAA,MACD;AACA,UAAI,CAAC,KAAK,UAAU,GAAG;AACtB;AAAA,MACD;AACA,YAAM,KAAK,gBAAgB,KAAK,aAAa,GAAG,SAAS;AACzD,cAAQ,YAAY,eAAe,EAAE;AACrC,oBAAc;AAAA,IACf;AAAA,IACA,OAAO;AACN,UAAI,CAAC,OAAO;AACX;AAAA,MACD;AACA,oBAAc,KAAK;AACnB,cAAQ;AAAA,IACT;AAAA,EACD;AACD;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/duration-parse.ts"],"sourcesContent":["import ms from \"ms\";\n\n/**\n * Parse a human-friendly duration string (e.g. \"150ms\", \"1.5s\", \"2 min\") into\n * milliseconds. Returns `defaultMs` when the input cannot be parsed.\n *\n * `options.allowHours` is retained for existing callers; the underlying `ms`\n * parser supports hours and other common duration units by default.\n */\nexport function parseDurationMs(\n\tvalue: string,\n\tdefaultMs: number,\n\t_options: { allowHours?: boolean } = {},\n): number {\n\tconst parsed = ms(value.trim() as ms.StringValue);\n\tif (!Number.isFinite(parsed) || parsed < 0) {\n\t\treturn defaultMs;\n\t}\n\n\treturn parsed;\n}\n"],"mappings":";AAAA,OAAO,QAAQ;AASR,SAAS,gBACf,OACA,WACA,WAAqC,CAAC,GAC7B;AACT,QAAM,SAAS,GAAG,MAAM,KAAK,CAAmB;AAChD,MAAI,CAAC,OAAO,SAAS,MAAM,KAAK,SAAS,GAAG;AAC3C,WAAO;AAAA,EACR;AAEA,SAAO;AACR;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/polling-coordinator.ts"],"sourcesContent":["import { createPollLoop, type PollResult, type PollResultWithErrors } from \"./poll-loop.js\";\n\nexport interface PollingLogger {\n\twarn(payload: Record<string, unknown>, message?: string): void;\n\terror(payload: Record<string, unknown>, message?: string): void;\n}\n\nexport interface PollingRegistration<T extends PollResultWithErrors> {\n\tid: string;\n\tpollOnce(): Promise<T>;\n\tisEnabled(): boolean;\n\tpollInterval(): string;\n\tdefaultIntervalMs?: number;\n}\n\nexport interface RegisteredPoller<T extends PollResultWithErrors = PollResult> {\n\tpoll(): Promise<T>;\n}\n\nexport interface PollingCoordinator {\n\tcreate<T extends PollResultWithErrors>(registration: PollingRegistration<T>): RegisteredPoller<T>;\n\tstart(): void;\n\tstop(): void;\n}\n\nexport function createPollingCoordinator(logger: PollingLogger): PollingCoordinator {\n\tconst loops = new Map<string, ReturnType<typeof createPollLoop>>();\n\tlet sealed = false;\n\n\treturn {\n\t\tcreate<T extends PollResultWithErrors>(\n\t\t\tregistration: PollingRegistration<T>,\n\t\t): RegisteredPoller<T> {\n\t\t\tconst id = registration.id.trim();\n\t\t\tif (!id) throw new Error(\"Polling registration id must not be empty\");\n\t\t\tif (sealed) throw new Error(`Cannot register poller '${id}' after polling has started`);\n\t\t\tif (loops.has(id)) throw new Error(`Duplicate poller '${id}'`);\n\n\t\t\tconst loop = createPollLoop({\n\t\t\t\tpollOnce: registration.pollOnce,\n\t\t\t\tisEnabled: registration.isEnabled,\n\t\t\t\tpollInterval: registration.pollInterval,\n\t\t\t\tdefaultIntervalMs: registration.defaultIntervalMs,\n\t\t\t\tonScheduledResult(result, durationMs) {\n\t\t\t\t\tif (result.errors.length === 0) return;\n\t\t\t\t\tlogger.warn({ pollerId: id, durationMs, result }, \"Poll completed with errors\");\n\t\t\t\t},\n\t\t\t\tonScheduledError(error, durationMs) {\n\t\t\t\t\tlogger.error({ pollerId: id, durationMs, err: error }, \"Poll failed\");\n\t\t\t\t},\n\t\t\t});\n\t\t\tloops.set(id, loop);\n\t\t\treturn { poll: loop.poll };\n\t\t},\n\t\tstart() {\n\t\t\tsealed = true;\n\t\t\tfor (const loop of loops.values()) loop.start();\n\t\t},\n\t\tstop() {\n\t\t\tfor (const loop of loops.values()) loop.stop();\n\t\t},\n\t};\n}\n"],"mappings":";;;;;AAyBO,SAAS,yBAAyB,QAA2C;AACnF,QAAM,QAAQ,oBAAI,IAA+C;AACjE,MAAI,SAAS;AAEb,SAAO;AAAA,IACN,OACC,cACsB;AACtB,YAAM,KAAK,aAAa,GAAG,KAAK;AAChC,UAAI,CAAC,GAAI,OAAM,IAAI,MAAM,2CAA2C;AACpE,UAAI,OAAQ,OAAM,IAAI,MAAM,2BAA2B,EAAE,6BAA6B;AACtF,UAAI,MAAM,IAAI,EAAE,EAAG,OAAM,IAAI,MAAM,qBAAqB,EAAE,GAAG;AAE7D,YAAM,OAAO,eAAe;AAAA,QAC3B,UAAU,aAAa;AAAA,QACvB,WAAW,aAAa;AAAA,QACxB,cAAc,aAAa;AAAA,QAC3B,mBAAmB,aAAa;AAAA,QAChC,kBAAkB,QAAQ,YAAY;AACrC,cAAI,OAAO,OAAO,WAAW,EAAG;AAChC,iBAAO,KAAK,EAAE,UAAU,IAAI,YAAY,OAAO,GAAG,4BAA4B;AAAA,QAC/E;AAAA,QACA,iBAAiB,OAAO,YAAY;AACnC,iBAAO,MAAM,EAAE,UAAU,IAAI,YAAY,KAAK,MAAM,GAAG,aAAa;AAAA,QACrE;AAAA,MACD,CAAC;AACD,YAAM,IAAI,IAAI,IAAI;AAClB,aAAO,EAAE,MAAM,KAAK,KAAK;AAAA,IAC1B;AAAA,IACA,QAAQ;AACP,eAAS;AACT,iBAAW,QAAQ,MAAM,OAAO,EAAG,MAAK,MAAM;AAAA,IAC/C;AAAA,IACA,OAAO;AACN,iBAAW,QAAQ,MAAM,OAAO,EAAG,MAAK,KAAK;AAAA,IAC9C;AAAA,EACD;AACD;","names":[]}
|
package/dist/chunk-XLVMFO6W.js
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ensureWorkerForActiveAgent,
|
|
3
|
-
hasProcessEvent,
|
|
4
|
-
processNeedsWorker
|
|
5
|
-
} from "./chunk-3KGAUQIG.js";
|
|
6
|
-
|
|
7
|
-
// src/watcher-coordinator.ts
|
|
8
|
-
import {
|
|
9
|
-
recordWriteIfMissing
|
|
10
|
-
} from "@leitwerk-dev/external-writes";
|
|
11
|
-
function requireWatcherStartTurnId(launchPlan, targetLabel) {
|
|
12
|
-
if (launchPlan.startTurnId) {
|
|
13
|
-
return launchPlan.startTurnId;
|
|
14
|
-
}
|
|
15
|
-
throw new Error(
|
|
16
|
-
`Watcher launcher '${launchPlan.launcherId}' did not declare a startTurnId for ${targetLabel}`
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
async function continueWatcherAgentStartup(deps, opts) {
|
|
20
|
-
const { process } = opts;
|
|
21
|
-
if (!hasProcessEvent(deps.events, process.id, "agent_created")) {
|
|
22
|
-
deps.events.create({
|
|
23
|
-
instanceId: process.id,
|
|
24
|
-
eventType: "agent_created",
|
|
25
|
-
data: opts.createdEventData
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
await opts.afterCreatedEvent?.(process);
|
|
29
|
-
await opts.beforeStart?.(process);
|
|
30
|
-
let currentProcess = deps.processes.getById(process.id) ?? process;
|
|
31
|
-
if (currentProcess.lifecycleStatus === "discovered") {
|
|
32
|
-
const startResult = await deps.commands.startProcess(currentProcess.id, opts.startTurnId);
|
|
33
|
-
if (!startResult.ok) {
|
|
34
|
-
currentProcess = startResult.process ?? currentProcess;
|
|
35
|
-
throw new Error(startResult.message ?? "Failed to start process");
|
|
36
|
-
}
|
|
37
|
-
currentProcess = startResult.process ?? currentProcess;
|
|
38
|
-
if (opts.broadcastCreated !== false) {
|
|
39
|
-
deps.broadcaster.sendDurable(
|
|
40
|
-
"process.created",
|
|
41
|
-
{
|
|
42
|
-
process: currentProcess,
|
|
43
|
-
processId: currentProcess.processId,
|
|
44
|
-
...opts.createdBroadcastData
|
|
45
|
-
},
|
|
46
|
-
currentProcess.id
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
await ensureWorkerForActiveAgent(deps, currentProcess);
|
|
51
|
-
return currentProcess.id;
|
|
52
|
-
}
|
|
53
|
-
async function runWatcherDiscoveryPass(opts) {
|
|
54
|
-
for (const item of opts.items) {
|
|
55
|
-
try {
|
|
56
|
-
const existingAgent = opts.findExistingAgent(item);
|
|
57
|
-
if (existingAgent) {
|
|
58
|
-
if (existingAgent.lifecycleStatus === "discovered") {
|
|
59
|
-
const resumeResult = await opts.resumeDiscoveredAgent(existingAgent, item);
|
|
60
|
-
if (resumeResult === "skipped") {
|
|
61
|
-
opts.result.skipped.push(opts.getResultLabel(item));
|
|
62
|
-
}
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
if (opts.ensureWorkerForExistingAgent) {
|
|
66
|
-
const handled = await opts.ensureWorkerForExistingAgent(existingAgent, item);
|
|
67
|
-
if (handled) {
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
opts.result.skipped.push(opts.getResultLabel(item));
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
const createResult = await opts.createAgent(item);
|
|
75
|
-
if (createResult?.outcome === "created") {
|
|
76
|
-
opts.result.created.push(opts.getResultLabel(item));
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
if (createResult?.outcome === "skipped") {
|
|
80
|
-
opts.result.skipped.push(opts.getResultLabel(item));
|
|
81
|
-
}
|
|
82
|
-
} catch (error) {
|
|
83
|
-
opts.result.errors.push(opts.formatError(item, error));
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
async function runWatcherAbortReconciliation(opts) {
|
|
88
|
-
for (const process of opts.candidates) {
|
|
89
|
-
try {
|
|
90
|
-
const shouldAbort = await opts.shouldAbort(process);
|
|
91
|
-
if (!shouldAbort) {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
const abortResult = await opts.commands.abortProcess(process.id);
|
|
95
|
-
if (!abortResult.ok) {
|
|
96
|
-
if (abortResult.code === "worker_reconcile_failed") {
|
|
97
|
-
throw new Error(abortResult.message ?? "Failed to abort process");
|
|
98
|
-
}
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
const abortedAgent = abortResult.process ?? process;
|
|
102
|
-
if (!hasProcessEvent(opts.events, abortedAgent.id, "aborted_label_removed")) {
|
|
103
|
-
opts.events.create({
|
|
104
|
-
instanceId: abortedAgent.id,
|
|
105
|
-
eventType: "aborted_label_removed",
|
|
106
|
-
data: opts.getAbortedEventData(abortedAgent)
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
opts.result.aborted.push(opts.getResultLabel(process));
|
|
110
|
-
} catch (error) {
|
|
111
|
-
opts.result.errors.push(
|
|
112
|
-
`${opts.getErrorLabel(process)}: ${error instanceof Error ? error.message : String(error)}`
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
async function runWatcherCompletionReconciliation(opts) {
|
|
118
|
-
for (const process of opts.candidates) {
|
|
119
|
-
try {
|
|
120
|
-
const reconciliation = await opts.reconcile(process);
|
|
121
|
-
if (!reconciliation.changed) {
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
if (reconciliation.writeIdentity) {
|
|
125
|
-
recordWriteIfMissing(
|
|
126
|
-
opts.externalWrites,
|
|
127
|
-
process.id,
|
|
128
|
-
reconciliation.writeIdentity,
|
|
129
|
-
reconciliation.metadata
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
opts.result.labelExchanged.push(opts.getResultLabel(process));
|
|
133
|
-
} catch (error) {
|
|
134
|
-
opts.result.errors.push(
|
|
135
|
-
`${opts.getErrorLabel(process)}: ${error instanceof Error ? error.message : String(error)}`
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
function shouldEnsureWorkerForExistingActiveAgent(deps, process) {
|
|
141
|
-
return processNeedsWorker(process) && !deps.supervisor.getWorker(process.id);
|
|
142
|
-
}
|
|
143
|
-
function deferWatcherLaunchStart(launchPlan) {
|
|
144
|
-
return launchPlan.startTurnId === null ? launchPlan : { ...launchPlan, startTurnId: null };
|
|
145
|
-
}
|
|
146
|
-
function launchFailureMessage(result) {
|
|
147
|
-
return typeof result.body.error === "string" && result.body.error.trim().length > 0 ? result.body.error : `Process launch failed with status ${result.status}`;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export {
|
|
151
|
-
requireWatcherStartTurnId,
|
|
152
|
-
continueWatcherAgentStartup,
|
|
153
|
-
runWatcherDiscoveryPass,
|
|
154
|
-
runWatcherAbortReconciliation,
|
|
155
|
-
runWatcherCompletionReconciliation,
|
|
156
|
-
shouldEnsureWorkerForExistingActiveAgent,
|
|
157
|
-
deferWatcherLaunchStart,
|
|
158
|
-
launchFailureMessage
|
|
159
|
-
};
|
|
160
|
-
//# sourceMappingURL=chunk-XLVMFO6W.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/watcher-coordinator.ts"],"sourcesContent":["import type { ProcessInstance, TurnId } from \"@leitwerk-dev/domain\";\nimport {\n\ttype ExternalWriteLogRepoLike,\n\trecordWriteIfMissing,\n\ttype WriteIdentity,\n} from \"@leitwerk-dev/external-writes\";\nimport type { KnownDurableWsFrameType, WsPayloadByType } from \"@leitwerk-dev/protocol\";\nimport type { PollResult } from \"./poll-loop.js\";\nimport {\n\tensureWorkerForActiveAgent,\n\thasProcessEvent,\n\ttype ProcessEventRepoLike,\n\tprocessNeedsWorker,\n\ttype WorkerSupervisorLike,\n} from \"./process-helpers.js\";\n\nexport interface AgentRepoAccessorLike {\n\tgetById(id: string): ProcessInstance | null;\n}\n\nexport interface BroadcasterLike {\n\tsendDurable<T extends KnownDurableWsFrameType>(\n\t\ttype: T,\n\t\tpayload: WsPayloadByType[T],\n\t\tinstanceId?: string,\n\t): void;\n}\n\nexport interface WatcherCommandResult {\n\tok: boolean;\n\tcode?: string;\n\tmessage?: string;\n\tprocess?: ProcessInstance | null;\n}\n\nexport interface WatcherCommandServiceLike {\n\tstartProcess(instanceId: string, startTurnId: TurnId): Promise<WatcherCommandResult>;\n\tabortProcess(instanceId: string): Promise<WatcherCommandResult>;\n}\n\nexport interface WatcherStartupDeps {\n\tprocesses: AgentRepoAccessorLike;\n\tevents: ProcessEventRepoLike;\n\tbroadcaster: BroadcasterLike;\n\tcommands: WatcherCommandServiceLike;\n\tsupervisor: WorkerSupervisorLike;\n}\n\nexport interface ContinueWatcherAgentStartupOptions {\n\tprocess: ProcessInstance;\n\tstartTurnId: TurnId;\n\tcreatedEventData: Record<string, unknown>;\n\tcreatedBroadcastData: Record<string, unknown>;\n\t/** Defaults to true. Set false when the shared launch executor already emitted process.created. */\n\tbroadcastCreated?: boolean;\n\tafterCreatedEvent?: (process: ProcessInstance) => Promise<void> | void;\n\tbeforeStart?: (process: ProcessInstance) => Promise<void>;\n}\n\nexport function requireWatcherStartTurnId(\n\tlaunchPlan: { launcherId: string; startTurnId: TurnId | null },\n\ttargetLabel: string,\n): TurnId {\n\tif (launchPlan.startTurnId) {\n\t\treturn launchPlan.startTurnId;\n\t}\n\tthrow new Error(\n\t\t`Watcher launcher '${launchPlan.launcherId}' did not declare a startTurnId for ${targetLabel}`,\n\t);\n}\n\nexport async function continueWatcherAgentStartup(\n\tdeps: WatcherStartupDeps,\n\topts: ContinueWatcherAgentStartupOptions,\n): Promise<string> {\n\tconst { process } = opts;\n\n\tif (!hasProcessEvent(deps.events, process.id, \"agent_created\")) {\n\t\tdeps.events.create({\n\t\t\tinstanceId: process.id,\n\t\t\teventType: \"agent_created\",\n\t\t\tdata: opts.createdEventData,\n\t\t});\n\t}\n\n\tawait opts.afterCreatedEvent?.(process);\n\tawait opts.beforeStart?.(process);\n\n\tlet currentProcess = deps.processes.getById(process.id) ?? process;\n\tif (currentProcess.lifecycleStatus === \"discovered\") {\n\t\tconst startResult = await deps.commands.startProcess(currentProcess.id, opts.startTurnId);\n\t\tif (!startResult.ok) {\n\t\t\tcurrentProcess = startResult.process ?? currentProcess;\n\t\t\tthrow new Error(startResult.message ?? \"Failed to start process\");\n\t\t}\n\t\tcurrentProcess = startResult.process ?? currentProcess;\n\t\tif (opts.broadcastCreated !== false) {\n\t\t\tdeps.broadcaster.sendDurable(\n\t\t\t\t\"process.created\",\n\t\t\t\t{\n\t\t\t\t\tprocess: currentProcess,\n\t\t\t\t\tprocessId: currentProcess.processId,\n\t\t\t\t\t...opts.createdBroadcastData,\n\t\t\t\t},\n\t\t\t\tcurrentProcess.id,\n\t\t\t);\n\t\t}\n\t}\n\n\tawait ensureWorkerForActiveAgent(deps, currentProcess);\n\treturn currentProcess.id;\n}\n\nexport type DiscoveryResumeResult = \"handled\" | \"skipped\" | undefined;\n\nexport type DiscoveryCreateResult =\n\t| { outcome: \"created\"; instanceId: string }\n\t| { outcome: \"skipped\" }\n\t| { outcome: \"handled\" }\n\t| null\n\t| undefined;\n\nexport interface DiscoveryPassOptions<TItem> {\n\titems: readonly TItem[];\n\tresult: PollResult;\n\tfindExistingAgent(item: TItem): ProcessInstance | null;\n\tgetResultLabel(item: TItem): string;\n\tresumeDiscoveredAgent(process: ProcessInstance, item: TItem): Promise<DiscoveryResumeResult>;\n\tcreateAgent(item: TItem): Promise<DiscoveryCreateResult>;\n\tensureWorkerForExistingAgent?(process: ProcessInstance, item: TItem): Promise<boolean>;\n\tformatError(item: TItem, error: unknown): string;\n}\n\nexport async function runWatcherDiscoveryPass<TItem>(\n\topts: DiscoveryPassOptions<TItem>,\n): Promise<void> {\n\tfor (const item of opts.items) {\n\t\ttry {\n\t\t\tconst existingAgent = opts.findExistingAgent(item);\n\t\t\tif (existingAgent) {\n\t\t\t\tif (existingAgent.lifecycleStatus === \"discovered\") {\n\t\t\t\t\tconst resumeResult = await opts.resumeDiscoveredAgent(existingAgent, item);\n\t\t\t\t\tif (resumeResult === \"skipped\") {\n\t\t\t\t\t\topts.result.skipped.push(opts.getResultLabel(item));\n\t\t\t\t\t}\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (opts.ensureWorkerForExistingAgent) {\n\t\t\t\t\tconst handled = await opts.ensureWorkerForExistingAgent(existingAgent, item);\n\t\t\t\t\tif (handled) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\topts.result.skipped.push(opts.getResultLabel(item));\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst createResult = await opts.createAgent(item);\n\t\t\tif (createResult?.outcome === \"created\") {\n\t\t\t\topts.result.created.push(opts.getResultLabel(item));\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (createResult?.outcome === \"skipped\") {\n\t\t\t\topts.result.skipped.push(opts.getResultLabel(item));\n\t\t\t}\n\t\t} catch (error) {\n\t\t\topts.result.errors.push(opts.formatError(item, error));\n\t\t}\n\t}\n}\n\nexport interface AbortReconciliationOptions {\n\tcandidates: readonly ProcessInstance[];\n\tresult: PollResult;\n\tevents: ProcessEventRepoLike;\n\tcommands: WatcherCommandServiceLike;\n\tshouldAbort(process: ProcessInstance): Promise<boolean>;\n\tgetResultLabel(process: ProcessInstance): string;\n\tgetErrorLabel(process: ProcessInstance): string;\n\tgetAbortedEventData(process: ProcessInstance): Record<string, unknown>;\n}\n\nexport async function runWatcherAbortReconciliation(\n\topts: AbortReconciliationOptions,\n): Promise<void> {\n\tfor (const process of opts.candidates) {\n\t\ttry {\n\t\t\tconst shouldAbort = await opts.shouldAbort(process);\n\t\t\tif (!shouldAbort) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst abortResult = await opts.commands.abortProcess(process.id);\n\t\t\tif (!abortResult.ok) {\n\t\t\t\tif (abortResult.code === \"worker_reconcile_failed\") {\n\t\t\t\t\tthrow new Error(abortResult.message ?? \"Failed to abort process\");\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst abortedAgent = abortResult.process ?? process;\n\t\t\tif (!hasProcessEvent(opts.events, abortedAgent.id, \"aborted_label_removed\")) {\n\t\t\t\topts.events.create({\n\t\t\t\t\tinstanceId: abortedAgent.id,\n\t\t\t\t\teventType: \"aborted_label_removed\",\n\t\t\t\t\tdata: opts.getAbortedEventData(abortedAgent),\n\t\t\t\t});\n\t\t\t}\n\t\t\topts.result.aborted.push(opts.getResultLabel(process));\n\t\t} catch (error) {\n\t\t\topts.result.errors.push(\n\t\t\t\t`${opts.getErrorLabel(process)}: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t}\n\t}\n}\n\nexport interface CompletionReconciliationResult {\n\tchanged: boolean;\n\twriteIdentity?: WriteIdentity;\n\tmetadata?: Record<string, unknown>;\n}\n\nexport interface CompletionReconciliationOptions {\n\tcandidates: readonly ProcessInstance[];\n\tresult: PollResult;\n\texternalWrites: ExternalWriteLogRepoLike;\n\treconcile(process: ProcessInstance): Promise<CompletionReconciliationResult>;\n\tgetResultLabel(process: ProcessInstance): string;\n\tgetErrorLabel(process: ProcessInstance): string;\n}\n\nexport async function runWatcherCompletionReconciliation(\n\topts: CompletionReconciliationOptions,\n): Promise<void> {\n\tfor (const process of opts.candidates) {\n\t\ttry {\n\t\t\tconst reconciliation = await opts.reconcile(process);\n\t\t\tif (!reconciliation.changed) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (reconciliation.writeIdentity) {\n\t\t\t\trecordWriteIfMissing(\n\t\t\t\t\topts.externalWrites,\n\t\t\t\t\tprocess.id,\n\t\t\t\t\treconciliation.writeIdentity,\n\t\t\t\t\treconciliation.metadata,\n\t\t\t\t);\n\t\t\t}\n\t\t\topts.result.labelExchanged.push(opts.getResultLabel(process));\n\t\t} catch (error) {\n\t\t\topts.result.errors.push(\n\t\t\t\t`${opts.getErrorLabel(process)}: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t}\n\t}\n}\n\nexport function shouldEnsureWorkerForExistingActiveAgent(\n\tdeps: { supervisor: WorkerSupervisorLike },\n\tprocess: ProcessInstance,\n): boolean {\n\treturn processNeedsWorker(process) && !deps.supervisor.getWorker(process.id);\n}\n\nexport function deferWatcherLaunchStart<TLaunchPlan extends { startTurnId: unknown }>(\n\tlaunchPlan: TLaunchPlan,\n): TLaunchPlan {\n\treturn launchPlan.startTurnId === null ? launchPlan : { ...launchPlan, startTurnId: null };\n}\n\nexport function launchFailureMessage(result: {\n\tstatus: number;\n\tbody: Record<string, unknown>;\n}): string {\n\treturn typeof result.body.error === \"string\" && result.body.error.trim().length > 0\n\t\t? result.body.error\n\t\t: `Process launch failed with status ${result.status}`;\n}\n"],"mappings":"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","names":[]}
|