zcodeSessionVisibleContent.test.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. getZCodeGoalActiveIterationCount,
  4. getZCodeGoalIterationByAssistantMessageId,
  5. getZCodeUserVisibleMessages,
  6. getConversationMessageProjectionPolicy,
  7. getConversationModelOnlyTurnTriggerSource,
  8. isZCodeModelOnlySyntheticUserMessage,
  9. resolveZCodeVisibleSessionTitle,
  10. type ZCodeMessagePart,
  11. type ZCodeMessageWithParts,
  12. } from "../src/index.js";
  13. const model = {
  14. providerId: "glm",
  15. modelId: "glm-4.6",
  16. };
  17. type ZCodeUserMessageInfo = Extract<ZCodeMessageWithParts["info"], { role: "user" }>;
  18. type ZCodeUserMessageInfoPatch = Partial<
  19. Pick<
  20. ZCodeUserMessageInfo,
  21. "metadata" | "semantics" | "source" | "synthetic" | "time" | "visibility"
  22. >
  23. >;
  24. function userMessage(
  25. messageId: string,
  26. text: string,
  27. partPatch: Partial<Extract<ZCodeMessagePart, { type: "text" }>> = {},
  28. createdAt = 1,
  29. infoPatch: ZCodeUserMessageInfoPatch = {},
  30. ): ZCodeMessageWithParts {
  31. const part: Extract<ZCodeMessagePart, { type: "text" }> = {
  32. partId: `part_${messageId}`,
  33. sessionId: "sess_1",
  34. messageId,
  35. type: "text",
  36. text,
  37. ...partPatch,
  38. };
  39. return {
  40. info: {
  41. messageId,
  42. sessionId: "sess_1",
  43. role: "user",
  44. time: { created: createdAt },
  45. agent: "zcode-agent",
  46. model,
  47. ...infoPatch,
  48. },
  49. parts: [part],
  50. };
  51. }
  52. function assistantMessage(
  53. messageId: string,
  54. text: string,
  55. createdAt: number,
  56. ): ZCodeMessageWithParts {
  57. return {
  58. info: {
  59. messageId,
  60. sessionId: "sess_1",
  61. role: "assistant",
  62. time: { created: createdAt, completed: createdAt + 10 },
  63. agent: "zcode-agent",
  64. model,
  65. path: { cwd: "/workspace/app", root: "/workspace/app" },
  66. cost: 0,
  67. tokens: {
  68. input: 1,
  69. output: 1,
  70. reasoning: 0,
  71. total: 2,
  72. cache: { read: 0, write: 0 },
  73. },
  74. },
  75. parts: [
  76. {
  77. partId: `part_${messageId}`,
  78. sessionId: "sess_1",
  79. messageId,
  80. type: "text",
  81. text,
  82. },
  83. ],
  84. };
  85. }
  86. describe("ZCode session visible content", () => {
  87. it("only advances failed verifier iteration counts for active goals", () => {
  88. const failedTimeline = [
  89. {
  90. goalIteration: 1,
  91. status: "completed" as const,
  92. verification: {
  93. nextAction: "继续下一轮。",
  94. passed: false,
  95. reason: "目标还没完成。",
  96. },
  97. },
  98. ];
  99. const cancelledTimeline = [
  100. {
  101. goalIteration: 1,
  102. status: "cancelled" as const,
  103. },
  104. ];
  105. expect(
  106. getZCodeGoalActiveIterationCount({
  107. targetStatus: "active",
  108. timeline: failedTimeline,
  109. }),
  110. ).toBe(2);
  111. expect(
  112. getZCodeGoalActiveIterationCount({
  113. targetStatus: "paused",
  114. timeline: cancelledTimeline,
  115. }),
  116. ).toBe(1);
  117. });
  118. it("filters background task notifications from user-visible messages", () => {
  119. const realUserMessage = userMessage("msg_real", "继续总结结果");
  120. const notification = userMessage(
  121. "msg_background",
  122. [
  123. "<task-notification>",
  124. " <task-id>exec_bg</task-id>",
  125. " <status>completed</status>",
  126. "</task-notification>",
  127. ].join("\n"),
  128. {
  129. synthetic: true,
  130. metadata: { source: "background_task" },
  131. },
  132. );
  133. expect(isZCodeModelOnlySyntheticUserMessage(notification)).toBe(true);
  134. expect(getZCodeUserVisibleMessages([realUserMessage, notification])).toEqual([realUserMessage]);
  135. });
  136. it("filters legacy synthetic task notification text without metadata", () => {
  137. const notification = userMessage(
  138. "msg_legacy_background",
  139. "<task-notification>\n <status>completed</status>\n</task-notification>",
  140. {
  141. synthetic: true,
  142. },
  143. );
  144. expect(isZCodeModelOnlySyntheticUserMessage(notification)).toBe(true);
  145. expect(getZCodeUserVisibleMessages([notification])).toEqual([]);
  146. });
  147. it("filters compact summary user messages from user-visible messages", () => {
  148. const summary: ZCodeMessageWithParts = {
  149. info: {
  150. messageId: "msg_summary",
  151. sessionId: "sess_1",
  152. role: "user",
  153. time: { created: 10 },
  154. agent: "zcode-agent",
  155. model,
  156. },
  157. parts: [
  158. {
  159. partId: "part_summary",
  160. sessionId: "sess_1",
  161. messageId: "msg_summary",
  162. type: "compaction",
  163. auto: false,
  164. metadata: {
  165. operationId: "cmp-summary",
  166. phase: "standalone_turn",
  167. trigger: "manual",
  168. },
  169. },
  170. ],
  171. };
  172. expect(getZCodeUserVisibleMessages([summary])).toEqual([]);
  173. });
  174. it("filters background subagent notifications from user-visible messages", () => {
  175. const notification = userMessage(
  176. "msg_subagent_background",
  177. "<subagent-notification>\nAgent Explore task completed.\n</subagent-notification>",
  178. {
  179. synthetic: true,
  180. metadata: { source: "subagent" },
  181. },
  182. );
  183. expect(isZCodeModelOnlySyntheticUserMessage(notification)).toBe(true);
  184. expect(getZCodeUserVisibleMessages([notification])).toEqual([]);
  185. });
  186. it("filters canonical and source-only subagent messages from visible content and titles", () => {
  187. const canonical = userMessage(
  188. "msg_subagent_message_canonical",
  189. "internal canonical response",
  190. { synthetic: true },
  191. 1,
  192. {
  193. source: "subagent_message",
  194. synthetic: true,
  195. visibility: "model-only",
  196. },
  197. );
  198. const infoSourceOnly = userMessage(
  199. "msg_subagent_message_info_source",
  200. "internal info source response",
  201. { synthetic: true },
  202. 2,
  203. { source: "subagent_message", synthetic: true },
  204. );
  205. const partSourceOnly = userMessage(
  206. "msg_subagent_message_part_source",
  207. "internal part source response",
  208. {
  209. synthetic: true,
  210. metadata: { source: "subagent_message" },
  211. },
  212. 3,
  213. { synthetic: true },
  214. );
  215. const realUserMessage = userMessage("msg_real_after_subagent_message", "真实用户问题", {}, 4);
  216. expect(
  217. getZCodeUserVisibleMessages([canonical, infoSourceOnly, partSourceOnly, realUserMessage]),
  218. ).toEqual([realUserMessage]);
  219. expect(
  220. resolveZCodeVisibleSessionTitle({
  221. messages: [infoSourceOnly, partSourceOnly, realUserMessage],
  222. target: null,
  223. }),
  224. ).toBe("真实用户问题");
  225. });
  226. it("classifies subagent_message as one provider-context model-only turn trigger", () => {
  227. const sourceOnly = userMessage(
  228. "msg_subagent_message_turn_trigger",
  229. "internal coordinator response",
  230. { synthetic: true, metadata: { source: "subagent_message" } },
  231. 1,
  232. { source: "subagent_message", synthetic: true },
  233. );
  234. expect(getConversationMessageProjectionPolicy(sourceOnly)).toBe("providerContextOnly");
  235. expect(getConversationModelOnlyTurnTriggerSource(sourceOnly)).toBe("subagent_message");
  236. });
  237. it("keeps persisted plugin references in provider context without creating a UI turn", () => {
  238. const pluginReference = userMessage(
  239. "msg_plugin_reference",
  240. "<plugin_reference>demo</plugin_reference>",
  241. { synthetic: true, metadata: { source: "plugin_reference" } },
  242. 1,
  243. {
  244. source: "plugin_reference",
  245. synthetic: true,
  246. visibility: "model-only",
  247. },
  248. );
  249. expect(getConversationMessageProjectionPolicy(pluginReference)).toBe("providerContextOnly");
  250. expect(isZCodeModelOnlySyntheticUserMessage(pluginReference)).toBe(true);
  251. expect(getConversationModelOnlyTurnTriggerSource(pluginReference)).toBeNull();
  252. expect(getZCodeUserVisibleMessages([pluginReference])).toEqual([]);
  253. });
  254. it("uses the target fallback when a subagent message is the only user-like input", () => {
  255. const sourceOnly = userMessage(
  256. "msg_subagent_message_target_fallback",
  257. "internal response must not become the title",
  258. { synthetic: true, metadata: { source: "subagent_message" } },
  259. 1,
  260. { synthetic: true },
  261. );
  262. expect(
  263. resolveZCodeVisibleSessionTitle({
  264. messages: [sourceOnly],
  265. target: {
  266. createdAt: 1,
  267. objective: "完成权限链路检查",
  268. sessionId: "sess_1",
  269. status: "active",
  270. summaryTitle: null,
  271. targetId: "goal_subagent_message",
  272. timeUsedSeconds: 0,
  273. tokenBudget: null,
  274. tokensUsed: 0,
  275. updatedAt: 1,
  276. },
  277. }),
  278. ).toBe("完成权限链路检查");
  279. });
  280. it("filters rewind notices from user-visible messages", () => {
  281. const notification = userMessage(
  282. "msg_rewind",
  283. [
  284. "<system-reminder>",
  285. "Conversation rewind applied.",
  286. "Continue from the conversation state before the target user message.",
  287. "</system-reminder>",
  288. ].join("\n"),
  289. {
  290. synthetic: true,
  291. metadata: { source: "rewind" },
  292. },
  293. );
  294. expect(isZCodeModelOnlySyntheticUserMessage(notification)).toBe(true);
  295. expect(getZCodeUserVisibleMessages([notification])).toEqual([]);
  296. });
  297. it("filters model-only goal continuation reminders from user-visible messages", () => {
  298. const realUserMessage = userMessage("msg_real_goal", "加一个地址系统,可以加,可以选");
  299. const continuation = userMessage(
  300. "msg_goal_continuation",
  301. [
  302. '<system-reminder source="goal-continuation">',
  303. "Continue working toward the active session goal.",
  304. "</system-reminder>",
  305. ].join("\n"),
  306. {
  307. synthetic: true,
  308. metadata: {
  309. source: "goal-continuation",
  310. visibility: "model-only",
  311. },
  312. },
  313. );
  314. expect(isZCodeModelOnlySyntheticUserMessage(continuation)).toBe(true);
  315. expect(getZCodeUserVisibleMessages([realUserMessage, continuation])).toEqual([realUserMessage]);
  316. });
  317. it("filters model-only goal state change reminders from user-visible messages", () => {
  318. const realUserMessage = userMessage("msg_real_goal_state", "继续做目标");
  319. const reminder = userMessage(
  320. "msg_goal_state_change",
  321. "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal.",
  322. {
  323. synthetic: true,
  324. metadata: { source: "goal_state_change" },
  325. },
  326. 2,
  327. {
  328. synthetic: true,
  329. source: "goal_state_change",
  330. },
  331. );
  332. expect(isZCodeModelOnlySyntheticUserMessage(reminder)).toBe(true);
  333. expect(getZCodeUserVisibleMessages([realUserMessage, reminder])).toEqual([realUserMessage]);
  334. });
  335. it("filters legacy goal continuation reminders without metadata", () => {
  336. const continuation = userMessage(
  337. "msg_legacy_goal_continuation",
  338. [
  339. '<system-reminder source="goal-continuation">',
  340. "<untrusted_objective>",
  341. "优化性能到 60fps",
  342. "</untrusted_objective>",
  343. "</system-reminder>",
  344. ].join("\n"),
  345. );
  346. expect(isZCodeModelOnlySyntheticUserMessage(continuation)).toBe(true);
  347. expect(getZCodeUserVisibleMessages([continuation])).toEqual([]);
  348. expect(
  349. resolveZCodeVisibleSessionTitle({
  350. messages: [continuation],
  351. target: {
  352. createdAt: 1,
  353. objective: "优化性能到 60fps",
  354. sessionId: "sess_1",
  355. status: "active",
  356. summaryTitle: null,
  357. targetId: "goal_1",
  358. timeUsedSeconds: 0,
  359. tokenBudget: null,
  360. tokensUsed: 0,
  361. updatedAt: 1,
  362. },
  363. }),
  364. ).toBe("优化性能到 60fps");
  365. });
  366. it("does not count current goal state reminders as a new goal iteration", () => {
  367. const continuation = userMessage(
  368. "msg_goal_continuation",
  369. [
  370. "<system-reminder>",
  371. "Continue working toward the active session goal.",
  372. "</system-reminder>",
  373. ].join("\n"),
  374. { synthetic: true },
  375. 100,
  376. {
  377. synthetic: true,
  378. visibility: "model-only",
  379. metadata: { visibility: "model-only" },
  380. },
  381. );
  382. const firstAssistant = assistantMessage("msg_goal_assistant_1", "第一段。", 200);
  383. const stateReminder = userMessage(
  384. "msg_goal_state",
  385. [
  386. "<system-reminder>",
  387. "Current session goal state (authoritative):",
  388. "Status: active",
  389. "</system-reminder>",
  390. ].join("\n"),
  391. { synthetic: true },
  392. 300,
  393. {
  394. synthetic: true,
  395. visibility: "model-only",
  396. metadata: { visibility: "model-only" },
  397. },
  398. );
  399. const followupAssistant = assistantMessage(
  400. "msg_goal_assistant_1_followup",
  401. "继续第一轮。",
  402. 400,
  403. );
  404. expect(isZCodeModelOnlySyntheticUserMessage(stateReminder)).toBe(true);
  405. const iterations = getZCodeGoalIterationByAssistantMessageId(
  406. [continuation, firstAssistant, stateReminder, followupAssistant],
  407. {
  408. target: {
  409. createdAt: 100,
  410. objective: "优化性能到 60fps",
  411. sessionId: "sess_1",
  412. status: "active",
  413. summaryTitle: null,
  414. targetId: "goal_1",
  415. timeUsedSeconds: 0,
  416. tokenBudget: null,
  417. tokensUsed: 0,
  418. updatedAt: 100,
  419. },
  420. },
  421. );
  422. expect(iterations.get("msg_goal_assistant_1")).toBe(1);
  423. expect(iterations.get("msg_goal_assistant_1_followup")).toBe(1);
  424. });
  425. it("does not double count a visible goal input followed by a model-only continuation", () => {
  426. const visibleGoalInput = userMessage("msg_visible_goal", "增加单测覆盖率,直到 90%", {}, 101);
  427. const continuation = userMessage(
  428. "msg_goal_continuation",
  429. [
  430. '<system-reminder source="goal-continuation">',
  431. "Continue working toward the active session goal.",
  432. "</system-reminder>",
  433. ].join("\n"),
  434. { synthetic: true },
  435. 102,
  436. {
  437. synthetic: true,
  438. source: "goal-continuation",
  439. visibility: "model-only",
  440. metadata: { source: "goal-continuation", visibility: "model-only" },
  441. },
  442. );
  443. const assistant = assistantMessage("msg_goal_assistant_1", "第一轮。", 200);
  444. const iterations = getZCodeGoalIterationByAssistantMessageId(
  445. [visibleGoalInput, continuation, assistant],
  446. {
  447. target: {
  448. createdAt: 100,
  449. objective: "增加单测覆盖率,直到 90%",
  450. sessionId: "sess_1",
  451. status: "active",
  452. summaryTitle: null,
  453. targetId: "goal_1",
  454. timeUsedSeconds: 0,
  455. tokenBudget: null,
  456. tokensUsed: 0,
  457. updatedAt: 100,
  458. },
  459. },
  460. );
  461. expect(iterations.get("msg_goal_assistant_1")).toBe(1);
  462. });
  463. it("uses a visible goal input as a fallback boundary when continuation is absent", () => {
  464. const visibleGoalInput = userMessage("msg_visible_goal", "增加单测覆盖率,直到 90%", {}, 101);
  465. const assistant = assistantMessage("msg_goal_assistant_1", "第一轮。", 200);
  466. const iterations = getZCodeGoalIterationByAssistantMessageId([visibleGoalInput, assistant], {
  467. target: {
  468. createdAt: 100,
  469. objective: "增加单测覆盖率,直到 90%",
  470. sessionId: "sess_1",
  471. status: "active",
  472. summaryTitle: null,
  473. targetId: "goal_1",
  474. timeUsedSeconds: 0,
  475. tokenBudget: null,
  476. tokensUsed: 0,
  477. updatedAt: 100,
  478. },
  479. });
  480. expect(iterations.get("msg_goal_assistant_1")).toBe(1);
  481. });
  482. it("caps assistant display iteration by verified goal iteration count", () => {
  483. const firstContinuation = userMessage(
  484. "msg_goal_continuation_1",
  485. [
  486. '<system-reminder source="goal-continuation">',
  487. "Continue working toward the active session goal.",
  488. "</system-reminder>",
  489. ].join("\n"),
  490. { synthetic: true },
  491. 102,
  492. {
  493. synthetic: true,
  494. source: "goal-continuation",
  495. visibility: "model-only",
  496. metadata: { source: "goal-continuation", visibility: "model-only" },
  497. },
  498. );
  499. const firstAssistant = assistantMessage("msg_goal_assistant_1", "第一轮。", 200);
  500. const secondContinuation = userMessage(
  501. "msg_goal_continuation_2",
  502. [
  503. '<system-reminder source="goal-continuation">',
  504. "Continue working toward the active session goal.",
  505. "</system-reminder>",
  506. ].join("\n"),
  507. { synthetic: true },
  508. 300,
  509. {
  510. synthetic: true,
  511. source: "goal-continuation",
  512. visibility: "model-only",
  513. metadata: { source: "goal-continuation", visibility: "model-only" },
  514. },
  515. );
  516. const secondAssistant = assistantMessage("msg_goal_assistant_2", "继续处理。", 400);
  517. const iterations = getZCodeGoalIterationByAssistantMessageId(
  518. [firstContinuation, firstAssistant, secondContinuation, secondAssistant],
  519. {
  520. maxGoalIteration: 1,
  521. target: {
  522. createdAt: 100,
  523. objective: "增加单测覆盖率,直到 90%",
  524. sessionId: "sess_1",
  525. status: "active",
  526. summaryTitle: null,
  527. targetId: "goal_1",
  528. timeUsedSeconds: 0,
  529. tokenBudget: null,
  530. tokensUsed: 0,
  531. updatedAt: 100,
  532. },
  533. },
  534. );
  535. expect(iterations.get("msg_goal_assistant_1")).toBe(1);
  536. expect(iterations.get("msg_goal_assistant_2")).toBe(1);
  537. });
  538. it("does not assign completed goal iteration to follow-up assistant replies", () => {
  539. const visibleGoalInput = userMessage("msg_visible_goal", "把重连恢复修好", {}, 101);
  540. const goalAssistant = assistantMessage("msg_goal_assistant_1", "第一轮已完成。", 200);
  541. const followupUser = userMessage("msg_followup_user", "你刚刚的方案是什么?", {}, 400);
  542. const followupAssistant = assistantMessage(
  543. "msg_followup_assistant",
  544. "刚刚的方案是先补边界测试。",
  545. 500,
  546. );
  547. const iterations = getZCodeGoalIterationByAssistantMessageId(
  548. [visibleGoalInput, goalAssistant, followupUser, followupAssistant],
  549. {
  550. maxGoalIteration: 1,
  551. target: {
  552. createdAt: 100,
  553. objective: "把重连恢复修好",
  554. sessionId: "sess_1",
  555. status: "complete",
  556. summaryTitle: null,
  557. targetId: "goal_1",
  558. timeUsedSeconds: 0,
  559. tokenBudget: null,
  560. tokensUsed: 0,
  561. updatedAt: 300,
  562. },
  563. },
  564. );
  565. expect(iterations.get("msg_goal_assistant_1")).toBe(1);
  566. expect(iterations.get("msg_followup_assistant")).toBeUndefined();
  567. });
  568. it("does not assign paused goal iteration to follow-up assistant replies", () => {
  569. const visibleGoalInput = userMessage("msg_visible_goal", "把停止目标修好", {}, 101);
  570. const goalAssistant = assistantMessage("msg_goal_assistant_1", "第一轮被停止。", 200);
  571. const followupUser = userMessage("msg_followup_user", "nihao", {}, 400);
  572. const followupAssistant = assistantMessage(
  573. "msg_followup_assistant",
  574. "你好!有什么我可以帮你的吗?",
  575. 500,
  576. );
  577. const iterations = getZCodeGoalIterationByAssistantMessageId(
  578. [visibleGoalInput, goalAssistant, followupUser, followupAssistant],
  579. {
  580. maxGoalIteration: 1,
  581. target: {
  582. createdAt: 100,
  583. objective: "把停止目标修好",
  584. sessionId: "sess_1",
  585. status: "paused",
  586. summaryTitle: null,
  587. targetId: "goal_1",
  588. timeUsedSeconds: 0,
  589. tokenBudget: null,
  590. tokensUsed: 0,
  591. updatedAt: 300,
  592. },
  593. },
  594. );
  595. expect(iterations.get("msg_goal_assistant_1")).toBe(1);
  596. expect(iterations.get("msg_followup_assistant")).toBeUndefined();
  597. });
  598. it("classifies old persisted fork notices as timeline-only", () => {
  599. const notification = userMessage(
  600. "msg_fork",
  601. "<system-reminder>\nThis session was forked from a previous session checkpoint.\n</system-reminder>",
  602. {
  603. synthetic: true,
  604. metadata: {
  605. source: "fork",
  606. forkContext: {
  607. kind: "session_fork",
  608. parentSessionId: "sess_parent",
  609. targetMessageId: "msg_target",
  610. targetCheckpointId: "checkpoint_1",
  611. },
  612. },
  613. },
  614. );
  615. expect(getConversationMessageProjectionPolicy(notification)).toBe("timelineOnly");
  616. expect(isZCodeModelOnlySyntheticUserMessage(notification)).toBe(false);
  617. expect(getZCodeUserVisibleMessages([notification])).toEqual([notification]);
  618. });
  619. it("classifies model-only fork notices as provider context", () => {
  620. const forkMetadata = {
  621. forkContext: {
  622. kind: "session_fork",
  623. parentSessionId: "sess_parent",
  624. targetMessageId: "msg_target",
  625. },
  626. source: "fork",
  627. visibility: "model-only",
  628. };
  629. const notification = userMessage(
  630. "msg_fork_model_only",
  631. "This session was forked from a previous session message.",
  632. {
  633. metadata: forkMetadata,
  634. synthetic: true,
  635. },
  636. 1,
  637. {
  638. metadata: forkMetadata,
  639. semantics: {
  640. kind: "fork_notice",
  641. origin: "agent_runtime",
  642. providerVisibility: "visible",
  643. source: "fork",
  644. transcriptVisibility: "hidden",
  645. uiVisibility: "hidden",
  646. },
  647. source: "fork",
  648. synthetic: true,
  649. visibility: "model-only",
  650. },
  651. );
  652. expect(getConversationMessageProjectionPolicy(notification)).toBe("providerContextOnly");
  653. expect(getZCodeUserVisibleMessages([notification])).toEqual([]);
  654. });
  655. it("classifies model-only synthetic users as provider context", () => {
  656. const notification = userMessage("msg_model_only", "hidden context", { synthetic: true }, 1, {
  657. synthetic: true,
  658. visibility: "model-only",
  659. });
  660. expect(getConversationMessageProjectionPolicy(notification)).toBe("providerContextOnly");
  661. expect(getZCodeUserVisibleMessages([notification])).toEqual([]);
  662. });
  663. it("does not treat unknown synthetic users as real user input", () => {
  664. const notification = userMessage(
  665. "msg_unknown_synthetic",
  666. "runtime context",
  667. { synthetic: true },
  668. 1,
  669. { synthetic: true },
  670. );
  671. expect(getConversationMessageProjectionPolicy(notification)).toBe("hiddenSynthetic");
  672. expect(getZCodeUserVisibleMessages([notification])).toEqual([]);
  673. });
  674. it("classifies plain user text as real user input", () => {
  675. const message = userMessage("msg_real_input", "真实用户输入");
  676. expect(getConversationMessageProjectionPolicy(message)).toBe("realUserInput");
  677. expect(getZCodeUserVisibleMessages([message])).toEqual([message]);
  678. });
  679. it("classifies a persisted visible assistant response as visible assistant content", () => {
  680. const base = assistantMessage("msg_visible_assistant", "已完成首轮回复", 2);
  681. const message: ZCodeMessageWithParts = {
  682. ...base,
  683. info: {
  684. ...base.info,
  685. semantics: {
  686. origin: "agent_runtime",
  687. kind: "assistant_response",
  688. uiVisibility: "visible",
  689. providerVisibility: "visible",
  690. transcriptVisibility: "visible",
  691. },
  692. },
  693. };
  694. expect(getConversationMessageProjectionPolicy(message)).toBe("visibleAssistant");
  695. expect(getZCodeUserVisibleMessages([message])).toEqual([message]);
  696. });
  697. it("does not treat plain user text as a task notification", () => {
  698. const message = userMessage(
  699. "msg_user_xml",
  700. "<task-notification>为什么这里显示出来了?</task-notification>",
  701. );
  702. expect(isZCodeModelOnlySyntheticUserMessage(message)).toBe(false);
  703. expect(getZCodeUserVisibleMessages([message])).toEqual([message]);
  704. });
  705. it("does not use background task notifications as the visible session title", () => {
  706. const notification = userMessage(
  707. "msg_background_first",
  708. "<task-notification>\n <status>completed</status>\n</task-notification>",
  709. {
  710. synthetic: true,
  711. metadata: { source: "background_task" },
  712. },
  713. );
  714. const realUserMessage = userMessage("msg_real", "修复登录按钮");
  715. expect(
  716. resolveZCodeVisibleSessionTitle({
  717. messages: [notification, realUserMessage],
  718. }),
  719. ).toBe("修复登录按钮");
  720. });
  721. });