| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710 |
- import { describe, expect, it } from "vitest";
- import type { ModelInputMessage, RuntimeMessageEntry } from "../src/agent/message-history.js";
- import {
- realUserRuntimeMetadata,
- systemReminderRuntimeMetadata,
- } from "../src/agent/message-history.js";
- import { buildProviderRequestMessages } from "../src/runtime/helpers/provider-request-messages.js";
- import { buildRuntimeProviderRequestMessages } from "../src/runtime/helpers/runtime-provider-request-messages.js";
- import { createTestRuntimeModel } from "./test-runtime-model.js";
- function entry(
- message: ModelInputMessage,
- source?: RuntimeMessageEntry["metadata"]["source"],
- ): RuntimeMessageEntry {
- return {
- message,
- metadata: source ? { source } : undefined,
- };
- }
- function attachment(
- source: RuntimeMessageEntry["metadata"]["source"],
- content: string,
- ): RuntimeMessageEntry {
- return {
- kind: "attachment",
- content,
- metadata: { source },
- };
- }
- function user(content: ModelInputMessage["content"]): ModelInputMessage {
- return { role: "user", content };
- }
- function assistant(content = "assistant"): ModelInputMessage {
- return { role: "assistant", content };
- }
- function toolResultUser(content = "tool result"): ModelInputMessage {
- return {
- role: "user",
- content: [{ type: "text", text: content }],
- toolCallId: "tool-1",
- toolName: "Read",
- };
- }
- function toolResult(
- content = "tool result",
- toolCallId = "tool-1",
- toolName = "Read",
- ): ModelInputMessage {
- return {
- role: "tool",
- content,
- toolCallId,
- toolName,
- };
- }
- // 折叠只在 Anthropic Messages 协议打开,由模型请求能力决定。
- const FOLD = { foldRemindersIntoToolResults: true } as const;
- function providerText(message: ModelInputMessage): string {
- return typeof message.content === "string"
- ? message.content
- : message.content.map((block) => (block.type === "text" ? block.text : "")).join("");
- }
- function deepFreeze<T>(value: T): T {
- if (value && typeof value === "object") {
- Object.freeze(value);
- for (const nested of Object.values(value as Record<string, unknown>)) {
- deepFreeze(nested);
- }
- }
- return value;
- }
- describe("buildProviderRequestMessages", () => {
- it("keeps committed assistant entry ownership separate from provider messages", () => {
- const assistant = {
- message: { role: "assistant" as const, content: "answer" },
- tokens: { input: 100, output: 20, reasoning: 0, cache: { read: 0, write: 0 }, total: 120 },
- } satisfies RuntimeMessageEntry;
- const entries = [entry(user("prompt"), "real_user"), assistant] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- { role: "user", content: "prompt" },
- { role: "assistant", content: "answer" },
- ]);
- expect(result.sourceEntries).toEqual([entries[0], assistant]);
- });
- it("keeps a persisted Continue provider-visible without moving the latest real user", () => {
- const entries = [
- entry(user("real prompt"), "real_user"),
- entry(assistant("partial")),
- entry(user("continue locally"), "output_token_continuation"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- user("real prompt"),
- assistant("partial"),
- user("continue locally"),
- ]);
- expect(result.diagnostics.latestRealUserMessageIndex).toBe(0);
- });
- it("projects structured system reminder attachments using raw body content", () => {
- const entries = [
- entry(user("real prompt"), "real_user"),
- attachment("output_style", "style body"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "user", content: "real prompt", cacheControl: { type: "ephemeral" } },
- { role: "system", content: "style body" },
- ]);
- });
- it("projects structured prompt attachments as mid-conversation system messages", () => {
- const entries = [
- entry(user("Summarize notes."), "real_user"),
- attachment(
- "prompt_attachment",
- 'Called the Read tool with the following input: {"file_path":"notes.md"}',
- ),
- attachment("prompt_attachment", "Result of calling the Read tool:\n1\t# Notes"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- { role: "user", content: "Summarize notes." },
- {
- role: "system",
- content:
- 'Called the Read tool with the following input: {"file_path":"notes.md"}\n\nResult of calling the Read tool:\n1\t# Notes',
- },
- ]);
- });
- it("projects MCS-capable attachments as system messages when MCS is explicitly enabled", () => {
- const entries = [
- entry(user("Summarize notes."), "real_user"),
- attachment("prompt_attachment", "Called the Read tool with the following input: {}"),
- attachment("output_style", "Terse output style is active."),
- ] satisfies RuntimeMessageEntry[];
- expect(
- buildProviderRequestMessages({
- entries,
- useMidConversationSystem: true,
- }).messages,
- ).toEqual([
- { role: "user", content: "Summarize notes." },
- {
- role: "system",
- content:
- "Called the Read tool with the following input: {}\n\nTerse output style is active.",
- },
- ]);
- });
- it("projects goal state changes as mid-conversation system messages when MCS is enabled", () => {
- const entries = [
- entry(user("Continue"), "real_user"),
- attachment(
- "goal_state_change",
- "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal.",
- ),
- ] satisfies RuntimeMessageEntry[];
- expect(
- buildProviderRequestMessages({
- entries,
- useMidConversationSystem: true,
- }).messages,
- ).toEqual([
- { role: "user", content: "Continue" },
- {
- role: "system",
- content:
- "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal.",
- },
- ]);
- });
- it("falls back goal state changes to user system reminders when MCS is disabled", () => {
- const entries = [
- entry(user("Continue"), "real_user"),
- attachment(
- "goal_state_change",
- "The session goal has been cleared. Do not continue pursuing any previous goal unless the user sets a new goal.",
- ),
- ] satisfies RuntimeMessageEntry[];
- expect(
- buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- }).messages,
- ).toEqual([
- { role: "user", content: "Continue" },
- {
- role: "user",
- content:
- "<system-reminder>\nThe session goal has been cleared. Do not continue pursuing any previous goal unless the user sets a new goal.\n</system-reminder>",
- },
- ]);
- });
- it("does not bubble a goal state change before an older target continuation", () => {
- const pauseReminder =
- "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal.";
- const entries = [
- entry(user("Original goal"), "real_user"),
- entry(user("Continue working toward the active session goal."), "target_continuation"),
- attachment("goal_state_change", pauseReminder),
- entry(user("Follow-up after Stop"), "real_user"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- });
- expect(result.messages).toEqual([
- { role: "user", content: "Original goal" },
- { role: "user", content: "Continue working toward the active session goal." },
- {
- role: "user",
- content: `<system-reminder>\n${pauseReminder}\n</system-reminder>`,
- },
- { role: "user", content: "Follow-up after Stop" },
- ]);
- expect(result.diagnostics.bubbledAttachmentEntryCount).toBe(0);
- });
- it("renders MCS-capable attachments as legacy user system reminders when MCS is disabled", () => {
- const entries = [
- entry(user("Summarize notes."), "real_user"),
- attachment("prompt_attachment", "Called the Read tool with the following input: {}"),
- ] satisfies RuntimeMessageEntry[];
- expect(
- buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- }).messages,
- ).toEqual([
- {
- role: "user",
- content:
- "<system-reminder>\nCalled the Read tool with the following input: {}\n</system-reminder>",
- },
- { role: "user", content: "Summarize notes." },
- ]);
- });
- it("applies attachment bubble ordering before MCS projection", () => {
- const entries = [
- entry(user("Real query"), "real_user"),
- attachment("output_style", "Style body"),
- entry(assistant("Answer")),
- ] satisfies RuntimeMessageEntry[];
- const legacy = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- }).messages;
- expect(legacy.map((message) => message.role)).toEqual(["user", "user", "assistant"]);
- expect(String(legacy[0]?.content)).toContain("Style body");
- expect(legacy[1]?.content).toBe("Real query");
- const mcs = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: true,
- });
- expect(mcs.messages.map((message) => message.role)).toEqual(["user", "system", "assistant"]);
- expect(mcs.messages[0]?.content).toBe("Real query");
- expect(mcs.messages[1]?.content).toBe("Style body");
- expect(mcs.diagnostics.bubbledAttachmentEntryCount).toBe(1);
- });
- it("keeps target verifier prompts as plain user messages while projecting history attachments", () => {
- const entries = [
- entry(user("original goal turn"), "real_user"),
- attachment("output_style", "Use verifier style."),
- entry(user("Verify whether the active goal is complete.")),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "user", content: "original goal turn" },
- {
- role: "user",
- content: "Verify whether the active goal is complete.",
- cacheControl: { type: "ephemeral" },
- },
- {
- role: "system",
- content: "Use verifier style.",
- },
- ]);
- });
- it("does not project legacy message entries just because their source is eligible", () => {
- const entries = [
- entry(user("real prompt"), "real_user"),
- entry(user("style body"), "output_style"),
- entry(user("<system-reminder>\ntodo\n</system-reminder>"), "todo_reminder"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- { role: "user", content: "real prompt" },
- { role: "user", content: "style body" },
- { role: "user", content: "<system-reminder>\ntodo\n</system-reminder>" },
- ]);
- });
- it("moves skills listing after the first real user as a system message", () => {
- const entries = [
- entry({ role: "system", content: "prefix" }),
- attachment("skills_listing", "skills"),
- attachment("context_prefix", "# currentDate\nToday"),
- entry(user("real prompt"), "real_user"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "system", content: "prefix" },
- {
- role: "user",
- content: "<system-reminder>\n# currentDate\nToday\n</system-reminder>",
- },
- {
- role: "user",
- content: "real prompt",
- cacheControl: { type: "ephemeral" },
- },
- { role: "system", content: "skills" },
- ]);
- expect(result.diagnostics.cacheControlIndex).toBe(2);
- expect(result.diagnostics.latestRealUserMessageIndex).toBe(2);
- });
- it("ends the user text right before a real user input with a newline", () => {
- const entries = [
- attachment("context_prefix", "instructions"),
- attachment("session_context", "git"),
- entry(user("real prompt"), "real_user"),
- entry(assistant("answer")),
- entry(toolResultUser("tool output")),
- entry(user("next prompt"), "real_user"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- ...FOLD,
- });
- expect(result.messages.map((message) => message.content)).toEqual([
- "<system-reminder>\ninstructions\n</system-reminder>",
- "<system-reminder>\ngit\n</system-reminder>\n",
- "real prompt",
- "answer",
- [{ type: "text", text: "tool output" }],
- "next prompt",
- ]);
- });
- it("ends the user text right before a compact summary with a newline", () => {
- const summary: RuntimeMessageEntry = {
- message: user("This session is being continued from a previous conversation."),
- metadata: { source: "legacy_synthetic", compactBoundary: { boundaryId: "b1" } },
- };
- const entries = [
- attachment("context_prefix", "instructions"),
- attachment("session_context", "git"),
- summary,
- entry(assistant("kept answer")),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- ...FOLD,
- });
- // summary 与真实输入一样,前一条 user 文本以换行结尾。
- expect(result.messages[1]?.content).toBe("<system-reminder>\ngit\n</system-reminder>\n");
- expect(result.messages[2]?.content).toBe(
- "This session is being continued from a previous conversation.",
- );
- });
- it("adds no newline when the protocol does not merge adjacent user content", () => {
- const summary: RuntimeMessageEntry = {
- message: user("This session is being continued from a previous conversation."),
- metadata: { source: "legacy_synthetic", compactBoundary: { boundaryId: "b1" } },
- };
- const entries = [
- attachment("session_context", "git"),
- summary,
- entry(user("turn two"), "real_user"),
- entry(user("[Request interrupted by user]"), "interrupt_marker"),
- entry(user("next prompt"), "real_user"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, useMidConversationSystem: false });
- // OpenAI 兼容协议不合并 user:上一次请求的最后一块(例如 "turn two")保持原字节。
- expect(result.messages.map((message) => message.content)).toEqual([
- "<system-reminder>\ngit\n</system-reminder>",
- "This session is being continued from a previous conversation.",
- "turn two",
- "[Request interrupted by user]",
- "next prompt",
- ]);
- });
- it.each([true, false])(
- "keeps the interrupt marker as plain user text that attachments pass (MCS=%s)",
- (useMidConversationSystem) => {
- const entries = [
- entry(user("first prompt"), "real_user"),
- entry(assistant("partial")),
- entry(user("[Request interrupted by user]"), "interrupt_marker"),
- attachment("runtime_mode", "mode"),
- entry(user("next prompt"), "real_user"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, useMidConversationSystem, ...FOLD });
- const texts = result.messages.map((message) => [message.role, providerText(message)]);
- // 标记不冒泡、不进 system;后面的输入前以换行结尾。
- expect(texts).toContainEqual(["user", "[Request interrupted by user]\n"]);
- const markerIndex = texts.findIndex(([, text]) => text.startsWith("[Request interrupted"));
- expect(texts[markerIndex - 1]?.[0]).toBe(useMidConversationSystem ? "assistant" : "user");
- expect(texts.at(-1)).toEqual(
- useMidConversationSystem
- ? ["system", expect.stringContaining("mode")]
- : ["user", "next prompt"],
- );
- },
- );
- it("projects non-context meta reminders to system without moving context prefix", () => {
- const entries = [
- entry(user("follow up"), "real_user"),
- attachment("runtime_mode", "mode"),
- attachment("todo_reminder", "todo"),
- attachment("output_style", "style"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "user", content: "follow up", cacheControl: { type: "ephemeral" } },
- { role: "system", content: "mode\n\ntodo\n\nstyle" },
- ]);
- expect(result.diagnostics.bubbledAttachmentEntryCount).toBe(3);
- expect(result.diagnostics.cacheControlIndex).toBe(0);
- expect(result.diagnostics.latestRealUserMessageIndex).toBe(0);
- });
- it("keeps the same real user anchor open for prefix and current-turn system projections", () => {
- const entries = [
- attachment("skills_listing", "skills"),
- entry(user("real prompt"), "real_user"),
- attachment("output_style", "style"),
- entry(assistant("assistant response")),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- { role: "user", content: "real prompt" },
- { role: "system", content: "skills\n\nstyle" },
- { role: "assistant", content: "assistant response" },
- ]);
- });
- it("projects current-turn attachment reminders to system without crossing assistant boundaries", () => {
- const entries = [
- entry(assistant("previous response")),
- entry(user("follow-up prompt"), "real_user"),
- attachment("relevant_memory", "memory reminder"),
- attachment("runtime_mode", "runtime mode reminder"),
- entry(assistant("next response")),
- entry(user("later prompt"), "real_user"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- { role: "assistant", content: "previous response" },
- { role: "user", content: "follow-up prompt" },
- { role: "system", content: "memory reminder\n\nruntime mode reminder" },
- { role: "assistant", content: "next response" },
- { role: "user", content: "later prompt" },
- ]);
- expect(result.diagnostics.bubbledAttachmentEntryCount).toBe(2);
- });
- it("places cache-control on the latest non-system message after projection", () => {
- const entries = [
- entry(user("inspect the repo"), "real_user"),
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- entry(user("output style reminder"), "output_style"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "user", content: "inspect the repo" },
- { role: "assistant", content: "used Read" },
- {
- role: "tool",
- content: "read result",
- toolCallId: "tool-1",
- toolName: "Read",
- },
- {
- role: "user",
- content: "output style reminder",
- cacheControl: { type: "ephemeral" },
- },
- ]);
- expect(result.diagnostics.cacheControlIndex).toBe(3);
- expect(result.diagnostics.latestRealUserMessageIndex).toBe(0);
- });
- it("keeps plugin reference after the user while placing cache-control on that user", () => {
- const entries = [
- entry(user("use the plugin"), "real_user"),
- attachment("plugin_reference", "<plugin_reference>demo</plugin_reference>"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- {
- role: "user",
- content: "use the plugin",
- cacheControl: { type: "ephemeral" },
- },
- {
- role: "system",
- content: "<plugin_reference>demo</plugin_reference>",
- },
- ]);
- expect(result.diagnostics.cacheControlIndex).toBe(0);
- expect(result.diagnostics.latestRealUserMessageIndex).toBe(0);
- });
- it("moves cache-control before the latest message when cache write is skipped", () => {
- const entries = [
- entry(user("inspect the repo"), "real_user"),
- entry(assistant("summarizable response")),
- entry(user("compact prompt")),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- applyCacheControl: true,
- skipCacheWrite: true,
- });
- expect(result.messages).toEqual([
- { role: "user", content: "inspect the repo" },
- {
- role: "assistant",
- content: "summarizable response",
- cacheControl: { type: "ephemeral" },
- },
- { role: "user", content: "compact prompt" },
- ]);
- expect(result.diagnostics.cacheControlIndex).toBe(1);
- });
- it("places cache-control on a tool message when it is the latest non-system message", () => {
- const entries = [
- entry(user("inspect the repo"), "real_user"),
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "user", content: "inspect the repo" },
- { role: "assistant", content: "used Read" },
- {
- role: "tool",
- content: "read result",
- toolCallId: "tool-1",
- toolName: "Read",
- cacheControl: { type: "ephemeral" },
- },
- ]);
- expect(result.diagnostics.cacheControlIndex).toBe(2);
- });
- it("projects a todo reminder after a tool result as mid-conversation system", () => {
- const entries = [
- entry(user("inspect the repo"), "real_user"),
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- attachment("todo_reminder", "todo body"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "user", content: "inspect the repo" },
- { role: "assistant", content: "used Read" },
- {
- role: "tool",
- content: "read result",
- toolCallId: "tool-1",
- toolName: "Read",
- cacheControl: { type: "ephemeral" },
- },
- { role: "system", content: "todo body" },
- ]);
- expect(result.diagnostics.cacheControlIndex).toBe(2);
- });
- it("delays a pending mid-conversation system reminder until sibling tool results complete", () => {
- const entries = [
- entry(user("inspect the repo"), "real_user"),
- entry(assistant("issued parallel tools")),
- entry(toolResult("read result", "tool-1", "Read")),
- attachment("todo_reminder", "todo body"),
- entry(toolResult("grep result", "tool-2", "Grep")),
- entry(toolResult("glob result", "tool-3", "Glob")),
- entry(assistant("next answer")),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- { role: "user", content: "inspect the repo" },
- { role: "assistant", content: "issued parallel tools" },
- {
- role: "tool",
- content: "read result",
- toolCallId: "tool-1",
- toolName: "Read",
- },
- {
- role: "tool",
- content: "grep result",
- toolCallId: "tool-2",
- toolName: "Grep",
- },
- {
- role: "tool",
- content: "glob result",
- toolCallId: "tool-3",
- toolName: "Glob",
- },
- { role: "system", content: "todo body" },
- { role: "assistant", content: "next answer" },
- ]);
- });
- it("keeps legacy reminder text after sibling tool results when MCS is disabled", () => {
- const entries = [
- entry(user("inspect the repo"), "real_user"),
- entry(assistant("issued parallel tools")),
- entry(toolResult("read result", "tool-1", "Read")),
- attachment("todo_reminder", "todo body"),
- entry(toolResult("grep result", "tool-2", "Grep")),
- entry(toolResult("glob result", "tool-3", "Glob")),
- entry(assistant("next answer")),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- });
- expect(result.messages).toEqual([
- { role: "user", content: "inspect the repo" },
- { role: "assistant", content: "issued parallel tools" },
- {
- role: "tool",
- content: "read result",
- toolCallId: "tool-1",
- toolName: "Read",
- },
- {
- role: "tool",
- content: "grep result",
- toolCallId: "tool-2",
- toolName: "Grep",
- },
- {
- role: "tool",
- content: "glob result",
- toolCallId: "tool-3",
- toolName: "Glob",
- },
- {
- role: "user",
- content: "<system-reminder>\ntodo body\n</system-reminder>",
- },
- { role: "assistant", content: "next answer" },
- ]);
- });
- it("trims a string tool result and its reminders when folding them", () => {
- const entries = [
- entry(assistant("used Bash")),
- entry(toolResult("bash output\n", "tool-1", "Bash")),
- attachment("todo_reminder", "todo body"),
- entry(user(" <system-reminder>\nmemory\n</system-reminder>\n"), "relevant_memory"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- ...FOLD,
- });
- expect(result.messages).toEqual([
- { role: "assistant", content: "used Bash" },
- {
- role: "tool",
- content:
- "bash output\n\n<system-reminder>\ntodo body\n</system-reminder>\n\n<system-reminder>\nmemory\n</system-reminder>",
- toolCallId: "tool-1",
- toolName: "Bash",
- },
- ]);
- });
- it("appends reminder text blocks to an array tool result without trimming", () => {
- const image = {
- type: "image" as const,
- mediaType: "image/png",
- dataUrl: "data:image/png;base64,abc",
- };
- const entries = [
- entry(assistant("read two files")),
- entry({
- role: "tool",
- content: [{ type: "text", text: "page" }],
- toolCallId: "tool-1",
- toolName: "Read",
- }),
- entry({ role: "tool", content: [image], toolCallId: "tool-2", toolName: "Read" }),
- attachment("todo_reminder", "todo body"),
- attachment("output_style", "style body"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- ...FOLD,
- });
- expect(result.messages.slice(1)).toEqual([
- {
- role: "tool",
- content: [{ type: "text", text: "page" }],
- toolCallId: "tool-1",
- toolName: "Read",
- },
- {
- role: "tool",
- content: [
- image,
- { type: "text", text: "<system-reminder>\ntodo body\n</system-reminder>\n" },
- { type: "text", text: "<system-reminder>\nstyle body\n</system-reminder>" },
- ],
- toolCallId: "tool-2",
- toolName: "Read",
- },
- ]);
- });
- it("does not fold reminders into a tool result that holds tool references", () => {
- const entries = [
- entry(assistant("searched tools")),
- entry({
- role: "tool",
- content: [{ type: "tool_reference", toolName: "Write" }],
- toolCallId: "tool-1",
- toolName: "ToolSearch",
- }),
- attachment("todo_reminder", "todo body"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- ...FOLD,
- });
- expect(result.messages.slice(1)).toEqual([
- {
- role: "tool",
- content: [{ type: "tool_reference", toolName: "Write" }],
- toolCallId: "tool-1",
- toolName: "ToolSearch",
- },
- { role: "user", content: "<system-reminder>\ntodo body\n</system-reminder>" },
- ]);
- });
- it("folds mid-turn inputs with the other reminders but never folds user inputs", () => {
- const steer: RuntimeMessageEntry = {
- message: user("check the tests too"),
- metadata: { source: "real_user", inputPresentation: "user_steer" },
- };
- const entries = [
- entry(user("inspect"), "real_user"),
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- attachment("todo_reminder", "todo body"),
- steer,
- attachment("output_style", "style body"),
- entry(assistant("used Grep")),
- entry(toolResult("grep result", "tool-2", "Grep")),
- entry(user("next prompt"), "real_user"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- ...FOLD,
- });
- const folded = providerText(result.messages[2]!);
- expect(
- folded.startsWith("read result\n\n<system-reminder>\ntodo body\n</system-reminder>\n\n"),
- ).toBe(true);
- expect(folded).toContain("check the tests too");
- expect(folded.endsWith("\n\n<system-reminder>\nstyle body\n</system-reminder>")).toBe(true);
- expect(result.messages.slice(3)).toEqual([
- { role: "assistant", content: "used Grep" },
- { role: "tool", content: "grep result", toolCallId: "tool-2", toolName: "Grep" },
- { role: "user", content: "next prompt" },
- ]);
- expect(result.diagnostics.latestRealUserMessageIndex).toBe(5);
- expect(result.sourceEntries[2]).toBe(entries[2]);
- expect(result.sourceEntries[5]).toBe(entries[8]);
- });
- describe("native ToolSearch Tool loaded notice", () => {
- const searchBatch = [
- entry(assistant("searched tools")),
- entry({
- role: "tool",
- content: [{ type: "tool_reference", toolName: "Write" }],
- toolCallId: "search-1",
- toolName: "ToolSearch",
- }),
- entry(toolResult("out", "bash-1", "Bash")),
- ] satisfies RuntimeMessageEntry[];
- const loaded = { loadedToolCallIds: new Set(["search-1"]) };
- it("keeps reminders after Tool loaded instead of folding them into the batch", () => {
- const result = buildProviderRequestMessages({
- entries: [...searchBatch, attachment("session_context", "ctx")],
- ...loaded,
- ...FOLD,
- });
- // Tool loaded. 在并入之前插入:提醒跟在它后面,Bash 结果保持原样。
- expect(result.messages.slice(1)).toEqual([
- {
- role: "tool",
- content: [{ type: "tool_reference", toolName: "Write" }],
- toolCallId: "search-1",
- toolName: "ToolSearch",
- },
- toolResult("out", "bash-1", "Bash"),
- { role: "user", content: "Tool loaded." },
- { role: "user", content: "<system-reminder>\nctx\n</system-reminder>" },
- ]);
- });
- it("ends Tool loaded with a newline before a following user input", () => {
- const prompt = entry(user("check the tests too"), "real_user");
- const result = buildProviderRequestMessages({
- entries: [...searchBatch, prompt],
- ...loaded,
- ...FOLD,
- });
- expect(result.messages[3]).toEqual({ role: "user", content: "Tool loaded.\n" });
- expect(providerText(result.messages[4]!)).toContain("check the tests too");
- });
- });
- describe("MCS human steer in the run's system message", () => {
- const steer = (content: ModelInputMessage["content"]): RuntimeMessageEntry => ({
- message: user(content),
- metadata: { source: "real_user", inputPresentation: "user_steer" },
- });
- const steerText = (body: string) =>
- `The user sent a new message while you were working:\n${body}\n\nThis is how ZCode surfaces messages the user sends mid-turn — within the running turn, often alongside the next tool result, rather than as a separate conversation turn. Address the message above as you continue this turn.`;
- // guide 是轮内中途消息:MCS 下与其他中途消息一样并入 run 的 system,不带外层标签。
- it("puts a text steer into the run system message, user-side reminders still fold", () => {
- const entries = [
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- steer("check the tests too"),
- attachment("session_context", "git"),
- attachment("todo_reminder", "todo body"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, ...FOLD });
- expect(result.messages).toEqual([
- assistant("used Read"),
- toolResult("read result\n\n<system-reminder>\ngit\n</system-reminder>"),
- { role: "system", content: `${steerText("check the tests too")}\n\ntodo body` },
- ]);
- });
- it("keeps several steers in order in one system message before the next assistant", () => {
- const entries = [
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- steer("first"),
- attachment("session_context", "git"),
- steer("second"),
- entry(assistant("next")),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, ...FOLD });
- expect(result.messages).toEqual([
- assistant("used Read"),
- toolResult("read result\n\n<system-reminder>\ngit\n</system-reminder>"),
- { role: "system", content: `${steerText("first")}\n\n${steerText("second")}` },
- assistant("next"),
- ]);
- });
- // text-only 回复之后没有 user/tool 锚点,system 放不下,整组降级为一条 user reminder。
- it("degrades a steer after a text-only reply to one wrapped user reminder", () => {
- const entries = [
- entry(user("first"), "real_user"),
- entry(assistant("answer")),
- steer("follow up"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, ...FOLD });
- expect(result.messages).toEqual([
- user("first"),
- assistant("answer"),
- user(`<system-reminder>\n${steerText("follow up")}\n</system-reminder>`),
- ]);
- });
- // 带图 guide 放不进 system:MCS 开关下都留在 user 侧,文本包装一次放首块,不并入 tool result。
- it.each([true, false])("keeps a steer with media on the user side (MCS=%s)", (mcs) => {
- const image = {
- type: "image" as const,
- mediaType: "image/png",
- dataUrl: "data:image/png;base64,abc",
- };
- const entries = [
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- steer([{ type: "text", text: "look at this" }, image]),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: mcs,
- ...FOLD,
- });
- expect(result.messages).toEqual([
- assistant("used Read"),
- toolResult("read result"),
- user([
- {
- type: "text",
- text: `<system-reminder>\n${steerText("look at this")}\n</system-reminder>`,
- },
- image,
- ]),
- ]);
- });
- });
- describe("send-now before the preempted prompt has any output", () => {
- // 历史:B 还没有任何输出就被 send-now 抢占(不写中断标记),C 开新一轮。
- // 现在 C 是普通用户消息;旧版本把 C 存成 user_steer,已存会话照旧投影。
- const entriesWith = (c: RuntimeMessageEntry) =>
- [
- entry(user("first"), "real_user"),
- entry(assistant("answer A")),
- entry(user("prompt B"), "real_user"),
- attachment("output_style", "style for B"),
- c,
- attachment("todo_reminder", "todo after C"),
- ] satisfies RuntimeMessageEntry[];
- const plainC = entry(user("message C"), "real_user");
- const storedSteerC: RuntimeMessageEntry = {
- message: user("message C"),
- metadata: { source: "real_user", inputPresentation: "user_steer" },
- };
- const order = (messages: ModelInputMessage[], ...needles: string[]): number[] =>
- needles.map((needle) =>
- messages.findIndex((message) => providerText(message).includes(needle)),
- );
- // 人类输入按时间顺序:被抢占的 B 在前,新消息 C 在后。
- // MCS 关:公告照常越过普通 user;普通 C 之后的提醒越过 C 和 B,旧 user_steer C 之后的提醒留在 C 之后。
- // MCS 开:run 内的 MCS 文本合成一条 system,放在 run 末尾(C 之后)。
- it.each([
- ["plain C, MCS off", plainC, false, ["style for B", "todo after C", "prompt B", "message C"]],
- ["plain C, MCS on", plainC, true, ["prompt B", "message C", "style for B"]],
- [
- "stored user_steer C, MCS off",
- storedSteerC,
- false,
- ["style for B", "prompt B", "message C", "todo after C"],
- ],
- ])("keeps the new message after the preempted prompt (%s)", (_label, c, mcs, expected) => {
- const result = buildProviderRequestMessages({
- entries: entriesWith(c),
- useMidConversationSystem: mcs,
- });
- const indices = order(result.messages, ...expected);
- expect(indices[0]).toBeGreaterThan(1);
- expect(indices).toEqual([...new Set(indices)].sort((a, b) => a - b));
- });
- // MCS 开时旧 user_steer C 是中途消息,和 run 内的提醒按生成顺序合成 B 之后的一条 system。
- it("keeps a stored user_steer C after the preempted prompt in the run system (MCS on)", () => {
- const result = buildProviderRequestMessages({
- entries: entriesWith(storedSteerC),
- useMidConversationSystem: true,
- });
- expect(result.messages.slice(2).map((message) => message.role)).toEqual(["user", "system"]);
- expect(providerText(result.messages[2]!)).toBe("prompt B");
- const system = providerText(result.messages[3]!);
- expect(order([user(system)], "style for B", "message C", "todo after C")).toEqual([0, 0, 0]);
- expect(system.indexOf("style for B")).toBeLessThan(system.indexOf("message C"));
- expect(system.indexOf("message C")).toBeLessThan(system.indexOf("todo after C"));
- });
- });
- // 被打断时只产出了思考(send now、停止、思考后流失败)或只有空白文字的回复。Anthropic 协议的请求不带它,
- // 删除是投影的第一步:system 按「后面是 assistant」落在它前面,它再被删掉,system 就直接接 user,
- // provider 返回 400,之后整个会话的请求都失败。
- describe("assistant replies the protocol drops", () => {
- const DROP = { dropReasoningOnlyOrBlankAssistants: true } as const;
- const reasoningOnly: ModelInputMessage = {
- role: "assistant",
- content: [{ type: "reasoning", text: "thinking B" }],
- };
- const preempted = (reply: ModelInputMessage, ...rest: RuntimeMessageEntry[]) =>
- [
- entry(user("first"), "real_user"),
- entry(assistant("answer A")),
- entry(user("prompt B"), "real_user"),
- attachment("environment", "env for B"),
- entry(reply),
- entry(user("message C"), "real_user"),
- ...rest,
- ] satisfies RuntimeMessageEntry[];
- const roles = (messages: ModelInputMessage[]) => messages.map((message) => message.role);
- it.each([
- ["reasoning-only", reasoningOnly],
- ["blank", { role: "assistant", content: [{ type: "text", text: "\n\n" }] }],
- ["placeholder", { role: "assistant", content: [{ type: "text", text: "(no content)" }] }],
- [
- "reasoning and blank",
- {
- role: "assistant",
- content: [
- { type: "text", text: "\n\n" },
- { type: "reasoning", text: "thinking B" },
- ],
- },
- ],
- ] satisfies [string, ModelInputMessage][])(
- "puts the run's system after the new input when a %s reply is dropped",
- (_label, reply) => {
- const result = buildProviderRequestMessages({
- entries: preempted(reply),
- useMidConversationSystem: true,
- ...FOLD,
- ...DROP,
- });
- expect(roles(result.messages)).toEqual(["user", "assistant", "user", "user", "system"]);
- expect(providerText(result.messages[2]!)).toBe("prompt B\n");
- expect(providerText(result.messages[3]!)).toBe("message C");
- expect(providerText(result.messages[4]!)).toContain("env for B");
- },
- );
- // MCS 关:被删的回复不再是冒泡停止点,C 之后的提醒越过 C 和 B,落在 A 之后。
- it("bubbles the new turn's reminders past the earlier prompt when MCS is off", () => {
- const result = buildProviderRequestMessages({
- entries: preempted(reasoningOnly, attachment("output_style", "style after C")),
- useMidConversationSystem: false,
- ...FOLD,
- ...DROP,
- });
- const texts = result.messages.map(providerText);
- const indexOf = (needle: string) => texts.findIndex((text) => text.includes(needle));
- expect(roles(result.messages).filter((role) => role === "assistant")).toHaveLength(1);
- expect(JSON.stringify(result.messages)).not.toContain("thinking B");
- expect(indexOf("answer A")).toBeLessThan(indexOf("style after C"));
- expect(indexOf("style after C")).toBeLessThan(indexOf("prompt B"));
- expect(texts[indexOf("prompt B")]).toBe("prompt B\n");
- expect(texts.at(-1)).toBe("message C");
- });
- // 思考用完输出上限:回复只有思考,runtime 追加 Continue 提示;回复删除后提示直接接在 B 后面。
- it("separates the earlier prompt from a Continue prompt after the reply is dropped", () => {
- const result = buildProviderRequestMessages({
- entries: [
- ...preempted(reasoningOnly).slice(0, 4),
- entry(reasoningOnly),
- entry(user("Output token limit hit."), "output_token_continuation"),
- ],
- useMidConversationSystem: false,
- ...FOLD,
- ...DROP,
- });
- expect(result.messages.slice(-2).map(providerText)).toEqual([
- "prompt B\n",
- "Output token limit hit.",
- ]);
- });
- // 续写提示写入历史之前存的会话里,两段回复相邻。按条目判断:只删只有思考的第一段。
- // 产出第二段的请求里第一段已被删除,整段保留会把第一段的思考放回第二段前面。
- it("drops only the reasoning-only entry of two adjacent assistant entries", () => {
- const result = buildProviderRequestMessages({
- entries: [
- ...preempted(reasoningOnly).slice(0, 4),
- entry(reasoningOnly),
- entry({
- role: "assistant",
- content: [
- { type: "reasoning", text: "thinking C" },
- { type: "text", text: "answer C" },
- ],
- }),
- entry(user("message D"), "real_user"),
- ],
- useMidConversationSystem: false,
- ...FOLD,
- ...DROP,
- });
- const assistants = result.messages.filter((message) => message.role === "assistant");
- expect(assistants).toHaveLength(2);
- expect(JSON.stringify(assistants[1])).toContain("thinking C");
- expect(JSON.stringify(result.messages)).not.toContain("thinking B");
- });
- // send now 最常见的形态:工具结果之后、模型还在思考时打断。被删回复前面是 tool result,
- // 提醒按 MCS 落点或并入 tool result,都不能让 system 直接接 user。
- it.each([true, false])(
- "keeps a valid request when a reply after a tool result is dropped (MCS %s)",
- (useMidConversationSystem) => {
- const result = buildProviderRequestMessages({
- entries: [
- entry(user("inspect"), "real_user"),
- entry({
- role: "assistant",
- content: "used Read",
- toolCalls: [{ id: "tool-1", name: "Read", input: {} }],
- }),
- entry(toolResult("read result")),
- attachment("environment", "env after tool"),
- entry(reasoningOnly),
- entry(user("message C"), "real_user"),
- ],
- useMidConversationSystem,
- ...FOLD,
- ...DROP,
- });
- const messages = result.messages;
- messages.forEach((message, index) => {
- if (message.role === "system")
- expect(messages[index + 1]?.role ?? "assistant").toBe("assistant");
- });
- expect(JSON.stringify(messages)).not.toContain("thinking B");
- expect(messages.some((message) => message.role === "tool")).toBe(true);
- expect(JSON.stringify(messages)).toContain("env after tool");
- expect(messages.map(providerText).join("")).toContain("message C");
- },
- );
- it.each([
- [
- "a tool call",
- {
- role: "assistant",
- content: [{ type: "reasoning", text: "thinking B" }],
- toolCalls: [{ id: "call-1", name: "Read", input: {} }],
- },
- ],
- ["visible text", { role: "assistant", content: [{ type: "text", text: "partial" }] }],
- ["legacy string content", { role: "assistant", content: " " }],
- // 留作估算锚点的空回复存成空字符串(上一条);空数组的 every 恒为真,要单独排除。
- ["empty content", { role: "assistant", content: [] }],
- ] satisfies [string, ModelInputMessage][])("keeps a reply with %s", (_label, reply) => {
- const result = buildProviderRequestMessages({
- entries: preempted(reply),
- useMidConversationSystem: false,
- ...DROP,
- });
- expect(roles(result.messages).filter((role) => role === "assistant")).toHaveLength(2);
- });
- it.each([true, false])("reads the drop switch from the model capability (%s)", (drop) => {
- const model = {
- ...createTestRuntimeModel({
- generateText: async () => ({ text: "" }) as never,
- propertyOverrides: { supportsMidConversationSystem: true },
- }),
- requestCapabilities: { dropReasoningOnlyOrBlankAssistants: drop },
- };
- const result = buildRuntimeProviderRequestMessages(
- { config: {} },
- { model, entries: preempted(reasoningOnly) },
- );
- expect(roles(result.messages)).toEqual(
- drop
- ? ["user", "assistant", "user", "user", "system"]
- : ["user", "assistant", "user", "system", "assistant", "user"],
- );
- });
- });
- it("does not keep a folded steer as the latest real user for the media budget", () => {
- const entries = [
- entry(user("inspect"), "real_user"),
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- {
- message: user("check the tests too"),
- metadata: { source: "real_user", inputPresentation: "user_steer" },
- },
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({
- entries,
- useMidConversationSystem: false,
- ...FOLD,
- });
- expect(result.messages).toHaveLength(3);
- expect(result.diagnostics.latestRealUserMessageIndex).toBe(-1);
- });
- it.each([true, false])("reads the fold switch from the model capability (%s)", (fold) => {
- const model = {
- ...createTestRuntimeModel({
- generateText: async () => ({ text: "" }) as never,
- propertyOverrides: { supportsMidConversationSystem: false },
- }),
- requestCapabilities: { foldRemindersIntoToolResults: fold },
- };
- const result = buildRuntimeProviderRequestMessages(
- { config: {} },
- {
- model,
- entries: [
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- attachment("todo_reminder", "todo body"),
- ],
- },
- );
- expect(result.messages.map((message) => message.role)).toEqual(
- fold ? ["assistant", "tool"] : ["assistant", "tool", "user"],
- );
- });
- it("keeps a todo reminder anchored after its tool result when later tool exchanges are appended", () => {
- const entries = [
- entry(user("inspect the repo"), "real_user"),
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- attachment("todo_reminder", "todo body"),
- entry(assistant("used Grep")),
- {
- message: {
- role: "tool",
- content: "grep result",
- toolCallId: "tool-2",
- toolName: "Grep",
- },
- },
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "user", content: "inspect the repo" },
- { role: "assistant", content: "used Read" },
- {
- role: "tool",
- content: "read result",
- toolCallId: "tool-1",
- toolName: "Read",
- },
- { role: "system", content: "todo body" },
- { role: "assistant", content: "used Grep" },
- {
- role: "tool",
- content: "grep result",
- toolCallId: "tool-2",
- toolName: "Grep",
- cacheControl: { type: "ephemeral" },
- },
- ]);
- });
- it("projects a todo reminder after an Anthropic-style tool-result user message", () => {
- const entries = [
- entry(user("inspect the repo"), "real_user"),
- entry(assistant("used Read")),
- entry(toolResultUser("read result")),
- attachment("todo_reminder", "todo body"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "user", content: "inspect the repo" },
- { role: "assistant", content: "used Read" },
- {
- role: "user",
- content: [{ type: "text", text: "read result" }],
- toolCallId: "tool-1",
- toolName: "Read",
- cacheControl: { type: "ephemeral" },
- },
- { role: "system", content: "todo body" },
- ]);
- });
- it("keeps a pending mid-conversation system reminder after a following user message", () => {
- const entries = [
- entry(user("first prompt"), "real_user"),
- attachment("output_style", "style body"),
- entry(user("second prompt"), "target_continuation"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- { role: "user", content: "first prompt" },
- { role: "user", content: "second prompt" },
- { role: "system", content: "style body" },
- ]);
- });
- it("does not treat a mid-conversation system fallback as latest real user", () => {
- const entries = [
- entry(assistant("used Read")),
- entry(toolResult("read result")),
- attachment("output_style", "style body"),
- entry({ role: "system", content: "stable system" }),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "assistant", content: "used Read" },
- {
- role: "tool",
- content: "read result",
- toolCallId: "tool-1",
- toolName: "Read",
- },
- {
- role: "user",
- content: "<system-reminder>\nstyle body\n</system-reminder>",
- cacheControl: { type: "ephemeral" },
- },
- { role: "system", content: "stable system" },
- ]);
- expect(result.diagnostics.latestRealUserMessageIndex).toBeUndefined();
- });
- it("places cache-control on an assistant message when it is the latest non-system message", () => {
- const entries = [
- entry(user("first prompt"), "real_user"),
- entry(assistant("assistant answer")),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- { role: "user", content: "first prompt" },
- {
- role: "assistant",
- content: "assistant answer",
- cacheControl: { type: "ephemeral" },
- },
- ]);
- expect(result.diagnostics.cacheControlIndex).toBe(1);
- });
- it("preserves system cache-control and clears stale non-system cache-control", () => {
- const entries = [
- {
- message: {
- role: "system",
- content: "stable system",
- cacheControl: { type: "ephemeral" },
- },
- },
- entry({ ...user("first prompt"), cacheControl: { type: "ephemeral" } }, "real_user"),
- entry({ ...assistant("assistant answer"), cacheControl: { type: "ephemeral" } }),
- entry(toolResult("read result")),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries, applyCacheControl: true });
- expect(result.messages).toEqual([
- {
- role: "system",
- content: "stable system",
- cacheControl: { type: "ephemeral" },
- },
- { role: "user", content: "first prompt" },
- { role: "assistant", content: "assistant answer" },
- {
- role: "tool",
- content: "read result",
- toolCallId: "tool-1",
- toolName: "Read",
- cacheControl: { type: "ephemeral" },
- },
- ]);
- expect(result.diagnostics.cacheControlIndex).toBe(3);
- });
- it("does not bubble attachment-like reminders across tool-result user boundaries", () => {
- const entries = [
- entry(assistant("assistant used a tool")),
- entry(toolResultUser()),
- entry(user("memory reminder"), "relevant_memory"),
- entry(user("next real prompt"), "real_user"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- { role: "assistant", content: "assistant used a tool" },
- {
- role: "user",
- content: [{ type: "text", text: "tool result" }],
- toolCallId: "tool-1",
- toolName: "Read",
- },
- { role: "user", content: "memory reminder" },
- { role: "user", content: "next real prompt" },
- ]);
- });
- it("treats literal system-reminder user text as real input when metadata says real_user", () => {
- const entries = [
- entry(user("<system-reminder>\nthis is user-provided text\n</system-reminder>"), "real_user"),
- entry(user("mode reminder"), "runtime_mode"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- {
- role: "user",
- content: "<system-reminder>\nthis is user-provided text\n</system-reminder>",
- },
- { role: "user", content: "mode reminder" },
- ]);
- });
- it("keeps direct pasted image blocks in their original real-user content order", () => {
- const entries = [
- entry(
- user([
- { type: "text", text: "Please inspect this screenshot." },
- { type: "image", mediaType: "image/png", dataUrl: "data:image/png;base64,paste" },
- ]),
- "real_user",
- ),
- entry(user("output style reminder"), "output_style"),
- ] satisfies RuntimeMessageEntry[];
- const result = buildProviderRequestMessages({ entries });
- expect(result.messages).toEqual([
- {
- role: "user",
- content: [
- { type: "text", text: "Please inspect this screenshot." },
- { type: "image", mediaType: "image/png", dataUrl: "data:image/png;base64,paste" },
- ],
- },
- { role: "user", content: "output style reminder" },
- ]);
- });
- it("is deterministic, idempotent, and does not mutate frozen runtime entries", () => {
- const entries = deepFreeze([
- {
- message: {
- role: "user",
- content: [
- { type: "image", mediaType: "image/png", dataUrl: "data:image/png;base64,abc" },
- { type: "text", text: "image context" },
- ],
- },
- metadata: systemReminderRuntimeMetadata("prompt_attachment"),
- },
- {
- message: {
- role: "user",
- content: "real prompt",
- cacheControl: { type: "ephemeral" },
- },
- metadata: realUserRuntimeMetadata(),
- },
- ] satisfies RuntimeMessageEntry[]);
- const first = buildProviderRequestMessages({ entries, applyCacheControl: true, ...FOLD });
- const second = buildProviderRequestMessages({ entries, applyCacheControl: true, ...FOLD });
- const projectedAgain = buildProviderRequestMessages({
- entries: first.messages.map((message) => ({ message })),
- applyCacheControl: true,
- ...FOLD,
- });
- expect(first).toEqual(second);
- expect(projectedAgain.messages).toEqual(first.messages);
- expect(first.messages).toEqual([
- {
- role: "user",
- content: [
- { type: "image", mediaType: "image/png", dataUrl: "data:image/png;base64,abc" },
- { type: "text", text: "image context\n" },
- ],
- },
- {
- role: "user",
- content: "real prompt",
- cacheControl: { type: "ephemeral" },
- },
- ]);
- });
- it("returns request messages detached from runtime message objects", () => {
- const inputMessage: ModelInputMessage = {
- role: "assistant",
- content: [{ type: "text", text: "original answer" }],
- cacheControl: { type: "ephemeral" },
- toolCalls: [{ id: "tool-1", name: "Read", input: { file_path: "notes.md" } }],
- };
- const entries = [{ message: inputMessage }] satisfies RuntimeMessageEntry[];
- const projectedMessage = buildProviderRequestMessages({ entries }).messages[0]!;
- expect(projectedMessage).not.toBe(inputMessage);
- expect(projectedMessage.content).not.toBe(inputMessage.content);
- expect(projectedMessage.cacheControl).not.toBe(inputMessage.cacheControl);
- expect(projectedMessage.toolCalls).not.toBe(inputMessage.toolCalls);
- if (typeof projectedMessage.content !== "string") {
- const textBlock = projectedMessage.content[0];
- if (textBlock?.type === "text") textBlock.text = "mutated answer";
- }
- projectedMessage.toolCalls![0]!.name = "Write";
- projectedMessage.cacheControl = undefined;
- expect(inputMessage).toEqual({
- role: "assistant",
- content: [{ type: "text", text: "original answer" }],
- cacheControl: { type: "ephemeral" },
- toolCalls: [{ id: "tool-1", name: "Read", input: { file_path: "notes.md" } }],
- });
- });
- });
- describe("compaction file restore projection", () => {
- const called = 'Called the Read tool with the following input: {"file_path":"/w/a.txt"}';
- const result = "Result of calling the Read tool:\n1\tA";
- const note =
- "Note: /w/big.txt was read before the last conversation was summarized, but the contents are too large to include. Use Read tool if you need to access it.";
- const wrap = (body: string) => `<system-reminder>\n${body}\n</system-reminder>`;
- const retainedToolRun = (): RuntimeMessageEntry[] => [
- entry(user("prompt"), "real_user"),
- entry({
- role: "assistant",
- content: "",
- toolCalls: [{ id: "tool-1", name: "Read", input: { file_path: "/w/b.txt" } }],
- }),
- entry(toolResult("1\tB")),
- ];
- const compactionAttachments = (): RuntimeMessageEntry[] => [
- attachment("resume_referenced_session_context", called),
- attachment("resume_referenced_session_context", result),
- attachment("compact_file_reference", note),
- attachment("environment", "env body"),
- ];
- // 保留段以 tool result 结尾时,补回正文是 tool result 之后的独立 user 文本,不并入 tool result;
- // 两条正文相邻时调用说明以换行结尾。引用提示和其余附件按 MCS 规则投影。
- it("keeps restored bodies as separate user text after a retained tool result with MCS", () => {
- const messages = buildProviderRequestMessages({
- entries: [...retainedToolRun(), ...compactionAttachments()],
- ...FOLD,
- }).messages;
- expect(messages.slice(2)).toEqual([
- toolResult("1\tB"),
- user(`${wrap(called)}\n`),
- user(wrap(result)),
- { role: "system", content: `${note}\n\nenv body` },
- ]);
- });
- it("keeps restored bodies unfolded without MCS and stops the fold there", () => {
- const messages = buildProviderRequestMessages({
- entries: [...retainedToolRun(), ...compactionAttachments()],
- useMidConversationSystem: false,
- ...FOLD,
- }).messages;
- expect(messages.slice(2)).toEqual([
- toolResult("1\tB"),
- user(`${wrap(called)}\n`),
- user(wrap(result)),
- user(wrap(note)),
- user(wrap("env body")),
- ]);
- });
- // 旧版本写的补回是一条(调用说明和结果用 \n 相连),来源相同,冷恢复后同样在 user 侧。
- it("projects an old single restore entry user-side and unfolded", () => {
- const messages = buildProviderRequestMessages({
- entries: [
- ...retainedToolRun(),
- attachment("resume_referenced_session_context", `${called}\n${result}`),
- ],
- ...FOLD,
- }).messages;
- expect(messages.slice(2)).toEqual([toolResult("1\tB"), user(wrap(`${called}\n${result}`))]);
- });
- it("ends the summary with a newline when restored bodies follow it", () => {
- const messages = buildProviderRequestMessages({
- entries: [
- {
- message: user("Summary:\nA"),
- metadata: { source: "legacy_synthetic", compactBoundary: { boundaryId: "b1" } },
- },
- ...compactionAttachments(),
- ],
- ...FOLD,
- }).messages;
- expect(messages).toEqual([
- user("Summary:\nA\n"),
- user(`${wrap(called)}\n`),
- user(wrap(result)),
- { role: "system", content: `${note}\n\nenv body` },
- ]);
- });
- });
|