| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344 |
- import { setup as setupProfileScenario, done as profileDone } from "./subagent-profile-scenario.js";
- import { createRecordingSubagentSessionStore } from "./subagent-test-store.js";
- import { describe, expect, it, vi } from "vitest";
- import { createTestModelSelection } from "./test-model-selection.js";
- import { mkdtemp, readFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import {
- CoreErrorType,
- type HttpClientPort,
- type McpPort,
- type SkillContent,
- type SkillLoadOutcome,
- type SkillPort,
- SEND_MESSAGE_TOOL_NAME,
- RESPOND_TO_COORDINATOR_TOOL_NAME,
- SessionEventType,
- type SessionId,
- type SessionEvent,
- type SessionStorePort,
- createModelId,
- createModelProviderId,
- createSessionId,
- createToolCallId,
- createTraceId,
- createTurnId,
- isCoreError,
- modelMessageContentToText,
- } from "@zcode/contracts";
- import { createTestAgentRuntime } from "./test-agent-runtime.js";
- import { toMcpToolName } from "../src/mcp/index.js";
- import { PermissionService } from "../src/permission/index.js";
- import { createExploreSubagentPort } from "../src/subagent/runner.js";
- import { createTestSessionEventStore } from "./test-event-store.js";
- import {
- createTestModelFactory,
- type TestModelExecutionObservation,
- } from "./test-runtime-model.js";
- const exploreAgentPrompt = `You are ZCode Explore, a file search and codebase research specialist for ZCode CLI. You excel at thoroughly navigating and exploring codebases.
- === CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS ===
- This is a READ-ONLY exploration task. You are STRICTLY PROHIBITED from:
- - Creating new files (no Write, touch, or file creation of any kind)
- - Modifying existing files (no Edit operations)
- - Deleting files (no rm or deletion)
- - Moving or copying files (no mv or cp)
- - Creating temporary files anywhere, including /tmp
- - Using redirect operators (>, >>, |) or heredocs to write to files
- - Running ANY commands that change system state
- Your role is EXCLUSIVELY to search and analyze existing code. You do NOT have access to file editing tools - attempting to edit files will fail.
- Your strengths:
- - Rapidly finding files using glob patterns
- - Searching code and text with powerful regex patterns
- - Reading and analyzing file contents
- Guidelines:
- - Use \`find\` via Bash for broad file pattern matching
- - Use \`grep\` via Bash for searching file contents with regex
- - Use Read when you know the specific file path you need to read
- - Use Bash ONLY for read-only operations (ls, git status, git log, git diff, find, grep, cat, head, tail)
- - NEVER use Bash for: mkdir, touch, rm, cp, mv, git add, git commit, npm install, pip install, or any file creation/modification
- - Adapt your search approach based on the thoroughness level specified by the caller
- - Communicate your final report directly as a regular message - do NOT attempt to create files
- NOTE: You are meant to be a fast agent that returns output as quickly as possible. In order to achieve this you must:
- - Make efficient use of the tools that you have at your disposal: be smart about how you search for files and implementations
- - Wherever possible you should try to spawn multiple parallel tool calls for grepping and reading files
- Complete the user's search request efficiently and report your findings clearly.`;
- const subagentCommonNotes = `Notes:
- - Agent threads always have their cwd reset between bash calls, as a result please only use absolute file paths.
- - In your final response, share file paths (always absolute, never relative) that are relevant to the task. Include code snippets only when the exact text is load-bearing (e.g., a bug you found, a function signature the caller asked for) — do not recap code you merely read.
- - For clear communication with the user the assistant MUST avoid using emojis.
- - Do not use a colon before tool calls. Text like "Let me read the file:" followed by a read tool call should just be "Let me read the file." with a period.
- - Do NOT Write report/summary/findings/analysis .md files. Return findings directly as your final assistant message — the parent agent reads your text output, not files you create.`;
- const subagentEnvironmentContext = `Here is useful information about the environment you are running in:
- <env>
- Working directory: /Users/dev/Desktop/Z/z-code/apps/zcode-cli
- Is directory a git repo: Yes
- Platform: darwin
- Shell: Git Bash
- OS Version: Darwin 24.3.0
- </env>
- You are powered by the model named anthropic/claude-haiku-4-5-20251001-cc.`;
- const exploreSystemMessages = [
- "You are ZCode, an interactive coding agent",
- // 正文已合并为一段;保留原 prompt、Notes、环境及换行顺序的逐字校验。
- `\n${exploreAgentPrompt}\n\n${subagentCommonNotes}\n\n${subagentEnvironmentContext}`,
- ];
- async function waitForCondition(check: () => boolean, timeoutMs = 1_000): Promise<void> {
- const startedAt = Date.now();
- while (!check()) {
- if (Date.now() - startedAt > timeoutMs) {
- throw new Error("Timed out waiting for condition");
- }
- await new Promise((resolve) => setTimeout(resolve, 5));
- }
- }
- describe("Explore subagent", () => {
- it("fails a foreground run after the child runner stays inactive", async () => {
- const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-inactive-"));
- const emittedEvents: any[] = [];
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_inactive",
- inactivityTimeoutMs: 20,
- outputRootDir,
- async emitParentEvent(event) {
- emittedEvents.push(event);
- },
- runExploreAgent: async (request) => {
- await request.onSessionReady?.();
- return new Promise(() => {});
- },
- });
- const runPromise = port
- .run({
- agentType: "Explore",
- description: "Inactive child",
- parentToolCallId: createToolCallId("call_inactive_child"),
- prompt: "Search until the child stops reporting activity.",
- sessionId: createSessionId("parent_inactive_child"),
- trace: {
- traceId: createTraceId(),
- sessionId: createSessionId("parent_inactive_child"),
- turnId: createTurnId("turn_inactive_child"),
- },
- turnId: createTurnId("turn_inactive_child"),
- workingDirectory: "/tmp",
- workspaceRoot: "/tmp",
- })
- .then(
- () => ({ kind: "completed" as const }),
- (error: unknown) => ({ error, kind: "failed" as const }),
- );
- const result = await Promise.race([
- runPromise,
- new Promise<{ kind: "hung" }>((resolve) => setTimeout(() => resolve({ kind: "hung" }), 160)),
- ]);
- expect(result.kind).toBe("failed");
- if (result.kind !== "failed") return;
- expect(isCoreError(result.error)).toBe(true);
- if (isCoreError(result.error)) {
- expect(result.error.type).toBe(CoreErrorType.ToolTimeout);
- }
- expect(
- emittedEvents.some(
- (event) =>
- event.type === SessionEventType.SubagentStopped &&
- (event.payload as any).status === "failed",
- ),
- ).toBe(true);
- });
- it("fails a foreground run when the child stays inactive before session ready", async () => {
- const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-pre-ready-inactive-"));
- const emittedEvents: SessionEvent[] = [];
- const childStarted = createDeferred<void>();
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_pre_ready_inactive",
- inactivityTimeoutMs: 20,
- outputRootDir,
- async emitParentEvent(event) {
- emittedEvents.push(event);
- },
- runExploreAgent: async () => {
- childStarted.resolve();
- return new Promise(() => {});
- },
- });
- const runPromise = port
- .run({
- agentType: "Explore",
- description: "Pre-ready inactive child",
- parentToolCallId: createToolCallId("call_pre_ready_inactive_child"),
- prompt: "Never finish child setup.",
- sessionId: createSessionId("parent_pre_ready_inactive_child"),
- trace: {
- traceId: createTraceId(),
- sessionId: createSessionId("parent_pre_ready_inactive_child"),
- turnId: createTurnId("turn_pre_ready_inactive_child"),
- },
- turnId: createTurnId("turn_pre_ready_inactive_child"),
- workingDirectory: "/tmp",
- workspaceRoot: "/tmp",
- })
- .then(
- () => ({ kind: "completed" as const }),
- (error: unknown) => ({ error, kind: "failed" as const }),
- );
- await childStarted.promise;
- const result = await Promise.race([
- runPromise,
- new Promise<{ kind: "hung" }>((resolve) => setTimeout(() => resolve({ kind: "hung" }), 160)),
- ]);
- expect(result.kind).toBe("failed");
- if (result.kind !== "failed") return;
- expect(isCoreError(result.error)).toBe(true);
- if (isCoreError(result.error)) {
- expect(result.error.type).toBe(CoreErrorType.ToolTimeout);
- }
- expect(emittedEvents).toEqual([]);
- await expect(port.getTask?.("agent_pre_ready_inactive")).resolves.toBeUndefined();
- });
- it("stops waiting when the parent aborts while the child runner is unresponsive", async () => {
- const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-abort-"));
- const abortController = new AbortController();
- const emittedEvents: any[] = [];
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_unresponsive",
- outputRootDir,
- async emitParentEvent(event) {
- emittedEvents.push(event);
- },
- runExploreAgent: async (request) => {
- await request.onSessionReady?.();
- return new Promise(() => {});
- },
- });
- const runPromise = port
- .run(
- {
- agentType: "Explore",
- description: "Unresponsive child",
- parentToolCallId: createToolCallId("call_unresponsive_child"),
- prompt: "Search until the model stops responding.",
- sessionId: createSessionId("parent_unresponsive_child"),
- trace: {
- traceId: createTraceId(),
- sessionId: createSessionId("parent_unresponsive_child"),
- turnId: createTurnId("turn_unresponsive_child"),
- },
- turnId: createTurnId("turn_unresponsive_child"),
- workingDirectory: "/tmp",
- workspaceRoot: "/tmp",
- },
- { signal: abortController.signal },
- )
- .then(
- () => ({ kind: "completed" as const }),
- (error: unknown) => ({ error, kind: "failed" as const }),
- );
- await waitForCondition(() =>
- emittedEvents.some((event) => event.type === SessionEventType.SubagentSpawned),
- );
- abortController.abort(new Error("parent aborted unresponsive subagent"));
- const result = await Promise.race([
- runPromise,
- new Promise<{ kind: "hung" }>((resolve) => setTimeout(() => resolve({ kind: "hung" }), 80)),
- ]);
- expect(result.kind).toBe("failed");
- if (result.kind !== "failed") return;
- expect(isCoreError(result.error)).toBe(true);
- if (isCoreError(result.error)) {
- expect(result.error.type).toBe(CoreErrorType.ToolCancelled);
- }
- expect(
- emittedEvents.some(
- (event) =>
- event.type === SessionEventType.SubagentStopped &&
- (event.payload as any).status === "failed",
- ),
- ).toBe(true);
- });
- it("stops waiting when the parent aborts before the child session is ready", async () => {
- const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-pre-ready-abort-"));
- const abortController = new AbortController();
- const emittedEvents: SessionEvent[] = [];
- const childStarted = createDeferred<void>();
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_pre_ready_abort",
- outputRootDir,
- async emitParentEvent(event) {
- emittedEvents.push(event);
- },
- runExploreAgent: async () => {
- childStarted.resolve();
- return new Promise(() => {});
- },
- });
- const runPromise = port
- .run(
- {
- agentType: "Explore",
- description: "Pre-ready aborted child",
- parentToolCallId: createToolCallId("call_pre_ready_aborted_child"),
- prompt: "Never finish child setup.",
- sessionId: createSessionId("parent_pre_ready_aborted_child"),
- trace: {
- traceId: createTraceId(),
- sessionId: createSessionId("parent_pre_ready_aborted_child"),
- turnId: createTurnId("turn_pre_ready_aborted_child"),
- },
- turnId: createTurnId("turn_pre_ready_aborted_child"),
- workingDirectory: "/tmp",
- workspaceRoot: "/tmp",
- },
- { signal: abortController.signal },
- )
- .then(
- () => ({ kind: "completed" as const }),
- (error: unknown) => ({ error, kind: "failed" as const }),
- );
- await childStarted.promise;
- abortController.abort(new Error("parent aborted before child session ready"));
- const result = await Promise.race([
- runPromise,
- new Promise<{ kind: "hung" }>((resolve) => setTimeout(() => resolve({ kind: "hung" }), 80)),
- ]);
- expect(result.kind).toBe("failed");
- if (result.kind !== "failed") return;
- expect(isCoreError(result.error)).toBe(true);
- if (isCoreError(result.error)) {
- expect(result.error.type).toBe(CoreErrorType.ToolCancelled);
- }
- expect(emittedEvents).toEqual([]);
- await expect(port.getTask?.("agent_pre_ready_abort")).resolves.toBeUndefined();
- });
- it("runs through Agent with a read-only Explore tool pool and preserves trace context", async () => {
- const sessionId = createSessionId("runtime-explore-subagent");
- const eventStore = createTestSessionEventStore();
- const sessionStore = createRecordingSubagentSessionStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const childObservations: TestModelExecutionObservation[] = [];
- const parentSecondRequests: any[] = [];
- const liveEvents: SessionEvent[] = [];
- const childPersistedAtSpawn: boolean[] = [];
- let parentCallCount = 0;
- const modelSelection = {
- providerId: createModelProviderId("anthropic"),
- modelId: createModelId("claude-haiku-4-5-20251001-cc"),
- };
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- modelSelection: createTestModelSelection(modelSelection),
- bashShellSelection: {
- dialect: "git-bash",
- display: { name: "Git Bash" },
- path: "C:\\Program Files\\Git\\bin\\bash.exe",
- source: "user-config",
- },
- workingDirectory: "/Users/dev/Desktop/Z/z-code/apps/zcode-cli",
- currentDate: "2026-06-04",
- projectContext: {
- type: "node",
- packageManager: "pnpm",
- scripts: {
- test: "vitest",
- },
- },
- envInfo: {
- cwd: "/Users/dev/Desktop/Z/z-code/apps/zcode-cli",
- platform: "darwin",
- shell: "zsh",
- osVersion: "Darwin 24.3.0",
- nodeVersion: "24.14.0",
- isGitRepository: true,
- },
- },
- {
- eventStore,
- eventSink: {
- async onSessionEvent(event) {
- liveEvents.push(event);
- if (event.type === SessionEventType.SubagentSpawned) {
- const childSessionId = (event.payload as { childSessionId: string }).childSessionId;
- const child = await sessionStore.getSession(childSessionId as SessionId);
- childPersistedAtSpawn.push(child?.taskType === "subagent_child");
- }
- },
- },
- sessionStore,
- modelFactory: createTestModelFactory({
- properties: { supportsNativeWebSearch: true },
- async generateText(request: any, observation) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_explore",
- name: "Agent",
- input: {
- description: "Find runtime loop",
- prompt: "Find where the runtime injects tool results.",
- subagent_type: "Explore",
- },
- },
- ],
- usage: {
- inputTokens: 1,
- outputTokens: 1,
- totalTokens: 2,
- },
- };
- }
- parentSecondRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent used explore result",
- usage: {
- inputTokens: 2,
- outputTokens: 3,
- totalTokens: 5,
- },
- };
- }
- childRequests.push(request);
- childObservations.push(observation);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "Explore found runtime tool result injection in packages/core/src/runtime.ts.",
- usage: {
- inputTokens: 3,
- outputTokens: 4,
- totalTokens: 7,
- },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("Use an explore agent for this question");
- const parentEvents = await eventStore.getEvents(sessionId);
- const spawnEvent = parentEvents.find(
- (event) => event.type === SessionEventType.SubagentSpawned,
- );
- const stoppedEvent = parentEvents.find(
- (event) => event.type === SessionEventType.SubagentStopped,
- );
- const toolResultEvent = parentEvents.find(
- (event) => event.type === SessionEventType.ToolCallResult,
- );
- const childSessionId = (spawnEvent?.payload as any)?.childSessionId;
- const childEvents = await eventStore.getEvents(childSessionId);
- const childPersistedMessages = await sessionStore.messages({ sessionID: childSessionId });
- const childPersistedUserMessage = childPersistedMessages.find(
- (message) => message.info.role === "user",
- );
- const initialModelTimeline = childPersistedMessages
- .flatMap((message) => message.parts)
- .find((part) => part.type === "timeline" && part.timelineType === "model_change");
- const parentFirstRequestText = parentRequests[0].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const parentFirstToolNames = parentRequests[0].tools.map((tool: any) => tool.name);
- const childRequestText = childRequests[0].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const parentToolMessage = parentSecondRequests[0].messages.find(
- (message: any) => message.role === "tool",
- );
- expect(result.response).toBe("parent used explore result");
- expect(parentFirstRequestText).not.toContain("# agentsMd");
- expect(parentFirstRequestText).not.toContain("# claudeMd");
- expect(parentFirstRequestText).not.toContain("Project context:");
- expect(parentFirstRequestText).not.toContain("- Package manager: pnpm");
- expect(parentFirstRequestText).not.toContain("- `test`: vitest");
- expect(parentFirstRequestText).toContain("# currentDate");
- expect(parentFirstToolNames).toContain("Agent");
- expect(parentFirstToolNames).not.toContain("Task");
- expect(parentFirstToolNames).not.toContain("Glob");
- expect(parentFirstToolNames).not.toContain("Grep");
- expect(parentFirstToolNames).not.toContain("GoalRead");
- expect(childRequests).toHaveLength(1);
- expect(childRequestText).not.toContain("# agentsMd");
- expect(childRequestText).not.toContain("# claudeMd");
- expect(childRequestText).not.toContain("# user_instructions");
- expect(childRequestText).not.toContain("Project context:");
- expect(childRequestText).not.toContain("- Package manager: pnpm");
- expect(childRequestText).toContain("# currentDate");
- expect(childRequestText).toContain("Today's date is 2026-06-04.");
- expect(childObservations[0].model).toMatchObject({
- providerId: modelSelection.providerId,
- modelId: modelSelection.modelId,
- });
- expect(childObservations[0].factoryInput.providerOptions).toBeUndefined();
- expect(childRequests[0].options?.maxOutputTokens).toBe(32_000);
- // Explore 工具面:本地搜索/读取工具 + WebSearch client-side wrapper。
- // provider-visible 顺序由最终输出边界统一排序,本地工具追加在后。
- expect(childRequests[0].tools.map((tool: any) => tool.name)).toEqual([
- "Bash",
- "Read",
- "TodoWrite",
- "WebFetch",
- "WebSearch",
- RESPOND_TO_COORDINATOR_TOOL_NAME,
- ]);
- const childToolsByName = new Map(childRequests[0].tools.map((tool: any) => [tool.name, tool]));
- expect(childToolsByName.has("Glob")).toBe(false);
- expect(childToolsByName.has("Grep")).toBe(false);
- expect(childRequests[0].tools.map((tool: any) => tool.name)).not.toContain("Agent");
- expect(childRequests[0].tools.map((tool: any) => tool.name)).not.toContain("Task");
- expect(
- childRequests[0].messages
- .filter((message: any) => message.role === "system")
- .map((message: any) => message.content),
- ).toEqual(exploreSystemMessages);
- expect(childRequests[0].messages[1].content).not.toContain(
- "You help the user with software engineering work in the current workspace.",
- );
- expect(childPersistedUserMessage?.info.contextSnapshot?.envInfo?.shell).toBe("Git Bash");
- expect(childObservations[0].invocationContext?.traceContext?.traceId).toBe(result.traceId);
- if (!spawnEvent || !stoppedEvent || !toolResultEvent) {
- throw new Error("Expected subagent lifecycle events");
- }
- const spawnPayload = spawnEvent.payload as any;
- const stoppedPayload = stoppedEvent.payload as any;
- const toolResultPayload = toolResultEvent.payload as any;
- expect(spawnPayload.allowedTools).toEqual([
- "Bash",
- "Read",
- "WebFetch",
- "WebSearch",
- "TodoWrite",
- ]);
- expect(stoppedPayload.status).toBe("completed");
- expect(toolResultPayload.toolCallId).toBe("call_explore");
- expect(parentToolMessage.toolCallId).toBe("call_explore");
- expect(parentToolMessage.content).toContain(
- "Explore found runtime tool result injection in packages/core/src/runtime.ts.",
- );
- expect(parentToolMessage.content).toContain("agentId: agent_");
- expect(parentToolMessage.content).toContain("use SendMessage with to:");
- expect(parentToolMessage.content).toContain("<usage>subagent_tokens:");
- expect(parentToolMessage.content).toContain("tool_uses:");
- expect(parentToolMessage.content).toContain("duration_ms:");
- expect(parentToolMessage.content).not.toContain("totalToolUseCount");
- expect(childEvents.length).toBeGreaterThan(0);
- const initialModelSelected = childEvents.find(
- (event) => event.type === SessionEventType.ModelSelected,
- )?.payload;
- expect(initialModelSelected).toMatchObject({
- previousModelSelection: null,
- modelSelection: {
- providerId: modelSelection.providerId,
- modelId: modelSelection.modelId,
- },
- });
- expect(
- initialModelSelected && "modelSelection" in initialModelSelected
- ? initialModelSelected.modelSelection
- : undefined,
- ).not.toHaveProperty("role");
- expect(initialModelSelected).not.toHaveProperty("origin");
- expect(initialModelTimeline).toMatchObject({
- timelineType: "model_change",
- toModel: {
- providerId: modelSelection.providerId,
- modelId: modelSelection.modelId,
- },
- });
- expect(
- initialModelTimeline?.type === "timeline" ? initialModelTimeline.fromModel : "unexpected",
- ).toBeUndefined();
- expect(childPersistedAtSpawn).toEqual([true]);
- expect(
- liveEvents.filter((event) => event.sessionId === childSessionId).map((event) => event.id),
- ).toEqual(childEvents.map((event) => event.id));
- expect(new Set([...parentEvents, ...childEvents].map((event) => event.traceId))).toEqual(
- new Set([result.traceId]),
- );
- });
- it("reports child turn tool call count in the Agent result usage trailer", async () => {
- const sessionId = createSessionId("runtime-explore-subagent-tool-count");
- const eventStore = createTestSessionEventStore();
- const sessionStore = createRecordingSubagentSessionStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const childObservations: TestModelExecutionObservation[] = [];
- let parentCallCount = 0;
- let childCallCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- sessionStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any, observation) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_counted_explore",
- name: "Agent",
- input: {
- description: "Count child tools",
- prompt: "Use one todo update before returning.",
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent saw counted child tool use",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- childObservations.push(observation);
- childCallCount++;
- if (childCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_child_todo",
- name: "TodoWrite",
- input: {
- todos: [
- {
- content: "Count child tool calls",
- priority: "high",
- status: "in_progress",
- },
- ],
- },
- },
- ],
- usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "Child completed after one TodoWrite.",
- usage: { inputTokens: 3, outputTokens: 4, totalTokens: 7 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("Use an explore agent and report its usage");
- const parentEvents = await eventStore.getEvents(sessionId);
- const stoppedEvent = parentEvents.find(
- (event) => event.type === SessionEventType.SubagentStopped,
- );
- const parentToolMessage = parentRequests[1].messages.find(
- (message: any) => message.role === "tool",
- );
- expect(result.response).toBe("parent saw counted child tool use");
- expect(childRequests).toHaveLength(2);
- expect((stoppedEvent?.payload as any)?.totalToolUseCount).toBe(1);
- expect(parentToolMessage.content).toContain("Child completed after one TodoWrite.");
- expect(parentToolMessage.content).toContain("tool_uses: 1");
- expect(parentToolMessage.content).not.toContain("tool_uses: 0");
- });
- it("normalizes lowercase built-in Explore subagent_type before launching", async () => {
- const sessionId = createSessionId("runtime-lowercase-explore-subagent");
- const eventStore = createTestSessionEventStore();
- const childRequests: any[] = [];
- let parentCallCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_lowercase_explore",
- name: "Agent",
- input: {
- description: "Find runtime loop",
- prompt: "Find where runtime loop is implemented.",
- subagent_type: "explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- const toolResultText = request.messages
- .filter((message: any) => message.role === "tool")
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- expect(toolResultText).not.toContain("Unknown subagent type");
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent used normalized explore result",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "lowercase explore resolved to the built-in Explore agent.",
- usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("Use explore to inspect the runtime");
- expect(result.response).toBe("parent used normalized explore result");
- expect(childRequests).toHaveLength(1);
- const childToolNames = childRequests[0].tools.map((tool: any) => tool.name);
- expect(childToolNames).toContain("Read");
- expect(childToolNames).toContain("Bash");
- expect(childToolNames).toContain("TodoWrite");
- expect(childToolNames).not.toContain("Agent");
- expect(childToolNames).not.toContain("Write");
- });
- it("reports ambiguous normalized subagent_type from the launch path", async () => {
- const sessionId = createSessionId("runtime-ambiguous-normalized-subagent");
- const eventStore = createTestSessionEventStore();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "foo-bar",
- description: "First normalized collision.",
- source: "project",
- systemPrompt: "first",
- tools: ["Read"],
- },
- {
- name: "foo_bar",
- description: "Second normalized collision.",
- source: "project",
- systemPrompt: "second",
- tools: ["Read"],
- background: true,
- },
- ],
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (!toolNames.includes("Agent")) {
- throw new Error("Ambiguous subagent_type must not launch a child agent");
- }
- const toolResultText = request.messages
- .filter((message: any) => message.role === "tool")
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- if (toolResultText) {
- expect(toolResultText).toContain("Agent type 'foo bar' is ambiguous");
- expect(toolResultText).toContain("foo-bar");
- expect(toolResultText).toContain("foo_bar");
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent saw ambiguous agent type",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_ambiguous_agent",
- name: "Agent",
- input: {
- description: "Resolve ambiguous agent",
- prompt: "Try the ambiguous agent.",
- subagent_type: "foo bar",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("Use the ambiguous custom agent");
- expect(result.response).toBe("parent saw ambiguous agent type");
- });
- it("normalizes subagent_type without stripping arbitrary punctuation", async () => {
- const sessionId = createSessionId("runtime-punctuation-subagent");
- const eventStore = createTestSessionEventStore();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "report-v2",
- description: "Report agent.",
- source: "project",
- systemPrompt: "report",
- tools: ["Read"],
- },
- ],
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (!toolNames.includes("Agent")) {
- throw new Error("Punctuation mismatch must not launch a child agent");
- }
- const toolResultText = request.messages
- .filter((message: any) => message.role === "tool")
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- if (toolResultText) {
- expect(toolResultText).toContain("Agent type 'report.v2' not found");
- expect(toolResultText).toContain("report-v2");
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent saw unknown agent type",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_punctuation_agent",
- name: "Agent",
- input: {
- description: "Resolve punctuation agent",
- prompt: "Try the punctuation agent.",
- subagent_type: "report.v2",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("Use the report.v2 custom agent");
- expect(result.response).toBe("parent saw unknown agent type");
- });
- // docs/dynamic-workflow/launch.md「Gray release」:父会话关着灰度时,Agent 工具不能成为
- // 绕过它的后门;父会话开着(或根本不参与灰度)时子代理照旧拿得到。
- //
- // 这里刻意用**显式声明 tools 的自定义 agent profile**:默认 agent 的子工具面是「父
- // registry 里现存工具名」的交集,父会话已经被剃过,子代理自然也没有——那条路径挡得住,
- // 但挡住它的不是灰度门。profile 显式写 `tools: [...]` 时 resolveSubagentToolAllowlist
- // 走的是另一条分支(request.allowedTools 原样保留,只过 disallow 规则),父 registry
- // 不再参与。唯一能挡住它的就是子 runtime 自己继承到的 dynamicWorkflowEnabled。
- describe("dynamic-workflow gray gate inheritance into subagent children", () => {
- async function childToolNamesForParent(
- parentConfig: Record<string, unknown>,
- options: { activateParentFirst?: boolean } = {},
- ): Promise<{ child: string[]; parent: string[] }> {
- const sessionId = createSessionId(
- [
- "runtime-gray-gate-subagent",
- String(parentConfig.dynamicWorkflowEnabled),
- String(parentConfig.dynamicWorkflowToolsOnDemand),
- String(options.activateParentFirst),
- ].join("-"),
- );
- const childRequests: any[] = [];
- let parentCallCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- ...parentConfig,
- modelSelection: createTestModelSelection({
- providerId: createModelProviderId("provider-main"),
- modelId: createModelId("main-model"),
- }),
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "workflow-author",
- description: "显式点名工作流工具的自定义 agent。",
- source: "project",
- systemPrompt: "You author workflows.",
- tools: ["Read", "CreateWorkflow", "ResumeWorkflowRun"],
- model: "main",
- maxTurns: 2,
- },
- ],
- },
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_gray_gate_agent",
- name: "Agent",
- input: {
- description: "Author a workflow",
- prompt: "Try to author a workflow.",
- subagent_type: "workflow-author",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "child done.",
- usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 },
- };
- },
- } as never),
- },
- );
- if (options.activateParentFirst) {
- await runtime.activateDynamicWorkflowTools({ source: "command" });
- }
- await runtime.executeTurn("Use the workflow-author agent");
- return {
- child: childRequests[0].tools.map((tool: any) => tool.name),
- parent: runtime.getTools().map((tool) => tool.name),
- };
- }
- it.each([
- { parentConfig: { dynamicWorkflowEnabled: false }, available: false },
- { parentConfig: { dynamicWorkflowEnabled: true }, available: true },
- // 不参与灰度的父会话(TUI / headless `-p`,DWG-04):子代理必须保留全部工具。
- { parentConfig: {}, available: true },
- // DWG-12:onDemand 父会话未激活时子代理没有工作流工具;父已激活则子出生即有
- // (launch.md「On demand: activation」Children 段)。
- {
- parentConfig: { dynamicWorkflowEnabled: true, dynamicWorkflowToolsOnDemand: true },
- available: false,
- },
- {
- parentConfig: { dynamicWorkflowEnabled: true, dynamicWorkflowToolsOnDemand: true },
- activateParentFirst: true,
- available: true,
- },
- ])("parent %j", async ({ parentConfig, available, activateParentFirst }) => {
- const { child, parent } = await childToolNamesForParent(parentConfig, {
- activateParentFirst,
- });
- for (const name of ["CreateWorkflow", "ResumeWorkflowRun"]) {
- expect({ name, inParent: parent.includes(name) }).toEqual({ name, inParent: available });
- expect({ name, inChild: child.includes(name) }).toEqual({ name, inChild: available });
- }
- // 反向断言:profile 里其余工具在三种取值下都照常给到,门只碰工作流那几件。
- expect(child).toContain("Read");
- });
- });
- it("routes Explore provider runtime headers refreshes through the parent session", async () => {
- const sessionId = createSessionId("runtime-explore-provider-headers-parent-route");
- const eventStore = createTestSessionEventStore();
- // 主 runtime 每次调用报自己的 sessionId;子 runtime 拿到的是父 runtime 派生的实例
- // (helpers/child-client-ports.ts),派生层把 sessionId 改写成父会话。
- const refreshes: Array<{ providerId: string; boundSessionId: string; routedSessionId?: string }> =
- [];
- const childRequests: any[] = [];
- let parentCallCount = 0;
- const modelSelection = {
- providerId: createModelProviderId("provider-start-plan"),
- modelId: createModelId("start-plan-model"),
- };
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- modelSelection: createTestModelSelection(modelSelection),
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- providerRuntimeHeadersPort: {
- shouldRefreshBeforeModelRequest: () => true,
- async refreshBeforeModelRequest(input) {
- refreshes.push({
- providerId: input.providerId,
- boundSessionId: sessionId,
- // 回归守卫:子 runtime 的刷新到这里必须已经是父会话的 sessionId。若派生层漏掉
- // 改写,子 runtime 会把子会话发给客户端(2026-09-09 挂死事故)。
- routedSessionId: input.sessionId,
- });
- return {
- headersApplied: true,
- };
- },
- },
- modelFactory: createTestModelFactory({
- async generateText(request: any, observation) {
- await observation.invocationContext?.refreshRuntimeHeadersBeforeAttempt?.({
- attempt: 1,
- abortSignal: request.abortSignal,
- });
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_explore_headers",
- name: "Agent",
- input: {
- description: "Check provider headers",
- prompt: "Check the provider headers route.",
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent completed after explore",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "Explore checked the provider headers route.",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- await runtime.executeTurn("Use an explore agent to check headers");
- const parentEvents = await eventStore.getEvents(sessionId);
- const spawnEvent = parentEvents.find(
- (event) => event.type === SessionEventType.SubagentSpawned,
- );
- if (!spawnEvent) {
- throw new Error("Expected subagent spawn event");
- }
- const childSessionId = (spawnEvent.payload as any).childSessionId;
- const childEvents = await eventStore.getEvents(childSessionId);
- expect(childRequests).toHaveLength(1);
- expect(childSessionId).not.toBe(sessionId);
- expect(childEvents.length).toBeGreaterThan(0);
- // 三次刷新(父两次 + 子一次)全部以父会话为路由身份到达端口。
- expect(refreshes).toEqual([
- { providerId: "provider-start-plan", boundSessionId: sessionId, routedSessionId: sessionId },
- { providerId: "provider-start-plan", boundSessionId: sessionId, routedSessionId: sessionId },
- { providerId: "provider-start-plan", boundSessionId: sessionId, routedSessionId: sessionId },
- ]);
- });
- it("passes the parent HTTP client port into Explore WebFetch calls", async () => {
- const sessionId = createSessionId("runtime-explore-webfetch-http-client");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const childObservations: TestModelExecutionObservation[] = [];
- const webFetchProcessingRequests: any[] = [];
- let parentCallCount = 0;
- let childCallCount = 0;
- const body = new TextEncoder().encode(
- "<html><body><h1>Explore WebFetch</h1><p>http client port is wired.</p></body></html>",
- );
- const httpClientPort: HttpClientPort = {
- request: vi.fn<HttpClientPort["request"]>(async (request) => {
- expect(request.url).toBe("https://example.com/explore-webfetch-port");
- return {
- body,
- bytes: body.byteLength,
- durationMs: 7,
- headers: { "content-type": "text/html; charset=utf-8" },
- status: 200,
- statusText: "OK",
- url: "https://example.com/explore-webfetch-port",
- };
- }),
- };
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- httpClientPort,
- modelFactory: createTestModelFactory({
- async generateText(request: any, observation) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_explore_webfetch",
- name: "Agent",
- input: {
- description: "Fetch a public page",
- prompt: "Use WebFetch to inspect the page.",
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent saw Explore WebFetch result",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- if (toolNames.includes("WebFetch")) {
- childRequests.push(request);
- childCallCount++;
- if (childCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_child_webfetch",
- name: "WebFetch",
- input: {
- prompt: "What confirms the HTTP client port wiring?",
- url: "https://example.com/explore-webfetch-port",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "Explore fetched the page through the injected HTTP client.",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- webFetchProcessingRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "The page confirms the HTTP client port is wired.",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("Use an explore agent to fetch a URL");
- expect(result.response).toBe("parent saw Explore WebFetch result");
- expect(parentRequests).toHaveLength(2);
- expect(childRequests).toHaveLength(2);
- expect(httpClientPort.request).toHaveBeenCalledTimes(1);
- expect(webFetchProcessingRequests).toHaveLength(1);
- expect(
- webFetchProcessingRequests[0].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n"),
- ).toContain("http client port is wired");
- });
- it("runs a custom profile-backed subagent with its prompt and tool allowlist", async () => {
- const sessionId = createSessionId("runtime-custom-subagent-profile");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "zcode-reviewer",
- description: "用中文检查实现边界和风险。",
- source: "project",
- systemPrompt: "你是 zcode-reviewer,只输出中文结论。",
- tools: ["Read"],
- disallowedTools: ["Bash"],
- model: "main",
- maxTurns: 2,
- },
- ],
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_custom_agent",
- name: "Agent",
- input: {
- description: "检查实现边界",
- prompt: "检查 subagent 与 main agent 的边界。",
- subagent_type: "ZCode Reviewer",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 采纳了自定义 agent 的结论。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- childObservations.push(observation);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "自定义 agent 已按中文 system prompt 完成边界检查。",
- usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让自定义 agent 检查边界");
- const parentFirstRequestText = parentRequests[0].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const childRequestText = childRequests[0].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const childToolNames = childRequests[0].tools.map((tool: any) => tool.name);
- const parentAgentToolDescription =
- parentRequests[0].tools.find((tool: any) => tool.name === "Agent")?.description ?? "";
- expect(result.response).toBe("父 agent 采纳了自定义 agent 的结论。");
- expect(parentFirstRequestText).toContain("Available agent types for the Agent tool:");
- expect(parentFirstRequestText).toContain("zcode-reviewer");
- expect(parentAgentToolDescription).toContain(
- "Available agent types are listed in <system-reminder> messages in the conversation.",
- );
- expect(parentFirstRequestText).toContain(
- "- zcode-reviewer: 用中文检查实现边界和风险。 (Tools: Read)",
- );
- expect(childRequestText).toContain("你是 zcode-reviewer,只输出中文结论。");
- expect(childRequestText).not.toContain("READ-ONLY MODE");
- expect(childToolNames).toContain("Read");
- expect(childToolNames).not.toContain("Bash");
- expect(childToolNames).not.toContain("Edit");
- expect(childToolNames).not.toContain("Agent");
- expect(childToolNames).not.toContain("Task");
- });
- it("runs a custom Explore with its own prompt, main tools, MCP descriptor, and parent permissions", async () => {
- const sessionId = createSessionId("runtime-custom-explore-profile");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const descriptor = {
- serverName: "search__server",
- toolName: "lookup__item",
- description: "Look up an indexed item.",
- inputSchema: { type: "object", properties: {} },
- };
- const mcpToolName = toMcpToolName(descriptor);
- const connectedStatus = {
- status: "connected" as const,
- toolCount: 1,
- transport: "stdio" as const,
- updatedAt: new Date(0).toISOString(),
- };
- const callTool = vi.fn(async () => ({ content: [{ text: "found", type: "text" as const }] }));
- const mcpPort = createMockMcpPort({
- callTool,
- async connectConfiguredServers() {
- return {
- statuses: { search__server: connectedStatus },
- tools: [descriptor],
- };
- },
- });
- const parentPermissionService = new PermissionService({
- allowedTools: new Set([mcpToolName]),
- disallowedTools: new Set(),
- autoApproveHighRisk: false,
- allowMediumRiskInAutoMode: false,
- });
- const checkPermission = vi.spyOn(parentPermissionService, "checkPermission");
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mcp: {
- enabled: true,
- servers: {
- search__server: { command: "search-mcp", type: "stdio" },
- },
- },
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "Explore",
- description: "自定义可写 Explore。",
- source: "project",
- systemPrompt: "你是项目自定义 Explore,可以执行写入和 MCP 查询。",
- tools: ["Write", mcpToolName],
- disallowedTools: ["Bash"],
- mcpServers: ["search__server"],
- model: "main",
- maxTurns: 2,
- },
- ],
- },
- },
- {
- eventStore,
- mcpPort,
- permissionService: parentPermissionService,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_custom_agent",
- name: "Agent",
- input: {
- description: "检查实现边界",
- prompt: "检查 subagent 与 main agent 的边界。",
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 采纳了自定义 agent 的结论。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- if (childRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_custom_explore_mcp",
- name: mcpToolName,
- input: {},
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "自定义 Explore 已完成边界检查。",
- usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让自定义 agent 检查边界");
- const parentFirstRequestText = parentRequests[0].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const childRequestText = childRequests[0].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const childToolNames = childRequests[0].tools.map((tool: any) => tool.name);
- const parentAgentToolDescription =
- parentRequests[0].tools.find((tool: any) => tool.name === "Agent")?.description ?? "";
- expect(result.response).toBe("父 agent 采纳了自定义 agent 的结论。");
- expect(parentFirstRequestText).toContain("Available agent types for the Agent tool:");
- expect(parentFirstRequestText).toContain("自定义可写 Explore");
- expect(parentAgentToolDescription).toContain(
- "Available agent types are listed in <system-reminder> messages in the conversation.",
- );
- expect(parentFirstRequestText).toContain(
- `- Explore: 自定义可写 Explore。 (Tools: Write, ${mcpToolName})`,
- );
- expect(childRequestText).toContain("你是项目自定义 Explore,可以执行写入和 MCP 查询。");
- expect(childRequestText).not.toContain("READ-ONLY MODE");
- expect(childToolNames).toContain("Write");
- expect(childToolNames).toContain(mcpToolName);
- expect(childToolNames).not.toContain("Bash");
- expect(childToolNames).not.toContain("Agent");
- expect(childToolNames).not.toContain("Task");
- expect(callTool).toHaveBeenCalledTimes(1);
- expect(
- checkPermission.mock.calls.some(
- ([context]) => context.toolName === mcpToolName && context.mode === "build",
- ),
- ).toBe(true);
- });
- it.each([
- { label: "omitted", mcpServers: undefined },
- { label: "empty", mcpServers: [] as readonly string[] },
- ])(
- "keeps an empty-prompt custom Explore unrestricted when mcpServers is $label",
- async ({ label, mcpServers }) => {
- const sessionId = createSessionId(`runtime-custom-explore-unrestricted-${label}`);
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const descriptor = {
- serverName: "plugin:android-emulator:android-emulator",
- toolName: "android_preflight",
- description: "Check Android emulator prerequisites.",
- inputSchema: { type: "object", properties: {} },
- };
- const mcpToolName = toMcpToolName(descriptor);
- const connectedStatus = {
- status: "connected" as const,
- toolCount: 1,
- transport: "stdio" as const,
- updatedAt: new Date(0).toISOString(),
- };
- const connectConfiguredServers = vi.fn(async () => ({
- statuses: {
- "plugin:android-emulator:android-emulator": connectedStatus,
- },
- tools: [descriptor],
- }));
- const listTools = vi.fn(async () => []);
- const status = vi.fn(async () => ({}));
- const mcpPort = createMockMcpPort({ connectConfiguredServers, listTools, status });
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mcp: {
- enabled: true,
- servers: {
- "plugin:android-emulator:android-emulator": {
- command: "android-emulator-mcp",
- type: "stdio",
- },
- },
- },
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "Explore",
- description: "Custom Explore using the unrestricted parent MCP snapshot.",
- source: "user",
- systemPrompt: "",
- tools: ["*"],
- ...(mcpServers === undefined ? {} : { mcpServers }),
- },
- ],
- },
- },
- {
- eventStore,
- mcpPort,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: `call_custom_explore_unrestricted_${label}`,
- name: "Agent",
- input: {
- description: "Test unrestricted MCP snapshot",
- prompt: "Confirm the custom Explore can use the parent MCP snapshot.",
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent received the custom Explore result",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "custom Explore received the unrestricted MCP descriptor",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("Use the custom Explore agent");
- const childRequestText = childRequests[0].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const childToolNames = childRequests[0].tools.map((tool: any) => tool.name);
- expect(result.response).toBe("parent received the custom Explore result");
- expect(childRequests).toHaveLength(1);
- expect(childRequestText).not.toContain("READ-ONLY MODE");
- expect(childToolNames).toContain(mcpToolName);
- expect(connectConfiguredServers).toHaveBeenCalledTimes(1);
- expect(status).not.toHaveBeenCalled();
- expect(listTools).not.toHaveBeenCalled();
- },
- );
- it("removes plan-mode tools from custom wildcard subagents", async () => {
- const sessionId = createSessionId("runtime-custom-subagent-no-plan-mode-tools");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "wildcard-reviewer",
- description: "Use every safe inherited tool.",
- source: "project",
- systemPrompt: "Review with inherited tools, but do not enter planning mode.",
- tools: ["*"],
- },
- ],
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_wildcard_agent",
- name: "Agent",
- input: {
- description: "检查 wildcard custom agent",
- prompt: "检查 custom wildcard 子 agent 工具面。",
- subagent_type: "wildcard-reviewer",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 收到 wildcard 结果。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "wildcard child done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让 wildcard-reviewer 跑一下");
- const childToolNames = childRequests[0].tools.map((tool: any) => tool.name);
- expect(result.response).toBe("父 agent 收到 wildcard 结果。");
- expect(childToolNames).toContain("Read");
- expect(childToolNames).toContain("Bash");
- expect(childToolNames).not.toContain("EnterPlanMode");
- expect(childToolNames).not.toContain("ExitPlanMode");
- expect(childToolNames).not.toContain("Agent");
- expect(childToolNames).not.toContain("Task");
- });
- it("removes plan-mode tools from explicit custom subagent tool lists", async () => {
- const sessionId = createSessionId("runtime-explicit-custom-subagent-no-plan-tools");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "explicit-reviewer",
- description: "Asks for an explicit tool subset.",
- source: "project",
- systemPrompt: "Review with explicitly declared tools.",
- tools: ["Read", "EnterPlanMode", "ExitPlanMode"],
- },
- ],
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_explicit_agent",
- name: "Agent",
- input: {
- description: "检查 explicit custom agent",
- prompt: "检查 custom explicit 子 agent 工具面。",
- subagent_type: "explicit-reviewer",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 收到 explicit 结果。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "explicit child done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让 explicit-reviewer 跑一下");
- const childToolNames = childRequests[0].tools.map((tool: any) => tool.name);
- const parentAgentToolDescription =
- parentRequests[0].tools.find((tool: any) => tool.name === "Agent")?.description ?? "";
- expect(result.response).toBe("父 agent 收到 explicit 结果。");
- expect(
- modelMessageContentToText(
- parentRequests[0].messages.find((message: any) =>
- modelMessageContentToText(message.content).includes(
- "Available agent types for the Agent tool:",
- ),
- ).content,
- ),
- ).toContain("- explicit-reviewer: Asks for an explicit tool subset. (Tools: Read)");
- expect(parentAgentToolDescription).not.toContain("EnterPlanMode");
- expect(parentAgentToolDescription).not.toContain("ExitPlanMode");
- expect(childToolNames).toContain("Read");
- expect(childToolNames).not.toContain("EnterPlanMode");
- expect(childToolNames).not.toContain("ExitPlanMode");
- });
- it("inherits disabled native find and grep enhancements in child Bash", async () => {
- const sessionId = createSessionId("runtime-subagent-native-search-disabled");
- const eventStore = createTestSessionEventStore();
- const backend = {
- kind: "native-binaries" as const,
- findCommand: "/tools/bfs",
- grepCommand: "/tools/ugrep",
- rgCommand: "/tools/rg",
- };
- let childCallCount = 0;
- let parentCallCount = 0;
- let childBashPrelude: unknown;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- bashShellSelection: {
- dialect: "posix",
- display: { name: "bash" },
- id: "posix:/bin/bash",
- label: "bash",
- path: "/bin/bash",
- source: "auto-detected",
- },
- embeddedSearchBackend: backend,
- mode: "yolo",
- nativeSearchEnhancementsEnabled: false,
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "search-child",
- description: "Runs one search command.",
- source: "project",
- systemPrompt: "Run the requested search.",
- tools: ["Bash"],
- },
- ],
- },
- },
- {
- eventStore,
- executionPort: {
- async run(request) {
- childBashPrelude = request.bashPrelude;
- const now = new Date();
- return {
- status: "completed",
- exitCode: 0,
- stdout: { text: "needle", bytes: 6, truncated: false },
- stderr: { text: "", bytes: 0, truncated: false },
- durationMs: 1,
- timedOut: false,
- cancelled: false,
- startedAt: now,
- completedAt: now,
- };
- },
- },
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_native_search_child",
- name: "Agent",
- input: {
- description: "检查搜索继承",
- prompt: "搜索 needle。",
- subagent_type: "search-child",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childCallCount++;
- if (childCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_child_grep",
- name: "Bash",
- input: { command: "grep needle file.txt" },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "child done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- await runtime.executeTurn("让 search-child 搜索");
- expect(childBashPrelude).toEqual({
- kind: "embedded-search",
- backend,
- findAndGrepEnabled: false,
- });
- });
- it("does not let a subagent profile re-add tools removed by the parent runtime", async () => {
- const sessionId = createSessionId("runtime-subagent-parent-disallowed-tools");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- toolDisallowlist: ["Bash(git *)"],
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "bashy-reviewer",
- description: "Requests Bash and Read.",
- source: "project",
- systemPrompt: "Review without shell access.",
- tools: ["Bash", "Read"],
- model: "main",
- maxTurns: 2,
- },
- ],
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_bashy_agent",
- name: "Agent",
- input: {
- description: "检查 Bash 是否被移除",
- prompt: "检查子 agent 工具面。",
- subagent_type: "bashy-reviewer",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 收到结果。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "child done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让 bashy-reviewer 跑一下");
- const childToolNames = childRequests[0].tools.map((tool: any) => tool.name);
- expect(result.response).toBe("父 agent 收到结果。");
- expect(childToolNames).toContain("Read");
- expect(childToolNames).not.toContain("Bash");
- });
- it("auto-enables Skill and allows bare aliases for declared plugin skills", async () => {
- const sessionId = createSessionId("runtime-subagent-filtered-skills");
- const eventStore = createTestSessionEventStore();
- const loadedSkills: string[] = [];
- const allowedPluginSkill = {
- ...skillMetadata("allowed-skill", "/plugins/superpowers/skills/allowed/SKILL.md"),
- pluginName: "superpowers",
- qualifiedName: "superpowers:allowed-skill",
- rootPath: "/plugins/superpowers/skills",
- scope: "system" as const,
- source: "plugin" as const,
- };
- const conflictingPluginSkill = {
- ...skillMetadata("allowed-skill", "/plugins/other/skills/allowed/SKILL.md"),
- pluginName: "other-plugin",
- qualifiedName: "other-plugin:allowed-skill",
- rootPath: "/plugins/other/skills",
- scope: "system" as const,
- source: "plugin" as const,
- };
- const skillOutcome: SkillLoadOutcome = {
- diagnostics: [],
- skills: [
- conflictingPluginSkill,
- allowedPluginSkill,
- skillMetadata("blocked-skill", "/skills/blocked/SKILL.md"),
- ],
- totalDiscovered: 3,
- };
- const skillPort: SkillPort = {
- async discoverSkills() {
- return skillOutcome;
- },
- async loadSkill(request): Promise<SkillContent> {
- const metadata = skillOutcome.skills.find(
- (skill) => skill.name === request.name || skill.qualifiedName === request.name,
- );
- if (!metadata) throw new Error(`Skill not found: ${request.name}`);
- loadedSkills.push(metadata.qualifiedName ?? metadata.name);
- return {
- baseDirectory: metadata.directory,
- bytesRead: 12,
- content: `# ${metadata.qualifiedName ?? metadata.name}\n\n只使用允许的 skill。`,
- metadata,
- sizeBytes: 12,
- truncated: false,
- };
- },
- };
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "skill-agent",
- description: "使用声明的 skill 完成任务。",
- source: "project",
- systemPrompt: "你必须先加载允许的 skill。",
- tools: ["Read"],
- skills: ["superpowers:allowed-skill"],
- },
- ],
- },
- },
- {
- eventStore,
- skillPort,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_skill_agent",
- name: "Agent",
- input: {
- description: "测试 skill 过滤",
- prompt: "加载 allowed-skill 后返回结论。",
- subagent_type: "skill-agent",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 收到了 skill-agent 的结论。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- if (childRequests.length === 1) {
- expect(toolNames).toContain("Skill");
- expect(toolNames).toContain("Read");
- expect(toolNames).not.toContain("Agent");
- expect(toolNames).not.toContain("Task");
- const requestText = request.messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- expect(requestText).toContain("superpowers:allowed-skill");
- expect(requestText).not.toContain("other-plugin:allowed-skill");
- expect(requestText).not.toContain("blocked-skill");
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_allowed_skill",
- name: "Skill",
- input: { skill: "allowed-skill" },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "skill-agent 已加载允许的 skill。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让 skill-agent 跑一下");
- expect(result.response).toBe("父 agent 收到了 skill-agent 的结论。");
- expect(loadedSkills).toEqual(["superpowers:allowed-skill"]);
- expect(childRequests).toHaveLength(2);
- });
- it("fails before the child request when a scoped MCP server is unavailable in the parent startup snapshot", async () => {
- const sessionId = createSessionId("runtime-subagent-scoped-mcp-server-unavailable");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const mcpPort = createMockMcpPort();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mcp: { enabled: true, servers: {} },
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "mcp-agent",
- description: "需要 MCP server 的 agent。",
- source: "project",
- systemPrompt: "已配置的 MCP server 在 parent 启动快照中不可用时不应该启动。",
- mcpServers: ["required-server"],
- },
- ],
- },
- },
- {
- eventStore,
- mcpPort,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_mcp_agent",
- name: "Agent",
- input: {
- description: "测试 scoped MCP server 不可用",
- prompt: "检查已配置的 MCP server 是否在 parent 启动快照中已连接。",
- subagent_type: "mcp-agent",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 看到了 scoped MCP server 不可用错误。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "mcp-agent 正常启动。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让 mcp-agent 跑一下");
- const secondParentRequestText = parentRequests[1].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- expect(result.response).toBe("父 agent 看到了 scoped MCP server 不可用错误。");
- expect(childRequests).toEqual([]);
- expect(secondParentRequestText).toContain("Required MCP server is not connected");
- expect(secondParentRequestText).toContain("required-server");
- });
- it("fails fast when an explicitly allowed MCP descriptor is absent from the parent startup snapshot", async () => {
- const sessionId = createSessionId("runtime-subagent-missing-mcp-descriptor");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const mcpPort = createMockMcpPort();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mcp: { enabled: true, servers: {} },
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "mcp-agent",
- description: "需要 MCP tool 的 agent。",
- source: "project",
- systemPrompt: "如果显式 MCP tool 不在 parent 启动快照中就不应该启动。",
- tools: ["mcp__required-server__search"],
- },
- ],
- },
- },
- {
- eventStore,
- mcpPort,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_mcp_agent",
- name: "Agent",
- input: {
- description: "测试 MCP descriptor 缺失",
- prompt: "检查显式 MCP tool 是否存在于 parent 启动快照。",
- subagent_type: "mcp-agent",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 看到了 MCP tool 缺失错误。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "不应该真正启动子 agent。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让 mcp-agent 跑一下");
- const secondParentRequestText = parentRequests[1].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- expect(result.response).toBe("父 agent 看到了 MCP tool 缺失错误。");
- expect(childRequests).toEqual([]);
- expect(secondParentRequestText).toContain(
- "Required MCP tool is not available in the parent startup snapshot",
- );
- expect(secondParentRequestText).toContain("mcp__required-server__search");
- });
- it("starts a wildcard general-purpose subagent when plugin MCP status uses raw server names", async () => {
- const sessionId = createSessionId("runtime-subagent-wildcard-plugin-mcp");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const connectedStatus = {
- status: "connected" as const,
- toolCount: 1,
- transport: "stdio" as const,
- updatedAt: new Date(0).toISOString(),
- };
- const mcpPort = createMockMcpPort({
- async connectConfiguredServers() {
- return {
- statuses: {
- "plugin:android-emulator:android-emulator": connectedStatus,
- },
- tools: [
- {
- serverName: "plugin:android-emulator:android-emulator",
- toolName: "android_preflight",
- description: "Check Android emulator prerequisites.",
- inputSchema: { type: "object", properties: {} },
- },
- ],
- };
- },
- async listTools() {
- return [
- {
- serverName: "plugin:android-emulator:android-emulator",
- toolName: "android_preflight",
- description: "Check Android emulator prerequisites.",
- inputSchema: { type: "object", properties: {} },
- },
- ];
- },
- async status() {
- return {
- "plugin:android-emulator:android-emulator": connectedStatus,
- };
- },
- });
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mcp: {
- enabled: true,
- servers: {
- "plugin:android-emulator:android-emulator": {
- command: "android-emulator-mcp",
- type: "stdio",
- },
- },
- },
- mode: "build",
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- mcpPort,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- expect(toolNames).toContain(
- "mcp__plugin_android-emulator_android-emulator__android_preflight",
- );
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_general_purpose",
- name: "Agent",
- input: {
- description: "测试 plugin MCP 名称",
- prompt: "确认 general-purpose 可以启动。",
- subagent_type: "general-purpose",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 收到了 general-purpose 结果。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- const childToolNames = (request.tools ?? []).map((tool: any) => tool.name);
- expect(childToolNames).toContain(
- "mcp__plugin_android-emulator_android-emulator__android_preflight",
- );
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "general-purpose 正常启动。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让 general-purpose 跑一下");
- const secondParentRequestText = parentRequests[1].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- expect(result.response).toBe("父 agent 收到了 general-purpose 结果。");
- expect(childRequests).toHaveLength(1);
- expect(secondParentRequestText).not.toContain("Required MCP server is not connected");
- });
- it("scopes custom Explore wildcard MCP tools to the fixed parent startup snapshot", async () => {
- const sessionId = createSessionId("runtime-custom-explore-wildcard-mcp");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const allowedDescriptor = {
- serverName: "alpha__server",
- toolName: "allowed__lookup",
- description: "Allowed alpha lookup.",
- inputSchema: { type: "object", properties: {} },
- };
- const blockedDescriptor = {
- serverName: "alpha__server",
- toolName: "blocked__lookup",
- description: "Blocked alpha lookup.",
- inputSchema: { type: "object", properties: {} },
- };
- const outOfScopeDescriptor = {
- serverName: "beta__server",
- toolName: "beta__lookup",
- description: "Out-of-scope beta lookup.",
- inputSchema: { type: "object", properties: {} },
- };
- const lateDescriptor = {
- serverName: "alpha__server",
- toolName: "late__lookup",
- description: "Only visible after startup.",
- inputSchema: { type: "object", properties: {} },
- };
- const allowedMcpToolName = toMcpToolName(allowedDescriptor);
- const blockedMcpToolName = toMcpToolName(blockedDescriptor);
- const outOfScopeMcpToolName = toMcpToolName(outOfScopeDescriptor);
- const lateMcpToolName = toMcpToolName(lateDescriptor);
- const connectedStatus = {
- status: "connected" as const,
- toolCount: 2,
- transport: "stdio" as const,
- updatedAt: new Date(0).toISOString(),
- };
- const connectConfiguredServers = vi.fn(async () => ({
- statuses: {
- alpha__server: connectedStatus,
- beta__server: { ...connectedStatus, toolCount: 1 },
- },
- tools: [allowedDescriptor, blockedDescriptor, outOfScopeDescriptor],
- }));
- const listTools = vi.fn(async () => [lateDescriptor]);
- const status = vi.fn(async () => ({
- alpha__server: { ...connectedStatus, toolCount: 1 },
- }));
- const mcpPort = createMockMcpPort({
- connectConfiguredServers,
- listTools,
- status,
- });
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mcp: {
- enabled: true,
- servers: {
- alpha__server: { command: "alpha-mcp", type: "stdio" },
- beta__server: { command: "beta-mcp", type: "stdio" },
- },
- },
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "Explore",
- description: "Custom wildcard Explore.",
- source: "user",
- systemPrompt: "Use every inherited tool within the configured MCP scope.",
- tools: ["*"],
- disallowedTools: [blockedMcpToolName],
- mcpServers: ["alpha__server"],
- },
- ],
- },
- },
- {
- eventStore,
- mcpPort,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_custom_explore_wildcard",
- name: "Agent",
- input: {
- description: "测试 wildcard MCP snapshot",
- prompt: "确认 custom Explore 只看到 scoped startup snapshot。",
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 收到了 custom Explore 结果。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- const childToolNames = (request.tools ?? []).map((tool: any) => tool.name);
- expect(childToolNames).toContain(allowedMcpToolName);
- expect(childToolNames).not.toContain(blockedMcpToolName);
- expect(childToolNames).not.toContain(outOfScopeMcpToolName);
- expect(childToolNames).not.toContain(lateMcpToolName);
- expect(childToolNames).not.toContain("Agent");
- expect(childToolNames).not.toContain("Task");
- expect(childToolNames).not.toContain("EnterPlanMode");
- expect(childToolNames).not.toContain("ExitPlanMode");
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "custom Explore 正常启动。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让 custom Explore 跑一下");
- expect(result.response).toBe("父 agent 收到了 custom Explore 结果。");
- expect(childRequests).toHaveLength(1);
- expect(connectConfiguredServers).toHaveBeenCalledTimes(1);
- expect(status).not.toHaveBeenCalled();
- expect(listTools).not.toHaveBeenCalled();
- });
- it("does not require an MCP server for a parent-denied subagent tool", async () => {
- const sessionId = createSessionId("runtime-subagent-parent-denied-mcp-tool");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const mcpPort = createMockMcpPort();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mcp: { enabled: true, servers: {} },
- mode: "build",
- toolDisallowlist: ["mcp__required-server__search"],
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "mcp-agent",
- description: "声明了会被 parent deny 的 MCP tool。",
- source: "project",
- systemPrompt: "被 parent 移除的 MCP tool 不应该触发 server 检查。",
- tools: ["mcp__required-server__search", "Read"],
- },
- ],
- },
- },
- {
- eventStore,
- mcpPort,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_mcp_agent",
- name: "Agent",
- input: {
- description: "测试被 deny 的 MCP tool",
- prompt: "检查被 parent deny 的 MCP tool 是否还会要求 server。",
- subagent_type: "mcp-agent",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "父 agent 收到了 mcp-agent 结果。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "mcp-agent 正常启动。",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让 mcp-agent 跑一下");
- const childToolNames = childRequests[0].tools.map((tool: any) => tool.name);
- const secondParentRequestText = parentRequests[1].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- expect(result.response).toBe("父 agent 收到了 mcp-agent 结果。");
- expect(childToolNames).toContain("Read");
- expect(childToolNames).not.toContain("mcp__required-server__search");
- expect(secondParentRequestText).not.toContain("Required MCP server is not connected");
- });
- it("returns after launching background Explore and consumes notification on the next turn", async () => {
- const sessionId = createSessionId("runtime-background-explore-join");
- const eventStore = createTestSessionEventStore();
- const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-output-"));
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- let releaseChild!: () => void;
- const childCanComplete = new Promise<void>((resolve) => {
- releaseChild = resolve;
- });
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- outputRootDir,
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_background_explore",
- name: "Agent",
- input: {
- description: "Find runtime join",
- prompt: "Find whether the parent turn waits for background results.",
- run_in_background: true,
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- if (parentRequests.length === 2) {
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "background explore launched",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent summarized the background explore result",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- await childCanComplete;
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "Background Explore found the join boundary in turn-stop.ts.",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- let firstTurnSettled = false;
- const turnPromise = runtime.executeTurn("Use a background explore agent").then((value) => {
- firstTurnSettled = true;
- return value;
- });
- await waitForCondition(() => parentRequests.length === 2 && childRequests.length === 1);
- await waitForCondition(() => firstTurnSettled);
- const eventsBeforeChildCompletes = await eventStore.getEvents(sessionId);
- expect(
- eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.TurnComplete),
- ).toBe(true);
- expect(
- eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.SubagentStopped),
- ).toBe(false);
- const launchResult = await turnPromise;
- releaseChild();
- await waitForCondition(async () => {
- const events = await eventStore.getEvents(sessionId);
- return events.some((event) => event.type === SessionEventType.SubagentStopped);
- });
- await waitForCondition(async () => {
- const events = await eventStore.getEvents(sessionId);
- return events.some((event) => event.type === SessionEventType.BackgroundTaskCompleted);
- });
- await waitForCondition(() => parentRequests.length === 3);
- const parentThirdRequestText = parentRequests[2].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const events = await eventStore.getEvents(sessionId);
- const stoppedIndex = events.findIndex(
- (event) => event.type === SessionEventType.SubagentStopped,
- );
- expect(launchResult.response).toBe("background explore launched");
- expect(parentRequests).toHaveLength(3);
- expect(parentThirdRequestText).toContain("<task-notification>");
- expect(parentThirdRequestText).toContain("<tool-use-id>call_background_explore</tool-use-id>");
- expect(parentThirdRequestText).not.toContain("<task-type>local_agent</task-type>");
- expect(parentThirdRequestText).toContain("<output-file>");
- expect(parentThirdRequestText).toContain("<status>completed</status>");
- expect(parentThirdRequestText).toContain("<usage>");
- expect(parentThirdRequestText).toContain("<subagent_tokens>");
- expect(parentThirdRequestText).toContain("<tool_uses>");
- expect(parentThirdRequestText).toContain("<duration_ms>");
- expect(parentThirdRequestText).toContain("Background Explore found the join boundary");
- expect(stoppedIndex).toBeGreaterThanOrEqual(0);
- const stoppedPayload = events[stoppedIndex]?.payload as any;
- const agentOutputDir = join(outputRootDir, sessionId, stoppedPayload.agentId);
- await expect(readFile(join(agentOutputDir, "output.txt"), "utf8")).resolves.toContain(
- "Background Explore found the join boundary",
- );
- await expect(readFile(join(agentOutputDir, "task.output"), "utf8")).resolves.toContain(
- "Background Explore found the join boundary",
- );
- await expect(readFile(join(agentOutputDir, "transcript.jsonl"), "utf8")).rejects.toMatchObject({
- code: "ENOENT",
- });
- const metadata = JSON.parse(await readFile(join(agentOutputDir, "metadata.json"), "utf8"));
- expect(metadata).toMatchObject({
- agentId: stoppedPayload.agentId,
- parentSessionId: sessionId,
- parentToolUseId: "call_background_explore",
- profileId: "Explore",
- status: "completed",
- });
- expect(metadata).not.toHaveProperty("transcriptFile");
- });
- it("launches background Explore by default when run_in_background is requested", async () => {
- const sessionId = createSessionId("runtime-subagent-background-default");
- const eventStore = createTestSessionEventStore();
- const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-output-"));
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const childGate = createDeferred<void>();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- outputRootDir,
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_legacy_background_explore",
- name: "Agent",
- input: {
- description: "Legacy background",
- prompt: "Return after the background child finishes.",
- run_in_background: true,
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "foreground subagent completed",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- await childGate.promise;
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "Background child finished after run_in_background detached.",
- usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 },
- };
- },
- } as never),
- },
- );
- let turnSettled = false;
- const turnPromise = runtime.executeTurn("Use a legacy background explore").then((value) => {
- turnSettled = true;
- return value;
- });
- await waitFor(async () => childRequests.length === 1);
- await waitFor(() => parentRequests.length === 2);
- await waitFor(() => turnSettled);
- const eventsBeforeChildCompletes = await eventStore.getEvents(sessionId);
- expect(turnSettled).toBe(true);
- expect(
- eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.TurnComplete),
- ).toBe(true);
- expect(
- eventsBeforeChildCompletes.some(
- (event) => event.type === SessionEventType.BackgroundTaskStarted,
- ),
- ).toBe(true);
- expect(
- eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.SubagentStopped),
- ).toBe(false);
- childGate.resolve();
- const result = await turnPromise;
- await waitFor(async () => {
- const events = await eventStore.getEvents(sessionId);
- return events.some((event) => event.type === SessionEventType.BackgroundTaskCompleted);
- });
- await waitFor(() => parentRequests.length === 3);
- const notificationRequestText = parentRequests[2].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const events = await eventStore.getEvents(sessionId);
- expect(result.response).toBe("foreground subagent completed");
- expect(parentRequests).toHaveLength(3);
- expect(notificationRequestText).toContain("<task-notification>");
- expect(notificationRequestText).toContain(
- "<tool-use-id>call_legacy_background_explore</tool-use-id>",
- );
- expect(notificationRequestText).toContain(
- "Background child finished after run_in_background detached.",
- );
- expect(events.some((event) => event.type === SessionEventType.SubagentStopped)).toBe(true);
- expect(events.some((event) => event.type === SessionEventType.BackgroundTaskStarted)).toBe(
- true,
- );
- expect(events.some((event) => event.type === SessionEventType.BackgroundTaskCompleted)).toBe(
- true,
- );
- });
- it("runs foreground subagents in parallel when run_in_background is omitted", async () => {
- const sessionId = createSessionId("runtime-subagent-foreground-parallel");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const childGate = createDeferred<void>();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_foreground_parallel_one",
- name: "Agent",
- input: {
- description: "Parallel one",
- prompt: "Run foreground child one.",
- subagent_type: "Explore",
- },
- },
- {
- id: "call_foreground_parallel_two",
- name: "Agent",
- input: {
- description: "Parallel two",
- prompt: "Run foreground child two.",
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "foreground parallel subagents completed",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- const childIndex = childRequests.length + 1;
- childRequests.push(request);
- await childGate.promise;
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: `Parallel foreground child ${childIndex} finished.`,
- usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 },
- };
- },
- } as never),
- },
- );
- let turnSettled = false;
- const turnPromise = runtime.executeTurn("Use parallel foreground agents").then((value) => {
- turnSettled = true;
- return value;
- });
- await waitFor(async () => childRequests.length === 2);
- await new Promise((resolve) => setTimeout(resolve, 25));
- const eventsBeforeChildCompletes = await eventStore.getEvents(sessionId);
- expect(turnSettled).toBe(false);
- expect(
- eventsBeforeChildCompletes.some((event) => event.type === SessionEventType.TurnComplete),
- ).toBe(false);
- expect(
- eventsBeforeChildCompletes.some(
- (event) => event.type === SessionEventType.BackgroundTaskStarted,
- ),
- ).toBe(false);
- childGate.resolve();
- const result = await turnPromise;
- const finalParentRequestText = parentRequests[1].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const events = await eventStore.getEvents(sessionId);
- const spawnEvents = events.filter((event) => event.type === SessionEventType.SubagentSpawned);
- expect(result.response).toBe("foreground parallel subagents completed");
- expect(childRequests).toHaveLength(2);
- expect(parentRequests).toHaveLength(2);
- expect(finalParentRequestText).toContain("Parallel foreground child 1 finished.");
- expect(finalParentRequestText).toContain("Parallel foreground child 2 finished.");
- expect(spawnEvents).toHaveLength(2);
- expect(spawnEvents.every((event) => (event.payload as any).background !== true)).toBe(true);
- expect(events.some((event) => event.type === SessionEventType.BackgroundTaskStarted)).toBe(
- false,
- );
- expect(events.some((event) => event.type === SessionEventType.BackgroundTaskCompleted)).toBe(
- false,
- );
- });
- it("can SendMessage to a running background local_agent", async () => {
- const sessionId = createSessionId("runtime-background-local-agent-send-message");
- const eventStore = createTestSessionEventStore();
- const outputRootDir = await mkdtemp(join(tmpdir(), "zcode-subagent-output-"));
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- let capturedAgentId = "";
- let releaseChild!: () => void;
- const childCanComplete = new Promise<void>((resolve) => {
- releaseChild = resolve;
- });
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- subagents: {
- outputRootDir,
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_background_agent",
- name: "Agent",
- input: {
- description: "后台 agent 等待消息",
- prompt: "先等待父 agent 的补充消息。",
- run_in_background: true,
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- if (parentRequests.length === 2) {
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "background launched",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- if (parentRequests.length === 3) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_SendMessage_to_agent",
- name: SEND_MESSAGE_TOOL_NAME,
- input: {
- to: capturedAgentId,
- summary: "补充检查目标",
- message: "请把检查目标改成权限链路,并在最终回答里说明已收到消息。",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "message sent",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- if (childRequests.length === 1) {
- await childCanComplete;
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "child initial result",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "child handled coordinator message",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const launchResult = await runtime.executeTurn("启动后台 agent");
- await waitForCondition(() => childRequests.length === 1);
- const spawnEvent = (await eventStore.getEvents(sessionId)).find(
- (event) => event.type === SessionEventType.SubagentSpawned,
- );
- capturedAgentId = String((spawnEvent?.payload as any)?.agentId ?? "");
- const sendResult = await runtime.executeTurn("给后台 agent 发送补充消息");
- releaseChild();
- await waitForCondition(() => childRequests.length >= 2);
- const secondChildRequestText = childRequests[1].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- expect(launchResult.response).toBe("background launched");
- expect(sendResult.response).toBe("message sent");
- expect(capturedAgentId).toMatch(/^agent_/u);
- expect(childRequests[1].messages.slice(-2).map((message: any) => message.role)).toEqual([
- "assistant",
- "user",
- ]);
- expect(secondChildRequestText).toContain(
- "The coordinator sent a message while you were working:",
- );
- expect(secondChildRequestText).toContain("补充检查目标");
- expect(secondChildRequestText).toContain("Address this before completing your current task.");
- expect(secondChildRequestText).toContain("请把检查目标改成权限链路");
- });
- it("routes RespondToCoordinator to the parent and continues the child tool loop", async () => {
- const sessionId = createSessionId("runtime-child-respond-to-coordinator");
- const eventStore = createTestSessionEventStore();
- const sessionStore = createRecordingSubagentSessionStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- sessionStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- if (parentRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_child_response_agent",
- name: "Agent",
- input: {
- description: "检查权限链路",
- prompt: "检查权限链路,并在收到询问时回复进度后继续任务。",
- subagent_type: "general-purpose",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "parent received child response",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- if (childRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_child_todo_before_response",
- name: "TodoRead",
- input: {},
- },
- {
- id: "call_child_response_progress",
- name: RESPOND_TO_COORDINATOR_TOOL_NAME,
- input: {
- summary: "权限链路进度",
- message: "已完成前半段检查,正在继续剩余权限分支。",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- if (childRequests.length === 2) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_child_todo_after_response",
- name: "TodoRead",
- input: {},
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "child completed the remaining permission work",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("让 child 检查权限链路");
- const secondChildRequestText = childRequests[1].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- const finalParentRequestText = parentRequests[1].messages
- .map((message: any) => modelMessageContentToText(message.content))
- .join("\n");
- expect(result.response).toBe("parent received child response");
- expect(parentRequests).toHaveLength(2);
- expect(childRequests).toHaveLength(3);
- expect(childRequests[0].tools.map((tool: any) => tool.name)).toContain(
- RESPOND_TO_COORDINATOR_TOOL_NAME,
- );
- expect(secondChildRequestText).toContain("was queued for the coordinator");
- expect(finalParentRequestText).toContain("<subagent-message>");
- expect(finalParentRequestText).toContain("<summary>权限链路进度</summary>");
- expect(finalParentRequestText).toContain(
- "<message>已完成前半段检查,正在继续剩余权限分支。</message>",
- );
- expect(finalParentRequestText).toContain("child completed the remaining permission work");
- });
- it("runs built-in Explore on the main model by default while attributing usage to the subagent role", async () => {
- const sessionId = createSessionId("runtime-explore-main-model");
- const eventStore = createTestSessionEventStore();
- const childRequests: any[] = [];
- const childObservations: TestModelExecutionObservation[] = [];
- let parentCallCount = 0;
- const modelSelection = {
- providerId: createModelProviderId("provider-main"),
- modelId: createModelId("main-model"),
- };
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- modelSelection: createTestModelSelection(modelSelection),
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any, observation) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_explore_main",
- name: "Agent",
- input: {
- description: "Find runtime loop",
- prompt: "Find where the runtime injects tool results.",
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- childObservations.push(observation);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "Explore used the inherited main model.",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- await runtime.executeTurn("Use an explore agent");
- expect(childRequests).toHaveLength(1);
- // Explore 默认继承主 Loop 的 Model;usage 由 child runtime 的 taskType 归因,
- // 不再依赖旧 ModelSelection.role 修改模型身份。
- expect(childObservations[0].model).toMatchObject({
- providerId: modelSelection.providerId,
- modelId: modelSelection.modelId,
- });
- expect(childObservations[0].factoryInput.providerOptions).toBeUndefined();
- });
- it("ignores a legacy call model and runs the built-in general-purpose agent on its configured model", async () => {
- const sessionId = createSessionId("runtime-general-purpose-built-in-model-override");
- const eventStore = createTestSessionEventStore();
- const childRequests: any[] = [];
- const childObservations: TestModelExecutionObservation[] = [];
- let parentCallCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- modelSelection: {
- providerId: createModelProviderId("provider-main"),
- modelId: createModelId("main-model"),
- },
- workingDirectory: "/workspace/project",
- subagents: {
- builtInModelSelectionOverrides: {
- "general-purpose": {
- providerId: "custom-openai",
- modelId: "gpt-5.4",
- options: { reasoningLevel: "high" },
- },
- },
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any, observation) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_general_purpose_override",
- name: "Agent",
- input: {
- description: "Investigate default agent override",
- model: "haiku",
- prompt: "Check the configured model override.",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- childObservations.push(observation);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "general-purpose used the override.",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- await runtime.executeTurn("Use the default Agent");
- expect(childRequests).toHaveLength(1);
- expect(childObservations[0].model).toMatchObject({
- providerId: "custom-openai",
- modelId: "gpt-5.4",
- });
- });
- it("ignores a legacy call model and uses the built-in Explore override for child request, spawn event, and tool branch capability", async () => {
- const sessionId = createSessionId("runtime-explore-built-in-model-override");
- const eventStore = createTestSessionEventStore();
- const childRequests: any[] = [];
- const childObservations: TestModelExecutionObservation[] = [];
- let parentCallCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- modelSelection: {
- providerId: createModelProviderId("provider-main"),
- modelId: createModelId("main-model"),
- },
- workingDirectory: "/workspace/project",
- subagents: {
- builtInModelSelectionOverrides: {
- Explore: { providerId: "custom-openai", modelId: "glm-5.2" },
- },
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- properties: { supportsNativeWebSearch: true },
- async generateText(request: any, observation) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_explore_override",
- name: "Agent",
- input: {
- description: "Check Explore override",
- // 修复原因:旧 transcript 可能继续生成调用级 model;Explore 也必须只消费 profile。
- model: "haiku",
- prompt: "Check the configured model override.",
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- childObservations.push(observation);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "Explore used the override.",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- await runtime.executeTurn("Use an explore agent");
- const events = await eventStore.getEvents(sessionId);
- const spawnEvent = events.find((event) => event.type === SessionEventType.SubagentSpawned);
- const childToolNames = childRequests[0].tools.map((tool: any) => tool.name);
- expect(childRequests).toHaveLength(1);
- expect(childObservations[0].model).toMatchObject({
- providerId: "custom-openai",
- modelId: "glm-5.2",
- });
- expect(spawnEvent?.payload).toMatchObject({
- agentType: "Explore",
- model: "custom-openai/glm-5.2",
- });
- expect(childToolNames).toEqual([
- "Bash",
- "Read",
- "TodoWrite",
- "WebFetch",
- "WebSearch",
- RESPOND_TO_COORDINATOR_TOOL_NAME,
- ]);
- });
- it("freezes each custom child from the latest ModelFactory facts", async () => {
- const sessionId = createSessionId("runtime-subagent-concrete-model");
- const eventStore = createTestSessionEventStore();
- const childRequests: any[] = [];
- const childObservations: TestModelExecutionObservation[] = [];
- let parentCallCount = 0;
- let currentMaxOutputTokens = 64_000;
- const modelSelection = {
- providerId: createModelProviderId("provider-main"),
- modelId: createModelId("main-model"),
- };
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- modelSelection: createTestModelSelection(modelSelection),
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "code-reviewer",
- description: "Review code",
- source: "user",
- systemPrompt: "Review carefully.",
- modelSelection: { providerId: "custom-openai", modelId: "gpt-5.4" },
- tools: ["Read"],
- },
- ],
- },
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- maxOutputTokens: (input) =>
- input.selection.providerId === "custom-openai" && input.selection.modelId === "gpt-5.4"
- ? currentMaxOutputTokens
- : 32_000,
- properties: { contextWindow: 128_000 },
- async generateText(request: any, observation) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentCallCount++;
- if (parentCallCount <= 2) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: `call_custom_model_agent_${parentCallCount}`,
- name: "Agent",
- input: {
- description: "Review changed files",
- prompt: "Review changed files.",
- subagent_type: "code-reviewer",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- childObservations.push(observation);
- currentMaxOutputTokens = 16_000;
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "custom model reviewer finished.",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- await runtime.executeTurn("Use custom reviewer");
- expect(childRequests).toHaveLength(2);
- expect(
- childRequests.map((request, index) => ({
- maxOutputTokens: request.options?.maxOutputTokens,
- modelId: childObservations[index].model.modelId,
- providerId: childObservations[index].model.providerId,
- })),
- ).toEqual([
- {
- maxOutputTokens: 64_000,
- modelId: "gpt-5.4",
- providerId: "custom-openai",
- },
- {
- maxOutputTokens: 16_000,
- modelId: "gpt-5.4",
- providerId: "custom-openai",
- },
- ]);
- });
- it("resolves an explicit profile again for a resumed child new input without changing the profile", async () => {
- const sessionId = createSessionId("subagent-effective-resume");
- const original = {
- providerId: "account:personal",
- modelId: "fixture",
- options: { reasoningLevel: "high" },
- };
- let effective = { ...original, providerId: "account:team" };
- const resolveEffectiveModelSelection = vi.fn(() => ({ effectiveSelection: effective }));
- const requests: string[] = [];
- const requestMessages: unknown[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "yolo",
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "reviewer",
- description: "Review",
- source: "user",
- systemPrompt: "Review",
- tools: ["Read"],
- modelSelection: original,
- },
- ],
- },
- },
- {
- eventStore: createTestSessionEventStore(),
- sessionStore: createRecordingSubagentSessionStore(),
- resolveEffectiveModelSelection,
- modelFactory: createTestModelFactory({
- async generateText(_request, observation) {
- requests.push(observation.model.providerId);
- requestMessages.push(_request.messages);
- return {
- finishReason: "stop",
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- }),
- },
- );
- const port = (runtime as any).subagentPort;
- const turnId = createTurnId("effective-resume-turn");
- const trace = { traceId: createTraceId("effective-resume-trace"), sessionId, turnId };
- const first = await port.run({
- agentType: "reviewer",
- description: "Review",
- parentToolCallId: "first",
- prompt: "Review first",
- sessionId,
- trace,
- turnId,
- workingDirectory: "/workspace/project",
- workspaceRoot: "/workspace/project",
- });
- expect(first.status).toBe("completed");
- effective = original;
- const resumed = await port.sendMessage({
- to: first.agentId,
- message: "Review next",
- summary: "Next",
- parentToolCallId: "second",
- sessionId,
- trace,
- turnId,
- workingDirectory: "/workspace/project",
- workspaceRoot: "/workspace/project",
- });
- expect(resumed.delivery).toBe("resumed_background");
- expect((await port.waitForTask(first.agentId))?.status).toBe("completed");
- expect(requests).toEqual(["account:team", "account:personal"]);
- expect(JSON.stringify(requestMessages[1])).toContain("Review next");
- expect(JSON.stringify(requestMessages[1])).not.toContain(
- "The coordinator sent a message while you were working:",
- );
- expect(resolveEffectiveModelSelection).toHaveBeenCalledTimes(2);
- expect(original.providerId).toBe("account:personal");
- });
- it("resumes a failed child with the next context profile while retaining identity and transcript", async () => {
- const profile = (version: string) => ({
- name: "reviewer",
- source: "user" as const,
- description: "Review",
- tools: version === "old" ? ["Read"] : ["Grep"],
- systemPrompt: `PROFILE_${version}`,
- modelSelection: {
- providerId: "custom",
- modelId: version,
- options: { reasoningLevel: version === "old" ? "low" : "high" },
- },
- });
- const scenario = setupProfileScenario(async ({ model }) => {
- if (model === "old") throw new Error("Invalid model configuration");
- return profileDone();
- });
- scenario.update(profile("old"));
- const { runtime, store: sessionStore, children: requests } = scenario;
- const sessionId = runtime.sessionId;
- const internal = runtime as any;
- const turnId = createTurnId();
- const trace = { traceId: createTraceId(), sessionId, turnId };
- const context = {
- sessionId,
- turnId,
- trace,
- workingDirectory: "/workspace/project",
- workspaceRoot: "/workspace/project",
- };
- const prepare = () =>
- internal.prepareAgentDefinitions({
- signal: new AbortController().signal,
- traceContext: trace,
- });
- await prepare();
- await expect(
- internal.subagentPort.run({
- ...context,
- agentType: "reviewer",
- description: "Review",
- parentToolCallId: "first",
- prompt: "ORIGINAL_CHILD_INPUT",
- }),
- ).rejects.toThrow("Turn execution failed");
- const first = Object.values(internal.runtimeTaskRegistry.all())[0] as {
- status: string;
- agentId: string;
- };
- expect(first.status).toBe("failed");
- const originalTask = await internal.subagentPort.getTask(first.agentId);
- scenario.update(profile("new"));
- // 保存发生在当前上下文内:同轮 SendMessage 仍使用旧 profile,直到下轮 prepare。
- await internal.subagentPort.sendMessage({
- ...context,
- to: first.agentId,
- message: "SAME_TURN_RETRY",
- summary: "Retry",
- parentToolCallId: "same-turn",
- });
- expect((await internal.subagentPort.waitForTask(first.agentId))?.status).toBe("failed");
- expect(requests.at(-1)?.model).toBe("old");
- await prepare();
- const resumed = await internal.subagentPort.sendMessage({
- ...context,
- to: first.agentId,
- message: "RESUME_CHILD_INPUT",
- summary: "Next",
- parentToolCallId: "second",
- });
- expect(resumed.delivery).toBe("resumed_background");
- const task = await internal.subagentPort.waitForTask(first.agentId);
- expect(task).toMatchObject({
- status: "completed",
- agentId: first.agentId,
- childSessionId: originalTask.childSessionId,
- });
- const captured = requests.at(-1)!;
- const request = {
- ...captured,
- content: JSON.stringify(captured.request.messages),
- tools: captured.request.tools?.map((tool) => tool.name) ?? [],
- };
- expect(request.model).toBe("new");
- expect(request.reasoning).toBe("high");
- expect(request.content).toContain("PROFILE_new");
- expect(request.content).not.toContain("PROFILE_old");
- expect(request.content).toContain("ORIGINAL_CHILD_INPUT");
- expect(request.content).toContain("RESUME_CHILD_INPUT");
- expect(request.tools).toContain("Grep");
- expect(request.tools).not.toContain("Read");
- const modelEntry = (
- await sessionStore.sessionEntries({
- sessionID: task.childSessionId,
- type: "runtime/model_selection",
- })
- ).at(-1);
- expect(modelEntry?.data).toMatchObject({ modelId: "new", options: { reasoningLevel: "high" } });
- const history = await sessionStore.messages({ sessionID: task.childSessionId });
- expect(history.flatMap((message) => message.parts)).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- type: "timeline",
- timelineType: "model_change",
- fromModel: expect.objectContaining({ modelId: "old" }),
- toModel: expect.objectContaining({ modelId: "new" }),
- }),
- ]),
- );
- scenario.load.mockResolvedValueOnce({ activeAgents: [] });
- await prepare();
- const missing = await internal.subagentPort.sendMessage({
- ...context,
- to: first.agentId,
- message: "Again",
- summary: "Next",
- parentToolCallId: "third",
- });
- expect(missing.status).toBe("failed");
- expect(JSON.stringify(missing)).toContain("profile reviewer is unavailable");
- scenario.update(profile("new"));
- await prepare();
- const childSessions = async () =>
- (await sessionStore.listSessions({} as never)).filter((session) => session.id !== sessionId);
- const sessionsBeforeMissingHistory = await childSessions();
- sessionStore.messages = async () => [];
- await expect(
- internal.subagentPort.sendMessage({
- ...context,
- to: first.agentId,
- message: "Again",
- summary: "Next",
- parentToolCallId: "fourth",
- }),
- ).rejects.toThrow("No transcript found");
- expect(await childSessions()).toEqual(sessionsBeforeMissingHistory);
- });
- it("fails custom profiles instead of falling back when the configured model is invalid", async () => {
- const sessionId = createSessionId("runtime-subagent-invalid-model");
- const eventStore = createTestSessionEventStore();
- const parentRequests: any[] = [];
- const childRequests: any[] = [];
- let parentCallCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- modelSelection: {
- providerId: createModelProviderId("provider-main"),
- modelId: createModelId("main-model"),
- },
- workingDirectory: "/workspace/project",
- subagents: {
- profiles: [
- {
- name: "broken-reviewer",
- description: "Review code",
- source: "user",
- systemPrompt: "Review carefully.",
- modelSelection: { providerId: "missing-provider", modelId: "missing-model" },
- tools: ["Read"],
- },
- ],
- },
- },
- {
- eventStore,
- modelFactory: ((factory) => (input: Parameters<typeof factory>[0]) => {
- if (
- input.selection.providerId === "missing-provider" &&
- input.selection.modelId === "missing-model"
- ) {
- throw new Error("Invalid subagent model configuration");
- }
- return factory(input);
- })(
- createTestModelFactory({
- async generateText(request: any) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentRequests.push(request);
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_invalid_model_agent",
- name: "Agent",
- input: {
- description: "Review changed files",
- prompt: "Review changed files.",
- subagent_type: "broken-reviewer",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "should not run",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- ),
- },
- );
- const result = await runtime.executeTurn("Use broken reviewer");
- const toolBatchCompleteEvent = result.events.find(
- (event) => event.type === SessionEventType.ToolBatchComplete,
- );
- const toolMessage = parentRequests[1]?.messages.find((message: any) => message.role === "tool");
- expect(toolMessage).toMatchObject({
- content: "Invalid subagent model configuration",
- isError: true,
- toolName: "Agent",
- });
- expect(toolBatchCompleteEvent?.payload).toMatchObject({
- successCount: 0,
- errorCount: 1,
- });
- expect(childRequests).toHaveLength(0);
- });
- it("inherits parent model streaming for Explore child requests", async () => {
- const sessionId = createSessionId("runtime-explore-streaming-inheritance");
- const eventStore = createTestSessionEventStore();
- const streamRequests: any[] = [];
- const childStreamRequests: any[] = [];
- let parentCallCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- modelStreaming: "on",
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- properties: { supportsNativeWebSearch: true },
- async generateText() {
- throw new Error("generateText should not be used when modelStreaming is on");
- },
- async *streamText(request: any) {
- streamRequests.push(request);
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentCallCount++;
- if (parentCallCount === 1) {
- yield {
- type: "tool_call",
- toolCall: {
- id: "call_explore_stream",
- name: "Agent",
- input: {
- description: "Find runtime loop",
- prompt: "Find where the runtime injects tool results.",
- subagent_type: "Explore",
- },
- },
- };
- yield {
- type: "finish",
- finishReason: "tool-calls",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- return;
- }
- yield { type: "text_delta", text: "parent used streamed explore result" };
- yield {
- type: "finish",
- finishReason: "stop",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- return;
- }
- childStreamRequests.push(request);
- yield { type: "text_delta", text: "Explore streamed child result." };
- yield {
- type: "finish",
- finishReason: "stop",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- const result = await runtime.executeTurn("Use an explore agent with streaming");
- expect(result.response).toBe("parent used streamed explore result");
- expect(streamRequests).toHaveLength(3);
- expect(childStreamRequests).toHaveLength(1);
- expect(childStreamRequests[0].tools.map((tool: any) => tool.name)).toEqual([
- "Bash",
- "Read",
- "TodoWrite",
- "WebFetch",
- "WebSearch",
- RESPOND_TO_COORDINATOR_TOOL_NAME,
- ]);
- });
- it("uses the latest parent model config when spawning Explore", async () => {
- const sessionId = createSessionId("runtime-explore-model-update");
- const eventStore = createTestSessionEventStore();
- const childRequests: any[] = [];
- const childObservations: TestModelExecutionObservation[] = [];
- let parentCallCount = 0;
- const initialModelSelection = {
- providerId: createModelProviderId("provider-old"),
- modelId: createModelId("old-model"),
- };
- const updatedModelSelection = {
- providerId: createModelProviderId("provider-new"),
- modelId: createModelId("new-model"),
- };
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- mode: "build",
- modelSelection: createTestModelSelection(initialModelSelection),
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(request: any, observation) {
- const toolNames = (request.tools ?? []).map((tool: any) => tool.name);
- if (toolNames.includes("Agent")) {
- parentCallCount++;
- if (parentCallCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_explore_updated_model",
- name: "Agent",
- input: {
- description: "Check model",
- prompt: "Check the inherited model.",
- subagent_type: "Explore",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- childRequests.push(request);
- childObservations.push(observation);
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "child used updated model",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- runtime.setSessionModelSelection({
- providerId: String(updatedModelSelection.providerId),
- modelId: String(updatedModelSelection.modelId),
- });
- await runtime.executeTurn("Use an explore agent");
- expect(childRequests).toHaveLength(1);
- expect(childObservations[0].model).toMatchObject({
- providerId: updatedModelSelection.providerId,
- modelId: updatedModelSelection.modelId,
- });
- });
- it("does not register Agent when subagents are disabled", () => {
- const runtime = createTestAgentRuntime(
- createSessionId("runtime-explore-disabled"),
- {
- subagents: {
- enabled: false,
- },
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText() {
- throw new Error("model should not be called");
- },
- } as never),
- },
- );
- expect(runtime.getToolRegistry().has("Agent")).toBe(false);
- expect(runtime.getToolRegistry().has("Task")).toBe(false);
- });
- });
- function createDeferred<T>(): {
- promise: Promise<T>;
- reject: (reason?: unknown) => void;
- resolve: (value?: T | PromiseLike<T>) => void;
- } {
- let resolve!: (value?: T | PromiseLike<T>) => void;
- let reject!: (reason?: unknown) => void;
- const promise = new Promise<T>((innerResolve, innerReject) => {
- resolve = (value) => innerResolve(value as T | PromiseLike<T>);
- reject = innerReject;
- });
- return { promise, reject, resolve };
- }
- async function waitFor(
- predicate: () => boolean | Promise<boolean>,
- timeoutMs = 2_500,
- ): Promise<void> {
- const deadline = Date.now() + timeoutMs;
- while (Date.now() < deadline) {
- if (await predicate()) return;
- await new Promise((resolve) => setTimeout(resolve, 10));
- }
- throw new Error("Timed out waiting for condition");
- }
- function skillMetadata(name: string, path: string): SkillLoadOutcome["skills"][number] {
- return {
- description: `${name} description`,
- directory: path.slice(0, path.lastIndexOf("/")),
- frontmatterKeys: ["name", "description"],
- name,
- path,
- rootPath: "/skills",
- safeToAutoLoad: true,
- scope: "project",
- source: "zcode",
- };
- }
- function createMockMcpPort(overrides: Partial<McpPort> = {}): McpPort {
- return {
- async callTool() {
- return { content: [{ text: "ok", type: "text" }] };
- },
- async close() {},
- async connectConfiguredServers() {
- return { statuses: {}, tools: [] };
- },
- async connectServer() {
- return {
- status: "connected",
- toolCount: 0,
- transport: "stdio",
- updatedAt: new Date(0).toISOString(),
- };
- },
- async disconnectServer() {
- return undefined;
- },
- async listTools() {
- return [];
- },
- async status() {
- return {};
- },
- ...overrides,
- };
- }
|