| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- import { SessionLifecycleController } from "../src/session-lifecycle/index.js";
- import {
- applyConversationDeltas,
- type ConversationSnapshot,
- } from "@zcode/shared/zcode-protocol-v4";
- import { describe, expect, it, vi } from "vitest";
- import { ConversationV4Gateway } from "../src/zcode-protocol-v4/v4-gateway.js";
- import type { ConversationTopicFrame } from "@zcode/shared/zcode-protocol-v4";
- function deferred() {
- let resolve!: () => void;
- let reject!: (error: Error) => void;
- const promise = new Promise<void>((yes, no) => {
- resolve = yes;
- reject = no;
- });
- return { promise, resolve, reject };
- }
- function setup(
- lookupTranscriptCommand?: () => {
- commandId: string;
- status: "accepted";
- revisionAtDecision: number;
- },
- retainSessionRuntime?: () => Promise<() => void>,
- ) {
- const gate = deferred();
- const frames: ConversationTopicFrame[] = [];
- let observed: ConversationSnapshot | undefined;
- const execute = vi.fn();
- const resume = vi.fn(async () => {
- await gate.promise;
- return { status: "resumed" as const, persistedMessages: [] };
- });
- const read = vi.fn(async () => []);
- const loadPersistedEvents = vi.fn(async () => ({
- events: [],
- synthesized: true,
- sourceEventSeq: 0,
- }));
- const gateway = new ConversationV4Gateway({
- sessionExists: () => false,
- executeCommand: execute,
- lookupTranscriptCommand,
- retainSessionRuntime,
- loadPersistedMessages: read,
- resumePersistedSession: resume,
- loadPersistedEvents,
- emitWireFrame: (wire) => {
- if (wire.kind === "complete") {
- const frame = wire.frame as ConversationTopicFrame;
- frames.push(frame);
- if (frame.payload.kind === "snapshot") observed = frame.payload.snapshot;
- else if (observed)
- observed = {
- ...applyConversationDeltas(observed, frame.payload.deltas),
- seq: frame.toSeq,
- };
- }
- },
- });
- const params = {
- topic: "conversation/history",
- connectionId: "desktop",
- clientMode: "desktop-continuous",
- historyFirst: true,
- };
- const snapshot = () => observed;
- return {
- gateway,
- gate,
- frames,
- execute,
- resume,
- read,
- params,
- snapshot,
- loadPersistedEvents,
- seed: (frame: ConversationTopicFrame | null) => {
- if (frame?.payload.kind === "snapshot") observed = frame.payload.snapshot;
- },
- };
- }
- function setupOwned() {
- const fixture = setup();
- const activate = vi.fn(
- async (_messages: import("@zcode/contracts").MessageWithParts[]) => undefined,
- );
- let active = false;
- const lifecycle = new SessionLifecycleController({
- exists: async () => true,
- validateActivation: async () => {},
- hasRuntime: () => active,
- assertCanStop: () => {},
- activate: async () => {
- const messages = await fixture.gateway.prepareSessionHistory("history");
- await activate(messages);
- active = true;
- },
- release: async (_id, failed) => {
- active = false;
- if (!failed) await fixture.gateway.releaseSessionProjection("history");
- },
- });
- fixture.resume.mockImplementation(async () => {
- await lifecycle.ensureReady("history");
- return { status: "resumed", persistedMessages: [] };
- });
- return { ...fixture, lifecycle, activate };
- }
- describe("history-first cold subscription", () => {
- it.each(["desktop-continuous", "web-remote-replayable"])(
- "%s reads history while an earlier legacy title handoff is waiting for activation",
- async (clientMode) => {
- const { gateway, gate, resume, read, execute, params, seed, snapshot } = setup();
- const workspace = { workspacePath: "/project", workspaceKey: "/project" };
- const rename = gateway.handleCommand({
- sessionId: "history",
- commandId: "legacy-title",
- clientId: "host",
- issuedAt: 1,
- type: "renameSession",
- payload: { title: "Migrated title" },
- workspace,
- });
- try {
- await vi.waitFor(() => expect(resume).toHaveBeenCalledTimes(1));
- let opened = false;
- const history = gateway.subscribe({ ...params, clientMode, workspace }).then((result) => {
- seed(result.initialFrame);
- opened = true;
- });
- // 保持激活屏障关闭,证明历史首帧不等待先到的迁移命令。
- await vi.waitFor(() => expect(opened).toBe(true));
- await history;
- expect(snapshot()?.runtimeInitialization?.status).toBe("initializing");
- expect(execute).not.toHaveBeenCalled();
- let legacyOpened = false;
- const legacy = gateway
- .subscribe({ ...params, connectionId: "legacy", historyFirst: false })
- .then(() => {
- legacyOpened = true;
- });
- await new Promise<void>((resolve) => setImmediate(resolve));
- expect(legacyOpened).toBe(false);
- gate.resolve();
- await rename;
- await legacy;
- expect(snapshot()?.runtimeInitialization?.status).toBe("ready");
- expect(resume).toHaveBeenCalledTimes(1);
- expect(read).toHaveBeenCalledTimes(1);
- expect(execute).toHaveBeenCalledTimes(1);
- } finally {
- gate.resolve();
- await rename;
- gateway.dispose();
- }
- },
- );
- it("retries a failed owned activation without waiting on the old failed publisher", async () => {
- const { gateway, lifecycle, activate, params, seed, snapshot } = setupOwned();
- activate.mockRejectedValueOnce(new Error("startup failed"));
- await expect(lifecycle.ensureReady("history")).rejects.toThrow("startup failed");
- await lifecycle.ensureReady("history");
- expect(activate).toHaveBeenCalledTimes(2);
- seed((await gateway.subscribe(params)).initialFrame);
- await vi.waitFor(() => expect(snapshot()?.runtimeInitialization?.status).toBe("ready"));
- gateway.dispose();
- });
- it("rejects late subscription and command admission while the session is stopping", async () => {
- const executeCommand = vi.fn();
- const gateway = new ConversationV4Gateway({
- sessionExists: () => true,
- isSessionStopping: () => true,
- executeCommand,
- emitWireFrame: () => {},
- });
- await expect(
- gateway.subscribe({
- topic: "conversation/history",
- connectionId: "desktop",
- clientMode: "desktop-continuous",
- }),
- ).rejects.toThrow("fault.sessionLifecycle.stopping");
- await expect(
- gateway.handleCommand({
- sessionId: "history",
- commandId: "late-rename",
- clientId: "desktop",
- issuedAt: 1,
- type: "renameSession",
- payload: { title: "late" },
- expectedRevision: 0,
- }),
- ).resolves.toMatchObject({ status: "failed" });
- expect(executeCommand).not.toHaveBeenCalled();
- gateway.dispose();
- });
- it("waits for old final hydration before releasing the publisher", async () => {
- const { gateway, lifecycle, loadPersistedEvents } = setupOwned();
- const hydrationEntered = deferred();
- const hydrationGate = deferred();
- loadPersistedEvents
- .mockImplementationOnce(async () => ({ events: [], synthesized: true, sourceEventSeq: 0 }))
- .mockImplementationOnce(async () => {
- hydrationEntered.resolve();
- await hydrationGate.promise;
- return { events: [], synthesized: true, sourceEventSeq: 0 };
- });
- await lifecycle.ensureReady("history");
- await hydrationEntered.promise;
- let released = false;
- const release = gateway.releaseSessionProjection("history").then(() => {
- released = true;
- });
- await Promise.resolve();
- expect(released).toBe(false);
- hydrationGate.resolve();
- await release;
- expect(released).toBe(true);
- gateway.dispose();
- });
- it("opens fresh history during an externally owned restart without recursively resuming", async () => {
- const { gateway, lifecycle, activate, params, read, snapshot, seed } = setupOwned();
- const gate = deferred();
- activate.mockImplementationOnce(async () => {
- await gate.promise;
- });
- const restarting = lifecycle.restart("history");
- await vi.waitFor(() => expect(activate).toHaveBeenCalledTimes(1));
- seed((await gateway.subscribe(params)).initialFrame);
- expect(snapshot()?.runtimeInitialization?.status).toBe("initializing");
- expect(read).toHaveBeenCalledTimes(1);
- await vi.waitFor(() => expect(activate).toHaveBeenCalledTimes(1));
- gate.resolve();
- await restarting;
- await vi.waitFor(() => expect(snapshot()?.runtimeInitialization?.status).toBe("ready"));
- gateway.dispose();
- });
- it("completes an externally owned restart without any subscriber", async () => {
- const { gateway, lifecycle, activate } = setupOwned();
- await lifecycle.restart("history");
- expect(activate).toHaveBeenCalledTimes(1);
- gateway.dispose();
- });
- it.each(["desktop-continuous", "web-remote-replayable"])(
- "%s preserves project directories alongside the persisted history snapshot",
- async (clientMode) => {
- const { gateway, gate, resume, params, snapshot, seed } = setup();
- const workspace = { workspacePath: "/project", workspaceKey: "/project" };
- const additionalDirectories = ["/shared"];
- seed(
- (await gateway.subscribe({ ...params, clientMode, workspace, additionalDirectories }))
- .initialFrame,
- );
- await vi.waitFor(() => expect(resume).toHaveBeenCalledTimes(1));
- expect(resume).toHaveBeenCalledWith(
- "history",
- undefined,
- workspace,
- additionalDirectories,
- [],
- );
- gate.resolve();
- await vi.waitFor(() => expect(snapshot()?.runtimeInitialization?.status).toBe("ready"));
- gateway.dispose();
- },
- );
- it("publishes history before activation and rejects execution while restoring", async () => {
- const { gateway, gate, resume, read, execute, params, snapshot, seed } = setup();
- const dispatch = await gateway.subscribeReserved(params);
- expect(resume).not.toHaveBeenCalled();
- expect(dispatch.initialFrame?.payload.kind).toBe("snapshot");
- if (dispatch.initialFrame?.payload.kind !== "snapshot") throw new Error("missing history");
- expect(dispatch.initialFrame.payload.snapshot.runtimeInitialization?.status).toBe(
- "initializing",
- );
- seed(dispatch.initialFrame);
- dispatch.commit();
- await vi.waitFor(() => expect(resume).toHaveBeenCalledTimes(1));
- const rows = await gateway.rowsRange({ sessionId: "history", limit: 10 });
- expect(rows.rows).toEqual([]);
- const ack = await gateway.handleCommand({
- commandId: "early",
- sessionId: "history",
- type: "compact",
- payload: {},
- clientId: "client",
- issuedAt: Date.now(),
- });
- expect(ack.status).toBe("rejected");
- expect(ack.reasonCode).toBe("guard.runtimeInitializing");
- expect(execute).not.toHaveBeenCalled();
- gate.resolve();
- await vi.waitFor(() => expect(snapshot()?.runtimeInitialization?.status).toBe("ready"));
- expect(read).toHaveBeenCalledTimes(1);
- gateway.dispose();
- });
- it("retains failed history until explicit retry and coalesces repeated retry", async () => {
- const { gateway, gate, resume, params, snapshot, seed } = setup();
- seed((await gateway.subscribe(params)).initialFrame);
- await vi.waitFor(() => expect(resume).toHaveBeenCalledTimes(1));
- gate.reject(new Error("hook unavailable"));
- await vi.waitFor(() => expect(snapshot()?.runtimeInitialization?.status).toBe("failed"));
- const attempt = snapshot()!.runtimeInitialization!.attemptId;
- await gateway.subscribe({ ...params, connectionId: "reconnect" });
- await expect(
- gateway.subscribe({ ...params, historyFirst: false, connectionId: "old-client" }),
- ).rejects.toMatchObject({ reasonCode: "fault.subscribe.resumeFailed" });
- expect(resume).toHaveBeenCalledTimes(1);
- resume.mockImplementationOnce(async () => ({ status: "resumed", persistedMessages: [] }));
- await Promise.all([
- gateway.subscribe({ ...params, retryInitialization: attempt }),
- gateway.subscribe({
- ...params,
- connectionId: "phone",
- clientMode: "web-remote-replayable",
- retryInitialization: attempt,
- }),
- ]);
- await vi.waitFor(() => expect(snapshot()?.runtimeInitialization?.status).toBe("ready"));
- expect(resume).toHaveBeenCalledTimes(2);
- gateway.dispose();
- });
- it("retains a ready runtime when only final history reconciliation fails", async () => {
- const { gateway, params, resume, snapshot, seed, loadPersistedEvents } = setup();
- const disposeOnFailure = vi.fn(async () => undefined);
- resume.mockImplementationOnce(async () => ({
- status: "resumed",
- persistedMessages: [],
- disposeOnFailure,
- }));
- seed((await gateway.subscribe(params)).initialFrame);
- loadPersistedEvents.mockRejectedValueOnce(new Error("storage unavailable"));
- await vi.waitFor(() => expect(snapshot()?.runtimeInitialization?.status).toBe("failed"));
- expect(disposeOnFailure).not.toHaveBeenCalled();
- gateway.dispose();
- });
- it("keeps durable command deduplication ahead of initialization guards", async () => {
- const { gateway, gate, params, execute } = setup(() => ({
- commandId: "settled",
- status: "accepted",
- revisionAtDecision: 1,
- }));
- await gateway.subscribe(params);
- const ack = await gateway.handleCommand({
- commandId: "settled",
- sessionId: "history",
- type: "compact",
- payload: {},
- clientId: "client",
- issuedAt: Date.now(),
- });
- expect(ack.status).toBe("duplicate");
- expect(execute).not.toHaveBeenCalled();
- gateway.dispose();
- gate.resolve();
- });
- it("does not activate a disposed opening when its reserved frame commits late", async () => {
- const { gateway, gate, params, resume } = setup();
- const dispatch = await gateway.subscribeReserved(params);
- gateway.dispose();
- dispatch.commit();
- await new Promise<void>((resolve) => setImmediate(resolve));
- expect(resume).not.toHaveBeenCalled();
- gate.resolve();
- });
- it("keeps legacy subscribers behind runtime readiness", async () => {
- const { gateway, gate, params } = setup();
- await gateway.subscribe(params);
- let returned = false;
- const legacy = gateway
- .subscribe({ ...params, historyFirst: false, connectionId: "legacy" })
- .then(() => {
- returned = true;
- });
- await new Promise<void>((resolve) => setImmediate(resolve));
- expect(returned).toBe(false);
- gate.resolve();
- await legacy;
- gateway.dispose();
- });
- it("releases a delayed residency lease after the opening was disposed", async () => {
- const lease = deferred();
- const release = vi.fn();
- const retain = vi.fn(async () => {
- await lease.promise;
- return release;
- });
- const { gateway, params, resume } = setup(undefined, retain);
- await gateway.subscribe(params);
- await vi.waitFor(() => expect(retain).toHaveBeenCalledTimes(1));
- gateway.dispose();
- lease.resolve();
- await vi.waitFor(() => expect(release).toHaveBeenCalledTimes(1));
- expect(resume).not.toHaveBeenCalled();
- });
- });
|