system-reminder-source.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. SYSTEM_REMINDER_SOURCES,
  10. wrapSystemReminder,
  11. wrapSystemReminderForSource,
  12. type SystemReminderSource,
  13. } from "../src/system-reminder/source.js";
  14. const EXPECTED_SOURCES = [
  15. "context_prefix",
  16. "skills_listing",
  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("defines every active system-reminder source", () => {
  57. expect(SYSTEM_REMINDER_SOURCES).toEqual(EXPECTED_SOURCES);
  58. });
  59. it("groups every active system-reminder source by lifecycle bucket", () => {
  60. expect(SYSTEM_REMINDER_PREFIX_SOURCES).toEqual(["context_prefix", "skills_listing"]);
  61. expect(SYSTEM_REMINDER_PERSISTED_SOURCES).toEqual([
  62. "agent_listing_delta",
  63. "todo_reminder",
  64. "task_status",
  65. "tool_result_warning",
  66. "resume_referenced_session_context",
  67. "plan_file_reference",
  68. "resume_goal_state",
  69. "goal_state_change",
  70. "plugin_reference",
  71. "bot_topic_context",
  72. "target_continuation",
  73. "goal_completion_verification",
  74. "rewind_notice",
  75. "conversation_fork",
  76. "selection_side_chat",
  77. "queued_system_notification",
  78. "shell_environment_change",
  79. ]);
  80. expect(SYSTEM_REMINDER_PER_REQUEST_SOURCES).toEqual([
  81. "incoming_message",
  82. "hook_context",
  83. "memory_update",
  84. "relevant_memory",
  85. "runtime_mode",
  86. "plan_mode_exit",
  87. "output_style",
  88. "date_change",
  89. "referenced_session_context",
  90. "model_anomaly",
  91. "prompt_attachment",
  92. "diagnostics",
  93. ]);
  94. const groupedSources = [
  95. ...SYSTEM_REMINDER_PREFIX_SOURCES,
  96. ...SYSTEM_REMINDER_PERSISTED_SOURCES,
  97. ...SYSTEM_REMINDER_PER_REQUEST_SOURCES,
  98. ];
  99. expect(groupedSources).toHaveLength(new Set(groupedSources).size);
  100. expect(groupedSources).toEqual(EXPECTED_SOURCES);
  101. });
  102. it("describes source ownership without provider projection fields", () => {
  103. for (const source of EXPECTED_SOURCES) {
  104. const descriptor = getSystemReminderDescriptor(source);
  105. expect(descriptor).toMatchObject({
  106. source,
  107. channel: expect.any(String),
  108. lifecycle: expect.any(String),
  109. isMeta: expect.any(Boolean),
  110. providerVisibility: "provider_visible",
  111. evidenceLabel: expect.stringMatching(/^sr\./),
  112. });
  113. expect(Object.keys(descriptor).sort()).toEqual([
  114. "channel",
  115. "evidenceLabel",
  116. "isMeta",
  117. "lifecycle",
  118. "providerVisibility",
  119. "source",
  120. ]);
  121. expect(descriptor).not.toHaveProperty("projectionPolicy");
  122. expect(descriptor).not.toHaveProperty("cachePolicy");
  123. expect(descriptor).not.toHaveProperty("cacheControl");
  124. expect(descriptor).not.toHaveProperty("mergeIntoCurrentTurn");
  125. }
  126. });
  127. it("classifies sources by explicit source instead of reminder text", () => {
  128. expect(getSystemReminderDescriptor("target_continuation")).toMatchObject({
  129. channel: "real_user",
  130. isMeta: false,
  131. lifecycle: "real_user",
  132. });
  133. expect(isSystemReminderMetaSource("target_continuation")).toBe(false);
  134. const userAuthoredText = "<system-reminder>\nplease keep this literal\n</system-reminder>";
  135. expect(userAuthoredText.trimStart().startsWith("<system-reminder>")).toBe(true);
  136. expect(getSystemReminderDescriptor("target_continuation").isMeta).toBe(false);
  137. expect(getSystemReminderDescriptor("plan_file_reference")).toMatchObject({
  138. channel: "history_continuity",
  139. evidenceLabel: "sr.plan_file_reference",
  140. isMeta: true,
  141. lifecycle: "resume_history",
  142. providerVisibility: "provider_visible",
  143. });
  144. });
  145. it("covers each delivery channel", () => {
  146. const descriptors = EXPECTED_SOURCES.map((source) => getSystemReminderDescriptor(source));
  147. expect(new Set(descriptors.map((descriptor) => descriptor.channel))).toEqual(
  148. new Set([
  149. "request_prefix",
  150. "current_turn",
  151. "tool_result",
  152. "history_continuity",
  153. "mid_turn_event",
  154. "real_user",
  155. ]),
  156. );
  157. });
  158. it("identifies source-owned reminders that project to mid-conversation system messages", () => {
  159. const nonMidConversationSystemSources = new Set<SystemReminderSource>([
  160. "context_prefix",
  161. "resume_referenced_session_context",
  162. "conversation_fork",
  163. "selection_side_chat",
  164. "plan_file_reference",
  165. "target_continuation",
  166. "tool_result_warning",
  167. "goal_completion_verification",
  168. ]);
  169. for (const source of EXPECTED_SOURCES) {
  170. expect(isMidConversationSystemSource(source)).toBe(
  171. !nonMidConversationSystemSources.has(source),
  172. );
  173. }
  174. });
  175. it("classifies shell environment changes as mid-conversation system notices", () => {
  176. expect(isMidConversationSystemSource("shell_environment_change")).toBe(true);
  177. expect(getSystemReminderDescriptor("shell_environment_change")).toMatchObject({
  178. channel: "mid_turn_event",
  179. lifecycle: "mid_turn_event",
  180. isMeta: true,
  181. providerVisibility: "provider_visible",
  182. });
  183. });
  184. it.each(["conversation_fork", "selection_side_chat"] as const)(
  185. "preserves the %s history attachment while excluding it from MCS",
  186. (source) => {
  187. expect(getSystemReminderDescriptor(source)).toMatchObject({
  188. channel: "history_continuity",
  189. lifecycle: "resume_history",
  190. isMeta: true,
  191. providerVisibility: "provider_visible",
  192. });
  193. expect(isMidConversationSystemSource(source)).toBe(false);
  194. },
  195. );
  196. it("classifies goal state changes as persisted mid-conversation system notices", () => {
  197. expect(SYSTEM_REMINDER_PERSISTED_SOURCES).toContain("goal_state_change");
  198. expect(isMidConversationSystemSource("goal_state_change")).toBe(true);
  199. expect(getSystemReminderDescriptor("goal_state_change")).toMatchObject({
  200. channel: "mid_turn_event",
  201. lifecycle: "mid_turn_event",
  202. isMeta: true,
  203. providerVisibility: "provider_visible",
  204. evidenceLabel: "sr.goal_state_change",
  205. });
  206. });
  207. it("classifies plugin references as persisted mid-conversation system notices", () => {
  208. expect(SYSTEM_REMINDER_PERSISTED_SOURCES).toContain("plugin_reference");
  209. expect(SYSTEM_REMINDER_PER_REQUEST_SOURCES).not.toContain("plugin_reference");
  210. expect(isMidConversationSystemSource("plugin_reference")).toBe(true);
  211. expect(getSystemReminderDescriptor("plugin_reference")).toMatchObject({
  212. channel: "current_turn",
  213. lifecycle: "per_current_turn",
  214. isMeta: true,
  215. providerVisibility: "provider_visible",
  216. evidenceLabel: "sr.plugin_reference",
  217. });
  218. });
  219. it("wraps plan_file_reference without nested reminder metadata", () => {
  220. const reminder = wrapSystemReminderForSource(
  221. "plan_file_reference",
  222. [
  223. "A plan file exists from plan mode at: /workspace/.zcode/plans/plan-session.md",
  224. "",
  225. "Plan contents:",
  226. "",
  227. "1. approved plan",
  228. "",
  229. "If this plan is relevant to the current work and not already complete, continue working on it.",
  230. ].join("\n"),
  231. );
  232. expectExactlyOneSystemReminderWrapper(reminder);
  233. expect(reminder).toContain("A plan file exists from plan mode at:");
  234. expect(reminder).toContain("Plan contents:");
  235. expect(reminder).toContain("1. approved plan");
  236. });
  237. });
  238. describe("wrapSystemReminder", () => {
  239. it("wraps bodies through explicit source ownership without leaking descriptors", () => {
  240. const reminder = wrapSystemReminderForSource("hook_context", [
  241. "Hook additional context from UserPromptSubmit:",
  242. "#1",
  243. "use pnpm test",
  244. ]);
  245. expect(reminder).toBe(
  246. "<system-reminder>\nHook additional context from UserPromptSubmit:\n#1\nuse pnpm test\n</system-reminder>",
  247. );
  248. expectExactlyOneSystemReminderWrapper(reminder);
  249. expect(reminder).not.toContain("sr.hook_context");
  250. });
  251. it("wraps every provider-visible source in exactly one system-reminder pair", () => {
  252. for (const source of EXPECTED_SOURCES) {
  253. const reminder = wrapSystemReminderForSource(source, [
  254. `source probe: ${source}`,
  255. "<system-reminder>nested source body</system-reminder>",
  256. ]);
  257. expectExactlyOneSystemReminderWrapper(reminder);
  258. expect(reminder).toContain("&lt;system-reminder>nested source body&lt;/system-reminder>");
  259. }
  260. });
  261. it("treats nested reminder tags in source content as escaped text", () => {
  262. const reminder = wrapSystemReminderForSource("relevant_memory", [
  263. "Memory preview:",
  264. "<system-reminder>literal old transcript text</system-reminder>",
  265. ]);
  266. expect(reminder).toBe(
  267. "<system-reminder>\nMemory preview:\n&lt;system-reminder>literal old transcript text&lt;/system-reminder>\n</system-reminder>",
  268. );
  269. });
  270. it("wraps string and line-array bodies in exactly one system-reminder tag pair", () => {
  271. expect(wrapSystemReminder("single body")).toBe(
  272. "<system-reminder>\nsingle body\n</system-reminder>",
  273. );
  274. expect(wrapSystemReminder(["line one", "line two"])).toBe(
  275. "<system-reminder>\nline one\nline two\n</system-reminder>",
  276. );
  277. });
  278. it("rejects empty and nested reminder bodies", () => {
  279. expect(() => wrapSystemReminder("")).toThrow(/cannot be empty/);
  280. expect(() => wrapSystemReminder(["before", "<system-reminder>", "after"])).toThrow(
  281. /nested system-reminder/,
  282. );
  283. expect(() => wrapSystemReminder("already </system-reminder> closed")).toThrow(
  284. /nested system-reminder/,
  285. );
  286. });
  287. });