| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206 |
- 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,
- 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 = {
- 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 repo.syncTaskMeta({
- meta: {
- taskId: seedTarget.sessionId,
- traceId: "trace_seed",
- 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,
- archivedAt: 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("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("falls back to a full snapshot when title arrives before task index row exists", async () => {
- const env = setupEnv();
- const target: ZCodeAgentSessionTarget = {
- workspacePath: "/repo",
- sessionId: "sess_missing",
- };
- vi.mocked(env.agent.readSession).mockResolvedValue(
- snapshotWith("sess_missing", "/repo", {
- title: "first prompt title",
- updatedAt: 8,
- }),
- );
- 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 命令路径首发:会话以带首标题的 upsert 出现,sqlite 尚无行 → 回源完整 snapshot。
- env.indexEmitter.fire(
- indexUpsertFrame(summaryOf("sess_missing", "running", { title: "first prompt title" })),
- );
- await vi.waitFor(() => {
- expect(env.agent.readSession).toHaveBeenCalledWith({
- ...target,
- runtimePolicy: "existing-only",
- contentProfile: "index",
- });
- expect(env.agent.resumeSession).not.toHaveBeenCalled();
- });
- await vi.waitFor(async () => {
- const meta = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_missing",
- });
- expect(meta?.title).toBe("first prompt title");
- });
- const groupedStructure = await env.repo.queryGroupedTaskViewStructure({
- workspaceScopes: [{ workspacePath: "/repo" }],
- });
- expect(groupedStructure.topLevelOrders[0]).toMatchObject({
- type: "task",
- taskId: "sess_missing",
- });
- expect(broadcasts.length).toBe(1);
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("places a promoted v4 draft at grouped root top before its title arrives", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf("sess-promoted", "draft")],
- });
- const target: ZCodeAgentSessionTarget = {
- workspacePath: "/repo",
- sessionId: "sess-promoted",
- };
- vi.mocked(env.agent.readSession).mockResolvedValue(
- snapshotWith("sess-promoted", "/repo", { title: "", updatedAt: 8 }),
- );
- 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);
- env.indexEmitter.fire(indexUpsertFrame(summaryOf("sess-promoted", "running")));
- await vi.waitFor(async () => {
- const structure = await env.repo.queryGroupedTaskViewStructure({
- workspaceScopes: [{ workspacePath: "/repo" }],
- });
- expect(structure.topLevelOrders[0]).toMatchObject({
- type: "task",
- taskId: "sess-promoted",
- });
- });
- expect(broadcasts).toContainEqual(
- expect.objectContaining({
- type: "workspace_task_list_changed",
- taskId: "sess-promoted",
- reason: "task_created",
- }),
- );
- subscription.dispose();
- syncer.disposeAll();
- env.indexEmitter.dispose();
- env.configEmitter.dispose();
- });
- it("silently seeds missing rows from the first sessions-index snapshot and skips drafts", async () => {
- const env = setupEnv({
- initialSessions: [summaryOf("sess_history", "completedSuccess", { 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",
- });
- });
- // draft 会话(纯内存态)不进 task index。
- env.indexEmitter.fire(indexUpsertFrame(summaryOf("sess_draft", "draft", { title: "草稿" })));
- await new Promise((resolve) => setImmediate(resolve));
- expect(await env.repo.getTaskMeta({ workspacePath: "/repo", taskId: "sess_draft" })).toBeNull();
- 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?.status).toBe("running");
- });
- 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("SAT29: 只清理明确属于当前 workspace 的 subagent 索引,保留 fork 与未确认记录", async () => {
- const env = setupEnv();
- for (const taskId of ["child", "fork", "unknown"]) {
- await env.repo.syncTaskMetaAtGroupedTop({
- meta: {
- ...snapshotWith(taskId, "/repo").session,
- taskId,
- traceId: "trace",
- title: taskId,
- workspacePath: "/repo",
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- provider: "glm",
- status: "completed",
- },
- });
- }
- await env.repo.syncTaskMeta({
- meta: {
- taskId: "child",
- traceId: "trace",
- title: "foreign",
- workspacePath: "/repo",
- workspaceIdentity: "remote:ssh:foreign:/repo",
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- provider: "glm",
- status: "completed",
- },
- });
- vi.mocked(env.agent.listSessions).mockResolvedValue([
- { ...snapshotWith("child", "/repo").session, sessionKind: "subagent_child" },
- { ...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:/repo",
- taskId: "child",
- }),
- ).not.toBeNull();
- expect(env.agent.listSessions).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath: "/repo",
- sessionIds: expect.arrayContaining(["child", "fork", "unknown"]),
- includeArchived: true,
- }),
- );
- 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 env.repo.syncTaskMeta({
- 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("SAT28: child 完整快照不重新写入主列表", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({ agentService: env.agent, taskIndexRepo: env.repo });
- const snapshot = snapshotWith("child", "/repo", { userMessageText: "child prompt" });
- snapshot.session.sessionKind = "subagent_child";
- 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 env.repo.syncTaskMeta({
- 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: 100,
- 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("syncSnapshotAndBroadcast writes meta and fires workspace_task_list_changed", 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));
- const meta = await syncer.syncSnapshotAndBroadcast(
- snapshotWith("sess_new", "/repo", {
- title: "", // 模拟 runtime 还没写 title,触发 deriveTitleFromSnapshot
- userMessageText: "fix the bug in checkout flow",
- updatedAt: 999,
- }),
- { broadcastReason: "task_meta_changed" },
- );
- // 期望从首条 user message 推断 title
- 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();
- const writes: unknown[] = [];
- const original = env.repo.syncTaskMeta.bind(env.repo);
- vi.spyOn(env.repo, "syncTaskMeta").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({
- meta: { title: "排查订单列表慢查询", status: "completed" },
- 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();
- 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();
- 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();
- 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();
- 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 env.repo.syncTaskMeta({
- 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();
- 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();
- 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("syncSnapshotAndBroadcast writes sqlite but skips broadcast when snapshot has no user content", async () => {
- // Bugfix: createSession 刚返回时 snapshot 既没有 title 也没有 user message,
- // 此时不应该广播 workspace_task_list_changed,否则侧边栏会先闪一下 "New session" 占位符。
- // sqlite 行仍然要写,让后续 session.titleUpdated (source="first_input") 的 applyAgentPatch 能找到行。
- 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);
- // sqlite 行应该已经写入,等待后续 applyAgentPatch 找得到
- const persisted = await env.repo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "sess_empty",
- });
- expect(persisted).not.toBeNull();
- expect(persisted?.taskId).toBe("sess_empty");
- subscription.dispose();
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast can place desktop-created sessions at the grouped view top", async () => {
- const env = setupEnv();
- await env.repo.syncTaskMeta({
- meta: {
- taskId: "older-task",
- traceId: "trace_older",
- title: "Older task",
- workspacePath: "/repo",
- createdAt: 10,
- updatedAt: 10,
- mode: "build",
- status: "completed",
- },
- });
- await env.repo.syncTaskMeta({
- meta: {
- taskId: "newer-existing-task",
- traceId: "trace_newer_existing",
- title: "Newer existing task",
- workspacePath: "/repo",
- createdAt: 20,
- updatedAt: 20,
- mode: "build",
- status: "completed",
- },
- });
- await env.repo.queryGroupedTaskView({
- workspaceScopes: [{ workspacePath: "/repo" }],
- });
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- await syncer.syncSnapshotAndBroadcast(
- snapshotWith("desktop-created-task", "/repo", {
- title: "Desktop created task",
- userMessageText: "Desktop created task",
- createdAt: 30,
- updatedAt: 30,
- }),
- { moveGroupedTaskToTop: true },
- );
- const view = await env.repo.queryGroupedTaskView({
- workspaceScopes: [{ workspacePath: "/repo" }],
- });
- expect(view.nodes[0]).toMatchObject({
- type: "task",
- task: { taskId: "desktop-created-task" },
- });
- 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,
- });
- // 先完成空 workspace 的一次性 grouped bootstrap,后续 task 才按真实新建路径进入 root。
- await env.repo.queryGroupedTaskView({
- workspaceScopes: [{ workspacePath: "/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,
- });
- await syncer.syncSnapshotAndBroadcast(firstTask, {
- moveGroupedTaskToTop: true,
- broadcastReason: "task_status_changed",
- });
- await syncer.syncSnapshotAndBroadcast(secondTask, {
- moveGroupedTaskToTop: true,
- broadcastReason: "task_status_changed",
- });
- // Bugfix 回归:可见状态与首标题缺行可能让 first-task 的重复回源晚于 second-task 完成。
- // 重复 snapshot 只能更新 meta,不能再次给 first-task 分配更小的顶层 sort_order。
- await syncer.syncSnapshotAndBroadcast(firstTask, {
- moveGroupedTaskToTop: true,
- broadcastReason: "task_status_changed",
- });
- const view = await env.repo.queryGroupedTaskView({
- workspaceScopes: [{ workspacePath: "/repo" }],
- });
- expect(view.nodes.slice(0, 2)).toMatchObject([
- { type: "task", task: { taskId: "second-task" } },
- { type: "task", task: { taskId: "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 env.repo.syncTaskMeta({
- 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 env.repo.syncTaskMeta({
- 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 repairs running status when snapshot has a completed assistant turn", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- await env.repo.syncTaskMeta({
- 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).toBe("completed");
- syncer.disposeAll();
- });
- it("syncSnapshotAndBroadcast keeps idle completed snapshots terminal in sqlite", async () => {
- const env = setupEnv();
- const syncer = createZCodeTaskIndexSyncer({
- agentService: env.agent,
- taskIndexRepo: env.repo,
- });
- await env.repo.syncTaskMeta({
- 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).toBe("completed");
- 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,
- ).toBe("running");
- 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,
- ).toBe("running");
- 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,
- ).toBe("running");
- 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();
- });
- });
|