| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400 |
- import { seedTaskIndex } from "./helpers/taskIndexFixture.js";
- import { readTrustedZCodeAgentV4Connection } from "#src/zcode-agent/zcodeAgentConnectionScope.js";
- import {
- readBotGroupRuntimeSnapshot,
- waitBotGroupExecutionEnd,
- } from "#src/zcode-agent/groupRuntimeSnapshot.js";
- import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
- import { tmpdir } from "node:os";
- import { dirname, join } from "node:path";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import { Emitter, setNetworkTelemetrySink } from "@zcode/rpc";
- import type {
- SessionSummary,
- SessionsIndexTopicFrame,
- SessionsIndexTopicWireFrame,
- WorkspaceConfigTopicWireFrame,
- } from "@zcode/shared/zcode-protocol-v4";
- import type { NetworkObservation } from "@zcode/rpc";
- import {
- getLegacyTaskSessionSnapshotPath,
- getTasksIndexDatabasePath,
- setDataBaseDir,
- } from "../src/paths.js";
- import { createZCodeTaskServiceAdapter } from "../src/zcode-agent/zcodeTaskServiceAdapter.js";
- import { createZCodeTaskIndexSyncer } from "../src/zcode-agent/zcodeTaskIndexSyncer.js";
- import { TaskIndexRepo } from "../src/session/taskIndexRepo.js";
- import type { CuaProductMcpServerResolver } from "../src/cua-permission-broker/index.js";
- import type {
- IZCodeAgentService,
- ZCodeAgentCreateSessionParams,
- ZCodeAgentServiceEvent,
- } from "../src/zcode-agent/zcodeAgent.js";
- import type {
- AppSettings,
- TraceId,
- ZCodeMessageWithParts,
- ModelSelection,
- ZCodeProvider,
- ZCodeSessionEvent,
- ZCodeSessionStateSnapshot,
- ZCodeStreamEvent,
- ZCodeWorkspaceStateSnapshot,
- } from "@zcode/shared";
- import {
- resolveWorkspaceKey,
- ZCODE_CUA_OFFICIAL_MCP_NAMESPACE_NAME,
- ZCODE_CUA_OFFICIAL_PLUGIN_ID,
- ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY,
- ZCODE_PLUGIN_ID_ENV_KEY,
- } from "@zcode/shared";
- vi.mock("#src/zcode-agent/groupRuntimeSnapshot.js", () => ({
- readBotGroupRuntimeSnapshot: vi.fn(async () => ({})),
- waitBotGroupExecutionEnd: vi.fn(async () => ({})),
- }));
- const tempDirs: string[] = [];
- const disposables: Array<{ disposeAll(): void }> = [];
- const repositories: TaskIndexRepo[] = [];
- const BACKGROUND_AGENT_PROVIDER_ERROR =
- "Requests are too frequent. Please reduce your request frequency, wait a short moment, and retry your request.";
- const BACKGROUND_AGENT_FAILED_SUMMARY = `Agent general-purpose task "Review" failed. ${BACKGROUND_AGENT_PROVIDER_ERROR}`;
- afterEach(async () => {
- vi.useRealTimers();
- setNetworkTelemetrySink(null);
- while (disposables.length > 0) {
- disposables.pop()?.disposeAll();
- }
- for (const repo of repositories.splice(0)) {
- await repo.ensureReady();
- repo.close({ throwOnError: true });
- }
- setDataBaseDir(null);
- while (tempDirs.length > 0) {
- const dir = tempDirs.pop();
- if (dir) {
- rmSync(dir, { recursive: true, force: true });
- }
- }
- });
- function useTempDataBaseDir(): string {
- const dir = mkdtempSync(join(tmpdir(), "zcode-task-index-"));
- tempDirs.push(dir);
- setDataBaseDir(dir);
- return dir;
- }
- function makeSnapshot(params: {
- sessionId: string;
- workspacePath: string;
- workspaceIdentity?: string;
- title?: string;
- createdAt?: number;
- updatedAt?: number;
- model?: ModelSelection;
- messages?: ZCodeMessageWithParts[];
- backgroundJobs?: ZCodeSessionStateSnapshot["projection"]["backgroundJobs"];
- }): ZCodeSessionStateSnapshot {
- const workspace = {
- workspacePath: params.workspacePath,
- workspaceIdentity: params.workspaceIdentity,
- workspaceKey: params.workspaceIdentity ?? params.workspacePath,
- };
- const model = params.model ?? { providerId: "glm", modelId: "glm-4.6" };
- return {
- protocol: { name: "ZCode Protocol", version: 1 },
- session: {
- sessionId: params.sessionId,
- workspace,
- sessionKind: "interactive",
- title: params.title ?? "Indexed session",
- mode: "build",
- status: "idle",
- model,
- createdAt: params.createdAt ?? 10,
- updatedAt: params.updatedAt ?? 20,
- },
- settings: {
- model: {
- current: model,
- available: [
- {
- ref: model,
- label: model.modelId,
- contextWindow: 128000,
- },
- ],
- lastUsed: model,
- },
- thoughtLevel: {
- enabled: true,
- current: "think",
- available: [{ value: "think", label: "Think" }],
- },
- mode: { current: "build" },
- },
- projection: {
- sessionId: params.sessionId,
- status: "idle",
- mode: "build",
- turnCount: 0,
- totalTokenCount: 0,
- contextUsed: 0,
- contextWindow: 128000,
- pendingPermissions: [],
- activeToolCalls: [],
- backgroundJobs: params.backgroundJobs ?? [],
- },
- runtime: { eventSeq: 0, stateRevision: 1, pendingRequestIds: [] },
- messages: params.messages ?? [],
- };
- }
- function makeUserMessage(params: {
- sessionId: string;
- messageId: string;
- parts?: ZCodeMessageWithParts["parts"];
- text: string;
- }): ZCodeMessageWithParts {
- return {
- info: {
- messageId: params.messageId,
- sessionId: params.sessionId,
- role: "user",
- time: { created: 30 },
- agent: "glm",
- model: { providerId: "glm", modelId: "glm-4.6" },
- },
- parts: params.parts ?? [
- {
- partId: `${params.messageId}_part_1`,
- sessionId: params.sessionId,
- messageId: params.messageId,
- type: "text",
- text: params.text,
- },
- ],
- };
- }
- function makeAssistantAgentToolMessage(params: {
- sessionId: string;
- messageId: string;
- toolCallId: string;
- }): ZCodeMessageWithParts {
- return {
- info: {
- messageId: params.messageId,
- sessionId: params.sessionId,
- role: "assistant",
- time: { created: 40, completed: 50 },
- agent: "glm",
- model: { providerId: "glm", modelId: "glm-4.6" },
- },
- parts: [
- {
- partId: `${params.messageId}_tool_1`,
- sessionId: params.sessionId,
- messageId: params.messageId,
- type: "tool",
- callId: params.toolCallId,
- tool: "Agent",
- state: {
- status: "completed",
- input: { prompt: "Explore project structure" },
- output: "Explore summary",
- title: "Agent",
- metadata: {},
- startedAt: 41,
- completedAt: 49,
- },
- },
- ],
- };
- }
- function createAgentMock() {
- const eventEmitter = new Emitter<ZCodeAgentServiceEvent>();
- const indexFrameEmitter = new Emitter<SessionsIndexTopicWireFrame>();
- const snapshots = new Map<string, ZCodeSessionStateSnapshot>();
- const workspaceState = (params: {
- workspacePath: string;
- workspaceIdentity?: string;
- }): ZCodeWorkspaceStateSnapshot => {
- const snapshot =
- [...snapshots.values()].find(
- (item) =>
- item.session.workspace.workspacePath === params.workspacePath &&
- item.session.workspace.workspaceIdentity === params.workspaceIdentity,
- ) ??
- makeSnapshot({
- sessionId: "draft",
- workspacePath: params.workspacePath,
- workspaceIdentity: params.workspaceIdentity,
- });
- return {
- workspace: snapshot.session.workspace,
- settings: snapshot.settings,
- };
- };
- const agent: IZCodeAgentService = {
- reconcileSessionsIndexV4: vi.fn(async ({ sessionIds }) => ({
- sessions: sessionIds.map((sessionId) => {
- const snapshot = snapshots.get(sessionId);
- // 夹具模拟 CLI 持久会话;结果固定为已完成,不从运行态反推持久结果。
- return snapshot
- ? {
- sessionId,
- exists: true as const,
- deleted: false,
- title: snapshot.session.title,
- titleSource: "generated",
- createdAt: snapshot.session.createdAt,
- updatedAt: snapshot.session.updatedAt,
- lastOutcome: "completed" as const,
- }
- : { sessionId, exists: false as const, deleted: false };
- }),
- })),
- initialize: vi.fn(async (params) => ({
- available: true,
- workspaceKey: params.workspaceIdentity ?? params.workspacePath,
- protocolName: "ZCode Protocol",
- protocolVersion: 1,
- transportKind: "stdio",
- })),
- createSession: vi.fn(async (params: ZCodeAgentCreateSessionParams) => {
- const sessionId = params.sessionId ?? `sess_${snapshots.size + 1}`;
- const snapshot = makeSnapshot({
- sessionId,
- workspacePath: params.workspacePath,
- workspaceIdentity: params.workspaceIdentity,
- model: params.model,
- title: params.importedHistory?.title,
- createdAt: params.importedHistory?.createdAt,
- updatedAt: params.importedHistory?.updatedAt,
- messages: params.importedHistory?.messages.map((message, index) =>
- message.role === "assistant"
- ? ({
- info: {
- messageId: `msg_import_${index}`,
- sessionId,
- role: "assistant",
- time: {
- created: message.timestamp ?? 30 + index,
- completed: message.timestamp ?? 30 + index,
- },
- parentMessageId: `msg_import_${Math.max(0, index - 1)}`,
- agent: "glm",
- model: { providerId: "glm", modelId: "glm-4.6" },
- path: {
- cwd: params.workspacePath,
- root: params.workspacePath,
- },
- cost: 0,
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- cache: { read: 0, write: 0 },
- },
- },
- parts: [
- {
- partId: `part_import_${index}`,
- sessionId,
- messageId: `msg_import_${index}`,
- type: "text",
- text: message.content,
- },
- ],
- } satisfies ZCodeMessageWithParts)
- : makeUserMessage({
- sessionId,
- messageId: `msg_import_${index}`,
- text: message.content,
- }),
- ),
- });
- snapshots.set(snapshot.session.sessionId, snapshot);
- return snapshot;
- }),
- resumeSession: vi.fn(async (params) => {
- const snapshot = snapshots.get(params.sessionId);
- if (!snapshot) {
- throw new Error(`Session not found: ${params.sessionId}`);
- }
- return snapshot;
- }),
- listSessions: vi.fn(async () => {
- throw new Error("listSessions should not be used for task index lists");
- }),
- getTaskTokenUsage: vi.fn(async (params) => ({
- sessionId: params.sessionId,
- totalTokens: 0,
- inputTokens: 0,
- outputTokens: 0,
- reasoningTokens: 0,
- cacheCreationTokens: 0,
- cacheReadTokens: 0,
- modelRequestCount: 0,
- modelErrorCount: 0,
- inputBaselineBySource: {},
- })),
- readSession: vi.fn(
- async (params) =>
- snapshots.get(params.sessionId) ??
- makeSnapshot({
- sessionId: params.sessionId,
- workspacePath: params.workspacePath,
- workspaceIdentity: params.workspaceIdentity,
- }),
- ),
- readSessionMessages: vi.fn(async () => []),
- readSessionEvents: vi.fn(async () => []),
- readWorkspacePresentation: vi.fn(async (params) => ({
- workspace: workspaceState(params).workspace,
- mode: "build" as const,
- slashCommands: [],
- })),
- sendPrompt: vi.fn(
- async (params) =>
- snapshots.get(params.sessionId) ??
- makeSnapshot({
- sessionId: params.sessionId,
- workspacePath: params.workspacePath,
- workspaceIdentity: params.workspaceIdentity,
- }),
- ),
- // M5 ③-2:send/stop/交互回执主路径收敛 v4 命令面(旧 sendPrompt 仅剩附件回退分支)。
- sendConversationCommandV4: vi.fn(async (params) => ({
- commandId: params.envelope.commandId,
- status: "accepted" as const,
- revisionAtDecision: 1,
- ...(params.envelope.type === "createSession"
- ? {
- result: {
- type: "createSession" as const,
- sessionId: "sess_v4_created",
- },
- }
- : {}),
- })),
- closeSession: vi.fn(async () => undefined),
- upsertModelProvider: vi.fn(async (params) => workspaceState(params)),
- removeModelProvider: vi.fn(async (params) => workspaceState(params)),
- setModel: vi.fn(
- async (params) =>
- snapshots.get(params.sessionId) ??
- makeSnapshot({
- sessionId: params.sessionId,
- workspacePath: params.workspacePath,
- workspaceIdentity: params.workspaceIdentity,
- }),
- ),
- setThoughtLevel: vi.fn(
- async (params) =>
- snapshots.get(params.sessionId) ??
- makeSnapshot({
- sessionId: params.sessionId,
- workspacePath: params.workspacePath,
- workspaceIdentity: params.workspaceIdentity,
- }),
- ),
- setMode: vi.fn(
- async (params) =>
- snapshots.get(params.sessionId) ??
- makeSnapshot({
- sessionId: params.sessionId,
- workspacePath: params.workspacePath,
- workspaceIdentity: params.workspaceIdentity,
- }),
- ),
- onDynamicSessionEvent: vi.fn(() => eventEmitter.event),
- // M5 删波次 2:syncer 的终态/ready 摄入换到 v4 sessions-index 帧,
- // 真实 syncer 会在首个 session 激活信号时建立 workspace 级订阅。
- subscribeSessionsIndexV4: vi.fn(async () => {
- // fresh subscribe 必须先给 syncer 一份完整 initial snapshot;后续 ready deltas
- // 才能在同一 log epoch/seq 链上作为权威状态迁移被消费。
- fireIndexSnapshot(indexFrameEmitter);
- return {
- ack: {
- subscriptionId: "six-compat-1",
- mode: "snapshot" as const,
- logEpoch: "epoch-compat",
- },
- };
- }),
- unsubscribeSessionsIndexV4: vi.fn(async () => undefined),
- onDynamicSessionsIndexFrame: vi.fn(() => indexFrameEmitter.event),
- subscribeWorkspaceConfigV4: vi.fn(async () => ({
- ack: {
- subscriptionId: "wcs-compat-1",
- mode: "snapshot" as const,
- logEpoch: "epoch-compat-cfg",
- },
- })),
- unsubscribeWorkspaceConfigV4: vi.fn(async () => undefined),
- onDynamicWorkspaceConfigFrame: vi.fn(() => new Emitter<WorkspaceConfigTopicWireFrame>().event),
- hasActiveCuaOperationTurn: vi.fn(() => false),
- disposeWorkspace: vi.fn(async () => undefined),
- disposeAll: vi.fn(),
- };
- return { agent, eventEmitter, indexFrameEmitter };
- }
- function createService(options?: {
- settings?: Partial<AppSettings>;
- cuaProductMcpServerResolver?: CuaProductMcpServerResolver;
- }) {
- useTempDataBaseDir();
- const { agent, eventEmitter, indexFrameEmitter } = createAgentMock();
- const settingService = options?.settings
- ? {
- get: vi.fn(
- async () =>
- ({
- recentProjects: [],
- locale: "zh-CN",
- ...options.settings,
- }) as AppSettings,
- ),
- }
- : undefined;
- // adapter 现在要求注入 syncer;测试里给一个真实的 syncer(agent 上面已经 mock 了 onDynamicSessionEvent)。
- // 旧夹具跟随全局路径,迟到的异步初始化会打开下一用例的库并造成锁冲突。
- // 与真实 Host 装配一样固定数据库路径,并在删除夹具目录前关闭连接。
- const taskIndexRepo = new TaskIndexRepo(undefined, undefined, getTasksIndexDatabasePath());
- repositories.push(taskIndexRepo);
- const taskIndexSyncer = createZCodeTaskIndexSyncer({
- agentService: agent,
- taskIndexRepo,
- });
- const service = createZCodeTaskServiceAdapter({
- zcodeAgentService: agent,
- taskIndexRepo,
- taskIndexSyncer,
- ...(settingService ? { settingService } : {}),
- ...(options?.cuaProductMcpServerResolver
- ? { cuaProductMcpServerResolver: options.cuaProductMcpServerResolver }
- : {}),
- }) as ReturnType<typeof createZCodeTaskServiceAdapter> & {
- disposeAll(): void;
- };
- disposables.push(service);
- return { agent, eventEmitter, indexFrameEmitter, service, taskIndexRepo };
- }
- async function flushMicrotasks(times = 5): Promise<void> {
- for (let index = 0; index < times; index += 1) {
- await Promise.resolve();
- }
- }
- const compatIndexFrameSeqByEmitter = new WeakMap<Emitter<SessionsIndexTopicWireFrame>, number>();
- function fireIndexSnapshot(indexFrameEmitter: Emitter<SessionsIndexTopicWireFrame>): void {
- const seq = 100;
- compatIndexFrameSeqByEmitter.set(indexFrameEmitter, seq);
- const frame: SessionsIndexTopicFrame = {
- topic: "sessions-index//repo",
- subscriptionId: "six-compat-1",
- fromSeq: 0,
- toSeq: seq,
- sentAt: 100,
- payload: {
- kind: "snapshot",
- snapshot: {
- protocolVersion: 1,
- workspaceId: "/repo",
- logEpoch: "epoch-compat",
- sessions: [],
- },
- },
- };
- indexFrameEmitter.fire({
- wireVersion: 3,
- deliveryKind: "online",
- kind: "complete",
- logicalFrameId: `logical-compat-${seq}`,
- logicalFrameOrdinal: seq,
- topic: frame.topic,
- subscriptionId: frame.subscriptionId,
- frame,
- });
- }
- function compatSummary(
- sessionId: string,
- phase: "running" | "completedSuccess" | "error",
- ): SessionSummary {
- return {
- sessionId,
- workspaceId: "/repo",
- title: "",
- phase,
- sessionEnded: phase !== "running",
- hasBackgroundWork: false,
- lastActivityAt: 100,
- createdAt: 1,
- };
- }
- function fireIndexUpsert(
- indexFrameEmitter: Emitter<SessionsIndexTopicWireFrame>,
- session: SessionSummary,
- ): void {
- const compatIndexFrameSeq = (compatIndexFrameSeqByEmitter.get(indexFrameEmitter) ?? 0) + 1;
- compatIndexFrameSeqByEmitter.set(indexFrameEmitter, compatIndexFrameSeq);
- const frame: SessionsIndexTopicFrame = {
- topic: "sessions-index//repo",
- subscriptionId: "six-compat-1",
- fromSeq: compatIndexFrameSeq - 1,
- toSeq: compatIndexFrameSeq,
- sentAt: 100,
- payload: { kind: "deltas", deltas: [{ op: "session.upserted", session }] },
- };
- indexFrameEmitter.fire({
- wireVersion: 3,
- deliveryKind: "online",
- kind: "complete",
- logicalFrameId: `logical-compat-${compatIndexFrameSeq}`,
- logicalFrameOrdinal: compatIndexFrameSeq,
- topic: frame.topic,
- subscriptionId: frame.subscriptionId,
- frame,
- });
- }
- /**
- * M5 删波次 2:prompt ready 边界改由 v4 sessions-index 的 phase 终态迁移驱动
- * (running → completedSuccess),替代旧 state.updated prompt_completed 通知。
- */
- async function firePromptCompletedState(
- indexFrameEmitter: Emitter<SessionsIndexTopicWireFrame>,
- params: {
- sessionId: string;
- workspacePath: string;
- workspaceIdentity?: string;
- },
- ): Promise<void> {
- // 等 syncer 的 workspace v4 订阅建立完(subscriptionId 闸门就位)。
- await flushMicrotasks();
- fireIndexUpsert(indexFrameEmitter, compatSummary(params.sessionId, "running"));
- fireIndexUpsert(indexFrameEmitter, compatSummary(params.sessionId, "completedSuccess"));
- }
- async function seedIndexedTask(
- repo: TaskIndexRepo,
- params: {
- taskId: string;
- provider: ZCodeProvider;
- model?: string;
- workspacePath?: string;
- createdAt?: number;
- updatedAt?: number;
- pinned?: boolean;
- archived?: boolean;
- migrationSource?: "claudeCode";
- thoughtLevel?: string;
- },
- ) {
- await seedTaskIndex(repo, {
- meta: {
- taskId: params.taskId,
- traceId: `trace_${params.taskId}`,
- title: params.taskId,
- workspacePath: params.workspacePath ?? "/repo",
- createdAt: params.createdAt ?? 10,
- updatedAt: params.updatedAt ?? 20,
- mode: "build",
- status: "completed",
- provider: params.provider,
- ...(params.model ? { model: params.model } : {}),
- ...(params.thoughtLevel ? { thoughtLevel: params.thoughtLevel } : {}),
- ...(params.migrationSource ? { migrationSource: params.migrationSource } : {}),
- },
- pinned: params.pinned,
- archived: params.archived,
- });
- }
- async function seedLegacyAcpSessionId(
- repo: TaskIndexRepo,
- params: {
- workspacePath: string;
- workspaceIdentity?: string;
- taskId: string;
- acpSessionId: string;
- },
- ) {
- await repo.ensureReady();
- const db = (
- repo as unknown as {
- getDatabase(): {
- exec(sql: string): unknown;
- prepare(sql: string): {
- run(...args: unknown[]): unknown;
- };
- };
- }
- ).getDatabase();
- db.exec("ALTER TABLE tasks ADD COLUMN acp_session_id TEXT");
- db.prepare(`UPDATE tasks SET acp_session_id = ? WHERE workspace_key = ? AND task_id = ?`).run(
- params.acpSessionId,
- resolveWorkspaceKey(params),
- params.taskId,
- );
- // Bugfix: 老库的 acp_session_id 只在初始化迁移阶段读取。
- // 测试里手工补列后重开 repo,验证迁移不是运行时 facade 兜底。
- repo.close();
- await repo.ensureReady();
- }
- describe("ZCode legacy task compat task index", () => {
- it("各夹具的迟到写入保持在自己的数据库中", async () => {
- const first = createService();
- await first.taskIndexRepo.ensureReady();
- const second = createService();
- await second.taskIndexRepo.ensureReady();
- await seedIndexedTask(first.taskIndexRepo, { taskId: "late-first", provider: "zcode" });
- expect(await second.taskIndexRepo.listReconciliationRows({ workspacePath: "/repo" })).toEqual(
- [],
- );
- expect(await first.taskIndexRepo.listReconciliationRows({ workspacePath: "/repo" })).toEqual([
- expect.objectContaining({ meta: expect.objectContaining({ taskId: "late-first" }) }),
- ]);
- });
- it("creates fresh CLI identities for explicit imports instead of overwriting a deleted import", async () => {
- const { service, taskIndexRepo } = createService();
- const module = await import("../src/session/claude-native/claudeNativeSessionImportService.js");
- const spy = vi
- .spyOn(module, "importClaudeNativeSessions")
- .mockImplementation(async (params) => {
- const source = {
- provider: "claude" as const,
- workspacePath: "/repo",
- sessionId: "external",
- sourcePath: "/fixture.jsonl",
- title: "Imported",
- createdAt: 1,
- updatedAt: 2,
- messages: [{ role: "user" as const, content: "hello", timestamp: 1 }],
- };
- const first = await params.createImportedSession(source);
- await taskIndexRepo.applyCliDeletion({ workspacePath: "/repo", taskId: first.taskId });
- const second = await params.createImportedSession(source);
- expect(second.taskId).not.toBe(first.taskId);
- expect(await taskIndexRepo.getTaskMeta(first)).toBeNull();
- return { imported: [], failed: [], skipped: [] };
- });
- try {
- await service.importClaudeSessions({ workspacePath: "/repo", sessionIds: ["external"] });
- } finally {
- spy.mockRestore();
- }
- });
- it.each([undefined, "ssh://host/repo"])(
- "waits for an in-flight topic admission before confirming cancellation (%s)",
- async (workspaceIdentity) => {
- const { service, agent } = createService();
- const task = await service.createTask({
- workspacePath: "/repo",
- workspaceIdentity,
- provider: "glm",
- });
- const target = { taskId: task.taskId, workspacePath: "/repo", workspaceIdentity };
- let release!: () => void;
- agent.sendConversationCommandV4 = vi.fn(async () => {
- await new Promise<void>((resolve) => {
- release = resolve;
- });
- return { commandId: "topic-inflight", status: "accepted", revisionAtDecision: 1 };
- });
- const sending = service.submitBotGroupInput!({
- ...target,
- commandId: "topic-inflight",
- content: "read",
- source: {
- provider: "feishu",
- botId: "bot",
- chatId: "chat",
- threadId: "topic",
- messageId: "message",
- senderId: "owner",
- senderName: "Owner",
- },
- });
- await vi.waitFor(() => expect(release).toBeTypeOf("function"));
- let cancelled = false;
- const cancellation = service.invalidateBotTopicInputs!(target).then(() => {
- cancelled = true;
- });
- await flushMicrotasks();
- const completedBeforeAdmission = cancelled;
- // 总是释放挂起请求,失败断言也不能遗留正在写入临时任务的异步工作。
- release();
- await Promise.all([sending, cancellation]);
- expect(completedBeforeAdmission).toBe(false);
- expect(cancelled).toBe(true);
- },
- );
- it("does not admit an uploaded topic input after cancellation", async () => {
- const { service, agent } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- let finish!: () => void;
- agent.attachmentBeginV4 = vi.fn(async (p) => ({
- uploadId: p.uploadId,
- state: "staging" as const,
- nextChunkIndex: 0,
- }));
- agent.attachmentChunkV4 = vi.fn(async (p) => ({ uploadId: p.uploadId, nextChunkIndex: 1 }));
- agent.attachmentCommitV4 = vi.fn(async (p) => {
- await new Promise<void>((resolve) => {
- finish = resolve;
- });
- return { uploadId: p.uploadId, ref: "artifact://prepared" };
- });
- const sending = service.submitBotGroupInput!({
- taskId: task.taskId,
- workspacePath: "/repo",
- commandId: "topic-upload",
- content: "read",
- attachments: [
- { kind: "file", filename: "input.txt", textContent: "material", mimeType: "text/plain" },
- ],
- source: {
- provider: "feishu",
- botId: "bot",
- chatId: "chat",
- threadId: "topic",
- messageId: "message",
- senderId: "owner",
- senderName: "Owner",
- },
- });
- await vi.waitFor(() => expect(finish).toBeTypeOf("function"));
- await service.invalidateBotTopicInputs!({ taskId: task.taskId, workspacePath: "/repo" });
- finish();
- await expect(sending).rejects.toThrow(/cancelled/);
- expect(agent.sendConversationCommandV4).not.toHaveBeenCalled();
- });
- it("uploads Bot attachments and admits input on the same trusted connection", async () => {
- const { service, agent } = createService();
- const task = await service.createTask({
- workspacePath: "/repo",
- workspaceIdentity: "ssh://host/repo",
- provider: "glm",
- });
- const connections: string[] = [];
- const check = (params: unknown) => {
- const connection = readTrustedZCodeAgentV4Connection(params);
- if (!connection) throw new Error("fault.attachment.connectionUntrusted");
- expect(connection.clientMode).toBe("desktop-continuous");
- expect(params).toEqual(
- expect.objectContaining({
- workspaceIdentity: "ssh://host/repo",
- remoteSessionId: "current-remote",
- }),
- );
- connections.push(connection.connectionId);
- };
- agent.attachmentBeginV4 = vi.fn(async (params) => {
- check(params);
- return { uploadId: params.uploadId, state: "staging" as const, nextChunkIndex: 0 };
- });
- agent.attachmentChunkV4 = vi.fn(async (params) => {
- check(params);
- return { uploadId: params.uploadId, nextChunkIndex: 1 };
- });
- agent.attachmentCommitV4 = vi.fn(async (params) => {
- check(params);
- return { uploadId: params.uploadId, ref: "artifact://bot-file" };
- });
- agent.attachmentAbortV4 = vi.fn(async () => undefined);
- agent.sendConversationCommandV4 = vi.fn(async (params) => {
- check(params);
- expect(params.envelope.payload).toEqual(
- expect.objectContaining({
- attachments: [expect.objectContaining({ ref: "artifact://bot-file" })],
- }),
- );
- return {
- commandId: params.envelope.commandId,
- status: "accepted" as const,
- revisionAtDecision: 1,
- result: {
- type: "inputAccepted" as const,
- inputId: params.envelope.commandId,
- delivery: "queue" as const,
- },
- };
- });
- await service.submitBotGroupInput!({
- taskId: task.taskId,
- workspacePath: "/repo",
- workspaceIdentity: "ssh://host/repo",
- remoteSessionId: "current-remote",
- commandId: "group-with-file",
- content: "Read this file",
- attachments: [
- { kind: "file", filename: "example.txt", mimeType: "text/plain", textContent: "hello" },
- ],
- source: {
- provider: "feishu",
- botId: "bot",
- chatId: "chat",
- senderId: "member",
- messageId: "message",
- },
- });
- expect(connections).toHaveLength(4);
- expect(new Set(connections).size).toBe(1);
- });
- it("keeps trusted remote session routing on group admission and rejects a foreign workspace before IO", async () => {
- const { service, agent } = createService();
- const task = await service.createTask({
- workspacePath: "/repo",
- workspaceIdentity: "ssh://host/repo",
- provider: "glm",
- });
- agent.sendConversationCommandV4 = vi.fn(async (params) => ({
- commandId: params.envelope.commandId,
- status: "accepted" as const,
- revisionAtDecision: 1,
- result: {
- type: "inputAccepted" as const,
- inputId: params.envelope.commandId,
- delivery: "queue" as const,
- },
- }));
- const input = {
- taskId: task.taskId,
- workspacePath: "/repo",
- workspaceIdentity: "ssh://host/repo",
- remoteSessionId: "current-remote",
- commandId: "group-command",
- content: "hello",
- source: {
- provider: "feishu" as const,
- botId: "bot",
- chatId: "group",
- senderId: "member",
- senderName: "Member",
- messageId: "message",
- threadId: "omt_source",
- rootMessageId: "om_root",
- },
- };
- vi.mocked(readBotGroupRuntimeSnapshot).mockClear();
- vi.mocked(agent.sendConversationCommandV4).mockImplementation(async (params) => {
- // 冷启动时先恢复同一任务,不能依赖 Desktop 曾经打开过它。
- expect(readBotGroupRuntimeSnapshot).toHaveBeenCalledWith(expect.any(Object), {
- workspacePath: "/repo",
- workspaceIdentity: "ssh://host/repo",
- remoteSessionId: "current-remote",
- sessionId: task.taskId,
- });
- return {
- commandId: params.envelope.commandId,
- status: "accepted",
- revisionAtDecision: 1,
- result: { type: "inputAccepted", inputId: params.envelope.commandId, delivery: "queue" },
- };
- });
- await service.submitBotGroupInput!(input);
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({
- workspaceIdentity: "ssh://host/repo",
- remoteSessionId: "current-remote",
- envelope: expect.objectContaining({
- payload: expect.objectContaining({
- requestedDelivery: "startNow",
- botDeliveryTarget: expect.objectContaining({
- threadId: "omt_source",
- rootMessageId: "om_root",
- }),
- }),
- }),
- }),
- );
- await expect(
- service.submitBotGroupInput!({ ...input, workspaceIdentity: "ssh://other/repo" }),
- ).rejects.toThrow("workspace mismatch");
- await expect(
- service.getBotGroupTaskBlockReason!({
- taskId: task.taskId,
- workspacePath: "/repo",
- workspaceIdentity: "ssh://other/repo",
- }),
- ).rejects.toThrow("workspace mismatch");
- expect(agent.sendConversationCommandV4).toHaveBeenCalledOnce();
- vi.mocked(readBotGroupRuntimeSnapshot).mockRejectedValueOnce(new Error("restore failed"));
- await expect(
- service.submitBotGroupInput!({ ...input, commandId: "cold-failure" }),
- ).rejects.toThrow("restore failed");
- expect(agent.sendConversationCommandV4).toHaveBeenCalledOnce();
- await service.readBotTopicSummaries!({
- taskId: task.taskId,
- workspacePath: "/repo",
- workspaceIdentity: "ssh://host/repo",
- remoteSessionId: "current-remote",
- });
- expect(agent.readSessionMessages).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: task.taskId,
- workspaceIdentity: "ssh://host/repo",
- remoteSessionId: "current-remote",
- }),
- );
- await expect(
- service.readBotTopicSummaries!({
- taskId: task.taskId,
- workspacePath: "/repo",
- workspaceIdentity: "ssh://other/repo",
- }),
- ).rejects.toThrow("workspace mismatch");
- });
- it("exposes task ready only after the indexed session returns to an input-ready phase", async () => {
- const { indexFrameEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const readyReasons: string[] = [];
- const disposable = service.onDynamicTaskReady(task.taskId)((event) => {
- readyReasons.push(event.reason);
- });
- try {
- await flushMicrotasks();
- fireIndexUpsert(indexFrameEmitter, compatSummary("another-task", "running"));
- fireIndexUpsert(indexFrameEmitter, compatSummary("another-task", "completedSuccess"));
- fireIndexUpsert(indexFrameEmitter, compatSummary(task.taskId, "running"));
- fireIndexUpsert(indexFrameEmitter, compatSummary(task.taskId, "completedSuccess"));
- fireIndexUpsert(indexFrameEmitter, compatSummary(task.taskId, "running"));
- fireIndexUpsert(indexFrameEmitter, compatSummary(task.taskId, "error"));
- await flushMicrotasks();
- expect(readyReasons).toEqual(["prompt_completed", "prompt_failed"]);
- } finally {
- disposable.dispose();
- }
- });
- it("reports the manual automation runId on terminal outcome", async () => {
- const { indexFrameEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const outcomes: Array<{ inputId?: string; outcome: string }> = [];
- const disposable = service.onDynamicTaskTerminalOutcome(task.taskId)((event) => {
- outcomes.push(event);
- });
- const runId = "automation-1:manual:1700000000000";
- try {
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: runId,
- content: "run scheduled report",
- automationId: "automation-1",
- });
- await flushMicrotasks();
- fireIndexUpsert(indexFrameEmitter, compatSummary(task.taskId, "running"));
- fireIndexUpsert(indexFrameEmitter, compatSummary(task.taskId, "completedSuccess"));
- await flushMicrotasks();
- expect(outcomes).toEqual([expect.objectContaining({ inputId: runId, outcome: "succeeded" })]);
- } finally {
- disposable.dispose();
- }
- });
- it("keeps automation mutation isolation turn-scoped when creating an execution task", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const task = await service.createTask({
- workspacePath: "/repo",
- provider: "glm",
- automationId: "automation-1",
- });
- const createParams = vi.mocked(agent.createSession).mock.calls[0]?.[0];
- expect(createParams).toEqual(
- expect.objectContaining({
- workspacePath: "/repo",
- persistence: "deferred",
- titleGenerationEnabled: false,
- }),
- );
- expect(createParams).not.toHaveProperty("toolDenylist");
- await expect(
- taskIndexRepo.getTaskMeta({
- workspacePath: "/repo",
- taskId: task.taskId,
- }),
- ).resolves.toEqual(expect.objectContaining({ cronAutomationId: "automation-1" }));
- });
- it("preserves an explicitly empty frozen directory list across adapter create and resume", async () => {
- const { agent, service } = createService();
- await service.createTask({
- workspacePath: "/repo",
- projectId: "p",
- additionalDirectories: [],
- v4Create: true,
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({
- projectId: "p",
- envelope: expect.objectContaining({
- payload: expect.objectContaining({ additionalDirectories: [] }),
- }),
- }),
- );
- const nativeTask = await service.createTask({
- workspacePath: "/repo",
- projectId: "p",
- additionalDirectories: [],
- });
- expect(agent.createSession).toHaveBeenCalledWith(
- expect.objectContaining({ projectId: "p", additionalDirectories: [] }),
- );
- await service.resumeTask({
- workspacePath: "/repo",
- taskId: nativeTask.taskId,
- additionalDirectories: [],
- });
- expect(agent.resumeSession).toHaveBeenCalledWith(
- expect.objectContaining({ additionalDirectories: [] }),
- );
- });
- it("SAT28: 显式恢复 child 只返回详情,不创建主列表索引", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const snapshot = makeSnapshot({ sessionId: "hidden-child", workspacePath: "/repo" });
- snapshot.session.sessionKind = "subagent_child";
- vi.mocked(agent.resumeSession).mockResolvedValue(snapshot);
- await service.resumeTask({ workspacePath: "/repo", taskId: "hidden-child" });
- expect(
- await taskIndexRepo.getTaskMeta({ workspacePath: "/repo", taskId: "hidden-child" }),
- ).toBeNull();
- });
- it("keeps automation mutation isolation turn-scoped when resuming an execution task", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.mocked(agent.resumeSession).mockClear();
- await service.resumeTask({
- workspacePath: "/repo",
- taskId: task.taskId,
- automationId: "automation-2",
- });
- const resumeParams = vi.mocked(agent.resumeSession).mock.calls[0]?.[0];
- expect(resumeParams).toEqual(
- expect.objectContaining({
- workspacePath: "/repo",
- sessionId: task.taskId,
- }),
- );
- expect(resumeParams).not.toHaveProperty("toolDenylist");
- await expect(
- taskIndexRepo.getTaskMeta({
- workspacePath: "/repo",
- taskId: task.taskId,
- }),
- ).resolves.toEqual(expect.objectContaining({ cronAutomationId: "automation-2" }));
- });
- it("marks automation execution prompts on v4 sendText commands", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.mocked(agent.sendConversationCommandV4).mockClear();
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "trace-automation-run",
- content: "run scheduled prompt",
- automationId: "automation-1",
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({
- envelope: expect.objectContaining({
- type: "sendText",
- commandId: "trace-automation-run",
- payload: {
- text: "run scheduled prompt",
- heldQueueDisposition: "keepQueueAndSend",
- automationId: "automation-1",
- toolDisallowlist: ["CronCreate", "CronUpdate", "CronDelete"],
- },
- }),
- }),
- );
- });
- it("applies a caller-scoped CronCreate denylist to a first-run prompt", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.mocked(agent.sendConversationCommandV4).mockClear();
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "trace-off-peak-first-run",
- content: "run off-peak prompt",
- offPeakTaskId: "offpeak-stable-1",
- offPeakRunType: "init",
- toolDenylist: ["CronCreate"],
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({
- envelope: expect.objectContaining({
- type: "sendText",
- commandId: "trace-off-peak-first-run",
- payload: {
- text: "run off-peak prompt",
- heldQueueDisposition: "keepQueueAndSend",
- offPeakTaskId: "offpeak-stable-1",
- offPeakRunType: "init",
- // D49-2:闲时派发轮在调用方 CronCreate 之外,纵深补 OffPeakCreate
- // (免费池自我放大);OffPeakList 只读保留。
- toolDisallowlist: ["CronCreate", "OffPeakCreate"],
- },
- }),
- }),
- );
- await expect(
- taskIndexRepo.getTaskMeta({
- workspacePath: "/repo",
- taskId: task.taskId,
- }),
- ).resolves.toEqual(expect.objectContaining({ cronAutomationId: undefined }));
- });
- it("applies a caller-scoped CronCreate denylist after resuming a normal session", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.resumeTask({ workspacePath: "/repo", taskId: task.taskId });
- vi.mocked(agent.sendConversationCommandV4).mockClear();
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "trace-off-peak-resume",
- content: "continue off-peak prompt",
- offPeakTaskId: "offpeak-stable-1",
- offPeakRunType: "resume",
- toolDenylist: ["CronCreate"],
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({
- envelope: expect.objectContaining({
- type: "sendText",
- commandId: "trace-off-peak-resume",
- payload: {
- text: "continue off-peak prompt",
- heldQueueDisposition: "keepQueueAndSend",
- offPeakTaskId: "offpeak-stable-1",
- offPeakRunType: "resume",
- // D49-2:续跑段与首跑同语义。
- toolDisallowlist: ["CronCreate", "OffPeakCreate"],
- },
- }),
- }),
- );
- });
- it("restores automation mutation tools for a user turn in a cron-owned session", async () => {
- const { agent, service } = createService();
- // 会话因用户在其中创建过定时任务而归属 automation:DB meta 带 cronAutomationId。
- const task = await service.createTask({
- workspacePath: "/repo",
- provider: "glm",
- automationId: "automation-owned",
- });
- // 复刻打开已有定时任务会话:归属仍保存在 DB,但普通用户轮不携带 automationId。
- await service.resumeTask({ workspacePath: "/repo", taskId: task.taskId });
- service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })(() => undefined);
- vi.mocked(agent.sendConversationCommandV4).mockClear();
- // 普通用户后续输入:不带 automationId。
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "trace-followup",
- content: "再帮我建一个每5分钟的提醒",
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({
- envelope: expect.objectContaining({
- type: "sendText",
- commandId: "trace-followup",
- payload: {
- text: "再帮我建一个每5分钟的提醒",
- heldQueueDisposition: "keepQueueAndSend",
- },
- }),
- }),
- );
- });
- it("does not hide CronCreate for plain user turns in a normal (non cron-owned) session", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.resumeTask({ workspacePath: "/repo", taskId: task.taskId });
- service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })(() => undefined);
- vi.mocked(agent.sendConversationCommandV4).mockClear();
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "trace-normal",
- content: "帮我看看这段代码",
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({
- envelope: expect.objectContaining({
- type: "sendText",
- commandId: "trace-normal",
- payload: {
- text: "帮我看看这段代码",
- heldQueueDisposition: "keepQueueAndSend",
- },
- }),
- }),
- );
- });
- it("injects product CUA helper broker config before legacy createTask session creation", async () => {
- // services 只负责建会话前把 MCP 配置交给 producer 的 resolver;改写细节由 producer 自己的测试覆盖。
- // 这里用测试自己的 resolver,不依赖私有 producer 的注入实现(开源占位包不改写配置)。
- const resolver = {
- resolveMcpServers: vi.fn(async (servers) =>
- servers?.map((server) =>
- "command" in server
- ? {
- ...server,
- args: [
- ...(server.args ?? []),
- "--permission-broker-socket",
- "/tmp/zcode-cua/helper.sock",
- ],
- }
- : server,
- ),
- ),
- } satisfies CuaProductMcpServerResolver;
- const { agent, service } = createService({ cuaProductMcpServerResolver: resolver });
- await service.createTask({
- workspacePath: "/repo",
- provider: "glm",
- mcpServers: [
- {
- name: ZCODE_CUA_OFFICIAL_MCP_NAMESPACE_NAME,
- command: "uvx",
- args: ["zcode-cua"],
- env: [
- { name: ZCODE_PLUGIN_ID_ENV_KEY, value: ZCODE_CUA_OFFICIAL_PLUGIN_ID },
- { name: ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY, value: "test-authority" },
- ],
- },
- ],
- });
- const createParams = vi.mocked(agent.createSession).mock.calls[0]?.[0];
- const server = createParams?.mcpServers?.[0];
- expect(resolver.resolveMcpServers).toHaveBeenCalledTimes(1);
- expect(server && "command" in server ? server.args : []).toEqual([
- "zcode-cua",
- "--permission-broker-socket",
- "/tmp/zcode-cua/helper.sock",
- ]);
- });
- it("restarts only the current workspace agent process", async () => {
- const { agent, service } = createService();
- await service.restartWorkspaceProcess({
- workspacePath: "/repo",
- workspaceIdentity: "remote:ssh:dev:/repo",
- provider: "glm",
- });
- // Bugfix: restartWorkspaceProcess 是切模型/配置后的 workspace 级重启,
- // 不能调用 disposeAll 把整个 agent manager 永久关闭。
- expect(agent.disposeWorkspace).toHaveBeenCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: "remote:ssh:dev:/repo",
- });
- expect(agent.disposeAll).not.toHaveBeenCalled();
- });
- it("releases workspace preparation through the workspace-scoped agent lifecycle", async () => {
- const { agent, service } = createService();
- await service.releaseWorkspacePreparation({
- workspacePath: "/repo",
- workspaceIdentity: "remote:wsl:ubuntu:/repo",
- provider: "glm",
- });
- expect(agent.disposeWorkspace).toHaveBeenCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: "remote:wsl:ubuntu:/repo",
- });
- expect(agent.disposeAll).not.toHaveBeenCalled();
- });
- it("uses structured ModelSelection for a custom model", async () => {
- const { agent, service } = createService();
- const workspacePath = "/repo-custom-model-ref";
- const task = await service.createTask({ workspacePath, provider: "glm" });
- await service.setModel({
- taskId: task.taskId,
- traceId: "trace_custom_model_ref",
- modelSelection: { providerId: "provider-b", modelId: "gpt-5.5" },
- });
- expect(agent.setModel).toHaveBeenCalledWith({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId: task.taskId,
- model: { providerId: "provider-b", modelId: "gpt-5.5" },
- });
- });
- it("records agent model network status events as HTTP network telemetry", async () => {
- const observations: NetworkObservation[] = [];
- setNetworkTelemetrySink((observation) => observations.push(observation));
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })(() => undefined);
- try {
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt-model-completed",
- sessionId: task.taskId,
- seq: 1,
- timestamp: 1_700_000_000_000,
- type: "session.updated",
- payload: {
- type: "model_request_completed",
- timestamp: "2026-06-05T00:00:00.000Z",
- traceId: "trace-model-network",
- requestId: "request-model-network",
- model: { providerId: "glm", modelId: "glm-4.6" },
- baseURL: "https://api.example.com/v1?token=secret",
- providerKind: "openai-compatible",
- transport: "sse",
- attempt: 2,
- maxAttempts: 3,
- durationMs: 456,
- },
- },
- });
- } finally {
- disposable.dispose();
- }
- expect(observations).toEqual([
- {
- transport: "http",
- interface: "zcode_agent.model.openai-compatible.sse.api.example.com/v1",
- durationMs: 456,
- ok: true,
- attempt: 2,
- },
- ]);
- });
- it("projects user input response to elicitation response in dynamic task fallback subscriptions", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const streamEvents: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- streamEvents.push(event);
- });
- try {
- eventEmitter.fire({
- type: "userInput.response",
- requestId: "input-1",
- response: {
- action: "accept",
- content: { answers: { answer_0: "a" } },
- },
- });
- } finally {
- disposable.dispose();
- }
- expect(streamEvents).toContainEqual(
- expect.objectContaining({
- type: "elicitation_response",
- taskId: task.taskId,
- requestId: "input-1",
- action: "accept",
- content: { answers: { answer_0: "a" } },
- }),
- );
- });
- it("projects structured turn failure codes into dynamic task errors", async () => {
- const { eventEmitter, service, taskIndexRepo } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const streamEvents: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- streamEvents.push(event);
- });
- try {
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_turn_failed",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 1,
- timestamp: 10,
- traceId: "protocol_trace_1",
- type: "turn.failed",
- payload: {
- turnPhase: "model",
- inputId: "input_failed_1",
- error: {
- 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",
- },
- },
- },
- },
- });
- } finally {
- disposable.dispose();
- }
- expect(streamEvents).toContainEqual(
- expect.objectContaining({
- type: "task_error",
- taskId: task.taskId,
- traceId: "input_failed_1",
- inputId: "input_failed_1",
- error: "Model provider is not configured: stale-provider",
- code: "provider_not_found",
- detail: "stale-provider/gpt-5.5",
- attribution: {
- source: "runtime",
- reason: "provider_not_configured",
- providerId: "account:zai-individual-coding-plan",
- },
- }),
- );
- // 旧 stream 适配只负责转发错误,不能作为第二个 task index 写者。
- expect(
- await taskIndexRepo.getTaskMeta({ workspacePath: "/repo", taskId: task.taskId }),
- ).toMatchObject({ updatedAt: task.updatedAt, title: task.title });
- });
- it("projects streaming Write input in dynamic task fallback subscriptions", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const streamEvents: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- streamEvents.push(event);
- });
- try {
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_turn_started",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 1,
- timestamp: 10,
- traceId: "protocol_trace_1",
- type: "turn.started",
- payload: {
- input: "write file",
- inputId: "input_write_1",
- turnNumber: 1,
- },
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_tool_input_delta_1",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 2,
- timestamp: 11,
- traceId: "protocol_trace_1",
- type: "model.streaming",
- payload: {
- kind: "tool_input_delta",
- toolCallId: "call_write",
- delta: '{"file_path":"src/app.ts","content":"line 1',
- },
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_tool_input_delta_2",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 3,
- timestamp: 12,
- traceId: "protocol_trace_1",
- type: "model.streaming",
- payload: {
- kind: "tool_input_delta",
- toolCallId: "call_write",
- delta: "\\nline 2",
- },
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_tool_call_final",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 4,
- timestamp: 13,
- traceId: "protocol_trace_1",
- type: "model.streaming",
- payload: {
- kind: "tool_call",
- toolCallId: "call_write",
- toolName: "Write",
- input: {
- file_path: "src/app.ts",
- content: "line 1\nline 2",
- },
- },
- },
- });
- } finally {
- disposable.dispose();
- }
- const runStarted = streamEvents.find((event) => event.type === "task_run_started");
- const toolUpdates = streamEvents.filter(
- (event): event is Extract<ZCodeStreamEvent, { type: "tool_call_update" }> =>
- event.type === "tool_call_update",
- );
- expect(runStarted).toMatchObject({
- type: "task_run_started",
- traceId: "input_write_1",
- inputId: "input_write_1",
- });
- expect(toolUpdates.at(-1)).toMatchObject({
- type: "tool_call_update",
- traceId: "input_write_1",
- inputId: "input_write_1",
- toolId: "call_write",
- toolName: "Write",
- kind: "Write",
- title: "Write",
- input: {
- file_path: "src/app.ts",
- content: "line 1\nline 2",
- },
- });
- });
- it("keeps background Agent launch acknowledgements in progress in dynamic task streams", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const streamEvents: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- streamEvents.push(event);
- });
- try {
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_parent_agent",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 1,
- timestamp: 10,
- traceId: "trace_session",
- type: "tool.updated",
- payload: {
- kind: "scheduled",
- toolCallId: "call_parent_agent",
- toolName: "Agent",
- input: {
- description: "Inspect workspace",
- prompt: "Find the project shape",
- run_in_background: true,
- },
- },
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_parent_agent_result",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 2,
- timestamp: 11,
- traceId: "trace_session",
- type: "tool.updated",
- payload: {
- kind: "result",
- toolCallId: "call_parent_agent",
- duration: 4,
- result: {
- success: true,
- content: [
- 'Agent Explore task "Inspect workspace" started in background.',
- "agentId: agent_1",
- "backgroundTaskId: agent_1",
- "Runtime will wait for this background Agent before completing the current turn.",
- ].join("\n"),
- },
- },
- },
- });
- } finally {
- disposable.dispose();
- }
- expect(streamEvents).toContainEqual(
- expect.objectContaining({
- type: "tool_call_update",
- toolId: "call_parent_agent",
- toolName: "Agent",
- status: "in_progress",
- }),
- );
- });
- it("keeps previous and current async Task launch acknowledgements in progress in dynamic task streams", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const streamEvents: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- streamEvents.push(event);
- });
- try {
- const acknowledgementContents = [
- [
- 'Agent general-purpose task "Inspect workspace" started in background.',
- "agentId: agent_1",
- "outputFile: /tmp/agent_1/output.txt",
- "You will be notified when the Agent completes.",
- ].join("\n"),
- [
- "Async agent launched successfully.",
- "agentId: agent_1 (internal ID - do not mention to user. Use SendMessage with to: 'agent_1' to continue this agent.)",
- "The agent is working in the background. You will be notified automatically when it completes.",
- "output_file: /tmp/agent_1/output.txt",
- "Do NOT Read or tail this file via the shell tool.",
- ].join("\n"),
- ];
- for (const [index, content] of acknowledgementContents.entries()) {
- const toolCallId = `call_parent_task_${index}`;
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: `evt_parent_task_${index}`,
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: index * 2 + 1,
- timestamp: 10 + index * 2,
- traceId: "trace_session",
- type: "tool.updated",
- payload: {
- kind: "scheduled",
- toolCallId,
- toolName: "Task",
- input: {
- description: "Inspect workspace",
- prompt: "Find the project shape",
- },
- },
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: `evt_parent_task_result_${index}`,
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: index * 2 + 2,
- timestamp: 11 + index * 2,
- traceId: "trace_session",
- type: "tool.updated",
- payload: {
- kind: "result",
- toolCallId,
- duration: 4,
- result: {
- success: true,
- content,
- },
- },
- },
- });
- }
- } finally {
- disposable.dispose();
- }
- for (let index = 0; index < 2; index++) {
- expect(streamEvents).toContainEqual(
- expect.objectContaining({
- type: "tool_call_update",
- toolId: `call_parent_task_${index}`,
- toolName: "Task",
- status: "in_progress",
- }),
- );
- }
- });
- // M5 ③ 删除:listWorkspaceTaskLists(workspace 行分组查询)——列表面(本机 + remote shard)
- // 已切 sessions-index,分组结果在客户端由 sessions + tasks-index 归属 join 构建。
- it("broadcasts task_created when createTask inserts a new indexed task", async () => {
- const { service } = createService();
- const events: Array<{ reason?: string; taskId?: string }> = [];
- const disposable = service.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) => {
- if (event.type === "workspace_task_list_changed") {
- events.push({ reason: event.reason, taskId: event.taskMeta?.taskId });
- }
- });
- await service.createTask({ workspacePath: "/repo", provider: "glm" });
- disposable.dispose();
- expect(events).toEqual([
- {
- reason: "task_created",
- taskId: "sess_1",
- },
- ]);
- });
- it("broadcasts explicit task list membership reasons for pin and archive actions", async () => {
- const { service } = createService();
- const events: Array<{ reason?: string; taskId?: string }> = [];
- const disposable = service.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) => {
- if (event.type === "workspace_task_list_changed") {
- events.push({ reason: event.reason, taskId: event.taskMeta?.taskId });
- }
- });
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.setTaskPinned({
- workspacePath: "/repo",
- taskId: task.taskId,
- pinned: true,
- });
- await service.setTaskPinned({
- workspacePath: "/repo",
- taskId: task.taskId,
- pinned: false,
- });
- await service.archiveTask({
- workspacePath: "/repo",
- taskId: task.taskId,
- });
- await service.unarchiveTask({
- workspacePath: "/repo",
- taskId: task.taskId,
- });
- disposable.dispose();
- expect(events).toEqual([
- { reason: "task_created", taskId: task.taskId },
- { reason: "task_pinned", taskId: task.taskId },
- { reason: "task_unpinned", taskId: task.taskId },
- { reason: "task_archived", taskId: task.taskId },
- { reason: "task_unarchived", taskId: task.taskId },
- ]);
- });
- it("does not clear a newer unread marker when the system clock does not advance", async () => {
- vi.useFakeTimers();
- const { service, taskIndexRepo } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.setSystemTime(100);
- const [openedUnread, terminalUnread] = await Promise.all([
- service.setTaskUnread({
- workspacePath: "/repo",
- taskId: task.taskId,
- unread: true,
- }),
- service.setTaskUnread({
- workspacePath: "/repo",
- taskId: task.taskId,
- unread: true,
- }),
- ]);
- expect(openedUnread.unreadAt).toBe(100);
- expect(terminalUnread.unreadAt).toBe(101);
- await expect(
- service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- }),
- ).resolves.toEqual(
- expect.objectContaining({ meta: expect.objectContaining({ unreadAt: 101 }) }),
- );
- const events: string[] = [];
- const disposable = service.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) => {
- if (event.type === "workspace_task_list_changed") {
- events.push(event.reason);
- }
- });
- const result = await service.setTaskUnread({
- workspacePath: "/repo",
- taskId: task.taskId,
- unread: false,
- expectedUnreadAt: openedUnread.unreadAt,
- });
- expect(result.unreadAt).toBe(terminalUnread.unreadAt);
- await expect(
- taskIndexRepo.getTaskMeta({
- workspacePath: "/repo",
- taskId: task.taskId,
- }),
- ).resolves.toEqual(expect.objectContaining({ unreadAt: terminalUnread.unreadAt }));
- expect(events).toEqual([]);
- await service.setTaskUnread({
- workspacePath: "/repo",
- taskId: task.taskId,
- unread: false,
- expectedUnreadAt: terminalUnread.unreadAt,
- });
- events.length = 0;
- const unreadAfterClear = await service.setTaskUnread({
- workspacePath: "/repo",
- taskId: task.taskId,
- unread: true,
- });
- expect(unreadAfterClear.unreadAt).toBe(102);
- events.length = 0;
- const staleClearAfterNewUnread = await service.setTaskUnread({
- workspacePath: "/repo",
- taskId: task.taskId,
- unread: false,
- expectedUnreadAt: terminalUnread.unreadAt,
- });
- disposable.dispose();
- expect(staleClearAfterNewUnread.unreadAt).toBe(unreadAfterClear.unreadAt);
- expect(events).toEqual([]);
- });
- it("clears the unread marker when expectedUnreadAt still matches", async () => {
- vi.useFakeTimers();
- vi.setSystemTime(100);
- const { service, taskIndexRepo } = createService();
- await seedIndexedTask(taskIndexRepo, {
- taskId: "matching-read-clear",
- provider: "glm",
- });
- const openedUnread = await service.setTaskUnread({
- workspacePath: "/repo",
- taskId: "matching-read-clear",
- unread: true,
- });
- const events: string[] = [];
- const disposable = service.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) => {
- if (event.type === "workspace_task_list_changed") {
- events.push(event.reason);
- }
- });
- const result = await service.setTaskUnread({
- workspacePath: "/repo",
- taskId: "matching-read-clear",
- unread: false,
- expectedUnreadAt: openedUnread.unreadAt,
- });
- disposable.dispose();
- expect(result.unreadAt).toBeUndefined();
- const persisted = await taskIndexRepo.getTaskMeta({
- workspacePath: "/repo",
- taskId: "matching-read-clear",
- });
- expect(persisted?.unreadAt).toBeUndefined();
- expect(events).toEqual(["task_meta_changed"]);
- });
- it("keeps task facts unchanged when CLI rejects rename or deletion", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.mocked(agent.sendConversationCommandV4).mockRejectedValue(new Error("offline"));
- await expect(service.renameTask({ ...task, title: "must not persist" })).rejects.toThrow(
- "offline",
- );
- await expect(service.deleteTask(task)).rejects.toThrow("offline");
- expect(await taskIndexRepo.getTaskMeta(task)).toMatchObject({
- title: task.title,
- updatedAt: task.updatedAt,
- });
- expect(await service.listDeletedTaskIds({ workspacePath: "/repo" })).toEqual([]);
- });
- it("deletes through CLI before mirroring its durable tombstone", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const events: Array<{ reason?: string; taskId?: string }> = [];
- const disposable = service.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) => {
- if (event.type === "workspace_task_list_changed") {
- events.push({ reason: event.reason, taskId: event.taskId ?? event.taskMeta?.taskId });
- }
- });
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.archiveTask({ workspacePath: "/repo", taskId: task.taskId });
- vi.mocked(agent.sendConversationCommandV4).mockClear();
- agent.reconcileSessionsIndexV4 = vi.fn(async () => ({
- sessions: [
- {
- sessionId: task.taskId,
- exists: true as const,
- deleted: true,
- title: task.title,
- titleSource: "default" as const,
- createdAt: task.createdAt,
- updatedAt: task.updatedAt,
- },
- ],
- }));
- await service.deleteTask({ workspacePath: "/repo", taskId: task.taskId });
- disposable.dispose();
- await expect(service.listDeletedTaskIds({ workspacePath: "/repo" })).resolves.toEqual([
- task.taskId,
- ]);
- await expect(service.listArchivedTasks({ workspacePath: "/repo" })).resolves.toEqual([]);
- await expect(
- taskIndexRepo.listTaskMetas({
- workspacePath: "/repo",
- provider: "glm",
- archived: true,
- includeDeleted: true,
- }),
- ).resolves.toEqual([expect.objectContaining({ taskId: task.taskId })]);
- expect(events.at(-1)).toEqual({ reason: "task_deleted", taskId: task.taskId });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({ envelope: expect.objectContaining({ type: "deleteSession" }) }),
- );
- });
- it("renames through CLI while preserving CLI activity time", async () => {
- vi.useFakeTimers();
- vi.setSystemTime(new Date("2026-06-13T14:45:00.000Z"));
- const { agent, service, taskIndexRepo } = createService();
- await seedIndexedTask(taskIndexRepo, {
- taskId: "running-rename",
- provider: "glm",
- workspacePath: "/repo",
- updatedAt: 100,
- });
- vi.mocked(agent.sendConversationCommandV4).mockClear();
- agent.reconcileSessionsIndexV4 = vi.fn(async () => ({
- sessions: [
- {
- sessionId: "running-rename",
- exists: true as const,
- deleted: false,
- title: "短名",
- titleSource: "custom" as const,
- createdAt: 1,
- updatedAt: 100,
- },
- ],
- }));
- const renamed = await service.renameTask({
- taskId: "running-rename",
- workspacePath: "/repo",
- title: "短名",
- });
- expect(renamed.title).toBe("短名");
- expect(renamed.titleOverridden).toBe(true);
- expect(renamed.updatedAt).toBe(100);
- await expect(
- taskIndexRepo.getTaskMeta({
- taskId: "running-rename",
- workspacePath: "/repo",
- }),
- ).resolves.toMatchObject({
- title: "短名",
- titleOverridden: true,
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- envelope: expect.objectContaining({
- type: "renameSession",
- sessionId: "running-rename",
- payload: { title: "短名" },
- }),
- });
- });
- it("reads the bound Session original selection without filtering unavailable model options", async () => {
- const { agent, service } = createService();
- const workspaceIdentity = "remote:ssh:dev:/repo";
- const task = await service.createTask({
- workspacePath: "/repo",
- workspaceIdentity,
- provider: "glm",
- });
- const original = {
- providerId: "account:bigmodel-individual-coding-plan",
- modelId: "removed-model",
- options: { reasoningLevel: "removed-level" },
- };
- const snapshot = makeSnapshot({
- sessionId: task.taskId,
- workspacePath: "/repo",
- workspaceIdentity,
- model: original,
- });
- snapshot.settings.model.available = [];
- snapshot.settings.thoughtLevel.current = undefined;
- vi.mocked(agent.readSession).mockResolvedValueOnce(snapshot);
- vi.mocked(agent.setModel).mockClear();
- expect(await service.getTaskModelSelection({ taskId: task.taskId })).toEqual(original);
- expect(agent.readSession).toHaveBeenLastCalledWith({
- workspacePath: "/repo",
- workspaceIdentity,
- sessionId: task.taskId,
- });
- expect(agent.setModel).not.toHaveBeenCalled();
- });
- it("creates a Bot draft with the native v4 createSession command", async () => {
- const { agent, service } = createService();
- const mcpServers = [{ name: "docs", command: "node", args: ["server.js"], env: [] }];
- const task = await service.createTask({
- workspacePath: "/repo",
- provider: "glm",
- model: "glm/deepseek-v4-flash",
- mcpServers,
- v4Create: true,
- });
- expect(agent.createSession).not.toHaveBeenCalled();
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- projectId: null,
- envelope: expect.objectContaining({
- sessionId: null,
- type: "createSession",
- payload: expect.objectContaining({
- workspaceId: "/repo",
- config: expect.objectContaining({
- provider: "glm",
- model: "deepseek-v4-flash",
- }),
- mcpServers,
- }),
- }),
- });
- expect(task.taskId).toBe("sess_v4_created");
- });
- it("desktop-attached remote Bot draft 只发送 ModelSelection", async () => {
- const { agent, service } = createService();
- await service.createTask({
- workspacePath: "/repo",
- workspaceIdentity: "remote:ssh:dev:/repo",
- provider: "glm",
- model: "alternate-provider/deepseek-v4-pro",
- v4Create: true,
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({
- workspaceIdentity: "remote:ssh:dev:/repo",
- envelope: expect.objectContaining({
- type: "createSession",
- payload: expect.objectContaining({
- config: expect.objectContaining({
- provider: "alternate-provider",
- model: "deepseek-v4-pro",
- }),
- }),
- }),
- }),
- );
- const command = vi.mocked(agent.sendConversationCommandV4).mock.calls.at(-1)?.[0];
- expect(command?.envelope.payload).not.toHaveProperty("runtimeModel");
- });
- it("uses deferred persistence and disables generated titles only for automation sessions", async () => {
- const { agent, service } = createService();
- await service.createTask({
- workspacePath: "/repo",
- provider: "glm",
- automationId: "automation_1",
- });
- expect(agent.createSession).toHaveBeenLastCalledWith(
- expect.objectContaining({
- persistence: "deferred",
- titleGenerationEnabled: false,
- }),
- );
- await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const regularCreate = vi.mocked(agent.createSession).mock.calls.at(-1)?.[0];
- expect(regularCreate).not.toHaveProperty("titleGenerationEnabled");
- expect(regularCreate).not.toHaveProperty("persistence");
- });
- it("uses deferred persistence without cron metadata for headless first-prompt sessions", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({
- workspacePath: "/repo",
- provider: "glm",
- deferPersistenceUntilFirstPrompt: true,
- });
- expect(agent.createSession).toHaveBeenLastCalledWith(
- expect.objectContaining({
- persistence: "deferred",
- }),
- );
- expect(vi.mocked(agent.createSession).mock.calls.at(-1)?.[0]).not.toHaveProperty(
- "titleGenerationEnabled",
- );
- expect(task.cronAutomationId).toBeUndefined();
- });
- it("drains an accepted mobile task command after the current turn completes", async () => {
- const { agent, eventEmitter, indexFrameEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "replayable",
- })(() => undefined);
- try {
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "input_first",
- queryId: "query_first",
- content: "first",
- });
- await service.enqueueTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_second",
- traceId: "input_second",
- queryId: "query_second",
- type: "send_prompt",
- content: "second",
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledTimes(1);
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_turn_completed",
- sessionId: task.taskId,
- turnId: "turn_first",
- seq: 2,
- timestamp: 100,
- traceId: "protocol_trace_first",
- type: "turn.completed",
- payload: {
- inputId: "input_first",
- response: "done",
- },
- },
- });
- await flushMicrotasks();
- expect(agent.sendConversationCommandV4).toHaveBeenCalledTimes(1);
- await firePromptCompletedState(indexFrameEmitter, {
- sessionId: task.taskId,
- workspacePath: "/repo",
- });
- await flushMicrotasks();
- expect(agent.sendConversationCommandV4).toHaveBeenCalledTimes(2);
- // 幂等键 inputId→commandId:drain 落 v4 sendText,终态收口仍按 traceId 对账。
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- envelope: expect.objectContaining({
- type: "sendText",
- sessionId: task.taskId,
- commandId: "input_second",
- payload: { text: "second", heldQueueDisposition: "keepQueueAndSend" },
- }),
- });
- } finally {
- disposable.dispose();
- }
- });
- it("drains automation host command with CronCreate denied", async () => {
- const { agent, eventEmitter, indexFrameEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "input_first",
- queryId: "query_first",
- content: "first",
- });
- const result = await service.enqueueTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_automation",
- traceId: "automation-parent:manual:run-1",
- queryId: "automation-parent:manual:run-1",
- type: "send_prompt",
- content: "每 5 分钟给我讲个冷笑话",
- automationId: "automation-parent",
- });
- expect(result.command).toEqual(
- expect.objectContaining({
- automationId: "automation-parent",
- status: "accepted",
- }),
- );
- expect(agent.sendConversationCommandV4).toHaveBeenCalledTimes(1);
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_turn_completed",
- sessionId: task.taskId,
- turnId: "turn_first",
- seq: 2,
- timestamp: 100,
- traceId: "protocol_trace_first",
- type: "turn.completed",
- payload: {
- inputId: "input_first",
- response: "done",
- },
- },
- });
- await flushMicrotasks();
- await firePromptCompletedState(indexFrameEmitter, {
- sessionId: task.taskId,
- workspacePath: "/repo",
- });
- await flushMicrotasks();
- expect(agent.sendConversationCommandV4).toHaveBeenCalledTimes(2);
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- envelope: expect.objectContaining({
- type: "sendText",
- sessionId: task.taskId,
- commandId: "automation-parent:manual:run-1",
- payload: {
- text: "每 5 分钟给我讲个冷笑话",
- heldQueueDisposition: "keepQueueAndSend",
- automationId: "automation-parent",
- toolDisallowlist: ["CronCreate", "CronUpdate", "CronDelete"],
- },
- }),
- });
- });
- it("drains a mobile task command when the session syncer observes the ready event", async () => {
- const { agent, eventEmitter, indexFrameEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "input_first",
- queryId: "query_first",
- content: "first",
- });
- await service.enqueueTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_second",
- traceId: "input_second",
- queryId: "query_second",
- type: "send_prompt",
- content: "second",
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledTimes(1);
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_turn_completed",
- sessionId: task.taskId,
- turnId: "turn_first",
- seq: 2,
- timestamp: 100,
- traceId: "protocol_trace_first",
- type: "turn.completed",
- payload: {
- inputId: "input_first",
- response: "done",
- },
- },
- });
- await flushMicrotasks();
- expect(agent.sendConversationCommandV4).toHaveBeenCalledTimes(1);
- await firePromptCompletedState(indexFrameEmitter, {
- sessionId: task.taskId,
- workspacePath: "/repo",
- });
- await flushMicrotasks();
- expect(agent.sendConversationCommandV4).toHaveBeenCalledTimes(2);
- // 幂等键 inputId→commandId:drain 落 v4 sendText,终态收口仍按 traceId 对账。
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- envelope: expect.objectContaining({
- type: "sendText",
- sessionId: task.taskId,
- commandId: "input_second",
- payload: { text: "second", heldQueueDisposition: "keepQueueAndSend" },
- }),
- });
- });
- it("rejects a stale mobile enqueue command for an older owner run", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "input_current",
- queryId: "query_current",
- content: "current",
- });
- await expect(
- service.enqueueTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_stale",
- traceId: "input_stale_queued",
- queryId: "query_stale_queued",
- type: "send_prompt",
- content: "stale queued",
- ownerRunId: "input_old",
- }),
- ).rejects.toMatchObject({
- code: "STALE_TASK_OWNER_COMMAND",
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledTimes(1);
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- clientMode: "web-remote-replayable",
- });
- expect(snapshot?.runtime?.pendingCommands).toEqual([]);
- });
- it("rejects a stale mobile promote command for an older owner run", async () => {
- const { service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "input_current",
- queryId: "query_current",
- content: "current",
- });
- await service.enqueueTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_second",
- traceId: "input_second",
- queryId: "query_second",
- type: "send_prompt",
- content: "second",
- ownerRunId: "input_current",
- });
- await expect(
- service.promoteTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_second",
- ownerRunId: "input_old",
- clientMode: "web-remote-replayable",
- }),
- ).rejects.toMatchObject({
- code: "STALE_TASK_OWNER_COMMAND",
- });
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- clientMode: "web-remote-replayable",
- });
- expect(snapshot?.runtime?.pendingCommands).toEqual([
- expect.objectContaining({
- commandId: "queued_second",
- status: "accepted",
- traceId: "input_second",
- }),
- ]);
- });
- it("cancels an accepted mobile task command before it drains", async () => {
- const { agent, indexFrameEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "input_current",
- queryId: "query_current",
- content: "current",
- });
- await service.enqueueTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_second",
- traceId: "input_second",
- queryId: "query_second",
- type: "send_prompt",
- content: "second",
- ownerRunId: "input_current",
- });
- const result = await service.cancelTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_second",
- ownerRunId: "input_current",
- clientMode: "web-remote-replayable",
- });
- expect(result).toEqual(
- expect.objectContaining({
- canceled: true,
- commandId: "queued_second",
- status: "accepted",
- }),
- );
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- clientMode: "web-remote-replayable",
- });
- expect(snapshot?.runtime?.pendingCommands).toEqual([]);
- await firePromptCompletedState(indexFrameEmitter, {
- sessionId: task.taskId,
- workspacePath: "/repo",
- });
- await flushMicrotasks();
- expect(agent.sendConversationCommandV4).toHaveBeenCalledTimes(1);
- });
- it("keeps a running mobile task command when cancel races with drain", async () => {
- const { service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.enqueueTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_running",
- traceId: "input_running",
- queryId: "query_running",
- type: "send_prompt",
- content: "already running",
- });
- await flushMicrotasks();
- const result = await service.cancelTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_running",
- clientMode: "web-remote-replayable",
- });
- expect(result).toEqual(
- expect.objectContaining({
- canceled: false,
- commandId: "queued_running",
- reason: "already_running",
- status: "running",
- }),
- );
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- clientMode: "web-remote-replayable",
- });
- expect(snapshot?.runtime?.pendingCommands).toEqual([
- expect.objectContaining({
- commandId: "queued_running",
- status: "running",
- traceId: "input_running",
- }),
- ]);
- });
- it("keeps mobile clientId on the v4 sendText envelope when draining a host command", async () => {
- const { agent, indexFrameEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "input_first",
- content: "first",
- });
- await service.enqueueTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_second",
- traceId: "input_second",
- type: "send_prompt",
- content: "second",
- clientId: "mobile-client-7",
- });
- await firePromptCompletedState(indexFrameEmitter, {
- sessionId: task.taskId,
- workspacePath: "/repo",
- });
- await flushMicrotasks();
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith(
- expect.objectContaining({
- envelope: expect.objectContaining({
- commandId: "input_second",
- clientId: "mobile-client-7",
- }),
- }),
- );
- });
- it("marks a drained mobile command failed when the v4 sendText ACK is rejected", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.mocked(agent.sendConversationCommandV4).mockResolvedValueOnce({
- commandId: "input_failed",
- status: "failed",
- reasonCode: "fault.command.executionFailed",
- message: "boom",
- revisionAtDecision: 3,
- });
- await service.enqueueTaskCommand({
- workspacePath: "/repo",
- taskId: task.taskId,
- commandId: "queued_failed",
- traceId: "input_failed",
- type: "send_prompt",
- content: "will fail",
- });
- await flushMicrotasks();
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- clientMode: "web-remote-replayable",
- });
- expect(snapshot?.runtime?.pendingCommands).toEqual([
- expect.objectContaining({
- commandId: "queued_failed",
- status: "failed",
- error: expect.stringContaining("fault.command.executionFailed"),
- }),
- ]);
- });
- it("keeps attachment prompts on the legacy session/send fallback", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.mocked(agent.sendConversationCommandV4).mockClear();
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "input_attach",
- content: "see image",
- modelSelection: {
- providerId: "test-provider",
- modelId: "test-model",
- options: { reasoningLevel: "high" },
- },
- modelExecution: { selectionScope: "execution", memoryExtraction: "skip" },
- attachments: [
- { kind: "image", filename: "a.png", mimeType: "image/png", dataBase64: "aGk=" },
- ],
- });
- // 附件命令面未建模(v4 attachmentRef 引用模型缺上传面):带附件输入仍走旧 session/send。
- expect(agent.sendConversationCommandV4).not.toHaveBeenCalled();
- expect(agent.sendPrompt).toHaveBeenCalledWith(
- expect.objectContaining({
- inputId: "input_attach",
- modelSelection: {
- providerId: "test-provider",
- modelId: "test-model",
- options: { reasoningLevel: "high" },
- },
- modelExecution: { selectionScope: "execution", memoryExtraction: "skip" },
- attachments: [expect.objectContaining({ filename: "a.png" })],
- }),
- );
- });
- it("requires authoritative identity before stopping a running topic", async () => {
- const { service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const target = { taskId: task.taskId, workspacePath: "/repo" };
- vi.mocked(readBotGroupRuntimeSnapshot).mockResolvedValueOnce({
- control: { stopState: "idle", activeWorks: [] },
- } as never);
- await expect(service.readBotTopicExecution!(target)).resolves.toBeUndefined();
- vi.mocked(readBotGroupRuntimeSnapshot).mockResolvedValueOnce({
- control: {
- stopState: "stoppable",
- activeWorks: [
- {
- kind: "primaryTurn",
- foregroundExecutionId: "execution",
- sourceCommandId: "input-current",
- },
- ],
- },
- } as never);
- await expect(service.readBotTopicExecution!(target)).resolves.toEqual({
- executionId: "execution",
- sourceCommandId: "input-current",
- });
- vi.mocked(readBotGroupRuntimeSnapshot).mockResolvedValueOnce({
- control: { stopState: "stoppable", activeWorks: [] },
- } as never);
- await expect(service.readBotTopicExecution!(target)).rejects.toThrow("identity is unavailable");
- });
- it("routes topic stop through the same workspace and waits for its execution to end", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- let finish!: () => void;
- vi.mocked(waitBotGroupExecutionEnd).mockImplementationOnce(
- () =>
- new Promise((resolve) => {
- finish = () => resolve({} as never);
- }),
- );
- let completed = false;
- const stopping = service.stopBotTopicExecution!({
- taskId: task.taskId,
- workspacePath: "/repo",
- remoteSessionId: "remote-a",
- executionId: "old",
- }).then(() => {
- completed = true;
- });
- for (let i = 0; i < 8; i++) await Promise.resolve();
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith(
- expect.objectContaining({
- remoteSessionId: "remote-a",
- envelope: expect.objectContaining({
- type: "stop",
- payload: { expectedForegroundExecutionId: "old" },
- }),
- }),
- );
- expect(completed).toBe(false);
- finish();
- await stopping;
- expect(completed).toBe(true);
- });
- it("preserves the observed execution identity in a topic stop", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.stopGeneration({
- taskId: task.taskId,
- expectedForegroundExecutionId: "old-execution",
- });
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith(
- expect.objectContaining({
- envelope: expect.objectContaining({
- type: "stop",
- payload: { expectedForegroundExecutionId: "old-execution" },
- }),
- }),
- );
- });
- it("routes stopGeneration through the v4 stop command", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.stopGeneration({ taskId: task.taskId });
- // M5 ③-4:agentService.stopSession 已删除(v4 stop 命令唯一路径),断言收敛为 v4 面。
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- envelope: expect.objectContaining({
- type: "stop",
- sessionId: task.taskId,
- payload: {},
- }),
- });
- });
- // M5 ③-3:模式切换(手机远控语义入口 setMode/setConfigOption)收敛 v4 switchCollaborationMode。
- it("routes setMode through the v4 switchCollaborationMode command", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.setMode({ taskId: task.taskId, mode: "plan" });
- expect(agent.setMode).not.toHaveBeenCalled();
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- envelope: expect.objectContaining({
- type: "switchCollaborationMode",
- sessionId: task.taskId,
- payload: { mode: "plan" },
- }),
- });
- });
- // M5 ③-3:host 无本地 v4 投影,CAS 首发 baseRevision=0 探测,stale 用 revisionAtDecision 收敛。
- it("retries a stale switchCollaborationMode ACK with the server revision", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.mocked(agent.sendConversationCommandV4).mockResolvedValueOnce({
- commandId: "probe",
- status: "stale",
- reasonCode: "proto.staleRevision",
- revisionAtDecision: 7,
- });
- await service.setMode({ taskId: task.taskId, mode: "yolo" });
- const envelopes = vi
- .mocked(agent.sendConversationCommandV4)
- .mock.calls.filter(([params]) => params.envelope.type === "switchCollaborationMode")
- .map(([params]) => params.envelope);
- expect(envelopes).toHaveLength(2);
- expect(envelopes[0]).toMatchObject({ baseRevision: 0 });
- expect(envelopes[1]).toMatchObject({ baseRevision: 7, payload: { mode: "yolo" } });
- });
- // M5 ③-3 残留保真:v4 switchCollaborationMode 值域刻意排除 auto,auto 继续走旧 op。
- it("keeps mode=auto on the legacy session/setMode fallback", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.setMode({ taskId: task.taskId, mode: "auto" });
- expect(agent.setMode).toHaveBeenCalledWith(
- expect.objectContaining({ sessionId: task.taskId, mode: "auto" }),
- );
- expect(
- vi
- .mocked(agent.sendConversationCommandV4)
- .mock.calls.some(([params]) => params.envelope.type === "switchCollaborationMode"),
- ).toBe(false);
- });
- // M5 ③-3:思考深度收敛 v4 switchModelConfig(thought 字段承载,provider/model 取当前选型)。
- it("routes thought level config through v4 switchModelConfig with the current model", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const configOptions = await service.setConfigOption({
- taskId: task.taskId,
- configId: "thought_level",
- value: "max",
- });
- expect(agent.setThoughtLevel).not.toHaveBeenCalled();
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- envelope: expect.objectContaining({
- type: "switchModelConfig",
- sessionId: task.taskId,
- payload: { provider: "glm", model: "glm-4.6", thought: "max" },
- }),
- });
- expect(configOptions.some((option) => option.id === "thought_level")).toBe(true);
- });
- // M5 ③-3 评估标注保真:setModel 仍走旧 op(v4 switchModelConfig 无 runtimeModel 解析,
- // 迁移会让手机远控的 custom provider 跨切失效)。
- it("keeps setModel on the legacy session/setModel op", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.setModel({
- taskId: task.taskId,
- modelSelection: { providerId: "glm", modelId: "glm-4.7" },
- });
- expect(agent.setModel).toHaveBeenCalledWith(
- expect.objectContaining({ sessionId: task.taskId }),
- );
- expect(
- vi
- .mocked(agent.sendConversationCommandV4)
- .mock.calls.some(([params]) => params.envelope.type === "switchModelConfig"),
- ).toBe(false);
- });
- it("routes local automation selection through V4 commands without Host runtimeModel", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.setAutomationSessionConfig({
- taskId: task.taskId,
- traceId: "automation-run" as TraceId,
- modelSelection: { providerId: "provider-deepseek", modelId: "deepseek-v4-pro" },
- thoughtLevel: "high",
- mode: "edit",
- });
- expect(agent.setModel).not.toHaveBeenCalled();
- expect(agent.setThoughtLevel).not.toHaveBeenCalled();
- const automationCommands = vi
- .mocked(agent.sendConversationCommandV4)
- .mock.calls.map(([params]) => params.envelope)
- .slice(-3);
- expect(automationCommands).toEqual([
- expect.objectContaining({
- type: "switchModelConfig",
- payload: expect.objectContaining({
- provider: "provider-deepseek",
- model: "deepseek-v4-pro",
- thought: "high",
- }),
- }),
- expect.objectContaining({
- type: "switchModelConfig",
- payload: {
- provider: "provider-deepseek",
- model: "deepseek-v4-pro",
- thought: "high",
- },
- }),
- expect.objectContaining({
- type: "switchCollaborationMode",
- payload: { mode: "edit" },
- }),
- ]);
- });
- it("routes desktop-attached remote automation with ModelSelection only", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({
- workspacePath: "/repo",
- workspaceIdentity: "remote:ssh:dev:/repo",
- provider: "glm",
- });
- await service.setAutomationSessionConfig({
- taskId: task.taskId,
- traceId: "remote-automation-run" as TraceId,
- modelSelection: { providerId: "provider-deepseek", modelId: "deepseek-v4-pro" },
- thoughtLevel: "high",
- mode: "edit",
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({
- workspaceIdentity: "remote:ssh:dev:/repo",
- envelope: expect.objectContaining({
- type: "switchModelConfig",
- payload: expect.objectContaining({
- provider: "provider-deepseek",
- model: "deepseek-v4-pro",
- thought: "high",
- }),
- }),
- }),
- );
- const modelCommand = vi
- .mocked(agent.sendConversationCommandV4)
- .mock.calls.map(([params]) => params.envelope)
- .find((envelope) => envelope.type === "switchModelConfig");
- expect(modelCommand?.payload).not.toHaveProperty("runtimeModel");
- });
- it("routes respondPermission through v4 resolveInteraction keyed by requestId", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const submitted = await service.respondPermission({
- taskId: task.taskId,
- requestId: "perm_req_1",
- optionId: "allow_project",
- response: { decision: "allow", reason: "Approved" },
- });
- expect(submitted).toBe(true);
- // M5 ③-4:agentService.respondPermission 已删除(v4 resolveInteraction 唯一路径)。
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- envelope: expect.objectContaining({
- type: "resolveInteraction",
- sessionId: task.taskId,
- payload: {
- interactionId: "perm_req_1",
- answer: { optionId: "allow_project" },
- },
- }),
- });
- });
- it("routes respondElicitation through v4 resolveInteraction with action/content", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const received: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "replayable",
- })((event) => {
- received.push(event);
- });
- try {
- const submitted = await service.respondElicitation({
- taskId: task.taskId,
- requestId: "elicit_req_1",
- action: "accept",
- content: { answers: { "Pick one": "B" } },
- clientMode: "web-remote-replayable",
- });
- expect(submitted).toBe(true);
- expect(agent.sendConversationCommandV4).toHaveBeenLastCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- envelope: expect.objectContaining({
- type: "resolveInteraction",
- sessionId: task.taskId,
- payload: {
- interactionId: "elicit_req_1",
- answer: {
- action: "accept",
- content: { answers: { "Pick one": "B" } },
- },
- },
- }),
- });
- // 语义保真:replayable 应答后 observer 弹窗按 elicitation_response 清理(原
- // respondUserInput 的 web-remote-replayable 分支,现由 adapter 本地补投)。
- expect(received).toContainEqual(
- expect.objectContaining({
- type: "elicitation_response",
- requestId: "elicit_req_1",
- action: "accept",
- }),
- );
- } finally {
- disposable.dispose();
- }
- });
- it("treats a noop resolveInteraction ACK as an idempotent success", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.mocked(agent.sendConversationCommandV4).mockResolvedValueOnce({
- commandId: "any",
- status: "noop",
- reasonCode: "proto.alreadyResolved",
- revisionAtDecision: 9,
- });
- await expect(
- service.respondPermission({
- taskId: task.taskId,
- requestId: "perm_req_late",
- optionId: "deny",
- response: { decision: "deny", reason: "Denied" },
- }),
- ).resolves.toBe(true);
- });
- it("merges live subagent tool projection into terminal ZCode snapshots", async () => {
- const { agent, eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const terminalSnapshot = makeSnapshot({
- sessionId: task.taskId,
- workspacePath: "/repo",
- messages: [
- makeUserMessage({
- sessionId: task.taskId,
- messageId: "msg_user_1",
- text: "加一个地址系统,可以加,可以选",
- }),
- makeAssistantAgentToolMessage({
- sessionId: task.taskId,
- messageId: "msg_assistant_1",
- toolCallId: "call_00_agent",
- }),
- ],
- });
- vi.mocked(agent.resumeSession).mockResolvedValue({
- ...terminalSnapshot,
- session: {
- ...terminalSnapshot.session,
- status: "completed",
- },
- projection: {
- ...terminalSnapshot.projection,
- status: "completed",
- },
- });
- const streamEvents: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- })((event) => {
- streamEvents.push(event);
- });
- try {
- const scheduledEvent: ZCodeSessionEvent = {
- eventId: "evt_subagent_tool_scheduled",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 1,
- traceId: "trace_subagent_tool",
- timestamp: 60,
- type: "tool.updated",
- payload: {
- kind: "scheduled",
- toolCallId: "tool_subagent_read",
- toolName: "Read",
- input: { file_path: "package.json" },
- parentToolCallId: "call_00_agent",
- source: "subagent",
- agentId: "agent_1",
- agentType: "Explore",
- childSessionId: "sess_subagent_agent_1",
- childToolCallId: "call_child_read",
- },
- };
- const resultEvent: ZCodeSessionEvent = {
- eventId: "evt_subagent_tool_result",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 4,
- traceId: "trace_subagent_tool",
- timestamp: 63,
- type: "tool.updated",
- payload: {
- kind: "result",
- toolCallId: "tool_subagent_read",
- parentToolCallId: "call_00_agent",
- source: "subagent",
- agentId: "agent_1",
- agentType: "Explore",
- childSessionId: "sess_subagent_agent_1",
- childToolCallId: "call_child_read",
- result: { content: "read output" },
- duration: 10,
- },
- };
- eventEmitter.fire({ type: "session.event", event: scheduledEvent });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_subagent_tool_progress",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 2,
- traceId: "trace_subagent_tool",
- timestamp: 61,
- type: "tool.updated",
- payload: {
- kind: "started",
- toolCallId: "tool_subagent_read",
- toolName: "Read",
- parentToolCallId: "call_00_agent",
- source: "subagent",
- agentId: "agent_1",
- },
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_parent_turn_completed",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 3,
- traceId: "trace_subagent_tool",
- timestamp: 62,
- type: "turn.completed",
- payload: {
- inputId: "input_parent",
- resultType: "success",
- },
- },
- });
- eventEmitter.fire({ type: "session.event", event: resultEvent });
- expect(streamEvents).toContainEqual(
- expect.objectContaining({
- type: "tool_call_update",
- toolId: "tool_subagent_read",
- toolName: "Read",
- status: "in_progress",
- input: { file_path: "package.json" },
- }),
- );
- expect(streamEvents).toContainEqual(
- expect.objectContaining({
- type: "tool_call_update",
- toolId: "tool_subagent_read",
- parentToolUseId: "call_00_agent",
- toolName: "Read",
- status: "completed",
- input: { file_path: "package.json" },
- }),
- );
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- });
- const tools = snapshot?.messages[1]?.tools ?? [];
- const childTool = tools.find(
- (tool) =>
- (tool.raw as { toolCallId?: string } | undefined)?.toolCallId === "tool_subagent_read",
- );
- expect(tools).toHaveLength(2);
- expect(childTool?.output).toBe("read output");
- expect((childTool?.raw as { parentToolCallId?: string } | undefined)?.parentToolCallId).toBe(
- "call_00_agent",
- );
- } finally {
- disposable.dispose();
- }
- });
- it("attaches hidden task notification metadata to background Agent tools in restored snapshots", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const restored = makeSnapshot({
- sessionId: task.taskId,
- workspacePath: "/repo",
- messages: [
- makeUserMessage({
- sessionId: task.taskId,
- messageId: "msg_user_1",
- text: "启动后台 Agent",
- }),
- {
- info: {
- messageId: "msg_assistant_agent",
- sessionId: task.taskId,
- role: "assistant",
- time: { created: 40, completed: 50 },
- agent: "glm",
- model: { providerId: "glm", modelId: "glm-4.6" },
- },
- parts: [
- {
- partId: "part_agent_tool",
- sessionId: task.taskId,
- messageId: "msg_assistant_agent",
- type: "tool",
- callId: "toolu_background_agent",
- tool: "Agent",
- state: {
- status: "completed",
- input: {
- prompt: "Explore project structure",
- run_in_background: true,
- },
- output:
- "Async agent launched successfully.\noutput_file: /tmp/background-agent.output",
- title: "Agent",
- metadata: {},
- startedAt: 41,
- completedAt: 49,
- },
- },
- ],
- } satisfies ZCodeMessageWithParts,
- makeUserMessage({
- sessionId: task.taskId,
- messageId: "msg_task_notification",
- text: [
- "<task-notification>",
- "<task-id>agent_1</task-id>",
- "<tool-use-id>toolu_background_agent</tool-use-id>",
- "<output-file>/tmp/background-agent.output</output-file>",
- "<status>completed</status>",
- "<result>Background Agent restored result</result>",
- "</task-notification>",
- ].join("\n"),
- parts: [
- {
- partId: "part_task_notification",
- sessionId: task.taskId,
- messageId: "msg_task_notification",
- type: "text",
- synthetic: true,
- metadata: { source: "background_task" },
- text: [
- "<task-notification>",
- "<task-id>agent_1</task-id>",
- "<tool-use-id>toolu_background_agent</tool-use-id>",
- "<output-file>/tmp/background-agent.output</output-file>",
- "<status>completed</status>",
- "<result>Background Agent restored result</result>",
- "</task-notification>",
- ].join("\n"),
- },
- ],
- }),
- ],
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(restored);
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- });
- expect(snapshot?.messages).toHaveLength(2);
- expect(snapshot?.messages[1]?.tools?.[0]?.raw).toMatchObject({
- _meta: {
- zcode: {
- taskNotification: {
- outputFile: "/tmp/background-agent.output",
- result: "Background Agent restored result",
- status: "completed",
- taskId: "agent_1",
- },
- },
- },
- });
- });
- it.each([
- {
- expectedError: BACKGROUND_AGENT_PROVIDER_ERROR,
- expectedStatus: "failed" as const,
- notificationStatus: "failed",
- },
- {
- expectedError: undefined,
- expectedStatus: "completed" as const,
- notificationStatus: "stopped",
- },
- ])(
- "restores a $notificationStatus background Agent notification without changing unrelated launch semantics",
- async ({ expectedError, expectedStatus, notificationStatus }) => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const notificationSummary = `Agent general-purpose task "Review" ${notificationStatus}.`;
- const notificationText = [
- "<task-notification>",
- "<task-id>agent_failed</task-id>",
- "<tool-use-id>toolu_background_agent_failed</tool-use-id>",
- "<output-file>/tmp/background-agent-failed.output</output-file>",
- `<status>${notificationStatus}</status>`,
- `<summary>${notificationSummary.replaceAll('"', """)}</summary>`,
- ...(expectedError ? [`<error>${expectedError}</error>`] : []),
- "</task-notification>",
- ].join("\n");
- const restored = makeSnapshot({
- sessionId: task.taskId,
- workspacePath: "/repo",
- messages: [
- makeUserMessage({
- sessionId: task.taskId,
- messageId: "msg_user_failed",
- text: "启动会失败的后台 Agent",
- }),
- {
- info: {
- messageId: "msg_assistant_failed_agent",
- sessionId: task.taskId,
- role: "assistant",
- time: { created: 40, completed: 50 },
- agent: "glm",
- model: { providerId: "glm", modelId: "glm-4.6" },
- },
- parts: [
- {
- partId: "part_failed_agent_tool",
- sessionId: task.taskId,
- messageId: "msg_assistant_failed_agent",
- type: "tool",
- callId: "toolu_background_agent_failed",
- tool: "Agent",
- state: {
- status: "completed",
- input: {
- prompt: "Review the workspace",
- run_in_background: true,
- },
- output:
- "Async agent launched successfully.\noutput_file: /tmp/background-agent-failed.output",
- title: "Agent",
- metadata: {},
- startedAt: 41,
- completedAt: 49,
- },
- },
- ],
- } satisfies ZCodeMessageWithParts,
- makeUserMessage({
- sessionId: task.taskId,
- messageId: "msg_failed_task_notification",
- text: notificationText,
- parts: [
- {
- partId: "part_failed_task_notification",
- sessionId: task.taskId,
- messageId: "msg_failed_task_notification",
- type: "text",
- synthetic: true,
- metadata: { source: "background_task" },
- text: notificationText,
- },
- ],
- }),
- ],
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(restored);
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- });
- const restoredTool = snapshot?.messages[1]?.tools?.[0];
- expect(restoredTool).toMatchObject({
- status: expectedStatus,
- raw: {
- _meta: {
- zcode: {
- taskNotification: {
- status: notificationStatus,
- summary: notificationSummary,
- taskId: "agent_failed",
- },
- },
- },
- },
- });
- expect(restoredTool?.error).toBe(expectedError);
- },
- );
- it("projects live task notification turns back onto matching background Agent tools", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const streamEvents: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- })((event) => {
- streamEvents.push(event);
- });
- try {
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_task_notification_started",
- sessionId: task.taskId,
- turnId: "turn_task_notification",
- seq: 1,
- traceId: "trace_task_notification",
- timestamp: 70,
- type: "turn.started",
- payload: {
- input: [
- "<task-notification>",
- "<task-id>agent_1</task-id>",
- "<tool-use-id>toolu_background_agent</tool-use-id>",
- "<output-file>/tmp/background-agent.output</output-file>",
- "<status>completed</status>",
- "<summary>Agent general-purpose task "Review" completed.</summary>",
- "<result>Background Agent live result</result>",
- "</task-notification>",
- ].join("\n"),
- inputSource: "task-notification",
- inputVisibility: "model-only",
- },
- },
- });
- expect(streamEvents).toContainEqual(
- expect.objectContaining({
- type: "tool_call_update",
- taskId: task.taskId,
- toolId: "toolu_background_agent",
- status: "completed",
- content: "Background Agent live result",
- raw: expect.objectContaining({
- _meta: expect.objectContaining({
- zcode: expect.objectContaining({
- taskNotification: {
- outputFile: "/tmp/background-agent.output",
- result: "Background Agent live result",
- status: "completed",
- summary: 'Agent general-purpose task "Review" completed.',
- taskId: "agent_1",
- },
- }),
- }),
- toolCallId: "toolu_background_agent",
- }),
- }),
- );
- } finally {
- disposable.dispose();
- }
- });
- it("projects failed live task notifications with the provider error on matching Agent tools", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const streamEvents: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- })((event) => {
- streamEvents.push(event);
- });
- try {
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_failed_task_notification_started",
- sessionId: task.taskId,
- turnId: "turn_failed_task_notification",
- seq: 1,
- traceId: "trace_failed_task_notification",
- timestamp: 80,
- type: "turn.started",
- payload: {
- input: [
- "<task-notification>",
- "<task-id>agent_failed</task-id>",
- "<tool-use-id>toolu_background_agent_failed</tool-use-id>",
- "<output-file>/tmp/background-agent-failed.output</output-file>",
- "<status>failed</status>",
- `<summary>${BACKGROUND_AGENT_FAILED_SUMMARY.replaceAll('"', """)}</summary>`,
- `<error>${BACKGROUND_AGENT_PROVIDER_ERROR}</error>`,
- "</task-notification>",
- ].join("\n"),
- inputSource: "task-notification",
- inputVisibility: "model-only",
- },
- },
- });
- } finally {
- disposable.dispose();
- }
- const failedUpdate = streamEvents.find(
- (event): event is Extract<ZCodeStreamEvent, { type: "tool_call_update" }> =>
- event.type === "tool_call_update" && event.toolId === "toolu_background_agent_failed",
- );
- expect(failedUpdate).toMatchObject({
- type: "tool_call_update",
- taskId: task.taskId,
- toolId: "toolu_background_agent_failed",
- status: "failed",
- error: BACKGROUND_AGENT_PROVIDER_ERROR,
- raw: {
- _meta: {
- zcode: {
- taskNotification: {
- error: BACKGROUND_AGENT_PROVIDER_ERROR,
- status: "failed",
- summary: BACKGROUND_AGENT_FAILED_SUMMARY,
- taskId: "agent_failed",
- },
- },
- },
- },
- });
- });
- it("merges live background job updates with restored replayable snapshot jobs", async () => {
- const { agent, eventEmitter, service } = createService();
- const workspacePath = "/repo";
- const workspaceIdentity = "remote:ssh:dev:/repo";
- const task = await service.createTask({
- workspacePath,
- workspaceIdentity,
- provider: "glm",
- });
- const restored = makeSnapshot({
- sessionId: task.taskId,
- workspacePath,
- workspaceIdentity,
- backgroundJobs: [
- {
- type: "background_bash",
- taskId: "bash_a",
- command: "sleep 1",
- status: "running",
- background: true,
- },
- {
- type: "background_bash",
- taskId: "bash_b",
- command: "sleep 2",
- status: "running",
- background: true,
- },
- ],
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(restored);
- const snapshot = await service.getTaskSnapshot({
- workspacePath,
- workspaceIdentity,
- taskId: task.taskId,
- });
- expect(snapshot?.runtime?.backgroundBashJobs?.map((job) => job.jobId)).toEqual([
- "bash_a",
- "bash_b",
- ]);
- const streamEvents: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath,
- workspaceIdentity,
- taskId: task.taskId,
- deliveryKind: "replayable",
- })((event) => {
- streamEvents.push(event);
- });
- try {
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_background_job_update",
- sessionId: task.taskId,
- seq: 1,
- timestamp: 70,
- type: "session.updated",
- payload: {
- type: "background_bash",
- taskId: "bash_a",
- command: "sleep 1",
- status: "completed",
- background: true,
- },
- },
- });
- const backgroundUpdate = streamEvents.findLast(
- (event): event is Extract<ZCodeStreamEvent, { type: "background_bash_jobs_update" }> =>
- event.type === "background_bash_jobs_update",
- );
- expect(backgroundUpdate?.jobs.map((job) => [job.jobId, job.status])).toEqual([
- ["bash_a", "completed"],
- ["bash_b", "running"],
- ]);
- } finally {
- disposable.dispose();
- }
- });
- it("filters non-glm legacy task rows in sqlite-backed lists", async () => {
- const { service, taskIndexRepo } = createService();
- await seedIndexedTask(taskIndexRepo, { taskId: "glm_active", provider: "glm" });
- await seedIndexedTask(taskIndexRepo, { taskId: "claude_active", provider: "claude" });
- await seedIndexedTask(taskIndexRepo, {
- taskId: "claude_imported",
- provider: "claude",
- migrationSource: "claudeCode",
- });
- await seedIndexedTask(taskIndexRepo, {
- taskId: "glm_pinned",
- provider: "glm",
- pinned: true,
- });
- await seedIndexedTask(taskIndexRepo, {
- taskId: "claude_pinned",
- provider: "claude",
- pinned: true,
- });
- await seedIndexedTask(taskIndexRepo, {
- taskId: "claude_imported_pinned",
- provider: "claude",
- pinned: true,
- migrationSource: "claudeCode",
- });
- const active = await service.listTasks({ workspacePath: "/repo" });
- const pinned = await service.listPinnedTasks({ workspacePath: "/repo" });
- const pinnedIds = await service.listPinnedTaskIds();
- const taskList = await service.listTaskList({
- kind: "active",
- workspaceScopes: [{ workspacePath: "/repo" }],
- sortBy: "updated",
- });
- // 历史 provider 仍按兼容列表 API 的过滤约定读取。
- expect(active.map((task) => task.taskId)).toEqual(["glm_active"]);
- expect(pinned.map((task) => task.taskId)).toEqual(["glm_pinned"]);
- expect(pinnedIds).toEqual(["glm_pinned"]);
- expect(taskList.items.map((task) => task.taskId)).toEqual(["glm_active"]);
- expect(taskList.total).toBe(1);
- // 项目树曾绕过原列表的 provider 边界,导致能展示却无法经 Controller 操作。
- const tree = (await taskIndexRepo.getProjectRepository()).readTree();
- expect(tree.tasks.map((task) => task.taskId).sort()).toEqual(["glm_active", "glm_pinned"]);
- expect(tree.tasks.filter((task) => task.pinned).map((task) => task.taskId)).toEqual([
- "glm_pinned",
- ]);
- });
- it("does not include zero ZCode Protocol context usage in task snapshots", async () => {
- const { service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- });
- expect(snapshot?.runtime?.contextUsage).toBeUndefined();
- });
- it("uses runtime context usage when restored projection has no usage yet", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const restored = makeSnapshot({
- sessionId: task.taskId,
- workspacePath: "/repo",
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce({
- ...restored,
- projection: {
- ...restored.projection,
- contextUsed: 0,
- },
- runtime: {
- ...restored.runtime,
- contextUsage: {
- used: 12345,
- size: 128000,
- cost: null,
- },
- },
- });
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- });
- expect(snapshot?.runtime?.contextUsage).toEqual({
- used: 12345,
- size: 128000,
- cost: null,
- });
- });
- it("prefers structured projection error code in restored task snapshots", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const restored = makeSnapshot({
- sessionId: task.taskId,
- workspacePath: "/repo",
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce({
- ...restored,
- session: {
- ...restored.session,
- status: "error",
- },
- projection: {
- ...restored.projection,
- status: "error",
- lastError: {
- type: "MODEL_ERROR",
- code: "provider_not_found",
- message: "Model provider is not configured: stale-provider",
- detail: "stale-provider/gpt-5.5",
- },
- },
- });
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- });
- expect(snapshot?.meta.lastError).toEqual({
- code: "provider_not_found",
- detail: "stale-provider/gpt-5.5",
- message: "Model provider is not configured: stale-provider",
- });
- });
- it("restores protocol user file parts as task message attachments", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const imageBase64 = "QUJD";
- const videoBase64 = "REVG";
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(
- makeSnapshot({
- sessionId: task.taskId,
- workspacePath: "/repo",
- messages: [
- makeUserMessage({
- sessionId: task.taskId,
- messageId: "msg_with_attachments",
- text: "inspect attachments",
- parts: [
- {
- partId: "msg_with_attachments_text",
- sessionId: task.taskId,
- messageId: "msg_with_attachments",
- type: "text",
- text: "inspect attachments",
- },
- {
- partId: "msg_with_attachments_file",
- sessionId: task.taskId,
- messageId: "msg_with_attachments",
- type: "file",
- mime: "text/plain",
- filename: "notes.txt",
- url: "/repo/notes.txt",
- metadata: {
- originalUrl: "/repo/notes.txt",
- preview: {
- text: "hello world",
- truncated: false,
- },
- sizeBytes: 11,
- storageKind: "inline",
- },
- },
- {
- partId: "msg_with_attachments_image",
- sessionId: task.taskId,
- messageId: "msg_with_attachments",
- type: "file",
- mime: "image/png",
- filename: "diagram.png",
- url: `data:image/png;base64,${imageBase64}`,
- metadata: {
- originalUrl: "/repo/diagram.png",
- sizeBytes: 3,
- storageKind: "inline",
- },
- },
- {
- partId: "msg_with_attachments_video",
- sessionId: task.taskId,
- messageId: "msg_with_attachments",
- type: "file",
- mime: "video/mp4",
- filename: "demo.mp4",
- url: `data:video/mp4;base64,${videoBase64}`,
- metadata: {
- originalUrl: "/repo/demo.mp4",
- sizeBytes: 3,
- storageKind: "inline",
- },
- },
- ],
- }),
- ],
- }),
- );
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- });
- expect(snapshot?.messages[0]).toMatchObject({
- role: "user",
- content: "inspect attachments",
- attachments: [
- {
- kind: "file",
- filename: "notes.txt",
- mimeType: "text/plain",
- sizeBytes: 11,
- localPath: "/repo/notes.txt",
- },
- {
- kind: "image",
- filename: "diagram.png",
- mimeType: "image/png",
- sizeBytes: 3,
- dataBase64: imageBase64,
- localPath: "/repo/diagram.png",
- },
- {
- kind: "video",
- filename: "demo.mp4",
- mimeType: "video/mp4",
- sizeBytes: 3,
- dataBase64: videoBase64,
- localPath: "/repo/demo.mp4",
- },
- ],
- });
- });
- it("backfills indexed model hint for web remote replayable snapshot resume", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const taskId = "remote_deepseek_task";
- const workspacePath = "/repo";
- await seedIndexedTask(taskIndexRepo, {
- taskId,
- workspacePath,
- provider: "glm",
- model: "default-deepseek/deepseek-v4-flash",
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(
- makeSnapshot({
- sessionId: taskId,
- workspacePath,
- }),
- );
- await service.getTaskSnapshot({
- workspacePath,
- taskId,
- clientMode: "web-remote-replayable",
- });
- expect(agent.resumeSession).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId: taskId,
- model: {
- providerId: "default-deepseek",
- modelId: "deepseek-v4-flash",
- },
- }),
- );
- });
- it("does not backfill indexed model hint for desktop continuous snapshot resume", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const taskId = "desktop_deepseek_task";
- const workspacePath = "/repo";
- await seedIndexedTask(taskIndexRepo, {
- taskId,
- workspacePath,
- provider: "glm",
- model: "default-deepseek/deepseek-v4-flash",
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(
- makeSnapshot({
- sessionId: taskId,
- workspacePath,
- }),
- );
- await service.getTaskSnapshot({
- workspacePath,
- taskId,
- clientMode: "desktop-continuous",
- });
- expect(vi.mocked(agent.resumeSession).mock.calls.at(-1)?.[0]?.model).toBeUndefined();
- });
- it("backfills indexed model hint when resumeTask omits model", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const taskId = "resume_deepseek_task";
- const workspacePath = "/repo";
- await seedIndexedTask(taskIndexRepo, {
- taskId,
- workspacePath,
- provider: "glm",
- model: "default-deepseek/deepseek-v4-flash",
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(
- makeSnapshot({
- sessionId: taskId,
- workspacePath,
- }),
- );
- await service.resumeTask({
- workspacePath,
- taskId,
- });
- expect(agent.resumeSession).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId: taskId,
- model: {
- providerId: "default-deepseek",
- modelId: "deepseek-v4-flash",
- },
- }),
- );
- });
- it("stamps offPeakTaskId on normal snapshot resume (D48)", async () => {
- // SG-01:legacy 分支已补写闲时标记,正常 snapshot 续跑路径必须同语义,
- // 否则 pre-D48 会话被闲时续跑后月亮标识与系统分组归属不会出现。
- const { agent, service, taskIndexRepo } = createService();
- const taskId = "resume_offpeak_stamp_task";
- const workspacePath = "/repo";
- await seedIndexedTask(taskIndexRepo, { taskId, workspacePath, provider: "glm" });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(
- makeSnapshot({
- sessionId: taskId,
- workspacePath,
- }),
- );
- const meta = await service.resumeTask({
- workspacePath,
- taskId,
- offPeakTaskId: "offpeak-resume-stamp",
- });
- expect(meta.offPeakTaskId).toBe("offpeak-resume-stamp");
- const indexed = await taskIndexRepo.getTaskMeta({ workspacePath, taskId });
- expect(indexed?.offPeakTaskId).toBe("offpeak-resume-stamp");
- });
- it("backfills indexed thought level when resumeTask omits thought level", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const taskId = "resume_glm52_task";
- const workspacePath = "/repo";
- await seedIndexedTask(taskIndexRepo, {
- taskId,
- workspacePath,
- provider: "glm",
- model: "bigmodel-api/GLM-5.2",
- thoughtLevel: "high",
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(
- makeSnapshot({
- sessionId: taskId,
- workspacePath,
- }),
- );
- await service.resumeTask({
- workspacePath,
- taskId,
- });
- expect(agent.resumeSession).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId: taskId,
- model: {
- providerId: "bigmodel-api",
- modelId: "GLM-5.2",
- },
- thoughtLevel: "high",
- }),
- );
- });
- it("backfills indexed thought level for web replayable snapshot resume", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const taskId = "replayable_glm52_task";
- const workspacePath = "/repo";
- await seedIndexedTask(taskIndexRepo, {
- taskId,
- workspacePath,
- provider: "glm",
- model: "bigmodel-api/GLM-5.2",
- thoughtLevel: "high",
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(
- makeSnapshot({
- sessionId: taskId,
- workspacePath,
- }),
- );
- await service.getTaskSnapshot({
- workspacePath,
- taskId,
- clientMode: "web-remote-replayable",
- });
- expect(agent.resumeSession).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId: taskId,
- model: {
- providerId: "bigmodel-api",
- modelId: "GLM-5.2",
- },
- thoughtLevel: "high",
- }),
- );
- });
- it("skips indexed model backfill for web replayable ui-resolved snapshot restore", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const taskId = "replayable_readonly_unavailable_model";
- const workspacePath = "/repo";
- await seedIndexedTask(taskIndexRepo, {
- taskId,
- workspacePath,
- provider: "glm",
- model: "account:bigmodel-individual-coding-plan/GLM-5.2",
- thoughtLevel: "max",
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(
- makeSnapshot({
- sessionId: taskId,
- workspacePath,
- }),
- );
- await service.getTaskSnapshot({
- workspacePath,
- taskId,
- clientMode: "web-remote-replayable",
- resumeModelPolicy: "ui-resolved-only",
- } as Parameters<typeof service.getTaskSnapshot>[0]);
- const resumeParams = vi.mocked(agent.resumeSession).mock.calls[0]?.[0];
- expect(resumeParams).toMatchObject({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId: taskId,
- model: undefined,
- });
- expect(resumeParams?.thoughtLevel).toBeUndefined();
- });
- it("marks idle protocol snapshots with completed assistant turns as completed tasks", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const restored = makeSnapshot({
- sessionId: task.taskId,
- workspacePath: "/repo",
- messages: [
- makeUserMessage({
- sessionId: task.taskId,
- messageId: "msg_user_done",
- text: "继续执行",
- }),
- makeAssistantAgentToolMessage({
- sessionId: task.taskId,
- messageId: "msg_assistant_done",
- toolCallId: "tool_done",
- }),
- ],
- });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce({
- ...restored,
- session: {
- ...restored.session,
- status: "idle",
- },
- projection: {
- ...restored.projection,
- status: "idle",
- pendingPermissions: [],
- activeToolCalls: [],
- },
- runtime: {
- ...restored.runtime,
- pendingRequestIds: [],
- },
- });
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- });
- expect(snapshot?.meta.status).toBe("completed");
- });
- it("marks protocol task snapshots when messageLimit trims older history", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.mocked(agent.resumeSession).mockResolvedValueOnce(
- makeSnapshot({
- sessionId: task.taskId,
- workspacePath: "/repo",
- messages: [
- makeUserMessage({
- sessionId: task.taskId,
- messageId: "message_1",
- text: "message 1",
- }),
- makeUserMessage({
- sessionId: task.taskId,
- messageId: "message_2",
- text: "message 2",
- }),
- makeUserMessage({
- sessionId: task.taskId,
- messageId: "message_3",
- text: "message 3",
- }),
- ],
- }),
- );
- const snapshot = await service.getTaskSnapshot({
- workspacePath: "/repo",
- taskId: task.taskId,
- messageLimit: 2,
- });
- expect(snapshot?.messages.map((message) => message.content)).toEqual([
- "message 2",
- "message 3",
- ]);
- expect(snapshot?.history?.truncatedBefore).toBe(true);
- expect(snapshot?.history?.totalMessages).toBe(3);
- });
- it("restores legacy v2 task snapshots when the protocol session is missing", async () => {
- const { agent, service } = createService();
- const workspacePath = "/repo";
- const taskId = "legacy_task";
- const snapshotPath = getLegacyTaskSessionSnapshotPath(workspacePath, taskId);
- mkdirSync(dirname(snapshotPath), { recursive: true });
- writeFileSync(
- snapshotPath,
- JSON.stringify({
- meta: {
- taskId,
- traceId: "trace_legacy",
- title: "Legacy ACP task",
- workspacePath,
- createdAt: 10,
- updatedAt: 20,
- mode: "yolo",
- provider: "glm",
- },
- messages: [
- {
- role: "user",
- content: "old prompt",
- timestamp: 10,
- },
- ],
- }),
- "utf-8",
- );
- vi.mocked(agent.resumeSession).mockRejectedValue(new Error(`Session not found: ${taskId}`));
- const snapshot = await service.getTaskSnapshot({ workspacePath, taskId });
- expect(snapshot?.meta).toMatchObject({
- taskId,
- title: "Legacy ACP task",
- workspacePath,
- status: "completed",
- });
- expect(snapshot?.messages.map((message) => message.content)).toEqual(["old prompt"]);
- vi.mocked(agent.onDynamicSessionEvent).mockClear();
- const disposable = service.onDynamicTaskEvent({
- workspacePath,
- taskId,
- deliveryKind: "continuous",
- })(() => undefined);
- disposable.dispose();
- expect(agent.onDynamicSessionEvent).not.toHaveBeenCalled();
- });
- it("upgrades legacy imported Claude snapshots to protocol sessions before model switching", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const workspacePath = "/repo";
- const taskId = "claude-import-590ccc622ab6db6bd9f1d7bf";
- const snapshotPath = getLegacyTaskSessionSnapshotPath(workspacePath, taskId);
- mkdirSync(dirname(snapshotPath), { recursive: true });
- writeFileSync(
- snapshotPath,
- JSON.stringify({
- meta: {
- taskId,
- traceId: "trace_claude_import",
- title: "Claude imported task",
- workspacePath,
- createdAt: 10,
- updatedAt: 20,
- provider: "glm",
- migrationSource: "claudeCode",
- },
- messages: [
- {
- role: "user",
- content: "旧 Claude 提问",
- timestamp: 10,
- },
- {
- role: "assistant",
- content: "旧 Claude 回答",
- timestamp: 20,
- },
- ],
- }),
- "utf-8",
- );
- vi.mocked(agent.resumeSession).mockImplementation(async (params) => {
- throw new Error(`Session not found: ${params.sessionId}`);
- });
- const snapshot = await service.getTaskSnapshot({ workspacePath, taskId });
- expect(agent.createSession).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId: taskId,
- persistence: "immediate",
- importedHistory: expect.objectContaining({
- source: "claudeCode",
- title: "Claude imported task",
- messages: [
- { role: "user", content: "旧 Claude 提问", timestamp: 10 },
- { role: "assistant", content: "旧 Claude 回答", timestamp: 20 },
- ],
- }),
- }),
- );
- expect(snapshot?.meta).toMatchObject({
- taskId,
- title: "Claude imported task",
- workspacePath,
- migrationSource: "claudeCode",
- model: "glm/glm-4.6",
- });
- expect(snapshot?.messages.map((message) => message.content)).toEqual([
- "旧 Claude 提问",
- "旧 Claude 回答",
- ]);
- await expect(taskIndexRepo.getTaskMeta({ workspacePath, taskId })).resolves.toMatchObject({
- taskId,
- migrationSource: "claudeCode",
- model: "glm/glm-4.6",
- });
- vi.mocked(agent.resumeSession).mockRestore();
- await service.setModel({
- taskId,
- traceId: "trace_set_model",
- modelSelection: { providerId: "glm", modelId: "glm-4.6" },
- });
- expect(agent.setModel).toHaveBeenCalledWith({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId: taskId,
- model: { providerId: "glm", modelId: "glm-4.6" },
- });
- });
- it("repairs empty protocol snapshots for imported Claude tasks from legacy backups", async () => {
- const { agent, service } = createService();
- const workspacePath = "/repo";
- const taskId = "claude-import-empty-protocol";
- const snapshotPath = getLegacyTaskSessionSnapshotPath(workspacePath, taskId);
- mkdirSync(dirname(snapshotPath), { recursive: true });
- writeFileSync(
- snapshotPath,
- JSON.stringify({
- meta: {
- taskId,
- traceId: "trace_empty_protocol",
- title: "Claude empty protocol",
- workspacePath,
- createdAt: 10,
- updatedAt: 20,
- migrationSource: "claudeCode",
- },
- messages: [
- { role: "user", content: "旧问题", timestamp: 10 },
- { role: "assistant", content: "旧回答", timestamp: 20 },
- ],
- }),
- "utf-8",
- );
- vi.mocked(agent.resumeSession).mockResolvedValue(
- makeSnapshot({
- sessionId: taskId,
- workspacePath,
- title: "Claude empty protocol",
- messages: [],
- }),
- );
- const snapshot = await service.getTaskSnapshot({ workspacePath, taskId });
- expect(agent.createSession).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath,
- sessionId: taskId,
- persistence: "immediate",
- importedHistory: expect.objectContaining({
- source: "claudeCode",
- messages: [
- { role: "user", content: "旧问题", timestamp: 10 },
- { role: "assistant", content: "旧回答", timestamp: 20 },
- ],
- }),
- }),
- );
- expect(snapshot?.messages.map((message) => message.content)).toEqual(["旧问题", "旧回答"]);
- expect(snapshot?.meta).toMatchObject({
- taskId,
- migrationSource: "claudeCode",
- });
- });
- it("migrates legacy wrapper task ids to acp_session_id backed protocol sessions", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const workspacePath = "/repo";
- const taskId = "legacy_wrapper_task";
- const acpSessionId = "sess_legacy_real";
- await seedIndexedTask(taskIndexRepo, {
- taskId,
- provider: "glm",
- workspacePath,
- });
- await seedLegacyAcpSessionId(taskIndexRepo, {
- workspacePath,
- taskId,
- acpSessionId,
- });
- await expect(taskIndexRepo.getTaskMeta({ workspacePath, taskId })).resolves.toBeNull();
- await expect(
- taskIndexRepo.getTaskMeta({ workspacePath, taskId: acpSessionId }),
- ).resolves.toMatchObject({
- taskId: acpSessionId,
- workspacePath,
- provider: "glm",
- });
- const dbSnapshot = makeSnapshot({
- sessionId: acpSessionId,
- workspacePath,
- title: "DB session",
- messages: [
- makeUserMessage({
- sessionId: acpSessionId,
- messageId: "msg_db_user",
- text: "db prompt",
- }),
- ],
- });
- vi.mocked(agent.resumeSession).mockImplementation(async (params) => {
- if (params.sessionId === acpSessionId) {
- return dbSnapshot;
- }
- throw new Error(`Session not found: ${params.sessionId}`);
- });
- agent.reconcileSessionsIndexV4 = vi.fn(async () => ({
- sessions: [
- {
- sessionId: acpSessionId,
- exists: true as const,
- deleted: false,
- title: "DB session",
- titleSource: "generated" as const,
- createdAt: dbSnapshot.session.createdAt,
- updatedAt: dbSnapshot.session.updatedAt,
- },
- ],
- }));
- const snapshot = await service.getTaskSnapshot({
- workspacePath,
- taskId: acpSessionId,
- });
- expect(snapshot?.meta).toMatchObject({
- taskId: acpSessionId,
- title: "DB session",
- workspacePath,
- model: "glm/glm-4.6",
- thoughtLevel: "think",
- });
- expect(snapshot?.messages.map((message) => message.content)).toEqual(["db prompt"]);
- expect(agent.resumeSession).toHaveBeenCalledWith({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId: acpSessionId,
- });
- const disposable = service.onDynamicTaskEvent({
- workspacePath,
- taskId: acpSessionId,
- deliveryKind: "continuous",
- })(() => undefined);
- disposable.dispose();
- expect(agent.onDynamicSessionEvent).toHaveBeenCalledWith({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId: acpSessionId,
- deliveryKind: "desktop-continuous",
- includeSnapshot: false,
- });
- await service.sendPrompt({
- taskId: acpSessionId,
- traceId: "input_1",
- content: "continue",
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith({
- workspacePath,
- workspaceIdentity: undefined,
- envelope: expect.objectContaining({
- type: "sendText",
- sessionId: acpSessionId,
- commandId: "input_1",
- payload: { text: "continue", heldQueueDisposition: "keepQueueAndSend" },
- }),
- });
- });
- it("maps ZCode Protocol context projection events to legacy usage_update", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const received: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- received.push(event);
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_usage",
- sessionId: task.taskId,
- seq: 1,
- timestamp: 2,
- traceId: "trace_usage",
- type: "session.updated",
- payload: {
- contextUsed: 24000,
- contextWindow: 128000,
- },
- },
- });
- disposable.dispose();
- expect(received).toContainEqual({
- type: "usage_update",
- taskId: task.taskId,
- traceId: "trace_usage",
- used: 24000,
- size: 128000,
- cost: null,
- });
- });
- it.each([
- { streamed: "", expected: "完整项目说明" },
- { streamed: "完整项目", expected: "说明" },
- { streamed: "完整项目说明", expected: "" },
- ])(
- "补齐工具调用后缺失的终态正文且不重复已收到的部分:$streamed",
- async ({ streamed, expected }) => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const received: ZCodeStreamEvent[] = [];
- const subscription = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "bot-channel-continuous",
- })((event) => {
- received.push(event);
- });
- let seq = 0;
- const emit = (
- type: ZCodeSessionEvent["type"],
- payload: Record<string, unknown>,
- turnId = "turn_repair",
- ) => {
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: `repair_${++seq}`,
- sessionId: task.taskId,
- turnId,
- seq,
- timestamp: seq,
- traceId: turnId,
- type,
- payload,
- },
- });
- };
- emit("turn.started", { inputId: "input_repair" });
- emit("session.updated", {
- kind: "text_delta",
- delta: "我来看一下项目目录的内容。",
- assistantMessageId: "preface",
- });
- emit("tool.updated", {
- kind: "scheduled",
- toolCallId: "read_project",
- toolName: "Read",
- input: {},
- });
- if (streamed)
- emit("session.updated", {
- kind: "text_delta",
- delta: streamed,
- assistantMessageId: "answer",
- });
- const before = received.length;
- emit("turn.completed", {
- inputId: "input_repair",
- response: "完整项目说明",
- resultType: "success",
- });
- const terminal = received.slice(before);
- expect(
- terminal
- .filter((event) => event.type === "agent_message_chunk")
- .map((event) => event.content)
- .join(""),
- ).toBe(expected);
- expect(terminal.at(-1)?.type).toBe("task_complete");
- emit("turn.started", { inputId: "input_next" }, "turn_next");
- const next = received.length;
- emit(
- "turn.completed",
- { inputId: "input_next", response: "完整项目说明", resultType: "success" },
- "turn_next",
- );
- expect(received.slice(next)).toContainEqual(
- expect.objectContaining({
- type: "agent_message_chunk",
- content: "完整项目说明",
- inputId: "input_next",
- }),
- );
- subscription.dispose();
- },
- );
- it("不同 assistant message 的正文独立核对,不受前言或子代理文字影响", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const received: ZCodeStreamEvent[] = [];
- const subscription = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "bot-channel-continuous",
- })((event) => {
- received.push(event);
- });
- let seq = 0;
- const emit = (
- payload: Record<string, unknown>,
- type: "session.updated" | "turn.completed" = "session.updated",
- ) =>
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: `message_${++seq}`,
- sessionId: task.taskId,
- turnId: "message_turn",
- seq,
- timestamp: seq,
- traceId: "message_turn",
- type,
- payload,
- },
- });
- emit({ kind: "text_delta", assistantMessageId: "preface", delta: "先检查。" });
- emit({ kind: "text_delta", assistantMessageId: "answer", delta: "最终" });
- emit({
- kind: "text_delta",
- assistantMessageId: "child",
- parentToolUseId: "agent_tool",
- delta: "子代理内容",
- });
- const before = received.length;
- emit({ response: "最终回答", resultType: "success" }, "turn.completed");
- expect(received.slice(before)).toEqual([
- expect.objectContaining({ type: "agent_message_chunk", content: "回答" }),
- expect.objectContaining({ type: "task_complete" }),
- ]);
- subscription.dispose();
- });
- it("uses prompt inputId as per-turn traceId for session stream events", async () => {
- const { agent, eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const received: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- received.push(event);
- });
- await service.sendPrompt({
- taskId: task.taskId,
- traceId: "input_turn_1",
- content: "continue",
- });
- expect(agent.sendConversationCommandV4).toHaveBeenCalledWith(
- expect.objectContaining({
- envelope: expect.objectContaining({ commandId: "input_turn_1" }),
- }),
- );
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_stream",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 1,
- timestamp: 2,
- traceId: "runtime_trace_1",
- type: "session.updated",
- payload: { kind: "text_delta", delta: "hello" },
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_tool",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 2,
- timestamp: 3,
- traceId: "runtime_trace_1",
- type: "tool.updated",
- payload: {
- kind: "scheduled",
- toolCallId: "call_1",
- toolName: "Read",
- input: { filePath: "/repo/app.ts" },
- },
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_done",
- sessionId: task.taskId,
- turnId: "turn_1",
- seq: 3,
- timestamp: 4,
- traceId: "runtime_trace_1",
- type: "turn.completed",
- payload: { inputId: "input_turn_1", resultType: "success" },
- },
- });
- disposable.dispose();
- expect(received).toContainEqual(
- expect.objectContaining({
- type: "agent_message_chunk",
- traceId: "input_turn_1",
- inputId: "input_turn_1",
- }),
- );
- expect(received).toContainEqual(
- expect.objectContaining({
- type: "tool_call",
- traceId: "input_turn_1",
- inputId: "input_turn_1",
- toolId: "call_1",
- }),
- );
- expect(received).toContainEqual(
- expect.objectContaining({
- type: "task_complete",
- traceId: "input_turn_1",
- inputId: "input_turn_1",
- }),
- );
- });
- it("maps main turn model usage with context window to legacy usage_update", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const received: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- received.push(event);
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_model_usage",
- sessionId: task.taskId,
- seq: 1,
- timestamp: 2,
- traceId: "trace_model_usage",
- type: "session.updated",
- payload: {
- contextWindow: 1_000_000,
- querySource: "main_turn",
- usage: {
- inputTokens: 279454,
- outputTokens: 177,
- totalTokens: 279631,
- cacheReadTokens: 1000,
- cacheWriteTokens: 200,
- cacheHitRate: 0.25,
- },
- contextUsageBreakdown: [
- { source: "messages", chars: 1200 },
- { source: "system_tool_schemas", chars: 300 },
- ],
- },
- },
- });
- disposable.dispose();
- expect(received).toContainEqual({
- type: "usage_update",
- taskId: task.taskId,
- traceId: "trace_model_usage",
- used: 279631,
- size: 1_000_000,
- cost: null,
- cache: {
- inputTokens: 279454,
- cacheReadTokens: 1000,
- cacheWriteTokens: 200,
- latestHitRate: 0.25,
- hitRate: 0.25,
- },
- breakdown: [
- { source: "messages", chars: 1200 },
- { source: "system_tool_schemas", chars: 300 },
- ],
- });
- expect(received).toContainEqual({
- type: "task_token_usage_delta",
- taskId: task.taskId,
- traceId: "trace_model_usage",
- eventKey: "evt_model_usage",
- eventId: "evt_model_usage",
- querySource: "main_turn",
- usage: {
- inputTokens: 279454,
- outputTokens: 177,
- totalTokens: 279631,
- reasoningTokens: undefined,
- cachedInputTokens: 1000,
- cachedWriteInputTokens: 200,
- },
- });
- });
- it("maps aggregate main turn cache hit usage to legacy usage_update", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const received: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- received.push(event);
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_model_usage_average",
- sessionId: task.taskId,
- seq: 1,
- timestamp: 2,
- traceId: "trace_model_usage_average",
- type: "session.updated",
- payload: {
- contextWindow: 1_000_000,
- querySource: "main_turn",
- cacheHit: {
- inputTokens: 200,
- cacheReadTokens: 80,
- cacheWriteTokens: 10,
- latestHitRate: 0.4,
- hitRate: 0.3,
- hitRateRequestCount: 2,
- totalInputTokens: 500,
- totalCacheReadTokens: 150,
- totalCacheWriteTokens: 25,
- },
- usage: {
- inputTokens: 200,
- outputTokens: 20,
- totalTokens: 220,
- cacheReadTokens: 80,
- cacheWriteTokens: 10,
- },
- },
- },
- });
- disposable.dispose();
- expect(received).toContainEqual({
- type: "usage_update",
- taskId: task.taskId,
- traceId: "trace_model_usage_average",
- used: 220,
- size: 1_000_000,
- cost: null,
- cache: {
- inputTokens: 200,
- cacheReadTokens: 80,
- cacheWriteTokens: 10,
- latestHitRate: 0.4,
- hitRate: 0.3,
- hitRateRequestCount: 2,
- totalInputTokens: 500,
- totalCacheReadTokens: 150,
- totalCacheWriteTokens: 25,
- },
- });
- });
- it("prefers real main turn model usage over projected context used", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const received: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- received.push(event);
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_model_usage_projection",
- sessionId: task.taskId,
- seq: 1,
- timestamp: 2,
- traceId: "trace_model_usage_projection",
- type: "session.updated",
- payload: {
- contextUsed: 64000,
- contextWindow: 1_000_000,
- querySource: "main_turn",
- usage: {
- inputTokens: 12345,
- outputTokens: 77,
- totalTokens: 12422,
- },
- },
- },
- });
- disposable.dispose();
- expect(received).toContainEqual({
- type: "usage_update",
- taskId: task.taskId,
- traceId: "trace_model_usage_projection",
- used: 12422,
- size: 1_000_000,
- cost: null,
- });
- expect(received).toContainEqual({
- type: "task_token_usage_delta",
- taskId: task.taskId,
- traceId: "trace_model_usage_projection",
- eventKey: "evt_model_usage_projection",
- eventId: "evt_model_usage_projection",
- querySource: "main_turn",
- usage: {
- inputTokens: 12345,
- outputTokens: 77,
- totalTokens: 12422,
- reasoningTokens: undefined,
- cachedInputTokens: undefined,
- cachedWriteInputTokens: undefined,
- },
- });
- });
- it("does not map sidecar model usage to legacy usage_update", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const received: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- received.push(event);
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_title_usage",
- sessionId: task.taskId,
- seq: 1,
- timestamp: 2,
- traceId: "trace_title_usage",
- type: "session.updated",
- payload: {
- contextWindow: 1_000_000,
- querySource: "session_title",
- usage: {
- inputTokens: 89,
- outputTokens: 84,
- totalTokens: 173,
- },
- },
- },
- });
- disposable.dispose();
- expect(received).not.toContainEqual(
- expect.objectContaining({
- type: "usage_update",
- traceId: "trace_title_usage",
- }),
- );
- expect(received).toContainEqual({
- type: "task_token_usage_delta",
- taskId: task.taskId,
- traceId: "trace_title_usage",
- eventKey: "evt_title_usage",
- eventId: "evt_title_usage",
- querySource: "session_title",
- usage: {
- inputTokens: 89,
- outputTokens: 84,
- totalTokens: 173,
- reasoningTokens: undefined,
- cachedInputTokens: undefined,
- cachedWriteInputTokens: undefined,
- },
- });
- });
- it("maps ZCode model retry status to apiRetry stream events and runtime snapshots", async () => {
- const { eventEmitter, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const received: ZCodeStreamEvent[] = [];
- const disposable = service.onDynamicTaskEvent({
- workspacePath: "/repo",
- taskId: task.taskId,
- deliveryKind: "continuous",
- })((event) => {
- received.push(event);
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_retry",
- sessionId: task.taskId,
- seq: 1,
- timestamp: 2,
- traceId: "trace_retry",
- type: "session.updated",
- payload: {
- type: "model_retry_scheduled",
- attempt: 1,
- maxAttempts: 4,
- delayMs: 2_000,
- statusCode: 429,
- message: "rate limited",
- },
- },
- });
- expect(received).toContainEqual({
- type: "session_info_update",
- taskId: task.taskId,
- traceId: "trace_retry",
- apiRetry: {
- kind: "api_retry",
- attempt: 1,
- maxRetries: 3,
- retryDelayMs: 2_000,
- errorStatus: 429,
- error: "rate limited",
- },
- });
- await expect(
- service.getTaskSnapshot({ workspacePath: "/repo", taskId: task.taskId }),
- ).resolves.toMatchObject({
- runtime: {
- apiRetry: {
- kind: "api_retry",
- attempt: 1,
- maxRetries: 3,
- retryDelayMs: 2_000,
- errorStatus: 429,
- error: "rate limited",
- },
- },
- });
- const retryStartedReceivedIndex = received.length;
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_retry_started",
- sessionId: task.taskId,
- seq: 2,
- timestamp: 3,
- traceId: "trace_retry",
- type: "session.updated",
- payload: {
- type: "model_request_started",
- attempt: 2,
- maxAttempts: 4,
- requestId: "request_retry",
- },
- },
- });
- const retryStartedEvents = received.slice(retryStartedReceivedIndex);
- expect(retryStartedEvents).toContainEqual(
- expect.objectContaining({
- type: "task_network_debug_status",
- taskId: task.taskId,
- traceId: "trace_retry",
- eventKey: "evt_retry_started",
- statusType: "model_request_started",
- attempt: 2,
- maxAttempts: 4,
- }),
- );
- expect(retryStartedEvents).not.toContainEqual({
- type: "session_info_update",
- taskId: task.taskId,
- traceId: "trace_retry",
- apiRetry: null,
- });
- await expect(
- service.getTaskSnapshot({ workspacePath: "/repo", taskId: task.taskId }),
- ).resolves.toMatchObject({
- runtime: {
- apiRetry: {
- kind: "api_retry",
- attempt: 1,
- maxRetries: 3,
- retryDelayMs: 2_000,
- errorStatus: 429,
- error: "rate limited",
- },
- },
- });
- const retryProgressReceivedIndex = received.length;
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_retry_progress",
- sessionId: task.taskId,
- seq: 3,
- timestamp: 4,
- traceId: "trace_retry",
- type: "session.updated",
- payload: {
- kind: "text_delta",
- delta: "recovered",
- assistantMessageId: "msg_retry",
- },
- },
- });
- const retryProgressEvents = received.slice(retryProgressReceivedIndex);
- expect(retryProgressEvents).toContainEqual({
- type: "session_info_update",
- taskId: task.taskId,
- traceId: "trace_retry",
- apiRetry: null,
- });
- expect(retryProgressEvents).toContainEqual({
- type: "agent_message_chunk",
- taskId: task.taskId,
- traceId: "trace_retry",
- messageId: "msg_retry",
- content: "recovered",
- });
- await expect(
- service.getTaskSnapshot({ workspacePath: "/repo", taskId: task.taskId }),
- ).resolves.toMatchObject({
- runtime: {
- apiRetry: null,
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_stream_recovery_retry",
- sessionId: task.taskId,
- seq: 4,
- timestamp: 5,
- traceId: "trace_retry",
- type: "session.updated",
- payload: {
- type: "model_request_started",
- attempt: 1,
- maxAttempts: 11,
- requestId: "request_recovery",
- streamRecovery: {
- attemptId: "stream_attempt_1",
- anchorId: "anchor_1",
- maxRetries: 10,
- recoveredFromRequestId: "request_failed",
- retryNumber: 3,
- },
- },
- },
- });
- expect(received).toContainEqual({
- type: "session_info_update",
- taskId: task.taskId,
- traceId: "trace_retry",
- apiRetry: {
- kind: "api_retry",
- attempt: 3,
- maxRetries: 10,
- retryDelayMs: 0,
- errorStatus: null,
- error: "Model stream recovery retry started",
- },
- });
- await expect(
- service.getTaskSnapshot({ workspacePath: "/repo", taskId: task.taskId }),
- ).resolves.toMatchObject({
- runtime: {
- apiRetry: {
- kind: "api_retry",
- attempt: 3,
- maxRetries: 10,
- retryDelayMs: 0,
- errorStatus: null,
- error: "Model stream recovery retry started",
- },
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_stream_recovery_progress",
- sessionId: task.taskId,
- seq: 5,
- timestamp: 6,
- traceId: "trace_retry",
- type: "streamRecovery.updated",
- payload: {
- attemptId: "stream_attempt_2",
- failedRequestId: "request_failed",
- maxRetries: 10,
- recoveredFromRequestId: "request_failed",
- retryNumber: 4,
- streamMode: "sse",
- },
- },
- });
- expect(received).toContainEqual({
- type: "session_info_update",
- taskId: task.taskId,
- traceId: "trace_retry",
- apiRetry: {
- kind: "api_retry",
- attempt: 4,
- maxRetries: 10,
- retryDelayMs: 0,
- errorStatus: null,
- error: "Model stream recovery retry started",
- },
- });
- await expect(
- service.getTaskSnapshot({ workspacePath: "/repo", taskId: task.taskId }),
- ).resolves.toMatchObject({
- runtime: {
- apiRetry: {
- kind: "api_retry",
- attempt: 4,
- maxRetries: 10,
- retryDelayMs: 0,
- errorStatus: null,
- error: "Model stream recovery retry started",
- },
- },
- });
- eventEmitter.fire({
- type: "session.event",
- event: {
- eventId: "evt_retry_clear",
- sessionId: task.taskId,
- seq: 6,
- timestamp: 7,
- traceId: "trace_retry",
- type: "session.updated",
- payload: {
- type: "model_request_completed",
- attempt: 2,
- maxAttempts: 4,
- },
- },
- });
- disposable.dispose();
- expect(received).toContainEqual({
- type: "session_info_update",
- taskId: task.taskId,
- traceId: "trace_retry",
- apiRetry: null,
- });
- await expect(
- service.getTaskSnapshot({ workspacePath: "/repo", taskId: task.taskId }),
- ).resolves.toMatchObject({
- runtime: {
- apiRetry: null,
- },
- });
- });
- it("keeps pinned state in sqlite without resuming the agent session", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- vi.mocked(agent.resumeSession).mockClear();
- await service.setTaskPinned({
- workspacePath: "/repo",
- taskId: task.taskId,
- pinned: true,
- });
- const pinned = await service.listPinnedTasks({ workspacePath: "/repo" });
- const active = await service.listTasks({ workspacePath: "/repo" });
- expect(agent.resumeSession).not.toHaveBeenCalled();
- expect(pinned.map((item) => item.taskId)).toEqual([task.taskId]);
- expect(active).toEqual([]);
- });
- });
|