deferred-call-todo-runtime.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { mkdtemp, rm } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { tmpdir } from "node:os";
  4. import { createSessionId, createProjectId } from "@zcode/contracts";
  5. import { createSqliteSessionStore } from "../../adapters/src/storage/session-store/sqlite-session-store.js";
  6. import { createTestAgentRuntime } from "./test-agent-runtime.js";
  7. import { createTestModelFactory } from "./test-runtime-model.js";
  8. import { createTestSessionEventStore } from "./test-event-store.js";
  9. import { getTodoReminderTurnCounts } from "../src/runtime/helpers/runtime-reminders.js";
  10. import { expect, it } from "vitest";
  11. import type { ModelRequest, ModelResult } from "@zcode/contracts";
  12. import type { AgentRuntimeInternal } from "../src/runtime/internal.js";
  13. it.each(["off", "on"] as const)(
  14. "keeps TodoWrite reminders equivalent for direct/wrapped runtime calls (stream=%s)",
  15. async (modelStreaming) => {
  16. const root = await mkdtemp(join(tmpdir(), "deferred-todo-"));
  17. try {
  18. for (const wrapped of [false, true]) {
  19. const sessionId = createSessionId(`todo-${modelStreaming}-${wrapped}`);
  20. const store = await createSqliteSessionStore({
  21. dbPath: join(root, `${modelStreaming}-${wrapped}.sqlite`),
  22. });
  23. await store.createSession({
  24. id: sessionId,
  25. projectID: createProjectId("todo-review"),
  26. slug: "review",
  27. title: "Review",
  28. directory: root,
  29. version: "test",
  30. time: { created: 1, updated: 1 },
  31. });
  32. const requests: ModelRequest[] = [];
  33. const respond = async (request: ModelRequest): Promise<ModelResult> => {
  34. requests.push(request);
  35. const step = requests.length;
  36. const input = {
  37. todos: [{ content: `Step ${step}`, status: "in_progress", priority: "high" }],
  38. };
  39. return step <= 10
  40. ? {
  41. text: "",
  42. finishReason: "tool-calls",
  43. usage: {},
  44. toolCalls: [
  45. {
  46. id: `call-${step}`,
  47. name: wrapped ? "DeferredToolCall" : "TodoWrite",
  48. input: wrapped ? { name: "TodoWrite", input } : input,
  49. },
  50. ],
  51. }
  52. : { text: "Done", finishReason: "stop", usage: {} };
  53. };
  54. const runtime = createTestAgentRuntime(
  55. sessionId,
  56. {
  57. mode: "yolo",
  58. toolSearch: { mode: "local" },
  59. modelStreaming,
  60. titleGeneration: { enabled: false },
  61. workspacePath: root,
  62. toolAllowlist: ["ToolSearch", "DeferredToolCall", "TodoWrite"],
  63. },
  64. {
  65. sessionStore: store,
  66. eventStore: createTestSessionEventStore(),
  67. modelFactory: createTestModelFactory({
  68. generateText: respond,
  69. async *streamText(request) {
  70. const r = await respond(request);
  71. for (const toolCall of r.toolCalls ?? []) yield { type: "tool_call", toolCall };
  72. if (r.text) {
  73. yield { type: "text_start", id: "txt" };
  74. yield { type: "text_delta", id: "txt", text: r.text };
  75. yield { type: "text_end", id: "txt" };
  76. }
  77. yield { type: "finish", finishReason: r.finishReason, usage: r.usage };
  78. },
  79. }),
  80. },
  81. );
  82. try {
  83. await runtime.executeTurn("Update the todo for each step.");
  84. const parts = (await store.messages({ sessionID: sessionId })).flatMap((m) => m.parts);
  85. expect(requests).toHaveLength(11);
  86. expect(
  87. parts.filter(
  88. (p) => p.type === "tool" && p.tool === "TodoWrite" && p.state.status === "completed",
  89. ),
  90. ).toHaveLength(10);
  91. expect(
  92. requests.filter((r) =>
  93. JSON.stringify(r.messages).includes("The TodoWrite tool hasn't been used recently."),
  94. ),
  95. ).toEqual([]);
  96. expect(
  97. getTodoReminderTurnCounts(
  98. (
  99. runtime as unknown as AgentRuntimeInternal
  100. ).messageHistory.borrowReadOnlyRuntimeEntries(),
  101. ).turnsSinceLastTodoWrite,
  102. ).toBe(1);
  103. } finally {
  104. await runtime.closeBrowserSession();
  105. store.close();
  106. }
  107. }
  108. } finally {
  109. await rm(root, { recursive: true, force: true });
  110. }
  111. },
  112. );