coremind-runtime 0.3.1 → 0.7.1
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 +49 -1
- package/dist/active-run-binding.d.ts +24 -0
- package/dist/active-run-binding.d.ts.map +1 -0
- package/dist/active-run-binding.js +22 -0
- package/dist/active-run-binding.js.map +1 -0
- package/dist/agent-driver.d.ts +139 -0
- package/dist/agent-driver.d.ts.map +1 -0
- package/dist/agent-driver.js +135 -0
- package/dist/agent-driver.js.map +1 -0
- package/dist/agent-factory.d.ts +10 -10
- package/dist/agent-factory.d.ts.map +1 -1
- package/dist/agent-factory.js +179 -14
- package/dist/agent-factory.js.map +1 -1
- package/dist/budget.d.ts +11 -1
- package/dist/budget.d.ts.map +1 -1
- package/dist/budget.js +59 -16
- package/dist/budget.js.map +1 -1
- package/dist/canonical-json.d.ts +3 -0
- package/dist/canonical-json.d.ts.map +1 -0
- package/dist/canonical-json.js +28 -0
- package/dist/canonical-json.js.map +1 -0
- package/dist/chat-session.d.ts +3 -0
- package/dist/chat-session.d.ts.map +1 -1
- package/dist/chat-session.js +5 -0
- package/dist/chat-session.js.map +1 -1
- package/dist/checkpoint.d.ts +25 -8
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +135 -52
- package/dist/checkpoint.js.map +1 -1
- package/dist/child-run.d.ts +344 -0
- package/dist/child-run.d.ts.map +1 -0
- package/dist/child-run.js +1373 -0
- package/dist/child-run.js.map +1 -0
- package/dist/child-runtime-adapter.d.ts +18 -0
- package/dist/child-runtime-adapter.d.ts.map +1 -0
- package/dist/child-runtime-adapter.js +309 -0
- package/dist/child-runtime-adapter.js.map +1 -0
- package/dist/compaction-projection.d.ts +5 -0
- package/dist/compaction-projection.d.ts.map +1 -1
- package/dist/compaction-projection.js +19 -1
- package/dist/compaction-projection.js.map +1 -1
- package/dist/context-lifecycle.d.ts +182 -0
- package/dist/context-lifecycle.d.ts.map +1 -0
- package/dist/context-lifecycle.js +455 -0
- package/dist/context-lifecycle.js.map +1 -0
- package/dist/context-task-state.d.ts +23 -0
- package/dist/context-task-state.d.ts.map +1 -0
- package/dist/context-task-state.js +183 -0
- package/dist/context-task-state.js.map +1 -0
- package/dist/control-inbox.d.ts +106 -0
- package/dist/control-inbox.d.ts.map +1 -0
- package/dist/control-inbox.js +291 -0
- package/dist/control-inbox.js.map +1 -0
- package/dist/delegated-context.d.ts +29 -0
- package/dist/delegated-context.d.ts.map +1 -0
- package/dist/delegated-context.js +76 -0
- package/dist/delegated-context.js.map +1 -0
- package/dist/delegation-tool.d.ts +30 -0
- package/dist/delegation-tool.d.ts.map +1 -0
- package/dist/delegation-tool.js +232 -0
- package/dist/delegation-tool.js.map +1 -0
- package/dist/effect-receipt-binding.d.ts +34 -0
- package/dist/effect-receipt-binding.d.ts.map +1 -0
- package/dist/effect-receipt-binding.js +125 -0
- package/dist/effect-receipt-binding.js.map +1 -0
- package/dist/errors.d.ts +11 -315
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +7 -129
- package/dist/errors.js.map +1 -1
- package/dist/evaluation.d.ts.map +1 -1
- package/dist/evaluation.js +12 -5
- package/dist/evaluation.js.map +1 -1
- package/dist/events.d.ts +91 -3
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +44 -0
- package/dist/events.js.map +1 -1
- package/dist/execution-error.d.ts +14 -0
- package/dist/execution-error.d.ts.map +1 -0
- package/dist/execution-error.js +129 -0
- package/dist/execution-error.js.map +1 -0
- package/dist/execution-security.d.ts +11 -0
- package/dist/execution-security.d.ts.map +1 -0
- package/dist/execution-security.js +45 -0
- package/dist/execution-security.js.map +1 -0
- package/dist/fact-ledger.d.ts +60 -0
- package/dist/fact-ledger.d.ts.map +1 -0
- package/dist/fact-ledger.js +167 -0
- package/dist/fact-ledger.js.map +1 -0
- package/dist/ids.d.ts +4 -0
- package/dist/ids.d.ts.map +1 -1
- package/dist/ids.js.map +1 -1
- package/dist/index.d.ts +15 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +12 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +12 -0
- package/dist/internal.js.map +1 -0
- package/dist/invariant-checker.d.ts.map +1 -1
- package/dist/invariant-checker.js +51 -10
- package/dist/invariant-checker.js.map +1 -1
- package/dist/loop-runner.d.ts.map +1 -1
- package/dist/loop-runner.js +14 -8
- package/dist/loop-runner.js.map +1 -1
- package/dist/observability.d.ts +211 -0
- package/dist/observability.d.ts.map +1 -0
- package/dist/observability.js +814 -0
- package/dist/observability.js.map +1 -0
- package/dist/orchestrator.d.ts +2 -2
- package/dist/orchestrator.d.ts.map +1 -1
- package/dist/orchestrator.js +12 -6
- package/dist/orchestrator.js.map +1 -1
- package/dist/project-check.d.ts +3 -0
- package/dist/project-check.d.ts.map +1 -1
- package/dist/project-check.js +21 -5
- package/dist/project-check.js.map +1 -1
- package/dist/projection.d.ts +131 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/projection.js +981 -0
- package/dist/projection.js.map +1 -0
- package/dist/provider.d.ts +23 -1
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +134 -23
- package/dist/provider.js.map +1 -1
- package/dist/public-tool.d.ts +3 -0
- package/dist/public-tool.d.ts.map +1 -1
- package/dist/public-tool.js +28 -3
- package/dist/public-tool.js.map +1 -1
- package/dist/replay-kit.d.ts +41 -0
- package/dist/replay-kit.d.ts.map +1 -0
- package/dist/replay-kit.js +96 -0
- package/dist/replay-kit.js.map +1 -0
- package/dist/result.d.ts +17 -1
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +16 -0
- package/dist/result.js.map +1 -1
- package/dist/retry-policy.d.ts.map +1 -1
- package/dist/retry-policy.js +22 -72
- package/dist/retry-policy.js.map +1 -1
- package/dist/run-context.d.ts +77 -0
- package/dist/run-context.d.ts.map +1 -0
- package/dist/run-context.js +159 -0
- package/dist/run-context.js.map +1 -0
- package/dist/run-effect-coordinator.d.ts.map +1 -1
- package/dist/run-effect-coordinator.js +3 -0
- package/dist/run-effect-coordinator.js.map +1 -1
- package/dist/run-kernel.d.ts +14 -0
- package/dist/run-kernel.d.ts.map +1 -0
- package/dist/run-kernel.js +34 -0
- package/dist/run-kernel.js.map +1 -0
- package/dist/run-state.d.ts +71 -5
- package/dist/run-state.d.ts.map +1 -1
- package/dist/run-state.js +601 -78
- package/dist/run-state.js.map +1 -1
- package/dist/run-terminalizer.d.ts.map +1 -1
- package/dist/run-terminalizer.js +4 -3
- package/dist/run-terminalizer.js.map +1 -1
- package/dist/runtime-instance-authority.d.ts +3 -0
- package/dist/runtime-instance-authority.d.ts.map +1 -0
- package/dist/runtime-instance-authority.js +8 -0
- package/dist/runtime-instance-authority.js.map +1 -0
- package/dist/runtime.d.ts +93 -2
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +2136 -347
- package/dist/runtime.js.map +1 -1
- package/dist/session.d.ts +4 -3
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js.map +1 -1
- package/dist/tool-call-lifecycle.d.ts +89 -0
- package/dist/tool-call-lifecycle.d.ts.map +1 -0
- package/dist/tool-call-lifecycle.js +379 -0
- package/dist/tool-call-lifecycle.js.map +1 -0
- package/dist/tool-capability-identity.d.ts +3 -0
- package/dist/tool-capability-identity.d.ts.map +1 -0
- package/dist/tool-capability-identity.js +5 -0
- package/dist/tool-capability-identity.js.map +1 -0
- package/dist/tool-capability-projection.d.ts +17 -0
- package/dist/tool-capability-projection.d.ts.map +1 -0
- package/dist/tool-capability-projection.js +99 -0
- package/dist/tool-capability-projection.js.map +1 -0
- package/dist/tool-effect-selectors.d.ts +3 -0
- package/dist/tool-effect-selectors.d.ts.map +1 -0
- package/dist/tool-effect-selectors.js +18 -0
- package/dist/tool-effect-selectors.js.map +1 -0
- package/dist/tool-policy.d.ts +13 -2
- package/dist/tool-policy.d.ts.map +1 -1
- package/dist/tool-policy.js +149 -44
- package/dist/tool-policy.js.map +1 -1
- package/dist/trace.d.ts +1 -0
- package/dist/trace.d.ts.map +1 -1
- package/dist/trace.js +10 -3
- package/dist/trace.js.map +1 -1
- package/dist/turn-tracker.d.ts.map +1 -1
- package/dist/turn-tracker.js +1 -0
- package/dist/turn-tracker.js.map +1 -1
- package/dist/workspace-lease.d.ts +81 -0
- package/dist/workspace-lease.d.ts.map +1 -0
- package/dist/workspace-lease.js +300 -0
- package/dist/workspace-lease.js.map +1 -0
- package/package.json +11 -5
|
@@ -0,0 +1,981 @@
|
|
|
1
|
+
import { isResolvedToolCapability, RECOVERY_DISPOSITIONS, recoveryDispositionFor, } from "coremind-tools";
|
|
2
|
+
import { canonicalJson } from "./canonical-json.js";
|
|
3
|
+
import { childRunRecoveryAssessment, childRunResultFingerprint, childRunResultRequiresDisposition, decodeChildRunFact, delegationDispositionViolation, foldChildRunLifecycleStatus, isChildRunRecoverySafeForRedelegation, } from "./child-run.js";
|
|
4
|
+
import { projectPendingControlFacts } from "./control-inbox.js";
|
|
5
|
+
import { CoreMindError } from "./errors.js";
|
|
6
|
+
import { LIFECYCLE_EVENTS } from "./lifecycle-extension.js";
|
|
7
|
+
import { projectLocalObservability, validateTelemetryConsentFact, } from "./observability.js";
|
|
8
|
+
import { isRunStateResumable, operationSnapshotFromRecords, prepareRunResume, } from "./run-state.js";
|
|
9
|
+
import { createRunSnapshot } from "./snapshot.js";
|
|
10
|
+
import { validateToolCallLifecycleFact } from "./tool-call-lifecycle.js";
|
|
11
|
+
import { projectWorkspaceLeasesFromRecords, } from "./workspace-lease.js";
|
|
12
|
+
/** 从 append-only Run Facts 生成可删除、可重建的唯一运行投影。 */
|
|
13
|
+
export const ProjectionEngine = {
|
|
14
|
+
project(records) {
|
|
15
|
+
if (records.length === 0) {
|
|
16
|
+
throw new CoreMindError("unknown_run", "没有可投影的 Run Facts");
|
|
17
|
+
}
|
|
18
|
+
const ordered = [...records].sort((left, right) => left.sequence - right.sequence);
|
|
19
|
+
const runId = ordered[0].runId;
|
|
20
|
+
for (const [index, record] of ordered.entries()) {
|
|
21
|
+
if (record.runId !== runId || record.sequence !== index + 1) {
|
|
22
|
+
throw new CoreMindError("run_state_corrupt", "Run Facts 身份或 sequence 不连续");
|
|
23
|
+
}
|
|
24
|
+
if (record.kind === "telemetry_consent")
|
|
25
|
+
validateTelemetryConsentFact(record.payload);
|
|
26
|
+
}
|
|
27
|
+
const trace = ordered.flatMap((record) => {
|
|
28
|
+
if (record.kind !== "event")
|
|
29
|
+
return [];
|
|
30
|
+
if (!isTraceEvent(record.payload, runId)) {
|
|
31
|
+
throw new CoreMindError("run_state_corrupt", "Run Fact 包含损坏的 event payload");
|
|
32
|
+
}
|
|
33
|
+
return [publicTraceEvent(record.payload)];
|
|
34
|
+
});
|
|
35
|
+
const lastResumeSequence = [...ordered]
|
|
36
|
+
.reverse()
|
|
37
|
+
.find((record) => record.kind === "resume")?.sequence;
|
|
38
|
+
const terminal = [...ordered]
|
|
39
|
+
.reverse()
|
|
40
|
+
.find((record) => (lastResumeSequence === undefined || record.sequence > lastResumeSequence) &&
|
|
41
|
+
(record.kind === "finish" || record.kind === "pause"));
|
|
42
|
+
const status = terminal?.kind === "finish"
|
|
43
|
+
? "finished"
|
|
44
|
+
: terminal?.kind === "pause"
|
|
45
|
+
? "paused"
|
|
46
|
+
: "interrupted";
|
|
47
|
+
if (terminal && !isRecord(terminal.payload)) {
|
|
48
|
+
throw new CoreMindError("run_state_corrupt", "Run Fact 包含损坏的 terminal payload");
|
|
49
|
+
}
|
|
50
|
+
const terminalPayload = terminal?.payload;
|
|
51
|
+
const terminalOperation = terminalField(terminalPayload, "operation", (value) => asOperation(value, runId));
|
|
52
|
+
const operation = terminalOperation ?? operationSnapshotFromRecords(ordered);
|
|
53
|
+
const outcome = terminalField(terminalPayload, "outcome", asRunOutcome);
|
|
54
|
+
const metrics = terminalField(terminalPayload, "metrics", asRunMetrics);
|
|
55
|
+
const evaluation = terminalField(terminalPayload, "evaluation", asEvaluationReport);
|
|
56
|
+
const releaseReadiness = terminalField(terminalPayload, "releaseReadiness", asReleaseReadiness);
|
|
57
|
+
const checkpoints = projectCheckpoints(ordered, runId);
|
|
58
|
+
const exactArtifacts = terminalField(terminalPayload, "artifacts", asArtifactRecords);
|
|
59
|
+
const artifacts = exactArtifacts ?? projectArtifacts(trace);
|
|
60
|
+
const extensions = terminalField(terminalPayload, "extensions", asExtensionReceipts) ?? projectExtensions(trace);
|
|
61
|
+
const requiresHuman = outcome?.error?.code === "unclassified_error";
|
|
62
|
+
const recovery = {
|
|
63
|
+
resumable: isRunStateResumable(ordered) && !requiresHuman,
|
|
64
|
+
requiresHuman,
|
|
65
|
+
...(operation ? { operation } : {}),
|
|
66
|
+
};
|
|
67
|
+
const snapshot = projectSnapshot({
|
|
68
|
+
runId,
|
|
69
|
+
operation,
|
|
70
|
+
outcome,
|
|
71
|
+
metrics,
|
|
72
|
+
evaluation,
|
|
73
|
+
releaseReadiness,
|
|
74
|
+
trace,
|
|
75
|
+
checkpoints,
|
|
76
|
+
artifacts: exactArtifacts ?? (artifacts.length === 0 ? [] : undefined),
|
|
77
|
+
extensions,
|
|
78
|
+
});
|
|
79
|
+
const context = projectContext(trace);
|
|
80
|
+
const pendingControls = [
|
|
81
|
+
...projectPendingApprovals(trace),
|
|
82
|
+
...projectPendingControlFacts(runId, ordered),
|
|
83
|
+
];
|
|
84
|
+
const childRuns = projectChildRuns(ordered, runId);
|
|
85
|
+
const observability = projectLocalObservability(ordered, {
|
|
86
|
+
runStatus: status,
|
|
87
|
+
resumable: recovery.resumable,
|
|
88
|
+
...(operation ? { operationState: operation.state } : {}),
|
|
89
|
+
context: {
|
|
90
|
+
budgets: context.budgets.length,
|
|
91
|
+
compactions: context.compactions.length,
|
|
92
|
+
failures: context.failures.length + context.lifecycleFailures.length,
|
|
93
|
+
},
|
|
94
|
+
artifacts: {
|
|
95
|
+
stored: artifacts.filter((artifact) => artifact.status === "stored").length,
|
|
96
|
+
blocked: artifacts.filter((artifact) => artifact.status === "blocked").length,
|
|
97
|
+
},
|
|
98
|
+
pendingControls: pendingControls.length,
|
|
99
|
+
});
|
|
100
|
+
return structuredClone({
|
|
101
|
+
schemaVersion: 1,
|
|
102
|
+
runId,
|
|
103
|
+
status,
|
|
104
|
+
resumable: recovery.resumable,
|
|
105
|
+
...(operation ? { operation } : {}),
|
|
106
|
+
...(outcome ? { outcome } : {}),
|
|
107
|
+
recovery,
|
|
108
|
+
trace,
|
|
109
|
+
checkpoints,
|
|
110
|
+
artifacts,
|
|
111
|
+
extensions,
|
|
112
|
+
context,
|
|
113
|
+
pendingControls,
|
|
114
|
+
...(childRuns ? { childRuns } : {}),
|
|
115
|
+
observability,
|
|
116
|
+
records: ordered.map(publicRunStateRecord),
|
|
117
|
+
...(snapshot ? { snapshot } : {}),
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
prepareResume(records, configFingerprint, requestedPrompt) {
|
|
121
|
+
if (records.length === 0) {
|
|
122
|
+
return prepareRunResume([], configFingerprint, requestedPrompt);
|
|
123
|
+
}
|
|
124
|
+
const projection = ProjectionEngine.project(records);
|
|
125
|
+
if (projection.recovery.requiresHuman) {
|
|
126
|
+
throw new CoreMindError("unclassified_error", "运行包含未分类外部错误,必须人工审计并通过显式处置流程继续");
|
|
127
|
+
}
|
|
128
|
+
const plan = prepareRunResume([...records], configFingerprint, requestedPrompt);
|
|
129
|
+
if (!projection.recovery.resumable) {
|
|
130
|
+
throw new CoreMindError("run_state_corrupt", "Projection 与恢复计划的准入结果不一致");
|
|
131
|
+
}
|
|
132
|
+
return plan;
|
|
133
|
+
},
|
|
134
|
+
async projectTree(store, rootRunId) {
|
|
135
|
+
const root = this.project(await store.read(rootRunId));
|
|
136
|
+
const visited = new Set([rootRunId]);
|
|
137
|
+
const nodes = [];
|
|
138
|
+
const collect = async (projection) => {
|
|
139
|
+
for (const sourceNode of projection.childRuns?.nodes ?? []) {
|
|
140
|
+
const node = structuredClone(sourceNode);
|
|
141
|
+
nodes.push(node);
|
|
142
|
+
if (visited.has(node.childRunId)) {
|
|
143
|
+
throw new CoreMindError("run_state_corrupt", "Child Run tree 包含循环或重复 ChildRunId");
|
|
144
|
+
}
|
|
145
|
+
visited.add(node.childRunId);
|
|
146
|
+
const childRecords = await store.read(node.childRunId);
|
|
147
|
+
if (childRecords.length === 0)
|
|
148
|
+
continue;
|
|
149
|
+
const childProjection = this.project(childRecords);
|
|
150
|
+
node.recovery = structuredClone(childProjection.recovery);
|
|
151
|
+
const workspaceLeases = projectWorkspaceLeasesFromRecords(childRecords);
|
|
152
|
+
if (workspaceLeases.length > 0)
|
|
153
|
+
node.workspaceLeases = workspaceLeases;
|
|
154
|
+
await collect(childProjection);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
await collect(root);
|
|
158
|
+
if (nodes.length === 0)
|
|
159
|
+
return root;
|
|
160
|
+
const activeDescendants = nodes.filter((node) => node.status === "created" || node.status === "running").length;
|
|
161
|
+
const unhandledDescendants = nodes.filter(childRunNodeIsUnhandled).length;
|
|
162
|
+
return {
|
|
163
|
+
...root,
|
|
164
|
+
childRuns: {
|
|
165
|
+
nodes,
|
|
166
|
+
activeDescendants,
|
|
167
|
+
unhandledDescendants,
|
|
168
|
+
quiescent: unhandledDescendants === 0,
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
function projectChildRuns(records, parentRunId) {
|
|
174
|
+
const nodes = new Map();
|
|
175
|
+
for (const record of records) {
|
|
176
|
+
if (record.kind !== "delegation")
|
|
177
|
+
continue;
|
|
178
|
+
const fact = decodeChildRunFact(record.payload);
|
|
179
|
+
if (!fact) {
|
|
180
|
+
throw new CoreMindError("run_state_corrupt", "Run Fact 包含损坏的 delegation payload");
|
|
181
|
+
}
|
|
182
|
+
if (fact.parentRunId !== parentRunId) {
|
|
183
|
+
throw new CoreMindError("run_state_corrupt", "Delegation Fact 的父 Run 身份不一致");
|
|
184
|
+
}
|
|
185
|
+
const existing = nodes.get(fact.delegationId);
|
|
186
|
+
if (fact.type === "delegation_recorded") {
|
|
187
|
+
if (existing) {
|
|
188
|
+
throw new CoreMindError("run_state_corrupt", "同一 DelegationId 存在重复 recorded Fact");
|
|
189
|
+
}
|
|
190
|
+
if (fact.predecessorDelegationId) {
|
|
191
|
+
const predecessor = nodes.get(fact.predecessorDelegationId);
|
|
192
|
+
if (predecessor?.disposition?.state !== "awaiting_redelegation" ||
|
|
193
|
+
predecessor.disposition.action !== "redelegate" ||
|
|
194
|
+
predecessor.disposition.successorDelegationId !== undefined ||
|
|
195
|
+
predecessor.disposition.recoveryDisposition !== "replay_safe") {
|
|
196
|
+
throw new CoreMindError("run_state_corrupt", "关联 Child Run 缺少匹配的安全重新委派处置");
|
|
197
|
+
}
|
|
198
|
+
predecessor.disposition.state = "recorded";
|
|
199
|
+
predecessor.disposition.successorDelegationId = fact.delegationId;
|
|
200
|
+
}
|
|
201
|
+
nodes.set(fact.delegationId, {
|
|
202
|
+
parentRunId,
|
|
203
|
+
childRunId: fact.childRunId,
|
|
204
|
+
delegationId: fact.delegationId,
|
|
205
|
+
...(fact.predecessorDelegationId
|
|
206
|
+
? { predecessorDelegationId: fact.predecessorDelegationId }
|
|
207
|
+
: {}),
|
|
208
|
+
agentName: fact.agentName,
|
|
209
|
+
inputFingerprint: fact.inputFingerprint,
|
|
210
|
+
budget: structuredClone(fact.inheritedPolicy.budget),
|
|
211
|
+
permissions: structuredClone(fact.inheritedPolicy.permissions),
|
|
212
|
+
model: structuredClone(fact.model),
|
|
213
|
+
workspace: structuredClone(fact.workspace),
|
|
214
|
+
status: "recorded",
|
|
215
|
+
});
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (!existing ||
|
|
219
|
+
existing.childRunId !== fact.childRunId ||
|
|
220
|
+
existing.inputFingerprint !== fact.inputFingerprint) {
|
|
221
|
+
throw new CoreMindError("run_state_corrupt", "Child Run 生命周期缺少匹配的 Delegation Fact");
|
|
222
|
+
}
|
|
223
|
+
if (fact.type === "delegation_disposition_recorded") {
|
|
224
|
+
if (existing.status !== "joined" ||
|
|
225
|
+
!existing.result ||
|
|
226
|
+
!childRunResultRequiresDisposition(existing.result) ||
|
|
227
|
+
existing.disposition?.state !== "required" ||
|
|
228
|
+
childRunResultFingerprint(existing.result) !== fact.resultFingerprint ||
|
|
229
|
+
canonicalRecovery(existing.result) !== canonicalJson(fact.recovery) ||
|
|
230
|
+
delegationDispositionViolation(existing.result, fact.action, fact.decidedBy) !== undefined) {
|
|
231
|
+
throw new CoreMindError("run_state_corrupt", "Delegation Disposition 与已 join 的 Child Run 结果不匹配");
|
|
232
|
+
}
|
|
233
|
+
existing.disposition = {
|
|
234
|
+
state: fact.action === "redelegate" ? "awaiting_redelegation" : "recorded",
|
|
235
|
+
action: fact.action,
|
|
236
|
+
decidedBy: fact.decidedBy,
|
|
237
|
+
reason: fact.reason,
|
|
238
|
+
recoveryDisposition: fact.recovery.recoveryDisposition,
|
|
239
|
+
};
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (fact.type === "delegation_redelegation_cancelled") {
|
|
243
|
+
if (existing.status !== "joined" ||
|
|
244
|
+
existing.disposition?.state !== "awaiting_redelegation" ||
|
|
245
|
+
existing.disposition.action !== "redelegate" ||
|
|
246
|
+
existing.disposition.successorDelegationId !== undefined) {
|
|
247
|
+
throw new CoreMindError("run_state_corrupt", "重新委派撤销 Fact 与待建立 successor 的处置不匹配");
|
|
248
|
+
}
|
|
249
|
+
existing.disposition.state = "redelegation_cancelled";
|
|
250
|
+
existing.disposition.parentTerminalCode = fact.parentTerminalCode;
|
|
251
|
+
existing.disposition.cancellationReason = fact.reason;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
existing.status = foldChildRunLifecycleStatus(existing.status, fact.type);
|
|
255
|
+
if (fact.type === "child_terminal") {
|
|
256
|
+
existing.outcome = structuredClone(fact.result.outcome);
|
|
257
|
+
existing.result = structuredClone(fact.result);
|
|
258
|
+
}
|
|
259
|
+
if (fact.type === "child_paused") {
|
|
260
|
+
existing.outcome = structuredClone(fact.result.outcome);
|
|
261
|
+
existing.result = structuredClone(fact.result);
|
|
262
|
+
}
|
|
263
|
+
if (fact.type === "child_orphaned") {
|
|
264
|
+
existing.outcome = structuredClone(fact.result.outcome);
|
|
265
|
+
existing.result = structuredClone(fact.result);
|
|
266
|
+
}
|
|
267
|
+
if (fact.type === "parent_joined") {
|
|
268
|
+
existing.outcome = structuredClone(fact.result.outcome);
|
|
269
|
+
existing.result = structuredClone(fact.result);
|
|
270
|
+
const recovery = childRunRecoveryAssessment(fact.result);
|
|
271
|
+
existing.disposition = !childRunResultRequiresDisposition(fact.result)
|
|
272
|
+
? { state: "not_required", recoveryDisposition: recovery.recoveryDisposition }
|
|
273
|
+
: {
|
|
274
|
+
state: "required",
|
|
275
|
+
requiredActor: isChildRunRecoverySafeForRedelegation(recovery)
|
|
276
|
+
? "parent_agent"
|
|
277
|
+
: "human",
|
|
278
|
+
recoveryDisposition: recovery.recoveryDisposition,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (nodes.size === 0)
|
|
283
|
+
return undefined;
|
|
284
|
+
const projected = [...nodes.values()];
|
|
285
|
+
const activeDescendants = projected.filter((node) => node.status === "created" || node.status === "running").length;
|
|
286
|
+
const unhandledDescendants = projected.filter(childRunNodeIsUnhandled).length;
|
|
287
|
+
return {
|
|
288
|
+
nodes: structuredClone(projected),
|
|
289
|
+
activeDescendants,
|
|
290
|
+
unhandledDescendants,
|
|
291
|
+
quiescent: unhandledDescendants === 0,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function childRunNodeIsUnhandled(node) {
|
|
295
|
+
return (node.status !== "joined" ||
|
|
296
|
+
node.disposition?.state === "required" ||
|
|
297
|
+
node.disposition?.state === "awaiting_redelegation");
|
|
298
|
+
}
|
|
299
|
+
function canonicalRecovery(result) {
|
|
300
|
+
return canonicalJson(childRunRecoveryAssessment(result));
|
|
301
|
+
}
|
|
302
|
+
function projectSnapshot(input) {
|
|
303
|
+
const { operation, outcome, metrics, evaluation, releaseReadiness } = input;
|
|
304
|
+
if (!operation || !outcome || !metrics || !evaluation || !releaseReadiness || !input.artifacts) {
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
return createRunSnapshot({
|
|
308
|
+
runId: input.runId,
|
|
309
|
+
operation,
|
|
310
|
+
outcome,
|
|
311
|
+
metrics,
|
|
312
|
+
evaluation,
|
|
313
|
+
releaseReadiness,
|
|
314
|
+
trace: input.trace,
|
|
315
|
+
checkpoints: input.checkpoints,
|
|
316
|
+
artifacts: input.artifacts,
|
|
317
|
+
extensions: input.extensions,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
function projectCheckpoints(records, runId) {
|
|
321
|
+
const checkpoints = new Map();
|
|
322
|
+
for (const record of records) {
|
|
323
|
+
if (record.kind !== "checkpoint")
|
|
324
|
+
continue;
|
|
325
|
+
if (!isRecord(record.payload)) {
|
|
326
|
+
throw new CoreMindError("run_state_corrupt", "Run Fact 包含损坏的 checkpoint payload");
|
|
327
|
+
}
|
|
328
|
+
const checkpoint = record.payload;
|
|
329
|
+
if (checkpoint.version !== 1 ||
|
|
330
|
+
checkpoint.runId !== runId ||
|
|
331
|
+
typeof checkpoint.checkpointId !== "string" ||
|
|
332
|
+
typeof checkpoint.timestamp !== "string" ||
|
|
333
|
+
Number.isNaN(Date.parse(checkpoint.timestamp)) ||
|
|
334
|
+
typeof checkpoint.tool !== "string" ||
|
|
335
|
+
typeof checkpoint.reversible !== "boolean" ||
|
|
336
|
+
typeof checkpoint.snapshotFile !== "string" ||
|
|
337
|
+
!optionalString(checkpoint.operationId) ||
|
|
338
|
+
!optionalString(checkpoint.toolCallId) ||
|
|
339
|
+
!optionalString(checkpoint.idempotencyKey) ||
|
|
340
|
+
!optionalString(checkpoint.targetPath) ||
|
|
341
|
+
!optionalBoolean(checkpoint.existed) ||
|
|
342
|
+
!optionalString(checkpoint.beforeSha256) ||
|
|
343
|
+
!optionalBoolean(checkpoint.afterExisted) ||
|
|
344
|
+
!optionalString(checkpoint.afterSha256) ||
|
|
345
|
+
!optionalString(checkpoint.reason)) {
|
|
346
|
+
throw new CoreMindError("run_state_corrupt", "Run Fact 包含损坏的 checkpoint payload");
|
|
347
|
+
}
|
|
348
|
+
const candidate = checkpoint;
|
|
349
|
+
const previous = checkpoints.get(candidate.checkpointId);
|
|
350
|
+
if (previous && !isCheckpointRefinement(previous, candidate)) {
|
|
351
|
+
throw new CoreMindError("run_state_corrupt", `Checkpoint ${candidate.checkpointId} 的写前身份或写后状态发生冲突`);
|
|
352
|
+
}
|
|
353
|
+
checkpoints.set(candidate.checkpointId, candidate);
|
|
354
|
+
}
|
|
355
|
+
return [...checkpoints.values()];
|
|
356
|
+
}
|
|
357
|
+
function isCheckpointRefinement(previous, candidate) {
|
|
358
|
+
const immutableFields = [
|
|
359
|
+
"version",
|
|
360
|
+
"checkpointId",
|
|
361
|
+
"runId",
|
|
362
|
+
"operationId",
|
|
363
|
+
"toolCallId",
|
|
364
|
+
"idempotencyKey",
|
|
365
|
+
"timestamp",
|
|
366
|
+
"tool",
|
|
367
|
+
"reversible",
|
|
368
|
+
"targetPath",
|
|
369
|
+
"existed",
|
|
370
|
+
"beforeSha256",
|
|
371
|
+
"reason",
|
|
372
|
+
"snapshotFile",
|
|
373
|
+
];
|
|
374
|
+
if (immutableFields.some((field) => previous[field] !== candidate[field]))
|
|
375
|
+
return false;
|
|
376
|
+
if (previous.afterExisted !== undefined && previous.afterExisted !== candidate.afterExisted) {
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
return previous.afterSha256 === undefined || previous.afterSha256 === candidate.afterSha256;
|
|
380
|
+
}
|
|
381
|
+
function projectArtifacts(trace) {
|
|
382
|
+
return trace.flatMap(({ event }) => {
|
|
383
|
+
if (event.type !== "artifact_created")
|
|
384
|
+
return [];
|
|
385
|
+
return [
|
|
386
|
+
{
|
|
387
|
+
artifactId: event.artifactId,
|
|
388
|
+
status: event.status,
|
|
389
|
+
...(event.relativePath ? { relativePath: event.relativePath } : {}),
|
|
390
|
+
sizeBytes: event.sizeBytes,
|
|
391
|
+
...(event.sha256 ? { sha256: event.sha256 } : {}),
|
|
392
|
+
mediaType: event.mediaType,
|
|
393
|
+
redaction: event.redaction,
|
|
394
|
+
},
|
|
395
|
+
];
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
function asArtifactRecords(value) {
|
|
399
|
+
if (!Array.isArray(value))
|
|
400
|
+
return undefined;
|
|
401
|
+
return value.every((artifact) => isRecord(artifact) &&
|
|
402
|
+
typeof artifact.artifactId === "string" &&
|
|
403
|
+
(artifact.status === "stored" || artifact.status === "blocked") &&
|
|
404
|
+
(artifact.relativePath === undefined || typeof artifact.relativePath === "string") &&
|
|
405
|
+
isNumber(artifact.sizeBytes) &&
|
|
406
|
+
(artifact.sha256 === undefined || typeof artifact.sha256 === "string") &&
|
|
407
|
+
typeof artifact.mediaType === "string" &&
|
|
408
|
+
isTimestamp(artifact.createdAt) &&
|
|
409
|
+
artifact.retention === "run" &&
|
|
410
|
+
(artifact.redaction === "none" || artifact.redaction === "blocked-secret"))
|
|
411
|
+
? value
|
|
412
|
+
: undefined;
|
|
413
|
+
}
|
|
414
|
+
function asExtensionReceipts(value) {
|
|
415
|
+
if (!Array.isArray(value))
|
|
416
|
+
return undefined;
|
|
417
|
+
return value.every((receipt) => isRecord(receipt) &&
|
|
418
|
+
typeof receipt.extensionId === "string" &&
|
|
419
|
+
typeof receipt.extensionVersion === "string" &&
|
|
420
|
+
LIFECYCLE_EVENTS.includes(receipt.event) &&
|
|
421
|
+
(receipt.status === "succeeded" ||
|
|
422
|
+
receipt.status === "failed" ||
|
|
423
|
+
receipt.status === "timed_out") &&
|
|
424
|
+
isNumber(receipt.durationMs) &&
|
|
425
|
+
(receipt.error === undefined || typeof receipt.error === "string") &&
|
|
426
|
+
(receipt.denied === undefined || typeof receipt.denied === "boolean"))
|
|
427
|
+
? value
|
|
428
|
+
: undefined;
|
|
429
|
+
}
|
|
430
|
+
function projectExtensions(trace) {
|
|
431
|
+
return trace.flatMap(({ event }) => event.type === "extension_lifecycle"
|
|
432
|
+
? [
|
|
433
|
+
{
|
|
434
|
+
extensionId: event.extensionId,
|
|
435
|
+
extensionVersion: event.extensionVersion,
|
|
436
|
+
event: event.lifecycle,
|
|
437
|
+
status: event.status,
|
|
438
|
+
durationMs: event.durationMs,
|
|
439
|
+
...(event.error ? { error: event.error } : {}),
|
|
440
|
+
...(event.denied !== undefined ? { denied: event.denied } : {}),
|
|
441
|
+
},
|
|
442
|
+
]
|
|
443
|
+
: []);
|
|
444
|
+
}
|
|
445
|
+
function projectPendingApprovals(trace) {
|
|
446
|
+
const resolved = new Set(trace.flatMap(({ event }) => (event.type === "approval_resolved" ? [event.approvalId] : [])));
|
|
447
|
+
return trace.flatMap(({ event }) => event.type === "approval_required" && !resolved.has(event.approvalId)
|
|
448
|
+
? [
|
|
449
|
+
{
|
|
450
|
+
type: "approval",
|
|
451
|
+
approvalId: event.approvalId,
|
|
452
|
+
runId: event.runId,
|
|
453
|
+
agent: event.agent,
|
|
454
|
+
tool: event.tool,
|
|
455
|
+
risk: event.risk,
|
|
456
|
+
},
|
|
457
|
+
]
|
|
458
|
+
: []);
|
|
459
|
+
}
|
|
460
|
+
function projectContext(trace) {
|
|
461
|
+
return {
|
|
462
|
+
stablePrefixes: trace.flatMap(({ event }) => event.type === "context_prefix"
|
|
463
|
+
? [{ agent: event.agent, fingerprint: event.fingerprint }]
|
|
464
|
+
: []),
|
|
465
|
+
budgets: trace.flatMap(({ event }) => event.type === "context_budget_resolved"
|
|
466
|
+
? [
|
|
467
|
+
{
|
|
468
|
+
providerId: event.providerId,
|
|
469
|
+
modelId: event.modelId,
|
|
470
|
+
capabilityFingerprint: event.capabilityFingerprint,
|
|
471
|
+
source: event.source,
|
|
472
|
+
confidence: event.confidence,
|
|
473
|
+
effectiveContextWindow: event.effectiveContextWindow,
|
|
474
|
+
reservedOutputTokens: event.reservedOutputTokens,
|
|
475
|
+
availableInputTokens: event.availableInputTokens,
|
|
476
|
+
messageTokens: event.messageTokens,
|
|
477
|
+
estimator: event.estimator,
|
|
478
|
+
},
|
|
479
|
+
]
|
|
480
|
+
: []),
|
|
481
|
+
compactions: trace.flatMap(({ event }) => event.type === "context_compacted"
|
|
482
|
+
? [
|
|
483
|
+
{
|
|
484
|
+
beforeTokens: event.beforeTokens,
|
|
485
|
+
afterTokens: event.afterTokens,
|
|
486
|
+
removedMessages: event.removedMessages,
|
|
487
|
+
strategy: event.strategy,
|
|
488
|
+
reason: event.reason,
|
|
489
|
+
summaryFingerprint: event.summaryFingerprint,
|
|
490
|
+
...(event.sessionEntryId ? { sessionEntryId: event.sessionEntryId } : {}),
|
|
491
|
+
...(event.capabilityFingerprint
|
|
492
|
+
? { capabilityFingerprint: event.capabilityFingerprint }
|
|
493
|
+
: {}),
|
|
494
|
+
...(event.lineageDepth === undefined ? {} : { lineageDepth: event.lineageDepth }),
|
|
495
|
+
...(event.rebuiltFromCanonical === undefined
|
|
496
|
+
? {}
|
|
497
|
+
: { rebuiltFromCanonical: event.rebuiltFromCanonical }),
|
|
498
|
+
...(event.trigger ? { trigger: event.trigger } : {}),
|
|
499
|
+
},
|
|
500
|
+
]
|
|
501
|
+
: []),
|
|
502
|
+
failures: trace.flatMap(({ event }) => event.type === "context_compaction_failed"
|
|
503
|
+
? [{ message: event.message, preservedMessages: event.preservedMessages }]
|
|
504
|
+
: []),
|
|
505
|
+
lifecycleFailures: trace.flatMap(({ event }) => event.type === "context_lifecycle_failed"
|
|
506
|
+
? [
|
|
507
|
+
{
|
|
508
|
+
code: event.code,
|
|
509
|
+
reason: event.reason,
|
|
510
|
+
pausable: event.pausable,
|
|
511
|
+
preservedMessages: event.preservedMessages,
|
|
512
|
+
providerCallBlocked: event.providerCallBlocked,
|
|
513
|
+
},
|
|
514
|
+
]
|
|
515
|
+
: []),
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
function isTraceEvent(value, runId) {
|
|
519
|
+
return (isRecord(value) &&
|
|
520
|
+
typeof value.eventId === "string" &&
|
|
521
|
+
value.runId === runId &&
|
|
522
|
+
Number.isInteger(value.sequence) &&
|
|
523
|
+
isTimestamp(value.timestamp) &&
|
|
524
|
+
isRecord(value.event) &&
|
|
525
|
+
typeof value.event.type === "string" &&
|
|
526
|
+
isProjectionEventValid(value.event, runId));
|
|
527
|
+
}
|
|
528
|
+
function publicTraceEvent(entry) {
|
|
529
|
+
const { protocolToolResult: _internal, ...publicEntry } = entry;
|
|
530
|
+
return publicEntry;
|
|
531
|
+
}
|
|
532
|
+
function publicRunStateRecord(record) {
|
|
533
|
+
if (record.kind !== "event")
|
|
534
|
+
return record;
|
|
535
|
+
return { ...record, payload: publicTraceEvent(record.payload) };
|
|
536
|
+
}
|
|
537
|
+
function isProjectionEventValid(event, runId) {
|
|
538
|
+
switch (event.type) {
|
|
539
|
+
case "agent_start":
|
|
540
|
+
case "agent_end":
|
|
541
|
+
return (typeof event.agent === "string" &&
|
|
542
|
+
optionalString(event.stepId) &&
|
|
543
|
+
optionalString(event.turnId));
|
|
544
|
+
case "turn_end":
|
|
545
|
+
return (typeof event.agent === "string" &&
|
|
546
|
+
optionalString(event.stepId) &&
|
|
547
|
+
optionalString(event.turnId) &&
|
|
548
|
+
optionalNumber(event.tokens) &&
|
|
549
|
+
optionalNumber(event.inputTokens) &&
|
|
550
|
+
optionalNumber(event.outputTokens) &&
|
|
551
|
+
optionalNumber(event.cacheReadTokens) &&
|
|
552
|
+
optionalNumber(event.cacheWriteTokens) &&
|
|
553
|
+
(event.promptCacheStatus === undefined ||
|
|
554
|
+
event.promptCacheStatus === "available" ||
|
|
555
|
+
event.promptCacheStatus === "unavailable") &&
|
|
556
|
+
optionalNumber(event.costUsd) &&
|
|
557
|
+
optionalBoolean(event.requestsAnotherTurn));
|
|
558
|
+
case "text_delta":
|
|
559
|
+
return (typeof event.agent === "string" &&
|
|
560
|
+
typeof event.delta === "string" &&
|
|
561
|
+
optionalString(event.stepId));
|
|
562
|
+
case "tool_call":
|
|
563
|
+
return (typeof event.agent === "string" &&
|
|
564
|
+
typeof event.tool === "string" &&
|
|
565
|
+
"args" in event &&
|
|
566
|
+
optionalString(event.argumentsFingerprint) &&
|
|
567
|
+
optionalString(event.callId) &&
|
|
568
|
+
optionalString(event.idempotencyKey) &&
|
|
569
|
+
optionalString(event.stepId) &&
|
|
570
|
+
optionalString(event.turnId));
|
|
571
|
+
case "tool_result":
|
|
572
|
+
return (typeof event.agent === "string" &&
|
|
573
|
+
typeof event.tool === "string" &&
|
|
574
|
+
typeof event.isError === "boolean" &&
|
|
575
|
+
optionalString(event.callId) &&
|
|
576
|
+
optionalString(event.idempotencyKey) &&
|
|
577
|
+
optionalString(event.stepId) &&
|
|
578
|
+
optionalString(event.turnId));
|
|
579
|
+
case "tool_attempt":
|
|
580
|
+
return (typeof event.attemptId === "string" &&
|
|
581
|
+
typeof event.previousReceiptId === "string" &&
|
|
582
|
+
Number.isInteger(event.attempt) &&
|
|
583
|
+
typeof event.agent === "string" &&
|
|
584
|
+
typeof event.tool === "string" &&
|
|
585
|
+
typeof event.callId === "string" &&
|
|
586
|
+
optionalString(event.stepId) &&
|
|
587
|
+
typeof event.argumentsFingerprint === "string");
|
|
588
|
+
case "capability_resolved":
|
|
589
|
+
return (typeof event.agent === "string" &&
|
|
590
|
+
typeof event.tool === "string" &&
|
|
591
|
+
typeof event.callId === "string" &&
|
|
592
|
+
optionalString(event.stepId) &&
|
|
593
|
+
isResolvedToolCapability(event.capability, event.tool) &&
|
|
594
|
+
RECOVERY_DISPOSITIONS.includes(event.recoveryDisposition) &&
|
|
595
|
+
recoveryDispositionFor(event.capability) === event.recoveryDisposition);
|
|
596
|
+
case "workspace_lease":
|
|
597
|
+
return (["acquired", "released", "recovery_required"].includes(String(event.status)) &&
|
|
598
|
+
typeof event.canonicalRoot === "string" &&
|
|
599
|
+
["parallel", "run_serial", "workspace_exclusive"].includes(String(event.lane)) &&
|
|
600
|
+
isRecord(event.owner) &&
|
|
601
|
+
(event.status === "recovery_required" || event.owner.runId === runId) &&
|
|
602
|
+
typeof event.owner.callId === "string" &&
|
|
603
|
+
Number.isInteger(event.owner.pid) &&
|
|
604
|
+
typeof event.agent === "string" &&
|
|
605
|
+
typeof event.callId === "string" &&
|
|
606
|
+
optionalString(event.stepId));
|
|
607
|
+
case "effect_receipt":
|
|
608
|
+
return (typeof event.idempotencyKey === "string" &&
|
|
609
|
+
typeof event.tool === "string" &&
|
|
610
|
+
["not_started", "started", "committed", "unknown"].includes(String(event.status)) &&
|
|
611
|
+
optionalString(event.agent) &&
|
|
612
|
+
optionalString(event.callId) &&
|
|
613
|
+
optionalString(event.stepId) &&
|
|
614
|
+
optionalString(event.turnId) &&
|
|
615
|
+
(event.binding === undefined || isEffectReceiptBinding(event.binding, event, runId)));
|
|
616
|
+
case "step_start":
|
|
617
|
+
return typeof event.stepId === "string" && typeof event.kind === "string";
|
|
618
|
+
case "step_output":
|
|
619
|
+
return (typeof event.stepId === "string" &&
|
|
620
|
+
typeof event.agent === "string" &&
|
|
621
|
+
typeof event.text === "string" &&
|
|
622
|
+
optionalString(event.saveAs));
|
|
623
|
+
case "step_resumed":
|
|
624
|
+
return typeof event.stepId === "string";
|
|
625
|
+
case "step_end":
|
|
626
|
+
return typeof event.stepId === "string" && typeof event.ok === "boolean";
|
|
627
|
+
case "loop_state":
|
|
628
|
+
return (isLoopPhase(event.from) &&
|
|
629
|
+
isLoopPhase(event.to) &&
|
|
630
|
+
typeof event.trigger === "string" &&
|
|
631
|
+
nonNegativeInteger(event.iteration) &&
|
|
632
|
+
nonNegativeInteger(event.repairs) &&
|
|
633
|
+
optionalString(event.reason));
|
|
634
|
+
case "retry":
|
|
635
|
+
return ((event.scope === "provider" || event.scope === "workflow") &&
|
|
636
|
+
Number.isInteger(event.attempt) &&
|
|
637
|
+
event.attempt >= 1 &&
|
|
638
|
+
optionalString(event.stepId));
|
|
639
|
+
case "artifact_created":
|
|
640
|
+
return (typeof event.artifactId === "string" &&
|
|
641
|
+
(event.status === "stored" || event.status === "blocked") &&
|
|
642
|
+
isNumber(event.sizeBytes) &&
|
|
643
|
+
optionalString(event.relativePath) &&
|
|
644
|
+
optionalString(event.sha256) &&
|
|
645
|
+
typeof event.mediaType === "string" &&
|
|
646
|
+
(event.redaction === "none" || event.redaction === "blocked-secret") &&
|
|
647
|
+
typeof event.tool === "string" &&
|
|
648
|
+
optionalString(event.callId));
|
|
649
|
+
case "extension_lifecycle":
|
|
650
|
+
return (typeof event.extensionId === "string" &&
|
|
651
|
+
typeof event.extensionVersion === "string" &&
|
|
652
|
+
LIFECYCLE_EVENTS.includes(event.lifecycle) &&
|
|
653
|
+
(event.status === "succeeded" ||
|
|
654
|
+
event.status === "failed" ||
|
|
655
|
+
event.status === "timed_out") &&
|
|
656
|
+
isNumber(event.durationMs) &&
|
|
657
|
+
optionalString(event.error) &&
|
|
658
|
+
optionalBoolean(event.denied));
|
|
659
|
+
case "approval_required":
|
|
660
|
+
return (typeof event.approvalId === "string" &&
|
|
661
|
+
event.runId === runId &&
|
|
662
|
+
typeof event.agent === "string" &&
|
|
663
|
+
typeof event.tool === "string" &&
|
|
664
|
+
"args" in event &&
|
|
665
|
+
(event.risk === "low" || event.risk === "high") &&
|
|
666
|
+
isToolEffect(event.effect) &&
|
|
667
|
+
(event.capability === undefined || isResolvedToolCapability(event.capability, event.tool)));
|
|
668
|
+
case "approval_resolved":
|
|
669
|
+
return (typeof event.approvalId === "string" &&
|
|
670
|
+
event.runId === runId &&
|
|
671
|
+
(event.decision === "allow" || event.decision === "deny"));
|
|
672
|
+
case "policy_denied":
|
|
673
|
+
return (typeof event.agent === "string" &&
|
|
674
|
+
typeof event.tool === "string" &&
|
|
675
|
+
typeof event.reason === "string");
|
|
676
|
+
case "budget_exceeded":
|
|
677
|
+
return (["turns", "toolCalls", "toolFailures", "tokens", "costUsd"].includes(String(event.dimension)) &&
|
|
678
|
+
isNumber(event.limit) &&
|
|
679
|
+
isNumber(event.actual) &&
|
|
680
|
+
typeof event.message === "string");
|
|
681
|
+
case "context_prefix":
|
|
682
|
+
return typeof event.agent === "string" && typeof event.fingerprint === "string";
|
|
683
|
+
case "provider_request":
|
|
684
|
+
return (typeof event.requestId === "string" &&
|
|
685
|
+
typeof event.agent === "string" &&
|
|
686
|
+
optionalString(event.stepId) &&
|
|
687
|
+
typeof event.providerId === "string" &&
|
|
688
|
+
typeof event.modelId === "string" &&
|
|
689
|
+
typeof event.messageFingerprint === "string" &&
|
|
690
|
+
typeof event.stablePrefixFingerprint === "string" &&
|
|
691
|
+
typeof event.toolSchemaFingerprint === "string" &&
|
|
692
|
+
typeof event.capabilityFingerprint === "string" &&
|
|
693
|
+
typeof event.contextWorkingSetFingerprint === "string");
|
|
694
|
+
case "context_budget_resolved":
|
|
695
|
+
return (typeof event.providerId === "string" &&
|
|
696
|
+
typeof event.modelId === "string" &&
|
|
697
|
+
typeof event.capabilityFingerprint === "string" &&
|
|
698
|
+
[
|
|
699
|
+
"locked_catalog",
|
|
700
|
+
"explicit_config",
|
|
701
|
+
"provider_metadata",
|
|
702
|
+
"conservative_fallback",
|
|
703
|
+
].includes(String(event.source)) &&
|
|
704
|
+
["verified", "declared", "assumed"].includes(String(event.confidence)) &&
|
|
705
|
+
isNumber(event.effectiveContextWindow) &&
|
|
706
|
+
isNumber(event.reservedOutputTokens) &&
|
|
707
|
+
isNumber(event.availableInputTokens) &&
|
|
708
|
+
isNumber(event.messageTokens) &&
|
|
709
|
+
isNumber(event.stablePrefixTokens) &&
|
|
710
|
+
isNumber(event.toolSchemaTokens) &&
|
|
711
|
+
isNumber(event.structuredOutputTokens) &&
|
|
712
|
+
isNumber(event.multimodalTokens) &&
|
|
713
|
+
isNumber(event.protocolOverheadTokens) &&
|
|
714
|
+
isNumber(event.safetyMarginTokens) &&
|
|
715
|
+
event.estimator === "pi-agent-core-estimate-v1" &&
|
|
716
|
+
Array.isArray(event.evidence) &&
|
|
717
|
+
event.evidence.every((item) => item === "safe_context_intersection" || item === "assumed_context_window"));
|
|
718
|
+
case "context_compacted":
|
|
719
|
+
return (isNumber(event.beforeTokens) &&
|
|
720
|
+
isNumber(event.afterTokens) &&
|
|
721
|
+
isNumber(event.removedMessages) &&
|
|
722
|
+
(event.strategy === "deterministic-v1" || event.strategy === "task-state-v1") &&
|
|
723
|
+
event.reason === "threshold" &&
|
|
724
|
+
typeof event.summaryFingerprint === "string" &&
|
|
725
|
+
optionalString(event.sessionEntryId) &&
|
|
726
|
+
optionalString(event.capabilityFingerprint) &&
|
|
727
|
+
optionalNumber(event.lineageDepth) &&
|
|
728
|
+
optionalBoolean(event.rebuiltFromCanonical) &&
|
|
729
|
+
(event.trigger === undefined ||
|
|
730
|
+
event.trigger === "threshold" ||
|
|
731
|
+
event.trigger === "model_switch" ||
|
|
732
|
+
event.trigger === "provider_overflow"));
|
|
733
|
+
case "context_compaction_failed":
|
|
734
|
+
return typeof event.message === "string" && isNumber(event.preservedMessages);
|
|
735
|
+
case "context_lifecycle_failed":
|
|
736
|
+
return ([
|
|
737
|
+
"context_capability_conflict",
|
|
738
|
+
"context_budget_exhausted",
|
|
739
|
+
"context_artifact_missing",
|
|
740
|
+
"context_lineage_corrupt",
|
|
741
|
+
].includes(String(event.code)) &&
|
|
742
|
+
typeof event.reason === "string" &&
|
|
743
|
+
typeof event.pausable === "boolean" &&
|
|
744
|
+
isNumber(event.preservedMessages) &&
|
|
745
|
+
event.providerCallBlocked === true);
|
|
746
|
+
case "checkpoint_created":
|
|
747
|
+
return (typeof event.checkpointId === "string" &&
|
|
748
|
+
typeof event.tool === "string" &&
|
|
749
|
+
optionalString(event.callId) &&
|
|
750
|
+
optionalString(event.idempotencyKey) &&
|
|
751
|
+
optionalString(event.targetPath) &&
|
|
752
|
+
typeof event.reversible === "boolean");
|
|
753
|
+
case "tool_execution_evidence":
|
|
754
|
+
return (typeof event.agent === "string" &&
|
|
755
|
+
typeof event.tool === "string" &&
|
|
756
|
+
typeof event.callId === "string" &&
|
|
757
|
+
optionalString(event.stepId) &&
|
|
758
|
+
isRecord(event.execution) &&
|
|
759
|
+
isNumber(event.execution.durationMs) &&
|
|
760
|
+
(event.execution.exitCode === null || Number.isInteger(event.execution.exitCode)) &&
|
|
761
|
+
optionalString(event.execution.commandSha256) &&
|
|
762
|
+
optionalBoolean(event.execution.testCommand));
|
|
763
|
+
case "engineering_evidence":
|
|
764
|
+
return (typeof event.stepId === "string" &&
|
|
765
|
+
typeof event.textPassed === "boolean" &&
|
|
766
|
+
typeof event.passed === "boolean" &&
|
|
767
|
+
nonNegativeInteger(event.successfulTestCommands) &&
|
|
768
|
+
typeof event.regressionCommandMatched === "boolean" &&
|
|
769
|
+
typeof event.checkpointRecorded === "boolean" &&
|
|
770
|
+
typeof event.diffReviewed === "boolean" &&
|
|
771
|
+
Array.isArray(event.reasons) &&
|
|
772
|
+
event.reasons.every((reason) => typeof reason === "string"));
|
|
773
|
+
case "error":
|
|
774
|
+
return typeof event.message === "string" && typeof event.fatal === "boolean";
|
|
775
|
+
case "input_receipt":
|
|
776
|
+
return (typeof event.inputId === "string" &&
|
|
777
|
+
event.status === "pending" &&
|
|
778
|
+
typeof event.contentFingerprint === "string" &&
|
|
779
|
+
isTimestamp(event.timestamp));
|
|
780
|
+
case "input_claimed":
|
|
781
|
+
return (typeof event.inputId === "string" &&
|
|
782
|
+
event.status === "claimed" &&
|
|
783
|
+
typeof event.turnId === "string" &&
|
|
784
|
+
isTimestamp(event.timestamp));
|
|
785
|
+
case "input_completed":
|
|
786
|
+
return (typeof event.inputId === "string" &&
|
|
787
|
+
event.status === "completed" &&
|
|
788
|
+
isTimestamp(event.timestamp));
|
|
789
|
+
case "input_discarded":
|
|
790
|
+
return (typeof event.inputId === "string" &&
|
|
791
|
+
event.status === "discarded" &&
|
|
792
|
+
isTimestamp(event.timestamp));
|
|
793
|
+
case "quiescence_timeout":
|
|
794
|
+
return isNumber(event.timeoutMs);
|
|
795
|
+
case "tool_lifecycle":
|
|
796
|
+
return isToolLifecycleFact(event);
|
|
797
|
+
default:
|
|
798
|
+
return false;
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
const LOOP_PHASES = new Set([
|
|
802
|
+
"idle",
|
|
803
|
+
"planning",
|
|
804
|
+
"executing",
|
|
805
|
+
"verifying",
|
|
806
|
+
"repairing",
|
|
807
|
+
"paused",
|
|
808
|
+
"succeeded",
|
|
809
|
+
"failed",
|
|
810
|
+
"aborted",
|
|
811
|
+
"timeout",
|
|
812
|
+
"budget_exceeded",
|
|
813
|
+
]);
|
|
814
|
+
function isLoopPhase(value) {
|
|
815
|
+
return typeof value === "string" && LOOP_PHASES.has(value);
|
|
816
|
+
}
|
|
817
|
+
function nonNegativeInteger(value) {
|
|
818
|
+
return Number.isInteger(value) && value >= 0;
|
|
819
|
+
}
|
|
820
|
+
function isToolEffect(value) {
|
|
821
|
+
if (!isRecord(value))
|
|
822
|
+
return false;
|
|
823
|
+
return (Array.isArray(value.operations) &&
|
|
824
|
+
value.operations.every((operation) => ["read", "write", "process", "network", "external"].includes(String(operation))) &&
|
|
825
|
+
Array.isArray(value.paths) &&
|
|
826
|
+
value.paths.every((path) => typeof path === "string") &&
|
|
827
|
+
Array.isArray(value.urls) &&
|
|
828
|
+
value.urls.every((url) => typeof url === "string") &&
|
|
829
|
+
typeof value.reversible === "boolean" &&
|
|
830
|
+
typeof value.declared === "boolean");
|
|
831
|
+
}
|
|
832
|
+
function isEffectReceiptBinding(value, event, runId) {
|
|
833
|
+
if (!isRecord(value))
|
|
834
|
+
return false;
|
|
835
|
+
return (value.version === 1 &&
|
|
836
|
+
value.runId === runId &&
|
|
837
|
+
typeof value.turnId === "string" &&
|
|
838
|
+
value.agent === event.agent &&
|
|
839
|
+
optionalString(value.stepId) &&
|
|
840
|
+
value.stepId === event.stepId &&
|
|
841
|
+
value.callId === event.callId &&
|
|
842
|
+
value.tool === event.tool &&
|
|
843
|
+
typeof value.argumentsFingerprint === "string" &&
|
|
844
|
+
value.argumentsFingerprint.length === 64 &&
|
|
845
|
+
typeof value.capabilityFingerprint === "string" &&
|
|
846
|
+
value.capabilityFingerprint.length === 64);
|
|
847
|
+
}
|
|
848
|
+
function isToolLifecycleFact(value) {
|
|
849
|
+
try {
|
|
850
|
+
validateToolCallLifecycleFact(value);
|
|
851
|
+
return true;
|
|
852
|
+
}
|
|
853
|
+
catch {
|
|
854
|
+
return false;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
function terminalField(payload, field, parse) {
|
|
858
|
+
if (!payload || !(field in payload))
|
|
859
|
+
return undefined;
|
|
860
|
+
const parsed = parse(payload[field]);
|
|
861
|
+
if (parsed === undefined) {
|
|
862
|
+
throw new CoreMindError("run_state_corrupt", `Run Fact 包含损坏的 terminal.${field}`);
|
|
863
|
+
}
|
|
864
|
+
return parsed;
|
|
865
|
+
}
|
|
866
|
+
function optionalString(value) {
|
|
867
|
+
return value === undefined || typeof value === "string";
|
|
868
|
+
}
|
|
869
|
+
function optionalBoolean(value) {
|
|
870
|
+
return value === undefined || typeof value === "boolean";
|
|
871
|
+
}
|
|
872
|
+
function optionalNumber(value) {
|
|
873
|
+
return value === undefined || isNumber(value);
|
|
874
|
+
}
|
|
875
|
+
function isNumber(value) {
|
|
876
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
877
|
+
}
|
|
878
|
+
function isTimestamp(value) {
|
|
879
|
+
return typeof value === "string" && !Number.isNaN(Date.parse(value));
|
|
880
|
+
}
|
|
881
|
+
function asOperation(value, runId) {
|
|
882
|
+
if (!isRecord(value) ||
|
|
883
|
+
value.schemaVersion !== 1 ||
|
|
884
|
+
value.runId !== runId ||
|
|
885
|
+
typeof value.operationId !== "string" ||
|
|
886
|
+
typeof value.correlationId !== "string" ||
|
|
887
|
+
!["accepted", "running", "paused", "aborting", "completed", "failed"].includes(String(value.state)) ||
|
|
888
|
+
!Number.isInteger(value.transitionSequence) ||
|
|
889
|
+
!isTimestamp(value.createdAt) ||
|
|
890
|
+
!isTimestamp(value.updatedAt) ||
|
|
891
|
+
!optionalString(value.pauseReason) ||
|
|
892
|
+
!optionalString(value.failureReason)) {
|
|
893
|
+
return undefined;
|
|
894
|
+
}
|
|
895
|
+
return value;
|
|
896
|
+
}
|
|
897
|
+
function asRunOutcome(value) {
|
|
898
|
+
return isRecord(value) &&
|
|
899
|
+
["succeeded", "failed", "paused", "aborted", "timeout", "budget_exceeded"].includes(String(value.status)) &&
|
|
900
|
+
typeof value.finishReason === "string" &&
|
|
901
|
+
(value.error === undefined ||
|
|
902
|
+
(isRecord(value.error) &&
|
|
903
|
+
typeof value.error.code === "string" &&
|
|
904
|
+
typeof value.error.message === "string" &&
|
|
905
|
+
(value.error.audit === undefined ||
|
|
906
|
+
(isRecord(value.error.audit) && typeof value.error.audit.originalCode === "string"))))
|
|
907
|
+
? value
|
|
908
|
+
: undefined;
|
|
909
|
+
}
|
|
910
|
+
function asRunMetrics(value) {
|
|
911
|
+
return isRecord(value) &&
|
|
912
|
+
isNumber(value.durationMs) &&
|
|
913
|
+
isNumber(value.turns) &&
|
|
914
|
+
isRecord(value.steps) &&
|
|
915
|
+
isNumber(value.steps.total) &&
|
|
916
|
+
isNumber(value.steps.succeeded) &&
|
|
917
|
+
isNumber(value.steps.failed) &&
|
|
918
|
+
isNumber(value.toolCalls) &&
|
|
919
|
+
isNumber(value.toolFailures) &&
|
|
920
|
+
isNumber(value.retries) &&
|
|
921
|
+
optionalNumber(value.tokens) &&
|
|
922
|
+
optionalNumber(value.costUsd) &&
|
|
923
|
+
isNumber(value.outputChars) &&
|
|
924
|
+
(value.context === undefined || isRunContextMetrics(value.context)) &&
|
|
925
|
+
(value.artifacts === undefined || isArtifactMetrics(value.artifacts)) &&
|
|
926
|
+
optionalNumber(value.rejectedAfterAbort)
|
|
927
|
+
? value
|
|
928
|
+
: undefined;
|
|
929
|
+
}
|
|
930
|
+
function isRunContextMetrics(value) {
|
|
931
|
+
return (isRecord(value) &&
|
|
932
|
+
isNumber(value.inputTokens) &&
|
|
933
|
+
isNumber(value.outputTokens) &&
|
|
934
|
+
isNumber(value.cacheReadTokens) &&
|
|
935
|
+
isNumber(value.cacheWriteTokens) &&
|
|
936
|
+
["available", "unavailable", "unknown"].includes(String(value.promptCacheStatus)) &&
|
|
937
|
+
isNumber(value.compactions) &&
|
|
938
|
+
optionalString(value.lastSummaryFingerprint) &&
|
|
939
|
+
Array.isArray(value.stablePrefixFingerprints) &&
|
|
940
|
+
value.stablePrefixFingerprints.every((item) => typeof item === "string"));
|
|
941
|
+
}
|
|
942
|
+
function isArtifactMetrics(value) {
|
|
943
|
+
return (isRecord(value) &&
|
|
944
|
+
isNumber(value.stored) &&
|
|
945
|
+
isNumber(value.blocked) &&
|
|
946
|
+
isNumber(value.totalBytes));
|
|
947
|
+
}
|
|
948
|
+
function asEvaluationReport(value) {
|
|
949
|
+
return isRecord(value) &&
|
|
950
|
+
["development", "standard", "strict"].includes(String(value.profile)) &&
|
|
951
|
+
Array.isArray(value.scenarioResults) &&
|
|
952
|
+
value.scenarioResults.every((result) => isRecord(result) &&
|
|
953
|
+
typeof result.id === "string" &&
|
|
954
|
+
typeof result.passed === "boolean" &&
|
|
955
|
+
optionalNumber(result.score) &&
|
|
956
|
+
optionalString(result.reason)) &&
|
|
957
|
+
isRecord(value.qualityScores) &&
|
|
958
|
+
Object.values(value.qualityScores).every(isNumber) &&
|
|
959
|
+
Array.isArray(value.securityFindings) &&
|
|
960
|
+
value.securityFindings.every((finding) => typeof finding === "string")
|
|
961
|
+
? value
|
|
962
|
+
: undefined;
|
|
963
|
+
}
|
|
964
|
+
function asReleaseReadiness(value) {
|
|
965
|
+
return isRecord(value) &&
|
|
966
|
+
typeof value.ready === "boolean" &&
|
|
967
|
+
Array.isArray(value.blockers) &&
|
|
968
|
+
value.blockers.every((blocker) => typeof blocker === "string") &&
|
|
969
|
+
Array.isArray(value.warnings) &&
|
|
970
|
+
value.warnings.every((warning) => typeof warning === "string") &&
|
|
971
|
+
(value.overrideRecord === undefined ||
|
|
972
|
+
(isRecord(value.overrideRecord) &&
|
|
973
|
+
typeof value.overrideRecord.reason === "string" &&
|
|
974
|
+
isTimestamp(value.overrideRecord.recordedAt)))
|
|
975
|
+
? value
|
|
976
|
+
: undefined;
|
|
977
|
+
}
|
|
978
|
+
function isRecord(value) {
|
|
979
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
980
|
+
}
|
|
981
|
+
//# sourceMappingURL=projection.js.map
|