spool-engine 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/README.md +12 -2
- package/dist/config.js +130 -0
- package/dist/config.js.map +1 -0
- package/dist/domain/errors.js +108 -0
- package/dist/domain/errors.js.map +1 -0
- package/dist/domain/journal.js +21 -0
- package/dist/domain/journal.js.map +1 -0
- package/dist/domain/json.js +55 -0
- package/dist/domain/json.js.map +1 -0
- package/dist/domain/llm.js +22 -0
- package/dist/domain/llm.js.map +1 -0
- package/dist/domain/retry.js +21 -0
- package/dist/domain/retry.js.map +1 -0
- package/dist/domain/run-status.js +40 -0
- package/dist/domain/run-status.js.map +1 -0
- package/dist/domain/sleep.js +7 -0
- package/dist/domain/sleep.js.map +1 -0
- package/dist/engine.js +131 -0
- package/dist/engine.js.map +1 -0
- package/dist/loops/every.js +32 -0
- package/dist/loops/every.js.map +1 -0
- package/dist/loops/listener.js +58 -0
- package/dist/loops/listener.js.map +1 -0
- package/dist/loops/live-runs.js +30 -0
- package/dist/loops/live-runs.js.map +1 -0
- package/dist/loops/queue-depth.js +28 -0
- package/dist/loops/queue-depth.js.map +1 -0
- package/dist/loops/reaper.js +71 -0
- package/dist/loops/reaper.js.map +1 -0
- package/dist/loops/waker.js +42 -0
- package/dist/loops/waker.js.map +1 -0
- package/dist/main.js +48 -0
- package/dist/main.js.map +1 -0
- package/dist/observability/logger.js +9 -0
- package/dist/observability/logger.js.map +1 -0
- package/dist/observability/metrics.js +97 -0
- package/dist/observability/metrics.js.map +1 -0
- package/dist/observability/tracing.js +146 -0
- package/dist/observability/tracing.js.map +1 -0
- package/dist/proto/limits_pb.d.ts +212 -0
- package/dist/proto/limits_pb.js +76 -0
- package/dist/proto/runs_pb.d.ts +740 -0
- package/dist/proto/runs_pb.js +147 -0
- package/dist/proto/workers_pb.d.ts +900 -0
- package/dist/proto/workers_pb.js +168 -0
- package/dist/services/admission.js +47 -0
- package/dist/services/admission.js.map +1 -0
- package/dist/services/cancel-run.js +43 -0
- package/dist/services/cancel-run.js.map +1 -0
- package/dist/services/claim-tasks.js +59 -0
- package/dist/services/claim-tasks.js.map +1 -0
- package/dist/services/complete-task.js +45 -0
- package/dist/services/complete-task.js.map +1 -0
- package/dist/services/fail-step.js +102 -0
- package/dist/services/fail-step.js.map +1 -0
- package/dist/services/group-commit.js +83 -0
- package/dist/services/group-commit.js.map +1 -0
- package/dist/services/live-tokens.js +90 -0
- package/dist/services/live-tokens.js.map +1 -0
- package/dist/services/next-step.js +33 -0
- package/dist/services/next-step.js.map +1 -0
- package/dist/services/notifier.js +54 -0
- package/dist/services/notifier.js.map +1 -0
- package/dist/services/query-runs.js +14 -0
- package/dist/services/query-runs.js.map +1 -0
- package/dist/services/replay-run.js +63 -0
- package/dist/services/replay-run.js.map +1 -0
- package/dist/services/report-step.js +129 -0
- package/dist/services/report-step.js.map +1 -0
- package/dist/services/run-watches.js +46 -0
- package/dist/services/run-watches.js.map +1 -0
- package/dist/services/send-event.js +16 -0
- package/dist/services/send-event.js.map +1 -0
- package/dist/services/sleep-run.js +47 -0
- package/dist/services/sleep-run.js.map +1 -0
- package/dist/services/throttle-step.js +76 -0
- package/dist/services/throttle-step.js.map +1 -0
- package/dist/services/trigger-run.js +38 -0
- package/dist/services/trigger-run.js.map +1 -0
- package/dist/services/wait-for-event.js +73 -0
- package/dist/services/wait-for-event.js.map +1 -0
- package/dist/services/wakeups.js +70 -0
- package/dist/services/wakeups.js.map +1 -0
- package/dist/services/watch-run.js +36 -0
- package/dist/services/watch-run.js.map +1 -0
- package/dist/services/worker-session.js +198 -0
- package/dist/services/worker-session.js.map +1 -0
- package/dist/store/errors.js +42 -0
- package/dist/store/errors.js.map +1 -0
- package/dist/store/events.js +96 -0
- package/dist/store/events.js.map +1 -0
- package/dist/store/idempotency.js +29 -0
- package/dist/store/idempotency.js.map +1 -0
- package/dist/store/json.js +9 -0
- package/dist/store/json.js.map +1 -0
- package/dist/store/limits.js +60 -0
- package/dist/store/limits.js.map +1 -0
- package/dist/store/migrate.js +20 -0
- package/dist/store/migrate.js.map +1 -0
- package/dist/store/run-views.js +96 -0
- package/dist/store/run-views.js.map +1 -0
- package/dist/store/runs.js +104 -0
- package/dist/store/runs.js.map +1 -0
- package/dist/store/schema.js +191 -0
- package/dist/store/schema.js.map +1 -0
- package/dist/store/steps.js +114 -0
- package/dist/store/steps.js.map +1 -0
- package/dist/store/tasks.js +463 -0
- package/dist/store/tasks.js.map +1 -0
- package/dist/store/transaction.js +23 -0
- package/dist/store/transaction.js.map +1 -0
- package/dist/store/waits.js +75 -0
- package/dist/store/waits.js.map +1 -0
- package/dist/store/watches.js +31 -0
- package/dist/store/watches.js.map +1 -0
- package/dist/transport/auth.js +26 -0
- package/dist/transport/auth.js.map +1 -0
- package/dist/transport/channel.js +46 -0
- package/dist/transport/channel.js.map +1 -0
- package/dist/transport/errors.js +74 -0
- package/dist/transport/errors.js.map +1 -0
- package/dist/transport/limits-service.js +60 -0
- package/dist/transport/limits-service.js.map +1 -0
- package/dist/transport/protocol.js +14 -0
- package/dist/transport/protocol.js.map +1 -0
- package/dist/transport/run-events.js +133 -0
- package/dist/transport/run-events.js.map +1 -0
- package/dist/transport/run-messages.js +78 -0
- package/dist/transport/run-messages.js.map +1 -0
- package/dist/transport/run-service.js +147 -0
- package/dist/transport/run-service.js.map +1 -0
- package/dist/transport/server.js +227 -0
- package/dist/transport/server.js.map +1 -0
- package/dist/transport/validation.js +61 -0
- package/dist/transport/validation.js.map +1 -0
- package/dist/transport/worker-service.js +396 -0
- package/dist/transport/worker-service.js.map +1 -0
- package/drizzle/0000_init.sql +95 -0
- package/drizzle/0001_tasks_storage.sql +2 -0
- package/drizzle/0002_waits_route.sql +3 -0
- package/drizzle/0003_event_waits.sql +8 -0
- package/drizzle/0004_llm_steps.sql +2 -0
- package/drizzle/0005_tasks_notify.sql +19 -0
- package/drizzle/0006_drop_tasks_notify.sql +8 -0
- package/drizzle/0007_tasks_fair.sql +5 -0
- package/drizzle/0008_concurrency_limits.sql +8 -0
- package/drizzle/0009_rate_limits.sql +12 -0
- package/drizzle/0010_run_listings.sql +2 -0
- package/drizzle/0011_run_replays.sql +7 -0
- package/drizzle/0012_run_watches.sql +8 -0
- package/drizzle/meta/0000_snapshot.json +668 -0
- package/drizzle/meta/0001_snapshot.json +668 -0
- package/drizzle/meta/0002_snapshot.json +680 -0
- package/drizzle/meta/0003_snapshot.json +723 -0
- package/drizzle/meta/0004_snapshot.json +733 -0
- package/drizzle/meta/0005_snapshot.json +733 -0
- package/drizzle/meta/0006_snapshot.json +733 -0
- package/drizzle/meta/0007_snapshot.json +739 -0
- package/drizzle/meta/0008_snapshot.json +791 -0
- package/drizzle/meta/0009_snapshot.json +843 -0
- package/drizzle/meta/0010_snapshot.json +881 -0
- package/drizzle/meta/0011_snapshot.json +903 -0
- package/drizzle/meta/0012_snapshot.json +958 -0
- package/drizzle/meta/_journal.json +97 -0
- package/package.json +58 -3
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* This engine's live viewers by run: event streams subscribe to a run, and tokens arriving
|
|
4
|
+
* by NOTIFY, from this engine or another, are handed to each of them.
|
|
5
|
+
*/
|
|
6
|
+
export class TokenHub {
|
|
7
|
+
#viewers = new Map();
|
|
8
|
+
subscribe(runId, viewer) {
|
|
9
|
+
const viewers = this.#viewers.get(runId) ?? new Set();
|
|
10
|
+
this.#viewers.set(runId, viewers);
|
|
11
|
+
viewers.add(viewer);
|
|
12
|
+
return () => {
|
|
13
|
+
viewers.delete(viewer);
|
|
14
|
+
if (viewers.size === 0)
|
|
15
|
+
this.#viewers.delete(runId);
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
deliver(tokens) {
|
|
19
|
+
for (const viewer of this.#viewers.get(tokens.runId) ?? [])
|
|
20
|
+
viewer(tokens);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** NOTIFY payloads are limited to 8,000 bytes; text is split well below that. */
|
|
24
|
+
export const MAX_TOKEN_CHUNK_BYTES = 6000;
|
|
25
|
+
/** Splits text into pieces of at most `maxBytes` UTF-8 bytes, never inside a character. */
|
|
26
|
+
export function chunkText(text, maxBytes = MAX_TOKEN_CHUNK_BYTES) {
|
|
27
|
+
const chunks = [];
|
|
28
|
+
let current = "";
|
|
29
|
+
let bytes = 0;
|
|
30
|
+
for (const char of text) {
|
|
31
|
+
const size = Buffer.byteLength(char);
|
|
32
|
+
if (bytes + size > maxBytes && current !== "") {
|
|
33
|
+
chunks.push(current);
|
|
34
|
+
current = "";
|
|
35
|
+
bytes = 0;
|
|
36
|
+
}
|
|
37
|
+
current += char;
|
|
38
|
+
bytes += size;
|
|
39
|
+
}
|
|
40
|
+
if (current !== "")
|
|
41
|
+
chunks.push(current);
|
|
42
|
+
return chunks;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Passes a worker's tokens on to every engine by NOTIFY, but only for runs someone follows
|
|
46
|
+
* (the watched set, refreshed from run_watches each second): tokens of a run nobody is
|
|
47
|
+
* watching cost nothing past this check. Best effort: a failed NOTIFY drops the tokens,
|
|
48
|
+
* which are never stored; the step's recorded output is what counts.
|
|
49
|
+
*/
|
|
50
|
+
export class TokenRelay {
|
|
51
|
+
publish;
|
|
52
|
+
logger;
|
|
53
|
+
#watched = new Set();
|
|
54
|
+
constructor(publish, logger) {
|
|
55
|
+
this.publish = publish;
|
|
56
|
+
this.logger = logger;
|
|
57
|
+
}
|
|
58
|
+
/** The runs followed anywhere now. */
|
|
59
|
+
setWatched(runIds) {
|
|
60
|
+
this.#watched = new Set(runIds);
|
|
61
|
+
}
|
|
62
|
+
/** Forwards the tokens if the run is watched; returns whether it was. */
|
|
63
|
+
forward(tokens) {
|
|
64
|
+
if (!this.#watched.has(tokens.runId))
|
|
65
|
+
return false;
|
|
66
|
+
for (const text of chunkText(tokens.text)) {
|
|
67
|
+
const payload = JSON.stringify({ r: tokens.runId, s: tokens.seq, t: text });
|
|
68
|
+
void this.publish(payload).catch((error) => {
|
|
69
|
+
this.logger.debug({ err: error, runId: tokens.runId }, "live tokens dropped");
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const payloadSchema = z.object({ r: z.string(), s: z.number().int(), t: z.string() });
|
|
76
|
+
/** Reads a NOTIFY payload back; undefined for anything malformed, which is dropped. */
|
|
77
|
+
export function parseTokenPayload(payload) {
|
|
78
|
+
let value;
|
|
79
|
+
try {
|
|
80
|
+
value = JSON.parse(payload);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const parsed = payloadSchema.safeParse(value);
|
|
86
|
+
return parsed.success
|
|
87
|
+
? { runId: parsed.data.r, seq: parsed.data.s, text: parsed.data.t }
|
|
88
|
+
: undefined;
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=live-tokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"live-tokens.js","sourceRoot":"","sources":["../../src/services/live-tokens.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAWxB;;;GAGG;AACH,MAAM,OAAO,QAAQ;IACV,QAAQ,GAAG,IAAI,GAAG,EAAuB,CAAC;IAEnD,SAAS,CAAC,KAAa,EAAE,MAAc;QACrC,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,GAAG,EAAE,CAAC;QACtD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAClC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACpB,OAAO,GAAG,EAAE;YACV,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YACvB,IAAI,OAAO,CAA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AAS,CAAC;IACnB,CAAC;IACD,MAAM,MAAM,GAAG,aAAa,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC9C,OAAO,MAAM,CAAC,OAAO;QACnB,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE;QACnE,CAAC,CAAC,SAAS,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { InvalidArgumentError, LeaseLostError, StepMismatchError } from "../domain/errors.js";
|
|
2
|
+
import { classifyStepReport, MAX_STEPS_PER_RUN } from "../domain/journal.js";
|
|
3
|
+
import { journalPosition } from "../store/steps.js";
|
|
4
|
+
import { lockLeasedTask } from "../store/tasks.js";
|
|
5
|
+
/**
|
|
6
|
+
* The start of every transaction that parks a run at its next step (a sleep or a wait):
|
|
7
|
+
* fences and locks the task, then checks that `seq` is the next step. A position already
|
|
8
|
+
* recorded is refused rather than acknowledged: the worker must replay it, not start it
|
|
9
|
+
* again, and the first copy of a parking request has given up the task anyway.
|
|
10
|
+
*/
|
|
11
|
+
export async function lockForNextStep(tx, claim) {
|
|
12
|
+
const locked = await lockLeasedTask(tx, {
|
|
13
|
+
taskId: claim.taskId,
|
|
14
|
+
owner: claim.workerId,
|
|
15
|
+
attempt: claim.attempt,
|
|
16
|
+
});
|
|
17
|
+
if (locked === undefined)
|
|
18
|
+
throw new LeaseLostError();
|
|
19
|
+
const outcome = classifyStepReport(await journalPosition(tx, locked.runId, claim.seq), claim);
|
|
20
|
+
switch (outcome.kind) {
|
|
21
|
+
case "mismatch":
|
|
22
|
+
throw new StepMismatchError(claim.seq);
|
|
23
|
+
case "duplicate":
|
|
24
|
+
throw new InvalidArgumentError(`step ${String(claim.seq)} is already recorded; replay it instead of starting it again`);
|
|
25
|
+
case "gap":
|
|
26
|
+
throw new InvalidArgumentError(`seq ${String(claim.seq)} skips ahead: expected step ${String(outcome.expected)}`);
|
|
27
|
+
case "too_many_steps":
|
|
28
|
+
throw new InvalidArgumentError(`a run may record at most ${String(MAX_STEPS_PER_RUN)} steps`);
|
|
29
|
+
case "record":
|
|
30
|
+
return locked;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=next-step.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"next-step.js","sourceRoot":"","sources":["../../src/services/next-step.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAC9F,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAC7E,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAmB,MAAM,mBAAmB,CAAC;AAWpE;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,EAAa,EAAE,KAAoB;IACvE,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,EAAE,EAAE;QACtC,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,KAAK,EAAE,KAAK,CAAC,QAAQ;QACrB,OAAO,EAAE,KAAK,CAAC,OAAO;KACvB,CAAC,CAAC;IACH,IAAI,MAAM,KAAK,SAAS;QAAE,MAAM,IAAI,cAAc,EAAE,CAAC;IACrD,MAAM,OAAO,GAAG,kBAAkB,CAAC,MAAM,eAAe,CAAC,EAAE,EAAE,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9F,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;QACrB,KAAK,UAAU;YACb,MAAM,IAAI,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACzC,KAAK,WAAW;YACd,MAAM,IAAI,oBAAoB,CAC5B,QAAQ,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,8DAA8D,CACxF,CAAC;QACJ,KAAK,KAAK;YACR,MAAM,IAAI,oBAAoB,CAC5B,OAAO,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,+BAA+B,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAClF,CAAC;QACJ,KAAK,gBAAgB;YACnB,MAAM,IAAI,oBAAoB,CAAC,4BAA4B,MAAM,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QAChG,KAAK,QAAQ;YACX,OAAO,MAAM,CAAC;IAClB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/** The payload that means "work may be claimable on any queue": no queue name is empty. */
|
|
2
|
+
export const ANY_QUEUE = "";
|
|
3
|
+
/**
|
|
4
|
+
* Tells idle worker streams that tasks became claimable. The engine that made them
|
|
5
|
+
* claimable wakes one of its own streams at once, in process, and tells the other engines
|
|
6
|
+
* with a NOTIFY of its own, after the data has committed, never inside the transaction
|
|
7
|
+
* that wrote it: Postgres serialises every notifying commit on one database-wide lock held
|
|
8
|
+
* until the commit is flushed, which at a few hundred triggers a second queued every commit
|
|
9
|
+
* behind the disk (measured in the soak). A NOTIFY-only statement writes no data, so its
|
|
10
|
+
* commit waits for no flush.
|
|
11
|
+
*
|
|
12
|
+
* Bursts fold: per payload, one NOTIFY is in flight and at most one more is owed. A failed
|
|
13
|
+
* NOTIFY is logged and dropped: every stream still polls, so it costs at most one poll
|
|
14
|
+
* interval, never a task.
|
|
15
|
+
*/
|
|
16
|
+
export class TaskNotifier {
|
|
17
|
+
wakeups;
|
|
18
|
+
publish;
|
|
19
|
+
logger;
|
|
20
|
+
#inFlight = new Set();
|
|
21
|
+
#owed = new Set();
|
|
22
|
+
constructor(wakeups, publish, logger) {
|
|
23
|
+
this.wakeups = wakeups;
|
|
24
|
+
this.publish = publish;
|
|
25
|
+
this.logger = logger;
|
|
26
|
+
}
|
|
27
|
+
/** Tasks on `queue` became claimable now. */
|
|
28
|
+
claimable(queue) {
|
|
29
|
+
this.wakeups.notify(queue);
|
|
30
|
+
this.#send(queue);
|
|
31
|
+
}
|
|
32
|
+
/** Tasks on queues not known here became claimable (released leases, woken runs). */
|
|
33
|
+
claimableAnywhere() {
|
|
34
|
+
this.wakeups.notifyAll();
|
|
35
|
+
this.#send(ANY_QUEUE);
|
|
36
|
+
}
|
|
37
|
+
#send(payload) {
|
|
38
|
+
if (this.#inFlight.has(payload)) {
|
|
39
|
+
this.#owed.add(payload);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
this.#inFlight.add(payload);
|
|
43
|
+
void this.publish(payload)
|
|
44
|
+
.catch((error) => {
|
|
45
|
+
this.logger.warn({ err: error }, "could not notify other engines; they will poll");
|
|
46
|
+
})
|
|
47
|
+
.finally(() => {
|
|
48
|
+
this.#inFlight.delete(payload);
|
|
49
|
+
if (this.#owed.delete(payload))
|
|
50
|
+
this.#send(payload);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=notifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notifier.js","sourceRoot":"","sources":["../../src/services/notifier.ts"],"names":[],"mappings":"AAGA,2FAA2F;AAC3F,MAAM,CAAC,MAAM,SAAS,GAAG,EAAE,CAAC;AAE5B;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,YAAY;IAKJ;IACA;IACA;IANV,SAAS,GAAG,IAAI,GAAG,EAAU,CAAC;IAC9B,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;IAEnC,YACmB,OAAgB,EAChB,OAA2C,EAC3C,MAAc;QAFd,YAAO,GAAP,OAAO,CAAS;QAChB,YAAO,GAAP,OAAO,CAAoC;QAC3C,WAAM,GAAN,MAAM,CAAQ;IAC9B,CAAC;IAEJ,6CAA6C;IAC7C,SAAS,CAAC,KAAa;QACrB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC3B,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,qFAAqF;IACrF,iBAAiB;QACf,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;QACzB,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IACxB,CAAC;IAED,KAAK,CAAC,OAAe;QACnB,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YAChC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YACxB,OAAO;QACT,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC5B,KAAK,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;aACvB,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;YACxB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,gDAAgD,CAAC,CAAC;QACrF,CAAC,CAAC;aACD,OAAO,CAAC,GAAG,EAAE;YACZ,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAC/B,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC;gBAAE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACtD,CAAC,CAAC,CAAC;IACP,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RunNotFoundError } from "../domain/errors.js";
|
|
2
|
+
import { getRunDetail, listRuns, } from "../store/run-views.js";
|
|
3
|
+
/** A page of runs, newest first (F16). Read-only: one statement, no transaction. */
|
|
4
|
+
export function findRuns(sql, filter) {
|
|
5
|
+
return listRuns(sql, filter);
|
|
6
|
+
}
|
|
7
|
+
/** Everything recorded about one run; RunNotFoundError when there is no such run. */
|
|
8
|
+
export async function getRun(sql, runId) {
|
|
9
|
+
const run = await getRunDetail(sql, runId);
|
|
10
|
+
if (run === undefined)
|
|
11
|
+
throw new RunNotFoundError();
|
|
12
|
+
return run;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=query-runs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-runs.js","sourceRoot":"","sources":["../../src/services/query-runs.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EACL,YAAY,EACZ,QAAQ,GAIT,MAAM,uBAAuB,CAAC;AAI/B,oFAAoF;AACpF,MAAM,UAAU,QAAQ,CAAC,GAAQ,EAAE,MAAiB;IAClD,OAAO,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;AAC/B,CAAC;AAED,qFAAqF;AACrF,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,GAAQ,EAAE,KAAa;IAClD,MAAM,GAAG,GAAG,MAAM,YAAY,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAC3C,IAAI,GAAG,KAAK,SAAS;QAAE,MAAM,IAAI,gBAAgB,EAAE,CAAC;IACpD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { IdempotencyConflictError, InvalidArgumentError, RunNotFinishedError, RunNotFoundError, } from "../domain/errors.js";
|
|
2
|
+
import { isTerminal } from "../domain/run-status.js";
|
|
3
|
+
import { claimIdempotencyKey } from "../store/idempotency.js";
|
|
4
|
+
import { insertRun, replaySource } from "../store/runs.js";
|
|
5
|
+
import { copySteps } from "../store/steps.js";
|
|
6
|
+
import { insertTask } from "../store/tasks.js";
|
|
7
|
+
import { withTransaction } from "../store/transaction.js";
|
|
8
|
+
/**
|
|
9
|
+
* Replays a finished run from a chosen step (F14) as a new run, in one transaction: the
|
|
10
|
+
* same tenant, workflow and input, with the original's steps before `fromSeq` copied into
|
|
11
|
+
* its journal, and a task to run the rest. The original is untouched, so its journal stays
|
|
12
|
+
* append-only (invariant 4), and the new run's steps get new idempotency keys
|
|
13
|
+
* (`run_id:seq`): replaying a step runs its side effects again, which is what a replay is
|
|
14
|
+
* for.
|
|
15
|
+
*/
|
|
16
|
+
export async function replayRun(sql, command,
|
|
17
|
+
/** Admission control for the run's tenant (invariant 9): throws to shed the replay. */
|
|
18
|
+
admit = () => undefined) {
|
|
19
|
+
return withTransaction(sql, async (tx) => {
|
|
20
|
+
const source = await replaySource(tx, command.runId);
|
|
21
|
+
if (source === undefined)
|
|
22
|
+
throw new RunNotFoundError();
|
|
23
|
+
if (!isTerminal(source.status))
|
|
24
|
+
throw new RunNotFinishedError();
|
|
25
|
+
if (command.fromSeq > source.steps) {
|
|
26
|
+
throw new InvalidArgumentError(`fromSeq ${String(command.fromSeq)} is past the run's ${String(source.steps)} recorded steps`);
|
|
27
|
+
}
|
|
28
|
+
const queue = command.queue ?? source.queue ?? "default";
|
|
29
|
+
const { tenantId, workflow } = source;
|
|
30
|
+
admit(tenantId);
|
|
31
|
+
let reservedId;
|
|
32
|
+
if (command.idempotencyKey !== undefined) {
|
|
33
|
+
const claim = await claimIdempotencyKey(tx, {
|
|
34
|
+
tenantId,
|
|
35
|
+
workflow,
|
|
36
|
+
key: command.idempotencyKey,
|
|
37
|
+
// What the key stands for: this replay of this run, not the run's input.
|
|
38
|
+
input: { replayOf: command.runId, fromSeq: command.fromSeq },
|
|
39
|
+
});
|
|
40
|
+
if (claim.kind === "existing") {
|
|
41
|
+
if (!claim.sameInput)
|
|
42
|
+
throw new IdempotencyConflictError();
|
|
43
|
+
return { runId: claim.runId, created: false, queue };
|
|
44
|
+
}
|
|
45
|
+
reservedId = claim.runId;
|
|
46
|
+
}
|
|
47
|
+
const concurrencyKey = source.concurrencyKey ?? undefined;
|
|
48
|
+
const runId = await insertRun(tx, {
|
|
49
|
+
id: reservedId,
|
|
50
|
+
tenantId,
|
|
51
|
+
workflow,
|
|
52
|
+
input: source.input,
|
|
53
|
+
idempotencyKey: command.idempotencyKey,
|
|
54
|
+
concurrencyKey,
|
|
55
|
+
queue,
|
|
56
|
+
replay: { of: command.runId, from: command.fromSeq },
|
|
57
|
+
});
|
|
58
|
+
await copySteps(tx, command.runId, runId, command.fromSeq);
|
|
59
|
+
await insertTask(tx, { runId, queue, tenantId, workflow, concurrencyKey });
|
|
60
|
+
return { runId, created: true, queue };
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=replay-run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay-run.js","sourceRoot":"","sources":["../../src/services/replay-run.ts"],"names":[],"mappings":"AACA,OAAO,EACL,wBAAwB,EACxB,oBAAoB,EACpB,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAoB1D;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,GAAQ,EACR,OAAyB;AACzB,uFAAuF;AACvF,QAAoC,GAAG,EAAE,CAAC,SAAS;IAEnD,OAAO,eAAe,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE;QACvC,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,EAAE,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;QACrD,IAAI,MAAM,KAAK,SAAS;YAAE,MAAM,IAAI,gBAAgB,EAAE,CAAC;QACvD,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,MAA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;YACR,QAAQ;YACR,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,cAAc,EAAE,OAAO,CAAC,cAAc;YACtC,cAAc;YACd,KAAK;YACL,MAAM,EAAE,EAAE,EAAE,EAAE,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE;SACrD,CAAC,CAAC;QACH,MAAM,SAAS,CAAC,EAAE,EAAE,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;QAC3D,MAAM,UAAU,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,cAAc,EAAE,CAAC,CAAC;QAC3E,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IACzC,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { InvalidArgumentError, LeaseLostError, StepMismatchError } from "../domain/errors.js";
|
|
2
|
+
import { classifyStepReport, MAX_STEPS_PER_RUN } from "../domain/journal.js";
|
|
3
|
+
import { llmCostUsd } from "../domain/llm.js";
|
|
4
|
+
import { markRunsRunning } from "../store/runs.js";
|
|
5
|
+
import { insertSteps, journalPositions } from "../store/steps.js";
|
|
6
|
+
import { lockLeasedTasks, renewAfterReports } from "../store/tasks.js";
|
|
7
|
+
import { withTransaction } from "../store/transaction.js";
|
|
8
|
+
/**
|
|
9
|
+
* Records a batch of finished steps in one transaction (group commit). Every report is
|
|
10
|
+
* judged on its own, exactly as a lone report would be: fenced on its lease, then classified
|
|
11
|
+
* against its run's journal. One report's refusal (a lost lease, a renamed step, a gap)
|
|
12
|
+
* never fails the others; a failure of the transaction itself fails them all, and the
|
|
13
|
+
* worker resends. Fencing, the journal appends, the runs' moves to running and the lease
|
|
14
|
+
* renewals commit together (invariant 3), with about five statements for the whole batch.
|
|
15
|
+
*
|
|
16
|
+
* Each report in the batch must be for a different task: two reports of one run would race
|
|
17
|
+
* on its journal position. The batcher guarantees it.
|
|
18
|
+
*/
|
|
19
|
+
export async function reportSteps(deps, commands) {
|
|
20
|
+
if (new Set(commands.map((c) => c.taskId)).size !== commands.length) {
|
|
21
|
+
throw new Error("a batch of step reports must have at most one report per task");
|
|
22
|
+
}
|
|
23
|
+
const llms = commands.map((c) => c.llm === undefined ? undefined : { ...c.llm, costUsd: llmCostUsd(deps.llmPrices, c.llm) });
|
|
24
|
+
// Each report's run, from its lease, for the step duration metric and its span.
|
|
25
|
+
const runs = commands.map(() => undefined);
|
|
26
|
+
const outcomes = await withTransaction(deps.sql, async (tx) => {
|
|
27
|
+
const locked = await lockLeasedTasks(tx, commands.map((c) => ({ taskId: c.taskId, owner: c.workerId, attempt: c.attempt })));
|
|
28
|
+
const held = commands.flatMap((command, index) => {
|
|
29
|
+
const lease = locked.get(command.taskId);
|
|
30
|
+
if (lease === undefined)
|
|
31
|
+
return [];
|
|
32
|
+
runs[index] = { runId: lease.runId, tenantId: lease.tenantId, workflow: lease.workflow };
|
|
33
|
+
return [{ index, command, runId: lease.runId }];
|
|
34
|
+
});
|
|
35
|
+
const positions = await journalPositions(tx, held.map((h) => ({ runId: h.runId, seq: h.command.seq })));
|
|
36
|
+
const results = commands.map(() => ({
|
|
37
|
+
ok: false,
|
|
38
|
+
error: new LeaseLostError(),
|
|
39
|
+
}));
|
|
40
|
+
const steps = [];
|
|
41
|
+
const progressed = [];
|
|
42
|
+
const unchanged = [];
|
|
43
|
+
for (const [n, { index, command, runId }] of held.entries()) {
|
|
44
|
+
const position = positions[n];
|
|
45
|
+
if (position === undefined)
|
|
46
|
+
throw new Error("journal positions came back short");
|
|
47
|
+
const outcome = classifyStepReport(position, command);
|
|
48
|
+
switch (outcome.kind) {
|
|
49
|
+
case "mismatch":
|
|
50
|
+
results[index] = { ok: false, error: new StepMismatchError(command.seq) };
|
|
51
|
+
break;
|
|
52
|
+
case "gap":
|
|
53
|
+
results[index] = {
|
|
54
|
+
ok: false,
|
|
55
|
+
error: new InvalidArgumentError(`seq ${String(command.seq)} skips ahead: expected step ${String(outcome.expected)}`),
|
|
56
|
+
};
|
|
57
|
+
break;
|
|
58
|
+
case "too_many_steps":
|
|
59
|
+
results[index] = {
|
|
60
|
+
ok: false,
|
|
61
|
+
error: new InvalidArgumentError(`a run may record at most ${String(MAX_STEPS_PER_RUN)} steps`),
|
|
62
|
+
};
|
|
63
|
+
break;
|
|
64
|
+
case "duplicate":
|
|
65
|
+
unchanged.push(command.taskId);
|
|
66
|
+
results[index] = { ok: true, value: { recorded: false } };
|
|
67
|
+
break;
|
|
68
|
+
case "record": {
|
|
69
|
+
const llm = llms[index];
|
|
70
|
+
steps.push({
|
|
71
|
+
runId,
|
|
72
|
+
seq: command.seq,
|
|
73
|
+
name: command.name,
|
|
74
|
+
output: command.output,
|
|
75
|
+
durationMs: command.durationMs,
|
|
76
|
+
...(llm === undefined ? { kind: "run" } : { kind: "llm", llm }),
|
|
77
|
+
});
|
|
78
|
+
progressed.push(command.taskId);
|
|
79
|
+
results[index] = { ok: true, value: { recorded: true } };
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
await insertSteps(tx, steps);
|
|
85
|
+
await markRunsRunning(tx, steps.map((step) => step.runId));
|
|
86
|
+
await renewAfterReports(tx, progressed, unchanged, deps.leaseMs);
|
|
87
|
+
return results;
|
|
88
|
+
});
|
|
89
|
+
// After the commit, and only for reports that recorded a step: a resent report or a
|
|
90
|
+
// rolled-back batch counts nothing, so the totals match the steps table.
|
|
91
|
+
for (const [index, outcome] of outcomes.entries()) {
|
|
92
|
+
if (!outcome.ok && outcome.error instanceof LeaseLostError)
|
|
93
|
+
deps.metrics.fencedCompletions.inc();
|
|
94
|
+
if (!outcome.ok || !outcome.value.recorded)
|
|
95
|
+
continue;
|
|
96
|
+
const command = commands[index];
|
|
97
|
+
const run = runs[index];
|
|
98
|
+
const llm = llms[index];
|
|
99
|
+
if (command !== undefined && run !== undefined) {
|
|
100
|
+
deps.metrics.stepDuration.observe({ workflow: run.workflow, step: command.name }, command.durationMs / 1000);
|
|
101
|
+
deps.tracer?.stepRecorded({
|
|
102
|
+
...run,
|
|
103
|
+
seq: command.seq,
|
|
104
|
+
name: command.name,
|
|
105
|
+
durationMs: command.durationMs,
|
|
106
|
+
...(llm === undefined ? {} : { llm }),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (llm !== undefined)
|
|
110
|
+
countLlmUsage(deps.metrics, llm);
|
|
111
|
+
}
|
|
112
|
+
return outcomes;
|
|
113
|
+
}
|
|
114
|
+
/** Records one finished step: a batch of one. */
|
|
115
|
+
export async function reportStep(deps, command) {
|
|
116
|
+
const [outcome] = await reportSteps(deps, [command]);
|
|
117
|
+
if (outcome === undefined)
|
|
118
|
+
throw new Error("a batch of one returned no outcome");
|
|
119
|
+
if (!outcome.ok)
|
|
120
|
+
throw outcome.error;
|
|
121
|
+
return outcome.value;
|
|
122
|
+
}
|
|
123
|
+
function countLlmUsage(metrics, llm) {
|
|
124
|
+
metrics.llmTokens.inc({ model: llm.model, direction: "input" }, llm.inputTokens);
|
|
125
|
+
metrics.llmTokens.inc({ model: llm.model, direction: "output" }, llm.outputTokens);
|
|
126
|
+
if (llm.costUsd !== undefined)
|
|
127
|
+
metrics.llmCostUsd.inc({ model: llm.model }, llm.costUsd);
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=report-step.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"report-step.js","sourceRoot":"","sources":["../../src/services/report-step.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAC9F,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE7E,OAAO,EAAE,UAAU,EAAiC,MAAM,kBAAkB,CAAC;AAG7E,OAAO,EAA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,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC;IACjF,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC;IACnF,IAAI,GAAG,CAAC,OAAO,KAAK,SAAS;QAAE,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;AAC3F,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { removeWatch, renewWatches } from "../store/watches.js";
|
|
2
|
+
/** How long a watch lasts unless renewed, and how often this engine renews its own. */
|
|
3
|
+
export const WATCH_TTL_MS = 30_000;
|
|
4
|
+
export const WATCH_RENEW_MS = 10_000;
|
|
5
|
+
/**
|
|
6
|
+
* The runs this engine's event streams follow, kept in run_watches so every engine knows
|
|
7
|
+
* whose tokens to forward. Counted per run, so two viewers of one run on this engine
|
|
8
|
+
* share a row, which goes when the last of them leaves.
|
|
9
|
+
*/
|
|
10
|
+
export class RunWatches {
|
|
11
|
+
sql;
|
|
12
|
+
engineId;
|
|
13
|
+
logger;
|
|
14
|
+
#viewers = new Map();
|
|
15
|
+
constructor(sql, engineId, logger) {
|
|
16
|
+
this.sql = sql;
|
|
17
|
+
this.engineId = engineId;
|
|
18
|
+
this.logger = logger;
|
|
19
|
+
}
|
|
20
|
+
/** Starts following a run; resolves once the watch is recorded. Returns the release. */
|
|
21
|
+
async watch(runId) {
|
|
22
|
+
this.#viewers.set(runId, (this.#viewers.get(runId) ?? 0) + 1);
|
|
23
|
+
await renewWatches(this.sql, this.engineId, [runId], WATCH_TTL_MS);
|
|
24
|
+
let released = false;
|
|
25
|
+
return () => {
|
|
26
|
+
if (released)
|
|
27
|
+
return;
|
|
28
|
+
released = true;
|
|
29
|
+
const left = (this.#viewers.get(runId) ?? 1) - 1;
|
|
30
|
+
if (left > 0) {
|
|
31
|
+
this.#viewers.set(runId, left);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.#viewers.delete(runId);
|
|
35
|
+
void removeWatch(this.sql, this.engineId, runId).catch((error) => {
|
|
36
|
+
// It expires on its own within the TTL.
|
|
37
|
+
this.logger.debug({ err: error, runId }, "could not remove a run watch");
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Renews every watch this engine holds, in one statement. */
|
|
42
|
+
renew() {
|
|
43
|
+
return renewWatches(this.sql, this.engineId, [...this.#viewers.keys()], WATCH_TTL_MS);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=run-watches.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-watches.js","sourceRoot":"","sources":["../../src/services/run-watches.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEhE,uFAAuF;AACvF,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC;AACnC,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC;AAErC;;;;GAIG;AACH,MAAM,OAAO,UAAU;IAIF;IACR;IACQ;IALV,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IAE9C,YACmB,GAAQ,EAChB,QAAgB,EACR,MAAc;QAFd,QAAG,GAAH,GAAG,CAAK;QAChB,aAAQ,GAAR,QAAQ,CAAQ;QACR,WAAM,GAAN,MAAM,CAAQ;IAC9B,CAAC;IAEJ,wFAAwF;IACxF,KAAK,CAAC,KAAK,CAAC,KAAa;QACvB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAC9D,MAAM,YAAY,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,EAAE,YAAY,CAAC,CAAC;QACnE,IAAI,QAAQ,GAAG,KAAK,CAAC;QACrB,OAAO,GAAG,EAAE;YACV,IAAI,QAAQ;gBAAE,OAAO;YACrB,QAAQ,GAAG,IAAI,CAAC;YAChB,MAAM,IAAI,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;YACjD,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;gBACb,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;gBAC/B,OAAO;YACT,CAAC;YACD,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC5B,KAAK,WAAW,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;gBACxE,wCAAwC;gBACxC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,8BAA8B,CAAC,CAAC;YAC3E,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;IACJ,CAAC;IAED,8DAA8D;IAC9D,KAAK;QACH,OAAO,YAAY,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,EAAE,YAAY,CAAC,CAAC;IACxF,CAAC;CACF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { insertEvent, lockEventName, resolveWaits } from "../store/events.js";
|
|
2
|
+
import { withTransaction } from "../store/transaction.js";
|
|
3
|
+
/**
|
|
4
|
+
* Delivers an event (F9). In one transaction under the event name's lock: keeps the
|
|
5
|
+
* event, so a run that starts waiting later still finds it, and ends every wait it matches
|
|
6
|
+
* now, recording each wait's step with the payload and queueing its run.
|
|
7
|
+
*/
|
|
8
|
+
export async function sendEvent(sql, command) {
|
|
9
|
+
return withTransaction(sql, async (tx) => {
|
|
10
|
+
await lockEventName(tx, command.tenantId, command.name);
|
|
11
|
+
const eventId = await insertEvent(tx, command);
|
|
12
|
+
const resumedRunIds = await resolveWaits(tx, { id: eventId, ...command });
|
|
13
|
+
return { eventId, resumedRunIds };
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=send-event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send-event.js","sourceRoot":"","sources":["../../src/services/send-event.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAC9E,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAe1D;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,GAAQ,EAAE,OAAyB;IACjE,OAAO,eAAe,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE;QACvC,MAAM,aAAa,CAAC,EAAE,EAAE,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;QACxD,MAAM,OAAO,GAAG,MAAM,WAAW,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;QAC/C,MAAM,aAAa,GAAG,MAAM,YAAY,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;QAC1E,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,CAAC;IACpC,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { LeaseLostError } from "../domain/errors.js";
|
|
2
|
+
import { markRunParked } from "../store/runs.js";
|
|
3
|
+
import { insertStep } from "../store/steps.js";
|
|
4
|
+
import { deleteTask } from "../store/tasks.js";
|
|
5
|
+
import { withTransaction } from "../store/transaction.js";
|
|
6
|
+
import { insertSleep } from "../store/waits.js";
|
|
7
|
+
import { lockForNextStep } from "./next-step.js";
|
|
8
|
+
/**
|
|
9
|
+
* Starts a sleep (F8). In one transaction (invariant 3): fences the lease, records the sleep
|
|
10
|
+
* as the run's next step, gives up the task, writes the wake time and moves the run to
|
|
11
|
+
* sleeping. A sleeping run so holds no worker, lease or connection; the waker queues it
|
|
12
|
+
* again when the time comes, and the next worker replays the sleep from the journal.
|
|
13
|
+
*
|
|
14
|
+
* Not idempotent, like a failure report: the first copy removes the task, so a resent copy
|
|
15
|
+
* gets LEASE_LOST. A sleep at a recorded position is refused: the worker must replay it.
|
|
16
|
+
*/
|
|
17
|
+
export async function sleepRun(deps, command) {
|
|
18
|
+
try {
|
|
19
|
+
const locked = await withTransaction(deps.sql, async (tx) => {
|
|
20
|
+
const locked = await lockForNextStep(tx, command);
|
|
21
|
+
const { runId } = locked;
|
|
22
|
+
await insertStep(tx, {
|
|
23
|
+
runId,
|
|
24
|
+
kind: "sleep",
|
|
25
|
+
seq: command.seq,
|
|
26
|
+
name: command.name,
|
|
27
|
+
output: null,
|
|
28
|
+
durationMs: 0,
|
|
29
|
+
});
|
|
30
|
+
await deleteTask(tx, command.taskId);
|
|
31
|
+
await insertSleep(tx, { ...locked, seq: command.seq, durationMs: command.durationMs });
|
|
32
|
+
// A run with a task is queued or running, both of which may sleep; throwing rolls back.
|
|
33
|
+
if (!(await markRunParked(tx, runId, "sleeping"))) {
|
|
34
|
+
throw new Error(`run ${runId} has a task but cannot move to sleeping`);
|
|
35
|
+
}
|
|
36
|
+
return locked;
|
|
37
|
+
});
|
|
38
|
+
if (locked.concurrencyKey !== null)
|
|
39
|
+
deps.slotFreed?.(locked.queue);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error instanceof LeaseLostError)
|
|
43
|
+
deps.metrics.fencedCompletions.inc();
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=sleep-run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sleep-run.js","sourceRoot":"","sources":["../../src/services/sleep-run.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAsBjD;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,IAAkB,EAAE,OAAwB;IACzE,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,eAAe,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE;YAC1D,MAAM,MAAM,GAAG,MAAM,eAAe,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;YAClD,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;YACzB,MAAM,UAAU,CAAC,EAAE,EAAE;gBACnB,KAAK;gBACL,IAAI,EAAE,OAAO;gBACb,GAAG,EAAE,OAAO,CAAC,GAAG;gBAChB,IAAI,EAAE,OAAO,CAAC,IAAI;gBAClB,MAAM,EAAE,IAAI;gBACZ,UAAU,EAAE,CAAC;aACd,CAAC,CAAC;YACH,MAAM,UAAU,CAAC,EAAE,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;YACrC,MAAM,WAAW,CAAC,EAAE,EAAE,EAAE,GAAG,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;YACvF,wFAAwF;YACxF,IAAI,CAAC,CAAC,MAAM,aAAa,CAAC,EAAE,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC;gBAClD,MAAM,IAAI,KAAK,CAAC,OAAO,KAAK,yCAAyC,CAAC,CAAC;YACzE,CAAC;YACD,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC,CAAC;QACH,IAAI,MAAM,CAAC,cAAc,KAAK,IAAI;YAAE,IAAI,CAAC,SAAS,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACrE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,cAAc;YAAE,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAC,GAAG,EAAE,CAAC;QAC1E,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { InvalidArgumentError, LeaseLostError, StepMismatchError } from "../domain/errors.js";
|
|
2
|
+
import { classifyStepReport, MAX_STEPS_PER_RUN } from "../domain/journal.js";
|
|
3
|
+
import { takeTokens } from "../store/limits.js";
|
|
4
|
+
import { markRunParked, markRunRunning } from "../store/runs.js";
|
|
5
|
+
import { insertStep, journalPosition } from "../store/steps.js";
|
|
6
|
+
import { deleteTask, lockLeasedTask, recordProgress } from "../store/tasks.js";
|
|
7
|
+
import { withTransaction } from "../store/transaction.js";
|
|
8
|
+
import { insertSleep } from "../store/waits.js";
|
|
9
|
+
/**
|
|
10
|
+
* Takes a token from a key's rate limit before a step (F11), in one transaction
|
|
11
|
+
* (invariant 3): fences the lease, records step `seq` as the token taken, and either renews
|
|
12
|
+
* the lease when the token is due now, or parks the run like a sleep until it is due, so a
|
|
13
|
+
* run waiting for a rate limit holds no worker, lease or connection. Because the token and
|
|
14
|
+
* its step commit together, a replay finds the step recorded and never takes it twice.
|
|
15
|
+
*
|
|
16
|
+
* A resend of a request that was answered with "go on" finds its step recorded under the
|
|
17
|
+
* same name while the lease is still held, and is answered "go on" again; after a park the
|
|
18
|
+
* lease is gone and a resend gets LEASE_LOST, as for a sleep.
|
|
19
|
+
*/
|
|
20
|
+
export async function throttleStep(deps, command) {
|
|
21
|
+
try {
|
|
22
|
+
const { waitMs, parked } = await withTransaction(deps.sql, async (tx) => {
|
|
23
|
+
const locked = await lockLeasedTask(tx, {
|
|
24
|
+
taskId: command.taskId,
|
|
25
|
+
owner: command.workerId,
|
|
26
|
+
attempt: command.attempt,
|
|
27
|
+
});
|
|
28
|
+
if (locked === undefined)
|
|
29
|
+
throw new LeaseLostError();
|
|
30
|
+
const { runId } = locked;
|
|
31
|
+
const outcome = classifyStepReport(await journalPosition(tx, runId, command.seq), command);
|
|
32
|
+
switch (outcome.kind) {
|
|
33
|
+
case "duplicate":
|
|
34
|
+
return { waitMs: 0, parked: undefined };
|
|
35
|
+
case "mismatch":
|
|
36
|
+
throw new StepMismatchError(command.seq);
|
|
37
|
+
case "gap":
|
|
38
|
+
throw new InvalidArgumentError(`seq ${String(command.seq)} skips ahead: expected step ${String(outcome.expected)}`);
|
|
39
|
+
case "too_many_steps":
|
|
40
|
+
throw new InvalidArgumentError(`a run may record at most ${String(MAX_STEPS_PER_RUN)} steps`);
|
|
41
|
+
case "record":
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
const waitMs = await takeTokens(tx, command.key, command.cost);
|
|
45
|
+
await insertStep(tx, {
|
|
46
|
+
runId,
|
|
47
|
+
kind: "rate",
|
|
48
|
+
seq: command.seq,
|
|
49
|
+
name: command.name,
|
|
50
|
+
output: { key: command.key, waitMs },
|
|
51
|
+
durationMs: 0,
|
|
52
|
+
});
|
|
53
|
+
if (waitMs === 0) {
|
|
54
|
+
await markRunRunning(tx, runId);
|
|
55
|
+
await recordProgress(tx, command.taskId, deps.leaseMs);
|
|
56
|
+
return { waitMs, parked: undefined };
|
|
57
|
+
}
|
|
58
|
+
// Parked like a sleep: the waker queues the run again once the token is due.
|
|
59
|
+
await deleteTask(tx, command.taskId);
|
|
60
|
+
await insertSleep(tx, { ...locked, seq: command.seq, durationMs: waitMs });
|
|
61
|
+
if (!(await markRunParked(tx, runId, "sleeping"))) {
|
|
62
|
+
throw new Error(`run ${runId} has a task but cannot move to sleeping`);
|
|
63
|
+
}
|
|
64
|
+
return { waitMs, parked: locked };
|
|
65
|
+
});
|
|
66
|
+
if (parked !== undefined && parked.concurrencyKey !== null)
|
|
67
|
+
deps.slotFreed?.(parked.queue);
|
|
68
|
+
return { waitMs };
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error instanceof LeaseLostError)
|
|
72
|
+
deps.metrics.fencedCompletions.inc();
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=throttle-step.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle-step.js","sourceRoot":"","sources":["../../src/services/throttle-step.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAC9F,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE7E,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAC/E,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAgChD;;;;;;;;;;GAUG;AACH,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