model-io-continuity.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { deepStrictEqual, equal } from "node:assert/strict";
  2. import { describe, it } from "node:test";
  3. import { deriveTrajectoriesFromModelIoEntries, type ModelIoJsonlEntry } from "../src/model-io.js";
  4. import type { OpenAiMessage } from "../src/types.js";
  5. const user: OpenAiMessage = {
  6. role: "user",
  7. content: [{ type: "text", text: "inspect" }],
  8. };
  9. const toolUse = { type: "tool_use", id: "call_1", name: "Read", input: { path: "a.ts" } };
  10. const answer = { type: "text", text: "reading" };
  11. const result: OpenAiMessage = {
  12. role: "user",
  13. content: [{ type: "tool_result", tool_use_id: "call_1", content: "file content" }],
  14. };
  15. function record(messages: OpenAiMessage[], response = false): ModelIoJsonlEntry {
  16. return {
  17. querySource: "main_turn",
  18. request: { body: { model: "glm-test", messages } },
  19. ...(response
  20. ? {
  21. response: {
  22. reasoningText: "summary without provider signature",
  23. text: "reading",
  24. toolCalls: [{ id: "call_1", name: "Read", input: { path: "a.ts" } }],
  25. },
  26. }
  27. : {}),
  28. };
  29. }
  30. describe("model-io response continuity", () => {
  31. for (const thinking of [
  32. { type: "thinking", thinking: "recorded reasoning", signature: "provider-signature" },
  33. { type: "redacted_thinking", data: "opaque-provider-data" },
  34. ]) {
  35. it(`preserves the real ${thinking.type} block and signature without splitting`, () => {
  36. const assistant = { role: "assistant", content: [thinking, answer, toolUse] };
  37. const messages = [user, assistant, result];
  38. const entries = [record([user], true), record(messages)];
  39. const original = structuredClone(entries);
  40. const output = deriveTrajectoriesFromModelIoEntries(entries);
  41. equal(output.trajectories.length, 1);
  42. equal(output.trajectories[0]?.requestCount, 2);
  43. deepStrictEqual(output.trajectories[0]?.requestBody.messages, messages);
  44. deepStrictEqual(entries, original);
  45. });
  46. }
  47. it("uses the next selected request after expanding deltas across a sidecar", () => {
  48. const assistant = {
  49. role: "assistant",
  50. content: [{ type: "thinking", thinking: "thought", signature: "sig" }, answer, toolUse],
  51. };
  52. const first = { ...record([user], true), querySource: "subagent" };
  53. const sidecar = { ...record([user]), querySource: "session_title" };
  54. const next: ModelIoJsonlEntry = {
  55. querySource: "subagent",
  56. request: {
  57. body: { model: "glm-test", messages: [assistant, result] },
  58. bodyMessagesKind: "delta",
  59. bodyMessageOffset: 1,
  60. },
  61. };
  62. const output = deriveTrajectoriesFromModelIoEntries([first, sidecar, next], {
  63. querySource: "subagent",
  64. });
  65. equal(output.trajectories.length, 1);
  66. deepStrictEqual(output.trajectories[0]?.requestBody.messages, [user, assistant, result]);
  67. });
  68. for (const changed of [
  69. [{ type: "text", text: "different answer" }, toolUse],
  70. [answer, { ...toolUse, input: { path: "changed.ts" } }],
  71. [answer, { ...toolUse, id: "different-call" }],
  72. ]) {
  73. it(`retains a split when the recorded reply differs: ${JSON.stringify(changed)}`, () => {
  74. const assistant = {
  75. role: "assistant",
  76. content: [{ type: "thinking", thinking: "thought", signature: "sig" }, ...changed],
  77. };
  78. const output = deriveTrajectoriesFromModelIoEntries([
  79. record([user], true),
  80. record([user, assistant, result]),
  81. ]);
  82. equal(output.trajectories.length, 2);
  83. equal(output.trajectories[1]?.reason, "non-incremental-change");
  84. deepStrictEqual(output.trajectories[0]?.requestBody.messages.at(-1), {
  85. role: "assistant",
  86. content: [answer, toolUse],
  87. });
  88. });
  89. }
  90. it("does not borrow reasoning from a request with rewritten history", () => {
  91. const assistant = {
  92. role: "assistant",
  93. content: [{ type: "thinking", thinking: "unrelated", signature: "sig" }, answer, toolUse],
  94. };
  95. const output = deriveTrajectoriesFromModelIoEntries([
  96. record([user], true),
  97. record([{ role: "user", content: "new history" }, assistant, result]),
  98. ]);
  99. equal(output.trajectories.length, 2);
  100. deepStrictEqual(output.trajectories[0]?.requestBody.messages.at(-1), {
  101. role: "assistant",
  102. content: [answer, toolUse],
  103. });
  104. });
  105. it("skips records without request bodies when completing a response", () => {
  106. const assistant = {
  107. role: "assistant",
  108. content: [{ type: "thinking", thinking: "thought", signature: "sig" }, answer, toolUse],
  109. };
  110. const output = deriveTrajectoriesFromModelIoEntries([
  111. record([user], true),
  112. { querySource: "main_turn", error: "session stopped" },
  113. record([user, assistant, result]),
  114. ]);
  115. equal(output.trajectories.length, 1);
  116. equal(output.trajectories[0]?.requestCount, 2);
  117. deepStrictEqual(output.trajectories[0]?.requestBody.messages, [user, assistant, result]);
  118. });
  119. for (const field of ["thinking", "signature"]) {
  120. it(`still splits when an existing history ${field} changes`, () => {
  121. const thinking = { type: "thinking", thinking: "original", signature: "original" };
  122. const assistant = { role: "assistant", content: [thinking, answer, toolUse] };
  123. const changedAssistant = {
  124. role: "assistant",
  125. content: [{ ...thinking, [field]: "changed" }, answer, toolUse],
  126. };
  127. const output = deriveTrajectoriesFromModelIoEntries([
  128. record([user], true),
  129. record([user, assistant, result]),
  130. record([user, changedAssistant, result]),
  131. ]);
  132. equal(output.trajectories.length, 2);
  133. equal(output.trajectories[0]?.requestCount, 2);
  134. equal(output.trajectories[1]?.reason, "non-incremental-change");
  135. });
  136. }
  137. });