compaction-summary-reasoning.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import type { AgentMessage } from "@earendil-works/pi-agent-core";
  2. import type { AssistantMessage, Model } from "@earendil-works/pi-ai";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. import { type CompactionPreparation, compact, generateSummary } from "../src/core/compaction/index.ts";
  5. const { completeSimpleMock } = vi.hoisted(() => ({
  6. completeSimpleMock: vi.fn(),
  7. }));
  8. vi.mock("@earendil-works/pi-ai", async (importOriginal) => {
  9. const actual = await importOriginal<typeof import("@earendil-works/pi-ai")>();
  10. return {
  11. ...actual,
  12. completeSimple: completeSimpleMock,
  13. };
  14. });
  15. function createModel(reasoning: boolean, maxTokens = 8192): Model<"anthropic-messages"> {
  16. return {
  17. id: reasoning ? "reasoning-model" : "non-reasoning-model",
  18. name: reasoning ? "Reasoning Model" : "Non-reasoning Model",
  19. api: "anthropic-messages",
  20. provider: "anthropic",
  21. baseUrl: "https://api.anthropic.com",
  22. reasoning,
  23. input: ["text"],
  24. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
  25. contextWindow: 200000,
  26. maxTokens,
  27. };
  28. }
  29. const mockSummaryResponse: AssistantMessage = {
  30. role: "assistant",
  31. content: [{ type: "text", text: "## Goal\nTest summary" }],
  32. api: "anthropic-messages",
  33. provider: "anthropic",
  34. model: "claude-sonnet-4-5",
  35. usage: {
  36. input: 10,
  37. output: 10,
  38. cacheRead: 0,
  39. cacheWrite: 0,
  40. totalTokens: 20,
  41. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  42. },
  43. stopReason: "stop",
  44. timestamp: Date.now(),
  45. };
  46. const messages: AgentMessage[] = [{ role: "user", content: "Summarize this.", timestamp: Date.now() }];
  47. describe("generateSummary reasoning options", () => {
  48. beforeEach(() => {
  49. completeSimpleMock.mockReset();
  50. completeSimpleMock.mockResolvedValue(mockSummaryResponse);
  51. });
  52. it("uses the provided thinking level for reasoning-capable models", async () => {
  53. await generateSummary(
  54. messages,
  55. createModel(true),
  56. 2000,
  57. "test-key",
  58. undefined,
  59. undefined,
  60. undefined,
  61. undefined,
  62. "medium",
  63. );
  64. expect(completeSimpleMock).toHaveBeenCalledTimes(1);
  65. expect(completeSimpleMock.mock.calls[0][2]).toMatchObject({
  66. reasoning: "medium",
  67. apiKey: "test-key",
  68. });
  69. });
  70. it("does not set reasoning when thinking is off", async () => {
  71. await generateSummary(
  72. messages,
  73. createModel(true),
  74. 2000,
  75. "test-key",
  76. undefined,
  77. undefined,
  78. undefined,
  79. undefined,
  80. "off",
  81. );
  82. expect(completeSimpleMock).toHaveBeenCalledTimes(1);
  83. expect(completeSimpleMock.mock.calls[0][2]).toMatchObject({
  84. apiKey: "test-key",
  85. });
  86. expect(completeSimpleMock.mock.calls[0][2]).not.toHaveProperty("reasoning");
  87. });
  88. it("does not set reasoning for non-reasoning models", async () => {
  89. await generateSummary(
  90. messages,
  91. createModel(false),
  92. 2000,
  93. "test-key",
  94. undefined,
  95. undefined,
  96. undefined,
  97. undefined,
  98. "medium",
  99. );
  100. expect(completeSimpleMock).toHaveBeenCalledTimes(1);
  101. expect(completeSimpleMock.mock.calls[0][2]).toMatchObject({
  102. apiKey: "test-key",
  103. });
  104. expect(completeSimpleMock.mock.calls[0][2]).not.toHaveProperty("reasoning");
  105. });
  106. it("clamps compaction summary maxTokens to the model output cap", async () => {
  107. const preparation: CompactionPreparation = {
  108. firstKeptEntryId: "entry-keep",
  109. messagesToSummarize: messages,
  110. turnPrefixMessages: messages,
  111. isSplitTurn: true,
  112. tokensBefore: 600000,
  113. fileOps: { read: new Set(), written: new Set(), edited: new Set() },
  114. settings: { enabled: true, reserveTokens: 500000, keepRecentTokens: 20000 },
  115. };
  116. await compact(preparation, createModel(false, 128000), "test-key");
  117. expect(completeSimpleMock.mock.calls.map((call) => call[2]?.maxTokens)).toEqual([128000, 128000]);
  118. });
  119. });