@hasna/loops 0.4.13 → 0.4.14
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 +74 -3
- package/README.md +54 -12
- package/dist/api/index.d.ts +35 -0
- package/dist/api/index.js +695 -10
- package/dist/cli/index.js +1633 -366
- package/dist/cli/ui.d.ts +44 -0
- package/dist/daemon/index.js +948 -212
- package/dist/generated/storage-kit/health.d.ts +19 -0
- package/dist/generated/storage-kit/index.d.ts +7 -0
- package/dist/generated/storage-kit/migrations.d.ts +47 -0
- package/dist/generated/storage-kit/mode.d.ts +47 -0
- package/dist/generated/storage-kit/pool.d.ts +33 -0
- package/dist/generated/storage-kit/query.d.ts +35 -0
- package/dist/generated/storage-kit/tls.d.ts +25 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3567 -2010
- package/dist/lib/health.d.ts +83 -3
- package/dist/lib/mode.js +14 -2
- package/dist/lib/scheduler.d.ts +5 -2
- package/dist/lib/storage/index.d.ts +1 -0
- package/dist/lib/storage/index.js +1329 -211
- package/dist/lib/storage/pg-executor.d.ts +27 -0
- package/dist/lib/storage/pg-runner-claim.d.ts +40 -0
- package/dist/lib/storage/postgres-loop-storage.d.ts +114 -0
- package/dist/lib/storage/sqlite.js +336 -8
- package/dist/lib/store.d.ts +234 -0
- package/dist/lib/store.js +350 -8
- package/dist/mcp/index.js +1067 -306
- package/dist/runner/index.js +124 -25
- package/dist/sdk/http.d.ts +115 -0
- package/dist/sdk/http.js +145 -0
- package/dist/sdk/index.d.ts +5 -1
- package/dist/sdk/index.js +1052 -312
- package/dist/serve/index.d.ts +4 -0
- package/dist/serve/index.js +7619 -0
- package/dist/types.d.ts +3 -0
- package/docs/CUTOVER-RUNBOOK.md +61 -0
- package/docs/USAGE.md +50 -11
- package/package.json +14 -2
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// src/lib/store.ts
|
|
3
3
|
import { Database } from "bun:sqlite";
|
|
4
|
-
import { chmodSync, existsSync, mkdirSync as mkdirSync3, mkdtempSync, rmSync as
|
|
5
|
-
import { tmpdir } from "os";
|
|
6
|
-
import { basename as basename2, dirname as dirname2, join as
|
|
4
|
+
import { chmodSync, existsSync, mkdirSync as mkdirSync3, mkdtempSync as mkdtempSync2, rmSync as rmSync3 } from "fs";
|
|
5
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
6
|
+
import { basename as basename2, dirname as dirname2, join as join4 } from "path";
|
|
7
7
|
|
|
8
8
|
// src/lib/errors.ts
|
|
9
9
|
class CodedError extends Error {
|
|
@@ -1202,6 +1202,196 @@ function discardWorkflowRunManifest(staged) {
|
|
|
1202
1202
|
} catch {}
|
|
1203
1203
|
}
|
|
1204
1204
|
|
|
1205
|
+
// src/lib/route/todos-cli.ts
|
|
1206
|
+
import { closeSync, mkdtempSync, openSync, readFileSync as readFileSync3, rmSync as rmSync2 } from "fs";
|
|
1207
|
+
import { spawnSync as spawnSync2 } from "child_process";
|
|
1208
|
+
import { join as join3 } from "path";
|
|
1209
|
+
import { homedir as homedir2, tmpdir } from "os";
|
|
1210
|
+
|
|
1211
|
+
// src/lib/format.ts
|
|
1212
|
+
var TEXT_OUTPUT_LIMIT = 32 * 1024;
|
|
1213
|
+
var SENSITIVE_PAYLOAD_KEYS = new Set(["env", "error", "prompt", "reason", "stderr", "stdout"]);
|
|
1214
|
+
function redact(value, visible = 0) {
|
|
1215
|
+
if (!value)
|
|
1216
|
+
return value;
|
|
1217
|
+
if (value.length <= visible)
|
|
1218
|
+
return value;
|
|
1219
|
+
if (visible <= 0)
|
|
1220
|
+
return `[redacted ${value.length} chars]`;
|
|
1221
|
+
return `${value.slice(0, visible)}... [redacted ${value.length - visible} chars]`;
|
|
1222
|
+
}
|
|
1223
|
+
function truncateTextOutput(value) {
|
|
1224
|
+
if (value.length <= TEXT_OUTPUT_LIMIT)
|
|
1225
|
+
return value;
|
|
1226
|
+
return `${value.slice(0, TEXT_OUTPUT_LIMIT)}
|
|
1227
|
+
[truncated ${value.length - TEXT_OUTPUT_LIMIT} chars]`;
|
|
1228
|
+
}
|
|
1229
|
+
function redactSensitivePayload(value, key) {
|
|
1230
|
+
if (key && SENSITIVE_PAYLOAD_KEYS.has(key)) {
|
|
1231
|
+
if (typeof value === "string")
|
|
1232
|
+
return redact(value);
|
|
1233
|
+
if (value === undefined || value === null)
|
|
1234
|
+
return value;
|
|
1235
|
+
return "[redacted]";
|
|
1236
|
+
}
|
|
1237
|
+
if (Array.isArray(value))
|
|
1238
|
+
return value.map((item) => redactSensitivePayload(item));
|
|
1239
|
+
if (value && typeof value === "object") {
|
|
1240
|
+
return Object.fromEntries(Object.entries(value).map(([entryKey, entryValue]) => [entryKey, redactSensitivePayload(entryValue, entryKey)]));
|
|
1241
|
+
}
|
|
1242
|
+
return value;
|
|
1243
|
+
}
|
|
1244
|
+
function textOutputBlocks(value, opts = {}) {
|
|
1245
|
+
const indent = opts.indent ?? "";
|
|
1246
|
+
const nested = `${indent} `;
|
|
1247
|
+
const blocks = [];
|
|
1248
|
+
for (const [label, output] of [
|
|
1249
|
+
["stdout", value.stdout],
|
|
1250
|
+
["stderr", value.stderr]
|
|
1251
|
+
]) {
|
|
1252
|
+
if (!output)
|
|
1253
|
+
continue;
|
|
1254
|
+
blocks.push(`${indent}${label}:`);
|
|
1255
|
+
for (const line of truncateTextOutput(output).replace(/\s+$/, "").split(/\r?\n/)) {
|
|
1256
|
+
blocks.push(`${nested}${line}`);
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
return blocks;
|
|
1260
|
+
}
|
|
1261
|
+
function publicLoop(loop) {
|
|
1262
|
+
const target = loop.target.type === "command" ? { ...loop.target, env: loop.target.env ? "[redacted]" : undefined } : loop.target.type === "agent" ? { ...loop.target, prompt: redact(loop.target.prompt) } : loop.target;
|
|
1263
|
+
return {
|
|
1264
|
+
...loop,
|
|
1265
|
+
target
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
function publicRun(run, showOutput = false, opts = {}) {
|
|
1269
|
+
return {
|
|
1270
|
+
...run,
|
|
1271
|
+
stdout: showOutput ? run.stdout : run.stdout ? `[redacted ${run.stdout.length} chars]` : undefined,
|
|
1272
|
+
stderr: showOutput ? run.stderr : run.stderr ? `[redacted ${run.stderr.length} chars]` : undefined,
|
|
1273
|
+
error: opts.redactError ? redact(run.error) : run.error
|
|
1274
|
+
};
|
|
1275
|
+
}
|
|
1276
|
+
function publicExecutorResult(result, showOutput = false) {
|
|
1277
|
+
return {
|
|
1278
|
+
...result,
|
|
1279
|
+
stdout: showOutput ? result.stdout : result.stdout ? `[redacted ${result.stdout.length} chars]` : undefined,
|
|
1280
|
+
stderr: showOutput ? result.stderr : result.stderr ? `[redacted ${result.stderr.length} chars]` : undefined,
|
|
1281
|
+
error: redact(result.error)
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
function publicWorkflow(workflow) {
|
|
1285
|
+
return {
|
|
1286
|
+
...workflow,
|
|
1287
|
+
steps: workflow.steps.map((step) => ({
|
|
1288
|
+
...step,
|
|
1289
|
+
target: step.target.type === "agent" ? { ...step.target, prompt: redact(step.target.prompt) } : step.target.type === "command" && step.target.env ? { ...step.target, env: "[redacted]" } : step.target
|
|
1290
|
+
}))
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
function publicWorkflowRun(run) {
|
|
1294
|
+
return { ...run, error: redact(run.error) };
|
|
1295
|
+
}
|
|
1296
|
+
function publicWorkflowInvocation(invocation) {
|
|
1297
|
+
return redactSensitivePayload(invocation);
|
|
1298
|
+
}
|
|
1299
|
+
function publicWorkflowWorkItem(item) {
|
|
1300
|
+
return { ...item, lastReason: redact(item.lastReason, 240) };
|
|
1301
|
+
}
|
|
1302
|
+
function publicWorkflowStepRun(run, showOutput = false) {
|
|
1303
|
+
return {
|
|
1304
|
+
...run,
|
|
1305
|
+
stdout: showOutput ? run.stdout : run.stdout ? `[redacted ${run.stdout.length} chars]` : undefined,
|
|
1306
|
+
stderr: showOutput ? run.stderr : run.stderr ? `[redacted ${run.stderr.length} chars]` : undefined,
|
|
1307
|
+
error: redact(run.error)
|
|
1308
|
+
};
|
|
1309
|
+
}
|
|
1310
|
+
function publicWorkflowEvent(event) {
|
|
1311
|
+
return { ...event, payload: redactSensitivePayload(event.payload) };
|
|
1312
|
+
}
|
|
1313
|
+
function publicGoal(goal) {
|
|
1314
|
+
return {
|
|
1315
|
+
...goal,
|
|
1316
|
+
objective: redact(goal.objective, 120)
|
|
1317
|
+
};
|
|
1318
|
+
}
|
|
1319
|
+
function publicGoalRun(run) {
|
|
1320
|
+
return {
|
|
1321
|
+
...run,
|
|
1322
|
+
evidence: redactSensitivePayload(run.evidence),
|
|
1323
|
+
rawResponse: redactSensitivePayload(run.rawResponse)
|
|
1324
|
+
};
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
// src/lib/route/todos-cli.ts
|
|
1328
|
+
function defaultLoopsProject() {
|
|
1329
|
+
return process.env.LOOPS_TASK_PROJECT || process.env.LOOPS_DATA_DIR || `${process.env.HOME || homedir2()}/.hasna/loops`;
|
|
1330
|
+
}
|
|
1331
|
+
function runLocalCommand(command, args, opts = {}) {
|
|
1332
|
+
const result = spawnSync2(command, args, {
|
|
1333
|
+
input: opts.input,
|
|
1334
|
+
encoding: "utf8",
|
|
1335
|
+
timeout: opts.timeoutMs ?? 30000,
|
|
1336
|
+
maxBuffer: opts.maxBuffer ?? 8 * 1024 * 1024,
|
|
1337
|
+
env: process.env
|
|
1338
|
+
});
|
|
1339
|
+
return {
|
|
1340
|
+
ok: result.status === 0,
|
|
1341
|
+
status: result.status,
|
|
1342
|
+
stdout: result.stdout || "",
|
|
1343
|
+
stderr: result.stderr || "",
|
|
1344
|
+
error: result.error ? String(result.error.message || result.error) : ""
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
function runLocalCommandWithStdoutFile(command, args, opts = {}) {
|
|
1348
|
+
const tempDir = mkdtempSync(join3(tmpdir(), "loops-command-output-"));
|
|
1349
|
+
const stdoutPath = join3(tempDir, "stdout");
|
|
1350
|
+
const stdoutFd = openSync(stdoutPath, "w");
|
|
1351
|
+
let result;
|
|
1352
|
+
try {
|
|
1353
|
+
result = spawnSync2(command, args, {
|
|
1354
|
+
input: opts.input,
|
|
1355
|
+
encoding: "utf8",
|
|
1356
|
+
timeout: opts.timeoutMs ?? 30000,
|
|
1357
|
+
maxBuffer: opts.maxBuffer ?? 8 * 1024 * 1024,
|
|
1358
|
+
env: process.env,
|
|
1359
|
+
stdio: ["pipe", stdoutFd, "pipe"]
|
|
1360
|
+
});
|
|
1361
|
+
} finally {
|
|
1362
|
+
closeSync(stdoutFd);
|
|
1363
|
+
}
|
|
1364
|
+
try {
|
|
1365
|
+
return {
|
|
1366
|
+
ok: result.status === 0,
|
|
1367
|
+
status: result.status,
|
|
1368
|
+
stdout: readFileSync3(stdoutPath, "utf8"),
|
|
1369
|
+
stderr: typeof result.stderr === "string" ? result.stderr : result.stderr?.toString() || "",
|
|
1370
|
+
error: result.error ? String(result.error.message || result.error) : ""
|
|
1371
|
+
};
|
|
1372
|
+
} finally {
|
|
1373
|
+
rmSync2(tempDir, { recursive: true, force: true });
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
function ensureTodosTaskList(project, slug, name, description) {
|
|
1377
|
+
runLocalCommand("todos", ["--project", project, "task-lists", "--add", name, "--slug", slug, "-d", description]);
|
|
1378
|
+
const list = runLocalCommand("todos", ["--project", project, "--json", "task-lists"]);
|
|
1379
|
+
if (!list.ok)
|
|
1380
|
+
throw new Error(list.stderr || list.error || "failed to list todos task lists");
|
|
1381
|
+
const values = JSON.parse(list.stdout || "[]");
|
|
1382
|
+
const found = values.find((entry) => entry.slug === slug);
|
|
1383
|
+
if (!found)
|
|
1384
|
+
throw new Error(`todos task list not found after ensure: ${slug}`);
|
|
1385
|
+
return found.id;
|
|
1386
|
+
}
|
|
1387
|
+
function todosMutationSummary(result) {
|
|
1388
|
+
return {
|
|
1389
|
+
ok: result.ok,
|
|
1390
|
+
status: result.status,
|
|
1391
|
+
error: result.ok ? undefined : redact(result.stderr || result.error || "todos mutation failed", 320)
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1205
1395
|
// src/lib/store.ts
|
|
1206
1396
|
var DEFAULT_RECOVERY_BATCH_LIMIT = 100;
|
|
1207
1397
|
var DEFAULT_RECOVERY_SCAN_MULTIPLIER = 5;
|
|
@@ -1211,6 +1401,7 @@ var TERMINAL_RUN_STATUSES = ["succeeded", "failed", "timed_out", "abandoned", "s
|
|
|
1211
1401
|
var PRUNE_BATCH_SIZE = 400;
|
|
1212
1402
|
var GENERATED_ROUTE_TEMPLATE_IDS = new Set(["todos-task-worker-verifier", "task-lifecycle", "event-worker-verifier"]);
|
|
1213
1403
|
var GENERATED_ROUTE_KEYS = new Set(["todos-task", "generic-event"]);
|
|
1404
|
+
var TASK_LIFECYCLE_TEMPLATE_ID = "task-lifecycle";
|
|
1214
1405
|
function rowToLoop(row) {
|
|
1215
1406
|
return {
|
|
1216
1407
|
id: row.id,
|
|
@@ -1261,6 +1452,9 @@ function rowToRun(row) {
|
|
|
1261
1452
|
updatedAt: row.updated_at
|
|
1262
1453
|
};
|
|
1263
1454
|
}
|
|
1455
|
+
function latestRunTime(row) {
|
|
1456
|
+
return row.finished_at ?? row.started_at ?? row.created_at;
|
|
1457
|
+
}
|
|
1264
1458
|
function rowToWorkflow(row) {
|
|
1265
1459
|
return {
|
|
1266
1460
|
id: row.id,
|
|
@@ -1515,7 +1709,7 @@ class Store {
|
|
|
1515
1709
|
const file = path ?? dbPath();
|
|
1516
1710
|
if (file !== ":memory:")
|
|
1517
1711
|
ensurePrivateStorePath(file);
|
|
1518
|
-
this.rootDir = file === ":memory:" ?
|
|
1712
|
+
this.rootDir = file === ":memory:" ? mkdtempSync2(join4(tmpdir2(), "open-loops-store-")) : dirname2(file);
|
|
1519
1713
|
if (file === ":memory:")
|
|
1520
1714
|
this.memoryRootDir = this.rootDir;
|
|
1521
1715
|
this.db = new Database(file);
|
|
@@ -2013,7 +2207,32 @@ class Store {
|
|
|
2013
2207
|
} else {
|
|
2014
2208
|
rows = this.db.query("SELECT * FROM loops WHERE archived_at IS NULL ORDER BY status ASC, next_run_at ASC LIMIT ?").all(limit);
|
|
2015
2209
|
}
|
|
2016
|
-
return rows.map(rowToLoop);
|
|
2210
|
+
return this.withLatestRunSummaries(rows.map(rowToLoop));
|
|
2211
|
+
}
|
|
2212
|
+
withLatestRunSummaries(loops) {
|
|
2213
|
+
if (loops.length === 0)
|
|
2214
|
+
return loops;
|
|
2215
|
+
const placeholders = loops.map(() => "?").join(",");
|
|
2216
|
+
const rows = this.db.query(`SELECT loop_id, id, status, started_at, finished_at, created_at
|
|
2217
|
+
FROM (
|
|
2218
|
+
SELECT loop_id, id, status, started_at, finished_at, created_at,
|
|
2219
|
+
ROW_NUMBER() OVER (PARTITION BY loop_id ORDER BY created_at DESC, id DESC) AS rn
|
|
2220
|
+
FROM loop_runs
|
|
2221
|
+
WHERE loop_id IN (${placeholders})
|
|
2222
|
+
)
|
|
2223
|
+
WHERE rn = 1`).all(...loops.map((loop) => loop.id));
|
|
2224
|
+
const latestByLoopId = new Map(rows.map((row) => [row.loop_id, row]));
|
|
2225
|
+
return loops.map((loop) => {
|
|
2226
|
+
const latest = latestByLoopId.get(loop.id);
|
|
2227
|
+
if (!latest)
|
|
2228
|
+
return loop;
|
|
2229
|
+
return {
|
|
2230
|
+
...loop,
|
|
2231
|
+
latestRunId: latest.id,
|
|
2232
|
+
latestRunStatus: latest.status,
|
|
2233
|
+
lastRunAt: latestRunTime(latest)
|
|
2234
|
+
};
|
|
2235
|
+
});
|
|
2017
2236
|
}
|
|
2018
2237
|
dueLoops(now, limit = 500) {
|
|
2019
2238
|
const rows = this.db.query(`SELECT * FROM loops
|
|
@@ -2132,6 +2351,45 @@ class Store {
|
|
|
2132
2351
|
throw error;
|
|
2133
2352
|
}
|
|
2134
2353
|
}
|
|
2354
|
+
updateAgentLoopTimeout(idOrName, timeoutMs, opts = {}) {
|
|
2355
|
+
const updated = (opts.now ?? new Date).toISOString();
|
|
2356
|
+
this.db.exec("BEGIN IMMEDIATE");
|
|
2357
|
+
try {
|
|
2358
|
+
const current = this.requireUniqueLoop(idOrName);
|
|
2359
|
+
if (current.archivedAt)
|
|
2360
|
+
throw new LoopArchivedError(current.name || current.id);
|
|
2361
|
+
if (current.target.type !== "agent")
|
|
2362
|
+
throw new Error(`loop is not an agent loop: ${idOrName}`);
|
|
2363
|
+
if (this.hasRunningRun(current.id))
|
|
2364
|
+
throw new Error(`refusing to update running loop: ${current.id}`);
|
|
2365
|
+
const target = { ...current.target, timeoutMs };
|
|
2366
|
+
if (timeoutMs === null && target.idleTimeoutMs !== undefined)
|
|
2367
|
+
delete target.idleTimeoutMs;
|
|
2368
|
+
const res = this.db.query(`UPDATE loops SET target_json=$target, updated_at=$updated
|
|
2369
|
+
WHERE id=$id
|
|
2370
|
+
AND ($daemonLeaseId IS NULL OR EXISTS (
|
|
2371
|
+
SELECT 1 FROM daemon_lease WHERE id=$daemonLeaseId AND expires_at > $now
|
|
2372
|
+
))`).run({
|
|
2373
|
+
$id: current.id,
|
|
2374
|
+
$target: JSON.stringify(target),
|
|
2375
|
+
$updated: updated,
|
|
2376
|
+
$daemonLeaseId: opts.daemonLeaseId ?? null,
|
|
2377
|
+
$now: updated
|
|
2378
|
+
});
|
|
2379
|
+
if (res.changes !== 1)
|
|
2380
|
+
throw new Error("daemon lease lost");
|
|
2381
|
+
this.db.exec("COMMIT");
|
|
2382
|
+
const after = this.getLoop(current.id);
|
|
2383
|
+
if (!after)
|
|
2384
|
+
throw new Error(`loop not found after timeout update: ${current.id}`);
|
|
2385
|
+
return after;
|
|
2386
|
+
} catch (error) {
|
|
2387
|
+
try {
|
|
2388
|
+
this.db.exec("ROLLBACK");
|
|
2389
|
+
} catch {}
|
|
2390
|
+
throw error;
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2135
2393
|
createAndRetargetWorkflowLoop(idOrName, workflowInput, opts = {}) {
|
|
2136
2394
|
const normalized = normalizeCreateWorkflowInput(workflowInput);
|
|
2137
2395
|
const updated = (opts.now ?? new Date).toISOString();
|
|
@@ -2462,6 +2720,59 @@ class Store {
|
|
|
2462
2720
|
updated
|
|
2463
2721
|
});
|
|
2464
2722
|
}
|
|
2723
|
+
taskLifecycleTodosPointerContext(workflowRunId) {
|
|
2724
|
+
const run = this.getWorkflowRun(workflowRunId);
|
|
2725
|
+
if (!run || run.status !== "succeeded" || !run.invocationId || !run.workItemId || !run.manifestPath)
|
|
2726
|
+
return;
|
|
2727
|
+
const workItem = this.getWorkflowWorkItem(run.workItemId);
|
|
2728
|
+
if (!workItem || workItem.routeKey !== "todos-task")
|
|
2729
|
+
return;
|
|
2730
|
+
const invocation = this.getWorkflowInvocation(run.invocationId);
|
|
2731
|
+
if (!invocation || invocation.templateId !== TASK_LIFECYCLE_TEMPLATE_ID)
|
|
2732
|
+
return;
|
|
2733
|
+
const projectPath = workItem.projectKey ?? invocation.scope?.projectPath;
|
|
2734
|
+
const taskId = invocation.subjectRef.id ?? workItem.subjectRef;
|
|
2735
|
+
if (!projectPath || !taskId)
|
|
2736
|
+
return;
|
|
2737
|
+
return {
|
|
2738
|
+
projectPath,
|
|
2739
|
+
taskId,
|
|
2740
|
+
invocationId: invocation.id,
|
|
2741
|
+
workflowRunId: run.id,
|
|
2742
|
+
manifestPath: run.manifestPath
|
|
2743
|
+
};
|
|
2744
|
+
}
|
|
2745
|
+
syncSuccessfulTaskLifecycleTodosPointers(workflowRunId) {
|
|
2746
|
+
const context = this.taskLifecycleTodosPointerContext(workflowRunId);
|
|
2747
|
+
if (!context)
|
|
2748
|
+
return;
|
|
2749
|
+
const result = runLocalCommand("todos", [
|
|
2750
|
+
"--project",
|
|
2751
|
+
context.projectPath,
|
|
2752
|
+
"task",
|
|
2753
|
+
"workflow-pointers",
|
|
2754
|
+
context.taskId,
|
|
2755
|
+
"--clear",
|
|
2756
|
+
"--invocation",
|
|
2757
|
+
context.invocationId,
|
|
2758
|
+
"--run",
|
|
2759
|
+
context.workflowRunId,
|
|
2760
|
+
"--manifest",
|
|
2761
|
+
context.manifestPath,
|
|
2762
|
+
"--state",
|
|
2763
|
+
"succeeded",
|
|
2764
|
+
"--actor",
|
|
2765
|
+
"openloops:task-lifecycle"
|
|
2766
|
+
]);
|
|
2767
|
+
this.appendWorkflowEvent(workflowRunId, result.ok ? "todos_workflow_pointers_synced" : "todos_workflow_pointers_sync_failed", undefined, {
|
|
2768
|
+
projectPath: context.projectPath,
|
|
2769
|
+
taskId: context.taskId,
|
|
2770
|
+
invocationId: context.invocationId,
|
|
2771
|
+
workflowRunId: context.workflowRunId,
|
|
2772
|
+
manifestPath: context.manifestPath,
|
|
2773
|
+
mutation: todosMutationSummary(result)
|
|
2774
|
+
});
|
|
2775
|
+
}
|
|
2465
2776
|
createWorkflowInvocation(input) {
|
|
2466
2777
|
const now = nowIso();
|
|
2467
2778
|
const sourceDedupeKey = input.sourceRef.dedupeKey ?? undefined;
|
|
@@ -3424,6 +3735,8 @@ class Store {
|
|
|
3424
3735
|
const run = this.getWorkflowRun(workflowRunId);
|
|
3425
3736
|
if (!run)
|
|
3426
3737
|
throw new Error(`workflow run not found after finalize: ${workflowRunId}`);
|
|
3738
|
+
if (changed && status === "succeeded")
|
|
3739
|
+
this.syncSuccessfulTaskLifecycleTodosPointers(workflowRunId);
|
|
3427
3740
|
return run;
|
|
3428
3741
|
}
|
|
3429
3742
|
cancelWorkflowRun(workflowRunId, reason = "cancelled by user") {
|
|
@@ -3485,6 +3798,17 @@ class Store {
|
|
|
3485
3798
|
const row = this.db.query("SELECT COUNT(*) AS count FROM loop_runs WHERE loop_id = ? AND scheduled_for = ? AND status = 'running'").get(loopId, scheduledFor);
|
|
3486
3799
|
return (row?.count ?? 0) > 0;
|
|
3487
3800
|
}
|
|
3801
|
+
hasBlockingRunningRunForOtherSlot(loopId, scheduledFor, nowIso2) {
|
|
3802
|
+
const rows = this.db.query(`SELECT * FROM loop_runs
|
|
3803
|
+
WHERE loop_id = ? AND scheduled_for <> ? AND status = 'running'`).all(loopId, scheduledFor);
|
|
3804
|
+
return rows.some((row) => {
|
|
3805
|
+
if (!row.lease_expires_at || row.lease_expires_at > nowIso2)
|
|
3806
|
+
return true;
|
|
3807
|
+
if (isRecordedProcessAlive(row.pid, row.process_started_at))
|
|
3808
|
+
return true;
|
|
3809
|
+
return this.hasLiveWorkflowStepProcesses(row.id);
|
|
3810
|
+
});
|
|
3811
|
+
}
|
|
3488
3812
|
markRunPid(id, pid, claimedBy, opts = {}) {
|
|
3489
3813
|
const now = (opts.now ?? new Date).toISOString();
|
|
3490
3814
|
const startedMs = processStartTimeMs(pid);
|
|
@@ -3617,6 +3941,10 @@ class Store {
|
|
|
3617
3941
|
loop = currentLoop;
|
|
3618
3942
|
const leaseExpiresAt = new Date(now.getTime() + loop.leaseMs).toISOString();
|
|
3619
3943
|
const existing = this.getRunBySlot(loop.id, scheduledFor);
|
|
3944
|
+
if (loop.overlap === "skip" && this.hasBlockingRunningRunForOtherSlot(loop.id, scheduledFor, startedAt)) {
|
|
3945
|
+
this.db.exec("COMMIT");
|
|
3946
|
+
return;
|
|
3947
|
+
}
|
|
3620
3948
|
if (existing) {
|
|
3621
3949
|
if (existing.status === "running") {
|
|
3622
3950
|
if (existing.leaseExpiresAt && existing.leaseExpiresAt <= startedAt && isRecordedProcessAlive(existing.pid, existing.processStartedAt)) {
|
|
@@ -4188,9 +4516,9 @@ class Store {
|
|
|
4188
4516
|
const runDir = dirname2(manifestPath);
|
|
4189
4517
|
try {
|
|
4190
4518
|
if (/^[0-9a-f]{12,64}$/.test(basename2(runDir))) {
|
|
4191
|
-
|
|
4519
|
+
rmSync3(runDir, { recursive: true, force: true });
|
|
4192
4520
|
} else {
|
|
4193
|
-
|
|
4521
|
+
rmSync3(manifestPath, { force: true });
|
|
4194
4522
|
}
|
|
4195
4523
|
} catch {}
|
|
4196
4524
|
}
|
|
@@ -4257,215 +4585,13 @@ class Store {
|
|
|
4257
4585
|
this.db.close();
|
|
4258
4586
|
if (this.memoryRootDir) {
|
|
4259
4587
|
try {
|
|
4260
|
-
|
|
4588
|
+
rmSync3(this.memoryRootDir, { recursive: true, force: true });
|
|
4261
4589
|
} catch {}
|
|
4262
4590
|
this.memoryRootDir = undefined;
|
|
4263
4591
|
}
|
|
4264
4592
|
}
|
|
4265
4593
|
}
|
|
4266
4594
|
|
|
4267
|
-
// src/lib/storage/sqlite.ts
|
|
4268
|
-
class SqliteLoopStorage {
|
|
4269
|
-
store;
|
|
4270
|
-
backend = "sqlite";
|
|
4271
|
-
supportsRemoteRunners = false;
|
|
4272
|
-
constructor(store = new Store) {
|
|
4273
|
-
this.store = store;
|
|
4274
|
-
}
|
|
4275
|
-
async close() {
|
|
4276
|
-
this.store.close();
|
|
4277
|
-
}
|
|
4278
|
-
async call(method, ...args) {
|
|
4279
|
-
return this.store[method](...args);
|
|
4280
|
-
}
|
|
4281
|
-
createLoop(...args) {
|
|
4282
|
-
return this.call("createLoop", ...args);
|
|
4283
|
-
}
|
|
4284
|
-
getLoop(...args) {
|
|
4285
|
-
return this.call("getLoop", ...args);
|
|
4286
|
-
}
|
|
4287
|
-
findLoopByName(...args) {
|
|
4288
|
-
return this.call("findLoopByName", ...args);
|
|
4289
|
-
}
|
|
4290
|
-
requireLoop(...args) {
|
|
4291
|
-
return this.call("requireLoop", ...args);
|
|
4292
|
-
}
|
|
4293
|
-
listLoops(...args) {
|
|
4294
|
-
return this.call("listLoops", ...args);
|
|
4295
|
-
}
|
|
4296
|
-
dueLoops(...args) {
|
|
4297
|
-
return this.call("dueLoops", ...args);
|
|
4298
|
-
}
|
|
4299
|
-
updateLoop(...args) {
|
|
4300
|
-
return this.call("updateLoop", ...args);
|
|
4301
|
-
}
|
|
4302
|
-
renameLoop(...args) {
|
|
4303
|
-
return this.call("renameLoop", ...args);
|
|
4304
|
-
}
|
|
4305
|
-
archiveLoop(...args) {
|
|
4306
|
-
return this.call("archiveLoop", ...args);
|
|
4307
|
-
}
|
|
4308
|
-
unarchiveLoop(...args) {
|
|
4309
|
-
return this.call("unarchiveLoop", ...args);
|
|
4310
|
-
}
|
|
4311
|
-
deleteLoop(...args) {
|
|
4312
|
-
return this.call("deleteLoop", ...args);
|
|
4313
|
-
}
|
|
4314
|
-
createWorkflow(...args) {
|
|
4315
|
-
return this.call("createWorkflow", ...args);
|
|
4316
|
-
}
|
|
4317
|
-
getWorkflow(...args) {
|
|
4318
|
-
return this.call("getWorkflow", ...args);
|
|
4319
|
-
}
|
|
4320
|
-
listWorkflows(...args) {
|
|
4321
|
-
return this.call("listWorkflows", ...args);
|
|
4322
|
-
}
|
|
4323
|
-
countWorkflows(...args) {
|
|
4324
|
-
return this.call("countWorkflows", ...args);
|
|
4325
|
-
}
|
|
4326
|
-
archiveWorkflow(...args) {
|
|
4327
|
-
return this.call("archiveWorkflow", ...args);
|
|
4328
|
-
}
|
|
4329
|
-
createWorkflowInvocation(...args) {
|
|
4330
|
-
return this.call("createWorkflowInvocation", ...args);
|
|
4331
|
-
}
|
|
4332
|
-
getWorkflowInvocation(...args) {
|
|
4333
|
-
return this.call("getWorkflowInvocation", ...args);
|
|
4334
|
-
}
|
|
4335
|
-
listWorkflowInvocations(...args) {
|
|
4336
|
-
return this.call("listWorkflowInvocations", ...args);
|
|
4337
|
-
}
|
|
4338
|
-
upsertWorkflowWorkItem(...args) {
|
|
4339
|
-
return this.call("upsertWorkflowWorkItem", ...args);
|
|
4340
|
-
}
|
|
4341
|
-
getWorkflowWorkItem(...args) {
|
|
4342
|
-
return this.call("getWorkflowWorkItem", ...args);
|
|
4343
|
-
}
|
|
4344
|
-
listWorkflowWorkItems(...args) {
|
|
4345
|
-
return this.call("listWorkflowWorkItems", ...args);
|
|
4346
|
-
}
|
|
4347
|
-
countActiveWorkflowWorkItems(...args) {
|
|
4348
|
-
return this.call("countActiveWorkflowWorkItems", ...args);
|
|
4349
|
-
}
|
|
4350
|
-
admitWorkflowWorkItem(...args) {
|
|
4351
|
-
return this.call("admitWorkflowWorkItem", ...args);
|
|
4352
|
-
}
|
|
4353
|
-
createGoal(...args) {
|
|
4354
|
-
return this.call("createGoal", ...args);
|
|
4355
|
-
}
|
|
4356
|
-
getGoal(...args) {
|
|
4357
|
-
return this.call("getGoal", ...args);
|
|
4358
|
-
}
|
|
4359
|
-
listGoals(...args) {
|
|
4360
|
-
return this.call("listGoals", ...args);
|
|
4361
|
-
}
|
|
4362
|
-
createGoalPlanNodes(...args) {
|
|
4363
|
-
return this.call("createGoalPlanNodes", ...args);
|
|
4364
|
-
}
|
|
4365
|
-
listGoalPlanNodes(...args) {
|
|
4366
|
-
return this.call("listGoalPlanNodes", ...args);
|
|
4367
|
-
}
|
|
4368
|
-
updateGoalStatus(...args) {
|
|
4369
|
-
return this.call("updateGoalStatus", ...args);
|
|
4370
|
-
}
|
|
4371
|
-
updateGoalPlanNode(...args) {
|
|
4372
|
-
return this.call("updateGoalPlanNode", ...args);
|
|
4373
|
-
}
|
|
4374
|
-
recordGoalEvent(...args) {
|
|
4375
|
-
return this.call("recordGoalEvent", ...args);
|
|
4376
|
-
}
|
|
4377
|
-
listGoalRuns(...args) {
|
|
4378
|
-
return this.call("listGoalRuns", ...args);
|
|
4379
|
-
}
|
|
4380
|
-
createWorkflowRun(...args) {
|
|
4381
|
-
return this.call("createWorkflowRun", ...args);
|
|
4382
|
-
}
|
|
4383
|
-
getWorkflowRun(...args) {
|
|
4384
|
-
return this.call("getWorkflowRun", ...args);
|
|
4385
|
-
}
|
|
4386
|
-
listWorkflowRuns(...args) {
|
|
4387
|
-
return this.call("listWorkflowRuns", ...args);
|
|
4388
|
-
}
|
|
4389
|
-
listWorkflowStepRuns(...args) {
|
|
4390
|
-
return this.call("listWorkflowStepRuns", ...args);
|
|
4391
|
-
}
|
|
4392
|
-
getWorkflowStepRun(...args) {
|
|
4393
|
-
return this.call("getWorkflowStepRun", ...args);
|
|
4394
|
-
}
|
|
4395
|
-
startWorkflowStepRun(...args) {
|
|
4396
|
-
return this.call("startWorkflowStepRun", ...args);
|
|
4397
|
-
}
|
|
4398
|
-
recoverWorkflowRun(...args) {
|
|
4399
|
-
return this.call("recoverWorkflowRun", ...args);
|
|
4400
|
-
}
|
|
4401
|
-
finalizeWorkflowStepRun(...args) {
|
|
4402
|
-
return this.call("finalizeWorkflowStepRun", ...args);
|
|
4403
|
-
}
|
|
4404
|
-
finalizeWorkflowRun(...args) {
|
|
4405
|
-
return this.call("finalizeWorkflowRun", ...args);
|
|
4406
|
-
}
|
|
4407
|
-
appendWorkflowEvent(...args) {
|
|
4408
|
-
return this.call("appendWorkflowEvent", ...args);
|
|
4409
|
-
}
|
|
4410
|
-
listWorkflowEvents(...args) {
|
|
4411
|
-
return this.call("listWorkflowEvents", ...args);
|
|
4412
|
-
}
|
|
4413
|
-
recordRunProcess(...args) {
|
|
4414
|
-
return this.call("recordRunProcess", ...args);
|
|
4415
|
-
}
|
|
4416
|
-
createSkippedRun(...args) {
|
|
4417
|
-
return this.call("createSkippedRun", ...args);
|
|
4418
|
-
}
|
|
4419
|
-
getRun(...args) {
|
|
4420
|
-
return this.call("getRun", ...args);
|
|
4421
|
-
}
|
|
4422
|
-
getRunBySlot(...args) {
|
|
4423
|
-
return this.call("getRunBySlot", ...args);
|
|
4424
|
-
}
|
|
4425
|
-
claimRun(...args) {
|
|
4426
|
-
return this.call("claimRun", ...args);
|
|
4427
|
-
}
|
|
4428
|
-
finalizeRun(...args) {
|
|
4429
|
-
return this.call("finalizeRun", ...args);
|
|
4430
|
-
}
|
|
4431
|
-
heartbeatRunLease(...args) {
|
|
4432
|
-
return this.call("heartbeatRunLease", ...args);
|
|
4433
|
-
}
|
|
4434
|
-
listRuns(...args) {
|
|
4435
|
-
return this.call("listRuns", ...args);
|
|
4436
|
-
}
|
|
4437
|
-
recoverExpiredRunLeases(...args) {
|
|
4438
|
-
return this.call("recoverExpiredRunLeases", ...args);
|
|
4439
|
-
}
|
|
4440
|
-
recoverExpiredRunLeasesDetailed(...args) {
|
|
4441
|
-
return this.call("recoverExpiredRunLeasesDetailed", ...args);
|
|
4442
|
-
}
|
|
4443
|
-
countLoops(...args) {
|
|
4444
|
-
return this.call("countLoops", ...args);
|
|
4445
|
-
}
|
|
4446
|
-
countRuns(...args) {
|
|
4447
|
-
return this.call("countRuns", ...args);
|
|
4448
|
-
}
|
|
4449
|
-
pruneHistory(...args) {
|
|
4450
|
-
return this.call("pruneHistory", ...args);
|
|
4451
|
-
}
|
|
4452
|
-
acquireDaemonLease(...args) {
|
|
4453
|
-
return this.call("acquireDaemonLease", ...args);
|
|
4454
|
-
}
|
|
4455
|
-
heartbeatDaemonLease(...args) {
|
|
4456
|
-
return this.call("heartbeatDaemonLease", ...args);
|
|
4457
|
-
}
|
|
4458
|
-
releaseDaemonLease(...args) {
|
|
4459
|
-
return this.call("releaseDaemonLease", ...args);
|
|
4460
|
-
}
|
|
4461
|
-
getDaemonLease(...args) {
|
|
4462
|
-
return this.call("getDaemonLease", ...args);
|
|
4463
|
-
}
|
|
4464
|
-
}
|
|
4465
|
-
function createSqliteLoopStorage(path) {
|
|
4466
|
-
return new SqliteLoopStorage(new Store(path));
|
|
4467
|
-
}
|
|
4468
|
-
|
|
4469
4595
|
// src/lib/storage/postgres-schema.ts
|
|
4470
4596
|
import { createHash as createHash2 } from "crypto";
|
|
4471
4597
|
var POSTGRES_MIGRATION_LEDGER_TABLE = "open_loops_schema_migrations";
|
|
@@ -4888,13 +5014,1005 @@ function isMissingLedgerError(error) {
|
|
|
4888
5014
|
const message = error.message.toLowerCase();
|
|
4889
5015
|
return message.includes(POSTGRES_MIGRATION_LEDGER_TABLE.toLowerCase()) && (message.includes("does not exist") || message.includes("no such table") || message.includes("undefined_table"));
|
|
4890
5016
|
}
|
|
5017
|
+
|
|
5018
|
+
// src/lib/storage/sqlite.ts
|
|
5019
|
+
class SqliteLoopStorage {
|
|
5020
|
+
store;
|
|
5021
|
+
backend = "sqlite";
|
|
5022
|
+
supportsRemoteRunners = false;
|
|
5023
|
+
constructor(store = new Store) {
|
|
5024
|
+
this.store = store;
|
|
5025
|
+
}
|
|
5026
|
+
async close() {
|
|
5027
|
+
this.store.close();
|
|
5028
|
+
}
|
|
5029
|
+
async call(method, ...args) {
|
|
5030
|
+
return this.store[method](...args);
|
|
5031
|
+
}
|
|
5032
|
+
createLoop(...args) {
|
|
5033
|
+
return this.call("createLoop", ...args);
|
|
5034
|
+
}
|
|
5035
|
+
getLoop(...args) {
|
|
5036
|
+
return this.call("getLoop", ...args);
|
|
5037
|
+
}
|
|
5038
|
+
findLoopByName(...args) {
|
|
5039
|
+
return this.call("findLoopByName", ...args);
|
|
5040
|
+
}
|
|
5041
|
+
requireLoop(...args) {
|
|
5042
|
+
return this.call("requireLoop", ...args);
|
|
5043
|
+
}
|
|
5044
|
+
listLoops(...args) {
|
|
5045
|
+
return this.call("listLoops", ...args);
|
|
5046
|
+
}
|
|
5047
|
+
dueLoops(...args) {
|
|
5048
|
+
return this.call("dueLoops", ...args);
|
|
5049
|
+
}
|
|
5050
|
+
updateLoop(...args) {
|
|
5051
|
+
return this.call("updateLoop", ...args);
|
|
5052
|
+
}
|
|
5053
|
+
renameLoop(...args) {
|
|
5054
|
+
return this.call("renameLoop", ...args);
|
|
5055
|
+
}
|
|
5056
|
+
archiveLoop(...args) {
|
|
5057
|
+
return this.call("archiveLoop", ...args);
|
|
5058
|
+
}
|
|
5059
|
+
unarchiveLoop(...args) {
|
|
5060
|
+
return this.call("unarchiveLoop", ...args);
|
|
5061
|
+
}
|
|
5062
|
+
deleteLoop(...args) {
|
|
5063
|
+
return this.call("deleteLoop", ...args);
|
|
5064
|
+
}
|
|
5065
|
+
createWorkflow(...args) {
|
|
5066
|
+
return this.call("createWorkflow", ...args);
|
|
5067
|
+
}
|
|
5068
|
+
getWorkflow(...args) {
|
|
5069
|
+
return this.call("getWorkflow", ...args);
|
|
5070
|
+
}
|
|
5071
|
+
listWorkflows(...args) {
|
|
5072
|
+
return this.call("listWorkflows", ...args);
|
|
5073
|
+
}
|
|
5074
|
+
countWorkflows(...args) {
|
|
5075
|
+
return this.call("countWorkflows", ...args);
|
|
5076
|
+
}
|
|
5077
|
+
archiveWorkflow(...args) {
|
|
5078
|
+
return this.call("archiveWorkflow", ...args);
|
|
5079
|
+
}
|
|
5080
|
+
createWorkflowInvocation(...args) {
|
|
5081
|
+
return this.call("createWorkflowInvocation", ...args);
|
|
5082
|
+
}
|
|
5083
|
+
getWorkflowInvocation(...args) {
|
|
5084
|
+
return this.call("getWorkflowInvocation", ...args);
|
|
5085
|
+
}
|
|
5086
|
+
listWorkflowInvocations(...args) {
|
|
5087
|
+
return this.call("listWorkflowInvocations", ...args);
|
|
5088
|
+
}
|
|
5089
|
+
upsertWorkflowWorkItem(...args) {
|
|
5090
|
+
return this.call("upsertWorkflowWorkItem", ...args);
|
|
5091
|
+
}
|
|
5092
|
+
getWorkflowWorkItem(...args) {
|
|
5093
|
+
return this.call("getWorkflowWorkItem", ...args);
|
|
5094
|
+
}
|
|
5095
|
+
listWorkflowWorkItems(...args) {
|
|
5096
|
+
return this.call("listWorkflowWorkItems", ...args);
|
|
5097
|
+
}
|
|
5098
|
+
countActiveWorkflowWorkItems(...args) {
|
|
5099
|
+
return this.call("countActiveWorkflowWorkItems", ...args);
|
|
5100
|
+
}
|
|
5101
|
+
admitWorkflowWorkItem(...args) {
|
|
5102
|
+
return this.call("admitWorkflowWorkItem", ...args);
|
|
5103
|
+
}
|
|
5104
|
+
createGoal(...args) {
|
|
5105
|
+
return this.call("createGoal", ...args);
|
|
5106
|
+
}
|
|
5107
|
+
getGoal(...args) {
|
|
5108
|
+
return this.call("getGoal", ...args);
|
|
5109
|
+
}
|
|
5110
|
+
listGoals(...args) {
|
|
5111
|
+
return this.call("listGoals", ...args);
|
|
5112
|
+
}
|
|
5113
|
+
createGoalPlanNodes(...args) {
|
|
5114
|
+
return this.call("createGoalPlanNodes", ...args);
|
|
5115
|
+
}
|
|
5116
|
+
listGoalPlanNodes(...args) {
|
|
5117
|
+
return this.call("listGoalPlanNodes", ...args);
|
|
5118
|
+
}
|
|
5119
|
+
updateGoalStatus(...args) {
|
|
5120
|
+
return this.call("updateGoalStatus", ...args);
|
|
5121
|
+
}
|
|
5122
|
+
updateGoalPlanNode(...args) {
|
|
5123
|
+
return this.call("updateGoalPlanNode", ...args);
|
|
5124
|
+
}
|
|
5125
|
+
recordGoalEvent(...args) {
|
|
5126
|
+
return this.call("recordGoalEvent", ...args);
|
|
5127
|
+
}
|
|
5128
|
+
listGoalRuns(...args) {
|
|
5129
|
+
return this.call("listGoalRuns", ...args);
|
|
5130
|
+
}
|
|
5131
|
+
createWorkflowRun(...args) {
|
|
5132
|
+
return this.call("createWorkflowRun", ...args);
|
|
5133
|
+
}
|
|
5134
|
+
getWorkflowRun(...args) {
|
|
5135
|
+
return this.call("getWorkflowRun", ...args);
|
|
5136
|
+
}
|
|
5137
|
+
listWorkflowRuns(...args) {
|
|
5138
|
+
return this.call("listWorkflowRuns", ...args);
|
|
5139
|
+
}
|
|
5140
|
+
listWorkflowStepRuns(...args) {
|
|
5141
|
+
return this.call("listWorkflowStepRuns", ...args);
|
|
5142
|
+
}
|
|
5143
|
+
getWorkflowStepRun(...args) {
|
|
5144
|
+
return this.call("getWorkflowStepRun", ...args);
|
|
5145
|
+
}
|
|
5146
|
+
startWorkflowStepRun(...args) {
|
|
5147
|
+
return this.call("startWorkflowStepRun", ...args);
|
|
5148
|
+
}
|
|
5149
|
+
recoverWorkflowRun(...args) {
|
|
5150
|
+
return this.call("recoverWorkflowRun", ...args);
|
|
5151
|
+
}
|
|
5152
|
+
finalizeWorkflowStepRun(...args) {
|
|
5153
|
+
return this.call("finalizeWorkflowStepRun", ...args);
|
|
5154
|
+
}
|
|
5155
|
+
finalizeWorkflowRun(...args) {
|
|
5156
|
+
return this.call("finalizeWorkflowRun", ...args);
|
|
5157
|
+
}
|
|
5158
|
+
appendWorkflowEvent(...args) {
|
|
5159
|
+
return this.call("appendWorkflowEvent", ...args);
|
|
5160
|
+
}
|
|
5161
|
+
listWorkflowEvents(...args) {
|
|
5162
|
+
return this.call("listWorkflowEvents", ...args);
|
|
5163
|
+
}
|
|
5164
|
+
recordRunProcess(...args) {
|
|
5165
|
+
return this.call("recordRunProcess", ...args);
|
|
5166
|
+
}
|
|
5167
|
+
createSkippedRun(...args) {
|
|
5168
|
+
return this.call("createSkippedRun", ...args);
|
|
5169
|
+
}
|
|
5170
|
+
getRun(...args) {
|
|
5171
|
+
return this.call("getRun", ...args);
|
|
5172
|
+
}
|
|
5173
|
+
getRunBySlot(...args) {
|
|
5174
|
+
return this.call("getRunBySlot", ...args);
|
|
5175
|
+
}
|
|
5176
|
+
claimRun(...args) {
|
|
5177
|
+
return this.call("claimRun", ...args);
|
|
5178
|
+
}
|
|
5179
|
+
finalizeRun(...args) {
|
|
5180
|
+
return this.call("finalizeRun", ...args);
|
|
5181
|
+
}
|
|
5182
|
+
heartbeatRunLease(...args) {
|
|
5183
|
+
return this.call("heartbeatRunLease", ...args);
|
|
5184
|
+
}
|
|
5185
|
+
listRuns(...args) {
|
|
5186
|
+
return this.call("listRuns", ...args);
|
|
5187
|
+
}
|
|
5188
|
+
recoverExpiredRunLeases(...args) {
|
|
5189
|
+
return this.call("recoverExpiredRunLeases", ...args);
|
|
5190
|
+
}
|
|
5191
|
+
recoverExpiredRunLeasesDetailed(...args) {
|
|
5192
|
+
return this.call("recoverExpiredRunLeasesDetailed", ...args);
|
|
5193
|
+
}
|
|
5194
|
+
countLoops(...args) {
|
|
5195
|
+
return this.call("countLoops", ...args);
|
|
5196
|
+
}
|
|
5197
|
+
countRuns(...args) {
|
|
5198
|
+
return this.call("countRuns", ...args);
|
|
5199
|
+
}
|
|
5200
|
+
pruneHistory(...args) {
|
|
5201
|
+
return this.call("pruneHistory", ...args);
|
|
5202
|
+
}
|
|
5203
|
+
acquireDaemonLease(...args) {
|
|
5204
|
+
return this.call("acquireDaemonLease", ...args);
|
|
5205
|
+
}
|
|
5206
|
+
heartbeatDaemonLease(...args) {
|
|
5207
|
+
return this.call("heartbeatDaemonLease", ...args);
|
|
5208
|
+
}
|
|
5209
|
+
releaseDaemonLease(...args) {
|
|
5210
|
+
return this.call("releaseDaemonLease", ...args);
|
|
5211
|
+
}
|
|
5212
|
+
getDaemonLease(...args) {
|
|
5213
|
+
return this.call("getDaemonLease", ...args);
|
|
5214
|
+
}
|
|
5215
|
+
}
|
|
5216
|
+
function createSqliteLoopStorage(path) {
|
|
5217
|
+
return new SqliteLoopStorage(new Store(path));
|
|
5218
|
+
}
|
|
5219
|
+
// src/lib/storage/postgres-loop-storage.ts
|
|
5220
|
+
import pgLib from "pg";
|
|
5221
|
+
var { types: pgTypes } = pgLib;
|
|
5222
|
+
pgTypes.setTypeParser(3802, (v) => v);
|
|
5223
|
+
pgTypes.setTypeParser(114, (v) => v);
|
|
5224
|
+
var toIso = (v) => v == null ? null : new Date(v).toISOString();
|
|
5225
|
+
pgTypes.setTypeParser(1184, (v) => toIso(v));
|
|
5226
|
+
pgTypes.setTypeParser(1114, (v) => toIso(v));
|
|
5227
|
+
|
|
5228
|
+
class NotImplementedError extends Error {
|
|
5229
|
+
code = "not_implemented";
|
|
5230
|
+
constructor(method) {
|
|
5231
|
+
super(`PostgresLoopStorage.${method} is not implemented on the Postgres backend yet. ` + `This path must not silently no-op; port the sqlite Store.${method} logic.`);
|
|
5232
|
+
this.name = "NotImplementedError";
|
|
5233
|
+
}
|
|
5234
|
+
}
|
|
5235
|
+
var TERMINAL_RUN_STATUSES2 = ["succeeded", "failed", "timed_out", "abandoned", "skipped"];
|
|
5236
|
+
var PRUNE_BATCH_SIZE2 = 400;
|
|
5237
|
+
var DEFAULT_RECOVERY_BATCH_LIMIT2 = 100;
|
|
5238
|
+
var DEFAULT_RECOVERY_SCAN_MULTIPLIER2 = 5;
|
|
5239
|
+
|
|
5240
|
+
class PostgresLoopStorage {
|
|
5241
|
+
client;
|
|
5242
|
+
backend = "postgres";
|
|
5243
|
+
supportsRemoteRunners = true;
|
|
5244
|
+
constructor(client) {
|
|
5245
|
+
this.client = client;
|
|
5246
|
+
}
|
|
5247
|
+
async close() {
|
|
5248
|
+
await this.client.close();
|
|
5249
|
+
}
|
|
5250
|
+
async assertDaemonLeaseFence(c, opts, now) {
|
|
5251
|
+
if (!opts.daemonLeaseId)
|
|
5252
|
+
return;
|
|
5253
|
+
const row = await c.get("SELECT id FROM daemon_lease WHERE id = $1 AND expires_at > $2", [opts.daemonLeaseId, now]);
|
|
5254
|
+
if (!row)
|
|
5255
|
+
throw new Error("daemon lease lost");
|
|
5256
|
+
}
|
|
5257
|
+
async loadLoop(c, id) {
|
|
5258
|
+
const row = await c.get("SELECT * FROM loops WHERE id = $1", [id]);
|
|
5259
|
+
return row ? rowToLoop(row) : undefined;
|
|
5260
|
+
}
|
|
5261
|
+
async loadRun(c, id) {
|
|
5262
|
+
const row = await c.get("SELECT * FROM loop_runs WHERE id = $1", [id]);
|
|
5263
|
+
return row ? rowToRun(row) : undefined;
|
|
5264
|
+
}
|
|
5265
|
+
async loadRunBySlot(c, loopId, scheduledFor) {
|
|
5266
|
+
const row = await c.get("SELECT * FROM loop_runs WHERE loop_id = $1 AND scheduled_for = $2", [loopId, scheduledFor]);
|
|
5267
|
+
return row ? rowToRun(row) : undefined;
|
|
5268
|
+
}
|
|
5269
|
+
async loadDaemonLease(c) {
|
|
5270
|
+
const row = await c.get("SELECT * FROM daemon_lease LIMIT 1", []);
|
|
5271
|
+
return row ? rowToLease(row) : undefined;
|
|
5272
|
+
}
|
|
5273
|
+
async setWorkItemsForLoop(c, loopId, status, reason, updated, statuses = ["admitted", "running"]) {
|
|
5274
|
+
const placeholders = statuses.map((_, i) => `$${i + 5}`).join(",");
|
|
5275
|
+
await c.execute(`UPDATE workflow_work_items
|
|
5276
|
+
SET status=$1, lease_expires_at=NULL, last_reason=COALESCE($2, last_reason), updated_at=$3
|
|
5277
|
+
WHERE loop_id = $4 AND status IN (${placeholders})`, [status, reason ?? null, updated, loopId, ...statuses]);
|
|
5278
|
+
}
|
|
5279
|
+
async setWorkItemsForWorkflowRun(c, workflowRunId, status, reason, updated, statuses = ["admitted", "running"]) {
|
|
5280
|
+
const placeholders = statuses.map((_, i) => `$${i + 5}`).join(",");
|
|
5281
|
+
await c.execute(`UPDATE workflow_work_items
|
|
5282
|
+
SET status=$1, lease_expires_at=NULL, last_reason=COALESCE($2, last_reason), updated_at=$3
|
|
5283
|
+
WHERE workflow_run_id = $4 AND status IN (${placeholders})`, [status, reason ?? null, updated, workflowRunId, ...statuses]);
|
|
5284
|
+
}
|
|
5285
|
+
async cascadeWorkItemsForLoopRun(c, run, reason, updated) {
|
|
5286
|
+
const loop = await this.loadLoop(c, run.loopId);
|
|
5287
|
+
const status = workItemStatusForLoopRun(run.status, run.attempt, loop?.maxAttempts);
|
|
5288
|
+
if (!status)
|
|
5289
|
+
return;
|
|
5290
|
+
const statuses = status === "admitted" ? ["admitted", "running", "failed"] : ["admitted", "running"];
|
|
5291
|
+
const nextReason = status === "admitted" ? reason ? `attempt failed; retry pending: ${reason}` : "attempt failed; retry pending" : reason;
|
|
5292
|
+
await this.setWorkItemsForLoop(c, run.loopId, status, nextReason, updated, statuses);
|
|
5293
|
+
}
|
|
5294
|
+
async createLoop(...args) {
|
|
5295
|
+
const [input, from = new Date] = args;
|
|
5296
|
+
const now = nowIso();
|
|
5297
|
+
const target = input.target.type === "workflow" ? input.target : normalizeCreateWorkflowInput({
|
|
5298
|
+
name: "loop-target-validation",
|
|
5299
|
+
steps: [{ id: "target", target: input.target }]
|
|
5300
|
+
}).steps[0].target;
|
|
5301
|
+
if (input.goal && target.type === "workflow") {
|
|
5302
|
+
const workflow = await this.loadWorkflow(this.client, target.workflowId);
|
|
5303
|
+
if (workflow?.goal) {
|
|
5304
|
+
throw new Error(`workflow loop cannot define a loop-level goal when workflow ${workflow.name} already has a top-level goal; remove one goal wrapper`);
|
|
5305
|
+
}
|
|
5306
|
+
}
|
|
5307
|
+
const loop = {
|
|
5308
|
+
id: genId(),
|
|
5309
|
+
name: input.name,
|
|
5310
|
+
description: input.description,
|
|
5311
|
+
status: "active",
|
|
5312
|
+
schedule: input.schedule,
|
|
5313
|
+
target,
|
|
5314
|
+
goal: input.goal,
|
|
5315
|
+
machine: input.machine,
|
|
5316
|
+
nextRunAt: initialNextRun(input.schedule, from),
|
|
5317
|
+
catchUp: input.catchUp ?? "latest",
|
|
5318
|
+
catchUpLimit: input.catchUpLimit ?? 50,
|
|
5319
|
+
overlap: input.overlap ?? "skip",
|
|
5320
|
+
maxAttempts: input.maxAttempts ?? 1,
|
|
5321
|
+
retryDelayMs: input.retryDelayMs ?? 60000,
|
|
5322
|
+
leaseMs: input.leaseMs ?? 30 * 60000,
|
|
5323
|
+
expiresAt: input.expiresAt,
|
|
5324
|
+
createdAt: now,
|
|
5325
|
+
updatedAt: now
|
|
5326
|
+
};
|
|
5327
|
+
await this.client.execute(`INSERT INTO loops (id, name, description, status, schedule_json, target_json, machine_json, next_run_at, retry_scheduled_for,
|
|
5328
|
+
goal_json, catch_up, catch_up_limit, overlap, max_attempts, retry_delay_ms, lease_ms, expires_at, created_at, updated_at)
|
|
5329
|
+
VALUES ($1,$2,$3,$4,$5::jsonb,$6::jsonb,$7::jsonb,$8,NULL,$9::jsonb,$10,$11,$12,$13,$14,$15,$16,$17,$18)`, [
|
|
5330
|
+
loop.id,
|
|
5331
|
+
loop.name,
|
|
5332
|
+
loop.description ?? null,
|
|
5333
|
+
loop.status,
|
|
5334
|
+
JSON.stringify(loop.schedule),
|
|
5335
|
+
JSON.stringify(loop.target),
|
|
5336
|
+
loop.machine ? JSON.stringify(loop.machine) : null,
|
|
5337
|
+
loop.nextRunAt ?? null,
|
|
5338
|
+
loop.goal ? JSON.stringify(loop.goal) : null,
|
|
5339
|
+
loop.catchUp,
|
|
5340
|
+
loop.catchUpLimit,
|
|
5341
|
+
loop.overlap,
|
|
5342
|
+
loop.maxAttempts,
|
|
5343
|
+
loop.retryDelayMs,
|
|
5344
|
+
loop.leaseMs,
|
|
5345
|
+
loop.expiresAt ?? null,
|
|
5346
|
+
loop.createdAt,
|
|
5347
|
+
loop.updatedAt
|
|
5348
|
+
]);
|
|
5349
|
+
return loop;
|
|
5350
|
+
}
|
|
5351
|
+
async getLoop(...args) {
|
|
5352
|
+
return this.loadLoop(this.client, args[0]);
|
|
5353
|
+
}
|
|
5354
|
+
async findLoopByName(...args) {
|
|
5355
|
+
const row = await this.client.get("SELECT * FROM loops WHERE name = $1 ORDER BY created_at DESC LIMIT 1", [args[0]]);
|
|
5356
|
+
return row ? rowToLoop(row) : undefined;
|
|
5357
|
+
}
|
|
5358
|
+
async requireLoop(...args) {
|
|
5359
|
+
const idOrName = args[0];
|
|
5360
|
+
const found = await this.getLoop(idOrName) ?? await this.findLoopByName(idOrName);
|
|
5361
|
+
if (!found)
|
|
5362
|
+
throw new LoopNotFoundError(idOrName);
|
|
5363
|
+
return found;
|
|
5364
|
+
}
|
|
5365
|
+
async listLoops(...args) {
|
|
5366
|
+
const opts = args[0] ?? {};
|
|
5367
|
+
const limit = opts.limit ?? 200;
|
|
5368
|
+
let rows;
|
|
5369
|
+
if (opts.status && opts.archived) {
|
|
5370
|
+
rows = await this.client.many("SELECT * FROM loops WHERE status = $1 AND archived_at IS NOT NULL ORDER BY next_run_at ASC LIMIT $2", [opts.status, limit]);
|
|
5371
|
+
} else if (opts.status && opts.includeArchived) {
|
|
5372
|
+
rows = await this.client.many("SELECT * FROM loops WHERE status = $1 ORDER BY next_run_at ASC LIMIT $2", [opts.status, limit]);
|
|
5373
|
+
} else if (opts.status) {
|
|
5374
|
+
rows = await this.client.many("SELECT * FROM loops WHERE status = $1 AND archived_at IS NULL ORDER BY next_run_at ASC LIMIT $2", [opts.status, limit]);
|
|
5375
|
+
} else if (opts.archived) {
|
|
5376
|
+
rows = await this.client.many("SELECT * FROM loops WHERE archived_at IS NOT NULL ORDER BY archived_at DESC LIMIT $1", [limit]);
|
|
5377
|
+
} else if (opts.includeArchived) {
|
|
5378
|
+
rows = await this.client.many("SELECT * FROM loops ORDER BY status ASC, next_run_at ASC LIMIT $1", [limit]);
|
|
5379
|
+
} else {
|
|
5380
|
+
rows = await this.client.many("SELECT * FROM loops WHERE archived_at IS NULL ORDER BY status ASC, next_run_at ASC LIMIT $1", [limit]);
|
|
5381
|
+
}
|
|
5382
|
+
return rows.map(rowToLoop);
|
|
5383
|
+
}
|
|
5384
|
+
async dueLoops(...args) {
|
|
5385
|
+
const [now, limit = 500] = args;
|
|
5386
|
+
const rows = await this.client.many(`SELECT * FROM loops
|
|
5387
|
+
WHERE status = 'active' AND archived_at IS NULL AND next_run_at IS NOT NULL AND next_run_at <= $1
|
|
5388
|
+
ORDER BY next_run_at ASC LIMIT $2`, [now.toISOString(), limit]);
|
|
5389
|
+
return rows.map(rowToLoop);
|
|
5390
|
+
}
|
|
5391
|
+
async updateLoop(...args) {
|
|
5392
|
+
const [id, patch, opts = {}] = args;
|
|
5393
|
+
const updated = (opts.now ?? new Date).toISOString();
|
|
5394
|
+
return this.client.transaction(async (c) => {
|
|
5395
|
+
const current = await this.loadLoop(c, id);
|
|
5396
|
+
if (!current)
|
|
5397
|
+
throw new LoopNotFoundError(id);
|
|
5398
|
+
if (current.archivedAt)
|
|
5399
|
+
throw new LoopArchivedError(current.name || id);
|
|
5400
|
+
const merged = { ...current, ...patch, updatedAt: updated };
|
|
5401
|
+
const res = await c.query(`UPDATE loops SET status=$1, next_run_at=$2, retry_scheduled_for=$3, expires_at=$4, updated_at=$5
|
|
5402
|
+
WHERE id=$6
|
|
5403
|
+
AND ($7::text IS NULL OR EXISTS (SELECT 1 FROM daemon_lease WHERE id=$7 AND expires_at > $8))`, [
|
|
5404
|
+
merged.status,
|
|
5405
|
+
merged.nextRunAt ?? null,
|
|
5406
|
+
merged.retryScheduledFor ?? null,
|
|
5407
|
+
merged.expiresAt ?? null,
|
|
5408
|
+
merged.updatedAt,
|
|
5409
|
+
id,
|
|
5410
|
+
opts.daemonLeaseId ?? null,
|
|
5411
|
+
updated
|
|
5412
|
+
]);
|
|
5413
|
+
if (res.rowCount !== 1)
|
|
5414
|
+
throw new Error("daemon lease lost");
|
|
5415
|
+
if (patch.status && patch.status !== "active") {
|
|
5416
|
+
const status = patch.status === "paused" ? "deferred" : "cancelled";
|
|
5417
|
+
await this.setWorkItemsForLoop(c, id, status, `loop ${patch.status}`, updated);
|
|
5418
|
+
}
|
|
5419
|
+
const after = await this.loadLoop(c, id);
|
|
5420
|
+
if (!after)
|
|
5421
|
+
throw new Error(`loop not found after update: ${id}`);
|
|
5422
|
+
return after;
|
|
5423
|
+
});
|
|
5424
|
+
}
|
|
5425
|
+
async renameLoop(...args) {
|
|
5426
|
+
const [id, name, opts = {}] = args;
|
|
5427
|
+
const current = await this.getLoop(id);
|
|
5428
|
+
if (!current)
|
|
5429
|
+
throw new LoopNotFoundError(id);
|
|
5430
|
+
const trimmed = name.trim();
|
|
5431
|
+
if (!trimmed)
|
|
5432
|
+
throw new ValidationError("loop name must not be empty");
|
|
5433
|
+
const updated = (opts.now ?? new Date).toISOString();
|
|
5434
|
+
await this.client.execute(`UPDATE loops SET name=$1, updated_at=$2
|
|
5435
|
+
WHERE id=$3 AND ($4::text IS NULL OR EXISTS (SELECT 1 FROM daemon_lease WHERE id=$4 AND expires_at > $5))`, [trimmed, updated, id, opts.daemonLeaseId ?? null, updated]);
|
|
5436
|
+
const after = await this.getLoop(id);
|
|
5437
|
+
if (!after)
|
|
5438
|
+
throw new Error(`loop not found after rename: ${id}`);
|
|
5439
|
+
return after;
|
|
5440
|
+
}
|
|
5441
|
+
async archiveLoop(...args) {
|
|
5442
|
+
const idOrName = args[0];
|
|
5443
|
+
return this.client.transaction(async (c) => {
|
|
5444
|
+
const loop = await this.requireLoopIn(c, idOrName);
|
|
5445
|
+
if (loop.archivedAt)
|
|
5446
|
+
return loop;
|
|
5447
|
+
const updated = nowIso();
|
|
5448
|
+
const archivedStatus = loop.status === "active" ? "paused" : loop.status;
|
|
5449
|
+
await c.execute(`UPDATE loops SET status=$1, archived_at=$2, archived_from_status=$3, updated_at=$4 WHERE id=$5`, [archivedStatus, updated, loop.status, updated, loop.id]);
|
|
5450
|
+
await this.setWorkItemsForLoop(c, loop.id, "deferred", "loop archived", updated);
|
|
5451
|
+
const archived = await this.loadLoop(c, loop.id);
|
|
5452
|
+
if (!archived)
|
|
5453
|
+
throw new Error(`loop not found after archive: ${loop.id}`);
|
|
5454
|
+
return archived;
|
|
5455
|
+
});
|
|
5456
|
+
}
|
|
5457
|
+
async unarchiveLoop(...args) {
|
|
5458
|
+
const idOrName = args[0];
|
|
5459
|
+
const loop = await this.requireLoop(idOrName);
|
|
5460
|
+
if (!loop.archivedAt)
|
|
5461
|
+
return loop;
|
|
5462
|
+
const updated = nowIso();
|
|
5463
|
+
const restoredStatus = loop.archivedFromStatus ?? loop.status;
|
|
5464
|
+
await this.client.execute(`UPDATE loops SET status=$1, archived_at=NULL, archived_from_status=NULL, updated_at=$2 WHERE id=$3`, [restoredStatus, updated, loop.id]);
|
|
5465
|
+
const unarchived = await this.getLoop(loop.id);
|
|
5466
|
+
if (!unarchived)
|
|
5467
|
+
throw new Error(`loop not found after unarchive: ${loop.id}`);
|
|
5468
|
+
return unarchived;
|
|
5469
|
+
}
|
|
5470
|
+
async deleteLoop(...args) {
|
|
5471
|
+
const idOrName = args[0];
|
|
5472
|
+
return this.client.transaction(async (c) => {
|
|
5473
|
+
const loop = await this.requireLoopIn(c, idOrName);
|
|
5474
|
+
await this.setWorkItemsForLoop(c, loop.id, "cancelled", "loop deleted", nowIso());
|
|
5475
|
+
const res = await c.query(`DELETE FROM loops WHERE id = $1`, [loop.id]);
|
|
5476
|
+
return res.rowCount > 0;
|
|
5477
|
+
});
|
|
5478
|
+
}
|
|
5479
|
+
async requireLoopIn(c, idOrName) {
|
|
5480
|
+
const byId = await this.loadLoop(c, idOrName);
|
|
5481
|
+
if (byId)
|
|
5482
|
+
return byId;
|
|
5483
|
+
const row = await c.get("SELECT * FROM loops WHERE name = $1 ORDER BY created_at DESC LIMIT 1", [idOrName]);
|
|
5484
|
+
if (!row)
|
|
5485
|
+
throw new LoopNotFoundError(idOrName);
|
|
5486
|
+
return rowToLoop(row);
|
|
5487
|
+
}
|
|
5488
|
+
async countLoops(...args) {
|
|
5489
|
+
const [status, opts = {}] = args;
|
|
5490
|
+
let sql;
|
|
5491
|
+
const params = [];
|
|
5492
|
+
if (status && opts.archived) {
|
|
5493
|
+
sql = "SELECT COUNT(*)::int AS count FROM loops WHERE status = $1 AND archived_at IS NOT NULL";
|
|
5494
|
+
params.push(status);
|
|
5495
|
+
} else if (status && opts.includeArchived) {
|
|
5496
|
+
sql = "SELECT COUNT(*)::int AS count FROM loops WHERE status = $1";
|
|
5497
|
+
params.push(status);
|
|
5498
|
+
} else if (status) {
|
|
5499
|
+
sql = "SELECT COUNT(*)::int AS count FROM loops WHERE status = $1 AND archived_at IS NULL";
|
|
5500
|
+
params.push(status);
|
|
5501
|
+
} else if (opts.archived) {
|
|
5502
|
+
sql = "SELECT COUNT(*)::int AS count FROM loops WHERE archived_at IS NOT NULL";
|
|
5503
|
+
} else if (opts.includeArchived) {
|
|
5504
|
+
sql = "SELECT COUNT(*)::int AS count FROM loops";
|
|
5505
|
+
} else {
|
|
5506
|
+
sql = "SELECT COUNT(*)::int AS count FROM loops WHERE archived_at IS NULL";
|
|
5507
|
+
}
|
|
5508
|
+
const row = await this.client.get(sql, params);
|
|
5509
|
+
return row?.count ?? 0;
|
|
5510
|
+
}
|
|
5511
|
+
async createSkippedRun(...args) {
|
|
5512
|
+
const [loop, scheduledFor, reason, opts = {}] = args;
|
|
5513
|
+
const now = nowIso();
|
|
5514
|
+
const id = genId();
|
|
5515
|
+
return this.client.transaction(async (c) => {
|
|
5516
|
+
await this.assertDaemonLeaseFence(c, opts, now);
|
|
5517
|
+
await c.execute(`INSERT INTO loop_runs (id, loop_id, loop_name, scheduled_for, attempt, status, started_at, finished_at,
|
|
5518
|
+
claimed_by, lease_expires_at, pid, exit_code, duration_ms, stdout, stderr, error, created_at, updated_at)
|
|
5519
|
+
VALUES ($1,$2,$3,$4,1,'skipped',NULL,$5,NULL,NULL,NULL,NULL,NULL,NULL,NULL,$6,$7,$7)
|
|
5520
|
+
ON CONFLICT (loop_id, scheduled_for) DO NOTHING`, [id, loop.id, loop.name, scheduledFor, now, reason, now]);
|
|
5521
|
+
const run = await this.loadRunBySlot(c, loop.id, scheduledFor);
|
|
5522
|
+
if (run)
|
|
5523
|
+
return run;
|
|
5524
|
+
return {
|
|
5525
|
+
id,
|
|
5526
|
+
loopId: loop.id,
|
|
5527
|
+
loopName: loop.name,
|
|
5528
|
+
scheduledFor,
|
|
5529
|
+
attempt: 1,
|
|
5530
|
+
status: "skipped",
|
|
5531
|
+
finishedAt: now,
|
|
5532
|
+
error: reason,
|
|
5533
|
+
createdAt: now,
|
|
5534
|
+
updatedAt: now
|
|
5535
|
+
};
|
|
5536
|
+
});
|
|
5537
|
+
}
|
|
5538
|
+
async getRun(...args) {
|
|
5539
|
+
return this.loadRun(this.client, args[0]);
|
|
5540
|
+
}
|
|
5541
|
+
async getRunBySlot(...args) {
|
|
5542
|
+
return this.loadRunBySlot(this.client, args[0], args[1]);
|
|
5543
|
+
}
|
|
5544
|
+
async claimRun(...args) {
|
|
5545
|
+
const [loopArg, scheduledFor, runnerId, now = new Date, opts = {}] = args;
|
|
5546
|
+
const startedAt = now.toISOString();
|
|
5547
|
+
const claimToken = opts.claimToken ?? genId();
|
|
5548
|
+
return this.client.transaction(async (c) => {
|
|
5549
|
+
await this.assertDaemonLeaseFence(c, opts, startedAt);
|
|
5550
|
+
const loop = await this.loadLoop(c, loopArg.id);
|
|
5551
|
+
if (!loop || loop.archivedAt)
|
|
5552
|
+
return;
|
|
5553
|
+
const leaseExpiresAt = new Date(now.getTime() + loop.leaseMs).toISOString();
|
|
5554
|
+
if (loop.overlap === "skip") {
|
|
5555
|
+
const blocking = await c.get(`SELECT id FROM loop_runs
|
|
5556
|
+
WHERE loop_id=$1 AND scheduled_for<>$2 AND status='running'
|
|
5557
|
+
AND lease_expires_at IS NOT NULL AND lease_expires_at > $3
|
|
5558
|
+
LIMIT 1`, [loop.id, scheduledFor, startedAt]);
|
|
5559
|
+
if (blocking)
|
|
5560
|
+
return;
|
|
5561
|
+
}
|
|
5562
|
+
const existing = await c.get("SELECT * FROM loop_runs WHERE loop_id=$1 AND scheduled_for=$2 FOR UPDATE", [loop.id, scheduledFor]);
|
|
5563
|
+
if (existing) {
|
|
5564
|
+
if (existing.status === "running") {
|
|
5565
|
+
if (existing.lease_expires_at && existing.lease_expires_at > startedAt) {
|
|
5566
|
+
return;
|
|
5567
|
+
}
|
|
5568
|
+
const res3 = await c.query(`UPDATE loop_runs SET status='running', started_at=$2, finished_at=NULL, claimed_by=$3, claim_token=$4,
|
|
5569
|
+
lease_expires_at=$5, pid=NULL, pgid=NULL, process_started_at=NULL, exit_code=NULL, duration_ms=NULL,
|
|
5570
|
+
stdout=NULL, stderr=NULL, error=NULL, updated_at=$2
|
|
5571
|
+
WHERE id=$1 AND status='running' AND lease_expires_at <= $6`, [existing.id, startedAt, runnerId, claimToken, leaseExpiresAt, startedAt]);
|
|
5572
|
+
if (res3.rowCount !== 1)
|
|
5573
|
+
return;
|
|
5574
|
+
const run3 = await this.loadRun(c, existing.id);
|
|
5575
|
+
return run3 ? { run: run3, loop, claimToken } : undefined;
|
|
5576
|
+
}
|
|
5577
|
+
if (existing.status === "succeeded" || existing.status === "skipped")
|
|
5578
|
+
return;
|
|
5579
|
+
const attempt = existing.attempt + 1;
|
|
5580
|
+
const res2 = await c.query(`UPDATE loop_runs SET attempt=$2, status='running', started_at=$3, finished_at=NULL, claimed_by=$4, claim_token=$5,
|
|
5581
|
+
lease_expires_at=$6, pid=NULL, pgid=NULL, process_started_at=NULL, exit_code=NULL, duration_ms=NULL,
|
|
5582
|
+
stdout=NULL, stderr=NULL, error=NULL, updated_at=$3
|
|
5583
|
+
WHERE id=$1 AND status IN ('failed','timed_out','abandoned') AND attempt < $7`, [existing.id, attempt, startedAt, runnerId, claimToken, leaseExpiresAt, loop.maxAttempts]);
|
|
5584
|
+
if (res2.rowCount !== 1)
|
|
5585
|
+
return;
|
|
5586
|
+
const run2 = await this.loadRun(c, existing.id);
|
|
5587
|
+
return run2 ? { run: run2, loop, claimToken } : undefined;
|
|
5588
|
+
}
|
|
5589
|
+
const id = genId();
|
|
5590
|
+
const res = await c.query(`INSERT INTO loop_runs (id, loop_id, loop_name, scheduled_for, attempt, status, started_at, finished_at,
|
|
5591
|
+
claimed_by, claim_token, lease_expires_at, pid, exit_code, duration_ms, stdout, stderr, error, created_at, updated_at)
|
|
5592
|
+
VALUES ($1,$2,$3,$4,1,'running',$5,NULL,$6,$7,$8,NULL,NULL,NULL,NULL,NULL,NULL,$5,$5)
|
|
5593
|
+
ON CONFLICT (loop_id, scheduled_for) DO NOTHING`, [id, loop.id, loop.name, scheduledFor, startedAt, runnerId, claimToken, leaseExpiresAt]);
|
|
5594
|
+
if (res.rowCount !== 1)
|
|
5595
|
+
return;
|
|
5596
|
+
const run = await this.loadRun(c, id);
|
|
5597
|
+
return run ? { run, loop, claimToken } : undefined;
|
|
5598
|
+
});
|
|
5599
|
+
}
|
|
5600
|
+
async finalizeRun(...args) {
|
|
5601
|
+
const [id, patch, opts = {}] = args;
|
|
5602
|
+
const finishedAt = patch.finishedAt ?? nowIso();
|
|
5603
|
+
const error = patch.error === undefined ? undefined : persistedRunOutput(patch.error) ?? undefined;
|
|
5604
|
+
const nowStr = (opts.now ?? new Date).toISOString();
|
|
5605
|
+
return this.client.transaction(async (c) => {
|
|
5606
|
+
let res;
|
|
5607
|
+
if (opts.claimedBy) {
|
|
5608
|
+
res = await c.query(`UPDATE loop_runs SET status=$2, finished_at=$3, claim_token=NULL, lease_expires_at=NULL, pid=$4, exit_code=$5,
|
|
5609
|
+
duration_ms=$6, stdout=$7, stderr=$8, error=$9, updated_at=$3
|
|
5610
|
+
WHERE id=$1 AND status='running' AND claimed_by=$10 AND lease_expires_at > $11
|
|
5611
|
+
AND ($12::text IS NULL OR claim_token=$12)
|
|
5612
|
+
AND ($13::text IS NULL OR EXISTS (SELECT 1 FROM daemon_lease WHERE id=$13 AND expires_at > $11))`, [
|
|
5613
|
+
id,
|
|
5614
|
+
patch.status,
|
|
5615
|
+
finishedAt,
|
|
5616
|
+
patch.pid ?? null,
|
|
5617
|
+
patch.exitCode ?? null,
|
|
5618
|
+
patch.durationMs ?? null,
|
|
5619
|
+
persistedRunOutput(patch.stdout),
|
|
5620
|
+
persistedRunOutput(patch.stderr),
|
|
5621
|
+
error ?? null,
|
|
5622
|
+
opts.claimedBy,
|
|
5623
|
+
nowStr,
|
|
5624
|
+
opts.claimToken ?? null,
|
|
5625
|
+
opts.daemonLeaseId ?? null
|
|
5626
|
+
]);
|
|
5627
|
+
} else {
|
|
5628
|
+
res = await c.query(`UPDATE loop_runs SET status=$2, finished_at=$3, claim_token=NULL, lease_expires_at=NULL, pid=$4, exit_code=$5,
|
|
5629
|
+
duration_ms=$6, stdout=$7, stderr=$8, error=$9, updated_at=$3
|
|
5630
|
+
WHERE id=$1 AND status='running'`, [
|
|
5631
|
+
id,
|
|
5632
|
+
patch.status,
|
|
5633
|
+
finishedAt,
|
|
5634
|
+
patch.pid ?? null,
|
|
5635
|
+
patch.exitCode ?? null,
|
|
5636
|
+
patch.durationMs ?? null,
|
|
5637
|
+
persistedRunOutput(patch.stdout),
|
|
5638
|
+
persistedRunOutput(patch.stderr),
|
|
5639
|
+
error ?? null
|
|
5640
|
+
]);
|
|
5641
|
+
}
|
|
5642
|
+
const run = await this.loadRun(c, id);
|
|
5643
|
+
if (!run)
|
|
5644
|
+
throw new Error(`run not found after finalize: ${id}`);
|
|
5645
|
+
if (opts.claimedBy && res.rowCount !== 1)
|
|
5646
|
+
return run;
|
|
5647
|
+
if (res.rowCount === 1) {
|
|
5648
|
+
await this.cascadeWorkItemsForLoopRun(c, run, error, finishedAt);
|
|
5649
|
+
}
|
|
5650
|
+
return run;
|
|
5651
|
+
});
|
|
5652
|
+
}
|
|
5653
|
+
async heartbeatRunLease(...args) {
|
|
5654
|
+
const [id, claimedBy, leaseMs, now = new Date, opts = {}] = args;
|
|
5655
|
+
const nowStr = now.toISOString();
|
|
5656
|
+
const expiresAt = new Date(now.getTime() + leaseMs).toISOString();
|
|
5657
|
+
const res = await this.client.query(`UPDATE loop_runs SET lease_expires_at=$2, updated_at=$3
|
|
5658
|
+
WHERE id=$1 AND status='running' AND claimed_by=$4 AND lease_expires_at > $5
|
|
5659
|
+
AND ($6::text IS NULL OR claim_token=$6)
|
|
5660
|
+
AND ($7::text IS NULL OR EXISTS (SELECT 1 FROM daemon_lease WHERE id=$7 AND expires_at > $5))`, [id, expiresAt, nowStr, claimedBy, nowStr, opts.claimToken ?? null, opts.daemonLeaseId ?? null]);
|
|
5661
|
+
if (res.rowCount !== 1)
|
|
5662
|
+
return;
|
|
5663
|
+
return this.getRun(id);
|
|
5664
|
+
}
|
|
5665
|
+
async recordRunProcess(...args) {
|
|
5666
|
+
const [runId, info, opts = {}] = args;
|
|
5667
|
+
const now = (opts.now ?? new Date).toISOString();
|
|
5668
|
+
const res = await this.client.query(`UPDATE loop_runs SET pid=$2, pgid=$3, process_started_at=$4, updated_at=$5
|
|
5669
|
+
WHERE id=$1 AND status='running'
|
|
5670
|
+
AND ($6::text IS NULL OR EXISTS (SELECT 1 FROM daemon_lease WHERE id=$6 AND expires_at > $7))`, [runId, info.pid, info.pgid ?? null, info.processStartedAt ?? now, now, opts.daemonLeaseId ?? null, now]);
|
|
5671
|
+
if (res.rowCount !== 1)
|
|
5672
|
+
return;
|
|
5673
|
+
return this.getRun(runId);
|
|
5674
|
+
}
|
|
5675
|
+
async listRuns(...args) {
|
|
5676
|
+
const opts = args[0] ?? {};
|
|
5677
|
+
const limit = opts.limit ?? 100;
|
|
5678
|
+
let rows;
|
|
5679
|
+
if (opts.loopId && opts.status) {
|
|
5680
|
+
rows = await this.client.many("SELECT * FROM loop_runs WHERE loop_id = $1 AND status = $2 ORDER BY created_at DESC LIMIT $3", [opts.loopId, opts.status, limit]);
|
|
5681
|
+
} else if (opts.loopId) {
|
|
5682
|
+
rows = await this.client.many("SELECT * FROM loop_runs WHERE loop_id = $1 ORDER BY created_at DESC LIMIT $2", [opts.loopId, limit]);
|
|
5683
|
+
} else if (opts.status) {
|
|
5684
|
+
rows = await this.client.many("SELECT * FROM loop_runs WHERE status = $1 ORDER BY created_at DESC LIMIT $2", [opts.status, limit]);
|
|
5685
|
+
} else {
|
|
5686
|
+
rows = await this.client.many("SELECT * FROM loop_runs ORDER BY created_at DESC LIMIT $1", [limit]);
|
|
5687
|
+
}
|
|
5688
|
+
return rows.map(rowToRun);
|
|
5689
|
+
}
|
|
5690
|
+
async countRuns(...args) {
|
|
5691
|
+
const status = args[0];
|
|
5692
|
+
const row = status ? await this.client.get("SELECT COUNT(*)::int AS count FROM loop_runs WHERE status = $1", [status]) : await this.client.get("SELECT COUNT(*)::int AS count FROM loop_runs", []);
|
|
5693
|
+
return row?.count ?? 0;
|
|
5694
|
+
}
|
|
5695
|
+
async recoverExpiredRunLeases(...args) {
|
|
5696
|
+
const detailed = await this.recoverExpiredRunLeasesDetailed(...args);
|
|
5697
|
+
return detailed.abandoned;
|
|
5698
|
+
}
|
|
5699
|
+
async recoverExpiredRunLeasesDetailed(...args) {
|
|
5700
|
+
const [now = new Date, opts = {}] = args;
|
|
5701
|
+
const limit = Math.max(1, Math.min(1000, Math.floor(opts.limit ?? DEFAULT_RECOVERY_BATCH_LIMIT2)));
|
|
5702
|
+
const scanLimit = Math.max(limit, Math.min(5000, Math.floor(opts.scanLimit ?? limit * DEFAULT_RECOVERY_SCAN_MULTIPLIER2)));
|
|
5703
|
+
const finished = now.toISOString();
|
|
5704
|
+
const rows = await this.client.many(`SELECT * FROM loop_runs WHERE status='running' AND lease_expires_at <= $1 ORDER BY lease_expires_at ASC LIMIT $2`, [finished, scanLimit]);
|
|
5705
|
+
const recovered = [];
|
|
5706
|
+
for (const row of rows) {
|
|
5707
|
+
if (recovered.length >= limit)
|
|
5708
|
+
break;
|
|
5709
|
+
const run = await this.client.transaction(async (c) => {
|
|
5710
|
+
const res = await c.query(`UPDATE loop_runs SET status='abandoned', finished_at=$2, lease_expires_at=NULL,
|
|
5711
|
+
error='run lease expired before completion', updated_at=$2
|
|
5712
|
+
WHERE id=$1 AND status='running' AND lease_expires_at <= $3
|
|
5713
|
+
AND ($4::text IS NULL OR EXISTS (SELECT 1 FROM daemon_lease WHERE id=$4 AND expires_at > $3))`, [row.id, finished, finished, opts.daemonLeaseId ?? null]);
|
|
5714
|
+
if (res.rowCount !== 1)
|
|
5715
|
+
return;
|
|
5716
|
+
const workflowRows = await c.many("SELECT * FROM workflow_runs WHERE loop_run_id = $1 AND status NOT IN ('succeeded','failed','timed_out','cancelled')", [row.id]);
|
|
5717
|
+
for (const wf of workflowRows) {
|
|
5718
|
+
const wfRes = await c.query(`UPDATE workflow_runs SET status='failed', finished_at=$2,
|
|
5719
|
+
error='parent loop run lease expired before completion', updated_at=$2
|
|
5720
|
+
WHERE id=$1 AND status NOT IN ('succeeded','failed','timed_out','cancelled')`, [wf.id, finished]);
|
|
5721
|
+
if (wfRes.rowCount !== 1)
|
|
5722
|
+
continue;
|
|
5723
|
+
await c.execute(`UPDATE workflow_step_runs SET status='skipped', finished_at=$2, pid=NULL,
|
|
5724
|
+
error='parent loop run lease expired before completion', updated_at=$2
|
|
5725
|
+
WHERE workflow_run_id=$1 AND status IN ('pending','running')`, [wf.id, finished]);
|
|
5726
|
+
await this.setWorkItemsForWorkflowRun(c, wf.id, "failed", "parent loop run lease expired before completion", finished);
|
|
5727
|
+
}
|
|
5728
|
+
const loop = await this.loadLoop(c, row.loop_id);
|
|
5729
|
+
const itemStatus = workItemStatusForLoopRun("abandoned", row.attempt, loop?.maxAttempts);
|
|
5730
|
+
if (itemStatus) {
|
|
5731
|
+
const statuses = itemStatus === "admitted" ? ["admitted", "running", "failed"] : ["admitted", "running"];
|
|
5732
|
+
const reason = itemStatus === "admitted" ? "run lease expired before completion; retry pending" : "run lease expired before completion";
|
|
5733
|
+
await this.setWorkItemsForLoop(c, row.loop_id, itemStatus, reason, finished, statuses);
|
|
5734
|
+
}
|
|
5735
|
+
return this.loadRun(c, row.id);
|
|
5736
|
+
});
|
|
5737
|
+
if (run)
|
|
5738
|
+
recovered.push(run);
|
|
5739
|
+
}
|
|
5740
|
+
return { abandoned: recovered, deferred: [] };
|
|
5741
|
+
}
|
|
5742
|
+
async pruneHistory(...args) {
|
|
5743
|
+
const opts = args[0];
|
|
5744
|
+
const { maxAgeDays, keepPerLoop } = opts;
|
|
5745
|
+
if (maxAgeDays === undefined && keepPerLoop === undefined) {
|
|
5746
|
+
throw new ValidationError("pruneHistory requires maxAgeDays and/or keepPerLoop");
|
|
5747
|
+
}
|
|
5748
|
+
if (maxAgeDays !== undefined && (!Number.isFinite(maxAgeDays) || maxAgeDays < 0)) {
|
|
5749
|
+
throw new ValidationError(`pruneHistory maxAgeDays must be a non-negative number: ${maxAgeDays}`);
|
|
5750
|
+
}
|
|
5751
|
+
if (keepPerLoop !== undefined && (!Number.isInteger(keepPerLoop) || keepPerLoop < 0)) {
|
|
5752
|
+
throw new ValidationError(`pruneHistory keepPerLoop must be a non-negative integer: ${keepPerLoop}`);
|
|
5753
|
+
}
|
|
5754
|
+
const now = opts.now ?? new Date;
|
|
5755
|
+
const dryRun = opts.dryRun ?? false;
|
|
5756
|
+
const cutoff = maxAgeDays === undefined ? undefined : new Date(now.getTime() - maxAgeDays * 86400000).toISOString();
|
|
5757
|
+
const terminal = TERMINAL_RUN_STATUSES2.map((s) => `'${s}'`).join(",");
|
|
5758
|
+
const candidateIds = (await this.client.many(`WITH ranked AS (
|
|
5759
|
+
SELECT id, status, created_at,
|
|
5760
|
+
ROW_NUMBER() OVER (PARTITION BY loop_id ORDER BY created_at DESC, id DESC) AS recency
|
|
5761
|
+
FROM loop_runs
|
|
5762
|
+
)
|
|
5763
|
+
SELECT id FROM ranked
|
|
5764
|
+
WHERE status IN (${terminal})
|
|
5765
|
+
AND ($1::timestamptz IS NULL OR created_at < $1::timestamptz)
|
|
5766
|
+
AND ($2::int IS NULL OR recency > $2)`, [cutoff ?? null, keepPerLoop ?? null])).map((r) => r.id);
|
|
5767
|
+
const summary = {
|
|
5768
|
+
dryRun,
|
|
5769
|
+
cutoff,
|
|
5770
|
+
keepPerLoop,
|
|
5771
|
+
loopRuns: dryRun ? candidateIds.length : 0,
|
|
5772
|
+
workflowRuns: 0,
|
|
5773
|
+
goalRuns: 0
|
|
5774
|
+
};
|
|
5775
|
+
for (let offset = 0;offset < candidateIds.length; offset += PRUNE_BATCH_SIZE2) {
|
|
5776
|
+
const batch = candidateIds.slice(offset, offset + PRUNE_BATCH_SIZE2);
|
|
5777
|
+
if (dryRun) {
|
|
5778
|
+
const workflowRunIds = (await this.client.many(`SELECT id FROM workflow_runs WHERE loop_run_id = ANY($1)`, [batch])).map((r) => r.id);
|
|
5779
|
+
summary.workflowRuns += workflowRunIds.length;
|
|
5780
|
+
const gr = await this.client.get(`SELECT COUNT(*)::int AS count FROM goal_runs WHERE loop_run_id = ANY($1) OR workflow_run_id = ANY($2)`, [batch, workflowRunIds]);
|
|
5781
|
+
summary.goalRuns += gr?.count ?? 0;
|
|
5782
|
+
continue;
|
|
5783
|
+
}
|
|
5784
|
+
await this.client.transaction(async (c) => {
|
|
5785
|
+
const confirmed = (await c.many(`SELECT id FROM loop_runs WHERE id = ANY($1) AND status IN (${terminal})`, [batch])).map((r) => r.id);
|
|
5786
|
+
if (confirmed.length === 0)
|
|
5787
|
+
return;
|
|
5788
|
+
const workflowRunIds = (await c.many(`SELECT id FROM workflow_runs WHERE loop_run_id = ANY($1)`, [confirmed])).map((r) => r.id);
|
|
5789
|
+
summary.loopRuns += confirmed.length;
|
|
5790
|
+
summary.workflowRuns += workflowRunIds.length;
|
|
5791
|
+
const gr = await c.query(`DELETE FROM goal_runs WHERE loop_run_id = ANY($1) OR workflow_run_id = ANY($2)`, [confirmed, workflowRunIds]);
|
|
5792
|
+
summary.goalRuns += gr.rowCount;
|
|
5793
|
+
if (workflowRunIds.length > 0) {
|
|
5794
|
+
await c.execute(`DELETE FROM workflow_runs WHERE id = ANY($1)`, [workflowRunIds]);
|
|
5795
|
+
}
|
|
5796
|
+
await c.execute(`DELETE FROM loop_runs WHERE id = ANY($1) AND status IN (${terminal})`, [confirmed]);
|
|
5797
|
+
});
|
|
5798
|
+
}
|
|
5799
|
+
return summary;
|
|
5800
|
+
}
|
|
5801
|
+
async acquireDaemonLease(...args) {
|
|
5802
|
+
const input = args[0];
|
|
5803
|
+
const now = input.now ?? new Date;
|
|
5804
|
+
const expiresAt = new Date(now.getTime() + input.ttlMs).toISOString();
|
|
5805
|
+
return this.client.transaction(async (c) => {
|
|
5806
|
+
const existing = await c.get("SELECT * FROM daemon_lease LIMIT 1 FOR UPDATE", []);
|
|
5807
|
+
if (existing && existing.expires_at > now.toISOString() && existing.id !== input.id) {
|
|
5808
|
+
return;
|
|
5809
|
+
}
|
|
5810
|
+
await c.execute("DELETE FROM daemon_lease", []);
|
|
5811
|
+
await c.execute(`INSERT INTO daemon_lease (id, pid, hostname, heartbeat_at, expires_at, created_at, updated_at)
|
|
5812
|
+
VALUES ($1,$2,$3,$4,$5,$4,$4)`, [input.id, input.pid, input.hostname, now.toISOString(), expiresAt]);
|
|
5813
|
+
return this.loadDaemonLease(c);
|
|
5814
|
+
});
|
|
5815
|
+
}
|
|
5816
|
+
async heartbeatDaemonLease(...args) {
|
|
5817
|
+
const [id, ttlMs, now = new Date] = args;
|
|
5818
|
+
const expiresAt = new Date(now.getTime() + ttlMs).toISOString();
|
|
5819
|
+
const res = await this.client.query(`UPDATE daemon_lease SET heartbeat_at=$2, expires_at=$3, updated_at=$2 WHERE id=$1 AND expires_at > $4`, [id, now.toISOString(), expiresAt, now.toISOString()]);
|
|
5820
|
+
if (res.rowCount !== 1)
|
|
5821
|
+
return;
|
|
5822
|
+
return this.getDaemonLease();
|
|
5823
|
+
}
|
|
5824
|
+
async releaseDaemonLease(...args) {
|
|
5825
|
+
await this.client.execute("DELETE FROM daemon_lease WHERE id = $1", [args[0]]);
|
|
5826
|
+
}
|
|
5827
|
+
async getDaemonLease(...args) {
|
|
5828
|
+
return this.loadDaemonLease(this.client);
|
|
5829
|
+
}
|
|
5830
|
+
async loadWorkflow(c, id) {
|
|
5831
|
+
const row = await c.get("SELECT * FROM workflow_specs WHERE id = $1", [id]);
|
|
5832
|
+
return row ? rowToWorkflow(row) : undefined;
|
|
5833
|
+
}
|
|
5834
|
+
async getWorkflow(...args) {
|
|
5835
|
+
return this.loadWorkflow(this.client, args[0]);
|
|
5836
|
+
}
|
|
5837
|
+
async listWorkflows(...args) {
|
|
5838
|
+
const opts = args[0] ?? {};
|
|
5839
|
+
const limit = opts.limit ?? 200;
|
|
5840
|
+
const offset = opts.offset ?? 0;
|
|
5841
|
+
const rows = opts.status ? await this.client.many("SELECT * FROM workflow_specs WHERE status = $1 ORDER BY status ASC, name ASC LIMIT $2 OFFSET $3", [opts.status, limit, offset]) : await this.client.many("SELECT * FROM workflow_specs ORDER BY status ASC, name ASC LIMIT $1 OFFSET $2", [limit, offset]);
|
|
5842
|
+
return rows.map(rowToWorkflow);
|
|
5843
|
+
}
|
|
5844
|
+
async countWorkflows(...args) {
|
|
5845
|
+
const opts = args[0] ?? {};
|
|
5846
|
+
const row = opts.status ? await this.client.get("SELECT COUNT(*)::int AS count FROM workflow_specs WHERE status = $1", [opts.status]) : await this.client.get("SELECT COUNT(*)::int AS count FROM workflow_specs", []);
|
|
5847
|
+
return row?.count ?? 0;
|
|
5848
|
+
}
|
|
5849
|
+
async getWorkflowInvocation(...args) {
|
|
5850
|
+
const row = await this.client.get("SELECT * FROM workflow_invocations WHERE id = $1", [args[0]]);
|
|
5851
|
+
return row ? rowToWorkflowInvocation(row) : undefined;
|
|
5852
|
+
}
|
|
5853
|
+
async listWorkflowInvocations(...args) {
|
|
5854
|
+
const opts = args[0] ?? {};
|
|
5855
|
+
const rows = await this.client.many("SELECT * FROM workflow_invocations ORDER BY created_at DESC LIMIT $1", [opts.limit ?? 100]);
|
|
5856
|
+
return rows.map(rowToWorkflowInvocation);
|
|
5857
|
+
}
|
|
5858
|
+
async getWorkflowWorkItem(...args) {
|
|
5859
|
+
const row = await this.client.get("SELECT * FROM workflow_work_items WHERE id = $1", [args[0]]);
|
|
5860
|
+
return row ? rowToWorkflowWorkItem(row) : undefined;
|
|
5861
|
+
}
|
|
5862
|
+
async listWorkflowWorkItems(...args) {
|
|
5863
|
+
const opts = args[0] ?? {};
|
|
5864
|
+
const limit = opts.limit ?? 100;
|
|
5865
|
+
let rows;
|
|
5866
|
+
if (opts.status && opts.routeKey) {
|
|
5867
|
+
rows = await this.client.many("SELECT * FROM workflow_work_items WHERE status = $1 AND route_key = $2 ORDER BY priority DESC, created_at ASC LIMIT $3", [opts.status, opts.routeKey, limit]);
|
|
5868
|
+
} else if (opts.status) {
|
|
5869
|
+
rows = await this.client.many("SELECT * FROM workflow_work_items WHERE status = $1 ORDER BY priority DESC, created_at ASC LIMIT $2", [opts.status, limit]);
|
|
5870
|
+
} else if (opts.routeKey) {
|
|
5871
|
+
rows = await this.client.many("SELECT * FROM workflow_work_items WHERE route_key = $1 ORDER BY priority DESC, created_at ASC LIMIT $2", [opts.routeKey, limit]);
|
|
5872
|
+
} else {
|
|
5873
|
+
rows = await this.client.many("SELECT * FROM workflow_work_items ORDER BY priority DESC, created_at ASC LIMIT $1", [limit]);
|
|
5874
|
+
}
|
|
5875
|
+
return rows.map(rowToWorkflowWorkItem);
|
|
5876
|
+
}
|
|
5877
|
+
async countActiveWorkflowWorkItems(...args) {
|
|
5878
|
+
const a = args[0] ?? {};
|
|
5879
|
+
const active = "('admitted','running')";
|
|
5880
|
+
const scoped = async (col, val) => {
|
|
5881
|
+
const row = await this.client.get(`SELECT COUNT(*)::int AS count FROM workflow_work_items WHERE status IN ${active} AND ${col} = $1`, [val]);
|
|
5882
|
+
return row?.count ?? 0;
|
|
5883
|
+
};
|
|
5884
|
+
const routeScope = a.routeScope?.trim() || undefined;
|
|
5885
|
+
const global = routeScope ? await scoped("route_scope", routeScope) : (await this.client.get(`SELECT COUNT(*)::int AS count FROM workflow_work_items WHERE status IN ${active}`, []))?.count ?? 0;
|
|
5886
|
+
const project = a.projectKey ? await scoped("project_key", a.projectKey) : 0;
|
|
5887
|
+
const projectGroup = a.projectGroup ? await scoped("project_group", a.projectGroup) : undefined;
|
|
5888
|
+
return {
|
|
5889
|
+
global,
|
|
5890
|
+
project,
|
|
5891
|
+
...projectGroup !== undefined ? { projectGroup } : {}
|
|
5892
|
+
};
|
|
5893
|
+
}
|
|
5894
|
+
async getWorkflowRun(...args) {
|
|
5895
|
+
const row = await this.client.get("SELECT * FROM workflow_runs WHERE id = $1", [args[0]]);
|
|
5896
|
+
return row ? rowToWorkflowRun(row) : undefined;
|
|
5897
|
+
}
|
|
5898
|
+
async listWorkflowRuns(...args) {
|
|
5899
|
+
const opts = args[0] ?? {};
|
|
5900
|
+
const limit = opts.limit ?? 100;
|
|
5901
|
+
let rows;
|
|
5902
|
+
if (opts.workflowId && opts.loopRunId) {
|
|
5903
|
+
rows = await this.client.many("SELECT * FROM workflow_runs WHERE workflow_id = $1 AND loop_run_id = $2 ORDER BY created_at DESC LIMIT $3", [opts.workflowId, opts.loopRunId, limit]);
|
|
5904
|
+
} else if (opts.workflowId) {
|
|
5905
|
+
rows = await this.client.many("SELECT * FROM workflow_runs WHERE workflow_id = $1 ORDER BY created_at DESC LIMIT $2", [opts.workflowId, limit]);
|
|
5906
|
+
} else if (opts.loopRunId) {
|
|
5907
|
+
rows = await this.client.many("SELECT * FROM workflow_runs WHERE loop_run_id = $1 ORDER BY created_at DESC LIMIT $2", [opts.loopRunId, limit]);
|
|
5908
|
+
} else {
|
|
5909
|
+
rows = await this.client.many("SELECT * FROM workflow_runs ORDER BY created_at DESC LIMIT $1", [limit]);
|
|
5910
|
+
}
|
|
5911
|
+
return rows.map(rowToWorkflowRun);
|
|
5912
|
+
}
|
|
5913
|
+
async listWorkflowStepRuns(...args) {
|
|
5914
|
+
const rows = await this.client.many("SELECT * FROM workflow_step_runs WHERE workflow_run_id = $1 ORDER BY sequence ASC", [args[0]]);
|
|
5915
|
+
return rows.map(rowToWorkflowStepRun);
|
|
5916
|
+
}
|
|
5917
|
+
async getWorkflowStepRun(...args) {
|
|
5918
|
+
const row = await this.client.get("SELECT * FROM workflow_step_runs WHERE workflow_run_id = $1 AND step_id = $2", [args[0], args[1]]);
|
|
5919
|
+
return row ? rowToWorkflowStepRun(row) : undefined;
|
|
5920
|
+
}
|
|
5921
|
+
async listWorkflowEvents(...args) {
|
|
5922
|
+
const [workflowRunId, limit = 200] = args;
|
|
5923
|
+
const rows = await this.client.many("SELECT * FROM workflow_events WHERE workflow_run_id = $1 ORDER BY sequence ASC LIMIT $2", [workflowRunId, limit]);
|
|
5924
|
+
return rows.map(rowToWorkflowEvent);
|
|
5925
|
+
}
|
|
5926
|
+
async getGoal(...args) {
|
|
5927
|
+
const row = await this.client.get("SELECT * FROM goals WHERE id = $1", [args[0]]);
|
|
5928
|
+
return row ? rowToGoal(row) : undefined;
|
|
5929
|
+
}
|
|
5930
|
+
async listGoals(...args) {
|
|
5931
|
+
const opts = args[0] ?? {};
|
|
5932
|
+
const limit = opts.limit ?? 100;
|
|
5933
|
+
const rows = opts.status ? await this.client.many("SELECT * FROM goals WHERE status = $1 ORDER BY updated_at DESC LIMIT $2", [opts.status, limit]) : await this.client.many("SELECT * FROM goals ORDER BY updated_at DESC LIMIT $1", [limit]);
|
|
5934
|
+
return rows.map(rowToGoal);
|
|
5935
|
+
}
|
|
5936
|
+
async listGoalPlanNodes(...args) {
|
|
5937
|
+
const idOrPlan = args[0];
|
|
5938
|
+
const rows = await this.client.many("SELECT * FROM goal_plan_nodes WHERE goal_id = $1 OR plan_id = $1 ORDER BY sequence ASC", [idOrPlan]);
|
|
5939
|
+
return rows.map((r) => rowToGoalPlanNode({ ...r, ready: r.ready ? 1 : 0 }));
|
|
5940
|
+
}
|
|
5941
|
+
async listGoalRuns(...args) {
|
|
5942
|
+
const opts = args[0] ?? {};
|
|
5943
|
+
const limit = opts.limit ?? 100;
|
|
5944
|
+
let rows;
|
|
5945
|
+
if (opts.runId) {
|
|
5946
|
+
rows = await this.client.many("SELECT * FROM goal_runs WHERE id = $1", [opts.runId]);
|
|
5947
|
+
} else if (opts.goalId) {
|
|
5948
|
+
rows = await this.client.many("SELECT * FROM goal_runs WHERE goal_id = $1 ORDER BY created_at ASC LIMIT $2", [opts.goalId, limit]);
|
|
5949
|
+
} else {
|
|
5950
|
+
rows = await this.client.many("SELECT * FROM goal_runs ORDER BY created_at DESC LIMIT $1", [limit]);
|
|
5951
|
+
}
|
|
5952
|
+
return rows.map(rowToGoalRun);
|
|
5953
|
+
}
|
|
5954
|
+
createWorkflow() {
|
|
5955
|
+
throw new NotImplementedError("createWorkflow");
|
|
5956
|
+
}
|
|
5957
|
+
archiveWorkflow() {
|
|
5958
|
+
throw new NotImplementedError("archiveWorkflow");
|
|
5959
|
+
}
|
|
5960
|
+
createWorkflowInvocation() {
|
|
5961
|
+
throw new NotImplementedError("createWorkflowInvocation");
|
|
5962
|
+
}
|
|
5963
|
+
upsertWorkflowWorkItem() {
|
|
5964
|
+
throw new NotImplementedError("upsertWorkflowWorkItem");
|
|
5965
|
+
}
|
|
5966
|
+
admitWorkflowWorkItem() {
|
|
5967
|
+
throw new NotImplementedError("admitWorkflowWorkItem");
|
|
5968
|
+
}
|
|
5969
|
+
createGoal() {
|
|
5970
|
+
throw new NotImplementedError("createGoal");
|
|
5971
|
+
}
|
|
5972
|
+
createGoalPlanNodes() {
|
|
5973
|
+
throw new NotImplementedError("createGoalPlanNodes");
|
|
5974
|
+
}
|
|
5975
|
+
updateGoalStatus() {
|
|
5976
|
+
throw new NotImplementedError("updateGoalStatus");
|
|
5977
|
+
}
|
|
5978
|
+
updateGoalPlanNode() {
|
|
5979
|
+
throw new NotImplementedError("updateGoalPlanNode");
|
|
5980
|
+
}
|
|
5981
|
+
recordGoalEvent() {
|
|
5982
|
+
throw new NotImplementedError("recordGoalEvent");
|
|
5983
|
+
}
|
|
5984
|
+
createWorkflowRun() {
|
|
5985
|
+
throw new NotImplementedError("createWorkflowRun");
|
|
5986
|
+
}
|
|
5987
|
+
startWorkflowStepRun() {
|
|
5988
|
+
throw new NotImplementedError("startWorkflowStepRun");
|
|
5989
|
+
}
|
|
5990
|
+
recoverWorkflowRun() {
|
|
5991
|
+
throw new NotImplementedError("recoverWorkflowRun");
|
|
5992
|
+
}
|
|
5993
|
+
finalizeWorkflowStepRun() {
|
|
5994
|
+
throw new NotImplementedError("finalizeWorkflowStepRun");
|
|
5995
|
+
}
|
|
5996
|
+
finalizeWorkflowRun() {
|
|
5997
|
+
throw new NotImplementedError("finalizeWorkflowRun");
|
|
5998
|
+
}
|
|
5999
|
+
appendWorkflowEvent() {
|
|
6000
|
+
throw new NotImplementedError("appendWorkflowEvent");
|
|
6001
|
+
}
|
|
6002
|
+
}
|
|
6003
|
+
function createPostgresLoopStorage(client) {
|
|
6004
|
+
return new PostgresLoopStorage(client);
|
|
6005
|
+
}
|
|
4891
6006
|
export {
|
|
4892
6007
|
createSqliteLoopStorage,
|
|
4893
6008
|
createPostgresStorage,
|
|
6009
|
+
createPostgresLoopStorage,
|
|
4894
6010
|
checksumStorageSql,
|
|
4895
6011
|
Store,
|
|
4896
6012
|
SqliteLoopStorage,
|
|
4897
6013
|
PostgresStorage,
|
|
6014
|
+
PostgresLoopStorage,
|
|
4898
6015
|
POSTGRES_STORAGE_MIGRATIONS,
|
|
4899
|
-
POSTGRES_MIGRATION_LEDGER_TABLE
|
|
6016
|
+
POSTGRES_MIGRATION_LEDGER_TABLE,
|
|
6017
|
+
NotImplementedError
|
|
4900
6018
|
};
|