| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764 |
- 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/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/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", () => {
- 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>;
- 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");
- writeFileSync(scriptPath, V4_FAKE_AGENT);
- const commandResolver = vi.fn(() => ({
- command: process.execPath,
- args: [scriptPath, logPath],
- }));
- const service = createZCodeAgentService({
- commandResolver,
- 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 };
- }
- 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 repo.syncTaskMeta({
- 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("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 repo.syncTaskMeta({
- 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 repo.syncTaskMeta({
- 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 repo.syncTaskMeta({
- 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();
- }
- });
- });
|