offpeak-prompt-turn-disallowlist.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. buildTurnToolDisallowlist,
  4. resolveTurnOffPeakTaskId,
  5. startPromptTurn,
  6. } from "../src/zcode-protocol-v4/commands/prompt-turn.js";
  7. import { createFakeApp } from "./helpers/fake-zcode-app.js";
  8. import type { SendInputOptions } from "../src/app/types.js";
  9. import type { V4SessionRecordView } from "../src/zcode-protocol-v4/commands/types.js";
  10. describe("buildTurnToolDisallowlist (off-peak, D49)", () => {
  11. it("automation 轮只隐藏三个 cron 写工具,不隐藏 OffPeakCreate(D49-3 反向断言)", () => {
  12. const tools = buildTurnToolDisallowlist({ automationId: "automation-1" });
  13. expect(tools).not.toContain("SendMessage");
  14. expect(tools).toEqual(expect.arrayContaining(["CronCreate", "CronUpdate", "CronDelete"]));
  15. expect(tools).not.toContain("OffPeakCreate");
  16. expect(tools).not.toContain("Workflow");
  17. });
  18. it("闲时轮隐藏 OffPeakCreate 与工作流执行入口,SendMessage 可用,OffPeakList 与 cron 工具不受影响", () => {
  19. const tools = buildTurnToolDisallowlist({ offPeakTaskId: "offpeak-1" });
  20. expect(tools).toEqual([
  21. "OffPeakCreate",
  22. "Workflow",
  23. "CreateWorkflow",
  24. "AmendWorkflow",
  25. "ResumeWorkflowRun",
  26. "EvalWorkflowSnippet",
  27. ]);
  28. });
  29. it("闲时轮合并调用方已有 denylist(host 派发同时带 CronCreate,D45+D49)", () => {
  30. const tools = buildTurnToolDisallowlist({
  31. offPeakTaskId: "offpeak-1",
  32. toolDisallowlist: ["CronCreate"],
  33. });
  34. expect(tools).toEqual(expect.arrayContaining(["CronCreate", "OffPeakCreate", "Workflow"]));
  35. expect(tools).toHaveLength(7);
  36. });
  37. it("普通用户轮不追加任何隐藏", () => {
  38. expect(buildTurnToolDisallowlist({})).toBeUndefined();
  39. });
  40. });
  41. describe("resolveTurnOffPeakTaskId", () => {
  42. it("显式 offPeakTaskId 优先", () => {
  43. expect(resolveTurnOffPeakTaskId({ offPeakTaskId: "offpeak-1", inputId: "whatever" })).toBe(
  44. "offpeak-1",
  45. );
  46. });
  47. it("续跑 inputId 前缀兜底(offpeak-<uuid>:resume:<uuid>)", () => {
  48. expect(resolveTurnOffPeakTaskId({ inputId: "offpeak-abc:resume:def" })).toBe("offpeak-abc");
  49. });
  50. it("普通与 automation inputId 不误判", () => {
  51. expect(resolveTurnOffPeakTaskId({ inputId: "user-input-1" })).toBeUndefined();
  52. expect(resolveTurnOffPeakTaskId({ inputId: "automation-1:run:x" })).toBeUndefined();
  53. // 裸前缀(无 id 主体)不算有效身份。
  54. expect(resolveTurnOffPeakTaskId({ inputId: "offpeak-" })).toBeUndefined();
  55. });
  56. });
  57. describe("闲时 turn 清理与当前执行约束共存", () => {
  58. it.each(["completed", "queued", "rejected", "thrown"])(
  59. "%s 不残留 idle 身份,不丢 modelExecution/sharedContextRefs",
  60. async (outcome) => {
  61. const completion = Promise.withResolvers<void>();
  62. const sendInput = vi.fn(async () => {
  63. if (outcome === "thrown") throw new Error("admission failed");
  64. if (outcome === "rejected") return { kind: "rejected", reason: "no_active_turn" };
  65. if (outcome === "queued") return { kind: "queued", queueItemId: "q1" };
  66. return { kind: "started_turn", turnId: "turn-1", completion: completion.promise };
  67. });
  68. const record = {
  69. app: createFakeApp(undefined, {
  70. sendInput: sendInput as ReturnType<typeof createFakeApp>["sendInput"],
  71. }),
  72. workspace: { workspacePath: "/test/offpeak" },
  73. persistence: "immediate",
  74. traceContext: { traceId: "trace-offpeak", sessionId: "sess_test" },
  75. activeOffPeakTaskId: "previous-id",
  76. } as V4SessionRecordView;
  77. const modelExecution = { selectionScope: "execution" } satisfies NonNullable<
  78. SendInputOptions["modelExecution"]
  79. >;
  80. const sharedContextRefs: NonNullable<SendInputOptions["sharedContextRefs"]> = [];
  81. const run = startPromptTurn({ getRecord: () => record }, record, {
  82. content: "p",
  83. inputId: "input-1",
  84. offPeakTaskId: "current-id",
  85. modelExecution,
  86. sharedContextRefs,
  87. });
  88. if (outcome === "thrown" || outcome === "rejected") {
  89. await expect(run).rejects.toThrow();
  90. } else {
  91. const started = await run;
  92. if (outcome === "completed") {
  93. expect(record.activeOffPeakTaskId).toBe("current-id");
  94. completion.resolve();
  95. await started.completion;
  96. }
  97. }
  98. expect(sendInput).toHaveBeenCalledWith(
  99. expect.anything(),
  100. expect.objectContaining({
  101. modelExecution,
  102. sharedContextRefs,
  103. offPeakTaskId: "current-id",
  104. toolDisallowlist: [
  105. "OffPeakCreate",
  106. "Workflow",
  107. "CreateWorkflow",
  108. "AmendWorkflow",
  109. "ResumeWorkflowRun",
  110. "EvalWorkflowSnippet",
  111. ],
  112. }),
  113. );
  114. expect(record.activeOffPeakTaskId).toBe("previous-id");
  115. expect(record.activeBotDeliveryTarget).toBeUndefined();
  116. },
  117. );
  118. });