coremind-runtime 0.2.0-rc.1 → 0.3.0-rc.2
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 +2 -0
- package/dist/agent-factory.d.ts +4 -0
- package/dist/agent-factory.d.ts.map +1 -1
- package/dist/agent-factory.js +19 -7
- package/dist/agent-factory.js.map +1 -1
- package/dist/budget.d.ts +1 -2
- package/dist/budget.d.ts.map +1 -1
- package/dist/budget.js +9 -4
- package/dist/budget.js.map +1 -1
- package/dist/chat-session.d.ts.map +1 -1
- package/dist/chat-session.js.map +1 -1
- package/dist/checkpoint.d.ts +8 -1
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +3 -1
- package/dist/checkpoint.js.map +1 -1
- package/dist/coding/engineering-kernel.d.ts +160 -0
- package/dist/coding/engineering-kernel.d.ts.map +1 -0
- package/dist/coding/engineering-kernel.js +484 -0
- package/dist/coding/engineering-kernel.js.map +1 -0
- package/dist/coding/runtime-engineering-evidence.d.ts +27 -0
- package/dist/coding/runtime-engineering-evidence.d.ts.map +1 -0
- package/dist/coding/runtime-engineering-evidence.js +87 -0
- package/dist/coding/runtime-engineering-evidence.js.map +1 -0
- package/dist/context.d.ts +32 -4
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +69 -3
- package/dist/context.js.map +1 -1
- package/dist/dependency-adapter.d.ts +26 -0
- package/dist/dependency-adapter.d.ts.map +1 -0
- package/dist/dependency-adapter.js +30 -0
- package/dist/dependency-adapter.js.map +1 -0
- package/dist/evaluation.d.ts.map +1 -1
- package/dist/evaluation.js +39 -1
- package/dist/evaluation.js.map +1 -1
- package/dist/events.d.ts +65 -5
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +27 -13
- package/dist/events.js.map +1 -1
- package/dist/experiment.d.ts +63 -0
- package/dist/experiment.d.ts.map +1 -0
- package/dist/experiment.js +96 -0
- package/dist/experiment.js.map +1 -0
- package/dist/index.d.ts +12 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -7
- package/dist/index.js.map +1 -1
- package/dist/lifecycle-extension.d.ts +74 -0
- package/dist/lifecycle-extension.d.ts.map +1 -0
- package/dist/lifecycle-extension.js +204 -0
- package/dist/lifecycle-extension.js.map +1 -0
- package/dist/loop-runner.d.ts +6 -0
- package/dist/loop-runner.d.ts.map +1 -1
- package/dist/loop-runner.js +9 -1
- package/dist/loop-runner.js.map +1 -1
- package/dist/operation-state.d.ts +62 -0
- package/dist/operation-state.d.ts.map +1 -0
- package/dist/operation-state.js +160 -0
- package/dist/operation-state.js.map +1 -0
- package/dist/provider.d.ts +2 -0
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +32 -17
- package/dist/provider.js.map +1 -1
- package/dist/public-message.d.ts +31 -0
- package/dist/public-message.d.ts.map +1 -0
- package/dist/public-message.js +2 -0
- package/dist/public-message.js.map +1 -0
- package/dist/result.d.ts +17 -2
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +48 -6
- package/dist/result.js.map +1 -1
- package/dist/retry-policy.js +2 -2
- package/dist/retry-policy.js.map +1 -1
- package/dist/run-effect-coordinator.d.ts +17 -0
- package/dist/run-effect-coordinator.d.ts.map +1 -0
- package/dist/run-effect-coordinator.js +78 -0
- package/dist/run-effect-coordinator.js.map +1 -0
- package/dist/run-state.d.ts +22 -3
- package/dist/run-state.d.ts.map +1 -1
- package/dist/run-state.js +154 -21
- 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.d.ts +19 -5
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +266 -43
- package/dist/runtime.js.map +1 -1
- package/dist/session.d.ts +12 -2
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +455 -19
- package/dist/session.js.map +1 -1
- package/dist/snapshot.d.ts +25 -0
- package/dist/snapshot.d.ts.map +1 -0
- package/dist/snapshot.js +33 -0
- package/dist/snapshot.js.map +1 -0
- package/dist/trace.d.ts +4 -0
- package/dist/trace.d.ts.map +1 -1
- package/dist/trace.js +12 -8
- package/dist/trace.js.map +1 -1
- package/package.json +6 -6
package/dist/runtime.js
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { loadDirectorySkills, resolveSkills, SKILLS } from "coremind-templates";
|
|
4
|
-
import { buildTools } from "coremind-tools";
|
|
4
|
+
import { ArtifactStore, buildTools, extractArtifactRecord, wrapToolWithArtifactCapture, } from "coremind-tools";
|
|
5
5
|
import { buildAgent } from "./agent-factory.js";
|
|
6
6
|
import { RunBudgetController, resolveRuntimeLimits } from "./budget.js";
|
|
7
7
|
import { CheckpointManager, inspectCheckpoint as inspectStoredCheckpoint, restoreCheckpoint as restoreStoredCheckpoint, } from "./checkpoint.js";
|
|
8
|
+
import { assessRuntimeEngineeringEvidence, createToolExecutionEvidence, } from "./coding/runtime-engineering-evidence.js";
|
|
8
9
|
import { ContextProtector } from "./context.js";
|
|
9
10
|
import { CoreMindError } from "./errors.js";
|
|
10
11
|
import { extractAgentError, extractText } from "./events.js";
|
|
12
|
+
import { LifecycleExtensionHost, } from "./lifecycle-extension.js";
|
|
11
13
|
import { LoopRunner } from "./loop-runner.js";
|
|
14
|
+
import { DurableOperation, restoreDurableOperation, } from "./operation-state.js";
|
|
12
15
|
import { Orchestrator } from "./orchestrator.js";
|
|
13
16
|
import { buildProviderRuntime } from "./provider.js";
|
|
14
17
|
import { adaptCoreMindTool } from "./public-tool.js";
|
|
15
18
|
import { analyzeRunMetrics, assessReleaseReadiness, createEvaluationReport, } from "./result.js";
|
|
16
19
|
import { classifyRetry, runWithTransientRetry } from "./retry-policy.js";
|
|
20
|
+
import { RunEffectCoordinator } from "./run-effect-coordinator.js";
|
|
17
21
|
import { FileRunStore, fingerprintRunConfig, prepareRunResume, RunStateJournal, } from "./run-state.js";
|
|
18
22
|
import { RunTerminalizer } from "./run-terminalizer.js";
|
|
19
23
|
import { CoreMindSession } from "./session.js";
|
|
24
|
+
import { createRunSnapshot } from "./snapshot.js";
|
|
20
25
|
import { ToolPolicy } from "./tool-policy.js";
|
|
21
26
|
import { TraceRecorder } from "./trace.js";
|
|
22
27
|
/**
|
|
@@ -69,7 +74,8 @@ export class CoreMindRuntime {
|
|
|
69
74
|
const toolsByAgent = new Map();
|
|
70
75
|
const toolEffectsByAgent = new Map();
|
|
71
76
|
const skillsByAgent = new Map();
|
|
72
|
-
const
|
|
77
|
+
const artifactStore = new ArtifactStore({ cwd });
|
|
78
|
+
const externalTools = (options.toolDefinitions ?? []).map((definition) => wrapToolWithArtifactCapture(adaptCoreMindTool(definition), artifactStore));
|
|
73
79
|
// 自定义技能(生态机制):配置文件所在目录的 skills/ 下,每个子目录的 README.md 即一个技能
|
|
74
80
|
const customSkills = await loadDirectorySkills(path.join(configDir, "skills"));
|
|
75
81
|
for (const [name, agentCfg] of Object.entries(config.agents)) {
|
|
@@ -78,6 +84,7 @@ export class CoreMindRuntime {
|
|
|
78
84
|
cwd,
|
|
79
85
|
configDir,
|
|
80
86
|
env,
|
|
87
|
+
artifactStore,
|
|
81
88
|
});
|
|
82
89
|
for (const warning of warnings) {
|
|
83
90
|
emit({ type: "error", message: warning, fatal: false });
|
|
@@ -129,10 +136,11 @@ export class CoreMindRuntime {
|
|
|
129
136
|
return new CoreMindRuntime(config, agentConfigs, toolsByAgent, toolEffectsByAgent, providerRuntime, options, sessionMessages, resumedContextLength, skillsByAgent);
|
|
130
137
|
}
|
|
131
138
|
/** 按名字创建独立 Agent 实例(每次新实例,消息历史独立) */
|
|
132
|
-
createAgent(name, onEvent = this.options.events ?? (() => { })) {
|
|
139
|
+
createAgent(name, onEvent = this.options.events ?? (() => { }), stepId) {
|
|
133
140
|
const agentCfg = this.agentConfigs.get(name);
|
|
134
141
|
if (!agentCfg)
|
|
135
142
|
return undefined;
|
|
143
|
+
const harness = this.activeHarnessFactory?.(name, stepId);
|
|
136
144
|
const agent = buildAgent(agentCfg, {
|
|
137
145
|
models: this.providerRuntime.models,
|
|
138
146
|
model: this.providerRuntime.model,
|
|
@@ -143,8 +151,23 @@ export class CoreMindRuntime {
|
|
|
143
151
|
// 恢复视图只注入主 agent(会话归属者)
|
|
144
152
|
sessionMessages: name === this.mainAgentName ? this.sessionMessages : undefined,
|
|
145
153
|
skillsContent: this.skillsByAgent.get(name),
|
|
146
|
-
|
|
154
|
+
stableFacts: {
|
|
155
|
+
provider: this.providerRuntime.model.provider,
|
|
156
|
+
model: this.providerRuntime.model.id,
|
|
157
|
+
contextWindow: this.providerRuntime.model.contextWindow,
|
|
158
|
+
},
|
|
159
|
+
promptCacheStatus: this.providerRuntime.promptCacheStatus,
|
|
160
|
+
harness,
|
|
147
161
|
});
|
|
162
|
+
// 该停止钩子只属于 Runtime 内部 Harness,不扩大公开 AgentBuildContext 合同。
|
|
163
|
+
agent.shouldStopAfterTurn = harness?.shouldStopAfterTurn;
|
|
164
|
+
if (harness?.throwIfDenied) {
|
|
165
|
+
const waitForIdle = agent.waitForIdle.bind(agent);
|
|
166
|
+
agent.waitForIdle = async () => {
|
|
167
|
+
await waitForIdle();
|
|
168
|
+
harness.throwIfDenied?.();
|
|
169
|
+
};
|
|
170
|
+
}
|
|
148
171
|
this.lastAgents.set(name, agent);
|
|
149
172
|
return agent;
|
|
150
173
|
}
|
|
@@ -154,7 +177,9 @@ export class CoreMindRuntime {
|
|
|
154
177
|
}
|
|
155
178
|
/** 返回交互会话应继承的恢复消息副本。 */
|
|
156
179
|
initialMessagesFor(name) {
|
|
157
|
-
return name === this.mainAgentName
|
|
180
|
+
return (name === this.mainAgentName
|
|
181
|
+
? [...(this.sessionMessages ?? [])]
|
|
182
|
+
: []);
|
|
158
183
|
}
|
|
159
184
|
/**
|
|
160
185
|
* 把一轮交互对话作为完整 Run 执行。
|
|
@@ -184,6 +209,9 @@ export class CoreMindRuntime {
|
|
|
184
209
|
/** 执行:有 workflow 走编排,否则单 agent 直答。返回结果含质量摘要 */
|
|
185
210
|
async run() {
|
|
186
211
|
const started = performance.now();
|
|
212
|
+
const lifecycleHost = this.options.lifecycleExtensions
|
|
213
|
+
? new LifecycleExtensionHost(this.options.lifecycleExtensions)
|
|
214
|
+
: undefined;
|
|
187
215
|
const runStore = this.options.runStore ??
|
|
188
216
|
new FileRunStore(path.join(this.options.configDir, ".coremind", "runs"));
|
|
189
217
|
const configFingerprint = fingerprintRunConfig(this.config);
|
|
@@ -193,12 +221,30 @@ export class CoreMindRuntime {
|
|
|
193
221
|
const runId = resumePlan?.runId ?? randomUUID();
|
|
194
222
|
const effectiveInitialPrompt = resumePlan?.initialPrompt ?? this.options.initialPrompt;
|
|
195
223
|
const journal = new RunStateJournal(runId, runStore, resumePlan?.nextJournalSequence ?? 0);
|
|
224
|
+
const operation = resumePlan && resumePlan.operationRecords.length > 0
|
|
225
|
+
? restoreDurableOperation(resumePlan.operationRecords)
|
|
226
|
+
: DurableOperation.create({
|
|
227
|
+
runId,
|
|
228
|
+
operationId: randomUUID(),
|
|
229
|
+
eventId: randomUUID(),
|
|
230
|
+
});
|
|
231
|
+
const persistOperation = (event) => {
|
|
232
|
+
const transition = operation.transition(event);
|
|
233
|
+
if (transition.record)
|
|
234
|
+
journal.operation(transition.record);
|
|
235
|
+
};
|
|
236
|
+
const initialOperationState = operation.snapshot().state;
|
|
237
|
+
if (resumePlan &&
|
|
238
|
+
initialOperationState !== "accepted" &&
|
|
239
|
+
initialOperationState !== "running" &&
|
|
240
|
+
initialOperationState !== "paused") {
|
|
241
|
+
throw new CoreMindError("operation_not_resumable", `操作 ${operation.snapshot().operationId} 处于 ${initialOperationState},不能继续执行`);
|
|
242
|
+
}
|
|
196
243
|
if (resumePlan) {
|
|
197
244
|
journal.resume({
|
|
198
245
|
completedStepIds: [...resumePlan.completedSteps.keys()],
|
|
199
246
|
resumedAt: new Date().toISOString(),
|
|
200
247
|
});
|
|
201
|
-
await journal.flush();
|
|
202
248
|
}
|
|
203
249
|
else {
|
|
204
250
|
await journal.start({
|
|
@@ -207,48 +253,61 @@ export class CoreMindRuntime {
|
|
|
207
253
|
configFingerprint,
|
|
208
254
|
initialPrompt: this.options.initialPrompt,
|
|
209
255
|
sessionId: this.options.sessionId,
|
|
256
|
+
operationId: operation.snapshot().operationId,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
if (!resumePlan || resumePlan.operationRecords.length === 0) {
|
|
260
|
+
journal.operation(operation.records()[0]);
|
|
261
|
+
}
|
|
262
|
+
if (operation.snapshot().state === "running") {
|
|
263
|
+
persistOperation({
|
|
264
|
+
eventId: randomUUID(),
|
|
265
|
+
type: "PAUSE",
|
|
266
|
+
reason: "process_interrupted",
|
|
210
267
|
});
|
|
211
268
|
}
|
|
269
|
+
if (operation.snapshot().state === "accepted") {
|
|
270
|
+
persistOperation({ eventId: randomUUID(), type: "START" });
|
|
271
|
+
}
|
|
272
|
+
else if (operation.snapshot().state === "paused") {
|
|
273
|
+
persistOperation({ eventId: randomUUID(), type: "RESUME" });
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
throw new CoreMindError("operation_not_resumable", `操作 ${operation.snapshot().operationId} 处于 ${operation.snapshot().state},不能继续执行`);
|
|
277
|
+
}
|
|
278
|
+
await journal.flush();
|
|
212
279
|
const trace = new TraceRecorder(runId, (entry) => {
|
|
213
280
|
journal.event(entry);
|
|
214
281
|
this.options.trace?.(entry);
|
|
215
282
|
}, resumePlan?.previousTrace);
|
|
216
283
|
const collected = resumePlan?.previousTrace.map((entry) => entry.event) ?? [];
|
|
217
284
|
const userEvents = this.options.events ?? (() => { });
|
|
218
|
-
const
|
|
285
|
+
const extensionReceipts = [];
|
|
219
286
|
const recordEvent = (event) => {
|
|
220
287
|
collected.push(event);
|
|
221
288
|
trace.record(event);
|
|
222
289
|
userEvents(event);
|
|
223
290
|
};
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
idempotencyKey: effectIdempotencyKey(runId, enriched.stepId, enriched.callId),
|
|
242
|
-
tool: enriched.tool,
|
|
243
|
-
...(enriched.stepId ? { stepId: enriched.stepId } : {}),
|
|
244
|
-
};
|
|
245
|
-
activeToolEffects.delete(callKey);
|
|
246
|
-
recordEvent({
|
|
247
|
-
type: "effect_receipt",
|
|
248
|
-
...effect,
|
|
249
|
-
status: enriched.isError ? "unknown" : "committed",
|
|
291
|
+
const effectCoordinator = new RunEffectCoordinator(runId, recordEvent);
|
|
292
|
+
const emit = (event) => effectCoordinator.emit(event);
|
|
293
|
+
const dispatchLifecycle = async (lifecycle, payload) => {
|
|
294
|
+
if (!lifecycleHost)
|
|
295
|
+
return undefined;
|
|
296
|
+
const result = await lifecycleHost.dispatch(lifecycle, payload);
|
|
297
|
+
for (const receipt of result.receipts) {
|
|
298
|
+
extensionReceipts.push(receipt);
|
|
299
|
+
emit({
|
|
300
|
+
type: "extension_lifecycle",
|
|
301
|
+
extensionId: receipt.extensionId,
|
|
302
|
+
extensionVersion: receipt.extensionVersion,
|
|
303
|
+
lifecycle: receipt.event,
|
|
304
|
+
status: receipt.status,
|
|
305
|
+
durationMs: receipt.durationMs,
|
|
306
|
+
error: receipt.error,
|
|
307
|
+
denied: receipt.denied,
|
|
250
308
|
});
|
|
251
309
|
}
|
|
310
|
+
return result;
|
|
252
311
|
};
|
|
253
312
|
const workflow = this.config.workflow;
|
|
254
313
|
const loop = this.config.loop;
|
|
@@ -265,6 +324,7 @@ export class CoreMindRuntime {
|
|
|
265
324
|
runId: trace.runId,
|
|
266
325
|
});
|
|
267
326
|
let checkpointFailure;
|
|
327
|
+
const artifacts = [];
|
|
268
328
|
const checkpointByCallId = new Map();
|
|
269
329
|
const contextWindow = this.providerRuntime.model.contextWindow;
|
|
270
330
|
const contextProtector = new ContextProtector({
|
|
@@ -276,6 +336,9 @@ export class CoreMindRuntime {
|
|
|
276
336
|
beforeTokens: result.beforeTokens,
|
|
277
337
|
afterTokens: result.afterTokens,
|
|
278
338
|
removedMessages: result.removedMessages,
|
|
339
|
+
strategy: "deterministic-v1",
|
|
340
|
+
reason: "threshold",
|
|
341
|
+
summaryFingerprint: result.summaryFingerprint,
|
|
279
342
|
}), (failure) => emit({
|
|
280
343
|
type: "context_compaction_failed",
|
|
281
344
|
message: failure.message,
|
|
@@ -304,25 +367,67 @@ export class CoreMindRuntime {
|
|
|
304
367
|
decision,
|
|
305
368
|
}),
|
|
306
369
|
});
|
|
307
|
-
|
|
370
|
+
const deniedAgents = new Set();
|
|
371
|
+
this.activeHarnessFactory = (agentName, stepId) => ({
|
|
308
372
|
maxRetries: loop ? 0 : limits.maxRetries,
|
|
309
|
-
transformContext: async (messages) =>
|
|
373
|
+
transformContext: async (messages) => {
|
|
374
|
+
await dispatchLifecycle("before-model", {
|
|
375
|
+
runId,
|
|
376
|
+
agent: agentName,
|
|
377
|
+
stepId,
|
|
378
|
+
messageCount: messages.length,
|
|
379
|
+
});
|
|
380
|
+
return contextProtector.transform(messages);
|
|
381
|
+
},
|
|
310
382
|
beforeToolCall: async (context) => {
|
|
383
|
+
if (deniedAgents.has(agentName)) {
|
|
384
|
+
return {
|
|
385
|
+
block: true,
|
|
386
|
+
reason: "同一工具批次已有请求被拒绝",
|
|
387
|
+
terminate: true,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
311
390
|
const blockedByBudget = budget.beforeToolCall();
|
|
312
391
|
if (blockedByBudget)
|
|
313
392
|
return blockedByBudget;
|
|
314
393
|
const decision = await policy.authorize(agentName, context.toolCall.name, context.args, this.toolEffectsByAgent.get(agentName)?.get(context.toolCall.name));
|
|
315
394
|
if (!decision.allowed) {
|
|
395
|
+
deniedAgents.add(agentName);
|
|
316
396
|
emit({
|
|
317
397
|
type: "policy_denied",
|
|
318
398
|
agent: agentName,
|
|
319
399
|
tool: context.toolCall.name,
|
|
320
400
|
reason: decision.reason,
|
|
321
401
|
});
|
|
322
|
-
return { block: true, reason: decision.reason };
|
|
402
|
+
return { block: true, reason: decision.reason, terminate: true };
|
|
403
|
+
}
|
|
404
|
+
const extensionDecision = await dispatchLifecycle("before-tool", {
|
|
405
|
+
runId,
|
|
406
|
+
agent: agentName,
|
|
407
|
+
stepId,
|
|
408
|
+
tool: context.toolCall.name,
|
|
409
|
+
callId: context.toolCall.id,
|
|
410
|
+
args: context.args,
|
|
411
|
+
approvalAllowed: true,
|
|
412
|
+
});
|
|
413
|
+
if (extensionDecision?.denied) {
|
|
414
|
+
deniedAgents.add(agentName);
|
|
415
|
+
const reason = extensionDecision.denied.reason;
|
|
416
|
+
emit({
|
|
417
|
+
type: "policy_denied",
|
|
418
|
+
agent: agentName,
|
|
419
|
+
tool: context.toolCall.name,
|
|
420
|
+
reason,
|
|
421
|
+
});
|
|
422
|
+
return { block: true, reason, terminate: true };
|
|
323
423
|
}
|
|
324
424
|
try {
|
|
325
|
-
const
|
|
425
|
+
const idempotencyKey = effectIdempotencyKey(runId, stepId, context.toolCall.id);
|
|
426
|
+
const checkpoint = await checkpointManager.capture(context.toolCall.name, context.args, {
|
|
427
|
+
operationId: operation.snapshot().operationId,
|
|
428
|
+
toolCallId: context.toolCall.id,
|
|
429
|
+
idempotencyKey,
|
|
430
|
+
});
|
|
326
431
|
if (checkpoint) {
|
|
327
432
|
checkpointByCallId.set(context.toolCall.id, checkpoint.checkpointId);
|
|
328
433
|
journal.checkpoint(checkpoint);
|
|
@@ -330,6 +435,8 @@ export class CoreMindRuntime {
|
|
|
330
435
|
type: "checkpoint_created",
|
|
331
436
|
checkpointId: checkpoint.checkpointId,
|
|
332
437
|
tool: checkpoint.tool,
|
|
438
|
+
callId: context.toolCall.id,
|
|
439
|
+
idempotencyKey,
|
|
333
440
|
targetPath: checkpoint.targetPath,
|
|
334
441
|
reversible: checkpoint.reversible,
|
|
335
442
|
});
|
|
@@ -343,9 +450,41 @@ export class CoreMindRuntime {
|
|
|
343
450
|
emit({ type: "error", message: checkpointFailure.message, fatal: true });
|
|
344
451
|
return { block: true, reason: checkpointFailure.message };
|
|
345
452
|
}
|
|
453
|
+
effectCoordinator.markStarted(stepId, context.toolCall.id, context.toolCall.name);
|
|
346
454
|
return undefined;
|
|
347
455
|
},
|
|
348
456
|
afterToolCall: async (context) => {
|
|
457
|
+
const execution = createToolExecutionEvidence({
|
|
458
|
+
tool: context.toolCall.name,
|
|
459
|
+
args: context.args,
|
|
460
|
+
isError: context.isError,
|
|
461
|
+
result: context.result,
|
|
462
|
+
durationMs: effectCoordinator.consumeDuration(stepId, context.toolCall.id),
|
|
463
|
+
});
|
|
464
|
+
emit({
|
|
465
|
+
type: "tool_execution_evidence",
|
|
466
|
+
agent: agentName,
|
|
467
|
+
tool: context.toolCall.name,
|
|
468
|
+
callId: context.toolCall.id,
|
|
469
|
+
...(stepId ? { stepId } : {}),
|
|
470
|
+
execution,
|
|
471
|
+
});
|
|
472
|
+
const artifact = extractArtifactRecord(context.result.details);
|
|
473
|
+
if (artifact) {
|
|
474
|
+
artifacts.push(artifact);
|
|
475
|
+
emit({
|
|
476
|
+
type: "artifact_created",
|
|
477
|
+
artifactId: artifact.artifactId,
|
|
478
|
+
status: artifact.status,
|
|
479
|
+
sizeBytes: artifact.sizeBytes,
|
|
480
|
+
relativePath: artifact.relativePath,
|
|
481
|
+
sha256: artifact.sha256,
|
|
482
|
+
mediaType: artifact.mediaType,
|
|
483
|
+
redaction: artifact.redaction,
|
|
484
|
+
tool: context.toolCall.name,
|
|
485
|
+
callId: context.toolCall.id,
|
|
486
|
+
});
|
|
487
|
+
}
|
|
349
488
|
const checkpointId = checkpointByCallId.get(context.toolCall.id);
|
|
350
489
|
checkpointByCallId.delete(context.toolCall.id);
|
|
351
490
|
if (checkpointId) {
|
|
@@ -361,8 +500,24 @@ export class CoreMindRuntime {
|
|
|
361
500
|
}
|
|
362
501
|
}
|
|
363
502
|
const budgetResult = budget.afterToolCall(context.isError);
|
|
503
|
+
await dispatchLifecycle("after-tool", {
|
|
504
|
+
runId,
|
|
505
|
+
agent: agentName,
|
|
506
|
+
stepId,
|
|
507
|
+
tool: context.toolCall.name,
|
|
508
|
+
callId: context.toolCall.id,
|
|
509
|
+
isError: context.isError,
|
|
510
|
+
checkpointId,
|
|
511
|
+
artifactId: artifact?.artifactId,
|
|
512
|
+
});
|
|
364
513
|
return checkpointFailure ? { terminate: true } : budgetResult;
|
|
365
514
|
},
|
|
515
|
+
shouldStopAfterTurn: () => deniedAgents.has(agentName),
|
|
516
|
+
throwIfDenied: () => {
|
|
517
|
+
if (deniedAgents.has(agentName)) {
|
|
518
|
+
throw new CoreMindError("loop_paused", stepId ? `步骤 ${stepId} 的工具请求未获批准` : `Agent ${agentName} 的工具请求未获批准`);
|
|
519
|
+
}
|
|
520
|
+
},
|
|
366
521
|
onAgentEvent: (event, agent) => {
|
|
367
522
|
if (budget.observeAgentEvent(event))
|
|
368
523
|
agent.abort();
|
|
@@ -390,6 +545,13 @@ export class CoreMindRuntime {
|
|
|
390
545
|
journal.loop(snapshot);
|
|
391
546
|
await journal.flush();
|
|
392
547
|
},
|
|
548
|
+
verifyEvidence: loop.verify.evidence
|
|
549
|
+
? ({ stepId, textPassed }) => {
|
|
550
|
+
const report = assessRuntimeEngineeringEvidence(collected, loop.verify.evidence, stepId);
|
|
551
|
+
emit({ type: "engineering_evidence", stepId, textPassed, ...report });
|
|
552
|
+
return report.passed;
|
|
553
|
+
}
|
|
554
|
+
: undefined,
|
|
393
555
|
executeStep: (request) => runWithTransientRetry(() => this.executeLoopStep(request, emit, limits.stepTimeoutMs, collected), {
|
|
394
556
|
maxRetries: Math.max(0, limits.maxRetries - retryCount),
|
|
395
557
|
signal: this.options.signal,
|
|
@@ -415,7 +577,7 @@ export class CoreMindRuntime {
|
|
|
415
577
|
}
|
|
416
578
|
if (workflow && workflow.length > 0) {
|
|
417
579
|
const orchestrator = new Orchestrator(workflow, {
|
|
418
|
-
createAgent: (name, stepId) => this.createAgent(name, (event) => emit(stepId ? { ...event, stepId } : event)),
|
|
580
|
+
createAgent: (name, stepId) => this.createAgent(name, (event) => emit(stepId ? { ...event, stepId } : event), stepId),
|
|
419
581
|
events: emit,
|
|
420
582
|
initialPrompt: effectiveInitialPrompt,
|
|
421
583
|
signal: this.options.signal,
|
|
@@ -493,8 +655,50 @@ export class CoreMindRuntime {
|
|
|
493
655
|
if (irreversibleTools.length > 0) {
|
|
494
656
|
releaseReadiness.warnings.push(`存在不可自动回退的工具执行:${irreversibleTools.join("、")};请结合 Trace 和外部系统记录复核`);
|
|
495
657
|
}
|
|
658
|
+
if (outcome.status === "paused") {
|
|
659
|
+
persistOperation({
|
|
660
|
+
eventId: randomUUID(),
|
|
661
|
+
type: "PAUSE",
|
|
662
|
+
reason: outcome.finishReason,
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
if (outcome.status === "aborted") {
|
|
667
|
+
persistOperation({
|
|
668
|
+
eventId: randomUUID(),
|
|
669
|
+
type: "REQUEST_ABORT",
|
|
670
|
+
reason: outcome.finishReason,
|
|
671
|
+
});
|
|
672
|
+
persistOperation({
|
|
673
|
+
eventId: randomUUID(),
|
|
674
|
+
type: "FAIL",
|
|
675
|
+
reason: outcome.finishReason,
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
else if (outcome.status === "succeeded") {
|
|
679
|
+
persistOperation({ eventId: randomUUID(), type: "COMPLETE" });
|
|
680
|
+
}
|
|
681
|
+
else {
|
|
682
|
+
persistOperation({
|
|
683
|
+
eventId: randomUUID(),
|
|
684
|
+
type: "FAIL",
|
|
685
|
+
reason: outcome.finishReason,
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
await dispatchLifecycle("run-finished", {
|
|
690
|
+
runId,
|
|
691
|
+
operation: operation.snapshot(),
|
|
692
|
+
outcome,
|
|
693
|
+
metrics,
|
|
694
|
+
evaluation,
|
|
695
|
+
releaseReadiness,
|
|
696
|
+
checkpointCount: checkpointManager.records.length,
|
|
697
|
+
artifactCount: artifacts.length,
|
|
698
|
+
});
|
|
496
699
|
if (outcome.status === "paused") {
|
|
497
700
|
journal.pause({
|
|
701
|
+
operation: operation.snapshot(),
|
|
498
702
|
outcome,
|
|
499
703
|
metrics,
|
|
500
704
|
evaluation,
|
|
@@ -503,11 +707,30 @@ export class CoreMindRuntime {
|
|
|
503
707
|
});
|
|
504
708
|
}
|
|
505
709
|
else {
|
|
506
|
-
journal.finish({
|
|
710
|
+
journal.finish({
|
|
711
|
+
operation: operation.snapshot(),
|
|
712
|
+
outcome,
|
|
713
|
+
metrics,
|
|
714
|
+
evaluation,
|
|
715
|
+
releaseReadiness,
|
|
716
|
+
});
|
|
507
717
|
}
|
|
508
718
|
await journal.flush();
|
|
719
|
+
const snapshot = createRunSnapshot({
|
|
720
|
+
runId: trace.runId,
|
|
721
|
+
operation: operation.snapshot(),
|
|
722
|
+
outcome,
|
|
723
|
+
metrics,
|
|
724
|
+
evaluation,
|
|
725
|
+
releaseReadiness,
|
|
726
|
+
trace: trace.entries,
|
|
727
|
+
checkpoints: checkpointManager.records,
|
|
728
|
+
artifacts,
|
|
729
|
+
extensions: extensionReceipts,
|
|
730
|
+
});
|
|
509
731
|
return {
|
|
510
732
|
runId: trace.runId,
|
|
733
|
+
operation: operation.snapshot(),
|
|
511
734
|
outcome,
|
|
512
735
|
metrics,
|
|
513
736
|
evaluation,
|
|
@@ -519,6 +742,9 @@ export class CoreMindRuntime {
|
|
|
519
742
|
messages: this.collectMessages(),
|
|
520
743
|
transcript,
|
|
521
744
|
sessionFile,
|
|
745
|
+
artifacts,
|
|
746
|
+
extensions: extensionReceipts,
|
|
747
|
+
snapshot,
|
|
522
748
|
};
|
|
523
749
|
}
|
|
524
750
|
async runWithGuard(timeoutMs, operation, onGuardError) {
|
|
@@ -565,7 +791,7 @@ export class CoreMindRuntime {
|
|
|
565
791
|
}
|
|
566
792
|
async executeLoopStep(request, emit, timeoutMs, collected) {
|
|
567
793
|
const eventCursor = collected.length;
|
|
568
|
-
const agent = this.createAgent(request.agent, (event) => emit({ ...event, stepId: request.stepId }));
|
|
794
|
+
const agent = this.createAgent(request.agent, (event) => emit({ ...event, stepId: request.stepId }), request.stepId);
|
|
569
795
|
if (!agent) {
|
|
570
796
|
throw new CoreMindError("unknown_agent", `Loop 引用了未定义的 agent:${request.agent}(${request.stepId})`);
|
|
571
797
|
}
|
|
@@ -665,9 +891,6 @@ function lastOutputText(outputs) {
|
|
|
665
891
|
function firstKey(record) {
|
|
666
892
|
return Object.keys(record)[0];
|
|
667
893
|
}
|
|
668
|
-
function effectCallKey(stepId, callId) {
|
|
669
|
-
return `${stepId ?? "agent"}:${callId}`;
|
|
670
|
-
}
|
|
671
894
|
function effectIdempotencyKey(runId, stepId, callId) {
|
|
672
895
|
return stepId ? `${runId}:${stepId}:${callId}` : `${runId}:${callId}`;
|
|
673
896
|
}
|