| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253 |
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
- import type { IPlatformService } from "@zcode/shared";
- import type { ConversationTelemetryFact } from "@zcode/shared/zcode-protocol-v4";
- import { resetMessageTelemetryForTest } from "@/lib/messageTelemetry.js";
- import {
- clearUiPerfArmsReporterForTest,
- setUiPerfArmsReporter,
- } from "@/lib/uiPerfArmsTelemetry.js";
- import { ConversationTelemetrySupervisor } from "@/v4/telemetry/conversationTelemetrySupervisor.js";
- import { buildV4ConversationPromptTelemetryExtraDetail } from "@/v4/telemetry/conversationPromptTelemetry.js";
- function createPlatform() {
- const reportTelemetryEvent = vi.fn(async () => undefined);
- const reportArmsCustomEvent = vi.fn(async () => undefined);
- return {
- platform: {
- reportTelemetryEvent,
- reportArmsCustomEvent,
- } as unknown as IPlatformService,
- reportTelemetryEvent,
- reportArmsCustomEvent,
- };
- }
- function commonFact(eventId: string, eventSeq: number, sessionId = "session-1") {
- return {
- version: 1 as const,
- eventId,
- eventSeq,
- occurredAt: eventSeq,
- sessionId,
- sourceCommandId: "command-1",
- turnId: "turn-1",
- };
- }
- function armsByName(
- calls: ReturnType<typeof createPlatform>["reportArmsCustomEvent"],
- name: string,
- ) {
- return calls.mock.calls.map(([payload]) => payload).filter((payload) => payload.name === name);
- }
- function reportsByElement(
- calls: ReturnType<typeof createPlatform>["reportTelemetryEvent"],
- elementName: string,
- ) {
- return calls.mock.calls
- .map(([payload]) => payload)
- .filter((payload) => payload.elementName === elementName);
- }
- function startForegroundSubagent(supervisor: ConversationTelemetrySupervisor, sendTime: number) {
- supervisor.acceptPromptSeed({
- sessionId: "parent-session",
- sourceCommandId: "parent-command",
- memoryEnabled: true,
- sendTime,
- extraDetail: {
- model_name: "main-provider/main-model",
- model_provider: "main-provider",
- },
- });
- supervisor.handleFact({
- ...commonFact("parent-turn-start", 1, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.started",
- executionKind: "agent",
- });
- supervisor.handleFact({
- ...commonFact("agent-tool-start", 2, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "agent-tool",
- toolName: "Agent",
- });
- supervisor.handleFact({
- ...commonFact("subagent-spawned", 3, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "subagent.lifecycle",
- phase: "spawned",
- agentId: "agent-1",
- childSessionId: "child-session",
- parentToolCallId: "agent-tool",
- background: false,
- status: "running",
- });
- }
- function startForegroundSubagentRequest(supervisor: ConversationTelemetrySupervisor) {
- supervisor.handleFact({
- ...commonFact("child-request-start", 1, "child-session"),
- sourceCommandId: "child-command",
- turnId: "child-turn",
- kind: "model.request.status",
- requestId: "child-request",
- status: "model_request_started",
- providerId: "qwen-provider",
- modelId: "qwen3.7-max",
- providerHostname: "provider.example.com",
- transport: "sse",
- querySource: "subagent",
- attempt: 1,
- maxAttempts: 1,
- });
- }
- function spawnBackgroundSubagent(
- supervisor: ConversationTelemetrySupervisor,
- input: {
- agentId: string;
- childSessionId: string;
- sourceCommandId?: string;
- parentToolCallId?: string;
- eventId?: string;
- memoryEnabled?: boolean;
- },
- ) {
- supervisor.handleFact({
- memoryEnabled: input.memoryEnabled,
- ...commonFact(input.eventId ?? `${input.agentId}-spawned`, 1, "parent-session"),
- ...(input.sourceCommandId !== undefined
- ? { sourceCommandId: input.sourceCommandId }
- : { sourceCommandId: "parent-command" }),
- kind: "subagent.lifecycle",
- phase: "spawned",
- agentId: input.agentId,
- childSessionId: input.childSessionId,
- parentToolCallId: input.parentToolCallId ?? `${input.agentId}-parent-tool`,
- background: true,
- status: "running",
- });
- }
- function reportForegroundSubagentUsage(supervisor: ConversationTelemetrySupervisor) {
- supervisor.handleFact({
- ...commonFact("child-usage", 2, "child-session"),
- sourceCommandId: "child-command",
- turnId: "child-turn",
- kind: "usage.delta",
- requestId: "child-request",
- providerId: "qwen-provider",
- modelId: "qwen3.7-max",
- providerHostname: "provider.example.com",
- inputTokens: 8_616,
- outputTokens: 69,
- totalTokens: 8_685,
- reasoningTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 5_797,
- });
- }
- function terminateForegroundSubagentParent(
- supervisor: ConversationTelemetrySupervisor,
- error?: {
- code?: string;
- message?: string;
- },
- ) {
- supervisor.handleFact({
- ...commonFact("agent-tool-failed", 4, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "tool.lifecycle",
- phase: "failed",
- toolCallId: "agent-tool",
- toolName: "Agent",
- ...(error?.code ? { errorCode: error.code } : {}),
- errorMessage:
- error?.message ??
- "Agent was cancelled before the subagent returned findings or background launch completed",
- });
- supervisor.handleFact({
- ...commonFact("parent-turn-terminal", 5, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.terminal",
- status: "interrupted",
- resultType: "cancelled",
- });
- }
- function completionAgentCompositions(
- calls: ReturnType<typeof createPlatform>["reportTelemetryEvent"],
- ): string[] {
- return reportsByElement(calls, "message_completion").map(
- (report) => report.eventExtraDetail.agent_composition,
- );
- }
- describe("ConversationTelemetrySupervisor", () => {
- it("父 ACK 未到时 child step 从启动事实读取 Memory", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "memory-child-before-ack",
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "memory-child",
- childSessionId: "child-session",
- memoryEnabled: true,
- });
- supervisor.handleFact({
- ...commonFact("child-tool", 2, "child-session"),
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "read-1",
- toolName: "Read",
- });
- supervisor.handleFact({
- ...commonFact("child-tool-end", 3, "child-session"),
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "read-1",
- toolName: "Read",
- });
- await supervisor.flushReportsForTest();
- const steps = reportsByElement(reportTelemetryEvent, "agent_step");
- expect(steps).toHaveLength(1);
- expect(steps[0].eventExtraDetail.memory_enabled).toBe("1");
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- supervisor.dispose();
- });
- it.each([true, false, undefined])(
- "Memory 开关 %s 在三事件保持一致且不读取可变设置",
- async (memoryEnabled) => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "memory-test",
- });
- const seed = {
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: 1,
- memoryEnabled,
- extraDetail: {},
- };
- // 事实可能先于 ACK 到达,必须复用既有缓冲顺序。
- supervisor.handleFact({
- ...commonFact("memory-start", 1),
- kind: "turn.started",
- memoryEnabled,
- });
- supervisor.acceptPromptSeed(seed);
- seed.memoryEnabled = !memoryEnabled;
- supervisor.handleFact({
- ...commonFact("memory-tool", 2),
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "read-1",
- toolName: "Read",
- });
- supervisor.handleFact({
- ...commonFact("memory-tool-end", 3),
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "read-1",
- toolName: "Read",
- });
- supervisor.handleFact({
- ...commonFact("memory-end", 4),
- kind: "turn.terminal",
- status: "success",
- resultType: "success",
- });
- supervisor.acceptPromptSeed(seed);
- await vi.waitFor(() =>
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(1),
- );
- for (const name of ["send_btn", "message_completion", "agent_step"]) {
- const reports = reportsByElement(reportTelemetryEvent, name);
- expect(reports).toHaveLength(1);
- expect(reports[0].eventExtraDetail.memory_enabled).toBe(
- memoryEnabled === undefined ? "" : memoryEnabled ? "1" : "0",
- );
- }
- supervisor.dispose();
- },
- );
- it.each([true, false])(
- "后台轮从 live fact 继承 Memory %s 且不伪造 send_btn",
- async (memoryEnabled) => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "memory-background",
- });
- supervisor.handleFact({
- ...commonFact("bg-start", 1),
- kind: "turn.started",
- inputSource: "background_task",
- memoryEnabled,
- });
- supervisor.handleFact({
- ...commonFact("bg-end", 2),
- kind: "turn.terminal",
- status: "success",
- resultType: "success",
- });
- await vi.waitFor(() =>
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(1),
- );
- expect(reportsByElement(reportTelemetryEvent, "send_btn")).toHaveLength(0);
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion")[0].eventExtraDetail
- .memory_enabled,
- ).toBe(memoryEnabled ? "1" : "0");
- supervisor.dispose();
- },
- );
- it.each(["zai", "bigmodel"])(
- "%s 新旧身份在实际请求换模后的完成/step 上报对齐",
- async (family) => {
- const run = async (modern: boolean) => {
- resetMessageTelemetryForTest();
- const { platform, reportTelemetryEvent, reportArmsCustomEvent } = createPlatform();
- // perf_ui_* 走模块级 reporter,与 plan_* 直接经 platform 上报不同,需显式挂载才能观察到。
- setUiPerfArmsReporter(platform);
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "compat-workspace",
- now: () => now,
- });
- const detach = supervisor.attachForeground({}, "session-1");
- const team = modern
- ? `account:${family}-team-coding-plan`
- : `builtin:${family}-coding-plan`;
- const start = modern ? `account:${family}-start-plan` : `builtin:${family}-start-plan`;
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: now,
- extraDetail: buildV4ConversationPromptTelemetryExtraDetail({
- configProvider: team,
- modelName: "GLM-5.3",
- providerBaseURL: "https://example.com/api",
- planIdentitySnapshot: {
- generatedAt: 1,
- planStatus: "coding_plan",
- planProductId: "team",
- },
- }),
- });
- supervisor.handleFact({
- ...commonFact("start", 1),
- kind: "turn.started",
- executionKind: "agent",
- });
- let seq = 2;
- for (const providerId of [team, start]) {
- now += 10;
- const requestId = `request-${seq}`;
- const fact = Object.freeze({
- // plan_request 按 eventId 在模块级去重且跨用例保留;family/run 前缀保证每轮都能观察到上报。
- ...commonFact(`${family}-${modern ? "modern" : "legacy"}-request-${seq}`, seq++),
- kind: "model.request.status" as const,
- requestId,
- status: "model_request_started" as const,
- providerId,
- modelId: "GLM-5.3",
- providerHostname: "example.com",
- transport: "fetch" as const,
- querySource: "build",
- attempt: 1,
- maxAttempts: 1,
- });
- supervisor.handleFact(fact);
- expect(fact.providerId).toBe(providerId);
- supervisor.handleFact({
- ...commonFact(`chunk-${seq}`, seq++),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 5,
- firstChunk: true,
- });
- supervisor.handleFact({
- ...commonFact(`usage-${seq}`, seq++),
- kind: "usage.delta",
- requestId,
- providerId,
- modelId: "GLM-5.3",
- providerHostname: "example.com",
- inputTokens: 10,
- outputTokens: 5,
- totalTokens: 15,
- });
- supervisor.handleFact({
- ...commonFact(`tool-${seq}`, seq++),
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: requestId,
- toolName: "Read",
- });
- supervisor.handleFact({
- ...commonFact(`end-${seq}`, seq++),
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: requestId,
- toolName: "Read",
- });
- }
- now += 10;
- const terminal = {
- ...commonFact("terminal", seq++),
- kind: "turn.terminal" as const,
- status: "success" as const,
- resultType: "complete" as const,
- };
- supervisor.handleFact(terminal);
- supervisor.handleFact(terminal);
- await supervisor.flushReportsForTest();
- const reports = reportTelemetryEvent.mock.calls.map(([payload]) => ({
- element: payload.elementName,
- talkId: payload.talkId,
- messageId: payload.messageId,
- // step_id 是每次采集随机生成值,不是统计维度;其他字段完整成对比较。
- detail: Object.fromEntries(
- Object.entries(payload.eventExtraDetail).filter(([key]) => key !== "step_id"),
- ),
- }));
- expect(reports.filter((event) => event.element === "message_completion")).toHaveLength(1);
- expect(
- reports.find((event) => event.element === "message_completion")?.detail,
- ).toMatchObject({
- model_provider: `builtin:${family}-start-plan`,
- model_name: `builtin:${family}-start-plan/GLM-5.3`,
- input_tokens: "20",
- output_tokens: "10",
- });
- expect(
- reports
- .filter((event) => event.element === "agent_step")
- .map((event) => event.detail.model_provider),
- ).toEqual(
- expect.arrayContaining([`builtin:${family}-coding-plan`, `builtin:${family}-start-plan`]),
- );
- // ARMS 侧复用同一份 legacy detail:内置 provider 的旧报表身份必须保留、模型按白名单规范化,
- // 否则内置用户在 plan_ttft / perf_ui_* 看板里会整体落入 custom。
- expect(armsByName(reportArmsCustomEvent, "plan_ttft")[0]?.properties).toMatchObject({
- provider_id: `builtin:${family}-start-plan`,
- provider_scope: "builtin",
- model_name: "glm-5.3",
- });
- expect(armsByName(reportArmsCustomEvent, "perf_ui_first_token")[0]?.properties?.model).toBe(
- "glm-5.3",
- );
- // plan_request 直接使用协议事件里的 provider(account:* 或旧 builtin:*),两种命名空间都应保留。
- const planRequests = armsByName(reportArmsCustomEvent, "plan_request");
- expect(planRequests.map((payload) => payload.properties?.provider_id)).toEqual([
- team,
- start,
- ]);
- for (const payload of planRequests) {
- expect(payload.properties).toMatchObject({
- provider_scope: "builtin",
- model_name: "glm-5.3",
- });
- }
- detach();
- supervisor.dispose();
- return reports;
- };
- expect(await run(true)).toEqual(await run(false));
- },
- );
- beforeEach(() => {
- resetMessageTelemetryForTest();
- clearUiPerfArmsReporterForTest();
- });
- afterEach(() => {
- clearUiPerfArmsReporterForTest();
- resetMessageTelemetryForTest();
- });
- it("send seed 以 V4 config provider 为 model_provider,并保留旧 model value 与 agent 口径", () => {
- expect(
- buildV4ConversationPromptTelemetryExtraDetail({
- agentProvider: "glm",
- configProvider: "custom-provider-id",
- modelName: "custom-model",
- askMode: "plan",
- providerBaseURL: "https://api.example.com/v1/messages?token=secret",
- planIdentitySnapshot: {
- generatedAt: 123,
- planStatus: "coding_plan",
- planProductId: "product-1",
- },
- }),
- ).toEqual({
- ask_mode: "plan",
- model_name: "custom:custom-provider-id:custom-model",
- model_provider: "custom-provider-id",
- provider_name: "api.example.com",
- agent: "glm",
- plan_status: "coding_plan",
- plan_product_id: "product-1",
- message_source: "chat",
- task_trigger: "",
- });
- });
- it("send seed 缺少显式运行时 provider 时沿用 ZCode Agent 默认值", () => {
- expect(
- buildV4ConversationPromptTelemetryExtraDetail({
- configProvider: "glm",
- modelName: "glm-model",
- askMode: "build",
- }),
- ).toMatchObject({
- agent: "glm",
- message_source: "chat",
- model_name: "glm-model",
- model_provider: "glm",
- task_trigger: "",
- });
- });
- it("沿用旧 payload 收口 send/completion/step 与 foreground ARMS", async () => {
- const { platform, reportTelemetryEvent, reportArmsCustomEvent } = createPlatform();
- setUiPerfArmsReporter(platform);
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "base\u0000workspace\u00001",
- now: () => now,
- });
- const detach = supervisor.attachForeground({}, "session-1");
- supervisor.recordComposerFocus();
- now = 120;
- supervisor.recordComposerTextChange("hello");
- now = 200;
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: now,
- extraDetail: {
- ask_mode: "build",
- model_name: "seed-model",
- model_provider: "seed-provider",
- agent: "glm",
- plan_status: "unknown",
- plan_product_id: "",
- },
- });
- const send = reportsByElement(reportTelemetryEvent, "send_btn");
- expect(send).toHaveLength(1);
- expect(send[0]).toMatchObject({
- elementName: "send_btn",
- eventRegion: "app",
- eventType: "ck",
- talkId: "session-1",
- messageId: "command-1",
- eventExtraDetail: {
- ask_mode: "build",
- model_name: "seed-model",
- model_provider: "seed-provider",
- agent: "glm",
- plan_status: "unknown",
- plan_product_id: "",
- input_start_time: "100",
- input_first_char_time: "120",
- input_send_time: "200",
- },
- });
- now = 210;
- supervisor.handleFact({
- ...commonFact("event-start", 1),
- kind: "turn.started",
- executionKind: "agent",
- });
- now = 220;
- supervisor.handleFact({
- ...commonFact("event-model", 2),
- kind: "model.request.status",
- requestId: "request-1",
- status: "model_request_started",
- providerId: "runtime-provider",
- modelId: "runtime-model",
- providerHostname: "runtime.example.com",
- transport: "fetch",
- querySource: "build",
- attempt: 1,
- maxAttempts: 3,
- });
- now = 250;
- supervisor.handleFact({
- ...commonFact("event-thought", 3),
- kind: "stream.chunk",
- channel: "thought",
- chunkLength: 10,
- firstChunk: true,
- });
- now = 260;
- supervisor.handleFact({
- ...commonFact("event-permission-request", 4),
- kind: "permission.lifecycle",
- phase: "requested",
- requestId: "permission-1",
- toolCallId: "tool-1",
- toolName: "bash",
- });
- now = 300;
- supervisor.handleFact({
- ...commonFact("event-tool-start", 5),
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "tool-1",
- toolName: "bash",
- });
- now = 350;
- supervisor.handleFact({
- ...commonFact("event-permission-response", 6),
- kind: "permission.lifecycle",
- phase: "resolved",
- requestId: "permission-1",
- toolCallId: "tool-1",
- toolName: "bash",
- decision: "allow",
- });
- now = 500;
- supervisor.handleFact({
- ...commonFact("event-tool-complete", 7),
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "tool-1",
- toolName: "bash",
- parentToolCallId: "parent-tool",
- childToolCallId: "child-tool",
- childSessionId: "child-session",
- agentId: "agent-1",
- agentType: "subagent",
- performance: {
- totalMs: 200,
- permissionWaitMs: 90,
- commandRunMs: 110,
- exitCode: 0,
- timedOut: false,
- outputBytes: 42,
- commandCategory: "read",
- commandHash: "0123456789abcdef",
- workspaceKind: "local",
- },
- });
- now = 600;
- supervisor.handleFact({
- ...commonFact("event-text-1", 8),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 3,
- firstChunk: false,
- assistantMessageId: "assistant-1",
- });
- now = 3_600;
- supervisor.handleFact({
- ...commonFact("event-text-2", 9),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 3,
- firstChunk: false,
- assistantMessageId: "assistant-1",
- });
- expect(armsByName(reportArmsCustomEvent, "perf_ui_stream_stall")).toHaveLength(0);
- now = 6_601;
- supervisor.handleFact({
- ...commonFact("event-text-3", 10),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 3,
- firstChunk: false,
- assistantMessageId: "assistant-1",
- });
- expect(armsByName(reportArmsCustomEvent, "perf_ui_stream_stall")).toEqual([
- {
- name: "perf_ui_stream_stall",
- group: "ui_perf",
- value: 3001,
- properties: {
- stall_ms: 3001,
- waiting_tool: false,
- // `runtime-provider` 不是内置 provider,复合模型值必须归一,避免把自定义
- // provider 名与模型名透传到 ARMS。
- model: "custom",
- chunk_type: "message",
- talk_id: "session-1",
- message_id: "assistant-1",
- },
- },
- ]);
- now = 6_700;
- supervisor.handleFact({
- ...commonFact("event-usage", 11),
- kind: "usage.delta",
- inputTokens: 10,
- outputTokens: 5,
- totalTokens: 18,
- reasoningTokens: 3,
- cacheReadTokens: 2,
- cacheWriteTokens: 1,
- });
- now = 7_000;
- supervisor.handleFact({
- ...commonFact("event-terminal", 12),
- kind: "turn.terminal",
- status: "success",
- resultType: "complete",
- durationMs: 6_800,
- tokenCount: 18,
- toolCallCount: 1,
- });
- await supervisor.flushReportsForTest();
- const completion = reportsByElement(reportTelemetryEvent, "message_completion");
- expect(completion).toHaveLength(1);
- expect(completion[0]).toMatchObject({
- elementName: "message_completion",
- eventRegion: "app",
- eventType: "agent_trace",
- talkId: "session-1",
- messageId: "command-1",
- eventExtraDetail: {
- ask_mode: "build",
- model_name: "runtime-provider/runtime-model",
- model_provider: "runtime-provider",
- provider_name: "runtime.example.com",
- agent: "glm",
- plan_status: "unknown",
- plan_product_id: "",
- input_tokens: "10",
- output_tokens: "5",
- reasoning_tokens: "3",
- cached_input_tokens: "2",
- cache_write_input_tokens: "1",
- tool_use_prompt_tokens: "0",
- total_tokens: "18",
- duration_ms: "6800",
- waiting_ms: "90",
- generated_code_lines: "0",
- file_change_cnt: "0",
- time_to_first_token: "50",
- request_time: "200",
- status: "success",
- agent_step_cnt: "3",
- retry_cnt: "0",
- tool_call_total: "1",
- tool_call_failed: "0",
- error_type: "",
- error_msg: "",
- },
- });
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(3);
- expect(armsByName(reportArmsCustomEvent, "plan_request")).toHaveLength(1);
- expect(armsByName(reportArmsCustomEvent, "perf_ui_tool_call_detail")[0]).toMatchObject({
- properties: {
- tool_call_id: "tool-1",
- parent_tool_call_id: "parent-tool",
- child_tool_call_id: "child-tool",
- child_session_id: "child-session",
- agent_id: "agent-1",
- agent_type: "subagent",
- },
- });
- expect(armsByName(reportArmsCustomEvent, "perf_ui_first_token")[0]?.value).toBe(50);
- expect(armsByName(reportArmsCustomEvent, "plan_ttft")[0]?.value).toBe(50);
- expect(armsByName(reportArmsCustomEvent, "perf_ui_message_complete")[0]?.value).toBe(6_800);
- expect(armsByName(reportArmsCustomEvent, "perf_ui_turn_breakdown")[0]).toMatchObject({
- value: 6_800,
- properties: {
- waiting_ms: 90,
- file_change_cnt: 0,
- generated_code_lines: 0,
- },
- });
- detach();
- supervisor.dispose();
- });
- it.each([
- {
- phase: "completed" as const,
- errorMessage: undefined,
- expectedStatus: "success",
- expectedErrorType: "",
- expectedErrorMessage: "",
- expectedFailedCount: "0",
- },
- {
- phase: "failed" as const,
- errorMessage: "exit 2",
- expectedStatus: "fail",
- expectedErrorType: "TOOL_EXEC_ERROR",
- expectedErrorMessage: "exit 2",
- expectedFailedCount: "1",
- },
- ])(
- "工具 $phase terminal 立即收口 /event/report agent_step,不等待 turn terminal",
- async ({
- phase,
- errorMessage,
- expectedStatus,
- expectedErrorType,
- expectedErrorMessage,
- expectedFailedCount,
- }) => {
- const { platform, reportTelemetryEvent } = createPlatform();
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: `scope-tool-terminal-${phase}`,
- now: () => now,
- });
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: now,
- extraDetail: { model_name: "model", model_provider: "provider" },
- });
- now = 120;
- supervisor.handleFact({
- ...commonFact(`turn-start-${phase}`, 1),
- kind: "turn.started",
- executionKind: "agent",
- });
- now = 200;
- supervisor.handleFact({
- ...commonFact(`tool-start-${phase}`, 2),
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "tool-1",
- toolName: "Bash",
- });
- now = 350;
- supervisor.handleFact({
- ...commonFact(`tool-terminal-${phase}`, 3),
- kind: "tool.lifecycle",
- phase,
- toolCallId: "tool-1",
- toolName: "Bash",
- ...(errorMessage ? { errorMessage } : {}),
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toEqual([]);
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toEqual([
- expect.objectContaining({
- elementName: "agent_step",
- eventType: "agent_trace",
- talkId: "session-1",
- messageId: "command-1",
- eventExtraDetail: expect.objectContaining({
- step_type: "tool_call",
- tool_name: "Bash",
- tool_call_id: "tool-1",
- duration_ms: "150",
- status: expectedStatus,
- error_type: expectedErrorType,
- error_msg: expectedErrorMessage ? "[redacted]" : "",
- }),
- }),
- ]);
- now = 900;
- supervisor.handleFact({
- ...commonFact(`turn-terminal-${phase}`, 4),
- kind: "turn.terminal",
- status: "success",
- resultType: "complete",
- });
- await supervisor.flushReportsForTest();
- expect(
- reportTelemetryEvent.mock.calls
- .map(([payload]) => payload.elementName)
- .filter((name) => name === "agent_step" || name === "message_completion"),
- ).toEqual(["agent_step", "message_completion"]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toEqual([
- expect.objectContaining({
- eventExtraDetail: expect.objectContaining({
- agent_step_cnt: "1",
- tool_call_total: "1",
- tool_call_failed: expectedFailedCount,
- }),
- }),
- ]);
- supervisor.dispose();
- },
- );
- it.each([
- ["main-provider", "child-provider", "main-provider", "child-provider"],
- [
- "account:bigmodel-team-coding-plan",
- "account:zai-start-plan",
- "builtin:bigmodel-coding-plan",
- "builtin:zai-start-plan",
- ],
- ])(
- "主模型 %s、前台 Subagent %s、后续主模型分别保留 usage 与旧统计身份",
- async (mainId, childId, mainReportId, childReportId) => {
- const { platform, reportTelemetryEvent } = createPlatform();
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-foreground-subagent-usage",
- now: () => now,
- });
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: now,
- extraDetail: {
- model_name: "seed-model",
- model_provider: "seed-provider",
- },
- });
- supervisor.handleFact({
- ...commonFact("parent-turn-start", 1),
- kind: "turn.started",
- executionKind: "agent",
- });
- now = 110;
- supervisor.handleFact({
- ...commonFact("main-request-1-start", 2),
- kind: "model.request.status",
- requestId: "main-request-1",
- status: "model_request_started",
- providerId: mainId,
- modelId: "main-model",
- providerHostname: "main.example.com",
- transport: "sse",
- querySource: "main_turn",
- attempt: 1,
- maxAttempts: 1,
- });
- now = 120;
- supervisor.handleFact({
- ...commonFact("main-reasoning", 3),
- kind: "stream.chunk",
- channel: "thought",
- chunkLength: 3,
- firstChunk: true,
- });
- now = 130;
- supervisor.handleFact({
- ...commonFact("main-generation-1", 4),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 4,
- firstChunk: true,
- });
- supervisor.handleFact({
- ...commonFact("main-usage-1", 5),
- kind: "usage.delta",
- requestId: "main-request-1",
- providerId: mainId,
- modelId: "main-model",
- providerHostname: "main.example.com",
- inputTokens: 80,
- outputTokens: 20,
- totalTokens: 100,
- reasoningTokens: 5,
- cacheReadTokens: 10,
- cacheWriteTokens: 2,
- });
- now = 140;
- supervisor.handleFact({
- ...commonFact("agent-tool-start", 6),
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "agent-tool-1",
- toolName: "Agent",
- });
- supervisor.handleFact({
- ...commonFact("subagent-spawned", 7),
- kind: "subagent.lifecycle",
- phase: "spawned",
- agentId: "agent-1",
- agentType: "explore",
- childSessionId: "child-session-1",
- parentToolCallId: "agent-tool-1",
- background: false,
- status: "running",
- });
- supervisor.handleFact({
- // detached child 的 eventId 允许与父 session 同号,去重必须带 sessionId。
- ...commonFact("main-request-1-start", 1, "child-session-1"),
- sourceCommandId: "child-command-1",
- turnId: "child-turn-1",
- kind: "model.request.status",
- requestId: "child-request-1",
- status: "model_request_started",
- providerId: childId,
- modelId: "child-model",
- providerHostname: "child.example.com",
- transport: "sse",
- querySource: "subagent",
- attempt: 1,
- maxAttempts: 1,
- });
- supervisor.handleFact({
- ...commonFact("main-usage-1", 2, "child-session-1"),
- sourceCommandId: "child-command-1",
- turnId: "child-turn-1",
- kind: "usage.delta",
- requestId: "child-request-1",
- providerId: childId,
- modelId: "child-model",
- providerHostname: "child.example.com",
- inputTokens: 150,
- outputTokens: 50,
- totalTokens: 200,
- reasoningTokens: 12,
- cacheReadTokens: 30,
- cacheWriteTokens: 4,
- });
- supervisor.handleFact({
- ...commonFact("subagent-stopped", 8),
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "agent-1",
- agentType: "explore",
- childSessionId: "child-session-1",
- parentToolCallId: "agent-tool-1",
- background: false,
- status: "completed",
- });
- now = 200;
- supervisor.handleFact({
- ...commonFact("agent-tool-complete", 9),
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "agent-tool-1",
- toolName: "Agent",
- });
- now = 210;
- supervisor.handleFact({
- ...commonFact("main-request-2-start", 10),
- kind: "model.request.status",
- requestId: "main-request-2",
- status: "model_request_started",
- providerId: mainId,
- modelId: "main-model",
- providerHostname: "main.example.com",
- transport: "sse",
- querySource: "main_turn",
- attempt: 1,
- maxAttempts: 1,
- });
- supervisor.handleFact({
- ...commonFact("main-generation-2", 11),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 4,
- firstChunk: true,
- });
- supervisor.handleFact({
- ...commonFact("main-usage-2", 12),
- kind: "usage.delta",
- requestId: "main-request-2",
- providerId: mainId,
- modelId: "main-model",
- providerHostname: "main.example.com",
- inputTokens: 90,
- outputTokens: 30,
- totalTokens: 120,
- reasoningTokens: 0,
- cacheReadTokens: 20,
- cacheWriteTokens: 0,
- });
- now = 240;
- supervisor.handleFact({
- ...commonFact("parent-terminal", 13),
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- const steps = reportsByElement(reportTelemetryEvent, "agent_step");
- expect(steps).toHaveLength(4);
- expect(steps.map((step) => step.eventExtraDetail)).toEqual([
- expect.objectContaining({
- step_type: "reasoning",
- model_name: `${mainReportId}/main-model`,
- total_tokens: "0",
- }),
- expect.objectContaining({
- step_type: "generation",
- model_name: `${mainReportId}/main-model`,
- model_request_id: "main-request-1",
- total_tokens: "100",
- }),
- expect.objectContaining({
- step_type: "tool_call",
- tool_name: "Agent",
- model_name: `${childReportId}/child-model`,
- token_usage_scope: "subagent_requests",
- total_tokens: "200",
- }),
- expect.objectContaining({
- step_type: "generation",
- model_name: `${mainReportId}/main-model`,
- model_request_id: "main-request-2",
- total_tokens: "120",
- }),
- ]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toEqual([
- expect.objectContaining({
- messageId: "command-1",
- eventExtraDetail: expect.objectContaining({
- duration_ms: "140",
- request_time: "100",
- agent_step_cnt: "4",
- }),
- }),
- ]);
- expect(
- reportTelemetryEvent.mock.calls
- .map(([payload]) => payload.elementName)
- .filter(
- (elementName) => elementName === "agent_step" || elementName === "message_completion",
- ),
- ).toEqual(["agent_step", "agent_step", "agent_step", "agent_step", "message_completion"]);
- supervisor.dispose();
- },
- );
- it("父 Agent 与镜像 child tool 共享 agent_id,child tool 使用真实 Subagent 模型", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-subagent-tool-agent-id",
- now: () => now,
- });
- supervisor.acceptPromptSeed({
- sessionId: "parent-session",
- sourceCommandId: "parent-command",
- sendTime: now,
- extraDetail: {
- model_name: "main-provider/GLM-5.2-Highspeed",
- model_provider: "main-provider",
- },
- });
- supervisor.handleFact({
- ...commonFact("parent-turn-start", 1, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.started",
- executionKind: "agent",
- });
- supervisor.handleFact({
- ...commonFact("agent-tool-start", 2, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "call-parent-agent",
- toolName: "Agent",
- });
- supervisor.handleFact({
- ...commonFact("subagent-spawned", 3, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "subagent.lifecycle",
- phase: "spawned",
- agentId: "agent-child",
- agentType: "Explore",
- childSessionId: "child-session",
- parentToolCallId: "call-parent-agent",
- background: false,
- status: "running",
- });
- supervisor.handleFact({
- ...commonFact("child-request-start", 1, "child-session"),
- sourceCommandId: "child-command",
- turnId: "child-turn",
- kind: "model.request.status",
- requestId: "child-request",
- status: "model_request_started",
- providerId: "qwen-provider",
- modelId: "qwen3.7-max",
- providerHostname: "provider.example.com",
- transport: "sse",
- querySource: "subagent",
- attempt: 1,
- maxAttempts: 1,
- });
- supervisor.handleFact({
- ...commonFact("child-usage", 2, "child-session"),
- sourceCommandId: "child-command",
- turnId: "child-turn",
- kind: "usage.delta",
- requestId: "child-request",
- providerId: "qwen-provider",
- modelId: "qwen3.7-max",
- providerHostname: "provider.example.com",
- inputTokens: 100,
- outputTokens: 20,
- totalTokens: 120,
- reasoningTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 0,
- });
- const mirroredToolId = "tool_subagent_agent-child_toolu-child";
- const mirroredRelation = {
- agentId: "agent-child",
- agentType: "Explore",
- childSessionId: "child-session",
- childToolCallId: "toolu-child",
- parentToolCallId: "call-parent-agent",
- };
- now = 130;
- supervisor.handleFact({
- ...commonFact("child-tool-start", 4, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: mirroredToolId,
- toolName: "WebFetch",
- ...mirroredRelation,
- });
- now = 140;
- supervisor.handleFact({
- ...commonFact("subagent-stopped", 5, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "agent-child",
- agentType: "Explore",
- childSessionId: "child-session",
- parentToolCallId: "call-parent-agent",
- background: false,
- status: "completed",
- });
- now = 150;
- supervisor.handleFact({
- // lifecycle 与 mirror 不同流;工具终态即使晚于 stopped 仍需取得 child 归因。
- ...commonFact("child-tool-complete", 6, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: mirroredToolId,
- toolName: "WebFetch",
- durationMs: 20,
- ...mirroredRelation,
- });
- now = 170;
- supervisor.handleFact({
- ...commonFact("agent-tool-complete", 7, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "call-parent-agent",
- toolName: "Agent",
- });
- await supervisor.flushReportsForTest();
- const steps = reportsByElement(reportTelemetryEvent, "agent_step");
- const agentStep = steps.find((step) => step.eventExtraDetail.tool_name === "Agent");
- const childToolStep = steps.find((step) => step.eventExtraDetail.tool_name === "WebFetch");
- expect(agentStep?.eventExtraDetail).toEqual(
- expect.objectContaining({
- tool_call_id: "call-parent-agent",
- agent_id: "agent-child",
- agent_role: "foreground subagent",
- model_name: "qwen-provider/qwen3.7-max",
- token_usage_scope: "subagent_requests",
- total_tokens: "120",
- }),
- );
- expect(childToolStep?.eventExtraDetail).toEqual(
- expect.objectContaining({
- tool_call_id: mirroredToolId,
- agent_id: "agent-child",
- agent_role: "foreground subagent",
- model_name: "qwen-provider/qwen3.7-max",
- model_provider: "qwen-provider",
- token_usage_scope: "",
- total_tokens: "0",
- }),
- );
- supervisor.dispose();
- });
- it("父 Agent 非 timeout 终态先到时仍等待 SubagentStopped 回填晚到 usage", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-cancelled-foreground-subagent",
- now: () => now,
- });
- startForegroundSubagent(supervisor, now);
- startForegroundSubagentRequest(supervisor);
- now = 200;
- terminateForegroundSubagentParent(supervisor);
- reportForegroundSubagentUsage(supervisor);
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(0);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- now = 250;
- supervisor.handleFact({
- ...commonFact("subagent-stopped", 6, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "agent-1",
- childSessionId: "child-session",
- parentToolCallId: "agent-tool",
- background: false,
- status: "failed",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toEqual([
- expect.objectContaining({
- messageId: "parent-command",
- eventExtraDetail: expect.objectContaining({
- step_type: "tool_call",
- tool_name: "Agent",
- status: "fail",
- model_name: "qwen-provider/qwen3.7-max",
- model_provider: "qwen-provider",
- provider_name: "provider.example.com",
- model_request_id: "child-request",
- model_request_count: "1",
- token_usage_scope: "subagent_requests",
- input_tokens: "8616",
- output_tokens: "69",
- cache_write_input_tokens: "5797",
- total_tokens: "8685",
- }),
- }),
- ]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toEqual([
- expect.objectContaining({
- messageId: "parent-command",
- eventExtraDetail: expect.objectContaining({
- request_time: "100",
- duration_ms: "100",
- agent_step_cnt: "1",
- }),
- }),
- ]);
- expect(
- reportTelemetryEvent.mock.calls
- .map(([payload]) => payload.elementName)
- .filter(
- (elementName) => elementName === "agent_step" || elementName === "message_completion",
- ),
- ).toEqual(["agent_step", "message_completion"]);
- supervisor.dispose();
- });
- it("main completion 没有 subagent 结果时上报 0,background child 停止本身不计入结果", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-subagent-result-none",
- });
- supervisor.acceptPromptSeed({
- sessionId: "parent-session",
- sourceCommandId: "parent-command",
- sendTime: 100,
- extraDetail: { model_name: "main-model", model_provider: "main-provider" },
- });
- supervisor.handleFact({
- ...commonFact("parent-turn-start", 1, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.started",
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "background-agent",
- childSessionId: "background-child",
- sourceCommandId: "parent-command",
- parentToolCallId: "background-agent-tool",
- });
- supervisor.handleFact({
- ...commonFact("background-stopped", 2, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "background-agent",
- childSessionId: "background-child",
- parentToolCallId: "background-agent-tool",
- background: true,
- status: "completed",
- });
- supervisor.handleFact({
- ...commonFact("parent-terminal", 3, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(completionAgentCompositions(reportTelemetryEvent)).toEqual(["main_only"]);
- supervisor.dispose();
- });
- it("foreground subagent 结果在 child stopped 后计为 main_plus_fg", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-subagent-result-foreground",
- });
- startForegroundSubagent(supervisor, 100);
- supervisor.handleFact({
- ...commonFact("foreground-stopped", 4, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "agent-1",
- childSessionId: "child-session",
- parentToolCallId: "agent-tool",
- background: false,
- status: "completed",
- });
- supervisor.handleFact({
- ...commonFact("parent-terminal", 5, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(completionAgentCompositions(reportTelemetryEvent)).toEqual(["main_plus_fg"]);
- supervisor.dispose();
- });
- it("background notification dequeue 后启动的 main turn 计为 main_plus_bg", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-subagent-result-background",
- });
- supervisor.handleFact({
- ...commonFact("background-wake-start", 1, "session-1"),
- sourceCommandId: "background-wake-command",
- kind: "turn.started",
- inputSource: "background_task",
- backgroundSource: "subagent",
- });
- supervisor.handleFact({
- ...commonFact("background-wake-terminal", 2, "session-1"),
- sourceCommandId: "background-wake-command",
- kind: "turn.terminal",
- status: "success",
- backgroundSubagentResultConsumed: true,
- });
- await supervisor.flushReportsForTest();
- expect(completionAgentCompositions(reportTelemetryEvent)).toEqual(["main_plus_bg"]);
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion")[0]?.eventExtraDetail,
- ).toMatchObject({
- message_source: "background_subagent",
- });
- supervisor.dispose();
- });
- it("background notification 无具体来源时保留 background_task 来源", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background-bash-composition",
- });
- supervisor.handleFact({
- ...commonFact("background-bash-start", 1, "session-1"),
- sourceCommandId: "background-bash-command",
- kind: "turn.started",
- inputSource: "background_task",
- });
- supervisor.handleFact({
- ...commonFact("background-bash-terminal", 2, "session-1"),
- sourceCommandId: "background-bash-command",
- kind: "turn.terminal",
- status: "success",
- backgroundSubagentResultConsumed: true,
- });
- await supervisor.flushReportsForTest();
- expect(completionAgentCompositions(reportTelemetryEvent)).toEqual(["main_plus_bg"]);
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion")[0]?.eventExtraDetail,
- ).toMatchObject({ message_source: "background_task" });
- supervisor.dispose();
- });
- it("background Bash 发起后在 active-loop 消费 subagent 结果仍保留 background_task 来源", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background-bash-active-loop-subagent",
- });
- supervisor.handleFact({
- ...commonFact("background-bash-active-loop-start", 1, "session-1"),
- sourceCommandId: "background-bash-active-loop-command",
- kind: "turn.started",
- inputSource: "background_task",
- backgroundSource: "bash",
- });
- supervisor.handleFact({
- ...commonFact("background-bash-active-loop-terminal", 2, "session-1"),
- sourceCommandId: "background-bash-active-loop-command",
- kind: "turn.terminal",
- status: "success",
- backgroundSubagentResultConsumed: true,
- });
- await supervisor.flushReportsForTest();
- expect(completionAgentCompositions(reportTelemetryEvent)).toEqual(["main_plus_bg"]);
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion")[0]?.eventExtraDetail,
- ).toMatchObject({ message_source: "background_task" });
- supervisor.dispose();
- });
- it("用户 query active-loop 消费 background subagent 结果仍保持 chat 来源", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-user-query-background-result",
- });
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "user-command",
- sendTime: 1,
- extraDetail: { message_source: "chat" },
- });
- supervisor.handleFact({
- ...commonFact("user-turn-start", 1, "session-1"),
- sourceCommandId: "user-command",
- kind: "turn.started",
- executionKind: "agent",
- });
- supervisor.handleFact({
- ...commonFact("user-turn-terminal", 2, "session-1"),
- sourceCommandId: "user-command",
- kind: "turn.terminal",
- status: "success",
- backgroundSubagentResultConsumed: true,
- });
- await supervisor.flushReportsForTest();
- expect(completionAgentCompositions(reportTelemetryEvent)).toEqual(["main_plus_bg"]);
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion")[0]?.eventExtraDetail,
- ).toMatchObject({
- message_source: "chat",
- });
- supervisor.dispose();
- });
- it("同一 main wake turn 同时消费结果时计为 main_plus_fg_bg", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-subagent-result-mixed",
- });
- supervisor.handleFact({
- ...commonFact("mixed-wake-start", 1, "session-1"),
- sourceCommandId: "mixed-wake-command",
- kind: "turn.started",
- inputSource: "background_task",
- });
- supervisor.handleFact({
- ...commonFact("mixed-agent-start", 2, "session-1"),
- sourceCommandId: "mixed-wake-command",
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "mixed-agent-tool",
- toolName: "Agent",
- });
- supervisor.handleFact({
- ...commonFact("mixed-foreground-spawned", 3, "session-1"),
- sourceCommandId: "mixed-wake-command",
- kind: "subagent.lifecycle",
- phase: "spawned",
- agentId: "foreground-agent",
- childSessionId: "mixed-foreground-child",
- parentToolCallId: "mixed-agent-tool",
- background: false,
- status: "running",
- });
- supervisor.handleFact({
- ...commonFact("mixed-foreground-stopped", 4, "session-1"),
- sourceCommandId: "mixed-wake-command",
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "foreground-agent",
- childSessionId: "mixed-foreground-child",
- parentToolCallId: "mixed-agent-tool",
- background: false,
- status: "completed",
- });
- supervisor.handleFact({
- ...commonFact("mixed-agent-completed", 5, "session-1"),
- sourceCommandId: "mixed-wake-command",
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "mixed-agent-tool",
- toolName: "Agent",
- });
- supervisor.handleFact({
- ...commonFact("mixed-wake-terminal", 6, "session-1"),
- sourceCommandId: "mixed-wake-command",
- kind: "turn.terminal",
- status: "success",
- backgroundSubagentResultConsumed: true,
- });
- await supervisor.flushReportsForTest();
- expect(completionAgentCompositions(reportTelemetryEvent)).toEqual(["main_plus_fg_bg"]);
- supervisor.dispose();
- });
- it("background child 工具逐条上报,Agent step 独占累计 token,不生成 thought/generation/completion", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background-tool-step",
- now: () => now,
- });
- supervisor.acceptPromptSeed({
- sessionId: "parent-session",
- sourceCommandId: "parent-command",
- sendTime: now,
- memoryEnabled: true,
- extraDetail: {},
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "agent-background",
- childSessionId: "child-background",
- });
- now = 120;
- supervisor.handleFact({
- ...commonFact("child-model-started", 1, "child-background"),
- sourceCommandId: undefined,
- kind: "model.request.status",
- requestId: "child-request",
- status: "model_request_started",
- providerId: "child-provider",
- modelId: "child-model",
- providerHostname: "child.example.com",
- transport: "sse",
- querySource: "subagent",
- attempt: 1,
- maxAttempts: 1,
- });
- now = 130;
- supervisor.handleFact({
- ...commonFact("child-thought", 2, "child-background"),
- sourceCommandId: undefined,
- kind: "stream.chunk",
- channel: "thought",
- chunkLength: 10,
- firstChunk: true,
- });
- now = 140;
- supervisor.handleFact({
- ...commonFact("child-text", 3, "child-background"),
- sourceCommandId: undefined,
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 10,
- firstChunk: true,
- assistantMessageId: "child-message",
- });
- now = 150;
- supervisor.handleFact({
- ...commonFact("child-usage", 4, "child-background"),
- sourceCommandId: undefined,
- kind: "usage.delta",
- requestId: "child-request",
- providerId: "child-provider",
- modelId: "child-model",
- inputTokens: 100,
- outputTokens: 20,
- totalTokens: 120,
- reasoningTokens: 3,
- cacheReadTokens: 4,
- cacheWriteTokens: 5,
- });
- now = 160;
- supervisor.handleFact({
- ...commonFact("child-tool-started", 5, "child-background"),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "child-tool",
- toolName: "Bash",
- });
- now = 180;
- supervisor.handleFact({
- ...commonFact("child-tool-completed", 6, "child-background"),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "child-tool",
- toolName: "Bash",
- });
- now = 190;
- supervisor.handleFact({
- ...commonFact("child-terminal", 7, "child-background"),
- sourceCommandId: undefined,
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(
- reportTelemetryEvent.mock.calls
- .map(([payload]) => payload.elementName)
- .filter((name) => name === "agent_step" || name === "message_completion"),
- ).toEqual(["agent_step", "agent_step"]);
- expect(
- reportsByElement(reportTelemetryEvent, "agent_step").map(
- (report) => report.eventExtraDetail.memory_enabled,
- ),
- ).toEqual(["1", "1"]);
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toEqual([
- expect.objectContaining({
- talkId: "parent-session",
- messageId: "parent-command",
- eventExtraDetail: expect.objectContaining({
- step_type: "tool_call",
- tool_name: "Bash",
- tool_call_id: "tool_subagent_agent-background_child-tool",
- loop_index: "1",
- agent_id: "agent-background",
- agent_role: "background subagent",
- model_name: "child-provider/child-model",
- model_provider: "child-provider",
- provider_name: "child.example.com",
- token_usage_scope: "",
- total_tokens: "0",
- duration_ms: "20",
- }),
- }),
- expect.objectContaining({
- talkId: "parent-session",
- messageId: "parent-command",
- eventExtraDetail: expect.objectContaining({
- step_type: "tool_call",
- tool_name: "Agent",
- tool_call_id: "tool_subagent_agent-background_agent-background-parent-tool",
- loop_index: "2",
- agent_id: "agent-background",
- agent_role: "background subagent",
- model_name: "child-provider/child-model",
- token_usage_scope: "subagent_requests",
- model_request_count: "1",
- input_tokens: "100",
- output_tokens: "20",
- total_tokens: "120",
- reasoning_tokens: "3",
- cached_tokens: "4",
- cache_write_input_tokens: "5",
- duration_ms: "90",
- status: "success",
- }),
- }),
- ]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- supervisor.dispose();
- });
- it.each(["success", "failed", "interrupted"] as const)(
- "background %s 终态保留每个 child 的累计 usage,重复 usage/stopped/terminal 不重复上报",
- async (status) => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "bg-usage",
- });
- startForegroundSubagent(supervisor, 10);
- for (const agentId of ["a", "b", "c"]) {
- spawnBackgroundSubagent(supervisor, { agentId, childSessionId: agentId });
- supervisor.handleFact({
- ...commonFact(`${agentId}-request`, 1, agentId),
- kind: "model.request.status",
- status: "model_request_started",
- requestId: `${agentId}-r1`,
- providerId: "provider",
- modelId: "model",
- transport: "sse",
- querySource: "subagent",
- attempt: 1,
- maxAttempts: 1,
- });
- }
- terminateForegroundSubagentParent(supervisor);
- supervisor.handleFact({
- ...commonFact("fg-stopped", 8, "parent-session"),
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "agent-1",
- childSessionId: "child-session",
- parentToolCallId: "agent-tool",
- background: false,
- status: "stopped",
- });
- await supervisor.flushReportsForTest();
- const parentCompletion = reportsByElement(reportTelemetryEvent, "message_completion");
- expect(parentCompletion).toHaveLength(1);
- for (const [index, agentId] of ["a", "b", "c"].entries()) {
- for (const [eventId, requestId] of [
- ["u1", "r1"],
- ["u1-copy", "r1"],
- ["u2", "r2"],
- ]) {
- supervisor.handleFact({
- ...commonFact(`${agentId}-${eventId}`, 10, agentId),
- kind: "usage.delta",
- requestId: `${agentId}-${requestId}`,
- providerId: "provider",
- modelId: "model",
- inputTokens: 100 * (index + 1),
- outputTokens: 20,
- totalTokens: 100 * (index + 1) + 20,
- reasoningTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 0,
- });
- }
- await supervisor.flushReportsForTest();
- expect(
- reportsByElement(reportTelemetryEvent, "agent_step").filter(
- (r) => r.eventExtraDetail.agent_id === agentId,
- ),
- ).toHaveLength(0);
- for (const eventId of ["terminal", "terminal-copy"]) {
- supervisor.handleFact({
- ...commonFact(`${agentId}-${eventId}`, 20, agentId),
- kind: "turn.terminal",
- status,
- ...(status !== "success"
- ? { errorCode: "CHILD_FAILED", errorMessage: "child stopped" }
- : {}),
- });
- }
- supervisor.handleFact({
- ...commonFact(`${agentId}-stopped`, 21, "parent-session"),
- kind: "subagent.lifecycle",
- phase: "stopped",
- background: true,
- agentId,
- childSessionId: agentId,
- parentToolCallId: `${agentId}-parent-tool`,
- status: "stopped",
- });
- }
- await supervisor.flushReportsForTest();
- const background = reportsByElement(reportTelemetryEvent, "agent_step").filter(
- (r) => r.eventExtraDetail.agent_role === "background subagent",
- );
- expect(background).toHaveLength(3);
- for (const [index, step] of background.entries()) {
- expect(step.eventExtraDetail).toMatchObject({
- tool_name: "Agent",
- loop_index: "1",
- model_request_count: "2",
- token_usage_scope: "subagent_requests",
- total_tokens: String(240 + index * 200),
- status: status === "success" ? "success" : "fail",
- error_msg: status === "success" ? "" : "[redacted]",
- });
- }
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toEqual(
- parentCompletion,
- );
- supervisor.dispose();
- },
- );
- it.each([
- { status: "completed", hasUsage: true },
- { status: "failed", hasUsage: true },
- { status: "stopped", hasUsage: true },
- { status: "completed", hasUsage: false },
- { status: "failed", hasUsage: false },
- { status: "stopped", hasUsage: false },
- ])(
- "background 仅 stopped=$status 也结算一次(hasUsage=$hasUsage)",
- async ({ status, hasUsage }) => {
- const { platform, reportTelemetryEvent, reportArmsCustomEvent } = createPlatform();
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "bg-stopped-usage",
- now: () => now,
- });
- spawnBackgroundSubagent(supervisor, { agentId: "bg", childSessionId: "child-bg" });
- supervisor.handleFact({
- ...commonFact("request", 1, "child-bg"),
- kind: "model.request.status",
- status: "model_request_started",
- requestId: "request-bg",
- providerId: "provider",
- modelId: "model",
- providerHostname: "provider.example.com",
- transport: "sse",
- querySource: "subagent",
- attempt: 1,
- maxAttempts: 1,
- });
- if (hasUsage) {
- supervisor.handleFact({
- ...commonFact("usage", 2, "child-bg"),
- kind: "usage.delta",
- requestId: "request-bg",
- inputTokens: 100,
- outputTokens: 20,
- totalTokens: 120,
- reasoningTokens: 5,
- cacheReadTokens: 30,
- cacheWriteTokens: 40,
- });
- }
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(0);
- now = 250;
- const armsBeforeStopped = reportArmsCustomEvent.mock.calls.slice();
- const stopped = {
- ...commonFact("stopped", 3, "parent-session"),
- kind: "subagent.lifecycle" as const,
- phase: "stopped" as const,
- background: true,
- agentId: "bg",
- childSessionId: "child-bg",
- parentToolCallId: "bg-parent-tool",
- status,
- ...(status === "completed"
- ? {}
- : { errorMessage: "provider failure or user cancellation" }),
- };
- supervisor.handleFact(stopped);
- await supervisor.flushReportsForTest();
- const reports = reportsByElement(reportTelemetryEvent, "agent_step");
- expect(reports).toEqual([
- expect.objectContaining({
- talkId: "parent-session",
- messageId: "parent-command",
- eventExtraDetail: expect.objectContaining({
- agent_id: "bg",
- agent_role: "background subagent",
- tool_name: "Agent",
- tool_call_id: "tool_subagent_bg_bg-parent-tool",
- child_session_id: "child-bg",
- parent_tool_call_id: "bg-parent-tool",
- loop_index: "1",
- duration_ms: "150",
- model_name: "provider/model",
- model_provider: "provider",
- provider_name: "provider.example.com",
- model_request_count: hasUsage ? "1" : "0",
- token_usage_scope: hasUsage ? "subagent_requests" : "",
- input_tokens: hasUsage ? "100" : "0",
- output_tokens: hasUsage ? "20" : "0",
- total_tokens: hasUsage ? "120" : "0",
- reasoning_tokens: hasUsage ? "5" : "0",
- cached_tokens: hasUsage ? "30" : "0",
- cache_write_input_tokens: hasUsage ? "40" : "0",
- status: status === "completed" ? "success" : "fail",
- error_type: status === "completed" ? "" : "TOOL_EXEC_ERROR",
- error_msg: status === "completed" ? "" : "[redacted]",
- }),
- }),
- ]);
- supervisor.handleFact({ ...stopped, eventId: "stopped-copy" });
- supervisor.handleFact({
- ...commonFact("late-terminal", 4, "child-bg"),
- kind: "turn.terminal",
- status: "failed",
- errorMessage: "late error must not replace the first terminal",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toEqual(reports);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- expect(reportsByElement(reportTelemetryEvent, "send_btn")).toHaveLength(0);
- expect(reportArmsCustomEvent.mock.calls).toEqual(armsBeforeStopped);
- supervisor.dispose();
- },
- );
- it("background child 的 loop_index 按 child 独立递增,并行 child 互不影响", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background-loop-index",
- now: () => 100,
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "agent-a",
- childSessionId: "child-a",
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "agent-b",
- childSessionId: "child-b",
- });
- for (const [index, childSessionId, , toolCallId] of [
- ["a-1", "child-a", "agent-a", "tool-a-1"],
- ["b-1", "child-b", "agent-b", "tool-b-1"],
- ["a-2", "child-a", "agent-a", "tool-a-2"],
- ["b-2", "child-b", "agent-b", "tool-b-2"],
- ] as const) {
- supervisor.handleFact({
- ...commonFact(`${index}-started`, 10, childSessionId),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId,
- toolName: "Read",
- });
- supervisor.handleFact({
- ...commonFact(`${index}-completed`, 11, childSessionId),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId,
- toolName: "Read",
- });
- }
- await supervisor.flushReportsForTest();
- const steps = reportsByElement(reportTelemetryEvent, "agent_step");
- expect(
- steps.map((step) => [
- step.eventExtraDetail.agent_id,
- step.eventExtraDetail.loop_index,
- step.eventExtraDetail.tool_call_id,
- ]),
- ).toEqual([
- ["agent-a", "1", "tool_subagent_agent-a_tool-a-1"],
- ["agent-b", "1", "tool_subagent_agent-b_tool-b-1"],
- ["agent-a", "2", "tool_subagent_agent-a_tool-a-2"],
- ["agent-b", "2", "tool_subagent_agent-b_tool-b-2"],
- ]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- supervisor.dispose();
- });
- it("background stopped 汇总后仍收口全部已开始工具,不重复汇总或补算晚到 usage", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background-late-tool-terminal",
- now: () => now,
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "agent-background",
- childSessionId: "child-background",
- });
- supervisor.handleFact({
- ...commonFact("child-tool-started", 2, "child-background"),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "child-tool",
- toolName: "Bash",
- });
- supervisor.handleFact({
- ...commonFact("child-tool-2-started", 3, "child-background"),
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "child-tool-2",
- toolName: "Read",
- });
- const usage = {
- ...commonFact("child-usage", 4, "child-background"),
- kind: "usage.delta" as const,
- requestId: "child-request",
- inputTokens: 100,
- outputTokens: 20,
- totalTokens: 120,
- reasoningTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 0,
- };
- supervisor.handleFact(usage);
- now = 110;
- const permission = {
- ...commonFact("permission-requested", 5, "child-background"),
- kind: "permission.lifecycle" as const,
- phase: "requested" as const,
- requestId: "child-permission",
- toolCallId: "child-tool",
- };
- supervisor.handleFact(permission);
- now = 120;
- supervisor.handleFact({
- ...commonFact("subagent-stopped", 3, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "agent-background",
- childSessionId: "child-background",
- parentToolCallId: "agent-background-parent-tool",
- background: true,
- status: "stopped",
- });
- now = 150;
- supervisor.handleFact({ ...permission, eventId: "permission-resolved", phase: "resolved" });
- // stopped 后不能因未知工具/权限事实重新开启一份 telemetry 工作。
- supervisor.handleFact({ ...permission, eventId: "late-permission", toolCallId: "late-tool" });
- supervisor.handleFact({
- ...commonFact("late-tool-started", 6, "child-background"),
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "late-tool",
- toolName: "Bash",
- });
- now = 160;
- await supervisor.flushReportsForTest();
- const aggregate = reportsByElement(reportTelemetryEvent, "agent_step");
- expect(aggregate).toHaveLength(1);
- expect(aggregate[0]?.eventExtraDetail).toMatchObject({
- tool_name: "Agent",
- loop_index: "3",
- status: "fail",
- total_tokens: "120",
- token_usage_scope: "subagent_requests",
- });
- supervisor.handleFact({ ...usage, eventId: "late-usage", requestId: "late-request" });
- supervisor.handleFact({
- ...commonFact("stopped-copy", 4, "parent-session"),
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "agent-background",
- childSessionId: "child-background",
- parentToolCallId: "agent-background-parent-tool",
- background: true,
- status: "stopped",
- });
- supervisor.handleFact({
- ...commonFact("child-tool-completed", 4, "child-background"),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "child-tool",
- toolName: "Bash",
- });
- supervisor.handleFact({
- ...commonFact("child-tool-completed-copy", 7, "child-background"),
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "child-tool",
- toolName: "Bash",
- });
- supervisor.handleFact({
- ...commonFact("child-tool-2-completed", 5, "child-background"),
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "child-tool-2",
- toolName: "Read",
- });
- supervisor.handleFact({
- ...commonFact("child-terminal", 5, "child-background"),
- sourceCommandId: undefined,
- kind: "turn.terminal",
- status: "interrupted",
- resultType: "cancelled",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toEqual([
- ...aggregate,
- expect.objectContaining({
- messageId: "parent-command",
- eventExtraDetail: expect.objectContaining({
- tool_name: "Bash",
- tool_call_id: "tool_subagent_agent-background_child-tool",
- agent_id: "agent-background",
- agent_role: "background subagent",
- total_tokens: "0",
- loop_index: "1",
- waiting_ms: "40",
- }),
- }),
- expect.objectContaining({
- eventExtraDetail: expect.objectContaining({
- tool_name: "Read",
- loop_index: "2",
- total_tokens: "0",
- }),
- }),
- ]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- supervisor.dispose();
- });
- it("background child 没有待收口工具时在 SubagentStopped 后清理 ledger", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background-stopped-without-tool",
- now: () => 100,
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "agent-background",
- childSessionId: "child-background",
- });
- supervisor.handleFact({
- ...commonFact("subagent-stopped", 2, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "agent-background",
- childSessionId: "child-background",
- parentToolCallId: "agent-background-parent-tool",
- background: true,
- status: "failed",
- });
- supervisor.handleFact({
- ...commonFact("late-tool-completed", 3, "child-background"),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "late-tool",
- toolName: "Bash",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toEqual([
- expect.objectContaining({
- eventExtraDetail: expect.objectContaining({
- tool_name: "Agent",
- status: "fail",
- total_tokens: "0",
- }),
- }),
- ]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- supervisor.dispose();
- });
- it.each([
- { status: "success", hasUsage: false },
- { status: "failed", hasUsage: false },
- { status: "interrupted", hasUsage: false },
- { status: "success", hasUsage: true },
- ] as const)(
- "background $status 终态无 usage 也上报 Agent step(hasUsage=$hasUsage)",
- async ({ status, hasUsage }) => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background-terminal-only",
- now: () => 100,
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "agent-1",
- childSessionId: "child-session",
- });
- startForegroundSubagentRequest(supervisor);
- if (hasUsage) {
- supervisor.handleFact({
- ...commonFact("child-zero-usage", 2, "child-session"),
- kind: "usage.delta",
- requestId: "child-request",
- inputTokens: 0,
- outputTokens: 0,
- totalTokens: 0,
- reasoningTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 0,
- });
- }
- supervisor.handleFact({
- ...commonFact("child-thought", 3, "child-session"),
- sourceCommandId: undefined,
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 1,
- firstChunk: true,
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(0);
- for (const eventId of ["child-terminal", "duplicate-terminal"]) {
- supervisor.handleFact({
- ...commonFact(eventId, 4, "child-session"),
- sourceCommandId: undefined,
- kind: "turn.terminal",
- status,
- ...(status !== "success" ? { errorMessage: "child stopped without usage" } : {}),
- });
- }
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toEqual([
- expect.objectContaining({
- talkId: "parent-session",
- messageId: "parent-command",
- eventExtraDetail: expect.objectContaining({
- step_type: "tool_call",
- tool_name: "Agent",
- tool_call_id: "tool_subagent_agent-1_agent-1-parent-tool",
- agent_id: "agent-1",
- agent_role: "background subagent",
- loop_index: "1",
- model_name: "qwen-provider/qwen3.7-max",
- model_provider: "qwen-provider",
- provider_name: "provider.example.com",
- model_request_id: hasUsage ? "child-request" : "",
- model_request_count: hasUsage ? "1" : "0",
- token_usage_scope: hasUsage ? "subagent_requests" : "",
- input_tokens: "0",
- output_tokens: "0",
- reasoning_tokens: "0",
- cached_tokens: "0",
- cache_write_input_tokens: "0",
- total_tokens: "0",
- status: status === "success" ? "success" : "fail",
- error_type: status === "success" ? "" : "TOOL_EXEC_ERROR",
- error_msg: status === "success" ? "" : "[redacted]",
- }),
- }),
- ]);
- expect(reportsByElement(reportTelemetryEvent, "send_btn")).toHaveLength(0);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- supervisor.dispose();
- },
- );
- it("background child 的 failed tool lifecycle 按 foreground 口径保留错误", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background-tool-failed",
- now: () => 100,
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "agent-background",
- childSessionId: "child-background",
- });
- supervisor.handleFact({
- ...commonFact("child-tool-failed", 1, "child-background"),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "failed",
- toolCallId: "child-tool",
- toolName: "Bash",
- errorCode: "tool_timeout",
- errorMessage: "command timed out",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toEqual([
- expect.objectContaining({
- eventExtraDetail: expect.objectContaining({
- step_type: "tool_call",
- tool_call_id: "tool_subagent_agent-background_child-tool",
- agent_id: "agent-background",
- agent_role: "background subagent",
- status: "fail",
- error_type: "TOOL_EXEC_ERROR",
- error_msg: "[redacted]",
- }),
- }),
- ]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- supervisor.dispose();
- });
- it("main completion 后 background child 仍可上报 tool step,但不修改 main completion", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- let now = 10;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background-after-main",
- now: () => now,
- });
- supervisor.acceptPromptSeed({
- sessionId: "parent-session",
- sourceCommandId: "parent-command",
- sendTime: now,
- extraDetail: { model_name: "main-provider/main-model", model_provider: "main-provider" },
- });
- supervisor.handleFact({
- ...commonFact("parent-started", 1, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.started",
- executionKind: "agent",
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "agent-background",
- childSessionId: "child-background",
- });
- now = 20;
- supervisor.handleFact({
- ...commonFact("parent-terminal", 2, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.terminal",
- status: "success",
- });
- now = 30;
- supervisor.handleFact({
- ...commonFact("child-tool-started", 1, "child-background"),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "child-tool",
- toolName: "Read",
- });
- now = 40;
- supervisor.handleFact({
- ...commonFact("child-tool-completed", 2, "child-background"),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "child-tool",
- toolName: "Read",
- });
- await supervisor.flushReportsForTest();
- expect(
- reportTelemetryEvent.mock.calls
- .map(([payload]) => payload.elementName)
- .filter((name) => name === "agent_step" || name === "message_completion"),
- ).toEqual(["message_completion", "agent_step"]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toEqual([
- expect.objectContaining({
- messageId: "parent-command",
- eventExtraDetail: expect.objectContaining({ agent_step_cnt: "0" }),
- }),
- ]);
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(1);
- supervisor.dispose();
- });
- it("background mirror 与无 usage 的 child 汇总均不污染 main completion", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background-mirror-filter",
- now: () => 100,
- });
- supervisor.acceptPromptSeed({
- sessionId: "parent-session",
- sourceCommandId: "parent-command",
- sendTime: 100,
- extraDetail: { model_name: "main-model", model_provider: "main-provider" },
- });
- supervisor.handleFact({
- ...commonFact("parent-started", 1, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.started",
- executionKind: "agent",
- });
- spawnBackgroundSubagent(supervisor, {
- agentId: "agent-background",
- childSessionId: "child-background",
- });
- supervisor.handleFact({
- ...commonFact("mirror-permission-requested", 2, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "permission.lifecycle",
- phase: "requested",
- requestId: "permission-1",
- toolCallId: "tool_subagent_agent-background_child-tool",
- background: true,
- childSessionId: "child-background",
- });
- supervisor.handleFact({
- ...commonFact("mirror-tool-started", 3, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "tool_subagent_agent-background_child-tool",
- toolName: "Bash",
- agentId: "agent-background",
- childSessionId: "child-background",
- childToolCallId: "child-tool",
- parentToolCallId: "background-agent-tool",
- background: true,
- });
- supervisor.handleFact({
- ...commonFact("child-tool-started", 1, "child-background"),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "child-tool",
- toolName: "Bash",
- });
- supervisor.handleFact({
- ...commonFact("child-permission-requested", 2, "child-background"),
- sourceCommandId: undefined,
- kind: "permission.lifecycle",
- phase: "requested",
- requestId: "permission-1",
- toolCallId: "child-tool",
- });
- supervisor.handleFact({
- ...commonFact("child-permission-resolved", 3, "child-background"),
- sourceCommandId: undefined,
- kind: "permission.lifecycle",
- phase: "resolved",
- requestId: "permission-1",
- toolCallId: "child-tool",
- decision: "allow",
- });
- supervisor.handleFact({
- ...commonFact("child-tool-completed", 4, "child-background"),
- sourceCommandId: undefined,
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "child-tool",
- toolName: "Bash",
- });
- supervisor.handleFact({
- ...commonFact("parent-terminal", 5, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.terminal",
- status: "success",
- });
- supervisor.handleFact({
- ...commonFact("child-terminal", 6, "child-background"),
- sourceCommandId: undefined,
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(
- reportsByElement(reportTelemetryEvent, "agent_step").map((step) => [
- step.eventExtraDetail.tool_name,
- step.eventExtraDetail.loop_index,
- step.eventExtraDetail.total_tokens,
- ]),
- ).toEqual([
- ["Bash", "1", "0"],
- ["Agent", "2", "0"],
- ]);
- expect(
- reportTelemetryEvent.mock.calls
- .map(([payload]) => payload.elementName)
- .filter((name) => name === "agent_step" || name === "message_completion"),
- ).toEqual(["agent_step", "message_completion", "agent_step"]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toEqual([
- expect.objectContaining({
- eventExtraDetail: expect.objectContaining({
- agent_step_cnt: "0",
- waiting_ms: "0",
- }),
- }),
- ]);
- supervisor.dispose();
- });
- it("队列立即发送不会在前一 Subagent terminal deferred 时串用 message 状态", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-send-queued-now-after-subagent",
- now: () => now,
- });
- startForegroundSubagent(supervisor, now);
- startForegroundSubagentRequest(supervisor);
- reportForegroundSubagentUsage(supervisor);
- now = 180;
- supervisor.acceptPromptSeed({
- sessionId: "parent-session",
- sourceCommandId: "queued-command",
- sendTime: now,
- extraDetail: {
- model_name: "main-provider/main-model",
- model_provider: "main-provider",
- },
- });
- now = 200;
- terminateForegroundSubagentParent(supervisor);
- const queuedFact = (eventId: string, eventSeq: number) => ({
- ...commonFact(eventId, eventSeq, "parent-session"),
- sourceCommandId: "queued-command",
- turnId: "queued-turn",
- });
- now = 210;
- supervisor.handleFact({
- ...queuedFact("queued-turn-start", 7),
- kind: "turn.started",
- executionKind: "agent",
- });
- now = 220;
- supervisor.handleFact({
- ...queuedFact("queued-request-start", 8),
- kind: "model.request.status",
- requestId: "queued-request",
- status: "model_request_started",
- providerId: "main-provider",
- modelId: "main-model",
- providerHostname: "main.example.com",
- transport: "sse",
- querySource: "main_turn",
- attempt: 1,
- maxAttempts: 1,
- });
- now = 230;
- supervisor.handleFact({
- ...queuedFact("queued-text", 9),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 5,
- firstChunk: true,
- });
- now = 240;
- supervisor.handleFact({
- ...queuedFact("queued-usage", 10),
- kind: "usage.delta",
- requestId: "queued-request",
- providerId: "main-provider",
- modelId: "main-model",
- providerHostname: "main.example.com",
- inputTokens: 10,
- outputTokens: 2,
- totalTokens: 12,
- reasoningTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 0,
- });
- now = 250;
- supervisor.handleFact({
- ...queuedFact("queued-turn-terminal", 11),
- kind: "turn.terminal",
- status: "success",
- resultType: "complete",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(0);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- now = 300;
- supervisor.handleFact({
- ...commonFact("subagent-stopped-before-queued-replay", 12, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "agent-1",
- childSessionId: "child-session",
- parentToolCallId: "agent-tool",
- background: false,
- status: "failed",
- });
- await supervisor.flushReportsForTest();
- expect(
- reportsByElement(reportTelemetryEvent, "send_btn").map((payload) => payload.messageId),
- ).toEqual(["parent-command", "queued-command"]);
- expect(
- reportsByElement(reportTelemetryEvent, "agent_step").map((payload) => ({
- messageId: payload.messageId,
- stepType: payload.eventExtraDetail.step_type,
- tokenScope: payload.eventExtraDetail.token_usage_scope,
- totalTokens: payload.eventExtraDetail.total_tokens,
- })),
- ).toEqual([
- {
- messageId: "parent-command",
- stepType: "tool_call",
- tokenScope: "subagent_requests",
- totalTokens: "8685",
- },
- {
- messageId: "queued-command",
- stepType: "generation",
- tokenScope: "model_request",
- totalTokens: "12",
- },
- ]);
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion").map((payload) => ({
- messageId: payload.messageId,
- requestTime: payload.eventExtraDetail.request_time,
- duration: payload.eventExtraDetail.duration_ms,
- stepCount: payload.eventExtraDetail.agent_step_cnt,
- })),
- ).toEqual([
- {
- messageId: "parent-command",
- requestTime: "100",
- duration: "100",
- stepCount: "1",
- },
- {
- messageId: "queued-command",
- requestTime: "180",
- duration: "70",
- stepCount: "1",
- },
- ]);
- expect(
- reportTelemetryEvent.mock.calls
- .map(([payload]) => payload.elementName)
- .filter(
- (elementName) => elementName === "agent_step" || elementName === "message_completion",
- ),
- ).toEqual(["agent_step", "message_completion", "agent_step", "message_completion"]);
- supervisor.dispose();
- });
- it("Agent timeout 先于 SubagentStopped 时立即按已知 usage 收口且迟到 stopped 不重复", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-timeout-first-foreground-subagent",
- now: () => now,
- });
- startForegroundSubagent(supervisor, now);
- startForegroundSubagentRequest(supervisor);
- reportForegroundSubagentUsage(supervisor);
- now = 200;
- supervisor.handleFact({
- ...commonFact("parent-turn-terminal", 4, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "turn.terminal",
- status: "interrupted",
- resultType: "cancelled",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(0);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- now = 210;
- supervisor.handleFact({
- ...commonFact("agent-tool-timeout", 5, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "tool.lifecycle",
- phase: "failed",
- toolCallId: "agent-tool",
- toolName: "Agent",
- errorCode: "tool_timeout",
- errorMessage: "Tool execution timed out after 600000ms",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toEqual([
- expect.objectContaining({
- messageId: "parent-command",
- eventExtraDetail: expect.objectContaining({
- step_type: "tool_call",
- tool_name: "Agent",
- duration_ms: "110",
- status: "fail",
- model_name: "qwen-provider/qwen3.7-max",
- model_provider: "qwen-provider",
- provider_name: "provider.example.com",
- model_request_id: "child-request",
- model_request_count: "1",
- token_usage_scope: "subagent_requests",
- input_tokens: "8616",
- output_tokens: "69",
- cache_write_input_tokens: "5797",
- total_tokens: "8685",
- }),
- }),
- ]);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toEqual([
- expect.objectContaining({
- messageId: "parent-command",
- eventExtraDetail: expect.objectContaining({
- request_time: "100",
- duration_ms: "100",
- agent_step_cnt: "1",
- }),
- }),
- ]);
- expect(
- reportTelemetryEvent.mock.calls
- .map(([payload]) => payload.elementName)
- .filter(
- (elementName) => elementName === "agent_step" || elementName === "message_completion",
- ),
- ).toEqual(["agent_step", "message_completion"]);
- supervisor.handleFact({
- ...commonFact("late-subagent-stopped", 6, "parent-session"),
- sourceCommandId: "parent-command",
- kind: "subagent.lifecycle",
- phase: "stopped",
- agentId: "agent-1",
- childSessionId: "child-session",
- parentToolCallId: "agent-tool",
- background: false,
- status: "failed",
- });
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(1);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(1);
- supervisor.dispose();
- });
- it("background 仍上报 plan_request 和 seeded /report,但不产生 UI ARMS", async () => {
- const { platform, reportTelemetryEvent, reportArmsCustomEvent } = createPlatform();
- setUiPerfArmsReporter(platform);
- let now = 10;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-background",
- now: () => now,
- });
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: 10,
- extraDetail: {
- ask_mode: "build",
- model_name: "model",
- model_provider: "provider",
- },
- });
- supervisor.handleFact({
- ...commonFact("background-start", 1),
- kind: "turn.started",
- });
- now = 20;
- supervisor.handleFact({
- ...commonFact("background-model", 2),
- kind: "model.request.status",
- requestId: "request-bg",
- status: "model_request_completed",
- providerId: "provider",
- modelId: "model",
- transport: "fetch",
- attempt: 1,
- maxAttempts: 1,
- durationMs: 5,
- });
- now = 30;
- supervisor.handleFact({
- ...commonFact("background-chunk", 3),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 1,
- firstChunk: true,
- });
- now = 40;
- supervisor.handleFact({
- ...commonFact("background-terminal", 4),
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "send_btn")).toHaveLength(1);
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(1);
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(1);
- expect(reportArmsCustomEvent.mock.calls.map(([payload]) => payload.name)).toEqual([
- "plan_request",
- ]);
- supervisor.dispose();
- });
- it("automation TurnStarted 自动建 seed,不伪造 send_btn,且调度字段只进入 completion", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-automation",
- now: () => 1_700_000_000_500,
- });
- const sourceCommandId = "automation-1:1700000000000";
- const automationFact = {
- version: 1 as const,
- eventSeq: 1,
- occurredAt: 1_700_000_000_120,
- sessionId: "session-automation",
- sourceCommandId,
- turnId: "turn-automation",
- };
- supervisor.handleFact({
- ...automationFact,
- eventId: "automation-start",
- kind: "turn.started",
- automationId: "automation-1",
- taskTrigger: "schedule",
- scheduledAt: 1_700_000_000_000,
- });
- supervisor.handleFact({
- ...automationFact,
- eventId: "automation-chunk",
- eventSeq: 2,
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 2,
- firstChunk: true,
- });
- supervisor.handleFact({
- ...automationFact,
- eventId: "automation-terminal",
- eventSeq: 3,
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "send_btn")).toHaveLength(0);
- const step = reportsByElement(reportTelemetryEvent, "agent_step")[0]?.eventExtraDetail;
- expect(step).toMatchObject({
- message_source: "scheduled_task",
- task_trigger: "schedule",
- automation_id: "automation-1",
- });
- expect(step).not.toHaveProperty("scheduled_at");
- expect(step).not.toHaveProperty("schedule_lag_ms");
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion")[0]?.eventExtraDetail,
- ).toMatchObject({
- message_source: "scheduled_task",
- task_trigger: "schedule",
- automation_id: "automation-1",
- scheduled_at: "1700000000000",
- schedule_lag_ms: "120",
- });
- supervisor.dispose();
- });
- it("manual run 不带调度字段;同会话后续 chat 轮不继承 automation_id、step 不携带 seed 模型维度", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-manual",
- now: () => 1_700_000_001_000,
- });
- const manualCommandId = "automation-1:manual:run-1";
- const manualFact = {
- version: 1 as const,
- occurredAt: 1_700_000_000_500,
- sessionId: "session-shared",
- sourceCommandId: manualCommandId,
- turnId: "turn-manual",
- };
- supervisor.handleFact({
- ...manualFact,
- eventId: "manual-start",
- eventSeq: 1,
- kind: "turn.started",
- automationId: "automation-1",
- taskTrigger: "manual",
- });
- supervisor.handleFact({
- ...manualFact,
- eventId: "manual-chunk",
- eventSeq: 2,
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 2,
- firstChunk: true,
- });
- supervisor.handleFact({
- ...manualFact,
- eventId: "manual-terminal",
- eventSeq: 3,
- kind: "turn.terminal",
- status: "success",
- });
- const chatCommandId = "command-chat-1";
- supervisor.acceptPromptSeed({
- sessionId: "session-shared",
- sourceCommandId: chatCommandId,
- sendTime: 1_700_000_000_900,
- extraDetail: {
- message_source: "chat",
- task_trigger: "",
- model_name: "seed-model",
- plan_status: "coding_plan",
- },
- });
- const chatFact = {
- version: 1 as const,
- occurredAt: 1_700_000_000_950,
- sessionId: "session-shared",
- sourceCommandId: chatCommandId,
- turnId: "turn-chat",
- };
- supervisor.handleFact({
- ...chatFact,
- eventId: "chat-start",
- eventSeq: 4,
- kind: "turn.started",
- });
- supervisor.handleFact({
- ...chatFact,
- eventId: "chat-chunk",
- eventSeq: 5,
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 2,
- firstChunk: true,
- });
- supervisor.handleFact({
- ...chatFact,
- eventId: "chat-terminal",
- eventSeq: 6,
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "send_btn")).toHaveLength(1);
- const manualStep = reportsByElement(reportTelemetryEvent, "agent_step").find(
- (payload) => payload.messageId === manualCommandId,
- )?.eventExtraDetail;
- expect(manualStep).toMatchObject({
- message_source: "scheduled_task",
- task_trigger: "manual",
- automation_id: "automation-1",
- });
- expect(manualStep).not.toHaveProperty("scheduled_at");
- const manualCompletion = reportsByElement(reportTelemetryEvent, "message_completion").find(
- (payload) => payload.messageId === manualCommandId,
- )?.eventExtraDetail;
- expect(manualCompletion).toMatchObject({
- message_source: "scheduled_task",
- task_trigger: "manual",
- automation_id: "automation-1",
- });
- expect(manualCompletion).not.toHaveProperty("scheduled_at");
- expect(manualCompletion).not.toHaveProperty("schedule_lag_ms");
- const chatStep = reportsByElement(reportTelemetryEvent, "agent_step").find(
- (payload) => payload.messageId === chatCommandId,
- )?.eventExtraDetail;
- expect(chatStep).toMatchObject({ message_source: "chat", task_trigger: "" });
- expect(chatStep).not.toHaveProperty("automation_id");
- // seed 的模型/套餐维度只属于 send/completion,不进 step 契约。
- expect(chatStep).not.toHaveProperty("plan_status");
- const chatCompletion = reportsByElement(reportTelemetryEvent, "message_completion").find(
- (payload) => payload.messageId === chatCommandId,
- )?.eventExtraDetail;
- expect(chatCompletion).toMatchObject({ message_source: "chat", task_trigger: "" });
- expect(chatCompletion).not.toHaveProperty("automation_id");
- supervisor.dispose();
- });
- it("Off-Peak first/resume/retry talk 共用 task 归因,manual 不继承且每轮 message_id 独立", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-off-peak",
- now: () => 2_000,
- });
- let eventSeq = 0;
- const runOffPeakTurn = (params: {
- sessionId: string;
- sourceCommandId: string;
- turnId: string;
- offPeakRunType: "init" | "resume";
- }) => {
- const base = {
- version: 1 as const,
- occurredAt: 1_000 + eventSeq,
- sessionId: params.sessionId,
- sourceCommandId: params.sourceCommandId,
- turnId: params.turnId,
- };
- supervisor.handleFact({
- ...base,
- eventId: `${params.sourceCommandId}-start`,
- eventSeq: eventSeq++,
- kind: "turn.started",
- offPeakTaskId: "offpeak-stable-1",
- offPeakRunType: params.offPeakRunType,
- });
- supervisor.handleFact({
- ...base,
- eventId: `${params.sourceCommandId}-model`,
- eventSeq: eventSeq++,
- kind: "model.request.status",
- requestId: `${params.sourceCommandId}-request`,
- status: "model_request_started",
- providerId: "runtime-provider",
- modelId: "runtime-model",
- providerHostname: "api.example.com",
- transport: "fetch",
- attempt: 1,
- maxAttempts: 1,
- });
- supervisor.handleFact({
- ...base,
- eventId: `${params.sourceCommandId}-chunk`,
- eventSeq: eventSeq++,
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 1,
- firstChunk: true,
- });
- const terminal = {
- ...base,
- eventId: `${params.sourceCommandId}-terminal`,
- eventSeq: eventSeq++,
- kind: "turn.terminal",
- status: "success",
- } satisfies ConversationTelemetryFact;
- supervisor.handleFact(terminal);
- supervisor.handleFact({
- ...terminal,
- eventId: `${params.sourceCommandId}-terminal-duplicate`,
- eventSeq: eventSeq++,
- });
- };
- runOffPeakTurn({
- sessionId: "talk-first",
- sourceCommandId: "message-first",
- turnId: "turn-first",
- offPeakRunType: "init",
- });
- runOffPeakTurn({
- sessionId: "talk-first",
- sourceCommandId: "message-resume",
- turnId: "turn-resume",
- offPeakRunType: "resume",
- });
- runOffPeakTurn({
- sessionId: "talk-retry",
- sourceCommandId: "message-retry",
- turnId: "turn-retry",
- offPeakRunType: "init",
- });
- supervisor.acceptPromptSeed({
- sessionId: "talk-first",
- sourceCommandId: "message-manual",
- sendTime: 1_500,
- extraDetail: {
- message_source: "chat",
- task_trigger: "",
- model_name: "manual-seed",
- model_provider: "manual-provider",
- },
- });
- supervisor.handleFact({
- ...commonFact("manual-start", eventSeq++, "talk-first"),
- sourceCommandId: "message-manual",
- turnId: "turn-manual",
- kind: "turn.started",
- });
- supervisor.handleFact({
- ...commonFact("manual-chunk", eventSeq++, "talk-first"),
- sourceCommandId: "message-manual",
- turnId: "turn-manual",
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 1,
- firstChunk: true,
- });
- supervisor.handleFact({
- ...commonFact("manual-terminal", eventSeq++, "talk-first"),
- sourceCommandId: "message-manual",
- turnId: "turn-manual",
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "send_btn")).toHaveLength(1);
- const completions = reportsByElement(reportTelemetryEvent, "message_completion");
- const steps = reportsByElement(reportTelemetryEvent, "agent_step");
- expect(completions).toHaveLength(4);
- for (const [messageId, offPeakRunType] of [
- ["message-first", "init"],
- ["message-resume", "resume"],
- ["message-retry", "init"],
- ] as const) {
- const detail = completions.find(
- (payload) => payload.messageId === messageId,
- )?.eventExtraDetail;
- expect(detail).toMatchObject({
- message_source: "off_peak_task",
- off_peak_task_id: "offpeak-stable-1",
- off_peak_run_type: offPeakRunType,
- model_name: "runtime-provider/runtime-model",
- model_provider: "runtime-provider",
- });
- expect(detail).not.toHaveProperty("automation_id");
- expect(detail).not.toHaveProperty("task_trigger");
- expect(
- steps.find((payload) => payload.messageId === messageId)?.eventExtraDetail,
- ).toMatchObject({
- message_source: "off_peak_task",
- off_peak_task_id: "offpeak-stable-1",
- off_peak_run_type: offPeakRunType,
- });
- }
- expect(completions.find((payload) => payload.messageId === "message-retry")?.talkId).toBe(
- "talk-retry",
- );
- const manual = completions.find(
- (payload) => payload.messageId === "message-manual",
- )?.eventExtraDetail;
- expect(manual).toMatchObject({ message_source: "chat" });
- expect(manual).not.toHaveProperty("off_peak_task_id");
- expect(manual).not.toHaveProperty("off_peak_run_type");
- const manualStep = steps.find(
- (payload) => payload.messageId === "message-manual",
- )?.eventExtraDetail;
- expect(manualStep).not.toHaveProperty("off_peak_task_id");
- expect(manualStep).not.toHaveProperty("off_peak_run_type");
- supervisor.dispose();
- });
- it("Off-Peak usage fact 去重后 completion 保留精确总量,step 保持占位口径", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-off-peak-usage",
- now: () => 2_000,
- });
- const base = {
- version: 1 as const,
- occurredAt: 1_000,
- sessionId: "talk-usage",
- sourceCommandId: "message-usage",
- turnId: "turn-usage",
- };
- supervisor.handleFact({
- ...base,
- eventId: "usage-start",
- eventSeq: 1,
- kind: "turn.started",
- offPeakTaskId: "offpeak-usage",
- });
- const usage = {
- ...base,
- eventId: "usage-once",
- eventSeq: 2,
- kind: "usage.delta",
- inputTokens: 10,
- outputTokens: 5,
- totalTokens: 18,
- reasoningTokens: 3,
- cacheReadTokens: 2,
- cacheWriteTokens: 1,
- } satisfies ConversationTelemetryFact;
- supervisor.handleFact(usage);
- supervisor.handleFact(usage);
- supervisor.handleFact({
- ...base,
- eventId: "usage-chunk",
- eventSeq: 3,
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 1,
- firstChunk: true,
- });
- supervisor.handleFact({
- ...base,
- eventId: "usage-terminal",
- eventSeq: 4,
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion")[0]?.eventExtraDetail,
- ).toMatchObject({
- input_tokens: "10",
- output_tokens: "5",
- reasoning_tokens: "3",
- total_tokens: "18",
- off_peak_task_id: "offpeak-usage",
- });
- expect(reportsByElement(reportTelemetryEvent, "agent_step")[0]?.eventExtraDetail).toMatchObject(
- {
- input_tokens: "0",
- output_tokens: "0",
- cached_tokens: "0",
- off_peak_task_id: "offpeak-usage",
- },
- );
- supervisor.dispose();
- });
- it("CronCreate 成功终态把新任务 automation_id 写入对应 agent_step", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-cron-create",
- now: () => 500,
- });
- supervisor.acceptPromptSeed({
- sessionId: "session-cron-create",
- sourceCommandId: "command-cron-create",
- sendTime: 100,
- extraDetail: { message_source: "chat", task_trigger: "" },
- });
- const base = {
- version: 1 as const,
- sessionId: "session-cron-create",
- sourceCommandId: "command-cron-create",
- turnId: "turn-cron-create",
- };
- supervisor.handleFact({
- ...base,
- eventId: "cron-turn-start",
- eventSeq: 1,
- occurredAt: 110,
- kind: "turn.started",
- });
- supervisor.handleFact({
- ...base,
- eventId: "cron-tool-start",
- eventSeq: 2,
- occurredAt: 120,
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "tool-cron-create",
- toolName: "CronCreate",
- });
- supervisor.handleFact({
- ...base,
- eventId: "cron-tool-complete",
- eventSeq: 3,
- occurredAt: 150,
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "tool-cron-create",
- toolName: "CronCreate",
- automationId: "automation-created",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")[0]?.eventExtraDetail).toMatchObject(
- {
- tool_name: "CronCreate",
- status: "success",
- automation_id: "automation-created",
- },
- );
- supervisor.dispose();
- });
- it("异步 reporter 仍严格按 send → step → completion 串行", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const releases: Array<() => void> = [];
- reportTelemetryEvent.mockImplementation(
- () => new Promise<void>((resolve) => releases.push(resolve)),
- );
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-report-order",
- now: () => 100,
- });
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: 10,
- extraDetail: { model_name: "model", model_provider: "provider" },
- });
- supervisor.handleFact({
- ...commonFact("order-start", 1),
- kind: "turn.started",
- });
- supervisor.handleFact({
- ...commonFact("order-model", 2),
- kind: "model.request.status",
- requestId: "request-1",
- status: "model_request_started",
- providerId: "provider",
- modelId: "model",
- transport: "fetch",
- attempt: 1,
- maxAttempts: 1,
- });
- supervisor.handleFact({
- ...commonFact("order-chunk", 3),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 1,
- firstChunk: true,
- });
- supervisor.handleFact({
- ...commonFact("order-terminal", 4),
- kind: "turn.terminal",
- status: "success",
- });
- const reportedNames = () =>
- reportTelemetryEvent.mock.calls.map(([payload]) => payload.elementName);
- expect(reportedNames()).toEqual(["send_btn"]);
- releases.shift()?.();
- await vi.waitFor(() => expect(reportedNames()).toEqual(["send_btn", "agent_step"]));
- releases.shift()?.();
- await vi.waitFor(() =>
- expect(reportedNames()).toEqual(["send_btn", "agent_step", "message_completion"]),
- );
- releases.shift()?.();
- await supervisor.flushReportsForTest();
- supervisor.dispose();
- });
- it("ACK 前 buffer 保留 turn.started,并按 terminal key exactly-once", async () => {
- const { platform, reportTelemetryEvent, reportArmsCustomEvent } = createPlatform();
- setUiPerfArmsReporter(platform);
- let now = 1_000;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-buffer",
- now: () => now,
- });
- supervisor.handleFact({
- ...commonFact("buffer-start", 1),
- kind: "turn.started",
- });
- for (let index = 0; index < 140; index += 1) {
- now += 1;
- supervisor.handleFact({
- ...commonFact(`buffer-chunk-${index}`, index + 2),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 1,
- firstChunk: index === 0,
- });
- }
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: 900,
- extraDetail: {
- model_name: "model",
- model_provider: "provider",
- },
- });
- supervisor.attachForeground({}, "session-1");
- now = 2_000;
- const terminal = {
- ...commonFact("buffer-terminal-1", 200),
- kind: "turn.terminal",
- status: "interrupted",
- resultType: "cancelled",
- errorCode: "USER_INTERRUPT",
- errorMessage: "stopped",
- } satisfies ConversationTelemetryFact;
- supervisor.handleFact(terminal);
- supervisor.handleFact({ ...terminal, eventId: "buffer-terminal-2", eventSeq: 201 });
- await supervisor.flushReportsForTest();
- const completion = reportsByElement(reportTelemetryEvent, "message_completion");
- expect(completion).toHaveLength(1);
- expect(completion[0]).toMatchObject({
- talkId: "session-1",
- messageId: "command-1",
- eventExtraDetail: {
- status: "user_interrupt",
- error_type: "USER_INTERRUPT",
- error_msg: "[redacted]",
- time_to_first_token: "101",
- },
- });
- // buffered chunk 到达时仍在 background;terminal 前切回也不能补造 UI TTFT。
- expect(armsByName(reportArmsCustomEvent, "perf_ui_first_token")).toHaveLength(0);
- expect(armsByName(reportArmsCustomEvent, "plan_ttft")).toHaveLength(0);
- expect(reportsByElement(reportTelemetryEvent, "agent_step")[0]).toMatchObject({
- eventExtraDetail: { status: "success" },
- });
- supervisor.dispose();
- });
- it("同 session direct 与 queued seed 依次 promotion,terminal 不清后续队列", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-queued",
- now: () => now,
- });
- for (const sourceCommandId of ["command-direct", "command-queued"]) {
- supervisor.acceptPromptSeed({
- sessionId: "session-queued",
- sourceCommandId,
- sendTime: now,
- extraDetail: { model_name: "model", model_provider: "provider" },
- });
- now += 10;
- }
- supervisor.handleFact({
- ...commonFact("direct-start", 1, "session-queued"),
- sourceCommandId: "command-direct",
- turnId: "turn-direct",
- kind: "turn.started",
- });
- now = 200;
- supervisor.handleFact({
- ...commonFact("direct-terminal", 2, "session-queued"),
- sourceCommandId: "command-direct",
- turnId: "turn-direct",
- kind: "turn.terminal",
- status: "success",
- });
- now = 300;
- supervisor.handleFact({
- ...commonFact("queued-start", 3, "session-queued"),
- sourceCommandId: "command-queued",
- turnId: "turn-queued",
- kind: "turn.started",
- });
- now = 400;
- supervisor.handleFact({
- ...commonFact("queued-terminal", 4, "session-queued"),
- sourceCommandId: "command-queued",
- turnId: "turn-queued",
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "send_btn")).toHaveLength(2);
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion").map(
- (payload) => payload.messageId,
- ),
- ).toEqual(["command-direct", "command-queued"]);
- supervisor.dispose();
- });
- it("无本地 seed 的 command fact 使用 200 组 LRU 上限", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-pending-lru",
- now: () => 1_000,
- });
- for (let index = 0; index < 200; index += 1) {
- supervisor.handleFact({
- ...commonFact(`pending-${index}`, index, `session-${index}`),
- sourceCommandId: `command-${index}`,
- turnId: `turn-${index}`,
- kind: "turn.started",
- });
- }
- // 再命中 command-0,把它移到 LRU 尾部;新增 command-200 应淘汰 command-1。
- supervisor.handleFact({
- ...commonFact("pending-0-touch", 201, "session-0"),
- sourceCommandId: "command-0",
- turnId: "turn-0",
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 1,
- firstChunk: true,
- });
- supervisor.handleFact({
- ...commonFact("pending-200", 202, "session-200"),
- sourceCommandId: "command-200",
- turnId: "turn-200",
- kind: "turn.started",
- });
- expect(supervisor.getPendingCommandCountForTest()).toBe(200);
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: 900,
- extraDetail: {},
- });
- supervisor.handleFact({
- ...commonFact("evicted-terminal", 203, "session-1"),
- sourceCommandId: "command-1",
- turnId: "turn-1",
- kind: "turn.terminal",
- status: "success",
- });
- supervisor.acceptPromptSeed({
- sessionId: "session-0",
- sourceCommandId: "command-0",
- sendTime: 900,
- extraDetail: {},
- });
- supervisor.handleFact({
- ...commonFact("retained-terminal", 204, "session-0"),
- sourceCommandId: "command-0",
- turnId: "turn-0",
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion").map(
- (payload) => payload.messageId,
- ),
- ).toEqual(["command-0"]);
- supervisor.dispose();
- });
- it("foreground raw 负 TTFT 仅由 ui_perf 钳 0,plan_ttft 丢弃", () => {
- const { platform, reportArmsCustomEvent } = createPlatform();
- setUiPerfArmsReporter(platform);
- let now = 200;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-clock-rollback",
- now: () => now,
- });
- supervisor.attachForeground({}, "session-1");
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: 200,
- extraDetail: { model_name: "model", model_provider: "provider" },
- });
- supervisor.handleFact({
- ...commonFact("rollback-start", 1),
- kind: "turn.started",
- });
- now = 100;
- supervisor.handleFact({
- ...commonFact("rollback-token", 2),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 1,
- firstChunk: true,
- });
- now = 300;
- supervisor.handleFact({
- ...commonFact("rollback-terminal", 3),
- kind: "turn.terminal",
- status: "success",
- });
- expect(armsByName(reportArmsCustomEvent, "perf_ui_first_token")[0]?.value).toBe(0);
- expect(armsByName(reportArmsCustomEvent, "plan_ttft")).toHaveLength(0);
- supervisor.dispose();
- });
- it("plan_request 五态无需 seed,compaction 按到达时 foreground 与 operation 去重", () => {
- const { platform, reportTelemetryEvent, reportArmsCustomEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-independent",
- now: () => 2_000,
- });
- const statuses = [
- "model_request_started",
- "model_request_completed",
- "model_request_failed",
- "model_retry_scheduled",
- "model_stream_stalled",
- ] as const;
- statuses.forEach((status, index) => {
- supervisor.handleFact({
- ...commonFact(`plan-${index}`, index, "session-plan"),
- sourceCommandId: undefined,
- turnId: undefined,
- kind: "model.request.status",
- requestId: "request-plan",
- status,
- providerId: "provider",
- modelId: "model",
- transport: "fetch",
- attempt: 1,
- maxAttempts: 3,
- });
- });
- expect(
- armsByName(reportArmsCustomEvent, "plan_request").map(
- (payload) => payload.properties?.request_status,
- ),
- ).toEqual(["started", "completed", "failed", "retry_scheduled", "stream_stalled"]);
- const compaction = {
- ...commonFact("compact-1", 20, "session-compact"),
- sourceCommandId: undefined,
- turnId: undefined,
- kind: "compaction.terminal",
- operationId: "operation-1",
- summaryMessageId: "summary-1",
- status: "completed",
- trigger: "manual",
- reason: "manual request",
- attempt: 2,
- startedAt: 1_000,
- endedAt: 1_500,
- preCompactTokenCount: 100,
- postCompactTokenCount: 40,
- truePostCompactTokenCount: 35,
- modelName: "model",
- modelProvider: "provider",
- } satisfies ConversationTelemetryFact;
- supervisor.handleFact(compaction);
- expect(reportsByElement(reportTelemetryEvent, "context_compaction")).toHaveLength(0);
- supervisor.attachForeground({}, "session-compact");
- supervisor.handleFact({ ...compaction, eventId: "compact-2", eventSeq: 21 });
- supervisor.handleFact({ ...compaction, eventId: "compact-3", eventSeq: 22 });
- expect(reportsByElement(reportTelemetryEvent, "context_compaction")).toHaveLength(0);
- const foregroundCompaction = {
- ...compaction,
- eventId: "compact-4",
- eventSeq: 23,
- operationId: "operation-2",
- summaryMessageId: "summary-2",
- } satisfies ConversationTelemetryFact;
- supervisor.handleFact(foregroundCompaction);
- supervisor.handleFact({
- ...foregroundCompaction,
- eventId: "compact-5",
- eventSeq: 24,
- status: "failed",
- reason: "late contradictory terminal",
- });
- expect(reportsByElement(reportTelemetryEvent, "context_compaction")).toEqual([
- expect.objectContaining({
- elementName: "context_compaction",
- eventRegion: "app",
- eventType: "agent_trace",
- talkId: "session-compact",
- messageId: "summary-2",
- eventExtraDetail: {
- status: "completed",
- trigger: "manual",
- reason: "manual request",
- attempt: "2",
- duration_ms: "500",
- pre_compact_tokens: "100",
- post_compact_tokens: "40",
- true_post_compact_tokens: "35",
- compact_ratio: "0.4000",
- model_name: "provider/model",
- model_provider: "provider",
- },
- }),
- ]);
- supervisor.dispose();
- });
- it("compaction 不用 compactReason 回填旧 reason,glm 模型保持原值", () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-compact-legacy-fields",
- now: () => 2_000,
- });
- supervisor.attachForeground({}, "session-compact");
- supervisor.handleFact({
- ...commonFact("compact-legacy", 1, "session-compact"),
- sourceCommandId: undefined,
- turnId: undefined,
- kind: "compaction.terminal",
- operationId: "operation-legacy",
- status: "completed",
- trigger: "auto",
- compactReason: "context_limit",
- modelName: "glm-model",
- modelProvider: "glm",
- });
- expect(reportsByElement(reportTelemetryEvent, "context_compaction")[0]).toMatchObject({
- eventExtraDetail: {
- reason: "",
- model_name: "glm-model",
- model_provider: "glm",
- },
- });
- supervisor.dispose();
- });
- it("tool-first 与无 token terminal 保留旧 first-token ARMS 边界", () => {
- const { platform, reportArmsCustomEvent } = createPlatform();
- setUiPerfArmsReporter(platform);
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-tool-first",
- now: () => now,
- });
- supervisor.attachForeground({}, "session-1");
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: 100,
- extraDetail: { model_name: "model", model_provider: "provider" },
- });
- supervisor.handleFact({ ...commonFact("tool-first-start", 1), kind: "turn.started" });
- now = 140;
- supervisor.handleFact({
- ...commonFact("tool-first-tool", 2),
- kind: "tool.lifecycle",
- phase: "scheduled",
- toolCallId: "tool-1",
- toolName: "Read",
- });
- now = 200;
- supervisor.handleFact({
- ...commonFact("tool-first-terminal", 3),
- kind: "turn.terminal",
- status: "success",
- });
- expect(armsByName(reportArmsCustomEvent, "perf_ui_first_token")[0]?.value).toBe(40);
- expect(armsByName(reportArmsCustomEvent, "plan_ttft")[0]?.value).toBe(40);
- supervisor.acceptPromptSeed({
- sessionId: "session-2",
- sourceCommandId: "command-2",
- sendTime: 200,
- extraDetail: { model_name: "model", model_provider: "provider" },
- });
- supervisor.attachForeground({}, "session-2");
- supervisor.handleFact({
- ...commonFact("no-token-start", 4, "session-2"),
- sourceCommandId: "command-2",
- turnId: "turn-2",
- kind: "turn.started",
- });
- now = 260;
- supervisor.handleFact({
- ...commonFact("no-token-terminal", 5, "session-2"),
- sourceCommandId: "command-2",
- turnId: "turn-2",
- kind: "turn.terminal",
- status: "success",
- });
- expect(
- armsByName(reportArmsCustomEvent, "perf_ui_first_token").map((item) => item.value),
- ).toEqual([40, 0]);
- expect(armsByName(reportArmsCustomEvent, "plan_ttft")).toHaveLength(1);
- supervisor.dispose();
- });
- it("parented child text 不进入 generation,但保留旧 TTFT 与 stall", async () => {
- const { platform, reportTelemetryEvent, reportArmsCustomEvent } = createPlatform();
- setUiPerfArmsReporter(platform);
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-parented-child",
- now: () => now,
- });
- supervisor.attachForeground({}, "session-1");
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: 100,
- extraDetail: { model_name: "model", model_provider: "provider" },
- });
- supervisor.handleFact({ ...commonFact("child-start", 1), kind: "turn.started" });
- now = 150;
- supervisor.handleFact({
- ...commonFact("child-chunk-1", 2),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 10,
- firstChunk: true,
- parentToolCallId: "parent-agent-tool",
- });
- now = 3_251;
- supervisor.handleFact({
- ...commonFact("child-chunk-2", 3),
- kind: "stream.chunk",
- channel: "text",
- chunkLength: 10,
- firstChunk: false,
- parentToolCallId: "parent-agent-tool",
- });
- now = 3_300;
- supervisor.handleFact({
- ...commonFact("child-terminal", 4),
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(0);
- expect(armsByName(reportArmsCustomEvent, "perf_ui_stream_stall")[0]?.value).toBe(3_101);
- expect(armsByName(reportArmsCustomEvent, "perf_ui_first_token")[0]?.value).toBe(50);
- expect(armsByName(reportArmsCustomEvent, "plan_ttft")[0]?.value).toBe(50);
- supervisor.dispose();
- });
- it("parented child thought 保留旧 reasoning 与 TTFT 行为", async () => {
- const { platform, reportTelemetryEvent, reportArmsCustomEvent } = createPlatform();
- setUiPerfArmsReporter(platform);
- let now = 100;
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-parented-thought",
- now: () => now,
- });
- supervisor.attachForeground({}, "session-1");
- supervisor.acceptPromptSeed({
- sessionId: "session-1",
- sourceCommandId: "command-1",
- sendTime: 100,
- extraDetail: { model_name: "model", model_provider: "provider" },
- });
- supervisor.handleFact({ ...commonFact("thought-start", 1), kind: "turn.started" });
- now = 150;
- supervisor.handleFact({
- ...commonFact("parented-thought", 2),
- kind: "stream.chunk",
- channel: "thought",
- chunkLength: 10,
- firstChunk: true,
- parentToolCallId: "parent-agent-tool",
- });
- now = 200;
- supervisor.handleFact({
- ...commonFact("thought-terminal", 3),
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(1);
- expect(armsByName(reportArmsCustomEvent, "perf_ui_first_token")[0]?.value).toBe(50);
- expect(armsByName(reportArmsCustomEvent, "plan_ttft")[0]?.value).toBe(50);
- supervisor.dispose();
- });
- it("chat_error_banner 复用既有 builder 上报真实可见错误", () => {
- const { platform, reportArmsCustomEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-error",
- });
- supervisor.reportVisibleChatError({
- errorKey: "error-1",
- displayMessage: "Request failed",
- error: {
- message: "raw request failed",
- code: "MODEL_ERROR",
- traceId: "trace-1",
- taskId: "session-1",
- detail: "detail",
- },
- });
- expect(armsByName(reportArmsCustomEvent, "chat_error_banner")).toEqual([
- {
- name: "chat_error_banner",
- group: "ui_error",
- value: 1,
- properties: expect.objectContaining({
- surface: "chat_input_error_banner",
- error_code: "MODEL_ERROR",
- error_message: "Request failed",
- trace_id: "trace-1",
- task_id: "session-1",
- has_detail: true,
- provider_business_action: "",
- provider_business_code: "",
- }),
- },
- ]);
- supervisor.dispose();
- });
- it("chat_error_banner 保留普通业务恢复维度并隐藏 3007 内部动作", () => {
- const { platform, reportArmsCustomEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-provider-errors",
- });
- supervisor.reportVisibleChatError({
- displayMessage: "Please login",
- error: { message: "login required", code: "1006" },
- });
- supervisor.reportVisibleChatError({
- displayMessage: "Security check rejected",
- error: { message: "security check rejected", code: "3007" },
- });
- expect(
- armsByName(reportArmsCustomEvent, "chat_error_banner").map((payload) => payload.properties),
- ).toEqual([
- expect.objectContaining({
- provider_business_action: "login",
- provider_business_code: "1006",
- }),
- expect.objectContaining({
- provider_business_action: "",
- provider_business_code: "",
- }),
- ]);
- supervisor.dispose();
- });
- });
- describe("workflow subagent:动态工作流子代理的 token 埋点(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md「Token telemetry for subagents」)", () => {
- const CHILD = "sess_dwf-run-1-worker_1";
- function spawnWorkflowActor(
- supervisor: ConversationTelemetrySupervisor,
- input: { sourceCommandId?: string; childSessionId?: string; agentId?: string } = {},
- ) {
- supervisor.handleFact({
- ...commonFact("wf-actor-spawned", 1, "parent-session"),
- ...(input.sourceCommandId === undefined
- ? { sourceCommandId: "launch-input-1" }
- : { sourceCommandId: input.sourceCommandId }),
- kind: "workflow.lifecycle",
- phase: "actor-spawned",
- runId: "dwfrun-1",
- toolCallId: "tool-wf",
- agentId: input.agentId ?? "worker@1",
- childSessionId: input.childSessionId ?? CHILD,
- });
- }
- function childUsage(
- supervisor: ConversationTelemetrySupervisor,
- eventId: string,
- requestId: string,
- ) {
- supervisor.handleFact({
- ...commonFact(`${eventId}-request`, 1, CHILD),
- turnId: "child-turn-1",
- kind: "model.request.status",
- requestId,
- status: "model_request_started",
- providerId: "glm-provider",
- modelId: "glm-5.3",
- providerHostname: "glm.example.com",
- transport: "sse",
- querySource: "workflow_child",
- attempt: 1,
- maxAttempts: 1,
- });
- supervisor.handleFact({
- ...commonFact(eventId, 2, CHILD),
- turnId: "child-turn-1",
- kind: "usage.delta",
- requestId,
- providerId: "glm-provider",
- modelId: "glm-5.3",
- providerHostname: "glm.example.com",
- inputTokens: 1000,
- outputTokens: 50,
- totalTokens: 1050,
- reasoningTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 0,
- });
- }
- function settleRun(
- supervisor: ConversationTelemetrySupervisor,
- status: "completed" | "errored" | "stopped",
- errorMessage?: string,
- ) {
- supervisor.handleFact({
- ...commonFact("wf-run-settled", 9, "parent-session"),
- sourceCommandId: "launch-input-1",
- kind: "workflow.lifecycle",
- phase: "run-settled",
- runId: "dwfrun-1",
- toolCallId: "tool-wf",
- status,
- ...(errorMessage === undefined ? {} : { errorMessage }),
- });
- }
- it("工具 step 零 token 带角色与 run 字段;usage 按 requestId 去重;子会话 turn.terminal 不结算;run-settled 一条汇总 step", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-workflow-actor",
- });
- spawnWorkflowActor(supervisor);
- childUsage(supervisor, "wf-usage-1", "req-1");
- // 同一 requestId 重放(重连 / 重放)不重复计。
- childUsage(supervisor, "wf-usage-1-dup", "req-1");
- supervisor.handleFact({
- ...commonFact("wf-tool-start", 3, CHILD),
- turnId: "child-turn-1",
- kind: "tool.lifecycle",
- phase: "started",
- toolCallId: "bash-1",
- toolName: "Bash",
- });
- supervisor.handleFact({
- ...commonFact("wf-tool-done", 4, CHILD),
- turnId: "child-turn-1",
- kind: "tool.lifecycle",
- phase: "completed",
- toolCallId: "bash-1",
- toolName: "Bash",
- durationMs: 12,
- });
- // 一次 ask 结束:子会话自己的 turn.terminal 不是子代理终态。
- supervisor.handleFact({
- ...commonFact("wf-child-terminal", 5, CHILD),
- turnId: "child-turn-1",
- kind: "turn.terminal",
- status: "success",
- });
- await supervisor.flushReportsForTest();
- const beforeSettle = reportsByElement(reportTelemetryEvent, "agent_step");
- expect(beforeSettle).toHaveLength(1);
- expect(beforeSettle[0]).toEqual(
- expect.objectContaining({
- talkId: "parent-session",
- messageId: "launch-input-1",
- eventExtraDetail: expect.objectContaining({
- step_type: "tool_call",
- tool_name: "Bash",
- tool_call_id: "tool_workflow_worker@1_bash-1",
- agent_id: "worker@1",
- agent_role: "workflow subagent",
- workflow_run_id: "dwfrun-1",
- child_session_id: CHILD,
- workflow_tool_call_id: "tool-wf",
- total_tokens: "0",
- model_name: "glm-provider/glm-5.3",
- }),
- }),
- );
- // 第二次 ask 的用量继续累计到同一个子代理。
- supervisor.handleFact({
- ...commonFact("wf-usage-2", 6, CHILD),
- turnId: "child-turn-2",
- kind: "usage.delta",
- requestId: "req-2",
- providerId: "glm-provider",
- modelId: "glm-5.3",
- inputTokens: 200,
- outputTokens: 10,
- totalTokens: 210,
- reasoningTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 0,
- });
- settleRun(supervisor, "completed");
- await supervisor.flushReportsForTest();
- const steps = reportsByElement(reportTelemetryEvent, "agent_step");
- expect(steps).toHaveLength(2);
- expect(steps[1]).toEqual(
- expect.objectContaining({
- talkId: "parent-session",
- messageId: "launch-input-1",
- eventExtraDetail: expect.objectContaining({
- step_type: "tool_call",
- tool_name: "Agent",
- tool_call_id: "tool_workflow_worker@1_dwfrun-1",
- agent_id: "worker@1",
- agent_role: "workflow subagent",
- workflow_run_id: "dwfrun-1",
- status: "success",
- token_usage_scope: "subagent_requests",
- model_request_count: "2",
- input_tokens: "1200",
- output_tokens: "60",
- total_tokens: "1260",
- model_name: "glm-provider/glm-5.3",
- provider_name: "glm.example.com",
- }),
- }),
- );
- expect(reportsByElement(reportTelemetryEvent, "message_completion")).toHaveLength(0);
- supervisor.dispose();
- });
- it("停下的 run:每个子代理一条 fail 汇总 step,错误原文保留,无 usage 时 token 为 0", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-workflow-stopped",
- });
- spawnWorkflowActor(supervisor);
- settleRun(supervisor, "stopped", "Workflow run stopped (user)");
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toEqual([
- expect.objectContaining({
- messageId: "launch-input-1",
- eventExtraDetail: expect.objectContaining({
- tool_name: "Agent",
- agent_role: "workflow subagent",
- status: "fail",
- error_msg: "[redacted]",
- model_request_count: "0",
- total_tokens: "0",
- }),
- }),
- ]);
- // 结算后迟到的用量不再改变任何上报(也不再有新的 step)。
- childUsage(supervisor, "wf-late", "req-late");
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(1);
- supervisor.dispose();
- });
- it("没有锚点(sourceCommandId 缺席)不登记也不上报", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-workflow-no-anchor",
- });
- supervisor.handleFact({
- version: 1,
- eventId: "wf-no-anchor",
- eventSeq: 1,
- occurredAt: 1,
- sessionId: "parent-session",
- kind: "workflow.lifecycle",
- phase: "actor-spawned",
- runId: "dwfrun-1",
- agentId: "worker@1",
- childSessionId: CHILD,
- });
- childUsage(supervisor, "wf-usage", "req-1");
- settleRun(supervisor, "completed");
- await supervisor.flushReportsForTest();
- expect(reportsByElement(reportTelemetryEvent, "agent_step")).toHaveLength(0);
- supervisor.dispose();
- });
- it("workflow 通知唤起的 main turn 计为 main_plus_wf,message_source 为 background_workflow", async () => {
- const { platform, reportTelemetryEvent } = createPlatform();
- const supervisor = new ConversationTelemetrySupervisor({
- platform,
- workspaceScopeKey: "scope-workflow-composition",
- });
- supervisor.handleFact({
- ...commonFact("wf-wake-start", 1, "session-1"),
- sourceCommandId: "wf-wake-command",
- kind: "turn.started",
- inputSource: "background_task",
- backgroundSource: "workflow",
- });
- supervisor.handleFact({
- ...commonFact("wf-wake-terminal", 2, "session-1"),
- sourceCommandId: "wf-wake-command",
- kind: "turn.terminal",
- status: "success",
- workflowResultConsumed: true,
- backgroundSubagentResultConsumed: true,
- });
- await supervisor.flushReportsForTest();
- expect(completionAgentCompositions(reportTelemetryEvent)).toEqual(["main_plus_bg_wf"]);
- expect(
- reportsByElement(reportTelemetryEvent, "message_completion")[0]?.eventExtraDetail,
- ).toMatchObject({ message_source: "background_workflow" });
- supervisor.dispose();
- });
- });
|