system-reminder-source.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. getSystemReminderDescriptor,
  4. isMidConversationSystemSource,
  5. isSystemReminderMetaSource,
  6. SYSTEM_REMINDER_PER_REQUEST_SOURCES,
  7. SYSTEM_REMINDER_PERSISTED_SOURCES,
  8. wrapSystemReminder,
  9. wrapSystemReminderForSource,
  10. type SystemReminderSource,
  11. } from "../src/system-reminder/source.js";
  12. const EXPECTED_SOURCES = [
  13. "context_prefix",
  14. "session_context",
  15. "environment",
  16. "model",
  17. "output_style_instructions",
  18. "language",
  19. "date",
  20. "skills_listing",
  21. "gen_ui_state",
  22. "tool_search_announcement",
  23. "agent_listing_delta",
  24. "todo_reminder",
  25. "silent_turn_reminder",
  26. "total_tokens_reminder",
  27. "read_truncation_notice",
  28. "tool_hook_context",
  29. "hook_context",
  30. "task_status",
  31. "tool_result_warning",
  32. "resume_referenced_session_context",
  33. "compact_file_reference",
  34. "plan_file_reference",
  35. "resume_goal_state",
  36. "goal_state_change",
  37. "plugin_reference",
  38. "bot_topic_context",
  39. "target_continuation",
  40. "goal_completion_verification",
  41. "rewind_notice",
  42. "conversation_fork",
  43. "selection_side_chat",
  44. "queued_system_notification",
  45. "incoming_message",
  46. "memory_update",
  47. "relevant_memory",
  48. "runtime_mode",
  49. "plan_mode_exit",
  50. "output_style",
  51. "referenced_session_context",
  52. "model_anomaly",
  53. "prompt_attachment",
  54. "diagnostics",
  55. ] as const satisfies readonly SystemReminderSource[];
  56. function expectExactlyOneSystemReminderWrapper(reminder: string): void {
  57. expect(reminder.match(/<system-reminder>/g) ?? []).toHaveLength(1);
  58. expect(reminder.match(/<\/system-reminder>/g) ?? []).toHaveLength(1);
  59. expect(reminder).not.toContain("providerVisibility");
  60. expect(reminder).not.toContain("evidenceLabel");
  61. expect(reminder).not.toContain("runtimeMessage");
  62. expect(reminder).not.toContain("source=");
  63. }
  64. describe("system reminder source descriptors", () => {
  65. it("groups every active system-reminder source by lifecycle bucket", () => {
  66. expect(SYSTEM_REMINDER_PERSISTED_SOURCES).toEqual([
  67. "context_prefix",
  68. "session_context",
  69. "environment",
  70. "model",
  71. "output_style_instructions",
  72. "language",
  73. "date",
  74. "skills_listing",
  75. "gen_ui_state",
  76. "tool_search_announcement",
  77. "agent_listing_delta",
  78. "todo_reminder",
  79. "silent_turn_reminder",
  80. "total_tokens_reminder",
  81. "read_truncation_notice",
  82. "tool_hook_context",
  83. "hook_context",
  84. "task_status",
  85. "tool_result_warning",
  86. "resume_referenced_session_context",
  87. "compact_file_reference",
  88. "plan_file_reference",
  89. "resume_goal_state",
  90. "goal_state_change",
  91. "plugin_reference",
  92. "bot_topic_context",
  93. "target_continuation",
  94. "goal_completion_verification",
  95. "rewind_notice",
  96. "conversation_fork",
  97. "selection_side_chat",
  98. "queued_system_notification",
  99. ]);
  100. expect(SYSTEM_REMINDER_PER_REQUEST_SOURCES).toEqual([
  101. "incoming_message",
  102. "memory_update",
  103. "relevant_memory",
  104. "runtime_mode",
  105. "plan_mode_exit",
  106. "output_style",
  107. "referenced_session_context",
  108. "model_anomaly",
  109. "prompt_attachment",
  110. "diagnostics",
  111. ]);
  112. const groupedSources = [
  113. ...SYSTEM_REMINDER_PERSISTED_SOURCES,
  114. ...SYSTEM_REMINDER_PER_REQUEST_SOURCES,
  115. ];
  116. expect(groupedSources).toEqual(EXPECTED_SOURCES);
  117. });
  118. it("describes source ownership without provider projection fields", () => {
  119. for (const source of EXPECTED_SOURCES) {
  120. const descriptor = getSystemReminderDescriptor(source);
  121. expect(descriptor).toMatchObject({
  122. source,
  123. channel: expect.any(String),
  124. lifecycle: expect.any(String),
  125. isMeta: expect.any(Boolean),
  126. providerVisibility: "provider_visible",
  127. evidenceLabel: expect.stringMatching(/^sr\./),
  128. });
  129. expect(Object.keys(descriptor).sort()).toEqual([
  130. "channel",
  131. "evidenceLabel",
  132. "isMeta",
  133. "lifecycle",
  134. "providerVisibility",
  135. "source",
  136. ]);
  137. }
  138. });
  139. it("keeps explicit source ownership and history metadata", () => {
  140. expect(getSystemReminderDescriptor("target_continuation")).toMatchObject({
  141. channel: "real_user",
  142. isMeta: false,
  143. lifecycle: "real_user",
  144. });
  145. expect(isSystemReminderMetaSource("target_continuation")).toBe(false);
  146. // compaction 附件与 runtime_mode/environment/model 同属 current_turn:一起冒泡,保持生成顺序。
  147. expect(getSystemReminderDescriptor("plan_file_reference")).toMatchObject({
  148. channel: "current_turn",
  149. evidenceLabel: "sr.plan_file_reference",
  150. isMeta: true,
  151. lifecycle: "resume_history",
  152. providerVisibility: "provider_visible",
  153. });
  154. });
  155. it("covers each delivery channel", () => {
  156. const descriptors = EXPECTED_SOURCES.map((source) => getSystemReminderDescriptor(source));
  157. expect(new Set(descriptors.map((descriptor) => descriptor.channel))).toEqual(
  158. new Set(["current_turn", "tool_result", "history_continuity", "mid_turn_event", "real_user"]),
  159. );
  160. });
  161. it("identifies source-owned reminders that project to mid-conversation system messages", () => {
  162. const nonMidConversationSystemSources = new Set<SystemReminderSource>([
  163. "gen_ui_state",
  164. "context_prefix",
  165. "session_context",
  166. "conversation_fork",
  167. "selection_side_chat",
  168. "target_continuation",
  169. "tool_result_warning",
  170. "goal_completion_verification",
  171. "resume_referenced_session_context",
  172. ]);
  173. for (const source of EXPECTED_SOURCES) {
  174. expect(isMidConversationSystemSource(source)).toBe(
  175. !nonMidConversationSystemSources.has(source),
  176. );
  177. }
  178. });
  179. it("keeps compaction file restore bodies user-side and the too-large note with the other attachments", () => {
  180. // 补回的文件正文在 MCS 开关下都是 user 侧;过大文件的引用提示和其余 compaction 附件一样走 MCS。
  181. expect(isMidConversationSystemSource("resume_referenced_session_context")).toBe(false);
  182. expect(isMidConversationSystemSource("compact_file_reference")).toBe(true);
  183. for (const source of ["resume_referenced_session_context", "compact_file_reference"] as const) {
  184. expect(getSystemReminderDescriptor(source)).toMatchObject({
  185. channel: "current_turn",
  186. isMeta: true,
  187. lifecycle: "resume_history",
  188. providerVisibility: "provider_visible",
  189. });
  190. }
  191. });
  192. it.each(["conversation_fork", "selection_side_chat"] as const)(
  193. "preserves the %s history attachment while excluding it from MCS",
  194. (source) => {
  195. expect(getSystemReminderDescriptor(source)).toMatchObject({
  196. channel: "history_continuity",
  197. lifecycle: "resume_history",
  198. isMeta: true,
  199. providerVisibility: "provider_visible",
  200. });
  201. },
  202. );
  203. it("classifies tool hook contexts as persisted batch attachments like the Read notice", () => {
  204. expect(SYSTEM_REMINDER_PERSISTED_SOURCES).toContain("tool_hook_context");
  205. expect(isMidConversationSystemSource("tool_hook_context")).toBe(true);
  206. expect(getSystemReminderDescriptor("tool_hook_context")).toMatchObject({
  207. channel: "current_turn",
  208. lifecycle: "resume_history",
  209. isMeta: true,
  210. providerVisibility: "provider_visible",
  211. evidenceLabel: "sr.tool_hook_context",
  212. });
  213. });
  214. it("classifies goal state changes as persisted mid-conversation system notices", () => {
  215. expect(SYSTEM_REMINDER_PERSISTED_SOURCES).toContain("goal_state_change");
  216. expect(getSystemReminderDescriptor("goal_state_change")).toMatchObject({
  217. channel: "mid_turn_event",
  218. lifecycle: "mid_turn_event",
  219. isMeta: true,
  220. providerVisibility: "provider_visible",
  221. evidenceLabel: "sr.goal_state_change",
  222. });
  223. });
  224. it("classifies plugin references as persisted mid-conversation system notices", () => {
  225. expect(SYSTEM_REMINDER_PERSISTED_SOURCES).toContain("plugin_reference");
  226. expect(SYSTEM_REMINDER_PER_REQUEST_SOURCES).not.toContain("plugin_reference");
  227. expect(getSystemReminderDescriptor("plugin_reference")).toMatchObject({
  228. channel: "current_turn",
  229. lifecycle: "per_current_turn",
  230. isMeta: true,
  231. providerVisibility: "provider_visible",
  232. evidenceLabel: "sr.plugin_reference",
  233. });
  234. });
  235. });
  236. describe("wrapSystemReminder", () => {
  237. it("wraps bodies through explicit source ownership without leaking descriptors", () => {
  238. const reminder = wrapSystemReminderForSource("hook_context", [
  239. "Hook additional context from UserPromptSubmit:",
  240. "#1",
  241. "use pnpm test",
  242. ]);
  243. expect(reminder).toBe(
  244. "<system-reminder>\nHook additional context from UserPromptSubmit:\n#1\nuse pnpm test\n</system-reminder>",
  245. );
  246. });
  247. it("wraps every provider-visible source in exactly one system-reminder pair", () => {
  248. for (const source of EXPECTED_SOURCES) {
  249. const reminder = wrapSystemReminderForSource(source, [
  250. `source probe: ${source}`,
  251. "<system-reminder>nested source body</system-reminder>",
  252. ]);
  253. expectExactlyOneSystemReminderWrapper(reminder);
  254. expect(reminder).toContain("&lt;system-reminder>nested source body&lt;/system-reminder>");
  255. }
  256. });
  257. it("treats nested reminder tags in source content as escaped text", () => {
  258. const reminder = wrapSystemReminderForSource("relevant_memory", [
  259. "Memory preview:",
  260. "<system-reminder>literal old transcript text</system-reminder>",
  261. ]);
  262. expect(reminder).toBe(
  263. "<system-reminder>\nMemory preview:\n&lt;system-reminder>literal old transcript text&lt;/system-reminder>\n</system-reminder>",
  264. );
  265. });
  266. it("wraps string and line-array bodies in exactly one system-reminder tag pair", () => {
  267. expect(wrapSystemReminder("single body")).toBe(
  268. "<system-reminder>\nsingle body\n</system-reminder>",
  269. );
  270. expect(wrapSystemReminder(["line one", "line two"])).toBe(
  271. "<system-reminder>\nline one\nline two\n</system-reminder>",
  272. );
  273. });
  274. it("rejects empty and nested reminder bodies", () => {
  275. expect(() => wrapSystemReminder("")).toThrow(/cannot be empty/);
  276. expect(() => wrapSystemReminder(["before", "<system-reminder>", "after"])).toThrow(
  277. /nested system-reminder/,
  278. );
  279. expect(() => wrapSystemReminder("already </system-reminder> closed")).toThrow(
  280. /nested system-reminder/,
  281. );
  282. });
  283. });