@tangle-network/agent-runtime 0.15.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -84
- package/dist/agent.d.ts +1 -1
- package/dist/index.d.ts +167 -862
- package/dist/index.js +157 -1395
- package/dist/index.js.map +1 -1
- package/dist/{types-CYxfw14J.d.ts → types-DmhXdAhu.d.ts} +1 -1
- package/package.json +2 -4
package/dist/index.js
CHANGED
|
@@ -514,141 +514,9 @@ function sandboxAsChatTurnTarget(instance, opts) {
|
|
|
514
514
|
};
|
|
515
515
|
}
|
|
516
516
|
|
|
517
|
-
// src/durable/
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
const json = canonicalJson(value);
|
|
521
|
-
return createHash("sha256").update(json).digest("hex");
|
|
522
|
-
}
|
|
523
|
-
function canonicalJson(value) {
|
|
524
|
-
return JSON.stringify(canonicalize(value));
|
|
525
|
-
}
|
|
526
|
-
function canonicalize(value) {
|
|
527
|
-
if (value === null) return null;
|
|
528
|
-
if (Array.isArray(value)) return value.map(canonicalize);
|
|
529
|
-
const t = typeof value;
|
|
530
|
-
if (t === "string" || t === "boolean") return value;
|
|
531
|
-
if (t === "number") {
|
|
532
|
-
if (!Number.isFinite(value)) {
|
|
533
|
-
throw new TypeError(`canonicalJson: non-finite number ${String(value)} not serializable`);
|
|
534
|
-
}
|
|
535
|
-
return value;
|
|
536
|
-
}
|
|
537
|
-
if (t === "undefined" || t === "function" || t === "symbol") {
|
|
538
|
-
throw new TypeError(`canonicalJson: ${t} is not JSON-serializable`);
|
|
539
|
-
}
|
|
540
|
-
if (t === "bigint") {
|
|
541
|
-
return { __bigint: String(value) };
|
|
542
|
-
}
|
|
543
|
-
if (t === "object") {
|
|
544
|
-
const obj = value;
|
|
545
|
-
const proto = Object.getPrototypeOf(obj);
|
|
546
|
-
if (proto !== null && proto !== Object.prototype) {
|
|
547
|
-
const ctor = obj.constructor?.name ?? "unknown";
|
|
548
|
-
throw new TypeError(
|
|
549
|
-
`canonicalJson: class instance (${ctor}) is not JSON-serializable. Project to plain { ... } at the boundary.`
|
|
550
|
-
);
|
|
551
|
-
}
|
|
552
|
-
const keys = Object.keys(obj).sort();
|
|
553
|
-
const out = {};
|
|
554
|
-
for (const k of keys) out[k] = canonicalize(obj[k]);
|
|
555
|
-
return out;
|
|
556
|
-
}
|
|
557
|
-
throw new TypeError(`canonicalJson: unsupported type ${t}`);
|
|
558
|
-
}
|
|
559
|
-
function manifestHash(manifest) {
|
|
560
|
-
return canonicalHash({
|
|
561
|
-
projectId: manifest.projectId,
|
|
562
|
-
scenarioId: manifest.scenarioId ?? null,
|
|
563
|
-
taskId: manifest.task.id,
|
|
564
|
-
taskIntent: manifest.task.intent,
|
|
565
|
-
taskDomain: manifest.task.domain,
|
|
566
|
-
input: manifest.input
|
|
567
|
-
});
|
|
568
|
-
}
|
|
569
|
-
function stepId(runId, stepIndex, intent) {
|
|
570
|
-
return canonicalHash({ runId, stepIndex, intent });
|
|
571
|
-
}
|
|
572
|
-
var counter = 0;
|
|
573
|
-
function deriveWorkerId() {
|
|
574
|
-
const host = process.env.HOSTNAME ?? "host";
|
|
575
|
-
const pid = process.pid ?? 0;
|
|
576
|
-
const rand = Math.random().toString(36).slice(2, 10);
|
|
577
|
-
counter += 1;
|
|
578
|
-
return `${host}:${pid}:${rand}:${counter}`;
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
// src/durable/turn.ts
|
|
582
|
-
var STEP_INDEX = 0;
|
|
583
|
-
function runDurableTurn(options) {
|
|
584
|
-
const { store, runId, manifest, workerId } = options;
|
|
585
|
-
const leaseMs = options.leaseMs ?? 6e4;
|
|
586
|
-
const intent = options.intent ?? "turn";
|
|
587
|
-
const inputHash = canonicalHash(manifest.input);
|
|
588
|
-
let accumulated = "";
|
|
589
|
-
let didReplay = false;
|
|
590
|
-
let finalRecord;
|
|
591
|
-
async function* stream() {
|
|
592
|
-
const { completedSteps } = await store.startOrResume({
|
|
593
|
-
runId,
|
|
594
|
-
manifest,
|
|
595
|
-
workerId,
|
|
596
|
-
leaseMs
|
|
597
|
-
});
|
|
598
|
-
const prior = completedSteps.find((s) => s.stepIndex === STEP_INDEX);
|
|
599
|
-
if (prior && prior.status === "completed") {
|
|
600
|
-
didReplay = true;
|
|
601
|
-
const cached = prior.result;
|
|
602
|
-
accumulated = cached?.finalText ?? "";
|
|
603
|
-
yield options.replayEvent(accumulated);
|
|
604
|
-
finalRecord = await store.endRun({ runId, workerId, status: "completed" });
|
|
605
|
-
return;
|
|
606
|
-
}
|
|
607
|
-
await store.beginStep({
|
|
608
|
-
runId,
|
|
609
|
-
stepIndex: STEP_INDEX,
|
|
610
|
-
intent,
|
|
611
|
-
kind: "llm",
|
|
612
|
-
inputHash
|
|
613
|
-
});
|
|
614
|
-
try {
|
|
615
|
-
const producer = options.produce();
|
|
616
|
-
for await (const event of producer.stream) {
|
|
617
|
-
if (options.accumulate) {
|
|
618
|
-
const next = options.accumulate(event, accumulated);
|
|
619
|
-
if (typeof next === "string") accumulated = next;
|
|
620
|
-
}
|
|
621
|
-
yield event;
|
|
622
|
-
}
|
|
623
|
-
const producerText = producer.finalText();
|
|
624
|
-
if (producerText) accumulated = producerText;
|
|
625
|
-
await store.completeStep({
|
|
626
|
-
runId,
|
|
627
|
-
stepIndex: STEP_INDEX,
|
|
628
|
-
result: { finalText: accumulated }
|
|
629
|
-
});
|
|
630
|
-
finalRecord = await store.endRun({
|
|
631
|
-
runId,
|
|
632
|
-
workerId,
|
|
633
|
-
status: "completed",
|
|
634
|
-
outcome: { notes: intent, metadata: { chars: accumulated.length } }
|
|
635
|
-
});
|
|
636
|
-
} catch (err) {
|
|
637
|
-
await store.failStep({
|
|
638
|
-
runId,
|
|
639
|
-
stepIndex: STEP_INDEX,
|
|
640
|
-
error: { message: err instanceof Error ? err.message : String(err) }
|
|
641
|
-
});
|
|
642
|
-
finalRecord = await store.endRun({ runId, workerId, status: "failed" });
|
|
643
|
-
throw err;
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
return {
|
|
647
|
-
stream: stream(),
|
|
648
|
-
finalText: () => accumulated,
|
|
649
|
-
replayed: () => didReplay,
|
|
650
|
-
record: () => finalRecord
|
|
651
|
-
};
|
|
517
|
+
// src/durable/execution-handle.ts
|
|
518
|
+
function deriveExecutionId(input) {
|
|
519
|
+
return `${input.projectId}:${input.sessionId}:${input.turnIndex}`;
|
|
652
520
|
}
|
|
653
521
|
|
|
654
522
|
// src/durable/chat-engine.ts
|
|
@@ -657,1259 +525,85 @@ function encodeLine(event) {
|
|
|
657
525
|
return encoder.encode(`${JSON.stringify(event)}
|
|
658
526
|
`);
|
|
659
527
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
metadata: {
|
|
680
|
-
tenantId: identity.tenantId,
|
|
681
|
-
sessionId: identity.sessionId,
|
|
682
|
-
turnIndex: identity.turnIndex
|
|
528
|
+
function defaultLog(message, meta) {
|
|
529
|
+
if (meta) console.error(message, meta);
|
|
530
|
+
else console.error(message);
|
|
531
|
+
}
|
|
532
|
+
function handleChatTurn(input) {
|
|
533
|
+
const log = input.log ?? defaultLog;
|
|
534
|
+
const { identity, hooks } = input;
|
|
535
|
+
const body = new ReadableStream({
|
|
536
|
+
start: async (controller) => {
|
|
537
|
+
const emit2 = async (event) => {
|
|
538
|
+
controller.enqueue(encodeLine(event));
|
|
539
|
+
if (hooks.onEvent) {
|
|
540
|
+
try {
|
|
541
|
+
await hooks.onEvent(event);
|
|
542
|
+
} catch (err) {
|
|
543
|
+
log("[chat-engine] onEvent hook threw", {
|
|
544
|
+
error: err instanceof Error ? err.message : String(err)
|
|
545
|
+
});
|
|
546
|
+
}
|
|
683
547
|
}
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
}
|
|
693
|
-
};
|
|
694
|
-
const body = new ReadableStream({
|
|
695
|
-
start: async (controller) => {
|
|
696
|
-
const emit2 = async (event) => {
|
|
697
|
-
controller.enqueue(encodeLine(event));
|
|
698
|
-
if (input.hooks.onEvent) {
|
|
699
|
-
try {
|
|
700
|
-
await input.hooks.onEvent(event);
|
|
701
|
-
} catch (err) {
|
|
702
|
-
log("[chat-engine] onEvent hook threw", {
|
|
703
|
-
error: err instanceof Error ? err.message : String(err)
|
|
704
|
-
});
|
|
705
|
-
}
|
|
548
|
+
};
|
|
549
|
+
try {
|
|
550
|
+
await emit2({
|
|
551
|
+
type: "session.run.started",
|
|
552
|
+
data: {
|
|
553
|
+
sessionId: identity.sessionId,
|
|
554
|
+
tenantId: identity.tenantId,
|
|
555
|
+
turnIndex: identity.turnIndex
|
|
706
556
|
}
|
|
707
|
-
};
|
|
708
|
-
|
|
557
|
+
});
|
|
558
|
+
const producer = hooks.produce();
|
|
559
|
+
for await (const event of producer.stream) {
|
|
560
|
+
await emit2(event);
|
|
561
|
+
}
|
|
562
|
+
const rawFinal = producer.finalText();
|
|
563
|
+
const finalText = hooks.transformFinalText ? await hooks.transformFinalText(rawFinal) : rawFinal;
|
|
709
564
|
try {
|
|
710
|
-
await
|
|
711
|
-
type: "session.run.started",
|
|
712
|
-
data: {
|
|
713
|
-
sessionId: identity.sessionId,
|
|
714
|
-
tenantId: identity.tenantId,
|
|
715
|
-
turnIndex: identity.turnIndex
|
|
716
|
-
}
|
|
717
|
-
});
|
|
718
|
-
const turn = runDurableTurn({
|
|
719
|
-
store: input.store,
|
|
720
|
-
runId,
|
|
721
|
-
manifest,
|
|
722
|
-
workerId,
|
|
723
|
-
leaseMs: input.leaseMs,
|
|
724
|
-
intent: `chat:turn-${identity.turnIndex}`,
|
|
725
|
-
produce: input.hooks.produce,
|
|
726
|
-
replayEvent: (finalText2) => ({ type: "result", data: { finalText: finalText2 } }),
|
|
727
|
-
accumulate: (event, current) => {
|
|
728
|
-
if (event.type === "message.part.updated") {
|
|
729
|
-
const data = event.data ?? {};
|
|
730
|
-
const delta = typeof data.delta === "string" ? data.delta : "";
|
|
731
|
-
const part = data.part;
|
|
732
|
-
if (delta) return current + delta;
|
|
733
|
-
if (part?.type === "text" && typeof part.text === "string") return part.text;
|
|
734
|
-
return void 0;
|
|
735
|
-
}
|
|
736
|
-
if (event.type === "result") {
|
|
737
|
-
const data = event.data ?? {};
|
|
738
|
-
if (typeof data.finalText === "string") return data.finalText;
|
|
739
|
-
}
|
|
740
|
-
return void 0;
|
|
741
|
-
}
|
|
742
|
-
});
|
|
743
|
-
for await (const event of turn.stream) {
|
|
744
|
-
await emit2(event);
|
|
745
|
-
}
|
|
746
|
-
const rawFinal = turn.finalText();
|
|
747
|
-
const finalText = input.hooks.transformFinalText ? await input.hooks.transformFinalText(rawFinal) : rawFinal;
|
|
748
|
-
if (!turn.replayed()) {
|
|
749
|
-
try {
|
|
750
|
-
await input.hooks.persistAssistantMessage({
|
|
751
|
-
identity,
|
|
752
|
-
finalText,
|
|
753
|
-
record: turn.record()
|
|
754
|
-
});
|
|
755
|
-
} catch (err) {
|
|
756
|
-
log("[chat-engine] persistAssistantMessage threw", {
|
|
757
|
-
error: err instanceof Error ? err.message : String(err)
|
|
758
|
-
});
|
|
759
|
-
}
|
|
760
|
-
if (input.hooks.onTurnComplete) {
|
|
761
|
-
try {
|
|
762
|
-
await input.hooks.onTurnComplete({ identity, finalText });
|
|
763
|
-
} catch (err) {
|
|
764
|
-
log("[chat-engine] onTurnComplete threw", {
|
|
765
|
-
error: err instanceof Error ? err.message : String(err)
|
|
766
|
-
});
|
|
767
|
-
}
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
await emit2({
|
|
771
|
-
type: "session.run.completed",
|
|
772
|
-
data: { sessionId: identity.sessionId, replayed: turn.replayed() }
|
|
773
|
-
});
|
|
565
|
+
await hooks.persistAssistantMessage({ identity, finalText });
|
|
774
566
|
} catch (err) {
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
log("[chat-engine] turn failed", { error: message });
|
|
778
|
-
await emit2({ type: "error", data: { message } });
|
|
779
|
-
await emit2({
|
|
780
|
-
type: "session.run.failed",
|
|
781
|
-
data: { sessionId: identity.sessionId, message }
|
|
567
|
+
log("[chat-engine] persistAssistantMessage threw", {
|
|
568
|
+
error: err instanceof Error ? err.message : String(err)
|
|
782
569
|
});
|
|
783
|
-
} finally {
|
|
784
|
-
if (input.hooks.traceFlush) {
|
|
785
|
-
const flush = input.hooks.traceFlush().catch(
|
|
786
|
-
(err) => log("[chat-engine] traceFlush threw", {
|
|
787
|
-
error: err instanceof Error ? err.message : String(err)
|
|
788
|
-
})
|
|
789
|
-
);
|
|
790
|
-
if (input.waitUntil) input.waitUntil(flush);
|
|
791
|
-
else await flush;
|
|
792
|
-
}
|
|
793
|
-
controller.close();
|
|
794
|
-
void turnFailed;
|
|
795
570
|
}
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
var DurableRunError = class extends Error {
|
|
805
|
-
constructor(message, code) {
|
|
806
|
-
super(message);
|
|
807
|
-
this.code = code;
|
|
808
|
-
this.name = this.constructor.name;
|
|
809
|
-
}
|
|
810
|
-
code;
|
|
811
|
-
};
|
|
812
|
-
var DurableRunLeaseHeldError = class extends DurableRunError {
|
|
813
|
-
constructor(message) {
|
|
814
|
-
super(message, "lease_held");
|
|
815
|
-
}
|
|
816
|
-
};
|
|
817
|
-
var DurableRunInputMismatchError = class extends DurableRunError {
|
|
818
|
-
constructor(message) {
|
|
819
|
-
super(message, "manifest_mismatch");
|
|
820
|
-
}
|
|
821
|
-
};
|
|
822
|
-
var DurableRunDivergenceError = class extends DurableRunError {
|
|
823
|
-
constructor(message) {
|
|
824
|
-
super(message, "step_divergence");
|
|
825
|
-
}
|
|
826
|
-
};
|
|
827
|
-
var DurableAwaitEventTimeoutError = class extends DurableRunError {
|
|
828
|
-
constructor(message) {
|
|
829
|
-
super(message, "await_event_timeout");
|
|
830
|
-
}
|
|
831
|
-
};
|
|
832
|
-
|
|
833
|
-
// src/durable/d1-store.ts
|
|
834
|
-
var DEFAULT_LEASE_MS = 3e4;
|
|
835
|
-
var D1DurableRunStore = class {
|
|
836
|
-
constructor(db) {
|
|
837
|
-
this.db = db;
|
|
838
|
-
}
|
|
839
|
-
db;
|
|
840
|
-
/** Override for tests — defaults to Date.now(). */
|
|
841
|
-
now = () => Date.now();
|
|
842
|
-
async startOrResume(input) {
|
|
843
|
-
const leaseMs = input.leaseMs ?? DEFAULT_LEASE_MS;
|
|
844
|
-
const hash = manifestHash(input.manifest);
|
|
845
|
-
const nowMs = this.now();
|
|
846
|
-
const nowIso2 = new Date(nowMs).toISOString();
|
|
847
|
-
const leaseExpiresAt = new Date(nowMs + leaseMs).toISOString();
|
|
848
|
-
const existing = await this.db.prepare("SELECT * FROM durable_runs WHERE run_id = ?").bind(input.runId).first();
|
|
849
|
-
if (!existing) {
|
|
850
|
-
await this.db.prepare(
|
|
851
|
-
`INSERT INTO durable_runs
|
|
852
|
-
(run_id, manifest_hash, project_id, scenario_id, status,
|
|
853
|
-
created_at, updated_at, lease_holder_id, lease_expires_at, step_count)
|
|
854
|
-
VALUES (?, ?, ?, ?, 'running', ?, ?, ?, ?, 0)`
|
|
855
|
-
).bind(
|
|
856
|
-
input.runId,
|
|
857
|
-
hash,
|
|
858
|
-
input.manifest.projectId,
|
|
859
|
-
input.manifest.scenarioId ?? null,
|
|
860
|
-
nowIso2,
|
|
861
|
-
nowIso2,
|
|
862
|
-
input.workerId,
|
|
863
|
-
leaseExpiresAt
|
|
864
|
-
).run();
|
|
865
|
-
const record2 = {
|
|
866
|
-
runId: input.runId,
|
|
867
|
-
manifestHash: hash,
|
|
868
|
-
projectId: input.manifest.projectId,
|
|
869
|
-
scenarioId: input.manifest.scenarioId,
|
|
870
|
-
status: "running",
|
|
871
|
-
createdAt: nowIso2,
|
|
872
|
-
updatedAt: nowIso2,
|
|
873
|
-
leaseHolderId: input.workerId,
|
|
874
|
-
leaseExpiresAt,
|
|
875
|
-
stepCount: 0
|
|
876
|
-
};
|
|
877
|
-
return { run: record2, completedSteps: [], leaseExpiresAt };
|
|
878
|
-
}
|
|
879
|
-
if (existing.manifest_hash !== hash) {
|
|
880
|
-
throw new DurableRunInputMismatchError(
|
|
881
|
-
`runId ${input.runId} exists with a different manifest hash; refuse to corrupt prior steps`
|
|
882
|
-
);
|
|
883
|
-
}
|
|
884
|
-
const claim = await this.db.prepare(
|
|
885
|
-
`UPDATE durable_runs
|
|
886
|
-
SET lease_holder_id = ?,
|
|
887
|
-
lease_expires_at = ?,
|
|
888
|
-
updated_at = ?,
|
|
889
|
-
status = CASE WHEN status IN ('completed','failed') THEN status ELSE 'running' END
|
|
890
|
-
WHERE run_id = ?
|
|
891
|
-
AND (
|
|
892
|
-
lease_holder_id = ? OR
|
|
893
|
-
lease_holder_id IS NULL OR
|
|
894
|
-
lease_expires_at IS NULL OR
|
|
895
|
-
lease_expires_at < ?
|
|
896
|
-
)`
|
|
897
|
-
).bind(input.workerId, leaseExpiresAt, nowIso2, input.runId, input.workerId, nowIso2).run();
|
|
898
|
-
const changes = claim.meta?.changes ?? 0;
|
|
899
|
-
if (changes === 0) {
|
|
900
|
-
throw new DurableRunLeaseHeldError(
|
|
901
|
-
`runId ${input.runId} leased by ${existing.lease_holder_id} until ${existing.lease_expires_at}`
|
|
902
|
-
);
|
|
903
|
-
}
|
|
904
|
-
const completedSteps = await this.readSteps(input.runId, "completed");
|
|
905
|
-
const record = rowToRunRecord({
|
|
906
|
-
...existing,
|
|
907
|
-
lease_holder_id: input.workerId,
|
|
908
|
-
lease_expires_at: leaseExpiresAt,
|
|
909
|
-
updated_at: nowIso2,
|
|
910
|
-
status: existing.status === "completed" || existing.status === "failed" ? existing.status : "running"
|
|
911
|
-
});
|
|
912
|
-
return { run: record, completedSteps, leaseExpiresAt };
|
|
913
|
-
}
|
|
914
|
-
async renewLease(input) {
|
|
915
|
-
const nowMs = this.now();
|
|
916
|
-
const nowIso2 = new Date(nowMs).toISOString();
|
|
917
|
-
const leaseExpiresAt = new Date(nowMs + (input.leaseMs ?? DEFAULT_LEASE_MS)).toISOString();
|
|
918
|
-
const res = await this.db.prepare(
|
|
919
|
-
`UPDATE durable_runs
|
|
920
|
-
SET lease_expires_at = ?, updated_at = ?
|
|
921
|
-
WHERE run_id = ?
|
|
922
|
-
AND (lease_holder_id = ? OR lease_expires_at IS NULL OR lease_expires_at < ?)`
|
|
923
|
-
).bind(leaseExpiresAt, nowIso2, input.runId, input.workerId, nowIso2).run();
|
|
924
|
-
const ok = (res.meta?.changes ?? 0) > 0;
|
|
925
|
-
return ok ? { ok: true, leaseExpiresAt } : { ok: false };
|
|
926
|
-
}
|
|
927
|
-
async loadStep(runId, stepIndex) {
|
|
928
|
-
const row = await this.db.prepare("SELECT * FROM durable_steps WHERE run_id = ? AND step_index = ?").bind(runId, stepIndex).first();
|
|
929
|
-
return row ? rowToStepRecord(row) : void 0;
|
|
930
|
-
}
|
|
931
|
-
async beginStep(input) {
|
|
932
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
933
|
-
const prior = await this.loadStep(input.runId, input.stepIndex);
|
|
934
|
-
if (prior) {
|
|
935
|
-
if (prior.intent !== input.intent) {
|
|
936
|
-
throw new DurableRunDivergenceError(
|
|
937
|
-
`step ${input.stepIndex}: intent changed ('${prior.intent}' -> '${input.intent}')`
|
|
938
|
-
);
|
|
939
|
-
}
|
|
940
|
-
await this.db.prepare(
|
|
941
|
-
`UPDATE durable_steps
|
|
942
|
-
SET status='running', attempts = attempts + 1, started_at = ?, error_json = NULL
|
|
943
|
-
WHERE run_id = ? AND step_index = ?`
|
|
944
|
-
).bind(nowIso2, input.runId, input.stepIndex).run();
|
|
945
|
-
await this.bumpUpdated(input.runId, nowIso2);
|
|
946
|
-
return {
|
|
947
|
-
...prior,
|
|
948
|
-
attempts: prior.attempts + 1,
|
|
949
|
-
status: "running",
|
|
950
|
-
startedAt: nowIso2,
|
|
951
|
-
error: void 0
|
|
952
|
-
};
|
|
953
|
-
}
|
|
954
|
-
await this.db.prepare(
|
|
955
|
-
`INSERT INTO durable_steps
|
|
956
|
-
(run_id, step_index, intent, kind, input_hash, status, attempts, started_at)
|
|
957
|
-
VALUES (?, ?, ?, ?, ?, 'running', 1, ?)`
|
|
958
|
-
).bind(input.runId, input.stepIndex, input.intent, input.kind, input.inputHash, nowIso2).run();
|
|
959
|
-
await this.db.prepare(
|
|
960
|
-
`UPDATE durable_runs
|
|
961
|
-
SET step_count = MAX(step_count, ?), updated_at = ?
|
|
962
|
-
WHERE run_id = ?`
|
|
963
|
-
).bind(input.stepIndex + 1, nowIso2, input.runId).run();
|
|
964
|
-
return {
|
|
965
|
-
runId: input.runId,
|
|
966
|
-
stepIndex: input.stepIndex,
|
|
967
|
-
intent: input.intent,
|
|
968
|
-
kind: input.kind,
|
|
969
|
-
inputHash: input.inputHash,
|
|
970
|
-
status: "running",
|
|
971
|
-
attempts: 1,
|
|
972
|
-
startedAt: nowIso2
|
|
973
|
-
};
|
|
974
|
-
}
|
|
975
|
-
async completeStep(input) {
|
|
976
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
977
|
-
await this.db.prepare(
|
|
978
|
-
`UPDATE durable_steps
|
|
979
|
-
SET status='completed', result_json = ?, completed_at = ?, error_json = NULL
|
|
980
|
-
WHERE run_id = ? AND step_index = ?`
|
|
981
|
-
).bind(JSON.stringify(input.result ?? null), nowIso2, input.runId, input.stepIndex).run();
|
|
982
|
-
await this.bumpUpdated(input.runId, nowIso2);
|
|
983
|
-
const row = await this.loadStep(input.runId, input.stepIndex);
|
|
984
|
-
if (!row) {
|
|
985
|
-
throw new Error(`durable-runs: completeStep cannot find step ${input.stepIndex}`);
|
|
986
|
-
}
|
|
987
|
-
return row;
|
|
988
|
-
}
|
|
989
|
-
async failStep(input) {
|
|
990
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
991
|
-
await this.db.prepare(
|
|
992
|
-
`UPDATE durable_steps
|
|
993
|
-
SET status='failed', error_json = ?, completed_at = ?
|
|
994
|
-
WHERE run_id = ? AND step_index = ?`
|
|
995
|
-
).bind(JSON.stringify(input.error), nowIso2, input.runId, input.stepIndex).run();
|
|
996
|
-
await this.bumpUpdated(input.runId, nowIso2);
|
|
997
|
-
const row = await this.loadStep(input.runId, input.stepIndex);
|
|
998
|
-
if (!row) {
|
|
999
|
-
throw new Error(`durable-runs: failStep cannot find step ${input.stepIndex}`);
|
|
1000
|
-
}
|
|
1001
|
-
return row;
|
|
1002
|
-
}
|
|
1003
|
-
async endRun(input) {
|
|
1004
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
1005
|
-
await this.db.prepare(
|
|
1006
|
-
`UPDATE durable_runs
|
|
1007
|
-
SET status = ?, completed_at = ?, updated_at = ?,
|
|
1008
|
-
outcome_json = ?,
|
|
1009
|
-
lease_holder_id = CASE WHEN lease_holder_id = ? THEN NULL ELSE lease_holder_id END,
|
|
1010
|
-
lease_expires_at = CASE WHEN lease_holder_id = ? THEN NULL ELSE lease_expires_at END
|
|
1011
|
-
WHERE run_id = ?`
|
|
1012
|
-
).bind(
|
|
1013
|
-
input.status,
|
|
1014
|
-
nowIso2,
|
|
1015
|
-
nowIso2,
|
|
1016
|
-
input.outcome ? JSON.stringify(input.outcome) : null,
|
|
1017
|
-
input.workerId,
|
|
1018
|
-
input.workerId,
|
|
1019
|
-
input.runId
|
|
1020
|
-
).run();
|
|
1021
|
-
const row = await this.db.prepare("SELECT * FROM durable_runs WHERE run_id = ?").bind(input.runId).first();
|
|
1022
|
-
if (!row) throw new Error(`durable-runs: endRun cannot find run ${input.runId}`);
|
|
1023
|
-
return rowToRunRecord(row);
|
|
1024
|
-
}
|
|
1025
|
-
async emitEvent(input) {
|
|
1026
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
1027
|
-
const res = await this.db.prepare(
|
|
1028
|
-
`INSERT OR IGNORE INTO durable_events (run_id, key, payload_json, emitted_at)
|
|
1029
|
-
VALUES (?, ?, ?, ?)`
|
|
1030
|
-
).bind(input.runId, input.key, JSON.stringify(input.payload ?? null), nowIso2).run();
|
|
1031
|
-
const accepted = (res.meta?.changes ?? 0) > 0;
|
|
1032
|
-
const row = await this.db.prepare("SELECT * FROM durable_events WHERE run_id = ? AND key = ?").bind(input.runId, input.key).first();
|
|
1033
|
-
if (!row) throw new Error("durable-runs: emitEvent failed to persist or read back");
|
|
1034
|
-
return {
|
|
1035
|
-
accepted,
|
|
1036
|
-
record: rowToEventRecord(row)
|
|
1037
|
-
};
|
|
1038
|
-
}
|
|
1039
|
-
async loadEvent(runId, key) {
|
|
1040
|
-
const row = await this.db.prepare("SELECT * FROM durable_events WHERE run_id = ? AND key = ?").bind(runId, key).first();
|
|
1041
|
-
return row ? rowToEventRecord(row) : void 0;
|
|
1042
|
-
}
|
|
1043
|
-
async close() {
|
|
1044
|
-
}
|
|
1045
|
-
/** Inspect the currently-applied schema version. */
|
|
1046
|
-
async getSchemaVersion() {
|
|
1047
|
-
const row = await this.db.prepare("SELECT MAX(version) AS version FROM durable_schema_info").first();
|
|
1048
|
-
return row?.version ?? void 0;
|
|
1049
|
-
}
|
|
1050
|
-
// ── internals ──────────────────────────────────────────────────────
|
|
1051
|
-
async readSteps(runId, status) {
|
|
1052
|
-
const { results } = await this.db.prepare("SELECT * FROM durable_steps WHERE run_id = ? AND status = ? ORDER BY step_index").bind(runId, status).all();
|
|
1053
|
-
return results.map(rowToStepRecord);
|
|
1054
|
-
}
|
|
1055
|
-
async bumpUpdated(runId, nowIso2) {
|
|
1056
|
-
await this.db.prepare("UPDATE durable_runs SET updated_at = ? WHERE run_id = ?").bind(nowIso2, runId).run();
|
|
1057
|
-
}
|
|
1058
|
-
};
|
|
1059
|
-
function rowToRunRecord(row) {
|
|
1060
|
-
return {
|
|
1061
|
-
runId: row.run_id,
|
|
1062
|
-
manifestHash: row.manifest_hash,
|
|
1063
|
-
projectId: row.project_id,
|
|
1064
|
-
scenarioId: row.scenario_id ?? void 0,
|
|
1065
|
-
status: row.status,
|
|
1066
|
-
createdAt: row.created_at,
|
|
1067
|
-
updatedAt: row.updated_at,
|
|
1068
|
-
completedAt: row.completed_at ?? void 0,
|
|
1069
|
-
leaseHolderId: row.lease_holder_id ?? void 0,
|
|
1070
|
-
leaseExpiresAt: row.lease_expires_at ?? void 0,
|
|
1071
|
-
outcome: row.outcome_json ? JSON.parse(row.outcome_json) : void 0,
|
|
1072
|
-
stepCount: row.step_count
|
|
1073
|
-
};
|
|
1074
|
-
}
|
|
1075
|
-
function rowToStepRecord(row) {
|
|
1076
|
-
return {
|
|
1077
|
-
runId: row.run_id,
|
|
1078
|
-
stepIndex: row.step_index,
|
|
1079
|
-
intent: row.intent,
|
|
1080
|
-
kind: row.kind,
|
|
1081
|
-
inputHash: row.input_hash,
|
|
1082
|
-
status: row.status,
|
|
1083
|
-
attempts: row.attempts,
|
|
1084
|
-
result: row.result_json ? JSON.parse(row.result_json) : void 0,
|
|
1085
|
-
error: row.error_json ? JSON.parse(row.error_json) : void 0,
|
|
1086
|
-
startedAt: row.started_at ?? void 0,
|
|
1087
|
-
completedAt: row.completed_at ?? void 0
|
|
1088
|
-
};
|
|
1089
|
-
}
|
|
1090
|
-
function rowToEventRecord(row) {
|
|
1091
|
-
return {
|
|
1092
|
-
runId: row.run_id,
|
|
1093
|
-
key: row.key,
|
|
1094
|
-
payload: row.payload_json ? JSON.parse(row.payload_json) : null,
|
|
1095
|
-
emittedAt: row.emitted_at
|
|
1096
|
-
};
|
|
1097
|
-
}
|
|
1098
|
-
|
|
1099
|
-
// src/durable/file-system-store.ts
|
|
1100
|
-
import { existsSync, mkdirSync } from "fs";
|
|
1101
|
-
import { appendFile, readdir, readFile, rename, writeFile } from "fs/promises";
|
|
1102
|
-
import { join } from "path";
|
|
1103
|
-
var DEFAULT_LEASE_MS2 = 3e4;
|
|
1104
|
-
var FileSystemDurableRunStore = class {
|
|
1105
|
-
constructor(root) {
|
|
1106
|
-
this.root = root;
|
|
1107
|
-
mkdirSync(root, { recursive: true });
|
|
1108
|
-
}
|
|
1109
|
-
root;
|
|
1110
|
-
/** Override for tests — defaults to Date.now(). */
|
|
1111
|
-
now = () => Date.now();
|
|
1112
|
-
async startOrResume(input) {
|
|
1113
|
-
const leaseMs = input.leaseMs ?? DEFAULT_LEASE_MS2;
|
|
1114
|
-
const hash = manifestHash(input.manifest);
|
|
1115
|
-
const nowMs = this.now();
|
|
1116
|
-
const nowIso2 = new Date(nowMs).toISOString();
|
|
1117
|
-
const leaseExpiresAt = new Date(nowMs + leaseMs).toISOString();
|
|
1118
|
-
const dir = this.runDir(input.runId);
|
|
1119
|
-
if (!existsSync(dir)) {
|
|
1120
|
-
mkdirSync(dir, { recursive: true });
|
|
1121
|
-
const record2 = {
|
|
1122
|
-
runId: input.runId,
|
|
1123
|
-
manifestHash: hash,
|
|
1124
|
-
projectId: input.manifest.projectId,
|
|
1125
|
-
scenarioId: input.manifest.scenarioId,
|
|
1126
|
-
status: "running",
|
|
1127
|
-
createdAt: nowIso2,
|
|
1128
|
-
updatedAt: nowIso2,
|
|
1129
|
-
leaseHolderId: input.workerId,
|
|
1130
|
-
leaseExpiresAt,
|
|
1131
|
-
stepCount: 0
|
|
1132
|
-
};
|
|
1133
|
-
await this.writeRun(record2);
|
|
1134
|
-
await this.writeLease(input.runId, { workerId: input.workerId, leaseExpiresAt });
|
|
1135
|
-
await appendFile(join(dir, "steps.jsonl"), "", "utf8");
|
|
1136
|
-
await appendFile(join(dir, "events.jsonl"), "", "utf8");
|
|
1137
|
-
return { run: record2, completedSteps: [], leaseExpiresAt };
|
|
1138
|
-
}
|
|
1139
|
-
const record = await this.readRun(input.runId);
|
|
1140
|
-
if (record.manifestHash !== hash) {
|
|
1141
|
-
throw new DurableRunInputMismatchError(
|
|
1142
|
-
`runId ${input.runId} exists with a different manifest hash; refuse to corrupt prior steps`
|
|
1143
|
-
);
|
|
1144
|
-
}
|
|
1145
|
-
const lease = await this.readLeaseSafe(input.runId);
|
|
1146
|
-
const leaseStillLive = lease && lease.workerId !== input.workerId && new Date(lease.leaseExpiresAt).getTime() > nowMs;
|
|
1147
|
-
if (leaseStillLive) {
|
|
1148
|
-
throw new DurableRunLeaseHeldError(
|
|
1149
|
-
`runId ${input.runId} leased by ${lease.workerId} until ${lease.leaseExpiresAt}`
|
|
1150
|
-
);
|
|
1151
|
-
}
|
|
1152
|
-
const completedSteps = await this.readSteps(input.runId);
|
|
1153
|
-
const nextRecord = {
|
|
1154
|
-
...record,
|
|
1155
|
-
status: record.status === "completed" || record.status === "failed" ? record.status : "running",
|
|
1156
|
-
updatedAt: nowIso2,
|
|
1157
|
-
leaseHolderId: input.workerId,
|
|
1158
|
-
leaseExpiresAt
|
|
1159
|
-
};
|
|
1160
|
-
await this.writeRun(nextRecord);
|
|
1161
|
-
await this.writeLease(input.runId, { workerId: input.workerId, leaseExpiresAt });
|
|
1162
|
-
return { run: nextRecord, completedSteps, leaseExpiresAt };
|
|
1163
|
-
}
|
|
1164
|
-
async renewLease(input) {
|
|
1165
|
-
const lease = await this.readLeaseSafe(input.runId);
|
|
1166
|
-
const nowMs = this.now();
|
|
1167
|
-
if (lease && lease.workerId !== input.workerId) {
|
|
1168
|
-
if (new Date(lease.leaseExpiresAt).getTime() > nowMs) {
|
|
1169
|
-
return { ok: false };
|
|
1170
|
-
}
|
|
1171
|
-
}
|
|
1172
|
-
const leaseExpiresAt = new Date(nowMs + (input.leaseMs ?? DEFAULT_LEASE_MS2)).toISOString();
|
|
1173
|
-
await this.writeLease(input.runId, { workerId: input.workerId, leaseExpiresAt });
|
|
1174
|
-
return { ok: true, leaseExpiresAt };
|
|
1175
|
-
}
|
|
1176
|
-
async loadStep(runId, stepIndex) {
|
|
1177
|
-
const steps = await this.readSteps(runId, { includeFailed: true, includeRunning: true });
|
|
1178
|
-
return steps.find((s) => s.stepIndex === stepIndex);
|
|
1179
|
-
}
|
|
1180
|
-
async beginStep(input) {
|
|
1181
|
-
const all = await this.readSteps(input.runId, { includeFailed: true, includeRunning: true });
|
|
1182
|
-
const prior = all.find((s) => s.stepIndex === input.stepIndex);
|
|
1183
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
1184
|
-
if (prior) {
|
|
1185
|
-
if (prior.intent !== input.intent) {
|
|
1186
|
-
throw new DurableRunDivergenceError(
|
|
1187
|
-
`step ${input.stepIndex}: intent changed ('${prior.intent}' -> '${input.intent}')`
|
|
1188
|
-
);
|
|
1189
|
-
}
|
|
1190
|
-
const rec2 = {
|
|
1191
|
-
...prior,
|
|
1192
|
-
attempts: prior.attempts + 1,
|
|
1193
|
-
status: "running",
|
|
1194
|
-
startedAt: nowIso2,
|
|
1195
|
-
error: void 0
|
|
1196
|
-
};
|
|
1197
|
-
await this.appendStep(input.runId, rec2);
|
|
1198
|
-
await this.bumpRunUpdated(input.runId, nowIso2);
|
|
1199
|
-
return rec2;
|
|
1200
|
-
}
|
|
1201
|
-
const rec = {
|
|
1202
|
-
runId: input.runId,
|
|
1203
|
-
stepIndex: input.stepIndex,
|
|
1204
|
-
intent: input.intent,
|
|
1205
|
-
kind: input.kind,
|
|
1206
|
-
inputHash: input.inputHash,
|
|
1207
|
-
status: "running",
|
|
1208
|
-
attempts: 1,
|
|
1209
|
-
startedAt: nowIso2
|
|
1210
|
-
};
|
|
1211
|
-
await this.appendStep(input.runId, rec);
|
|
1212
|
-
const record = await this.readRun(input.runId);
|
|
1213
|
-
record.stepCount = Math.max(record.stepCount, input.stepIndex + 1);
|
|
1214
|
-
record.updatedAt = nowIso2;
|
|
1215
|
-
await this.writeRun(record);
|
|
1216
|
-
return rec;
|
|
1217
|
-
}
|
|
1218
|
-
async completeStep(input) {
|
|
1219
|
-
const all = await this.readSteps(input.runId, { includeFailed: true, includeRunning: true });
|
|
1220
|
-
const prior = all.find((s) => s.stepIndex === input.stepIndex);
|
|
1221
|
-
if (!prior) {
|
|
1222
|
-
throw new Error(
|
|
1223
|
-
`durable-runs: completeStep called before beginStep (step ${input.stepIndex})`
|
|
1224
|
-
);
|
|
1225
|
-
}
|
|
1226
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
1227
|
-
const rec = {
|
|
1228
|
-
...prior,
|
|
1229
|
-
status: "completed",
|
|
1230
|
-
result: input.result,
|
|
1231
|
-
completedAt: nowIso2,
|
|
1232
|
-
error: void 0
|
|
1233
|
-
};
|
|
1234
|
-
await this.appendStep(input.runId, rec);
|
|
1235
|
-
await this.bumpRunUpdated(input.runId, nowIso2);
|
|
1236
|
-
return rec;
|
|
1237
|
-
}
|
|
1238
|
-
async failStep(input) {
|
|
1239
|
-
const all = await this.readSteps(input.runId, { includeFailed: true, includeRunning: true });
|
|
1240
|
-
const prior = all.find((s) => s.stepIndex === input.stepIndex);
|
|
1241
|
-
if (!prior) {
|
|
1242
|
-
throw new Error(`durable-runs: failStep called before beginStep (step ${input.stepIndex})`);
|
|
1243
|
-
}
|
|
1244
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
1245
|
-
const rec = {
|
|
1246
|
-
...prior,
|
|
1247
|
-
status: "failed",
|
|
1248
|
-
error: input.error,
|
|
1249
|
-
completedAt: nowIso2
|
|
1250
|
-
};
|
|
1251
|
-
await this.appendStep(input.runId, rec);
|
|
1252
|
-
await this.bumpRunUpdated(input.runId, nowIso2);
|
|
1253
|
-
return rec;
|
|
1254
|
-
}
|
|
1255
|
-
async endRun(input) {
|
|
1256
|
-
const record = await this.readRun(input.runId);
|
|
1257
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
1258
|
-
record.status = input.status;
|
|
1259
|
-
record.outcome = input.outcome;
|
|
1260
|
-
record.completedAt = nowIso2;
|
|
1261
|
-
record.updatedAt = nowIso2;
|
|
1262
|
-
const lease = await this.readLeaseSafe(input.runId);
|
|
1263
|
-
if (lease && lease.workerId === input.workerId) {
|
|
1264
|
-
record.leaseHolderId = void 0;
|
|
1265
|
-
record.leaseExpiresAt = void 0;
|
|
1266
|
-
await this.writeLease(input.runId, null);
|
|
1267
|
-
}
|
|
1268
|
-
await this.writeRun(record);
|
|
1269
|
-
return record;
|
|
1270
|
-
}
|
|
1271
|
-
async emitEvent(input) {
|
|
1272
|
-
const existing = await this.loadEvent(input.runId, input.key);
|
|
1273
|
-
if (existing) return { accepted: false, record: existing };
|
|
1274
|
-
const rec = {
|
|
1275
|
-
runId: input.runId,
|
|
1276
|
-
key: input.key,
|
|
1277
|
-
payload: input.payload,
|
|
1278
|
-
emittedAt: new Date(this.now()).toISOString()
|
|
1279
|
-
};
|
|
1280
|
-
await appendFile(
|
|
1281
|
-
join(this.runDir(input.runId), "events.jsonl"),
|
|
1282
|
-
`${JSON.stringify(rec)}
|
|
1283
|
-
`,
|
|
1284
|
-
"utf8"
|
|
1285
|
-
);
|
|
1286
|
-
return { accepted: true, record: rec };
|
|
1287
|
-
}
|
|
1288
|
-
async loadEvent(runId, key) {
|
|
1289
|
-
const path = join(this.runDir(runId), "events.jsonl");
|
|
1290
|
-
if (!existsSync(path)) return void 0;
|
|
1291
|
-
const content = await readFile(path, "utf8");
|
|
1292
|
-
for (const line of content.split("\n").reverse()) {
|
|
1293
|
-
if (!line) continue;
|
|
1294
|
-
const rec = JSON.parse(line);
|
|
1295
|
-
if (rec.key === key) return rec;
|
|
1296
|
-
}
|
|
1297
|
-
return void 0;
|
|
1298
|
-
}
|
|
1299
|
-
async close() {
|
|
1300
|
-
}
|
|
1301
|
-
/** @internal — used by tests to list runs in the store. */
|
|
1302
|
-
async _listRunIds() {
|
|
1303
|
-
if (!existsSync(this.root)) return [];
|
|
1304
|
-
const entries = await readdir(this.root, { withFileTypes: true });
|
|
1305
|
-
return entries.filter((e) => e.isDirectory()).map((e) => e.name);
|
|
1306
|
-
}
|
|
1307
|
-
// ── internals ──────────────────────────────────────────────────────
|
|
1308
|
-
runDir(runId) {
|
|
1309
|
-
return join(this.root, runId);
|
|
1310
|
-
}
|
|
1311
|
-
async readRun(runId) {
|
|
1312
|
-
const path = join(this.runDir(runId), "run.json");
|
|
1313
|
-
const content = await readFile(path, "utf8");
|
|
1314
|
-
return JSON.parse(content);
|
|
1315
|
-
}
|
|
1316
|
-
async writeRun(record) {
|
|
1317
|
-
const dir = this.runDir(record.runId);
|
|
1318
|
-
const path = join(dir, "run.json");
|
|
1319
|
-
const tmp = `${path}.tmp`;
|
|
1320
|
-
await writeFile(tmp, JSON.stringify(record, null, 2), "utf8");
|
|
1321
|
-
await rename(tmp, path);
|
|
1322
|
-
}
|
|
1323
|
-
async readLeaseSafe(runId) {
|
|
1324
|
-
const path = join(this.runDir(runId), "lease.json");
|
|
1325
|
-
if (!existsSync(path)) return void 0;
|
|
1326
|
-
try {
|
|
1327
|
-
const content = await readFile(path, "utf8");
|
|
1328
|
-
if (!content.trim()) return void 0;
|
|
1329
|
-
return JSON.parse(content);
|
|
1330
|
-
} catch {
|
|
1331
|
-
return void 0;
|
|
1332
|
-
}
|
|
1333
|
-
}
|
|
1334
|
-
async writeLease(runId, lease) {
|
|
1335
|
-
const path = join(this.runDir(runId), "lease.json");
|
|
1336
|
-
const tmp = `${path}.tmp`;
|
|
1337
|
-
await writeFile(tmp, lease ? JSON.stringify(lease) : "", "utf8");
|
|
1338
|
-
await rename(tmp, path);
|
|
1339
|
-
}
|
|
1340
|
-
async readSteps(runId, opts = {}) {
|
|
1341
|
-
const path = join(this.runDir(runId), "steps.jsonl");
|
|
1342
|
-
if (!existsSync(path)) return [];
|
|
1343
|
-
const content = await readFile(path, "utf8");
|
|
1344
|
-
const latest = /* @__PURE__ */ new Map();
|
|
1345
|
-
for (const line of content.split("\n")) {
|
|
1346
|
-
if (!line) continue;
|
|
1347
|
-
const rec = JSON.parse(line);
|
|
1348
|
-
latest.set(rec.stepIndex, rec);
|
|
1349
|
-
}
|
|
1350
|
-
const out = [...latest.values()].sort((a, b) => a.stepIndex - b.stepIndex);
|
|
1351
|
-
return out.filter((s) => {
|
|
1352
|
-
if (s.status === "completed") return true;
|
|
1353
|
-
if (s.status === "failed") return opts.includeFailed ?? false;
|
|
1354
|
-
if (s.status === "running") return opts.includeRunning ?? false;
|
|
1355
|
-
return false;
|
|
1356
|
-
});
|
|
1357
|
-
}
|
|
1358
|
-
async appendStep(runId, rec) {
|
|
1359
|
-
await appendFile(join(this.runDir(runId), "steps.jsonl"), `${JSON.stringify(rec)}
|
|
1360
|
-
`, "utf8");
|
|
1361
|
-
}
|
|
1362
|
-
async bumpRunUpdated(runId, nowIso2) {
|
|
1363
|
-
const record = await this.readRun(runId);
|
|
1364
|
-
record.updatedAt = nowIso2;
|
|
1365
|
-
await this.writeRun(record);
|
|
1366
|
-
}
|
|
1367
|
-
};
|
|
1368
|
-
|
|
1369
|
-
// src/durable/in-memory-store.ts
|
|
1370
|
-
var DEFAULT_LEASE_MS3 = 3e4;
|
|
1371
|
-
var InMemoryDurableRunStore = class {
|
|
1372
|
-
runs = /* @__PURE__ */ new Map();
|
|
1373
|
-
/** Override for tests — defaults to Date.now(). */
|
|
1374
|
-
now = () => Date.now();
|
|
1375
|
-
async startOrResume(input) {
|
|
1376
|
-
const leaseMs = input.leaseMs ?? DEFAULT_LEASE_MS3;
|
|
1377
|
-
const hash = manifestHash(input.manifest);
|
|
1378
|
-
const nowMs = this.now();
|
|
1379
|
-
const nowIso2 = new Date(nowMs).toISOString();
|
|
1380
|
-
const leaseExpiresMs = nowMs + leaseMs;
|
|
1381
|
-
const leaseExpiresAt = new Date(leaseExpiresMs).toISOString();
|
|
1382
|
-
let state = this.runs.get(input.runId);
|
|
1383
|
-
if (!state) {
|
|
1384
|
-
const record = {
|
|
1385
|
-
runId: input.runId,
|
|
1386
|
-
manifestHash: hash,
|
|
1387
|
-
projectId: input.manifest.projectId,
|
|
1388
|
-
scenarioId: input.manifest.scenarioId,
|
|
1389
|
-
status: "running",
|
|
1390
|
-
createdAt: nowIso2,
|
|
1391
|
-
updatedAt: nowIso2,
|
|
1392
|
-
leaseHolderId: input.workerId,
|
|
1393
|
-
leaseExpiresAt,
|
|
1394
|
-
stepCount: 0
|
|
1395
|
-
};
|
|
1396
|
-
state = { record, steps: /* @__PURE__ */ new Map(), events: /* @__PURE__ */ new Map() };
|
|
1397
|
-
this.runs.set(input.runId, state);
|
|
1398
|
-
return { run: { ...record }, completedSteps: [], leaseExpiresAt };
|
|
1399
|
-
}
|
|
1400
|
-
if (state.record.manifestHash !== hash) {
|
|
1401
|
-
throw new DurableRunInputMismatchError(
|
|
1402
|
-
`runId ${input.runId} exists with a different manifest hash; refuse to corrupt prior steps`
|
|
1403
|
-
);
|
|
1404
|
-
}
|
|
1405
|
-
const leaseStillLive = state.record.leaseHolderId !== void 0 && state.record.leaseHolderId !== input.workerId && state.record.leaseExpiresAt !== void 0 && new Date(state.record.leaseExpiresAt).getTime() > nowMs;
|
|
1406
|
-
if (leaseStillLive) {
|
|
1407
|
-
throw new DurableRunLeaseHeldError(
|
|
1408
|
-
`runId ${input.runId} is leased by ${state.record.leaseHolderId} until ${state.record.leaseExpiresAt}`
|
|
1409
|
-
);
|
|
1410
|
-
}
|
|
1411
|
-
state.record.leaseHolderId = input.workerId;
|
|
1412
|
-
state.record.leaseExpiresAt = leaseExpiresAt;
|
|
1413
|
-
state.record.status = state.record.status === "completed" || state.record.status === "failed" ? state.record.status : "running";
|
|
1414
|
-
state.record.updatedAt = nowIso2;
|
|
1415
|
-
const completed = [...state.steps.values()].filter((s) => s.status === "completed").sort((a, b) => a.stepIndex - b.stepIndex);
|
|
1416
|
-
return {
|
|
1417
|
-
run: { ...state.record },
|
|
1418
|
-
completedSteps: completed.map((s) => ({ ...s })),
|
|
1419
|
-
leaseExpiresAt
|
|
1420
|
-
};
|
|
1421
|
-
}
|
|
1422
|
-
async renewLease(input) {
|
|
1423
|
-
const state = this.runs.get(input.runId);
|
|
1424
|
-
if (!state) return { ok: false };
|
|
1425
|
-
const nowMs = this.now();
|
|
1426
|
-
if (state.record.leaseHolderId !== input.workerId) {
|
|
1427
|
-
if (state.record.leaseExpiresAt && new Date(state.record.leaseExpiresAt).getTime() > nowMs) {
|
|
1428
|
-
return { ok: false };
|
|
1429
|
-
}
|
|
1430
|
-
}
|
|
1431
|
-
const leaseExpiresMs = nowMs + (input.leaseMs ?? DEFAULT_LEASE_MS3);
|
|
1432
|
-
const leaseExpiresAt = new Date(leaseExpiresMs).toISOString();
|
|
1433
|
-
state.record.leaseHolderId = input.workerId;
|
|
1434
|
-
state.record.leaseExpiresAt = leaseExpiresAt;
|
|
1435
|
-
state.record.updatedAt = new Date(nowMs).toISOString();
|
|
1436
|
-
return { ok: true, leaseExpiresAt };
|
|
1437
|
-
}
|
|
1438
|
-
async loadStep(runId, stepIndex) {
|
|
1439
|
-
const state = this.runs.get(runId);
|
|
1440
|
-
return state ? cloneStep(state.steps.get(stepIndex)) : void 0;
|
|
1441
|
-
}
|
|
1442
|
-
async beginStep(input) {
|
|
1443
|
-
const state = this.requireRun(input.runId);
|
|
1444
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
1445
|
-
const prior = state.steps.get(input.stepIndex);
|
|
1446
|
-
if (prior) {
|
|
1447
|
-
if (prior.intent !== input.intent) {
|
|
1448
|
-
throw new DurableRunDivergenceError(
|
|
1449
|
-
`step ${input.stepIndex}: intent changed across replays ('${prior.intent}' -> '${input.intent}')`
|
|
1450
|
-
);
|
|
1451
|
-
}
|
|
1452
|
-
prior.attempts += 1;
|
|
1453
|
-
prior.status = "running";
|
|
1454
|
-
prior.startedAt = nowIso2;
|
|
1455
|
-
prior.error = void 0;
|
|
1456
|
-
state.record.updatedAt = nowIso2;
|
|
1457
|
-
return cloneStep(prior);
|
|
1458
|
-
}
|
|
1459
|
-
const rec = {
|
|
1460
|
-
runId: input.runId,
|
|
1461
|
-
stepIndex: input.stepIndex,
|
|
1462
|
-
intent: input.intent,
|
|
1463
|
-
kind: input.kind,
|
|
1464
|
-
inputHash: input.inputHash,
|
|
1465
|
-
status: "running",
|
|
1466
|
-
attempts: 1,
|
|
1467
|
-
startedAt: nowIso2
|
|
1468
|
-
};
|
|
1469
|
-
state.steps.set(input.stepIndex, rec);
|
|
1470
|
-
state.record.stepCount = Math.max(state.record.stepCount, input.stepIndex + 1);
|
|
1471
|
-
state.record.updatedAt = nowIso2;
|
|
1472
|
-
return cloneStep(rec);
|
|
1473
|
-
}
|
|
1474
|
-
async completeStep(input) {
|
|
1475
|
-
const state = this.requireRun(input.runId);
|
|
1476
|
-
const rec = state.steps.get(input.stepIndex);
|
|
1477
|
-
if (!rec) {
|
|
1478
|
-
throw new Error(
|
|
1479
|
-
`durable-runs: completeStep called before beginStep (step ${input.stepIndex})`
|
|
1480
|
-
);
|
|
1481
|
-
}
|
|
1482
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
1483
|
-
rec.status = "completed";
|
|
1484
|
-
rec.result = input.result;
|
|
1485
|
-
rec.completedAt = nowIso2;
|
|
1486
|
-
rec.error = void 0;
|
|
1487
|
-
state.record.updatedAt = nowIso2;
|
|
1488
|
-
return cloneStep(rec);
|
|
1489
|
-
}
|
|
1490
|
-
async failStep(input) {
|
|
1491
|
-
const state = this.requireRun(input.runId);
|
|
1492
|
-
const rec = state.steps.get(input.stepIndex);
|
|
1493
|
-
if (!rec) {
|
|
1494
|
-
throw new Error(`durable-runs: failStep called before beginStep (step ${input.stepIndex})`);
|
|
1495
|
-
}
|
|
1496
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
1497
|
-
rec.status = "failed";
|
|
1498
|
-
rec.error = input.error;
|
|
1499
|
-
rec.completedAt = nowIso2;
|
|
1500
|
-
state.record.updatedAt = nowIso2;
|
|
1501
|
-
return cloneStep(rec);
|
|
1502
|
-
}
|
|
1503
|
-
async endRun(input) {
|
|
1504
|
-
const state = this.requireRun(input.runId);
|
|
1505
|
-
const nowIso2 = new Date(this.now()).toISOString();
|
|
1506
|
-
state.record.status = input.status;
|
|
1507
|
-
state.record.outcome = input.outcome;
|
|
1508
|
-
state.record.completedAt = nowIso2;
|
|
1509
|
-
state.record.updatedAt = nowIso2;
|
|
1510
|
-
if (state.record.leaseHolderId === input.workerId) {
|
|
1511
|
-
state.record.leaseHolderId = void 0;
|
|
1512
|
-
state.record.leaseExpiresAt = void 0;
|
|
1513
|
-
}
|
|
1514
|
-
return { ...state.record };
|
|
1515
|
-
}
|
|
1516
|
-
async emitEvent(input) {
|
|
1517
|
-
const state = this.requireRun(input.runId);
|
|
1518
|
-
const existing = state.events.get(input.key);
|
|
1519
|
-
if (existing) {
|
|
1520
|
-
return { accepted: false, record: { ...existing } };
|
|
1521
|
-
}
|
|
1522
|
-
const rec = {
|
|
1523
|
-
runId: input.runId,
|
|
1524
|
-
key: input.key,
|
|
1525
|
-
payload: input.payload,
|
|
1526
|
-
emittedAt: new Date(this.now()).toISOString()
|
|
1527
|
-
};
|
|
1528
|
-
state.events.set(input.key, rec);
|
|
1529
|
-
return { accepted: true, record: { ...rec } };
|
|
1530
|
-
}
|
|
1531
|
-
async loadEvent(runId, key) {
|
|
1532
|
-
const state = this.runs.get(runId);
|
|
1533
|
-
if (!state) return void 0;
|
|
1534
|
-
const rec = state.events.get(key);
|
|
1535
|
-
return rec ? { ...rec } : void 0;
|
|
1536
|
-
}
|
|
1537
|
-
async close() {
|
|
1538
|
-
this.runs.clear();
|
|
1539
|
-
}
|
|
1540
|
-
// ── test helpers ───────────────────────────────────────────────────
|
|
1541
|
-
/** @internal — used by tests to inspect lease metadata. */
|
|
1542
|
-
_inspect(runId) {
|
|
1543
|
-
const s = this.runs.get(runId);
|
|
1544
|
-
return s ? { ...s.record } : void 0;
|
|
1545
|
-
}
|
|
1546
|
-
/** @internal — used by tests to simulate lease expiry. */
|
|
1547
|
-
_expireLease(runId) {
|
|
1548
|
-
const s = this.runs.get(runId);
|
|
1549
|
-
if (s) {
|
|
1550
|
-
s.record.leaseHolderId = void 0;
|
|
1551
|
-
s.record.leaseExpiresAt = void 0;
|
|
1552
|
-
}
|
|
1553
|
-
}
|
|
1554
|
-
requireRun(runId) {
|
|
1555
|
-
const s = this.runs.get(runId);
|
|
1556
|
-
if (!s) {
|
|
1557
|
-
throw new Error(`durable-runs: run ${runId} not found (must call startOrResume first)`);
|
|
1558
|
-
}
|
|
1559
|
-
return s;
|
|
1560
|
-
}
|
|
1561
|
-
};
|
|
1562
|
-
function cloneStep(rec) {
|
|
1563
|
-
if (!rec) return void 0;
|
|
1564
|
-
return { ...rec, error: rec.error ? { ...rec.error } : void 0 };
|
|
1565
|
-
}
|
|
1566
|
-
|
|
1567
|
-
// src/durable/runner.ts
|
|
1568
|
-
var DEFAULT_LEASE_MS4 = 3e4;
|
|
1569
|
-
var DEFAULT_AWAIT_POLL_MS = 250;
|
|
1570
|
-
async function runDurable(input) {
|
|
1571
|
-
const workerId = input.workerId ?? deriveWorkerId();
|
|
1572
|
-
const leaseMs = input.leaseMs ?? DEFAULT_LEASE_MS4;
|
|
1573
|
-
const { completedSteps } = await input.store.startOrResume({
|
|
1574
|
-
runId: input.runId,
|
|
1575
|
-
manifest: input.manifest,
|
|
1576
|
-
workerId,
|
|
1577
|
-
leaseMs
|
|
1578
|
-
});
|
|
1579
|
-
const priorByIndex = /* @__PURE__ */ new Map();
|
|
1580
|
-
for (const s of completedSteps) priorByIndex.set(s.stepIndex, s);
|
|
1581
|
-
const collected = [...completedSteps].sort((a, b) => a.stepIndex - b.stepIndex);
|
|
1582
|
-
let leaseLost = false;
|
|
1583
|
-
const heartbeatIntervalMs = Math.max(1e3, Math.floor(leaseMs / 3));
|
|
1584
|
-
const heartbeat = setInterval(() => {
|
|
1585
|
-
void input.store.renewLease({ runId: input.runId, workerId, leaseMs }).then((res) => {
|
|
1586
|
-
if (!res.ok) leaseLost = true;
|
|
1587
|
-
}).catch(() => {
|
|
1588
|
-
leaseLost = true;
|
|
1589
|
-
});
|
|
1590
|
-
}, heartbeatIntervalMs);
|
|
1591
|
-
if (typeof heartbeat.unref === "function") heartbeat.unref();
|
|
1592
|
-
let positionCounter = 0;
|
|
1593
|
-
const ctx = {
|
|
1594
|
-
runId: input.runId,
|
|
1595
|
-
projectId: input.manifest.projectId,
|
|
1596
|
-
scenarioId: input.manifest.scenarioId,
|
|
1597
|
-
async step(intent, fn, opts) {
|
|
1598
|
-
checkAbortAndLease(input.signal, leaseLost);
|
|
1599
|
-
const stepIndex = positionCounter++;
|
|
1600
|
-
const prior = priorByIndex.get(stepIndex);
|
|
1601
|
-
const inputHash = opts?.inputFingerprint !== void 0 ? canonicalHash(opts.inputFingerprint) : "";
|
|
1602
|
-
if (prior && prior.status === "completed") {
|
|
1603
|
-
if (prior.intent !== intent) {
|
|
1604
|
-
throw new DurableRunDivergenceError(
|
|
1605
|
-
`step ${stepIndex}: intent changed across replays ('${prior.intent}' -> '${intent}')`
|
|
1606
|
-
);
|
|
571
|
+
if (hooks.onTurnComplete) {
|
|
572
|
+
try {
|
|
573
|
+
await hooks.onTurnComplete({ identity, finalText });
|
|
574
|
+
} catch (err) {
|
|
575
|
+
log("[chat-engine] onTurnComplete threw", {
|
|
576
|
+
error: err instanceof Error ? err.message : String(err)
|
|
577
|
+
});
|
|
578
|
+
}
|
|
1607
579
|
}
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
runId: input.runId,
|
|
1612
|
-
stepIndex,
|
|
1613
|
-
intent,
|
|
1614
|
-
kind: opts?.kind ?? "logic",
|
|
1615
|
-
inputHash
|
|
1616
|
-
});
|
|
1617
|
-
try {
|
|
1618
|
-
const result = await fn();
|
|
1619
|
-
const completed = await input.store.completeStep({
|
|
1620
|
-
runId: input.runId,
|
|
1621
|
-
stepIndex,
|
|
1622
|
-
result
|
|
580
|
+
await emit2({
|
|
581
|
+
type: "session.run.completed",
|
|
582
|
+
data: { sessionId: identity.sessionId }
|
|
1623
583
|
});
|
|
1624
|
-
upsertCollected(collected, completed);
|
|
1625
|
-
priorByIndex.set(stepIndex, completed);
|
|
1626
|
-
return result;
|
|
1627
584
|
} catch (err) {
|
|
1628
|
-
const
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
585
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
586
|
+
log("[chat-engine] turn failed", { error: message });
|
|
587
|
+
await emit2({ type: "error", data: { message } });
|
|
588
|
+
await emit2({
|
|
589
|
+
type: "session.run.failed",
|
|
590
|
+
data: { sessionId: identity.sessionId, message }
|
|
1632
591
|
});
|
|
1633
|
-
upsertCollected(collected, failed);
|
|
1634
|
-
priorByIndex.set(stepIndex, failed);
|
|
1635
|
-
throw err;
|
|
1636
592
|
} finally {
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
const prior = priorByIndex.get(stepIndex);
|
|
1643
|
-
if (prior && prior.status === "completed") {
|
|
1644
|
-
if (prior.intent !== `event:${key}`) {
|
|
1645
|
-
throw new DurableRunDivergenceError(
|
|
1646
|
-
`step ${stepIndex}: awaitEvent key changed across replays`
|
|
593
|
+
if (hooks.traceFlush) {
|
|
594
|
+
const flush = hooks.traceFlush().catch(
|
|
595
|
+
(err) => log("[chat-engine] traceFlush threw", {
|
|
596
|
+
error: err instanceof Error ? err.message : String(err)
|
|
597
|
+
})
|
|
1647
598
|
);
|
|
599
|
+
if (input.waitUntil) input.waitUntil(flush);
|
|
600
|
+
else await flush;
|
|
1648
601
|
}
|
|
1649
|
-
|
|
1650
|
-
}
|
|
1651
|
-
const beginAt = Date.now();
|
|
1652
|
-
const timeoutMs = opts?.timeoutMs ?? 6e4;
|
|
1653
|
-
const pollMs = opts?.pollMs ?? DEFAULT_AWAIT_POLL_MS;
|
|
1654
|
-
await input.store.beginStep({
|
|
1655
|
-
runId: input.runId,
|
|
1656
|
-
stepIndex,
|
|
1657
|
-
intent: `event:${key}`,
|
|
1658
|
-
kind: "event",
|
|
1659
|
-
inputHash: ""
|
|
1660
|
-
});
|
|
1661
|
-
try {
|
|
1662
|
-
for (; ; ) {
|
|
1663
|
-
checkAbortAndLease(input.signal, leaseLost);
|
|
1664
|
-
const evt = await input.store.loadEvent(input.runId, key);
|
|
1665
|
-
if (evt) {
|
|
1666
|
-
const completed = await input.store.completeStep({
|
|
1667
|
-
runId: input.runId,
|
|
1668
|
-
stepIndex,
|
|
1669
|
-
result: evt.payload
|
|
1670
|
-
});
|
|
1671
|
-
upsertCollected(collected, completed);
|
|
1672
|
-
priorByIndex.set(stepIndex, completed);
|
|
1673
|
-
return evt.payload;
|
|
1674
|
-
}
|
|
1675
|
-
if (Date.now() - beginAt > timeoutMs) {
|
|
1676
|
-
const err = new DurableAwaitEventTimeoutError(
|
|
1677
|
-
`awaitEvent('${key}') timed out after ${timeoutMs}ms`
|
|
1678
|
-
);
|
|
1679
|
-
const failed = await input.store.failStep({
|
|
1680
|
-
runId: input.runId,
|
|
1681
|
-
stepIndex,
|
|
1682
|
-
error: toStepError(err)
|
|
1683
|
-
});
|
|
1684
|
-
upsertCollected(collected, failed);
|
|
1685
|
-
priorByIndex.set(stepIndex, failed);
|
|
1686
|
-
throw err;
|
|
1687
|
-
}
|
|
1688
|
-
await sleep2(pollMs, input.signal);
|
|
1689
|
-
}
|
|
1690
|
-
} catch (err) {
|
|
1691
|
-
if (!(err instanceof DurableAwaitEventTimeoutError)) {
|
|
1692
|
-
const existing = priorByIndex.get(stepIndex);
|
|
1693
|
-
if (!existing || existing.status !== "failed") {
|
|
1694
|
-
const failed = await input.store.failStep({
|
|
1695
|
-
runId: input.runId,
|
|
1696
|
-
stepIndex,
|
|
1697
|
-
error: toStepError(err)
|
|
1698
|
-
});
|
|
1699
|
-
upsertCollected(collected, failed);
|
|
1700
|
-
priorByIndex.set(stepIndex, failed);
|
|
1701
|
-
}
|
|
1702
|
-
}
|
|
1703
|
-
throw err;
|
|
1704
|
-
}
|
|
1705
|
-
},
|
|
1706
|
-
async emitEvent(key, payload) {
|
|
1707
|
-
const res = await input.store.emitEvent({ runId: input.runId, key, payload });
|
|
1708
|
-
return { accepted: res.accepted };
|
|
1709
|
-
},
|
|
1710
|
-
async now() {
|
|
1711
|
-
const v = await this.step(`deterministic:now`, async () => (/* @__PURE__ */ new Date()).toISOString(), {
|
|
1712
|
-
kind: "deterministic"
|
|
1713
|
-
});
|
|
1714
|
-
return new Date(v);
|
|
1715
|
-
},
|
|
1716
|
-
async uuid() {
|
|
1717
|
-
return this.step(`deterministic:uuid`, async () => cryptoRandomUuid(), {
|
|
1718
|
-
kind: "deterministic"
|
|
1719
|
-
});
|
|
1720
|
-
}
|
|
1721
|
-
};
|
|
1722
|
-
try {
|
|
1723
|
-
const result = await input.taskFn(ctx);
|
|
1724
|
-
const finalRecord = await input.store.endRun({
|
|
1725
|
-
runId: input.runId,
|
|
1726
|
-
workerId,
|
|
1727
|
-
status: "completed",
|
|
1728
|
-
outcome: input.defaultOutcome
|
|
1729
|
-
});
|
|
1730
|
-
return { result, record: finalRecord, steps: collected };
|
|
1731
|
-
} catch (err) {
|
|
1732
|
-
const finalRecord = await input.store.endRun({
|
|
1733
|
-
runId: input.runId,
|
|
1734
|
-
workerId,
|
|
1735
|
-
status: "failed",
|
|
1736
|
-
outcome: {
|
|
1737
|
-
...input.defaultOutcome,
|
|
1738
|
-
notes: err instanceof Error ? err.message : String(err)
|
|
602
|
+
controller.close();
|
|
1739
603
|
}
|
|
1740
|
-
});
|
|
1741
|
-
void finalRecord;
|
|
1742
|
-
throw err;
|
|
1743
|
-
} finally {
|
|
1744
|
-
clearInterval(heartbeat);
|
|
1745
|
-
}
|
|
1746
|
-
}
|
|
1747
|
-
function checkAbortAndLease(signal, leaseLost) {
|
|
1748
|
-
if (signal?.aborted) throw signal.reason ?? new Error("aborted");
|
|
1749
|
-
if (leaseLost) throw new Error("durable-runs: lease lost; another worker has taken over this run");
|
|
1750
|
-
}
|
|
1751
|
-
function upsertCollected(list, rec) {
|
|
1752
|
-
const i = list.findIndex((s) => s.stepIndex === rec.stepIndex);
|
|
1753
|
-
if (i === -1) list.push(rec);
|
|
1754
|
-
else list[i] = rec;
|
|
1755
|
-
}
|
|
1756
|
-
function toStepError(err) {
|
|
1757
|
-
if (err instanceof Error) {
|
|
1758
|
-
return {
|
|
1759
|
-
message: err.message,
|
|
1760
|
-
code: err.code,
|
|
1761
|
-
stack: err.stack
|
|
1762
|
-
};
|
|
1763
|
-
}
|
|
1764
|
-
return { message: String(err) };
|
|
1765
|
-
}
|
|
1766
|
-
function sleep2(ms, signal) {
|
|
1767
|
-
return new Promise((resolve, reject) => {
|
|
1768
|
-
if (signal?.aborted) {
|
|
1769
|
-
reject(signal.reason ?? new Error("aborted"));
|
|
1770
|
-
return;
|
|
1771
604
|
}
|
|
1772
|
-
const t = setTimeout(() => {
|
|
1773
|
-
signal?.removeEventListener("abort", onAbort);
|
|
1774
|
-
resolve();
|
|
1775
|
-
}, ms);
|
|
1776
|
-
const onAbort = () => {
|
|
1777
|
-
clearTimeout(t);
|
|
1778
|
-
reject(signal?.reason ?? new Error("aborted"));
|
|
1779
|
-
};
|
|
1780
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1781
605
|
});
|
|
1782
|
-
}
|
|
1783
|
-
function cryptoRandomUuid() {
|
|
1784
|
-
if (typeof globalThis.crypto?.randomUUID === "function") {
|
|
1785
|
-
return globalThis.crypto.randomUUID();
|
|
1786
|
-
}
|
|
1787
|
-
const bytes = new Uint8Array(16);
|
|
1788
|
-
for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
1789
|
-
bytes[6] = (bytes[6] ?? 0) & 15 | 64;
|
|
1790
|
-
bytes[8] = (bytes[8] ?? 0) & 63 | 128;
|
|
1791
|
-
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
1792
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
1793
|
-
}
|
|
1794
|
-
|
|
1795
|
-
// src/durable/schema.ts
|
|
1796
|
-
var DURABLE_SCHEMA_VERSION = 1;
|
|
1797
|
-
var DURABLE_SCHEMA_SQL = `-- Durable-run substrate \u2014 versioned schema for D1 / SQLite.
|
|
1798
|
-
--
|
|
1799
|
-
-- Apply once per database. Subsequent migrations append; never rewrite a
|
|
1800
|
-
-- prior version. See \`durable_schema_info\` for the migration trail.
|
|
1801
|
-
--
|
|
1802
|
-
-- Concurrency notes for D1:
|
|
1803
|
-
-- - SQLite supports UNIQUE constraints for first-emit-wins (\`durable_events\`
|
|
1804
|
-
-- PK is (run_id, key) \u2014 duplicate insert raises, caller treats as "already
|
|
1805
|
-
-- emitted").
|
|
1806
|
-
-- - Lease takeover happens via a conditional UPDATE: we only claim the lease
|
|
1807
|
-
-- if (lease_holder_id IS NULL OR lease_expires_at < :now) \u2014 atomic under
|
|
1808
|
-
-- SQLite's row-level locking.
|
|
1809
|
-
-- - All timestamps stored as ISO-8601 TEXT for cross-platform consistency.
|
|
1810
|
-
-- - \`result_json\` / \`error_json\` / \`outcome_json\` / \`payload_json\` are
|
|
1811
|
-
-- JSON-encoded TEXT; the application enforces canonical-JSON discipline at
|
|
1812
|
-
-- the boundary so the store stays type-agnostic.
|
|
1813
|
-
|
|
1814
|
-
CREATE TABLE IF NOT EXISTS durable_schema_info (
|
|
1815
|
-
version INTEGER PRIMARY KEY,
|
|
1816
|
-
applied_at TEXT NOT NULL
|
|
1817
|
-
);
|
|
1818
|
-
|
|
1819
|
-
CREATE TABLE IF NOT EXISTS durable_runs (
|
|
1820
|
-
run_id TEXT PRIMARY KEY,
|
|
1821
|
-
manifest_hash TEXT NOT NULL,
|
|
1822
|
-
project_id TEXT NOT NULL,
|
|
1823
|
-
scenario_id TEXT,
|
|
1824
|
-
status TEXT NOT NULL CHECK (status IN ('pending','running','completed','failed','suspended')),
|
|
1825
|
-
created_at TEXT NOT NULL,
|
|
1826
|
-
updated_at TEXT NOT NULL,
|
|
1827
|
-
completed_at TEXT,
|
|
1828
|
-
lease_holder_id TEXT,
|
|
1829
|
-
lease_expires_at TEXT,
|
|
1830
|
-
outcome_json TEXT,
|
|
1831
|
-
step_count INTEGER NOT NULL DEFAULT 0
|
|
1832
|
-
);
|
|
1833
|
-
|
|
1834
|
-
CREATE INDEX IF NOT EXISTS idx_durable_runs_project_status ON durable_runs(project_id, status);
|
|
1835
|
-
CREATE INDEX IF NOT EXISTS idx_durable_runs_lease_expires ON durable_runs(lease_expires_at);
|
|
1836
|
-
|
|
1837
|
-
CREATE TABLE IF NOT EXISTS durable_steps (
|
|
1838
|
-
run_id TEXT NOT NULL,
|
|
1839
|
-
step_index INTEGER NOT NULL,
|
|
1840
|
-
intent TEXT NOT NULL,
|
|
1841
|
-
kind TEXT NOT NULL,
|
|
1842
|
-
input_hash TEXT NOT NULL DEFAULT '',
|
|
1843
|
-
status TEXT NOT NULL CHECK (status IN ('pending','running','completed','failed')),
|
|
1844
|
-
attempts INTEGER NOT NULL DEFAULT 0,
|
|
1845
|
-
result_json TEXT,
|
|
1846
|
-
error_json TEXT,
|
|
1847
|
-
started_at TEXT,
|
|
1848
|
-
completed_at TEXT,
|
|
1849
|
-
PRIMARY KEY (run_id, step_index)
|
|
1850
|
-
);
|
|
1851
|
-
|
|
1852
|
-
CREATE INDEX IF NOT EXISTS idx_durable_steps_status ON durable_steps(run_id, status);
|
|
1853
|
-
|
|
1854
|
-
CREATE TABLE IF NOT EXISTS durable_events (
|
|
1855
|
-
run_id TEXT NOT NULL,
|
|
1856
|
-
key TEXT NOT NULL,
|
|
1857
|
-
payload_json TEXT,
|
|
1858
|
-
emitted_at TEXT NOT NULL,
|
|
1859
|
-
PRIMARY KEY (run_id, key)
|
|
1860
|
-
);
|
|
1861
|
-
|
|
1862
|
-
INSERT OR IGNORE INTO durable_schema_info (version, applied_at)
|
|
1863
|
-
VALUES (1, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'));
|
|
1864
|
-
`;
|
|
1865
|
-
|
|
1866
|
-
// src/durable/workflows.ts
|
|
1867
|
-
async function runOnWorkflowStep(workflowStep, input) {
|
|
1868
|
-
const stepCfg = input.stepConfig;
|
|
1869
|
-
let counter2 = 0;
|
|
1870
|
-
const stepName = (intent) => `${input.workflowName}/${counter2++}:${intent}`;
|
|
1871
|
-
const ctx = {
|
|
1872
|
-
runId: `cf-workflow:${input.workflowName}`,
|
|
1873
|
-
projectId: input.workflowName,
|
|
1874
|
-
async step(intent, fn) {
|
|
1875
|
-
const name = stepName(intent);
|
|
1876
|
-
if (stepCfg) {
|
|
1877
|
-
return workflowStep.do(name, stepCfg, fn);
|
|
1878
|
-
}
|
|
1879
|
-
return workflowStep.do(name, fn);
|
|
1880
|
-
},
|
|
1881
|
-
async awaitEvent(key, opts) {
|
|
1882
|
-
const timeout = opts?.timeoutMs ? `${Math.ceil(opts.timeoutMs / 1e3)}s` : void 0;
|
|
1883
|
-
const ev = await workflowStep.waitForEvent(stepName(`event:${key}`), {
|
|
1884
|
-
type: key,
|
|
1885
|
-
timeout
|
|
1886
|
-
});
|
|
1887
|
-
return ev.payload;
|
|
1888
|
-
},
|
|
1889
|
-
async emitEvent() {
|
|
1890
|
-
throw new Error(
|
|
1891
|
-
"runOnWorkflowStep: ctx.emitEvent is not available inside a Workflows entrypoint. Emit from the sibling worker that drives the workflow."
|
|
1892
|
-
);
|
|
1893
|
-
},
|
|
1894
|
-
async now() {
|
|
1895
|
-
const iso = await this.step("deterministic:now", async () => (/* @__PURE__ */ new Date()).toISOString());
|
|
1896
|
-
return new Date(iso);
|
|
1897
|
-
},
|
|
1898
|
-
async uuid() {
|
|
1899
|
-
return this.step("deterministic:uuid", async () => {
|
|
1900
|
-
if (typeof globalThis.crypto?.randomUUID === "function") {
|
|
1901
|
-
return globalThis.crypto.randomUUID();
|
|
1902
|
-
}
|
|
1903
|
-
const bytes = new Uint8Array(16);
|
|
1904
|
-
crypto.getRandomValues(bytes);
|
|
1905
|
-
bytes[6] = (bytes[6] ?? 0) & 15 | 64;
|
|
1906
|
-
bytes[8] = (bytes[8] ?? 0) & 63 | 128;
|
|
1907
|
-
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
1908
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
1909
|
-
});
|
|
1910
|
-
}
|
|
1911
|
-
};
|
|
1912
|
-
return input.taskFn(ctx);
|
|
606
|
+
return { body, contentType: "application/x-ndjson" };
|
|
1913
607
|
}
|
|
1914
608
|
|
|
1915
609
|
// src/intent-router.ts
|
|
@@ -1983,6 +677,85 @@ function classifyIntent(profile, message, opts = {}) {
|
|
|
1983
677
|
return { id: bestId, subagent: bestSubagent, score: bestScore, scores, evaluated };
|
|
1984
678
|
}
|
|
1985
679
|
|
|
680
|
+
// src/model-resolution.ts
|
|
681
|
+
var DEFAULT_ROUTER_BASE_URL = "https://router.tangle.tools";
|
|
682
|
+
function resolveRouterBaseUrl(env = {}) {
|
|
683
|
+
return (env.TANGLE_ROUTER_URL ?? env.TANGLE_ROUTER_BASE_URL ?? DEFAULT_ROUTER_BASE_URL).replace(/\/v1\/?$/, "").replace(/\/$/, "");
|
|
684
|
+
}
|
|
685
|
+
async function getModels(routerBaseUrl = DEFAULT_ROUTER_BASE_URL) {
|
|
686
|
+
const res = await fetch(`${routerBaseUrl}/v1/models`, {
|
|
687
|
+
headers: { Accept: "application/json" }
|
|
688
|
+
});
|
|
689
|
+
if (!res.ok) throw new Error(`router /v1/models ${res.status}`);
|
|
690
|
+
const body = await res.json();
|
|
691
|
+
return Array.isArray(body.data) ? body.data : [];
|
|
692
|
+
}
|
|
693
|
+
function withConfiguredModels(models, extraIds) {
|
|
694
|
+
const known = new Set(models.map((model) => model.id));
|
|
695
|
+
const extra = extraIds.map((id) => cleanModelId(id)).filter((id) => id !== void 0 && !known.has(id)).map(
|
|
696
|
+
(id) => ({
|
|
697
|
+
id,
|
|
698
|
+
name: id,
|
|
699
|
+
description: "Configured chat model for this environment.",
|
|
700
|
+
architecture: {
|
|
701
|
+
modality: "text->text",
|
|
702
|
+
input_modalities: ["text"],
|
|
703
|
+
output_modalities: ["text"]
|
|
704
|
+
}
|
|
705
|
+
})
|
|
706
|
+
);
|
|
707
|
+
return extra.length > 0 ? [...extra, ...models] : models;
|
|
708
|
+
}
|
|
709
|
+
function cleanModelId(value) {
|
|
710
|
+
if (typeof value !== "string") return void 0;
|
|
711
|
+
const trimmed = value.trim();
|
|
712
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
713
|
+
}
|
|
714
|
+
function resolveChatModel(candidates, fallback) {
|
|
715
|
+
for (const candidate of candidates) {
|
|
716
|
+
const model = cleanModelId(candidate.model);
|
|
717
|
+
if (model) return { source: candidate.source, model };
|
|
718
|
+
}
|
|
719
|
+
return fallback;
|
|
720
|
+
}
|
|
721
|
+
var WELL_FORMED_MODEL_ID = /^[A-Za-z0-9._/@:-]+$/;
|
|
722
|
+
function isWellFormedModelId(modelId) {
|
|
723
|
+
return modelId.length <= 200 && WELL_FORMED_MODEL_ID.test(modelId);
|
|
724
|
+
}
|
|
725
|
+
function catalogIdsForModel(model) {
|
|
726
|
+
const ids = /* @__PURE__ */ new Set();
|
|
727
|
+
const id = cleanModelId(model.id);
|
|
728
|
+
if (id) ids.add(id);
|
|
729
|
+
const provider = cleanModelId(model._provider) ?? cleanModelId(model.provider);
|
|
730
|
+
if (provider && id && !id.includes("/")) ids.add(`${provider}/${id}`);
|
|
731
|
+
return [...ids];
|
|
732
|
+
}
|
|
733
|
+
async function validateChatModelId(modelId, options = {}) {
|
|
734
|
+
const {
|
|
735
|
+
allowlist = [],
|
|
736
|
+
routerBaseUrl = DEFAULT_ROUTER_BASE_URL,
|
|
737
|
+
loadModels = getModels
|
|
738
|
+
} = options;
|
|
739
|
+
const cleaned = cleanModelId(modelId);
|
|
740
|
+
if (!cleaned) return { succeeded: false, error: "Model id must be a non-empty string." };
|
|
741
|
+
if (!isWellFormedModelId(cleaned)) {
|
|
742
|
+
return { succeeded: false, error: `Model id is malformed: ${cleaned}` };
|
|
743
|
+
}
|
|
744
|
+
if (allowlist.some((id) => cleanModelId(id) === cleaned)) {
|
|
745
|
+
return { succeeded: true, value: cleaned };
|
|
746
|
+
}
|
|
747
|
+
let catalog;
|
|
748
|
+
try {
|
|
749
|
+
catalog = await loadModels(routerBaseUrl);
|
|
750
|
+
} catch (err) {
|
|
751
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
752
|
+
return { succeeded: false, error: `Could not validate model catalog: ${message}` };
|
|
753
|
+
}
|
|
754
|
+
const ids = new Set(catalog.flatMap(catalogIdsForModel));
|
|
755
|
+
if (!ids.has(cleaned)) return { succeeded: false, error: `Model is not available: ${cleaned}` };
|
|
756
|
+
return { succeeded: true, value: cleaned };
|
|
757
|
+
}
|
|
758
|
+
|
|
1986
759
|
// src/profile-conformance.ts
|
|
1987
760
|
var DEFAULT_SHELL_CAPS = [
|
|
1988
761
|
"bash",
|
|
@@ -2998,8 +1771,8 @@ function createTraceBridge(options) {
|
|
|
2998
1771
|
if (!options.runId) {
|
|
2999
1772
|
throw new ValidationError("createTraceBridge: runId is required");
|
|
3000
1773
|
}
|
|
3001
|
-
let
|
|
3002
|
-
const newEventId = options.newEventId ?? (() => `evt-${++
|
|
1774
|
+
let counter = 0;
|
|
1775
|
+
const newEventId = options.newEventId ?? (() => `evt-${++counter}`);
|
|
3003
1776
|
const baseSpanId = options.spanId;
|
|
3004
1777
|
const toTraceEvent = (event) => {
|
|
3005
1778
|
const projection = projectToTraceEvent(event);
|
|
@@ -3186,17 +1959,7 @@ export {
|
|
|
3186
1959
|
CaptureIntegrityError,
|
|
3187
1960
|
ChatTurnError,
|
|
3188
1961
|
ConfigError,
|
|
3189
|
-
|
|
3190
|
-
DURABLE_SCHEMA_SQL,
|
|
3191
|
-
DURABLE_SCHEMA_VERSION,
|
|
3192
|
-
DurableAwaitEventTimeoutError,
|
|
3193
|
-
DurableChatTurnEngine,
|
|
3194
|
-
DurableRunDivergenceError,
|
|
3195
|
-
DurableRunError,
|
|
3196
|
-
DurableRunInputMismatchError,
|
|
3197
|
-
DurableRunLeaseHeldError,
|
|
3198
|
-
FileSystemDurableRunStore,
|
|
3199
|
-
InMemoryDurableRunStore,
|
|
1962
|
+
DEFAULT_ROUTER_BASE_URL,
|
|
3200
1963
|
InMemoryRuntimeSessionStore,
|
|
3201
1964
|
JudgeError,
|
|
3202
1965
|
NotFoundError,
|
|
@@ -3206,9 +1969,8 @@ export {
|
|
|
3206
1969
|
ValidationError,
|
|
3207
1970
|
VerificationError,
|
|
3208
1971
|
assertProfileConformance,
|
|
3209
|
-
canonicalHash,
|
|
3210
|
-
canonicalJson,
|
|
3211
1972
|
classifyIntent,
|
|
1973
|
+
cleanModelId,
|
|
3212
1974
|
composeTurnProfile,
|
|
3213
1975
|
createIterableBackend,
|
|
3214
1976
|
createOpenAICompatibleBackend,
|
|
@@ -3217,25 +1979,25 @@ export {
|
|
|
3217
1979
|
createSandboxPromptBackend,
|
|
3218
1980
|
createTraceBridge,
|
|
3219
1981
|
decideKnowledgeReadiness,
|
|
3220
|
-
|
|
3221
|
-
durableChatTurnEngine,
|
|
1982
|
+
deriveExecutionId,
|
|
3222
1983
|
encodeServerSentEvent,
|
|
3223
|
-
|
|
1984
|
+
getModels,
|
|
1985
|
+
handleChatTurn,
|
|
3224
1986
|
readinessServerSentEvent,
|
|
1987
|
+
resolveChatModel,
|
|
1988
|
+
resolveRouterBaseUrl,
|
|
3225
1989
|
runAgentTask,
|
|
3226
1990
|
runAgentTaskStream,
|
|
3227
1991
|
runChatTurn,
|
|
3228
|
-
runDurable,
|
|
3229
|
-
runDurableTurn,
|
|
3230
|
-
runOnWorkflowStep,
|
|
3231
1992
|
runtimeStreamServerSentEvent,
|
|
3232
1993
|
sandboxAsChatTurnTarget,
|
|
3233
1994
|
sanitizeAgentRuntimeEvent,
|
|
3234
1995
|
sanitizeKnowledgeReadinessReport,
|
|
3235
1996
|
sanitizeRuntimeStreamEvent,
|
|
3236
1997
|
startRuntimeRun,
|
|
3237
|
-
stepId,
|
|
3238
1998
|
summarizeAgentTaskRun,
|
|
3239
|
-
toAgentEvalTrace
|
|
1999
|
+
toAgentEvalTrace,
|
|
2000
|
+
validateChatModelId,
|
|
2001
|
+
withConfiguredModels
|
|
3240
2002
|
};
|
|
3241
2003
|
//# sourceMappingURL=index.js.map
|