| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140 |
- import { seedTaskIndex } from "./helpers/taskIndexFixture.js";
- import { fixture, port } from "#src/project-storage/test-fixture.js";
- import type { ZCodeSessionInfo } from "@zcode/shared";
- import { channelReplyHostRequestSchema, type ChannelReplyHostRequest } from "@zcode/shared";
- // v4 conversation 通道 host 转发面测试(M3 竖切,docs/v4-refactor/01-topology §host 通道层)。
- // 验证 host 只做透传:subscribe/unsubscribe/command 转发给 CLI v4 方法,
- // v4/conversation/frame 通知按 workspace fan-out 给 onDynamicConversationFrame 订阅者。
- import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- TopicWireFrameAssembler,
- conversationTopicFrameSchema,
- type ConversationTopicWireCandidate,
- type ConversationTopicWireFrame,
- type SessionsIndexTopicWireCandidate,
- type WorkspaceConfigTopicWireCandidate,
- } from "@zcode/shared/zcode-protocol-v4";
- import { createZCodeAgentConnectionScope } from "../src/zcode-agent/zcodeAgentConnectionScope.js";
- import {
- createZCodeAgentService,
- supportsLegacyRemoteTaskAllowlist,
- } from "../src/zcode-agent/zcodeAgentService.js";
- import { setDataBaseDir } from "../src/paths.js";
- import { TaskIndexRepo } from "../src/session/taskIndexRepo.js";
- // 最小 v4 fake agent:
- // - v4/conversation/subscribe → 一次 stdout.write 连续写 ACK-only response + initial notification;
- // - v4/conversation/unsubscribe → 返回 {};
- // - v4/command → 返回 CommandAck accepted。
- // 所有入站请求写入 logPath 供断言转发参数。
- const V4_FAKE_AGENT = `
- const fs = require("node:fs");
- const logPath = process.argv[2];
- 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 (logPath && msg.method) fs.appendFileSync(logPath, line + "\\n");
- if (msg.method === "session/list" && msg.id !== undefined) {
- const sessions = JSON.parse(fs.readFileSync(process.argv[3], "utf8"));
- send({ id: msg.id, result: { sessions: sessions.filter((session) => msg.params.sessionIds.includes(session.sessionId)) } });
- continue;
- }
- if (msg.method === "session/debug" && msg.id !== undefined) {
- send({ id: msg.id, result: { sessionId: msg.params.sessionId, rounds: [], networkEntries: [], cache: null } });
- continue;
- }
- if (msg.method === "v4/conversation/backgroundBashOutput" && msg.id !== undefined) {
- if (msg.params.workId === "unsupported") send({ id: msg.id, error: { code: -32601, message: "Method not found" } });
- else send({ id: msg.id, result: { kind: "unavailable", workId: msg.params.workId } });
- continue;
- }
- if (msg.method === "v4/conversation/offPeakControl" && msg.id !== undefined) {
- send({ id: msg.id, result: { accepted: true } });
- send({ method: "v4/offPeak/lifecycle", params: { sessionId: msg.params.sessionId, executionId: msg.params.executionId, offPeakTaskId: "idle-task", type: "ticket_released", ticketId: "old" } });
- continue;
- }
- if (msg.method === "v4/conversation/subscribe" && msg.params.topic.endsWith("/channel-reply-source")) {
- send({ id: "reverse-reply", method: "bot/channel/reply", trace: { traceId: "trace-native" }, params: { taskId: "channel-reply-source", inputId: "input-a", toolCallId: "call-a", parts: [{type:"mention",refId:"m1"}] } });
- }
- if (msg.method === "v4/conversation/subscribe" && msg.params.topic.endsWith("/topic-resource-source")) {
- send({ id: "reverse-resource", method: "topic/resource/read", params: { requestId: "resource-a", taskId: "topic-resource-source", inputId: "input-a", authorizationId: "auth-a", messageId: "message-a", resourceIndex: 0 } });
- }
- if (msg.method === "v4/conversation/subscribe" && msg.id !== undefined) {
- const response = {
- id: msg.id,
- result: {
- ack: { subscriptionId: "sub-1", mode: "snapshot", logEpoch: "epoch-1" },
- },
- };
- const route = {
- wireVersion: 3,
- logicalFrameOrdinal: 1,
- topic: msg.params.topic,
- subscriptionId: "sub-1",
- deliveryKind: "online",
- };
- let params;
- if (msg.params.topic.endsWith("/missing-delivery-kind")) {
- params = {
- ...route,
- deliveryKind: undefined,
- kind: "complete",
- logicalFrameId: "logical-missing-delivery-kind",
- frame: {
- topic: msg.params.topic,
- subscriptionId: "sub-1",
- fromSeq: 0,
- toSeq: 0,
- sentAt: 1700000000000,
- payload: { kind: "deltas", deltas: [] },
- },
- };
- } else if (msg.params.topic.endsWith("/invalid-delivery-kind")) {
- params = {
- ...route,
- deliveryKind: "guessed-recovery",
- kind: "complete",
- logicalFrameId: "logical-invalid-delivery-kind",
- frame: {
- topic: msg.params.topic,
- subscriptionId: "sub-1",
- fromSeq: 0,
- toSeq: 0,
- sentAt: 1700000000000,
- payload: { kind: "deltas", deltas: [] },
- },
- };
- } else if (msg.params.topic.endsWith("/invalid-base64")) {
- params = {
- ...route,
- wireVersion: 3,
- kind: "fragment",
- logicalFrameId: "logical-invalid",
- fragmentIndex: 0,
- fragmentCount: 1,
- logicalBytes: 1,
- checksum: { algorithm: "crc32", value: "00000000" },
- dataBase64: "%%%="
- };
- } else if (msg.params.topic.endsWith("/fragment-count-1025")) {
- params = {
- ...route,
- kind: "fragment",
- logicalFrameId: "logical-too-many-fragments",
- fragmentIndex: 0,
- fragmentCount: 1025,
- logicalBytes: 1025,
- checksum: { algorithm: "crc32", value: "00000000" },
- dataBase64: "AA=="
- };
- } else if (msg.params.topic.endsWith("/missing-checksum")) {
- params = {
- ...route,
- kind: "fragment",
- logicalFrameId: "logical-missing-checksum",
- fragmentIndex: 0,
- fragmentCount: 1,
- logicalBytes: 1,
- dataBase64: "AA=="
- };
- } else if (msg.params.topic.endsWith("/checksum-value-number")) {
- params = {
- ...route,
- kind: "fragment",
- logicalFrameId: "logical-checksum-value-number",
- fragmentIndex: 0,
- fragmentCount: 1,
- logicalBytes: 1,
- checksum: { algorithm: "crc32", value: 1 },
- dataBase64: "AA=="
- };
- } else if (msg.params.topic.endsWith("/data-base64-number")) {
- params = {
- ...route,
- kind: "fragment",
- logicalFrameId: "logical-data-base64-number",
- fragmentIndex: 0,
- fragmentCount: 1,
- logicalBytes: 1,
- checksum: { algorithm: "crc32", value: "00000000" },
- dataBase64: 1
- };
- } else if (msg.params.topic.endsWith("/extra-inner-field")) {
- params = {
- ...route,
- kind: "fragment",
- logicalFrameId: "logical-extra-inner-field",
- fragmentIndex: 0,
- fragmentCount: 1,
- logicalBytes: 1,
- checksum: { algorithm: "crc32", value: "00000000" },
- dataBase64: "AA==",
- unexpected: true
- };
- } else if (msg.params.topic.endsWith("/invalid-complete")) {
- params = {
- ...route,
- kind: "complete",
- logicalFrameId: "logical-invalid-complete",
- frame: { topic: msg.params.topic, subscriptionId: "sub-1" },
- };
- } else if (msg.params.topic.endsWith("/invalid-checksum-algorithm")) {
- params = {
- ...route,
- kind: "fragment",
- logicalFrameId: "logical-invalid-checksum-algorithm",
- fragmentIndex: 0,
- fragmentCount: 1,
- logicalBytes: 1,
- checksum: { algorithm: "sha256", value: "00000000" },
- dataBase64: "AA=="
- };
- } else if (msg.params.topic.endsWith("/invalid-checksum-value")) {
- params = {
- ...route,
- kind: "fragment",
- logicalFrameId: "logical-invalid-checksum-value",
- fragmentIndex: 0,
- fragmentCount: 1,
- logicalBytes: 1,
- checksum: { algorithm: "crc32", value: "not-crc32" },
- dataBase64: "AA=="
- };
- } else {
- params = {
- ...route,
- kind: "complete",
- logicalFrameId: "logical-initial",
- frame: {
- topic: msg.params.topic,
- subscriptionId: "sub-1",
- fromSeq: 0,
- toSeq: 0,
- sentAt: 1700000000000,
- payload: { kind: "deltas", deltas: [] },
- },
- };
- }
- const initial = {
- method: "v4/conversation/frame",
- params,
- };
- process.stdout.write(JSON.stringify(response) + "\\n" + JSON.stringify(initial) + "\\n");
- }
- if (msg.method === "v4/conversation/unsubscribe" && msg.id !== undefined) {
- const reply = () => send({ id: msg.id, result: {} });
- if (msg.params.topic.endsWith("/delayed-unsubscribe")) setTimeout(reply, 200);
- else reply();
- }
- if (msg.method === "v4/connection/flow" && msg.id !== undefined) {
- send({ id: msg.id, result: {} });
- }
- if (msg.method === "v4/attachment/begin" && msg.id !== undefined) {
- send({ id: msg.id, result: { uploadId: msg.params.uploadId, state: "staging", nextChunkIndex: 0 } });
- }
- if (msg.method === "v4/attachment/chunk" && msg.id !== undefined) {
- send({ id: msg.id, result: { uploadId: msg.params.uploadId, nextChunkIndex: msg.params.chunkIndex + 1 } });
- }
- if (msg.method === "v4/attachment/commit" && msg.id !== undefined) {
- send({ id: msg.id, result: { ref: "zcode-artifact://upload/one" } });
- }
- if (msg.method === "v4/attachment/abort" && msg.id !== undefined) {
- send({ id: msg.id, result: {} });
- }
- if (msg.method === "v4/attachment/previewSource" && msg.id !== undefined) {
- send({ id: msg.id, result: {
- kind: "local_path",
- path: "/tmp/.zcode/video-cache/demo.mp4",
- mediaType: "video/mp4"
- } });
- }
- if (msg.method === "v4/conversation/resync" && msg.id !== undefined) {
- const response = {
- id: msg.id,
- result: {
- ack: {
- subscriptionId: msg.params.subscriptionId,
- mode: msg.params.forceSnapshot ? "snapshot" : "resume",
- logEpoch: "epoch-1",
- },
- },
- };
- const recovery = {
- method: "v4/conversation/frame",
- params: {
- wireVersion: 3,
- kind: "complete",
- logicalFrameId: "logical-recovery",
- logicalFrameOrdinal: 2,
- topic: msg.params.topic,
- subscriptionId: msg.params.subscriptionId,
- deliveryKind: "recovery",
- frame: {
- topic: msg.params.topic,
- subscriptionId: msg.params.subscriptionId,
- fromSeq: 0,
- toSeq: 0,
- sentAt: 1700000000001,
- payload: { kind: "deltas", deltas: [] },
- },
- },
- };
- const reply = () =>
- process.stdout.write(JSON.stringify(response) + "\\n" + JSON.stringify(recovery) + "\\n");
- if (msg.params.topic.endsWith("/delayed-resync")) setTimeout(reply, 200);
- else reply();
- }
- if (msg.method === "v4/command" && msg.id !== undefined) {
- const status = "accepted";
- const acceptedInput = {
- delivery: "startNow",
- inputId: msg.params.commandId,
- messageId: "msg-" + msg.params.commandId,
- };
- const createSessionResult = msg.params.type === "createSession"
- ? { result: { type: "createSession", sessionId: "created-session", input: acceptedInput } }
- : msg.params.type === "sendText"
- ? { result: { type: "inputAccepted", ...acceptedInput } }
- : {};
- send({
- id: msg.id,
- result: {
- commandId: msg.params.commandId,
- status,
- revisionAtDecision: 3,
- ...createSessionResult,
- },
- });
- }
- if (msg.method === "v4/commands/query" && msg.id !== undefined) {
- send({
- id: msg.id,
- result: {
- results: msg.params.commands.map((key) => ({ key, result: "unknown" })),
- },
- });
- }
- }
- });
- `;
- function readRequests(logPath: string): Array<{ method: string; params: Record<string, unknown> }> {
- if (!existsSync(logPath)) {
- return [];
- }
- return readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .filter(Boolean)
- .map((line) => JSON.parse(line) as { method: string; params: Record<string, unknown> });
- }
- describe("zcodeAgentService v4 conversation channel", () => {
- it("闲时换票只更新已有 runtime,生命周期通知按工作区投递", async () => {
- const { service, workspacePath, commandResolver, logPath } = setupService();
- const events: unknown[] = [];
- const foreign: unknown[] = [];
- const listener = service.onDynamicOffPeakLifecycle({ workspacePath })((event) =>
- events.push(event),
- );
- const other = service.onDynamicOffPeakLifecycle({ workspacePath: "/other-workspace" })(
- (event) => foreign.push(event),
- );
- const control = {
- sessionId: "session",
- executionId: "execution",
- action: "replace_ticket" as const,
- renewalId: 1,
- ticketId: "new",
- requestAuth: { headers: { ticket: "new" } },
- };
- try {
- await expect(
- service.controlOffPeakExecutionV4({ workspacePath, control }),
- ).rejects.toMatchObject({ code: "ZCODE_AGENT_RUNTIME_UNAVAILABLE" });
- expect(commandResolver).not.toHaveBeenCalled();
- await service.subscribeConversationV4({ workspacePath, sessionId: "session" });
- expect(await service.controlOffPeakExecutionV4({ workspacePath, control })).toEqual({
- accepted: true,
- });
- await expect.poll(() => events.length).toBe(1);
- expect(events[0]).toMatchObject({
- executionId: "execution",
- type: "ticket_released",
- ticketId: "old",
- });
- expect(foreign).toEqual([]);
- expect(
- readRequests(logPath)
- .filter((entry) => entry.method === "v4/conversation/offPeakControl")
- .map((entry) => entry.params),
- ).toEqual([control]);
- } finally {
- listener.dispose();
- other.dispose();
- await service.disposeAllAndWait();
- }
- });
- const createdDirs: string[] = [];
- afterEach(() => {
- setDataBaseDir(null);
- for (const dir of createdDirs.splice(0).reverse()) {
- // 根因:Windows 不允许删除仍有打开句柄的目录,而 runtime/子进程与 watcher 的句柄
- // 是异步收口的;`force: true` 只吞 ENOENT,不做任何重试。于是 afterEach 稳定抛
- // `EPERM, Permission denied: \\?\C:\...\Temp\zcode-agent-v4-xxxx`,把已经通过断言
- // 的用例判成失败(POSIX 允许删除仍被打开的 inode,所以只在 Windows 上出现)。
- // 先用 rm 自带的有界重试等句柄释放;真的等不到也不该让清理决定用例成败,
- // 临时目录由操作系统回收。
- try {
- rmSync(dir, {
- recursive: true,
- force: true,
- maxRetries: 20,
- retryDelay: 100,
- });
- } catch {
- /* 清理失败不影响断言结论,交给操作系统回收临时目录。 */
- }
- }
- });
- function setupService(
- options: {
- providerReady?: boolean;
- authorizeLocalMediaPreviewPath?: (path: string) => Promise<string>;
- prepareSessionCreation?: NonNullable<
- Parameters<typeof createZCodeAgentService>[0]
- >["prepareSessionCreation"];
- resolveSessionDirectories?: NonNullable<
- Parameters<typeof createZCodeAgentService>[0]
- >["resolveSessionDirectories"];
- storedSessions?: (workspacePath: string) => ZCodeSessionInfo[];
- channelReplyExecutor?: NonNullable<
- Parameters<typeof createZCodeAgentService>[0]
- >["channelReplyExecutor"];
- topicResourceExecutor?: NonNullable<
- Parameters<typeof createZCodeAgentService>[0]
- >["topicResourceExecutor"];
- } = {},
- ) {
- const workspacePath = mkdtempSync(join(tmpdir(), "zcode-agent-v4-"));
- createdDirs.push(workspacePath);
- setDataBaseDir(workspacePath);
- const scriptPath = join(workspacePath, "v4-fake-agent.cjs");
- const logPath = join(workspacePath, "protocol.log");
- const sessionsPath = join(workspacePath, "sessions.json");
- writeFileSync(scriptPath, V4_FAKE_AGENT);
- writeFileSync(sessionsPath, JSON.stringify(options.storedSessions?.(workspacePath) ?? []));
- const commandResolver = vi.fn(() => ({
- command: process.execPath,
- args: [scriptPath, logPath, sessionsPath],
- }));
- const service = createZCodeAgentService({
- commandResolver,
- prepareSessionCreation: options.prepareSessionCreation,
- resolveSessionDirectories: options.resolveSessionDirectories,
- repoSnapshotCaptureIntentScheduler: options.repoSnapshotCaptureIntentScheduler,
- repoSnapshotSidecar: options.repoSnapshotSidecar,
- topicResourceExecutor: options.topicResourceExecutor,
- channelReplyExecutor: options.channelReplyExecutor,
- authorizeLocalMediaPreviewPath: options.authorizeLocalMediaPreviewPath,
- modelSelectionReadinessSource: {
- async getView() {
- return {
- revision: 1,
- providers:
- options.providerReady === false
- ? []
- : [
- {
- 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: {} }],
- },
- ],
- };
- },
- },
- });
- return { service, workspacePath, logPath, commandResolver };
- }
- const childHostId = "remote:server:subagent-view";
- const childWorkspace = (workspacePath: string) => ({
- workspacePath,
- workspaceIdentity: `${childHostId}:${workspacePath}`,
- workspaceKey: `${childHostId}:${workspacePath}`,
- });
- describe.each([
- "subagent_child",
- "workflow_child",
- "selection_side_chat",
- "nested_workflow_child",
- ] as const)("SAT30 %s", (childKind) => {
- const storedSession = (
- workspacePath: string,
- sessionId: string,
- parentSessionId?: string,
- ): ZCodeSessionInfo => ({
- sessionId,
- workspace: childWorkspace(workspacePath),
- ...(parentSessionId ? { parentSessionId } : {}),
- sessionKind: parentSessionId ? childKind : "interactive",
- title: sessionId,
- mode: "build",
- status: "completed",
- createdAt: 1,
- updatedAt: 2,
- });
- for (const clientMode of ["desktop-continuous", "web-remote-replayable"] as const) {
- it.each(["running", "completed"] as const)(
- `SAT30: remote %s child resolves its persisted parent project on ${clientMode}`,
- async (status) => {
- const state = await fixture();
- await port.migrate(state.db, { ...state.context, hostId: childHostId });
- const repo = port.repository(state.db, childHostId);
- const resolveDirectories = vi.fn(
- async (target: {
- workspacePath: string;
- workspaceIdentity?: string;
- sessionId: string;
- }) =>
- repo.resolveSessionDirectories({ ...target, taskId: target.sessionId })
- .additionalDirectories,
- );
- const env = setupService({
- resolveSessionDirectories: resolveDirectories,
- storedSessions: (path) => [
- storedSession(path, "parent"),
- { ...storedSession(path, "child", "parent"), status },
- ],
- });
- const project = repo.saveProject({ roots: [env.workspacePath] });
- repo.reserveCreatedTask({
- task: { ...childWorkspace(env.workspacePath), taskId: "parent" },
- projectId: project.id,
- });
- if (status === "running") {
- const legacyIndex = new TaskIndexRepo();
- try {
- await seedTaskIndex(legacyIndex, {
- meta: {
- ...childWorkspace(env.workspacePath),
- taskId: "child",
- traceId: "trace-child",
- title: "Legacy child index",
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- provider: "glm",
- },
- });
- } finally {
- legacyIndex.close();
- }
- }
- const scope = createZCodeAgentConnectionScope(env.service, {
- connectionId: "child-view",
- clientMode,
- });
- try {
- await scope.service.helloConversationV4();
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "child-view",
- appVersion: "test",
- });
- for (let attempt = 0; attempt < 2; attempt += 1) {
- const result = await scope.service.subscribeConversationV4({
- ...childWorkspace(env.workspacePath),
- sessionId: "child",
- });
- await scope.service.unsubscribeConversationV4({
- ...childWorkspace(env.workspacePath),
- subscriptionId: result.ack.subscriptionId,
- });
- if (attempt === 0)
- await env.service.disposeWorkspace(childWorkspace(env.workspacePath));
- }
- expect(resolveDirectories).toHaveBeenCalledTimes(2);
- expect(resolveDirectories).toHaveBeenCalledWith(
- expect.objectContaining({ sessionId: "parent" }),
- );
- expect(
- state.db.prepare("SELECT count(*) AS count FROM tasks WHERE task_id='child'").get()
- ?.count,
- ).toBe(0);
- const subscriptions = readRequests(env.logPath).filter(
- (entry) => entry.method === "v4/conversation/subscribe",
- );
- expect(subscriptions).toHaveLength(2);
- expect(subscriptions[0]?.params).toMatchObject({
- topic: "conversation/child",
- clientMode,
- additionalDirectories: [],
- });
- } finally {
- await env.service.disposeAllAndWait();
- }
- },
- );
- }
- it.each([
- "missing-parent",
- "missing-parent-id",
- "foreign-child",
- "foreign-parent",
- "foreign-host",
- "foreign-cwd",
- "cycle",
- "unclassified-parent",
- "ordinary",
- ])(
- "SAT30: rejects remote subscription with %s without bypassing project admission",
- async (failure) => {
- const resolveDirectories = vi.fn(async () => {
- throw new Error("project.remote-project-required");
- });
- const env = setupService({
- resolveSessionDirectories: resolveDirectories,
- storedSessions: (path) => {
- const parent = storedSession(path, "parent");
- const child = storedSession(path, "child", "parent");
- if (failure === "missing-parent") return [child];
- if (failure === "missing-parent-id") delete child.parentSessionId;
- if (failure === "foreign-child") child.workspace = childWorkspace(`${path}-other`);
- if (failure === "foreign-parent") parent.workspace = childWorkspace(`${path}-other`);
- if (failure === "foreign-host") {
- parent.workspace.workspaceIdentity = `remote:server:other:${path}`;
- parent.workspace.workspaceKey = parent.workspace.workspaceIdentity;
- }
- if (failure === "foreign-cwd") parent.workspace.workspacePath = `${path}-other`;
- if (failure === "cycle") {
- parent.sessionKind = childKind;
- parent.parentSessionId = "child";
- }
- if (failure === "ordinary") {
- child.sessionKind = "interactive";
- delete child.parentSessionId;
- }
- return [parent, child];
- },
- });
- try {
- await expect(
- env.service.subscribeConversationV4({
- ...childWorkspace(env.workspacePath),
- sessionId: "child",
- }),
- ).rejects.toThrow();
- expect(
- readRequests(env.logPath).some((entry) => entry.method === "v4/conversation/subscribe"),
- ).toBe(false);
- if (failure === "ordinary")
- expect(resolveDirectories).toHaveBeenCalledWith(
- expect.objectContaining({ sessionId: "child" }),
- );
- else if (failure === "unclassified-parent")
- expect(resolveDirectories).toHaveBeenCalledWith(
- expect.objectContaining({ sessionId: "parent" }),
- );
- else expect(resolveDirectories).not.toHaveBeenCalled();
- } finally {
- await env.service.disposeAllAndWait();
- }
- },
- );
- });
- it.each(["interactive", "fork", "workflow_parent"] as const)(
- "SAT30: mixed auxiliary lineage stops at independent %s project",
- async (rootKind) => {
- const resolveDirectories = vi.fn(async () => []);
- const env = setupService({
- resolveSessionDirectories: resolveDirectories,
- storedSessions: (path) =>
- [
- { sessionId: "owner", parentSessionId: "different-project", sessionKind: rootKind },
- {
- sessionId: "side",
- parentSessionId: "owner",
- sessionKind: "selection_side_chat" as const,
- },
- { sessionId: "actor", parentSessionId: "side", sessionKind: "workflow_child" as const },
- ].map((session) => ({
- ...session,
- workspace: childWorkspace(path),
- title: session.sessionId,
- mode: "build" as const,
- status: "completed" as const,
- createdAt: 1,
- updatedAt: 2,
- })),
- });
- try {
- await env.service.subscribeConversationV4({
- ...childWorkspace(env.workspacePath),
- sessionId: "actor",
- });
- expect(resolveDirectories).toHaveBeenCalledWith(
- expect.objectContaining({ sessionId: "owner" }),
- );
- expect(
- readRequests(env.logPath)
- .filter((entry) => entry.method === "session/list")
- .flatMap((entry) => entry.params?.sessionIds ?? []),
- ).not.toContain("different-project");
- } finally {
- await env.service.disposeAllAndWait();
- }
- },
- );
- it.each([false, true])(
- "projects only channel reply scope from a live V4 connection (remote=%s)",
- async (remote) => {
- const reply = vi.fn(async (value: ChannelReplyHostRequest) => {
- // 与实际 Host executor 一样执行严格校验,不能用宽松 spy 掩盖额外连接字段。
- channelReplyHostRequestSchema.parse(value);
- return { status: "sent" as const, deliveryId: "delivery", providerMessageId: "om_sent" };
- });
- const { service, workspacePath } = setupService({ channelReplyExecutor: reply });
- const scope = remote
- ? { workspaceIdentity: "ssh://host/work", remoteSessionId: "logical-session" }
- : {};
- const target = {
- workspacePath,
- ...scope,
- sessionId: "channel-reply-source",
- envelope: { requestId: "transport-request" },
- __zcodeTrustedV4Connection: { connectionId: "test-connection" },
- };
- try {
- await service.subscribeConversationV4(target);
- await vi.waitFor(() => expect(reply).toHaveBeenCalled());
- const received = reply.mock.calls[0]![0];
- expect(() => channelReplyHostRequestSchema.parse(received)).not.toThrow();
- expect(received).toEqual({
- workspacePath,
- workspaceIdentity: scope.workspaceIdentity,
- remoteSessionId: scope.remoteSessionId,
- taskId: "channel-reply-source",
- inputId: "input-a",
- toolCallId: "call-a",
- trace: { traceId: "trace-native" },
- parts: [{ type: "mention", refId: "m1" }],
- });
- } finally {
- await service.disposeAllAndWait();
- }
- },
- );
- it("carries the active remote session on reverse resource requests after cold warmup", async () => {
- const read = vi.fn(async () => {
- throw new Error("download intentionally stopped after route capture");
- });
- const { service, workspacePath } = setupService({ topicResourceExecutor: read });
- try {
- await service.subscribeConversationV4({
- workspacePath,
- workspaceIdentity: "ssh://host/work",
- sessionId: "warmup",
- });
- await service.subscribeConversationV4({
- workspacePath,
- workspaceIdentity: "ssh://host/work",
- remoteSessionId: "logical-session",
- sessionId: "topic-resource-source",
- });
- await vi.waitFor(() =>
- expect(read).toHaveBeenCalledWith(
- expect.objectContaining({
- workspaceIdentity: "ssh://host/work",
- remoteSessionId: "logical-session",
- taskId: "topic-resource-source",
- }),
- expect.any(AbortSignal),
- ),
- );
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it.each(["desktop-continuous", "web-remote-replayable"] as const)(
- "session debug remains read-only and workspace-scoped on %s",
- async (clientMode) => {
- const { service, workspacePath, commandResolver, logPath } = setupService();
- const scope = createZCodeAgentConnectionScope(service, { connectionId: "debug", clientMode });
- const target = {
- workspacePath,
- workspaceIdentity: "ssh://debug/workspace",
- sessionId: "debug-session",
- };
- try {
- await expect(scope.service.readSessionDebug(target)).rejects.toThrow();
- expect(commandResolver).not.toHaveBeenCalled();
- await scope.service.helloConversationV4();
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "debug",
- appVersion: "test",
- });
- await scope.service.subscribeConversationV4(target);
- expect(await scope.service.readSessionDebug(target)).toEqual({
- sessionId: "debug-session",
- rounds: [],
- networkEntries: [],
- cache: null,
- });
- const lines = readFileSync(logPath, "utf8")
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line));
- expect(
- lines.filter((line) => line.method === "session/debug").map((line) => line.params),
- ).toEqual([{ sessionId: "debug-session" }]);
- expect(
- lines.some(
- (line) => line.method === "session/resume" || line.method === "session/subscribe",
- ),
- ).toBe(false);
- } finally {
- scope.dispose();
- await service.disposeAllAndWait();
- }
- },
- );
- it.each([undefined, { text: "Guarded work" }, { text: "Guarded work", mode: "guarded" }])(
- "Guarded create forwards the original payload once (firstInput=%j)",
- async (firstInput) => {
- const { service, workspacePath, logPath } = setupService();
- try {
- const payload = {
- workspaceId: workspacePath,
- config: { mode: "guarded" },
- ...(firstInput ? { firstInput } : {}),
- };
- const ack = await service.sendConversationCommandV4({
- workspacePath,
- envelope: {
- commandId: "guarded-create",
- clientId: "guarded-client",
- issuedAt: 1,
- sessionId: null,
- type: "createSession",
- payload,
- },
- });
- expect(ack).toMatchObject({
- status: "accepted",
- result: { type: "createSession", sessionId: "created-session" },
- });
- const commands = readRequests(logPath).filter((item) => item.method === "v4/command");
- // 根因:为不存在的版本错配补写 mode、补发无 revision 的模式命令,改变了原创建合同。
- expect(commands).toHaveLength(1);
- expect(commands[0]?.params.type).toBe("createSession");
- expect(commands[0]?.params.payload).toEqual(payload);
- } finally {
- await service.disposeAllAndWait();
- }
- },
- );
- it("keeps history topics readable without letting the active client bypass provider readiness", async () => {
- const { service, workspacePath, logPath, commandResolver } = setupService({
- providerReady: false,
- });
- try {
- const sessionsIndex = await service.subscribeSessionsIndexV4({ workspacePath });
- const conversation = await service.subscribeConversationV4({
- workspacePath,
- sessionId: "persisted-session",
- });
- expect(sessionsIndex.ack.subscriptionId).toBe("sub-1");
- expect(conversation.ack.subscriptionId).toBe("sub-1");
- expect(conversation.ack.openTiming).toMatchObject({
- version: 1,
- hostPrepareMs: expect.any(Number),
- providerRegistrySyncMs: expect.any(Number),
- taskMetaReadMs: expect.any(Number),
- cliRequestMs: expect.any(Number),
- });
- expect(commandResolver).toHaveBeenCalledTimes(1);
- // 修复原因:只读 topic 已经登记 active client,旧 fast path 会直接返回它并绕过
- // provider/model 门禁。createSession 必须仍在写入协议前确定性拒绝。
- await expect(service.createSession({ workspacePath })).rejects.toMatchObject({
- code: "ZCODE_AGENT_PROVIDER_NOT_READY",
- data: expect.objectContaining({ reason: "provider_not_ready" }),
- });
- const requests = readRequests(logPath);
- expect(
- requests
- .filter((entry) => entry.method === "v4/conversation/subscribe")
- .map((entry) => entry.params.topic),
- ).toEqual([`sessions-index/${workspacePath}`, "conversation/persisted-session"]);
- expect(requests.some((entry) => entry.method === "session/create")).toBe(false);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("uses the registry view only as a readiness gate for a cold conversation subscription", async () => {
- const { service, workspacePath, logPath } = setupService();
- try {
- await service.subscribeConversationV4({
- workspacePath,
- sessionId: "cold-registry-session",
- });
- // Worker 已经持有进程级 Registry;Host 读取 Registry View 只用于启动门禁,
- // 不能把完整 Registry 下发成另一份执行事实源。
- expect(readRequests(logPath).map((entry) => entry.method)).toEqual([
- "v4/conversation/subscribe",
- ]);
- expect(
- readRequests(logPath).find((entry) => entry.method === "v4/conversation/subscribe")?.params,
- ).toMatchObject({
- workspace: {
- workspaceKey: workspacePath,
- workspacePath,
- },
- });
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it.each(["desktop-continuous", "web-remote-replayable"] as const)(
- "background Bash query stays existing-only on %s",
- async (clientMode) => {
- const { service, workspacePath, commandResolver, logPath } = setupService();
- const scope = createZCodeAgentConnectionScope(service, {
- connectionId: "bash-query",
- clientMode,
- });
- const target = {
- workspacePath,
- workspaceIdentity: "ssh://bash-query/workspace",
- sessionId: "child",
- workId: "work",
- };
- try {
- await scope.service.helloConversationV4();
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "bash-query",
- appVersion: "test",
- });
- expect(await scope.service.backgroundBashOutputV4(target)).toEqual({
- kind: "unavailable",
- workId: "work",
- });
- expect(commandResolver).not.toHaveBeenCalled();
- await scope.service.subscribeConversationV4(target);
- expect(await scope.service.backgroundBashOutputV4(target)).toEqual({
- kind: "unavailable",
- workId: "work",
- });
- expect(commandResolver).toHaveBeenCalledTimes(1);
- const requests = readRequests(logPath);
- expect(
- requests
- .filter((entry) => entry.method === "v4/conversation/backgroundBashOutput")
- .map((entry) => entry.params),
- ).toEqual([{ sessionId: "child", workId: "work" }]);
- expect(requests.some((entry) => entry.method === "v4/command")).toBe(false);
- expect(
- await scope.service.backgroundBashOutputV4({ ...target, workId: "unsupported" }),
- ).toEqual({ kind: "unsupported", workId: "unsupported" });
- } finally {
- scope.dispose();
- await service.disposeAllAndWait();
- }
- },
- );
- it("existing-only subscriptions stay dormant until another operation starts the runtime", async () => {
- const { service, workspacePath, commandResolver } = setupService();
- const lifecycle: Array<{ state: "available" | "unavailable"; generation: number }> = [];
- const disposable = service.onAgentRuntimeLifecycle?.((event) => {
- lifecycle.push({
- state: event.state,
- generation: event.runtimeIdentity.generation,
- });
- });
- try {
- await expect(
- service.subscribeSessionsIndexV4({
- workspacePath,
- runtimePolicy: "existing-only",
- }),
- ).rejects.toMatchObject({ code: "ZCODE_AGENT_RUNTIME_UNAVAILABLE" });
- expect(commandResolver).not.toHaveBeenCalled();
- expect(lifecycle).toEqual([]);
- await service.subscribeConversationV4({
- workspacePath,
- sessionId: "explicit-user-session",
- });
- await service.subscribeSessionsIndexV4({
- workspacePath,
- runtimePolicy: "existing-only",
- });
- expect(commandResolver).toHaveBeenCalledTimes(1);
- expect(lifecycle).toEqual([{ state: "available", generation: 1 }]);
- await service.disposeWorkspace({ workspacePath });
- expect(lifecycle).toEqual([
- { state: "available", generation: 1 },
- { state: "unavailable", generation: 1 },
- ]);
- } finally {
- disposable?.dispose();
- await service.disposeAllAndWait();
- }
- });
- // `workflowRunDeltas` 与 `clientMode` 同族(10 §3.1):可信 host 从连接的 clientHello 注入,
- // UI 面的 subscribe 入参碰不到它——一个客户端认不认得 `workflowRun.*` 增量是**连接**的事实。
- it.each([true, false])(
- "subscribe 的 workflowRunDeltas 只由 clientHello 声明决定(declared=%s)",
- async (declared) => {
- const { service, workspacePath, logPath } = setupService();
- const scope = createZCodeAgentConnectionScope(service, {
- connectionId: "workflow-run-deltas",
- clientMode: "desktop-continuous",
- });
- try {
- const hello = await scope.service.helloConversationV4();
- expect(hello.capabilities.workflowRunDeltas).toBe(true);
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "workflow-run-deltas",
- appVersion: "test",
- ...(declared ? { capabilities: { workflowRunDeltas: true } } : {}),
- });
- await scope.service.subscribeConversationV4({
- workspacePath,
- sessionId: "workflow-run-deltas-session",
- // 调用方自报的同名字段:必须被可信值整个顶掉,两种声明下都不能生效。
- workflowRunDeltas: !declared,
- } as never);
- const params = readRequests(logPath).find(
- (entry) => entry.method === "v4/conversation/subscribe",
- )?.params;
- if (declared) {
- expect(params).toMatchObject({ workflowRunDeltas: true });
- } else {
- expect(params).not.toHaveProperty("workflowRunDeltas");
- }
- } finally {
- scope.dispose();
- await service.disposeAllAndWait();
- }
- },
- );
- it("forwards task-local thought only as a V4 cold-resume hint", async () => {
- const { service, workspacePath, logPath } = setupService();
- const repo = new TaskIndexRepo();
- try {
- await seedTaskIndex(repo, {
- meta: {
- taskId: "persisted-thought-session",
- traceId: "trace-persisted-thought-session",
- title: "persisted thought session",
- workspacePath,
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- provider: "glm",
- model: "glm/GLM-5.2",
- thoughtLevel: "max",
- },
- });
- await service.subscribeConversationV4({
- workspacePath,
- sessionId: "persisted-thought-session",
- });
- const request = readRequests(logPath).find(
- (entry) => entry.method === "v4/conversation/subscribe",
- );
- expect(request?.params).toMatchObject({
- topic: "conversation/persisted-thought-session",
- clientMode: "desktop-continuous",
- resumeThoughtLevel: "max",
- });
- } finally {
- repo.close();
- await service.disposeAllAndWait();
- }
- });
- it("forwards trusted continuous connection context and fans out frames per workspace", async () => {
- const { service, workspacePath, logPath } = setupService();
- try {
- const frames: ConversationTopicWireFrame[] = [];
- const disposable = service.onDynamicConversationFrame({ workspacePath })((frame) => {
- frames.push(frame);
- });
- const hello = await service.helloConversationV4();
- await service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "client-1",
- appVersion: "4.0.0",
- });
- expect(hello).toMatchObject({
- clientMode: "desktop-continuous",
- deliveryProfile: "continuous",
- });
- const result = await service.subscribeConversationV4({
- workspacePath,
- sessionId: "s1",
- });
- expect(result.ack).toMatchObject({
- subscriptionId: "sub-1",
- mode: "snapshot",
- logEpoch: "epoch-1",
- });
- expect(result.ack.openTiming).toMatchObject({
- version: 1,
- hostPrepareMs: expect.any(Number),
- cliProcessState: "spawned",
- });
- expect(result).toEqual({ ack: result.ack });
- await vi.waitFor(() => {
- expect(frames).toHaveLength(1);
- });
- expect(frames[0]).toMatchObject({
- topic: "conversation/s1",
- subscriptionId: "sub-1",
- kind: "complete",
- frame: { payload: { kind: "deltas", deltas: [] } },
- });
- const subscribeRequest = readRequests(logPath).find(
- (entry) => entry.method === "v4/conversation/subscribe",
- );
- expect(subscribeRequest?.params.topic).toBe("conversation/s1");
- expect(subscribeRequest?.params.clientMode).toBe("desktop-continuous");
- expect(subscribeRequest?.params).not.toHaveProperty("deliveryProfile");
- // 这条 clientHello 没有 capabilities:订阅就不能带增量位,CLI 按旧消费者发整键 patch。
- expect(subscribeRequest?.params).not.toHaveProperty("workflowRunDeltas");
- expect(typeof subscribeRequest?.params.connectionId).toBe("string");
- expect(String(subscribeRequest?.params.connectionId)).toMatch(/^host-/);
- disposable.dispose();
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("reserves explicit NULL for legacy V4 callers that omit projectId", async () => {
- const prepareSessionCreation = vi.fn(async () => [] as string[]);
- const { service, workspacePath } = setupService({ prepareSessionCreation });
- try {
- await service.sendConversationCommandV4({
- workspacePath,
- envelope: {
- commandId: "legacy-create",
- clientId: "test",
- sessionId: null,
- type: "createSession",
- payload: { workspaceId: workspacePath },
- issuedAt: 1,
- },
- });
- expect(prepareSessionCreation).toHaveBeenCalledWith(
- expect.objectContaining({ projectId: null, sessionId: expect.any(String) }),
- );
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("reserves project ownership before create wire and passes only directories to CLI", async () => {
- const prepareSessionCreation = vi.fn(
- async (_target: { workspacePath: string; sessionId: string; projectId: string | null }) => [
- "/extra",
- ],
- );
- const { service, workspacePath, logPath } = setupService({ prepareSessionCreation });
- const envelope = {
- commandId: "project-create",
- clientId: "test",
- sessionId: null,
- type: "createSession" as const,
- payload: { workspaceId: workspacePath },
- issuedAt: 1,
- };
- try {
- await service.sendConversationCommandV4({ workspacePath, projectId: "project-a", envelope });
- await service.sendConversationCommandV4({ workspacePath, projectId: "project-a", envelope });
- const creates = readRequests(logPath).filter((entry) => entry.method === "v4/command");
- expect(prepareSessionCreation).toHaveBeenCalledTimes(2);
- const target = prepareSessionCreation.mock.calls[0]![0];
- expect(target).toMatchObject({
- workspacePath,
- projectId: "project-a",
- sessionId: expect.any(String),
- });
- expect(creates[0]?.params.payload).toMatchObject({
- sessionId: target.sessionId,
- additionalDirectories: ["/extra"],
- });
- expect(creates[1]?.params.payload.sessionId).toBe(target.sessionId);
- expect(creates[0]?.params.payload).not.toHaveProperty("projectId");
- prepareSessionCreation.mockRejectedValueOnce(new Error("database unavailable"));
- await expect(
- service.sendConversationCommandV4({
- workspacePath,
- projectId: null,
- envelope: { ...envelope, commandId: "failed-create" },
- }),
- ).rejects.toThrow("database unavailable");
- expect(readRequests(logPath).filter((entry) => entry.method === "v4/command")).toHaveLength(
- 2,
- );
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("labels mobile input from the trusted attachment instead of caller metadata", async () => {
- const { service, workspacePath, logPath } = setupService();
- const scope = createZCodeAgentConnectionScope(service, {
- connectionId: "group-mobile-origin",
- clientMode: "web-remote-replayable",
- });
- try {
- await scope.service.helloConversationV4();
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "origin-client",
- appVersion: "4.0.0",
- });
- await scope.service.sendConversationCommandV4({
- workspacePath,
- clientMode: "desktop-continuous",
- envelope: {
- commandId: "origin-command",
- clientId: "origin-client",
- sessionId: "origin-session",
- type: "sendText",
- issuedAt: 1,
- payload: { text: "hello", inputOrigin: "desktop" },
- },
- });
- expect(
- readRequests(logPath).find((entry) => entry.method === "v4/command")?.params,
- ).toMatchObject({ payload: { text: "hello", inputOrigin: "mobile" } });
- } finally {
- await scope.dispose();
- await service.disposeAllAndWait();
- }
- });
- it("does not carry runtime preferences on v4 create, send, or subscribe", async () => {
- const { service, workspacePath, logPath } = setupService();
- try {
- await service.sendConversationCommandV4({
- workspacePath,
- envelope: {
- commandId: "cmd-runtime-preferences-create",
- clientId: "client-runtime-preferences",
- sessionId: null,
- type: "createSession",
- payload: { workspaceId: workspacePath },
- issuedAt: 1_700_000_000_000,
- },
- });
- await service.sendConversationCommandV4({
- workspacePath,
- envelope: {
- commandId: "cmd-runtime-preferences-send",
- clientId: "client-runtime-preferences",
- sessionId: "s-runtime-preferences",
- type: "sendText",
- payload: { text: "continue" },
- issuedAt: 1_700_000_000_001,
- },
- });
- await service.subscribeConversationV4({
- workspacePath,
- sessionId: "s-runtime-preferences",
- });
- const requests = readRequests(logPath);
- const commands = requests.filter((entry) => entry.method === "v4/command");
- expect(commands[0]?.params).toMatchObject({
- type: "createSession",
- payload: { workspaceId: workspacePath },
- });
- expect(commands[1]?.params).toMatchObject({
- type: "sendText",
- payload: { text: "continue" },
- });
- for (const command of commands) {
- expect(command.params.payload).not.toHaveProperty("nativeSearchEnhancementsEnabled");
- expect(command.params.payload).not.toHaveProperty("memoryEnabled");
- expect(command.params.payload).not.toHaveProperty("integratedTerminalShell");
- }
- const subscribe = requests.find((entry) => entry.method === "v4/conversation/subscribe");
- expect(subscribe?.params).not.toHaveProperty("nativeSearchEnhancementsEnabled");
- expect(subscribe?.params).not.toHaveProperty("memoryEnabled");
- expect(subscribe?.params).not.toHaveProperty("integratedTerminalShell");
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("SSH sessions-index 只传递当前 identity 的 GLM task-index 归属证明", async () => {
- const { service, workspacePath, logPath } = setupService();
- const repo = new TaskIndexRepo();
- const workspaceIdentity = `remote:ssh:example.test:22:coder:${workspacePath}`;
- const foreignWorkspaceIdentity = `remote:ssh:other.example.test:22:coder:${workspacePath}`;
- try {
- await seedTaskIndex(repo, {
- meta: {
- taskId: "legacy-current-ssh",
- traceId: "trace-legacy-current-ssh",
- title: "current SSH legacy task",
- workspacePath,
- workspaceIdentity,
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- provider: "glm",
- },
- });
- await seedTaskIndex(repo, {
- meta: {
- taskId: "legacy-foreign-ssh",
- traceId: "trace-legacy-foreign-ssh",
- title: "foreign SSH legacy task",
- workspacePath,
- workspaceIdentity: foreignWorkspaceIdentity,
- createdAt: 1,
- updatedAt: 3,
- mode: "build",
- provider: "glm",
- },
- });
- await service.subscribeSessionsIndexV4({ workspacePath, workspaceIdentity });
- const subscribeRequest = readRequests(logPath).find(
- (entry) => entry.method === "v4/conversation/subscribe",
- );
- expect(subscribeRequest?.params).toMatchObject({
- topic: `sessions-index/${workspaceIdentity}`,
- legacyTaskIds: ["legacy-current-ssh"],
- });
- } finally {
- repo.close();
- await service.disposeAllAndWait();
- }
- });
- it("历史任务 allowlist 只开放 SSH 与 WSL identity", () => {
- expect(supportsLegacyRemoteTaskAllowlist("remote:ssh:host:22:user:/repo")).toBe(true);
- expect(supportsLegacyRemoteTaskAllowlist("remote:wsl:Ubuntu-24.04:user:/repo")).toBe(true);
- expect(supportsLegacyRemoteTaskAllowlist("remote:docker:container:/repo")).toBe(false);
- expect(supportsLegacyRemoteTaskAllowlist("/repo")).toBe(false);
- });
- it("forwards connection flow only through the non-RPC trusted scope control", async () => {
- const { service, workspacePath, logPath } = setupService();
- const scope = createZCodeAgentConnectionScope(service, {
- connectionId: "mobile-flow",
- clientMode: "web-remote-replayable",
- });
- try {
- await scope.service.helloConversationV4();
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "mobile-flow-client",
- appVersion: "4.0.0",
- });
- await scope.service.subscribeConversationV4({ workspacePath, sessionId: "s-flow" });
- await scope.setTransportFlowState("saturated");
- const flowRequest = readRequests(logPath).find(
- (entry) => entry.method === "v4/connection/flow",
- );
- expect(flowRequest?.params).toEqual({
- connectionId: "mobile-flow",
- state: "saturated",
- });
- await expect(
- scope.service.setConnectionFlowStateV4({ workspacePath, state: "drained" }),
- ).rejects.toThrow("fault.connection.flowControlForbidden");
- } finally {
- await scope.dispose();
- await service.disposeAllAndWait();
- }
- });
- it("forwards chunked attachment methods with trusted connection and <=1MiB NDJSON", async () => {
- const { service, workspacePath, logPath } = setupService();
- await expect(
- service.attachmentBeginV4({
- workspacePath,
- sessionId: "s-upload",
- uploadId: "untrusted",
- fileName: "nope.bin",
- mime: "application/octet-stream",
- totalBytes: 0,
- totalChunks: 0,
- checksum: `sha256:${"0".repeat(64)}`,
- }),
- ).rejects.toThrow("fault.attachment.connectionUntrusted");
- const scope = createZCodeAgentConnectionScope(service, {
- connectionId: "mobile-upload",
- clientMode: "web-remote-replayable",
- });
- try {
- await scope.service.helloConversationV4();
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "mobile-upload-client",
- appVersion: "4.0.0",
- });
- const common = { workspacePath, sessionId: "s-upload", uploadId: "upload-1" };
- await scope.service.attachmentBeginV4({
- ...common,
- fileName: "big.bin",
- mime: "application/octet-stream",
- totalBytes: 512 * 1024,
- totalChunks: 1,
- checksum: `sha256:${"0".repeat(64)}`,
- });
- await scope.service.attachmentChunkV4({
- ...common,
- chunkIndex: 0,
- dataBase64: Buffer.alloc(512 * 1024).toString("base64"),
- });
- await expect(scope.service.attachmentCommitV4(common)).resolves.toEqual({
- ref: "zcode-artifact://upload/one",
- });
- await scope.service.attachmentAbortV4(common);
- const attachmentLines = readFileSync(logPath, "utf8")
- .split("\n")
- .filter((line) => line.includes('"method":"v4/attachment/'));
- expect(attachmentLines).toHaveLength(4);
- for (const line of attachmentLines) {
- expect(Buffer.byteLength(`${line}\n`, "utf8")).toBeLessThanOrEqual(1024 * 1024);
- expect(JSON.parse(line).params.connectionId).toBe("mobile-upload");
- }
- } finally {
- await scope.dispose();
- await service.disposeAllAndWait();
- }
- });
- it("only queries a local video source for Desktop local connections", async () => {
- const authorizeLocalMediaPreviewPath = vi.fn(async () => "/real/video-cache/demo.mp4");
- const { service, workspacePath, logPath } = setupService({
- authorizeLocalMediaPreviewPath,
- });
- const desktop = createZCodeAgentConnectionScope(service, {
- connectionId: "desktop-preview",
- clientMode: "desktop-continuous",
- });
- const mobile = createZCodeAgentConnectionScope(service, {
- connectionId: "mobile-preview",
- clientMode: "web-remote-replayable",
- });
- try {
- for (const [scope, clientId] of [
- [desktop, "desktop-preview-client"],
- [mobile, "mobile-preview-client"],
- ] as const) {
- await scope.service.helloConversationV4();
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId,
- appVersion: "4.0.0",
- });
- }
- const input = {
- workspacePath,
- sessionId: "s-preview",
- ref: "/tmp/demo.mp4",
- target: { rowId: 2, entityId: "message-preview" },
- attachmentIndex: 0,
- };
- await expect(desktop.service.attachmentPreviewSourceV4(input)).resolves.toEqual({
- kind: "local_path",
- path: "/real/video-cache/demo.mp4",
- mediaType: "video/mp4",
- });
- expect(authorizeLocalMediaPreviewPath).toHaveBeenCalledOnce();
- expect(authorizeLocalMediaPreviewPath).toHaveBeenCalledWith(
- "/tmp/.zcode/video-cache/demo.mp4",
- );
- await expect(mobile.service.attachmentPreviewSourceV4(input)).resolves.toEqual({
- kind: "chunked",
- });
- await expect(
- desktop.service.attachmentPreviewSourceV4({
- ...input,
- remoteSessionId: "remote-session",
- }),
- ).resolves.toEqual({ kind: "chunked" });
- const sourceLines = readFileSync(logPath, "utf8")
- .split("\n")
- .filter((line) => line.includes('"method":"v4/attachment/previewSource"'));
- expect(sourceLines).toHaveLength(1);
- expect(JSON.parse(sourceLines[0]!).params).toMatchObject({
- clientMode: "desktop-continuous",
- sessionId: "s-preview",
- attachmentIndex: 0,
- });
- } finally {
- await desktop.dispose();
- await mobile.dispose();
- await service.disposeAllAndWait();
- }
- });
- it("fails closed when Main cannot authorize a Desktop local preview path", async () => {
- const { service, workspacePath } = setupService({
- authorizeLocalMediaPreviewPath: async () => {
- throw new Error("preview path authorization failed");
- },
- });
- const desktop = createZCodeAgentConnectionScope(service, {
- connectionId: "desktop-preview-failed",
- clientMode: "desktop-continuous",
- });
- try {
- await desktop.service.helloConversationV4();
- await desktop.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "desktop-preview-failed-client",
- appVersion: "4.0.0",
- });
- await expect(
- desktop.service.attachmentPreviewSourceV4({
- workspacePath,
- sessionId: "s-preview",
- ref: "/tmp/demo.mp4",
- target: { rowId: 2, entityId: "message-preview" },
- attachmentIndex: 0,
- }),
- ).rejects.toThrow("preview path authorization failed");
- } finally {
- await desktop.dispose();
- await service.disposeAllAndWait();
- }
- });
- it.each([
- ["missing-delivery-kind", "proto.frameAssemblyMetadataMismatch"],
- ["invalid-delivery-kind", "proto.frameAssemblyMetadataMismatch"],
- ["invalid-base64", "proto.frameAssemblyInvalidBase64"],
- ["fragment-count-1025", "proto.frameFragmentCountExceeded"],
- ["missing-checksum", "proto.frameAssemblyMetadataMismatch"],
- ["checksum-value-number", "proto.frameAssemblyMetadataMismatch"],
- ["data-base64-number", "proto.frameAssemblyMetadataMismatch"],
- ["extra-inner-field", "proto.frameAssemblyMetadataMismatch"],
- ["invalid-complete", "proto.frameAssemblyInvalidPayload"],
- ["invalid-checksum-algorithm", "proto.frameAssemblyMetadataMismatch"],
- ["invalid-checksum-value", "proto.frameAssemblyMetadataMismatch"],
- ] as const)(
- "routes %s through service → owned facade → assembler typed fault",
- async (sessionId, reasonCode) => {
- const { service, workspacePath } = setupService();
- const scope = createZCodeAgentConnectionScope(service, {
- connectionId: `test-${sessionId}`,
- clientMode: "desktop-continuous",
- });
- try {
- await scope.service.helloConversationV4();
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "owned-consumer",
- appVersion: "4.0.0",
- });
- const candidates: ConversationTopicWireCandidate[] = [];
- const disposable = scope.service.onDynamicConversationFrame({ workspacePath })((wire) =>
- candidates.push(wire),
- );
- await scope.service.subscribeConversationV4({ workspacePath, sessionId });
- await vi.waitFor(() => expect(candidates).toHaveLength(1));
- const assembler = new TopicWireFrameAssembler(conversationTopicFrameSchema);
- expect(assembler.accept(candidates[0]!)).toMatchObject([
- { kind: "fault", fault: { reasonCode } },
- ]);
- disposable.dispose();
- } finally {
- await scope.dispose();
- await service.disposeAllAndWait();
- }
- },
- );
- it("records subscribe route and forwards exact topic/subscription/connection on unsubscribe", async () => {
- const { service, workspacePath, logPath } = setupService();
- try {
- const ack = await service.sendConversationCommandV4({
- workspacePath,
- envelope: {
- commandId: "cmd-1",
- clientId: "client-1",
- sessionId: "s1",
- type: "sendText",
- payload: { text: "hello" },
- issuedAt: 1700000000000,
- },
- });
- expect(ack).toEqual({
- commandId: "cmd-1",
- status: "accepted",
- revisionAtDecision: 3,
- result: {
- type: "inputAccepted",
- delivery: "startNow",
- inputId: "cmd-1",
- messageId: "msg-cmd-1",
- },
- });
- await service.subscribeConversationV4({
- workspacePath,
- sessionId: "s1",
- });
- await service.unsubscribeConversationV4({ workspacePath, subscriptionId: "sub-1" });
- const requests = readRequests(logPath);
- const commandRequest = requests.find((entry) => entry.method === "v4/command");
- expect(commandRequest?.params).toMatchObject({
- commandId: "cmd-1",
- type: "sendText",
- payload: { text: "hello" },
- });
- const unsubscribeRequest = requests.find(
- (entry) => entry.method === "v4/conversation/unsubscribe",
- );
- const subscribeRequest = requests.find(
- (entry) => entry.method === "v4/conversation/subscribe",
- );
- expect(unsubscribeRequest?.params).toEqual({
- topic: "conversation/s1",
- subscriptionId: "sub-1",
- connectionId: subscribeRequest?.params.connectionId,
- });
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("keeps automation mutation tools for desktop user turns in cron-owned sessions", async () => {
- const { service, workspacePath, logPath } = setupService();
- const repo = new TaskIndexRepo();
- try {
- await seedTaskIndex(repo, {
- meta: {
- taskId: "cron-session",
- traceId: "trace-cron-session",
- title: "cron session",
- workspacePath,
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- provider: "glm",
- cronAutomationId: "automation-cron",
- },
- });
- await service.sendConversationCommandV4({
- workspacePath,
- envelope: {
- commandId: "cmd-followup",
- clientId: "desktop-client",
- sessionId: "cron-session",
- type: "sendText",
- payload: { text: "再帮我建一个每5分钟提醒" },
- issuedAt: 1700000000000,
- },
- });
- const commandRequest = readRequests(logPath).find((entry) => entry.method === "v4/command");
- expect(commandRequest?.params).toMatchObject({
- commandId: "cmd-followup",
- type: "sendText",
- payload: {
- text: "再帮我建一个每5分钟提醒",
- },
- });
- expect(commandRequest?.params.payload).not.toHaveProperty("automationId");
- expect(commandRequest?.params.payload).not.toHaveProperty("toolDisallowlist");
- } finally {
- repo.close();
- await service.disposeAllAndWait();
- }
- });
- it("hides automation mutation tools only for the current automation execution turn", async () => {
- const { service, workspacePath, logPath } = setupService();
- try {
- await service.sendConversationCommandV4({
- workspacePath,
- envelope: {
- commandId: "cmd-automation-run",
- clientId: "desktop-client",
- sessionId: "cron-session",
- type: "sendText",
- payload: {
- text: "执行定时任务",
- automationId: "automation-cron",
- toolDisallowlist: ["ExistingDeniedTool"],
- },
- issuedAt: 1700000000000,
- },
- });
- const commandRequest = readRequests(logPath).find((entry) => entry.method === "v4/command");
- expect(commandRequest?.params.payload).toEqual({
- inputOrigin: "desktop",
- text: "执行定时任务",
- automationId: "automation-cron",
- toolDisallowlist: ["ExistingDeniedTool", "CronCreate", "CronUpdate", "CronDelete"],
- });
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("does not consult cron ownership metadata for a normal desktop user turn", async () => {
- const { service, workspacePath, logPath } = setupService();
- const getTaskMeta = vi
- .spyOn(TaskIndexRepo.prototype, "getTaskMeta")
- .mockRejectedValueOnce(new Error("task index unavailable"));
- try {
- await service.sendConversationCommandV4({
- workspacePath,
- envelope: {
- commandId: "cmd-ownership-unknown",
- clientId: "desktop-client",
- sessionId: "unknown-session",
- type: "sendText",
- payload: { text: "继续正常对话" },
- issuedAt: 1700000000000,
- },
- });
- const commandRequest = readRequests(logPath).find((entry) => entry.method === "v4/command");
- expect(commandRequest?.params).toMatchObject({
- commandId: "cmd-ownership-unknown",
- type: "sendText",
- payload: {
- text: "继续正常对话",
- },
- });
- expect(commandRequest?.params.payload).not.toHaveProperty("toolDisallowlist");
- expect(getTaskMeta).not.toHaveBeenCalled();
- } finally {
- getTaskMeta.mockRestore();
- await service.disposeAllAndWait();
- }
- });
- it("PV4-24 web-remote replayable 在 shared host 原样透传组合 rewind 命令", async () => {
- const { service, workspacePath, logPath, commandResolver } = setupService();
- const workspaceIdentity = "ssh://rewind-e2e/workspace";
- const scope = createZCodeAgentConnectionScope(service, {
- connectionId: "mobile-rewind",
- clientMode: "web-remote-replayable",
- });
- try {
- await scope.service.helloConversationV4();
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "mobile-rewind-client",
- appVersion: "4.0.0",
- });
- await scope.service.subscribeConversationV4({
- workspacePath,
- workspaceIdentity,
- sessionId: "s-mobile-rewind",
- });
- const ack = await scope.service.sendConversationCommandV4({
- workspacePath,
- workspaceIdentity,
- envelope: {
- commandId: "cmd-mobile-rewind",
- clientId: "mobile-rewind-client",
- sessionId: "s-mobile-rewind",
- type: "editUserQuery",
- baseRevision: 12,
- baseLogEpoch: "epoch-mobile",
- payload: {
- target: { rowId: 18, entityId: "msg-mobile-user" },
- newText: "mobile edited query",
- workspaceMode: "rewind",
- },
- issuedAt: 1700000000000,
- },
- });
- expect(ack).toMatchObject({
- commandId: "cmd-mobile-rewind",
- status: "accepted",
- });
- const requests = readRequests(logPath);
- expect(
- requests.find((entry) => entry.method === "v4/conversation/subscribe")?.params,
- ).toMatchObject({
- clientMode: "web-remote-replayable",
- connectionId: "mobile-rewind",
- topic: "conversation/s-mobile-rewind",
- });
- expect(requests.find((entry) => entry.method === "v4/command")?.params).toEqual({
- commandId: "cmd-mobile-rewind",
- clientId: "mobile-rewind-client",
- sessionId: "s-mobile-rewind",
- type: "editUserQuery",
- baseRevision: 12,
- baseLogEpoch: "epoch-mobile",
- payload: {
- target: { rowId: 18, entityId: "msg-mobile-user" },
- newText: "mobile edited query",
- workspaceMode: "rewind",
- },
- issuedAt: 1700000000000,
- });
- // 同一 workspaceIdentity 的 subscribe + command 复用同一 host child,
- // 手机端不单独创建 Agent runtime,relay/main 也不持有 rewind 状态。
- expect(commandResolver).toHaveBeenCalledTimes(1);
- } finally {
- await scope.dispose();
- await service.disposeAllAndWait();
- }
- });
- it("commands/query 经真实 child process wire 保序,且 64 cap/可信 scope 在 service 边界执行", async () => {
- const { service, workspacePath, logPath } = setupService();
- const commands = Array.from({ length: 64 }, (_, index) => ({
- sessionId: index === 0 ? null : "s1",
- commandId: `command-${index}`,
- }));
- const scope = createZCodeAgentConnectionScope(service, {
- connectionId: "query-wire",
- clientMode: "web-remote-replayable",
- });
- try {
- await expect(
- service.queryConversationCommandsV4({ workspacePath, commands: [commands[0]!] }),
- ).rejects.toThrow("fault.command.queryConnectionUntrusted");
- await scope.service.helloConversationV4();
- await scope.service.initializeConversationV4({
- kind: "clientHello",
- protocolVersion: 3,
- clientId: "query-wire-client",
- appVersion: "4.0.0",
- });
- const result = await scope.service.queryConversationCommandsV4({ workspacePath, commands });
- expect(result.results.map((item) => item.key.commandId)).toEqual(
- commands.map((key) => key.commandId),
- );
- expect(result.results.every((item) => item.result === "unknown")).toBe(true);
- await expect(
- scope.service.queryConversationCommandsV4({
- workspacePath,
- commands: [...commands, { sessionId: "s1", commandId: "command-64" }],
- }),
- ).rejects.toThrow();
- const requests = readRequests(logPath).filter(
- (entry) => entry.method === "v4/commands/query",
- );
- expect(requests).toHaveLength(1);
- expect(requests[0]?.params).toEqual({ commands });
- } finally {
- await scope.dispose();
- await service.disposeAllAndWait();
- }
- });
- it("forwards same-sub resync with the recorded exact route and client base", async () => {
- const { service, workspacePath, logPath } = setupService();
- try {
- const subscribed = await service.subscribeConversationV4({
- workspacePath,
- sessionId: "s-resync",
- });
- const result = await service.resyncConversationV4({
- workspacePath,
- subscriptionId: subscribed.ack.subscriptionId,
- base: { logEpoch: "epoch-1", seq: 0 },
- forceSnapshot: true,
- });
- expect(result.ack).toEqual({
- subscriptionId: subscribed.ack.subscriptionId,
- mode: "snapshot",
- logEpoch: "epoch-1",
- });
- const requests = readRequests(logPath);
- const subscribeRequest = requests.find(
- (entry) =>
- entry.method === "v4/conversation/subscribe" &&
- entry.params.topic === "conversation/s-resync",
- );
- const resyncRequest = requests.find((entry) => entry.method === "v4/conversation/resync");
- expect(resyncRequest?.params).toEqual({
- topic: "conversation/s-resync",
- connectionId: subscribeRequest?.params.connectionId,
- subscriptionId: subscribed.ack.subscriptionId,
- base: { logEpoch: "epoch-1", seq: 0 },
- forceSnapshot: true,
- });
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("迟到 unsubscribe response 不删除同 key 的新 route", async () => {
- const { service, workspacePath, logPath } = setupService();
- try {
- const first = await service.subscribeConversationV4({
- workspacePath,
- sessionId: "delayed-unsubscribe",
- });
- const staleUnsubscribe = service.unsubscribeConversationV4({
- workspacePath,
- subscriptionId: first.ack.subscriptionId,
- });
- await vi.waitFor(() => {
- expect(
- readRequests(logPath).some((entry) => entry.method === "v4/conversation/unsubscribe"),
- ).toBe(true);
- });
- const replacement = await service.subscribeConversationV4({
- workspacePath,
- sessionId: "delayed-unsubscribe",
- });
- expect(replacement.ack.subscriptionId).toBe(first.ack.subscriptionId);
- await staleUnsubscribe;
- await expect(
- service.resyncConversationV4({
- workspacePath,
- subscriptionId: replacement.ack.subscriptionId,
- base: { logEpoch: replacement.ack.logEpoch, seq: 0 },
- }),
- ).resolves.toMatchObject({ ack: { subscriptionId: replacement.ack.subscriptionId } });
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("route 换代后迟到 resync response 必须拒绝 stale success", async () => {
- const { service, workspacePath, logPath } = setupService();
- try {
- const first = await service.subscribeConversationV4({
- workspacePath,
- sessionId: "delayed-resync",
- });
- const staleResync = service.resyncConversationV4({
- workspacePath,
- subscriptionId: first.ack.subscriptionId,
- base: { logEpoch: first.ack.logEpoch, seq: 0 },
- });
- await vi.waitFor(() => {
- expect(
- readRequests(logPath).some((entry) => entry.method === "v4/conversation/resync"),
- ).toBe(true);
- });
- const replacement = await service.subscribeConversationV4({
- workspacePath,
- sessionId: "delayed-resync",
- });
- expect(replacement.ack.subscriptionId).toBe(first.ack.subscriptionId);
- await expect(staleResync).rejects.toThrow("fault.subscription.notOwned");
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("disposeWorkspace runtime invalidation 后三类 pre-restart listener 均接收新 runtime initial", async () => {
- const { service, workspacePath } = setupService();
- try {
- const frames: ConversationTopicWireCandidate[] = [];
- const indexFrames: SessionsIndexTopicWireCandidate[] = [];
- const configFrames: WorkspaceConfigTopicWireCandidate[] = [];
- const disposables = [
- service.onDynamicConversationFrame({ workspacePath })((frame) => frames.push(frame)),
- service.onDynamicSessionsIndexFrame({ workspacePath })((frame) => indexFrames.push(frame)),
- service.onDynamicWorkspaceConfigFrame({ workspacePath })((frame) =>
- configFrames.push(frame),
- ),
- ];
- await Promise.all([
- service.subscribeConversationV4({ workspacePath, sessionId: "before-restart" }),
- service.subscribeSessionsIndexV4({ workspacePath }),
- service.subscribeWorkspaceConfigV4({ workspacePath }),
- ]);
- await vi.waitFor(() => {
- expect(frames).toHaveLength(1);
- expect(indexFrames).toHaveLength(1);
- expect(configFrames).toHaveLength(1);
- });
- await service.disposeWorkspace({ workspacePath });
- await Promise.all([
- service.subscribeConversationV4({ workspacePath, sessionId: "after-restart" }),
- service.subscribeSessionsIndexV4({ workspacePath }),
- service.subscribeWorkspaceConfigV4({ workspacePath }),
- ]);
- await vi.waitFor(() => {
- expect(frames).toHaveLength(2);
- expect(indexFrames).toHaveLength(2);
- expect(configFrames).toHaveLength(2);
- });
- expect(frames.map((frame) => frame.topic)).toEqual([
- "conversation/before-restart",
- "conversation/after-restart",
- ]);
- expect(indexFrames.every((frame) => frame.topic.startsWith("sessions-index/"))).toBe(true);
- expect(configFrames.every((frame) => frame.topic.startsWith("workspace-config/"))).toBe(true);
- for (const disposable of disposables) disposable.dispose();
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("conversation/sessions-index/workspace-config 相同裸 subId 各自使用记录的精确 route", async () => {
- const { service, workspacePath, logPath } = setupService();
- try {
- const conversation = await service.subscribeConversationV4({
- workspacePath,
- sessionId: "s1",
- });
- const sessionsIndex = await service.subscribeSessionsIndexV4({
- workspacePath,
- });
- const workspaceConfig = await service.subscribeWorkspaceConfigV4({
- workspacePath,
- });
- expect([
- conversation.ack.subscriptionId,
- sessionsIndex.ack.subscriptionId,
- workspaceConfig.ack.subscriptionId,
- ]).toEqual(["sub-1", "sub-1", "sub-1"]);
- await service.unsubscribeConversationV4({
- workspacePath,
- subscriptionId: "sub-1",
- });
- await service.unsubscribeSessionsIndexV4({
- workspacePath,
- subscriptionId: "sub-1",
- });
- await service.unsubscribeWorkspaceConfigV4({
- workspacePath,
- subscriptionId: "sub-1",
- });
- const requests = readRequests(logPath);
- const subscribeRequests = requests.filter(
- (entry) => entry.method === "v4/conversation/subscribe",
- );
- const unsubscribeRequests = requests.filter(
- (entry) => entry.method === "v4/conversation/unsubscribe",
- );
- expect(unsubscribeRequests.map((entry) => entry.params)).toEqual(
- subscribeRequests.map((entry) => ({
- topic: entry.params.topic,
- subscriptionId: "sub-1",
- connectionId: entry.params.connectionId,
- })),
- );
- } finally {
- await service.disposeAllAndWait();
- }
- });
- it("trusted unsubscribe route 不能跨越调用方法的 topic 域", async () => {
- const { service, workspacePath, logPath } = setupService();
- try {
- const config = await service.subscribeWorkspaceConfigV4({
- workspacePath,
- });
- const configSubscribe = readRequests(logPath).find(
- (entry) =>
- entry.method === "v4/conversation/subscribe" &&
- String(entry.params.topic).startsWith("workspace-config/"),
- );
- expect(configSubscribe).toBeDefined();
- await service.unsubscribeConversationV4({
- workspacePath,
- subscriptionId: config.ack.subscriptionId,
- __zcodeTrustedV4UnsubscribeRoute: {
- topic: configSubscribe?.params.topic,
- connectionId: configSubscribe?.params.connectionId,
- },
- } as never);
- expect(
- readRequests(logPath).filter((entry) => entry.method === "v4/conversation/unsubscribe"),
- ).toHaveLength(0);
- } finally {
- await service.disposeAllAndWait();
- }
- });
- });
|