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

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