@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.
Files changed (49) hide show
  1. package/dist/{chunk-XF2QQLPO.js → chunk-BO52GPAV.js} +22 -11
  2. package/dist/chunk-BO52GPAV.js.map +1 -0
  3. package/dist/{chunk-IU3L35PC.js → chunk-D57H46H7.js} +1 -1
  4. package/dist/chunk-D57H46H7.js.map +1 -0
  5. package/dist/{chunk-3KGAUQIG.js → chunk-EWA6E2ZM.js} +1 -1
  6. package/dist/chunk-EWA6E2ZM.js.map +1 -0
  7. package/dist/chunk-ZW3LXDBX.js +46 -0
  8. package/dist/chunk-ZW3LXDBX.js.map +1 -0
  9. package/dist/duration-parse.d.ts +2 -0
  10. package/dist/duration-parse.d.ts.map +1 -1
  11. package/dist/duration-parse.js +6 -15
  12. package/dist/duration-parse.js.map +1 -1
  13. package/dist/index.d.ts +3 -4
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +48 -5
  16. package/dist/index.js.map +1 -1
  17. package/dist/poll-loop.d.ts +30 -3
  18. package/dist/poll-loop.d.ts.map +1 -1
  19. package/dist/poll-loop.js +11 -58
  20. package/dist/poll-loop.js.map +1 -1
  21. package/dist/polling-coordinator.d.ts +38 -0
  22. package/dist/polling-coordinator.d.ts.map +1 -0
  23. package/dist/polling-coordinator.js +9 -0
  24. package/dist/polling-coordinator.js.map +1 -0
  25. package/dist/process-helpers.d.ts +15 -0
  26. package/dist/process-helpers.d.ts.map +1 -1
  27. package/dist/process-helpers.js +10 -32
  28. package/dist/process-helpers.js.map +1 -1
  29. package/dist/trigger-files.d.ts +7 -0
  30. package/dist/trigger-files.d.ts.map +1 -1
  31. package/dist/watcher-coordinator.d.ts +77 -2
  32. package/dist/watcher-coordinator.d.ts.map +1 -1
  33. package/dist/watcher-coordinator.js +142 -114
  34. package/dist/watcher-coordinator.js.map +1 -1
  35. package/package.json +5 -5
  36. package/src/duration-parse.ts +5 -1
  37. package/src/index.ts +2 -30
  38. package/src/poll-loop.ts +50 -12
  39. package/src/polling-coordinator.ts +83 -0
  40. package/src/process-helpers.ts +30 -3
  41. package/src/trigger-files.ts +15 -1
  42. package/src/watcher-coordinator.ts +99 -11
  43. package/dist/chunk-3KGAUQIG.js.map +0 -1
  44. package/dist/chunk-IU3L35PC.js.map +0 -1
  45. package/dist/chunk-XF2QQLPO.js.map +0 -1
  46. package/dist/chunk-XLVMFO6W.js +0 -160
  47. package/dist/chunk-XLVMFO6W.js.map +0 -1
  48. package/dist/trigger-files.js +0 -31
  49. package/dist/trigger-files.js.map +0 -1
@@ -1,8 +1,16 @@
1
1
  import {
2
2
  parseDurationMs
3
- } from "./chunk-IU3L35PC.js";
3
+ } from "./chunk-D57H46H7.js";
4
4
 
5
5
  // src/poll-loop.ts
6
+ function createPollSchedule(now = () => Date.now()) {
7
+ const dueAt = /* @__PURE__ */ new Map();
8
+ return (key, interval = "30s", timestamp = now()) => {
9
+ if ((dueAt.get(key) ?? 0) > timestamp) return false;
10
+ dueAt.set(key, timestamp + parseDurationMs(interval, 3e4));
11
+ return true;
12
+ };
13
+ }
6
14
  function emptyPollResult() {
7
15
  return {
8
16
  created: [],
@@ -30,8 +38,12 @@ function createPollLoop(opts) {
30
38
  pollInFlight = running;
31
39
  return running;
32
40
  }
33
- function pollAndIgnoreUnhandledRejection() {
34
- void poll().catch(() => void 0);
41
+ function pollScheduled() {
42
+ const startedAt = Date.now();
43
+ void poll().then(
44
+ (result) => opts.onScheduledResult(result, Date.now() - startedAt),
45
+ (error) => opts.onScheduledError(error, Date.now() - startedAt)
46
+ );
35
47
  }
36
48
  return {
37
49
  poll,
@@ -43,21 +55,20 @@ function createPollLoop(opts) {
43
55
  return;
44
56
  }
45
57
  const ms = parseDurationMs(opts.pollInterval(), defaultMs);
46
- timer = setInterval(pollAndIgnoreUnhandledRejection, ms);
47
- pollAndIgnoreUnhandledRejection();
58
+ timer = setInterval(pollScheduled, ms);
59
+ pollScheduled();
48
60
  },
49
- stop() {
50
- if (!timer) {
51
- return;
52
- }
53
- clearInterval(timer);
61
+ async stop() {
62
+ if (timer) clearInterval(timer);
54
63
  timer = null;
64
+ await pollInFlight;
55
65
  }
56
66
  };
57
67
  }
58
68
 
59
69
  export {
70
+ createPollSchedule,
60
71
  emptyPollResult,
61
72
  createPollLoop
62
73
  };
63
- //# sourceMappingURL=chunk-XF2QQLPO.js.map
74
+ //# sourceMappingURL=chunk-BO52GPAV.js.map
@@ -0,0 +1 @@
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. @internal */\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\n/** @public */\nexport interface PollResult {\n\t/** @public */\n\tcreated: string[];\n\t/** @internal */\n\taborted: string[];\n\t/** @internal */\n\tlabelExchanged: string[];\n\t/** @public */\n\tskipped: string[];\n\t/** @public */\n\terrors: string[];\n}\n\n/** @public */\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\n/** @internal */\nexport interface PollResultWithErrors {\n\t/** @internal */\n\treadonly errors: readonly string[];\n}\n\n/** @internal */\nexport interface PollLoop<T extends PollResultWithErrors = PollResult> {\n\t/** @internal */\n\tpoll(): Promise<T>;\n\t/** @internal */\n\tstart(): void;\n\t/** @internal */\n\tstop(): Promise<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 */\n/** @internal */\nexport function createPollLoop<T extends PollResultWithErrors>(opts: {\n\t/** @internal */\n\tpollOnce: () => Promise<T>;\n\t/** @internal */\n\tisEnabled: () => boolean;\n\t/** @internal */\n\tpollInterval: () => string;\n\t/** @internal */\n\tdefaultIntervalMs?: number;\n\t/** @internal */\n\tonScheduledResult: (result: T, durationMs: number) => void;\n\t/** @internal */\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\tasync stop() {\n\t\t\tif (timer) clearInterval(timer);\n\t\t\ttimer = null;\n\t\t\tawait pollInFlight;\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,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+11,4 @@ function parseDurationMs(value, defaultMs, _options = {}) {
11
11
  export {
12
12
  parseDurationMs
13
13
  };
14
- //# sourceMappingURL=chunk-IU3L35PC.js.map
14
+ //# sourceMappingURL=chunk-D57H46H7.js.map
@@ -0,0 +1 @@
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 */\n/** @public */\nexport function parseDurationMs(\n\tvalue: string,\n\tdefaultMs: number,\n\t_options: {\n\t\t/** @public */\n\t\tallowHours?: boolean;\n\t} = {},\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;AAUR,SAAS,gBACf,OACA,WACA,WAGI,CAAC,GACI;AACT,QAAM,SAAS,GAAG,MAAM,KAAK,CAAmB;AAChD,MAAI,CAAC,OAAO,SAAS,MAAM,KAAK,SAAS,GAAG;AAC3C,WAAO;AAAA,EACR;AAEA,SAAO;AACR;","names":[]}
@@ -28,4 +28,4 @@ export {
28
28
  processNeedsWorker,
29
29
  ensureWorkerForActiveAgent
30
30
  };
31
- //# sourceMappingURL=chunk-3KGAUQIG.js.map
31
+ //# sourceMappingURL=chunk-EWA6E2ZM.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/process-helpers.ts"],"sourcesContent":["import type { ProcessInstance } from \"@leitwerk-dev/domain\";\n\n/** @internal */\nexport interface ProcessEventRepoLike {\n\t/** @internal */\n\tcreate(input: {\n\t\t/** @internal */\n\t\tinstanceId: string;\n\t\t/** @internal */\n\t\teventType: string;\n\t\t/** @internal */\n\t\tdata?: Record<string, unknown>;\n\t}): unknown;\n\t/** @internal */\n\tlistByInstance(\n\t\tinstanceId: string,\n\t\tlimit?: number,\n\t): Array<{\n\t\t/** @internal */\n\t\teventType: string;\n\t}>;\n}\n\n/** @internal */\nexport interface WorkerSupervisorLike {\n\t/** @internal */\n\tspawnWorker(instanceId: string): Promise<unknown>;\n\t/** @internal */\n\tgetWorker(instanceId: string): unknown | undefined;\n}\n\n/**\n * Check whether a process already has an event of the given type.\n */\n/** @internal */\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\n/** @internal */\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 */\n/** @internal */\nexport async function ensureWorkerForActiveAgent(\n\tdeps: {\n\t\t/** @internal */\n\t\tevents: ProcessEventRepoLike;\n\t\t/** @internal */\n\t\tsupervisor: WorkerSupervisorLike;\n\t},\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":";AAmCO,SAAS,gBACf,QACA,YACA,WACU;AACV,SAAO,OAAO,eAAe,YAAY,GAAG,EAAE,KAAK,CAAC,UAAU,MAAM,cAAc,SAAS;AAC5F;AAGO,SAAS,mBACf,SACU;AACV,SAAO,QAAQ,mBAAmB,QAAQ,QAAQ,oBAAoB;AACvE;AAQA,eAAsB,2BACrB,MAMA,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":[]}
@@ -0,0 +1,46 @@
1
+ import {
2
+ createPollLoop
3
+ } from "./chunk-BO52GPAV.js";
4
+
5
+ // src/polling-coordinator.ts
6
+ function createPollingCoordinator(logger) {
7
+ const loops = /* @__PURE__ */ new Map();
8
+ let sealed = false;
9
+ return {
10
+ create(registration) {
11
+ const id = registration.id.trim();
12
+ if (!id) throw new Error("Polling registration id must not be empty");
13
+ if (sealed) throw new Error(`Cannot register poller '${id}' after polling has started`);
14
+ if (loops.has(id)) throw new Error(`Duplicate poller '${id}'`);
15
+ const loop = createPollLoop({
16
+ pollOnce: registration.pollOnce,
17
+ isEnabled: registration.isEnabled,
18
+ pollInterval: registration.pollInterval,
19
+ defaultIntervalMs: registration.defaultIntervalMs,
20
+ onScheduledResult(result, durationMs) {
21
+ if (result.errors.length === 0) return;
22
+ logger.warn({ pollerId: id, durationMs, result }, "Poll completed with errors");
23
+ },
24
+ onScheduledError(error, durationMs) {
25
+ logger.error({ pollerId: id, durationMs, err: error }, "Poll failed");
26
+ }
27
+ });
28
+ loops.set(id, loop);
29
+ return { poll: loop.poll };
30
+ },
31
+ start() {
32
+ sealed = true;
33
+ for (const loop of loops.values()) loop.start();
34
+ },
35
+ async stop() {
36
+ const results = await Promise.allSettled([...loops.values()].map((loop) => loop.stop()));
37
+ const errors = results.filter((result) => result.status === "rejected").map((result) => result.reason);
38
+ if (errors.length) throw new AggregateError(errors, "Polling shutdown failed");
39
+ }
40
+ };
41
+ }
42
+
43
+ export {
44
+ createPollingCoordinator
45
+ };
46
+ //# sourceMappingURL=chunk-ZW3LXDBX.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/polling-coordinator.ts"],"sourcesContent":["import { createPollLoop, type PollResult, type PollResultWithErrors } from \"./poll-loop.js\";\n\n/** @internal */\nexport interface PollingLogger {\n\t/** @internal */\n\twarn(payload: Record<string, unknown>, message?: string): void;\n\t/** @internal */\n\terror(payload: Record<string, unknown>, message?: string): void;\n}\n\n/** @internal */\nexport interface PollingRegistration<T extends PollResultWithErrors> {\n\t/** @internal */\n\tid: string;\n\t/** @internal */\n\tpollOnce(): Promise<T>;\n\t/** @internal */\n\tisEnabled(): boolean;\n\t/** @internal */\n\tpollInterval(): string;\n\t/** @internal */\n\tdefaultIntervalMs?: number;\n}\n\n/** @internal */\nexport interface RegisteredPoller<T extends PollResultWithErrors = PollResult> {\n\t/** @internal */\n\tpoll(): Promise<T>;\n}\n\n/** @internal */\nexport interface PollingCoordinator {\n\t/** @internal */\n\tcreate<T extends PollResultWithErrors>(registration: PollingRegistration<T>): RegisteredPoller<T>;\n\t/** @internal */\n\tstart(): void;\n\t/** @internal */\n\tstop(): Promise<void>;\n}\n\n/** @internal */\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\tasync stop() {\n\t\t\tconst results = await Promise.allSettled([...loops.values()].map((loop) => loop.stop()));\n\t\t\tconst errors = results\n\t\t\t\t.filter((result) => result.status === \"rejected\")\n\t\t\t\t.map((result) => result.reason);\n\t\t\tif (errors.length) throw new AggregateError(errors, \"Polling shutdown failed\");\n\t\t},\n\t};\n}\n"],"mappings":";;;;;AAyCO,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,MAAM,OAAO;AACZ,YAAM,UAAU,MAAM,QAAQ,WAAW,CAAC,GAAG,MAAM,OAAO,CAAC,EAAE,IAAI,CAAC,SAAS,KAAK,KAAK,CAAC,CAAC;AACvF,YAAM,SAAS,QACb,OAAO,CAAC,WAAW,OAAO,WAAW,UAAU,EAC/C,IAAI,CAAC,WAAW,OAAO,MAAM;AAC/B,UAAI,OAAO,OAAQ,OAAM,IAAI,eAAe,QAAQ,yBAAyB;AAAA,IAC9E;AAAA,EACD;AACD;","names":[]}
@@ -5,7 +5,9 @@
5
5
  * `options.allowHours` is retained for existing callers; the underlying `ms`
6
6
  * parser supports hours and other common duration units by default.
7
7
  */
8
+ /** @public */
8
9
  export declare function parseDurationMs(value: string, defaultMs: number, _options?: {
10
+ /** @public */
9
11
  allowHours?: boolean;
10
12
  }): number;
11
13
  //# sourceMappingURL=duration-parse.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"duration-parse.d.ts","sourceRoot":"","sources":["../src/duration-parse.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC9B,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,QAAQ,GAAE;IAAE,UAAU,CAAC,EAAE,OAAO,CAAA;CAAO,GACrC,MAAM,CAOR"}
1
+ {"version":3,"file":"duration-parse.d.ts","sourceRoot":"","sources":["../src/duration-parse.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,cAAc;AACd,wBAAgB,eAAe,CAC9B,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,QAAQ,GAAE;IACT,cAAc;IACd,UAAU,CAAC,EAAE,OAAO,CAAC;CAChB,GACJ,MAAM,CAOR"}
@@ -1,16 +1,7 @@
1
- import ms from "ms";
2
- /**
3
- * Parse a human-friendly duration string (e.g. "150ms", "1.5s", "2 min") into
4
- * milliseconds. Returns `defaultMs` when the input cannot be parsed.
5
- *
6
- * `options.allowHours` is retained for existing callers; the underlying `ms`
7
- * parser supports hours and other common duration units by default.
8
- */
9
- export function parseDurationMs(value, defaultMs, _options = {}) {
10
- const parsed = ms(value.trim());
11
- if (!Number.isFinite(parsed) || parsed < 0) {
12
- return defaultMs;
13
- }
14
- return parsed;
15
- }
1
+ import {
2
+ parseDurationMs
3
+ } from "./chunk-D57H46H7.js";
4
+ export {
5
+ parseDurationMs
6
+ };
16
7
  //# sourceMappingURL=duration-parse.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"duration-parse.js","sourceRoot":"","sources":["../src/duration-parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AAEpB;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAC9B,KAAa,EACb,SAAiB,EACjB,WAAqC,EAAE;IAEvC,MAAM,MAAM,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,EAAoB,CAAC,CAAC;IAClD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5C,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export { parseDurationMs } from "./duration-parse.js";
2
- export { createPollLoop, emptyPollResult, type PollLoop, type PollResult, } from "./poll-loop.js";
3
- export { ensureWorkerForActiveAgent, hasProcessEvent, type ProcessEventRepoLike, processNeedsWorker, type WorkerSupervisorLike, } from "./process-helpers.js";
4
- export { consumeTriggerFile, errorCode, readTriggerFile, type TriggerFileReadResult, } from "./trigger-files.js";
5
- export { type AgentRepoAccessorLike, type BroadcasterLike, type CompletionReconciliationOptions, type CompletionReconciliationResult, type ContinueWatcherAgentStartupOptions, continueWatcherAgentStartup, type DiscoveryPassOptions, deferWatcherLaunchStart, launchFailureMessage, requireWatcherStartTurnId, runWatcherAbortReconciliation, runWatcherCompletionReconciliation, runWatcherDiscoveryPass, shouldEnsureWorkerForExistingActiveAgent, type WatcherCommandResult, type WatcherCommandServiceLike, type WatcherStartupDeps, } from "./watcher-coordinator.js";
2
+ export { createPollSchedule, emptyPollResult, type PollResult, } from "./poll-loop.js";
3
+ export { createPollingCoordinator } from "./polling-coordinator.js";
4
+ export { consumeTriggerFile, readTriggerFile, } from "./trigger-files.js";
6
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EACN,cAAc,EACd,eAAe,EACf,KAAK,QAAQ,EACb,KAAK,UAAU,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,0BAA0B,EAC1B,eAAe,EACf,KAAK,oBAAoB,EACzB,kBAAkB,EAClB,KAAK,oBAAoB,GACzB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,kBAAkB,EAClB,SAAS,EACT,eAAe,EACf,KAAK,qBAAqB,GAC1B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACN,KAAK,qBAAqB,EAC1B,KAAK,eAAe,EACpB,KAAK,+BAA+B,EACpC,KAAK,8BAA8B,EACnC,KAAK,kCAAkC,EACvC,2BAA2B,EAC3B,KAAK,oBAAoB,EACzB,uBAAuB,EACvB,oBAAoB,EACpB,yBAAyB,EACzB,6BAA6B,EAC7B,kCAAkC,EAClC,uBAAuB,EACvB,wCAAwC,EACxC,KAAK,oBAAoB,EACzB,KAAK,yBAAyB,EAC9B,KAAK,kBAAkB,GACvB,MAAM,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EACN,kBAAkB,EAClB,eAAe,EACf,KAAK,UAAU,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EACN,kBAAkB,EAClB,eAAe,GACf,MAAM,oBAAoB,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,49 @@
1
- export { parseDurationMs } from "./duration-parse.js";
2
- export { createPollLoop, emptyPollResult, } from "./poll-loop.js";
3
- export { ensureWorkerForActiveAgent, hasProcessEvent, processNeedsWorker, } from "./process-helpers.js";
4
- export { consumeTriggerFile, errorCode, readTriggerFile, } from "./trigger-files.js";
5
- export { continueWatcherAgentStartup, deferWatcherLaunchStart, launchFailureMessage, requireWatcherStartTurnId, runWatcherAbortReconciliation, runWatcherCompletionReconciliation, runWatcherDiscoveryPass, shouldEnsureWorkerForExistingActiveAgent, } from "./watcher-coordinator.js";
1
+ import {
2
+ createPollingCoordinator
3
+ } from "./chunk-ZW3LXDBX.js";
4
+ import {
5
+ createPollSchedule,
6
+ emptyPollResult
7
+ } from "./chunk-BO52GPAV.js";
8
+ import {
9
+ parseDurationMs
10
+ } from "./chunk-D57H46H7.js";
11
+
12
+ // src/trigger-files.ts
13
+ import { readFile, unlink } from "fs/promises";
14
+ function errorCode(error) {
15
+ if (typeof error !== "object" || error === null || !("code" in error)) {
16
+ return null;
17
+ }
18
+ const code = error.code;
19
+ return typeof code === "string" ? code : null;
20
+ }
21
+ async function readTriggerFile(filePath) {
22
+ try {
23
+ return { exists: true, content: await readFile(filePath, "utf8") };
24
+ } catch (error) {
25
+ if (errorCode(error) === "ENOENT") {
26
+ return { exists: false };
27
+ }
28
+ throw error;
29
+ }
30
+ }
31
+ async function consumeTriggerFile(filePath) {
32
+ try {
33
+ await unlink(filePath);
34
+ } catch (error) {
35
+ if (errorCode(error) === "ENOENT") {
36
+ return;
37
+ }
38
+ throw error;
39
+ }
40
+ }
41
+ export {
42
+ consumeTriggerFile,
43
+ createPollSchedule,
44
+ createPollingCoordinator,
45
+ emptyPollResult,
46
+ parseDurationMs,
47
+ readTriggerFile
48
+ };
6
49
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EACN,cAAc,EACd,eAAe,GAGf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,0BAA0B,EAC1B,eAAe,EAEf,kBAAkB,GAElB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,kBAAkB,EAClB,SAAS,EACT,eAAe,GAEf,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAMN,2BAA2B,EAE3B,uBAAuB,EACvB,oBAAoB,EACpB,yBAAyB,EACzB,6BAA6B,EAC7B,kCAAkC,EAClC,uBAAuB,EACvB,wCAAwC,GAIxC,MAAM,0BAA0B,CAAC"}
1
+ {"version":3,"sources":["../src/trigger-files.ts"],"sourcesContent":["import { readFile, unlink } from \"node:fs/promises\";\n\n/** @internal */\nexport type TriggerFileReadResult =\n\t| {\n\t\t\t/** @internal */\n\t\t\texists: false;\n\t }\n\t| {\n\t\t\t/** @internal */\n\t\t\texists: true;\n\t\t\t/** @internal */\n\t\t\tcontent: string;\n\t };\n\n/** @internal */\nexport function errorCode(error: unknown): string | null {\n\tif (typeof error !== \"object\" || error === null || !(\"code\" in error)) {\n\t\treturn null;\n\t}\n\tconst code = (error as { code?: unknown }).code;\n\treturn typeof code === \"string\" ? code : null;\n}\n\n/** @internal */\nexport async function readTriggerFile(filePath: string): Promise<TriggerFileReadResult> {\n\ttry {\n\t\treturn { exists: true, content: await readFile(filePath, \"utf8\") };\n\t} catch (error) {\n\t\tif (errorCode(error) === \"ENOENT\") {\n\t\t\treturn { exists: false };\n\t\t}\n\t\tthrow error;\n\t}\n}\n\n/** @internal */\nexport async function consumeTriggerFile(filePath: string): Promise<void> {\n\ttry {\n\t\tawait unlink(filePath);\n\t} catch (error) {\n\t\tif (errorCode(error) === \"ENOENT\") {\n\t\t\treturn;\n\t\t}\n\t\tthrow error;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;AAAA,SAAS,UAAU,cAAc;AAgB1B,SAAS,UAAU,OAA+B;AACxD,MAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,EAAE,UAAU,QAAQ;AACtE,WAAO;AAAA,EACR;AACA,QAAM,OAAQ,MAA6B;AAC3C,SAAO,OAAO,SAAS,WAAW,OAAO;AAC1C;AAGA,eAAsB,gBAAgB,UAAkD;AACvF,MAAI;AACH,WAAO,EAAE,QAAQ,MAAM,SAAS,MAAM,SAAS,UAAU,MAAM,EAAE;AAAA,EAClE,SAAS,OAAO;AACf,QAAI,UAAU,KAAK,MAAM,UAAU;AAClC,aAAO,EAAE,QAAQ,MAAM;AAAA,IACxB;AACA,UAAM;AAAA,EACP;AACD;AAGA,eAAsB,mBAAmB,UAAiC;AACzE,MAAI;AACH,UAAM,OAAO,QAAQ;AAAA,EACtB,SAAS,OAAO;AACf,QAAI,UAAU,KAAK,MAAM,UAAU;AAClC;AAAA,IACD;AACA,UAAM;AAAA,EACP;AACD;","names":[]}
@@ -1,24 +1,51 @@
1
+ /** Reserve the next poll time for a caller-owned key, before starting its work. @internal */
2
+ export declare function createPollSchedule(now?: () => number): (key: string, interval?: string, timestamp?: number) => boolean;
3
+ /** @public */
1
4
  export interface PollResult {
5
+ /** @public */
2
6
  created: string[];
7
+ /** @internal */
3
8
  aborted: string[];
9
+ /** @internal */
4
10
  labelExchanged: string[];
11
+ /** @public */
5
12
  skipped: string[];
13
+ /** @public */
6
14
  errors: string[];
7
15
  }
16
+ /** @public */
8
17
  export declare function emptyPollResult(): PollResult;
9
- export interface PollLoop<T extends PollResult = PollResult> {
18
+ /** @internal */
19
+ export interface PollResultWithErrors {
20
+ /** @internal */
21
+ readonly errors: readonly string[];
22
+ }
23
+ /** @internal */
24
+ export interface PollLoop<T extends PollResultWithErrors = PollResult> {
25
+ /** @internal */
10
26
  poll(): Promise<T>;
27
+ /** @internal */
11
28
  start(): void;
12
- stop(): void;
29
+ /** @internal */
30
+ stop(): Promise<void>;
13
31
  }
14
32
  /**
15
33
  * Create a non-overlapping polling loop. `pollOnce` is the actual work;
16
34
  * concurrent calls to `poll()` coalesce into the in-flight promise.
17
35
  */
18
- export declare function createPollLoop<T extends PollResult>(opts: {
36
+ /** @internal */
37
+ export declare function createPollLoop<T extends PollResultWithErrors>(opts: {
38
+ /** @internal */
19
39
  pollOnce: () => Promise<T>;
40
+ /** @internal */
20
41
  isEnabled: () => boolean;
42
+ /** @internal */
21
43
  pollInterval: () => string;
44
+ /** @internal */
22
45
  defaultIntervalMs?: number;
46
+ /** @internal */
47
+ onScheduledResult: (result: T, durationMs: number) => void;
48
+ /** @internal */
49
+ onScheduledError: (error: unknown, durationMs: number) => void;
23
50
  }): PollLoop<T>;
24
51
  //# sourceMappingURL=poll-loop.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"poll-loop.d.ts","sourceRoot":"","sources":["../src/poll-loop.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,UAAU;IAC1B,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,OAAO,EAAE,MAAM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gB;AAChB,MAAM,WAAW,QAAQ,CAAC,CAAC,SAAS,oBAAoB,GAAG,UAAU;IACpE,gBAAgB;IAChB,IAAI,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC;IACnB,gBAAgB;IAChB,KAAK,IAAI,IAAI,CAAC;IACd,gBAAgB;IAChB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACtB;AAED;;;GAGG;AACH,gBAAgB;AAChB,wBAAgB,cAAc,CAAC,CAAC,SAAS,oBAAoB,EAAE,IAAI,EAAE;IACpE,gBAAgB;IAChB,QAAQ,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;IAC3B,gBAAgB;IAChB,SAAS,EAAE,MAAM,OAAO,CAAC;IACzB,gBAAgB;IAChB,YAAY,EAAE,MAAM,MAAM,CAAC;IAC3B,gBAAgB;IAChB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,gBAAgB;IAChB,iBAAiB,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,UAAU,EAAE,MAAM,KAAK,IAAI,CAAC;IAC3D,gBAAgB;IAChB,gBAAgB,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,KAAK,IAAI,CAAC;CAC/D,GAAG,QAAQ,CAAC,CAAC,CAAC,CA+Cd"}
package/dist/poll-loop.js CHANGED
@@ -1,59 +1,12 @@
1
- import { parseDurationMs } from "./duration-parse.js";
2
- export function emptyPollResult() {
3
- return {
4
- created: [],
5
- aborted: [],
6
- labelExchanged: [],
7
- skipped: [],
8
- errors: [],
9
- };
10
- }
11
- /**
12
- * Create a non-overlapping polling loop. `pollOnce` is the actual work;
13
- * concurrent calls to `poll()` coalesce into the in-flight promise.
14
- */
15
- export function createPollLoop(opts) {
16
- let timer = null;
17
- let pollInFlight = null;
18
- const defaultMs = opts.defaultIntervalMs ?? 60_000;
19
- async function poll() {
20
- if (pollInFlight) {
21
- return pollInFlight;
22
- }
23
- const running = (async () => {
24
- try {
25
- return await opts.pollOnce();
26
- }
27
- finally {
28
- pollInFlight = null;
29
- }
30
- })();
31
- pollInFlight = running;
32
- return running;
33
- }
34
- function pollAndIgnoreUnhandledRejection() {
35
- void poll().catch(() => undefined);
36
- }
37
- return {
38
- poll,
39
- start() {
40
- if (timer) {
41
- return;
42
- }
43
- if (!opts.isEnabled()) {
44
- return;
45
- }
46
- const ms = parseDurationMs(opts.pollInterval(), defaultMs);
47
- timer = setInterval(pollAndIgnoreUnhandledRejection, ms);
48
- pollAndIgnoreUnhandledRejection();
49
- },
50
- stop() {
51
- if (!timer) {
52
- return;
53
- }
54
- clearInterval(timer);
55
- timer = null;
56
- },
57
- };
58
- }
1
+ import {
2
+ createPollLoop,
3
+ createPollSchedule,
4
+ emptyPollResult
5
+ } from "./chunk-BO52GPAV.js";
6
+ import "./chunk-D57H46H7.js";
7
+ export {
8
+ createPollLoop,
9
+ createPollSchedule,
10
+ emptyPollResult
11
+ };
59
12
  //# sourceMappingURL=poll-loop.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"poll-loop.js","sourceRoot":"","sources":["../src/poll-loop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAUtD,MAAM,UAAU,eAAe;IAC9B,OAAO;QACN,OAAO,EAAE,EAAE;QACX,OAAO,EAAE,EAAE;QACX,cAAc,EAAE,EAAE;QAClB,OAAO,EAAE,EAAE;QACX,MAAM,EAAE,EAAE;KACV,CAAC;AACH,CAAC;AAQD;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAuB,IAKpD;IACA,IAAI,KAAK,GAA0C,IAAI,CAAC;IACxD,IAAI,YAAY,GAAsB,IAAI,CAAC;IAC3C,MAAM,SAAS,GAAG,IAAI,CAAC,iBAAiB,IAAI,MAAM,CAAC;IAEnD,KAAK,UAAU,IAAI;QAClB,IAAI,YAAY,EAAE,CAAC;YAClB,OAAO,YAAY,CAAC;QACrB,CAAC;QACD,MAAM,OAAO,GAAG,CAAC,KAAK,IAAI,EAAE;YAC3B,IAAI,CAAC;gBACJ,OAAO,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC9B,CAAC;oBAAS,CAAC;gBACV,YAAY,GAAG,IAAI,CAAC;YACrB,CAAC;QACF,CAAC,CAAC,EAAE,CAAC;QACL,YAAY,GAAG,OAAO,CAAC;QACvB,OAAO,OAAO,CAAC;IAChB,CAAC;IAED,SAAS,+BAA+B;QACvC,KAAK,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;IAED,OAAO;QACN,IAAI;QACJ,KAAK;YACJ,IAAI,KAAK,EAAE,CAAC;gBACX,OAAO;YACR,CAAC;YACD,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,EAAE,CAAC;gBACvB,OAAO;YACR,CAAC;YACD,MAAM,EAAE,GAAG,eAAe,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,SAAS,CAAC,CAAC;YAC3D,KAAK,GAAG,WAAW,CAAC,+BAA+B,EAAE,EAAE,CAAC,CAAC;YACzD,+BAA+B,EAAE,CAAC;QACnC,CAAC;QACD,IAAI;YACH,IAAI,CAAC,KAAK,EAAE,CAAC;gBACZ,OAAO;YACR,CAAC;YACD,aAAa,CAAC,KAAK,CAAC,CAAC;YACrB,KAAK,GAAG,IAAI,CAAC;QACd,CAAC;KACD,CAAC;AACH,CAAC"}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,38 @@
1
+ import { type PollResult, type PollResultWithErrors } from "./poll-loop.js";
2
+ /** @internal */
3
+ export interface PollingLogger {
4
+ /** @internal */
5
+ warn(payload: Record<string, unknown>, message?: string): void;
6
+ /** @internal */
7
+ error(payload: Record<string, unknown>, message?: string): void;
8
+ }
9
+ /** @internal */
10
+ export interface PollingRegistration<T extends PollResultWithErrors> {
11
+ /** @internal */
12
+ id: string;
13
+ /** @internal */
14
+ pollOnce(): Promise<T>;
15
+ /** @internal */
16
+ isEnabled(): boolean;
17
+ /** @internal */
18
+ pollInterval(): string;
19
+ /** @internal */
20
+ defaultIntervalMs?: number;
21
+ }
22
+ /** @internal */
23
+ export interface RegisteredPoller<T extends PollResultWithErrors = PollResult> {
24
+ /** @internal */
25
+ poll(): Promise<T>;
26
+ }
27
+ /** @internal */
28
+ export interface PollingCoordinator {
29
+ /** @internal */
30
+ create<T extends PollResultWithErrors>(registration: PollingRegistration<T>): RegisteredPoller<T>;
31
+ /** @internal */
32
+ start(): void;
33
+ /** @internal */
34
+ stop(): Promise<void>;
35
+ }
36
+ /** @internal */
37
+ export declare function createPollingCoordinator(logger: PollingLogger): PollingCoordinator;
38
+ //# sourceMappingURL=polling-coordinator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"polling-coordinator.d.ts","sourceRoot":"","sources":["../src/polling-coordinator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,UAAU,EAAE,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAE5F,gBAAgB;AAChB,MAAM,WAAW,aAAa;IAC7B,gBAAgB;IAChB,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/D,gBAAgB;IAChB,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAChE;AAED,gBAAgB;AAChB,MAAM,WAAW,mBAAmB,CAAC,CAAC,SAAS,oBAAoB;IAClE,gBAAgB;IAChB,EAAE,EAAE,MAAM,CAAC;IACX,gBAAgB;IAChB,QAAQ,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC;IACvB,gBAAgB;IAChB,SAAS,IAAI,OAAO,CAAC;IACrB,gBAAgB;IAChB,YAAY,IAAI,MAAM,CAAC;IACvB,gBAAgB;IAChB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,gBAAgB;AAChB,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,oBAAoB,GAAG,UAAU;IAC5E,gBAAgB;IAChB,IAAI,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC;CACnB;AAED,gBAAgB;AAChB,MAAM,WAAW,kBAAkB;IAClC,gBAAgB;IAChB,MAAM,CAAC,CAAC,SAAS,oBAAoB,EAAE,YAAY,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAClG,gBAAgB;IAChB,KAAK,IAAI,IAAI,CAAC;IACd,gBAAgB;IAChB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACtB;AAED,gBAAgB;AAChB,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,aAAa,GAAG,kBAAkB,CAyClF"}
@@ -0,0 +1,9 @@
1
+ import {
2
+ createPollingCoordinator
3
+ } from "./chunk-ZW3LXDBX.js";
4
+ import "./chunk-BO52GPAV.js";
5
+ import "./chunk-D57H46H7.js";
6
+ export {
7
+ createPollingCoordinator
8
+ };
9
+ //# sourceMappingURL=polling-coordinator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,30 +1,45 @@
1
1
  import type { ProcessInstance } from "@leitwerk-dev/domain";
2
+ /** @internal */
2
3
  export interface ProcessEventRepoLike {
4
+ /** @internal */
3
5
  create(input: {
6
+ /** @internal */
4
7
  instanceId: string;
8
+ /** @internal */
5
9
  eventType: string;
10
+ /** @internal */
6
11
  data?: Record<string, unknown>;
7
12
  }): unknown;
13
+ /** @internal */
8
14
  listByInstance(instanceId: string, limit?: number): Array<{
15
+ /** @internal */
9
16
  eventType: string;
10
17
  }>;
11
18
  }
19
+ /** @internal */
12
20
  export interface WorkerSupervisorLike {
21
+ /** @internal */
13
22
  spawnWorker(instanceId: string): Promise<unknown>;
23
+ /** @internal */
14
24
  getWorker(instanceId: string): unknown | undefined;
15
25
  }
16
26
  /**
17
27
  * Check whether a process already has an event of the given type.
18
28
  */
29
+ /** @internal */
19
30
  export declare function hasProcessEvent(events: ProcessEventRepoLike, instanceId: string, eventType: string): boolean;
31
+ /** @internal */
20
32
  export declare function processNeedsWorker(process: Pick<ProcessInstance, "selectedTurnId" | "lifecycleStatus">): boolean;
21
33
  /**
22
34
  * Spawn a worker for a process whose selected turn is runnable but currently has
23
35
  * no running worker. No-ops when the process is not worker-runnable or already
24
36
  * has a worker.
25
37
  */
38
+ /** @internal */
26
39
  export declare function ensureWorkerForActiveAgent(deps: {
40
+ /** @internal */
27
41
  events: ProcessEventRepoLike;
42
+ /** @internal */
28
43
  supervisor: WorkerSupervisorLike;
29
44
  }, process: ProcessInstance): Promise<void>;
30
45
  //# sourceMappingURL=process-helpers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"process-helpers.d.ts","sourceRoot":"","sources":["../src/process-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,MAAM,WAAW,oBAAoB;IACpC,MAAM,CAAC,KAAK,EAAE;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,GAAG,OAAO,CAAC;IAClG,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACjF;AAED,MAAM,WAAW,oBAAoB;IACpC,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAClD,SAAS,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC;CACnD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC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AM,EAAE,oBAAoB,EAC5B,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,GACf,OAAO,CAET;AAED,gBAAgB;AAChB,wBAAgB,kBAAkB,CACjC,OAAO,EAAE,IAAI,CAAC,eAAe,EAAE,gBAAgB,GAAG,iBAAiB,CAAC,GAClE,OAAO,CAET;AAED;;;;GAIG;AACH,gBAAgB;AAChB,wBAAsB,0BAA0B,CAC/C,IAAI,EAAE;IACL,gBAAgB;IAChB,MAAM,EAAE,oBAAoB,CAAC;IAC7B,gBAAgB;IAChB,UAAU,EAAE,oBAAoB,CAAC;CACjC,EACD,OAAO,EAAE,eAAe,GACtB,OAAO,CAAC,IAAI,CAAC,CAgBf"}
@@ -1,33 +1,11 @@
1
- /**
2
- * Check whether a process already has an event of the given type.
3
- */
4
- export function hasProcessEvent(events, instanceId, eventType) {
5
- return events.listByInstance(instanceId, 200).some((event) => event.eventType === eventType);
6
- }
7
- export function processNeedsWorker(process) {
8
- return process.selectedTurnId !== null && process.lifecycleStatus === "active";
9
- }
10
- /**
11
- * Spawn a worker for a process whose selected turn is runnable but currently has
12
- * no running worker. No-ops when the process is not worker-runnable or already
13
- * has a worker.
14
- */
15
- export async function ensureWorkerForActiveAgent(deps, process) {
16
- if (!processNeedsWorker(process) || deps.supervisor.getWorker(process.id)) {
17
- return;
18
- }
19
- try {
20
- await deps.supervisor.spawnWorker(process.id);
21
- }
22
- catch (error) {
23
- deps.events.create({
24
- instanceId: process.id,
25
- eventType: "worker_spawn_failed",
26
- data: {
27
- reason: error instanceof Error ? error.message : "Failed to spawn worker",
28
- },
29
- });
30
- throw error;
31
- }
32
- }
1
+ import {
2
+ ensureWorkerForActiveAgent,
3
+ hasProcessEvent,
4
+ processNeedsWorker
5
+ } from "./chunk-EWA6E2ZM.js";
6
+ export {
7
+ ensureWorkerForActiveAgent,
8
+ hasProcessEvent,
9
+ processNeedsWorker
10
+ };
33
11
  //# sourceMappingURL=process-helpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"process-helpers.js","sourceRoot":"","sources":["../src/process-helpers.ts"],"names":[],"mappings":"AAYA;;GAEG;AACH,MAAM,UAAU,eAAe,CAC9B,MAA4B,EAC5B,UAAkB,EAClB,SAAiB;IAEjB,OAAO,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC;AAC9F,CAAC;AAED,MAAM,UAAU,kBAAkB,CACjC,OAAoE;IAEpE,OAAO,OAAO,CAAC,cAAc,KAAK,IAAI,IAAI,OAAO,CAAC,eAAe,KAAK,QAAQ,CAAC;AAChF,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC/C,IAAwE,EACxE,OAAwB;IAExB,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC;QAC3E,OAAO;IACR,CAAC;IACD,IAAI,CAAC;QACJ,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAC/C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;YAClB,UAAU,EAAE,OAAO,CAAC,EAAE;YACtB,SAAS,EAAE,qBAAqB;YAChC,IAAI,EAAE;gBACL,MAAM,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,wBAAwB;aACzE;SACD,CAAC,CAAC;QACH,MAAM,KAAK,CAAC;IACb,CAAC;AACF,CAAC"}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,10 +1,17 @@
1
+ /** @internal */
1
2
  export type TriggerFileReadResult = {
3
+ /** @internal */
2
4
  exists: false;
3
5
  } | {
6
+ /** @internal */
4
7
  exists: true;
8
+ /** @internal */
5
9
  content: string;
6
10
  };
11
+ /** @internal */
7
12
  export declare function errorCode(error: unknown): string | null;
13
+ /** @internal */
8
14
  export declare function readTriggerFile(filePath: string): Promise<TriggerFileReadResult>;
15
+ /** @internal */
9
16
  export declare function consumeTriggerFile(filePath: string): Promise<void>;
10
17
  //# sourceMappingURL=trigger-files.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"trigger-files.d.ts","sourceRoot":"","sources":["../src/trigger-files.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,qBAAqB,GAAG;IAAE,MAAM,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,IAAI,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAE1F,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAMvD;AAED,wBAAsB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAStF;AAED,wBAAsB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CASxE"}
1
+ {"version":3,"file":"trigger-files.d.ts","sourceRoot":"","sources":["../src/trigger-files.ts"],"names":[],"mappings":"AAEA,gBAAgB;AAChB,MAAM,MAAM,qBAAqB,GAC9B;IACA,gBAAgB;IAChB,MAAM,EAAE,KAAK,CAAC;CACb,GACD;IACA,gBAAgB;IAChB,MAAM,EAAE,IAAI,CAAC;IACb,gBAAgB;IAChB,OAAO,EAAE,MAAM,CAAC;CACf,CAAC;AAEL,gBAAgB;AAChB,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAMvD;AAED,gBAAgB;AAChB,wBAAsB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAStF;AAED,gBAAgB;AAChB,wBAAsB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CASxE"}