@leitwerk-dev/watcher-utils 0.1.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/dist/LICENSE +13 -0
- package/dist/chunk-3KGAUQIG.js +31 -0
- package/dist/chunk-3KGAUQIG.js.map +1 -0
- package/dist/chunk-IU3L35PC.js +14 -0
- package/dist/chunk-IU3L35PC.js.map +1 -0
- package/dist/chunk-XF2QQLPO.js +63 -0
- package/dist/chunk-XF2QQLPO.js.map +1 -0
- package/dist/chunk-XLVMFO6W.js +160 -0
- package/dist/chunk-XLVMFO6W.js.map +1 -0
- package/dist/duration-parse.d.ts +11 -0
- package/dist/duration-parse.d.ts.map +1 -0
- package/dist/duration-parse.js +16 -0
- package/dist/duration-parse.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/poll-loop.d.ts +24 -0
- package/dist/poll-loop.d.ts.map +1 -0
- package/dist/poll-loop.js +59 -0
- package/dist/poll-loop.js.map +1 -0
- package/dist/process-helpers.d.ts +30 -0
- package/dist/process-helpers.d.ts.map +1 -0
- package/dist/process-helpers.js +33 -0
- package/dist/process-helpers.js.map +1 -0
- package/dist/trigger-files.d.ts +10 -0
- package/dist/trigger-files.d.ts.map +1 -0
- package/dist/trigger-files.js +31 -0
- package/dist/trigger-files.js.map +1 -0
- package/dist/watcher-coordinator.d.ts +99 -0
- package/dist/watcher-coordinator.d.ts.map +1 -0
- package/dist/watcher-coordinator.js +131 -0
- package/dist/watcher-coordinator.js.map +1 -0
- package/package.json +43 -0
- package/src/duration-parse.ts +21 -0
- package/src/index.ts +49 -0
- package/src/poll-loop.ts +81 -0
- package/src/process-helpers.ts +54 -0
- package/src/trigger-files.ts +33 -0
- package/src/watcher-coordinator.ts +280 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { ProcessInstance, TurnId } from "@leitwerk-dev/domain";
|
|
2
|
+
import { type ExternalWriteLogRepoLike, type WriteIdentity } from "@leitwerk-dev/external-writes";
|
|
3
|
+
import type { KnownDurableWsFrameType, WsPayloadByType } from "@leitwerk-dev/protocol";
|
|
4
|
+
import type { PollResult } from "./poll-loop.js";
|
|
5
|
+
import { type ProcessEventRepoLike, type WorkerSupervisorLike } from "./process-helpers.js";
|
|
6
|
+
export interface AgentRepoAccessorLike {
|
|
7
|
+
getById(id: string): ProcessInstance | null;
|
|
8
|
+
}
|
|
9
|
+
export interface BroadcasterLike {
|
|
10
|
+
sendDurable<T extends KnownDurableWsFrameType>(type: T, payload: WsPayloadByType[T], instanceId?: string): void;
|
|
11
|
+
}
|
|
12
|
+
export interface WatcherCommandResult {
|
|
13
|
+
ok: boolean;
|
|
14
|
+
code?: string;
|
|
15
|
+
message?: string;
|
|
16
|
+
process?: ProcessInstance | null;
|
|
17
|
+
}
|
|
18
|
+
export interface WatcherCommandServiceLike {
|
|
19
|
+
startProcess(instanceId: string, startTurnId: TurnId): Promise<WatcherCommandResult>;
|
|
20
|
+
abortProcess(instanceId: string): Promise<WatcherCommandResult>;
|
|
21
|
+
}
|
|
22
|
+
export interface WatcherStartupDeps {
|
|
23
|
+
processes: AgentRepoAccessorLike;
|
|
24
|
+
events: ProcessEventRepoLike;
|
|
25
|
+
broadcaster: BroadcasterLike;
|
|
26
|
+
commands: WatcherCommandServiceLike;
|
|
27
|
+
supervisor: WorkerSupervisorLike;
|
|
28
|
+
}
|
|
29
|
+
export interface ContinueWatcherAgentStartupOptions {
|
|
30
|
+
process: ProcessInstance;
|
|
31
|
+
startTurnId: TurnId;
|
|
32
|
+
createdEventData: Record<string, unknown>;
|
|
33
|
+
createdBroadcastData: Record<string, unknown>;
|
|
34
|
+
/** Defaults to true. Set false when the shared launch executor already emitted process.created. */
|
|
35
|
+
broadcastCreated?: boolean;
|
|
36
|
+
afterCreatedEvent?: (process: ProcessInstance) => Promise<void> | void;
|
|
37
|
+
beforeStart?: (process: ProcessInstance) => Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
export declare function requireWatcherStartTurnId(launchPlan: {
|
|
40
|
+
launcherId: string;
|
|
41
|
+
startTurnId: TurnId | null;
|
|
42
|
+
}, targetLabel: string): TurnId;
|
|
43
|
+
export declare function continueWatcherAgentStartup(deps: WatcherStartupDeps, opts: ContinueWatcherAgentStartupOptions): Promise<string>;
|
|
44
|
+
export type DiscoveryResumeResult = "handled" | "skipped" | undefined;
|
|
45
|
+
export type DiscoveryCreateResult = {
|
|
46
|
+
outcome: "created";
|
|
47
|
+
instanceId: string;
|
|
48
|
+
} | {
|
|
49
|
+
outcome: "skipped";
|
|
50
|
+
} | {
|
|
51
|
+
outcome: "handled";
|
|
52
|
+
} | null | undefined;
|
|
53
|
+
export interface DiscoveryPassOptions<TItem> {
|
|
54
|
+
items: readonly TItem[];
|
|
55
|
+
result: PollResult;
|
|
56
|
+
findExistingAgent(item: TItem): ProcessInstance | null;
|
|
57
|
+
getResultLabel(item: TItem): string;
|
|
58
|
+
resumeDiscoveredAgent(process: ProcessInstance, item: TItem): Promise<DiscoveryResumeResult>;
|
|
59
|
+
createAgent(item: TItem): Promise<DiscoveryCreateResult>;
|
|
60
|
+
ensureWorkerForExistingAgent?(process: ProcessInstance, item: TItem): Promise<boolean>;
|
|
61
|
+
formatError(item: TItem, error: unknown): string;
|
|
62
|
+
}
|
|
63
|
+
export declare function runWatcherDiscoveryPass<TItem>(opts: DiscoveryPassOptions<TItem>): Promise<void>;
|
|
64
|
+
export interface AbortReconciliationOptions {
|
|
65
|
+
candidates: readonly ProcessInstance[];
|
|
66
|
+
result: PollResult;
|
|
67
|
+
events: ProcessEventRepoLike;
|
|
68
|
+
commands: WatcherCommandServiceLike;
|
|
69
|
+
shouldAbort(process: ProcessInstance): Promise<boolean>;
|
|
70
|
+
getResultLabel(process: ProcessInstance): string;
|
|
71
|
+
getErrorLabel(process: ProcessInstance): string;
|
|
72
|
+
getAbortedEventData(process: ProcessInstance): Record<string, unknown>;
|
|
73
|
+
}
|
|
74
|
+
export declare function runWatcherAbortReconciliation(opts: AbortReconciliationOptions): Promise<void>;
|
|
75
|
+
export interface CompletionReconciliationResult {
|
|
76
|
+
changed: boolean;
|
|
77
|
+
writeIdentity?: WriteIdentity;
|
|
78
|
+
metadata?: Record<string, unknown>;
|
|
79
|
+
}
|
|
80
|
+
export interface CompletionReconciliationOptions {
|
|
81
|
+
candidates: readonly ProcessInstance[];
|
|
82
|
+
result: PollResult;
|
|
83
|
+
externalWrites: ExternalWriteLogRepoLike;
|
|
84
|
+
reconcile(process: ProcessInstance): Promise<CompletionReconciliationResult>;
|
|
85
|
+
getResultLabel(process: ProcessInstance): string;
|
|
86
|
+
getErrorLabel(process: ProcessInstance): string;
|
|
87
|
+
}
|
|
88
|
+
export declare function runWatcherCompletionReconciliation(opts: CompletionReconciliationOptions): Promise<void>;
|
|
89
|
+
export declare function shouldEnsureWorkerForExistingActiveAgent(deps: {
|
|
90
|
+
supervisor: WorkerSupervisorLike;
|
|
91
|
+
}, process: ProcessInstance): boolean;
|
|
92
|
+
export declare function deferWatcherLaunchStart<TLaunchPlan extends {
|
|
93
|
+
startTurnId: unknown;
|
|
94
|
+
}>(launchPlan: TLaunchPlan): TLaunchPlan;
|
|
95
|
+
export declare function launchFailureMessage(result: {
|
|
96
|
+
status: number;
|
|
97
|
+
body: Record<string, unknown>;
|
|
98
|
+
}): string;
|
|
99
|
+
//# sourceMappingURL=watcher-coordinator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watcher-coordinator.d.ts","sourceRoot":"","sources":["../src/watcher-coordinator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AACpE,OAAO,EACN,KAAK,wBAAwB,EAE7B,KAAK,aAAa,EAClB,MAAM,+BAA+B,CAAC;AACvC,OAAO,KAAK,EAAE,uBAAuB,EAAE,eAAe,EAAE,MAAM,wBAAwB,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ACjD,aAAa,CAAC,OAAO,EAAE,eAAe,GAAG,MAAM,CAAC;CAChD;AAED,wBAAsB,kCAAkC,CACvD,IAAI,EAAE,+BAA+B,GACnC,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED,wBAAgB,wCAAwC,CACvD,IAAI,EAAE;IAAE,UAAU,EAAE,oBAAoB,CAAA;CAAE,EAC1C,OAAO,EAAE,eAAe,GACtB,OAAO,CAET;AAED,wBAAgB,uBAAuB,CAAC,WAAW,SAAS;IAAE,WAAW,EAAE,OAAO,CAAA;CAAE,EACnF,UAAU,EAAE,WAAW,GACrB,WAAW,CAEb;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE;IAC5C,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC9B,GAAG,MAAM,CAIT"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { recordWriteIfMissing, } from "@leitwerk-dev/external-writes";
|
|
2
|
+
import { ensureWorkerForActiveAgent, hasProcessEvent, processNeedsWorker, } from "./process-helpers.js";
|
|
3
|
+
export function requireWatcherStartTurnId(launchPlan, targetLabel) {
|
|
4
|
+
if (launchPlan.startTurnId) {
|
|
5
|
+
return launchPlan.startTurnId;
|
|
6
|
+
}
|
|
7
|
+
throw new Error(`Watcher launcher '${launchPlan.launcherId}' did not declare a startTurnId for ${targetLabel}`);
|
|
8
|
+
}
|
|
9
|
+
export async function continueWatcherAgentStartup(deps, opts) {
|
|
10
|
+
const { process } = opts;
|
|
11
|
+
if (!hasProcessEvent(deps.events, process.id, "agent_created")) {
|
|
12
|
+
deps.events.create({
|
|
13
|
+
instanceId: process.id,
|
|
14
|
+
eventType: "agent_created",
|
|
15
|
+
data: opts.createdEventData,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
await opts.afterCreatedEvent?.(process);
|
|
19
|
+
await opts.beforeStart?.(process);
|
|
20
|
+
let currentProcess = deps.processes.getById(process.id) ?? process;
|
|
21
|
+
if (currentProcess.lifecycleStatus === "discovered") {
|
|
22
|
+
const startResult = await deps.commands.startProcess(currentProcess.id, opts.startTurnId);
|
|
23
|
+
if (!startResult.ok) {
|
|
24
|
+
currentProcess = startResult.process ?? currentProcess;
|
|
25
|
+
throw new Error(startResult.message ?? "Failed to start process");
|
|
26
|
+
}
|
|
27
|
+
currentProcess = startResult.process ?? currentProcess;
|
|
28
|
+
if (opts.broadcastCreated !== false) {
|
|
29
|
+
deps.broadcaster.sendDurable("process.created", {
|
|
30
|
+
process: currentProcess,
|
|
31
|
+
processId: currentProcess.processId,
|
|
32
|
+
...opts.createdBroadcastData,
|
|
33
|
+
}, currentProcess.id);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
await ensureWorkerForActiveAgent(deps, currentProcess);
|
|
37
|
+
return currentProcess.id;
|
|
38
|
+
}
|
|
39
|
+
export async function runWatcherDiscoveryPass(opts) {
|
|
40
|
+
for (const item of opts.items) {
|
|
41
|
+
try {
|
|
42
|
+
const existingAgent = opts.findExistingAgent(item);
|
|
43
|
+
if (existingAgent) {
|
|
44
|
+
if (existingAgent.lifecycleStatus === "discovered") {
|
|
45
|
+
const resumeResult = await opts.resumeDiscoveredAgent(existingAgent, item);
|
|
46
|
+
if (resumeResult === "skipped") {
|
|
47
|
+
opts.result.skipped.push(opts.getResultLabel(item));
|
|
48
|
+
}
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (opts.ensureWorkerForExistingAgent) {
|
|
52
|
+
const handled = await opts.ensureWorkerForExistingAgent(existingAgent, item);
|
|
53
|
+
if (handled) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
opts.result.skipped.push(opts.getResultLabel(item));
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const createResult = await opts.createAgent(item);
|
|
61
|
+
if (createResult?.outcome === "created") {
|
|
62
|
+
opts.result.created.push(opts.getResultLabel(item));
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (createResult?.outcome === "skipped") {
|
|
66
|
+
opts.result.skipped.push(opts.getResultLabel(item));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
opts.result.errors.push(opts.formatError(item, error));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export async function runWatcherAbortReconciliation(opts) {
|
|
75
|
+
for (const process of opts.candidates) {
|
|
76
|
+
try {
|
|
77
|
+
const shouldAbort = await opts.shouldAbort(process);
|
|
78
|
+
if (!shouldAbort) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const abortResult = await opts.commands.abortProcess(process.id);
|
|
82
|
+
if (!abortResult.ok) {
|
|
83
|
+
if (abortResult.code === "worker_reconcile_failed") {
|
|
84
|
+
throw new Error(abortResult.message ?? "Failed to abort process");
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const abortedAgent = abortResult.process ?? process;
|
|
89
|
+
if (!hasProcessEvent(opts.events, abortedAgent.id, "aborted_label_removed")) {
|
|
90
|
+
opts.events.create({
|
|
91
|
+
instanceId: abortedAgent.id,
|
|
92
|
+
eventType: "aborted_label_removed",
|
|
93
|
+
data: opts.getAbortedEventData(abortedAgent),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
opts.result.aborted.push(opts.getResultLabel(process));
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
opts.result.errors.push(`${opts.getErrorLabel(process)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export async function runWatcherCompletionReconciliation(opts) {
|
|
104
|
+
for (const process of opts.candidates) {
|
|
105
|
+
try {
|
|
106
|
+
const reconciliation = await opts.reconcile(process);
|
|
107
|
+
if (!reconciliation.changed) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (reconciliation.writeIdentity) {
|
|
111
|
+
recordWriteIfMissing(opts.externalWrites, process.id, reconciliation.writeIdentity, reconciliation.metadata);
|
|
112
|
+
}
|
|
113
|
+
opts.result.labelExchanged.push(opts.getResultLabel(process));
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
opts.result.errors.push(`${opts.getErrorLabel(process)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
export function shouldEnsureWorkerForExistingActiveAgent(deps, process) {
|
|
121
|
+
return processNeedsWorker(process) && !deps.supervisor.getWorker(process.id);
|
|
122
|
+
}
|
|
123
|
+
export function deferWatcherLaunchStart(launchPlan) {
|
|
124
|
+
return launchPlan.startTurnId === null ? launchPlan : { ...launchPlan, startTurnId: null };
|
|
125
|
+
}
|
|
126
|
+
export function launchFailureMessage(result) {
|
|
127
|
+
return typeof result.body.error === "string" && result.body.error.trim().length > 0
|
|
128
|
+
? result.body.error
|
|
129
|
+
: `Process launch failed with status ${result.status}`;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=watcher-coordinator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watcher-coordinator.js","sourceRoot":"","sources":["../src/watcher-coordinator.ts"],"names":[],"mappings":"AACA,OAAO,EAEN,oBAAoB,GAEpB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,EACN,0BAA0B,EAC1B,eAAe,EAEf,kBAAkB,GAElB,MAAM,sBAAsB,CAAC;AA6C9B,MAAM,UAAU,yBAAyB,CACxC,UAA8D,EAC9D,WAAmB;IAEnB,IAAI,UAAU,CAAC,WAAW,EAAE,CAAC;QAC5B,OAAO,UAAU,CAAC,WAAW,CAAC;IAC/B,CAAC;IACD,MAAM,IAAI,KAAK,CACd,qBAAqB,UAAU,CAAC,UAAU,uCAAuC,WAAW,EAAE,CAC9F,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,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AAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAC3F,CAAC;QACH,CAAC;IACF,CAAC;AACF,CAAC;AAED,MAAM,UAAU,wCAAwC,CACvD,IAA0C,EAC1C,OAAwB;IAExB,OAAO,kBAAkB,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;AAC9E,CAAC;AAED,MAAM,UAAU,uBAAuB,CACtC,UAAuB;IAEvB,OAAO,UAAU,CAAC,WAAW,KAAK,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,UAAU,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;AAC5F,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,MAGpC;IACA,OAAO,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC;QAClF,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK;QACnB,CAAC,CAAC,qCAAqC,MAAM,CAAC,MAAM,EAAE,CAAC;AACzD,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leitwerk-dev/watcher-utils",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/leitwerk-dev/leitwerk.git",
|
|
7
|
+
"directory": "packages/watcher-utils"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"source": "./src/index.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup --silent"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@leitwerk-dev/domain": "0.1.0",
|
|
22
|
+
"@leitwerk-dev/external-writes": "0.1.0",
|
|
23
|
+
"@leitwerk-dev/protocol": "0.1.0",
|
|
24
|
+
"ms": "^2.1.3"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/ms": "^2.1.0",
|
|
28
|
+
"tsup": "^8.5.1"
|
|
29
|
+
},
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=22"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist/**",
|
|
39
|
+
"src/**",
|
|
40
|
+
"!dist/**/*.test.*",
|
|
41
|
+
"!src/**/*.test.ts"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import ms from "ms";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parse a human-friendly duration string (e.g. "150ms", "1.5s", "2 min") into
|
|
5
|
+
* milliseconds. Returns `defaultMs` when the input cannot be parsed.
|
|
6
|
+
*
|
|
7
|
+
* `options.allowHours` is retained for existing callers; the underlying `ms`
|
|
8
|
+
* parser supports hours and other common duration units by default.
|
|
9
|
+
*/
|
|
10
|
+
export function parseDurationMs(
|
|
11
|
+
value: string,
|
|
12
|
+
defaultMs: number,
|
|
13
|
+
_options: { allowHours?: boolean } = {},
|
|
14
|
+
): number {
|
|
15
|
+
const parsed = ms(value.trim() as ms.StringValue);
|
|
16
|
+
if (!Number.isFinite(parsed) || parsed < 0) {
|
|
17
|
+
return defaultMs;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return parsed;
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** @deprecated Import external-write idempotency from `@leitwerk-dev/external-writes`. */
|
|
2
|
+
export {
|
|
3
|
+
createWriteIdentity,
|
|
4
|
+
type EnsureWriteResult,
|
|
5
|
+
type ExternalWriteLogRecordInput,
|
|
6
|
+
type ExternalWriteLogRepoLike,
|
|
7
|
+
ensureWrite,
|
|
8
|
+
recordWriteIfMissing,
|
|
9
|
+
type WriteIdentity,
|
|
10
|
+
} from "@leitwerk-dev/external-writes";
|
|
11
|
+
export { parseDurationMs } from "./duration-parse.js";
|
|
12
|
+
export {
|
|
13
|
+
createPollLoop,
|
|
14
|
+
emptyPollResult,
|
|
15
|
+
type PollLoop,
|
|
16
|
+
type PollResult,
|
|
17
|
+
} from "./poll-loop.js";
|
|
18
|
+
export {
|
|
19
|
+
ensureWorkerForActiveAgent,
|
|
20
|
+
hasProcessEvent,
|
|
21
|
+
type ProcessEventRepoLike,
|
|
22
|
+
processNeedsWorker,
|
|
23
|
+
type WorkerSupervisorLike,
|
|
24
|
+
} from "./process-helpers.js";
|
|
25
|
+
export {
|
|
26
|
+
consumeTriggerFile,
|
|
27
|
+
errorCode,
|
|
28
|
+
readTriggerFile,
|
|
29
|
+
type TriggerFileReadResult,
|
|
30
|
+
} from "./trigger-files.js";
|
|
31
|
+
export {
|
|
32
|
+
type AgentRepoAccessorLike,
|
|
33
|
+
type BroadcasterLike,
|
|
34
|
+
type CompletionReconciliationOptions,
|
|
35
|
+
type CompletionReconciliationResult,
|
|
36
|
+
type ContinueWatcherAgentStartupOptions,
|
|
37
|
+
continueWatcherAgentStartup,
|
|
38
|
+
type DiscoveryPassOptions,
|
|
39
|
+
deferWatcherLaunchStart,
|
|
40
|
+
launchFailureMessage,
|
|
41
|
+
requireWatcherStartTurnId,
|
|
42
|
+
runWatcherAbortReconciliation,
|
|
43
|
+
runWatcherCompletionReconciliation,
|
|
44
|
+
runWatcherDiscoveryPass,
|
|
45
|
+
shouldEnsureWorkerForExistingActiveAgent,
|
|
46
|
+
type WatcherCommandResult,
|
|
47
|
+
type WatcherCommandServiceLike,
|
|
48
|
+
type WatcherStartupDeps,
|
|
49
|
+
} from "./watcher-coordinator.js";
|
package/src/poll-loop.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { parseDurationMs } from "./duration-parse.js";
|
|
2
|
+
|
|
3
|
+
export interface PollResult {
|
|
4
|
+
created: string[];
|
|
5
|
+
aborted: string[];
|
|
6
|
+
labelExchanged: string[];
|
|
7
|
+
skipped: string[];
|
|
8
|
+
errors: string[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function emptyPollResult(): PollResult {
|
|
12
|
+
return {
|
|
13
|
+
created: [],
|
|
14
|
+
aborted: [],
|
|
15
|
+
labelExchanged: [],
|
|
16
|
+
skipped: [],
|
|
17
|
+
errors: [],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PollLoop<T extends PollResult = PollResult> {
|
|
22
|
+
poll(): Promise<T>;
|
|
23
|
+
start(): void;
|
|
24
|
+
stop(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a non-overlapping polling loop. `pollOnce` is the actual work;
|
|
29
|
+
* concurrent calls to `poll()` coalesce into the in-flight promise.
|
|
30
|
+
*/
|
|
31
|
+
export function createPollLoop<T extends PollResult>(opts: {
|
|
32
|
+
pollOnce: () => Promise<T>;
|
|
33
|
+
isEnabled: () => boolean;
|
|
34
|
+
pollInterval: () => string;
|
|
35
|
+
defaultIntervalMs?: number;
|
|
36
|
+
}): PollLoop<T> {
|
|
37
|
+
let timer: ReturnType<typeof setInterval> | null = null;
|
|
38
|
+
let pollInFlight: Promise<T> | null = null;
|
|
39
|
+
const defaultMs = opts.defaultIntervalMs ?? 60_000;
|
|
40
|
+
|
|
41
|
+
async function poll(): Promise<T> {
|
|
42
|
+
if (pollInFlight) {
|
|
43
|
+
return pollInFlight;
|
|
44
|
+
}
|
|
45
|
+
const running = (async () => {
|
|
46
|
+
try {
|
|
47
|
+
return await opts.pollOnce();
|
|
48
|
+
} finally {
|
|
49
|
+
pollInFlight = null;
|
|
50
|
+
}
|
|
51
|
+
})();
|
|
52
|
+
pollInFlight = running;
|
|
53
|
+
return running;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function pollAndIgnoreUnhandledRejection(): void {
|
|
57
|
+
void poll().catch(() => undefined);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
poll,
|
|
62
|
+
start() {
|
|
63
|
+
if (timer) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!opts.isEnabled()) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const ms = parseDurationMs(opts.pollInterval(), defaultMs);
|
|
70
|
+
timer = setInterval(pollAndIgnoreUnhandledRejection, ms);
|
|
71
|
+
pollAndIgnoreUnhandledRejection();
|
|
72
|
+
},
|
|
73
|
+
stop() {
|
|
74
|
+
if (!timer) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
clearInterval(timer);
|
|
78
|
+
timer = null;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { ProcessInstance } from "@leitwerk-dev/domain";
|
|
2
|
+
|
|
3
|
+
export interface ProcessEventRepoLike {
|
|
4
|
+
create(input: { instanceId: string; eventType: string; data?: Record<string, unknown> }): unknown;
|
|
5
|
+
listByInstance(instanceId: string, limit?: number): Array<{ eventType: string }>;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface WorkerSupervisorLike {
|
|
9
|
+
spawnWorker(instanceId: string): Promise<unknown>;
|
|
10
|
+
getWorker(instanceId: string): unknown | undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Check whether a process already has an event of the given type.
|
|
15
|
+
*/
|
|
16
|
+
export function hasProcessEvent(
|
|
17
|
+
events: ProcessEventRepoLike,
|
|
18
|
+
instanceId: string,
|
|
19
|
+
eventType: string,
|
|
20
|
+
): boolean {
|
|
21
|
+
return events.listByInstance(instanceId, 200).some((event) => event.eventType === eventType);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function processNeedsWorker(
|
|
25
|
+
process: Pick<ProcessInstance, "selectedTurnId" | "lifecycleStatus">,
|
|
26
|
+
): boolean {
|
|
27
|
+
return process.selectedTurnId !== null && process.lifecycleStatus === "active";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Spawn a worker for a process whose selected turn is runnable but currently has
|
|
32
|
+
* no running worker. No-ops when the process is not worker-runnable or already
|
|
33
|
+
* has a worker.
|
|
34
|
+
*/
|
|
35
|
+
export async function ensureWorkerForActiveAgent(
|
|
36
|
+
deps: { events: ProcessEventRepoLike; supervisor: WorkerSupervisorLike },
|
|
37
|
+
process: ProcessInstance,
|
|
38
|
+
): Promise<void> {
|
|
39
|
+
if (!processNeedsWorker(process) || deps.supervisor.getWorker(process.id)) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
await deps.supervisor.spawnWorker(process.id);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
deps.events.create({
|
|
46
|
+
instanceId: process.id,
|
|
47
|
+
eventType: "worker_spawn_failed",
|
|
48
|
+
data: {
|
|
49
|
+
reason: error instanceof Error ? error.message : "Failed to spawn worker",
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { readFile, unlink } from "node:fs/promises";
|
|
2
|
+
|
|
3
|
+
export type TriggerFileReadResult = { exists: false } | { exists: true; content: string };
|
|
4
|
+
|
|
5
|
+
export function errorCode(error: unknown): string | null {
|
|
6
|
+
if (typeof error !== "object" || error === null || !("code" in error)) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const code = (error as { code?: unknown }).code;
|
|
10
|
+
return typeof code === "string" ? code : null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function readTriggerFile(filePath: string): Promise<TriggerFileReadResult> {
|
|
14
|
+
try {
|
|
15
|
+
return { exists: true, content: await readFile(filePath, "utf8") };
|
|
16
|
+
} catch (error) {
|
|
17
|
+
if (errorCode(error) === "ENOENT") {
|
|
18
|
+
return { exists: false };
|
|
19
|
+
}
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function consumeTriggerFile(filePath: string): Promise<void> {
|
|
25
|
+
try {
|
|
26
|
+
await unlink(filePath);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (errorCode(error) === "ENOENT") {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|