@nagi-js/core 0.1.1-rc.1 → 0.1.1-rc.5
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/dist/index.d.ts +156 -13
- package/dist/index.js +207 -30
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -104,6 +104,7 @@ function makeBuilder() {
|
|
|
104
104
|
kind: "signal",
|
|
105
105
|
needs: config.needs ?? {},
|
|
106
106
|
schema: config.schema,
|
|
107
|
+
...config.names !== void 0 ? { names: config.names } : {},
|
|
107
108
|
...config.timeoutMs !== void 0 ? { timeoutMs: config.timeoutMs } : {},
|
|
108
109
|
...config.when !== void 0 ? {
|
|
109
110
|
when: config.when
|
|
@@ -227,6 +228,7 @@ function flow(config) {
|
|
|
227
228
|
idByIdentity,
|
|
228
229
|
out: finalSteps
|
|
229
230
|
});
|
|
231
|
+
assertSignalNameUniqueness(config.id, finalSteps);
|
|
230
232
|
return {
|
|
231
233
|
id: config.id,
|
|
232
234
|
input: config.input,
|
|
@@ -236,7 +238,8 @@ function flow(config) {
|
|
|
236
238
|
...config.onComplete !== void 0 ? {
|
|
237
239
|
onComplete: config.onComplete
|
|
238
240
|
} : {},
|
|
239
|
-
...config.onError !== void 0 ? { onError: config.onError } : {}
|
|
241
|
+
...config.onError !== void 0 ? { onError: config.onError } : {},
|
|
242
|
+
...config.concurrency !== void 0 ? { concurrency: config.concurrency } : {}
|
|
240
243
|
};
|
|
241
244
|
}
|
|
242
245
|
function collectIds(map, prefix, idByIdentity) {
|
|
@@ -327,6 +330,28 @@ function walkAndRewrite(args) {
|
|
|
327
330
|
out[id] = attachDef({ kind: finalizedDef.kind, id }, finalizedDef);
|
|
328
331
|
}
|
|
329
332
|
}
|
|
333
|
+
function assertSignalNameUniqueness(flowId, finalSteps) {
|
|
334
|
+
const owners = /* @__PURE__ */ new Map();
|
|
335
|
+
for (const stepId of Object.keys(finalSteps)) {
|
|
336
|
+
owners.set(stepId, `step id "${stepId}"`);
|
|
337
|
+
}
|
|
338
|
+
for (const [stepId, step] of Object.entries(finalSteps)) {
|
|
339
|
+
const def = step.__def;
|
|
340
|
+
if (def === void 0 || def.kind !== "signal") continue;
|
|
341
|
+
if (def.names === void 0) continue;
|
|
342
|
+
for (const alias of def.names) {
|
|
343
|
+
if (alias === stepId) continue;
|
|
344
|
+
const prior = owners.get(alias);
|
|
345
|
+
const here = `alias of step "${stepId}"`;
|
|
346
|
+
if (prior !== void 0 && prior !== here) {
|
|
347
|
+
throw new Error(
|
|
348
|
+
`Flow "${flowId}": signal name "${alias}" is declared as both ${prior} and ${here}. Pick one \u2014 signal names share a namespace with step ids.`
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
owners.set(alias, here);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
330
355
|
|
|
331
356
|
// src/canonicalize.ts
|
|
332
357
|
var DEFAULT_RETRY = {
|
|
@@ -374,6 +399,9 @@ async function canonicalizeSignal(base, def) {
|
|
|
374
399
|
if (def.when !== void 0) out.whenHash = await hashFnSource(def.when);
|
|
375
400
|
if (def.timeoutMs !== void 0) out.timeoutMs = def.timeoutMs;
|
|
376
401
|
out.signalSchema = await canonicalizeSchema(def.schema);
|
|
402
|
+
if (def.names !== void 0) {
|
|
403
|
+
out.signalNames = [...def.names].sort();
|
|
404
|
+
}
|
|
377
405
|
return out;
|
|
378
406
|
}
|
|
379
407
|
async function canonicalizeMatch(base, def) {
|
|
@@ -421,6 +449,15 @@ async function hashFnSource(fn) {
|
|
|
421
449
|
async function sha256Canonical(dag) {
|
|
422
450
|
return sha256Hex(stableStringify(dag));
|
|
423
451
|
}
|
|
452
|
+
async function fingerprintFlows(flows) {
|
|
453
|
+
const entries = [];
|
|
454
|
+
for (const f of flows) {
|
|
455
|
+
const dag = await canonicalize(f);
|
|
456
|
+
entries.push([f.id, await sha256Canonical(dag)]);
|
|
457
|
+
}
|
|
458
|
+
entries.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
459
|
+
return sha256Hex(stableStringify(entries));
|
|
460
|
+
}
|
|
424
461
|
function stableStringify(value) {
|
|
425
462
|
if (value === void 0) return "null";
|
|
426
463
|
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
@@ -567,6 +604,14 @@ var InMemoryStore = class {
|
|
|
567
604
|
snapshots = /* @__PURE__ */ new Map();
|
|
568
605
|
refs = /* @__PURE__ */ new Map();
|
|
569
606
|
globalFacts = [];
|
|
607
|
+
/**
|
|
608
|
+
* The runId currently holding the active slot for `flowId::concurrencyKey`.
|
|
609
|
+
* Updated on `tryStartRun` (set when concurrency is supplied) and on
|
|
610
|
+
* terminal facts (cleared via {@link keyByActiveRun}).
|
|
611
|
+
*/
|
|
612
|
+
activeByKey = /* @__PURE__ */ new Map();
|
|
613
|
+
/** Reverse lookup so terminal facts can free the slot in O(1). */
|
|
614
|
+
keyByActiveRun = /* @__PURE__ */ new Map();
|
|
570
615
|
leaseMs;
|
|
571
616
|
constructor(opts = {}) {
|
|
572
617
|
this.leaseMs = opts.leaseMs ?? DEFAULT_STORE_LEASE_MS;
|
|
@@ -575,13 +620,40 @@ var InMemoryStore = class {
|
|
|
575
620
|
const list = this.facts.get(runId) ?? [];
|
|
576
621
|
list.push(fact);
|
|
577
622
|
this.facts.set(runId, list);
|
|
623
|
+
if (fact.kind === "flow.completed" || fact.kind === "flow.failed" || fact.kind === "flow.canceled") {
|
|
624
|
+
const slot = this.keyByActiveRun.get(runId);
|
|
625
|
+
if (slot !== void 0) {
|
|
626
|
+
this.keyByActiveRun.delete(runId);
|
|
627
|
+
if (this.activeByKey.get(slot) === runId) {
|
|
628
|
+
this.activeByKey.delete(slot);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
578
632
|
}
|
|
579
|
-
async tryStartRun(runId, fact) {
|
|
633
|
+
async tryStartRun(runId, fact, concurrency) {
|
|
580
634
|
if (this.facts.has(runId)) {
|
|
581
|
-
return { started: false };
|
|
635
|
+
return { started: false, canceled: [] };
|
|
636
|
+
}
|
|
637
|
+
const canceled = [];
|
|
638
|
+
if (concurrency !== void 0) {
|
|
639
|
+
const slot = `${fact.flowId}::${concurrency.key}`;
|
|
640
|
+
const priorRunId = this.activeByKey.get(slot);
|
|
641
|
+
if (priorRunId !== void 0) {
|
|
642
|
+
const cancelFact = {
|
|
643
|
+
kind: "flow.canceled",
|
|
644
|
+
runId: priorRunId,
|
|
645
|
+
at: fact.at,
|
|
646
|
+
canceledByRunId: runId,
|
|
647
|
+
concurrencyKey: concurrency.key
|
|
648
|
+
};
|
|
649
|
+
await this.appendFact(priorRunId, cancelFact);
|
|
650
|
+
canceled.push({ runId: priorRunId, fact: cancelFact });
|
|
651
|
+
}
|
|
652
|
+
this.activeByKey.set(slot, runId);
|
|
653
|
+
this.keyByActiveRun.set(runId, slot);
|
|
582
654
|
}
|
|
583
655
|
this.facts.set(runId, [fact]);
|
|
584
|
-
return { started: true };
|
|
656
|
+
return { started: true, canceled };
|
|
585
657
|
}
|
|
586
658
|
async loadRunState(runId) {
|
|
587
659
|
return projectRunState(runId, this.facts.get(runId) ?? []);
|
|
@@ -663,6 +735,9 @@ function projectRunState(runId, facts) {
|
|
|
663
735
|
case "flow.failed":
|
|
664
736
|
status = "failed";
|
|
665
737
|
break;
|
|
738
|
+
case "flow.canceled":
|
|
739
|
+
status = "canceled";
|
|
740
|
+
break;
|
|
666
741
|
case "step.started":
|
|
667
742
|
steps[fact.stepId] = {
|
|
668
743
|
stepId: fact.stepId,
|
|
@@ -980,6 +1055,10 @@ async function dispatchMessage(deps, message) {
|
|
|
980
1055
|
}
|
|
981
1056
|
const def = getDef(step);
|
|
982
1057
|
const preState = await store.loadRunState(runId);
|
|
1058
|
+
if (preState.status === "canceled") {
|
|
1059
|
+
await queue.ack(message.receipt);
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
983
1062
|
const preStep = preState.steps[stepId];
|
|
984
1063
|
if (preStep && (preStep.status === "completed" || preStep.status === "failed" || preStep.status === "skipped")) {
|
|
985
1064
|
await queue.ack(message.receipt);
|
|
@@ -1342,8 +1421,13 @@ function retryAllows(policy, err) {
|
|
|
1342
1421
|
return policy.retryOn(err);
|
|
1343
1422
|
}
|
|
1344
1423
|
function isFlowTerminal(facts) {
|
|
1345
|
-
|
|
1346
|
-
|
|
1424
|
+
for (let i = facts.length - 1; i >= 0; i--) {
|
|
1425
|
+
const f = facts[i];
|
|
1426
|
+
if (f !== void 0 && (f.kind === "flow.completed" || f.kind === "flow.failed" || f.kind === "flow.canceled")) {
|
|
1427
|
+
return true;
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
return false;
|
|
1347
1431
|
}
|
|
1348
1432
|
async function finalizeFlowFailure(args) {
|
|
1349
1433
|
const { deps, flow: flow2, runId, error } = args;
|
|
@@ -1504,6 +1588,24 @@ var NagiRuntimeError = class extends Error {
|
|
|
1504
1588
|
this.name = "NagiRuntimeError";
|
|
1505
1589
|
}
|
|
1506
1590
|
};
|
|
1591
|
+
var NagiCanceledError = class extends Error {
|
|
1592
|
+
runId;
|
|
1593
|
+
canceledByRunId;
|
|
1594
|
+
concurrencyKey;
|
|
1595
|
+
constructor(args) {
|
|
1596
|
+
super(
|
|
1597
|
+
`Run ${args.runId} was canceled (superseded by run ${args.canceledByRunId} for concurrency key "${args.concurrencyKey}").`
|
|
1598
|
+
);
|
|
1599
|
+
this.name = "NagiCanceledError";
|
|
1600
|
+
this.runId = args.runId;
|
|
1601
|
+
this.canceledByRunId = args.canceledByRunId;
|
|
1602
|
+
this.concurrencyKey = args.concurrencyKey;
|
|
1603
|
+
this.cause = {
|
|
1604
|
+
canceledByRunId: args.canceledByRunId,
|
|
1605
|
+
concurrencyKey: args.concurrencyKey
|
|
1606
|
+
};
|
|
1607
|
+
}
|
|
1608
|
+
};
|
|
1507
1609
|
var NagiSnapshotDriftError = class extends Error {
|
|
1508
1610
|
runId;
|
|
1509
1611
|
expected;
|
|
@@ -1549,6 +1651,7 @@ async function nagi(config) {
|
|
|
1549
1651
|
});
|
|
1550
1652
|
}
|
|
1551
1653
|
}
|
|
1654
|
+
const codeVersion = config.codeVersion ?? await fingerprintFlows(config.flows);
|
|
1552
1655
|
async function flowFor(runId) {
|
|
1553
1656
|
const runState = await config.store.loadRunState(runId);
|
|
1554
1657
|
const flow2 = flowsById.get(runState.flowId);
|
|
@@ -1599,12 +1702,63 @@ async function nagi(config) {
|
|
|
1599
1702
|
input: validated,
|
|
1600
1703
|
at: startedAt,
|
|
1601
1704
|
...flowHash !== void 0 ? { flowHash } : {},
|
|
1602
|
-
|
|
1705
|
+
codeVersion
|
|
1603
1706
|
};
|
|
1604
|
-
|
|
1707
|
+
let concurrencyArg;
|
|
1708
|
+
if (flow2.concurrency !== void 0) {
|
|
1709
|
+
const derived = flow2.concurrency.keyFn(validated);
|
|
1710
|
+
if (typeof derived !== "string" || derived.length === 0) {
|
|
1711
|
+
throw new NagiValidationError([
|
|
1712
|
+
{
|
|
1713
|
+
message: `flow.concurrency.keyFn must return a non-empty string (got ${typeof derived === "string" ? '""' : typeof derived})`,
|
|
1714
|
+
path: ["concurrency", "keyFn"]
|
|
1715
|
+
}
|
|
1716
|
+
]);
|
|
1717
|
+
}
|
|
1718
|
+
concurrencyArg = { key: derived, mode: flow2.concurrency.mode };
|
|
1719
|
+
}
|
|
1720
|
+
const { started, canceled } = await config.store.tryStartRun(
|
|
1721
|
+
runId,
|
|
1722
|
+
fact,
|
|
1723
|
+
concurrencyArg
|
|
1724
|
+
);
|
|
1605
1725
|
if (!started) {
|
|
1606
1726
|
return runId;
|
|
1607
1727
|
}
|
|
1728
|
+
for (const c of canceled) {
|
|
1729
|
+
const cancelError = new NagiCanceledError({
|
|
1730
|
+
runId: c.runId,
|
|
1731
|
+
canceledByRunId: runId,
|
|
1732
|
+
concurrencyKey: c.fact.concurrencyKey
|
|
1733
|
+
});
|
|
1734
|
+
const serialized = {
|
|
1735
|
+
name: cancelError.name,
|
|
1736
|
+
message: cancelError.message,
|
|
1737
|
+
...cancelError.stack !== void 0 ? { stack: cancelError.stack } : {},
|
|
1738
|
+
cause: {
|
|
1739
|
+
canceledByRunId: runId,
|
|
1740
|
+
concurrencyKey: c.fact.concurrencyKey
|
|
1741
|
+
}
|
|
1742
|
+
};
|
|
1743
|
+
const errorEvent = {
|
|
1744
|
+
runId: c.runId,
|
|
1745
|
+
flowId: flow2.id,
|
|
1746
|
+
error: serialized,
|
|
1747
|
+
at: c.fact.at
|
|
1748
|
+
};
|
|
1749
|
+
await fireHook(
|
|
1750
|
+
flow2.onError,
|
|
1751
|
+
errorEvent,
|
|
1752
|
+
"flow.onError",
|
|
1753
|
+
dispatchDeps
|
|
1754
|
+
);
|
|
1755
|
+
await fireHook(
|
|
1756
|
+
config.hooks?.onFlowError,
|
|
1757
|
+
errorEvent,
|
|
1758
|
+
"onFlowError",
|
|
1759
|
+
dispatchDeps
|
|
1760
|
+
);
|
|
1761
|
+
}
|
|
1608
1762
|
const startEvent = {
|
|
1609
1763
|
runId,
|
|
1610
1764
|
flowId: flow2.id,
|
|
@@ -1626,7 +1780,7 @@ async function nagi(config) {
|
|
|
1626
1780
|
await advance(dispatchDeps, runId);
|
|
1627
1781
|
return runId;
|
|
1628
1782
|
},
|
|
1629
|
-
async signal(runId,
|
|
1783
|
+
async signal(runId, signalName, payload) {
|
|
1630
1784
|
const runState = await config.store.loadRunState(runId);
|
|
1631
1785
|
const flow2 = flowsById.get(runState.flowId);
|
|
1632
1786
|
if (!flow2) {
|
|
@@ -1634,53 +1788,71 @@ async function nagi(config) {
|
|
|
1634
1788
|
`Run ${runId} references flow "${runState.flowId}" which is not registered with nagi().`
|
|
1635
1789
|
);
|
|
1636
1790
|
}
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
);
|
|
1791
|
+
let stepId;
|
|
1792
|
+
let def;
|
|
1793
|
+
const direct = flow2.steps[signalName];
|
|
1794
|
+
if (direct !== void 0) {
|
|
1795
|
+
const d = getDef(direct);
|
|
1796
|
+
if (d.kind === "signal" && d.names === void 0) {
|
|
1797
|
+
stepId = signalName;
|
|
1798
|
+
def = d;
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
if (stepId === void 0) {
|
|
1802
|
+
for (const [id, s] of Object.entries(flow2.steps)) {
|
|
1803
|
+
const d = getDef(s);
|
|
1804
|
+
if (d.kind === "signal" && d.names?.includes(signalName)) {
|
|
1805
|
+
stepId = id;
|
|
1806
|
+
def = d;
|
|
1807
|
+
break;
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1642
1810
|
}
|
|
1643
|
-
|
|
1644
|
-
if (def.kind !== "signal") {
|
|
1811
|
+
if (stepId === void 0 || def === void 0) {
|
|
1645
1812
|
throw new NagiRuntimeError(
|
|
1646
|
-
`
|
|
1813
|
+
`Flow "${flow2.id}" has no signal step accepting "${signalName}".`
|
|
1647
1814
|
);
|
|
1648
1815
|
}
|
|
1649
|
-
const stepState = runState.steps[
|
|
1816
|
+
const stepState = runState.steps[stepId];
|
|
1650
1817
|
if (stepState?.status !== "running") {
|
|
1818
|
+
if (stepState?.status === "completed") {
|
|
1819
|
+
config.logger?.info("nagi: signal arrived after step resolved", {
|
|
1820
|
+
runId,
|
|
1821
|
+
stepId,
|
|
1822
|
+
signalName
|
|
1823
|
+
});
|
|
1824
|
+
return;
|
|
1825
|
+
}
|
|
1651
1826
|
throw new NagiRuntimeError(
|
|
1652
|
-
`Step "${
|
|
1827
|
+
`Step "${stepId}" is not waiting for signal (status: ${stepState?.status ?? "pending"}).`
|
|
1653
1828
|
);
|
|
1654
1829
|
}
|
|
1655
1830
|
const validated = await validate(def.schema, payload);
|
|
1656
1831
|
const attempt = stepState.attempts > 0 ? stepState.attempts : 1;
|
|
1832
|
+
const carriesAlias = signalName !== stepId;
|
|
1657
1833
|
await config.store.appendFact(runId, {
|
|
1658
1834
|
kind: "signal.received",
|
|
1659
1835
|
runId,
|
|
1660
|
-
stepId
|
|
1836
|
+
stepId,
|
|
1661
1837
|
payload: validated,
|
|
1662
|
-
at: clock.now()
|
|
1838
|
+
at: clock.now(),
|
|
1839
|
+
...carriesAlias ? { signalName } : {}
|
|
1663
1840
|
});
|
|
1664
1841
|
const completedFact = {
|
|
1665
1842
|
kind: "step.completed",
|
|
1666
1843
|
runId,
|
|
1667
|
-
stepId
|
|
1844
|
+
stepId,
|
|
1668
1845
|
attempt,
|
|
1669
1846
|
output: validated,
|
|
1670
1847
|
at: clock.now()
|
|
1671
1848
|
};
|
|
1672
|
-
await config.store.completeStep(
|
|
1673
|
-
runId,
|
|
1674
|
-
stepName,
|
|
1675
|
-
validated,
|
|
1676
|
-
completedFact
|
|
1677
|
-
);
|
|
1849
|
+
await config.store.completeStep(runId, stepId, validated, completedFact);
|
|
1678
1850
|
await fireHook(
|
|
1679
1851
|
config.hooks?.onSignalReceived,
|
|
1680
1852
|
{
|
|
1681
1853
|
runId,
|
|
1682
1854
|
flowId: flow2.id,
|
|
1683
|
-
stepId
|
|
1855
|
+
stepId,
|
|
1684
1856
|
attempt,
|
|
1685
1857
|
kind: "signal",
|
|
1686
1858
|
payload: validated,
|
|
@@ -1707,6 +1879,11 @@ async function nagi(config) {
|
|
|
1707
1879
|
`Run ${runId} references flow "${runState.flowId}" which is not registered with nagi().`
|
|
1708
1880
|
);
|
|
1709
1881
|
}
|
|
1882
|
+
if (runState.status === "canceled") {
|
|
1883
|
+
throw new NagiRuntimeError(
|
|
1884
|
+
`Run ${runId} was canceled (superseded by a newer run with the same concurrency key). Replay is not supported for canceled runs \u2014 start a new run instead.`
|
|
1885
|
+
);
|
|
1886
|
+
}
|
|
1710
1887
|
if (opts.mode === "inspect") return;
|
|
1711
1888
|
const fireHooks = opts.fireHooks !== false;
|
|
1712
1889
|
const baseDeps = fireHooks ? dispatchDeps : { ...dispatchDeps, fireHooks: false };
|
|
@@ -1800,6 +1977,6 @@ function synthesizeReplayFlow(dag, liveFlow) {
|
|
|
1800
1977
|
};
|
|
1801
1978
|
}
|
|
1802
1979
|
|
|
1803
|
-
export { InMemoryClock, InMemoryQueue, InMemoryStore, InMemoryTrigger, NagiRuntimeError, NagiSnapshotDriftError, NagiValidationError, canonicalize, diffSnapshots, flow, nagi, projectRunState, sha256Canonical };
|
|
1980
|
+
export { InMemoryClock, InMemoryQueue, InMemoryStore, InMemoryTrigger, NagiCanceledError, NagiRuntimeError, NagiSnapshotDriftError, NagiValidationError, canonicalize, diffSnapshots, fingerprintFlows, flow, nagi, projectRunState, sha256Canonical };
|
|
1804
1981
|
//# sourceMappingURL=index.js.map
|
|
1805
1982
|
//# sourceMappingURL=index.js.map
|