| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126 |
- import { describe, expect, it } from "vitest";
- import type {
- SessionGoal,
- MessageId,
- MessagePart,
- MessageWithParts,
- ModelSelection,
- SessionEntryInfo,
- SessionInfo,
- SessionId,
- SessionStorePort,
- TraceContext,
- } from "@zcode/contracts";
- import { SESSION_ENTRY_MODEL_SELECTION } from "@zcode/contracts";
- import { HIGHSPEED_PROVIDER_IDS } from "@zcode/shared";
- import {
- activeSessionMessages,
- hydrateMessageHistoryFromSession,
- } from "../src/agent/session-history-hydrator.js";
- import { MessageHistoryImpl } from "../src/agent/message-history.js";
- import { buildProviderRequestMessages } from "../src/runtime/helpers/provider-request-messages.js";
- import { cloneMessageForFork, clonePartForFork } from "../src/runtime/helpers/steering.js";
- import { buildRuntimeReminderMessage } from "../src/runtime/methods/runtime-reminder-persistence.js";
- import {
- activeForkTranscriptMessages,
- buildForkHistoryMessages,
- createForkedSession,
- copyGoalStateForFork,
- conversationHistoryBeforeInput,
- createForkIdentityMap,
- deriveForkedGoalStatusFromCopiedVerifications,
- buildAtomicForkNotice,
- createSelectionSideConversation,
- forkConversationBeforeMessage,
- forkStableConversationAtMessage,
- selectionSideChatHistoryMessages,
- stableForkHistoryMessages,
- } from "../src/runtime/methods/session-fork.js";
- import type {
- StableConversationForkChildMetadata,
- StableConversationForkTarget,
- } from "../src/runtime/types.js";
- const SESSION_ID = "sess_parent" as SessionId;
- function partId(id: string) {
- return `part_${id}` as MessagePart["id"];
- }
- function userMessage(id: MessageId, text: string, created: number): MessageWithParts {
- return {
- info: {
- id,
- sessionID: SESSION_ID,
- role: "user",
- time: { created },
- agent: "zcode-agent",
- modelSelection: {
- modelId: "glm-4-air" as never,
- providerId: "glm" as never,
- },
- tools: {},
- },
- parts: [
- {
- id: partId(id),
- sessionID: SESSION_ID,
- messageID: id,
- type: "text",
- text,
- },
- ],
- };
- }
- function compactSummaryMessage(id: MessageId, created: number): MessageWithParts {
- return {
- info: {
- id,
- sessionID: SESSION_ID,
- role: "user",
- time: { created },
- agent: "zcode-agent",
- modelSelection: {
- modelId: "glm-4-air" as never,
- providerId: "glm" as never,
- },
- summary: {
- body: "历史摘要",
- diffs: [],
- title: "Compact summary",
- },
- tools: {},
- },
- parts: [
- {
- id: partId(`${id}_text`),
- sessionID: SESSION_ID,
- messageID: id,
- type: "text",
- text: "compact summary context",
- synthetic: true,
- },
- {
- id: partId(`${id}_compaction`),
- sessionID: SESSION_ID,
- messageID: id,
- type: "compaction",
- auto: false,
- compactReason: "user_requested",
- operationId: "cmp_test",
- phase: "standalone_turn",
- trigger: "manual",
- } as MessagePart,
- ],
- };
- }
- function assistantMessage(
- id: MessageId,
- parentId: MessageId,
- text: string,
- created: number,
- ): MessageWithParts {
- return {
- info: {
- id,
- sessionID: SESSION_ID,
- role: "assistant",
- time: { created, completed: created + 1 },
- parentID: parentId,
- agent: "zcode-agent",
- cost: 0,
- mode: "build",
- modelId: "glm-4-air" as never,
- path: { cwd: "/tmp", root: "/tmp" },
- providerId: "glm" as never,
- tokens: {
- cache: { read: 0, write: 0 },
- input: 0,
- output: 0,
- reasoning: 0,
- total: 0,
- },
- },
- parts: [
- {
- id: partId(id),
- sessionID: SESSION_ID,
- messageID: id,
- type: "text",
- text,
- },
- ],
- };
- }
- function goalVerificationEntry(
- id: string,
- anchorAssistantMessageId: string,
- goalIteration: number,
- passed: boolean,
- ): SessionEntryInfo {
- return {
- id,
- sessionID: SESSION_ID,
- time: { created: goalIteration * 10, updated: goalIteration * 10 + 1 },
- type: "target_completion_verification",
- data: {
- eventId: `event_${id}`,
- payload: {
- anchorAssistantMessageId,
- goalIteration,
- status: "completed",
- targetId: "target_goal_1",
- verification: {
- nextAction: passed ? null : "继续",
- passed,
- reason: passed ? "done" : "not yet",
- },
- verificationId: `verify_${goalIteration}`,
- },
- sequenceNumber: goalIteration,
- traceId: `trace_${id}`,
- },
- };
- }
- describe("session fork history", () => {
- it("selection side chat 在生成中只复制稳定 user input 边界", () => {
- const stableUser = userMessage("msg_side_stable_user" as MessageId, "稳定问题", 1);
- const stableAssistant = assistantMessage(
- "msg_side_stable_assistant" as MessageId,
- stableUser.info.id,
- "稳定回答",
- 2,
- );
- const activeUser = userMessage("msg_side_active_user" as MessageId, "本轮问题", 3);
- activeUser.info.anchor = {
- turnId: "turn_side_active" as never,
- productTurnId: "product_side_active",
- orderedMessageIds: [activeUser.info.id],
- boundaryMessageId: activeUser.info.id,
- origin: "realUser",
- };
- const streamingAssistant = assistantMessage(
- "msg_side_streaming_assistant" as MessageId,
- activeUser.info.id,
- "流式片段",
- 4,
- );
- streamingAssistant.info.anchor = {
- ...activeUser.info.anchor,
- orderedMessageIds: [activeUser.info.id, streamingAssistant.info.id],
- boundaryMessageId: streamingAssistant.info.id,
- };
- expect(
- selectionSideChatHistoryMessages(
- [stableUser, stableAssistant, activeUser, streamingAssistant],
- "turn_side_active" as never,
- ).map((message) => message.info.id),
- ).toEqual([stableUser.info.id, stableAssistant.info.id, activeUser.info.id]);
- });
- it("selection side chat 原子创建隐藏 child,不复制 goal 或 fork notice", async () => {
- const user = userMessage("msg_side_user" as MessageId, "解释这里", 1);
- const assistant = assistantMessage(
- "msg_side_assistant" as MessageId,
- user.info.id,
- "父任务回答",
- 2,
- );
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- path: "/workspace",
- permission: [{ permission: "bash", pattern: "*", action: "ask" }],
- projectID: "project-selection-side-chat",
- slug: "parent-selection-side-chat",
- taskType: "interactive",
- time: { created: 1, updated: 1 },
- title: "parent",
- version: "test",
- } as SessionInfo;
- let committedBundle: Parameters<NonNullable<SessionStorePort["commitForkBundle"]>>[0] | null =
- null;
- const parentEvents: unknown[] = [];
- const runtime = {
- activeTurn: null,
- appendEvent: async (event: unknown) => parentEvents.push(event),
- config: { agentName: "zcode-agent", mode: "build" },
- createEvent: (_type: unknown, payload: Record<string, unknown>) => ({ payload }),
- getSessionModelSelection: () => ({
- modelId: "glm-4-air",
- providerId: "glm",
- options: { reasoningLevel: "high" },
- }),
- rootTraceContext: { traceId: "trace_selection_side_chat" },
- sessionId: SESSION_ID,
- sessionStore: {
- async commitForkBundle(bundle) {
- committedBundle = bundle;
- return bundle.child as SessionInfo;
- },
- async getSession() {
- return parentSession;
- },
- async messages() {
- return [user, assistant];
- },
- } satisfies Partial<SessionStorePort>,
- workingDirectory: "/workspace",
- workspaceRoot: "/workspace",
- };
- const result = await createSelectionSideConversation.call(runtime as never, {
- sourceCommandId: "cmd-selection-side-chat",
- });
- expect(result.parentSessionId).toBe(SESSION_ID);
- expect(parentEvents).toEqual([]);
- expect(committedBundle!.child).toMatchObject({
- parentID: SESSION_ID,
- permission: parentSession.permission,
- taskType: "selection_side_chat",
- title: "Selection side chat",
- });
- expect(committedBundle!.goal).toBeUndefined();
- expect(committedBundle!.entries).toEqual([
- expect.objectContaining({
- sessionID: result.forkedSessionId,
- type: SESSION_ENTRY_MODEL_SELECTION,
- touchSession: false,
- data: {
- modelId: "glm-4-air",
- providerId: "glm",
- options: { reasoningLevel: "high" },
- },
- }),
- expect.objectContaining({
- type: "runtime/execution_state",
- data: { mode: "build", planEnabled: false },
- }),
- ]);
- expect(committedBundle!.commandFact.ack.result).toEqual({
- type: "createSelectionSideSession",
- sessionId: result.forkedSessionId,
- });
- expect(committedBundle!.messages).toHaveLength(3);
- expect(
- committedBundle!.messages.every(
- (message) =>
- message.info.visibility === "model-only" &&
- message.info.semantics?.uiVisibility === "hidden" &&
- message.info.semantics.providerVisibility === "visible",
- ),
- ).toBe(true);
- expect(committedBundle!.messages.at(-1)?.info.source).toBe("selection_side_chat");
- expect(JSON.stringify(committedBundle!.messages)).not.toContain("session_fork");
- // 直接恢复 producer 的原子 bundle,避免手写 metadata fixture 掩盖 fork 漏写字段。
- const persisted = JSON.parse(JSON.stringify(committedBundle!.messages)) as MessageWithParts[];
- const boundary = persisted.at(-1)!;
- expect(boundary.parts[0]).toMatchObject({
- type: "text",
- synthetic: true,
- metadata: {
- source: "selection_side_chat",
- runtimeMessage: { source: "selection_side_chat" },
- },
- });
- const boundaryText = boundary.parts[0]!.type === "text" ? boundary.parts[0]!.text : "";
- expect(boundaryText).toContain("Do not continue the parent's active work automatically");
- expect(boundaryText).not.toContain("<system-reminder>");
- const history = new MessageHistoryImpl();
- await hydrateMessageHistoryFromSession({ history, messages: persisted });
- expect(history.toRuntimeEntries().at(-1)).toEqual({
- kind: "attachment",
- content: boundaryText,
- metadata: { source: "selection_side_chat" },
- });
- history.addUser("child question", { source: "real_user" });
- for (const useMidConversationSystem of [true, false]) {
- const request = buildProviderRequestMessages({
- entries: history.toRuntimeEntries(),
- useMidConversationSystem,
- });
- expect(request.messages.slice(-2)).toEqual([
- { role: "user", content: `<system-reminder>\n${boundaryText}\n</system-reminder>` },
- { role: "user", content: "child question" },
- ]);
- expect(JSON.stringify(request.messages).split(boundaryText)).toHaveLength(2);
- }
- });
- const selectionSideChatGoalCases: ReadonlyArray<{
- label: string;
- passed?: boolean;
- status: SessionGoal["status"];
- }> = [
- { label: "active 无 verifier", status: "active" },
- { label: "failed verifier 后 paused", passed: false, status: "paused" },
- { label: "passed verifier 后 complete", passed: true, status: "complete" },
- ];
- it.each(selectionSideChatGoalCases)(
- "selection side chat 从 $label Goal 父任务创建时不继承 Goal 运行态",
- async ({ label, passed, status }) => {
- const user = userMessage(`msg_side_goal_user_${status}` as MessageId, "解释这里", 1);
- const assistant = assistantMessage(
- `msg_side_goal_assistant_${status}` as MessageId,
- user.info.id,
- "父任务回答",
- 2,
- );
- const targetId = `target_side_goal_${status}`;
- const verificationEntryId = `entry_side_goal_${status}`;
- const verificationId = `verify_side_goal_${status}`;
- const goal: SessionGoal = {
- activeInputId: status === "active" ? "input-side-goal" : null,
- activeRunLastSeenAtMs: status === "active" ? 2 : null,
- activeRunStartedAtMs: status === "active" ? 1 : null,
- objective: `Goal ${label}`,
- sessionID: SESSION_ID,
- status,
- summaryTitle: null,
- targetID: targetId,
- time: { created: 1, updated: 2 },
- timeUsedSeconds: 1,
- tokenBudget: null,
- tokensUsed: 1,
- };
- const anchor = {
- boundaryMessageId: assistant.info.id,
- goalBoundary: {
- kind: "snapshot" as const,
- target: goal,
- verificationEntryIds: passed === undefined ? [] : [verificationEntryId],
- },
- orderedMessageIds: [user.info.id, assistant.info.id],
- productTurnId: `product_side_goal_${status}`,
- turnId: `turn_side_goal_${status}` as never,
- };
- user.info.anchor = anchor;
- assistant.info.anchor = anchor;
- if (passed !== undefined) {
- assistant.parts.push({
- anchorMessageId: assistant.info.id,
- anchorTurnId: anchor.turnId,
- display: "separator",
- id: partId(`side_goal_timeline_${status}`),
- messageID: assistant.info.id,
- sessionID: SESSION_ID,
- status: "completed",
- targetId,
- timelineType: "goal_verification",
- type: "timeline",
- verificationId,
- } as MessagePart);
- }
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- path: "/workspace",
- projectID: "project-selection-side-chat-goal",
- slug: `parent-selection-side-chat-${status}`,
- taskType: "interactive",
- time: { created: 1, updated: 2 },
- title: "parent goal",
- version: "test",
- } as SessionInfo;
- let sessionEntriesReadCount = 0;
- let committedBundle: Parameters<NonNullable<SessionStorePort["commitForkBundle"]>>[0] | null =
- null;
- const runtime = {
- activeTurn: null,
- appendEvent: async () => undefined,
- config: { agentName: "zcode-agent", mode: "build" },
- createEvent: () => ({ type: "session.forked" }),
- getSessionModelSelection: () => ({ modelId: "glm-4-air", providerId: "glm" }),
- rootTraceContext: { traceId: `trace_selection_side_chat_${status}` },
- sessionId: SESSION_ID,
- sessionStore: {
- async commitForkBundle(bundle) {
- committedBundle = bundle;
- return bundle.child as SessionInfo;
- },
- async getSession() {
- return parentSession;
- },
- async messages() {
- return [user, assistant];
- },
- async sessionEntries() {
- sessionEntriesReadCount += 1;
- return [];
- },
- } satisfies Partial<SessionStorePort>,
- workingDirectory: "/workspace",
- workspaceRoot: "/workspace",
- };
- await expect(
- createSelectionSideConversation.call(runtime as never, {
- sourceCommandId: `cmd-selection-side-chat-${status}`,
- }),
- ).resolves.toMatchObject({ parentSessionId: SESSION_ID });
- expect(sessionEntriesReadCount).toBe(0);
- expect(committedBundle!.goal).toBeUndefined();
- expect(committedBundle!.entries).toEqual([
- expect.objectContaining({
- sessionID: committedBundle!.child.id,
- type: SESSION_ENTRY_MODEL_SELECTION,
- touchSession: false,
- data: { modelId: "glm-4-air", providerId: "glm" },
- }),
- expect.objectContaining({
- type: "runtime/execution_state",
- data: { mode: "build", planEnabled: false },
- }),
- ]);
- const copiedParentMessages = committedBundle!.messages.slice(0, -1);
- expect(copiedParentMessages).toHaveLength(2);
- expect(
- copiedParentMessages.every((message) => message.info.anchor?.goalBoundary === undefined),
- ).toBe(true);
- expect(copiedParentMessages[0]?.parts).toEqual(
- expect.arrayContaining([expect.objectContaining({ text: "解释这里", type: "text" })]),
- );
- expect(JSON.stringify(copiedParentMessages)).not.toContain(targetId);
- expect(JSON.stringify(copiedParentMessages)).not.toContain(verificationEntryId);
- expect(user.info.anchor?.goalBoundary).toBe(anchor.goalBoundary);
- },
- );
- it("ForkIdentityMap 预分配 fork notice 全部 identity,builder 只能消费 plan", () => {
- const user = userMessage("msg_notice_parent_user" as MessageId, "notice", 1);
- const assistant = assistantMessage(
- "msg_notice_parent_assistant" as MessageId,
- user.info.id,
- "answer",
- 2,
- );
- const childSessionId = "sess_notice_plan_child" as SessionId;
- const identities = createForkIdentityMap({
- childSessionId,
- entries: [],
- goalSnapshots: [],
- messages: [user, assistant],
- parentSessionId: SESSION_ID,
- });
- const runtime = {
- config: { agentName: "zcode-agent", mode: "build" },
- getSessionModelSelection: () => ({ modelId: "glm-4-air", providerId: "glm" }),
- sessionId: SESSION_ID,
- workingDirectory: "/workspace",
- workspaceRoot: "/workspace",
- };
- const notices = buildAtomicForkNotice(runtime as never, {
- identities,
- sourceCommandId: "cmd-notice-plan",
- targetMessageId: assistant.info.id,
- });
- const hidden = notices[0]!;
- const notice = notices[1]!;
- const timeline = notice.parts[0];
- expect(identities.notice).toEqual({
- hiddenMessageId: expect.any(String),
- hiddenPartId: expect.any(String),
- messageId: expect.any(String),
- partId: expect.any(String),
- turnId: expect.any(String),
- productTurnId: expect.any(String),
- });
- expect(hidden.info.id).toBe(identities.notice.hiddenMessageId);
- expect(hidden.parts[0]?.id).toBe(identities.notice.hiddenPartId);
- expect(notice.info.id).toBe(identities.notice.messageId);
- expect(notice.parts[0]?.id).toBe(identities.notice.partId);
- expect(hidden.info.anchor).toMatchObject({
- turnId: identities.notice.turnId,
- productTurnId: identities.notice.productTurnId,
- orderedMessageIds: [identities.notice.hiddenMessageId, identities.notice.messageId],
- boundaryMessageId: identities.notice.messageId,
- });
- expect(notice.info.anchor).toEqual(hidden.info.anchor);
- expect(notice.info.role).toBe("assistant");
- if (notice.info.role !== "assistant" || timeline?.type !== "timeline") return;
- expect(notice.info.parentID).toBe(identities.notice.hiddenMessageId);
- expect(timeline.anchorMessageId).toBe(identities.messageIds.get(assistant.info.id));
- expect(timeline.anchorTurnId).toBe(identities.notice.turnId);
- // parent target 只允许保留在 forkOrigin/session_fork 的来源事实;所有 child-local 引用必须闭合。
- const childLocalRefs = {
- hiddenId: hidden.info.id,
- hiddenPartId: hidden.parts[0]?.id,
- hiddenPartMessageId: hidden.parts[0]?.messageID,
- noticeId: notice.info.id,
- noticePartId: notice.parts[0]?.id,
- noticePartMessageId: notice.parts[0]?.messageID,
- noticeParentId: notice.info.parentID,
- hiddenAnchor: hidden.info.anchor,
- noticeAnchor: notice.info.anchor,
- timelineAnchorMessageId: timeline.anchorMessageId,
- timelineAnchorTurnId: timeline.anchorTurnId,
- };
- expect(JSON.stringify(childLocalRefs)).not.toContain("msg_notice_parent_user");
- expect(JSON.stringify(childLocalRefs)).not.toContain("msg_notice_parent_assistant");
- const grandchildSessionId = "sess_notice_plan_grandchild" as SessionId;
- const forkAgainIdentities = createForkIdentityMap({
- childSessionId: grandchildSessionId,
- entries: [],
- goalSnapshots: [],
- messages: notices,
- parentSessionId: childSessionId,
- });
- const forkAgainNotices = buildAtomicForkNotice(
- { ...runtime, sessionId: childSessionId } as never,
- {
- identities: forkAgainIdentities,
- sourceCommandId: "cmd-notice-plan-again",
- targetMessageId: notice.info.id,
- },
- );
- const forkAgainHidden = forkAgainNotices[0]!;
- const forkAgainNotice = forkAgainNotices[1]!;
- const forkAgainTimeline = forkAgainNotice.parts[0];
- expect(forkAgainNotice.info.role).toBe("assistant");
- if (forkAgainNotice.info.role !== "assistant" || forkAgainTimeline?.type !== "timeline") return;
- const forkAgainLocalRefs = {
- hiddenId: forkAgainHidden.info.id,
- hiddenPartId: forkAgainHidden.parts[0]?.id,
- hiddenPartMessageId: forkAgainHidden.parts[0]?.messageID,
- hiddenPartSessionId: forkAgainHidden.parts[0]?.sessionID,
- noticeId: forkAgainNotice.info.id,
- noticePartId: forkAgainTimeline.id,
- noticePartMessageId: forkAgainTimeline.messageID,
- noticeParentId: forkAgainNotice.info.parentID,
- hiddenAnchor: forkAgainHidden.info.anchor,
- noticeAnchor: forkAgainNotice.info.anchor,
- timelineAnchorMessageId: forkAgainTimeline.anchorMessageId,
- timelineAnchorTurnId: forkAgainTimeline.anchorTurnId,
- };
- for (const parentLocalId of [
- identities.notice.hiddenMessageId,
- identities.notice.hiddenPartId,
- identities.notice.messageId,
- identities.notice.partId,
- identities.notice.turnId,
- ]) {
- expect(JSON.stringify(forkAgainLocalRefs)).not.toContain(parentLocalId);
- }
- });
- it("fork-before-input 的选型回退跳过停止后的中断标记", async () => {
- const user = userMessage("msg_marker_user" as MessageId, "first", 1);
- const assistant = assistantMessage(
- "msg_marker_assistant" as MessageId,
- user.info.id,
- "stopped answer",
- 2,
- );
- if (assistant.info.role !== "assistant") return;
- assistant.info.reasoningLevel = "max";
- const target = userMessage("msg_marker_target" as MessageId, "edit me", 4);
- const runtimeSelection = {
- modelId: "glm-4-air",
- providerId: "glm",
- options: { reasoningLevel: "high" },
- };
- // 停止后写的中断标记:只记 provider/model,不带 options。
- const marker = buildRuntimeReminderMessage(
- {
- config: { agentName: "zcode-agent" },
- getSessionModelSelection: () => runtimeSelection,
- sessionId: SESSION_ID,
- } as never,
- {
- message: { role: "user", content: "[Request interrupted by user]" },
- metadata: { source: "interrupt_marker" },
- },
- { created: 3 },
- )!;
- let committedBundle: Parameters<NonNullable<SessionStorePort["commitForkBundle"]>>[0] | null =
- null;
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- path: "/workspace",
- projectID: "project-marker-fork",
- slug: "parent-marker-fork",
- taskType: "interactive",
- time: { created: 1, updated: 1 },
- title: "parent",
- version: "test",
- } as SessionInfo;
- const runtime = {
- appendEvent: async () => undefined,
- config: { agentName: "zcode-agent", mode: "build" },
- createEvent: (_type: unknown, payload: Record<string, unknown>) => ({ payload }),
- getSessionModelSelection: () => runtimeSelection,
- rootTraceContext: { traceId: "trace_marker_fork" },
- sessionId: SESSION_ID,
- sessionStore: {
- async commitForkBundle(bundle) {
- committedBundle = bundle;
- return bundle.child as SessionInfo;
- },
- async getSession() {
- return parentSession;
- },
- async messages() {
- return [user, assistant, marker, target];
- },
- } satisfies Partial<SessionStorePort>,
- workingDirectory: "/workspace",
- workspaceRoot: "/workspace",
- };
- await forkConversationBeforeMessage.call(runtime as never, {
- commandFact: {
- parentSessionId: SESSION_ID,
- sourceCommandId: "cmd-marker-fork",
- ack: {
- commandId: "cmd-marker-fork",
- status: "accepted",
- revisionAtDecision: 1,
- result: { type: "editUserQuery", disposition: "fork", sessionId: "sess_marker_child" },
- },
- metadata: {},
- },
- forkedSessionId: "sess_marker_child" as SessionId,
- goalBoundary: { kind: "none" },
- initialInput: {
- id: "queue_cmd-marker-fork",
- sessionID: "sess_marker_child" as SessionId,
- kind: "sendText",
- delivery: "startNow",
- payload: { text: "edited" },
- },
- sourceCommandId: "cmd-marker-fork",
- targetMessageId: target.info.id,
- targetProductTurnId: "turn-product",
- targetTranscriptTurnId: "turn-runtime",
- });
- // 取 fork 点之前最后一条对话消息(assistant)的选型,而不是中断标记或 runtime 当前值。
- expect(
- committedBundle!.entries.find((entry) => entry.type === SESSION_ENTRY_MODEL_SELECTION)?.data,
- ).toEqual({ modelId: "glm-4-air", providerId: "glm", options: { reasoningLevel: "max" } });
- });
- it("V4 stable fork 只通过 commitForkBundle 一次发布完整 child", async () => {
- const user = userMessage("msg_atomic_user" as MessageId, "fork me", 1);
- const assistant = assistantMessage(
- "msg_atomic_assistant" as MessageId,
- user.info.id,
- "stable answer",
- 2,
- );
- if (assistant.info.role !== "assistant") return;
- assistant.info.reasoningLevel = "max";
- assistant.info.mode = "edit";
- assistant.info.planEnabled = true;
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- path: "/workspace",
- projectID: "project-atomic-stable",
- slug: "parent-atomic-stable",
- taskType: "interactive",
- time: { created: 1, updated: 1 },
- title: "parent",
- version: "test",
- } as SessionInfo;
- let committedBundle: Parameters<NonNullable<SessionStorePort["commitForkBundle"]>>[0] | null =
- null;
- let legacyCreateCount = 0;
- const runtime = {
- appendEvent: async () => undefined,
- config: { agentName: "zcode-agent", mode: "build" },
- createEvent: (_type: unknown, payload: Record<string, unknown>) => ({ payload }),
- getSessionModelSelection: () => ({
- modelId: "glm-4-air",
- providerId: "glm",
- options: { reasoningLevel: "high" },
- }),
- rootTraceContext: { traceId: "trace_atomic_stable" },
- sessionId: SESSION_ID,
- sessionStore: {
- async commitForkBundle(bundle) {
- committedBundle = bundle;
- return bundle.child as SessionInfo;
- },
- async createForkedSessionWithMetadata() {
- legacyCreateCount += 1;
- throw new Error("V4 stable fork touched legacy create path");
- },
- async getSession() {
- return parentSession;
- },
- async messages() {
- return [user, assistant];
- },
- } satisfies Partial<SessionStorePort>,
- workingDirectory: "/workspace",
- workspaceRoot: "/workspace",
- };
- const result = await forkStableConversationAtMessage.call(runtime as never, {
- forkedSessionId: "sess_atomic_stable_child" as SessionId,
- goalBoundary: { kind: "none" },
- sourceCommandId: "cmd-atomic-stable",
- target: {
- productTurnId: "product-atomic",
- transcriptTurnId: "turn-atomic",
- orderedMessageIds: [String(user.info.id), String(assistant.info.id)],
- boundaryMessageId: String(assistant.info.id),
- },
- });
- expect(result.forkedSessionId).toBe("sess_atomic_stable_child");
- expect(committedBundle!.entries).toContainEqual(
- expect.objectContaining({
- type: "runtime/execution_state",
- data: { mode: "edit", planEnabled: true },
- }),
- );
- expect(legacyCreateCount).toBe(0);
- expect(committedBundle!.messages).toHaveLength(4);
- expect(committedBundle!.messages.at(-1)!.info).toMatchObject({
- mode: "edit",
- planEnabled: true,
- });
- const modelSelectionEntry = committedBundle!.entries.find(
- (entry) => entry.type === SESSION_ENTRY_MODEL_SELECTION,
- );
- expect(modelSelectionEntry).toMatchObject({
- id: "sess_atomic_stable_child:runtime-model-selection",
- sessionID: "sess_atomic_stable_child",
- type: SESSION_ENTRY_MODEL_SELECTION,
- touchSession: false,
- data: {
- modelId: "glm-4-air",
- providerId: "glm",
- options: { reasoningLevel: "max" },
- },
- });
- const forkNoticeAssistant = committedBundle!.messages.at(-1)?.info;
- expect(forkNoticeAssistant).toMatchObject({
- role: "assistant",
- modelId: "glm-4-air",
- providerId: "glm",
- reasoningLevel: "max",
- });
- expect(committedBundle!.commandFact.ack.result).toEqual({
- type: "forkAssistant",
- sessionId: "sess_atomic_stable_child",
- });
- // 从真实 producer bundle 恢复,防止 notice 缺少 part metadata 后退化为普通 user 文本。
- const persisted = JSON.parse(JSON.stringify(committedBundle!.messages)) as MessageWithParts[];
- const boundary = persisted.at(-2)!;
- expect(boundary.info.semantics).toMatchObject({
- uiVisibility: "hidden",
- providerVisibility: "visible",
- transcriptVisibility: "hidden",
- });
- expect(boundary.parts[0]).toMatchObject({
- type: "text",
- synthetic: true,
- metadata: { source: "fork", runtimeMessage: { source: "conversation_fork" } },
- });
- const boundaryText = boundary.parts[0]!.type === "text" ? boundary.parts[0]!.text : "";
- expect(boundaryText).toContain("This session was forked from a previous session message.");
- expect(boundaryText).not.toContain("<system-reminder>");
- const history = new MessageHistoryImpl();
- await hydrateMessageHistoryFromSession({ history, messages: persisted });
- expect(history.toRuntimeEntries().at(-1)).toEqual({
- kind: "attachment",
- content: boundaryText,
- metadata: { source: "conversation_fork" },
- });
- history.addUser("child question", { source: "real_user" });
- for (const useMidConversationSystem of [true, false]) {
- const request = buildProviderRequestMessages({
- entries: history.toRuntimeEntries(),
- useMidConversationSystem,
- });
- expect(request.messages.slice(-2)).toEqual([
- { role: "user", content: `<system-reminder>\n${boundaryText}\n</system-reminder>` },
- { role: "user", content: "child question" },
- ]);
- expect(
- request.messages.filter(
- (message) =>
- typeof message.content === "string" && message.content.includes(boundaryText),
- ),
- ).toHaveLength(1);
- }
- });
- it("加速轮分叉不继承加速卡 Selection,child 回落父会话常驻模型", async () => {
- const user = userMessage("msg_highspeed_user" as MessageId, "加速中提问", 1);
- const assistant = assistantMessage(
- "msg_highspeed_assistant" as MessageId,
- user.info.id,
- "加速回答",
- 2,
- );
- if (user.info.role !== "user" || assistant.info.role !== "assistant") return;
- // 加速轮的 user/assistant 按 highspeed-card-spec §5 记录本轮真实执行模型(单轮执行事实)。
- user.info.modelSelection = {
- modelId: "glm-4.6" as never,
- providerId: HIGHSPEED_PROVIDER_IDS.zai as never,
- };
- assistant.info.providerId = HIGHSPEED_PROVIDER_IDS.zai as never;
- assistant.info.modelId = "glm-4.6" as never;
- assistant.info.reasoningLevel = "max";
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- path: "/workspace",
- projectID: "project-highspeed-fork",
- slug: "parent-highspeed-fork",
- taskType: "interactive",
- time: { created: 1, updated: 1 },
- title: "parent",
- version: "test",
- } as SessionInfo;
- let committedBundle: Parameters<NonNullable<SessionStorePort["commitForkBundle"]>>[0] | null =
- null;
- const runtime = {
- appendEvent: async () => undefined,
- config: { agentName: "zcode-agent", mode: "build" },
- createEvent: (_type: unknown, payload: Record<string, unknown>) => ({ payload }),
- // 加速是 `selectionScope=execution`,从不改写常驻 Selection:它就是用户的原模型。
- getSessionModelSelection: () => ({
- modelId: "glm-4-air",
- providerId: "glm",
- options: { reasoningLevel: "high" },
- }),
- rootTraceContext: { traceId: "trace_highspeed_fork" },
- sessionId: SESSION_ID,
- sessionStore: {
- async commitForkBundle(bundle) {
- committedBundle = bundle;
- return bundle.child as SessionInfo;
- },
- async getSession() {
- return parentSession;
- },
- async messages() {
- return [user, assistant];
- },
- } satisfies Partial<SessionStorePort>,
- workingDirectory: "/workspace",
- workspaceRoot: "/workspace",
- };
- // 不带显式选型(回落历史/常驻)与调用方显式传入加速选型,child 结果必须一致。
- const explicitCases: (ModelSelection | undefined)[] = [
- undefined,
- { modelId: "glm-4.6" as never, providerId: HIGHSPEED_PROVIDER_IDS.zai as never },
- ];
- for (const [index, explicit] of explicitCases.entries()) {
- committedBundle = null;
- await forkStableConversationAtMessage.call(runtime as never, {
- forkedSessionId: `sess_highspeed_child_${index}` as SessionId,
- goalBoundary: { kind: "none" },
- sourceCommandId: `cmd-highspeed-fork-${index}`,
- ...(explicit ? { modelSelection: explicit } : {}),
- target: {
- productTurnId: "product-highspeed",
- transcriptTurnId: "turn-highspeed",
- orderedMessageIds: [String(user.info.id), String(assistant.info.id)],
- boundaryMessageId: String(assistant.info.id),
- },
- });
- // child 常驻在加速 Provider 上会让用户看到「原模型没带过来」,首轮还会 ModelRequestAuthMissing。
- expect(
- committedBundle!.entries.find((entry) => entry.type === SESSION_ENTRY_MODEL_SELECTION),
- ).toMatchObject({
- sessionID: `sess_highspeed_child_${index}`,
- data: { modelId: "glm-4-air", providerId: "glm", options: { reasoningLevel: "high" } },
- });
- expect(committedBundle!.messages.at(-1)!.info).toMatchObject({
- role: "assistant",
- modelId: "glm-4-air",
- providerId: "glm",
- });
- expect(JSON.stringify(committedBundle!.entries)).not.toContain(HIGHSPEED_PROVIDER_IDS.zai);
- }
- });
- it("fork-of-fork 只从 child-local anchor 构造 canonical target,不泄漏 parent id", () => {
- const user = userMessage("msg_parent_user" as MessageId, "fork twice", 1);
- const assistant = assistantMessage(
- "msg_parent_assistant" as MessageId,
- user.info.id,
- "stable answer",
- 2,
- );
- const parentOrderedIds = [user.info.id, assistant.info.id];
- user.info.anchor = {
- turnId: "turn_parent" as never,
- productTurnId: "product_parent",
- orderedMessageIds: parentOrderedIds,
- boundaryMessageId: assistant.info.id,
- };
- assistant.info.anchor = { ...user.info.anchor };
- const firstChildId = "sess_first_child" as SessionId;
- const messageIdMap = new Map<MessageId, MessageId>([
- [user.info.id, "msg_child_user" as MessageId],
- [assistant.info.id, "msg_child_assistant" as MessageId],
- ]);
- const turnIdMap = new Map([["turn_parent", "turn_child"]]);
- const productTurnIdMap = new Map([["product_parent", "product_child"]]);
- const cloned = [user, assistant].map((message) => ({
- info: cloneMessageForFork(message.info, {
- forkedSessionId: firstChildId,
- messageIdMap,
- nextMessageId: messageIdMap.get(message.info.id)!,
- turnIdMap,
- productTurnIdMap,
- strictLocalReferences: true,
- } as never),
- parts: [],
- }));
- const childAnchor = cloned[1]!.info.anchor!;
- expect(childAnchor.orderedMessageIds).toEqual(["msg_child_user", "msg_child_assistant"]);
- expect(childAnchor.boundaryMessageId).toBe("msg_child_assistant");
- expect(childAnchor.turnId).not.toBe("turn_parent");
- expect(childAnchor.productTurnId).not.toBe("product_parent");
- expect(() =>
- stableForkHistoryMessages(cloned, {
- productTurnId: childAnchor.productTurnId!,
- transcriptTurnId: String(childAnchor.turnId),
- orderedMessageIds: childAnchor.orderedMessageIds!.map(String),
- boundaryMessageId: String(childAnchor.boundaryMessageId),
- }),
- ).not.toThrow();
- });
- it("remaps compact leading announcement ids and drops ids outside the child transcript", () => {
- const summaryId = "msg_leading_summary" as MessageId;
- const leadingId = "msg_leading_announcement" as MessageId;
- const cloned = clonePartForFork(
- {
- id: partId("leading_compaction"),
- sessionID: SESSION_ID,
- messageID: summaryId,
- type: "compaction",
- auto: false,
- metadata: {
- compactLeadingMessageIds: [leadingId, "msg_parent_only" as MessageId],
- },
- } as MessagePart,
- {
- forkedSessionId: "ses_leading_child" as never,
- nextMessageId: "msg_child_summary" as MessageId,
- messageIdMap: new Map<MessageId, MessageId>([
- [summaryId, "msg_child_summary" as MessageId],
- [leadingId, "msg_child_announcement" as MessageId],
- ]),
- strictLocalReferences: true,
- },
- );
- // 旧版本 fork 原样复制 metadata;映射不到的声明只影响公告位置,不能阻断 fork。
- expect(cloned).toMatchObject({
- metadata: { compactLeadingMessageIds: ["msg_child_announcement"] },
- });
- });
- it("stable fork golden remap 覆盖 message/part/turn/compact/goal/verifier 全部 child-local identity", async () => {
- const user = userMessage("msg_golden_user" as MessageId, "golden", 1);
- const assistant = assistantMessage(
- "msg_golden_assistant" as MessageId,
- user.info.id,
- "golden answer",
- 2,
- );
- const goal: SessionGoal = {
- sessionID: SESSION_ID,
- targetID: "target_golden_parent",
- objective: "golden goal",
- summaryTitle: null,
- status: "active",
- tokenBudget: null,
- tokensUsed: 1,
- timeUsedSeconds: 1,
- activeInputId: "parent-active-input",
- activeRunStartedAtMs: 10,
- activeRunLastSeenAtMs: 11,
- time: { created: 1, updated: 2 },
- };
- const anchor = {
- turnId: "turn_golden_parent" as never,
- productTurnId: "product_golden_parent",
- orderedMessageIds: [user.info.id, assistant.info.id],
- boundaryMessageId: assistant.info.id,
- goalBoundary: {
- kind: "snapshot" as const,
- target: goal,
- verificationEntryIds: ["entry_golden_parent"],
- },
- };
- user.info.anchor = anchor;
- assistant.info.anchor = anchor;
- assistant.parts.push(
- {
- id: partId("golden_compaction"),
- sessionID: SESSION_ID,
- messageID: assistant.info.id,
- type: "compaction",
- auto: false,
- tail_start_id: user.info.id,
- summaryMessageId: assistant.info.id,
- compactBoundary: {
- boundaryId: "compact-golden",
- trigger: "manual",
- preCompactTokenCount: 10,
- summarizedMessageCount: 2,
- lastSummarizedMessageId: assistant.info.id,
- preservedSegment: {
- headMessageId: user.info.id,
- anchorMessageId: assistant.info.id,
- tailMessageId: assistant.info.id,
- },
- summaryMessageIds: [assistant.info.id],
- traceId: "trace-golden" as never,
- turnId: "turn_golden_parent" as never,
- },
- } as MessagePart,
- {
- id: partId("golden_compact_timeline"),
- sessionID: SESSION_ID,
- messageID: assistant.info.id,
- type: "timeline",
- timelineType: "context_compaction",
- display: "separator",
- status: "completed",
- operationId: "compact-golden",
- trigger: "manual",
- anchorMessageId: assistant.info.id,
- anchorTurnId: "turn_golden_parent" as never,
- summaryMessageId: assistant.info.id,
- } as MessagePart,
- {
- id: partId("golden_goal_timeline"),
- sessionID: SESSION_ID,
- messageID: assistant.info.id,
- type: "timeline",
- timelineType: "goal_verification",
- display: "separator",
- status: "completed",
- targetId: goal.targetID,
- verificationId: "verification_golden_parent",
- anchorMessageId: assistant.info.id,
- anchorTurnId: "turn_golden_parent" as never,
- } as MessagePart,
- {
- id: partId("golden_tool"),
- sessionID: SESSION_ID,
- messageID: assistant.info.id,
- type: "tool",
- callID: "tool_call_golden_parent",
- tool: "read",
- state: {
- status: "completed",
- input: {},
- output: "ok",
- title: "read",
- metadata: {},
- time: { start: 1, end: 2 },
- attachments: [
- {
- id: partId("golden_tool_attachment"),
- sessionID: SESSION_ID,
- messageID: assistant.info.id,
- type: "file",
- mime: "text/plain",
- url: "data:text/plain,ok",
- },
- ],
- },
- } as MessagePart,
- );
- const verificationEntry = goalVerificationEntry(
- "entry_golden_parent",
- String(assistant.info.id),
- 1,
- false,
- );
- const verificationData = verificationEntry.data as {
- payload: Record<string, unknown>;
- };
- verificationData.payload.targetId = goal.targetID;
- verificationData.payload.verificationId = "verification_golden_parent";
- verificationData.payload.anchorTurnId = "turn_golden_parent";
- const childSessionId = "sess_golden_child" as SessionId;
- let bundle: Parameters<NonNullable<SessionStorePort["commitForkBundle"]>>[0] | null = null;
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- projectID: "project-golden",
- slug: "parent-golden",
- taskType: "interactive",
- time: { created: 1, updated: 1 },
- title: "parent",
- version: "test",
- } as SessionInfo;
- const runtime = {
- appendEvent: async () => undefined,
- config: { agentName: "zcode-agent", mode: "build" },
- createEvent: () => ({ type: "session.forked" }),
- getSessionModelSelection: () => ({ modelId: "glm-4-air", providerId: "glm" }),
- rootTraceContext: { traceId: "trace-golden" },
- sessionId: SESSION_ID,
- sessionStore: {
- async commitForkBundle(value) {
- bundle = value;
- return value.child as SessionInfo;
- },
- async getSession() {
- return parentSession;
- },
- async messages() {
- return [user, assistant];
- },
- async sessionEntries() {
- return [verificationEntry];
- },
- } satisfies Partial<SessionStorePort>,
- workingDirectory: "/workspace",
- workspaceRoot: "/workspace",
- };
- await forkStableConversationAtMessage.call(runtime as never, {
- forkedSessionId: childSessionId,
- goalBoundary: anchor.goalBoundary,
- sourceCommandId: "cmd-golden",
- target: {
- productTurnId: anchor.productTurnId,
- transcriptTurnId: String(anchor.turnId),
- orderedMessageIds: anchor.orderedMessageIds.map(String),
- boundaryMessageId: String(anchor.boundaryMessageId),
- },
- });
- const copiedUser = bundle!.messages[0]!;
- const copiedAssistant = bundle!.messages[1]!;
- const copiedAnchor = copiedAssistant.info.anchor!;
- const copiedCompaction = copiedAssistant.parts.find((part) => part.type === "compaction");
- const copiedCompactTimeline = copiedAssistant.parts.find(
- (part) => part.type === "timeline" && part.timelineType === "context_compaction",
- );
- const copiedGoalTimeline = copiedAssistant.parts.find(
- (part) => part.type === "timeline" && part.timelineType === "goal_verification",
- );
- const copiedTool = copiedAssistant.parts.find((part) => part.type === "tool");
- const copiedEntry = bundle!.entries[0]!;
- const copiedEntryPayload = (copiedEntry.data as { payload: Record<string, unknown> }).payload;
- expect(copiedUser.info.sessionID).toBe(childSessionId);
- expect(copiedAssistant.info.sessionID).toBe(childSessionId);
- expect(copiedAssistant.info.role).toBe("assistant");
- if (copiedAssistant.info.role !== "assistant") return;
- expect(copiedAssistant.info.parentID).toBe(copiedUser.info.id);
- expect(copiedAnchor.orderedMessageIds).toEqual([copiedUser.info.id, copiedAssistant.info.id]);
- expect(copiedAnchor.boundaryMessageId).toBe(copiedAssistant.info.id);
- expect(copiedAnchor.turnId).not.toBe(anchor.turnId);
- expect(copiedAnchor.productTurnId).not.toBe(anchor.productTurnId);
- expect(copiedCompaction).toMatchObject({
- sessionID: childSessionId,
- messageID: copiedAssistant.info.id,
- tail_start_id: copiedUser.info.id,
- summaryMessageId: copiedAssistant.info.id,
- compactBoundary: {
- lastSummarizedMessageId: copiedAssistant.info.id,
- preservedSegment: {
- headMessageId: copiedUser.info.id,
- anchorMessageId: copiedAssistant.info.id,
- tailMessageId: copiedAssistant.info.id,
- },
- summaryMessageIds: [copiedAssistant.info.id],
- turnId: copiedAnchor.turnId,
- },
- });
- expect(copiedCompactTimeline).toMatchObject({
- anchorMessageId: copiedAssistant.info.id,
- anchorTurnId: copiedAnchor.turnId,
- summaryMessageId: copiedAssistant.info.id,
- });
- expect(bundle!.goal?.source).toMatchObject({
- sessionID: childSessionId,
- activeInputId: null,
- activeRunStartedAtMs: null,
- activeRunLastSeenAtMs: null,
- });
- expect(bundle!.goal?.source.targetID).not.toBe(goal.targetID);
- expect(copiedEntry.id).not.toBe(verificationEntry.id);
- expect(copiedEntry.sessionID).toBe(childSessionId);
- expect(copiedEntryPayload).toMatchObject({
- targetId: bundle!.goal?.source.targetID,
- anchorAssistantMessageId: copiedAssistant.info.id,
- anchorTurnId: copiedAnchor.turnId,
- });
- expect(copiedEntryPayload.verificationId).not.toBe("verification_golden_parent");
- expect(copiedGoalTimeline).toMatchObject({
- targetId: bundle!.goal?.source.targetID,
- verificationId: copiedEntryPayload.verificationId,
- anchorMessageId: copiedAssistant.info.id,
- anchorTurnId: copiedAnchor.turnId,
- });
- expect(copiedTool).toMatchObject({
- sessionID: childSessionId,
- messageID: copiedAssistant.info.id,
- state: {
- attachments: [
- {
- sessionID: childSessionId,
- messageID: copiedAssistant.info.id,
- },
- ],
- },
- });
- if (copiedTool?.type === "tool") {
- expect(copiedTool.callID).not.toBe("tool_call_golden_parent");
- if (copiedTool.state.status === "completed") {
- expect(copiedTool.state.attachments?.[0]?.id).not.toBe(partId("golden_tool_attachment"));
- }
- }
- expect(copiedAnchor.goalBoundary).toMatchObject({
- kind: "snapshot",
- target: {
- sessionID: childSessionId,
- targetID: bundle!.goal?.source.targetID,
- },
- verificationEntryIds: [copiedEntry.id],
- });
- });
- it("fork child title uses generated source", async () => {
- const createdSessions: unknown[] = [];
- const runtime = {
- rootTraceContext: { traceId: "trace_fork_title" },
- sessionId: SESSION_ID,
- sessionStore: {
- createSession: async (input: unknown) => {
- createdSessions.push(input);
- },
- },
- };
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- path: "/workspace",
- projectID: "project_fork_title",
- slug: "parent-title",
- taskType: "interactive",
- time: { created: 1, updated: 1 },
- title: "执行下 pwd",
- version: "test",
- } as SessionInfo;
- await createForkedSession(runtime as never, {
- forkedSessionId: "sess_child_title" as SessionId,
- parentSession,
- });
- expect(createdSessions[0]).toMatchObject({
- id: "sess_child_title",
- title: "Fork of 执行下 pwd",
- titleSource: "generated",
- });
- });
- it("stable fork child 与 sourceCommandId/target metadata 通过原子 store 能力一起创建", async () => {
- const atomicCreates: Array<{
- input: { id: SessionId };
- metadata: StableConversationForkChildMetadata;
- }> = [];
- let legacyCreateCount = 0;
- const target: StableConversationForkTarget = {
- productTurnId: "product-1",
- transcriptTurnId: "runtime-1",
- orderedMessageIds: ["msg-user", "msg-final"],
- boundaryMessageId: "msg-final",
- };
- const runtime = {
- rootTraceContext: { traceId: "trace_atomic_fork" },
- sessionId: SESSION_ID,
- sessionStore: {
- async createSession() {
- legacyCreateCount += 1;
- },
- async createForkedSessionWithMetadata(
- input: { id: SessionId },
- metadata: StableConversationForkChildMetadata,
- ) {
- atomicCreates.push({ input, metadata });
- // 原子 store 发现同 command fact 时会返回第一次创建的 child。
- return { id: "sess_existing_child" as SessionId };
- },
- },
- };
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- path: "/workspace",
- projectID: "project_atomic_fork",
- slug: "parent",
- taskType: "interactive",
- time: { created: 1, updated: 1 },
- title: "Parent",
- version: "test",
- } as SessionInfo;
- const forkedSessionId = await createForkedSession(runtime as never, {
- forkedSessionId: "sess_requested_child" as SessionId,
- parentSession,
- stableForkMetadata: {
- forkTarget: target,
- parentSessionId: SESSION_ID,
- sourceCommandId: "command-fork-1",
- },
- });
- expect(forkedSessionId).toBe("sess_existing_child");
- expect(legacyCreateCount).toBe(0);
- expect(atomicCreates).toEqual([
- {
- input: expect.objectContaining({
- id: "sess_requested_child",
- parentID: SESSION_ID,
- }),
- metadata: {
- forkTarget: target,
- parentSessionId: SESSION_ID,
- sourceCommandId: "command-fork-1",
- },
- },
- ]);
- });
- it("stable fork store 缺少原子 metadata 能力时在 child 创建前失败", async () => {
- let legacyCreateCount = 0;
- const runtime = {
- rootTraceContext: { traceId: "trace_atomic_required" },
- sessionId: SESSION_ID,
- sessionStore: {
- async createSession() {
- legacyCreateCount += 1;
- },
- },
- };
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- projectID: "project_atomic_required",
- slug: "parent",
- taskType: "interactive",
- time: { created: 1, updated: 1 },
- title: "Parent",
- version: "test",
- } as SessionInfo;
- await expect(
- createForkedSession(runtime as never, {
- parentSession,
- stableForkMetadata: {
- forkTarget: {
- productTurnId: "product-1",
- transcriptTurnId: "runtime-1",
- orderedMessageIds: ["msg-final"],
- boundaryMessageId: "msg-final",
- },
- parentSessionId: SESSION_ID,
- sourceCommandId: "command-fork-1",
- },
- }),
- ).rejects.toThrow(/atomic child metadata persistence/);
- expect(legacyCreateCount).toBe(0);
- });
- it("stable fork 保留历史前缀并严格复制 resolver target segment,不向 boundary 后扩张", () => {
- const firstUser = userMessage("msg_user_1" as MessageId, "第一轮", 1);
- const firstAssistant = assistantMessage(
- "msg_assistant_1" as MessageId,
- firstUser.info.id,
- "第一轮回复",
- 2,
- );
- const targetUser = userMessage("msg_user_2" as MessageId, "目标轮", 3);
- const earlyAssistant = assistantMessage(
- "msg_assistant_2a" as MessageId,
- targetUser.info.id,
- "工具前",
- 4,
- );
- const finalAssistant = assistantMessage(
- "msg_assistant_2b" as MessageId,
- targetUser.info.id,
- "最终回复",
- 5,
- );
- const sameParentButNotResolved = assistantMessage(
- "msg_assistant_unresolved" as MessageId,
- targetUser.info.id,
- "不得自动扩张",
- 6,
- );
- const activeMessages = [
- firstUser,
- firstAssistant,
- targetUser,
- earlyAssistant,
- finalAssistant,
- sameParentButNotResolved,
- ];
- const selected = stableForkHistoryMessages(activeMessages, {
- productTurnId: "product-2",
- transcriptTurnId: "runtime-2",
- orderedMessageIds: ["msg_user_2", "msg_assistant_2a", "msg_assistant_2b"],
- boundaryMessageId: "msg_assistant_2b",
- });
- expect(selected.map((message) => message.info.id)).toEqual([
- "msg_user_1",
- "msg_assistant_1",
- "msg_user_2",
- "msg_assistant_2a",
- "msg_assistant_2b",
- ]);
- });
- it("stable fork 对缺失、乱序、重复或非 assistant boundary 明确拒绝", () => {
- const user = userMessage("msg_user" as MessageId, "目标轮", 1);
- const assistant = assistantMessage("msg_assistant" as MessageId, user.info.id, "回复", 2);
- const base = {
- productTurnId: "product-1",
- transcriptTurnId: "runtime-1",
- };
- expect(() =>
- stableForkHistoryMessages([user, assistant], {
- ...base,
- orderedMessageIds: ["msg_missing"],
- boundaryMessageId: "msg_missing",
- }),
- ).toThrow(/active transcript segment/);
- expect(() =>
- stableForkHistoryMessages([user, assistant], {
- ...base,
- orderedMessageIds: ["msg_assistant", "msg_user"],
- boundaryMessageId: "msg_user",
- }),
- ).toThrow(/contiguous active transcript segment/);
- expect(() =>
- stableForkHistoryMessages([user, assistant], {
- ...base,
- orderedMessageIds: ["msg_assistant", "msg_assistant"],
- boundaryMessageId: "msg_assistant",
- }),
- ).toThrow(/duplicate message ids/);
- expect(() =>
- stableForkHistoryMessages([user], {
- ...base,
- orderedMessageIds: ["msg_user"],
- boundaryMessageId: "msg_user",
- }),
- ).toThrow(/completed assistant message/);
- });
- it("compact-covered edit fork selects only the active prefix before target input, including root", () => {
- const firstUser = userMessage("msg_user_1" as MessageId, "first", 1);
- const firstAssistant = assistantMessage(
- "msg_assistant_1" as MessageId,
- firstUser.info.id,
- "answer",
- 2,
- );
- const editedInput = userMessage("msg_user_2" as MessageId, "edit me", 3);
- const oldAnswer = assistantMessage(
- "msg_assistant_2" as MessageId,
- editedInput.info.id,
- "old answer",
- 4,
- );
- expect(
- conversationHistoryBeforeInput(
- [firstUser, firstAssistant, editedInput, oldAnswer],
- editedInput.info.id,
- ).map((message) => message.info.id),
- ).toEqual([firstUser.info.id, firstAssistant.info.id]);
- expect(conversationHistoryBeforeInput([firstUser, firstAssistant], firstUser.info.id)).toEqual(
- [],
- );
- expect(() =>
- conversationHistoryBeforeInput([firstUser, firstAssistant], "missing" as MessageId),
- ).toThrow(/not found/);
- });
- it("bundle commit 是 point-of-no-return:parent fork event 失败不反转 accepted child", async () => {
- const target = userMessage("msg_first_user" as MessageId, "edit me", 1);
- const childSessionId = "sess_committed_child" as SessionId;
- let committedBundle: Parameters<NonNullable<SessionStorePort["commitForkBundle"]>>[0] | null =
- null;
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- path: "/workspace",
- projectID: "project-committed-child",
- slug: "parent-committed-child",
- taskType: "interactive",
- time: { created: 1, updated: 1 },
- title: "parent",
- version: "test",
- } as SessionInfo;
- const runtime = {
- appendEvent: async () => {
- throw new Error("parent event store unavailable");
- },
- config: { agentName: "zcode-agent", mode: "build" },
- createEvent: () => ({ type: "session.forked" }),
- getSessionModelSelection: () => ({
- modelId: "glm-4-air",
- providerId: "glm",
- }),
- logger: { warn: () => undefined },
- rootTraceContext: { traceId: "trace_committed_child" },
- sessionId: SESSION_ID,
- sessionStore: {
- async commitForkBundle(bundle) {
- committedBundle = bundle;
- return bundle.child as SessionInfo;
- },
- async getSession() {
- return parentSession;
- },
- async messages() {
- return [target];
- },
- } satisfies Partial<SessionStorePort>,
- workingDirectory: "/workspace",
- workspaceRoot: "/workspace",
- };
- await expect(
- forkConversationBeforeMessage.call(runtime as never, {
- commandFact: {
- parentSessionId: SESSION_ID,
- sourceCommandId: "cmd-committed-child",
- ack: {
- commandId: "cmd-committed-child",
- status: "accepted",
- revisionAtDecision: 1,
- result: {
- type: "editUserQuery",
- disposition: "fork",
- sessionId: childSessionId,
- },
- },
- metadata: {},
- },
- forkedSessionId: childSessionId,
- goalBoundary: { kind: "none" },
- initialInput: {
- id: "queue_cmd-committed-child",
- sessionID: childSessionId,
- kind: "sendText",
- delivery: "startNow",
- payload: { text: "edited" },
- },
- sourceCommandId: "cmd-committed-child",
- targetMessageId: target.info.id,
- targetProductTurnId: "turn-product",
- targetTranscriptTurnId: "turn-runtime",
- }),
- ).resolves.toMatchObject({ forkedSessionId: childSessionId });
- const notice = committedBundle!.messages.find((message) => message.info.role === "assistant");
- expect(notice?.info.role).toBe("assistant");
- if (notice?.info.role !== "assistant") return;
- expect(notice.info.parentID).not.toBe(notice.info.id);
- expect(
- committedBundle!.messages.some((message) => message.info.id === notice.info.parentID),
- ).toBe(true);
- });
- it("duplicate bundle 后 core 全程使用 store 返回的 existing child id", async () => {
- const target = userMessage("msg_duplicate_user" as MessageId, "edit me", 1);
- const requestedChildId = "sess_requested_duplicate" as SessionId;
- const existingChildId = "sess_existing_duplicate" as SessionId;
- const eventPayloads: Array<Record<string, unknown>> = [];
- const parentSession = {
- directory: "/workspace",
- id: SESSION_ID,
- path: "/workspace",
- projectID: "project-duplicate-child",
- slug: "parent-duplicate-child",
- taskType: "interactive",
- time: { created: 1, updated: 1 },
- title: "parent",
- version: "test",
- } as SessionInfo;
- const runtime = {
- appendEvent: async () => undefined,
- config: { agentName: "zcode-agent", mode: "build" },
- createEvent: (_type: unknown, payload: Record<string, unknown>) => {
- eventPayloads.push(payload);
- return { payload };
- },
- getSessionModelSelection: () => ({ modelId: "glm-4-air", providerId: "glm" }),
- rootTraceContext: { traceId: "trace_duplicate_child" },
- sessionId: SESSION_ID,
- sessionStore: {
- async commitForkBundle() {
- return { ...parentSession, id: existingChildId, parentID: SESSION_ID };
- },
- async getSession() {
- return parentSession;
- },
- async messages() {
- return [target];
- },
- } satisfies Partial<SessionStorePort>,
- workingDirectory: "/workspace",
- workspaceRoot: "/workspace",
- };
- const result = await forkConversationBeforeMessage.call(runtime as never, {
- commandFact: {
- parentSessionId: SESSION_ID,
- sourceCommandId: "cmd-duplicate-child",
- ack: {
- commandId: "cmd-duplicate-child",
- status: "accepted",
- revisionAtDecision: 1,
- result: {
- type: "editUserQuery",
- disposition: "fork",
- sessionId: requestedChildId,
- },
- },
- metadata: {},
- },
- forkedSessionId: requestedChildId,
- goalBoundary: { kind: "none" },
- initialInput: {
- id: "queue_cmd-duplicate-child",
- sessionID: requestedChildId,
- kind: "sendText",
- delivery: "startNow",
- payload: { text: "edited" },
- },
- sourceCommandId: "cmd-duplicate-child",
- targetMessageId: target.info.id,
- targetProductTurnId: "turn-product",
- targetTranscriptTurnId: "turn-runtime",
- });
- expect(result.forkedSessionId).toBe(existingChildId);
- expect(eventPayloads).toContainEqual(
- expect.objectContaining({ forkedSessionId: existingChildId }),
- );
- });
- it("fork transcript 不按 compact boundary 截掉可见 tool/worklog 历史", () => {
- const user = userMessage("msg_user" as MessageId, "真实用户输入", 1);
- const toolAssistant = assistantMessage(
- "msg_tool_assistant" as MessageId,
- user.info.id,
- "我先读取文件。",
- 2,
- );
- toolAssistant.parts.push({
- id: partId("tool_read"),
- sessionID: SESSION_ID,
- messageID: toolAssistant.info.id,
- type: "tool",
- callID: "toolu_read",
- tool: "Read",
- state: {
- status: "completed",
- input: { file_path: "/tmp/demo.txt" },
- output: "demo",
- title: "Read",
- },
- } as MessagePart);
- const compact = compactSummaryMessage("msg_compact" as MessageId, 3);
- const finalAssistant = assistantMessage("msg_final" as MessageId, user.info.id, "最终回复", 4);
- const parentMessages = [user, toolAssistant, compact, finalAssistant];
- expect(activeSessionMessages(parentMessages).map((message) => message.info.id)).toEqual([
- compact.info.id,
- finalAssistant.info.id,
- ]);
- const forkSourceMessages = activeForkTranscriptMessages(parentMessages);
- const targetIndex = forkSourceMessages.findIndex(
- (message) => message.info.id === finalAssistant.info.id,
- );
- const forkHistoryMessages = buildForkHistoryMessages(
- parentMessages,
- forkSourceMessages,
- targetIndex,
- targetIndex + 1,
- );
- expect(forkHistoryMessages.map((message) => message.info.id)).toEqual([
- user.info.id,
- toolAssistant.info.id,
- compact.info.id,
- finalAssistant.info.id,
- ]);
- expect(
- forkHistoryMessages
- .flatMap((message) => message.parts)
- .some((part) => part.type === "tool" && part.tool === "Read"),
- ).toBe(true);
- });
- it("fork transcript 保留 rewind active branch 但不按 compact 截断", () => {
- const firstUser = userMessage("msg_first_user" as MessageId, "第一轮", 1);
- const rewoundUser = userMessage("msg_rewound_user" as MessageId, "被编辑的旧轮次", 2);
- const rewoundAssistant = assistantMessage(
- "msg_rewound_assistant" as MessageId,
- rewoundUser.info.id,
- "旧回复",
- 3,
- );
- const editedUser = userMessage("msg_edited_user" as MessageId, "编辑后的新轮次", 4);
- const editedCompact = compactSummaryMessage("msg_edited_compact" as MessageId, 5);
- const editedAssistant = assistantMessage(
- "msg_edited_assistant" as MessageId,
- editedUser.info.id,
- "新回复",
- 6,
- );
- const forkSourceMessages = activeForkTranscriptMessages(
- [firstUser, rewoundUser, rewoundAssistant, editedUser, editedCompact, editedAssistant],
- {
- rewindCreatedMessageId: editedUser.info.id,
- rewindKeptMessageIds: [firstUser.info.id],
- rewindTargetMessageId: rewoundUser.info.id,
- },
- );
- expect(forkSourceMessages.map((message) => message.info.id)).toEqual([
- firstUser.info.id,
- editedUser.info.id,
- editedCompact.info.id,
- editedAssistant.info.id,
- ]);
- });
- it("fork goal state 只复制 child 历史里的 verifier 并 remap anchor", async () => {
- const forkedSessionId = "sess_child" as SessionId;
- const parentTarget: SessionGoal = {
- activeInputId: "input_parent",
- activeRunLastSeenAtMs: 950,
- activeRunStartedAtMs: 900,
- objective: "完成四个 checkpoint",
- sessionID: SESSION_ID,
- status: "complete",
- summaryTitle: "checkpoint goal",
- targetID: "target_goal_1",
- time: { created: 1, updated: 100 },
- timeUsedSeconds: 12,
- tokenBudget: 1000,
- tokensUsed: 120,
- };
- const parentEntries = [
- goalVerificationEntry("entry_1", "msg_checkpoint_1", 1, false),
- goalVerificationEntry("entry_2", "msg_checkpoint_2", 2, false),
- goalVerificationEntry("entry_3", "msg_checkpoint_3", 3, false),
- ];
- const savedEntries: SessionEntryInfo[] = [];
- let clonedTarget: SessionGoal | null = null;
- const store = {
- async readTarget(input: { sessionID: SessionId }) {
- return input.sessionID === SESSION_ID ? parentTarget : clonedTarget;
- },
- async sessionEntries() {
- return parentEntries;
- },
- async saveSessionEntry(input: SessionEntryInfo) {
- savedEntries.push(input);
- },
- async cloneTargetForFork(input: {
- sessionID: SessionId;
- source: SessionGoal;
- status?: SessionGoal["status"];
- }) {
- clonedTarget = {
- ...input.source,
- activeInputId: null,
- activeRunLastSeenAtMs: null,
- activeRunStartedAtMs: null,
- sessionID: input.sessionID,
- status: input.status ?? input.source.status,
- };
- return clonedTarget;
- },
- } as unknown as SessionStorePort;
- await copyGoalStateForFork.call(
- {
- logger: undefined,
- sessionId: SESSION_ID,
- sessionStore: store,
- } as never,
- {
- forkedSessionId,
- messageIdMap: new Map<MessageId, MessageId>([
- ["msg_checkpoint_1" as MessageId, "msg_child_checkpoint_1" as MessageId],
- ["msg_checkpoint_2" as MessageId, "msg_child_checkpoint_2" as MessageId],
- ]),
- traceContext: {} as TraceContext,
- },
- );
- expect(clonedTarget).toMatchObject({
- activeInputId: null,
- activeRunLastSeenAtMs: null,
- activeRunStartedAtMs: null,
- sessionID: forkedSessionId,
- status: "active",
- targetID: parentTarget.targetID,
- });
- expect(savedEntries.map((entry) => entry.sessionID)).toEqual([
- forkedSessionId,
- forkedSessionId,
- ]);
- expect(
- savedEntries.map(
- (entry) =>
- (entry.data as { payload: { anchorAssistantMessageId: string } }).payload
- .anchorAssistantMessageId,
- ),
- ).toEqual(["msg_child_checkpoint_1", "msg_child_checkpoint_2"]);
- });
- it("stable fork 只使用 resolver 给出的 goal 快照与 verifier entry 边界", async () => {
- const forkedSessionId = "sess_stable_goal_child" as SessionId;
- const currentParentTarget: SessionGoal = {
- activeInputId: "input_new",
- activeRunLastSeenAtMs: 200,
- activeRunStartedAtMs: 190,
- objective: "fork 点之后的新目标",
- sessionID: SESSION_ID,
- status: "active",
- targetID: "target_new",
- time: { created: 180, updated: 200 },
- timeUsedSeconds: 1,
- tokensUsed: 1,
- };
- const forkPointTarget: SessionGoal = {
- activeInputId: null,
- activeRunLastSeenAtMs: null,
- activeRunStartedAtMs: null,
- objective: "fork 点目标快照",
- sessionID: SESSION_ID,
- status: "complete",
- targetID: "target_at_fork",
- time: { created: 1, updated: 80 },
- timeUsedSeconds: 8,
- tokensUsed: 80,
- };
- const entries = [
- goalVerificationEntry("entry_before", "msg_checkpoint_1", 1, true),
- goalVerificationEntry("entry_after", "msg_checkpoint_2", 2, false),
- ];
- // helper 默认 target_goal_1;fork 点快照测试需要两个 entry 都属于显式 target。
- for (const entry of entries) {
- const data = entry.data as { payload: Record<string, unknown> };
- data.payload.targetId = "target_at_fork";
- }
- const savedEntries: SessionEntryInfo[] = [];
- let clonedTarget: SessionGoal | null = null;
- const store = {
- async readTarget() {
- return currentParentTarget;
- },
- async sessionEntries() {
- return entries;
- },
- async saveSessionEntry(input: SessionEntryInfo) {
- savedEntries.push(input);
- },
- async cloneTargetForFork(input: {
- sessionID: SessionId;
- source: SessionGoal;
- status?: SessionGoal["status"];
- }) {
- clonedTarget = {
- ...input.source,
- sessionID: input.sessionID,
- status: input.status ?? input.source.status,
- };
- return clonedTarget;
- },
- } as unknown as SessionStorePort;
- await copyGoalStateForFork.call(
- {
- logger: undefined,
- sessionId: SESSION_ID,
- sessionStore: store,
- } as never,
- {
- forkedSessionId,
- goalBoundary: {
- kind: "snapshot",
- target: forkPointTarget,
- verificationEntryIds: ["entry_before"],
- },
- messageIdMap: new Map<MessageId, MessageId>([
- ["msg_checkpoint_1" as MessageId, "msg_child_checkpoint_1" as MessageId],
- ]),
- traceContext: {} as TraceContext,
- },
- );
- expect(clonedTarget).toMatchObject({
- objective: "fork 点目标快照",
- sessionID: forkedSessionId,
- status: "complete",
- targetID: "target_at_fork",
- });
- expect(savedEntries).toHaveLength(1);
- expect(savedEntries[0]?.id).not.toBe("entry_after");
- });
- it("stable fork goal boundary=none 时不复制 parent 当前 target", async () => {
- let readTargetCount = 0;
- let cloneTargetCount = 0;
- const store = {
- async readTarget() {
- readTargetCount += 1;
- return {};
- },
- async cloneTargetForFork() {
- cloneTargetCount += 1;
- },
- } as unknown as SessionStorePort;
- await copyGoalStateForFork.call(
- {
- logger: undefined,
- sessionId: SESSION_ID,
- sessionStore: store,
- } as never,
- {
- forkedSessionId: "sess_no_goal_child" as SessionId,
- goalBoundary: { kind: "none" },
- messageIdMap: new Map(),
- traceContext: {} as TraceContext,
- },
- );
- expect(readTargetCount).toBe(0);
- expect(cloneTargetCount).toBe(0);
- });
- it("stable fork verifier boundary 引用缺失 entry 时明确失败", async () => {
- const target: SessionGoal = {
- activeInputId: null,
- activeRunLastSeenAtMs: null,
- activeRunStartedAtMs: null,
- objective: "目标",
- sessionID: SESSION_ID,
- status: "active",
- targetID: "target_at_fork",
- time: { created: 1, updated: 1 },
- timeUsedSeconds: 0,
- tokensUsed: 0,
- };
- const store = {
- async sessionEntries() {
- return [];
- },
- async saveSessionEntry() {},
- async cloneTargetForFork() {},
- } as unknown as SessionStorePort;
- await expect(
- copyGoalStateForFork.call(
- {
- logger: undefined,
- sessionId: SESSION_ID,
- sessionStore: store,
- } as never,
- {
- forkedSessionId: "sess_missing_goal_entry" as SessionId,
- goalBoundary: {
- kind: "snapshot",
- target,
- verificationEntryIds: ["missing-entry"],
- },
- messageIdMap: new Map(),
- traceContext: {} as TraceContext,
- },
- ),
- ).rejects.toThrow(/references missing entries/);
- });
- it("fork goal status 只在 copied verifier passed 后保持 complete", () => {
- expect(
- deriveForkedGoalStatusFromCopiedVerifications("complete", [
- {
- goalIteration: 4,
- status: "completed",
- targetId: "target_goal_1",
- verification: { nextAction: null, passed: true, reason: "done" },
- verificationId: "verify_4",
- },
- ]),
- ).toBe("complete");
- expect(
- deriveForkedGoalStatusFromCopiedVerifications("complete", [
- {
- goalIteration: 2,
- status: "completed",
- targetId: "target_goal_1",
- verification: { nextAction: "继续", passed: false, reason: "not yet" },
- verificationId: "verify_2",
- },
- ]),
- ).toBe("active");
- });
- it("补回 compact active branch 中被隐藏的 assistant parent user", () => {
- const user = userMessage("msg_user" as MessageId, "真实用户输入", 1);
- const compact = compactSummaryMessage("msg_compact" as MessageId, 2);
- const assistant = assistantMessage("msg_assistant" as MessageId, user.info.id, "最终回复", 3);
- const parentMessages = [user, compact, assistant];
- const forkSourceMessages = activeSessionMessages(parentMessages);
- expect(forkSourceMessages.map((message) => message.info.id)).toEqual([
- compact.info.id,
- assistant.info.id,
- ]);
- const forkHistoryMessages = buildForkHistoryMessages(parentMessages, forkSourceMessages, 1, 2);
- expect(forkHistoryMessages.map((message) => message.info.id)).toEqual([
- user.info.id,
- compact.info.id,
- assistant.info.id,
- ]);
- expect(activeSessionMessages(forkHistoryMessages).map((message) => message.info.id)).toEqual([
- compact.info.id,
- assistant.info.id,
- ]);
- });
- });
|