@parcha/agentrun-dsl 0.1.0-beta.12 → 0.1.0-beta.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/dist/author.d.ts +9 -2
- package/dist/author.js +6 -3
- package/dist/decision-receipts.d.ts +98 -0
- package/dist/decision-receipts.js +51 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +2 -1
- package/dist/recovery/binding.d.ts +10 -0
- package/dist/recovery/binding.js +43 -0
- package/dist/recovery/call-key.d.ts +13 -0
- package/dist/recovery/call-key.js +24 -0
- package/dist/recovery/canonical-hash.d.ts +7 -0
- package/dist/recovery/canonical-hash.js +22 -0
- package/dist/recovery/driver.d.ts +186 -0
- package/dist/recovery/driver.js +777 -0
- package/dist/recovery/effect-failure.d.ts +34 -0
- package/dist/recovery/effect-failure.js +62 -0
- package/dist/recovery/errors.d.ts +31 -0
- package/dist/recovery/errors.js +51 -0
- package/dist/recovery/execution-path.d.ts +45 -0
- package/dist/recovery/execution-path.js +153 -0
- package/dist/recovery/file-store.d.ts +7 -0
- package/dist/recovery/file-store.js +131 -0
- package/dist/recovery/frozen-snapshot.d.ts +149 -0
- package/dist/recovery/frozen-snapshot.js +136 -0
- package/dist/recovery/handoff-block.d.ts +36 -0
- package/dist/recovery/handoff-block.js +148 -0
- package/dist/recovery/handoff-digest.d.ts +58 -0
- package/dist/recovery/handoff-digest.js +195 -0
- package/dist/recovery/handoff-files.d.ts +54 -0
- package/dist/recovery/handoff-files.js +162 -0
- package/dist/recovery/handoff-receipts.d.ts +42 -0
- package/dist/recovery/handoff-receipts.js +53 -0
- package/dist/recovery/handoff-redact.d.ts +12 -0
- package/dist/recovery/handoff-redact.js +44 -0
- package/dist/recovery/handoff.d.ts +44 -0
- package/dist/recovery/handoff.js +46 -0
- package/dist/recovery/index.d.ts +18 -0
- package/dist/recovery/index.js +16 -0
- package/dist/recovery/journal.d.ts +25 -0
- package/dist/recovery/journal.js +127 -0
- package/dist/recovery/memory-store.d.ts +4 -0
- package/dist/recovery/memory-store.js +20 -0
- package/dist/recovery/store.d.ts +81 -0
- package/dist/recovery/store.js +6 -0
- package/dist/recovery/testing/conformance.d.ts +4 -0
- package/dist/recovery/testing/conformance.js +213 -0
- package/dist/recovery/testing/crash-points.d.ts +18 -0
- package/dist/recovery/testing/crash-points.js +43 -0
- package/dist/recovery/testing/crash-sweep.d.ts +65 -0
- package/dist/recovery/testing/crash-sweep.js +146 -0
- package/dist/recovery/testing/index.d.ts +3 -0
- package/dist/recovery/testing/index.js +4 -0
- package/dist/recovery/testing/preload.d.ts +1 -0
- package/dist/recovery/testing/preload.js +3 -0
- package/dist/recovery/transport-facts.d.ts +23 -0
- package/dist/recovery/transport-facts.js +46 -0
- package/dist/recovery/with-recovery.d.ts +44 -0
- package/dist/recovery/with-recovery.js +84 -0
- package/dist/workflow.d.ts +28 -0
- package/dist/workflow.js +247 -87
- package/package.json +9 -1
package/dist/workflow.js
CHANGED
|
@@ -4,6 +4,7 @@ import { isDeepStrictEqual, types as utilTypes } from "node:util";
|
|
|
4
4
|
import { predicateMatches, getPath, MECHANICAL_PREDICATE_NAMES } from "./predicates.js";
|
|
5
5
|
import { WORKFLOW_NODE_KINDS, NODE_FIELDS, HOST_NODE_FIELDS, IGNORED_NODE_FIELDS, WORKFLOW_PREDICATES, THINKING_LEVELS, MODEL_TIERS, AGENT_CONTEXTS, CALL_TRANSPORTS, CALL_RETRY_CLASSES, EFFECT_FAILURE_CODES, PROSE_ARTIFACT_TYPES } from "./vocabulary.js";
|
|
6
6
|
import { validateAnswers, answerConfidence, answersSidecar, answersToValue, compileQuestions, SYSTEM_ONE_LIMITS } from "./system-one.js";
|
|
7
|
+
import { DECISION_RECEIPT_VERSION } from "./decision-receipts.js";
|
|
7
8
|
import { Compile } from "typebox/compile";
|
|
8
9
|
import { compileTransform, compileTransformSyntax } from "./code-exec.js";
|
|
9
10
|
import { workflowShapeErrors } from "./workflow-shape.js";
|
|
@@ -1350,17 +1351,18 @@ async function evaluatePredicate(pred, state, deps, at) {
|
|
|
1350
1351
|
if (pred.predicate !== "ask")
|
|
1351
1352
|
return { holds: predicateMatches(pred, state) };
|
|
1352
1353
|
const ask = pred;
|
|
1353
|
-
const runJudge = requireJudge(deps, `${at.kind} (ask predicate)`, at.label);
|
|
1354
1354
|
const asked = ask.state ? interpolateValue(ask.state, state, at.label) : promptStateOf(state);
|
|
1355
1355
|
const questions = { holds: { type: "noul", instructions: ask.instructions, ...(ask.criteria ? { criteria: ask.criteria } : {}) } };
|
|
1356
|
-
const result = await runJudge({ label: at.label, kind: "ask", state: asked, questions, signal: deps.signal });
|
|
1357
|
-
validateAnswers(questions, result.answers);
|
|
1358
|
-
const answer = result.answers.holds;
|
|
1359
|
-
const p = answer?.type === "noul" ? answer.noul : NaN;
|
|
1360
|
-
if (!Number.isFinite(p))
|
|
1361
|
-
throw new Error(`${at.kind} node "${at.label}": the ask predicate got no yes/no answer`);
|
|
1362
1356
|
const gte = ask.gte ?? 0.6;
|
|
1363
|
-
|
|
1357
|
+
const decided = await decide(deps, { nodeKind: `${at.kind} (ask predicate)`, kind: "ask", judgeKind: "ask", label: at.label, state: asked, questions }, (answers) => {
|
|
1358
|
+
const answer = answers.holds;
|
|
1359
|
+
const p = answer?.type === "noul" ? answer.noul : NaN;
|
|
1360
|
+
if (!Number.isFinite(p))
|
|
1361
|
+
throw new Error(`${at.kind} node "${at.label}": the ask predicate got no yes/no answer`);
|
|
1362
|
+
return { rule: { kind: "ask", gte }, action: { holds: p >= gte }, value: p };
|
|
1363
|
+
});
|
|
1364
|
+
const p = decided.value;
|
|
1365
|
+
deps.onEvent?.({ type: "ask.evaluated", label: at.label, detail: { kind: at.kind, p_yes: +p.toFixed(4), gte, holds: p >= gte, ...meteringOf(decided), decision_id: decided.id } });
|
|
1364
1366
|
return { holds: p >= gte, detail: { p_yes: p, gte } };
|
|
1365
1367
|
}
|
|
1366
1368
|
function promptStateOf(state) {
|
|
@@ -1810,6 +1812,124 @@ const requireJudge = (deps, kind, label) => {
|
|
|
1810
1812
|
return deps.runJudge(params);
|
|
1811
1813
|
};
|
|
1812
1814
|
};
|
|
1815
|
+
const sha256Hex = (text) => createHash("sha256").update(text).digest("hex");
|
|
1816
|
+
/** A text of any value, for a digest, in which two values that differ never read the same. JSON values are
|
|
1817
|
+
* spelled as JSON with sorted keys. Everything JSON cannot say is a bare word no JSON value produces: a
|
|
1818
|
+
* bigint as its digits and `n`, `undefined`, a non-finite number by name, a date, map or set under its
|
|
1819
|
+
* type's name, an object of another class under its constructor's name, and a reference back to an
|
|
1820
|
+
* enclosing object as `~` and how far out it is. So hashing never refuses a state a host is free to hand
|
|
1821
|
+
* its judge, and a state can share a decision id only with itself. */
|
|
1822
|
+
function digestText(value, enclosing = []) {
|
|
1823
|
+
switch (typeof value) {
|
|
1824
|
+
case "string":
|
|
1825
|
+
case "boolean": return JSON.stringify(value);
|
|
1826
|
+
case "number": return Number.isFinite(value) ? JSON.stringify(value) : String(value);
|
|
1827
|
+
case "bigint": return `${value}n`;
|
|
1828
|
+
case "undefined":
|
|
1829
|
+
case "function":
|
|
1830
|
+
case "symbol": return typeof value;
|
|
1831
|
+
}
|
|
1832
|
+
if (value === null)
|
|
1833
|
+
return "null";
|
|
1834
|
+
const object = value;
|
|
1835
|
+
const out = enclosing.lastIndexOf(object);
|
|
1836
|
+
if (out !== -1)
|
|
1837
|
+
return `~${enclosing.length - 1 - out}`;
|
|
1838
|
+
const inside = [...enclosing, object];
|
|
1839
|
+
const list = (entries) => `[${entries.map((entry) => digestText(entry, inside)).join(",")}]`;
|
|
1840
|
+
if (Array.isArray(object))
|
|
1841
|
+
return list(object);
|
|
1842
|
+
if (object instanceof Date)
|
|
1843
|
+
return `Date(${object.getTime()})`;
|
|
1844
|
+
if (object instanceof Map)
|
|
1845
|
+
return `Map${list([...object])}`;
|
|
1846
|
+
if (object instanceof Set)
|
|
1847
|
+
return `Set${list([...object])}`;
|
|
1848
|
+
if (ArrayBuffer.isView(object))
|
|
1849
|
+
return `${object.constructor.name}${list([...new Uint8Array(object.buffer, object.byteOffset, object.byteLength)])}`;
|
|
1850
|
+
const prototype = Object.getPrototypeOf(object);
|
|
1851
|
+
const name = prototype === null || prototype === Object.prototype ? "" : String(prototype.constructor?.name ?? "?");
|
|
1852
|
+
const record = object;
|
|
1853
|
+
return `${name}{${Object.keys(record).sort().map((key) => `${JSON.stringify(key)}:${digestText(record[key], inside)}`).join(",")}}`;
|
|
1854
|
+
}
|
|
1855
|
+
/** A persistence failure of a decision receipt. It fails the node: the answers were never applied. */
|
|
1856
|
+
export class WorkflowDecisionRecordError extends Error {
|
|
1857
|
+
label;
|
|
1858
|
+
decisionId;
|
|
1859
|
+
constructor(label, decisionId, cause) {
|
|
1860
|
+
super(`node "${label}": the decision receipt ${decisionId} could not be recorded: ${String(cause?.message || cause).slice(0, 300)}`, { cause });
|
|
1861
|
+
this.label = label;
|
|
1862
|
+
this.decisionId = decisionId;
|
|
1863
|
+
this.name = "WorkflowDecisionRecordError";
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
/** One typed-question request from identity to receipt. `settle` turns validated answers into the rule,
|
|
1867
|
+
* the action and whatever the caller applies; it throws exactly what the node would throw for answers it
|
|
1868
|
+
* refuses. In order: an `answered` receipt the host's recovery holds under the request's id answers it
|
|
1869
|
+
* with no judge call and no new receipt; otherwise the judge is asked, and the receipt (answered, failed
|
|
1870
|
+
* or cancelled) is recorded and awaited before this returns or throws. */
|
|
1871
|
+
async function decide(deps, ask, settle) {
|
|
1872
|
+
const runJudge = requireJudge(deps, ask.nodeKind, ask.label);
|
|
1873
|
+
const path = executionPath(deps);
|
|
1874
|
+
const item = deps[MAP_ITEM];
|
|
1875
|
+
const signal = ask.signal ?? deps.signal;
|
|
1876
|
+
const position = ask.request ?? { index: 0, count: 1 };
|
|
1877
|
+
const state_sha256 = sha256Hex(digestText(ask.state));
|
|
1878
|
+
const id = sha256Hex(canonicalJson(["decision-v1", path, ask.kind, position.index, ask.questions, state_sha256]));
|
|
1879
|
+
const recorded = deps.recovery?.decision ? await deps.recovery.decision(id) : undefined;
|
|
1880
|
+
if (recorded?.status === "answered") {
|
|
1881
|
+
validateAnswers(ask.questions, recorded.answers);
|
|
1882
|
+
const { value } = settle(recorded.answers);
|
|
1883
|
+
return { id, answers: recorded.answers, value, model: recorded.model ?? null, cost_usd: 0, replayed: true };
|
|
1884
|
+
}
|
|
1885
|
+
const request = { label: ask.label, kind: ask.judgeKind, ...(item ? { item } : {}), executionPath: path, signal, state: ask.state, questions: ask.questions, decisionId: id };
|
|
1886
|
+
const startedAt = Date.now();
|
|
1887
|
+
const receipt = (result, outcome) => ({
|
|
1888
|
+
v: DECISION_RECEIPT_VERSION, id, at: new Date().toISOString(), node: ask.label, kind: ask.kind, item: item?.index ?? null, execution_path: path, request: position,
|
|
1889
|
+
status: outcome.status, state_sha256, questions: ask.questions,
|
|
1890
|
+
answers: result?.answers && typeof result.answers === "object" && !Array.isArray(result.answers) ? result.answers : {},
|
|
1891
|
+
rule: outcome.rule, action: outcome.action,
|
|
1892
|
+
model: result?.model ?? null, usage: result?.usage ?? null, cost_usd: typeof result?.cost_usd === "number" ? result.cost_usd : null, request_sha256: result?.request_sha256 ?? null,
|
|
1893
|
+
latency_ms: Date.now() - startedAt,
|
|
1894
|
+
...(outcome.error !== undefined ? { error: String(outcome.error?.message || outcome.error).slice(0, 2000) } : {}),
|
|
1895
|
+
});
|
|
1896
|
+
/** Records `made`; with `failure`, the request's own failure, a recorder that also fails surfaces both. */
|
|
1897
|
+
const record = async (made, result, failure) => {
|
|
1898
|
+
if (!deps.recordDecision)
|
|
1899
|
+
return;
|
|
1900
|
+
try {
|
|
1901
|
+
await deps.recordDecision(made, { request, ...(result !== undefined ? { result } : {}) });
|
|
1902
|
+
}
|
|
1903
|
+
catch (cause) {
|
|
1904
|
+
const lost = new WorkflowDecisionRecordError(ask.label, id, cause);
|
|
1905
|
+
throw failure ? new AggregateError([failure.error, lost], `${String(failure.error?.message || failure.error)}; and ${lost.message}`) : lost;
|
|
1906
|
+
}
|
|
1907
|
+
};
|
|
1908
|
+
let result;
|
|
1909
|
+
try {
|
|
1910
|
+
result = await runJudge(request);
|
|
1911
|
+
}
|
|
1912
|
+
catch (error) {
|
|
1913
|
+
// The status is read before the caller reacts: a request that fails on its own is `failed`, even when
|
|
1914
|
+
// its failure is what cancels the others.
|
|
1915
|
+
const failed = receipt(undefined, { status: signal?.aborted ? "cancelled" : "failed", rule: null, action: null, error });
|
|
1916
|
+
ask.onFailure?.(error);
|
|
1917
|
+
await record(failed, undefined, { error });
|
|
1918
|
+
throw error;
|
|
1919
|
+
}
|
|
1920
|
+
let settled;
|
|
1921
|
+
try {
|
|
1922
|
+
validateAnswers(ask.questions, result.answers);
|
|
1923
|
+
settled = settle(result.answers);
|
|
1924
|
+
}
|
|
1925
|
+
catch (error) {
|
|
1926
|
+
ask.onFailure?.(error);
|
|
1927
|
+
await record(receipt(result, { status: "failed", rule: null, action: null, error }), result, { error });
|
|
1928
|
+
throw error;
|
|
1929
|
+
}
|
|
1930
|
+
await record(receipt(result, { status: "answered", rule: settled.rule, action: settled.action }), result);
|
|
1931
|
+
return { id, answers: result.answers, value: settled.value, model: result.model ?? null, cost_usd: result.cost_usd ?? null, replayed: false };
|
|
1932
|
+
}
|
|
1813
1933
|
function questionSetOf(workflow, node) {
|
|
1814
1934
|
const schema = resolveSchemaForWorkflow(workflow, workflow.schemas[node.out]);
|
|
1815
1935
|
const compiled = compileQuestions(schema, workflow.schemas);
|
|
@@ -1827,18 +1947,19 @@ function questionSetOf(workflow, node) {
|
|
|
1827
1947
|
};
|
|
1828
1948
|
}
|
|
1829
1949
|
const describeItem = (template, state, item, index) => interpolate(template, { ...state, item, item_index: index }).replace(/\s+/g, " ").trim();
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
validateAnswers(questions, result.answers);
|
|
1834
|
-
return { answers: result.answers, sidecar: answersSidecar(result.answers), model: result.model ?? null, cost_usd: result.cost_usd ?? null };
|
|
1835
|
-
}
|
|
1950
|
+
/** What a decision event says of the request behind it: who answered, what it cost, and that a recorded
|
|
1951
|
+
* decision answered it when one did. */
|
|
1952
|
+
const meteringOf = (decided) => ({ model: decided.model, cost_usd: decided.cost_usd, ...(decided.replayed ? { replayed: true } : {}) });
|
|
1836
1953
|
async function runJudgeNode(node, state, workflow, deps) {
|
|
1837
1954
|
assertNodeInputs(node, state);
|
|
1838
1955
|
const set = questionSetOf(workflow, node);
|
|
1839
|
-
const
|
|
1840
|
-
|
|
1841
|
-
|
|
1956
|
+
const decided = await decide(deps, { nodeKind: "judge", kind: "judge", judgeKind: "judge", label: node.label, state: interpolateValue(node.state, state, node.label), questions: set.questions }, (answers) => {
|
|
1957
|
+
const value = set.decode(answers);
|
|
1958
|
+
return { rule: { kind: "judge" }, action: value, value };
|
|
1959
|
+
});
|
|
1960
|
+
const value = decided.value;
|
|
1961
|
+
const sidecar = answersSidecar(decided.answers);
|
|
1962
|
+
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "judge", as: node.as, value, sidecar, ...meteringOf(decided), decision_id: decided.id } });
|
|
1842
1963
|
return { ...state, [node.as]: value, [`${node.as}$answers`]: sidecar };
|
|
1843
1964
|
}
|
|
1844
1965
|
async function runPickNode(node, state, deps) {
|
|
@@ -1861,16 +1982,20 @@ async function runPickNode(node, state, deps) {
|
|
|
1861
1982
|
const options = Object.fromEntries(items.map((item, i) => [`item_${i}`, describeItem(node.describe, state, item, i) || `item ${i}`]));
|
|
1862
1983
|
const criteria = node.allowNone ? { ...options, [NONE]: "none of the items fits" } : options;
|
|
1863
1984
|
const asked = { ...(node.state ? interpolateValue(node.state, state, node.label) : { context: promptStateOf(state) }), candidates: options };
|
|
1864
|
-
const
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1985
|
+
const decided = await decide(deps, { nodeKind: "pick", kind: "pick", judgeKind: "pick", label: node.label, state: asked, questions: { pick: { type: "choice", instructions: node.instructions, criteria } } }, (answers) => {
|
|
1986
|
+
const a = answers.pick;
|
|
1987
|
+
if (!a || a.type !== "choice")
|
|
1988
|
+
throw new Error(`pick node "${node.label}": no choice came back`);
|
|
1989
|
+
const none = a.choice === NONE;
|
|
1990
|
+
const index = none ? null : Object.keys(options).indexOf(a.choice);
|
|
1991
|
+
if (index !== null && index < 0)
|
|
1992
|
+
throw new Error(`pick node "${node.label}": the choice "${a.choice}" names no item`);
|
|
1993
|
+
const value = { index, item: index === null ? null : items[index], none, option: none ? null : options[a.choice] };
|
|
1994
|
+
return { rule: { kind: "pick", options: Object.keys(options), none: node.allowNone ? NONE : null }, action: { index, none }, value };
|
|
1995
|
+
});
|
|
1996
|
+
const value = decided.value;
|
|
1997
|
+
const sidecar = answersSidecar(decided.answers);
|
|
1998
|
+
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "pick", as: node.as, value, sidecar, ...meteringOf(decided), decision_id: decided.id } });
|
|
1874
1999
|
return { ...state, [node.as]: value, [`${node.as}$answers`]: sidecar };
|
|
1875
2000
|
}
|
|
1876
2001
|
async function runSiftNode(node, state, workflow, deps) {
|
|
@@ -1885,37 +2010,72 @@ async function runSiftNode(node, state, workflow, deps) {
|
|
|
1885
2010
|
const sidecars = [];
|
|
1886
2011
|
const kept = [];
|
|
1887
2012
|
let metering = null;
|
|
2013
|
+
const decisionIds = [];
|
|
1888
2014
|
if (items.length) {
|
|
1889
2015
|
const base = node.state ? interpolateValue(node.state, state, node.label) : {};
|
|
1890
2016
|
const named = items.map((item, i) => ({ id: `item_${i}`, ...(node.describe ? { summary: describeItem(node.describe, state, item, i) } : {}), item }));
|
|
1891
2017
|
const chunks = siftChunks(deps, node.label, ids.length, base, named);
|
|
1892
|
-
|
|
1893
|
-
//
|
|
1894
|
-
//
|
|
1895
|
-
|
|
1896
|
-
|
|
2018
|
+
const measure = keepTail === "confidence" ? "confidence" : "value";
|
|
2019
|
+
// The keep rule, as applied and as a receipt states it: the author's threshold, else 0.5 for a yes/no
|
|
2020
|
+
// value and 0 otherwise (an answer has its question's type).
|
|
2021
|
+
const keep = node.keep ? { id: keepId, measure, gte: node.keep.gte ?? (set.questions[keepId]?.type === "noul" && measure === "value" ? 0.5 : 0) } : null;
|
|
2022
|
+
// Each chunk is its own request and its own decision: item ids stay global (item_<i>), `items[j]`
|
|
2023
|
+
// indexes the chunk, and the chunk's receipt names its kept items by that position. A sift that fits
|
|
2024
|
+
// in one request sends exactly the request it always did. Several requests run at most
|
|
2025
|
+
// SIFT_REQUESTS_IN_FLIGHT at a time; the first failure stops dispatch and cancels the requests in
|
|
2026
|
+
// flight, each of which is recorded as cancelled, and is thrown once they settle. A request that had
|
|
2027
|
+
// already answered keeps its receipt; no event names it, since the sift decided nothing.
|
|
2028
|
+
const ask = (chunk, c, signal, onFailure) => {
|
|
1897
2029
|
const questions = {};
|
|
1898
2030
|
chunk.forEach((i, j) => { for (const id of ids)
|
|
1899
2031
|
questions[`${j}.${id}`] = { ...set.questions[id], instructions: `For \`items[${j}]\` (id item_${i}): ${set.questions[id].instructions}` }; });
|
|
1900
|
-
return
|
|
2032
|
+
return decide(deps, { nodeKind: "sift", kind: "sift", judgeKind: "sift", label: node.label, state: { ...base, items: chunk.map((i) => named[i]) }, questions, signal, request: { index: c, count: chunks.length, items: chunk }, ...(onFailure ? { onFailure } : {}) }, (answers) => {
|
|
2033
|
+
const decoded = [];
|
|
2034
|
+
const sides = [];
|
|
2035
|
+
const keptHere = [];
|
|
2036
|
+
chunk.forEach((i, j) => {
|
|
2037
|
+
const mine = Object.create(null);
|
|
2038
|
+
for (const id of ids) {
|
|
2039
|
+
const a = answers[`${j}.${id}`];
|
|
2040
|
+
if (!a)
|
|
2041
|
+
throw new Error(`sift node "${node.label}": no answer for item ${i} question "${id}"`);
|
|
2042
|
+
mine[id] = a;
|
|
2043
|
+
}
|
|
2044
|
+
decoded.push(set.decode(mine, ` answers for item ${i}`));
|
|
2045
|
+
sides.push(answersSidecar(mine));
|
|
2046
|
+
if (!keep) {
|
|
2047
|
+
keptHere.push(j);
|
|
2048
|
+
return;
|
|
2049
|
+
}
|
|
2050
|
+
const a = mine[keepId];
|
|
2051
|
+
const measured = keepTail === "confidence" ? answerConfidence(a) : a.type === "noul" ? a.noul : a.type === "score" ? a.score : NaN;
|
|
2052
|
+
if (Number.isFinite(measured) && measured >= keep.gte)
|
|
2053
|
+
keptHere.push(j);
|
|
2054
|
+
});
|
|
2055
|
+
return { rule: { kind: "sift", items: chunk.length, ids, keep }, action: { kept: keptHere }, value: { values: decoded, sidecars: sides, kept: keptHere.map((j) => chunk[j]) } };
|
|
2056
|
+
});
|
|
1901
2057
|
};
|
|
1902
2058
|
let results;
|
|
1903
2059
|
if (chunks.length === 1)
|
|
1904
|
-
results = [await ask(chunks[0], deps.signal)];
|
|
2060
|
+
results = [await ask(chunks[0], 0, deps.signal)];
|
|
1905
2061
|
else {
|
|
1906
2062
|
const stop = new AbortController();
|
|
1907
2063
|
const signal = deps.signal ? AbortSignal.any([deps.signal, stop.signal]) : stop.signal;
|
|
1908
2064
|
results = new Array(chunks.length);
|
|
1909
2065
|
let next = 0, failure;
|
|
2066
|
+
// The first request to fail is the sift's failure, from the moment it fails: dispatch stops and the
|
|
2067
|
+
// requests in flight are cancelled then, not when its receipt has been written. What it finally
|
|
2068
|
+
// throws (its own error, or that and a receipt that could not be recorded) replaces what it flagged.
|
|
2069
|
+
const fail = (request, error) => { if (!failure || failure.request === request)
|
|
2070
|
+
failure = { request, error }; stop.abort(); };
|
|
1910
2071
|
await Promise.all(Array.from({ length: Math.min(SIFT_REQUESTS_IN_FLIGHT, chunks.length) }, async () => {
|
|
1911
2072
|
while (!failure && next < chunks.length) {
|
|
1912
2073
|
const c = next++;
|
|
1913
2074
|
try {
|
|
1914
|
-
results[c] = await ask(chunks[c], signal);
|
|
2075
|
+
results[c] = await ask(chunks[c], c, signal, (error) => fail(c, error));
|
|
1915
2076
|
}
|
|
1916
2077
|
catch (error) {
|
|
1917
|
-
|
|
1918
|
-
stop.abort();
|
|
2078
|
+
fail(c, error);
|
|
1919
2079
|
}
|
|
1920
2080
|
}
|
|
1921
2081
|
}));
|
|
@@ -1927,33 +2087,18 @@ async function runSiftNode(node, state, workflow, deps) {
|
|
|
1927
2087
|
model: models.size === 1 ? results[0].model : null,
|
|
1928
2088
|
cost_usd: results.every((r) => r.cost_usd !== null) ? results.reduce((sum, r) => sum + r.cost_usd, 0) : null,
|
|
1929
2089
|
...(chunks.length > 1 ? { requests: chunks.length } : {}),
|
|
2090
|
+
...(results.every((r) => r.replayed) ? { replayed: true } : {}),
|
|
1930
2091
|
};
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
const a = answerOf(i, id);
|
|
1938
|
-
if (!a)
|
|
1939
|
-
throw new Error(`sift node "${node.label}": no answer for item ${i} question "${id}"`);
|
|
1940
|
-
mine[id] = a;
|
|
1941
|
-
}
|
|
1942
|
-
values.push(set.decode(mine, ` answers for item ${i}`));
|
|
1943
|
-
sidecars.push(answersSidecar(mine));
|
|
1944
|
-
if (!node.keep) {
|
|
1945
|
-
kept.push(i);
|
|
1946
|
-
continue;
|
|
1947
|
-
}
|
|
1948
|
-
const a = mine[keepId];
|
|
1949
|
-
const measure = keepTail === "confidence" ? answerConfidence(a) : a.type === "noul" ? a.noul : a.type === "score" ? a.score : NaN;
|
|
1950
|
-
const threshold = node.keep.gte ?? (a.type === "noul" && keepTail !== "confidence" ? 0.5 : 0);
|
|
1951
|
-
if (Number.isFinite(measure) && measure >= threshold)
|
|
1952
|
-
kept.push(i);
|
|
2092
|
+
// Chunks cover the list in order, so their results concatenate into list order.
|
|
2093
|
+
for (const r of results) {
|
|
2094
|
+
values.push(...r.value.values);
|
|
2095
|
+
sidecars.push(...r.value.sidecars);
|
|
2096
|
+
kept.push(...r.value.kept);
|
|
2097
|
+
decisionIds.push(r.id);
|
|
1953
2098
|
}
|
|
1954
2099
|
}
|
|
1955
2100
|
const value = { items: kept.map((i) => items[i]), values, answers: sidecars, kept };
|
|
1956
|
-
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "sift", as: node.as, value, count: items.length, kept: kept.length, ...(metering ?? {}) } });
|
|
2101
|
+
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "sift", as: node.as, value, count: items.length, kept: kept.length, ...(metering ?? {}), decision_ids: decisionIds } });
|
|
1957
2102
|
return { ...state, [node.as]: value };
|
|
1958
2103
|
}
|
|
1959
2104
|
async function runRouteNode(node, state, workflow, deps) {
|
|
@@ -1975,14 +2120,18 @@ async function runRouteNode(node, state, workflow, deps) {
|
|
|
1975
2120
|
}
|
|
1976
2121
|
const names = Object.keys(node.branches);
|
|
1977
2122
|
const criteria = Object.fromEntries(names.map((n) => [n, node.branches[n]?.criteria?.trim() || null]));
|
|
1978
|
-
const
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
2123
|
+
const decided = await decide(deps, { nodeKind: "route", kind: "route", judgeKind: "route", label: node.label, state: interpolateValue(node.state, state, node.label), questions: { branch: { type: "choice", instructions: node.instructions, criteria } } }, (answers) => {
|
|
2124
|
+
const a = answers.branch;
|
|
2125
|
+
if (!a || a.type !== "choice" || !names.includes(a.choice))
|
|
2126
|
+
throw new Error(`route node "${node.label}": the choice "${String(a?.choice)}" names no branch`);
|
|
2127
|
+
const unsure = Boolean(node.unsure && a.confidence < node.unsure.gte);
|
|
2128
|
+
const value = { branch: a.choice, taken: unsure ? node.unsure.branch : a.choice, unsure };
|
|
2129
|
+
return { rule: { kind: "route", unsure: node.unsure ? { gte: node.unsure.gte, branch: node.unsure.branch } : null }, action: value, value };
|
|
2130
|
+
});
|
|
2131
|
+
const value = decided.value;
|
|
2132
|
+
const taken = value.taken;
|
|
2133
|
+
const sidecar = answersSidecar(decided.answers);
|
|
2134
|
+
deps.onEvent?.({ type: "route.chosen", label: node.label, detail: { kind: "route", as: node.as ?? null, value, sidecar, ...meteringOf(decided), decision_id: decided.id } });
|
|
1986
2135
|
const routed = node.as ? { ...state, [node.as]: value, [`${node.as}$answers`]: sidecar } : state;
|
|
1987
2136
|
return runNodeOnState(node.branches[taken].body, routed, workflow, scopeExecution(deps, "branches", taken, "body"));
|
|
1988
2137
|
}
|
|
@@ -2056,6 +2205,7 @@ function scopeDepsToItem(deps, item, signal) {
|
|
|
2056
2205
|
pollStartedAt: (node, nestedItem, path) => recovery.pollStartedAt(node, nestedItem ?? item, path),
|
|
2057
2206
|
wait: (node, ms, nestedItem, path) => recovery.wait(node, ms, nestedItem ?? item, path),
|
|
2058
2207
|
...(recovery.fail ? { fail: (node, results, error, path) => recovery.fail(node, results, error, path) } : {}),
|
|
2208
|
+
...(recovery.decision ? { decision: id => recovery.decision(id) } : {}),
|
|
2059
2209
|
} } : {}),
|
|
2060
2210
|
};
|
|
2061
2211
|
}
|
|
@@ -2070,6 +2220,7 @@ function scopeDepsToChild(deps, invocation) {
|
|
|
2070
2220
|
...(deps.runNode ? { runNode: params => deps.runNode({ ...params, label: `${prefix}/${params.label}` }) } : {}),
|
|
2071
2221
|
...(deps.runEffect ? { runEffect: params => deps.runEffect({ ...params, node: relabel(params.node) }) } : {}),
|
|
2072
2222
|
...(deps.runJudge ? { runJudge: params => deps.runJudge({ ...params, label: `${prefix}/${params.label}` }) } : {}),
|
|
2223
|
+
...(deps.recordDecision ? { recordDecision: (receipt, exchange) => deps.recordDecision({ ...receipt, node: `${prefix}/${receipt.node}` }, { ...exchange, request: { ...exchange.request, label: `${prefix}/${exchange.request.label}` } }) } : {}),
|
|
2073
2224
|
...(deps.runCode ? { runCode: (node, state, ctx) => deps.runCode(relabel(node), state, { ...ctx, label: `${prefix}/${ctx.label}` }) } : {}),
|
|
2074
2225
|
...(recovery ? { recovery: {
|
|
2075
2226
|
supportsExecutionPaths: recovery.supportsExecutionPaths,
|
|
@@ -2078,6 +2229,7 @@ function scopeDepsToChild(deps, invocation) {
|
|
|
2078
2229
|
pollStartedAt: (node, item, path) => recovery.pollStartedAt(relabel(node), item, path),
|
|
2079
2230
|
wait: (node, ms, item, path) => recovery.wait(relabel(node), ms, item, path),
|
|
2080
2231
|
...(recovery.fail ? { fail: (node, results, error, path) => recovery.fail(relabel(node), results, error, path) } : {}),
|
|
2232
|
+
...(recovery.decision ? { decision: id => recovery.decision(id) } : {}),
|
|
2081
2233
|
} } : {}),
|
|
2082
2234
|
...(deps.onEvent ? { onEvent: forwardObserver(deps.onEvent, event => ({ ...event, label: `${prefix}/${event.label}` })) } : {}),
|
|
2083
2235
|
};
|
|
@@ -2541,7 +2693,8 @@ function compileVerifier(workflow, node, state, deps) {
|
|
|
2541
2693
|
const schema = workflow.schemas[clause.out].properties ?? {};
|
|
2542
2694
|
const maxDrives = clause.maxDrives ?? 2;
|
|
2543
2695
|
const floor = clause.override?.below ?? 0.3;
|
|
2544
|
-
|
|
2696
|
+
// A runner with no judge refuses the node here, before its generative call.
|
|
2697
|
+
requireJudge(deps, "judge", `${node.label} (verify)`);
|
|
2545
2698
|
const drives = [];
|
|
2546
2699
|
let reviewedCandidate;
|
|
2547
2700
|
let last = null;
|
|
@@ -2556,29 +2709,36 @@ function compileVerifier(workflow, node, state, deps) {
|
|
|
2556
2709
|
if (outSchema)
|
|
2557
2710
|
normalizeStringNullsForSchema(sub, outSchema);
|
|
2558
2711
|
const asked = interpolateValue({ ...(clause.state ?? {}), submission: "{submission}" }, { ...state, submission: sub, ...(node.as ? { [node.as]: sub } : {}) }, node.label);
|
|
2559
|
-
const result = await runJudge({ label: `${node.label} (verify)`, kind: "judge", state: asked, questions, signal: deps.signal });
|
|
2560
|
-
validateAnswers(questions, result.answers);
|
|
2561
|
-
const answers = result.answers;
|
|
2562
|
-
const doubted = [];
|
|
2563
|
-
const unmet = [];
|
|
2564
|
-
for (const [id, q] of Object.entries(questions)) {
|
|
2565
|
-
if (q.type !== "noul")
|
|
2566
|
-
continue;
|
|
2567
|
-
const p = pYes(answers[id]);
|
|
2568
|
-
if (Object.prototype.hasOwnProperty.call(sub, id)) {
|
|
2569
|
-
if (filled(sub[id]) && p < floor)
|
|
2570
|
-
doubted.push(id);
|
|
2571
|
-
}
|
|
2572
|
-
else if (p < 0.5)
|
|
2573
|
-
unmet.push(id);
|
|
2574
|
-
}
|
|
2575
2712
|
const drive = drives.length + 1;
|
|
2576
|
-
const
|
|
2713
|
+
const decided = await decide(deps, { nodeKind: "judge", kind: "verify", judgeKind: "judge", label: `${node.label} (verify)`, state: asked, questions, request: { index: drive - 1, count: maxDrives } }, (answers) => {
|
|
2714
|
+
const doubted = [];
|
|
2715
|
+
const unmet = [];
|
|
2716
|
+
const present = Object.keys(questions).filter((id) => Object.prototype.hasOwnProperty.call(sub, id));
|
|
2717
|
+
const thresholds = {};
|
|
2718
|
+
for (const [id, q] of Object.entries(questions)) {
|
|
2719
|
+
if (q.type !== "noul")
|
|
2720
|
+
continue;
|
|
2721
|
+
const p = pYes(answers[id]);
|
|
2722
|
+
if (Object.prototype.hasOwnProperty.call(sub, id)) {
|
|
2723
|
+
if (filled(sub[id]) && p < floor)
|
|
2724
|
+
doubted.push(id);
|
|
2725
|
+
}
|
|
2726
|
+
else if (p < 0.5)
|
|
2727
|
+
unmet.push(id);
|
|
2728
|
+
// A field the submission holds is judged at the floor, one it holds empty is not judged, and a
|
|
2729
|
+
// requirement that is not a field is judged at 0.5.
|
|
2730
|
+
thresholds[id] = !present.includes(id) ? 0.5 : filled(sub[id]) ? floor : null;
|
|
2731
|
+
}
|
|
2732
|
+
const accepted = doubted.length === 0 && unmet.length === 0;
|
|
2733
|
+
return { rule: { kind: "verify", thresholds, present }, action: { accepted, doubted, unmet }, value: { doubted, unmet, accepted } };
|
|
2734
|
+
});
|
|
2735
|
+
const answers = decided.answers;
|
|
2736
|
+
const { doubted, unmet, accepted } = decided.value;
|
|
2577
2737
|
reviewedCandidate = JSON.stringify(sub);
|
|
2578
2738
|
const sidecar = answersSidecar(answers);
|
|
2579
2739
|
drives.push({ drive, doubted, unmet, accepted, sidecar });
|
|
2580
2740
|
last = { answers, doubted, unmet };
|
|
2581
|
-
deps.onEvent?.({ type: "verify.answered", label: node.label, detail: { drive, doubted, unmet, accepted, sidecar,
|
|
2741
|
+
deps.onEvent?.({ type: "verify.answered", label: node.label, detail: { drive, doubted, unmet, accepted, sidecar, ...meteringOf(decided), decision_id: decided.id } });
|
|
2582
2742
|
if (accepted)
|
|
2583
2743
|
return { accepted: true };
|
|
2584
2744
|
if (drive >= maxDrives)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/agentrun-dsl",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.14",
|
|
4
4
|
"description": "Composable, typed workflows for agents",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -16,6 +16,14 @@
|
|
|
16
16
|
"types": "./dist/demo.d.ts",
|
|
17
17
|
"import": "./dist/demo.js"
|
|
18
18
|
},
|
|
19
|
+
"./recovery": {
|
|
20
|
+
"types": "./dist/recovery/index.d.ts",
|
|
21
|
+
"import": "./dist/recovery/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./recovery/testing": {
|
|
24
|
+
"types": "./dist/recovery/testing/index.d.ts",
|
|
25
|
+
"import": "./dist/recovery/testing/index.js"
|
|
26
|
+
},
|
|
19
27
|
"./schema": "./schema/workflow.schema.json"
|
|
20
28
|
},
|
|
21
29
|
"files": [
|