model-io-continuity.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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("restores interleaved thinking and separate text blocks from the next request", () => {
  48. // 请求里每个文字块单独一块(docs/design/v2/model/assistant-block-order.md);
  49. // 摘要的 text 是直接拼接,比较时折叠后仍能认出同一条回复。
  50. const assistant = {
  51. role: "assistant",
  52. content: [
  53. { type: "thinking", thinking: "first", signature: "sig-1" },
  54. { type: "text", text: "read" },
  55. { type: "thinking", thinking: "second", signature: "sig-2" },
  56. { type: "text", text: "ing" },
  57. toolUse,
  58. ],
  59. };
  60. const messages = [user, assistant, result];
  61. const output = deriveTrajectoriesFromModelIoEntries([record([user], true), record(messages)]);
  62. equal(output.trajectories.length, 1);
  63. deepStrictEqual(output.trajectories[0]?.requestBody.messages, messages);
  64. });
  65. it("uses the next selected request after expanding deltas across a sidecar", () => {
  66. const assistant = {
  67. role: "assistant",
  68. content: [{ type: "thinking", thinking: "thought", signature: "sig" }, answer, toolUse],
  69. };
  70. const first = { ...record([user], true), querySource: "subagent" };
  71. const sidecar = { ...record([user]), querySource: "session_title" };
  72. const next: ModelIoJsonlEntry = {
  73. querySource: "subagent",
  74. request: {
  75. body: { model: "glm-test", messages: [assistant, result] },
  76. bodyMessagesKind: "delta",
  77. bodyMessageOffset: 1,
  78. },
  79. };
  80. const output = deriveTrajectoriesFromModelIoEntries([first, sidecar, next], {
  81. querySource: "subagent",
  82. });
  83. equal(output.trajectories.length, 1);
  84. deepStrictEqual(output.trajectories[0]?.requestBody.messages, [user, assistant, result]);
  85. });
  86. // 请求规则会删掉纯空白文字块,摘要的 text 仍保留这些空白(E2E I76 形态 A P W 1、W A P 1)。
  87. for (const text of ["read\n\ning", "\n\nreading", "reading\n\n"]) {
  88. it(`restores the recorded reply when only whitespace blocks were dropped: ${JSON.stringify(text)}`, () => {
  89. const assistant = {
  90. role: "assistant",
  91. content: [
  92. { type: "thinking", thinking: "thought", signature: "sig" },
  93. ...(text === "read\n\ning"
  94. ? [
  95. { type: "text", text: "read" },
  96. { type: "text", text: "ing" },
  97. ]
  98. : [answer]),
  99. toolUse,
  100. ],
  101. };
  102. const first = record([user], true);
  103. const output = deriveTrajectoriesFromModelIoEntries([
  104. { ...first, response: { ...first.response, text } },
  105. record([user, assistant, result]),
  106. ]);
  107. equal(output.trajectories.length, 1);
  108. deepStrictEqual(output.trajectories[0]?.requestBody.messages, [user, assistant, result]);
  109. });
  110. }
  111. it("retains a split when the reply differs by more than dropped whitespace", () => {
  112. const assistant = {
  113. role: "assistant",
  114. content: [{ type: "thinking", thinking: "thought", signature: "sig" }, answer, toolUse],
  115. };
  116. const first = record([user], true);
  117. const output = deriveTrajectoriesFromModelIoEntries([
  118. { ...first, response: { ...first.response, text: "reading!" } },
  119. record([user, assistant, result]),
  120. ]);
  121. equal(output.trajectories.length, 2);
  122. });
  123. for (const changed of [
  124. [{ type: "text", text: "different answer" }, toolUse],
  125. [answer, { ...toolUse, input: { path: "changed.ts" } }],
  126. [answer, { ...toolUse, id: "different-call" }],
  127. ]) {
  128. it(`retains a split when the recorded reply differs: ${JSON.stringify(changed)}`, () => {
  129. const assistant = {
  130. role: "assistant",
  131. content: [{ type: "thinking", thinking: "thought", signature: "sig" }, ...changed],
  132. };
  133. const output = deriveTrajectoriesFromModelIoEntries([
  134. record([user], true),
  135. record([user, assistant, result]),
  136. ]);
  137. equal(output.trajectories.length, 2);
  138. equal(output.trajectories[1]?.reason, "non-incremental-change");
  139. deepStrictEqual(output.trajectories[0]?.requestBody.messages.at(-1), {
  140. role: "assistant",
  141. content: [answer, toolUse],
  142. });
  143. });
  144. }
  145. it("does not borrow reasoning from a request with rewritten history", () => {
  146. const assistant = {
  147. role: "assistant",
  148. content: [{ type: "thinking", thinking: "unrelated", signature: "sig" }, answer, toolUse],
  149. };
  150. const output = deriveTrajectoriesFromModelIoEntries([
  151. record([user], true),
  152. record([{ role: "user", content: "new history" }, assistant, result]),
  153. ]);
  154. equal(output.trajectories.length, 2);
  155. deepStrictEqual(output.trajectories[0]?.requestBody.messages.at(-1), {
  156. role: "assistant",
  157. content: [answer, toolUse],
  158. });
  159. });
  160. it("skips records without request bodies when completing a response", () => {
  161. const assistant = {
  162. role: "assistant",
  163. content: [{ type: "thinking", thinking: "thought", signature: "sig" }, answer, toolUse],
  164. };
  165. const output = deriveTrajectoriesFromModelIoEntries([
  166. record([user], true),
  167. { querySource: "main_turn", error: "session stopped" },
  168. record([user, assistant, result]),
  169. ]);
  170. equal(output.trajectories.length, 1);
  171. equal(output.trajectories[0]?.requestCount, 2);
  172. deepStrictEqual(output.trajectories[0]?.requestBody.messages, [user, assistant, result]);
  173. });
  174. for (const field of ["thinking", "signature"]) {
  175. it(`still splits when an existing history ${field} changes`, () => {
  176. const thinking = { type: "thinking", thinking: "original", signature: "original" };
  177. const assistant = { role: "assistant", content: [thinking, answer, toolUse] };
  178. const changedAssistant = {
  179. role: "assistant",
  180. content: [{ ...thinking, [field]: "changed" }, answer, toolUse],
  181. };
  182. const output = deriveTrajectoriesFromModelIoEntries([
  183. record([user], true),
  184. record([user, assistant, result]),
  185. record([user, changedAssistant, result]),
  186. ]);
  187. equal(output.trajectories.length, 2);
  188. equal(output.trajectories[0]?.requestCount, 2);
  189. equal(output.trajectories[1]?.reason, "non-incremental-change");
  190. });
  191. }
  192. });