Skip to content

Commit dc9f73c

Browse files
committed
🔌 Share one authoritative connection between storage and lifecycle
Lifecycle transitions write to the same databases storage opens, and one authoritative connection per database is a storage invariant rather than a convenience: the DOFS layer caches against it and effect transactions run serially on it. Two installations opening their own writer would have given one run two authorities. Whichever installs first creates the registry; the other reaches the same one. It is not an authority boundary — what may advance a run is the executor lease — it only decides which connection the work happens on.
1 parent 05d5039 commit dc9f73c

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎packages/workflow/src/deno/connections.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { DatabaseSync } from "node:sqlite";
22
import { resolve } from "node:path";
3+
import { createContext, ensure } from "effection";
4+
import type { Context } from "effection";
35
import type { WorkflowRunDatabase, WorkflowRunTransaction } from "../storage/api.ts";
46
import {
57
establishJournalProvenance,
@@ -26,6 +28,37 @@ import {
2628
type SavepointTransaction,
2729
} from "./savepoints.ts";
2830

31+
/**
32+
* The connections this host owns, shared by everything installed beside them.
33+
*
34+
* One authoritative connection per database is a storage invariant, not a
35+
* convenience: the DOFS layer caches against it and effect transactions run
36+
* serially on it. Storage and lifecycle are two installations over the same
37+
* files, so whichever installs first creates the registry and the other reaches
38+
* the same one rather than opening a second writer.
39+
*
40+
* Not an authority boundary. What may advance a run is the executor lease; this
41+
* only decides which connection the work happens on.
42+
*/
43+
const RunConnections: Context<WorkflowRunConnections> = createContext<WorkflowRunConnections>(
44+
"executablemd.workflow.deno.connections",
45+
);
46+
47+
export function* useRunConnections(
48+
observeSavepoint: SavepointObserver = () => {},
49+
hooks: WorkflowRunConnectionHooks = {},
50+
): Operation<WorkflowRunConnections> {
51+
const existing = yield* RunConnections.get();
52+
if (existing !== undefined) {
53+
return existing;
54+
}
55+
const connections = createWorkflowRunConnections(observeSavepoint, hooks);
56+
yield* ensure(() => {
57+
connections.close();
58+
});
59+
return yield* RunConnections.set(connections);
60+
}
61+
2962
export class ConnectionGeneration {
3063
#opaque = undefined;
3164
}

‎packages/workflow/src/deno/provider.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ import {
5858
import { canonicalJson, parseRunId, type WorkflowRunRecord } from "../storage/record.ts";
5959
import { openWorkflowRunDatabase, readRunRow } from "./database.ts";
6060
import {
61-
createWorkflowRunConnections,
6261
type RunConnection,
6362
type RunTransaction,
63+
useRunConnections,
6464
type WorkflowRunConnections,
6565
} from "./connections.ts";
6666
import { workflowRunPath } from "./path.ts";
@@ -125,10 +125,10 @@ export function* installWorkflowRunStorage(
125125
internal: PrivateWorkspaceOptions,
126126
): Operation<void> {
127127
const root = authorizedRoot(options.root);
128-
const connections = createWorkflowRunConnections(yield* SavepointObservation.get());
129-
yield* ensure(() => {
130-
connections.close();
131-
});
128+
// Shared rather than created here: the lifecycle installation writes to the
129+
// same databases, and two authoritative connections to one file is exactly
130+
// what the connection registry exists to prevent.
131+
const connections = yield* useRunConnections(yield* SavepointObservation.get());
132132
yield* useJournalRouting(connections);
133133
yield* usePrivateWorkspace(connections, internal);
134134
yield* useWorkspaceEffects(connections);

0 commit comments

Comments
 (0)