token-estimation-regression.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { describe, expect, it } from "vitest";
  2. import { ESTIMATED_TOKEN_CHAR_DIVISOR } from "@zcode/shared";
  3. import { estimateMessageTokens, shouldAutoCompact } from "../src/compact/index.js";
  4. import { estimateTokens } from "../src/context/utils.js";
  5. import { buildPostCompactReadStateReminderEntries } from "../src/runtime/helpers/compact-post-reminders.js";
  6. import { createReadFileStateKey } from "../src/tool/read-file-state.js";
  7. import type { ReadFileStateMap } from "../src/tool/types.js";
  8. describe("token estimation regression", () => {
  9. it("uses the shared three-character divisor for text and model messages", () => {
  10. const text = "x".repeat(10);
  11. expect(ESTIMATED_TOKEN_CHAR_DIVISOR).toBe(3);
  12. expect(estimateTokens(text)).toBe(4);
  13. expect(estimateMessageTokens([{ role: "user", content: text }])).toBe(4);
  14. });
  15. it("counts each tool call name and serialized input without counting its id", () => {
  16. const content = "assistant text";
  17. const toolCalls = [
  18. {
  19. id: "tool-id-must-not-be-counted-" + "x".repeat(120),
  20. name: "Read",
  21. input: {
  22. file_path: "/tmp/" + "nested/".repeat(12) + "large-input.txt",
  23. limit: 2_000,
  24. },
  25. },
  26. {
  27. id: "undefined-input",
  28. name: "Bash",
  29. input: undefined,
  30. },
  31. ];
  32. const estimatedCharacters =
  33. content.length +
  34. toolCalls.reduce(
  35. (total, toolCall) =>
  36. total + (toolCall.name + JSON.stringify(toolCall.input ?? {})).length,
  37. 0,
  38. );
  39. expect(
  40. estimateMessageTokens([
  41. {
  42. role: "assistant",
  43. content,
  44. toolCalls,
  45. },
  46. ]),
  47. ).toBe(Math.ceil(estimatedCharacters / ESTIMATED_TOKEN_CHAR_DIVISOR));
  48. });
  49. it("falls back to an empty object for non-JSON-safe tool call inputs", () => {
  50. const circularInput: Record<string, unknown> = {};
  51. circularInput.self = circularInput;
  52. const toolCalls = [
  53. { id: "bigint-input", name: "BigIntTool", input: 1n },
  54. { id: "circular-input", name: "CircularTool", input: circularInput },
  55. ];
  56. const estimatedCharacters = toolCalls.reduce(
  57. (total, toolCall) => total + `${toolCall.name}{}`.length,
  58. 0,
  59. );
  60. expect(
  61. estimateMessageTokens([
  62. {
  63. role: "assistant",
  64. content: "",
  65. toolCalls,
  66. },
  67. ]),
  68. ).toBe(Math.ceil(estimatedCharacters / ESTIMATED_TOKEN_CHAR_DIVISOR));
  69. });
  70. it("uses tool call inputs in local auto-compact decisions", () => {
  71. const toolCall = {
  72. name: "Bash",
  73. input: { command: "x".repeat(300) },
  74. };
  75. const messages = [
  76. { role: "user", content: "old context" },
  77. { role: "assistant", content: "", toolCalls: [toolCall] },
  78. ];
  79. const estimatedTokenCount =
  80. Math.ceil(messages[0]!.content.length / ESTIMATED_TOKEN_CHAR_DIVISOR) +
  81. Math.ceil(
  82. (toolCall.name + JSON.stringify(toolCall.input)).length /
  83. ESTIMATED_TOKEN_CHAR_DIVISOR,
  84. );
  85. expect(
  86. shouldAutoCompact({
  87. config: {
  88. bufferTokens: 100,
  89. contextWindow: 200,
  90. maxOutputTokens: 1,
  91. },
  92. messages,
  93. }),
  94. ).toMatchObject({
  95. estimatedTokenCount,
  96. reason: "above_threshold",
  97. shouldCompact: true,
  98. tokenCount: estimatedTokenCount,
  99. tokenSource: "estimate",
  100. });
  101. });
  102. it("uses the shared divisor when deciding whether to inline post-compact read content", () => {
  103. const filePath = "/tmp/zcode-compact-three-char-divisor.txt";
  104. const content = "x".repeat(31);
  105. const readFileState: ReadFileStateMap = new Map();
  106. readFileState.set(createReadFileStateKey(filePath, 1, undefined), {
  107. content,
  108. isPartialView: false,
  109. path: filePath,
  110. readAt: new Date(5_000),
  111. });
  112. const reminderText = JSON.stringify(
  113. buildPostCompactReadStateReminderEntries({
  114. maxFileApproxTokens: 10,
  115. readFileState,
  116. }),
  117. );
  118. expect(reminderText).toContain(`Note: ${filePath} was read before`);
  119. expect(reminderText).not.toContain(content);
  120. });
  121. });