no-progress-guard.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { describe, expect, it } from "vitest";
  2. import type { CallToolResult } from "@modelcontextprotocol/server";
  3. import { createNoProgressGuard, type NoProgressHint } from "../src/no-progress-guard.js";
  4. const meta = (sessionId: string, turnId?: string) => ({
  5. session_id: sessionId,
  6. ...(turnId ? { turn_id: turnId } : {}),
  7. });
  8. const text = (value: string): CallToolResult => ({ content: [{ type: "text", text: value }] });
  9. function hintsOf(
  10. calls: number,
  11. next: (index: number) => { code: string; result: CallToolResult },
  12. requestMeta = meta("s1", "t1"),
  13. ): Map<number, NoProgressHint> {
  14. const guard = createNoProgressGuard();
  15. const hints = new Map<number, NoProgressHint>();
  16. for (let index = 1; index <= calls; index += 1) {
  17. const hint = guard.observe({ requestMeta, ...next(index) });
  18. if (hint) hints.set(index, hint);
  19. }
  20. return hints;
  21. }
  22. describe("node_repl no-progress guard", () => {
  23. it("hints after five identical calls and then at ×3 steps", () => {
  24. const hints = hintsOf(45, (index) => ({ code: "end()", result: text(`changing ${index}`) }));
  25. expect([...hints.keys()].filter((index) => hints.get(index)?.kind === "same_call")).toEqual([
  26. 5, 15, 45,
  27. ]);
  28. expect(hints.get(5)?.text).toBe("[node_repl] Same call ×5. You may end this turn.");
  29. expect(hints.has(4)).toBe(false);
  30. });
  31. it("catches changing code that keeps returning the same result", () => {
  32. const hints = hintsOf(30, (index) => ({ code: `end${index}()`, result: text("(no output)") }));
  33. expect(hints.get(10)).toEqual({
  34. kind: "no_progress",
  35. count: 10,
  36. text: "[node_repl] No progress ×10. You may end this turn.",
  37. });
  38. expect(hints.has(9)).toBe(false);
  39. expect([...hints.keys()]).toEqual([10, 30]);
  40. });
  41. it("resets the result streak when the result changes", () => {
  42. const hints = hintsOf(18, (index) => ({
  43. code: `step${index}()`,
  44. result: text(index === 9 ? "moved" : "(no output)"),
  45. }));
  46. expect(hints.size).toBe(0);
  47. });
  48. it("treats a different isError flag as a different result", () => {
  49. const hints = hintsOf(12, (index) => ({
  50. code: `c${index}`,
  51. result: { ...text("same"), ...(index % 2 === 0 ? { isError: true } : {}) },
  52. }));
  53. expect(hints.size).toBe(0);
  54. });
  55. it("reports call volume at 30, 80, 200 and every 200 after", () => {
  56. const hints = hintsOf(600, (index) => ({ code: `c${index}`, result: text(`r${index}`) }));
  57. expect([...hints.keys()]).toEqual([30, 80, 200, 400, 600]);
  58. expect(hints.get(80)?.text).toBe("[node_repl] 80 calls this turn. You may end this turn.");
  59. });
  60. it("emits one hint per call with same call first", () => {
  61. const hints = hintsOf(30, () => ({ code: "same", result: text("same") }));
  62. expect(hints.get(5)?.kind).toBe("same_call");
  63. expect(hints.get(10)?.kind).toBe("no_progress");
  64. expect(hints.get(15)?.kind).toBe("same_call");
  65. expect(hints.get(30)?.kind).toBe("no_progress");
  66. });
  67. it("resets every counter when the turn changes", () => {
  68. const guard = createNoProgressGuard();
  69. for (let index = 0; index < 4; index += 1) {
  70. guard.observe({ requestMeta: meta("s1", "t1"), code: "x", result: text("y") });
  71. }
  72. expect(guard.observe({ requestMeta: meta("s1", "t2"), code: "x", result: text("y") })).toBe(
  73. undefined,
  74. );
  75. for (let index = 0; index < 3; index += 1) {
  76. guard.observe({ requestMeta: meta("s1", "t2"), code: "x", result: text("y") });
  77. }
  78. expect(
  79. guard.observe({ requestMeta: meta("s1", "t2"), code: "x", result: text("y") })?.kind,
  80. ).toBe("same_call");
  81. });
  82. it("keeps sessions apart", () => {
  83. const guard = createNoProgressGuard();
  84. const results: Array<NoProgressHint | undefined> = [];
  85. for (let index = 0; index < 5; index += 1) {
  86. results.push(guard.observe({ requestMeta: meta("a", "t"), code: "x", result: text("y") }));
  87. results.push(guard.observe({ requestMeta: meta("b", "t"), code: "x", result: text("y") }));
  88. }
  89. expect(results.filter(Boolean)).toHaveLength(2);
  90. expect(results.slice(0, 8).every((hint) => hint === undefined)).toBe(true);
  91. });
  92. it("does not count requests without a turn id", () => {
  93. const hints = hintsOf(40, () => ({ code: "x", result: text("y") }), meta("s1"));
  94. expect(hints.size).toBe(0);
  95. });
  96. });