| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767 |
- import { PassThrough } from "node:stream";
- import { describe, expect, it, vi } from "vitest";
- import { zcodeProtocolMethods, type ZCodeProtocolMessage } from "@zcode/shared";
- import { ZCodeProtocolNdjsonConnection } from "../src/zcode-protocol/transport.js";
- describe("ZCodeProtocolNdjsonConnection", () => {
- it("stdout 背压时丢弃观测批次,业务响应仍可写入", () => {
- const output = new PassThrough({ highWaterMark: 1 });
- const connection = new ZCodeProtocolNdjsonConnection({
- input: new PassThrough(),
- output,
- handleMessage: async () => undefined,
- });
- output.write("x".repeat(256 * 1024));
- const before = output.writableLength;
- connection.send({
- method: "process/networkRequests",
- params: { captureId: "one", records: [], dropped: 0 },
- });
- expect(output.writableLength).toBe(before);
- connection.send({ id: 1, result: {} });
- expect(output.writableLength).toBeGreaterThan(before);
- output.destroy();
- });
- it("网络观测控制不等待挂起业务请求,并保留启停顺序", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const controls: unknown[] = [];
- const connection = new ZCodeProtocolNdjsonConnection({
- input,
- output,
- handleMessage: async (message) => {
- if ("method" in message && message.method === "process/networkCapture") {
- controls.push(message.params);
- return undefined;
- }
- return new Promise(() => {});
- },
- });
- connection.start();
- input.write(frame({ id: "busy", method: "workspace/readState" }));
- input.write(frame({ method: "process/networkCapture", params: { captureId: "one" } }));
- input.write(frame({ method: "process/networkCapture", params: { captureId: null } }));
- await vi.waitFor(() => expect(controls).toEqual([{ captureId: "one" }, { captureId: null }]));
- input.end();
- await connection.waitForClose();
- });
- it("bounds EOF drain even while a handler never settles", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const closed = vi.fn();
- const connection = new ZCodeProtocolNdjsonConnection({
- input,
- output,
- handleMessage: () => new Promise(() => {}),
- onTransportClosed: closed,
- });
- connection.start();
- input.end(frame({ id: "hung", method: "workspace/readState", params: {} }));
- await connection.waitForClose();
- expect(closed).toHaveBeenCalledTimes(1);
- expect(input.listenerCount("data")).toBe(0);
- }, 1_000);
- it("writes response, every physical notification, then commits the reservation", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const order: string[] = [];
- output.on("data", (chunk: Buffer) => {
- const value = JSON.parse(chunk.toString("utf8")) as {
- id?: string;
- params?: { fragmentIndex?: number };
- };
- order.push(value.id ? "response" : `wire-${value.params?.fragmentIndex}`);
- });
- const connection = new ZCodeProtocolNdjsonConnection({
- handleMessage: async (message) =>
- "id" in message && "method" in message
- ? { id: message.id, result: { ack: {} } }
- : undefined,
- input,
- output,
- takePostResponseBatch: () => ({
- messages: [0, 1].map((fragmentIndex) => ({
- method: "v4/conversation/frame",
- params: { kind: "fragment", fragmentIndex },
- })),
- commit: () => {
- order.push("commit");
- return true;
- },
- }),
- });
- connection.start();
- input.end(frame({ id: "physical-order", method: "v4/conversation/subscribe", params: {} }));
- await connection.waitForClose();
- expect(order).toEqual(["response", "wire-0", "wire-1", "commit"]);
- });
- it("does not commit when a physical notification write throws", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const originalWrite = output.write.bind(output);
- let writes = 0;
- output.write = ((chunk: string | Uint8Array) => {
- writes += 1;
- if (writes === 3) throw new Error("physical write failed");
- return originalWrite(chunk);
- }) as typeof output.write;
- const commit = vi.fn(() => true);
- const connection = new ZCodeProtocolNdjsonConnection({
- handleMessage: async (message) =>
- "id" in message && "method" in message
- ? { id: message.id, result: { ack: {} } }
- : undefined,
- input,
- output,
- takePostResponseBatch: () => ({
- messages: [
- { method: "v4/conversation/frame", params: { fragmentIndex: 0 } },
- { method: "v4/conversation/frame", params: { fragmentIndex: 1 } },
- ],
- commit,
- }),
- });
- connection.start();
- input.end(frame({ id: "physical-fail", method: "v4/conversation/subscribe", params: {} }));
- await expect(connection.waitForClose()).rejects.toThrow("physical write failed");
- expect(commit).not.toHaveBeenCalled();
- });
- it("commits after queue admission, then async EPIPE terminalizes the connection epoch", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- let written = "";
- output.on("data", (chunk: Buffer) => {
- written += chunk.toString("utf8");
- });
- const commit = vi.fn(() => true);
- const clearPostResponseMessages = vi.fn();
- const takePostResponseBatch = vi.fn(() => ({
- messages: [{ method: "v4/conversation/frame", params: { kind: "complete" } }],
- commit,
- }));
- const connection = new ZCodeProtocolNdjsonConnection({
- clearPostResponseMessages,
- handleMessage: async (message) =>
- "id" in message && "method" in message
- ? { id: message.id, result: { ack: { subscriptionId: "sub-epoch" } } }
- : undefined,
- input,
- output,
- takePostResponseBatch,
- });
- connection.start();
- input.write(frame({ id: "async-epipe", method: "v4/conversation/subscribe", params: {} }));
- await vi.waitFor(() => expect(commit).toHaveBeenCalledTimes(1));
- const acceptedBytes = written;
- output.emit("error", new Error("async write EPIPE"));
- await expect(connection.waitForClose()).rejects.toThrow("async write EPIPE");
- connection.send({ method: "v4/conversation/frame", params: { shouldNotWrite: true } });
- input.write(
- frame({ id: "new-epoch-required", method: "v4/conversation/subscribe", params: {} }),
- );
- await new Promise((resolve) => setImmediate(resolve));
- expect(written).toBe(acceptedBytes);
- expect(takePostResponseBatch).toHaveBeenCalledTimes(1);
- expect(clearPostResponseMessages).toHaveBeenCalledTimes(1);
- input.destroy();
- });
- it("writes the response before request-scoped initial notifications from one input chunk", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- let written = "";
- output.on("data", (chunk: Buffer) => {
- written += chunk.toString("utf8");
- });
- const connection = new ZCodeProtocolNdjsonConnection({
- handleMessage: async (message) => {
- if (!("id" in message) || !("method" in message)) return undefined;
- return {
- id: message.id,
- result: {
- ack: { subscriptionId: "sub-1", mode: "snapshot", logEpoch: "epoch-1" },
- },
- };
- },
- input,
- output,
- takePostResponseMessages: (requestId) =>
- requestId === "subscribe-1"
- ? [
- {
- method: "v4/conversation/frame",
- params: {
- topic: "conversation/session-1",
- subscriptionId: "sub-1",
- fromSeq: 0,
- toSeq: 0,
- sentAt: 1,
- payload: { kind: "deltas", deltas: [] },
- },
- },
- ]
- : [],
- });
- connection.start();
- input.end(
- frame({
- id: "subscribe-1",
- method: "v4/conversation/subscribe",
- params: { topic: "conversation/session-1" },
- }),
- );
- await connection.waitForClose();
- const lines = written
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line) as Record<string, unknown>);
- expect(lines).toEqual([
- {
- id: "subscribe-1",
- result: {
- ack: { subscriptionId: "sub-1", mode: "snapshot", logEpoch: "epoch-1" },
- },
- },
- {
- method: "v4/conversation/frame",
- params: expect.objectContaining({
- topic: "conversation/session-1",
- subscriptionId: "sub-1",
- }),
- },
- ]);
- });
- it("writes only the response line when a subscribe has no initial frame", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- let written = "";
- output.on("data", (chunk: Buffer) => {
- written += chunk.toString("utf8");
- });
- const connection = new ZCodeProtocolNdjsonConnection({
- handleMessage: async (message) =>
- "id" in message && "method" in message
- ? {
- id: message.id,
- result: {
- ack: { subscriptionId: "sub-2", mode: "resume", logEpoch: "epoch-1" },
- },
- }
- : undefined,
- input,
- output,
- takePostResponseMessages: () => [],
- });
- connection.start();
- input.end(
- frame({
- id: "subscribe-2",
- method: "v4/conversation/subscribe",
- params: { topic: "conversation/session-1" },
- }),
- );
- await connection.waitForClose();
- expect(written.trim().split("\n")).toHaveLength(1);
- expect(JSON.parse(written.trim())).toEqual({
- id: "subscribe-2",
- result: {
- ack: { subscriptionId: "sub-2", mode: "resume", logEpoch: "epoch-1" },
- },
- });
- });
- it("clears post-response messages when queued request processing rejects", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const clearPostResponseMessages = vi.fn();
- const connection = new ZCodeProtocolNdjsonConnection({
- clearPostResponseMessages,
- handleMessage: async () => {
- throw new Error("request failed");
- },
- input,
- output,
- });
- connection.start();
- input.end(
- frame({
- id: "request-failure",
- method: "v4/conversation/subscribe",
- params: { topic: "conversation/session-1" },
- }),
- );
- await expect(connection.waitForClose()).rejects.toThrow("request failed");
- expect(clearPostResponseMessages).toHaveBeenCalledTimes(1);
- });
- it("clears post-response messages when a bypassed handler rejects", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const clearPostResponseMessages = vi.fn();
- const connection = new ZCodeProtocolNdjsonConnection({
- clearPostResponseMessages,
- handleMessage: async () => {
- throw new Error("bypass failed");
- },
- input,
- output,
- });
- connection.start();
- input.write(frame({ id: "server-1", result: {} }));
- await expect(connection.waitForClose()).rejects.toThrow("bypass failed");
- expect(clearPostResponseMessages).toHaveBeenCalledTimes(1);
- });
- it("clears on output EPIPE and discards an in-flight subscribe outbox that completes late", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const clearPostResponseMessages = vi.fn();
- const takePostResponseMessages = vi.fn(() => [
- {
- method: "v4/conversation/frame",
- params: { subscriptionId: "sub-late" },
- },
- ]);
- let resolveStarted!: () => void;
- const started = new Promise<void>((resolve) => {
- resolveStarted = resolve;
- });
- let resolveResponse!: (value: {
- id: string;
- result: { ack: { subscriptionId: string; mode: "snapshot"; logEpoch: string } };
- }) => void;
- const response = new Promise<{
- id: string;
- result: { ack: { subscriptionId: string; mode: "snapshot"; logEpoch: string } };
- }>((resolve) => {
- resolveResponse = resolve;
- });
- let written = "";
- output.on("data", (chunk: Buffer) => {
- written += chunk.toString("utf8");
- });
- const connection = new ZCodeProtocolNdjsonConnection({
- clearPostResponseMessages,
- handleMessage: async () => {
- resolveStarted();
- return await response;
- },
- input,
- output,
- takePostResponseMessages,
- });
- connection.start();
- input.write(
- frame({
- id: "subscribe-late",
- method: "v4/conversation/subscribe",
- params: { topic: "conversation/session-1" },
- }),
- );
- await started;
- const closed = connection.waitForClose().then(
- () => null,
- (error: Error) => error,
- );
- output.emit("error", new Error("write EPIPE"));
- expect((await closed)?.message).toBe("write EPIPE");
- expect(clearPostResponseMessages).toHaveBeenCalledTimes(1);
- resolveResponse({
- id: "subscribe-late",
- result: {
- ack: { subscriptionId: "sub-late", mode: "snapshot", logEpoch: "epoch-1" },
- },
- });
- await vi.waitFor(() => expect(takePostResponseMessages).toHaveBeenCalledWith("subscribe-late"));
- expect(written).toBe("");
- input.destroy();
- });
- it("lets session/stop bypass a long-running request in the serial queue", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const handledMethods: string[] = [];
- const responses: unknown[] = [];
- let resolveStopHandled!: () => void;
- const stopHandled = new Promise<void>((resolve) => {
- resolveStopHandled = resolve;
- });
- output.on("data", (chunk: Buffer) => {
- for (const line of chunk.toString("utf8").trim().split("\n")) {
- if (line) {
- responses.push(JSON.parse(line));
- }
- }
- });
- const connection = new ZCodeProtocolNdjsonConnection({
- handleMessage: async (message) => {
- if (!("method" in message)) {
- return undefined;
- }
- handledMethods.push(message.method);
- if (message.method === zcodeProtocolMethods.sessionCompact) {
- return await new Promise<never>(() => undefined);
- }
- if (message.method === zcodeProtocolMethods.sessionStop && "id" in message) {
- resolveStopHandled();
- return { id: message.id, result: {} };
- }
- return undefined;
- },
- input,
- output,
- });
- connection.start();
- input.write(
- [
- frame({
- id: 1,
- method: zcodeProtocolMethods.sessionCompact,
- params: { sessionId: "sess_1" },
- }),
- frame({
- id: 2,
- method: zcodeProtocolMethods.sessionStop,
- params: { sessionId: "sess_1" },
- }),
- ].join(""),
- );
- const stopResult = await Promise.race([
- stopHandled.then(() => "handled" as const),
- new Promise<"blocked">((resolve) => setTimeout(() => resolve("blocked"), 30)),
- ]);
- expect(stopResult).toBe("handled");
- expect(handledMethods).toEqual([
- zcodeProtocolMethods.sessionCompact,
- zcodeProtocolMethods.sessionStop,
- ]);
- await vi.waitFor(() => {
- expect(responses).toContainEqual({ id: 2, result: {} });
- });
- });
- it("lets workspace cancellation abort an active model request before the serial queue advances", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const responses: unknown[] = [];
- let releaseGenerate!: () => void;
- const generateReleased = new Promise<void>((resolve) => {
- releaseGenerate = resolve;
- });
- let resolveCancelHandled!: () => void;
- const cancelHandled = new Promise<void>((resolve) => {
- resolveCancelHandled = resolve;
- });
- let generateActive = false;
- let generateCount = 0;
- let activeGenerateCount = 0;
- let maxActiveGenerateCount = 0;
- let cancelSawActiveGenerate = false;
- output.on("data", (chunk: Buffer) => {
- for (const line of chunk.toString("utf8").trim().split("\n")) {
- if (line) {
- responses.push(JSON.parse(line));
- }
- }
- });
- const connection = new ZCodeProtocolNdjsonConnection({
- handleMessage: async (message) => {
- if (!("method" in message) || !("id" in message)) {
- return undefined;
- }
- if (message.method === zcodeProtocolMethods.workspaceGenerateText) {
- generateCount += 1;
- generateActive = true;
- activeGenerateCount += 1;
- maxActiveGenerateCount = Math.max(maxActiveGenerateCount, activeGenerateCount);
- if (generateCount === 1) {
- await generateReleased;
- }
- activeGenerateCount -= 1;
- generateActive = false;
- return { id: message.id, result: { text: `generate-${generateCount}` } };
- }
- if (message.method === zcodeProtocolMethods.workspaceCancelGenerateText) {
- cancelSawActiveGenerate = generateActive;
- releaseGenerate();
- resolveCancelHandled();
- return {
- id: message.id,
- result: { operationId: "workspace-model-1", cancelled: true },
- };
- }
- return { id: message.id, result: {} };
- },
- input,
- output,
- });
- connection.start();
- input.write(
- [
- frame({
- id: 1,
- method: zcodeProtocolMethods.workspaceGenerateText,
- params: {
- operationId: "workspace-model-1",
- workspace: { workspaceKey: "/workspace/app", workspacePath: "/workspace/app" },
- },
- }),
- frame({
- id: 2,
- method: zcodeProtocolMethods.workspaceCancelGenerateText,
- params: { operationId: "workspace-model-1" },
- }),
- frame({
- id: 3,
- method: zcodeProtocolMethods.workspaceGenerateText,
- params: {
- operationId: "workspace-model-2",
- workspace: { workspaceKey: "/workspace/app", workspacePath: "/workspace/app" },
- },
- }),
- ].join(""),
- );
- const cancelResult = await Promise.race([
- cancelHandled.then(() => "handled" as const),
- new Promise<"blocked">((resolve) => setTimeout(() => resolve("blocked"), 100)),
- ]);
- expect(cancelResult).toBe("handled");
- expect(cancelSawActiveGenerate).toBe(true);
- input.end();
- await connection.waitForClose();
- expect(generateCount).toBe(2);
- expect(maxActiveGenerateCount).toBe(1);
- expect(responses).toContainEqual({
- id: 2,
- result: { operationId: "workspace-model-1", cancelled: true },
- });
- expect(responses).toContainEqual({ id: 3, result: { text: "generate-2" } });
- });
- it("lets protocol responses bypass a long-running request with later requests queued", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const handled: unknown[] = [];
- let resolveResponseHandled!: () => void;
- const responseHandled = new Promise<void>((resolve) => {
- resolveResponseHandled = resolve;
- });
- const connection = new ZCodeProtocolNdjsonConnection({
- handleMessage: async (message) => {
- handled.push(message);
- if ("method" in message && message.method === zcodeProtocolMethods.sessionCompact) {
- return await new Promise<never>(() => undefined);
- }
- if ("result" in message && "id" in message) {
- resolveResponseHandled();
- }
- return undefined;
- },
- input,
- output,
- });
- connection.start();
- input.write(
- [
- frame({
- id: 1,
- method: zcodeProtocolMethods.sessionCompact,
- params: { sessionId: "sess_1" },
- }),
- frame({
- id: 2,
- method: zcodeProtocolMethods.sessionRead,
- params: { sessionId: "sess_1" },
- }),
- frame({
- id: "server-1",
- result: { headersApplied: true },
- }),
- ].join(""),
- );
- const responseResult = await Promise.race([
- responseHandled.then(() => "handled" as const),
- new Promise<"blocked">((resolve) => setTimeout(() => resolve("blocked"), 30)),
- ]);
- expect(responseResult).toBe("handled");
- expect(handled).toHaveLength(2);
- });
- it("waits for a queued session/send to start before handling session/stop", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- let releaseCompact!: () => void;
- const compactReleased = new Promise<void>((resolve) => {
- releaseCompact = resolve;
- });
- let releaseSend!: () => void;
- const sendReleased = new Promise<void>((resolve) => {
- releaseSend = resolve;
- });
- let resolveStopHandled!: () => void;
- const stopHandled = new Promise<void>((resolve) => {
- resolveStopHandled = resolve;
- });
- let sendActive = false;
- let stopSawActiveSend = false;
- const connection = new ZCodeProtocolNdjsonConnection({
- handleMessage: async (message) => {
- if (!("method" in message)) {
- return undefined;
- }
- if (message.method === zcodeProtocolMethods.sessionCompact) {
- await compactReleased;
- }
- if (message.method === zcodeProtocolMethods.sessionSend) {
- sendActive = true;
- await sendReleased;
- sendActive = false;
- }
- if (message.method === zcodeProtocolMethods.sessionStop) {
- stopSawActiveSend = sendActive;
- releaseSend();
- resolveStopHandled();
- }
- return "id" in message ? { id: message.id, result: {} } : undefined;
- },
- input,
- output,
- });
- connection.start();
- input.write(
- [
- frame({
- id: 1,
- method: zcodeProtocolMethods.sessionCompact,
- params: { sessionId: "sess_blocking" },
- }),
- frame({
- id: 2,
- method: zcodeProtocolMethods.sessionSend,
- params: { sessionId: "sess_target", inputId: "input_target", content: "run" },
- }),
- frame({
- id: 3,
- method: zcodeProtocolMethods.sessionStop,
- params: { sessionId: "sess_target" },
- }),
- ].join(""),
- );
- await new Promise<void>((resolve) => setImmediate(resolve));
- expect(stopSawActiveSend).toBe(false);
- releaseCompact();
- await stopHandled;
- expect(stopSawActiveSend).toBe(true);
- input.end();
- await connection.waitForClose();
- });
- // M5 收口:prompt/enhance 全簇删除后,请求方法级 bypass 名单只剩 session/stop;
- // 本用例保住「长请求占住串行队列时 stop 仍可进入 server」这一传输层语义。
- it("lets session/stop bypass a long-running request in the serial queue", async () => {
- const input = new PassThrough();
- const output = new PassThrough();
- const responses: unknown[] = [];
- let resolveStopHandled!: () => void;
- const stopHandled = new Promise<void>((resolve) => {
- resolveStopHandled = resolve;
- });
- output.on("data", (chunk: Buffer) => {
- for (const line of chunk.toString("utf8").trim().split("\n")) {
- if (line) {
- responses.push(JSON.parse(line));
- }
- }
- });
- const connection = new ZCodeProtocolNdjsonConnection({
- handleMessage: async (message) => {
- if (!("method" in message)) {
- return undefined;
- }
- if (message.method === zcodeProtocolMethods.sessionCompact) {
- return await new Promise<never>(() => undefined);
- }
- if (message.method === zcodeProtocolMethods.sessionStop && "id" in message) {
- resolveStopHandled();
- return {
- id: message.id,
- result: {},
- };
- }
- return undefined;
- },
- input,
- output,
- });
- connection.start();
- input.write(
- [
- frame({
- id: 1,
- method: zcodeProtocolMethods.sessionCompact,
- params: { sessionId: "sess_1" },
- }),
- frame({
- id: 2,
- method: zcodeProtocolMethods.sessionStop,
- params: { sessionId: "sess_1" },
- }),
- ].join(""),
- );
- const stopResult = await Promise.race([
- stopHandled.then(() => "handled" as const),
- new Promise<"blocked">((resolve) => setTimeout(() => resolve("blocked"), 30)),
- ]);
- expect(stopResult).toBe("handled");
- await vi.waitFor(() => {
- expect(responses).toContainEqual({
- id: 2,
- result: {},
- });
- });
- });
- });
- function frame(message: ZCodeProtocolMessage): string {
- return `${JSON.stringify(message)}\n`;
- }
|