@swirls/looms 0.11.1 → 0.11.3
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/CHANGELOG.md +12 -0
- package/dist/agent/debugger/summarize.js +2 -0
- package/dist/agent/effects.d.ts +268 -256
- package/dist/agent/effects.js +12 -18
- package/dist/agent/events.d.ts +10 -0
- package/dist/agent/events.js +6 -0
- package/dist/agent/module.d.ts +28 -0
- package/dist/agent/scope.d.ts +8 -0
- package/dist/agent/threads.js +35 -8
- package/dist/agent/types.d.ts +10 -0
- package/dist/cloudflare/durable-object.js +16 -8
- package/dist/debugger/app/assets/{index-BPu-NBr5.js → index-D13zm4x1.js} +2 -2
- package/dist/debugger/app/assets/{routes-FLQryDcz.js → routes-BrDGfYv0.js} +2 -2
- package/dist/debugger/app/index.html +1 -1
- package/dist/projectors/delivery.js +12 -2
- package/dist/projectors/event-log.d.ts +7 -1
- package/dist/projectors/event-log.js +26 -12
- package/dist/projectors/postgres.d.ts +9 -1
- package/dist/projectors/postgres.js +25 -2
- package/dist/projectors/replica/migrations.d.ts +14 -1
- package/dist/projectors/replica/migrations.js +25 -3
- package/dist/projectors/replica/postgres.d.ts +4 -2
- package/dist/projectors/replica/postgres.js +9 -6
- package/package.json +1 -1
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
6
|
<title>Looms debugger</title>
|
|
7
|
-
<script type="module" crossorigin src="./assets/index-
|
|
7
|
+
<script type="module" crossorigin src="./assets/index-D13zm4x1.js"></script>
|
|
8
8
|
<link rel="modulepreload" crossorigin href="./assets/rolldown-runtime-CbXtAM7H.js">
|
|
9
9
|
<link rel="stylesheet" crossorigin href="./assets/index-CO5gIvV8.css">
|
|
10
10
|
</head>
|
|
@@ -90,6 +90,16 @@ export function createProjectorDelivery(source, projectors, cursors, options = {
|
|
|
90
90
|
(!force && status.nextRetryAt !== null && status.nextRetryAt > currentTime)) {
|
|
91
91
|
return;
|
|
92
92
|
}
|
|
93
|
+
// Skip projector.init (often Hyperdrive/Postgres DDL) when there is nothing
|
|
94
|
+
// to project. Brand-new runs and caught-up deliveries stay off the critical
|
|
95
|
+
// path; projectors with `position` still need init to align the cursor.
|
|
96
|
+
const sourceTail = yield* source.tail(runId).pipe(Effect.orElseSucceed(() => 0));
|
|
97
|
+
if (sourceTail === 0) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (sourceTail <= status.cursor && projector.position === undefined) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
93
103
|
const initialization = yield* Effect.tryPromise({
|
|
94
104
|
try: () => initialize(projector),
|
|
95
105
|
catch: normalizeError,
|
|
@@ -139,14 +149,14 @@ export function createProjectorDelivery(source, projectors, cursors, options = {
|
|
|
139
149
|
nextRetryAt: null,
|
|
140
150
|
lastError: null,
|
|
141
151
|
};
|
|
142
|
-
const
|
|
152
|
+
const lagTail = yield* source.tail(runId).pipe(Effect.orElseSucceed(() => last.seq));
|
|
143
153
|
observe({
|
|
144
154
|
type: 'projector.lag',
|
|
145
155
|
runId,
|
|
146
156
|
projector: projectorIdentity(projector),
|
|
147
157
|
at: advancedAt,
|
|
148
158
|
cursor: last.seq,
|
|
149
|
-
lag: Math.max(0,
|
|
159
|
+
lag: Math.max(0, lagTail - last.seq),
|
|
150
160
|
attempts: 0,
|
|
151
161
|
});
|
|
152
162
|
}
|
|
@@ -3,7 +3,7 @@ import { type EventEnvelope, type EventStore, type RunAttributeFields } from '@s
|
|
|
3
3
|
import { eventLogOps, type EventLogEntry, type EventLogOp, type RunLogRow } from './event-log-ops';
|
|
4
4
|
import type { Projector } from './projector';
|
|
5
5
|
import { type ResolvedReplicaColumn } from './replica/columns';
|
|
6
|
-
import { POSTGRES_EVENT_LOG_SCHEMA_SQL } from './replica/migrations';
|
|
6
|
+
import { POSTGRES_EVENT_LOG_SCHEMA_SQL, type PostgresReplicaMigrate } from './replica/migrations';
|
|
7
7
|
export { POSTGRES_EVENT_LOG_SCHEMA_SQL, eventLogOps };
|
|
8
8
|
export type { EventLogEntry, EventLogOp, RunLogRow };
|
|
9
9
|
export interface EventLogProjector extends Projector {
|
|
@@ -25,6 +25,12 @@ export type PostgresEventLogOptions = ({
|
|
|
25
25
|
}) & {
|
|
26
26
|
/** Declared run attributes. Each one is a `looms_run_index` column. */
|
|
27
27
|
readonly additionalFields?: RunAttributeFields;
|
|
28
|
+
/**
|
|
29
|
+
* Schema ownership on init. Default `auto` applies pending versioned migrations
|
|
30
|
+
* (lock-free when nothing is pending). `verify` refuses to start when a migration
|
|
31
|
+
* is missing. `skip` leaves schema ownership to the host.
|
|
32
|
+
*/
|
|
33
|
+
readonly migrate?: PostgresReplicaMigrate;
|
|
28
34
|
};
|
|
29
35
|
export declare function postgresEventLog(options: PostgresEventLogOptions): EventLogProjector;
|
|
30
36
|
export declare function applySqlOps(sql: TransactionSql, ops: readonly EventLogOp[], columns?: readonly ResolvedReplicaColumn[]): Promise<void>;
|
|
@@ -4,7 +4,7 @@ import {} from '@swirls/looms/core';
|
|
|
4
4
|
import { eventLogOps } from './event-log-ops.js';
|
|
5
5
|
import { runProjectorPromise } from './projector.js';
|
|
6
6
|
import { columnValues, resolveReplicaColumns } from './replica/columns.js';
|
|
7
|
-
import { applyPostgresMigrations, POSTGRES_EVENT_LOG_SCHEMA_SQL,
|
|
7
|
+
import { applyPostgresMigrations, POSTGRES_EVENT_LOG_SCHEMA_SQL, postgresReplicaMigrations, verifyPostgresMigrations, } from './replica/migrations.js';
|
|
8
8
|
export { POSTGRES_EVENT_LOG_SCHEMA_SQL, eventLogOps };
|
|
9
9
|
const terminal = new Set(['completed', 'failed', 'cancelled']);
|
|
10
10
|
export function memoryEventLog() {
|
|
@@ -35,7 +35,7 @@ export function memoryEventLog() {
|
|
|
35
35
|
export function postgresEventLog(options) {
|
|
36
36
|
const created = 'url' in options;
|
|
37
37
|
const connected = created ? createPostgresClient(options.url) : options.sql;
|
|
38
|
-
return createEventLogProjector('postgres-event-log', postgresStore(connected, created, options.additionalFields));
|
|
38
|
+
return createEventLogProjector('postgres-event-log', postgresStore(connected, created, options.additionalFields, options.migrate ?? 'auto'));
|
|
39
39
|
}
|
|
40
40
|
function createEventLogProjector(name, store) {
|
|
41
41
|
return {
|
|
@@ -134,18 +134,32 @@ function mergeFinish(existing, op) {
|
|
|
134
134
|
function utcDate(ms) {
|
|
135
135
|
return DateTime.toDateUtc(DateTime.makeUnsafe(ms));
|
|
136
136
|
}
|
|
137
|
-
|
|
138
|
-
const POSTGRES_EVENT_LOG_INIT_SQL = [
|
|
139
|
-
POSTGRES_EVENT_LOG_SCHEMA_SQL,
|
|
140
|
-
POSTGRES_SCHEMA_MIGRATIONS_SQL,
|
|
141
|
-
].join('\n\n');
|
|
142
|
-
function postgresStore(sql, ownsClient, additionalFields) {
|
|
137
|
+
function postgresStore(sql, ownsClient, additionalFields, migrate) {
|
|
143
138
|
const columns = resolveReplicaColumns(additionalFields);
|
|
144
|
-
const
|
|
139
|
+
const migrations = postgresReplicaMigrations(additionalFields);
|
|
140
|
+
let ready;
|
|
141
|
+
const initSchema = () => {
|
|
142
|
+
switch (migrate) {
|
|
143
|
+
case 'skip':
|
|
144
|
+
return Promise.resolve();
|
|
145
|
+
case 'verify':
|
|
146
|
+
return verifyPostgresMigrations(sql, migrations);
|
|
147
|
+
case 'auto':
|
|
148
|
+
return applyPostgresMigrations(sql, migrations);
|
|
149
|
+
default: {
|
|
150
|
+
const exhaustive = migrate;
|
|
151
|
+
return exhaustive;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
145
155
|
return {
|
|
146
|
-
init: () =>
|
|
147
|
-
.
|
|
148
|
-
|
|
156
|
+
init: () => {
|
|
157
|
+
ready ??= runProjectorPromise('postgres-event-log', initSchema).catch((cause) => {
|
|
158
|
+
ready = undefined;
|
|
159
|
+
throw cause;
|
|
160
|
+
});
|
|
161
|
+
return ready;
|
|
162
|
+
},
|
|
149
163
|
apply: (events) => {
|
|
150
164
|
const ops = events.flatMap(eventLogOps);
|
|
151
165
|
if (ops.length === 0) {
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import { type Sql } from 'postgres';
|
|
2
2
|
import { type IndexOp, type IndexProjector } from './index-model';
|
|
3
|
+
import { type PostgresReplicaMigrate } from './replica/migrations';
|
|
3
4
|
export { POSTGRES_SCHEMA_SQL } from './schema';
|
|
4
5
|
export { memoryEventLog, postgresEventLog, POSTGRES_EVENT_LOG_SCHEMA_SQL, type EventLogEntry, type EventLogProjector, type MemoryEventLog, type RunLogRow, } from './event-log';
|
|
5
|
-
export type PostgresProjectorOptions = {
|
|
6
|
+
export type PostgresProjectorOptions = ({
|
|
6
7
|
url: string;
|
|
7
8
|
} | {
|
|
8
9
|
sql: Sql;
|
|
10
|
+
}) & {
|
|
11
|
+
/**
|
|
12
|
+
* Schema ownership on init. Default `auto` applies pending versioned migrations
|
|
13
|
+
* (lock-free when nothing is pending). `verify` refuses to start when a migration
|
|
14
|
+
* is missing. `skip` leaves schema ownership to the host.
|
|
15
|
+
*/
|
|
16
|
+
readonly migrate?: PostgresReplicaMigrate;
|
|
9
17
|
};
|
|
10
18
|
type SqlTag = (strings: TemplateStringsArray, ...values: readonly any[]) => PromiseLike<void>;
|
|
11
19
|
/** Apply actor and review index operations on an existing connection or transaction. */
|
|
@@ -2,7 +2,7 @@ import { DateTime } from 'effect';
|
|
|
2
2
|
import createPostgresClient, {} from 'postgres';
|
|
3
3
|
import { createIndexProjector, } from './index-model.js';
|
|
4
4
|
import { runProjectorPromise } from './projector.js';
|
|
5
|
-
import {
|
|
5
|
+
import { applyPostgresMigrations, postgresReplicaMigrations, verifyPostgresMigrations, } from './replica/migrations.js';
|
|
6
6
|
export { POSTGRES_SCHEMA_SQL } from './schema.js';
|
|
7
7
|
export { memoryEventLog, postgresEventLog, POSTGRES_EVENT_LOG_SCHEMA_SQL, } from './event-log.js';
|
|
8
8
|
function ownsClient(options) {
|
|
@@ -79,8 +79,31 @@ export function applyIndexOps(sql, ops) {
|
|
|
79
79
|
export function postgres(options) {
|
|
80
80
|
const createdClient = ownsClient(options);
|
|
81
81
|
const sql = createdClient ? createPostgresClient(options.url) : options.sql;
|
|
82
|
+
const migrate = options.migrate ?? 'auto';
|
|
83
|
+
const migrations = postgresReplicaMigrations();
|
|
84
|
+
let ready;
|
|
85
|
+
const initSchema = () => {
|
|
86
|
+
switch (migrate) {
|
|
87
|
+
case 'skip':
|
|
88
|
+
return Promise.resolve();
|
|
89
|
+
case 'verify':
|
|
90
|
+
return verifyPostgresMigrations(sql, migrations);
|
|
91
|
+
case 'auto':
|
|
92
|
+
return applyPostgresMigrations(sql, migrations);
|
|
93
|
+
default: {
|
|
94
|
+
const exhaustive = migrate;
|
|
95
|
+
return exhaustive;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
82
99
|
const backend = {
|
|
83
|
-
init: () =>
|
|
100
|
+
init: () => {
|
|
101
|
+
ready ??= runProjectorPromise('postgres', initSchema).catch((cause) => {
|
|
102
|
+
ready = undefined;
|
|
103
|
+
throw cause;
|
|
104
|
+
});
|
|
105
|
+
return ready;
|
|
106
|
+
},
|
|
84
107
|
applyOps: (ops) => runProjectorPromise('postgres', () => sql.begin((tx) => applyIndexOps(tx, ops))).then(() => undefined),
|
|
85
108
|
getActor: (runId, actorId) => runProjectorPromise('postgres', () => sql `
|
|
86
109
|
SELECT * FROM looms_run_actors WHERE run_id = ${runId} AND actor_id = ${actorId}
|
|
@@ -2,6 +2,13 @@ import type { Sql, TransactionSql } from 'postgres';
|
|
|
2
2
|
import type { RunAttributeFields } from '@swirls/looms/core';
|
|
3
3
|
import { type ReplicaMigration, type ResolvedReplicaColumn } from './columns';
|
|
4
4
|
export type { ReplicaMigration };
|
|
5
|
+
/**
|
|
6
|
+
* Schema ownership for Postgres projectors and the replica.
|
|
7
|
+
* `auto` applies pending migrations on init (lock-free when nothing is pending).
|
|
8
|
+
* `verify` refuses to start when a migration has not been applied.
|
|
9
|
+
* `skip` leaves schema ownership to the host.
|
|
10
|
+
*/
|
|
11
|
+
export type PostgresReplicaMigrate = 'auto' | 'verify' | 'skip';
|
|
5
12
|
/**
|
|
6
13
|
* Actors and reviews. Same statements the Postgres index projector has always
|
|
7
14
|
* applied, so a database created by an older Looms keeps working.
|
|
@@ -28,8 +35,14 @@ export declare function postgresColumnIndexSql(column: ResolvedReplicaColumn): s
|
|
|
28
35
|
export declare function postgresAttributeMigrations(additionalFields?: RunAttributeFields): readonly ReplicaMigration[];
|
|
29
36
|
/** Ids recorded in `looms_schema_migrations`, or none when the table does not exist yet. */
|
|
30
37
|
export declare function appliedPostgresMigrationIds(sql: Sql | TransactionSql): Promise<ReadonlySet<string>>;
|
|
31
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* Applies each migration that is not recorded yet, in order, under one advisory lock.
|
|
40
|
+
* When every migration is already recorded, returns after a lock-free read so cold
|
|
41
|
+
* Durable Object inits do not contend on DDL or the global migration lock.
|
|
42
|
+
*/
|
|
32
43
|
export declare function applyPostgresMigrations(sql: Sql, migrations: readonly ReplicaMigration[]): Promise<void>;
|
|
44
|
+
/** Throws when any migration has not been recorded yet. */
|
|
45
|
+
export declare function verifyPostgresMigrations(sql: Sql, migrations: readonly ReplicaMigration[]): Promise<void>;
|
|
33
46
|
/** Versioned DDL for the Postgres replica with no attribute columns. Looms only creates and alters `looms_*` objects. */
|
|
34
47
|
export declare const POSTGRES_REPLICA_MIGRATIONS: readonly ReplicaMigration[];
|
|
35
48
|
export declare function postgresReplicaMigrations(additionalFields?: RunAttributeFields): readonly ReplicaMigration[];
|
|
@@ -143,18 +143,40 @@ export async function appliedPostgresMigrationIds(sql) {
|
|
|
143
143
|
const rows = await sql `SELECT id FROM looms_schema_migrations`;
|
|
144
144
|
return new Set(rows.map((row) => row.id));
|
|
145
145
|
}
|
|
146
|
-
/**
|
|
146
|
+
/**
|
|
147
|
+
* Applies each migration that is not recorded yet, in order, under one advisory lock.
|
|
148
|
+
* When every migration is already recorded, returns after a lock-free read so cold
|
|
149
|
+
* Durable Object inits do not contend on DDL or the global migration lock.
|
|
150
|
+
*/
|
|
147
151
|
export async function applyPostgresMigrations(sql, migrations) {
|
|
152
|
+
if (migrations.length === 0) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const applied = await appliedPostgresMigrationIds(sql);
|
|
156
|
+
const pending = migrations.some((migration) => !applied.has(migration.id));
|
|
157
|
+
if (!pending) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
148
160
|
await sql.begin(async (tx) => {
|
|
149
161
|
await tx `SELECT pg_advisory_xact_lock(${MIGRATION_LOCK_KEY})`;
|
|
150
|
-
const
|
|
162
|
+
const appliedUnderLock = await appliedPostgresMigrationIds(tx);
|
|
151
163
|
for (const migration of migrations) {
|
|
152
|
-
if (!
|
|
164
|
+
if (!appliedUnderLock.has(migration.id)) {
|
|
153
165
|
await tx.unsafe(migration.sql);
|
|
154
166
|
}
|
|
155
167
|
}
|
|
156
168
|
});
|
|
157
169
|
}
|
|
170
|
+
/** Throws when any migration has not been recorded yet. */
|
|
171
|
+
export async function verifyPostgresMigrations(sql, migrations) {
|
|
172
|
+
const applied = await appliedPostgresMigrationIds(sql);
|
|
173
|
+
const pending = migrations
|
|
174
|
+
.filter((migration) => !applied.has(migration.id))
|
|
175
|
+
.map((migration) => migration.id);
|
|
176
|
+
if (pending.length > 0) {
|
|
177
|
+
throw new Error(`Looms replica is missing migrations: ${pending.join(', ')}. Generate them with \`looms replica generate\` and apply the files before starting.`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
158
180
|
/** Versioned DDL for the Postgres replica with no attribute columns. Looms only creates and alters `looms_*` objects. */
|
|
159
181
|
export const POSTGRES_REPLICA_MIGRATIONS = [
|
|
160
182
|
replicaMigration('0001_replica', [
|
|
@@ -2,6 +2,8 @@ import { type Sql } from 'postgres';
|
|
|
2
2
|
import { type EventStore, type FoldRegistry, type RunAttributeFields, type RunState } from '@swirls/looms/core';
|
|
3
3
|
import { ReplicaGapError, type Projector } from '../projector';
|
|
4
4
|
import { type NoAdditionalFields, type ReplicaPurgeOptions } from './columns';
|
|
5
|
+
import { type PostgresReplicaMigrate } from './migrations';
|
|
6
|
+
export type { PostgresReplicaMigrate };
|
|
5
7
|
export type { NoAdditionalFields, ReplicaPurgeOptions } from './columns';
|
|
6
8
|
export interface ReplicaStatus {
|
|
7
9
|
/** Highest sequence copied with no hole in front of it. */
|
|
@@ -16,16 +18,16 @@ export interface ReplicaStatus {
|
|
|
16
18
|
export interface ReplicaPurgeResult {
|
|
17
19
|
readonly runs: number;
|
|
18
20
|
}
|
|
19
|
-
export type PostgresReplicaMigrate = 'auto' | 'verify';
|
|
20
21
|
export type PostgresReplicaOptions<Fields extends RunAttributeFields = NoAdditionalFields> = ({
|
|
21
22
|
readonly url: string;
|
|
22
23
|
} | {
|
|
23
24
|
readonly sql: Sql;
|
|
24
25
|
}) & {
|
|
25
26
|
/**
|
|
26
|
-
* `auto` applies pending migrations on init.
|
|
27
|
+
* `auto` applies pending migrations on init (lock-free when nothing is pending).
|
|
27
28
|
* `verify` refuses to start when a migration has not been applied, and names
|
|
28
29
|
* `looms replica generate` so the host can commit the SQL.
|
|
30
|
+
* `skip` leaves schema ownership to the host (for example drizzle migrations).
|
|
29
31
|
*/
|
|
30
32
|
readonly migrate: PostgresReplicaMigrate;
|
|
31
33
|
/**
|
|
@@ -115,19 +115,22 @@ export function postgresReplica(options) {
|
|
|
115
115
|
.map((migration) => migration.id);
|
|
116
116
|
};
|
|
117
117
|
const migrate = () => applyPostgresMigrations(sql, migrations);
|
|
118
|
+
const verify = async () => {
|
|
119
|
+
const pending = await pendingMigrations();
|
|
120
|
+
if (pending.length > 0) {
|
|
121
|
+
throw new Error(`Looms replica is missing migrations: ${pending.join(', ')}. Generate them with \`looms replica generate\` and apply the files before starting.`);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
118
124
|
const init = () => {
|
|
125
|
+
if (options.migrate === 'skip') {
|
|
126
|
+
return Promise.resolve();
|
|
127
|
+
}
|
|
119
128
|
ready ??= (options.migrate === 'verify' ? verify() : migrate()).catch((cause) => {
|
|
120
129
|
ready = undefined;
|
|
121
130
|
throw cause;
|
|
122
131
|
});
|
|
123
132
|
return ready;
|
|
124
133
|
};
|
|
125
|
-
const verify = async () => {
|
|
126
|
-
const pending = await pendingMigrations();
|
|
127
|
-
if (pending.length > 0) {
|
|
128
|
-
throw new Error(`Looms replica is missing migrations: ${pending.join(', ')}. Generate them with \`looms replica generate\` and apply the files before starting.`);
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
134
|
const project = async (events) => {
|
|
132
135
|
await init();
|
|
133
136
|
if (events.length === 0) {
|