| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693 |
- 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, 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 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(() => {
- vi.useRealTimers();
- setNetworkTelemetrySink(null);
- while (disposables.length > 0) {
- disposables.pop()?.disposeAll();
- }
- 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 = {
- 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)。
- const taskIndexRepo = new 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 repo.syncTaskMeta({
- 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.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("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",
- },
- }),
- );
- await vi.waitFor(async () => {
- await expect(
- taskIndexRepo.getTaskMeta({
- workspacePath: "/repo",
- taskId: task.taskId,
- }),
- ).resolves.toMatchObject({
- lastError: {
- code: "provider_not_found",
- detail: "stale-provider/gpt-5.5",
- message: "Model provider is not configured: stale-provider",
- attribution: {
- source: "runtime",
- reason: "provider_not_configured",
- providerId: "account:zai-individual-coding-plan",
- },
- },
- });
- });
- });
- 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("persists deleted tombstone and broadcasts task_deleted without deleting the CLI session", 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();
- 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).not.toHaveBeenCalled();
- });
- it("deletes only still-archived targets, skips restored/missing targets, and retains CLI sessions", async () => {
- const { agent, service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const target = { workspacePath: "/repo", taskId: task.taskId };
- await service.archiveTask(target);
- await service.unarchiveTask(target);
- expect(await service.deleteArchivedTask(target)).toBe(false);
- await service.archiveTask(target);
- vi.mocked(agent.sendConversationCommandV4).mockClear();
- expect(await service.deleteArchivedTask(target)).toBe(true);
- expect(await service.deleteArchivedTask(target)).toBe(false);
- expect(await service.deleteArchivedTask({ ...target, taskId: "missing" })).toBe(false);
- expect(await service.listDeletedTaskIds({ workspacePath: "/repo" })).toEqual([task.taskId]);
- expect(agent.sendConversationCommandV4).not.toHaveBeenCalled();
- });
- it("does not publish deletion or hide the archived target when guarded persistence fails", async () => {
- const { service, taskIndexRepo } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- const target = { workspacePath: "/repo", taskId: task.taskId };
- await service.archiveTask(target);
- const events: string[] = [];
- const listener = service.onDynamicWorkspaceEvent({ workspacePath: "/repo" })((event) => {
- if (event.type === "workspace_task_list_changed") events.push(event.reason);
- });
- const write = vi
- .spyOn(taskIndexRepo, "deleteArchivedTask")
- .mockRejectedValueOnce(new Error("write failed"));
- await expect(service.deleteArchivedTask(target)).rejects.toThrow("write failed");
- expect(
- (await service.listArchivedTasks({ workspacePath: "/repo" })).map((meta) => meta.taskId),
- ).toContain(task.taskId);
- expect(events).toEqual([]);
- write.mockRestore();
- listener.dispose();
- });
- it("批量条件删除逐项提交,重复 ID 去重,混合失败只发布一次 workspace 删除事件", async () => {
- const { agent, service, taskIndexRepo } = createService();
- const tasks = [];
- for (let i = 0; i < 4; i += 1) {
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.archiveTask({ workspacePath: "/repo", taskId: task.taskId });
- tasks.push(task);
- }
- const [first, restored, failed, last] = tasks;
- await service.unarchiveTask({ workspacePath: "/repo", taskId: restored!.taskId });
- const onEvent = vi.fn();
- const listener = service.onDynamicWorkspaceEvent({ workspacePath: "/repo" })(onEvent);
- const originalDelete = taskIndexRepo.deleteArchivedTask.bind(taskIndexRepo);
- const write = vi
- .spyOn(taskIndexRepo, "deleteArchivedTask")
- .mockImplementation(async (target) => {
- if (target.taskId === failed!.taskId) throw new Error("write failed");
- return originalDelete(target);
- });
- vi.mocked(agent.sendConversationCommandV4).mockClear();
- const result = await service.deleteArchivedTasks({
- workspacePath: "/repo",
- taskIds: [
- first!.taskId,
- restored!.taskId,
- failed!.taskId,
- last!.taskId,
- first!.taskId,
- "missing",
- ],
- });
- expect(result).toEqual({
- deletedTaskIds: [first!.taskId, last!.taskId],
- skippedTaskIds: [restored!.taskId, "missing"],
- failedTaskIds: [failed!.taskId],
- });
- expect(write).toHaveBeenCalledTimes(5);
- expect(onEvent).toHaveBeenCalledExactlyOnceWith(
- expect.objectContaining({
- type: "workspace_task_list_changed",
- workspacePath: "/repo",
- reason: "task_deleted",
- }),
- );
- expect(onEvent.mock.calls[0]![0].taskId).toBeUndefined();
- expect(onEvent.mock.calls[0]![0].taskMeta).toBeUndefined();
- expect(
- (await service.listArchivedTasks({ workspacePath: "/repo" })).map((task) => task.taskId),
- ).toEqual([failed!.taskId]);
- expect(agent.sendConversationCommandV4).not.toHaveBeenCalled();
- onEvent.mockClear();
- expect(await service.deleteArchivedTasks({ workspacePath: "/repo", taskIds: [] })).toEqual({
- deletedTaskIds: [],
- skippedTaskIds: [],
- failedTaskIds: [],
- });
- expect(
- await service.deleteArchivedTasks({ workspacePath: "/repo", taskIds: [failed!.taskId] }),
- ).toEqual({ deletedTaskIds: [], skippedTaskIds: [], failedTaskIds: [failed!.taskId] });
- expect(
- await service.deleteArchivedTasks({ workspacePath: "/repo", taskIds: [first!.taskId] }),
- ).toEqual({ deletedTaskIds: [], skippedTaskIds: [first!.taskId], failedTaskIds: [] });
- expect(onEvent).not.toHaveBeenCalled();
- write.mockRestore();
- listener.dispose();
- });
- it("批次按 workspaceIdentity 隔离,同路径同 taskId 不会误删其它 source", async () => {
- const { service, taskIndexRepo } = createService();
- const identities = ["remote:ssh:first:/repo", "remote:ssh:second:/repo"];
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- for (const workspaceIdentity of identities) {
- await taskIndexRepo.syncTaskMeta({ meta: { ...task, workspaceIdentity } });
- await taskIndexRepo.updateTaskState({
- workspacePath: "/repo",
- workspaceIdentity,
- taskId: task.taskId,
- patch: { archived: true },
- });
- }
- expect(
- await service.deleteArchivedTasks({
- workspacePath: "/repo",
- workspaceIdentity: identities[0],
- taskIds: [task.taskId],
- }),
- ).toEqual({ deletedTaskIds: [task.taskId], skippedTaskIds: [], failedTaskIds: [] });
- expect(
- await taskIndexRepo.getTaskMeta({
- workspacePath: "/repo",
- workspaceIdentity: identities[1],
- taskId: task.taskId,
- }),
- ).toEqual(expect.objectContaining({ taskId: task.taskId, workspaceIdentity: identities[1] }));
- });
- it("并发批次仍由 Repo 条件写入裁决,每个目标只能成功删除一次", async () => {
- const { service } = createService();
- const task = await service.createTask({ workspacePath: "/repo", provider: "glm" });
- await service.archiveTask({ workspacePath: "/repo", taskId: task.taskId });
- const results = await Promise.all(
- [1, 2].map(() =>
- service.deleteArchivedTasks({
- workspacePath: "/repo",
- taskIds: [task.taskId],
- }),
- ),
- );
- expect(results.flatMap((result) => result.deletedTaskIds)).toEqual([task.taskId]);
- expect(results.flatMap((result) => result.skippedTaskIds)).toEqual([task.taskId]);
- expect(results.flatMap((result) => result.failedTaskIds)).toEqual([]);
- });
- it("bumps updatedAt when renaming a task so running UI title refreshes immediately", 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();
- const renamed = await service.renameTask({
- taskId: "running-rename",
- workspacePath: "/repo",
- title: "短名",
- });
- expect(renamed.title).toBe("短名");
- expect(renamed.titleOverridden).toBe(true);
- expect(renamed.updatedAt).toBe(Date.parse("2026-06-13T14:45:00.000Z"));
- 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("reuses a mobile replayable draft session when createTask receives draftSessionId", async () => {
- const { agent, service } = createService();
- await agent.createSession({
- workspacePath: "/repo",
- sessionId: "draft_mobile",
- persistence: "deferred",
- model: { providerId: "glm", modelId: "deepseek-v4-pro" },
- });
- vi.mocked(agent.createSession).mockClear();
- vi.mocked(agent.readSession).mockClear();
- const task = await service.createTask({
- workspacePath: "/repo",
- provider: "glm",
- draftSessionId: "draft_mobile",
- });
- const list = await service.listTasks({ workspacePath: "/repo" });
- expect(agent.readSession).toHaveBeenCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- sessionId: "draft_mobile",
- });
- expect(agent.createSession).not.toHaveBeenCalled();
- expect(task.taskId).toBe("draft_mobile");
- expect(list.map((item) => item.taskId)).toEqual(["draft_mobile"]);
- });
- 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,
- 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("syncs a structured model selection before reusing a mobile replayable draft session", async () => {
- const { agent, service } = createService();
- await agent.createSession({
- workspacePath: "/repo",
- sessionId: "draft_mobile",
- persistence: "deferred",
- model: { providerId: "glm", modelId: "glm-0606[1m]" },
- });
- vi.mocked(agent.setModel).mockClear();
- const task = await service.createTask({
- workspacePath: "/repo",
- provider: "glm",
- draftSessionId: "draft_mobile",
- modelSelection: {
- providerId: "glm",
- modelId: "glm-0606[1m]",
- options: { reasoningLevel: "max" },
- },
- });
- expect(agent.setModel).toHaveBeenCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- sessionId: "draft_mobile",
- model: {
- providerId: "glm",
- modelId: "glm-0606[1m]",
- options: { reasoningLevel: "max" },
- },
- });
- expect(task.taskId).toBe("draft_mobile");
- });
- it("syncs model and thoughtLevel before reusing a mobile replayable draft session", async () => {
- const { agent, service } = createService();
- await agent.createSession({
- workspacePath: "/repo",
- sessionId: "draft_mobile",
- persistence: "deferred",
- model: { providerId: "custom-deepseek", modelId: "deepseek-v4-flash" },
- });
- vi.mocked(agent.createSession).mockClear();
- vi.mocked(agent.setModel).mockClear();
- vi.mocked(agent.setThoughtLevel).mockClear();
- const task = await service.createTask({
- workspacePath: "/repo",
- provider: "glm",
- draftSessionId: "draft_mobile",
- modelSelection: {
- providerId: "account:bigmodel-individual-coding-plan",
- modelId: "GLM-5-Turbo",
- options: { reasoningLevel: "enabled" },
- },
- });
- expect(agent.setModel).toHaveBeenCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- sessionId: "draft_mobile",
- model: {
- providerId: "account:bigmodel-individual-coding-plan",
- modelId: "GLM-5-Turbo",
- options: { reasoningLevel: "enabled" },
- },
- });
- expect(agent.setThoughtLevel).toHaveBeenCalledWith({
- workspacePath: "/repo",
- workspaceIdentity: undefined,
- sessionId: "draft_mobile",
- thoughtLevel: "enabled",
- });
- expect(agent.createSession).not.toHaveBeenCalled();
- expect(task.taskId).toBe("draft_mobile");
- });
- 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("waits for the prompt completed ready state before draining a mobile task command", 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",
- });
- 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.some((event) => event.type === "tool_call_update")).toBe(true);
- 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",
- });
- // M5 ②:listGroupedTaskView(tasks 表 join 的 grouped 查询)已删除;grouped 任务内容
- // 改由 sessions-index 提供,provider 过滤断言只保留仍存在的 sqlite 列表面。
- 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);
- });
- it("filters non-glm task rows from grouped order save results", async () => {
- const { service, taskIndexRepo } = createService();
- await seedIndexedTask(taskIndexRepo, { taskId: "glm_active", provider: "glm" });
- await seedIndexedTask(taskIndexRepo, { taskId: "glm_grouped", provider: "glm" });
- await seedIndexedTask(taskIndexRepo, { taskId: "gemini_active", provider: "gemini" });
- const group = await service.createTaskGroup({ title: "Group" });
- const savedGroupedView = await service.applyGroupedTaskViewOrder({
- workspaceScopes: [{ workspacePath: "/repo" }],
- topLevelNodes: [
- { type: "group", groupId: group.id },
- {
- type: "task",
- task: { workspacePath: "/repo", taskId: "glm_active" },
- },
- {
- type: "task",
- task: { workspacePath: "/repo", taskId: "gemini_active" },
- },
- ],
- groups: [
- {
- groupId: group.id,
- taskRefs: [
- { workspacePath: "/repo", taskId: "glm_grouped" },
- { workspacePath: "/repo", taskId: "gemini_active" },
- ],
- },
- ],
- });
- const groupedTaskIds = savedGroupedView.nodes.flatMap((node) =>
- node.type === "group" ? node.tasks.map((task) => task.taskId) : [node.task.taskId],
- );
- const rawOrders = (
- taskIndexRepo as unknown as {
- getDatabase(): {
- prepare(sql: string): {
- all(): Array<{ node_key: string }>;
- };
- };
- }
- )
- .getDatabase()
- .prepare("SELECT node_key FROM task_group_view_node_orders WHERE node_type = 'task'")
- .all()
- .map((row) => row.node_key);
- const rawMembers = (
- taskIndexRepo as unknown as {
- getDatabase(): {
- prepare(sql: string): {
- all(): Array<{ task_id: string }>;
- };
- };
- }
- )
- .getDatabase()
- .prepare("SELECT task_id FROM task_group_members")
- .all()
- .map((row) => row.task_id);
- expect(groupedTaskIds).toEqual(["glm_grouped", "glm_active"]);
- expect(rawOrders.some((nodeKey) => nodeKey.includes("gemini_active"))).toBe(false);
- expect(rawMembers).toEqual(["glm_grouped"]);
- });
- it("keeps stale grouped tasks visible when auto archive is disabled", async () => {
- vi.useFakeTimers();
- const now = new Date("2026-06-09T00:00:00.000Z").getTime();
- vi.setSystemTime(now);
- const { service, taskIndexRepo } = createService({
- settings: {
- taskAutoArchiveEnabled: false,
- taskAutoArchiveOlderThanDays: 7,
- },
- });
- await seedIndexedTask(taskIndexRepo, {
- taskId: "stale_glm",
- provider: "glm",
- updatedAt: now - 8 * 24 * 60 * 60 * 1000,
- });
- await seedIndexedTask(taskIndexRepo, {
- taskId: "stale_gemini",
- provider: "gemini",
- updatedAt: now - 8 * 24 * 60 * 60 * 1000,
- });
- // M5 ②:grouped 入口改为结构读取(listGroupedTaskViewStructure),auto-archive 触发口径不变。
- await service.listGroupedTaskViewStructure({
- workspaceScopes: [{ workspacePath: "/repo" }],
- });
- const archived = await taskIndexRepo.listTaskMetas({
- workspacePath: "/repo",
- archived: true,
- });
- expect(archived.map((task) => task.taskId)).toEqual([]);
- });
- it("auto archives stale grouped tasks across providers when the setting is enabled", async () => {
- vi.useFakeTimers();
- const now = new Date("2026-06-09T00:00:00.000Z").getTime();
- vi.setSystemTime(now);
- const { service, taskIndexRepo } = createService({
- settings: {
- taskAutoArchiveEnabled: true,
- taskAutoArchiveOlderThanDays: 7,
- },
- });
- await seedIndexedTask(taskIndexRepo, {
- taskId: "stale_glm",
- provider: "glm",
- updatedAt: now - 8 * 24 * 60 * 60 * 1000,
- });
- await seedIndexedTask(taskIndexRepo, {
- taskId: "stale_gemini",
- provider: "gemini",
- updatedAt: now - 8 * 24 * 60 * 60 * 1000,
- });
- // M5 ②:grouped 入口改为结构读取(listGroupedTaskViewStructure),auto-archive 触发口径不变。
- await service.listGroupedTaskViewStructure({
- workspaceScopes: [{ workspacePath: "/repo" }],
- });
- const archived = await taskIndexRepo.listTaskMetas({
- workspacePath: "/repo",
- archived: true,
- });
- const visibleArchived = await service.listArchivedTasks({ workspacePath: "/repo" });
- expect(archived.map((task) => task.taskId)).toEqual(["stale_glm", "stale_gemini"]);
- expect(visibleArchived.map((task) => task.taskId)).toEqual(["stale_glm"]);
- });
- 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}`);
- });
- 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([]);
- });
- });
|