coremind-runtime 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/chat-session.d.ts +14 -3
- package/dist/chat-session.d.ts.map +1 -1
- package/dist/chat-session.js +36 -4
- package/dist/chat-session.js.map +1 -1
- package/dist/dependency-adapter.js +2 -2
- package/dist/evaluation-graders.js +9 -10
- package/dist/evaluation-graders.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +1 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +1 -0
- package/dist/internal.js.map +1 -1
- package/dist/orchestrator.d.ts.map +1 -1
- package/dist/orchestrator.js +40 -23
- package/dist/orchestrator.js.map +1 -1
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +19 -4
- package/dist/provider.js.map +1 -1
- package/dist/run-state.d.ts +8 -1
- package/dist/run-state.d.ts.map +1 -1
- package/dist/run-state.js +10 -1
- package/dist/run-state.js.map +1 -1
- package/dist/runtime.d.ts +5 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +193 -12
- package/dist/runtime.js.map +1 -1
- package/dist/session-tree.d.ts +51 -0
- package/dist/session-tree.d.ts.map +1 -0
- package/dist/session-tree.js +14 -0
- package/dist/session-tree.js.map +1 -0
- package/dist/session-v4/session/jsonl/repo.d.ts +2 -0
- package/dist/session-v4/session/jsonl/repo.d.ts.map +1 -1
- package/dist/session-v4/session/jsonl/repo.js +5 -0
- package/dist/session-v4/session/jsonl/repo.js.map +1 -1
- package/dist/session-write-lease.d.ts +2 -0
- package/dist/session-write-lease.d.ts.map +1 -1
- package/dist/session-write-lease.js +24 -0
- package/dist/session-write-lease.js.map +1 -1
- package/dist/session.d.ts +12 -2
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +159 -13
- package/dist/session.js.map +1 -1
- package/dist/task-calls.d.ts +126 -0
- package/dist/task-calls.d.ts.map +1 -0
- package/dist/task-calls.js +187 -0
- package/dist/task-calls.js.map +1 -0
- package/dist/task-commit.d.ts +82 -0
- package/dist/task-commit.d.ts.map +1 -0
- package/dist/task-commit.js +364 -0
- package/dist/task-commit.js.map +1 -0
- package/dist/task-session.d.ts +108 -0
- package/dist/task-session.d.ts.map +1 -0
- package/dist/task-session.js +688 -0
- package/dist/task-session.js.map +1 -0
- package/dist/task-tool-driver.d.ts +10 -0
- package/dist/task-tool-driver.d.ts.map +1 -0
- package/dist/task-tool-driver.js +108 -0
- package/dist/task-tool-driver.js.map +1 -0
- package/dist/tool-policy.d.ts +7 -0
- package/dist/tool-policy.d.ts.map +1 -1
- package/dist/tool-policy.js +98 -16
- package/dist/tool-policy.js.map +1 -1
- package/package.json +10 -7
package/dist/runtime.js
CHANGED
|
@@ -17,7 +17,7 @@ import { projectContextTaskState } from "./context-task-state.js";
|
|
|
17
17
|
import { ControlInbox, } from "./control-inbox.js";
|
|
18
18
|
import { buildDelegatedInitialPrompt, resolveDelegatedContextReferences, } from "./delegated-context.js";
|
|
19
19
|
import { createDelegationAgentTool, createDelegationDispositionAgentTool, DELEGATION_DISPOSITION_TOOL_CAPABILITY, DELEGATION_DISPOSITION_TOOL_EFFECT, DELEGATION_DISPOSITION_TOOL_NAME, DELEGATION_TOOL_CAPABILITY, DELEGATION_TOOL_EFFECT, DELEGATION_TOOL_NAME, parseDelegationDispositionToolArgs, parseDelegationToolArgs, resolveDelegationAllocation, resolveDelegationHierarchyLimits, } from "./delegation-tool.js";
|
|
20
|
-
import { createEffectReceiptBinding, fingerprintEffectReceiptValue, } from "./effect-receipt-binding.js";
|
|
20
|
+
import { createEffectReceiptBinding, fingerprintEffectReceiptValue, projectEffectReceiptBindings, } from "./effect-receipt-binding.js";
|
|
21
21
|
import { CoreMindError, isErrorCode } from "./errors.js";
|
|
22
22
|
import { extractAgentError, extractText } from "./events.js";
|
|
23
23
|
import { normalizeExecutionError } from "./execution-error.js";
|
|
@@ -41,14 +41,17 @@ import { classifyRetry, runWithTransientRetry } from "./retry-policy.js";
|
|
|
41
41
|
import { RunContext } from "./run-context.js";
|
|
42
42
|
import { RunEffectCoordinator } from "./run-effect-coordinator.js";
|
|
43
43
|
import { RunKernel } from "./run-kernel.js";
|
|
44
|
-
import { FileRunStore, fingerprintRunConfig, RunStateJournal, } from "./run-state.js";
|
|
44
|
+
import { FileRunStore, fingerprintRunConfig, isManagedRunPath, RunStateJournal, } from "./run-state.js";
|
|
45
45
|
import { RunTerminalizer } from "./run-terminalizer.js";
|
|
46
46
|
import { registerCoreMindRuntimeInstance } from "./runtime-instance-authority.js";
|
|
47
|
-
import { CoreMindSession, copySessionHistory, historySessionSnapshot, openWritableSession, rememberSessionHistory, sessionSnapshot, } from "./session.js";
|
|
47
|
+
import { CoreMindSession, copySessionHistory, historySessionSnapshot, isManagedSessionPath, openWritableSession, rememberSessionHistory, sessionSnapshot, } from "./session.js";
|
|
48
48
|
import { compactSessionInRun } from "./session-maintenance.js";
|
|
49
|
-
import {
|
|
49
|
+
import { bindSessionNavigation, } from "./session-tree.js";
|
|
50
|
+
import { assertJsonSerializable } from "./session-v4/session/session.js";
|
|
51
|
+
import { acquireSessionWriteLease, withSessionWriteLease, } from "./session-write-lease.js";
|
|
50
52
|
import { assertSkillSnapshotSize, freezeSkillSnapshot, restoreSkillSnapshot, SKILL_TOOL_CAPABILITY, SKILL_TOOL_EFFECT, SKILL_TOOL_NAME, SkillExecution, skillPrefixContents, } from "./skill-runtime.js";
|
|
51
53
|
import { createRunSnapshot } from "./snapshot.js";
|
|
54
|
+
import { buildTaskToolDriver } from "./task-tool-driver.js";
|
|
52
55
|
import { ToolExecutionEngine } from "./tool-call-lifecycle.js";
|
|
53
56
|
import { toolCapabilityCallKey } from "./tool-capability-identity.js";
|
|
54
57
|
import { DISCOVER_TOOLS_NAME, restoreToolDiscovery, restoreToolDiscoveryContinuations, ToolDiscoveryExecution, } from "./tool-discovery.js";
|
|
@@ -111,6 +114,16 @@ export class CoreMindRuntime {
|
|
|
111
114
|
this.resumedContextLength = resumedContextLength;
|
|
112
115
|
this.mainAgentName = config.defaultAgent ?? firstKey(config.agents) ?? "";
|
|
113
116
|
this.skillsByAgent = skillsByAgent;
|
|
117
|
+
bindSessionNavigation(this, (agentName, history, operation) => navigateRuntimeSession(this, agentName, history, operation));
|
|
118
|
+
const slot = executionSlotFor(this);
|
|
119
|
+
slot.sessionAgent = this.mainAgentName;
|
|
120
|
+
if (options.sessionId && config.session?.enabled) {
|
|
121
|
+
slot.sessionOptions = {
|
|
122
|
+
dir: sessionDir(config, options.configDir),
|
|
123
|
+
sessionId: options.sessionId,
|
|
124
|
+
cwd: options.cwd ?? process.cwd(),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
114
127
|
// 这些读取只用于保留已冻结的私有声明布局;执行状态已迁移到 RunContext。
|
|
115
128
|
void this.lastAgents;
|
|
116
129
|
void this.activeHarnessFactory;
|
|
@@ -284,9 +297,21 @@ export class CoreMindRuntime {
|
|
|
284
297
|
});
|
|
285
298
|
}
|
|
286
299
|
skillsByAgent.set(name, allContents);
|
|
287
|
-
driverBuilders.set(name, ({ onEvent, harness, sessionMessages, skillTool, mcpTools, discoveryTool }) => {
|
|
300
|
+
driverBuilders.set(name, ({ onEvent, harness, sessionMessages, skillTool, mcpTools, discoveryTool, taskTool }) => {
|
|
288
301
|
for (const tool of driverTools)
|
|
289
302
|
harness?.registerToolSource?.(tool.name, configuredToolSource(tool));
|
|
303
|
+
if (taskTool)
|
|
304
|
+
return buildTaskToolDriver({
|
|
305
|
+
tools: [
|
|
306
|
+
...driverTools,
|
|
307
|
+
...(mcpTools ?? []),
|
|
308
|
+
...(skillTool ? [skillTool] : []),
|
|
309
|
+
...(discoveryTool ? [discoveryTool] : []),
|
|
310
|
+
],
|
|
311
|
+
agentName: name,
|
|
312
|
+
harness,
|
|
313
|
+
onEvent,
|
|
314
|
+
}, taskTool);
|
|
290
315
|
return buildAgentDriver(agentCfg, {
|
|
291
316
|
models: providerRuntime.models,
|
|
292
317
|
model: agentModel,
|
|
@@ -334,7 +359,7 @@ export class CoreMindRuntime {
|
|
|
334
359
|
return runtime;
|
|
335
360
|
}
|
|
336
361
|
/** 按名字创建独立 Driver 实例(每次新实例,消息历史独立) */
|
|
337
|
-
async createAgent(name, onEvent = this.options.events ?? (() => { }), stepId, signal) {
|
|
362
|
+
async createAgent(name, onEvent = this.options.events ?? (() => { }), stepId, signal, taskTool) {
|
|
338
363
|
const buildDriver = this.driverBuilders.get(name);
|
|
339
364
|
if (!buildDriver)
|
|
340
365
|
return undefined;
|
|
@@ -348,16 +373,22 @@ export class CoreMindRuntime {
|
|
|
348
373
|
const harness = {
|
|
349
374
|
...originalHarness,
|
|
350
375
|
beforePrompt: async () => {
|
|
376
|
+
signal?.throwIfAborted();
|
|
351
377
|
if (originalHarness?.beforePrompt)
|
|
352
378
|
await originalHarness.beforePrompt();
|
|
353
379
|
if (name === this.mainAgentName) {
|
|
354
380
|
await context.currentControlInbox()?.applyPending("steering");
|
|
355
381
|
await context.currentControlInbox()?.applyPending("follow_up");
|
|
356
382
|
}
|
|
383
|
+
signal?.throwIfAborted();
|
|
357
384
|
if (this.options.signal?.aborted || context.currentJournal()?.isAborted()) {
|
|
358
385
|
throw new CoreMindError("aborted", "控制恢复完成前运行已取消");
|
|
359
386
|
}
|
|
360
387
|
},
|
|
388
|
+
beforeModelRequest: () => {
|
|
389
|
+
signal?.throwIfAborted();
|
|
390
|
+
originalHarness?.beforeModelRequest?.();
|
|
391
|
+
},
|
|
361
392
|
onObservation: (observation) => originalHarness?.onObservation?.(observation.type === "control_consumed"
|
|
362
393
|
? { ...observation, turnId: turnTracker.ensureCurrentTurn(name) }
|
|
363
394
|
: observation),
|
|
@@ -372,6 +403,7 @@ export class CoreMindRuntime {
|
|
|
372
403
|
mcpTools,
|
|
373
404
|
skillTool: executionSlotFor(this).skillExecutions?.get(executionId)?.tool,
|
|
374
405
|
discoveryTool: originalHarness?.discoveryTool,
|
|
406
|
+
taskTool,
|
|
375
407
|
});
|
|
376
408
|
context.registerAgent(name, agent, executionId);
|
|
377
409
|
return agent;
|
|
@@ -432,7 +464,7 @@ export class CoreMindRuntime {
|
|
|
432
464
|
if (this.config.loop?.verify.mode === "host") {
|
|
433
465
|
throw new CoreMindError("loop_config_invalid", "宿主验收模式须通过 run() 与绑定该 Run 的控制接口执行");
|
|
434
466
|
}
|
|
435
|
-
if (executionSlotFor(this).running) {
|
|
467
|
+
if (executionSlotFor(this).running || executionSlotFor(this).navigatingSession) {
|
|
436
468
|
throw new CoreMindError("concurrent_run", "同一 Runtime 不支持并发 run() 与交互 Turn");
|
|
437
469
|
}
|
|
438
470
|
if (!this.agentConfigs.has(agentName)) {
|
|
@@ -497,7 +529,7 @@ export class CoreMindRuntime {
|
|
|
497
529
|
/** 执行:有 workflow 走编排,否则单 agent 直答。返回结果含质量摘要 */
|
|
498
530
|
async run() {
|
|
499
531
|
const slot = executionSlotFor(this);
|
|
500
|
-
if (slot.running || boundDelegationExecutor(this)) {
|
|
532
|
+
if (slot.running || slot.navigatingSession || boundDelegationExecutor(this)) {
|
|
501
533
|
throw new CoreMindError("concurrent_run", "同一 Runtime 不支持并发执行");
|
|
502
534
|
}
|
|
503
535
|
slot.running = true;
|
|
@@ -906,6 +938,10 @@ export class CoreMindRuntime {
|
|
|
906
938
|
const resumeRecords = this.options.resumeRunId
|
|
907
939
|
? await runStore.read(this.options.resumeRunId)
|
|
908
940
|
: undefined;
|
|
941
|
+
const taskProgram = executionSlotFor(this).taskProgram;
|
|
942
|
+
const storedProgram = resumeRecords?.find((record) => record.kind === "start")?.payload?.taskProgram;
|
|
943
|
+
if (resumeRecords && storedProgram !== taskProgram?.id)
|
|
944
|
+
throw new CoreMindError("resume_config_mismatch", "任务调用 Run 必须由原任务作用域恢复");
|
|
909
945
|
const skillSnapshot = executionSlotFor(this).skills;
|
|
910
946
|
if (resumeRecords &&
|
|
911
947
|
restoreSkillSnapshot(resumeRecords, this.config.agents)?.sha256 !== skillSnapshot?.sha256) {
|
|
@@ -992,6 +1028,7 @@ export class CoreMindRuntime {
|
|
|
992
1028
|
schemaVersion: this.config.schemaVersion,
|
|
993
1029
|
configFingerprint,
|
|
994
1030
|
initialPrompt: this.options.initialPrompt,
|
|
1031
|
+
...(taskProgram ? { taskProgram: taskProgram.id } : {}),
|
|
995
1032
|
sessionId: this.options.sessionId,
|
|
996
1033
|
...(sessionSeqStart !== undefined
|
|
997
1034
|
? { sessionSeqStart, turnSeqStart: sessionSeqStart }
|
|
@@ -1293,6 +1330,12 @@ export class CoreMindRuntime {
|
|
|
1293
1330
|
});
|
|
1294
1331
|
const workspaceLeaseService = this.workspaceLeaseService;
|
|
1295
1332
|
const workspaceLeaseByCall = new Map();
|
|
1333
|
+
// 原生文件 Store 的租约关键 Fact 已同步完整前缀;自定义实现仍保留全局屏障。
|
|
1334
|
+
const leaseFlushDurability = Object.getPrototypeOf(runStore) === FileRunStore.prototype &&
|
|
1335
|
+
runStore.commit === FileRunStore.prototype.commit &&
|
|
1336
|
+
runStore.barrier === FileRunStore.prototype.barrier
|
|
1337
|
+
? "ordinary"
|
|
1338
|
+
: "critical";
|
|
1296
1339
|
const releaseWorkspaceLease = async (identity) => {
|
|
1297
1340
|
const callKey = toolCapabilityCallKey(identity.agent, identity.stepId, identity.callId);
|
|
1298
1341
|
const held = workspaceLeaseByCall.get(callKey);
|
|
@@ -1321,7 +1364,7 @@ export class CoreMindRuntime {
|
|
|
1321
1364
|
callId: identity.callId,
|
|
1322
1365
|
...(identity.stepId ? { stepId: identity.stepId } : {}),
|
|
1323
1366
|
});
|
|
1324
|
-
await journal.flush(
|
|
1367
|
+
await journal.flush(leaseFlushDurability);
|
|
1325
1368
|
};
|
|
1326
1369
|
const rollbackWorkspaceLease = async (identity) => {
|
|
1327
1370
|
const callKey = toolCapabilityCallKey(identity.agent, identity.stepId, identity.callId);
|
|
@@ -1344,7 +1387,7 @@ export class CoreMindRuntime {
|
|
|
1344
1387
|
callId: identity.callId,
|
|
1345
1388
|
...(identity.stepId ? { stepId: identity.stepId } : {}),
|
|
1346
1389
|
});
|
|
1347
|
-
await journal.flush(
|
|
1390
|
+
await journal.flush(leaseFlushDurability);
|
|
1348
1391
|
};
|
|
1349
1392
|
onToolResultRecorded = (event) => {
|
|
1350
1393
|
const recorded = toolExecutionEngine.inspect({
|
|
@@ -1538,6 +1581,29 @@ export class CoreMindRuntime {
|
|
|
1538
1581
|
const policy = new ToolPolicy({
|
|
1539
1582
|
permissions: this.config.permissions,
|
|
1540
1583
|
allowedPaths: this.options.childRunAuthority?.inheritedPolicy.permissions.paths,
|
|
1584
|
+
protectedPaths: [
|
|
1585
|
+
path.resolve(this.options.configDir, ".coremind", "runs"),
|
|
1586
|
+
path.resolve(this.options.configDir, ".coremind", "tasks"),
|
|
1587
|
+
path.resolve(this.options.cwd ?? process.cwd(), ".coremind", "tasks"),
|
|
1588
|
+
path.resolve(this.options.configDir, ".coremind", "checkpoints"),
|
|
1589
|
+
path.resolve(this.options.configDir, ".coremind", "quality-overrides.jsonl"),
|
|
1590
|
+
path.resolve(this.options.cwd ?? process.cwd(), ".coremind", "quality-overrides.jsonl"),
|
|
1591
|
+
path.resolve(this.options.cwd ?? process.cwd(), ".coremind", "artifacts"),
|
|
1592
|
+
path.resolve(this.options.cwd ?? process.cwd(), ".coremind", "leases"),
|
|
1593
|
+
...(runStore instanceof FileRunStore
|
|
1594
|
+
? [{ path: path.resolve(runStore.directory), contains: isManagedRunPath }]
|
|
1595
|
+
: runStore.pathFor
|
|
1596
|
+
? [path.resolve(runStore.pathFor(runId))]
|
|
1597
|
+
: []),
|
|
1598
|
+
...(this.config.session?.enabled
|
|
1599
|
+
? [
|
|
1600
|
+
{
|
|
1601
|
+
path: sessionDir(this.config, this.options.configDir),
|
|
1602
|
+
contains: isManagedSessionPath,
|
|
1603
|
+
},
|
|
1604
|
+
]
|
|
1605
|
+
: []),
|
|
1606
|
+
],
|
|
1541
1607
|
cwd: this.options.cwd ?? process.cwd(),
|
|
1542
1608
|
runId: trace.runId,
|
|
1543
1609
|
approve: this.options.approveTool,
|
|
@@ -1571,6 +1637,7 @@ export class CoreMindRuntime {
|
|
|
1571
1637
|
});
|
|
1572
1638
|
const deniedAgents = new Set();
|
|
1573
1639
|
const activeRunContext = context;
|
|
1640
|
+
let modelRequestsOpen = true;
|
|
1574
1641
|
context.setHarnessFactory((agentName, stepId, executionId) => {
|
|
1575
1642
|
const agentModel = this.agentModels.get(agentName) ?? this.providerRuntime.model;
|
|
1576
1643
|
const skillExecution = skillSnapshot?.agents[agentName]?.standard.length
|
|
@@ -1622,6 +1689,11 @@ export class CoreMindRuntime {
|
|
|
1622
1689
|
if (facts.state === "poisoned") {
|
|
1623
1690
|
throw new CoreMindError("fact_ledger_poisoned", `事实账本已失效,不能继续模型请求:${facts.reason}`);
|
|
1624
1691
|
}
|
|
1692
|
+
if (!modelRequestsOpen || facts.terminal) {
|
|
1693
|
+
throw new CoreMindError("run_already_finished", "Run 已封闭,不能继续模型请求");
|
|
1694
|
+
}
|
|
1695
|
+
if (journal.isAborted())
|
|
1696
|
+
throw new CoreMindError("aborted", "Run 已取消,不能继续模型请求");
|
|
1625
1697
|
throwIfContextFailed();
|
|
1626
1698
|
throwIfDelegationBlocksModel();
|
|
1627
1699
|
for (const name of discovery?.current?.names ?? [])
|
|
@@ -1951,7 +2023,8 @@ export class CoreMindRuntime {
|
|
|
1951
2023
|
return { block: true, reason: delegationPolicyFailure.message, terminate: true };
|
|
1952
2024
|
}
|
|
1953
2025
|
}
|
|
1954
|
-
const decision = await policy.authorize(agentName, context.toolCall.tool, authorizationArgs, resolvedCapability, executionEffects.get(context.toolCall.tool), delegationInputFingerprint, { callId: context.toolCall.callId, ...(stepId ? { stepId } : {}) });
|
|
2026
|
+
const decision = await policy.authorize(agentName, context.toolCall.tool, authorizationArgs, resolvedCapability, executionEffects.get(context.toolCall.tool), delegationInputFingerprint, { callId: context.toolCall.callId, ...(stepId ? { stepId } : {}), signal });
|
|
2027
|
+
signal?.throwIfAborted();
|
|
1955
2028
|
await toolExecutionEngine.advance(lifecycleIdentity, {
|
|
1956
2029
|
phase: "policy_resolved",
|
|
1957
2030
|
status: "completed",
|
|
@@ -2066,7 +2139,7 @@ export class CoreMindRuntime {
|
|
|
2066
2139
|
callId: context.toolCall.callId,
|
|
2067
2140
|
...(stepId ? { stepId } : {}),
|
|
2068
2141
|
});
|
|
2069
|
-
await journal.flush(
|
|
2142
|
+
await journal.flush(leaseFlushDurability);
|
|
2070
2143
|
await toolExecutionEngine.advance(lifecycleIdentity, {
|
|
2071
2144
|
phase: "lease_acquired",
|
|
2072
2145
|
status: "completed",
|
|
@@ -2665,6 +2738,67 @@ export class CoreMindRuntime {
|
|
|
2665
2738
|
}
|
|
2666
2739
|
throw deferredTerminalError;
|
|
2667
2740
|
}
|
|
2741
|
+
if (taskProgram) {
|
|
2742
|
+
if (loop || this.config.workflow)
|
|
2743
|
+
throw new CoreMindError("invalid_config", "精确任务调用不能替代已配置的 workflow/loop 验收");
|
|
2744
|
+
const name = this.mainAgentName;
|
|
2745
|
+
await taskProgram.execute(async (stepId, tool, args) => {
|
|
2746
|
+
if (journal.isAborted())
|
|
2747
|
+
throw new CoreMindError("aborted", "任务 Run 已停止");
|
|
2748
|
+
const completed = resumePlan?.completedSteps.get(stepId);
|
|
2749
|
+
if (completed)
|
|
2750
|
+
return JSON.parse(completed.output.text);
|
|
2751
|
+
const start = { type: "step_start", stepId, kind: "task_tool" };
|
|
2752
|
+
budget.observeAgentEvent(start);
|
|
2753
|
+
budget.throwIfExceeded();
|
|
2754
|
+
emit(start);
|
|
2755
|
+
let result;
|
|
2756
|
+
const agent = await this.createAgent(name, (event) => emit({ ...event, stepId }), stepId, this.options.signal, {
|
|
2757
|
+
tool,
|
|
2758
|
+
args,
|
|
2759
|
+
accept: (value) => {
|
|
2760
|
+
result = value;
|
|
2761
|
+
},
|
|
2762
|
+
});
|
|
2763
|
+
if (!agent)
|
|
2764
|
+
throw new CoreMindError("unknown_agent", "任务默认 Agent 不存在");
|
|
2765
|
+
await agent.prompt("任务工具调用");
|
|
2766
|
+
await agent.waitForIdle();
|
|
2767
|
+
while (lifecycleFinalizers.size > 0)
|
|
2768
|
+
await Promise.all([...lifecycleFinalizers.values()]);
|
|
2769
|
+
if (lifecycleFailure)
|
|
2770
|
+
throw lifecycleFailure;
|
|
2771
|
+
if (checkpointFailure)
|
|
2772
|
+
throw checkpointFailure;
|
|
2773
|
+
const adapterFailure = selectedAdapterFailure();
|
|
2774
|
+
if (adapterFailure)
|
|
2775
|
+
throw adapterFailure;
|
|
2776
|
+
budget.throwIfExceeded();
|
|
2777
|
+
if (journal.isAborted() || !result)
|
|
2778
|
+
throw new CoreMindError("aborted", "任务调用未收敛");
|
|
2779
|
+
const receipts = projectEffectReceiptBindings(collected).filter((item) => item.binding?.stepId === stepId);
|
|
2780
|
+
const committed = new Set(receipts
|
|
2781
|
+
.filter((item) => item.status === "committed")
|
|
2782
|
+
.map((item) => item.idempotencyKey));
|
|
2783
|
+
for (const event of [...collected].reverse()) {
|
|
2784
|
+
if (event.type === "tool_attempt" &&
|
|
2785
|
+
event.stepId === stepId &&
|
|
2786
|
+
committed.has(receiptId(runId, stepId, event.callId)))
|
|
2787
|
+
committed.add(event.previousReceiptId);
|
|
2788
|
+
}
|
|
2789
|
+
if (receipts.some((item) => !committed.has(item.idempotencyKey)))
|
|
2790
|
+
throw new CoreMindError("tool_execution_failed", "任务工具存在尚未收敛的副作用回执");
|
|
2791
|
+
// 先保存完整结果,再交给任务账本;此间崩溃由同一 Run 的整步结果恢复。
|
|
2792
|
+
const durable = { content: result.content, details: result.details ?? null };
|
|
2793
|
+
assertJsonSerializable(durable);
|
|
2794
|
+
const text = JSON.stringify(durable);
|
|
2795
|
+
emit({ type: "step_output", stepId, agent: name, text });
|
|
2796
|
+
emit({ type: "step_end", stepId, ok: true });
|
|
2797
|
+
await journal.flush("critical");
|
|
2798
|
+
return JSON.parse(text);
|
|
2799
|
+
});
|
|
2800
|
+
return { outputs: new Map(), transcript: "" };
|
|
2801
|
+
}
|
|
2668
2802
|
if (loop) {
|
|
2669
2803
|
const engineeringEvidence = loop.verify.mode === "host" ? undefined : loop.verify.evidence;
|
|
2670
2804
|
let retryCount = resumePlan?.previousTrace.filter((entry) => entry.event.type === "retry" && entry.event.scope === "provider").length ?? 0;
|
|
@@ -2843,6 +2977,7 @@ export class CoreMindRuntime {
|
|
|
2843
2977
|
}
|
|
2844
2978
|
}
|
|
2845
2979
|
finally {
|
|
2980
|
+
modelRequestsOpen = false;
|
|
2846
2981
|
hostVerification?.interrupt(new CoreMindError("run_already_finished", "Run 已结束验收等待"));
|
|
2847
2982
|
context.setHarnessFactory(undefined);
|
|
2848
2983
|
}
|
|
@@ -3438,6 +3573,52 @@ export class CoreMindRuntime {
|
|
|
3438
3573
|
return cm.filePath;
|
|
3439
3574
|
}
|
|
3440
3575
|
}
|
|
3576
|
+
/** 仅供 ChatSession 使用;导航与 Run 使用相同的写身份和历史水位。 */
|
|
3577
|
+
async function navigateRuntimeSession(runtime, agentName, history, navigation) {
|
|
3578
|
+
const slot = executionSlotFor(runtime);
|
|
3579
|
+
if (!slot.sessionOptions || slot.sessionAgent !== agentName)
|
|
3580
|
+
throw new CoreMindError("session_navigation_invalid", "导航需要启用持久会话并使用会话所属主 Agent");
|
|
3581
|
+
if (slot.running || slot.navigatingSession || boundDelegationExecutor(runtime))
|
|
3582
|
+
throw new CoreMindError("concurrent_run", "活动 Run 或导航期间不能操作会话树");
|
|
3583
|
+
slot.navigatingSession = true;
|
|
3584
|
+
try {
|
|
3585
|
+
const opts = slot.sessionOptions;
|
|
3586
|
+
return await withSessionWriteLease(opts.dir, opts.sessionId, async (lease) => {
|
|
3587
|
+
const session = await openWritableSession(opts, lease);
|
|
3588
|
+
if (navigation.type !== "inspect" &&
|
|
3589
|
+
historySessionSnapshot(history) !== sessionSnapshot(session))
|
|
3590
|
+
throw new CoreMindError("session_snapshot_stale", "会话已变化,请重新打开后再导航");
|
|
3591
|
+
if (navigation.type === "fork")
|
|
3592
|
+
return { tree: await session.fork(navigation.at) };
|
|
3593
|
+
if (navigation.type === "create")
|
|
3594
|
+
await session.createBranch(navigation.branchId, navigation.at);
|
|
3595
|
+
if (navigation.type === "select")
|
|
3596
|
+
await session.selectBranch(navigation.branchId);
|
|
3597
|
+
const tree = await session.inspectTree();
|
|
3598
|
+
if (navigation.type === "inspect")
|
|
3599
|
+
return { tree };
|
|
3600
|
+
const restored = rememberSessionHistory(session, projectBranchMessages(await session.branchEntries()).map((item) => item.message));
|
|
3601
|
+
slot.sessionPreviewHistory = copySessionHistory(restored);
|
|
3602
|
+
return { tree, history: restored };
|
|
3603
|
+
});
|
|
3604
|
+
}
|
|
3605
|
+
finally {
|
|
3606
|
+
slot.navigatingSession = false;
|
|
3607
|
+
}
|
|
3608
|
+
}
|
|
3609
|
+
/** 只供 TaskSession 使用:精确工具与子任务共享同一个 Run 的全部治理。 */
|
|
3610
|
+
export async function runTaskProgram(runtime, id, execute) {
|
|
3611
|
+
const slot = executionSlotFor(runtime);
|
|
3612
|
+
if (slot.running || slot.taskProgram)
|
|
3613
|
+
throw new CoreMindError("concurrent_run", "任务 Run 已在执行");
|
|
3614
|
+
slot.taskProgram = { id, execute };
|
|
3615
|
+
try {
|
|
3616
|
+
return await runtime.run();
|
|
3617
|
+
}
|
|
3618
|
+
finally {
|
|
3619
|
+
slot.taskProgram = undefined;
|
|
3620
|
+
}
|
|
3621
|
+
}
|
|
3441
3622
|
async function executeWithSessionLease(runtime, options, config, context, execute) {
|
|
3442
3623
|
if (!options.sessionId || !config.session?.enabled)
|
|
3443
3624
|
return execute();
|