system-reminder-source.test.ts 8.9 KB

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