system-reminder-source.test.ts 9.6 KB

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