|
|
@@ -0,0 +1,156 @@
|
|
|
+// ============================================================
|
|
|
+// Dispatch Tasks Tool Handler
|
|
|
+// ============================================================
|
|
|
+// 设计合同:docs/specs/workspace-task-dispatch.md(根目录)。
|
|
|
+// 边界要点(勿照抄 OffPeak 的方向):
|
|
|
+// - deny:闲时派发轮禁用(复用 assertNotOffPeakTurn);子任务/actor 会话由
|
|
|
+// bootstrap 的工具可见面过滤(同 offPeakPort 的 includeOffPeak 门),handler
|
|
|
+// 不重复判断宿主注入事实。
|
|
|
+// - 统一确认:permission.needsApproval=true,reason 内带清单摘要——这是
|
|
|
+// 「一次授权全部」的落点;execute 的最终值由确认结果回写(port context)。
|
|
|
+// - 失败隔离:port 返回逐项 results,handler 只做汇总序列化,不再吞错。
|
|
|
+
|
|
|
+import {
|
|
|
+ CoreErrorType,
|
|
|
+ createCoreError,
|
|
|
+ DispatchTasksInputSchema,
|
|
|
+ DispatchTasksOutputJsonSchema,
|
|
|
+ DispatchTasksInputJsonSchema,
|
|
|
+ DispatchTasksOutputSchema,
|
|
|
+ type DispatchTasksOutput,
|
|
|
+ type ToolPermissionSpec,
|
|
|
+} from "@zcode/contracts";
|
|
|
+import type { ToolEntry, ToolExecutionContext, ToolHandler } from "../types.js";
|
|
|
+import { assertNotOffPeakTurn } from "./off-peak.js";
|
|
|
+
|
|
|
+const DISPATCH_TOOL_TIMEOUT_MS = 120_000;
|
|
|
+const DISPATCH_MODEL_BYTES = 32_000;
|
|
|
+
|
|
|
+function assertDispatchPort(
|
|
|
+ context: ToolExecutionContext,
|
|
|
+): asserts context is ToolExecutionContext & {
|
|
|
+ taskDispatchPort: NonNullable<ToolExecutionContext["taskDispatchPort"]>;
|
|
|
+} {
|
|
|
+ if (context.taskDispatchPort) return;
|
|
|
+ throw createCoreError(
|
|
|
+ CoreErrorType.ConfigurationError,
|
|
|
+ "TaskDispatchPort is not configured for DispatchTasks",
|
|
|
+ {
|
|
|
+ context: {
|
|
|
+ toolCallId: context.toolCallId,
|
|
|
+ toolName: "DispatchTasks",
|
|
|
+ },
|
|
|
+ recoverable: false,
|
|
|
+ },
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+export const dispatchTasksHandler: ToolHandler = async (input, context) => {
|
|
|
+ assertNotOffPeakTurn(context, "DispatchTasks", {
|
|
|
+ hint: "Dispatch tasks from a regular conversation instead.",
|
|
|
+ });
|
|
|
+ const parsed = DispatchTasksInputSchema.parse(input);
|
|
|
+ assertDispatchPort(context);
|
|
|
+
|
|
|
+ const { results } = await context.taskDispatchPort.dispatch(parsed.tasks, {
|
|
|
+ sessionId: context.sessionId,
|
|
|
+ // 确认窗批准即视为对整批的统一授权;execute 以用户在确认窗的最终选择为准
|
|
|
+ //(host 侧回写,工具入参只是模型建议值)。
|
|
|
+ execute: parsed.execute,
|
|
|
+ });
|
|
|
+
|
|
|
+ const createdCount = results.filter((r) => r.status !== "failed").length;
|
|
|
+ const failedCount = results.length - createdCount;
|
|
|
+ const executed = parsed.execute;
|
|
|
+ const summaryLines = results.map((r) => {
|
|
|
+ if (r.status === "failed") {
|
|
|
+ return `- [failed] ${r.id}: ${r.title} — ${r.failure ?? "unknown error"}`;
|
|
|
+ }
|
|
|
+ const state = r.status === "started" ? "started" : "created";
|
|
|
+ return `- [${state}] ${r.id}: ${r.title}`;
|
|
|
+ });
|
|
|
+ const message = executed
|
|
|
+ ? `Dispatched ${createdCount} task(s) under the current workspace (${failedCount} failed). Task list:\n${summaryLines.join("\n")}`
|
|
|
+ : `Created ${createdCount} draft task(s) under the current workspace (${failedCount} failed); they are NOT executing. Task list:\n${summaryLines.join("\n")}`;
|
|
|
+
|
|
|
+ return {
|
|
|
+ executed,
|
|
|
+ createdCount,
|
|
|
+ failedCount,
|
|
|
+ results,
|
|
|
+ message,
|
|
|
+ } satisfies DispatchTasksOutput;
|
|
|
+};
|
|
|
+
|
|
|
+function dispatchPermission(): ToolPermissionSpec {
|
|
|
+ return {
|
|
|
+ permission: "dispatchTasks",
|
|
|
+ reason: "DispatchTasks creates tasks under the current workspace and (if confirmed) starts executing them with the granted permission scope. Confirming grants this scope to ALL tasks in the batch.",
|
|
|
+ riskLevel: "medium" as const,
|
|
|
+ sideEffectScope: "workspace" as const,
|
|
|
+ needsApproval: true,
|
|
|
+ patternSources: ["toolName", "input"],
|
|
|
+ denyPriority: "beforeAsk" as const,
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+const dispatchResultBudget = {
|
|
|
+ maxInlineBytes: DISPATCH_MODEL_BYTES,
|
|
|
+ maxModelBytes: DISPATCH_MODEL_BYTES,
|
|
|
+ strategy: "truncate" as const,
|
|
|
+ preview: {
|
|
|
+ maxBytes: DISPATCH_MODEL_BYTES,
|
|
|
+ direction: "head" as const,
|
|
|
+ },
|
|
|
+};
|
|
|
+
|
|
|
+const dispatchTimeout = {
|
|
|
+ defaultMs: DISPATCH_TOOL_TIMEOUT_MS,
|
|
|
+ maxMs: DISPATCH_TOOL_TIMEOUT_MS,
|
|
|
+ allowCallOverride: false,
|
|
|
+};
|
|
|
+
|
|
|
+export const dispatchTasksToolEntry: ToolEntry = {
|
|
|
+ capability:
|
|
|
+ "Organize a batch of bugs/requests into a task list and create them under the current workspace, optionally executing them concurrently",
|
|
|
+ metadata: {
|
|
|
+ name: "DispatchTasks",
|
|
|
+ description:
|
|
|
+ "Create multiple independent tasks under the current workspace from one organized list (typical use: the user pastes a pile of bugs). Each task becomes a real task in the workspace task list with its own session. The user confirms ONCE for the whole batch (task list + shared permission scope) and can switch between create-only and create-and-execute. All tasks run concurrently when executed; one failure does not block the others.",
|
|
|
+ modelInstructions: [
|
|
|
+ "Use this only when the user clearly wants multiple independent tasks handled as a batch (a pile of bugs, a list of small fixes). For a single piece of work, use a normal conversation instead.",
|
|
|
+ "First deduplicate and merge overlapping items, then emit the final list in ONE call. Do not call this tool repeatedly for the same batch.",
|
|
|
+ "Every prompt MUST be self-contained: the new sessions do NOT inherit this conversation's history. Restate files, symptoms and acceptance criteria inside each prompt.",
|
|
|
+ "Set execute=false only when the user explicitly asks to create tasks without running them now.",
|
|
|
+ "After a successful dispatch, reply with a brief summary only; the workspace task list is the authoritative view.",
|
|
|
+ ],
|
|
|
+ readOnly: false,
|
|
|
+ destructive: false,
|
|
|
+ concurrentSafe: false,
|
|
|
+ timeoutMs: DISPATCH_TOOL_TIMEOUT_MS,
|
|
|
+ maxOutputBytes: DISPATCH_MODEL_BYTES,
|
|
|
+ sideEffectScope: "workspace",
|
|
|
+ riskLevel: "medium",
|
|
|
+ needsApproval: true,
|
|
|
+ },
|
|
|
+ handler: dispatchTasksHandler,
|
|
|
+ inputSchema: DispatchTasksInputJsonSchema,
|
|
|
+ outputSchema: DispatchTasksOutputJsonSchema,
|
|
|
+ runtimeInputSchema: DispatchTasksInputSchema,
|
|
|
+ runtimeOutputSchema: DispatchTasksOutputSchema,
|
|
|
+ permission: dispatchPermission(),
|
|
|
+ resultBudget: dispatchResultBudget,
|
|
|
+ timeout: dispatchTimeout,
|
|
|
+ cancellation: {
|
|
|
+ supported: false,
|
|
|
+ cleanup: "none",
|
|
|
+ userVisibleMessage:
|
|
|
+ "DispatchTasks was cancelled; already-created tasks remain in the workspace task list",
|
|
|
+ },
|
|
|
+ trace: {
|
|
|
+ required: true,
|
|
|
+ propagateToAdapters: true,
|
|
|
+ recordInput: "summary",
|
|
|
+ recordOutput: "summary",
|
|
|
+ },
|
|
|
+};
|