@sema-agent/core 7.72.0 → 7.73.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/CHANGELOG.md +319 -0
- package/dist/agents/inherited-tool-pool.d.ts +26 -1
- package/dist/agents/inherited-tool-pool.js +46 -2
- package/dist/agents/parked-resume-drive.d.ts +10 -0
- package/dist/agents/parked-resume-drive.js +9 -0
- package/dist/agents/retain-ledger.d.ts +3 -0
- package/dist/agents/send-message-tool.js +1 -0
- package/dist/agents/send-user-message-tool.d.ts +12 -0
- package/dist/agents/send-user-message-tool.js +67 -0
- package/dist/agents/subagent-steps.d.ts +9 -0
- package/dist/agents/subagent-steps.js +38 -0
- package/dist/agents/subagent.d.ts +2 -0
- package/dist/agents/subagent.js +43 -12
- package/dist/agents/verify.js +23 -8
- package/dist/agents/yield-adoption.js +3 -1
- package/dist/brain/anthropic.js +14 -9
- package/dist/brain/open-responses.js +4 -5
- package/dist/brain/openai.js +30 -23
- package/dist/brain/stream-engine.js +159 -151
- package/dist/core/actions-digest.d.ts +43 -0
- package/dist/core/actions-digest.js +22 -0
- package/dist/core/allow-entry.js +3 -3
- package/dist/core/auto-mode.d.ts +7 -9
- package/dist/core/auto-mode.js +114 -8
- package/dist/core/checkpoint-features.d.ts +3 -2
- package/dist/core/checkpoint-features.js +1 -1
- package/dist/core/checkpoint-rekey.d.ts +1 -1
- package/dist/core/checkpoint-rekey.js +6 -1
- package/dist/core/checkpoint-requirements.d.ts +23 -3
- package/dist/core/checkpoint-requirements.js +5 -1
- package/dist/core/checkpoint-row-verdict.js +7 -1
- package/dist/core/checkpoint-store.d.ts +24 -2
- package/dist/core/checkpoint-store.js +1 -0
- package/dist/core/child-clamp-seats.d.ts +15 -0
- package/dist/core/child-clamp-seats.js +15 -0
- package/dist/core/delegation-frames.d.ts +1 -0
- package/dist/core/delegation-frames.js +1 -1
- package/dist/core/engine-capabilities.d.ts +5 -1
- package/dist/core/engine-capabilities.js +1 -1
- package/dist/core/engine-notice.d.ts +30 -22
- package/dist/core/engine-version.js +1 -1
- package/dist/core/entries-not-applied.d.ts +19 -7
- package/dist/core/entries-not-applied.js +33 -1
- package/dist/core/governance-codes.d.ts +7 -1
- package/dist/core/governance-codes.js +13 -5
- package/dist/core/hooks.d.ts +3 -0
- package/dist/core/hooks.js +1 -0
- package/dist/core/host-instance.d.ts +23 -0
- package/dist/core/host-instance.js +69 -0
- package/dist/core/instruction-preview.d.ts +63 -0
- package/dist/core/instruction-preview.js +406 -0
- package/dist/core/leg-budget.d.ts +26 -0
- package/dist/core/leg-budget.js +9 -0
- package/dist/core/leg-identity.d.ts +51 -1
- package/dist/core/leg-identity.js +45 -2
- package/dist/core/manager-launch-check.d.ts +48 -0
- package/dist/core/manager-launch-check.js +64 -0
- package/dist/core/mcp-call-meta.d.ts +2 -2
- package/dist/core/mcp-injection-drop.d.ts +20 -10
- package/dist/core/mcp-injection-drop.js +9 -3
- package/dist/core/mcp-server-spec.d.ts +2 -1
- package/dist/core/member-outcome.d.ts +3 -2
- package/dist/core/member-outcome.js +1 -1
- package/dist/core/memory-engine/deletion-record.d.ts +46 -0
- package/dist/core/memory-engine/deletion-record.js +96 -0
- package/dist/core/memory-engine/engine.js +24 -16
- package/dist/core/memory-engine/file-backend.js +7 -5
- package/dist/core/memory-engine/projection-ledger.d.ts +1 -1
- package/dist/core/memory-engine/projection-ledger.js +1 -1
- package/dist/core/memory-engine/scope-contract.d.ts +18 -13
- package/dist/core/memory-engine/scope-contract.js +18 -18
- package/dist/core/memory-engine/sync-client.js +26 -6
- package/dist/core/memory-engine/sync.d.ts +20 -2
- package/dist/core/memory-engine/sync.js +18 -17
- package/dist/core/mode-policy.d.ts +21 -6
- package/dist/core/mode-policy.js +15 -6
- package/dist/core/own-property-receipt.js +11 -4
- package/dist/core/parent-spec-seat.d.ts +13 -11
- package/dist/core/parent-spec-seat.js +13 -9
- package/dist/core/peer-seat.js +3 -3
- package/dist/core/peer-task-store.d.ts +365 -0
- package/dist/core/peer-task-store.js +682 -0
- package/dist/core/permission-rule-consent.js +6 -11
- package/dist/core/permission-rule-model.d.ts +27 -0
- package/dist/core/permission-rule-model.js +33 -5
- package/dist/core/permission-rule-provider.d.ts +35 -33
- package/dist/core/permission-rule-provider.js +38 -295
- package/dist/core/permission-rule-store.d.ts +181 -8
- package/dist/core/permission-rule-store.js +420 -17
- package/dist/core/permission-rule-sync.js +15 -1
- package/dist/core/project-seat.d.ts +55 -0
- package/dist/core/project-seat.js +96 -0
- package/dist/core/protocol-http-auth.d.ts +3 -2
- package/dist/core/protocol-http-auth.js +26 -18
- package/dist/core/remote-env.d.ts +4 -4
- package/dist/core/retired-keys.js +1 -0
- package/dist/core/rule-offer-read.d.ts +1 -1
- package/dist/core/rule-offer-read.js +32 -4
- package/dist/core/run-ledger-seat.d.ts +66 -0
- package/dist/core/run-ledger-seat.js +70 -0
- package/dist/core/run-ledger.d.ts +332 -0
- package/dist/core/run-ledger.js +550 -0
- package/dist/core/runner/checkpoint-scope.d.ts +13 -3
- package/dist/core/runner/checkpoint-scope.js +12 -4
- package/dist/core/runner/construction-doors.js +6 -0
- package/dist/core/runner/contracts.d.ts +21 -5
- package/dist/core/runner/delegation-owner.d.ts +30 -0
- package/dist/core/runner/delegation-owner.js +20 -4
- package/dist/core/runner/finish-reason-seat.js +4 -2
- package/dist/core/runner/leg-frame.js +3 -0
- package/dist/core/runner/nested-memory-lane.d.ts +22 -5
- package/dist/core/runner/nested-memory-lane.js +91 -30
- package/dist/core/runner/person-path-rows.d.ts +21 -16
- package/dist/core/runner/person-path-rows.js +80 -15
- package/dist/core/runner/prepare-caps-and-workflow.js +98 -88
- package/dist/core/runner/prepare-config-doors.d.ts +1 -0
- package/dist/core/runner/prepare-config-doors.js +5 -3
- package/dist/core/runner/prepare-context-lane.js +1 -0
- package/dist/core/runner/prepare-delegation-surface.d.ts +3 -0
- package/dist/core/runner/prepare-delegation-surface.js +6 -0
- package/dist/core/runner/prepare-execution-env.d.ts +3 -3
- package/dist/core/runner/prepare-hands-readface.d.ts +5 -2
- package/dist/core/runner/prepare-hands-readface.js +12 -1
- package/dist/core/runner/prepare-inherited-gate.d.ts +6 -3
- package/dist/core/runner/prepare-inherited-gate.js +13 -5
- package/dist/core/runner/prepare-run-refs.d.ts +1 -1
- package/dist/core/runner/prepare-run-refs.js +3 -2
- package/dist/core/runner/prepare-suspend-saga.d.ts +6 -2
- package/dist/core/runner/prepare-suspend-saga.js +4 -2
- package/dist/core/runner/prepare-task.js +36 -27
- package/dist/core/runner/prepare-turn-wiring.d.ts +4 -3
- package/dist/core/runner/prepare-turn-wiring.js +2 -1
- package/dist/core/runner/prepare-wiring-manifest.d.ts +3 -1
- package/dist/core/runner/prepare-wiring-manifest.js +5 -2
- package/dist/core/runner/resume-apply.js +11 -5
- package/dist/core/runner/resume-review-outcome.d.ts +3 -0
- package/dist/core/runner/resume-review-outcome.js +24 -1
- package/dist/core/runner/run-harness-handlers.js +3 -0
- package/dist/core/runner/run-identity-wiring.js +3 -2
- package/dist/core/runner/run-leg.js +3 -0
- package/dist/core/runner/run-notification-lane.js +20 -1
- package/dist/core/runner/run-settle-and-teardown.js +21 -12
- package/dist/core/runner/run-stop-and-final-verify.js +6 -0
- package/dist/core/runner/run-telemetry-and-budget-seats.js +1 -1
- package/dist/core/runner/runtask.d.ts +6 -2
- package/dist/core/runner/runtask.js +21 -9
- package/dist/core/runner/shell-env-withheld.d.ts +3 -2
- package/dist/core/runner/shell-env-withheld.js +1 -2
- package/dist/core/runner/stop-tail-notice.d.ts +10 -2
- package/dist/core/runner/stop-tail-notice.js +9 -2
- package/dist/core/runner/stream-settle-backstop.d.ts +1 -0
- package/dist/core/runner/stream-settle-backstop.js +1 -0
- package/dist/core/runner/task-start-doors.js +6 -3
- package/dist/core/runner/terminal-account.d.ts +6 -1
- package/dist/core/runner/terminal-account.js +16 -1
- package/dist/core/runner/unreadable-person-rows.d.ts +2 -1
- package/dist/core/runner-deps.d.ts +13 -10
- package/dist/core/secret-env.d.ts +53 -37
- package/dist/core/secret-env.js +44 -47
- package/dist/core/session-checkout.d.ts +3 -0
- package/dist/core/session-checkout.js +1 -0
- package/dist/core/session-store.d.ts +12 -6
- package/dist/core/session-store.js +26 -6
- package/dist/core/session.d.ts +31 -8
- package/dist/core/store-contracts/checkpoint-store-contract.js +10 -0
- package/dist/core/store-contracts/peer-task-store-contract.d.ts +21 -0
- package/dist/core/store-contracts/peer-task-store-contract.js +516 -0
- package/dist/core/store-contracts/session-store-contract.d.ts +15 -2
- package/dist/core/store-contracts/session-store-contract.js +87 -3
- package/dist/core/store-contracts/workflow-journal-store-contract.d.ts +19 -1
- package/dist/core/store-contracts/workflow-journal-store-contract.js +89 -1
- package/dist/core/task-event.d.ts +10 -3
- package/dist/core/task-notification.d.ts +96 -10
- package/dist/core/task-notification.js +129 -5
- package/dist/core/task-registry-shared.d.ts +12 -1
- package/dist/core/task-registry-shared.js +3 -1
- package/dist/core/task-registry-workflow.d.ts +1 -1
- package/dist/core/task-registry-workflow.js +9 -1
- package/dist/core/task-registry.d.ts +39 -2
- package/dist/core/task-registry.js +76 -12
- package/dist/core/task-result.d.ts +13 -0
- package/dist/core/task-spec-members.d.ts +3 -1
- package/dist/core/task-spec-members.js +11 -3
- package/dist/core/task-spec.d.ts +48 -22
- package/dist/core/tool-catalog-entries.js +2 -0
- package/dist/core/tool-mount-facts.d.ts +1 -1
- package/dist/core/tool-mount-facts.js +1 -0
- package/dist/core/tool-mount-plan.d.ts +3 -1
- package/dist/core/tool-mount-plan.js +6 -0
- package/dist/core/tool-mount-static.d.ts +1 -1
- package/dist/core/tool-mount-static.js +4 -2
- package/dist/core/tool-result-store.d.ts +19 -2
- package/dist/core/tool-result-store.js +51 -2
- package/dist/core/tool-spec.d.ts +9 -2
- package/dist/core/user-attachment.d.ts +5 -1
- package/dist/core/user-attachment.js +14 -7
- package/dist/core/wiring-manifest.d.ts +26 -20
- package/dist/core/wiring-manifest.js +7 -3
- package/dist/core/workflow-journal-store.d.ts +53 -10
- package/dist/core/workflow-journal-store.js +27 -4
- package/dist/core/workflow-run-store-contract.js +16 -0
- package/dist/core/workflow-run-store.d.ts +9 -7
- package/dist/core/workflow-run-store.js +4 -1
- package/dist/engine/execution-env/node-execution-env.js +3 -1
- package/dist/engine/harness/types.d.ts +24 -1
- package/dist/engine/harness/types.js +10 -0
- package/dist/engine/llm/types.d.ts +5 -7
- package/dist/engine/lsp/node-lsp-manager.js +2 -1
- package/dist/engine/session/import-validate.js +6 -1
- package/dist/engine/session/session.d.ts +15 -2
- package/dist/engine/session/session.js +34 -4
- package/dist/index.d.ts +18 -7
- package/dist/index.js +13 -4
- package/dist/orchestration/run-workflow-tool.js +40 -32
- package/dist/orchestration/workflow-park-adoption.d.ts +51 -12
- package/dist/orchestration/workflow-park-adoption.js +47 -12
- package/dist/orchestration/workflow-stop.d.ts +128 -0
- package/dist/orchestration/workflow-stop.js +156 -0
- package/dist/orchestration/workflow.d.ts +30 -6
- package/dist/orchestration/workflow.js +117 -55
- package/dist/prompts/manager-recipe.d.ts +18 -0
- package/dist/prompts/manager-recipe.js +24 -0
- package/dist/stores/file/permission-rule-store.d.ts +3 -1
- package/dist/stores/file/permission-rule-store.js +0 -1
- package/dist/stores/file/workflow-journal-store.d.ts +16 -3
- package/dist/stores/file/workflow-journal-store.js +93 -5
- package/dist/stores/file/workflow-run-store.d.ts +1 -1
- package/dist/tools/fs/file-write-check.js +9 -3
- package/dist/tools/fs/index.d.ts +8 -7
- package/dist/tools/fs/index.js +4 -4
- package/dist/tools/fs/read-deny-home.d.ts +1 -1
- package/dist/tools/fs/read-deny-home.js +5 -5
- package/dist/tools/fs/read-deny.d.ts +33 -8
- package/dist/tools/fs/read-deny.js +114 -25
- package/dist/tools/fs/safety.d.ts +4 -1
- package/dist/tools/fs/safety.js +4 -2
- package/dist/tools/fs/shell-read-check.d.ts +9 -2
- package/dist/tools/fs/shell-read-check.js +96 -14
- package/dist/tools/fs/shell-read-establish.d.ts +9 -0
- package/dist/tools/fs/shell-read-establish.js +21 -11
- package/dist/tools/fs/shell-read-judge.d.ts +13 -1
- package/dist/tools/fs/shell-read-judge.js +26 -7
- package/dist/tools/fs/shell-read-plan.d.ts +5 -0
- package/dist/tools/fs/shell-read-write-targets.js +17 -9
- package/package.json +2 -2
- package/test/export-surface.snapshot.json +131 -1
- package/wire-changes/answers/core-stores-sql@7.73.0.json +28 -0
- package/wire-changes/answers/core@7.73.0.json +216 -0
- package/wire-changes/answers/mods-kernel@7.73.0.json +7 -0
- package/wire-changes/answers/primitives@7.73.0.json +12 -0
- package/wire-changes/answers/wiki-gate@7.73.0.json +7 -0
- package/wire-changes/core-stores-sql@7.73.0.json +326 -0
- package/wire-changes/core@7.73.0.json +6480 -0
- package/wire-changes/mods-kernel@7.73.0.json +147 -0
- package/wire-changes/primitives@7.73.0.json +227 -0
- package/wire-changes/wiki-gate@7.73.0.json +145 -0
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { isMemberOutcome, MEMBER_OUTCOMES, memberCoverageOf, memberCountsOf, memberOutcomeText, sealMemberOutcome } from "./member-outcome.js";
|
|
3
|
+
import { isSubagentActions, subagentActionsLine } from "./actions-digest.js";
|
|
4
|
+
import { inlineUntrusted } from "./untrusted-text.js";
|
|
5
|
+
import { egressText } from "./untrusted-egress.js";
|
|
6
|
+
import { describeRejectedValue } from "./tool-search-setting.js";
|
|
7
|
+
import { strayMembersOf } from "./own-property-receipt.js";
|
|
8
|
+
export const RUN_LEDGER_VERSION = 1;
|
|
9
|
+
export const RUN_LEDGER_WIRE_KEY = "_sema_run_ledger";
|
|
10
|
+
export const RUN_LEDGER_SELF_REPORT_MAX_CHARS = 400;
|
|
11
|
+
export const RUN_LEDGER_CHECKS = Object.freeze(["verified", "evidence", "unverified", "n/a"]);
|
|
12
|
+
export function assertRunLedgerDeclaration(v) {
|
|
13
|
+
if (v === undefined || v === true)
|
|
14
|
+
return;
|
|
15
|
+
throw Object.assign(new Error(`TaskSpec.runLedger must be true or absent (got ${describeRejectedValue(v)}) — declare it to carry a member ledger; leave it out otherwise.`), { code: "config.invalid_argument" });
|
|
16
|
+
}
|
|
17
|
+
export const RUN_LEDGER_NOT_KEPT_SENTENCE = "This task's first leg kept no member ledger, so this leg keeps none either: whether a task keeps one is fixed when the task starts (declare runLedger on its first leg).";
|
|
18
|
+
export function runLedgerIdentityOf(spec, resumed, rowTaskId) {
|
|
19
|
+
if (resumed === undefined && spec.runLedger !== true)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (resumed !== undefined && resumed.section === undefined)
|
|
22
|
+
return spec.runLedger === true ? { notKept: RUN_LEDGER_NOT_KEPT_SENTENCE } : undefined;
|
|
23
|
+
const parked = resumed !== undefined ? readRunLedgerParked(resumed.section) : undefined;
|
|
24
|
+
if (resumed !== undefined && parked === undefined) {
|
|
25
|
+
throw Object.assign(new Error("the parked row's member ledger section does not read as a book this engine froze"), { code: "config.invalid_argument" });
|
|
26
|
+
}
|
|
27
|
+
if (resumed === undefined && rowTaskId !== undefined)
|
|
28
|
+
return { taskId: rowTaskId };
|
|
29
|
+
const named = spec.taskId;
|
|
30
|
+
if (named !== undefined && (typeof named !== "string" || named === "")) {
|
|
31
|
+
throw Object.assign(new Error(`TaskSpec.taskId must be a non-empty string on a run that keeps a member ledger (got ${describeRejectedValue(named)}) — name the task, or leave it out and the engine mints one.`), { code: "config.invalid_argument" });
|
|
32
|
+
}
|
|
33
|
+
if (parked !== undefined) {
|
|
34
|
+
if (named !== undefined && named !== parked.taskId) {
|
|
35
|
+
throw Object.assign(new Error(`this resume names task "${inlineUntrusted(named, 80)}", but the row it resumes is a leg of task "${inlineUntrusted(parked.taskId, 80)}" (the task its member ledger belongs to) — resume it under its own task id, or name none.`), { code: "config.invalid_argument" });
|
|
36
|
+
}
|
|
37
|
+
return { taskId: parked.taskId, parked };
|
|
38
|
+
}
|
|
39
|
+
return { taskId: named !== undefined && named !== spec.sessionId ? named : `rl-${randomBytes(8).toString("hex")}` };
|
|
40
|
+
}
|
|
41
|
+
export function runLedgerBookFor(spec, resumed, rowTaskId, now = Date.now) {
|
|
42
|
+
const id = runLedgerIdentityOf(spec, resumed, rowTaskId);
|
|
43
|
+
return id === undefined || "notKept" in id ? undefined : new RunLedgerBook(now, id.parked, id.taskId);
|
|
44
|
+
}
|
|
45
|
+
export function withRunLedgerTask(spec, book) {
|
|
46
|
+
if (book !== undefined)
|
|
47
|
+
return { ...spec, taskId: book.taskId, runLedger: true };
|
|
48
|
+
if (spec.runLedger === undefined)
|
|
49
|
+
return spec;
|
|
50
|
+
const { runLedger: _dropped, ...rest } = spec;
|
|
51
|
+
return rest;
|
|
52
|
+
}
|
|
53
|
+
const selfReportOf = (text) => {
|
|
54
|
+
if (text === undefined || text.trim() === "")
|
|
55
|
+
return undefined;
|
|
56
|
+
return inlineUntrusted(egressText("same-principal", text), RUN_LEDGER_SELF_REPORT_MAX_CHARS);
|
|
57
|
+
};
|
|
58
|
+
const labelOf = (text, fallback) => (text === undefined || text.trim() === "" ? fallback : inlineUntrusted(egressText("same-principal", text), 120));
|
|
59
|
+
const PARK_MAX_LISTED = 512;
|
|
60
|
+
const PARK_MAX_SYNC_ROWS = 64;
|
|
61
|
+
const PARK_MAX_ARRIVALS = 256;
|
|
62
|
+
const PARK_MAX_CALLS = 256;
|
|
63
|
+
const PARK_MAX_CLAIMS = 64;
|
|
64
|
+
const keepTail = (items, max) => items.length <= max ? { kept: [...items], dropped: 0 } : { kept: items.slice(items.length - max), dropped: items.length - max };
|
|
65
|
+
const isRec = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
|
|
66
|
+
const isNat = (x) => typeof x === "number" && Number.isSafeInteger(x) && x >= 0;
|
|
67
|
+
const isPos = (x) => typeof x === "number" && Number.isSafeInteger(x) && x >= 1;
|
|
68
|
+
const isOpt = (x, is) => x === undefined || is(x);
|
|
69
|
+
const isStr = (x) => typeof x === "string";
|
|
70
|
+
const isIn = (words) => (x) => typeof x === "string" && words.includes(x);
|
|
71
|
+
const isClaim = (c) => isRec(c) && isStr(c["about"]) && isStr(c["text"]) && isIn(["verified", "relayed", "inferred"])(c["basis"]) && isOpt(c["verdict"], isIn(["confirms", "refutes", "partial"])) && isOpt(c["evidence"], (e) => Array.isArray(e) && e.every(isStr));
|
|
72
|
+
const isSyncRow = (r) => isRec(r) && isStr(r["taskId"]) && isStr(r["kind"]) && isMemberOutcome(r["outcome"]) && isNat(r["settledAt"]) && isNat(r["order"]) &&
|
|
73
|
+
["label", "name", "selfReport", "call"].every((k) => isOpt(r[k], isStr)) && isOpt(r["began"], isNat) && isOpt(r["actions"], isSubagentActions);
|
|
74
|
+
const isArrivalMark = (a) => isRec(a) && isStr(a["taskId"]) && isPos(a["cycle"]) && isNat(a["order"]) && isNat(a["settledAt"]) && isOpt(a["began"], isNat);
|
|
75
|
+
const isHeardCall = (c) => isRec(c) && isStr(c["id"]) && isNat(c["at"]) && isNat(c["started"]) && isNat(c["turn"]) && isOpt(c["ended"], isNat) && isOpt(c["duplicate"], (d) => d === true);
|
|
76
|
+
const isReport = (r) => isRec(r) && isIn(["normal", "proactive"])(r["status"]) && isStr(r["message"]) && isOpt(r["kind"], isIn(["start", "interim", "final"])) && isOpt(r["claims"], (cs) => Array.isArray(cs) && cs.every(isClaim));
|
|
77
|
+
const TRUNCATED_KEYS = ["listed", "syncRows", "arrivals", "calls", "claims"];
|
|
78
|
+
const TRUNCATED_WORDS = { subject: "a parked member ledger's truncated section", unit: "member", moment: "when the row is read" };
|
|
79
|
+
const isTruncated = (t) => {
|
|
80
|
+
if (!isRec(t))
|
|
81
|
+
return false;
|
|
82
|
+
const keys = Object.keys(t);
|
|
83
|
+
return keys.length > 0 && strayMembersOf(t, TRUNCATED_WORDS, TRUNCATED_KEYS) === undefined && keys.every((k) => isPos(t[k]));
|
|
84
|
+
};
|
|
85
|
+
const isListOf = (x, is) => Array.isArray(x) && x.every(is);
|
|
86
|
+
const isReading = (p) => Array.isArray(p) && p.length === 2 && isStr(p[0]) && isNat(p[1]);
|
|
87
|
+
const isParked = (v) => isRec(v) && v["v"] === 1 && isStr(v["taskId"]) && v["taskId"] !== "" && isListOf(v["listed"], isStr) && isNat(v["tick"]) && isNat(v["turn"]) && isNat(v["seq"]) && isNat(v["reports"]) &&
|
|
88
|
+
isListOf(v["syncRows"], isSyncRow) && isListOf(v["arrivals"], isArrivalMark) && isListOf(v["calls"], isHeardCall) && isListOf(v["claims"], (c) => isClaim(c) && isRec(c) && isListOf(c["readings"], isReading)) &&
|
|
89
|
+
isOpt(v["lastReport"], isReport) && isOpt(v["truncated"], isTruncated);
|
|
90
|
+
export function readRunLedgerParked(v) {
|
|
91
|
+
return isParked(v) ? v : undefined;
|
|
92
|
+
}
|
|
93
|
+
export class RunLedgerBook {
|
|
94
|
+
taskId;
|
|
95
|
+
#listed = new Set();
|
|
96
|
+
#syncRows = new Map();
|
|
97
|
+
#arrivals = new Map();
|
|
98
|
+
#liveFrames = new Map();
|
|
99
|
+
#calls = new Map();
|
|
100
|
+
#truncated = { listed: 0, syncRows: 0, arrivals: 0, calls: 0, claims: 0 };
|
|
101
|
+
#tick = 0;
|
|
102
|
+
#turn = 0;
|
|
103
|
+
#isMember = () => undefined;
|
|
104
|
+
#claims = [];
|
|
105
|
+
#reports = 0;
|
|
106
|
+
#lastReport;
|
|
107
|
+
#launch;
|
|
108
|
+
#registry = () => [];
|
|
109
|
+
#registryBound = false;
|
|
110
|
+
#runId;
|
|
111
|
+
#seq = 0;
|
|
112
|
+
#now;
|
|
113
|
+
constructor(now = Date.now, parked, taskId) {
|
|
114
|
+
this.#now = now;
|
|
115
|
+
this.taskId = parked?.taskId ?? taskId;
|
|
116
|
+
if (parked === undefined)
|
|
117
|
+
return;
|
|
118
|
+
if (readRunLedgerParked(parked) === undefined)
|
|
119
|
+
throw Object.assign(new Error("the parked row's member ledger section does not read as a book this engine froze"), { code: "config.invalid_argument" });
|
|
120
|
+
if (taskId !== undefined && taskId !== parked.taskId)
|
|
121
|
+
throw Object.assign(new Error(`the parked row's member ledger is task "${parked.taskId}"'s, not "${taskId}"'s`), { code: "config.invalid_argument" });
|
|
122
|
+
for (const row of parked.syncRows)
|
|
123
|
+
this.#syncRows.set(row.taskId, structuredClone(row));
|
|
124
|
+
for (const mark of parked.arrivals)
|
|
125
|
+
this.#arrivals.set(mark.taskId, { ...mark });
|
|
126
|
+
for (const { id, ...call } of parked.calls)
|
|
127
|
+
this.#calls.set(id, { ...call });
|
|
128
|
+
for (const id of parked.listed)
|
|
129
|
+
this.#listed.add(id);
|
|
130
|
+
this.#claims.push(...parked.claims.map((c) => ({ ...c, readings: c.readings.map(([t, o]) => [t, o]), ...(c.evidence !== undefined ? { evidence: [...c.evidence] } : {}) })));
|
|
131
|
+
this.#tick = parked.tick;
|
|
132
|
+
this.#turn = parked.turn;
|
|
133
|
+
this.#seq = parked.seq;
|
|
134
|
+
this.#reports = parked.reports;
|
|
135
|
+
if (parked.lastReport !== undefined)
|
|
136
|
+
this.#lastReport = structuredClone(parked.lastReport);
|
|
137
|
+
if (parked.truncated !== undefined) {
|
|
138
|
+
this.#truncated.listed = parked.truncated.listed ?? 0;
|
|
139
|
+
this.#truncated.syncRows = parked.truncated.syncRows ?? 0;
|
|
140
|
+
this.#truncated.arrivals = parked.truncated.arrivals ?? 0;
|
|
141
|
+
this.#truncated.calls = parked.truncated.calls ?? 0;
|
|
142
|
+
this.#truncated.claims = parked.truncated.claims ?? 0;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
parkSection() {
|
|
146
|
+
if (this.taskId === undefined)
|
|
147
|
+
throw Object.assign(new Error("a member ledger book no leg minted cannot be parked: it belongs to no task"), { code: "internal.run_ledger_unbound" });
|
|
148
|
+
this.#listing();
|
|
149
|
+
const listedCut = keepTail([...this.#listed], PARK_MAX_LISTED);
|
|
150
|
+
const syncCut = keepTail([...this.#syncRows.values()].sort((a, b) => a.order - b.order), PARK_MAX_SYNC_ROWS);
|
|
151
|
+
const arrivalCut = keepTail([...this.#arrivals.values()].sort((a, b) => a.order - b.order), PARK_MAX_ARRIVALS);
|
|
152
|
+
const callCut = keepTail([...this.#calls.entries()].sort((a, b) => a[1].started - b[1].started), PARK_MAX_CALLS);
|
|
153
|
+
const claimCut = keepTail(this.#claims, PARK_MAX_CLAIMS);
|
|
154
|
+
const totals = {
|
|
155
|
+
listed: this.#truncated.listed + listedCut.dropped,
|
|
156
|
+
syncRows: this.#truncated.syncRows + syncCut.dropped,
|
|
157
|
+
arrivals: this.#truncated.arrivals + arrivalCut.dropped,
|
|
158
|
+
calls: this.#truncated.calls + callCut.dropped,
|
|
159
|
+
claims: this.#truncated.claims + claimCut.dropped,
|
|
160
|
+
};
|
|
161
|
+
const truncatedEntries = TRUNCATED_KEYS.flatMap((k) => (totals[k] > 0 ? [[k, totals[k]]] : []));
|
|
162
|
+
const truncated = truncatedEntries.length > 0 ? Object.fromEntries(truncatedEntries) : undefined;
|
|
163
|
+
return {
|
|
164
|
+
v: 1,
|
|
165
|
+
taskId: this.taskId,
|
|
166
|
+
listed: listedCut.kept,
|
|
167
|
+
tick: this.#tick,
|
|
168
|
+
turn: this.#turn,
|
|
169
|
+
seq: this.#seq + 1,
|
|
170
|
+
reports: this.#reports,
|
|
171
|
+
syncRows: syncCut.kept.map((row) => structuredClone(row)),
|
|
172
|
+
arrivals: arrivalCut.kept.map((mark) => ({ ...mark })),
|
|
173
|
+
calls: callCut.kept.map(([id, call]) => ({ id, ...call })),
|
|
174
|
+
claims: claimCut.kept.map((c) => structuredClone(c)),
|
|
175
|
+
...(this.#lastReport !== undefined ? { lastReport: structuredClone(this.#lastReport) } : {}),
|
|
176
|
+
...(truncated !== undefined ? { truncated } : {}),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
bind(runId, read, member) {
|
|
180
|
+
this.#runId = runId;
|
|
181
|
+
this.#registry = read;
|
|
182
|
+
this.#isMember = member ?? ((taskId) => read().find((r) => r.taskId === taskId));
|
|
183
|
+
this.#registryBound = true;
|
|
184
|
+
}
|
|
185
|
+
turnStarted() {
|
|
186
|
+
this.#turn = ++this.#tick;
|
|
187
|
+
}
|
|
188
|
+
setLaunch(launch) {
|
|
189
|
+
this.#launch = launch;
|
|
190
|
+
}
|
|
191
|
+
observeFrame(n, terminal) {
|
|
192
|
+
if (!terminal || (n.task_type !== "background_agent" && n.task_type !== "workflow"))
|
|
193
|
+
return;
|
|
194
|
+
const member = this.#registryBound ? this.#isMember(n.task_id) : undefined;
|
|
195
|
+
if (this.#registryBound && member === undefined)
|
|
196
|
+
return;
|
|
197
|
+
const prior = this.#arrivals.get(n.task_id);
|
|
198
|
+
if (prior !== undefined && !(n.seq !== undefined && n.seq > prior.cycle))
|
|
199
|
+
return;
|
|
200
|
+
const cycle = n.seq ?? 1;
|
|
201
|
+
const order = ++this.#tick;
|
|
202
|
+
const dispatch = this.#dispatchOf(member?.toolUseId, order);
|
|
203
|
+
this.#arrivals.set(n.task_id, {
|
|
204
|
+
taskId: n.task_id,
|
|
205
|
+
cycle,
|
|
206
|
+
order,
|
|
207
|
+
settledAt: this.#now(),
|
|
208
|
+
...(dispatch.began !== undefined ? { began: dispatch.began } : {}),
|
|
209
|
+
});
|
|
210
|
+
const selfReport = selfReportOf(n.result);
|
|
211
|
+
this.#liveFrames.set(n.task_id, {
|
|
212
|
+
kind: n.task_type,
|
|
213
|
+
...(n.spawnName !== undefined ? { name: n.spawnName } : {}),
|
|
214
|
+
...(selfReport !== undefined ? { selfReport } : {}),
|
|
215
|
+
...(n.actions !== undefined && isSubagentActions(n.actions) ? { actions: n.actions } : {}),
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
memberSettled(m) {
|
|
219
|
+
if (this.#syncRows.has(m.taskId))
|
|
220
|
+
return;
|
|
221
|
+
const selfReport = selfReportOf(m.selfReport);
|
|
222
|
+
const order = ++this.#tick;
|
|
223
|
+
this.#syncRows.set(m.taskId, {
|
|
224
|
+
taskId: m.taskId,
|
|
225
|
+
kind: m.kind,
|
|
226
|
+
label: labelOf(m.label, m.kind),
|
|
227
|
+
...(m.name !== undefined ? { name: m.name } : {}),
|
|
228
|
+
outcome: sealMemberOutcome(m.outcome),
|
|
229
|
+
...(selfReport !== undefined ? { selfReport } : {}),
|
|
230
|
+
...(m.actions !== undefined ? { actions: m.actions } : {}),
|
|
231
|
+
settledAt: this.#now(),
|
|
232
|
+
order,
|
|
233
|
+
...this.#dispatchOf(m.toolCallId, order),
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
#dispatchOf(callId, fallbackOrder) {
|
|
237
|
+
if (callId === undefined)
|
|
238
|
+
return this.#truncated.calls === 0 ? { began: fallbackOrder } : {};
|
|
239
|
+
const rec = this.#calls.get(callId);
|
|
240
|
+
const began = rec !== undefined && rec.duplicate !== true ? rec.started : undefined;
|
|
241
|
+
return { call: callId, ...(began !== undefined ? { began } : {}) };
|
|
242
|
+
}
|
|
243
|
+
callStarted(toolCallId) {
|
|
244
|
+
const dup = this.#calls.has(toolCallId);
|
|
245
|
+
this.#calls.set(toolCallId, { at: this.#now(), started: ++this.#tick, turn: this.#turn, ...(dup ? { duplicate: true } : {}) });
|
|
246
|
+
}
|
|
247
|
+
callResumed(toolCallId) {
|
|
248
|
+
const prior = this.#calls.get(toolCallId);
|
|
249
|
+
if (prior !== undefined && prior.ended === undefined && prior.duplicate !== true) {
|
|
250
|
+
prior.at = this.#now();
|
|
251
|
+
prior.started = ++this.#tick;
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
this.callStarted(toolCallId);
|
|
255
|
+
}
|
|
256
|
+
callEnded(toolCallId) {
|
|
257
|
+
const c = this.#calls.get(toolCallId);
|
|
258
|
+
if (c !== undefined && c.ended === undefined)
|
|
259
|
+
c.ended = ++this.#tick;
|
|
260
|
+
}
|
|
261
|
+
acceptReport(input) {
|
|
262
|
+
const claims = input.report?.claims ?? [];
|
|
263
|
+
const issued = input.toolCallId !== undefined ? (this.#calls.get(input.toolCallId)?.turn ?? this.#turn) : this.#tick + 1;
|
|
264
|
+
for (const [i, c] of claims.entries()) {
|
|
265
|
+
const n = i + 1;
|
|
266
|
+
const row = this.#rowNow(c.about);
|
|
267
|
+
if (row === undefined) {
|
|
268
|
+
if (this.#truncated.syncRows > 0 && this.#listed.has(c.about)) {
|
|
269
|
+
return { ok: false, why: `claim ${n} is about "${inlineUntrusted(c.about, 80)}", whose synchronous member row was truncated when the run parked (${this.#truncated.syncRows} earlier synchronous rows cut) — mark the claim "relayed" or "inferred".` };
|
|
270
|
+
}
|
|
271
|
+
return { ok: false, why: `claim ${n} is about "${inlineUntrusted(c.about, 80)}", which is not a member this run dispatched — name a row's task id, as the ledger lists it.` };
|
|
272
|
+
}
|
|
273
|
+
if (c.basis !== "verified")
|
|
274
|
+
continue;
|
|
275
|
+
const about = `${row.kind === "workflow" ? "workflow" : "agent"} ${row.name !== undefined ? `"${inlineUntrusted(row.name, 60)}"` : row.taskId}`;
|
|
276
|
+
if (row.reclaimed)
|
|
277
|
+
return { ok: false, why: `claim ${n} is marked "verified", but ${about} can no longer be read from its holder — mark the claim "relayed" or "inferred".` };
|
|
278
|
+
if (row.running) {
|
|
279
|
+
const cycleLabel = row.cycleExplicit ? ` (round ${row.cycle})` : "";
|
|
280
|
+
return { ok: false, why: `claim ${n} is marked "verified", but ${about} is still running${cycleLabel} and has not reported yet — wait until it finishes before verifying, or mark the claim "relayed" or "inferred".` };
|
|
281
|
+
}
|
|
282
|
+
if (row.arrival === undefined) {
|
|
283
|
+
if (row.cycleExplicit && (row.cycle > 1 || row.hasEarlierArrival)) {
|
|
284
|
+
return { ok: false, why: `claim ${n} is marked "verified", but ${about}'s result for round ${row.cycle} has not arrived yet — wait until you receive it before verifying, or mark the claim "relayed".` };
|
|
285
|
+
}
|
|
286
|
+
if (this.#truncated.arrivals > 0) {
|
|
287
|
+
return { ok: false, why: `claim ${n} is marked "verified", but ${about}'s arrival mark is not in this book (${this.#truncated.arrivals} earlier arrival ${this.#truncated.arrivals === 1 ? "mark was" : "marks were"} truncated when the run parked) — wait for a new result or mark the claim "relayed".` };
|
|
288
|
+
}
|
|
289
|
+
return { ok: false, why: `claim ${n} is marked "verified", but ${about} has not reported yet — its result has not arrived yet. Mark it "relayed" or "inferred", or report again once it has.` };
|
|
290
|
+
}
|
|
291
|
+
if (c.verdict === undefined)
|
|
292
|
+
return { ok: false, why: `claim ${n} is marked "verified" but states no verdict — say whether the check confirms, refutes or partly confirms what ${about} reported.` };
|
|
293
|
+
const evidence = c.evidence ?? [];
|
|
294
|
+
if (evidence.length === 0)
|
|
295
|
+
return { ok: false, why: `claim ${n} is marked "verified" but cites no evidence — cite the call id of the check you ran, or another member's task id, or mark the claim "relayed".` };
|
|
296
|
+
for (const e of evidence) {
|
|
297
|
+
if (e === c.about)
|
|
298
|
+
return { ok: false, why: `claim ${n} cites ${about} as evidence for itself — a member's own result cannot verify it. Cite your own check, or another member that checked it.` };
|
|
299
|
+
if (e === input.toolCallId)
|
|
300
|
+
return { ok: false, why: `claim ${n} cites this report call itself as evidence — cite the check you ran before it.` };
|
|
301
|
+
const call = this.#calls.get(e);
|
|
302
|
+
if (call?.duplicate === true) {
|
|
303
|
+
return { ok: false, why: `claim ${n} cites "${inlineUntrusted(e, 80)}", a call id that was used more than once in this run (non-unique) — its timing cannot be verified. Run a fresh check with a unique call id, or mark the claim "relayed".` };
|
|
304
|
+
}
|
|
305
|
+
const other = call === undefined ? this.#rowNow(e) : undefined;
|
|
306
|
+
if (other !== undefined && other.running) {
|
|
307
|
+
const cycleLabel = other.cycleExplicit ? ` (round ${other.cycle})` : "";
|
|
308
|
+
return { ok: false, why: `claim ${n} cites ${e}, a member that is still running${cycleLabel} (a member that has not reported yet) — a check that has no result cannot verify anything.` };
|
|
309
|
+
}
|
|
310
|
+
const at = call?.at ?? other?.arrival?.settledAt;
|
|
311
|
+
const began = call?.started ?? other?.arrival?.began;
|
|
312
|
+
const done = call !== undefined ? call.ended : other?.arrival?.order;
|
|
313
|
+
if (other !== undefined && other.arrival !== undefined && other.arrival.began === undefined) {
|
|
314
|
+
if (this.#truncated.calls > 0) {
|
|
315
|
+
return { ok: false, why: `claim ${n} cites ${e}, whose dispatch call record was truncated when the run parked (${this.#truncated.calls} earlier call ${this.#truncated.calls === 1 ? "record was" : "records were"} cut) — cannot verify when it started. Run a fresh check, or mark the claim "relayed".` };
|
|
316
|
+
}
|
|
317
|
+
return { ok: false, why: `claim ${n} cites ${e}, whose dispatch start was not recorded by this book — cannot verify when it started. Run a fresh check, or mark the claim "relayed".` };
|
|
318
|
+
}
|
|
319
|
+
if (at === undefined || began === undefined) {
|
|
320
|
+
if (other !== undefined) {
|
|
321
|
+
const cutNote = this.#truncated.arrivals > 0 ? ` (${this.#truncated.arrivals} earlier arrival marks were truncated when the run parked)` : other.cycleExplicit && other.cycle > 1 ? ` (round ${other.cycle} has not arrived)` : "";
|
|
322
|
+
return { ok: false, why: `claim ${n} cites ${e}, a member that has not reported yet${cutNote} — a check that has no result cannot verify anything.` };
|
|
323
|
+
}
|
|
324
|
+
if (this.#truncated.calls > 0) {
|
|
325
|
+
return { ok: false, why: `claim ${n} cites "${inlineUntrusted(e, 80)}", which is not in this book's call records (${this.#truncated.calls} earlier call ${this.#truncated.calls === 1 ? "record was" : "records were"} truncated when the run parked) — run a fresh check, or mark the claim "relayed".` };
|
|
326
|
+
}
|
|
327
|
+
return { ok: false, why: `claim ${n} cites "${inlineUntrusted(e, 80)}", which is neither a call you made in this run nor a member it dispatched — cite a call id from your own tool calls or a member's task id.` };
|
|
328
|
+
}
|
|
329
|
+
if (done === undefined || !(done < issued)) {
|
|
330
|
+
return { ok: false, why: `claim ${n} cites "${inlineUntrusted(e, 80)}", which had not finished when this report was sent — cite a check whose result you read before reporting.` };
|
|
331
|
+
}
|
|
332
|
+
if (!(began > row.arrival.order)) {
|
|
333
|
+
return { ok: false, why: `claim ${n} is marked "verified" but its evidence "${inlineUntrusted(e, 80)}" ran at ${isoTime(at)}, before ${about} reported at ${isoTime(row.arrival.settledAt)} — a check made before the claim cannot verify it. Check again, or mark the claim "relayed".` };
|
|
334
|
+
}
|
|
335
|
+
if (other?.reclaimed) {
|
|
336
|
+
return { ok: false, why: `claim ${n} cites ${e}, a member that can no longer be read from its holder — run a fresh check, or mark the claim "relayed".` };
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const readingsOf = (c) => [...new Set([c.about, ...(c.evidence ?? [])])].flatMap((id) => {
|
|
341
|
+
const view = this.#rowNow(id);
|
|
342
|
+
return view !== undefined && !view.running && view.arrival !== undefined ? [[id, view.cycle]] : [];
|
|
343
|
+
});
|
|
344
|
+
this.#claims.push(...claims.map((c) => ({ ...c, ...(c.evidence !== undefined ? { evidence: [...c.evidence] } : {}), readings: readingsOf(c) })));
|
|
345
|
+
if (input.report !== undefined)
|
|
346
|
+
this.#reports += 1;
|
|
347
|
+
this.#lastReport = {
|
|
348
|
+
status: input.status,
|
|
349
|
+
message: input.message,
|
|
350
|
+
...(input.report?.kind !== undefined ? { kind: input.report.kind } : {}),
|
|
351
|
+
...(claims.length > 0 ? { claims: claims.map((c) => ({ ...c })) } : {}),
|
|
352
|
+
};
|
|
353
|
+
return { ok: true };
|
|
354
|
+
}
|
|
355
|
+
#rowNow(taskId, regHint) {
|
|
356
|
+
const sync = this.#syncRows.get(taskId);
|
|
357
|
+
const mark = this.#arrivals.get(taskId);
|
|
358
|
+
const live = this.#liveFrames.get(taskId);
|
|
359
|
+
const reg = regHint ??
|
|
360
|
+
(this.#registryBound
|
|
361
|
+
? (this.#isMember(taskId) ?? (sync !== undefined ? { taskId, kind: sync.kind, running: false, outcome: sync.outcome, cycle: 1 } : undefined))
|
|
362
|
+
: sync !== undefined
|
|
363
|
+
? { taskId, kind: sync.kind, running: false, outcome: sync.outcome, cycle: 1 }
|
|
364
|
+
: mark !== undefined
|
|
365
|
+
? { taskId, kind: live?.kind ?? "background_agent", running: false, cycle: mark.cycle }
|
|
366
|
+
: undefined);
|
|
367
|
+
if (reg === undefined) {
|
|
368
|
+
if (mark === undefined)
|
|
369
|
+
return undefined;
|
|
370
|
+
return {
|
|
371
|
+
taskId,
|
|
372
|
+
kind: live?.kind ?? "background_agent",
|
|
373
|
+
...(live?.name !== undefined ? { name: live.name } : {}),
|
|
374
|
+
running: false,
|
|
375
|
+
cycle: mark.cycle,
|
|
376
|
+
cycleExplicit: false,
|
|
377
|
+
arrival: { order: mark.order, settledAt: mark.settledAt, ...(mark.began !== undefined ? { began: mark.began } : {}) },
|
|
378
|
+
hasEarlierArrival: false,
|
|
379
|
+
...(live?.selfReport !== undefined ? { selfReport: live.selfReport } : {}),
|
|
380
|
+
...(live?.actions !== undefined ? { actions: live.actions } : {}),
|
|
381
|
+
reclaimed: true,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
const isSync = sync !== undefined && reg.outcome === sync.outcome;
|
|
385
|
+
const isWorkflow = reg.kind === "workflow";
|
|
386
|
+
const cycleExplicit = reg.cycle !== undefined;
|
|
387
|
+
const cycle = isSync ? 1 : (reg.cycle ?? (isWorkflow ? 1 : (mark?.cycle ?? 1)));
|
|
388
|
+
const name = reg.name ?? (isSync ? sync.name : live?.name);
|
|
389
|
+
const label = isSync ? sync.label : reg.label;
|
|
390
|
+
const selfReport = isSync ? sync.selfReport : live?.selfReport;
|
|
391
|
+
const actions = isSync ? sync.actions : live?.actions;
|
|
392
|
+
if (reg.running) {
|
|
393
|
+
return {
|
|
394
|
+
taskId,
|
|
395
|
+
kind: reg.kind,
|
|
396
|
+
...(name !== undefined ? { name } : {}),
|
|
397
|
+
...(label !== undefined ? { label } : {}),
|
|
398
|
+
running: true,
|
|
399
|
+
cycle,
|
|
400
|
+
cycleExplicit,
|
|
401
|
+
hasEarlierArrival: mark !== undefined,
|
|
402
|
+
...(selfReport !== undefined ? { selfReport } : {}),
|
|
403
|
+
...(actions !== undefined ? { actions } : {}),
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
const arrival = isSync
|
|
407
|
+
? { order: sync.order, settledAt: sync.settledAt, ...(sync.began !== undefined ? { began: sync.began } : {}) }
|
|
408
|
+
: mark !== undefined && mark.cycle === cycle
|
|
409
|
+
? { order: mark.order, settledAt: mark.settledAt, ...(mark.began !== undefined ? { began: mark.began } : {}) }
|
|
410
|
+
: undefined;
|
|
411
|
+
return {
|
|
412
|
+
taskId,
|
|
413
|
+
kind: reg.kind,
|
|
414
|
+
...(name !== undefined ? { name } : {}),
|
|
415
|
+
...(label !== undefined ? { label } : {}),
|
|
416
|
+
running: false,
|
|
417
|
+
...(reg.outcome !== undefined ? { outcome: reg.outcome } : {}),
|
|
418
|
+
...(reg.members !== undefined ? { members: reg.members } : {}),
|
|
419
|
+
cycle,
|
|
420
|
+
cycleExplicit,
|
|
421
|
+
...(arrival !== undefined ? { arrival } : {}),
|
|
422
|
+
hasEarlierArrival: mark !== undefined,
|
|
423
|
+
...(selfReport !== undefined ? { selfReport } : {}),
|
|
424
|
+
...(actions !== undefined ? { actions } : {}),
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
#listing() {
|
|
428
|
+
const rows = this.#registry();
|
|
429
|
+
const held = new Set(rows.map((r) => r.taskId));
|
|
430
|
+
const own = [...this.#syncRows.values()].filter((s) => !held.has(s.taskId));
|
|
431
|
+
const now = [...rows.map((r) => r.taskId), ...own.map((s) => s.taskId)];
|
|
432
|
+
for (const id of now)
|
|
433
|
+
this.#listed.add(id);
|
|
434
|
+
const handed = new Set(now);
|
|
435
|
+
const unreadable = new Set([...this.#listed, ...this.#arrivals.keys(), ...this.#syncRows.keys()].filter((id) => !handed.has(id))).size;
|
|
436
|
+
return { rows: [...rows, ...own.map((s) => ({ taskId: s.taskId, kind: s.kind, running: false, outcome: s.outcome, cycle: 1 }))], unreadable };
|
|
437
|
+
}
|
|
438
|
+
snapshot(trigger) {
|
|
439
|
+
const listing = this.#listing();
|
|
440
|
+
const views = listing.rows.map((reg) => this.#rowNow(reg.taskId, reg));
|
|
441
|
+
const viewById = new Map(views.map((v) => [v.taskId, v]));
|
|
442
|
+
const covers = (c, taskId) => {
|
|
443
|
+
const view = viewById.get(taskId);
|
|
444
|
+
return view !== undefined && !view.running && c.readings.some(([id, cycle]) => id === taskId && cycle === view.cycle);
|
|
445
|
+
};
|
|
446
|
+
const verdictOf = new Map();
|
|
447
|
+
const cited = new Set();
|
|
448
|
+
for (const c of this.#claims) {
|
|
449
|
+
if (c.basis !== "verified")
|
|
450
|
+
continue;
|
|
451
|
+
if (covers(c, c.about))
|
|
452
|
+
verdictOf.set(c.about, c.verdict);
|
|
453
|
+
for (const e of c.evidence ?? [])
|
|
454
|
+
if (covers(c, e))
|
|
455
|
+
cited.add(e);
|
|
456
|
+
}
|
|
457
|
+
const verifiedAbout = new Set(verdictOf.keys());
|
|
458
|
+
const rows = views.map((view) => {
|
|
459
|
+
const outcome = view.running ? undefined : view.outcome;
|
|
460
|
+
const settled = outcome !== undefined;
|
|
461
|
+
const check = outcome?.outcome !== "completed" ? "n/a" : verifiedAbout.has(view.taskId) ? "verified" : cited.has(view.taskId) ? "evidence" : "unverified";
|
|
462
|
+
const verdict = check === "verified" ? verdictOf.get(view.taskId) : undefined;
|
|
463
|
+
return {
|
|
464
|
+
taskId: view.taskId,
|
|
465
|
+
kind: view.kind,
|
|
466
|
+
label: labelOf(view.label, view.kind),
|
|
467
|
+
...(view.name !== undefined ? { name: view.name } : {}),
|
|
468
|
+
state: settled ? "settled" : "running",
|
|
469
|
+
...(outcome !== undefined ? { outcome } : {}),
|
|
470
|
+
...(view.selfReport !== undefined ? { selfReport: view.selfReport } : {}),
|
|
471
|
+
...(view.actions !== undefined ? { actions: view.actions } : {}),
|
|
472
|
+
check,
|
|
473
|
+
...(verdict !== undefined ? { verdict } : {}),
|
|
474
|
+
...(view.members !== undefined ? { members: view.members } : {}),
|
|
475
|
+
...(view.arrival !== undefined && settled ? { settledAt: view.arrival.settledAt } : {}),
|
|
476
|
+
};
|
|
477
|
+
});
|
|
478
|
+
this.#seq += 1;
|
|
479
|
+
return runLedgerOf(rows, {
|
|
480
|
+
...(this.#runId !== undefined ? { runId: this.#runId } : {}),
|
|
481
|
+
seq: this.#seq,
|
|
482
|
+
trigger,
|
|
483
|
+
...(listing.unreadable > 0 ? { unreadable: listing.unreadable } : {}),
|
|
484
|
+
reports: this.#reports,
|
|
485
|
+
...(this.#lastReport !== undefined ? { lastReport: this.#lastReport } : {}),
|
|
486
|
+
...(this.#launch !== undefined ? { launch: this.#launch } : {}),
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
const isoTime = (ms) => new Date(ms).toISOString().slice(11, 23);
|
|
491
|
+
export function runLedgerOf(rows, at) {
|
|
492
|
+
const final = at.trigger.kind === "run_end";
|
|
493
|
+
const outcomes = rows.map((r) => r.outcome);
|
|
494
|
+
const unreadable = at.unreadable !== undefined && at.unreadable > 0 ? at.unreadable : undefined;
|
|
495
|
+
const rowCoverage = memberCoverageOf(outcomes) === "complete" && rows.some((r) => r.members !== undefined && r.members.coverage !== "complete") ? "partial" : memberCoverageOf(outcomes);
|
|
496
|
+
const coverage = unreadable !== undefined ? "incomplete" : rowCoverage;
|
|
497
|
+
const open = rows.filter((r) => r.state === "running").length;
|
|
498
|
+
const unverified = rows.filter((r) => r.check === "unverified").length;
|
|
499
|
+
const contradicted = rows.filter((r) => r.verdict !== undefined && r.verdict !== "confirms").length;
|
|
500
|
+
const base = { v: RUN_LEDGER_VERSION, ...(at.runId !== undefined ? { runId: at.runId } : {}), seq: at.seq, trigger: at.trigger, final, open, coverage, counts: memberCountsOf(outcomes), ...(unreadable !== undefined ? { unreadable } : {}), unverified, contradicted, reports: at.reports, rows };
|
|
501
|
+
const headline = runLedgerHeadline(base);
|
|
502
|
+
const ledger = { ...base, headline, ...(at.lastReport !== undefined ? { lastReport: at.lastReport } : {}), ...(at.launch !== undefined ? { launch: at.launch } : {}) };
|
|
503
|
+
return Object.freeze({ ...ledger, text: runLedgerText(ledger) });
|
|
504
|
+
}
|
|
505
|
+
export function runLedgerHeadline(l) {
|
|
506
|
+
const contradicted = l.contradicted > 0 ? ` · ${l.contradicted} contradicted by a check` : "";
|
|
507
|
+
const noReport = l.final && l.reports === 0 ? " · the manager submitted no structured conclusions" : "";
|
|
508
|
+
const unreadable = l.unreadable !== undefined && l.unreadable > 0 ? `${l.unreadable} ${l.unreadable === 1 ? "member" : "members"} no longer readable` : "";
|
|
509
|
+
if (l.rows.length === 0 && unreadable === "")
|
|
510
|
+
return `Settled · no members were dispatched${noReport}`;
|
|
511
|
+
const short = l.rows.filter((r) => r.members !== undefined && r.members.coverage !== "complete").length;
|
|
512
|
+
const fails = [...endingsOf(l.counts), unreadable, short > 0 ? `${short} ${short === 1 ? "workflow short of its members" : "workflows short of their members"}` : ""].filter((s) => s !== "").join(", ");
|
|
513
|
+
const completed = l.rows.some((r) => r.outcome?.outcome === "completed");
|
|
514
|
+
if (l.open > 0)
|
|
515
|
+
return `Not finished: ${l.open} ${l.open === 1 ? "row" : "rows"} still running${fails !== "" ? ` · ${fails}` : ""}${contradicted}${l.unverified > 0 ? ` · ${l.unverified} unverified` : ""}${noReport}`;
|
|
516
|
+
const coverage = `coverage ${l.coverage}${fails !== "" ? ` (${fails})` : ""}`;
|
|
517
|
+
const checks = l.unverified > 0 ? `${l.unverified} unverified` : !completed ? "none completed" : l.contradicted > 0 ? "none unverified" : "all verified";
|
|
518
|
+
return `Settled · ${coverage}${contradicted} · ${checks}${noReport}`;
|
|
519
|
+
}
|
|
520
|
+
const endingsOf = (counts) => MEMBER_OUTCOMES.filter((w) => w !== "completed" && (counts[w] ?? 0) > 0).map((w) => `${counts[w]} ${w}`);
|
|
521
|
+
const CHECK_WORDS = { verified: "verified", evidence: "used as evidence", unverified: "not verified", "n/a": "—" };
|
|
522
|
+
const VERDICT_WORDS = { confirms: "confirmed", refutes: "refuted", partial: "partly confirmed" };
|
|
523
|
+
export function runLedgerText(l) {
|
|
524
|
+
const lines = [l.headline];
|
|
525
|
+
const withResult = l.rows.filter((r) => r.state === "settled").length;
|
|
526
|
+
const endings = endingsOf(l.counts);
|
|
527
|
+
lines.push(`${l.rows.length} ${l.rows.length === 1 ? "member" : "members"} dispatched: ${withResult} with an ending${endings.length > 0 ? ` (${endings.join(", ")} of them not completed)` : ""}, ${l.open} still running; ${l.unverified} completed without a check, ${l.contradicted} contradicted by one.`);
|
|
528
|
+
if (l.unreadable !== undefined && l.unreadable > 0)
|
|
529
|
+
lines.push(`${l.unreadable} ${l.unreadable === 1 ? "member" : "members"} this task dispatched can no longer be read from ${l.unreadable === 1 ? "its holder" : "their holder"} (reclaimed, or held by another process): the coverage is not complete.`);
|
|
530
|
+
for (const r of l.rows) {
|
|
531
|
+
const who = `${r.label}${r.name !== undefined ? ` [${inlineUntrusted(r.name, 60)}]` : ""} (${r.kind} ${r.taskId})`;
|
|
532
|
+
const ending = r.outcome !== undefined ? memberOutcomeText(r.outcome) : "still running";
|
|
533
|
+
lines.push(`- ${who}: ${ending}${r.check !== "n/a" ? ` · ${CHECK_WORDS[r.check]}` : ""}${r.verdict !== undefined ? ` (${VERDICT_WORDS[r.verdict]})` : ""}`);
|
|
534
|
+
if (r.members !== undefined)
|
|
535
|
+
lines.push(` its members: coverage ${r.members.coverage}; ${Object.entries(r.members.counts).map(([w, c]) => `${c} ${w}`).join(", ")}`);
|
|
536
|
+
if (r.selfReport !== undefined)
|
|
537
|
+
lines.push(` said: ${r.selfReport}`);
|
|
538
|
+
if (r.actions !== undefined)
|
|
539
|
+
lines.push(` did: ${subagentActionsLine(r.actions)}`);
|
|
540
|
+
}
|
|
541
|
+
if (l.lastReport !== undefined) {
|
|
542
|
+
lines.push(`Last report (${l.lastReport.status}${l.lastReport.kind !== undefined ? `, ${l.lastReport.kind}` : ""}): ${inlineUntrusted(l.lastReport.message, 600)}`);
|
|
543
|
+
for (const c of l.lastReport.claims ?? [])
|
|
544
|
+
lines.push(` ${c.basis}${c.verdict !== undefined ? ` (${c.verdict})` : ""} · ${inlineUntrusted(c.about, 60)}: ${inlineUntrusted(c.text, 200)}`);
|
|
545
|
+
}
|
|
546
|
+
if (l.launch !== undefined && l.launch.verdict !== "ready") {
|
|
547
|
+
lines.push(`Launch check: ${l.launch.verdict} — ${l.launch.items.filter((i) => i.state !== "ok").map((i) => `${i.item} ${i.state}`).join("; ")}`);
|
|
548
|
+
}
|
|
549
|
+
return lines.join("\n");
|
|
550
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CompositionLeg, RunInternals } from "./contracts.js";
|
|
2
|
+
import type { CheckpointState } from "../checkpoint-store.js";
|
|
2
3
|
/**
|
|
3
4
|
* The SINGLE derivation of a durable-suspend checkpoint's scope — the LEG's scope, computed once per prepare and
|
|
4
5
|
* read by every mint lane of that leg (approval park, resource and `plan_review` parks, the forceDurableGate
|
|
@@ -86,6 +87,15 @@ export declare function compositionLegRecord(leg: CompositionLeg | undefined): C
|
|
|
86
87
|
export declare function workSessionOfLeg(leg: CompositionLeg | undefined): string | undefined;
|
|
87
88
|
/** Whether two composition-leg records name the same leg of the same composition for the same host session. */
|
|
88
89
|
export declare function sameCompositionLeg(a: CompositionLeg, b: CompositionLeg): boolean;
|
|
89
|
-
/** The
|
|
90
|
-
*
|
|
91
|
-
|
|
90
|
+
/** The identity a resumed leg runs as, read off the parked row it resumes: the registry row it ran as ({@link RunInternals.rowTaskId}),
|
|
91
|
+
* its spawning task ({@link RunInternals.parentTaskId}), its tree's root address — id, box scope and occupant (one anchor: taken
|
|
92
|
+
* whole or not at all) — and its explicit spawn name ({@link RunInternals.explicitAgentName}), each the live seat when the resuming call hands one,
|
|
93
|
+
* else the row's record (`CheckpointState.rowTaskId` / `legIdentity`) — folded into internals once, so the resume screen and every
|
|
94
|
+
* reader of the resumed leg (its prepare's mints, its stations, the throw backstop) read one value. A row that records none leaves
|
|
95
|
+
* the internals as handed. */
|
|
96
|
+
export declare function restoreRowIdentity(internals: RunInternals | undefined, state: Pick<CheckpointState, "rowTaskId" | "legIdentity"> | undefined): RunInternals | undefined;
|
|
97
|
+
/** The task id a resumed leg's host declared: the resume config's, else the one the parked row records its host declared
|
|
98
|
+
* (`CheckpointState.legIdentity.declaredTaskId`) — so a resume that names none runs as the task that parked, never as its session. */
|
|
99
|
+
export declare function resumedDeclaredTaskId(config: {
|
|
100
|
+
readonly taskId?: string | undefined;
|
|
101
|
+
}, state: Pick<CheckpointState, "legIdentity">): string | undefined;
|
|
@@ -49,8 +49,16 @@ export function workSessionOfLeg(leg) {
|
|
|
49
49
|
export function sameCompositionLeg(a, b) {
|
|
50
50
|
return a.entry === b.entry && a.leg === b.leg && a.hostSessionId === b.hostSessionId;
|
|
51
51
|
}
|
|
52
|
-
export function
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
export function restoreRowIdentity(internals, state) {
|
|
53
|
+
const row = state?.legIdentity;
|
|
54
|
+
const restored = {
|
|
55
|
+
...(internals?.rowTaskId === undefined && state?.rowTaskId !== undefined ? { rowTaskId: state.rowTaskId } : {}),
|
|
56
|
+
...(internals?.parentTaskId === undefined && row?.parentTaskId !== undefined ? { parentTaskId: row.parentTaskId } : {}),
|
|
57
|
+
...(internals?.rootSessionId === undefined && row?.rootSessionId !== undefined && row.rootSessionScope !== undefined ? { rootSessionId: row.rootSessionId, rootSessionScope: row.rootSessionScope, ...(row.rootSessionIncarnation !== undefined ? { rootSessionIncarnation: row.rootSessionIncarnation } : {}) } : {}),
|
|
58
|
+
...(internals?.explicitAgentName === undefined && row?.spawnName !== undefined ? { explicitAgentName: row.spawnName } : {}),
|
|
59
|
+
};
|
|
60
|
+
return Object.keys(restored).length === 0 ? internals : { ...(internals ?? {}), ...restored };
|
|
61
|
+
}
|
|
62
|
+
export function resumedDeclaredTaskId(config, state) {
|
|
63
|
+
return config.taskId ?? state.legIdentity?.declaredTaskId;
|
|
56
64
|
}
|
|
@@ -5,6 +5,7 @@ import { receiptRefusal, receiveOwnProperties, refuseProxy } from "../own-proper
|
|
|
5
5
|
import { assertNoRetiredRunnerDeps } from "./retired-runner-deps.js";
|
|
6
6
|
import { copyReadDenySeat, READ_DENY_DEPLOYMENT_SEATS } from "../read-face-seats.js";
|
|
7
7
|
import { receiveHostFloor } from "../host-floor.js";
|
|
8
|
+
import { receiveHostInstance } from "../host-instance.js";
|
|
8
9
|
import { holdOwnCredentialValues } from "../secret-env.js";
|
|
9
10
|
import { receivePeerSeat } from "../peer-seat.js";
|
|
10
11
|
import { receiveProtocolRevocations } from "../protocol-revocation.js";
|
|
@@ -12,6 +13,7 @@ import { receiveModelFallbackPolicy } from "../model-fallbacks.js";
|
|
|
12
13
|
import { receiveCacheRetention } from "./model-lane-seat.js";
|
|
13
14
|
import { assertJournalStoreGivesClaimsBack } from "../workflow-journal-store.js";
|
|
14
15
|
import { screenPermissionModeWhenUnset } from "../mode-policy.js";
|
|
16
|
+
import { receiveAutoModeFace } from "../auto-mode.js";
|
|
15
17
|
export function retainRunnerDeps(deps) {
|
|
16
18
|
const words = { subject: "RunnerDeps", unit: "seat", moment: "at new Runner" };
|
|
17
19
|
refuseProxy(deps, words);
|
|
@@ -26,6 +28,8 @@ export function retainRunnerDeps(deps) {
|
|
|
26
28
|
}
|
|
27
29
|
if (Object.hasOwn(retained, "hostFloor"))
|
|
28
30
|
Object.defineProperty(retained, "hostFloor", { value: receiveHostFloor(Reflect.get(retained, "hostFloor")), enumerable: true, writable: true, configurable: true });
|
|
31
|
+
if (Object.hasOwn(retained, "host"))
|
|
32
|
+
Object.defineProperty(retained, "host", { value: receiveHostInstance(Reflect.get(retained, "host"), "RunnerDeps.host"), enumerable: true, writable: true, configurable: true });
|
|
29
33
|
if (Object.hasOwn(retained, "peerSeat"))
|
|
30
34
|
Object.defineProperty(retained, "peerSeat", { value: receivePeerSeat(Reflect.get(retained, "peerSeat"), "RunnerDeps.peerSeat"), enumerable: true, writable: true, configurable: true });
|
|
31
35
|
if (Object.hasOwn(retained, "protocolRevocations"))
|
|
@@ -34,6 +38,8 @@ export function retainRunnerDeps(deps) {
|
|
|
34
38
|
Object.defineProperty(retained, "modelFallback", { value: receiveModelFallbackPolicy(Reflect.get(retained, "modelFallback"), "RunnerDeps.modelFallback"), enumerable: true, writable: true, configurable: true });
|
|
35
39
|
receiveCacheRetention(retained.cacheRetention, "RunnerDeps.cacheRetention");
|
|
36
40
|
screenPermissionModeWhenUnset(retained.permissionModeWhenUnset);
|
|
41
|
+
if (Object.hasOwn(retained, "autoMode"))
|
|
42
|
+
Object.defineProperty(retained, "autoMode", { value: receiveAutoModeFace(Reflect.get(retained, "autoMode"), "RunnerDeps.autoMode"), enumerable: true, writable: true, configurable: true });
|
|
37
43
|
const honorsReadChanges = retained.executionEnvFactoryHonorsReadChanges;
|
|
38
44
|
if (honorsReadChanges !== undefined && typeof honorsReadChanges !== "boolean") {
|
|
39
45
|
const e = new Error(`RunnerDeps.executionEnvFactoryHonorsReadChanges must be a boolean when given, got ${typeof honorsReadChanges} — refused at new Runner rather than read as false`);
|