lastlight-core 0.25.8 → 0.26.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/config/default.yaml +31 -6
- package/dist/admin/chat-session-reader.d.ts +3 -3
- package/dist/admin/chat-session-reader.js +8 -8
- package/dist/admin/chat-session-reader.js.map +1 -1
- package/dist/admin/routes.js +119 -124
- package/dist/admin/routes.js.map +1 -1
- package/dist/admin/sessions.d.ts +7 -7
- package/dist/admin/sessions.js +6 -4
- package/dist/admin/sessions.js.map +1 -1
- package/dist/config/config.d.ts +21 -2
- package/dist/config/config.js +33 -0
- package/dist/config/config.js.map +1 -1
- package/dist/config/repo-config.js +6 -7
- package/dist/config/repo-config.js.map +1 -1
- package/dist/connectors/messaging/base.js +2 -2
- package/dist/connectors/messaging/base.js.map +1 -1
- package/dist/connectors/messaging/session-manager.d.ts +29 -31
- package/dist/connectors/messaging/session-manager.js +156 -189
- package/dist/connectors/messaging/session-manager.js.map +1 -1
- package/dist/connectors/messaging/thread-transcript.d.ts +2 -2
- package/dist/connectors/messaging/thread-transcript.js +14 -8
- package/dist/connectors/messaging/thread-transcript.js.map +1 -1
- package/dist/connectors/slack/connector.js +5 -5
- package/dist/connectors/slack/connector.js.map +1 -1
- package/dist/connectors/slack/mrkdwn.d.ts +4 -0
- package/dist/connectors/slack/mrkdwn.js +2 -2
- package/dist/connectors/slack/mrkdwn.js.map +1 -1
- package/dist/cron/feedback-poll.js +6 -6
- package/dist/cron/feedback-poll.js.map +1 -1
- package/dist/cron/handlers.d.ts +16 -8
- package/dist/cron/handlers.js +49 -31
- package/dist/cron/handlers.js.map +1 -1
- package/dist/cron/jobs.d.ts +1 -1
- package/dist/cron/jobs.js +5 -2
- package/dist/cron/jobs.js.map +1 -1
- package/dist/cron/repo-digest.d.ts +54 -7
- package/dist/cron/repo-digest.js +236 -23
- package/dist/cron/repo-digest.js.map +1 -1
- package/dist/cron/runner.d.ts +59 -0
- package/dist/cron/runner.js +224 -0
- package/dist/cron/runner.js.map +1 -0
- package/dist/cron/scheduler.js +19 -9
- package/dist/cron/scheduler.js.map +1 -1
- package/dist/engine/chat/chat-prompt.d.ts +11 -4
- package/dist/engine/chat/chat-prompt.js.map +1 -1
- package/dist/engine/chat/chat-runner.d.ts +3 -2
- package/dist/engine/chat/chat-runner.js +9 -7
- package/dist/engine/chat/chat-runner.js.map +1 -1
- package/dist/engine/dispatcher.js +29 -25
- package/dist/engine/dispatcher.js.map +1 -1
- package/dist/engine/executors/orchestrator.d.ts +28 -0
- package/dist/engine/executors/orchestrator.js +70 -1
- package/dist/engine/executors/orchestrator.js.map +1 -1
- package/dist/engine/feedback/ingest.d.ts +4 -4
- package/dist/engine/feedback/ingest.js +13 -13
- package/dist/engine/feedback/ingest.js.map +1 -1
- package/dist/engine/feedback/slack.d.ts +2 -2
- package/dist/engine/feedback/slack.js +6 -6
- package/dist/engine/feedback/slack.js.map +1 -1
- package/dist/engine/fix-harvest.d.ts +1 -1
- package/dist/engine/fix-harvest.js +3 -3
- package/dist/engine/fix-harvest.js.map +1 -1
- package/dist/engine/github/github.d.ts +79 -0
- package/dist/engine/github/github.js +111 -0
- package/dist/engine/github/github.js.map +1 -1
- package/dist/engine/github/team-visibility.js +7 -7
- package/dist/engine/github/team-visibility.js.map +1 -1
- package/dist/engine/pr-escalation.js +7 -7
- package/dist/engine/pr-escalation.js.map +1 -1
- package/dist/engine/pr-state.d.ts +1 -1
- package/dist/engine/pr-state.js +12 -10
- package/dist/engine/pr-state.js.map +1 -1
- package/dist/engine/review-check.d.ts +1 -1
- package/dist/engine/review-check.js +5 -5
- package/dist/engine/review-check.js.map +1 -1
- package/dist/engine/router.js +3 -3
- package/dist/engine/router.js.map +1 -1
- package/dist/index.js +115 -152
- package/dist/index.js.map +1 -1
- package/dist/notify/digest-blocks.d.ts +61 -0
- package/dist/notify/digest-blocks.js +150 -22
- package/dist/notify/digest-blocks.js.map +1 -1
- package/dist/sandbox/docker.d.ts +33 -1
- package/dist/sandbox/docker.js +95 -1
- package/dist/sandbox/docker.js.map +1 -1
- package/dist/sandbox/k8s/kubernetes-sandbox.d.ts +3 -0
- package/dist/sandbox/k8s/kubernetes-sandbox.js +9 -0
- package/dist/sandbox/k8s/kubernetes-sandbox.js.map +1 -1
- package/dist/sandbox/k8s/pod.d.ts +11 -0
- package/dist/sandbox/k8s/pod.js +11 -6
- package/dist/sandbox/k8s/pod.js.map +1 -1
- package/dist/sandbox/k8s/service-containers.d.ts +43 -0
- package/dist/sandbox/k8s/service-containers.js +87 -0
- package/dist/sandbox/k8s/service-containers.js.map +1 -0
- package/dist/sandbox/reap.d.ts +9 -0
- package/dist/sandbox/reap.js +36 -0
- package/dist/sandbox/reap.js.map +1 -1
- package/dist/sandbox/sandbox.d.ts +14 -0
- package/dist/sandbox/sandbox.js +9 -0
- package/dist/sandbox/sandbox.js.map +1 -1
- package/dist/sandbox/service-containers-docker.d.ts +45 -0
- package/dist/sandbox/service-containers-docker.js +78 -0
- package/dist/sandbox/service-containers-docker.js.map +1 -0
- package/dist/state/approval-store.d.ts +23 -14
- package/dist/state/approval-store.js +137 -86
- package/dist/state/approval-store.js.map +1 -1
- package/dist/state/client.d.ts +105 -0
- package/dist/state/client.js +68 -0
- package/dist/state/client.js.map +1 -0
- package/dist/state/cron-run-store.d.ts +108 -0
- package/dist/state/cron-run-store.js +173 -0
- package/dist/state/cron-run-store.js.map +1 -0
- package/dist/state/db.d.ts +70 -23
- package/dist/state/db.js +179 -104
- package/dist/state/db.js.map +1 -1
- package/dist/state/dialect.d.ts +77 -0
- package/dist/state/dialect.js +129 -0
- package/dist/state/dialect.js.map +1 -0
- package/dist/state/execution-store.d.ts +124 -103
- package/dist/state/execution-store.js +569 -374
- package/dist/state/execution-store.js.map +1 -1
- package/dist/state/feedback-store.d.ts +20 -20
- package/dist/state/feedback-store.js +241 -187
- package/dist/state/feedback-store.js.map +1 -1
- package/dist/state/legacy-sqlite.d.ts +15 -0
- package/dist/state/legacy-sqlite.js +131 -0
- package/dist/state/legacy-sqlite.js.map +1 -0
- package/dist/state/repo-ref.d.ts +9 -4
- package/dist/state/repo-ref.js +12 -6
- package/dist/state/repo-ref.js.map +1 -1
- package/dist/state/schema/pg.d.ts +2788 -0
- package/dist/state/schema/pg.js +301 -0
- package/dist/state/schema/pg.js.map +1 -0
- package/dist/state/schema/sqlite.d.ts +3063 -0
- package/dist/state/schema/sqlite.js +391 -0
- package/dist/state/schema/sqlite.js.map +1 -0
- package/dist/state/team-store.d.ts +26 -13
- package/dist/state/team-store.js +140 -93
- package/dist/state/team-store.js.map +1 -1
- package/dist/state/user-store.d.ts +31 -13
- package/dist/state/user-store.js +180 -80
- package/dist/state/user-store.js.map +1 -1
- package/dist/state/workflow-run-store.d.ts +95 -56
- package/dist/state/workflow-run-store.js +419 -314
- package/dist/state/workflow-run-store.js.map +1 -1
- package/dist/telemetry/index.d.ts +8 -0
- package/dist/telemetry/index.js +11 -0
- package/dist/telemetry/index.js.map +1 -1
- package/dist/workflows/admission.d.ts +4 -3
- package/dist/workflows/admission.js +34 -10
- package/dist/workflows/admission.js.map +1 -1
- package/dist/workflows/handlers/post-review.js +3 -3
- package/dist/workflows/handlers/post-review.js.map +1 -1
- package/dist/workflows/resume.js +22 -15
- package/dist/workflows/resume.js.map +1 -1
- package/dist/workflows/runner.js +27 -12
- package/dist/workflows/runner.js.map +1 -1
- package/dist/workflows/simple.d.ts +23 -0
- package/dist/workflows/simple.js +40 -16
- package/dist/workflows/simple.js.map +1 -1
- package/drizzle/pg/0000_init.sql +235 -0
- package/drizzle/pg/meta/0000_snapshot.json +1751 -0
- package/drizzle/pg/meta/_journal.json +13 -0
- package/drizzle/sqlite/0000_baseline.sql +257 -0
- package/drizzle/sqlite/0001_backfill_repo_refs.sql +74 -0
- package/drizzle/sqlite/meta/0000_snapshot.json +1593 -0
- package/drizzle/sqlite/meta/0001_snapshot.json +1593 -0
- package/drizzle/sqlite/meta/_journal.json +20 -0
- package/package.json +15 -9
- package/dist/state/migrate.d.ts +0 -15
- package/dist/state/migrate.js +0 -493
- package/dist/state/migrate.js.map +0 -1
package/dist/state/db.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import { migrate } from "
|
|
1
|
+
import { createClient } from "@libsql/client";
|
|
2
|
+
import { drizzle } from "drizzle-orm/libsql";
|
|
3
|
+
import { migrate } from "drizzle-orm/libsql/migrator";
|
|
4
|
+
import { eq } from "drizzle-orm";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { resolve } from "node:path";
|
|
7
|
+
import * as sqliteSchema from "./schema/sqlite.js";
|
|
8
|
+
import { applyLegacySqliteCompat } from "./legacy-sqlite.js";
|
|
9
|
+
import { makeOpSerializer, tablesOf, } from "./client.js";
|
|
4
10
|
import { ExecutionStore } from "./execution-store.js";
|
|
11
|
+
import { CronRunStore } from "./cron-run-store.js";
|
|
5
12
|
import { ApprovalStore } from "./approval-store.js";
|
|
6
13
|
import { WorkflowRunStore } from "./workflow-run-store.js";
|
|
7
14
|
import { UserStore } from "./user-store.js";
|
|
@@ -13,21 +20,32 @@ export { WorkflowRunStore } from "./workflow-run-store.js";
|
|
|
13
20
|
export { UserStore, TRIGGER_ACTOR_TYPES, isTriggerActorType } from "./user-store.js";
|
|
14
21
|
export { TeamStore } from "./team-store.js";
|
|
15
22
|
export { FeedbackStore } from "./feedback-store.js";
|
|
23
|
+
export { CronRunStore } from "./cron-run-store.js";
|
|
16
24
|
const DEFAULT_DB_PATH = "lastlight.db";
|
|
25
|
+
/**
|
|
26
|
+
* The generated migrations live at the `apps/server/` package root, so this
|
|
27
|
+
* must resolve from BOTH `src/state/` (tsx dev) and `dist/state/` (compiled).
|
|
28
|
+
*/
|
|
29
|
+
const MIGRATIONS_DIR = fileURLToPath(new URL("../../drizzle/sqlite", import.meta.url));
|
|
17
30
|
/**
|
|
18
31
|
* Lightweight SQLite state for operational tracking.
|
|
19
32
|
* NOT for conversation history — only execution logs and rate limits.
|
|
20
33
|
*
|
|
21
|
-
* Construction root for the
|
|
22
|
-
* single
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
34
|
+
* Construction root for the seven per-table stores (issue #97): it opens the
|
|
35
|
+
* single Drizzle client, runs the compat pre-step + migrations, and builds the
|
|
36
|
+
* stores on top of it (all sharing the one client so transactions span every
|
|
37
|
+
* store). Callers reach the stores via `db.runs` / `db.approvals` /
|
|
38
|
+
* `db.executions` / …. StateDb itself retains only the cross-cutting
|
|
39
|
+
* cron-override and workflow kill-switch concerns.
|
|
40
|
+
*
|
|
41
|
+
* Construction is async — the driver is — so it is a static factory rather
|
|
42
|
+
* than a constructor: {@link StateDb.open} for production, {@link
|
|
43
|
+
* StateDb.fromClient} to adopt an existing Drizzle instance.
|
|
28
44
|
*/
|
|
29
45
|
export class StateDb {
|
|
30
|
-
|
|
46
|
+
_client;
|
|
47
|
+
_dialect;
|
|
48
|
+
closer;
|
|
31
49
|
/** Owns the `executions` ledger. */
|
|
32
50
|
executions;
|
|
33
51
|
/** Owns the `workflow_approvals` table. */
|
|
@@ -44,40 +62,107 @@ export class StateDb {
|
|
|
44
62
|
teams;
|
|
45
63
|
/** Reaction-derived eval signals + the anchors they hang on (issue #255). */
|
|
46
64
|
feedback;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.
|
|
61
|
-
this.
|
|
62
|
-
this.
|
|
65
|
+
/**
|
|
66
|
+
* One row per cron FIRE (issues #341/#327). The only record a zero-discovery
|
|
67
|
+
* backstop fire leaves — it dispatches nothing, so it writes no
|
|
68
|
+
* `workflow_runs` and no `executions` row.
|
|
69
|
+
*/
|
|
70
|
+
cronRuns;
|
|
71
|
+
/**
|
|
72
|
+
* Table objects for THIS client's dialect. Every method destructures what it
|
|
73
|
+
* needs off this instead of importing from `schema/sqlite.js` — see
|
|
74
|
+
* `client.ts` → {@link tablesOf} for why the cast alone cannot do it.
|
|
75
|
+
*/
|
|
76
|
+
t;
|
|
77
|
+
constructor(_client, _dialect, closer) {
|
|
78
|
+
this._client = _client;
|
|
79
|
+
this._dialect = _dialect;
|
|
80
|
+
this.closer = closer;
|
|
81
|
+
this.t = tablesOf(_client);
|
|
82
|
+
// ONE serializer per CONNECTION, handed to every store that opens a
|
|
83
|
+
// transaction. A per-store chain would leave WorkflowRunStore's five named
|
|
84
|
+
// ops racing TeamStore's four on this same client — see makeOpSerializer.
|
|
85
|
+
const serialize = makeOpSerializer();
|
|
86
|
+
this.executions = new ExecutionStore(_client);
|
|
87
|
+
this.approvals = new ApprovalStore(_client);
|
|
88
|
+
this.runs = new WorkflowRunStore(_client, { approvals: this.approvals, serialize });
|
|
89
|
+
this.users = new UserStore(_client);
|
|
90
|
+
this.teams = new TeamStore(_client, { serialize });
|
|
91
|
+
this.feedback = new FeedbackStore(_client);
|
|
92
|
+
this.cronRuns = new CronRunStore(_client);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Production entry: open (or create) the SQLite database, apply the legacy
|
|
96
|
+
* compat pre-step, then run migrations.
|
|
97
|
+
*
|
|
98
|
+
* Normalizes every accepted form HERE so no caller ever builds a `file:` URL:
|
|
99
|
+
* `:memory:` passes through, `file:` URLs pass through, and anything else is
|
|
100
|
+
* treated as a filesystem path.
|
|
101
|
+
*
|
|
102
|
+
* **`:memory:` is only safe for a caller that never opens a transaction.**
|
|
103
|
+
* The libsql client lazily opens a NEW connection after every
|
|
104
|
+
* `client.transaction()`, and for `:memory:` a new connection is a new, EMPTY
|
|
105
|
+
* database — so the first committed transaction silently discards the schema
|
|
106
|
+
* and every subsequent query fails with `no such table`. Verified on
|
|
107
|
+
* @libsql/client 0.17. Nothing in production uses `:memory:`; a test that
|
|
108
|
+
* touches `WorkflowRunStore`'s named ops or any `TeamStore` write must use a
|
|
109
|
+
* temp FILE instead.
|
|
110
|
+
*/
|
|
111
|
+
static async open(pathOrUrl) {
|
|
112
|
+
const input = pathOrUrl || DEFAULT_DB_PATH;
|
|
113
|
+
// Case-insensitive: URL schemes are, and `POSTGRES://…` reaching the libsql
|
|
114
|
+
// client produces an opaque ConnectionFailed instead of this message.
|
|
115
|
+
if (/^postgres(ql)?:\/\//i.test(input)) {
|
|
116
|
+
throw new Error(`PG runtime not enabled: StateDb.open() cannot open "${input}". ` +
|
|
117
|
+
`The Postgres dialect is test-only for now — construct it with StateDb.fromClient().`);
|
|
118
|
+
}
|
|
119
|
+
const url = input === ":memory:" || input.startsWith("file:") ? input : `file:${resolve(input)}`;
|
|
120
|
+
const raw = createClient({ url });
|
|
121
|
+
await raw.execute("PRAGMA journal_mode = WAL");
|
|
122
|
+
// Best-effort only: this pragma is connection-scoped and the libsql client
|
|
123
|
+
// opens a fresh connection after every transaction, so it covers just the
|
|
124
|
+
// pre-first-transaction window. The op serializer is the real defense.
|
|
125
|
+
await raw.execute("PRAGMA busy_timeout = 5000");
|
|
126
|
+
await applyLegacySqliteCompat(raw);
|
|
127
|
+
const client = drizzle(raw, { schema: sqliteSchema });
|
|
128
|
+
await migrate(client, { migrationsFolder: MIGRATIONS_DIR });
|
|
129
|
+
return new StateDb(client, "sqlite", () => raw.close());
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Test / Phase-4 entry: adopt an existing Drizzle instance. Runs no
|
|
133
|
+
* migrations — the caller owns the schema.
|
|
134
|
+
*/
|
|
135
|
+
static fromClient(client, dialect, opts) {
|
|
136
|
+
return new StateDb(client, dialect, opts?.close);
|
|
137
|
+
}
|
|
138
|
+
get client() {
|
|
139
|
+
return this._client;
|
|
140
|
+
}
|
|
141
|
+
get dialect() {
|
|
142
|
+
return this._dialect;
|
|
63
143
|
}
|
|
64
144
|
// ── Cron overrides ─────────────────────────────────────────────
|
|
65
145
|
/** Get the override row for a single cron, or null if none. */
|
|
66
|
-
getCronOverride(name) {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
.
|
|
70
|
-
|
|
146
|
+
async getCronOverride(name) {
|
|
147
|
+
const { cronOverrides } = this.t;
|
|
148
|
+
const [row] = await this._client
|
|
149
|
+
.select()
|
|
150
|
+
.from(cronOverrides)
|
|
151
|
+
.where(eq(cronOverrides.name, name))
|
|
152
|
+
.limit(1);
|
|
153
|
+
return row ? { ...row, schedule: row.schedule ?? null, updatedBy: row.updatedBy ?? null } : null;
|
|
71
154
|
}
|
|
72
155
|
/** All override rows keyed by cron name. */
|
|
73
|
-
getAllCronOverrides() {
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
.all();
|
|
156
|
+
async getAllCronOverrides() {
|
|
157
|
+
const { cronOverrides } = this.t;
|
|
158
|
+
const rows = await this._client.select().from(cronOverrides);
|
|
77
159
|
const map = new Map();
|
|
78
160
|
for (const row of rows) {
|
|
79
|
-
|
|
80
|
-
|
|
161
|
+
map.set(row.name, {
|
|
162
|
+
...row,
|
|
163
|
+
schedule: row.schedule ?? null,
|
|
164
|
+
updatedBy: row.updatedBy ?? null,
|
|
165
|
+
});
|
|
81
166
|
}
|
|
82
167
|
return map;
|
|
83
168
|
}
|
|
@@ -86,89 +171,79 @@ export class StateDb {
|
|
|
86
171
|
* fields preserve the existing value (or default to enabled=1, schedule=null
|
|
87
172
|
* on insert).
|
|
88
173
|
*/
|
|
89
|
-
setCronOverride(name, patch) {
|
|
174
|
+
async setCronOverride(name, patch) {
|
|
175
|
+
const { cronOverrides } = this.t;
|
|
90
176
|
const now = new Date().toISOString();
|
|
91
|
-
const existing = this.getCronOverride(name);
|
|
177
|
+
const existing = await this.getCronOverride(name);
|
|
92
178
|
const enabled = patch.enabled ?? existing?.enabled ?? true;
|
|
93
|
-
const schedule = patch.schedule === undefined ? existing?.schedule ?? null : patch.schedule;
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
179
|
+
const schedule = patch.schedule === undefined ? (existing?.schedule ?? null) : patch.schedule;
|
|
180
|
+
const values = { name, enabled, schedule, updatedAt: now, updatedBy: patch.updatedBy ?? null };
|
|
181
|
+
await this._client
|
|
182
|
+
.insert(cronOverrides)
|
|
183
|
+
.values(values)
|
|
184
|
+
.onConflictDoUpdate({
|
|
185
|
+
target: cronOverrides.name,
|
|
186
|
+
set: {
|
|
187
|
+
enabled: values.enabled,
|
|
188
|
+
schedule: values.schedule,
|
|
189
|
+
updatedAt: values.updatedAt,
|
|
190
|
+
updatedBy: values.updatedBy,
|
|
191
|
+
},
|
|
192
|
+
});
|
|
103
193
|
}
|
|
104
194
|
/** Remove the override entirely (revert to YAML defaults). */
|
|
105
|
-
clearCronOverride(name) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
deserializeCronOverride(row) {
|
|
109
|
-
return {
|
|
110
|
-
name: row.name,
|
|
111
|
-
enabled: row.enabled === 1,
|
|
112
|
-
schedule: row.schedule ?? null,
|
|
113
|
-
updatedAt: row.updated_at,
|
|
114
|
-
updatedBy: row.updated_by ?? null,
|
|
115
|
-
};
|
|
195
|
+
async clearCronOverride(name) {
|
|
196
|
+
const { cronOverrides } = this.t;
|
|
197
|
+
await this._client.delete(cronOverrides).where(eq(cronOverrides.name, name));
|
|
116
198
|
}
|
|
117
199
|
// ── Workflow overrides (kill switch) ───────────────────────────
|
|
118
200
|
/**
|
|
119
201
|
* Cheap check used by every dispatch path. Returns true unless an explicit
|
|
120
202
|
* `workflow_overrides` row says otherwise.
|
|
121
203
|
*/
|
|
122
|
-
isWorkflowEnabled(name) {
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
204
|
+
async isWorkflowEnabled(name) {
|
|
205
|
+
const { workflowOverrides } = this.t;
|
|
206
|
+
const [row] = await this._client
|
|
207
|
+
.select({ enabled: workflowOverrides.enabled })
|
|
208
|
+
.from(workflowOverrides)
|
|
209
|
+
.where(eq(workflowOverrides.name, name))
|
|
210
|
+
.limit(1);
|
|
211
|
+
return row ? row.enabled : true;
|
|
129
212
|
}
|
|
130
|
-
getWorkflowOverride(name) {
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
.
|
|
134
|
-
|
|
213
|
+
async getWorkflowOverride(name) {
|
|
214
|
+
const { workflowOverrides } = this.t;
|
|
215
|
+
const [row] = await this._client
|
|
216
|
+
.select()
|
|
217
|
+
.from(workflowOverrides)
|
|
218
|
+
.where(eq(workflowOverrides.name, name))
|
|
219
|
+
.limit(1);
|
|
220
|
+
return row ? { ...row, updatedBy: row.updatedBy ?? null } : null;
|
|
135
221
|
}
|
|
136
|
-
getAllWorkflowOverrides() {
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
.all();
|
|
222
|
+
async getAllWorkflowOverrides() {
|
|
223
|
+
const { workflowOverrides } = this.t;
|
|
224
|
+
const rows = await this._client.select().from(workflowOverrides);
|
|
140
225
|
const map = new Map();
|
|
141
|
-
for (const row of rows)
|
|
142
|
-
|
|
143
|
-
map.set(o.name, o);
|
|
144
|
-
}
|
|
226
|
+
for (const row of rows)
|
|
227
|
+
map.set(row.name, { ...row, updatedBy: row.updatedBy ?? null });
|
|
145
228
|
return map;
|
|
146
229
|
}
|
|
147
|
-
setWorkflowEnabled(name, enabled, updatedBy) {
|
|
230
|
+
async setWorkflowEnabled(name, enabled, updatedBy) {
|
|
231
|
+
const { workflowOverrides } = this.t;
|
|
148
232
|
const now = new Date().toISOString();
|
|
149
|
-
this.
|
|
150
|
-
.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
.run(name, enabled ? 1 : 0, now, updatedBy ?? null);
|
|
157
|
-
}
|
|
158
|
-
deserializeWorkflowOverride(row) {
|
|
159
|
-
return {
|
|
160
|
-
name: row.name,
|
|
161
|
-
enabled: row.enabled === 1,
|
|
162
|
-
updatedAt: row.updated_at,
|
|
163
|
-
updatedBy: row.updated_by ?? null,
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
/** Expose the underlying Database instance (for SessionManager, etc.) */
|
|
167
|
-
get database() {
|
|
168
|
-
return this.db;
|
|
233
|
+
await this._client
|
|
234
|
+
.insert(workflowOverrides)
|
|
235
|
+
.values({ name, enabled, updatedAt: now, updatedBy: updatedBy ?? null })
|
|
236
|
+
.onConflictDoUpdate({
|
|
237
|
+
target: workflowOverrides.name,
|
|
238
|
+
set: { enabled, updatedAt: now, updatedBy: updatedBy ?? null },
|
|
239
|
+
});
|
|
169
240
|
}
|
|
170
|
-
|
|
171
|
-
|
|
241
|
+
/**
|
|
242
|
+
* Async by contract: libsql's `close()` is synchronous today, but a Postgres
|
|
243
|
+
* pool's is not, and every caller already awaits this.
|
|
244
|
+
*/
|
|
245
|
+
async close() {
|
|
246
|
+
this.closer?.();
|
|
172
247
|
}
|
|
173
248
|
}
|
|
174
249
|
//# sourceMappingURL=db.js.map
|
package/dist/state/db.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db.js","sourceRoot":"","sources":["../../src/state/db.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"db.js","sourceRoot":"","sources":["../../src/state/db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AACtD,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AACnD,OA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AY,EAAE,OAAgB,EAAE,SAAkB;QACzE,MAAM,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;QACrC,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QACrC,MAAM,IAAI,CAAC,OAAO;aACf,MAAM,CAAC,iBAAiB,CAAC;aACzB,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,EAAE,CAAC;aACvE,kBAAkB,CAAC;YAClB,MAAM,EAAE,iBAAiB,CAAC,IAAI;YAC9B,GAAG,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,EAAE;SAC/D,CAAC,CAAC;IACP,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC;IAClB,CAAC;CACF"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portability seam.
|
|
3
|
+
*
|
|
4
|
+
* Raw-SQL execution, rows-affected shape, unique-violation detection and a
|
|
5
|
+
* handful of SQL functions are the only surfaces that differ between
|
|
6
|
+
* `drizzle-orm/libsql` and a Postgres driver. Everything else the stores do
|
|
7
|
+
* goes through the shared query-builder API, which is identical.
|
|
8
|
+
*
|
|
9
|
+
* IMPORTANT — `rows()` results are NOT column-mapped by Drizzle. No boolean
|
|
10
|
+
* conversion, no JSON parsing, no camelCase renaming: a raw query must alias
|
|
11
|
+
* its own columns and treat booleans as 0/1 integers. Only builder queries
|
|
12
|
+
* (`client.select()…`) return mapped rows. Mixing the two up is the #1
|
|
13
|
+
* regression class in this migration.
|
|
14
|
+
*/
|
|
15
|
+
import { type SQL, type SQLWrapper } from "drizzle-orm";
|
|
16
|
+
import type { StateDbc } from "./client.js";
|
|
17
|
+
/** Run a raw query and return all rows. libsql: `.all(sql)`. pg: `.execute(sql)` → `{rows}`. */
|
|
18
|
+
export declare function rows<T = Record<string, unknown>>(dbc: StateDbc, query: SQL): Promise<T[]>;
|
|
19
|
+
/** Run a raw statement for its side effect. Feed the result to {@link changes}. */
|
|
20
|
+
export declare function run(dbc: StateDbc, query: SQL): Promise<unknown>;
|
|
21
|
+
/**
|
|
22
|
+
* Rows-affected from an awaited builder mutation (or {@link run}).
|
|
23
|
+
*
|
|
24
|
+
* libsql `ResultSet` → `.rowsAffected`; node-postgres `QueryResult` →
|
|
25
|
+
* `.rowCount`; PGlite → `.affectedRows`; better-sqlite3 → `.changes`. Replaces
|
|
26
|
+
* every `result.changes` read in the old code — the compare-and-set guards
|
|
27
|
+
* behind the approval/reply-gate lifecycle depend on this being exact.
|
|
28
|
+
*/
|
|
29
|
+
export declare function changes(result: unknown): number;
|
|
30
|
+
/**
|
|
31
|
+
* UNIQUE-violation detection across drivers: Postgres reports SQLSTATE 23505 on
|
|
32
|
+
* a `code` property somewhere down the cause chain (Drizzle wraps driver errors
|
|
33
|
+
* in `DrizzleQueryError`), SQLite says so in the message.
|
|
34
|
+
*/
|
|
35
|
+
export declare function isUniqueViolation(error: unknown): boolean;
|
|
36
|
+
/** Escape `%`, `_` and `\` for a `LIKE … ESCAPE '\'` pattern. */
|
|
37
|
+
export declare function likeEscape(input: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* UTC day bucket over an ISO-8601 TEXT column — replaces sqlite `date(col)`.
|
|
40
|
+
* Timestamps are ISO text in both dialects, so `substr` yields identical
|
|
41
|
+
* `YYYY-MM-DD` keys and the JS side's bucket keys keep matching.
|
|
42
|
+
*/
|
|
43
|
+
export declare function dayBucket(col: SQLWrapper): SQL;
|
|
44
|
+
/** UTC hour bucket (`YYYY-MM-DDTHH`) — replaces `strftime('%Y-%m-%dT%H', col)`. */
|
|
45
|
+
export declare function hourBucket(col: SQLWrapper): SQL;
|
|
46
|
+
/**
|
|
47
|
+
* "Does `haystack` contain the literal `needle`" — a BOOLEAN predicate.
|
|
48
|
+
*
|
|
49
|
+
* This replaces what was `strposExpr()`, a 1-based position: sqlite spells that
|
|
50
|
+
* `instr(h, n)`, Postgres has no `instr` at all and spells it `strpos(h, n)`,
|
|
51
|
+
* so it looked like the widest-reaching dialect branch in the codebase
|
|
52
|
+
* (`repo-ref.ts`'s qualified-repo split fans out to 13 call sites across the
|
|
53
|
+
* execution and run stores). But every one of those call sites only ever asked
|
|
54
|
+
* `> 0`, and `LIKE` answers that identically in both dialects — so the seam
|
|
55
|
+
* disappears instead of being plumbed. `ESCAPE` keeps a needle containing `%`
|
|
56
|
+
* or `_` literal; SQLite's case-insensitive ASCII LIKE is immaterial for the
|
|
57
|
+
* one caller (a `/`).
|
|
58
|
+
*/
|
|
59
|
+
export declare function containsExpr(haystack: SQLWrapper, needle: string): SQL;
|
|
60
|
+
/**
|
|
61
|
+
* `SUM(CASE WHEN <col> THEN 1 ELSE 0 END)` — count of TRUE.
|
|
62
|
+
*
|
|
63
|
+
* Truthiness works on both sqlite 0/1 and pg boolean, and NULL falls to ELSE,
|
|
64
|
+
* which is what keeps a still-running execution (`success IS NULL`) out of both
|
|
65
|
+
* the success and the failure tally.
|
|
66
|
+
*/
|
|
67
|
+
export declare function sumTrue(col: SQLWrapper): SQL;
|
|
68
|
+
/**
|
|
69
|
+
* `SUM(CASE WHEN <col> = false THEN 1 ELSE 0 END)` — count of FALSE.
|
|
70
|
+
*
|
|
71
|
+
* Deliberately an explicit comparison against a BOUND `false` param, not
|
|
72
|
+
* `WHEN NOT <col>`: `NOT NULL` is NULL, which would be correct here by
|
|
73
|
+
* accident, but the bound parameter is also what keeps this portable — libsql
|
|
74
|
+
* binds `false` as 0 and pg as a real boolean. A literal `= 0` makes Postgres
|
|
75
|
+
* throw `operator does not exist: boolean = integer`.
|
|
76
|
+
*/
|
|
77
|
+
export declare function sumFalse(col: SQLWrapper): SQL;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portability seam.
|
|
3
|
+
*
|
|
4
|
+
* Raw-SQL execution, rows-affected shape, unique-violation detection and a
|
|
5
|
+
* handful of SQL functions are the only surfaces that differ between
|
|
6
|
+
* `drizzle-orm/libsql` and a Postgres driver. Everything else the stores do
|
|
7
|
+
* goes through the shared query-builder API, which is identical.
|
|
8
|
+
*
|
|
9
|
+
* IMPORTANT — `rows()` results are NOT column-mapped by Drizzle. No boolean
|
|
10
|
+
* conversion, no JSON parsing, no camelCase renaming: a raw query must alias
|
|
11
|
+
* its own columns and treat booleans as 0/1 integers. Only builder queries
|
|
12
|
+
* (`client.select()…`) return mapped rows. Mixing the two up is the #1
|
|
13
|
+
* regression class in this migration.
|
|
14
|
+
*/
|
|
15
|
+
import { sql } from "drizzle-orm";
|
|
16
|
+
/** Run a raw query and return all rows. libsql: `.all(sql)`. pg: `.execute(sql)` → `{rows}`. */
|
|
17
|
+
export async function rows(dbc, query) {
|
|
18
|
+
const d = dbc;
|
|
19
|
+
if (typeof d.all === "function")
|
|
20
|
+
return (await d.all(query));
|
|
21
|
+
const res = (await d.execute(query));
|
|
22
|
+
return (Array.isArray(res) ? res : (res.rows ?? []));
|
|
23
|
+
}
|
|
24
|
+
/** Run a raw statement for its side effect. Feed the result to {@link changes}. */
|
|
25
|
+
export async function run(dbc, query) {
|
|
26
|
+
const d = dbc;
|
|
27
|
+
if (typeof d.run === "function")
|
|
28
|
+
return d.run(query);
|
|
29
|
+
return d.execute(query);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Rows-affected from an awaited builder mutation (or {@link run}).
|
|
33
|
+
*
|
|
34
|
+
* libsql `ResultSet` → `.rowsAffected`; node-postgres `QueryResult` →
|
|
35
|
+
* `.rowCount`; PGlite → `.affectedRows`; better-sqlite3 → `.changes`. Replaces
|
|
36
|
+
* every `result.changes` read in the old code — the compare-and-set guards
|
|
37
|
+
* behind the approval/reply-gate lifecycle depend on this being exact.
|
|
38
|
+
*/
|
|
39
|
+
export function changes(result) {
|
|
40
|
+
const r = result;
|
|
41
|
+
return Number(r?.rowsAffected ?? r?.rowCount ?? r?.affectedRows ?? r?.changes ?? 0);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* UNIQUE-violation detection across drivers: Postgres reports SQLSTATE 23505 on
|
|
45
|
+
* a `code` property somewhere down the cause chain (Drizzle wraps driver errors
|
|
46
|
+
* in `DrizzleQueryError`), SQLite says so in the message.
|
|
47
|
+
*/
|
|
48
|
+
export function isUniqueViolation(error) {
|
|
49
|
+
for (let e = error, depth = 0; e != null && depth < 6; e = e.cause, depth++) {
|
|
50
|
+
if (e.code === "23505")
|
|
51
|
+
return true;
|
|
52
|
+
if (e instanceof Error && /UNIQUE|unique constraint/i.test(e.message))
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
/** Escape `%`, `_` and `\` for a `LIKE … ESCAPE '\'` pattern. */
|
|
58
|
+
export function likeEscape(input) {
|
|
59
|
+
return input.replace(/[\\%_]/g, (ch) => `\\${ch}`);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* UTC day bucket over an ISO-8601 TEXT column — replaces sqlite `date(col)`.
|
|
63
|
+
* Timestamps are ISO text in both dialects, so `substr` yields identical
|
|
64
|
+
* `YYYY-MM-DD` keys and the JS side's bucket keys keep matching.
|
|
65
|
+
*/
|
|
66
|
+
export function dayBucket(col) {
|
|
67
|
+
return sql `substr(${col}, 1, 10)`;
|
|
68
|
+
}
|
|
69
|
+
/** UTC hour bucket (`YYYY-MM-DDTHH`) — replaces `strftime('%Y-%m-%dT%H', col)`. */
|
|
70
|
+
export function hourBucket(col) {
|
|
71
|
+
return sql `substr(${col}, 1, 13)`;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* "Does `haystack` contain the literal `needle`" — a BOOLEAN predicate.
|
|
75
|
+
*
|
|
76
|
+
* This replaces what was `strposExpr()`, a 1-based position: sqlite spells that
|
|
77
|
+
* `instr(h, n)`, Postgres has no `instr` at all and spells it `strpos(h, n)`,
|
|
78
|
+
* so it looked like the widest-reaching dialect branch in the codebase
|
|
79
|
+
* (`repo-ref.ts`'s qualified-repo split fans out to 13 call sites across the
|
|
80
|
+
* execution and run stores). But every one of those call sites only ever asked
|
|
81
|
+
* `> 0`, and `LIKE` answers that identically in both dialects — so the seam
|
|
82
|
+
* disappears instead of being plumbed. `ESCAPE` keeps a needle containing `%`
|
|
83
|
+
* or `_` literal; SQLite's case-insensitive ASCII LIKE is immaterial for the
|
|
84
|
+
* one caller (a `/`).
|
|
85
|
+
*/
|
|
86
|
+
export function containsExpr(haystack, needle) {
|
|
87
|
+
// The pattern is INLINED as a SQL literal rather than bound as a parameter,
|
|
88
|
+
// and that is load-bearing rather than a shortcut. Postgres matches a GROUP BY
|
|
89
|
+
// expression to its SELECT twin STRUCTURALLY, and two occurrences of the same
|
|
90
|
+
// bound parameter are two different placeholders ($1 and $3) — so
|
|
91
|
+
// `distinctRepos`, which selects and groups by the same `qualifiedRepoSql`
|
|
92
|
+
// fragment, failed with "column workflow_runs.repo must appear in the GROUP BY
|
|
93
|
+
// clause". (SQLite is lax about this and never noticed.) Inlining makes the
|
|
94
|
+
// two spellings textually identical.
|
|
95
|
+
//
|
|
96
|
+
// Safe only because every needle is a code-owned literal, so the charset guard
|
|
97
|
+
// below is the actual contract — assert it rather than trusting callers, since
|
|
98
|
+
// the failure mode of a hostile needle here would be injection.
|
|
99
|
+
if (!/^[A-Za-z0-9/.:@-]+$/.test(needle)) {
|
|
100
|
+
throw new Error(`containsExpr: needle must be a plain code literal (got ${JSON.stringify(needle)}). ` +
|
|
101
|
+
"For user input, bind a parameter and accept that it cannot appear in a GROUP BY.");
|
|
102
|
+
}
|
|
103
|
+
// No ESCAPE clause: the charset above excludes `%`, `_` and `\` outright, so
|
|
104
|
+
// there is nothing for `likeEscape` to escape.
|
|
105
|
+
return sql `${haystack} LIKE ${sql.raw(`'%${needle}%'`)}`;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* `SUM(CASE WHEN <col> THEN 1 ELSE 0 END)` — count of TRUE.
|
|
109
|
+
*
|
|
110
|
+
* Truthiness works on both sqlite 0/1 and pg boolean, and NULL falls to ELSE,
|
|
111
|
+
* which is what keeps a still-running execution (`success IS NULL`) out of both
|
|
112
|
+
* the success and the failure tally.
|
|
113
|
+
*/
|
|
114
|
+
export function sumTrue(col) {
|
|
115
|
+
return sql `SUM(CASE WHEN ${col} THEN 1 ELSE 0 END)`;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* `SUM(CASE WHEN <col> = false THEN 1 ELSE 0 END)` — count of FALSE.
|
|
119
|
+
*
|
|
120
|
+
* Deliberately an explicit comparison against a BOUND `false` param, not
|
|
121
|
+
* `WHEN NOT <col>`: `NOT NULL` is NULL, which would be correct here by
|
|
122
|
+
* accident, but the bound parameter is also what keeps this portable — libsql
|
|
123
|
+
* binds `false` as 0 and pg as a real boolean. A literal `= 0` makes Postgres
|
|
124
|
+
* throw `operator does not exist: boolean = integer`.
|
|
125
|
+
*/
|
|
126
|
+
export function sumFalse(col) {
|
|
127
|
+
return sql `SUM(CASE WHEN ${col} = ${false} THEN 1 ELSE 0 END)`;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=dialect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dialect.js","sourceRoot":"","sources":["../../src/state/dialect.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,GAAG,EAA6B,MAAM,aAAa,CAAC;AAS7D,gGAAgG;AAChG,MAAM,CAAC,KAAK,UAAU,IAAI,CACxB,GAAa,EACb,KAAU;IAEV,MAAM,CAAC,GAAG,GAA4B,CAAC;IACvC,IAAI,OAAO,CAAC,CAAC,GAAG,KAAK,UAAU;QAAE,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAQ,CAAC;IACpE,MAAM,G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