| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367 |
- import { seedTaskIndex } from "./helpers/taskIndexFixture.js";
- import { mkdtempSync, rmSync } from "node:fs";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { Emitter, type Event } from "@zcode/rpc";
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
- import type {
- SessionsIndexTopicWireFrame,
- IZCodeAgentService,
- ZCodeAgentRuntimeLifecycleEvent,
- ZCodeAgentSessionTarget,
- } from "../src/zcode-agent/zcodeAgent.js";
- import { TaskIndexRepo } from "../src/session/taskIndexRepo.js";
- import { setDataBaseDir } from "../src/paths.js";
- import { createZCodeTaskIndexSyncer } from "../src/zcode-agent/zcodeTaskIndexSyncer.js";
- import type {
- ModelSelection,
- ZCodeSessionStateSnapshot,
- ZCodeTaskMeta,
- ZCodeWorkspaceEvent,
- } from "@zcode/shared";
- import { elideSessionSnapshotForIndex, ZCODE_AGENT_PROVIDER_NOT_READY_CODE } from "@zcode/shared";
- import type {
- SessionSummary,
- ErrorAttribution,
- SessionsIndexTopicFrame,
- WorkspaceConfigState,
- WorkspaceConfigTopicFrame,
- WorkspaceConfigTopicWireFrame,
- } from "@zcode/shared/zcode-protocol-v4";
- import {
- encodeTopicWireFrames,
- PROTOCOL_V4_LIMITS,
- utf8JsonByteLength,
- } from "@zcode/shared/zcode-protocol-v4";
- let tempDir: string;
- // 跟踪当前测试的 env,用于 afterEach 清理
- let currentEnv: TestEnv | undefined;
- beforeEach(() => {
- tempDir = mkdtempSync(join(tmpdir(), "zcode-task-index-syncer-"));
- setDataBaseDir(tempDir);
- });
- afterEach(async () => {
- // 先关闭 repo 确保 SQLite 连接释放,避免 Windows EPERM
- currentEnv?.repo?.close();
- currentEnv = undefined;
- setDataBaseDir(null);
- // Windows 需要等待一下让文件句柄释放
- await new Promise((resolve) => setTimeout(resolve, 50));
- try {
- rmSync(tempDir, { recursive: true, force: true });
- } catch {
- // Windows 有时仍会有权限问题,忽略即可
- }
- });
- interface TestEnv {
- agent: IZCodeAgentService;
- indexEmitter: TestTopicEmitter<SessionsIndexTopicFrame, SessionsIndexTopicWireFrame>;
- configEmitter: TestTopicEmitter<WorkspaceConfigTopicFrame, WorkspaceConfigTopicWireFrame>;
- lifecycleEmitter: Emitter<ZCodeAgentRuntimeLifecycleEvent>;
- repo: TaskIndexRepo;
- }
- interface TestTopicEmitter<F extends { topic: string; subscriptionId: string }, W> {
- event: Event<W>;
- fire(frame: F, deliveryKind?: "initial" | "online" | "recovery"): void;
- fireWire(wire: W): void;
- dispose(): void;
- }
- function createTestTopicEmitter<
- F extends { topic: string; subscriptionId: string },
- W,
- >(): TestTopicEmitter<F, W> {
- const emitter = new Emitter<W>();
- let serial = 1;
- return {
- event: emitter.event,
- fire(frame, deliveryKind = "online") {
- const logicalFrameOrdinal = serial++;
- emitter.fire({
- wireVersion: 3,
- kind: "complete",
- logicalFrameId: `test-logical-${logicalFrameOrdinal}`,
- logicalFrameOrdinal,
- deliveryKind,
- topic: frame.topic,
- subscriptionId: frame.subscriptionId,
- frame,
- } as W);
- },
- fireWire: (wire) => emitter.fire(wire),
- dispose: () => emitter.dispose(),
- };
- }
- const INDEX_SUBSCRIPTION_ID = "six-test-1";
- const CONFIG_SUBSCRIPTION_ID = "wcs-test-1";
- type IndexSubscribeResult = Awaited<ReturnType<IZCodeAgentService["subscribeSessionsIndexV4"]>>;
- type IndexResyncResult = Awaited<ReturnType<IZCodeAgentService["resyncSessionsIndexV4"]>>;
- type ConfigResyncResult = Awaited<ReturnType<IZCodeAgentService["resyncWorkspaceConfigV4"]>>;
- function summaryOf(
- sessionId: string,
- phase: SessionSummary["phase"],
- overrides: Partial<SessionSummary> = {},
- ): SessionSummary {
- return {
- sessionId,
- workspaceId: "/repo",
- title: "",
- phase,
- lastOutcome:
- phase === "error"
- ? "error"
- : phase === "completedSuccess"
- ? "completed"
- : phase === "completedInterrupted"
- ? "interrupted"
- : undefined,
- sessionEnded: phase === "completedSuccess" || phase === "completedInterrupted",
- hasBackgroundWork: false,
- lastActivityAt: 100,
- createdAt: 1,
- ...overrides,
- };
- }
- function indexSnapshotFrame(
- sessions: SessionSummary[],
- seq = 1,
- workspaceKey = "/repo",
- ): SessionsIndexTopicFrame {
- return {
- topic: `sessions-index/${workspaceKey}`,
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- fromSeq: 0,
- toSeq: seq,
- sentAt: 1,
- payload: {
- kind: "snapshot",
- snapshot: {
- protocolVersion: 1,
- workspaceId: workspaceKey,
- logEpoch: "epoch-test",
- sessions,
- },
- },
- };
- }
- function indexUpsertFrame(session: SessionSummary, seq = 2): SessionsIndexTopicFrame {
- return {
- topic: "sessions-index//repo",
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- fromSeq: seq - 1,
- toSeq: seq,
- sentAt: 2,
- payload: { kind: "deltas", deltas: [{ op: "session.upserted", session }] },
- };
- }
- function configFrame(config: WorkspaceConfigState, seq = 1): WorkspaceConfigTopicFrame {
- return {
- topic: "workspace-config//repo",
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- fromSeq: 0,
- toSeq: seq,
- sentAt: 3,
- payload: {
- kind: "snapshot",
- snapshot: {
- protocolVersion: 1,
- workspaceId: "/repo",
- logEpoch: "epoch-cfg",
- config,
- },
- },
- };
- }
- function configDeltaFrame(config: WorkspaceConfigState, seq: number): WorkspaceConfigTopicFrame {
- return {
- topic: "workspace-config//repo",
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- fromSeq: seq - 1,
- toSeq: seq,
- sentAt: 4,
- payload: {
- kind: "deltas",
- deltas: [{ op: "config.updated", config }],
- },
- };
- }
- function setupEnv(
- options: {
- initialSessions?: SessionSummary[];
- workspaceKey?: string;
- runtimeLifecycle?: boolean;
- } = {},
- ): TestEnv {
- const indexEmitter = createTestTopicEmitter<
- SessionsIndexTopicFrame,
- SessionsIndexTopicWireFrame
- >();
- const configEmitter = createTestTopicEmitter<
- WorkspaceConfigTopicFrame,
- WorkspaceConfigTopicWireFrame
- >();
- const lifecycleEmitter = new Emitter<ZCodeAgentRuntimeLifecycleEvent>();
- const agent: IZCodeAgentService = {
- reconcileSessionsIndexV4: vi.fn(async ({ sessionIds }) => ({
- sessions: sessionIds.map((sessionId) => {
- const fact = options.initialSessions?.find((entry) => entry.sessionId === sessionId);
- return fact
- ? {
- sessionId,
- exists: true as const,
- deleted: false,
- title: fact.title,
- titleSource: fact.titleSource ?? "generated",
- createdAt: fact.createdAt,
- updatedAt: fact.lastActivityAt,
- lastOutcome: fact.lastOutcome,
- }
- : { sessionId, exists: false as const, deleted: false };
- }),
- })),
- subscribeSessionsIndexV4: vi.fn(async () => {
- const ack = {
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- mode: "snapshot" as const,
- logEpoch: "epoch-test",
- };
- // 04A 真链路:response 与 initial 在同一 read;Promise continuation 尚未运行时
- // notification 已同步 fire,syncer 必须先 staging,ACK 后再按 subId 释放。
- indexEmitter.fire(
- indexSnapshotFrame(options.initialSessions ?? [], 1, options.workspaceKey),
- "initial",
- );
- return { ack };
- }),
- unsubscribeSessionsIndexV4: vi.fn(async () => {}),
- resyncSessionsIndexV4: vi.fn(async (params) => ({
- ack: {
- subscriptionId: params.subscriptionId,
- mode: params.forceSnapshot ? ("snapshot" as const) : ("resume" as const),
- logEpoch: "epoch-test",
- },
- })),
- onDynamicSessionsIndexFrame: vi.fn(() => indexEmitter.event),
- subscribeWorkspaceConfigV4: vi.fn(async () => ({
- ack: {
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- mode: "snapshot" as const,
- logEpoch: "epoch-cfg",
- },
- })),
- unsubscribeWorkspaceConfigV4: vi.fn(async () => {}),
- resyncWorkspaceConfigV4: vi.fn(async (params) => ({
- ack: {
- subscriptionId: params.subscriptionId,
- mode: params.forceSnapshot ? ("snapshot" as const) : ("resume" as const),
- logEpoch: "epoch-cfg",
- },
- })),
- onDynamicWorkspaceConfigFrame: vi.fn(() => configEmitter.event),
- resumeSession: vi.fn(async (target: ZCodeAgentSessionTarget) =>
- snapshotWith(target.sessionId, target.workspacePath),
- ),
- listSessions: vi.fn(async () => []),
- readSession: vi.fn(async (target: ZCodeAgentSessionTarget) =>
- snapshotWith(target.sessionId, target.workspacePath),
- ),
- hasActiveCuaOperationTurn: vi.fn(() => false),
- ...(options.runtimeLifecycle ? { onAgentRuntimeLifecycle: lifecycleEmitter.event } : {}),
- } as unknown as IZCodeAgentService;
- const env = {
- agent,
- indexEmitter,
- configEmitter,
- lifecycleEmitter,
- repo: new TaskIndexRepo(),
- };
- currentEnv = env;
- return env;
- }
- /** 等待 workspace v4 订阅建立完成(subscribe promise resolve + 初始帧静默入基线)。 */
- async function subscribed(env: TestEnv): Promise<void> {
- await vi.waitFor(() => {
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalled();
- });
- // establishWorkspaceSubscriptions 里 Promise.all 之后才写 subscriptionId;多让一拍。
- await new Promise((resolve) => setImmediate(resolve));
- }
- const seedTarget: ZCodeAgentSessionTarget = {
- workspacePath: "/repo",
- sessionId: "sess_complete",
- };
- async function seedRunningTask(repo: TaskIndexRepo): Promise<ZCodeTaskMeta> {
- return seedTaskIndex(repo, {
- meta: {
- taskId: seedTarget.sessionId,
- traceId: "trace_seed",
- provider: "glm",
- title: "Pending task",
- workspacePath: seedTarget.workspacePath,
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- status: "running",
- },
- });
- }
- function snapshotWith(
- sessionId: string,
- workspacePath: string,
- overrides: {
- title?: string;
- createdAt?: number;
- updatedAt?: number;
- userMessageText?: string;
- assistantMessageText?: string;
- assistantCompletedAt?: number;
- sessionStatus?: ZCodeSessionStateSnapshot["session"]["status"];
- settingsModel?: ModelSelection;
- messageModel?: ModelSelection;
- thoughtLevel?: string;
- target?: ZCodeSessionStateSnapshot["projection"]["target"];
- lastError?: { type: string; message: string; attribution?: ErrorAttribution };
- } = {},
- ): ZCodeSessionStateSnapshot {
- const settingsModel = overrides.settingsModel ?? {
- providerId: "anthropic",
- modelId: "haiku-4.5",
- };
- const messageModel = overrides.messageModel ?? settingsModel;
- const messages = [];
- if (overrides.userMessageText !== undefined) {
- messages.push({
- info: {
- messageId: "msg_u1",
- role: "user" as const,
- time: { created: overrides.updatedAt ?? 1, completed: overrides.updatedAt ?? 1 },
- model: messageModel,
- },
- parts: [
- {
- partId: "p1",
- type: "text" as const,
- text: overrides.userMessageText,
- },
- ],
- });
- }
- if (overrides.assistantMessageText !== undefined) {
- messages.push({
- info: {
- messageId: "msg_a1",
- sessionId,
- parentMessageId: "msg_u1",
- role: "assistant" as const,
- time: {
- created: overrides.updatedAt ?? 1,
- ...(typeof overrides.assistantCompletedAt === "number"
- ? { completed: overrides.assistantCompletedAt }
- : {}),
- },
- agent: "zcode",
- model: messageModel,
- path: { cwd: workspacePath, root: workspacePath },
- },
- parts: [
- {
- partId: "p2",
- type: "text" as const,
- text: overrides.assistantMessageText,
- },
- ],
- });
- }
- return {
- protocol: { name: "ZCode Protocol", version: 1 },
- session: {
- sessionId,
- workspace: { workspacePath, workspaceKey: workspacePath },
- sessionKind: "interactive",
- revision: 1,
- status: overrides.sessionStatus ?? "running",
- mode: "build",
- title: overrides.title ?? "",
- createdAt: overrides.createdAt ?? 1,
- updatedAt: overrides.updatedAt ?? 1,
- contextWindow: 100000,
- contextUsage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
- activeTurnId: undefined,
- lastTurnId: undefined,
- lastInputId: undefined,
- },
- settings: {
- model: {
- current: settingsModel,
- available: [],
- },
- thoughtLevel: {
- enabled: overrides.thoughtLevel !== undefined,
- current: overrides.thoughtLevel,
- available: overrides.thoughtLevel ? [overrides.thoughtLevel] : [],
- },
- mode: { current: "build" },
- },
- projection: {
- target: overrides.target ?? null,
- lastError: overrides.lastError ?? null,
- pendingPermissions: [],
- },
- runtime: { stream: { recovering: false } },
- messages: messages as unknown as ZCodeSessionStateSnapshot["messages"],
- } as unknown as ZCodeSessionStateSnapshot;
- }
- describe("createZCodeTaskIndexSyncer", () => {
- it("reconciles snapshot omissions: only explicit deleted hides a row, absent preserves it", async () => {
- const env = setupEnv();
- const existing = await seedRunningTask(env.repo);
- await env.repo.applyCliFacts({
- meta: existing,
- titleSource: "default",
- lastOutcome: "completed",
- });
- await env.repo.applyCliFacts({
- meta: { ...existing, taskId: "deleted" },
- titleSource: "default",
- lastOutcome: "completed",
- });
- env.agent.reconcileSessionsIndexV4 = vi.fn(async ({ sessionIds }) => ({
- sessions: sessionIds.map((sessionId) =>
- sessionId === "deleted"
- ? {
- sessionId,
- exists: true as const,
- deleted: true,
- title: "Deleted",
- titleSource: "custom" as const,
- createdAt: 1,
- updatedAt: 10,
- }
- : { sessionId, exists: false as const, deleted: false as const },
- ),
- }));
- const syncer = createZCodeTaskIndexSyncer({ agentService: env.agent, taskIndexRepo: env.repo });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- const repository = await env.repo.getProjectRepository();
- await vi.waitFor(() =>
- expect(repository.readTree().tasks.map((task) => task.taskId)).toEqual([
- seedTarget.sessionId,
- ]),
- );
- expect(
- (await env.repo.listReconciliationRows({ workspacePath: "/repo" })).find(
- (row) => row.meta.taskId === seedTarget.sessionId,
- )?.handoffPending,
- ).toBe(12);
- syncer.disposeAll();
- });
- it("mirrors initial and live CLI facts without host activity timestamps and hides explicit deletion", async () => {
- const env = setupEnv({
- initialSessions: [
- summaryOf(seedTarget.sessionId, "completedSuccess", {
- title: "CLI title",
- titleSource: "custom",
- lastActivityAt: 40,
- lastOutcome: "completed",
- }),
- ],
- });
- await seedRunningTask(env.repo);
- await env.repo.updateTaskState({
- workspacePath: "/repo",
- taskId: seedTarget.sessionId,
- patch: { pinned: true },
- });
- const syncer = createZCodeTaskIndexSyncer({ agentService: env.agent, taskIndexRepo: env.repo });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- const repository = await env.repo.getProjectRepository();
- await vi.waitFor(() =>
- expect(repository.readTree().tasks[0]).toMatchObject({
- title: "CLI title",
- updatedAt: 40,
- pinned: true,
- }),
- );
- env.indexEmitter.fire(
- indexUpsertFrame(
- summaryOf(seedTarget.sessionId, "completedSuccess", {
- title: "Renamed by CLI",
- titleSource: "custom",
- lastActivityAt: 40,
- lastOutcome: "completed",
- }),
- ),
- );
- await vi.waitFor(() =>
- expect(repository.readTree().tasks[0]).toMatchObject({
- title: "Renamed by CLI",
- updatedAt: 40,
- pinned: true,
- }),
- );
- env.indexEmitter.fire({
- ...indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess"), 3),
- payload: {
- kind: "deltas",
- deltas: [{ op: "session.removed", sessionId: seedTarget.sessionId }],
- },
- });
- await vi.waitFor(() => expect(repository.readTree().tasks).toEqual([]));
- syncer.disposeAll();
- });
- it("writes status: completed on v4 phase running->completedSuccess, broadcasts and emits terminal/ready", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- await seedRunningTask(env.repo);
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: seedTarget.workspacePath,
- })((event) => broadcasts.push(event));
- const terminals: Array<{ kind: string; sessionId: string }> = [];
- const readies: Array<{ reason: string; sessionId: string }> = [];
- syncer.onSessionTerminalEvent((event) =>
- terminals.push({ kind: event.kind, sessionId: event.target.sessionId }),
- );
- syncer.onSessionReadyEvent((event) =>
- readies.push({ reason: event.reason, sessionId: event.target.sessionId }),
- );
- syncer.ensureSessionSubscription(seedTarget);
- await subscribed(env);
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess")));
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- });
- expect(meta?.status).toBe("completed");
- });
- // 终态迁移同时给 host runtime command queue 发终态 + ready(先 terminal 后 ready)。
- expect(terminals).toEqual([{ kind: "turn.completed", sessionId: seedTarget.sessionId }]);
- expect(readies).toEqual([{ reason: "prompt_completed", sessionId: seedTarget.sessionId }]);
- expect(env.agent.readSession).toHaveBeenCalledWith({
- ...seedTarget,
- runtimePolicy: "existing-only",
- contentProfile: "index",
- });
- expect(env.agent.resumeSession).not.toHaveBeenCalled();
- expect(broadcasts.length).toBeGreaterThanOrEqual(1);
- const changed = broadcasts.filter((event) => event.type === "workspace_task_list_changed");
- expect(changed.length).toBeGreaterThanOrEqual(1);
- const first = changed[0];
- if (first.type === "workspace_task_list_changed") {
- expect(first.taskId).toBe(seedTarget.sessionId);
- expect(first.taskMeta?.status).toBe("completed");
- expect(first.unreadSignal).toBe("background_terminal");
- }
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it.each([
- ["普通完成", undefined, "completedSuccess", true],
- ["Goal verified", "verified", "completedSuccess", true],
- ["Goal active 中间轮", "active", "completedSuccess", false],
- ["Goal verifying", "verifying", "completedSuccess", false],
- ["Goal notSatisfied", "notSatisfied", "completedSuccess", false],
- ["Goal paused/取消", "paused", "completedInterrupted", false],
- ["Goal failed 非错误终态", "failed", "completedSuccess", false],
- ["真实错误", "failed", "error", true],
- ] as const)(
- "%s 的终态未读信号符合 Goal 边界",
- async (_label, goalStatus, phase, shouldSignal) => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running", { goalStatus })],
- });
- await seedRunningTask(env.repo);
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- syncer.onDynamicWorkspaceEvent({
- workspacePath: seedTarget.workspacePath,
- })((event) => broadcasts.push(event));
- syncer.ensureSessionSubscription(seedTarget);
- await subscribed(env);
- env.indexEmitter.fire(
- indexUpsertFrame(summaryOf(seedTarget.sessionId, phase, { goalStatus })),
- );
- await vi.waitFor(() => expect(env.agent.readSession).toHaveBeenCalled());
- const unreadSignals = broadcasts.filter(
- (event) =>
- event.type === "workspace_task_list_changed" &&
- event.unreadSignal === "background_terminal",
- );
- expect(unreadSignals).toHaveLength(shouldSignal ? 1 : 0);
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- },
- );
- it("resyncs full snapshot on terminal transition so searchable_text catches up (v4 command path)", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf("sess_v4_search", "running")],
- });
- vi.mocked(env.agent.readSession).mockResolvedValue(
- snapshotWith("sess_v4_search", "/repo", {
- title: "查询订单接口",
- userMessageText: "帮我查询订单接口的实现",
- assistantMessageText: "订单接口在 orderService 里实现。",
- assistantCompletedAt: 130,
- updatedAt: 120,
- }),
- );
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- syncer.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) => broadcasts.push(event));
- syncer.ensureSessionSubscription({
- workspacePath: "/repo",
- sessionId: "sess_v4_search",
- });
- await subscribed(env);
- env.indexEmitter.fire(indexUpsertFrame(summaryOf("sess_v4_search", "completedSuccess")));
- // 行由回源 snapshot 创建(v4 命令路径没有 op 驱动的 snapshot 同步),正文可搜。
- await vi.waitFor(async () => {
- const result = await env.repo.queryTaskList({
- workspaceScopes: [{ workspacePath: "/repo" }],
- search: "orderService",
- });
- expect(result.total).toBe(1);
- });
- expect(
- broadcasts.filter(
- (event) =>
- event.type === "workspace_task_list_changed" &&
- event.unreadSignal === "background_terminal",
- ),
- ).toHaveLength(1);
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("writes status: error on v4 phase running->error and fills lastError from resynced snapshot", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- await seedRunningTask(env.repo);
- vi.mocked(env.agent.readSession).mockResolvedValue(
- snapshotWith(seedTarget.sessionId, seedTarget.workspacePath, {
- title: "Pending task",
- userMessageText: "run it",
- lastError: { type: "MODEL_ERROR", message: "boom" },
- updatedAt: 6,
- }),
- );
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: seedTarget.workspacePath,
- })((event) => broadcasts.push(event));
- const readies: string[] = [];
- syncer.onSessionReadyEvent((event) => readies.push(event.reason));
- syncer.ensureSessionSubscription(seedTarget);
- await subscribed(env);
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "error")));
- // sessions-index 摘要没有错误详情:status 先收敛,lastError 由回源 snapshot 补权威值。
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- });
- expect(meta?.status).toBe("error");
- expect(meta?.lastError?.message).toBe("boom");
- expect(meta?.lastError?.code).toBe("MODEL_ERROR");
- });
- expect(readies).toEqual(["prompt_failed"]);
- expect(broadcasts.length).toBeGreaterThanOrEqual(1);
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("writes title and broadcasts when v4 summary title changes", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- await seedRunningTask(env.repo);
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: seedTarget.workspacePath,
- })((event) => broadcasts.push(event));
- syncer.ensureSessionSubscription(seedTarget);
- await subscribed(env);
- env.indexEmitter.fire(
- indexUpsertFrame(summaryOf(seedTarget.sessionId, "running", { title: "hello world" })),
- );
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- });
- expect(meta?.title).toBe("hello world");
- });
- expect(broadcasts.length).toBeGreaterThanOrEqual(1);
- const last = broadcasts[broadcasts.length - 1];
- if (last.type === "workspace_task_list_changed") {
- expect(last.taskMeta?.title).toBe("hello world");
- }
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("creates the first task directly from its authoritative summary", async () => {
- const env = setupEnv();
- const target: ZCodeAgentSessionTarget = {
- workspacePath: "/repo",
- sessionId: "sess_missing",
- };
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: "/repo",
- })((event) => broadcasts.push(event));
- syncer.ensureSessionSubscription(target);
- await subscribed(env);
- // v4 命令首发:摘要包含所需 CLI 事实,创建行不必回源正文。
- env.indexEmitter.fire(
- indexUpsertFrame(summaryOf("sess_missing", "running", { title: "first prompt title" })),
- );
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_missing",
- });
- expect(meta?.title).toBe("first prompt title");
- });
- expect(
- (await env.repo.getProjectRepository()).readTree().tasks.map((task) => task.taskId),
- ).toContain("sess_missing");
- expect(broadcasts.length).toBe(1);
- expect(env.agent.readSession).not.toHaveBeenCalled();
- expect(env.agent.resumeSession).not.toHaveBeenCalled();
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("FSV02:已持久化 draft 摘要直接创建并广播,不读取正文或激活 runtime", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({ agentService: env.agent, taskIndexRepo: env.repo });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) =>
- broadcasts.push(event),
- );
- syncer.ensureSessionSubscription({ workspacePath: "/repo", sessionId: "accepted" });
- await subscribed(env);
- env.indexEmitter.fire(indexUpsertFrame(summaryOf("accepted", "draft", { title: "first" })));
- await vi.waitFor(() => expect(broadcasts).toHaveLength(1));
- expect(broadcasts[0]).toMatchObject({
- type: "workspace_task_list_changed",
- reason: "task_created",
- });
- expect((await env.repo.getProjectRepository()).readTree().tasks).toEqual([
- expect.objectContaining({ taskId: "accepted", title: "first" }),
- ]);
- expect(env.agent.readSession).not.toHaveBeenCalled();
- expect(env.agent.resumeSession).not.toHaveBeenCalled();
- env.indexEmitter.fire(
- indexUpsertFrame(summaryOf("accepted", "running", { title: "first" }), 3),
- );
- await new Promise((resolve) => setImmediate(resolve));
- expect(broadcasts).toHaveLength(1);
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("silently seeds persisted summaries including draft execution phase", async () => {
- const env = setupEnv({
- initialSessions: [
- summaryOf("sess_history", "completedSuccess", { title: "历史会话" }),
- summaryOf("sess_draft", "draft", { title: "已提交" }),
- ],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: "/repo",
- })((event) => broadcasts.push(event));
- syncer.ensureSessionSubscription({
- workspacePath: "/repo",
- sessionId: "sess_history",
- });
- await subscribed(env);
- // 冷启动 snapshot 不回放历史终态/广播,但必须补齐缺失索引行,避免远端新库永远为空。
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({ workspacePath: "/repo", taskId: "sess_history" });
- expect(meta).toMatchObject({
- taskId: "sess_history",
- title: "历史会话",
- provider: "glm",
- status: "completed",
- });
- });
- // 未提交草稿由 CLI 排除;initial 中的 draft 是已持久化但尚未开始执行的任务。
- expect(
- await env.repo.getTaskMeta({ workspacePath: "/repo", taskId: "sess_draft" }),
- ).toMatchObject({ title: "已提交" });
- expect(broadcasts.length).toBe(0);
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("keeps dynamic workspace-event consumers passive when no runtime exists", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: "/repo",
- workspaceIdentity: "remote:ssh:dev:/repo",
- })(() => {});
- await new Promise((resolve) => setImmediate(resolve));
- expect(env.agent.subscribeSessionsIndexV4).not.toHaveBeenCalled();
- expect(env.agent.subscribeWorkspaceConfigV4).not.toHaveBeenCalled();
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("installs dormant frame listeners before first available subscribe and reuses them after restart", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- runtimeLifecycle: true,
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const terminals: string[] = [];
- const readies: string[] = [];
- syncer.onSessionTerminalEvent((event) => terminals.push(event.kind));
- syncer.onSessionReadyEvent((event) => readies.push(event.reason));
- // 生产中 task adapter 会先 notify/ensure session,再由实际命令启动名单外 runtime。
- syncer.ensureSessionSubscription(seedTarget);
- await Promise.resolve();
- expect(env.agent.subscribeSessionsIndexV4).not.toHaveBeenCalled();
- expect(env.agent.onDynamicSessionsIndexFrame).not.toHaveBeenCalled();
- expect(env.agent.onDynamicWorkspaceConfigFrame).not.toHaveBeenCalled();
- env.lifecycleEmitter.fire({
- workspacePath: seedTarget.workspacePath,
- workspaceKey: seedTarget.workspacePath,
- runtimeIdentity: {
- generation: 1,
- identity: `${seedTarget.workspacePath}:1`,
- workspaceKey: seedTarget.workspacePath,
- },
- state: "available",
- });
- await subscribed(env);
- // 回归根因:首次 available 必须先挂 listener,subscribe response resolve 后、await
- // continuation 前同步到达的 initial frame 才能写入 task index;旧实现只重订阅。
- expect(env.agent.onDynamicSessionsIndexFrame).toHaveBeenCalledTimes(1);
- expect(env.agent.onDynamicWorkspaceConfigFrame).toHaveBeenCalledTimes(1);
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(1);
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- });
- expect(meta?.taskId).toBe(seedTarget.sessionId);
- expect(meta?.status).toBeUndefined();
- });
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess")));
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- });
- expect(meta?.status).toBe("completed");
- });
- expect(terminals).toEqual(["turn.completed"]);
- expect(readies).toEqual(["prompt_completed"]);
- env.lifecycleEmitter.fire({
- workspacePath: seedTarget.workspacePath,
- workspaceKey: seedTarget.workspacePath,
- runtimeIdentity: {
- generation: 1,
- identity: `${seedTarget.workspacePath}:1`,
- workspaceKey: seedTarget.workspacePath,
- },
- state: "unavailable",
- });
- env.lifecycleEmitter.fire({
- workspacePath: seedTarget.workspacePath,
- workspaceKey: seedTarget.workspacePath,
- runtimeIdentity: {
- generation: 2,
- identity: `${seedTarget.workspacePath}:2`,
- workspaceKey: seedTarget.workspacePath,
- },
- state: "available",
- });
- await vi.waitFor(() => {
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(2);
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(2);
- });
- expect(env.agent.onDynamicSessionsIndexFrame).toHaveBeenCalledTimes(1);
- expect(env.agent.onDynamicWorkspaceConfigFrame).toHaveBeenCalledTimes(1);
- expect(terminals).toEqual(["turn.completed"]);
- expect(readies).toEqual(["prompt_completed"]);
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- env.lifecycleEmitter.dispose();
- });
- it("attaches only to an available runtime and returns to dormant after it exits", async () => {
- vi.useFakeTimers();
- try {
- const env = setupEnv({ runtimeLifecycle: true });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const workspaceIdentity = "remote:ssh:dev:/repo";
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: "/repo",
- workspaceIdentity,
- })(() => {});
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo", workspaceIdentity });
- await Promise.resolve();
- expect(env.agent.subscribeSessionsIndexV4).not.toHaveBeenCalled();
- env.lifecycleEmitter.fire({
- workspacePath: "/repo",
- workspaceIdentity,
- workspaceKey: workspaceIdentity,
- runtimeIdentity: {
- generation: 1,
- identity: `${workspaceIdentity}:1`,
- workspaceKey: workspaceIdentity,
- },
- state: "available",
- });
- await vi.waitFor(() => expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1));
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenLastCalledWith(
- expect.objectContaining({
- workspacePath: "/repo",
- workspaceIdentity,
- subscriberScope: "task-index",
- runtimePolicy: "existing-only",
- }),
- );
- env.lifecycleEmitter.fire({
- workspacePath: "/repo",
- workspaceIdentity,
- workspaceKey: workspaceIdentity,
- runtimeIdentity: {
- generation: 1,
- identity: `${workspaceIdentity}:1`,
- workspaceKey: workspaceIdentity,
- },
- state: "unavailable",
- });
- await vi.advanceTimersByTimeAsync(30_000);
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1);
- env.lifecycleEmitter.fire({
- workspacePath: "/repo",
- workspaceIdentity,
- workspaceKey: workspaceIdentity,
- runtimeIdentity: {
- generation: 2,
- identity: `${workspaceIdentity}:2`,
- workspaceKey: workspaceIdentity,
- },
- state: "available",
- });
- await vi.waitFor(() => expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(2));
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- env.lifecycleEmitter.dispose();
- } finally {
- vi.useRealTimers();
- }
- });
- it.each([
- "subagent_child",
- "workflow_child",
- "selection_side_chat",
- "nested_workflow_child",
- ] as const)(
- "SAT29: 只清理当前 workspace 的 %s 索引,保留 fork 与未确认记录",
- async (childKind) => {
- const env = setupEnv();
- for (const taskId of ["child", "fork", "unknown"]) {
- await seedTaskIndex(env.repo, {
- meta: {
- ...snapshotWith(taskId, "/repo").session,
- taskId,
- traceId: "trace",
- title: taskId,
- workspacePath: "/repo",
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- provider: "glm",
- status: "completed",
- },
- });
- }
- await seedTaskIndex(env.repo, {
- meta: {
- taskId: "child",
- traceId: "trace",
- title: "foreign",
- workspacePath: "/repo",
- workspaceIdentity: "remote:ssh:foreign:22:user:/repo",
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- provider: "glm",
- status: "completed",
- },
- });
- vi.mocked(env.agent.listSessions).mockResolvedValue([
- { ...snapshotWith("child", "/repo").session, sessionKind: childKind },
- {
- ...snapshotWith("fork", "/repo").session,
- sessionKind: "fork",
- parentSessionId: "parent",
- },
- ]);
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const changed = vi.fn();
- const listener = syncer.onDynamicWorkspaceEvent("/repo")(changed);
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await vi.waitFor(async () =>
- expect(await env.repo.getTaskMeta({ workspacePath: "/repo", taskId: "child" })).toBeNull(),
- );
- expect(await env.repo.getTaskMeta({ workspacePath: "/repo", taskId: "fork" })).not.toBeNull();
- expect(
- await env.repo.getTaskMeta({ workspacePath: "/repo", taskId: "unknown" }),
- ).not.toBeNull();
- expect(
- await env.repo.getTaskMeta({
- workspacePath: "/repo",
- workspaceIdentity: "remote:ssh:foreign:22:user:/repo",
- taskId: "child",
- }),
- ).not.toBeNull();
- expect(env.agent.listSessions).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath: "/repo",
- sessionIds: expect.arrayContaining(["child", "fork", "unknown"]),
- }),
- );
- expect(env.agent.resumeSession).not.toHaveBeenCalled();
- expect(changed).toHaveBeenCalled();
- listener.dispose();
- syncer.disposeAll();
- },
- );
- it.each(["failure", "disposed"] as const)(
- "SAT29: 身份查询 %s 时保留既有索引",
- async (failure) => {
- const env = setupEnv();
- await seedTaskIndex(env.repo, {
- meta: { ...(await seedRunningTask(env.repo)), provider: "glm" },
- });
- let finish:
- | ((value: Awaited<ReturnType<IZCodeAgentService["listSessions"]>>) => void)
- | undefined;
- vi.mocked(env.agent.listSessions).mockImplementation(() =>
- failure === "failure"
- ? Promise.reject(new Error("remote unavailable"))
- : new Promise((resolve) => {
- finish = resolve;
- }),
- );
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await vi.waitFor(() => expect(env.agent.listSessions).toHaveBeenCalled());
- syncer.disposeAll();
- finish?.([
- { ...snapshotWith(seedTarget.sessionId, "/repo").session, sessionKind: "subagent_child" },
- ]);
- await new Promise((resolve) => setImmediate(resolve));
- expect(
- await env.repo.getTaskMeta({ workspacePath: "/repo", taskId: seedTarget.sessionId }),
- ).not.toBeNull();
- },
- );
- it.each([
- "subagent_child",
- "workflow_child",
- "selection_side_chat",
- "nested_workflow_child",
- ] as const)("SAT28: %s 完整快照不重新写入主列表", async (childKind) => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({ agentService: env.agent, taskIndexRepo: env.repo });
- const snapshot = snapshotWith("child", "/repo", { userMessageText: "child prompt" });
- snapshot.session.sessionKind = childKind;
- vi.mocked(env.agent.reconcileSessionsIndexV4).mockResolvedValue({
- sessions: [
- {
- sessionId: "child",
- exists: true,
- deleted: false,
- title: "child",
- titleSource: "generated",
- createdAt: 1,
- updatedAt: 2,
- },
- ],
- });
- await syncer.syncSnapshotAndBroadcast(snapshot, { broadcastReason: "task_status_changed" });
- expect(await env.repo.getTaskMeta({ workspacePath: "/repo", taskId: "child" })).toBeNull();
- syncer.disposeAll();
- });
- it("does not overwrite existing product-shell state during initial snapshot reconciliation", async () => {
- const env = setupEnv({
- initialSessions: [
- summaryOf("sess_existing", "completedSuccess", {
- title: "Agent 标题",
- titleSource: "generated",
- lastActivityAt: 999,
- }),
- ],
- });
- await seedTaskIndex(env.repo, {
- meta: {
- taskId: "sess_existing",
- traceId: "trace-existing",
- title: "用户标题",
- titleOverridden: true,
- workspacePath: "/repo",
- createdAt: 1,
- updatedAt: 100,
- mode: "build",
- model: "anthropic/haiku-4.5",
- provider: "glm",
- status: "completed",
- },
- pinned: true,
- titleOverridden: true,
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- const [meta] = await env.repo.listTaskMetas({ workspacePath: "/repo", pinned: true });
- expect(meta).toMatchObject({
- taskId: "sess_existing",
- title: "用户标题",
- titleOverridden: true,
- updatedAt: 999,
- mode: "build",
- model: "anthropic/haiku-4.5",
- });
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("seeds remote rows under workspaceIdentity instead of the shared workspacePath", async () => {
- const workspaceIdentity = "remote:ssh:dev.example:22:codegeex:/repo";
- const env = setupEnv({
- workspaceKey: workspaceIdentity,
- initialSessions: [
- summaryOf("sess_remote", "completedSuccess", {
- workspaceId: workspaceIdentity,
- title: "远端会话",
- }),
- ],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.onDynamicWorkspaceEvent({
- workspacePath: "/repo",
- workspaceIdentity,
- })(() => {});
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo", workspaceIdentity });
- await subscribed(env);
- await vi.waitFor(async () => {
- const remoteMeta = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- workspaceIdentity,
- taskId: "sess_remote",
- });
- expect(remoteMeta?.workspaceIdentity).toBe(workspaceIdentity);
- });
- expect(
- await env.repo.getTaskMeta({ workspacePath: "/repo", taskId: "sess_remote" }),
- ).toBeNull();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("forwards v4 workspace-config frames as workspace_config_options_update and drops empty catalogs", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: "/repo",
- })((event) => broadcasts.push(event));
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- // 空目录(无 live session 的订阅种子)不下发,避免清掉工具栏模型目录。
- env.configEmitter.fire(configFrame({ configOptions: [], slashCommands: [] }));
- expect(broadcasts.length).toBe(0);
- env.configEmitter.fire(
- configFrame(
- {
- configOptions: [
- {
- id: "model",
- name: "Model",
- category: "model",
- type: "select",
- currentValue: "anthropic/haiku-4.5",
- options: [{ value: "anthropic/haiku-4.5", name: "Haiku 4.5" }],
- },
- ],
- slashCommands: [{ name: "compact", description: "Compact" }],
- },
- 2,
- ),
- );
- expect(broadcasts.length).toBe(1);
- const event = broadcasts[0];
- expect(event.type).toBe("workspace_config_options_update");
- if (event.type === "workspace_config_options_update") {
- expect(event.workspacePath).toBe("/repo");
- expect(event.configOptions).toHaveLength(1);
- expect(event.configOptions[0]?.id).toBe("model");
- }
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("refreshes CLI facts before indexing presentation and ignores contradictory snapshot facts", async () => {
- const env = setupEnv();
- env.agent.reconcileSessionsIndexV4 = vi.fn(async () => ({
- sessions: [
- {
- sessionId: "fact-owner",
- exists: true,
- deleted: false,
- title: "CLI title",
- titleSource: "generated",
- createdAt: 10,
- updatedAt: 20,
- lastOutcome: "interrupted",
- },
- ],
- }));
- const syncer = createZCodeTaskIndexSyncer({ agentService: env.agent, taskIndexRepo: env.repo });
- const meta = await syncer.syncSnapshotAndBroadcast(
- snapshotWith("fact-owner", "/repo", {
- title: "Snapshot title",
- userMessageText: "searchable text",
- createdAt: 90,
- updatedAt: 99,
- sessionStatus: "error",
- }),
- { broadcastReason: "task_model_changed" },
- );
- expect(meta).toMatchObject({
- title: "CLI title",
- createdAt: 10,
- updatedAt: 20,
- status: "completed",
- });
- expect(
- (
- await env.repo.queryTaskList({
- workspaceScopes: [{ workspacePath: "/repo" }],
- kind: "active",
- search: "searchable text",
- })
- ).items,
- ).toHaveLength(1);
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast writes meta and fires workspace_task_list_changed", async () => {
- const env = setupEnv({
- initialSessions: [
- summaryOf("sess_new", "running", {
- title: "fix the bug in checkout flow",
- lastActivityAt: 999,
- }),
- ],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: "/repo",
- })((event) => broadcasts.push(event));
- const meta = await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_new", "/repo", {
- title: "", // 快照未带标题,使用 CLI 事实
- userMessageText: "fix the bug in checkout flow",
- updatedAt: 999,
- }),
- { broadcastReason: "task_meta_changed" },
- );
- // 标题来自 CLI reconcile,Host 只索引正文。
- expect(meta.title).toBe("fix the bug in checkout flow");
- expect(meta.updatedAt).toBe(999);
- const persisted = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_new",
- });
- expect(persisted?.title).toBe("fix the bug in checkout flow");
- expect(broadcasts.length).toBe(1);
- const evt = broadcasts[0];
- if (evt.type === "workspace_task_list_changed") {
- expect(evt.taskId).toBe("sess_new");
- expect(evt.taskMeta?.title).toBe("fix the bug in checkout flow");
- } else {
- throw new Error("expected workspace_task_list_changed");
- }
- subscription.dispose();
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast 对 index profile 裁剪后的 snapshot 产出与 full 完全一致的索引写入", async () => {
- const big = "z".repeat(256 * 1024);
- const full = snapshotWith("sess_profile", "/repo", {
- title: "",
- userMessageText: "排查订单列表慢查询",
- assistantMessageText: "根因是缺少索引",
- assistantCompletedAt: 50,
- sessionStatus: "completed",
- updatedAt: 60,
- });
- // 固定 traceId:缺省时 buildMetaFromSnapshot 会随机生成,与裁剪无关。
- full.session.traceId = "trace_profile";
- const assistant = full.messages.find((message) => message.info.role === "assistant");
- if (!assistant) throw new Error("fixture assistant missing");
- assistant.parts.unshift(
- { partId: "p_reason", type: "reasoning", text: big } as never,
- {
- partId: "p_tool",
- type: "tool",
- callId: "call_profile",
- tool: "bash",
- state: {
- status: "completed",
- input: { command: big },
- output: big,
- title: "bash",
- metadata: { output: big },
- startedAt: 40,
- completedAt: 41,
- },
- } as never,
- );
- const elided = elideSessionSnapshotForIndex(full);
- expect(JSON.stringify(elided).length).toBeLessThan(big.length / 10);
- const capture = async (snapshot: ZCodeSessionStateSnapshot) => {
- const env = setupEnv({
- initialSessions: [
- summaryOf(snapshot.session.sessionId, "completedSuccess", {
- title: snapshot.session.title,
- }),
- ],
- });
- const writes: unknown[] = [];
- const original = env.repo.updateTaskPresentation.bind(env.repo);
- vi.spyOn(env.repo, "updateTaskPresentation").mockImplementation(async (input) => {
- writes.push(structuredClone(input));
- return await original(input);
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) =>
- broadcasts.push(event),
- );
- const meta = await syncer.syncSnapshotAndBroadcast(snapshot, {
- broadcastReason: "task_status_changed",
- });
- subscription.dispose();
- syncer.disposeAll();
- return { meta, writes, broadcasts };
- };
- const fromFull = await capture(full);
- const fromIndex = await capture(elided);
- expect(fromFull.writes).toHaveLength(1);
- expect(fromFull.writes[0]).toMatchObject({
- searchableText: "排查订单列表慢查询\n根因是缺少索引",
- });
- expect(fromIndex.writes).toEqual(fromFull.writes);
- expect(fromIndex.meta).toEqual(fromFull.meta);
- expect(fromIndex.broadcasts).toEqual(fromFull.broadcasts);
- });
- it("syncSnapshotAndBroadcast prefers structured projection error code in task meta", async () => {
- const env = setupEnv({
- initialSessions: [
- summaryOf("sess_error", "running", { title: "stale model", lastActivityAt: 999 }),
- ],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const restored = snapshotWith("sess_error", "/repo", {
- title: "stale model",
- updatedAt: 999,
- });
- const meta = await syncer.syncSnapshotAndBroadcast(
- {
- ...restored,
- session: {
- ...restored.session,
- status: "error",
- },
- projection: {
- ...restored.projection,
- lastError: {
- type: "MODEL_ERROR",
- code: "provider_not_found",
- message: "Model provider is not configured: stale-provider",
- detail: "stale-provider/gpt-5.5",
- attribution: {
- source: "runtime",
- reason: "provider_not_configured",
- providerId: "account:zai-individual-coding-plan",
- },
- },
- },
- },
- { broadcastReason: "task_status_changed" },
- );
- expect(meta.lastError).toEqual({
- code: "provider_not_found",
- detail: "stale-provider/gpt-5.5",
- message: "Model provider is not configured: stale-provider",
- attribution: {
- source: "runtime",
- reason: "provider_not_configured",
- providerId: "account:zai-individual-coding-plan",
- },
- });
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast stores the latest message model when settings current is polluted", async () => {
- const env = setupEnv({
- initialSessions: [
- summaryOf("sess_model", "running", { title: "model", lastActivityAt: 100 }),
- ],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const meta = await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_model", "/repo", {
- title: "historical model",
- userMessageText: "fix perf",
- settingsModel: {
- providerId: "36965ea6-734a-46a8-8851-3dfec026589e",
- modelId: "glm-5.1-highspeed",
- },
- messageModel: {
- providerId: "default-deepseek",
- modelId: "deepseek-v4-flash",
- },
- }),
- { broadcastReason: "task_meta_changed" },
- );
- expect(meta.model).toBe("default-deepseek/deepseek-v4-flash");
- await expect(
- env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_model",
- }),
- ).resolves.toMatchObject({
- model: "default-deepseek/deepseek-v4-flash",
- });
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast can override task model for explicit model switches", async () => {
- const env = setupEnv({
- initialSessions: [
- summaryOf("sess_model_override", "running", { title: "model", lastActivityAt: 100 }),
- ],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const meta = await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_model_override", "/repo", {
- title: "historical model",
- userMessageText: "continue with visible model",
- settingsModel: {
- providerId: "current-provider",
- modelId: "current-model",
- },
- messageModel: {
- providerId: "removed-provider",
- modelId: "removed-model",
- },
- }),
- { modelOverride: "current-provider/current-model", broadcastReason: "task_meta_changed" },
- );
- expect(meta.model).toBe("current-provider/current-model");
- await expect(
- env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_model_override",
- }),
- ).resolves.toMatchObject({
- model: "current-provider/current-model",
- });
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast can override task thought level for explicit resume", async () => {
- const env = setupEnv({
- initialSessions: [
- summaryOf("sess_thought_override", "running", { title: "thought", lastActivityAt: 100 }),
- ],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const meta = await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_thought_override", "/repo", {
- title: "historical thought level",
- userMessageText: "continue with task-local thought level",
- thoughtLevel: "high",
- }),
- { thoughtLevelOverride: "max", broadcastReason: "task_meta_changed" },
- );
- expect(meta.thoughtLevel).toBe("max");
- await expect(
- env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_thought_override",
- }),
- ).resolves.toMatchObject({
- thoughtLevel: "max",
- });
- syncer.disposeAll();
- });
- it("syncTaskModel updates only the persisted task model", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- await seedTaskIndex(env.repo, {
- meta: {
- taskId: "sess_model_patch",
- traceId: "trace_model_patch",
- title: "Existing task",
- workspacePath: "/repo",
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- model: "removed-provider/removed-model",
- status: "completed",
- },
- });
- await expect(
- syncer.syncTaskModel(
- {
- workspacePath: "/repo",
- sessionId: "sess_model_patch",
- },
- "current-provider/current-model",
- ),
- ).resolves.toMatchObject({
- model: "current-provider/current-model",
- status: "completed",
- title: "Existing task",
- });
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast titles /goal sessions without indexing continuation reminders", async () => {
- const env = setupEnv({
- initialSessions: [
- summaryOf("sess_goal", "running", { title: "优化性能到 60fps", lastActivityAt: 100 }),
- ],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const internalGoalPrompt = [
- '<system-reminder source="goal-continuation">',
- "Continue working toward the active session goal.",
- "",
- "<untrusted_objective>",
- "优化性能到 60fps",
- "</untrusted_objective>",
- "</system-reminder>",
- ].join("\n");
- const meta = await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_goal", "/repo", {
- title: internalGoalPrompt,
- userMessageText: internalGoalPrompt,
- target: {
- sessionId: "sess_goal",
- targetId: "goal_1",
- objective: "优化性能到 60fps",
- summaryTitle: "优化性能到 60fps",
- status: "active",
- tokenBudget: null,
- tokensUsed: 0,
- timeUsedSeconds: 0,
- createdAt: 1,
- updatedAt: 1,
- },
- }),
- { broadcastReason: "task_meta_changed" },
- );
- // Bugfix: /goal 自动续跑的内部 reminder wrapper 只是模型输入,不是用户可见 query。
- // 标题可以回退到真实 goal objective,但正文搜索索引不能写入内部安全封装文本。
- expect(meta.title).toBe("优化性能到 60fps");
- expect(meta.target).toMatchObject({
- objective: "优化性能到 60fps",
- summaryTitle: "优化性能到 60fps",
- targetID: "goal_1",
- });
- const objectiveResult = await env.repo.queryTaskList({
- workspaceScopes: [{ workspacePath: "/repo" }],
- search: "60fps",
- });
- expect(objectiveResult.total).toBe(1);
- const persisted = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_goal",
- });
- expect(persisted?.target).toMatchObject({
- objective: "优化性能到 60fps",
- summaryTitle: "优化性能到 60fps",
- targetID: "goal_1",
- });
- const internalPromptResult = await env.repo.queryTaskList({
- workspaceScopes: [{ workspacePath: "/repo" }],
- search: "system-reminder",
- });
- expect(internalPromptResult.total).toBe(0);
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast preserves indexed goal when snapshot omits target", async () => {
- const env = setupEnv({
- initialSessions: [
- summaryOf("sess_goal_preserve", "running", { title: "goal", lastActivityAt: 100 }),
- ],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_goal_preserve", "/repo", {
- target: {
- sessionId: "sess_goal_preserve",
- targetId: "goal_keep",
- objective: "保留历史目标",
- summaryTitle: null,
- status: "active",
- tokenBudget: null,
- tokensUsed: 0,
- timeUsedSeconds: 0,
- createdAt: 1,
- updatedAt: 1,
- },
- }),
- { broadcastReason: "task_meta_changed" },
- );
- const omittedTargetSnapshot = snapshotWith("sess_goal_preserve", "/repo", {
- title: "继续处理历史任务",
- userMessageText: "继续处理历史任务",
- });
- delete omittedTargetSnapshot.projection.target;
- await syncer.syncSnapshotAndBroadcast(omittedTargetSnapshot, {
- broadcastReason: "task_meta_changed",
- });
- expect(
- (
- await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_goal_preserve",
- })
- )?.target,
- ).toMatchObject({
- objective: "保留历史目标",
- targetID: "goal_keep",
- });
- await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_goal_preserve", "/repo", {
- title: "明确清空历史目标",
- userMessageText: "明确清空历史目标",
- target: null,
- }),
- { broadcastReason: "task_meta_changed" },
- );
- expect(
- (
- await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_goal_preserve",
- })
- )?.target,
- ).toBeNull();
- syncer.disposeAll();
- });
- it("does not create an index row or broadcast for an unpersisted empty draft", async () => {
- // Bugfix: createSession 刚返回时 snapshot 既没有 title 也没有 user message,
- // 此时不应该广播 workspace_task_list_changed,否则侧边栏会先闪一下 "New session" 占位符。
- // 真实 CLI facts 到达前不得创建持久列表行。
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const broadcasts: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: "/repo",
- })((event) => broadcasts.push(event));
- await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_empty", "/repo", {
- title: "",
- // 不带 userMessageText → snapshot.messages 为空
- updatedAt: 1,
- }),
- { broadcastReason: "task_meta_changed" },
- );
- expect(broadcasts.length).toBe(0);
- // 后续 sessions-index 负责创建持久行。
- const persisted = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_empty",
- });
- expect(persisted).toBeNull();
- subscription.dispose();
- syncer.disposeAll();
- });
- it("does not reorder an existing task when duplicate first-snapshot sync finishes late", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const firstTask = snapshotWith("first-task", "/repo", {
- title: "First task",
- userMessageText: "First task",
- createdAt: 10,
- updatedAt: 10,
- });
- const secondTask = snapshotWith("second-task", "/repo", {
- title: "Second task",
- userMessageText: "Second task",
- createdAt: 20,
- updatedAt: 20,
- });
- for (const snapshot of [firstTask, secondTask]) {
- await env.repo.applyCliFacts({
- meta: {
- taskId: snapshot.session.sessionId,
- workspacePath: "/repo",
- traceId: "seed",
- provider: "glm",
- title: snapshot.session.title,
- mode: "default",
- createdAt: snapshot.session.createdAt,
- updatedAt: snapshot.session.updatedAt,
- },
- lastOutcome: "completed",
- });
- }
- await syncer.syncSnapshotAndBroadcast(firstTask, {
- broadcastReason: "task_status_changed",
- });
- await syncer.syncSnapshotAndBroadcast(secondTask, {
- broadcastReason: "task_status_changed",
- });
- // 历史 bug:迟到首帧曾再次置顶旧任务;现在顺序仅由 CLI 活动镜像决定。
- await syncer.syncSnapshotAndBroadcast(firstTask, {
- broadcastReason: "task_status_changed",
- });
- const view = (await env.repo.getProjectRepository()).readTree();
- expect(view.tasks.slice(0, 2).map((task) => task.taskId)).toEqual([
- "second-task",
- "first-task",
- ]);
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast preserves a newer sqlite updated_at (no regression)", async () => {
- // Bugfix: snapshot 的 session.updatedAt 是 runtime sessionStore 里的"结构变更时间",
- // 不一定包含 session.titleUpdated / turn.completed 这些事件触发的 Date.now() 增量。
- // 如果直接用 snapshot.updatedAt 覆盖会让刚被 applyAgentPatch 写入的更新时间倒退,
- // 表现就是新会话第一次 prompt 后又被压到列表底部。
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- // 先写一条 sqlite 行,updated_at 是较新的时间戳(模拟 applyAgentPatch 已经写过一次 Date.now())
- await seedTaskIndex(env.repo, {
- meta: {
- taskId: "sess_regress",
- traceId: "trace_seed",
- title: "你好",
- workspacePath: "/repo",
- createdAt: 100,
- updatedAt: 99999,
- mode: "build",
- },
- });
- // 用一个较旧的 snapshot.updatedAt 触发 sync
- await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_regress", "/repo", {
- title: "你好",
- userMessageText: "你好",
- createdAt: 100,
- updatedAt: 100, // 远小于 sqlite 已有的 99999
- }),
- { broadcastReason: "task_meta_changed" },
- );
- const persisted = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_regress",
- });
- // 期望 updated_at 不会倒退
- expect(persisted?.updatedAt).toBe(99999);
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast does not downgrade newer terminal status with an older running snapshot", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- await seedTaskIndex(env.repo, {
- meta: {
- taskId: "sess_terminal_status",
- traceId: "trace_complete",
- title: "阅读这个项目",
- workspacePath: "/repo",
- createdAt: 100,
- updatedAt: 99_999,
- mode: "build",
- status: "completed",
- },
- });
- await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_terminal_status", "/repo", {
- title: "阅读这个项目",
- userMessageText: "阅读这个项目",
- createdAt: 100,
- updatedAt: 100,
- }),
- { broadcastReason: "task_meta_changed" },
- );
- const persisted = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_terminal_status",
- });
- expect(persisted?.updatedAt).toBe(99_999);
- expect(persisted?.status).toBe("completed");
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast does not infer durable outcome from assistant messages", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- await seedTaskIndex(env.repo, {
- meta: {
- taskId: "sess_repair_terminal",
- traceId: "trace_running",
- title: "阅读这个项目",
- workspacePath: "/repo",
- createdAt: 100,
- updatedAt: 100,
- mode: "build",
- status: "running",
- },
- });
- await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_repair_terminal", "/repo", {
- title: "阅读这个项目",
- userMessageText: "阅读这个项目",
- assistantMessageText: "以下是该项目的完整概览。",
- assistantCompletedAt: 130,
- createdAt: 100,
- updatedAt: 120,
- }),
- { broadcastReason: "task_meta_changed" },
- );
- const persisted = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_repair_terminal",
- });
- expect(persisted?.status).toBeUndefined();
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast does not infer durable outcome from idle runtime", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- await seedTaskIndex(env.repo, {
- meta: {
- taskId: "sess_idle_completed",
- traceId: "trace_idle_completed",
- title: "切换项目后继续对话",
- workspacePath: "/repo",
- createdAt: 100,
- updatedAt: 100,
- mode: "build",
- status: "running",
- },
- });
- await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_idle_completed", "/repo", {
- title: "切换项目后继续对话",
- userMessageText: "继续",
- assistantMessageText: "已经处理完成。",
- assistantCompletedAt: 130,
- sessionStatus: "idle",
- createdAt: 100,
- updatedAt: 130,
- }),
- { broadcastReason: "task_meta_changed" },
- );
- const persisted = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_idle_completed",
- });
- expect(persisted?.status).toBeUndefined();
- syncer.disposeAll();
- });
- it("only subscribes the workspace once even across many sessions", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureSessionSubscription(seedTarget);
- syncer.ensureSessionSubscription(seedTarget);
- syncer.ensureSessionSubscription({
- workspacePath: seedTarget.workspacePath,
- sessionId: "another_session",
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: seedTarget.workspacePath });
- await subscribed(env);
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1);
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(1);
- // 与 renderer 侧栏共 topic 不同代际:syncer 必须带独立 subscriberScope。
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledWith(
- expect.objectContaining({ subscriberScope: "task-index" }),
- );
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("stages same-read index/config initial notifications and releases only the ACK subId", async () => {
- const env = setupEnv();
- await seedRunningTask(env.repo);
- let resolveIndex!: (result: IndexSubscribeResult) => void;
- vi.mocked(env.agent.subscribeSessionsIndexV4).mockImplementationOnce(
- () => new Promise((resolve) => (resolveIndex = resolve)),
- );
- vi.mocked(env.agent.subscribeWorkspaceConfigV4).mockImplementationOnce(async () => {
- env.configEmitter.fire(
- configFrame({
- configOptions: [
- {
- id: "model",
- name: "Model",
- category: "model",
- type: "select",
- currentValue: "anthropic/haiku-4.5",
- options: [{ value: "anthropic/haiku-4.5", name: "Haiku 4.5" }],
- },
- ],
- slashCommands: [],
- }),
- );
- return {
- ack: {
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- mode: "snapshot" as const,
- logEpoch: "epoch-cfg",
- },
- };
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const workspaceEvents: ZCodeWorkspaceEvent[] = [];
- const terminalEvents: string[] = [];
- const workspaceSubscription = syncer.onDynamicWorkspaceEvent({ workspacePath: "/repo" })(
- (event) => workspaceEvents.push(event),
- );
- const terminalSubscription = syncer.onSessionTerminalEvent((event) =>
- terminalEvents.push(event.target.sessionId),
- );
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await vi.waitFor(() => expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1));
- env.indexEmitter.fire({
- ...indexSnapshotFrame([summaryOf(seedTarget.sessionId, "completedSuccess")]),
- subscriptionId: "foreign-sub",
- });
- env.indexEmitter.fire(indexSnapshotFrame([summaryOf(seedTarget.sessionId, "running")]));
- resolveIndex({
- ack: {
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- mode: "snapshot",
- logEpoch: "epoch-test",
- },
- });
- await new Promise((resolve) => setImmediate(resolve));
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess")));
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- });
- expect(meta?.status).toBe("completed");
- });
- expect(terminalEvents).toEqual([seedTarget.sessionId]);
- expect(workspaceEvents.some((event) => event.type === "workspace_config_options_update")).toBe(
- true,
- );
- terminalSubscription.dispose();
- workspaceSubscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("physical sessions-index 分片在最后一片前不修改 task index,齐片后只 apply 一次", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- await seedRunningTask(env.repo);
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const terminalEvents: string[] = [];
- const terminalSubscription = syncer.onSessionTerminalEvent((event) =>
- terminalEvents.push(event.target.sessionId),
- );
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- const logical = indexUpsertFrame(
- summaryOf(seedTarget.sessionId, "completedSuccess", { title: "中文🙂".repeat(300) }),
- );
- const wires = encodeTopicWireFrames(logical, {
- topic: logical.topic,
- subscriptionId: logical.subscriptionId,
- logicalFrameId: "logical-index-fragmented",
- logicalFrameOrdinal: 1_000,
- deliveryKind: "online",
- maxPhysicalFrameBytes: 500,
- measurePhysicalFrameBytes: utf8JsonByteLength,
- }) as SessionsIndexTopicWireFrame[];
- expect(wires.length).toBeGreaterThan(2);
- for (const wire of wires.slice(0, -1)) {
- env.indexEmitter.fireWire(wire);
- }
- await new Promise((resolve) => setImmediate(resolve));
- expect(
- (
- await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- })
- )?.status,
- ).toBeUndefined();
- expect(terminalEvents).toEqual([]);
- env.indexEmitter.fireWire(wires.at(-1)!);
- await vi.waitFor(async () => {
- expect(
- (
- await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- })
- )?.status,
- ).toBe("completed");
- });
- expect(terminalEvents).toEqual([seedTarget.sessionId]);
- terminalSubscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("physical superseded fault 与 newer complete 同批出现时 task index fail closed", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- await seedRunningTask(env.repo);
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const terminalEvents: string[] = [];
- const terminalSubscription = syncer.onSessionTerminalEvent((event) =>
- terminalEvents.push(event.target.sessionId),
- );
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- const oldLogical = indexUpsertFrame(
- summaryOf(seedTarget.sessionId, "completedSuccess", { title: "old".repeat(500) }),
- );
- const oldWires = encodeTopicWireFrames(oldLogical, {
- topic: oldLogical.topic,
- subscriptionId: oldLogical.subscriptionId,
- logicalFrameId: "logical-index-old-partial",
- logicalFrameOrdinal: 1_000,
- deliveryKind: "online",
- maxPhysicalFrameBytes: 500,
- measurePhysicalFrameBytes: utf8JsonByteLength,
- }) as SessionsIndexTopicWireFrame[];
- env.indexEmitter.fireWire(oldWires[0]!);
- const newer = indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess"));
- env.indexEmitter.fireWire({
- wireVersion: 3,
- kind: "complete",
- deliveryKind: "online",
- logicalFrameId: "logical-index-newer",
- logicalFrameOrdinal: 1_001,
- topic: newer.topic,
- subscriptionId: newer.subscriptionId,
- frame: newer,
- });
- await new Promise((resolve) => setImmediate(resolve));
- expect(
- (
- await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- })
- )?.status,
- ).toBeUndefined();
- expect(terminalEvents).toEqual([]);
- terminalSubscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("stale subscribe ACK cannot replace the new generation and is unsubscribed", async () => {
- const env = setupEnv();
- await seedRunningTask(env.repo);
- const restartEmitter = new Emitter<{ workspaceKey: string }>();
- (env.agent as { onAgentRuntimeRestarted?: unknown }).onAgentRuntimeRestarted =
- restartEmitter.event;
- const resolvers: Array<(result: IndexSubscribeResult) => void> = [];
- vi.mocked(env.agent.subscribeSessionsIndexV4).mockImplementation(
- () => new Promise((resolve) => resolvers.push(resolve)),
- );
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await vi.waitFor(() => expect(resolvers).toHaveLength(1));
- env.indexEmitter.fire(indexSnapshotFrame([summaryOf(seedTarget.sessionId, "running")]));
- restartEmitter.fire({ workspaceKey: "/repo" });
- await vi.waitFor(() => expect(resolvers).toHaveLength(2));
- env.indexEmitter.fire({
- ...indexSnapshotFrame([summaryOf(seedTarget.sessionId, "running")]),
- subscriptionId: "six-test-2",
- });
- resolvers[1]!({
- ack: {
- subscriptionId: "six-test-2",
- mode: "snapshot",
- logEpoch: "epoch-test-2",
- },
- });
- await new Promise((resolve) => setImmediate(resolve));
- resolvers[0]!({
- ack: {
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- mode: "snapshot",
- logEpoch: "epoch-test",
- },
- });
- await vi.waitFor(() =>
- expect(env.agent.unsubscribeSessionsIndexV4).toHaveBeenCalledWith(
- expect.objectContaining({ subscriptionId: INDEX_SUBSCRIPTION_ID }),
- ),
- );
- env.indexEmitter.fire({
- ...indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess")),
- subscriptionId: "six-test-2",
- });
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- });
- expect(meta?.status).toBe("completed");
- });
- syncer.disposeAll();
- restartEmitter.dispose();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("dispose clears staged frames and unsubscribes ACKs that resolve after close", async () => {
- const env = setupEnv();
- let resolveIndex!: (result: IndexSubscribeResult) => void;
- vi.mocked(env.agent.subscribeSessionsIndexV4).mockImplementationOnce(
- () => new Promise((resolve) => (resolveIndex = resolve)),
- );
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await vi.waitFor(() => expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1));
- env.indexEmitter.fire(indexSnapshotFrame([summaryOf("staged-after-close", "running")]));
- syncer.disposeAll();
- resolveIndex({
- ack: {
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- mode: "snapshot",
- logEpoch: "epoch-test",
- },
- });
- await vi.waitFor(() => {
- expect(env.agent.unsubscribeSessionsIndexV4).toHaveBeenCalledWith(
- expect.objectContaining({ subscriptionId: INDEX_SUBSCRIPTION_ID }),
- );
- expect(env.agent.unsubscribeWorkspaceConfigV4).toHaveBeenCalledWith(
- expect.objectContaining({ subscriptionId: CONFIG_SUBSCRIPTION_ID }),
- );
- });
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("staging overflow 自动恢复且不会永久 freeze", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- await seedRunningTask(env.repo);
- let resolveIndex!: (result: IndexSubscribeResult) => void;
- vi.mocked(env.agent.subscribeSessionsIndexV4).mockImplementationOnce(
- () => new Promise((resolve) => (resolveIndex = resolve)),
- );
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await vi.waitFor(() => expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1));
- for (let index = 0; index < 1_025; index += 1) {
- env.indexEmitter.fire({
- ...indexSnapshotFrame([summaryOf(seedTarget.sessionId, "running")]),
- toSeq: index,
- });
- }
- resolveIndex({
- ack: {
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- mode: "snapshot",
- logEpoch: "epoch-test",
- },
- });
- await new Promise((resolve) => setImmediate(resolve));
- // pre-ACK overflow 没有可证明 base,必须仅换代 index topic 的 fresh snapshot。
- await vi.waitFor(() => expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(2));
- expect(env.agent.resyncSessionsIndexV4).not.toHaveBeenCalled();
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(1);
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenLastCalledWith(
- expect.objectContaining({
- workspacePath: "/repo",
- visibility: "background",
- subscriberScope: "task-index",
- runtimePolicy: "existing-only",
- }),
- );
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess"), 2));
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- });
- expect(meta?.status).toBe("completed");
- });
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("sessions-index initial assembly fault 不拿 ACK epoch 伪造 base,强制恢复冷 baseline", async () => {
- const cold = summaryOf(seedTarget.sessionId, "running");
- const env = setupEnv();
- await seedRunningTask(env.repo);
- vi.mocked(env.agent.subscribeSessionsIndexV4).mockImplementationOnce(async () => {
- // ACK 前到达但 logical payload 非法:activation 后 assembler 产 typed fault,
- // 这时客户端尚未原子 apply 任何 snapshot,ACK 本身不能证明 base=(epoch,0)。
- env.indexEmitter.fire(
- {
- topic: "sessions-index//repo",
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- fromSeq: 0,
- toSeq: 0,
- sentAt: 1,
- payload: { kind: "deltas", deltas: [{ op: "invalid" }] },
- } as unknown as SessionsIndexTopicFrame,
- "initial",
- );
- return {
- ack: {
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- mode: "snapshot" as const,
- logEpoch: "epoch-test",
- },
- };
- });
- let releaseRecovery!: () => void;
- vi.mocked(env.agent.resyncSessionsIndexV4).mockImplementationOnce(
- (params) =>
- new Promise<IndexResyncResult>((resolve) => {
- releaseRecovery = () => {
- env.indexEmitter.fire(indexSnapshotFrame([cold], 1), "recovery");
- resolve({
- ack: {
- subscriptionId: params.subscriptionId,
- mode: "snapshot",
- logEpoch: "epoch-test",
- },
- });
- };
- }),
- );
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await vi.waitFor(() =>
- expect(env.agent.resyncSessionsIndexV4).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath: "/repo",
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- base: null,
- forceSnapshot: true,
- }),
- ),
- );
- // 整个 initial batch 丢失后,数值上衔接的 online delta 也不能抢先建立 baseline。
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess"), 1));
- await new Promise((resolve) => setImmediate(resolve));
- expect(
- (
- await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- })
- )?.status,
- ).toBeUndefined();
- releaseRecovery();
- await new Promise((resolve) => setImmediate(resolve));
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess"), 2));
- await vi.waitFor(async () => {
- expect(
- (
- await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- })
- )?.status,
- ).toBe("completed");
- });
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("workspace-config initial assembly fault 仅以已 apply 状态为 base", async () => {
- const env = setupEnv();
- const recoveredConfig: WorkspaceConfigState = {
- configOptions: [
- {
- id: "model",
- name: "Model",
- type: "select",
- currentValue: "anthropic/haiku",
- options: [],
- },
- ],
- slashCommands: [],
- };
- vi.mocked(env.agent.subscribeWorkspaceConfigV4).mockImplementationOnce(async () => {
- env.configEmitter.fire(
- {
- topic: "workspace-config//repo",
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- fromSeq: 0,
- toSeq: 0,
- sentAt: 1,
- payload: { kind: "deltas", deltas: [{ op: "invalid" }] },
- } as unknown as WorkspaceConfigTopicFrame,
- "initial",
- );
- return {
- ack: {
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- mode: "snapshot" as const,
- logEpoch: "epoch-cfg",
- },
- };
- });
- vi.mocked(env.agent.resyncWorkspaceConfigV4).mockImplementationOnce(async (params) => {
- if (params.forceSnapshot) {
- env.configEmitter.fire(configFrame(recoveredConfig, 1), "recovery");
- } else {
- env.configEmitter.fire(
- {
- topic: "workspace-config//repo",
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- fromSeq: 0,
- toSeq: 0,
- sentAt: 2,
- payload: { kind: "deltas", deltas: [] },
- },
- "recovery",
- );
- }
- return {
- ack: {
- subscriptionId: params.subscriptionId,
- mode: params.forceSnapshot ? ("snapshot" as const) : ("resume" as const),
- logEpoch: "epoch-cfg",
- },
- };
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const workspaceEvents: ZCodeWorkspaceEvent[] = [];
- syncer.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) =>
- workspaceEvents.push(event),
- );
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await vi.waitFor(() =>
- expect(env.agent.resyncWorkspaceConfigV4).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath: "/repo",
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- base: null,
- forceSnapshot: true,
- }),
- ),
- );
- await vi.waitFor(() =>
- expect(
- workspaceEvents.some(
- (event) =>
- event.type === "workspace_config_options_update" &&
- event.configOptions[0]?.currentValue === "anthropic/haiku",
- ),
- ).toBe(true),
- );
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("sessions-index gap 只恢复 index,workspace-config sibling 保持可用", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- await seedRunningTask(env.repo);
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const workspaceEvents: ZCodeWorkspaceEvent[] = [];
- syncer.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) =>
- workspaceEvents.push(event),
- );
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- env.indexEmitter.fire(
- indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess"), 99),
- );
- await vi.waitFor(() => expect(env.agent.resyncSessionsIndexV4).toHaveBeenCalledTimes(1));
- expect(env.agent.resyncWorkspaceConfigV4).not.toHaveBeenCalled();
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(1);
- env.configEmitter.fire(
- configFrame({
- configOptions: [
- {
- id: "model",
- name: "Model",
- category: "model",
- type: "select",
- currentValue: "anthropic/haiku",
- options: [],
- },
- ],
- slashCommands: [],
- }),
- );
- await vi.waitFor(() =>
- expect(
- workspaceEvents.some((event) => event.type === "workspace_config_options_update"),
- ).toBe(true),
- );
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("recovery assembly fault 早于 snapshot ACK 时 fresh-subscribe index 而不悬空 flight", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- let resolveResync!: (result: IndexResyncResult) => void;
- vi.mocked(env.agent.resyncSessionsIndexV4).mockImplementationOnce(
- () => new Promise((resolve) => (resolveResync = resolve)),
- );
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- // online gap 启动 same-sub recovery。
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "running"), 99));
- await vi.waitFor(() => expect(env.agent.resyncSessionsIndexV4).toHaveBeenCalledTimes(1));
- // 同 ordinal 不同 logical id 产生 typed fault;deliveryKind 在 fault.fault 层。
- const invalidRecoveryFrame = indexUpsertFrame(
- summaryOf(seedTarget.sessionId, "completedSuccess"),
- 100,
- );
- env.indexEmitter.fireWire({
- wireVersion: 3,
- kind: "complete",
- deliveryKind: "recovery",
- logicalFrameId: "conflicting-recovery-id",
- logicalFrameOrdinal: 2,
- topic: invalidRecoveryFrame.topic,
- subscriptionId: invalidRecoveryFrame.subscriptionId,
- frame: invalidRecoveryFrame,
- });
- resolveResync({
- ack: {
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- mode: "snapshot",
- logEpoch: "epoch-test",
- },
- });
- await vi.waitFor(() => expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(2));
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(1);
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("recovery flight 中缺失 deliveryKind 的 owned fault 会升级强制 snapshot", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "running"), 99));
- await vi.waitFor(() => expect(env.agent.resyncSessionsIndexV4).toHaveBeenCalledTimes(1));
- const invalid = indexUpsertFrame(summaryOf(seedTarget.sessionId, "running"), 100);
- env.indexEmitter.fireWire({
- wireVersion: 3,
- kind: "complete",
- // candidate 允许先进 owned assembler 再产生 typed fault;此处故意伪造非法值。
- deliveryKind: "invalid-kind",
- logicalFrameId: "invalid-delivery-kind",
- logicalFrameOrdinal: 3,
- topic: invalid.topic,
- subscriptionId: invalid.subscriptionId,
- frame: invalid,
- } as unknown as SessionsIndexTopicWireFrame);
- await vi.waitFor(() => expect(env.agent.resyncSessionsIndexV4).toHaveBeenCalledTimes(2));
- expect(vi.mocked(env.agent.resyncSessionsIndexV4).mock.calls[1]?.[0]).toMatchObject({
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- base: null,
- forceSnapshot: true,
- });
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("workspace-config recovery gap 早于 snapshot ACK 时仅 fresh-subscribe config", async () => {
- const env = setupEnv();
- let resolveResync!: (result: ConfigResyncResult) => void;
- vi.mocked(env.agent.resyncWorkspaceConfigV4).mockImplementationOnce(
- () => new Promise((resolve) => (resolveResync = resolve)),
- );
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- const config: WorkspaceConfigState = {
- configOptions: [
- {
- id: "model",
- name: "Model",
- type: "select",
- currentValue: "anthropic/haiku",
- options: [],
- },
- ],
- slashCommands: [],
- };
- env.configEmitter.fire(configFrame(config));
- env.configEmitter.fire(configDeltaFrame(config, 99));
- await vi.waitFor(() => expect(env.agent.resyncWorkspaceConfigV4).toHaveBeenCalledTimes(1));
- env.configEmitter.fire(configDeltaFrame(config, 100), "recovery");
- resolveResync({
- ack: {
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- mode: "snapshot",
- logEpoch: "epoch-cfg",
- },
- });
- await vi.waitFor(() => expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(2));
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1);
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("sessions-index duplicate delta 静默丢弃且不重复发射 terminal", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- await seedRunningTask(env.repo);
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const terminals: string[] = [];
- syncer.onSessionTerminalEvent((event) => terminals.push(event.kind));
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess"), 2));
- await vi.waitFor(() => expect(terminals).toEqual(["turn.completed"]));
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "error"), 2));
- await new Promise((resolve) => setImmediate(resolve));
- expect(terminals).toEqual(["turn.completed"]);
- expect(env.agent.resyncSessionsIndexV4).not.toHaveBeenCalled();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("sessions-index recovery 后同 read residual online gap 启动 successor", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- await seedRunningTask(env.repo);
- let attempt = 0;
- vi.mocked(env.agent.resyncSessionsIndexV4).mockImplementation(async (params) => {
- attempt += 1;
- if (attempt === 1) {
- env.indexEmitter.fire(
- indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess"), 2),
- "recovery",
- );
- env.indexEmitter.fire(
- indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess"), 4),
- "online",
- );
- } else {
- env.indexEmitter.fire(
- {
- topic: "sessions-index//repo",
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- fromSeq: 2,
- toSeq: 2,
- sentAt: 4,
- payload: { kind: "deltas", deltas: [] },
- },
- "recovery",
- );
- }
- return {
- ack: {
- subscriptionId: params.subscriptionId,
- mode: "resume" as const,
- logEpoch: "epoch-test",
- },
- };
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess"), 3));
- await vi.waitFor(async () => {
- expect(env.agent.resyncSessionsIndexV4).toHaveBeenCalledTimes(2);
- expect(
- (
- await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- })
- )?.status,
- ).toBe("completed");
- });
- expect(vi.mocked(env.agent.resyncSessionsIndexV4).mock.calls[1]?.[0]).toMatchObject({
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- base: { logEpoch: "epoch-test", seq: 2 },
- });
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1);
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("workspace-config recovery 后 residual online gap 只启动 config successor", async () => {
- const env = setupEnv();
- const config: WorkspaceConfigState = {
- configOptions: [
- {
- id: "model",
- name: "Model",
- type: "select",
- currentValue: "anthropic/haiku",
- options: [],
- },
- ],
- slashCommands: [],
- };
- let attempt = 0;
- vi.mocked(env.agent.resyncWorkspaceConfigV4).mockImplementation(async (params) => {
- attempt += 1;
- if (attempt === 1) {
- env.configEmitter.fire(configDeltaFrame(config, 2), "recovery");
- env.configEmitter.fire(configDeltaFrame(config, 4), "online");
- } else {
- env.configEmitter.fire(
- {
- topic: "workspace-config//repo",
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- fromSeq: 2,
- toSeq: 2,
- sentAt: 5,
- payload: { kind: "deltas", deltas: [] },
- },
- "recovery",
- );
- }
- return {
- ack: { subscriptionId: params.subscriptionId, mode: "resume", logEpoch: "epoch-cfg" },
- };
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- env.configEmitter.fire(configFrame(config, 1));
- env.configEmitter.fire(configDeltaFrame(config, 3));
- await vi.waitFor(() => expect(env.agent.resyncWorkspaceConfigV4).toHaveBeenCalledTimes(2));
- expect(vi.mocked(env.agent.resyncWorkspaceConfigV4).mock.calls[1]?.[0]).toMatchObject({
- subscriptionId: CONFIG_SUBSCRIPTION_ID,
- base: { logEpoch: "epoch-cfg", seq: 2 },
- });
- expect(env.agent.resyncSessionsIndexV4).not.toHaveBeenCalled();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("online snapshot 覆盖在途 recovery 后,重复 recovery 可正常收口", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- let resolveRecovery!: (result: IndexResyncResult) => void;
- vi.mocked(env.agent.resyncSessionsIndexV4).mockImplementationOnce(
- () => new Promise((resolve) => (resolveRecovery = resolve)),
- );
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "running"), 3));
- await vi.waitFor(() => expect(env.agent.resyncSessionsIndexV4).toHaveBeenCalledTimes(1));
- env.indexEmitter.fire(indexSnapshotFrame([summaryOf(seedTarget.sessionId, "running")], 5));
- env.indexEmitter.fire(
- {
- topic: "sessions-index//repo",
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- fromSeq: 1,
- toSeq: 5,
- sentAt: 5,
- payload: {
- kind: "deltas",
- deltas: [{ op: "session.upserted", session: summaryOf(seedTarget.sessionId, "running") }],
- },
- },
- "recovery",
- );
- resolveRecovery({
- ack: { subscriptionId: INDEX_SUBSCRIPTION_ID, mode: "resume", logEpoch: "epoch-test" },
- });
- await new Promise((resolve) => setImmediate(resolve));
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1);
- expect(env.agent.resyncSessionsIndexV4).toHaveBeenCalledTimes(1);
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("index/config recovery ACK 后零 wire:30s 强制一次,再超时 topic-local fresh", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- const config: WorkspaceConfigState = {
- configOptions: [
- { id: "model", name: "Model", type: "select", currentValue: "m", options: [] },
- ],
- slashCommands: [],
- };
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- env.configEmitter.fire(configFrame(config, 1));
- vi.useFakeTimers();
- try {
- env.indexEmitter.fire(indexUpsertFrame(summaryOf(seedTarget.sessionId, "running"), 99));
- env.configEmitter.fire(configDeltaFrame(config, 99));
- await Promise.resolve();
- await Promise.resolve();
- expect(env.agent.resyncSessionsIndexV4).toHaveBeenCalledTimes(1);
- expect(env.agent.resyncWorkspaceConfigV4).toHaveBeenCalledTimes(1);
- await vi.advanceTimersByTimeAsync(PROTOCOL_V4_LIMITS.logicalFrameAssemblyTimeoutMs);
- expect(env.agent.resyncSessionsIndexV4).toHaveBeenCalledTimes(2);
- expect(env.agent.resyncWorkspaceConfigV4).toHaveBeenCalledTimes(2);
- expect(vi.mocked(env.agent.resyncSessionsIndexV4).mock.calls[1]?.[0]).toMatchObject({
- forceSnapshot: true,
- });
- expect(vi.mocked(env.agent.resyncWorkspaceConfigV4).mock.calls[1]?.[0]).toMatchObject({
- forceSnapshot: true,
- });
- await vi.advanceTimersByTimeAsync(PROTOCOL_V4_LIMITS.logicalFrameAssemblyTimeoutMs);
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(2);
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(2);
- } finally {
- syncer.disposeAll();
- vi.useRealTimers();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- }
- });
- it("provider/model 未就绪时不写 warn,低频探测后自动恢复两个 topic", async () => {
- const env = setupEnv();
- const providerNotReadyError = Object.assign(
- new Error("当前没有可用的模型供应商和模型,请先登录或配置 API Key。"),
- { code: ZCODE_AGENT_PROVIDER_NOT_READY_CODE },
- );
- vi.mocked(env.agent.subscribeSessionsIndexV4).mockRejectedValueOnce(providerNotReadyError);
- vi.mocked(env.agent.subscribeWorkspaceConfigV4).mockRejectedValueOnce(providerNotReadyError);
- const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => undefined);
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- vi.useFakeTimers();
- try {
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await vi.advanceTimersByTimeAsync(0);
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1);
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(1);
- expect(warnSpy).not.toHaveBeenCalled();
- await vi.advanceTimersByTimeAsync(4_999);
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1);
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(1);
- await vi.advanceTimersByTimeAsync(1);
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(2);
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(2);
- expect(warnSpy).not.toHaveBeenCalled();
- } finally {
- syncer.disposeAll();
- vi.useRealTimers();
- warnSpy.mockRestore();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- }
- });
- it("持续订阅故障使用固定 retry reason,并将相同 topic 的 warn 限频到每分钟一次", async () => {
- const env = setupEnv();
- const persistentError = new Error("persistent topic failure");
- vi.mocked(env.agent.subscribeSessionsIndexV4).mockRejectedValue(persistentError);
- vi.mocked(env.agent.subscribeWorkspaceConfigV4).mockRejectedValue(persistentError);
- const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => undefined);
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- vi.useFakeTimers();
- try {
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await vi.advanceTimersByTimeAsync(0);
- expect(warnSpy).toHaveBeenCalledTimes(2);
- await vi.advanceTimersByTimeAsync(59_999);
- expect(warnSpy).toHaveBeenCalledTimes(2);
- await vi.advanceTimersByTimeAsync(1_000);
- expect(warnSpy).toHaveBeenCalledTimes(4);
- const messages = warnSpy.mock.calls.map((call) => String(call[1]));
- expect(messages.filter((message) => message.includes("reason=initial"))).toHaveLength(2);
- expect(messages.filter((message) => message.includes("reason=retry"))).toHaveLength(2);
- expect(messages.every((message) => !message.includes("retry:retry"))).toBe(true);
- } finally {
- syncer.disposeAll();
- vi.useRealTimers();
- warnSpy.mockRestore();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- }
- });
- // M5 ③-2(CLI 重连重订):进程换代后订阅静默失活,syncer 必须重发 subscribe
- // 并按新 subscriptionId 闸门继续摄入帧。
- it("re-subscribes workspace topics after an agent runtime restart and accepts new-generation frames", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- await seedRunningTask(env.repo);
- const restartEmitter = new Emitter<{ workspaceKey: string }>();
- (env.agent as { onAgentRuntimeRestarted?: unknown }).onAgentRuntimeRestarted =
- restartEmitter.event;
- // 第二代订阅换 id:重启后旧 id 的帧必须被闸门丢弃,新 id 的帧被接受。
- vi.mocked(env.agent.subscribeSessionsIndexV4)
- .mockImplementationOnce(async () => {
- env.indexEmitter.fire(indexSnapshotFrame([summaryOf(seedTarget.sessionId, "running")]));
- return {
- ack: {
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- mode: "snapshot" as const,
- logEpoch: "epoch-test",
- },
- };
- })
- .mockImplementationOnce(async () => {
- env.indexEmitter.fire({
- ...indexSnapshotFrame([summaryOf(seedTarget.sessionId, "running")]),
- subscriptionId: "six-test-2",
- });
- return {
- ack: {
- subscriptionId: "six-test-2",
- mode: "snapshot" as const,
- logEpoch: "epoch-test-2",
- },
- };
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureSessionSubscription(seedTarget);
- await subscribed(env);
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(1);
- restartEmitter.fire({ workspaceKey: seedTarget.workspacePath });
- await vi.waitFor(() => {
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(2);
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(2);
- });
- await new Promise((resolve) => setImmediate(resolve));
- // 新代际帧(新 subscriptionId)驱动终态写入。
- env.indexEmitter.fire({
- ...indexUpsertFrame(summaryOf(seedTarget.sessionId, "completedSuccess")),
- subscriptionId: "six-test-2",
- });
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: seedTarget.workspacePath,
- taskId: seedTarget.sessionId,
- });
- expect(meta?.status).toBe("completed");
- });
- syncer.disposeAll();
- restartEmitter.dispose();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("runtime restart 后 sessions-index 暂态失败独立重试,config 不被撤销", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf(seedTarget.sessionId, "running")],
- });
- const restartEmitter = new Emitter<{ workspaceKey: string }>();
- (env.agent as { onAgentRuntimeRestarted?: unknown }).onAgentRuntimeRestarted =
- restartEmitter.event;
- vi.mocked(env.agent.subscribeSessionsIndexV4)
- .mockImplementationOnce(async () => {
- env.indexEmitter.fire(
- indexSnapshotFrame([summaryOf(seedTarget.sessionId, "running")]),
- "initial",
- );
- return {
- ack: {
- subscriptionId: INDEX_SUBSCRIPTION_ID,
- mode: "snapshot" as const,
- logEpoch: "epoch-test",
- },
- };
- })
- .mockRejectedValueOnce(new Error("transient restart failure"))
- .mockImplementationOnce(async () => {
- env.indexEmitter.fire(
- {
- ...indexSnapshotFrame([summaryOf(seedTarget.sessionId, "running")]),
- subscriptionId: "six-test-3",
- },
- "initial",
- );
- return {
- ack: {
- subscriptionId: "six-test-3",
- mode: "snapshot" as const,
- logEpoch: "epoch-test-3",
- },
- };
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.ensureWorkspaceSubscription({ workspacePath: "/repo" });
- await subscribed(env);
- restartEmitter.fire({ workspaceKey: "/repo" });
- await vi.waitFor(() => {
- expect(env.agent.subscribeSessionsIndexV4).toHaveBeenCalledTimes(3);
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(2);
- });
- // config 一次就成功,不跟随 index retry 重订。
- await new Promise((resolve) => setTimeout(resolve, 75));
- expect(env.agent.subscribeWorkspaceConfigV4).toHaveBeenCalledTimes(2);
- syncer.disposeAll();
- restartEmitter.dispose();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("ignores ensureSessionSubscription after disposeAll", () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- syncer.disposeAll();
- syncer.ensureSessionSubscription(seedTarget);
- expect(env.agent.subscribeSessionsIndexV4).not.toHaveBeenCalled();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("shares the same workspace emitter across getWorkspaceEmitter and onDynamicWorkspaceEvent", () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- const received: ZCodeWorkspaceEvent[] = [];
- const subscription = syncer.onDynamicWorkspaceEvent({
- workspacePath: "/repo",
- })((event) => received.push(event));
- // adapter 通过 getWorkspaceEmitter 直接 fire 其他类型事件时,订阅者也应该能收到
- syncer.getWorkspaceEmitter({ workspacePath: "/repo" }).fire({
- type: "workspace_config_options_update",
- workspacePath: "/repo",
- configOptions: [],
- } as ZCodeWorkspaceEvent);
- expect(received.length).toBe(1);
- expect(received[0].type).toBe("workspace_config_options_update");
- subscription.dispose();
- syncer.disposeAll();
- });
- });
|