| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970 |
- import {
- existsSync,
- mkdtempSync,
- readFileSync,
- realpathSync,
- rmSync,
- writeFileSync,
- } from "node:fs";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { DatabaseSync } from "node:sqlite";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- ZCODE_SESSION_RUNTIME_PREFERENCES_REQUEST_TIMEOUT_MS,
- type ZCodeSessionRuntimePreferencesScope,
- } from "@zcode/shared";
- import { createZCodeAgentService } from "../src/zcode-agent/zcodeAgentService.js";
- import { ZCodeAgentProcessManager } from "../src/zcode-agent/zcodeAgentProcessManager.js";
- import type { ZCodeProtocolClient } from "../src/zcode-agent/zcodeProtocolClient.js";
- import { ZCODE_AGENT_MCP_STATUS_MODE_UNSUPPORTED_ERROR_CODE } from "../src/zcode-agent/zcodeAgentErrors.js";
- import type { ZCodeAgentServiceEvent } from "../src/zcode-agent/zcodeAgent.js";
- import { getTasksIndexDatabasePath, setDataBaseDir } from "../src/paths.js";
- const protocolModelProperties = {
- inputFormat: {
- supportsText: true,
- supportsImage: false,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: false,
- },
- outputFormat: { supportsText: true },
- };
- const protocolModelPropertiesJson = JSON.stringify(protocolModelProperties);
- function createReadyZCodeAgentService(options?: Parameters<typeof createZCodeAgentService>[0]) {
- return createZCodeAgentService({
- modelSelectionReadinessSource: {
- async getView() {
- return {
- revision: 1,
- providers: [
- {
- providerId: "glm",
- config: {
- kind: "api" as const,
- api: {
- type: "openai-chat-completions" as const,
- baseUrl: "https://provider.test/v1",
- },
- models: ["glm-4.6"],
- },
- models: [{ modelId: "glm-4.6", config: {} }],
- },
- ],
- };
- },
- },
- ...options,
- });
- }
- // 一个最小的 ZCode Protocol stdio fake agent:
- // 第一次收到 session/subscribe 返回错误(模拟 "Session is not active" 之类的瞬时失败),
- // 第二次返回成功并带一条 session.titleUpdated 事件。用来验证订阅建立失败后会重试。
- const FLAKY_SUBSCRIBE_FAKE_AGENT = `
- let buf = "";
- let subscribeCount = 0;
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "session/subscribe" && msg.id !== undefined) {
- subscribeCount += 1;
- if (subscribeCount === 1) {
- send({ id: msg.id, error: { code: -32000, message: "Session is not active" } });
- } else {
- send({
- id: msg.id,
- result: {
- sessionId: msg.params.sessionId,
- eventSeq: 1,
- events: [{
- type: "session.titleUpdated",
- eventId: "evt-1",
- sessionId: msg.params.sessionId,
- seq: 1,
- timestamp: 1700000000000,
- payload: { previousTitle: "", source: "default", title: "from-retry" },
- }],
- },
- });
- }
- }
- }
- });
- `;
- const SUBSCRIBE_REPLAY_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "session/subscribe" && msg.id !== undefined) {
- send({
- id: msg.id,
- result: {
- sessionId: msg.params.sessionId,
- eventSeq: 1,
- events: [{
- type: "session.titleUpdated",
- eventId: "evt-replay-title",
- sessionId: msg.params.sessionId,
- seq: 1,
- timestamp: 1700000000000,
- payload: { previousTitle: "", source: "generated", title: "replay-title" },
- }],
- },
- });
- }
- }
- });
- `;
- const CUA_OPERATION_LIVE_EVENT_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- function operationEvent(kind, eventId, sequenceNumber, sessionId, turnId, extra = {}) {
- return {
- kind,
- eventId,
- sequenceNumber,
- sessionId,
- timestamp: Date.now(),
- ...(turnId ? { turnId } : {}),
- ...extra,
- };
- }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method !== "session/subscribe" || msg.id === undefined) continue;
- const sessionId = msg.params.sessionId;
- send({ id: msg.id, result: { sessionId, eventSeq: 0, events: [] } });
- const turnId = "turn-cua-live";
- const started = operationEvent("tool-started", "evt-tool-started", 3, sessionId, turnId, {
- toolCallId: "call-cua-live",
- });
- setTimeout(() => {
- send({ method: "computer-use/operation-event", params: operationEvent(
- "turn-started", "evt-turn-started", 1, sessionId, turnId,
- ) });
- send({ method: "computer-use/operation-event", params: operationEvent(
- "tool-started", "evt-invalid", 2, sessionId, turnId, {
- toolCallId: "",
- }) });
- send({ method: "computer-use/operation-event", params: operationEvent(
- "tool-scheduled", "evt-tool-scheduled", 2, sessionId, turnId, {
- toolCallId: "call-cua-live",
- toolName: "mcp__node_repl__js",
- computerUse: true,
- }) });
- send({ method: "computer-use/operation-event", params: started });
- send({ method: "computer-use/operation-event", params: started });
- send({ method: "computer-use/operation-event", params: operationEvent(
- "turn-completed", "evt-turn-completed", 4, sessionId, turnId,
- ) });
- }, 0);
- }
- });
- `;
- const CUA_OPERATION_RUNTIME_EXIT_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- function operationEvent(kind, eventId, sequenceNumber, sessionId, turnId, extra = {}) {
- return {
- kind,
- eventId,
- sequenceNumber,
- sessionId,
- timestamp: Date.now(),
- ...(turnId ? { turnId } : {}),
- ...extra,
- };
- }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method !== "session/subscribe" || msg.id === undefined) continue;
- const sessionId = msg.params.sessionId;
- send({ id: msg.id, result: { sessionId, eventSeq: 0, events: [] } });
- setTimeout(() => {
- const turnId = "turn-cua-runtime-exit";
- send({ method: "computer-use/operation-event", params: operationEvent(
- "turn-started", "evt-runtime-exit-turn", 1, sessionId, turnId,
- ) });
- send({ method: "computer-use/operation-event", params: operationEvent(
- "tool-scheduled", "evt-runtime-exit-scheduled", 2, sessionId, turnId, {
- toolCallId: "call-runtime-exit",
- toolName: "mcp__node_repl__js",
- computerUse: true,
- }) });
- send({ method: "computer-use/operation-event", params: operationEvent(
- "tool-started", "evt-runtime-exit-tool", 3, sessionId, turnId, {
- toolCallId: "call-runtime-exit",
- }) });
- setTimeout(() => process.exit(1), 10);
- }, 0);
- }
- });
- `;
- const BROWSER_DISCOVERY_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- function context(sessionId) {
- return {
- requestId: "browser-request-context",
- sessionId,
- turnId: "turn-browser-1",
- workspaceKey: "remote:ssh:dev:/workspace/app",
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- remoteSessionId: "remote-session-1",
- clientMode: "web-remote-replayable",
- sessionContext: "live",
- };
- }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "session/subscribe" && msg.id !== undefined) {
- send({
- id: msg.id,
- result: { sessionId: msg.params.sessionId, eventSeq: 0, events: [] },
- });
- send({
- id: "browser-list-1",
- method: "interaction/browserList",
- params: context(msg.params.sessionId),
- });
- continue;
- }
- if (msg.id === "browser-list-1" && msg.result?.browsers?.[0]?.id) {
- send({
- id: "browser-execute-1",
- method: "interaction/browserExecute",
- params: {
- ...context("sess-browser"),
- requestId: "browser-execute-request",
- browserId: msg.result.browsers[0].id,
- browserGeneration: msg.result.browsers[0].generation,
- command: { method: "getState" },
- },
- });
- }
- }
- });
- `;
- const SHELL_SETTINGS_SNAPSHOT_FAKE_AGENT = `
- const fs = require("node:fs");
- const logPath = process.argv[2];
- const rejectBrowserAmbient = process.argv[3] === "reject-browser-ambient";
- let buf = "";
- let pendingCreate;
- let pendingSend;
- let userExecutionResolved = false;
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- function record(msg) {
- fs.appendFileSync(logPath, JSON.stringify({ method: msg.method, params: msg.params }) + "\\n");
- }
- function rejectBrowserAmbientParams(id) {
- const issues = [{
- code: "unrecognized_keys",
- keys: ["browserAmbientContext"],
- path: [],
- message: 'Unrecognized key: "browserAmbientContext"',
- }];
- send({
- id,
- error: {
- code: -32602,
- message: 'Invalid params — (root): Unrecognized key: "browserAmbientContext"',
- data: { name: "ZodError", message: JSON.stringify(issues) },
- },
- });
- }
- function workspaceState(workspace) {
- return {
- workspace,
- settings: {
- model: {
- current: { providerId: "zai-api", modelId: "glm-4.6" },
- available: [{ ref: { providerId: "zai-api", modelId: "glm-4.6" }, label: "GLM 4.6", properties: ${protocolModelPropertiesJson} }],
- },
- thoughtLevel: { enabled: false, available: [] },
- mode: { current: "build" },
- },
- };
- }
- function snapshot(workspace, sessionId = "sess_shell_settings") {
- return {
- protocol: { name: "ZCode Protocol", version: 1 },
- session: {
- sessionId,
- workspace,
- sessionKind: "interactive",
- title: "Shell settings",
- mode: "build",
- status: "idle",
- createdAt: 1,
- updatedAt: 2,
- },
- settings: workspaceState(workspace).settings,
- projection: {
- sessionId,
- status: "idle",
- mode: "build",
- turnCount: 1,
- totalTokenCount: 0,
- contextUsed: 0,
- contextWindow: 128000,
- pendingPermissions: [],
- activeToolCalls: [],
- backgroundJobs: [],
- },
- runtime: { eventSeq: 0, stateRevision: 1, pendingRequestIds: [] },
- messages: [],
- };
- }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "session/create" && msg.id !== undefined) {
- record(msg);
- pendingCreate = msg;
- send({
- id: "runtime-preferences-materialization",
- method: "session/requestRuntimePreferences",
- params: {
- sessionId: msg.params.sessionId ?? "sess_shell_settings",
- scope: "runtime-materialization",
- },
- });
- continue;
- }
- if (msg.method === "session/resume" && msg.id !== undefined) {
- record(msg);
- send({ id: msg.id, result: snapshot(msg.params.workspace, msg.params.sessionId) });
- continue;
- }
- if (msg.method === "session/send" && msg.id !== undefined) {
- record(msg);
- if (rejectBrowserAmbient && msg.params.browserAmbientContext !== undefined) {
- rejectBrowserAmbientParams(msg.id);
- continue;
- }
- if (!userExecutionResolved) {
- pendingSend = msg;
- send({
- id: "runtime-preferences-user-execution",
- method: "session/requestRuntimePreferences",
- params: {
- sessionId: msg.params.sessionId,
- scope: "user-execution",
- },
- });
- continue;
- }
- send({
- id: msg.id,
- result: {
- sessionId: msg.params.sessionId,
- accepted: true,
- stateRevision: 2,
- },
- });
- continue;
- }
- if (msg.id === "runtime-preferences-materialization" && pendingCreate) {
- const create = pendingCreate;
- pendingCreate = undefined;
- send({ id: create.id, result: snapshot(create.params.workspace, create.params.sessionId) });
- continue;
- }
- if (msg.id === "runtime-preferences-user-execution" && pendingSend) {
- const prompt = pendingSend;
- pendingSend = undefined;
- userExecutionResolved = true;
- send({
- id: prompt.id,
- result: {
- sessionId: prompt.params.sessionId,
- accepted: true,
- stateRevision: 2,
- },
- });
- }
- }
- });
- `;
- const RUNTIME_PREFERENCES_FAKE_AGENT = `
- const fs = require("node:fs");
- const logPath = process.argv[2];
- const requestMode = process.argv[3] ?? "valid";
- let buf = "";
- let pendingCreate;
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- function record(obj) { fs.appendFileSync(logPath, JSON.stringify(obj) + "\\n"); }
- function workspaceState(workspace) {
- return {
- workspace,
- settings: {
- model: {
- current: { providerId: "zai-api", modelId: "glm-4.6" },
- available: [{ ref: { providerId: "zai-api", modelId: "glm-4.6" }, label: "GLM 4.6", properties: ${protocolModelPropertiesJson} }],
- },
- thoughtLevel: { enabled: false, available: [] },
- mode: { current: "build" },
- },
- };
- }
- function snapshot(workspace) {
- const sessionId = "sess_runtime_preferences";
- return {
- protocol: { name: "ZCode Protocol", version: 1 },
- session: {
- sessionId,
- workspace,
- sessionKind: "interactive",
- title: "Runtime preferences",
- mode: "build",
- status: "idle",
- createdAt: 1,
- updatedAt: 2,
- },
- settings: workspaceState(workspace).settings,
- projection: {
- sessionId,
- status: "idle",
- mode: "build",
- turnCount: 0,
- totalTokenCount: 0,
- contextUsed: 0,
- contextWindow: 128000,
- pendingPermissions: [],
- activeToolCalls: [],
- backgroundJobs: [],
- },
- runtime: { eventSeq: 0, stateRevision: 1, pendingRequestIds: [] },
- messages: [],
- };
- }
- function completeCreateRequest() {
- const msg = pendingCreate;
- pendingCreate = undefined;
- record({ method: msg.method, params: msg.params });
- send({ id: msg.id, result: snapshot(msg.params.workspace) });
- }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "session/create" && msg.id !== undefined) {
- // Worker 进程已经自持 Registry,runtime preferences 在 session 创建时物化。
- pendingCreate = msg;
- send({
- id: "runtime-preferences-1",
- method: "session/requestRuntimePreferences",
- params: requestMode === "invalid"
- ? {}
- : { sessionId: "sess_runtime_preferences", scope: "runtime-materialization" },
- });
- if (requestMode === "disconnect") {
- process.stdin.destroy();
- process.stdout.end();
- }
- continue;
- }
- if (msg.id === "runtime-preferences-1" && (msg.result !== undefined || msg.error)) {
- record({
- method: "session/requestRuntimePreferences:response",
- result: msg.result,
- error: msg.error,
- });
- completeCreateRequest();
- }
- }
- });
- `;
- const DELAYED_COMPACT_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- function createSnapshot(sessionId) {
- return {
- protocol: { name: "ZCode Protocol", version: 1 },
- session: {
- sessionId,
- workspace: {
- workspacePath: "/workspace/compact",
- workspaceKey: "/workspace/compact",
- },
- sessionKind: "interactive",
- title: "Compact",
- mode: "build",
- status: "idle",
- createdAt: 1,
- updatedAt: 1,
- },
- settings: {
- model: {
- current: { providerId: "glm", modelId: "glm-4.6" },
- available: [{ ref: { providerId: "glm", modelId: "glm-4.6" }, label: "GLM 4.6", properties: ${protocolModelPropertiesJson} }],
- },
- thoughtLevel: { enabled: true, available: [] },
- mode: { current: "build" },
- },
- projection: {
- sessionId,
- status: "idle",
- mode: "build",
- turnCount: 0,
- totalTokenCount: 0,
- contextUsed: 0,
- contextWindow: 128000,
- pendingPermissions: [],
- activeToolCalls: [],
- backgroundJobs: [],
- },
- runtime: { eventSeq: 0, stateRevision: 1, pendingRequestIds: [] },
- messages: [],
- };
- }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "session/compact" && msg.id !== undefined) {
- setTimeout(() => {
- send({
- id: msg.id,
- result: {
- response: "",
- snapshot: createSnapshot(msg.params.sessionId),
- compact: {
- state: "accepted",
- inputId: msg.params.inputId,
- },
- },
- });
- }, 80);
- }
- }
- });
- `;
- const LEGACY_CREATE_PARAMS_FAKE_AGENT = `
- let buf = "";
- let currentWorkspace = null;
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- function createSnapshot(workspace, thoughtLevel) {
- return {
- protocol: { name: "ZCode Protocol", version: 1 },
- session: {
- sessionId: "sess_compat",
- workspace,
- sessionKind: "interactive",
- title: "Compat",
- mode: "build",
- status: "idle",
- createdAt: 1,
- updatedAt: 1,
- },
- settings: {
- model: {
- current: { providerId: "glm", modelId: "glm-4.6" },
- available: [{ ref: { providerId: "glm", modelId: "glm-4.6" }, label: "GLM 4.6", properties: ${protocolModelPropertiesJson} }],
- },
- thoughtLevel: {
- enabled: true,
- current: thoughtLevel,
- available: [{ value: "max", label: "Max" }],
- },
- mode: { current: "build" },
- },
- projection: {
- sessionId: "sess_compat",
- status: "idle",
- mode: "build",
- turnCount: 0,
- totalTokenCount: 0,
- contextUsed: 0,
- contextWindow: 128000,
- pendingPermissions: [],
- activeToolCalls: [],
- backgroundJobs: [],
- },
- runtime: { eventSeq: 0, stateRevision: thoughtLevel ? 2 : 1, pendingRequestIds: [] },
- messages: [],
- };
- }
- function invalidParams(id, keys) {
- send({
- id,
- error: {
- code: -32602,
- message: "Invalid params",
- data: {
- name: "ZodError",
- message: JSON.stringify([
- {
- code: "unrecognized_keys",
- keys,
- path: [],
- message: "Unrecognized key",
- },
- ]),
- },
- },
- });
- }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "session/create" && msg.id !== undefined) {
- const rejectedKeys = [];
- if (Object.prototype.hasOwnProperty.call(msg.params, "persistence")) {
- rejectedKeys.push("persistence");
- }
- if (Object.prototype.hasOwnProperty.call(msg.params, "thoughtLevel")) {
- rejectedKeys.push("thoughtLevel");
- }
- if (Object.prototype.hasOwnProperty.call(msg.params, "mcpServers")) {
- rejectedKeys.push("mcpServers");
- }
- if (rejectedKeys.length > 0) {
- invalidParams(msg.id, rejectedKeys);
- } else {
- currentWorkspace = msg.params.workspace;
- send({ id: msg.id, result: createSnapshot(msg.params.workspace, undefined) });
- }
- continue;
- }
- if (msg.method === "session/setThoughtLevel" && msg.id !== undefined) {
- send({ id: msg.id, result: createSnapshot(currentWorkspace, msg.params.thoughtLevel) });
- }
- }
- });
- `;
- const AUTOMATION_BINDING_CHECK_FAKE_AGENT = `
- const fs = require("node:fs");
- const logPath = process.argv[2];
- let buf = "";
- let requested = false;
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- setTimeout(() => {
- if (requested) return;
- requested = true;
- send({
- id: "binding-check-1",
- method: "automation/checkTaskBinding",
- params: { targetTaskId: "session-1" },
- });
- }, 25);
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.id === "binding-check-1" && (msg.result || msg.error)) {
- fs.writeFileSync(logPath, JSON.stringify(msg));
- }
- }
- });
- `;
- const STALE_THEN_PLUGIN_LIST_FAKE_AGENT = `
- const fs = require("node:fs");
- const markerPath = process.argv[2];
- const shouldHang = !fs.existsSync(markerPath);
- if (shouldHang) fs.writeFileSync(markerPath, "hung-once");
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "plugins/list" && msg.id !== undefined) {
- if (shouldHang) continue;
- send({
- id: msg.id,
- result: {
- plugins: [{
- id: "skill-creator@zcode-plugins-official",
- name: "skill-creator",
- enabled: true,
- source: "official",
- marketplace: "zcode-plugins-official",
- skillCount: 1,
- skillRootCount: 1,
- commandRootCount: 0,
- mcpServerNames: [],
- rootPath: "/cache/skill-creator",
- }],
- diagnostics: [],
- },
- });
- }
- }
- });
- `;
- const STALE_THEN_MCP_LIST_FAKE_AGENT = `
- const fs = require("node:fs");
- const markerPath = process.argv[2];
- const shouldHang = !fs.existsSync(markerPath);
- if (shouldHang) fs.writeFileSync(markerPath, "hung-once");
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "mcp/list" && msg.id !== undefined) {
- if (shouldHang) continue;
- send({
- id: msg.id,
- result: {
- statuses: {
- filesystem: {
- status: "disconnected",
- transport: "stdio",
- toolCount: 0,
- updatedAt: "2026-06-23T00:00:00.000Z",
- },
- },
- },
- });
- }
- }
- });
- `;
- const MCP_LIST_ECHO_CWD_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "mcp/list" && msg.id !== undefined) {
- send({
- id: msg.id,
- result: {
- statuses: {
- cwd: {
- status: "disconnected",
- transport: "stdio",
- toolCount: 0,
- updatedAt: "2026-06-23T00:00:00.000Z",
- error: msg.params.workspace.workspacePath,
- },
- },
- },
- });
- }
- }
- });
- `;
- const MCP_LIST_ECHO_MCP_SERVERS_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "mcp/list" && msg.id !== undefined) {
- send({
- id: msg.id,
- result: {
- statuses: {
- explicit: {
- status: "disconnected",
- transport: "stdio",
- toolCount: 0,
- updatedAt: "2026-06-23T00:00:00.000Z",
- error: JSON.stringify(msg.params.mcpServers ?? null),
- },
- },
- },
- });
- }
- }
- });
- `;
- const MCP_LIST_ECHO_MODE_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "mcp/list" && msg.id !== undefined) {
- send({
- id: msg.id,
- result: {
- statuses: {
- mode: {
- status: "disconnected",
- transport: "stdio",
- toolCount: 0,
- updatedAt: "2026-06-23T00:00:00.000Z",
- error: JSON.stringify(msg.params.mode ?? null),
- },
- },
- },
- });
- }
- }
- });
- `;
- const MCP_LIST_REJECTS_MCP_SERVERS_ONCE_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "mcp/list" && msg.id !== undefined) {
- if (msg.params.mcpServers !== undefined) {
- send({
- id: msg.id,
- error: {
- code: -32602,
- message: "Invalid params",
- data: {
- message: JSON.stringify([{ code: "unrecognized_keys", keys: ["mcpServers"], path: [] }]),
- },
- },
- });
- continue;
- }
- send({
- id: msg.id,
- result: {
- statuses: {
- fallback: {
- status: "disconnected",
- transport: "stdio",
- toolCount: 0,
- updatedAt: "2026-06-23T00:00:00.000Z",
- error: JSON.stringify(msg.params.mcpServers ?? null),
- },
- },
- },
- });
- }
- }
- });
- `;
- const MCP_LIST_REJECTS_MODE_ONCE_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "mcp/list" && msg.id !== undefined) {
- if (msg.params.mode !== undefined) {
- send({
- id: msg.id,
- error: {
- code: -32602,
- message: "Invalid params",
- data: {
- message: JSON.stringify([{ code: "unrecognized_keys", keys: ["mode"], path: [] }]),
- },
- },
- });
- continue;
- }
- send({
- id: msg.id,
- result: {
- statuses: {
- fallback: {
- status: "disconnected",
- transport: "stdio",
- toolCount: 0,
- updatedAt: "2026-06-23T00:00:00.000Z",
- },
- },
- },
- });
- }
- }
- });
- `;
- // 模拟真实 CLI transport 的串行队列:同一进程内 mcp/list 一直不返回,
- // 排在它后面的所有请求都拿不到响应(Bug:卸载插件被 30s 的 MCP 探测卡死)。
- const SERIAL_QUEUE_HANG_ON_MCP_LIST_FAKE_AGENT = `
- const fs = require("node:fs");
- const markerPath = process.argv[2];
- let buf = "";
- let blocked = false;
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.id === undefined || !msg.method) continue;
- if (blocked) continue;
- if (msg.method === "mcp/list") {
- blocked = true;
- fs.writeFileSync(markerPath, String(process.pid));
- continue;
- }
- if (msg.method === "plugins/uninstall") {
- send({ id: msg.id, result: { diagnostics: [] } });
- }
- }
- });
- `;
- const PLUGIN_LIST_ECHO_CWD_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "plugins/list" && msg.id !== undefined) {
- send({
- id: msg.id,
- result: {
- plugins: [{
- id: "cwd-check@zcode-plugins-official",
- name: "cwd-check",
- enabled: true,
- source: "official",
- marketplace: "zcode-plugins-official",
- skillCount: 0,
- skillRootCount: 0,
- commandRootCount: 0,
- mcpServerNames: [],
- rootPath: process.cwd(),
- }],
- diagnostics: [{
- code: "workspace-path",
- message: msg.params.workspace.workspacePath,
- }],
- },
- });
- }
- }
- });
- `;
- const PLUGIN_UPDATE_ECHO_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "plugins/update" && msg.id !== undefined) {
- send({
- id: msg.id,
- result: {
- installedPlugins: [],
- dependencyClosure: [],
- diagnostics: [{
- code: "echo-plugin-id",
- message: String(msg.params.pluginId),
- }],
- },
- });
- }
- }
- });
- `;
- const PLUGIN_RESTORE_BUILTIN_ECHO_FAKE_AGENT = `
- let buf = "";
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "plugins/restoreBuiltin" && msg.id !== undefined) {
- send({
- id: msg.id,
- result: {
- pluginId: msg.params.pluginId,
- diagnostics: [],
- },
- });
- }
- }
- });
- `;
- const INTERACTION_PREFERENCES_FAKE_AGENT = `
- const fs = require("node:fs");
- const logPath = process.argv[2];
- const failFirst = process.argv[3] === "fail-first";
- const delayFirst = process.argv[3] === "delay-first";
- let buf = "";
- let updateCount = 0;
- function send(obj) { process.stdout.write(JSON.stringify(obj) + "\\n"); }
- process.stdin.on("data", (chunk) => {
- buf += chunk.toString("utf8");
- let idx;
- while ((idx = buf.indexOf("\\n")) >= 0) {
- const line = buf.slice(0, idx);
- buf = buf.slice(idx + 1);
- if (!line.trim()) continue;
- let msg;
- try { msg = JSON.parse(line); } catch { continue; }
- if (msg.method === "workspace/updateModelIoPreferences" && msg.id !== undefined) {
- fs.appendFileSync(logPath + ".model-io", JSON.stringify({
- method: msg.method,
- params: msg.params,
- }) + "\\n");
- send({
- id: msg.id,
- result: {
- workspace: msg.params.workspace,
- fullRetentionEnabled: msg.params.preferences.fullRetentionEnabled,
- updatedSessionCount: 0,
- },
- });
- continue;
- }
- if (msg.method !== "workspace/updateInteractionPreferences" || msg.id === undefined) {
- continue;
- }
- updateCount += 1;
- fs.appendFileSync(logPath, JSON.stringify({
- method: msg.method,
- params: msg.params,
- updateCount,
- }) + "\\n");
- if (failFirst && updateCount === 1) {
- send({ id: msg.id, error: { code: -32000, message: "transient sync failure" } });
- continue;
- }
- const response = {
- id: msg.id,
- result: {
- workspace: msg.params.workspace,
- askUserQuestionAutoResolutionEnabled:
- msg.params.preferences.askUserQuestionAutoResolutionEnabled,
- snoozedInteractionCount: 0,
- },
- };
- if (delayFirst && updateCount === 1) {
- setTimeout(() => send(response), 100);
- } else {
- send(response);
- }
- }
- });
- `;
- describe("createZCodeAgentService", () => {
- it("资源查询仅观察就绪的现存 runtime,透传取消且不启动或等待迁移", async () => {
- const request = vi.fn(async () => ({ processes: [] }));
- const migratingRequest = vi.fn();
- const enumeration = vi
- .spyOn(ZCodeAgentProcessManager.prototype, "listManagedProcesses")
- .mockReturnValue([])
- .mockReturnValueOnce([
- {
- pid: 101,
- workspacePath: "/ready",
- client: {
- request,
- isDisposed: false,
- storageStartup: { isWaiting: false },
- } as unknown as ZCodeProtocolClient,
- },
- {
- pid: 102,
- workspacePath: "/migrating",
- client: {
- request: migratingRequest,
- isDisposed: false,
- storageStartup: { isWaiting: true },
- } as unknown as ZCodeProtocolClient,
- },
- ]);
- const commandResolver = vi.fn(() => null);
- const service = createZCodeAgentService({ commandResolver });
- const controller = new AbortController();
- try {
- await expect(service.collectLocalRuntimeChildProcesses(controller.signal)).resolves.toEqual([
- { pid: 101, workspacePath: "/ready", provider: "glm", children: [] },
- ]);
- expect(request).toHaveBeenCalledWith("process/childProcesses", {}, expect.anything(), {
- lifecycle: "observation",
- timeoutMs: 800,
- signal: controller.signal,
- });
- expect(migratingRequest).not.toHaveBeenCalled();
- expect(commandResolver).not.toHaveBeenCalled();
- } finally {
- enumeration.mockRestore();
- await service.disposeAllAndWait();
- }
- });
- it("does not resolve or spawn a command while provider/model is unavailable", async () => {
- const commandResolver = vi.fn(() => ({
- command: process.execPath,
- args: ["-e", "setInterval(() => {}, 1000);"],
- }));
- const processLifecycleReporter = {
- onSpawn: vi.fn(),
- onExit: vi.fn(),
- onError: vi.fn(),
- };
- const service = createZCodeAgentService({
- commandResolver,
- processLifecycleReporter,
- modelSelectionReadinessSource: {
- async getView() {
- return { revision: 1, providers: [] };
- },
- },
- });
- await expect(
- service.initialize({ workspacePath: "/workspace/provider-waiting" }),
- ).resolves.toMatchObject({
- available: false,
- reasonCode: "provider_not_ready",
- });
- expect(commandResolver).not.toHaveBeenCalled();
- expect(processLifecycleReporter.onSpawn).not.toHaveBeenCalled();
- expect(processLifecycleReporter.onError).not.toHaveBeenCalled();
- await service.disposeAllAndWait();
- });
- const createdDirs: string[] = [];
- afterEach(() => {
- setDataBaseDir(null);
- for (const dir of createdDirs.splice(0).reverse()) {
- // 根因同 zcodeAgentService.v4.test.ts:Windows 上仍有打开句柄的目录删不掉,
- // `force: true` 不重试,afterEach 会以 EPERM 把断言已通过的用例判成失败。
- try {
- rmSync(dir, {
- recursive: true,
- force: true,
- maxRetries: 20,
- retryDelay: 100,
- });
- } catch {
- /* 清理失败不影响断言结论,交给操作系统回收临时目录。 */
- }
- }
- });
- it("caches interaction preferences without spawning and applies ordered snapshots to active/new runtimes", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-interaction-preferences-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "interaction-preferences-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, INTERACTION_PREFERENCES_FAKE_AGENT);
- const commandResolver = vi.fn(() => ({
- command: process.execPath,
- args: [scriptPath, logPath],
- }));
- const service = createReadyZCodeAgentService({ commandResolver });
- try {
- await service.syncAppRuntimePreferences({
- askUserQuestionAutoResolutionEnabled: false,
- modelIoFullRetentionEnabled: false,
- });
- expect(commandResolver).not.toHaveBeenCalled();
- await expect(service.initialize({ workspacePath })).resolves.toMatchObject({
- available: true,
- });
- await Promise.all([
- service.syncAppRuntimePreferences({
- askUserQuestionAutoResolutionEnabled: true,
- modelIoFullRetentionEnabled: true,
- }),
- service.syncAppRuntimePreferences({
- askUserQuestionAutoResolutionEnabled: false,
- modelIoFullRetentionEnabled: false,
- }),
- ]);
- const updates = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map(
- (line) =>
- JSON.parse(line) as {
- params: {
- preferences: {
- askUserQuestionAutoResolutionEnabled: boolean;
- };
- workspace: {
- workspaceKey: string;
- workspacePath: string;
- };
- };
- },
- );
- expect(
- updates.map((entry) => entry.params.preferences.askUserQuestionAutoResolutionEnabled),
- ).toEqual([false, true, false]);
- expect(updates[0]?.params.workspace).toMatchObject({
- workspaceKey: workspacePath,
- workspacePath,
- });
- const modelIoUpdates = readFileSync(`${logPath}.model-io`, "utf8")
- .trim()
- .split("\n")
- .map(
- (line) =>
- JSON.parse(line) as {
- params: { preferences: { fullRetentionEnabled: boolean } };
- },
- );
- expect(modelIoUpdates.map((entry) => entry.params.preferences.fullRetentionEnabled)).toEqual([
- false,
- true,
- false,
- ]);
- expect(commandResolver).toHaveBeenCalledTimes(1);
- await service.disposeWorkspace({ workspacePath });
- await expect(service.initialize({ workspacePath })).resolves.toMatchObject({
- available: true,
- });
- const restartedUpdates = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line) as { params: { preferences: object } });
- expect(restartedUpdates).toHaveLength(4);
- expect(restartedUpdates[3]?.params.preferences).toEqual({
- askUserQuestionAutoResolutionEnabled: false,
- });
- expect(commandResolver).toHaveBeenCalledTimes(2);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("retries cached interaction preferences when a new runtime sync fails", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-interaction-retry-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "interaction-preferences-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, INTERACTION_PREFERENCES_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({
- command: process.execPath,
- args: [scriptPath, logPath, "fail-first"],
- }),
- });
- try {
- await service.syncAppRuntimePreferences({
- askUserQuestionAutoResolutionEnabled: false,
- });
- await expect(service.initialize({ workspacePath })).resolves.toMatchObject({
- available: false,
- });
- await expect(service.initialize({ workspacePath })).resolves.toMatchObject({
- available: true,
- });
- const updates = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line) as { updateCount: number });
- expect(updates.map((entry) => entry.updateCount)).toEqual([1, 2]);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("does not lose a rapid preference toggle while a new runtime is applying its cache", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-interaction-startup-race-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "interaction-preferences-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, INTERACTION_PREFERENCES_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({
- command: process.execPath,
- args: [scriptPath, logPath, "delay-first"],
- }),
- });
- try {
- await service.syncAppRuntimePreferences({
- askUserQuestionAutoResolutionEnabled: false,
- });
- const initializing = service.initialize({ workspacePath });
- await vi.waitFor(() => expect(existsSync(logPath)).toBe(true));
- const enabling = service.syncAppRuntimePreferences({
- askUserQuestionAutoResolutionEnabled: true,
- });
- await expect(initializing).resolves.toMatchObject({ available: true });
- await expect(enabling).resolves.toBeUndefined();
- const updates = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map(
- (line) =>
- JSON.parse(line) as {
- params: {
- preferences: {
- askUserQuestionAutoResolutionEnabled: boolean;
- };
- };
- },
- );
- expect(updates[0]?.params.preferences.askUserQuestionAutoResolutionEnabled).toBe(false);
- expect(updates.at(-1)?.params.preferences.askUserQuestionAutoResolutionEnabled).toBe(true);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("dispatcher 缺失时 Run Now 不创建台账或占用 manual claim", async () => {
- const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-agent-service-run-now-"));
- createdDirs.push(sandboxRoot);
- setDataBaseDir(sandboxRoot);
- const workspacePath = "/workspace/run-now";
- const serviceWithoutDispatcher = createZCodeAgentService();
- const automation = await serviceWithoutDispatcher.createAutomation({
- workspacePath,
- title: "daily check",
- cronExpr: "0 9 * * *",
- prompt: "check logs",
- recurring: true,
- });
- await expect(
- serviceWithoutDispatcher.runAutomationNow({
- workspacePath,
- automationId: automation.automationId,
- }),
- ).rejects.toThrow("Automation immediate dispatcher is unavailable.");
- await expect(
- serviceWithoutDispatcher.listAutomationRuns({
- workspacePath,
- automationId: automation.automationId,
- }),
- ).resolves.toEqual([]);
- const dispatch = vi.fn(async () => undefined);
- const serviceWithDispatcher = createZCodeAgentService({
- onAutomationManualRunRequested: dispatch,
- });
- await expect(
- serviceWithDispatcher.runAutomationNow({
- workspacePath,
- automationId: automation.automationId,
- }),
- ).resolves.toEqual({ status: "queued" });
- expect(dispatch).toHaveBeenCalledTimes(1);
- });
- it("automation/checkTaskBinding 不依赖完整列表字段,历史空 mode 仍可完成归属判断", async () => {
- const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-agent-binding-check-"));
- createdDirs.push(sandboxRoot);
- setDataBaseDir(sandboxRoot);
- const workspacePath = sandboxRoot;
- const scriptPath = join(sandboxRoot, "binding-check-agent.cjs");
- const logPath = join(sandboxRoot, "binding-check-response.json");
- writeFileSync(scriptPath, AUTOMATION_BINDING_CHECK_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath, logPath] }),
- });
- const automation = await service.createAutomation({
- workspacePath,
- title: "legacy task",
- cronExpr: "0 9 * * *",
- prompt: "summarize",
- mode: "build",
- recurring: true,
- });
- const db = new DatabaseSync(getTasksIndexDatabasePath());
- db.prepare(
- "UPDATE automations SET mode = '', target_task_id = 'session-1' WHERE automation_id = ?",
- ).run(automation.automationId);
- db.close();
- try {
- await expect(service.initialize({ workspacePath })).resolves.toMatchObject({
- available: true,
- });
- await vi.waitFor(() => expect(existsSync(logPath)).toBe(true), { timeout: 5000 });
- expect(JSON.parse(readFileSync(logPath, "utf8"))).toEqual({
- id: "binding-check-1",
- result: { bound: true },
- });
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("reports unavailable without starting a process when no command is configured", async () => {
- const service = createReadyZCodeAgentService({ commandResolver: () => null });
- await expect(
- service.initialize({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- }),
- ).resolves.toMatchObject({
- available: false,
- workspaceKey: "remote:ssh:dev:/workspace/app",
- protocolName: "ZCode Protocol",
- protocolVersion: 1,
- });
- });
- it("starts stdio app-server during initialize", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-"));
- createdDirs.push(workspacePath);
- const commandResolver = vi.fn(() => ({
- command: process.execPath,
- args: ["-e", "setInterval(() => {}, 1000);"],
- }));
- const processLifecycleReporter = {
- onSpawn: vi.fn(),
- onReady: vi.fn(),
- onExit: vi.fn(),
- onError: vi.fn(),
- };
- const service = createReadyZCodeAgentService({ commandResolver, processLifecycleReporter });
- await expect(
- service.initialize({
- workspacePath,
- }),
- ).resolves.toMatchObject({
- available: true,
- workspaceKey: workspacePath,
- protocolName: "ZCode Protocol",
- protocolVersion: 1,
- transportKind: "stdio",
- });
- expect(commandResolver).toHaveBeenCalledTimes(1);
- await vi.waitFor(() => expect(processLifecycleReporter.onReady).toHaveBeenCalledTimes(1));
- expect(processLifecycleReporter.onReady.mock.calls[0]![0]).toMatchObject({
- pid: processLifecycleReporter.onSpawn.mock.calls[0]![0].pid,
- provider: "glm",
- workspacePath,
- runtimeGeneration: 1,
- runtimeInstanceId: processLifecycleReporter.onSpawn.mock.calls[0]![0].runtimeInstanceId,
- readyAt: expect.any(Number),
- startupDurationMs: expect.any(Number),
- });
- await expect(
- service.initialize({
- workspacePath,
- }),
- ).resolves.toMatchObject({ available: true });
- expect(processLifecycleReporter.onReady).toHaveBeenCalledTimes(1);
- await service.disposeAllAndWait();
- await vi.waitFor(() => expect(processLifecycleReporter.onExit).toHaveBeenCalledTimes(1));
- expect(processLifecycleReporter.onExit.mock.calls[0]![0]).toMatchObject({
- runtimeReady: true,
- runtimeInstanceId: processLifecycleReporter.onReady.mock.calls[0]![0].runtimeInstanceId,
- });
- });
- it("routes browser discovery and browserId-aware execute with full remote context", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-browser-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "browser-agent.cjs");
- writeFileSync(scriptPath, BROWSER_DISCOVERY_FAKE_AGENT);
- const list = vi.fn(async () => [
- {
- id: "iab-runtime-1",
- generation: 1,
- type: "iab" as const,
- name: "ZCode In-app Browser",
- capabilities: {},
- },
- ]);
- const execute = vi.fn(async () => ({
- ok: true,
- state: {
- url: "https://example.com",
- title: "Example",
- canGoBack: false,
- canGoForward: false,
- },
- elapsedMs: 1,
- }));
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- browserControlExecutor: { list, execute },
- });
- const subscription = service.onDynamicSessionEvent({
- workspacePath,
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "sess-browser",
- deliveryKind: "web-remote-replayable",
- includeSnapshot: false,
- })(() => {});
- await vi.waitFor(() => expect(execute).toHaveBeenCalledTimes(1), {
- timeout: 5000,
- interval: 25,
- });
- expect(list).toHaveBeenCalledWith(
- expect.objectContaining({
- workspaceKey: "remote:ssh:dev:/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- remoteSessionId: "remote-session-1",
- clientMode: "web-remote-replayable",
- }),
- );
- expect(execute).toHaveBeenCalledWith(
- expect.objectContaining({
- requestId: "browser-execute-request",
- browserId: "iab-runtime-1",
- browserGeneration: 1,
- sessionId: "sess-browser",
- turnId: "turn-browser-1",
- workspaceKey: "remote:ssh:dev:/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- remoteSessionId: "remote-session-1",
- clientMode: "web-remote-replayable",
- command: { method: "getState" },
- }),
- );
- subscription.dispose();
- await service.disposeAllAndWait();
- });
- it("passes session tool lists through protocol session/create", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-tool-list-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "tool-list-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, SHELL_SETTINGS_SNAPSHOT_FAKE_AGENT);
- // 修复原因:本用例只验证 session/create 的工具列表透传,不验证 provider readiness。
- // 门禁启用后必须复用标准 ready registry fixture,否则测试会在协议请求前被正确拦截。
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath, logPath] }),
- });
- try {
- await service.createSession({
- workspacePath,
- toolAllowlist: ["mcp__zcode-cua__request_access", "mcp__zcode-cua__list_apps"],
- toolDenylist: ["Bash"],
- });
- const createRequest = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line) as { method: string; params: Record<string, unknown> })
- .find((entry) => entry.method === "session/create");
- expect(createRequest?.params.toolAllowlist).toEqual([
- "mcp__zcode-cua__request_access",
- "mcp__zcode-cua__list_apps",
- ]);
- expect(createRequest?.params.toolDenylist).toEqual(["Bash"]);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("resolves runtime preferences at materialization and first execution without forwarding them", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-shell-snapshot-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "shell-settings-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, SHELL_SETTINGS_SNAPSHOT_FAKE_AGENT);
- const resolveSessionRuntimePreferences = vi.fn(
- async (scope: ZCodeSessionRuntimePreferencesScope) => ({
- askUserQuestionAutoResolutionEnabled: false,
- nativeSearchEnhancementsEnabled: false,
- memoryEnabled: false,
- modelContextBudgetStrategy:
- scope === "runtime-materialization" ? ("preflight-v1" as const) : ("legacy" as const),
- ...(scope === "user-execution"
- ? {
- integratedTerminalShell: {
- mode: "shell" as const,
- dialect: "git-bash" as const,
- id: "git-bash:/usr/bin/new-session-bash",
- label: "Git Bash",
- path: "/usr/bin/new-session-bash",
- },
- }
- : {}),
- }),
- );
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath, logPath] }),
- resolveSessionRuntimePreferences,
- });
- const created = await service.createSession({ workspacePath });
- expect(resolveSessionRuntimePreferences).toHaveBeenCalledTimes(1);
- expect(resolveSessionRuntimePreferences).toHaveBeenNthCalledWith(1, "runtime-materialization");
- await service.resumeSession({
- workspacePath,
- sessionId: created.session.sessionId,
- });
- expect(resolveSessionRuntimePreferences).toHaveBeenCalledTimes(1);
- await service.sendPrompt({
- workspacePath,
- sessionId: created.session.sessionId,
- content: "continue",
- });
- expect(resolveSessionRuntimePreferences).toHaveBeenCalledTimes(2);
- expect(resolveSessionRuntimePreferences).toHaveBeenNthCalledWith(2, "user-execution");
- await service.sendPrompt({
- workspacePath,
- sessionId: created.session.sessionId,
- content: "second prompt",
- modelSelection: {
- providerId: "test-provider",
- modelId: "test-model",
- options: { reasoningLevel: "high" },
- },
- modelExecution: { selectionScope: "execution", memoryExtraction: "skip" },
- });
- const requests = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line) as { method: string; params: Record<string, unknown> });
- expect(resolveSessionRuntimePreferences).toHaveBeenCalledTimes(2);
- expect(requests).toMatchObject([
- { method: "session/create" },
- { method: "session/resume" },
- { method: "session/send" },
- {
- method: "session/send",
- params: {
- modelSelection: {
- providerId: "test-provider",
- modelId: "test-model",
- options: { reasoningLevel: "high" },
- },
- modelExecution: { selectionScope: "execution", memoryExtraction: "skip" },
- },
- },
- ]);
- for (const request of requests) {
- expect(request.params).not.toHaveProperty("integratedTerminalShell");
- expect(request.params).not.toHaveProperty("nativeSearchEnhancementsEnabled");
- expect(request.params).not.toHaveProperty("memoryEnabled");
- expect(request.params).not.toHaveProperty("modelContextBudgetStrategy");
- }
- await service.disposeAllAndWait();
- });
- it("adds bounded IAB ambient context to session/send without changing prompt content", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-browser-ambient-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "browser-ambient-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, SHELL_SETTINGS_SNAPSHOT_FAKE_AGENT);
- const list = vi.fn(async () => [
- {
- id: "iab-runtime-1",
- generation: 3,
- type: "iab" as const,
- name: "ZCode In-app Browser",
- capabilities: {},
- },
- ]);
- const execute = vi.fn(async (input: { command: { method: string } }) =>
- input.command.method === "list"
- ? { ok: true, tabs: [], elapsedMs: 0 }
- : {
- ok: true,
- userTabs: [
- {
- id: "user-tab-1",
- title: "Current",
- url: "https://example.com/current?q=1",
- },
- ],
- elapsedMs: 0,
- },
- );
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({
- command: process.execPath,
- args: [scriptPath, logPath],
- }),
- browserControlExecutor: { list, execute } as never,
- });
- const workspaceIdentity = "remote:ssh:dev:/workspace/app";
- const created = await service.createSession({
- workspacePath,
- workspaceIdentity,
- });
- await service.sendPrompt({
- workspacePath,
- workspaceIdentity,
- remoteSessionId: "remote-session-1",
- sessionId: created.session.sessionId,
- clientMode: "web-remote-replayable",
- content: "continue",
- });
- const sendRequest = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map(
- (line) =>
- JSON.parse(line) as {
- method: string;
- params: Record<string, unknown>;
- },
- )
- .find((request) => request.method === "session/send");
- expect(sendRequest?.params).toMatchObject({
- content: "continue",
- browserAmbientContext: {
- tabCount: 1,
- currentUrl: "https://example.com/current?q=1",
- },
- });
- expect(list).toHaveBeenCalledWith(
- expect.objectContaining({
- workspaceKey: workspaceIdentity,
- remoteSessionId: "remote-session-1",
- clientMode: "web-remote-replayable",
- }),
- );
- expect(execute).toHaveBeenCalledTimes(2);
- await service.disposeAllAndWait();
- });
- it("retries session/send without browser ambient context when a legacy agent returns an enhanced invalid params error", async () => {
- const workspacePath = mkdtempSync(
- join(tmpdir(), "zcode-agent-service-browser-ambient-compat-"),
- );
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "browser-ambient-compat-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, SHELL_SETTINGS_SNAPSHOT_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({
- command: process.execPath,
- args: [scriptPath, logPath, "reject-browser-ambient"],
- }),
- browserControlExecutor: {
- list: vi.fn(async () => [
- {
- id: "iab-runtime-legacy",
- generation: 1,
- type: "iab" as const,
- name: "ZCode In-app Browser",
- capabilities: {},
- },
- ]),
- execute: vi.fn(async (input: { command: { method: string } }) =>
- input.command.method === "list"
- ? { ok: true, tabs: [], elapsedMs: 0 }
- : {
- ok: true,
- userTabs: [
- {
- id: "user-tab-legacy",
- title: "Current",
- url: "https://example.com/current",
- },
- ],
- elapsedMs: 0,
- },
- ),
- } as never,
- });
- const created = await service.createSession({ workspacePath });
- await expect(
- service.sendPrompt({
- workspacePath,
- sessionId: created.session.sessionId,
- content: "continue",
- }),
- ).resolves.toMatchObject({ accepted: true });
- const sendRequests = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map(
- (line) =>
- JSON.parse(line) as {
- method: string;
- params: Record<string, unknown>;
- },
- )
- .filter((request) => request.method === "session/send");
- expect(sendRequests).toHaveLength(2);
- expect(sendRequests[0]?.params).toHaveProperty("browserAmbientContext");
- expect(sendRequests[1]?.params).not.toHaveProperty("browserAmbientContext");
- expect(sendRequests[1]?.params.content).toBe("continue");
- await service.disposeAllAndWait();
- });
- it("resolves execution preferences before a deferred draft accepts its first prompt", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-shell-draft-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "shell-settings-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, SHELL_SETTINGS_SNAPSHOT_FAKE_AGENT);
- const resolveSessionRuntimePreferences = vi.fn(async () => ({
- askUserQuestionAutoResolutionEnabled: true,
- nativeSearchEnhancementsEnabled: true,
- memoryEnabled: true,
- integratedTerminalShell: {
- mode: "shell" as const,
- dialect: "git-bash" as const,
- id: "git-bash:/usr/bin/first-send-bash",
- label: "Git Bash",
- path: "/usr/bin/first-send-bash",
- },
- }));
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath, logPath] }),
- resolveSessionRuntimePreferences,
- });
- const draft = await service.createSession({ workspacePath, persistence: "deferred" });
- expect(resolveSessionRuntimePreferences).toHaveBeenCalledTimes(1);
- await service.sendPrompt({
- workspacePath,
- sessionId: draft.session.sessionId,
- content: "first prompt",
- });
- const requests = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line) as { method: string; params: Record<string, unknown> });
- expect(resolveSessionRuntimePreferences).toHaveBeenCalledTimes(2);
- expect(requests).toMatchObject([
- {
- method: "session/create",
- params: {
- persistence: "deferred",
- },
- },
- {
- method: "session/send",
- },
- ]);
- for (const request of requests) {
- expect(request.params).not.toHaveProperty("integratedTerminalShell");
- expect(request.params).not.toHaveProperty("nativeSearchEnhancementsEnabled");
- expect(request.params).not.toHaveProperty("memoryEnabled");
- }
- await service.disposeAllAndWait();
- });
- it("answers Agent runtime preference requests from the Host resolver", async () => {
- const cases: Array<{
- name: string;
- requestMode?: "invalid" | "valid";
- resolver?: ReturnType<
- typeof vi.fn<
- () => Promise<{
- askUserQuestionAutoResolutionEnabled: boolean;
- nativeSearchEnhancementsEnabled: boolean;
- memoryEnabled: boolean;
- }>
- >
- >;
- expectedResult?: {
- askUserQuestionAutoResolutionEnabled: boolean;
- nativeSearchEnhancementsEnabled: boolean;
- subagentRuntimeConfigEnabled: boolean;
- memoryEnabled: boolean;
- modelContextBudgetStrategy: "legacy" | "preflight-v1";
- };
- expectedErrorCode?: number;
- expectedResolverCalls: number;
- }> = [
- {
- name: "current-setting",
- resolver: vi.fn(async () => ({
- askUserQuestionAutoResolutionEnabled: false,
- nativeSearchEnhancementsEnabled: false,
- memoryEnabled: false,
- })),
- expectedResult: {
- askUserQuestionAutoResolutionEnabled: false,
- nativeSearchEnhancementsEnabled: false,
- subagentRuntimeConfigEnabled: false,
- memoryEnabled: false,
- modelContextBudgetStrategy: "preflight-v1" as const,
- },
- expectedResolverCalls: 1,
- },
- {
- name: "missing-resolver",
- expectedResult: {
- askUserQuestionAutoResolutionEnabled: true,
- nativeSearchEnhancementsEnabled: true,
- subagentRuntimeConfigEnabled: false,
- memoryEnabled: false,
- modelContextBudgetStrategy: "preflight-v1" as const,
- },
- expectedResolverCalls: 0,
- },
- {
- name: "resolver-failure",
- resolver: vi.fn(async () => {
- throw new Error("setting failed");
- }),
- expectedErrorCode: -32603,
- expectedResolverCalls: 1,
- },
- {
- name: "invalid-params",
- requestMode: "invalid",
- resolver: vi.fn(async () => ({
- askUserQuestionAutoResolutionEnabled: false,
- nativeSearchEnhancementsEnabled: false,
- memoryEnabled: false,
- })),
- expectedErrorCode: -32602,
- expectedResolverCalls: 0,
- },
- ];
- for (const testCase of cases) {
- const workspacePath = mkdtempSync(
- join(tmpdir(), `zcode-agent-runtime-preferences-${testCase.name}-`),
- );
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "runtime-preferences-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, RUNTIME_PREFERENCES_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({
- command: process.execPath,
- args: [scriptPath, logPath, testCase.requestMode ?? "valid"],
- }),
- resolveSessionRuntimePreferences: testCase.resolver,
- });
- try {
- await service.createSession({ workspacePath });
- const response = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map(
- (line) =>
- JSON.parse(line) as {
- error?: { code: number; message: string };
- method: string;
- result?: {
- askUserQuestionAutoResolutionEnabled: boolean;
- nativeSearchEnhancementsEnabled: boolean;
- subagentRuntimeConfigEnabled: boolean;
- memoryEnabled: boolean;
- modelContextBudgetStrategy: "legacy" | "preflight-v1";
- };
- },
- )
- .find((entry) => entry.method === "session/requestRuntimePreferences:response");
- expect(response, testCase.name).toBeDefined();
- if (testCase.expectedResult) {
- expect(response?.result, testCase.name).toEqual(testCase.expectedResult);
- expect(response?.error, testCase.name).toBeUndefined();
- } else {
- expect(response?.error, testCase.name).toMatchObject({
- code: testCase.expectedErrorCode,
- });
- }
- if (testCase.resolver) {
- expect(testCase.resolver, testCase.name).toHaveBeenCalledTimes(
- testCase.expectedResolverCalls,
- );
- }
- } finally {
- await service.disposeAllAndWait();
- }
- }
- });
- it("does not send a second runtime preference response after the Agent disconnects", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-runtime-preferences-close-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "runtime-preferences-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, RUNTIME_PREFERENCES_FAKE_AGENT);
- let markResolverStarted!: () => void;
- let releaseResolver!: () => void;
- let markResolverFinished!: () => void;
- const resolverStarted = new Promise<void>((resolve) => {
- markResolverStarted = resolve;
- });
- const resolverReleased = new Promise<void>((resolve) => {
- releaseResolver = resolve;
- });
- const resolverFinished = new Promise<void>((resolve) => {
- markResolverFinished = resolve;
- });
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({
- command: process.execPath,
- args: [scriptPath, logPath, "disconnect"],
- }),
- resolveSessionRuntimePreferences: async () => {
- markResolverStarted();
- await resolverReleased;
- markResolverFinished();
- return {
- askUserQuestionAutoResolutionEnabled: false,
- nativeSearchEnhancementsEnabled: false,
- memoryEnabled: false,
- };
- },
- });
- try {
- const createResult = service.createSession({ workspacePath }).then(
- () => null,
- (error: unknown) => error,
- );
- await resolverStarted;
- expect(await createResult).toBeInstanceOf(Error);
- releaseResolver();
- await resolverFinished;
- await new Promise<void>((resolve) => setImmediate(resolve));
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("routes external runtime preference requests by opaque requestId", async () => {
- const fixtureDir = mkdtempSync(join(tmpdir(), "zcode-agent-external-preferences-"));
- const firstWorkspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-preferences-first-"));
- const secondWorkspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-preferences-second-"));
- createdDirs.push(fixtureDir, firstWorkspacePath, secondWorkspacePath);
- const scriptPath = join(fixtureDir, "runtime-preferences-agent.cjs");
- const logPath = join(fixtureDir, "protocol.log");
- writeFileSync(scriptPath, RUNTIME_PREFERENCES_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath, logPath] }),
- sessionRuntimePreferencesAuthority: "external",
- });
- const requests: Array<{ requestId: string; sessionId: string; scope: string }> = [];
- const subscription = service.onDynamicSessionRuntimePreferencesRequest()((request) => {
- requests.push(request);
- void service.respondSessionRuntimePreferences({
- requestId: request.requestId,
- resolution: {
- status: "resolved",
- preferences: {
- askUserQuestionAutoResolutionEnabled: false,
- nativeSearchEnhancementsEnabled: false,
- memoryEnabled: false,
- },
- },
- });
- });
- try {
- await service.createSession({ workspacePath: firstWorkspacePath });
- await service.createSession({ workspacePath: secondWorkspacePath });
- expect(requests).toHaveLength(2);
- expect(new Set(requests.map((request) => request.requestId)).size).toBe(2);
- for (const request of requests) {
- expect(request).toMatchObject({
- sessionId: "sess_runtime_preferences",
- scope: "runtime-materialization",
- });
- expect(request).not.toHaveProperty("workspacePath");
- expect(request).not.toHaveProperty("workspaceIdentity");
- }
- } finally {
- subscription.dispose();
- await service.disposeAllAndWait();
- }
- });
- it("expires unanswered external runtime preference requests without replaying them", async () => {
- vi.useFakeTimers({ toFake: ["clearTimeout", "setTimeout"] });
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-preferences-timeout-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "runtime-preferences-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- writeFileSync(scriptPath, RUNTIME_PREFERENCES_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath, logPath] }),
- sessionRuntimePreferencesAuthority: "external",
- });
- let markRequestSeen!: () => void;
- const requestSeen = new Promise<void>((resolve) => {
- markRequestSeen = resolve;
- });
- const subscription = service.onDynamicSessionRuntimePreferencesRequest()(() => {
- markRequestSeen();
- });
- try {
- const createPromise = service.createSession({ workspacePath });
- await requestSeen;
- await vi.advanceTimersByTimeAsync(ZCODE_SESSION_RUNTIME_PREFERENCES_REQUEST_TIMEOUT_MS);
- await createPromise;
- const response = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map(
- (line) =>
- JSON.parse(line) as {
- error?: { code: number; data?: { timeoutMs?: number } };
- method: string;
- },
- )
- .find((entry) => entry.method === "session/requestRuntimePreferences:response");
- expect(response?.error).toEqual({
- code: -32022,
- data: {
- timeoutMs: ZCODE_SESSION_RUNTIME_PREFERENCES_REQUEST_TIMEOUT_MS,
- },
- message: "Session runtime preferences request timed out",
- });
- subscription.dispose();
- const replayedRequests: unknown[] = [];
- const replaySubscription = service.onDynamicSessionRuntimePreferencesRequest()((request) => {
- replayedRequests.push(request);
- });
- expect(replayedRequests).toEqual([]);
- replaySubscription.dispose();
- } finally {
- subscription.dispose();
- vi.useRealTimers();
- await service.disposeAllAndWait();
- }
- });
- it("uses the compact-specific request timeout for delayed compact ACKs", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-compact-timeout-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "delayed-compact-agent.cjs");
- writeFileSync(scriptPath, DELAYED_COMPACT_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- requestTimeoutMs: 20,
- });
- try {
- const result = await service.compactSession({
- workspacePath,
- sessionId: "sess_compact_timeout",
- inputId: "input_compact_timeout",
- });
- expect(result.compact).toMatchObject({
- state: "accepted",
- inputId: "input_compact_timeout",
- });
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("retries session subscription after a transient subscribe failure", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-retry-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "flaky-agent.cjs");
- writeFileSync(scriptPath, FLAKY_SUBSCRIBE_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- });
- const received: ZCodeAgentServiceEvent[] = [];
- const subscription = service.onDynamicSessionEvent({
- workspacePath,
- sessionId: "sess-1",
- deliveryKind: "desktop-continuous",
- includeSnapshot: false,
- })((event) => received.push(event));
- // 第一次 subscribe 失败后必须重试,最终把成功响应里的事件投递出来。
- await vi.waitFor(
- () => {
- expect(received.some((event) => event.type === "session.event")).toBe(true);
- },
- { timeout: 5000, interval: 50 },
- );
- subscription.dispose();
- await service.disposeAllAndWait();
- });
- it("delivers subscribe replay events only to the subscriber that requested them", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-replay-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "replay-agent.cjs");
- writeFileSync(scriptPath, SUBSCRIBE_REPLAY_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- });
- const firstReceived: ZCodeAgentServiceEvent[] = [];
- const firstSubscription = service.onDynamicSessionEvent({
- workspacePath,
- sessionId: "sess-1",
- deliveryKind: "desktop-continuous",
- includeSnapshot: false,
- })((event) => firstReceived.push(event));
- await vi.waitFor(
- () => {
- expect(firstReceived.filter((event) => event.type === "session.event")).toHaveLength(1);
- },
- { timeout: 5000, interval: 50 },
- );
- const secondReceived: ZCodeAgentServiceEvent[] = [];
- const secondSubscription = service.onDynamicSessionEvent({
- workspacePath,
- sessionId: "sess-1",
- deliveryKind: "desktop-continuous",
- includeSnapshot: false,
- })((event) => secondReceived.push(event));
- await vi.waitFor(
- () => {
- expect(secondReceived.filter((event) => event.type === "session.event")).toHaveLength(1);
- },
- { timeout: 5000, interval: 50 },
- );
- // Bugfix: 第二个订阅者自己的 replay 缺口不能广播回第一个订阅者。
- expect(firstReceived.filter((event) => event.type === "session.event")).toHaveLength(1);
- firstSubscription.dispose();
- secondSubscription.dispose();
- await service.disposeAllAndWait();
- });
- it("reports only validated de-duplicated CUA sideband events without legacy session delivery", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-cua-operation-live-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "cua-operation-live-agent.cjs");
- writeFileSync(scriptPath, CUA_OPERATION_LIVE_EVENT_FAKE_AGENT);
- const reportedStates: Array<{ active: boolean; sessionId: string; turnId: string }> = [];
- const observedServiceActiveStates: boolean[] = [];
- let service: ReturnType<typeof createReadyZCodeAgentService>;
- const onStateChanged = vi.fn(
- (event: { active: boolean; sessionId: string; turnId: string }) => {
- reportedStates.push(event);
- observedServiceActiveStates.push(service.hasActiveCuaOperationTurn());
- if (event.active) throw new Error("host port closed");
- },
- );
- service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- cuaOperationStateReporter: { onStateChanged },
- });
- expect(service.hasActiveCuaOperationTurn()).toBe(false);
- const received: ZCodeAgentServiceEvent[] = [];
- const subscription = service.onDynamicSessionEvent({
- workspacePath,
- sessionId: "sess-cua-live",
- deliveryKind: "desktop-continuous",
- includeSnapshot: false,
- })((event) => received.push(event));
- try {
- await vi.waitFor(
- () => {
- expect(reportedStates.map((event) => event.active)).toEqual([true, false]);
- },
- { timeout: 5_000, interval: 50 },
- );
- const liveEvents = received
- .filter(
- (event): event is Extract<ZCodeAgentServiceEvent, { type: "session.event" }> =>
- event.type === "session.event",
- )
- .map((event) => event.event);
- expect(liveEvents).toEqual([]);
- expect(onStateChanged).toHaveBeenCalledTimes(2);
- expect(observedServiceActiveStates).toEqual([true, false]);
- } finally {
- subscription.dispose();
- await service.disposeAllAndWait();
- }
- });
- it("clears CUA state when the runtime becomes unavailable without a restart", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-cua-runtime-exit-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "cua-operation-runtime-exit-agent.cjs");
- writeFileSync(scriptPath, CUA_OPERATION_RUNTIME_EXIT_FAKE_AGENT);
- const reportedStates: boolean[] = [];
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- cuaOperationStateReporter: {
- onStateChanged: (event) => reportedStates.push(event.active),
- },
- });
- const subscription = service.onDynamicSessionEvent({
- workspacePath,
- sessionId: "sess-cua-runtime-exit",
- deliveryKind: "desktop-continuous",
- includeSnapshot: false,
- })(() => undefined);
- try {
- await vi.waitFor(
- () => {
- expect(reportedStates).toEqual([true, false]);
- expect(service.hasActiveCuaOperationTurn()).toBe(false);
- },
- { timeout: 5_000, interval: 50 },
- );
- } finally {
- subscription.dispose();
- await service.disposeAllAndWait();
- }
- });
- it("retries createSession without optional fields rejected by legacy agents", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-compat-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "legacy-create-agent.cjs");
- writeFileSync(scriptPath, LEGACY_CREATE_PARAMS_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- });
- try {
- const snapshot = await service.createSession({
- workspacePath,
- model: { providerId: "glm", modelId: "glm-4.6" },
- persistence: "deferred",
- thoughtLevel: "max",
- mcpServers: [
- {
- name: "chrome-devtools",
- command: "npx",
- args: ["-y", "chrome-devtools-mcp@latest"],
- env: [],
- },
- ],
- });
- expect(snapshot.session.sessionId).toBe("sess_compat");
- expect(snapshot.settings.thoughtLevel.current).toBe("max");
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("restarts a stale protocol client and retries plugin list without provider registry sync", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-plugins-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "stale-plugin-agent.cjs");
- const markerPath = join(workspacePath, "first-run-marker");
- writeFileSync(scriptPath, STALE_THEN_PLUGIN_LIST_FAKE_AGENT);
- const commandResolver = vi.fn(() => ({
- command: process.execPath,
- args: [scriptPath, markerPath],
- }));
- const service = createReadyZCodeAgentService({
- commandResolver,
- // Bugfix: 该用例只需要首个 fake agent 超时,full suite 并发时第二个进程启动到响应可能超过 500ms。
- // 局部放宽 request timeout,避免把重启后成功恢复的路径误判成第二次 stale。
- requestTimeoutMs: 1_500,
- });
- try {
- const result = await service.listPlugins({ workspacePath });
- expect(result.plugins.map((plugin) => plugin.name)).toEqual(["skill-creator"]);
- expect(commandResolver).toHaveBeenCalledTimes(2);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("loads plugin list from the dedicated plugin workspace when the real workspace is missing", async () => {
- const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-agent-service-plugin-cwd-"));
- createdDirs.push(sandboxRoot);
- setDataBaseDir(sandboxRoot);
- const scriptPath = join(sandboxRoot, "plugin-cwd-agent.cjs");
- writeFileSync(scriptPath, PLUGIN_LIST_ECHO_CWD_FAKE_AGENT);
- const missingWorkspacePath = join(sandboxRoot, "deleted-workspace");
- const pluginWorkspacePath = join(sandboxRoot, ".zcode", "plugin-workspace");
- const commandResolver = vi.fn(({ workspacePath }) => ({
- command: process.execPath,
- args: [scriptPath],
- cwd: workspacePath,
- }));
- const service = createReadyZCodeAgentService({ commandResolver });
- try {
- const result = await service.listPlugins({ workspacePath: missingWorkspacePath });
- expect(existsSync(pluginWorkspacePath)).toBe(true);
- expect(realpathSync(result.plugins[0]!.rootPath)).toBe(realpathSync(pluginWorkspacePath));
- expect(result.diagnostics[0]?.message).toBe(missingWorkspacePath);
- expect(commandResolver).toHaveBeenCalledWith({
- workspacePath: pluginWorkspacePath,
- workspaceKey: pluginWorkspacePath,
- });
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("updatePlugin issues plugins/update with the requested pluginId", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-plugin-update-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "plugin-update-agent.cjs");
- writeFileSync(scriptPath, PLUGIN_UPDATE_ECHO_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- });
- try {
- const result = await service.updatePlugin({
- workspacePath,
- pluginId: "skill-creator@zcode-plugins-official",
- });
- expect(result.installedPlugins).toEqual([]);
- expect(result.diagnostics[0]?.code).toBe("echo-plugin-id");
- expect(result.diagnostics[0]?.message).toBe("skill-creator@zcode-plugins-official");
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("restoreBuiltinPlugin issues plugins/restoreBuiltin with the requested pluginId", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-plugin-restore-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "plugin-restore-agent.cjs");
- writeFileSync(scriptPath, PLUGIN_RESTORE_BUILTIN_ECHO_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- });
- try {
- const result = await service.restoreBuiltinPlugin({
- workspacePath,
- pluginId: "skill-creator@zcode-plugins-official",
- });
- expect(result.pluginId).toBe("skill-creator@zcode-plugins-official");
- expect(result.diagnostics).toEqual([]);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("restarts a stale protocol client and retries MCP status list without provider registry sync", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-mcp-list-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "stale-mcp-list-agent.cjs");
- const markerPath = join(workspacePath, "first-run-marker");
- writeFileSync(scriptPath, STALE_THEN_MCP_LIST_FAKE_AGENT);
- const commandResolver = vi.fn(() => ({
- command: process.execPath,
- args: [scriptPath, markerPath],
- }));
- const service = createReadyZCodeAgentService({
- commandResolver,
- requestTimeoutMs: 1_500,
- });
- try {
- const result = await service.listMcpServerStatuses({ workspacePath });
- expect(result.statuses.filesystem?.status).toBe("disconnected");
- expect(commandResolver).toHaveBeenCalledTimes(2);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("loads MCP status list through the dedicated plugin workspace", async () => {
- const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-agent-service-mcp-cwd-"));
- createdDirs.push(sandboxRoot);
- setDataBaseDir(sandboxRoot);
- const scriptPath = join(sandboxRoot, "mcp-cwd-agent.cjs");
- writeFileSync(scriptPath, MCP_LIST_ECHO_CWD_FAKE_AGENT);
- const missingWorkspacePath = join(sandboxRoot, "deleted-workspace");
- const pluginWorkspacePath = join(sandboxRoot, ".zcode", "plugin-workspace");
- const commandResolver = vi.fn(({ workspacePath }) => ({
- command: process.execPath,
- args: [scriptPath],
- cwd: workspacePath,
- }));
- const service = createReadyZCodeAgentService({ commandResolver });
- try {
- const result = await service.listMcpServerStatuses({ workspacePath: missingWorkspacePath });
- expect(existsSync(pluginWorkspacePath)).toBe(true);
- expect(result.statuses.cwd?.error).toBe(missingWorkspacePath);
- expect(commandResolver).toHaveBeenCalledWith({
- workspacePath: pluginWorkspacePath,
- workspaceKey: pluginWorkspacePath,
- });
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("plugins/uninstall 不会排在挂起的 mcp/list 后面:MCP 状态探测使用独立 agent 进程", async () => {
- const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-agent-service-mcp-lane-"));
- createdDirs.push(sandboxRoot);
- setDataBaseDir(sandboxRoot);
- const scriptPath = join(sandboxRoot, "serial-queue-agent.cjs");
- const markerPath = join(sandboxRoot, "mcp-list-received");
- writeFileSync(scriptPath, SERIAL_QUEUE_HANG_ON_MCP_LIST_FAKE_AGENT);
- const commandResolver = vi.fn(() => ({
- command: process.execPath,
- args: [scriptPath, markerPath],
- }));
- const service = createReadyZCodeAgentService({ commandResolver });
- try {
- // connect 模式的 mcp/list 在真实环境里会因坏 MCP server 吃满 30s 超时;这里让它永不返回。
- const mcpListPromise = service.listMcpServerStatuses({ workspacePath: sandboxRoot });
- mcpListPromise.catch(() => undefined);
- await vi.waitFor(() => expect(existsSync(markerPath)).toBe(true), { timeout: 5_000 });
- const result = await Promise.race([
- service.uninstallPlugin({ workspacePath: sandboxRoot, pluginId: "pick-funds" }),
- new Promise<never>((_, reject) =>
- setTimeout(() => reject(new Error("plugins/uninstall 被 mcp/list 阻塞")), 3_000),
- ),
- ]);
- expect(result.diagnostics).toEqual([]);
- // 两类请求必须落在不同的 agent 进程上
- expect(commandResolver).toHaveBeenCalledTimes(2);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("mcp-status lane 进程的 spawn/exit 会上报 processLifecycleReporter", async () => {
- const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-agent-service-mcp-lane-report-"));
- createdDirs.push(sandboxRoot);
- setDataBaseDir(sandboxRoot);
- const scriptPath = join(sandboxRoot, "serial-queue-agent.cjs");
- const markerPath = join(sandboxRoot, "mcp-list-received");
- writeFileSync(scriptPath, SERIAL_QUEUE_HANG_ON_MCP_LIST_FAKE_AGENT);
- const processLifecycleReporter = {
- onSpawn: vi.fn(),
- onReady: vi.fn(),
- onExit: vi.fn(),
- onError: vi.fn(),
- };
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath, markerPath] }),
- processLifecycleReporter,
- });
- try {
- // 只走 mcp-status lane:该进程是本 lane 新增的长驻 agent,进程监控器必须能看到它。
- const mcpListPromise = service.listMcpServerStatuses({ workspacePath: sandboxRoot });
- mcpListPromise.catch(() => undefined);
- await vi.waitFor(() => expect(existsSync(markerPath)).toBe(true), { timeout: 5_000 });
- await vi.waitFor(() => expect(processLifecycleReporter.onSpawn).toHaveBeenCalledTimes(1));
- // macOS tmpdir 会被 realpath 成 /private/var,这里不比对 workspacePath 字面值。
- // 评审 SG-02:mcp-status 与 plugin lane 共用同一 workspacePath/command,事件必须带 lane 才可归因。
- expect(processLifecycleReporter.onSpawn.mock.calls[0]![0]).toMatchObject({
- pid: expect.any(Number),
- provider: "glm",
- lane: "mcp-status",
- });
- } finally {
- await service.disposeAllAndWait();
- }
- await vi.waitFor(() => expect(processLifecycleReporter.onExit).toHaveBeenCalledTimes(1));
- expect(processLifecycleReporter.onExit.mock.calls[0]![0]).toMatchObject({ lane: "mcp-status" });
- });
- it("mcp-status lane 空闲超时后回收进程并释放 MCP 连接,下次探测重新拉起", async () => {
- const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-agent-service-mcp-lane-idle-"));
- createdDirs.push(sandboxRoot);
- setDataBaseDir(sandboxRoot);
- const scriptPath = join(sandboxRoot, "mcp-echo-agent.cjs");
- writeFileSync(scriptPath, MCP_LIST_ECHO_MODE_FAKE_AGENT);
- const commandResolver = vi.fn(() => ({ command: process.execPath, args: [scriptPath] }));
- const processLifecycleReporter = {
- onSpawn: vi.fn(),
- onReady: vi.fn(),
- onExit: vi.fn(),
- onError: vi.fn(),
- };
- const service = createReadyZCodeAgentService({
- commandResolver,
- processLifecycleReporter,
- mcpStatusIdleTimeoutMs: 300,
- });
- try {
- await service.listMcpServerStatuses({ workspacePath: sandboxRoot });
- expect(commandResolver).toHaveBeenCalledTimes(1);
- // 探测完成后无请求在飞,超过空闲阈值必须主动回收,且以 expected/idle-timeout 归因。
- await vi.waitFor(() => expect(processLifecycleReporter.onExit).toHaveBeenCalledTimes(1), {
- timeout: 5_000,
- });
- expect(processLifecycleReporter.onExit.mock.calls[0]![0]).toMatchObject({
- lane: "mcp-status",
- terminationKind: "expected",
- terminationReason: "idle-timeout",
- });
- // 回收后再次探测要透明地重新拉起进程,而不是拿到已回收的 client 报错。
- await service.listMcpServerStatuses({ workspacePath: sandboxRoot });
- expect(commandResolver).toHaveBeenCalledTimes(2);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("mcp/list 超时触发 watchdog 回收后重试仍落在 mcp-status lane,plugin lane 进程不受影响", async () => {
- const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-agent-service-mcp-lane-timeout-"));
- createdDirs.push(sandboxRoot);
- setDataBaseDir(sandboxRoot);
- const scriptPath = join(sandboxRoot, "serial-queue-agent.cjs");
- const markerPath = join(sandboxRoot, "mcp-list-received");
- writeFileSync(scriptPath, SERIAL_QUEUE_HANG_ON_MCP_LIST_FAKE_AGENT);
- const commandResolver = vi.fn(() => ({
- command: process.execPath,
- args: [scriptPath, markerPath],
- }));
- const processLifecycleReporter = {
- onSpawn: vi.fn(),
- onReady: vi.fn(),
- onExit: vi.fn(),
- onError: vi.fn(),
- };
- const service = createReadyZCodeAgentService({
- commandResolver,
- processLifecycleReporter,
- // 覆盖 fake agent 冷启动(node 进程拉起 + 首个响应),又足够短让两次超时在秒级完成。
- requestTimeoutMs: 1_500,
- });
- try {
- // 先让 plugin lane 起一个进程,作为“不受影响”的对照(该 fake agent 只应答 plugins/uninstall)。
- await service.uninstallPlugin({ workspacePath: sandboxRoot, pluginId: "pick-funds" });
- expect(commandResolver).toHaveBeenCalledTimes(1);
- // mcp/list 永不返回:首次超时 → watchdog 回收 mcp-status 进程 → 重试一次 → 再超时 → 抛错。
- await expect(service.listMcpServerStatuses({ workspacePath: sandboxRoot })).rejects.toThrow(
- /timed out/u,
- );
- // plugin lane 1 次 + mcp-status lane 首发 1 次 + 重试重新拉起 1 次
- expect(commandResolver).toHaveBeenCalledTimes(3);
- await vi.waitFor(
- () =>
- expect(
- processLifecycleReporter.onExit.mock.calls.filter(
- ([event]) =>
- event.lane === "mcp-status" && event.terminationKind === "watchdog_recycle",
- ),
- ).toHaveLength(2),
- // 修复原因:第二次 watchdog 与 mcp/list 的 reject 同 tick 触发,exit 事件要等
- // Windows 进程树回收(taskkill /T + 重试)完成后才上报;vi.waitFor 默认 1000ms
- // 在慢盘/高负载本机会先超时,把环境慢误报成用例失败。CI 快机器不受影响。
- { timeout: 8_000 },
- );
- // plugin lane 进程从未被回收,且仍能正常服务。
- expect(
- processLifecycleReporter.onExit.mock.calls.filter(([event]) => event.lane !== "mcp-status"),
- ).toHaveLength(0);
- await service.uninstallPlugin({ workspacePath: sandboxRoot, pluginId: "pick-funds" });
- expect(commandResolver).toHaveBeenCalledTimes(3);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("passes UI-resolved MCP servers to mcp/list", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-mcp-explicit-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "mcp-explicit-agent.cjs");
- writeFileSync(scriptPath, MCP_LIST_ECHO_MCP_SERVERS_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- });
- try {
- const result = await service.listMcpServerStatuses({
- workspacePath,
- mcpServers: [
- {
- name: "agents-fallback",
- command: "node",
- args: ["server.mjs"],
- env: [{ name: "TOKEN", value: "secret" }],
- timeoutMs: 3000,
- },
- ],
- });
- expect(JSON.parse(result.statuses.explicit?.error ?? "null")).toEqual([
- {
- name: "agents-fallback",
- command: "node",
- args: ["server.mjs"],
- env: [{ name: "TOKEN", value: "secret" }],
- timeoutMs: 3000,
- },
- ]);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("passes status mode to mcp/list", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-mcp-mode-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "mcp-mode-agent.cjs");
- writeFileSync(scriptPath, MCP_LIST_ECHO_MODE_FAKE_AGENT);
- const service = createZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- });
- try {
- const result = await service.listMcpServerStatuses({
- workspacePath,
- mode: "status",
- });
- expect(JSON.parse(result.statuses.mode?.error ?? "null")).toBe("status");
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("retries mcp/list without explicit MCP servers for legacy agents", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-mcp-compat-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "mcp-compat-agent.cjs");
- writeFileSync(scriptPath, MCP_LIST_REJECTS_MCP_SERVERS_ONCE_FAKE_AGENT);
- const service = createReadyZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- });
- try {
- const result = await service.listMcpServerStatuses({
- workspacePath,
- mcpServers: [],
- });
- expect(result.statuses.fallback?.status).toBe("disconnected");
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("retries connect-mode mcp/list without mode for legacy agents", async () => {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-service-mcp-mode-compat-"));
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "mcp-mode-compat-agent.cjs");
- writeFileSync(scriptPath, MCP_LIST_REJECTS_MODE_ONCE_FAKE_AGENT);
- const service = createZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- });
- try {
- const result = await service.listMcpServerStatuses({
- workspacePath,
- mode: "connect",
- });
- expect(result.statuses.fallback?.status).toBe("disconnected");
- expect(JSON.parse(result.statuses.fallback?.error ?? "null")).toBeNull();
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("does not retry status-only mcp/list as connect for legacy agents", async () => {
- const workspacePath = mkdtempSync(
- join(tmpdir(), "zcode-agent-service-mcp-mode-subset-compat-"),
- );
- createdDirs.push(workspacePath);
- const scriptPath = join(workspacePath, "mcp-mode-subset-compat-agent.cjs");
- writeFileSync(scriptPath, MCP_LIST_REJECTS_MODE_ONCE_FAKE_AGENT);
- const service = createZCodeAgentService({
- commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
- });
- try {
- await expect(
- service.listMcpServerStatuses({
- workspacePath,
- mode: "status",
- mcpServers: [
- {
- name: "plugin:canva:canva",
- type: "http",
- url: "https://mcp.canva.example.test/mcp",
- headers: [],
- },
- ],
- }),
- ).rejects.toMatchObject({
- code: ZCODE_AGENT_MCP_STATUS_MODE_UNSUPPORTED_ERROR_CODE,
- name: "ZCodeAgentMcpStatusModeUnsupportedError",
- });
- } finally {
- await service.disposeAllAndWait();
- }
- });
- });
|