refactor: lazy load task cancellation control runtime

This commit is contained in:
Shakker
2026-04-01 21:41:34 +01:00
committed by Peter Steinberger
parent 192c02cd92
commit 08560c1f48
2 changed files with 13 additions and 2 deletions

View File

@@ -0,0 +1,2 @@
export { getAcpSessionManager } from "../acp/control-plane/manager.js";
export { killSubagentRunAdmin } from "../agents/subagent-control.js";

View File

@@ -1,6 +1,4 @@
import crypto from "node:crypto";
import { getAcpSessionManager } from "../acp/control-plane/manager.js";
import { killSubagentRunAdmin } from "../agents/subagent-control.js";
import type { OpenClawConfig } from "../config/config.js";
import { onAgentEvent } from "../infra/agent-events.js";
import { requestHeartbeatNow } from "../infra/heartbeat-wake.js";
@@ -61,6 +59,8 @@ let listenerStop: (() => void) | null = null;
let restoreAttempted = false;
let deliveryRuntimePromise: Promise<typeof import("./task-registry-delivery-runtime.js")> | null =
null;
let controlRuntimePromise: Promise<typeof import("./task-registry-control.runtime.js")> | null =
null;
type TaskDeliveryOwner = {
sessionKey?: string;
@@ -365,6 +365,13 @@ function loadTaskRegistryDeliveryRuntime() {
return deliveryRuntimePromise;
}
function loadTaskRegistryControlRuntime() {
// Registry reads happen far more often than task cancellation, so keep the ACP/subagent
// control graph off the default import path until a cancellation flow actually needs it.
controlRuntimePromise ??= import("./task-registry-control.runtime.js");
return controlRuntimePromise;
}
function addRunIdIndex(taskId: string, runId?: string) {
const trimmed = runId?.trim();
if (!trimmed) {
@@ -1708,12 +1715,14 @@ export async function cancelTaskById(params: {
}
try {
if (task.runtime === "acp") {
const { getAcpSessionManager } = await loadTaskRegistryControlRuntime();
await getAcpSessionManager().cancelSession({
cfg: params.cfg,
sessionKey: childSessionKey,
reason: "task-cancel",
});
} else if (task.runtime === "subagent") {
const { killSubagentRunAdmin } = await loadTaskRegistryControlRuntime();
const result = await killSubagentRunAdmin({
cfg: params.cfg,
sessionKey: childSessionKey,