| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845 |
- // M5 冷恢复(打开历史会话)验收网:CLI 重启后内存注册表为空,v4 订阅历史会话
- // 必须经 host.resumePersistedSession 把 record 拉起来,而不是抛
- // "Session is not active"(真 bug:桌面重启后点侧栏历史会话永远"连接中")。
- //
- // 两层覆盖:
- // - gateway 单元(stub host):单飞幂等 / 完整事件日志 hydration 路径 /
- // notFound vs resumeFailed 错误分型 / 宿主未实现钩子的退化行为。
- // - 集成(真 sqlite sessionStore + ZCodeProtocolAgentServer + binder):
- // 写 transcript → 关库重开(模拟 CLI 重启,context.sessions 为空)→
- // v4 subscribe → snapshot 含历史行(transcript 合成路径)→ sendText 再发一轮成功;
- // restoreWarning 语义保真(workspace 无可用模型时 sendText 被拒);
- // store 无此会话 → fault.subscribe.sessionNotFound。
- import { mkdtemp, rm } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
- import { createSqliteSessionStore } from "@zcode/adapters/storage";
- import {
- zcodeProtocolMethods,
- zcodeSessionStateSnapshotSchema,
- type ZCodeProtocolMessage,
- } from "@zcode/shared";
- import {
- V4_METHODS,
- V4_NOTIFICATIONS,
- commandAckSchema,
- conversationTopic,
- conversationTopicFrameSchema,
- routedTopicWireFrameSchema,
- sessionsIndexTopic,
- sessionsIndexTopicFrameSchema,
- v4ConversationSubscribeResultSchema,
- v4ConversationRowsRangeResultSchema,
- v4SessionsIndexSubscribeResultSchema,
- type ConversationTopicFrame,
- } from "@zcode/shared/zcode-protocol-v4";
- import type {
- EventId,
- MessageId,
- MessagePart,
- MessageWithParts,
- ModelId,
- ModelProviderId,
- ProjectId,
- SessionEvent,
- SessionId,
- TraceId,
- WorkspaceId,
- } from "@zcode/contracts";
- import {
- CompactTrigger,
- SESSION_ENTRY_MODEL_SELECTION,
- SessionEventType,
- type CompactBoundaryPayload,
- } from "@zcode/contracts";
- import {
- ModelConfig,
- ModelOptionSpecsConfig,
- ModelPropertiesConfig,
- ProviderRegistry,
- type Provider,
- } from "@zcode/provider";
- import {
- ConversationV4Gateway,
- V4SubscribeSessionUnavailableError,
- type ColdSessionResumeOutcome,
- } from "../src/zcode-protocol-v4/index.js";
- import { resolveSessionContextUsage } from "../src/zcode-protocol/session-mapper.js";
- import { getRegistryBackedModel } from "../src/app/provider-registry-selection.js";
- import { ZCodeProtocolAgentServer } from "../src/zcode-protocol/server.js";
- import type { SessionUsageSeed } from "../src/zcode-protocol-v4/product-projection.js";
- import type { ZCodeApp, ZCodeAppOptions } from "../src/app/types.js";
- import { createApiKeyProviderConfig } from "./provider-config-fixtures.js";
- import { createRegistryBackedTestApp } from "./helpers/registry-backed-test-app.js";
- // ── gateway 单元层(stub host,模式对齐 v4-gateway.test.ts)──────────────────
- interface ColdHostStub {
- sessions: Set<string>;
- frames: ConversationTopicFrame[];
- errors: Array<{ scope: string; error: unknown }>;
- resumeCalls: string[];
- resumeThoughtLevels: Array<string | undefined>;
- resumeImpl: (sessionId: string) => Promise<ColdSessionResumeOutcome>;
- persistedEvents: Map<string, SessionEvent[]>;
- usageSeeds: Map<string, SessionUsageSeed>;
- synthesizedSessions: Set<string>;
- }
- function makeColdHost(): ColdHostStub {
- const host: ColdHostStub = {
- sessions: new Set(),
- frames: [],
- errors: [],
- resumeCalls: [],
- resumeThoughtLevels: [],
- // 默认打桩:恢复成功 = 会话进入内存注册表(对应 binder 的 resumeSession 建 record)。
- resumeImpl: async (sessionId) => {
- host.sessions.add(sessionId);
- return { status: "resumed" };
- },
- persistedEvents: new Map(),
- usageSeeds: new Map(),
- synthesizedSessions: new Set(),
- };
- return host;
- }
- function coldResumeHook(host: ColdHostStub) {
- return async (sessionId: string, resumeThoughtLevel?: string) => {
- host.resumeCalls.push(sessionId);
- host.resumeThoughtLevels.push(resumeThoughtLevel);
- return await host.resumeImpl(sessionId);
- };
- }
- function makeColdGateway(
- host: ColdHostStub,
- options: { withResumeHook?: boolean; now?: () => number } = {},
- ): ConversationV4Gateway {
- const withResumeHook = options.withResumeHook ?? true;
- return new ConversationV4Gateway(
- {
- sessionExists: (sessionId) => host.sessions.has(sessionId),
- emitWireFrame: (wire) => {
- if (wire.kind !== "complete" || !wire.topic.startsWith("conversation/")) {
- throw new Error("cold-resume test adapter only accepts complete conversation wires");
- }
- host.frames.push(wire.frame as ConversationTopicFrame);
- },
- executeCommand: async () => undefined,
- ...(withResumeHook
- ? {
- resumePersistedSession: coldResumeHook(host),
- }
- : {}),
- loadPersistedEvents: async (sessionId) => ({
- events: host.persistedEvents.get(sessionId) ?? [],
- synthesized: host.synthesizedSessions.has(sessionId),
- }),
- getSessionUsageSeed: (sessionId) => host.usageSeeds.get(sessionId) ?? null,
- onError: (scope, error) => host.errors.push({ scope, error }),
- },
- {
- now: options.now ?? (() => 1_700_000_999_000),
- createLogEpoch: () => "epoch-cold",
- },
- );
- }
- function makeEvent(
- sessionId: string,
- seq: number,
- type: SessionEvent["type"],
- payload: unknown,
- ): SessionEvent {
- return {
- id: `event-${seq}` as EventId,
- sessionId: sessionId as SessionId,
- turnId: undefined,
- type,
- timestamp: new Date(1_700_000_000_000 + seq * 1000),
- traceId: "trace-cold" as TraceId,
- sequenceNumber: seq,
- payload,
- } as SessionEvent;
- }
- function subscribeParams(sessionId: string) {
- return {
- topic: conversationTopic(sessionId),
- connectionId: "conn-cold",
- clientMode: "desktop-continuous",
- };
- }
- function deferred() {
- let resolve!: () => void;
- const promise = new Promise<void>((complete) => {
- resolve = complete;
- });
- return { promise, resolve };
- }
- function nextMacrotask(): Promise<void> {
- return new Promise((resolve) => setImmediate(resolve));
- }
- describe("v4 冷恢复(gateway 单元)", () => {
- it("冷订阅未加载会话:单一 READY 完成 activation 与 hydration", async () => {
- const host = makeColdHost();
- host.persistedEvents.set("sess-cold", [
- makeEvent("sess-cold", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- ]);
- const gateway = makeColdGateway(host);
- const result = await gateway.subscribe(subscribeParams("sess-cold"));
- expect(host.resumeCalls).toEqual(["sess-cold"]);
- expect(host.sessions.has("sess-cold")).toBe(true);
- expect(result.ack.mode).toBe("snapshot");
- expect(result.ack.openTiming).toMatchObject({
- version: 1,
- sessionRuntimeState: "cold",
- cliSessionRestoreMs: expect.any(Number),
- initialFrameEncodeMs: expect.any(Number),
- snapshotRowCount: expect.any(Number),
- });
- expect(result.initialFrame?.payload.kind).toBe("snapshot");
- });
- it("Todo123: 同批容量种子不二次查询,恢复期间新模型和用量覆盖旧种子", async () => {
- const entered = deferred();
- const release = deferred();
- const getSessionUsageSeed = vi.fn(() => null);
- const sessionId = "sess-usage-race";
- const gateway = new ConversationV4Gateway({
- sessionExists: () => true,
- emitWireFrame: () => {},
- executeCommand: async () => undefined,
- getSessionUsageSeed,
- loadPersistedEvents: async () => {
- entered.resolve();
- await release.promise;
- return {
- synthesized: true,
- sourceEventSeq: 1,
- events: [
- makeEvent(sessionId, 1, SessionEventType.SessionCreated, { contextWindow: 1_000_000 }),
- ],
- usageSeed: {
- contextWindow: {
- usedTokens: 29908,
- maxTokens: 1_000_000,
- autoCompactThresholdTokens: null,
- },
- },
- };
- },
- });
- try {
- const pending = gateway.subscribe(subscribeParams(sessionId));
- await entered.promise;
- gateway.ingest(
- sessionId,
- makeEvent(sessionId, 2, SessionEventType.ModelSelected, {
- modelSelection: { providerId: "new", modelId: "new-model" },
- contextWindow: 500_000,
- }),
- );
- gateway.ingest(
- sessionId,
- makeEvent(sessionId, 3, SessionEventType.ModelComplete, {
- content: "",
- stopReason: "end_turn",
- querySource: "main_turn",
- usage: { inputTokens: 700, outputTokens: 30 },
- }),
- );
- release.resolve();
- const result = await pending;
- if (result.initialFrame?.payload.kind !== "snapshot") throw new Error("Expected snapshot");
- expect(result.initialFrame.payload.snapshot.config.model).toBe("new-model");
- expect(result.initialFrame.payload.snapshot.usage.contextWindow).toMatchObject({
- usedTokens: 730,
- maxTokens: 500_000,
- });
- expect(getSessionUsageSeed).not.toHaveBeenCalled();
- } finally {
- release.resolve();
- gateway.dispose();
- }
- });
- it("首帧编码耗时包含 wire projection 构建", async () => {
- const sessionId = "sess-live-initial-frame-timing";
- const host = makeColdHost();
- host.sessions.add(sessionId);
- let monotonicTime = 0;
- let simulateWireProjectionBuild = false;
- const gateway = makeColdGateway(host, {
- now: () => {
- // publisher 构建 initial frame 时会写 sentAt;借该公开时钟缝隙推进 monotonic clock,
- // 可确定性验证构建耗时是否包含在 initialFrameEncodeMs,而不依赖真实 CPU 速度。
- if (simulateWireProjectionBuild) monotonicTime += 25;
- return 1_700_000_999_000;
- },
- });
- gateway.ingest(
- sessionId,
- makeEvent(sessionId, 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- const nowSpy = vi.spyOn(performance, "now").mockImplementation(() => monotonicTime);
- simulateWireProjectionBuild = true;
- try {
- const result = await gateway.subscribe(subscribeParams(sessionId));
- expect(result.ack.openTiming?.initialFrameEncodeMs).toBe(25);
- } finally {
- nowSpy.mockRestore();
- gateway.dispose();
- }
- });
- it("冷订阅只把 task-local thought hint 交给首次 resume", async () => {
- const host = makeColdHost();
- const gateway = makeColdGateway(host);
- await gateway.subscribe({
- ...subscribeParams("sess-cold-thought"),
- resumeThoughtLevel: "deep",
- });
- expect(host.resumeThoughtLevels).toEqual(["deep"]);
- });
- it("桌面与手机并发冷订阅同一会话:activation 单飞且首个终态一致", async () => {
- const host = makeColdHost();
- const messages: MessageWithParts[] = [];
- const loadedPersistedMessages: unknown[] = [];
- const usagePersistedMessages: unknown[] = [];
- const resumeEntered = deferred();
- const resumeGate = deferred();
- host.resumeImpl = async (sessionId) => {
- // production 在 app.resume() 前先注册 record;第二个订阅此时会观察到 live,
- // 但仍必须加入 activation flight,不能用空 materialization 抢跑 hydration。
- host.sessions.add(sessionId);
- resumeEntered.resolve();
- await resumeGate.promise;
- return { status: "resumed", persistedMessages: messages };
- };
- const gateway = new ConversationV4Gateway(
- {
- sessionExists: (sessionId) => host.sessions.has(sessionId),
- resumePersistedSession: coldResumeHook(host),
- loadPersistedEvents: async (_sessionId, persistedMessages) => {
- loadedPersistedMessages.push(persistedMessages);
- return { events: [], synthesized: true };
- },
- getSessionUsageSeed: (_sessionId, persistedMessages) => {
- usagePersistedMessages.push(persistedMessages);
- return null;
- },
- emitWireFrame: () => {},
- executeCommand: async () => undefined,
- },
- { now: () => 1_700_000_999_000, createLogEpoch: () => "epoch-cold" },
- );
- const first = gateway.subscribe(subscribeParams("sess-cold"));
- const second = gateway.subscribe({
- ...subscribeParams("sess-cold"),
- clientMode: "web-remote-replayable",
- connectionId: "conn-cold-2",
- });
- // 等真实恢复进入 barrier 后再放行,避免依赖内部 Promise 链的 microtask 层数。
- await resumeEntered.promise;
- resumeGate.resolve();
- const [a, b] = await Promise.all([first, second]);
- expect(host.resumeCalls).toEqual(["sess-cold"]);
- expect(loadedPersistedMessages).toEqual([messages]);
- expect(usagePersistedMessages).toEqual([messages]);
- if (a.initialFrame?.payload.kind !== "snapshot") {
- throw new Error("Expected desktop snapshot frame");
- }
- if (b.initialFrame?.payload.kind !== "snapshot") {
- throw new Error("Expected mobile snapshot frame");
- }
- expect(a.ack.subscriptionId).not.toBe(b.ack.subscriptionId);
- expect(a.initialFrame.payload.snapshot).toEqual(b.initialFrame.payload.snapshot);
- });
- it("cold READY 同时阻塞 command/query,完成后各只进入一次既有流程", async () => {
- const host = makeColdHost();
- const resumeStarted = deferred();
- const resumeGate = deferred();
- const hydrationStarted = deferred();
- const hydrationGate = deferred();
- const queryCommandId = "cmd-query-cold-ready";
- let discardedReady = false;
- host.resumeImpl = async (sessionId) => {
- // 复现 production:record 先入册,遗留 admitted input 在 resume 收尾时才变成 discarded。
- host.sessions.add(sessionId);
- resumeStarted.resolve();
- await resumeGate.promise;
- discardedReady = true;
- return { status: "resumed", persistedMessages: [] };
- };
- const admitCommandInput = vi.fn(async () => null);
- const executeCommand = vi.fn(async () => undefined);
- const invalidatePersistentCommandFacts = vi.fn();
- const lookupDiscardedCommand = vi.fn(async (key: { commandId: string }) =>
- discardedReady && key.commandId === queryCommandId
- ? {
- commandId: queryCommandId,
- status: "failed" as const,
- reasonCode: "fault.command.inputDiscardedOnRestart",
- revisionAtDecision: 0,
- }
- : null,
- );
- const gateway = new ConversationV4Gateway(
- {
- sessionExists: (sessionId) => host.sessions.has(sessionId),
- resumePersistedSession: coldResumeHook(host),
- loadPersistedEvents: async () => {
- hydrationStarted.resolve();
- await hydrationGate.promise;
- return { events: [], synthesized: true };
- },
- admitCommandInput,
- lookupDiscardedCommand,
- invalidatePersistentCommandFacts,
- emitWireFrame: () => {},
- executeCommand,
- },
- { now: () => 1_700_000_999_000, createLogEpoch: () => "epoch-cold" },
- );
- const sessionId = "sess-command-ready";
- const subscribing = gateway.subscribe(subscribeParams(sessionId));
- await resumeStarted.promise;
- let commandSettled = false;
- let querySettled = false;
- const commanding = gateway
- .handleCommand({
- commandId: "cmd-command-ready",
- clientId: "client-command-ready",
- sessionId,
- type: "sendText",
- payload: { text: "继续" },
- issuedAt: 1_700_000_999_000,
- })
- .then((ack) => {
- commandSettled = true;
- return ack;
- });
- const querying = gateway
- .queryCommands({ commands: [{ sessionId, commandId: queryCommandId }] })
- .then((result) => {
- querySettled = true;
- return result;
- });
- try {
- await nextMacrotask();
- expect(commandSettled).toBe(false);
- expect(querySettled).toBe(false);
- expect(admitCommandInput).not.toHaveBeenCalled();
- expect(lookupDiscardedCommand).not.toHaveBeenCalled();
- expect(executeCommand).not.toHaveBeenCalled();
- resumeGate.resolve();
- await hydrationStarted.promise;
- expect(invalidatePersistentCommandFacts).not.toHaveBeenCalled();
- await nextMacrotask();
- expect(commandSettled).toBe(false);
- expect(querySettled).toBe(false);
- expect(admitCommandInput).not.toHaveBeenCalled();
- expect(lookupDiscardedCommand).not.toHaveBeenCalled();
- expect(executeCommand).not.toHaveBeenCalled();
- hydrationGate.resolve();
- const [subscription, ack, query] = await Promise.all([subscribing, commanding, querying]);
- expect(subscription.initialFrame?.payload.kind).toBe("snapshot");
- expect(ack.status).toBe("accepted");
- expect(query.results).toEqual([
- {
- key: { sessionId, commandId: queryCommandId },
- result: {
- commandId: queryCommandId,
- status: "failed",
- reasonCode: "fault.command.inputDiscardedOnRestart",
- revisionAtDecision: 0,
- },
- },
- ]);
- expect(admitCommandInput).toHaveBeenCalledTimes(1);
- expect(executeCommand).toHaveBeenCalledTimes(1);
- expect(
- lookupDiscardedCommand.mock.calls.filter(([key]) => key.commandId === queryCommandId),
- ).toHaveLength(1);
- } finally {
- resumeGate.resolve();
- hydrationGate.resolve();
- await Promise.allSettled([subscribing, commanding, querying]);
- gateway.dispose();
- }
- });
- it("既有 live session 的首次 hydration 不引入 cold READY command 等待", async () => {
- const host = makeColdHost();
- host.sessions.add("sess-live-hydration");
- const hydrationStarted = deferred();
- const hydrationGate = deferred();
- const executeCommand = vi.fn(async () => undefined);
- const gateway = new ConversationV4Gateway({
- sessionExists: (sessionId) => host.sessions.has(sessionId),
- loadPersistedEvents: async () => {
- hydrationStarted.resolve();
- await hydrationGate.promise;
- return { events: [], synthesized: false };
- },
- emitWireFrame: () => {},
- executeCommand,
- });
- const subscribing = gateway.subscribe(subscribeParams("sess-live-hydration"));
- await hydrationStarted.promise;
- const commanding = gateway.handleCommand({
- commandId: "cmd-live-hydration",
- clientId: "client-live-hydration",
- sessionId: "sess-live-hydration",
- type: "sendText",
- payload: { text: "继续" },
- issuedAt: 1_700_000_999_000,
- });
- try {
- await nextMacrotask();
- expect(executeCommand).toHaveBeenCalledTimes(1);
- await expect(commanding).resolves.toMatchObject({ status: "accepted" });
- hydrationGate.resolve();
- await subscribing;
- } finally {
- hydrationGate.resolve();
- await Promise.allSettled([subscribing, commanding]);
- gateway.dispose();
- }
- });
- it("没有 cold READY 时 command 只由 inbox 解析一次", async () => {
- const executeCommand = vi.fn(async () => undefined);
- const gateway = new ConversationV4Gateway({
- sessionExists: () => true,
- emitWireFrame: () => {},
- executeCommand,
- });
- let sessionIdReads = 0;
- const command = {
- commandId: "cmd-without-cold-ready",
- clientId: "client-without-cold-ready",
- get sessionId() {
- sessionIdReads += 1;
- return "sess-without-cold-ready";
- },
- type: "sendText",
- payload: { text: "继续" },
- issuedAt: 1_700_000_999_000,
- };
- try {
- await expect(gateway.handleCommand(command)).resolves.toMatchObject({ status: "accepted" });
- expect(executeCommand).toHaveBeenCalledTimes(1);
- expect(sessionIdReads).toBe(1);
- } finally {
- gateway.dispose();
- }
- });
- it("record 已入册但冷恢复失败时:command 保留 lifecycle error,且绝不 admission", async () => {
- const host = makeColdHost();
- const resumeStarted = deferred();
- const resumeGate = deferred();
- const resumeError = new Error("resume storage failed");
- host.resumeImpl = async (sessionId) => {
- host.sessions.add(sessionId);
- resumeStarted.resolve();
- await resumeGate.promise;
- throw resumeError;
- };
- const admitCommandInput = vi.fn(async () => null);
- const executeCommand = vi.fn(async () => undefined);
- const gateway = new ConversationV4Gateway({
- sessionExists: (sessionId) => host.sessions.has(sessionId),
- resumePersistedSession: coldResumeHook(host),
- loadPersistedEvents: async () => ({ events: [], synthesized: true }),
- admitCommandInput,
- emitWireFrame: () => {},
- executeCommand,
- });
- const subscribing = gateway.subscribe(subscribeParams("sess-command-resume-failed"));
- await resumeStarted.promise;
- const commanding = gateway.handleCommand({
- commandId: "cmd-command-resume-failed",
- clientId: "client-command-resume-failed",
- sessionId: "sess-command-resume-failed",
- type: "sendText",
- payload: { text: "继续" },
- issuedAt: 1_700_000_999_000,
- });
- try {
- resumeGate.resolve();
- const [subscribeFailure, commandFailure] = await Promise.all([
- subscribing.catch((error: unknown) => error),
- commanding.catch((error: unknown) => error),
- ]);
- expect(subscribeFailure).toBeInstanceOf(V4SubscribeSessionUnavailableError);
- expect(commandFailure).toBe(subscribeFailure);
- expect((commandFailure as V4SubscribeSessionUnavailableError).reasonCode).toBe(
- "fault.subscribe.resumeFailed",
- );
- expect(host.sessions.has("sess-command-resume-failed")).toBe(true);
- expect(admitCommandInput).not.toHaveBeenCalled();
- expect(executeCommand).not.toHaveBeenCalled();
- } finally {
- resumeGate.resolve();
- await Promise.allSettled([subscribing, commanding]);
- gateway.dispose();
- }
- });
- it("store 里也没有:fault.subscribe.sessionNotFound(结构化 reasonCode)", async () => {
- const host = makeColdHost();
- host.resumeImpl = async () => ({ status: "notFound" });
- const gateway = makeColdGateway(host);
- const failure = await gateway
- .subscribe(subscribeParams("sess-ghost"))
- .then(() => null)
- .catch((error: unknown) => error);
- expect(failure).toBeInstanceOf(V4SubscribeSessionUnavailableError);
- const typed = failure as V4SubscribeSessionUnavailableError;
- expect(typed.reasonCode).toBe("fault.subscribe.sessionNotFound");
- // renderer 订阅错误块直显 message → reasonCode 必须随文透出。
- expect(typed.message).toContain("fault.subscribe.sessionNotFound");
- });
- it("record 入册前恢复失败:保留原始 cause,并释放 rejected READY entry", async () => {
- const host = makeColdHost();
- host.resumeImpl = async () => {
- // 只代表 production 中 record 入册前的失败;入册后的 lifecycle 恢复不属于 READY 的职责。
- throw new Error("sqlite is on fire");
- };
- const gateway = makeColdGateway(host);
- const failure = await gateway
- .subscribe(subscribeParams("sess-cold"))
- .then(() => null)
- .catch((error: unknown) => error);
- expect(failure).toBeInstanceOf(V4SubscribeSessionUnavailableError);
- const typed = failure as V4SubscribeSessionUnavailableError;
- expect(typed.reasonCode).toBe("fault.subscribe.resumeFailed");
- expect(typed.message).toContain("sqlite is on fire");
- expect((typed.cause as Error).message).toBe("sqlite is on fire");
- expect(host.errors.some((entry) => entry.scope === "v4.subscribe.resume")).toBe(true);
- expect(host.resumeCalls).toEqual(["sess-cold"]);
- // 这里只证明 rejected READY promise 没被缓存;不宣称 record 入册后的 activation 可自动重试。
- host.resumeImpl = async (sessionId) => {
- host.sessions.add(sessionId);
- return { status: "resumed" };
- };
- const retried = await gateway.subscribe(subscribeParams("sess-cold"));
- expect(retried.initialFrame?.payload.kind).toBe("snapshot");
- expect(host.resumeCalls).toEqual(["sess-cold", "sess-cold"]);
- });
- it("宿主未实现钩子(旧宿主/测试桩):维持 not active 拒绝且带 sessionNotFound", async () => {
- const host = makeColdHost();
- const gateway = makeColdGateway(host, { withResumeHook: false });
- await expect(gateway.subscribe(subscribeParams("sess-cold"))).rejects.toThrow(
- /not active.*fault\.subscribe\.sessionNotFound/,
- );
- });
- it("transcript 合成冷恢复:用 runtime usage seed 覆盖 0/20万占位 context meter", async () => {
- const host = makeColdHost();
- host.synthesizedSessions.add("sess-cold");
- host.persistedEvents.set("sess-cold", [
- makeEvent("sess-cold", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- makeEvent("sess-cold", 2, SessionEventType.ModelComplete, {
- content: "",
- contextWindow: 200_000,
- querySource: "main_turn",
- stopReason: "end_turn",
- usage: {
- inputTokens: 0,
- outputTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 0,
- },
- }),
- ]);
- host.usageSeeds.set("sess-cold", {
- contextWindow: {
- usedTokens: 16_727,
- maxTokens: 1_000_000,
- autoCompactThresholdTokens: null,
- cache: {
- inputTokens: 16_638,
- cacheReadTokens: 1408,
- cacheWriteTokens: 0,
- latestHitRate: 0.08462555595624474,
- hitRate: 0.08462555595624474,
- hitRateRequestCount: 1,
- totalInputTokens: 16_638,
- totalCacheReadTokens: 1408,
- totalCacheWriteTokens: 0,
- },
- breakdown: [
- { source: "system_prompt", chars: 3236 },
- { source: "system_tool_schemas", chars: 57_600 },
- ],
- },
- cumulative: {
- inputTokens: 16_638,
- outputTokens: 89,
- cacheReadTokens: 1408,
- cacheWriteTokens: 0,
- },
- });
- const gateway = makeColdGateway(host);
- const result = await gateway.subscribe(subscribeParams("sess-cold"));
- if (result.initialFrame?.payload.kind !== "snapshot") {
- throw new Error("Expected snapshot initial frame");
- }
- expect(result.initialFrame.payload.snapshot.usage.contextWindow).toMatchObject({
- usedTokens: 16_727,
- maxTokens: 1_000_000,
- cache: { hitRate: 0.08462555595624474 },
- breakdown: [
- { source: "system_prompt", chars: 3236 },
- { source: "system_tool_schemas", chars: 57_600 },
- ],
- });
- });
- });
- // ── 集成层(真 sqlite store + ZCodeProtocolAgentServer + binder)─────────────
- const WORKSPACE_PATH = "/workspace/cold-resume";
- const workspace = { workspacePath: WORKSPACE_PATH, workspaceKey: WORKSPACE_PATH };
- const REMOTE_WORKSPACE_IDENTITY =
- "remote:ssh:example.test:22:coder:/workspace/cold-resume" as WorkspaceId;
- const remoteWorkspace = {
- workspaceIdentity: REMOTE_WORKSPACE_IDENTITY,
- workspaceKey: REMOTE_WORKSPACE_IDENTITY,
- workspacePath: WORKSPACE_PATH,
- };
- type TestSessionStore = ReturnType<typeof createSqliteSessionStore>;
- async function requestResult(server: ZCodeProtocolAgentServer, message: ZCodeProtocolMessage) {
- const response = await server.handleMessage(message);
- if (!response || !("result" in response)) {
- throw new Error(`Expected protocol success response: ${JSON.stringify(response)}`);
- }
- return response.result;
- }
- function takeInitialFrameParams(
- server: ZCodeProtocolAgentServer,
- requestId: string | number,
- ): unknown {
- const messages = server.takePostResponseMessages(requestId);
- expect(messages).toHaveLength(1);
- expect(messages[0]).toMatchObject({ method: V4_NOTIFICATIONS.conversationFrame });
- const wire = routedTopicWireFrameSchema.parse((messages[0] as { params: unknown }).params);
- if (wire.kind !== "complete") {
- throw new Error("cold-resume initial-frame adapter expected one complete physical wire");
- }
- return wire.frame;
- }
- async function saveUserTextMessage(
- store: TestSessionStore,
- sessionId: SessionId,
- messageId: MessageId,
- text: string,
- created: number,
- ) {
- await store.saveMessage({
- id: messageId,
- sessionID: sessionId,
- role: "user",
- time: { created },
- agent: "zcode-agent",
- model: {
- providerID: "glm" as ModelProviderId,
- modelID: "glm-4-air" as ModelId,
- },
- tools: {},
- });
- await store.savePart({
- id: `${messageId}_text` as MessagePart["id"],
- sessionID: sessionId,
- messageID: messageId,
- type: "text",
- text,
- time: { start: created, end: created },
- });
- }
- async function saveAssistantTextMessage(
- store: TestSessionStore,
- sessionId: SessionId,
- messageId: MessageId,
- parentMessageId: MessageId,
- text: string,
- created: number,
- ) {
- await store.saveMessage({
- id: messageId,
- sessionID: sessionId,
- role: "assistant",
- time: { created, completed: created },
- parentID: parentMessageId,
- providerID: "glm" as ModelProviderId,
- modelID: "glm-4-air" as ModelId,
- mode: "build",
- agent: "zcode-agent",
- path: { cwd: WORKSPACE_PATH, root: WORKSPACE_PATH },
- cost: 0,
- tokens: { input: 1, output: 1, reasoning: 0, cache: { read: 0, write: 0 } },
- });
- await store.savePart({
- id: `${messageId}_text` as MessagePart["id"],
- sessionID: sessionId,
- messageID: messageId,
- type: "text",
- text,
- time: { start: created, end: created },
- });
- }
- /** 冷恢复集成用最小 fake app(子集自 zcode-protocol.test.ts createFakeApp)。 */
- interface ColdFakeAppProbe {
- persistedMessagesReloadRequired?: boolean;
- readTodosCalls: number;
- resumeCalls: number;
- resumeEventTypes?: SessionEvent["type"][];
- resumePersistedMessagesProvided?: boolean[];
- }
- function createColdFakeApp(
- options: ZCodeAppOptions | undefined,
- sentInputs: Array<{ text: string }>,
- probe?: ColdFakeAppProbe,
- ): ZCodeApp {
- const sessionId = options?.sessionId ?? ("sess_cold" as SessionId);
- const traceId = options?.traceContext?.traceId ?? ("trace_cold" as never);
- let runtimeSink: { onSessionEvent(event: SessionEvent): Promise<void> | void } | undefined;
- let mode = options?.runtimeConfig?.mode ?? "build";
- let model = options?.runtimeConfig?.modelSelection
- ? `${options.runtimeConfig.modelSelection.providerId}/${options.runtimeConfig.modelSelection.modelId}`
- : "glm/glm-4.6";
- let thoughtLevel = options?.runtimeConfig?.modelSelection?.options?.reasoningLevel ?? "medium";
- const providerRegistry = options?.providerRegistry;
- const initialProviderId = options?.runtimeConfig?.modelSelection?.providerId ?? "glm";
- const initialModelId = options?.runtimeConfig?.modelSelection?.modelId ?? "glm-4.6";
- let runtimeContextWindow =
- options?.runtimeConfig?.contextWindow ??
- providerRegistry?.getModel(initialProviderId, initialModelId)?.config.properties
- ?.contextWindow ??
- 128_000;
- const modelProperties = {
- inputFormat: {
- supportsText: true,
- supportsImage: false,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: false,
- },
- outputFormat: { supportsText: true },
- } as const;
- const baseModels = [
- {
- ref: { providerId: "glm", modelId: "glm-4.6" },
- label: "GLM 4.6",
- providerLabel: "glm",
- properties: modelProperties,
- },
- {
- ref: { providerId: "glm", modelId: "glm-4-air" },
- label: "GLM 4 Air",
- providerLabel: "glm",
- properties: modelProperties,
- },
- ];
- let models = [...baseModels];
- return {
- clearTarget: async () => false,
- close: async () => {},
- connectMcpServer: async () => {
- throw new Error("MCP is not available in fake app");
- },
- continueActiveTarget: async () => null,
- disconnectMcpServer: async () => undefined,
- forkFromCheckpoint: async () => {
- throw new Error("Fork is not available in fake app");
- },
- getDefaultThoughtLevel: () => "medium",
- getLocale: () => "en-US",
- getMode: () => mode,
- getModel: () => model,
- getTheme: () => "auto",
- getThoughtLevel: () => thoughtLevel,
- generateWorkspaceText: async () => {
- throw new Error("Workspace text generation is not available in fake app");
- },
- listCheckpoints: async () => [],
- listMcpServers: async () => ({}),
- listModels: () => models,
- getModelOption: (selection) =>
- providerRegistry && getRegistryBackedModel(providerRegistry, selection),
- listPlugins: async () => ({ loaded: [], failed: [] }) as never,
- listThoughtLevels: () => ["medium", "deep"],
- loadSessionTranscript: async () => [],
- readToolResultArtifact: async (uri) => ({
- bytes: 0,
- content: "",
- contentType: "text/plain",
- uri,
- }),
- readTodos: async () => {
- if (probe) probe.readTodosCalls += 1;
- return [];
- },
- readTarget: async () => null,
- recallPreviousInputHistory: async () => null,
- recordInputHistory: async () => null,
- resume: async (resumeOptions) => {
- if (probe) probe.resumeCalls += 1;
- probe?.resumePersistedMessagesProvided?.push(resumeOptions?.persistedMessages !== undefined);
- for (const [index, type] of (probe?.resumeEventTypes ?? []).entries()) {
- const payload =
- type === SessionEventType.WorkspaceHookAdmissionUpdated
- ? {
- pendingCount: 0,
- bundleDigest: "cold-resume-hooks",
- }
- : {
- hookEventName: "SessionStart",
- hookIndex: 0,
- hookRunId: `cold-resume-hook-${index + 1}`,
- };
- await runtimeSink?.onSessionEvent(makeEvent(String(sessionId), index + 1, type, payload));
- }
- return {
- directory: options?.runtimeConfig?.workingDirectory ?? WORKSPACE_PATH,
- interruptedToolCount: 0,
- messageCount: 0,
- partCount: 0,
- persistedMessagesReloadRequired: probe?.persistedMessagesReloadRequired ?? false,
- restoredMessages: [],
- traceId,
- };
- },
- runtime: {
- // TTL pool 在 resident 未超水位时也要读取权威 idle facts;冷恢复 fake 必须和
- // 生产 runtime 一样显式声明没有前台或后台工作,不能再依赖旧容量分支短路。
- hasActiveOrQueuedTurnWork: () => false,
- hasRunningBackgroundTasks: () => false,
- hasResidencyBlockingWork: () => false,
- // 合并后 sendText 的 afterLegacyStateMutation 会经 auto-drain 读前台执行位;
- // 与上面同一条论证:fake 必须显式声明没有前台工作,而不是让属性缺席变成 TypeError。
- getActiveForegroundExecutionId: () => undefined,
- getSessionModelSelection: () => {
- const separator = model.indexOf("/");
- return {
- providerId: model.slice(0, separator),
- modelId: model.slice(separator + 1),
- ...(thoughtLevel ? { options: { reasoningLevel: thoughtLevel } } : {}),
- };
- },
- getProjection: async () =>
- ({
- activeToolCalls: [],
- backgroundTasks: [],
- contextUsed: 0,
- contextWindow: runtimeContextWindow,
- createdAt: new Date(1),
- id: sessionId,
- mode,
- pendingPermissions: [],
- pendingSteerInputs: [],
- status: "idle",
- streamingToolLedger: [],
- target: null,
- totalTokenCount: 0,
- turnCount: 0,
- updatedAt: new Date(1),
- }) as never,
- getActiveTurnInfo: () => undefined,
- initializeSessionShellEnvironmentIfNeeded: () => false,
- subscribeEvents: (sink) => {
- runtimeSink = sink;
- return () => {};
- },
- updateConfig: (patch) => {
- if ("contextWindow" in patch) {
- runtimeContextWindow = patch.contextWindow ?? 0;
- }
- },
- } as never,
- sendInput: async (input, sendOptions) => {
- const text = (input as { text: string }).text;
- sentInputs.push({ text });
- const turnStarted = {
- ...makeEvent(String(sessionId), 0, SessionEventType.TurnStarted, {
- input: text,
- inputId: sendOptions?.inputId,
- turnNumber: 3,
- }),
- turnId: "turn-cold-send",
- } as SessionEvent;
- await runtimeSink?.onSessionEvent(turnStarted);
- sendOptions?.onTurnStartedObserved?.(turnStarted);
- return { kind: "accepted" } as never;
- },
- sessionId,
- setLocale: async (locale) => ({
- configPath: "/tmp/zcode/config.json",
- locale: locale === "zh-CN" ? "zh-CN" : "en-US",
- previousLocale: "en-US",
- requestedLocale: locale,
- traceId,
- }),
- setMode: async (nextMode) => {
- const previousMode = mode;
- mode = nextMode;
- return { mode, previousMode, traceId };
- },
- setPluginEnabled: async () => {
- throw new Error("Plugins are not available in fake app");
- },
- setTarget: async () => {
- throw new Error("Targets are not available in fake app");
- },
- setModel: async (nextModel) => {
- if (
- !models.some((option) => `${option.ref.providerId}/${option.ref.modelId}` === nextModel)
- ) {
- throw new Error(`Unsupported model in fake app: ${nextModel}`);
- }
- const previousModel = model;
- model = nextModel;
- return { model, previousModel, traceId };
- },
- setThoughtLevel: async (level) => {
- const previousThoughtLevel = thoughtLevel;
- thoughtLevel = level;
- return { previousThoughtLevel, thoughtLevel, traceId };
- },
- steerTurn: async () => ({ kind: "rejected", reason: "no_active_turn" }),
- submitPrompt: async () => {
- throw new Error("Prompt execution is not available in fake app");
- },
- traceId,
- updateTargetStatus: async () => null,
- } as ZCodeApp;
- }
- const SESSION_ID = "sess_cold_resume" as SessionId;
- const PARENT_SESSION_ID = "sess_cold_parent" as SessionId;
- function createColdProviderRegistry(): ProviderRegistry {
- const modelConfig = new ModelConfig({
- properties: new ModelPropertiesConfig({
- requiresMfjsToolSchema: false,
- contextWindow: 1_000_000,
- inputFormat: {
- supportsText: true,
- supportsImage: true,
- supportsVideo: true,
- supportsAudio: false,
- supportsPdf: true,
- },
- outputFormat: { supportsText: true },
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: true,
- }),
- optionSpecs: new ModelOptionSpecsConfig({
- reasoningLevel: {
- values: ["medium", "deep"],
- map: '{"reasoning_effort":reasoningLevel}',
- },
- maxOutputTokens: {
- max: 32_000,
- map: '{"max_completion_tokens":maxOutputTokens}',
- },
- }),
- });
- const provider: Provider = {
- providerId: "glm",
- config: createApiKeyProviderConfig({
- apiFormat: "openai-chat-completions",
- apiKey: "sk-test",
- baseURL: "https://api.example.test/v1",
- models: ["glm-4-air", "glm-4.6"],
- }),
- models: [
- { modelId: "glm-4-air", config: modelConfig },
- { modelId: "glm-4.6", config: modelConfig },
- ],
- };
- return new ProviderRegistry([provider]);
- }
- describe("v4 冷恢复(集成:真 sqlite + server + binder)", () => {
- let tempRoot: string;
- beforeEach(async () => {
- tempRoot = await mkdtemp(join(tmpdir(), "zcode-v4-cold-resume-"));
- });
- afterEach(async () => {
- await rm(tempRoot, { force: true, recursive: true });
- });
- /** 第一段人生:往 store 写入一段 user/assistant transcript 后关库(模拟 CLI 退出)。 */
- async function seedPersistedSession(
- dbPath: string,
- workspaceIdentity?: WorkspaceId,
- taskType: "interactive" | "fork" | "subagent_child" = "interactive",
- ): Promise<void> {
- const store = createSqliteSessionStore({ dbPath });
- try {
- await store.createSession({
- id: SESSION_ID,
- parentID: PARENT_SESSION_ID,
- taskType,
- projectID: "project_cold_resume" as ProjectId,
- ...(workspaceIdentity ? { workspaceID: workspaceIdentity } : {}),
- slug: "cold-resume-session",
- directory: WORKSPACE_PATH,
- path: WORKSPACE_PATH,
- title: "Cold resume session",
- version: "test-version",
- time: { created: 1, updated: 4 },
- });
- await saveUserTextMessage(store, SESSION_ID, "msg_cold_user_1" as MessageId, "历史问题一", 1);
- await saveAssistantTextMessage(
- store,
- SESSION_ID,
- "msg_cold_assistant_1" as MessageId,
- "msg_cold_user_1" as MessageId,
- "历史回答一",
- 2,
- );
- await saveUserTextMessage(store, SESSION_ID, "msg_cold_user_2" as MessageId, "历史问题二", 3);
- await saveAssistantTextMessage(
- store,
- SESSION_ID,
- "msg_cold_assistant_2" as MessageId,
- "msg_cold_user_2" as MessageId,
- "历史回答二",
- 4,
- );
- await store.savePart({
- id: "msg_cold_assistant_2_reasoning" as MessagePart["id"],
- sessionID: SESSION_ID,
- messageID: "msg_cold_assistant_2" as MessageId,
- type: "reasoning",
- text: "历史思考二",
- time: { start: 4, end: 4 },
- });
- await store.savePart({
- id: "msg_cold_assistant_2_tool" as MessagePart["id"],
- sessionID: SESSION_ID,
- messageID: "msg_cold_assistant_2" as MessageId,
- type: "tool",
- callID: "call_cold_read",
- tool: "Read",
- state: {
- status: "completed",
- input: { file_path: "README.md" },
- output: "历史工具输出",
- title: "Read README.md",
- metadata: {},
- time: { start: 4, end: 4 },
- },
- });
- } finally {
- store.close();
- }
- }
- async function seedPersistedCompactedSession(dbPath: string): Promise<void> {
- await seedPersistedSession(dbPath);
- const store = createSqliteSessionStore({ dbPath });
- const summaryMessageId = "msg_cold_compact_summary" as MessageId;
- const compactBoundary: CompactBoundaryPayload = {
- boundaryId: "boundary_cold_compact",
- trigger: CompactTrigger.Manual,
- preCompactTokenCount: 2,
- postCompactTokenCount: 2,
- truePostCompactTokenCount: 1,
- summarizedMessageCount: 4,
- summaryMessageIds: [summaryMessageId],
- traceId: "trace_cold_compact" as TraceId,
- };
- try {
- await store.saveMessage({
- id: summaryMessageId,
- sessionID: SESSION_ID,
- role: "user",
- time: { created: 5 },
- summary: { title: "Compact summary", body: "压缩后的摘要", diffs: [] },
- agent: "zcode-agent",
- model: {
- providerID: "glm" as ModelProviderId,
- modelID: "glm-4-air" as ModelId,
- },
- tools: {},
- });
- await store.savePart({
- id: "part_cold_compact_boundary" as MessagePart["id"],
- sessionID: SESSION_ID,
- messageID: summaryMessageId,
- type: "compaction",
- auto: false,
- compactBoundary,
- });
- } finally {
- store.close();
- }
- }
- /** 第二段人生:同一 db 文件重开 → 新 server(内存注册表为空 = CLI 重启)。 */
- function bootRestartedServer(
- dbPath: string,
- sentInputs: Array<{ text: string }>,
- probe?: ColdFakeAppProbe,
- createdAppOptions: ZCodeAppOptions[] = [],
- ) {
- const store = createSqliteSessionStore({ dbPath });
- const providerRegistry = createColdProviderRegistry();
- const server = new ZCodeProtocolAgentServer({
- createZCodeApp: (options) => {
- const effectiveOptions = { ...options, providerRegistry };
- createdAppOptions.push(effectiveOptions);
- return createColdFakeApp(effectiveOptions, sentInputs, probe);
- },
- cwd: WORKSPACE_PATH,
- sessionStore: store,
- });
- return { server, store };
- }
- // 模型恢复须走真实 App,不能让生命周期测试替身复制生产迁移逻辑。
- function bootRealRestartedServer(dbPath: string, registry = createColdProviderRegistry()) {
- const store = createSqliteSessionStore({ dbPath });
- const apps: ZCodeApp[] = [];
- const server = new ZCodeProtocolAgentServer({
- cwd: process.cwd(),
- sessionStore: store,
- createZCodeApp: async (options) => {
- const app = await createRegistryBackedTestApp({
- ...options,
- env: {},
- skipUserConfig: true,
- providerRegistry: registry,
- });
- apps.push(app);
- return app;
- },
- });
- server.setNotificationSink((message) => {
- if (!("id" in message)) return;
- if (message.method === zcodeProtocolMethods.sessionRequestRuntimePreferences) {
- void server.handleMessage({
- id: message.id,
- result: {
- nativeSearchEnhancementsEnabled: false,
- memoryEnabled: false,
- },
- });
- }
- });
- return {
- server,
- apps,
- close: async () => {
- for (const app of apps) await app.close();
- store.close();
- },
- };
- }
- it.each(["desktop-continuous", "web-remote-replayable"] as const)(
- "Todo123: %s 缺档位旧选择仍按 Registry 展示 1M,执行选择不被补齐",
- async (clientMode) => {
- const dbPath = join(tempRoot, "missing-reasoning.db");
- await seedPersistedSession(dbPath);
- const store = createSqliteSessionStore({ dbPath });
- const selection = { providerId: "glm", modelId: "glm-4-air" };
- await store.saveSessionEntry({
- id: "entry_missing_reasoning",
- sessionID: SESSION_ID,
- type: SESSION_ENTRY_MODEL_SELECTION,
- data: selection,
- time: { created: 5, updated: 5 },
- });
- store.close();
- const { server, apps, close } = bootRealRestartedServer(dbPath);
- try {
- await requestResult(server, {
- id: "missing-reasoning",
- method: V4_METHODS.conversationSubscribe,
- params: { topic: conversationTopic(SESSION_ID), connectionId: clientMode, clientMode },
- });
- const frame = conversationTopicFrameSchema.parse(
- takeInitialFrameParams(server, "missing-reasoning"),
- );
- if (frame.payload.kind !== "snapshot") throw new Error("Expected snapshot");
- expect(frame.payload.snapshot.config.modelSelection).toEqual(selection);
- expect(frame.payload.snapshot.usage.contextWindow).toMatchObject({
- usedTokens: 2,
- maxTokens: 1_000_000,
- });
- expect(apps[0].runtime.getSessionModelSelection()).toBeUndefined();
- expect(
- frame.payload.snapshot.rows.window.some(
- (row) => row.kind === "assistantText" && row.text === "历史回答二",
- ),
- ).toBe(true);
- } finally {
- await close();
- }
- },
- );
- it("Todo123: 无对应 Registry 模型仍可冷读内容,容量未知而非 20 万", async () => {
- const dbPath = join(tempRoot, "unknown-model.db");
- await seedPersistedSession(dbPath);
- const { server, close } = bootRealRestartedServer(dbPath, new ProviderRegistry([]));
- try {
- await requestResult(server, {
- id: "unknown-model",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "unknown",
- clientMode: "desktop-continuous",
- },
- });
- const frame = conversationTopicFrameSchema.parse(
- takeInitialFrameParams(server, "unknown-model"),
- );
- if (frame.payload.kind !== "snapshot") throw new Error("Expected snapshot");
- expect(frame.payload.snapshot.usage.contextWindow).toBeNull();
- expect(
- frame.payload.snapshot.rows.window.some(
- (row) => row.kind === "assistantText" && row.text === "历史回答二",
- ),
- ).toBe(true);
- } finally {
- await close();
- }
- });
- it("rowsRange-first 与 subscribe 并发时只按 Host 当前设置冷恢复一次", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const createdAppOptions: ZCodeAppOptions[] = [];
- const { server, store } = bootRestartedServer(dbPath, [], undefined, createdAppOptions);
- let pendingPreferenceRequestId: string | number | undefined;
- let markPreferenceRequested: (() => void) | undefined;
- const preferenceRequested = new Promise<void>((resolve) => {
- markPreferenceRequested = resolve;
- });
- const preferenceRequests: ZCodeProtocolMessage[] = [];
- server.setNotificationSink((message) => {
- if (
- "id" in message &&
- message.method === zcodeProtocolMethods.sessionRequestRuntimePreferences
- ) {
- preferenceRequests.push(message);
- pendingPreferenceRequestId = message.id;
- markPreferenceRequested?.();
- }
- });
- try {
- const rowsPromise = requestResult(server, {
- id: 2,
- method: V4_METHODS.conversationRowsRange,
- params: { sessionId: SESSION_ID, limit: 20 },
- });
- await preferenceRequested;
- const subscribePromise = requestResult(server, {
- id: 3,
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-query-first",
- clientMode: "desktop-continuous",
- },
- });
- await server.handleMessage({
- id: pendingPreferenceRequestId!,
- result: {
- nativeSearchEnhancementsEnabled: false,
- memoryEnabled: false,
- },
- });
- const [rows] = await Promise.all([rowsPromise, subscribePromise]);
- expect(v4ConversationRowsRangeResultSchema.parse(rows).rows).not.toHaveLength(0);
- expect(preferenceRequests).toHaveLength(1);
- expect(preferenceRequests[0]).toMatchObject({
- params: { sessionId: SESSION_ID, scope: "runtime-materialization" },
- });
- const resumedRuntimeOptions = createdAppOptions.filter(
- (options) => options.sessionId === SESSION_ID,
- );
- expect(resumedRuntimeOptions).toHaveLength(1);
- expect(resumedRuntimeOptions[0]?.runtimeConfig?.nativeSearchEnhancementsEnabled).toBe(false);
- expect(resumedRuntimeOptions[0]?.runtimeConfig?.memory?.enabled).toBe(false);
- } finally {
- store.close();
- }
- });
- it("冷恢复 3.4.2 遗留的 WSL 显式用户任务时修复真实工作目录", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- const legacySessionId = "sess_wsl_explicit_user_legacy" as SessionId;
- const unrelatedSessionId = "sess_wsl_explicit_user_unrelated" as SessionId;
- const workspacePath = "/home/dev/coding/DEMO-ERP-NEW";
- const workspaceIdentity =
- "remote:wsl:Ubuntu-24.04:dev:/home/dev/coding/DEMO-ERP-NEW" as WorkspaceId;
- const unrelatedWorkspaceIdentity =
- "remote:wsl:Ubuntu-24.04:other:/home/dev/coding/DEMO-ERP-NEW" as WorkspaceId;
- const seedStore = createSqliteSessionStore({ dbPath });
- try {
- await seedStore.createSession({
- id: legacySessionId,
- projectID: "project_wsl_explicit_user_legacy" as ProjectId,
- slug: "wsl-explicit-user-legacy",
- directory: workspaceIdentity,
- path: workspaceIdentity,
- title: "WSL legacy task",
- version: "3.4.2",
- time: { created: 1, updated: 2 },
- });
- await seedStore.createSession({
- id: unrelatedSessionId,
- projectID: "project_wsl_explicit_user_unrelated" as ProjectId,
- slug: "wsl-explicit-user-unrelated",
- directory: unrelatedWorkspaceIdentity,
- path: unrelatedWorkspaceIdentity,
- title: "Unrelated WSL legacy task",
- version: "3.4.2",
- time: { created: 3, updated: 4 },
- });
- } finally {
- seedStore.close();
- }
- const store = createSqliteSessionStore({ dbPath });
- const resumedWorkingDirectories: string[] = [];
- const server = new ZCodeProtocolAgentServer({
- createZCodeApp: (options) => {
- resumedWorkingDirectories.push(options?.runtimeConfig?.workingDirectory ?? "");
- return createColdFakeApp(options, []);
- },
- cwd: workspacePath,
- sessionStore: store,
- });
- try {
- await requestResult(server, {
- id: "wsl-legacy-resume",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(legacySessionId),
- connectionId: "conn-wsl-legacy-resume",
- clientMode: "desktop-continuous",
- },
- });
- expect(resumedWorkingDirectories).toEqual([workspacePath]);
- await expect(store.getSession(legacySessionId)).resolves.toMatchObject({
- directory: workspacePath,
- path: workspacePath,
- workspaceID: workspaceIdentity,
- });
- await expect(store.getSession(unrelatedSessionId)).resolves.toMatchObject({
- directory: unrelatedWorkspaceIdentity,
- path: unrelatedWorkspaceIdentity,
- workspaceID: undefined,
- });
- } finally {
- store.close();
- }
- });
- it("冷恢复被 path.resolve 拼接过 remote identity 的 3.4.2 WSL 任务", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- const legacySessionId = "sess_wsl_resolved_identity_legacy" as SessionId;
- const workspacePath = "/home/dev/coding/DEMO-ERP-NEW";
- const workspaceIdentity =
- "remote:wsl:Ubuntu-24.04:dev:/home/dev/coding/DEMO-ERP-NEW" as WorkspaceId;
- const resolvedLegacyDirectory = `${workspacePath}/${workspaceIdentity}`;
- const seedStore = createSqliteSessionStore({ dbPath });
- try {
- await seedStore.createSession({
- id: legacySessionId,
- projectID: "project_wsl_resolved_identity_legacy" as ProjectId,
- slug: "wsl-resolved-identity-legacy",
- directory: resolvedLegacyDirectory,
- path: resolvedLegacyDirectory,
- title: "WSL resolved identity legacy task",
- version: "3.4.2",
- time: { created: 1, updated: 2 },
- });
- } finally {
- seedStore.close();
- }
- const store = createSqliteSessionStore({ dbPath });
- const resumedWorkingDirectories: string[] = [];
- const server = new ZCodeProtocolAgentServer({
- createZCodeApp: (options) => {
- resumedWorkingDirectories.push(options?.runtimeConfig?.workingDirectory ?? "");
- return createColdFakeApp(options, []);
- },
- cwd: workspacePath,
- sessionStore: store,
- });
- try {
- await requestResult(server, {
- id: "wsl-resolved-legacy-resume",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(legacySessionId),
- connectionId: "conn-wsl-resolved-legacy-resume",
- clientMode: "desktop-continuous",
- },
- });
- expect(resumedWorkingDirectories).toEqual([workspacePath]);
- await expect(store.getSession(legacySessionId)).resolves.toMatchObject({
- directory: workspacePath,
- path: workspacePath,
- workspaceID: workspaceIdentity,
- });
- } finally {
- store.close();
- }
- });
- it("NULL identity 原子修复未命中且重读仍污染时禁止物化 runtime", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- const sessionId = "sess_wsl_failed_atomic_repair" as SessionId;
- const workspacePath = "/home/dev/project";
- const workspaceIdentity = "remote:wsl:Debian:dev:/home/dev/project" as WorkspaceId;
- const store = createSqliteSessionStore({ dbPath });
- await store.createSession({
- id: sessionId,
- projectID: "project_wsl_failed_atomic_repair" as ProjectId,
- slug: "wsl-failed-atomic-repair",
- directory: workspaceIdentity,
- path: workspaceIdentity,
- title: "WSL failed atomic repair",
- version: "3.4.2",
- time: { created: 1, updated: 2 },
- });
- store.repairLegacyRemoteSessionWorkspace = vi.fn(async () => false);
- const resumedWorkingDirectories: string[] = [];
- const server = new ZCodeProtocolAgentServer({
- createZCodeApp: (options) => {
- resumedWorkingDirectories.push(options?.runtimeConfig?.workingDirectory ?? "");
- return createColdFakeApp(options, []);
- },
- cwd: workspacePath,
- sessionStore: store,
- });
- try {
- const response = await server.handleMessage({
- id: "wsl-failed-atomic-repair",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(sessionId),
- connectionId: "conn-wsl-failed-atomic-repair",
- clientMode: "desktop-continuous",
- },
- });
- expect(response).toMatchObject({
- error: { message: expect.stringContaining("workspace repair") },
- });
- expect(resumedWorkingDirectories).toEqual([]);
- await expect(store.getSession(sessionId)).resolves.toMatchObject({
- directory: workspaceIdentity,
- path: workspaceIdentity,
- workspaceID: undefined,
- });
- } finally {
- store.close();
- }
- });
- it("NULL identity 原子修复返回未命中但并发请求已完整修复时继续恢复", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- const sessionId = "sess_wsl_concurrent_atomic_repair" as SessionId;
- const workspacePath = "/home/dev/project";
- const workspaceIdentity = "remote:wsl:Debian:dev:/home/dev/project" as WorkspaceId;
- const store = createSqliteSessionStore({ dbPath });
- await store.createSession({
- id: sessionId,
- projectID: "project_wsl_concurrent_atomic_repair" as ProjectId,
- slug: "wsl-concurrent-atomic-repair",
- directory: workspaceIdentity,
- path: workspaceIdentity,
- title: "WSL concurrent atomic repair",
- version: "3.4.2",
- time: { created: 1, updated: 2 },
- });
- const persistRepair = store.repairLegacyRemoteSessionWorkspace.bind(store);
- store.repairLegacyRemoteSessionWorkspace = vi.fn(async (input) => {
- await persistRepair(input);
- return false;
- });
- const resumedWorkingDirectories: string[] = [];
- const server = new ZCodeProtocolAgentServer({
- createZCodeApp: (options) => {
- resumedWorkingDirectories.push(options?.runtimeConfig?.workingDirectory ?? "");
- return createColdFakeApp(options, []);
- },
- cwd: workspacePath,
- sessionStore: store,
- });
- try {
- await requestResult(server, {
- id: "wsl-concurrent-atomic-repair",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(sessionId),
- connectionId: "conn-wsl-concurrent-atomic-repair",
- clientMode: "desktop-continuous",
- },
- });
- expect(resumedWorkingDirectories).toEqual([workspacePath]);
- await expect(store.getSession(sessionId)).resolves.toMatchObject({
- directory: workspacePath,
- path: workspacePath,
- workspaceID: workspaceIdentity,
- });
- } finally {
- store.close();
- }
- });
- it("SSH 冷订阅 sessions-index:只迁移 task-index allowlist 证明归属的 3.3.6 历史会话", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- const seedStore = createSqliteSessionStore({ dbPath });
- const currentSessionId = "sess_current_remote_index" as SessionId;
- const foreignSessionId = "sess_foreign_remote_index" as SessionId;
- const unprovenLegacySessionId = "sess_unproven_legacy_remote_index" as SessionId;
- const remoteWorkspaceIdentity = "remote:ssh:example.test:22:coder:/workspace/cold-resume";
- const foreignWorkspaceIdentity =
- "remote:ssh:other.example.test:22:coder:/workspace/cold-resume";
- try {
- await seedStore.createSession({
- id: SESSION_ID,
- projectID: "project_remote_index" as ProjectId,
- slug: "remote-index-session",
- directory: WORKSPACE_PATH,
- path: WORKSPACE_PATH,
- title: "Remote index session",
- version: "test-version",
- time: { created: 1, updated: 2 },
- });
- await seedStore.createSession({
- id: currentSessionId,
- projectID: "project_current_remote_index" as ProjectId,
- workspaceID: remoteWorkspaceIdentity as WorkspaceId,
- slug: "current-remote-index-session",
- directory: WORKSPACE_PATH,
- path: WORKSPACE_PATH,
- title: "Current remote index session",
- version: "test-version",
- time: { created: 3, updated: 4 },
- });
- await seedStore.createSession({
- id: unprovenLegacySessionId,
- projectID: "project_unproven_legacy_remote_index" as ProjectId,
- slug: "unproven-legacy-remote-index-session",
- directory: WORKSPACE_PATH,
- path: WORKSPACE_PATH,
- title: "Unproven legacy remote index session",
- version: "test-version",
- time: { created: 4, updated: 5 },
- });
- await seedStore.createSession({
- id: foreignSessionId,
- projectID: "project_foreign_remote_index" as ProjectId,
- workspaceID: foreignWorkspaceIdentity as WorkspaceId,
- slug: "foreign-remote-index-session",
- directory: WORKSPACE_PATH,
- path: WORKSPACE_PATH,
- title: "Foreign remote index session",
- version: "test-version",
- time: { created: 5, updated: 6 },
- });
- } finally {
- seedStore.close();
- }
- const { server, store } = bootRestartedServer(dbPath, []);
- try {
- await requestResult(server, {
- id: "remote-index",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: sessionsIndexTopic(remoteWorkspaceIdentity),
- connectionId: "conn-remote-index",
- clientMode: "desktop-continuous",
- legacyTaskIds: [SESSION_ID],
- },
- });
- const frame = sessionsIndexTopicFrameSchema.parse(
- takeInitialFrameParams(server, "remote-index"),
- );
- if (frame.payload.kind !== "snapshot") {
- throw new Error("Expected sessions-index snapshot frame");
- }
- expect(frame.payload.snapshot.workspaceId).toBe(remoteWorkspaceIdentity);
- expect(frame.payload.snapshot.sessions).toEqual([
- expect.objectContaining({
- sessionId: currentSessionId,
- workspaceId: remoteWorkspaceIdentity,
- title: "Current remote index session",
- }),
- expect.objectContaining({
- sessionId: SESSION_ID,
- workspaceId: remoteWorkspaceIdentity,
- title: "Remote index session",
- }),
- ]);
- await expect(store.getSession(SESSION_ID)).resolves.toMatchObject({
- workspaceID: remoteWorkspaceIdentity,
- });
- expect((await store.getSession(unprovenLegacySessionId))?.workspaceID).toBeUndefined();
- await expect(store.getSession(foreignSessionId)).resolves.toMatchObject({
- workspaceID: foreignWorkspaceIdentity,
- });
- // 未被当前 identity 的 task index 证明归属的空 identity 会话不得迁移;
- // 同一数据库里的本地路径订阅仍只能看到这条未归属记录。
- await requestResult(server, {
- id: "local-index-after-remote",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: sessionsIndexTopic(WORKSPACE_PATH),
- connectionId: "conn-local-index-after-remote",
- clientMode: "desktop-continuous",
- },
- });
- const localFrame = sessionsIndexTopicFrameSchema.parse(
- takeInitialFrameParams(server, "local-index-after-remote"),
- );
- if (localFrame.payload.kind !== "snapshot") {
- throw new Error("Expected local sessions-index snapshot frame");
- }
- expect(localFrame.payload.snapshot.sessions).toEqual([
- expect.objectContaining({ sessionId: unprovenLegacySessionId }),
- ]);
- } finally {
- store.close();
- }
- });
- it("本地路径冷订阅 sessions-index:保持原目录查询语义", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- const seedStore = createSqliteSessionStore({ dbPath });
- const remoteSessionId = "sess_remote_same_path" as SessionId;
- try {
- await seedStore.createSession({
- id: SESSION_ID,
- projectID: "project_local_index" as ProjectId,
- slug: "local-index-session",
- directory: WORKSPACE_PATH,
- path: WORKSPACE_PATH,
- title: "Local index session",
- version: "test-version",
- time: { created: 1, updated: 2 },
- });
- await seedStore.createSession({
- id: remoteSessionId,
- projectID: "project_remote_same_path" as ProjectId,
- workspaceID: "remote:ssh:example.test:22:coder:/workspace/cold-resume" as WorkspaceId,
- slug: "remote-same-path-session",
- directory: WORKSPACE_PATH,
- path: WORKSPACE_PATH,
- title: "Remote same path session",
- version: "test-version",
- time: { created: 3, updated: 4 },
- });
- } finally {
- seedStore.close();
- }
- const { server, store } = bootRestartedServer(dbPath, []);
- try {
- await requestResult(server, {
- id: "local-index",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: sessionsIndexTopic(WORKSPACE_PATH),
- connectionId: "conn-local-index",
- clientMode: "desktop-continuous",
- // 防御旧/混合版本客户端误传:本地 topic 必须完全忽略远端迁移 allowlist。
- legacyTaskIds: [SESSION_ID],
- },
- });
- const frame = sessionsIndexTopicFrameSchema.parse(
- takeInitialFrameParams(server, "local-index"),
- );
- if (frame.payload.kind !== "snapshot") {
- throw new Error("Expected sessions-index snapshot frame");
- }
- expect(frame.payload.snapshot.workspaceId).toBe(WORKSPACE_PATH);
- expect(frame.payload.snapshot.sessions).toEqual([
- expect.objectContaining({
- sessionId: SESSION_ID,
- workspaceId: WORKSPACE_PATH,
- title: "Local index session",
- }),
- ]);
- expect((await store.getSession(SESSION_ID))?.workspaceID).toBeUndefined();
- } finally {
- store.close();
- }
- });
- it("重启后首次订阅 sessions-index:parented fork 可见且辅助 child 保持隐藏", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- const seedStore = createSqliteSessionStore({ dbPath });
- const rootSessionId = "sess_index_root" as SessionId;
- const forkSessionId = "sess_index_fork" as SessionId;
- const workflowParentSessionId = "sess_index_workflow_parent" as SessionId;
- try {
- const fixtures = [
- { id: rootSessionId, taskType: "interactive" as const },
- {
- id: forkSessionId,
- parentID: rootSessionId,
- taskType: "fork" as const,
- },
- {
- id: workflowParentSessionId,
- taskType: "workflow_parent" as const,
- },
- {
- id: "sess_index_selection_side_chat" as SessionId,
- parentID: rootSessionId,
- taskType: "selection_side_chat" as const,
- },
- {
- id: "sess_index_subagent_child" as SessionId,
- parentID: rootSessionId,
- taskType: "subagent_child" as const,
- },
- {
- id: "sess_index_workflow_child" as SessionId,
- parentID: workflowParentSessionId,
- taskType: "workflow_child" as const,
- },
- {
- id: "sess_index_nested_workflow_child" as SessionId,
- parentID: workflowParentSessionId,
- taskType: "nested_workflow_child" as const,
- },
- ];
- for (const [index, fixture] of fixtures.entries()) {
- await seedStore.createSession({
- ...fixture,
- projectID: `project_index_membership_${index}` as ProjectId,
- slug: `index-membership-${index}`,
- directory: WORKSPACE_PATH,
- path: WORKSPACE_PATH,
- title: `Index membership ${index}`,
- version: "test-version",
- time: { created: index + 1, updated: index + 1 },
- });
- }
- } finally {
- seedStore.close();
- }
- const { server, store } = bootRestartedServer(dbPath, []);
- try {
- // 不先 resume 任一 child,直接模拟 App 重启后的首次列表订阅。
- await requestResult(server, {
- id: "cold-index-membership",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: sessionsIndexTopic(WORKSPACE_PATH),
- connectionId: "conn-cold-index-membership",
- clientMode: "desktop-continuous",
- },
- });
- const frame = sessionsIndexTopicFrameSchema.parse(
- takeInitialFrameParams(server, "cold-index-membership"),
- );
- if (frame.payload.kind !== "snapshot") {
- throw new Error("Expected sessions-index snapshot frame");
- }
- expect(frame.payload.snapshot.sessions.map((session) => session.sessionId).sort()).toEqual(
- [rootSessionId, forkSessionId, workflowParentSessionId].sort(),
- );
- expect(
- frame.payload.snapshot.sessions.find((session) => session.sessionId === forkSessionId),
- ).toMatchObject({
- parentSessionId: rootSessionId,
- sessionId: forkSessionId,
- });
- } finally {
- store.close();
- }
- });
- for (const clientMode of ["desktop-continuous", "web-remote-replayable"] as const) {
- for (const lifecycle of ["warm", "grace", "parent-release", "cold"] as const) {
- it(`SAT27/SAT28: ${clientMode} child ${lifecycle} 重开保持隐藏且拒绝用户输入`, async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath, undefined, "subagent_child");
- const sentInputs: Array<{ text: string }> = [];
- const options: ZCodeAppOptions[] = [];
- const { server, store } = bootRestartedServer(dbPath, sentInputs, undefined, options);
- const { v4Gateway: gateway } = (
- server as unknown as {
- context: { v4Gateway: ConversationV4Gateway };
- }
- ).context;
- async function subscribe(id: string, topic: string) {
- await requestResult(server, {
- id,
- method: V4_METHODS.conversationSubscribe,
- params: { topic, connectionId: id, clientMode },
- });
- return takeInitialFrameParams(server, id);
- }
- try {
- await subscribe("index-before", sessionsIndexTopic(WORKSPACE_PATH));
- if (lifecycle !== "cold") {
- gateway.ingestDetachedLiveSession(
- SESSION_ID,
- makeEvent(SESSION_ID, 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- PARENT_SESSION_ID,
- );
- gateway.ingestDetachedLiveSession(
- SESSION_ID,
- {
- ...makeEvent(SESSION_ID, 2, SessionEventType.TurnStarted, {
- turnNumber: 1,
- input: "child task",
- }),
- turnId: "child-turn",
- },
- PARENT_SESSION_ID,
- );
- gateway.ingestDetachedLiveSession(
- SESSION_ID,
- {
- ...makeEvent(SESSION_ID, 3, SessionEventType.ModelStreaming, {
- kind: "text_delta",
- delta: "child done",
- done: false,
- }),
- turnId: "child-turn",
- },
- PARENT_SESSION_ID,
- );
- gateway.ingestDetachedLiveSession(
- SESSION_ID,
- {
- ...makeEvent(SESSION_ID, 4, SessionEventType.TurnComplete, {
- duration: 1,
- response: "done",
- resultType: "success",
- tokenCount: 1,
- toolCallCount: 0,
- }),
- turnId: "child-turn",
- },
- PARENT_SESSION_ID,
- );
- }
- if (lifecycle === "grace")
- expect(gateway.pruneDetachedChildPublishers(Date.now() + 180_000)).toBe(1);
- if (lifecycle === "parent-release") gateway.deactivateSession(PARENT_SESSION_ID);
- const child = conversationTopicFrameSchema.parse(
- await subscribe("child", conversationTopic(SESSION_ID)),
- );
- expect(child.payload.kind).toBe("snapshot");
- if (child.payload.kind !== "snapshot") throw new Error("expected child snapshot");
- expect(child.payload.snapshot.rows.window.length).toBeGreaterThan(0);
- const index = sessionsIndexTopicFrameSchema.parse(
- await subscribe("index-after", sessionsIndexTopic(WORKSPACE_PATH)),
- );
- if (index.payload.kind !== "snapshot") throw new Error("expected index snapshot");
- expect(index.payload.snapshot.sessions.map((session) => session.sessionId)).not.toContain(
- SESSION_ID,
- );
- const legacy = (await requestResult(server, {
- id: "legacy-list",
- method: zcodeProtocolMethods.sessionList,
- params: { workspace },
- })) as { sessions: Array<{ sessionId: string }> };
- expect(legacy.sessions.map((session) => session.sessionId)).not.toContain(SESSION_ID);
- expect((await store.getSession(SESSION_ID))?.taskType).toBe("subagent_child");
- if (lifecycle !== "warm")
- expect(
- options.find((option) => option.sessionId === SESSION_ID)?.runtimeConfig?.taskType,
- ).toBe("subagent_child");
- for (const type of [
- "sendText",
- "sendGoalCommand",
- "compact",
- "retryTurn",
- "editUserQuery",
- ] as const) {
- const targetRow = child.payload.snapshot.rows.window.findLast(
- (row) => row.kind === (type === "editUserQuery" ? "userInput" : "assistantText"),
- );
- const ack = commandAckSchema.parse(
- await requestResult(server, {
- id: type,
- method: V4_METHODS.command,
- params: {
- commandId: `child-${type}`,
- clientId: "child-test",
- sessionId: SESSION_ID,
- type,
- baseRevision: child.payload.snapshot.revision,
- baseLogEpoch: child.payload.snapshot.logEpoch,
- payload:
- type === "sendText" || type === "sendGoalCommand"
- ? { text: "continue child" }
- : type === "retryTurn" || type === "editUserQuery"
- ? {
- target: {
- rowId: targetRow?.rowId ?? 1,
- entityId: targetRow?.entityId ?? "missing",
- },
- ...(type === "editUserQuery" ? { newText: "edit child" } : {}),
- }
- : {},
- issuedAt: Date.now(),
- },
- }),
- );
- expect(ack).toMatchObject(
- lifecycle === "warm"
- ? { status: "rejected", reasonCode: "proto.sessionNotFound" }
- : { status: "failed", reasonCode: "guard.subagentReadOnly" },
- );
- }
- expect(await store.listSessionInputs({ sessionID: SESSION_ID })).toEqual([]);
- expect(sentInputs).toEqual([]);
- } finally {
- gateway.dispose();
- store.close();
- }
- });
- }
- }
- it("SAT28: legacy session/send 同样拒绝 child 续聊", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath, undefined, "subagent_child");
- const { server, store } = bootRestartedServer(dbPath, []);
- try {
- await requestResult(server, {
- id: "open-child",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "legacy-child",
- clientMode: "desktop-continuous",
- },
- });
- const result = await server.handleMessage({
- id: "legacy-send",
- method: zcodeProtocolMethods.sessionSend,
- params: { sessionId: SESSION_ID, content: "continue child", queryId: "legacy-child-send" },
- });
- expect(result).toMatchObject({ error: { data: { reasonCode: "guard.subagentReadOnly" } } });
- expect(await store.listSessionInputs({ sessionID: SESSION_ID })).toEqual([]);
- } finally {
- store.close();
- }
- });
- it("SAT29: 显式 session/list 按 workspace 查询隐藏身份,不激活 child;普通列表保留 fork", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath, undefined, "subagent_child");
- const options: ZCodeAppOptions[] = [];
- const { server, store } = bootRestartedServer(dbPath, [], undefined, options);
- try {
- await store.createSession({
- id: "visible-fork" as SessionId,
- parentID: PARENT_SESSION_ID,
- taskType: "fork",
- projectID: "project" as ProjectId,
- slug: "fork",
- title: "fork",
- directory: WORKSPACE_PATH,
- path: WORKSPACE_PATH,
- version: "test",
- });
- const read = (id: string, params: unknown) =>
- requestResult(server, { id, method: zcodeProtocolMethods.sessionList, params }) as Promise<{
- sessions: Array<{ sessionId: string; sessionKind: string }>;
- }>;
- const normal = await read("normal", { workspace });
- expect(normal.sessions.map((item) => item.sessionId)).toEqual(["visible-fork"]);
- const explicit = await read("explicit", {
- workspace,
- sessionIds: [SESSION_ID, "visible-fork", "missing"],
- });
- expect(explicit.sessions.map((item) => [item.sessionId, item.sessionKind])).toEqual([
- [SESSION_ID, "subagent_child"],
- ["visible-fork", "fork"],
- ]);
- expect(
- (await read("foreign", { workspace: remoteWorkspace, sessionIds: [SESSION_ID] })).sessions,
- ).toEqual([]);
- expect(options).toEqual([]);
- } finally {
- store.close();
- }
- });
- it("重启后冷订阅:transcript 合成重建历史 + 可交互 + sendText 再发一轮成功", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const sentInputs: Array<{ text: string }> = [];
- const { server, store } = bootRestartedServer(dbPath, sentInputs);
- try {
- const subscribed = v4ConversationSubscribeResultSchema.parse(
- await requestResult(server, {
- id: 2,
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-desktop",
- clientMode: "desktop-continuous",
- },
- }),
- );
- expect(subscribed.ack.mode).toBe("snapshot");
- const frame = conversationTopicFrameSchema.parse(takeInitialFrameParams(server, 2));
- if (frame?.payload.kind !== "snapshot") {
- throw new Error("Expected snapshot initial frame");
- }
- const snapshot = frame.payload.snapshot;
- // Bug 根因:旧 cold hydration 固定合成 20 万,导致首帧在后续 live 事件纠正前
- // 已触发满载/压缩。首个原始 snapshot 必须直接使用当前 registry 的 1M。
- expect(snapshot.usage.contextWindow).toMatchObject({
- maxTokens: 1_000_000,
- usedTokens: 2,
- });
- // 历史行齐全(transcript→event 合成路径:重启后 record 的内存事件日志为空)。
- const texts = snapshot.rows.window
- .filter((row) => row.kind === "userInput" || row.kind === "assistantText")
- .map((row) => ("text" in row ? row.text : ""));
- expect(texts).toEqual(["历史问题一", "历史回答一", "历史问题二", "历史回答二"]);
- expect(snapshot.rows.window.find((row) => row.kind === "reasoning")).toMatchObject({
- text: "历史思考二",
- state: "complete",
- });
- expect(snapshot.rows.window.find((row) => row.kind === "toolCall")).toMatchObject({
- output: { text: "历史工具输出" },
- status: "success",
- toolCallId: "call_cold_read",
- toolName: "Read",
- });
- // control 可交互:完成态 + 输入直发(composer 不该禁用)。
- expect(snapshot.control.phase).toBe("completedSuccess");
- expect(snapshot.control.canStop).toBe(false);
- expect(snapshot.inputRouting.mode).toBe("startNow");
- // 再发一轮:sendText 必须 accepted 并落到 runtime sendInput。
- const ack = commandAckSchema.parse(
- await requestResult(server, {
- id: 3,
- method: V4_METHODS.command,
- params: {
- commandId: "cmd-cold-send-1",
- clientId: "client-cold",
- sessionId: SESSION_ID,
- type: "sendText",
- payload: { text: "重启后的新问题" },
- issuedAt: 1_700_000_100_000,
- },
- }),
- );
- expect(ack.status).toBe("accepted");
- await new Promise((resolve) => setTimeout(resolve, 0));
- expect(sentInputs).toEqual([{ text: "重启后的新问题" }]);
- // sessions-index 联动:恢复后的会话进入 live 注册表,列表订阅可见。
- const indexResult = v4SessionsIndexSubscribeResultSchema.parse(
- await requestResult(server, {
- id: 4,
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: sessionsIndexTopic(WORKSPACE_PATH),
- connectionId: "conn-desktop-index",
- clientMode: "desktop-continuous",
- },
- }),
- );
- const indexFrame = sessionsIndexTopicFrameSchema.parse(takeInitialFrameParams(server, 4));
- if (indexFrame.payload.kind !== "snapshot") {
- throw new Error("Expected sessions-index snapshot frame");
- }
- expect(indexFrame.payload.snapshot.sessions.map((session) => session.sessionId)).toContain(
- SESSION_ID,
- );
- const resumedSummary = indexFrame.payload.snapshot.sessions.find(
- (session) => session.sessionId === SESSION_ID,
- );
- expect(resumedSummary?.parentSessionId).toBe(PARENT_SESSION_ID);
- // 幂等:再次订阅同一会话不再重建 record(无重复恢复副作用),直接出帧。
- const resubscribed = v4ConversationSubscribeResultSchema.parse(
- await requestResult(server, {
- id: 5,
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-desktop-2",
- clientMode: "desktop-continuous",
- },
- }),
- );
- expect(resubscribed.ack.mode).toBe("snapshot");
- expect(
- conversationTopicFrameSchema.parse(takeInitialFrameParams(server, 5)).payload.kind,
- ).toBe("snapshot");
- } finally {
- store.close();
- }
- });
- it("当前本地 attachment 路径优先于旧 session 的规范化路径", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const sentInputs: Array<{ text: string }> = [];
- const createdAppOptions: ZCodeAppOptions[] = [];
- const { server, store } = bootRestartedServer(dbPath, sentInputs, undefined, createdAppOptions);
- const attachmentWorkspace = {
- workspacePath: `${WORKSPACE_PATH}/`,
- workspaceKey: `${WORKSPACE_PATH}/`,
- };
- try {
- await requestResult(server, {
- id: "subscribe-with-trailing-slash",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-trailing-slash",
- clientMode: "desktop-continuous",
- workspace: attachmentWorkspace,
- },
- });
- takeInitialFrameParams(server, "subscribe-with-trailing-slash");
- const ack = commandAckSchema.parse(
- await requestResult(server, {
- id: "send-after-trailing-slash-resume",
- method: V4_METHODS.command,
- params: {
- commandId: "cmd-trailing-slash-resume",
- clientId: "client-trailing-slash-resume",
- sessionId: SESSION_ID,
- type: "sendText",
- payload: { text: "继续" },
- issuedAt: 1_700_000_100_000,
- },
- }),
- );
- expect(ack.status).toBe("accepted");
- await new Promise((resolve) => setTimeout(resolve, 0));
- expect(sentInputs).toEqual([{ text: "继续" }]);
- expect(createdAppOptions[0]?.runtimeConfig?.workspacePath).toBe(
- attachmentWorkspace.workspacePath,
- );
- } finally {
- store.close();
- }
- });
- it("V4 冷订阅只激活 runtime 并复用一次持久物化,legacy resume 仍返回完整 snapshot", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const probe: ColdFakeAppProbe = { readTodosCalls: 0, resumeCalls: 0 };
- const { server, store } = bootRestartedServer(dbPath, [], probe);
- const messagesSpy = vi.spyOn(store, "messages");
- try {
- await requestResult(server, {
- id: "subscribe-activation-only",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-activation-only",
- clientMode: "desktop-continuous",
- },
- });
- const frame = conversationTopicFrameSchema.parse(
- takeInitialFrameParams(server, "subscribe-activation-only"),
- );
- if (frame.payload.kind !== "snapshot") throw new Error("Expected snapshot frame");
- // Bug 根因:旧 V4 resume 钩子会先构建一份无人消费的 legacy snapshot,
- // usage seed 随后又构建第二份。readTodos 只属于 full snapshot,可作为稳定 seam
- // 证明 V4 现在只做 activation + 自己的 durable hydration。
- expect(probe).toEqual({ readTodosCalls: 0, resumeCalls: 1 });
- // 性能根因:同一份父会话 messages 过去又被 hydration、subagent seed、usage seed
- // 各读一次。正常冷订阅必须只在 activation 起点物化一次。
- expect(
- messagesSpy.mock.calls.filter(([input]) => input.sessionID === SESSION_ID),
- ).toHaveLength(1);
- expect(frame.payload.snapshot.usage.contextWindow).toMatchObject({
- // 当前分支的模型 registry 是 context-window 权威;窄读只替换 usage 取数,
- // 不能把冷 runtime projection 的旧 128k 分母重新覆盖到 V4 首帧。
- maxTokens: 1_000_000,
- usedTokens: 2,
- });
- const legacy = zcodeSessionStateSnapshotSchema.parse(
- await requestResult(server, {
- id: "legacy-resume-after-v4",
- method: zcodeProtocolMethods.sessionResume,
- params: { sessionId: SESSION_ID, workspace },
- }),
- );
- expect(probe).toEqual({ readTodosCalls: 1, resumeCalls: 1 });
- expect(legacy.runtime.contextUsage).toMatchObject({
- size: 1_000_000,
- used: 2,
- });
- expect(frame.payload.snapshot.usage.contextWindow?.cache).toEqual(
- legacy.runtime.contextUsage?.cache,
- );
- } finally {
- store.close();
- }
- });
- it("fresh server 的 legacy session/resume 不向 runtime 注入 V4 持久物化", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const probe: ColdFakeAppProbe = {
- readTodosCalls: 0,
- resumeCalls: 0,
- resumePersistedMessagesProvided: [],
- };
- const { server, store } = bootRestartedServer(dbPath, [], probe);
- try {
- const legacy = zcodeSessionStateSnapshotSchema.parse(
- await requestResult(server, {
- id: "fresh-legacy-resume",
- method: zcodeProtocolMethods.sessionResume,
- params: { sessionId: SESSION_ID, workspace },
- }),
- );
- expect(probe).toMatchObject({
- readTodosCalls: 1,
- resumeCalls: 1,
- resumePersistedMessagesProvided: [false],
- });
- expect(legacy.session.sessionId).toBe(SESSION_ID);
- expect(legacy.runtime.contextUsage).toMatchObject({
- size: 1_000_000,
- used: 2,
- });
- } finally {
- store.close();
- }
- });
- it("runtime 修补中断 compact 后只刷新一次持久物化", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const probe: ColdFakeAppProbe = {
- persistedMessagesReloadRequired: true,
- readTodosCalls: 0,
- resumeCalls: 0,
- };
- const { server, store } = bootRestartedServer(dbPath, [], probe);
- const messagesSpy = vi.spyOn(store, "messages");
- try {
- await requestResult(server, {
- id: "subscribe-compact-refresh",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-compact-refresh",
- clientMode: "desktop-continuous",
- },
- });
- expect(
- messagesSpy.mock.calls.filter(([input]) => input.sessionID === SESSION_ID),
- ).toHaveLength(2);
- expect(probe.resumeCalls).toBe(1);
- } finally {
- store.close();
- }
- });
- it("compact 是最后操作:重启后的首帧保持压缩后 usage", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedCompactedSession(dbPath);
- const { server, store } = bootRestartedServer(dbPath, []);
- try {
- await requestResult(server, {
- id: "subscribe-compact-desktop",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-compact-desktop",
- clientMode: "desktop-continuous",
- },
- });
- const desktopFrame = conversationTopicFrameSchema.parse(
- takeInitialFrameParams(server, "subscribe-compact-desktop"),
- );
- if (desktopFrame.payload.kind !== "snapshot") {
- throw new Error("Expected desktop snapshot frame");
- }
- expect(desktopFrame.payload.snapshot.usage.contextWindow).toMatchObject({
- maxTokens: 1_000_000,
- usedTokens: 1,
- });
- expect(desktopFrame.payload.snapshot.usage.contextWindow?.cache).toBeUndefined();
- } finally {
- store.close();
- }
- });
- it("旧 assistant boundary 缺少压缩后 token 时保持 assistant usage fallback", async () => {
- const compactBoundary: CompactBoundaryPayload = {
- boundaryId: "boundary_cold_legacy",
- trigger: CompactTrigger.Manual,
- preCompactTokenCount: 9,
- summarizedMessageCount: 2,
- summaryMessageIds: [],
- traceId: "trace_cold_legacy_boundary" as TraceId,
- };
- const legacyMessage = {
- info: {
- role: "assistant",
- tokens: { input: 7, output: 2, reasoning: 0, cache: { read: 0, write: 0 } },
- } as MessageWithParts["info"],
- parts: [
- {
- type: "compaction",
- auto: false,
- compactBoundary,
- } as MessagePart,
- ],
- };
- const projection = await createColdFakeApp(undefined, []).runtime.getProjection();
- expect(resolveSessionContextUsage({ messages: [legacyMessage], projection })).toMatchObject({
- size: 128_000,
- used: 9,
- });
- });
- it("重启后冷订阅:优先从 Agent durable selection 恢复 task-local thought", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const seedStore = createSqliteSessionStore({ dbPath });
- try {
- await seedStore.saveSessionEntry({
- id: `${SESSION_ID}:runtime-model-selection`,
- sessionID: SESSION_ID,
- type: SESSION_ENTRY_MODEL_SELECTION,
- time: { created: 5, updated: 5 },
- data: { modelId: "glm-4.6", providerId: "glm", thoughtLevel: "deep" },
- });
- } finally {
- seedStore.close();
- }
- const { server, close } = bootRealRestartedServer(dbPath);
- try {
- await requestResult(server, {
- id: "subscribe-thought",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-thought",
- clientMode: "desktop-continuous",
- // 即便 host 的 legacy hint 迟到/过期,Agent 自己的 durable selection 仍优先。
- resumeThoughtLevel: "medium",
- },
- });
- const frame = conversationTopicFrameSchema.parse(
- takeInitialFrameParams(server, "subscribe-thought"),
- );
- if (frame.payload.kind !== "snapshot") throw new Error("Expected snapshot frame");
- expect(frame.payload.snapshot.config).toMatchObject({
- model: "glm-4.6",
- provider: "glm",
- thought: "deep",
- thoughtLevels: ["medium", "deep"],
- });
- } finally {
- await close();
- }
- });
- it("重启后冷订阅:旧 fork 消息缺档位时不借用相邻消息的档位", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- const legacyForkSessionId = "sess_legacy_fork_thought" as SessionId;
- const parentSessionId = "sess_legacy_fork_parent";
- const targetMessageId = "msg_legacy_fork_target";
- const hiddenMessageId = "msg_legacy_fork_hidden" as MessageId;
- const noticeMessageId = "msg_legacy_fork_notice" as MessageId;
- const forkOrigin = { parentSessionId, targetMessageId };
- const seedStore = createSqliteSessionStore({ dbPath });
- try {
- await seedStore.createSession({
- id: legacyForkSessionId,
- parentID: parentSessionId as SessionId,
- projectID: "project_legacy_fork_thought" as ProjectId,
- slug: "legacy-fork-thought",
- directory: WORKSPACE_PATH,
- path: WORKSPACE_PATH,
- title: "Legacy fork thought",
- version: "test-version",
- time: { created: 1, updated: 4 },
- });
- await seedStore.saveMessage({
- id: hiddenMessageId,
- sessionID: legacyForkSessionId,
- role: "user",
- time: { created: 1 },
- agent: "zcode-agent",
- model: {
- providerID: "glm" as ModelProviderId,
- modelID: "glm-4-air" as ModelId,
- variant: "deep",
- },
- metadata: { forkOrigin },
- semantics: {
- origin: "system",
- kind: "fork_notice",
- uiVisibility: "hidden",
- providerVisibility: "hidden",
- transcriptVisibility: "hidden",
- },
- source: "fork",
- synthetic: true,
- visibility: "model-only",
- tools: {},
- });
- await seedStore.savePart({
- id: `${hiddenMessageId}_text` as MessagePart["id"],
- sessionID: legacyForkSessionId,
- messageID: hiddenMessageId,
- type: "text",
- text: "fork notice",
- synthetic: true,
- time: { start: 1, end: 1 },
- });
- await seedStore.saveMessage({
- id: noticeMessageId,
- sessionID: legacyForkSessionId,
- role: "assistant",
- time: { created: 2, completed: 2 },
- parentID: hiddenMessageId,
- providerID: "glm" as ModelProviderId,
- modelID: "glm-4-air" as ModelId,
- mode: "build",
- agent: "zcode-agent",
- path: { cwd: WORKSPACE_PATH, root: WORKSPACE_PATH },
- cost: 0,
- tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
- metadata: { forkOrigin },
- semantics: {
- origin: "system",
- kind: "timeline_event",
- uiVisibility: "visible",
- providerVisibility: "hidden",
- transcriptVisibility: "visible",
- },
- });
- await seedStore.savePart({
- id: `${noticeMessageId}_timeline` as MessagePart["id"],
- sessionID: legacyForkSessionId,
- messageID: noticeMessageId,
- type: "timeline",
- timelineType: "session_fork",
- display: "separator",
- status: "completed",
- parentSessionId: parentSessionId as SessionId,
- targetMessageId: targetMessageId as MessageId,
- time: { start: 2, end: 2 },
- });
- } finally {
- seedStore.close();
- }
- const { server, close } = bootRealRestartedServer(dbPath);
- try {
- await requestResult(server, {
- id: "subscribe-legacy-fork-thought",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(legacyForkSessionId),
- connectionId: "conn-legacy-fork-thought",
- clientMode: "desktop-continuous",
- },
- });
- const frame = conversationTopicFrameSchema.parse(
- takeInitialFrameParams(server, "subscribe-legacy-fork-thought"),
- );
- if (frame.payload.kind !== "snapshot") throw new Error("Expected snapshot frame");
- expect(frame.payload.snapshot.config).toMatchObject({
- model: "glm-4-air",
- provider: "glm",
- thought: "",
- });
- } finally {
- await close();
- }
- });
- it("冷恢复期间内部 hook 事件不刷新 sessions-index 活动时间", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const seedStore = createSqliteSessionStore({ dbPath });
- const persistedActivityAt = (await seedStore.getSession(SESSION_ID))?.time.updated;
- seedStore.close();
- expect(persistedActivityAt).toBeTypeOf("number");
- const resumeNow = persistedActivityAt! + 60_000;
- const probe: ColdFakeAppProbe = {
- readTodosCalls: 0,
- resumeCalls: 0,
- resumeEventTypes: [
- SessionEventType.HookRunStarted,
- SessionEventType.HookRunProgress,
- SessionEventType.HookRunCompleted,
- SessionEventType.HookRunFailed,
- SessionEventType.HookRunBlocked,
- SessionEventType.WorkspaceHookAdmissionUpdated,
- ],
- };
- const { server, store } = bootRestartedServer(dbPath, [], probe);
- const nowSpy = vi.spyOn(Date, "now").mockReturnValue(resumeNow);
- try {
- await requestResult(server, {
- id: "subscribe-resume-activity",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-resume-activity",
- clientMode: "desktop-continuous",
- },
- });
- takeInitialFrameParams(server, "subscribe-resume-activity");
- await requestResult(server, {
- id: "index-resume-activity",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: sessionsIndexTopic(WORKSPACE_PATH),
- connectionId: "conn-index-resume-activity",
- clientMode: "desktop-continuous",
- },
- });
- const frame = sessionsIndexTopicFrameSchema.parse(
- takeInitialFrameParams(server, "index-resume-activity"),
- );
- if (frame.payload.kind !== "snapshot") {
- throw new Error("Expected sessions-index snapshot frame");
- }
- expect(
- frame.payload.snapshot.sessions.find((session) => session.sessionId === SESSION_ID)
- ?.lastActivityAt,
- ).toBe(persistedActivityAt);
- } finally {
- nowSpy.mockRestore();
- store.close();
- }
- });
- it("重启后冷订阅:旧 transcript 无档位时留空,不读取 task-index 提示", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const { server, close } = bootRealRestartedServer(dbPath);
- try {
- await requestResult(server, {
- id: "subscribe-legacy-thought",
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-legacy-thought",
- clientMode: "desktop-continuous",
- // 旧消息没有档位,桌面提示不能替用户补选。
- resumeThoughtLevel: "deep",
- },
- });
- const frame = conversationTopicFrameSchema.parse(
- takeInitialFrameParams(server, "subscribe-legacy-thought"),
- );
- if (frame.payload.kind !== "snapshot") throw new Error("Expected snapshot frame");
- expect(frame.payload.snapshot.config).toMatchObject({
- model: "glm-4-air",
- provider: "glm",
- thought: "",
- thoughtLevels: ["medium", "deep"],
- });
- } finally {
- await close();
- }
- });
- it("远端 identity 历史会话冷恢复后沿用持久化 identity,sendText 成功", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath, REMOTE_WORKSPACE_IDENTITY);
- const sentInputs: Array<{ text: string }> = [];
- const { server, store } = bootRestartedServer(dbPath, sentInputs);
- try {
- await requestResult(server, {
- id: 2,
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-remote-desktop",
- clientMode: "desktop-continuous",
- },
- });
- const ack = commandAckSchema.parse(
- await requestResult(server, {
- id: 3,
- method: V4_METHODS.command,
- params: {
- commandId: "cmd-remote-cold-send",
- clientId: "client-remote-cold",
- sessionId: SESSION_ID,
- type: "sendText",
- payload: { text: "远端重连后的新问题" },
- issuedAt: 1_700_000_100_000,
- },
- }),
- );
- expect(ack.status).toBe("accepted");
- await new Promise((resolve) => setTimeout(resolve, 0));
- expect(sentInputs).toEqual([{ text: "远端重连后的新问题" }]);
- } finally {
- store.close();
- }
- });
- it("进程 Registry 已就绪时冷恢复不等待旧 workspace registry", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const sentInputs: Array<{ text: string }> = [];
- const { server, store } = bootRestartedServer(dbPath, sentInputs);
- try {
- await requestResult(server, {
- id: 1,
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic(SESSION_ID),
- connectionId: "conn-process-registry",
- clientMode: "desktop-continuous",
- },
- });
- const ack = commandAckSchema.parse(
- await requestResult(server, {
- id: 2,
- method: V4_METHODS.command,
- params: {
- commandId: "cmd-process-registry-cold-send",
- clientId: "client-process-registry",
- sessionId: SESSION_ID,
- type: "sendText",
- payload: { text: "使用当前 Environment Registry" },
- issuedAt: 1_700_000_100_000,
- },
- }),
- );
- expect(ack.status).toBe("accepted");
- await new Promise((resolve) => setTimeout(resolve, 0));
- expect(sentInputs).toEqual([{ text: "使用当前 Environment Registry" }]);
- } finally {
- store.close();
- }
- });
- it("store 无此会话:订阅报 fault.subscribe.sessionNotFound(binder notFound 分型)", async () => {
- const dbPath = join(tempRoot, "sessions.db");
- await seedPersistedSession(dbPath);
- const sentInputs: Array<{ text: string }> = [];
- const { server, store } = bootRestartedServer(dbPath, sentInputs);
- try {
- const response = await server.handleMessage({
- id: 1,
- method: V4_METHODS.conversationSubscribe,
- params: {
- topic: conversationTopic("sess_never_existed"),
- connectionId: "conn-desktop",
- clientMode: "desktop-continuous",
- },
- } as ZCodeProtocolMessage);
- if (!response || !("error" in response)) {
- throw new Error(`Expected protocol error response: ${JSON.stringify(response)}`);
- }
- expect(response.error.message).toContain("fault.subscribe.sessionNotFound");
- } finally {
- store.close();
- }
- });
- });
|