@leitwerk-dev/watcher-utils 0.1.9 → 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-XF2QQLPO.js → chunk-BO52GPAV.js} +22 -11
- 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-ZW3LXDBX.js +46 -0
- 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 +6 -15
- package/dist/duration-parse.js.map +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +48 -5
- package/dist/index.js.map +1 -1
- package/dist/poll-loop.d.ts +30 -3
- package/dist/poll-loop.d.ts.map +1 -1
- package/dist/poll-loop.js +11 -58
- package/dist/poll-loop.js.map +1 -1
- package/dist/polling-coordinator.d.ts +38 -0
- package/dist/polling-coordinator.d.ts.map +1 -0
- package/dist/polling-coordinator.js +9 -0
- package/dist/polling-coordinator.js.map +1 -0
- package/dist/process-helpers.d.ts +15 -0
- package/dist/process-helpers.d.ts.map +1 -1
- package/dist/process-helpers.js +10 -32
- package/dist/process-helpers.js.map +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 +142 -114
- package/dist/watcher-coordinator.js.map +1 -1
- package/package.json +5 -5
- package/src/duration-parse.ts +5 -1
- package/src/index.ts +2 -30
- package/src/poll-loop.ts +50 -12
- package/src/polling-coordinator.ts +83 -0
- 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-IU3L35PC.js.map +0 -1
- package/dist/chunk-XF2QQLPO.js.map +0 -1
- package/dist/chunk-XLVMFO6W.js +0 -160
- package/dist/chunk-XLVMFO6W.js.map +0 -1
- package/dist/trigger-files.js +0 -31
- package/dist/trigger-files.js.map +0 -1
package/src/poll-loop.ts
CHANGED
|
@@ -1,13 +1,30 @@
|
|
|
1
1
|
import { parseDurationMs } from "./duration-parse.js";
|
|
2
2
|
|
|
3
|
+
/** Reserve the next poll time for a caller-owned key, before starting its work. @internal */
|
|
4
|
+
export function createPollSchedule(now: () => number = () => Date.now()) {
|
|
5
|
+
const dueAt = new Map<string, number>();
|
|
6
|
+
return (key: string, interval = "30s", timestamp = now()): boolean => {
|
|
7
|
+
if ((dueAt.get(key) ?? 0) > timestamp) return false;
|
|
8
|
+
dueAt.set(key, timestamp + parseDurationMs(interval, 30_000));
|
|
9
|
+
return true;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** @public */
|
|
3
14
|
export interface PollResult {
|
|
15
|
+
/** @public */
|
|
4
16
|
created: string[];
|
|
17
|
+
/** @internal */
|
|
5
18
|
aborted: string[];
|
|
19
|
+
/** @internal */
|
|
6
20
|
labelExchanged: string[];
|
|
21
|
+
/** @public */
|
|
7
22
|
skipped: string[];
|
|
23
|
+
/** @public */
|
|
8
24
|
errors: string[];
|
|
9
25
|
}
|
|
10
26
|
|
|
27
|
+
/** @public */
|
|
11
28
|
export function emptyPollResult(): PollResult {
|
|
12
29
|
return {
|
|
13
30
|
created: [],
|
|
@@ -18,21 +35,40 @@ export function emptyPollResult(): PollResult {
|
|
|
18
35
|
};
|
|
19
36
|
}
|
|
20
37
|
|
|
21
|
-
|
|
38
|
+
/** @internal */
|
|
39
|
+
export interface PollResultWithErrors {
|
|
40
|
+
/** @internal */
|
|
41
|
+
readonly errors: readonly string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @internal */
|
|
45
|
+
export interface PollLoop<T extends PollResultWithErrors = PollResult> {
|
|
46
|
+
/** @internal */
|
|
22
47
|
poll(): Promise<T>;
|
|
48
|
+
/** @internal */
|
|
23
49
|
start(): void;
|
|
24
|
-
|
|
50
|
+
/** @internal */
|
|
51
|
+
stop(): Promise<void>;
|
|
25
52
|
}
|
|
26
53
|
|
|
27
54
|
/**
|
|
28
55
|
* Create a non-overlapping polling loop. `pollOnce` is the actual work;
|
|
29
56
|
* concurrent calls to `poll()` coalesce into the in-flight promise.
|
|
30
57
|
*/
|
|
31
|
-
|
|
58
|
+
/** @internal */
|
|
59
|
+
export function createPollLoop<T extends PollResultWithErrors>(opts: {
|
|
60
|
+
/** @internal */
|
|
32
61
|
pollOnce: () => Promise<T>;
|
|
62
|
+
/** @internal */
|
|
33
63
|
isEnabled: () => boolean;
|
|
64
|
+
/** @internal */
|
|
34
65
|
pollInterval: () => string;
|
|
66
|
+
/** @internal */
|
|
35
67
|
defaultIntervalMs?: number;
|
|
68
|
+
/** @internal */
|
|
69
|
+
onScheduledResult: (result: T, durationMs: number) => void;
|
|
70
|
+
/** @internal */
|
|
71
|
+
onScheduledError: (error: unknown, durationMs: number) => void;
|
|
36
72
|
}): PollLoop<T> {
|
|
37
73
|
let timer: ReturnType<typeof setInterval> | null = null;
|
|
38
74
|
let pollInFlight: Promise<T> | null = null;
|
|
@@ -53,8 +89,12 @@ export function createPollLoop<T extends PollResult>(opts: {
|
|
|
53
89
|
return running;
|
|
54
90
|
}
|
|
55
91
|
|
|
56
|
-
function
|
|
57
|
-
|
|
92
|
+
function pollScheduled(): void {
|
|
93
|
+
const startedAt = Date.now();
|
|
94
|
+
void poll().then(
|
|
95
|
+
(result) => opts.onScheduledResult(result, Date.now() - startedAt),
|
|
96
|
+
(error: unknown) => opts.onScheduledError(error, Date.now() - startedAt),
|
|
97
|
+
);
|
|
58
98
|
}
|
|
59
99
|
|
|
60
100
|
return {
|
|
@@ -67,15 +107,13 @@ export function createPollLoop<T extends PollResult>(opts: {
|
|
|
67
107
|
return;
|
|
68
108
|
}
|
|
69
109
|
const ms = parseDurationMs(opts.pollInterval(), defaultMs);
|
|
70
|
-
timer = setInterval(
|
|
71
|
-
|
|
110
|
+
timer = setInterval(pollScheduled, ms);
|
|
111
|
+
pollScheduled();
|
|
72
112
|
},
|
|
73
|
-
stop() {
|
|
74
|
-
if (
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
clearInterval(timer);
|
|
113
|
+
async stop() {
|
|
114
|
+
if (timer) clearInterval(timer);
|
|
78
115
|
timer = null;
|
|
116
|
+
await pollInFlight;
|
|
79
117
|
},
|
|
80
118
|
};
|
|
81
119
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { createPollLoop, type PollResult, type PollResultWithErrors } from "./poll-loop.js";
|
|
2
|
+
|
|
3
|
+
/** @internal */
|
|
4
|
+
export interface PollingLogger {
|
|
5
|
+
/** @internal */
|
|
6
|
+
warn(payload: Record<string, unknown>, message?: string): void;
|
|
7
|
+
/** @internal */
|
|
8
|
+
error(payload: Record<string, unknown>, message?: string): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** @internal */
|
|
12
|
+
export interface PollingRegistration<T extends PollResultWithErrors> {
|
|
13
|
+
/** @internal */
|
|
14
|
+
id: string;
|
|
15
|
+
/** @internal */
|
|
16
|
+
pollOnce(): Promise<T>;
|
|
17
|
+
/** @internal */
|
|
18
|
+
isEnabled(): boolean;
|
|
19
|
+
/** @internal */
|
|
20
|
+
pollInterval(): string;
|
|
21
|
+
/** @internal */
|
|
22
|
+
defaultIntervalMs?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** @internal */
|
|
26
|
+
export interface RegisteredPoller<T extends PollResultWithErrors = PollResult> {
|
|
27
|
+
/** @internal */
|
|
28
|
+
poll(): Promise<T>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** @internal */
|
|
32
|
+
export interface PollingCoordinator {
|
|
33
|
+
/** @internal */
|
|
34
|
+
create<T extends PollResultWithErrors>(registration: PollingRegistration<T>): RegisteredPoller<T>;
|
|
35
|
+
/** @internal */
|
|
36
|
+
start(): void;
|
|
37
|
+
/** @internal */
|
|
38
|
+
stop(): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** @internal */
|
|
42
|
+
export function createPollingCoordinator(logger: PollingLogger): PollingCoordinator {
|
|
43
|
+
const loops = new Map<string, ReturnType<typeof createPollLoop>>();
|
|
44
|
+
let sealed = false;
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
create<T extends PollResultWithErrors>(
|
|
48
|
+
registration: PollingRegistration<T>,
|
|
49
|
+
): RegisteredPoller<T> {
|
|
50
|
+
const id = registration.id.trim();
|
|
51
|
+
if (!id) throw new Error("Polling registration id must not be empty");
|
|
52
|
+
if (sealed) throw new Error(`Cannot register poller '${id}' after polling has started`);
|
|
53
|
+
if (loops.has(id)) throw new Error(`Duplicate poller '${id}'`);
|
|
54
|
+
|
|
55
|
+
const loop = createPollLoop({
|
|
56
|
+
pollOnce: registration.pollOnce,
|
|
57
|
+
isEnabled: registration.isEnabled,
|
|
58
|
+
pollInterval: registration.pollInterval,
|
|
59
|
+
defaultIntervalMs: registration.defaultIntervalMs,
|
|
60
|
+
onScheduledResult(result, durationMs) {
|
|
61
|
+
if (result.errors.length === 0) return;
|
|
62
|
+
logger.warn({ pollerId: id, durationMs, result }, "Poll completed with errors");
|
|
63
|
+
},
|
|
64
|
+
onScheduledError(error, durationMs) {
|
|
65
|
+
logger.error({ pollerId: id, durationMs, err: error }, "Poll failed");
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
loops.set(id, loop);
|
|
69
|
+
return { poll: loop.poll };
|
|
70
|
+
},
|
|
71
|
+
start() {
|
|
72
|
+
sealed = true;
|
|
73
|
+
for (const loop of loops.values()) loop.start();
|
|
74
|
+
},
|
|
75
|
+
async stop() {
|
|
76
|
+
const results = await Promise.allSettled([...loops.values()].map((loop) => loop.stop()));
|
|
77
|
+
const errors = results
|
|
78
|
+
.filter((result) => result.status === "rejected")
|
|
79
|
+
.map((result) => result.reason);
|
|
80
|
+
if (errors.length) throw new AggregateError(errors, "Polling shutdown failed");
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
package/src/process-helpers.ts
CHANGED
|
@@ -1,18 +1,38 @@
|
|
|
1
1
|
import type { ProcessInstance } from "@leitwerk-dev/domain";
|
|
2
2
|
|
|
3
|
+
/** @internal */
|
|
3
4
|
export interface ProcessEventRepoLike {
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
/** @internal */
|
|
6
|
+
create(input: {
|
|
7
|
+
/** @internal */
|
|
8
|
+
instanceId: string;
|
|
9
|
+
/** @internal */
|
|
10
|
+
eventType: string;
|
|
11
|
+
/** @internal */
|
|
12
|
+
data?: Record<string, unknown>;
|
|
13
|
+
}): unknown;
|
|
14
|
+
/** @internal */
|
|
15
|
+
listByInstance(
|
|
16
|
+
instanceId: string,
|
|
17
|
+
limit?: number,
|
|
18
|
+
): Array<{
|
|
19
|
+
/** @internal */
|
|
20
|
+
eventType: string;
|
|
21
|
+
}>;
|
|
6
22
|
}
|
|
7
23
|
|
|
24
|
+
/** @internal */
|
|
8
25
|
export interface WorkerSupervisorLike {
|
|
26
|
+
/** @internal */
|
|
9
27
|
spawnWorker(instanceId: string): Promise<unknown>;
|
|
28
|
+
/** @internal */
|
|
10
29
|
getWorker(instanceId: string): unknown | undefined;
|
|
11
30
|
}
|
|
12
31
|
|
|
13
32
|
/**
|
|
14
33
|
* Check whether a process already has an event of the given type.
|
|
15
34
|
*/
|
|
35
|
+
/** @internal */
|
|
16
36
|
export function hasProcessEvent(
|
|
17
37
|
events: ProcessEventRepoLike,
|
|
18
38
|
instanceId: string,
|
|
@@ -21,6 +41,7 @@ export function hasProcessEvent(
|
|
|
21
41
|
return events.listByInstance(instanceId, 200).some((event) => event.eventType === eventType);
|
|
22
42
|
}
|
|
23
43
|
|
|
44
|
+
/** @internal */
|
|
24
45
|
export function processNeedsWorker(
|
|
25
46
|
process: Pick<ProcessInstance, "selectedTurnId" | "lifecycleStatus">,
|
|
26
47
|
): boolean {
|
|
@@ -32,8 +53,14 @@ export function processNeedsWorker(
|
|
|
32
53
|
* no running worker. No-ops when the process is not worker-runnable or already
|
|
33
54
|
* has a worker.
|
|
34
55
|
*/
|
|
56
|
+
/** @internal */
|
|
35
57
|
export async function ensureWorkerForActiveAgent(
|
|
36
|
-
deps: {
|
|
58
|
+
deps: {
|
|
59
|
+
/** @internal */
|
|
60
|
+
events: ProcessEventRepoLike;
|
|
61
|
+
/** @internal */
|
|
62
|
+
supervisor: WorkerSupervisorLike;
|
|
63
|
+
},
|
|
37
64
|
process: ProcessInstance,
|
|
38
65
|
): Promise<void> {
|
|
39
66
|
if (!processNeedsWorker(process) || deps.supervisor.getWorker(process.id)) {
|
package/src/trigger-files.ts
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import { readFile, unlink } from "node:fs/promises";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/** @internal */
|
|
4
|
+
export type TriggerFileReadResult =
|
|
5
|
+
| {
|
|
6
|
+
/** @internal */
|
|
7
|
+
exists: false;
|
|
8
|
+
}
|
|
9
|
+
| {
|
|
10
|
+
/** @internal */
|
|
11
|
+
exists: true;
|
|
12
|
+
/** @internal */
|
|
13
|
+
content: string;
|
|
14
|
+
};
|
|
4
15
|
|
|
16
|
+
/** @internal */
|
|
5
17
|
export function errorCode(error: unknown): string | null {
|
|
6
18
|
if (typeof error !== "object" || error === null || !("code" in error)) {
|
|
7
19
|
return null;
|
|
@@ -10,6 +22,7 @@ export function errorCode(error: unknown): string | null {
|
|
|
10
22
|
return typeof code === "string" ? code : null;
|
|
11
23
|
}
|
|
12
24
|
|
|
25
|
+
/** @internal */
|
|
13
26
|
export async function readTriggerFile(filePath: string): Promise<TriggerFileReadResult> {
|
|
14
27
|
try {
|
|
15
28
|
return { exists: true, content: await readFile(filePath, "utf8") };
|
|
@@ -21,6 +34,7 @@ export async function readTriggerFile(filePath: string): Promise<TriggerFileRead
|
|
|
21
34
|
}
|
|
22
35
|
}
|
|
23
36
|
|
|
37
|
+
/** @internal */
|
|
24
38
|
export async function consumeTriggerFile(filePath: string): Promise<void> {
|
|
25
39
|
try {
|
|
26
40
|
await unlink(filePath);
|
|
@@ -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/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/poll-loop.ts"],"sourcesContent":["import { parseDurationMs } from \"./duration-parse.js\";\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 PollLoop<T extends PollResult = 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 PollResult>(opts: {\n\tpollOnce: () => Promise<T>;\n\tisEnabled: () => boolean;\n\tpollInterval: () => string;\n\tdefaultIntervalMs?: number;\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 pollAndIgnoreUnhandledRejection(): void {\n\t\tvoid poll().catch(() => undefined);\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(pollAndIgnoreUnhandledRejection, ms);\n\t\t\tpollAndIgnoreUnhandledRejection();\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":";;;;;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;AAYO,SAAS,eAAqC,MAKrC;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,kCAAwC;AAChD,SAAK,KAAK,EAAE,MAAM,MAAM,MAAS;AAAA,EAClC;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,iCAAiC,EAAE;AACvD,sCAAgC;AAAA,IACjC;AAAA,IACA,OAAO;AACN,UAAI,CAAC,OAAO;AACX;AAAA,MACD;AACA,oBAAc,KAAK;AACnB,cAAQ;AAAA,IACT;AAAA,EACD;AACD;","names":[]}
|