| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951 |
- import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
- import { tmpdir } from "node:os";
- import { dirname, join } from "node:path";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import type { IZCodeAgentService } from "@zcode/services";
- import type { ZCodeAgentMcpServer, ZCodeSessionStateSnapshot } from "@zcode/shared";
- import { createZCodeSessionService } from "@zcode/services/node";
- import type { ZCodeTaskIndexSyncer } from "../src/zcode-agent/zcodeTaskIndexSyncer.js";
- import { getLegacyTaskSessionSnapshotPath, setDataBaseDir } from "../src/paths.js";
- const tempDirs: string[] = [];
- afterEach(() => {
- setDataBaseDir(null);
- while (tempDirs.length > 0) {
- const dir = tempDirs.pop();
- if (dir) {
- rmSync(dir, { recursive: true, force: true });
- }
- }
- });
- function useTempDataBaseDir(): string {
- const dir = mkdtempSync(join(tmpdir(), "zcode-session-service-"));
- tempDirs.push(dir);
- setDataBaseDir(dir);
- return dir;
- }
- describe("createZCodeSessionService", () => {
- it("injects product CUA helper broker config before desktop-continuous session creation", async () => {
- const snapshot = {
- session: {
- sessionId: "session-cua",
- workspace: {
- workspacePath: "/workspace/app",
- workspaceIdentity: undefined,
- },
- },
- runtime: { eventSeq: 1, stateRevision: 1, pendingRequestIds: [] },
- settings: {},
- messages: [],
- } as unknown as ZCodeSessionStateSnapshot;
- const agentService = {
- createSession: vi.fn(async () => snapshot),
- } as unknown as IZCodeAgentService;
- // services 只负责建会话前把 MCP 配置交给 producer 的 resolver;改写细节由 producer 自己的测试覆盖。
- // 这里用测试自己的 resolver,不依赖私有 producer 的注入实现(开源占位包不改写配置)。
- const resolver = {
- resolveMcpServers: vi.fn(async (servers: ZCodeAgentMcpServer[] | undefined) =>
- servers?.map((server) =>
- "command" in server
- ? {
- ...server,
- args: [
- ...(server.args ?? []),
- "--permission-broker-socket",
- "/tmp/zcode-cua/helper.sock",
- ],
- }
- : server,
- ),
- ),
- };
- const service = createZCodeSessionService({
- agentService,
- cuaProductMcpServerResolver: resolver,
- });
- await service.createSession({
- workspacePath: "/workspace/app",
- mcpServers: [
- {
- name: "plugin:computer-use:computer-use",
- command: "uvx",
- args: ["zcode-cua"],
- env: [
- { name: "ZCODE_PLUGIN_ID", value: "computer-use@zcode-plugins-official" },
- {
- name: "ZCODE_CUA_PLUGIN_AUTHORITY",
- value: "dev.zcode.cua-helper/dev",
- },
- ],
- },
- ],
- });
- const createParams = vi.mocked(agentService.createSession).mock.calls[0]?.[0];
- const server = createParams?.mcpServers?.[0];
- expect(resolver.resolveMcpServers).toHaveBeenCalledTimes(1);
- expect(server && "command" in server ? server.args : []).toEqual([
- "zcode-cua",
- "--permission-broker-socket",
- "/tmp/zcode-cua/helper.sock",
- ]);
- });
- it("does not sync deferred draft sessions into the task index", async () => {
- const snapshot = {
- session: {
- sessionId: "draft-1",
- workspace: {
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- },
- },
- };
- const agentService = {
- createSession: vi.fn(async () => snapshot),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(),
- };
- const service = createZCodeSessionService({
- agentService,
- taskIndexSyncer: taskIndexSyncer as never,
- });
- await expect(
- service.createSession({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- persistence: "deferred",
- }),
- ).resolves.toBe(snapshot);
- // Bugfix: zcodeSessionService 会为 createSession 补 sessionTraceId 贯穿日志链路。
- // 这个 deferred draft 用例只验证草稿不进入 task index,因此需要显式接受并校验 trace 字段。
- expect(agentService.createSession).toHaveBeenCalledWith({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- persistence: "deferred",
- sessionTraceId: expect.any(String),
- });
- expect(taskIndexSyncer.ensureSessionSubscription).not.toHaveBeenCalled();
- expect(taskIndexSyncer.syncSnapshotAndBroadcast).not.toHaveBeenCalled();
- });
- it("does not sync deferred draft session model changes into the task index", async () => {
- const snapshot = {
- session: {
- sessionId: "draft-1",
- workspace: {
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- },
- },
- } as unknown as ZCodeSessionStateSnapshot;
- const agentService = {
- createSession: vi.fn(async () => snapshot),
- setModel: vi.fn(async () => snapshot),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(),
- } as unknown as ZCodeTaskIndexSyncer;
- const service = createZCodeSessionService({ agentService, taskIndexSyncer });
- await service.createSession({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- persistence: "deferred",
- });
- await expect(
- service.setModel({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "draft-1",
- model: { providerId: "current-provider", modelId: "current-model" },
- }),
- ).resolves.toBe(snapshot);
- expect(agentService.setModel).toHaveBeenCalledWith({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "draft-1",
- model: { providerId: "current-provider", modelId: "current-model" },
- });
- expect(taskIndexSyncer.ensureSessionSubscription).not.toHaveBeenCalled();
- expect(taskIndexSyncer.syncSnapshotAndBroadcast).not.toHaveBeenCalled();
- });
- it("promotes deferred draft sessions before replayable task facade owns them", async () => {
- const snapshot = {
- session: {
- sessionId: "draft-1",
- workspace: {
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- },
- },
- } as unknown as ZCodeSessionStateSnapshot;
- const agentService = {
- createSession: vi.fn(async () => snapshot),
- setModel: vi.fn(async () => snapshot),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(),
- } as unknown as ZCodeTaskIndexSyncer;
- const service = createZCodeSessionService({ agentService, taskIndexSyncer });
- await service.createSession({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- persistence: "deferred",
- });
- await service.promoteDeferredDraftSession({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "draft-1",
- });
- vi.mocked(taskIndexSyncer.ensureSessionSubscription).mockClear();
- vi.mocked(taskIndexSyncer.syncSnapshotAndBroadcast).mockClear();
- await expect(
- service.setModel({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "draft-1",
- model: { providerId: "current-provider", modelId: "current-model" },
- }),
- ).resolves.toBe(snapshot);
- expect(taskIndexSyncer.ensureSessionSubscription).toHaveBeenCalledWith(
- {
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "draft-1",
- },
- undefined,
- );
- expect(taskIndexSyncer.syncSnapshotAndBroadcast).toHaveBeenCalledWith(snapshot, {
- modelOverride: "current-provider/current-model",
- broadcastReason: "task_model_changed",
- });
- });
- it("uses a conditional stale-draft close after a remote session is promoted", async () => {
- const snapshot = {
- session: {
- sessionId: "remote-task-1",
- workspace: {
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- },
- },
- } as unknown as ZCodeSessionStateSnapshot;
- let persistence: "deferred" | "immediate" = "deferred";
- const closeSession: IZCodeAgentService["closeSession"] = vi.fn(async (params) => {
- if (params.expectedPersistence && params.expectedPersistence !== persistence) {
- return false;
- }
- return true;
- });
- const agentService = {
- closeSession,
- createSession: vi.fn(async () => snapshot),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(),
- } as unknown as ZCodeTaskIndexSyncer;
- const service = createZCodeSessionService({ agentService, taskIndexSyncer });
- const target = {
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "remote-task-1",
- };
- await service.createSession({
- workspacePath: target.workspacePath,
- workspaceIdentity: target.workspaceIdentity,
- persistence: "deferred",
- });
- // 远程端首发会把同一个 session 原地提升;设置页异步闭包仍可能持有旧 draftSessionId。
- persistence = "immediate";
- await service.promoteDeferredDraftSession(target);
- await expect(service.closeDeferredDraftSession(target)).resolves.toBe(false);
- expect(closeSession).toHaveBeenCalledWith({
- ...target,
- expectedPersistence: "deferred",
- });
- expect(taskIndexSyncer.ensureSessionSubscription).toHaveBeenCalledWith(target, undefined);
- });
- it("only closes a draft when the Agent confirms it is still deferred", async () => {
- const closeSession = vi.fn(async () => false);
- const agentService = { closeSession } as unknown as IZCodeAgentService;
- const service = createZCodeSessionService({ agentService });
- await expect(
- service.closeDeferredDraftSession({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "draft-promoted-elsewhere",
- }),
- ).resolves.toBe(false);
- expect(closeSession).toHaveBeenCalledWith({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "draft-promoted-elsewhere",
- expectedPersistence: "deferred",
- });
- });
- it("does not fall back to unconditional close when an older Agent rejects conditional close", async () => {
- const closeSession = vi.fn(async () => {
- throw new Error("Invalid params: expectedPersistence");
- });
- const agentService = { closeSession } as unknown as IZCodeAgentService;
- const service = createZCodeSessionService({ agentService });
- await expect(
- service.closeDeferredDraftSession({
- workspacePath: "/workspace/app",
- sessionId: "draft-on-old-agent",
- }),
- ).resolves.toBe(false);
- expect(closeSession).toHaveBeenCalledTimes(1);
- });
- it("delegates workspace initialization and read paths to the agent service", async () => {
- const agentService = {
- initialize: vi.fn(async () => ({
- available: true,
- workspaceKey: "remote:ssh:dev:/workspace/app",
- protocolName: "ZCode Protocol",
- protocolVersion: 1,
- transportKind: "stdio",
- })),
- readSessionMessages: vi.fn(async () => []),
- readSessionEvents: vi.fn(async () => []),
- } as unknown as IZCodeAgentService;
- const service = createZCodeSessionService({ agentService });
- await expect(
- service.initializeWorkspace({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- }),
- ).resolves.toMatchObject({
- available: true,
- workspaceKey: "remote:ssh:dev:/workspace/app",
- });
- await service.readSessionMessages({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "session-1",
- limit: 20,
- });
- await service.readSessionEvents({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "session-1",
- afterSeq: 4,
- });
- expect(agentService.initialize).toHaveBeenCalledWith({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- });
- expect(agentService.readSessionMessages).toHaveBeenCalledWith({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "session-1",
- limit: 20,
- });
- expect(agentService.readSessionEvents).toHaveBeenCalledWith({
- workspacePath: "/workspace/app",
- workspaceIdentity: "remote:ssh:dev:/workspace/app",
- sessionId: "session-1",
- afterSeq: 4,
- });
- });
- it("repairs empty imported Claude protocol sessions from legacy snapshots", async () => {
- useTempDataBaseDir();
- const workspacePath = "/workspace/imported";
- const sessionId = "claude-import-empty";
- const legacyPath = getLegacyTaskSessionSnapshotPath(workspacePath, sessionId);
- mkdirSync(dirname(legacyPath), { recursive: true });
- writeFileSync(
- legacyPath,
- JSON.stringify({
- meta: {
- taskId: sessionId,
- traceId: "trace_imported",
- title: "Imported Claude",
- workspacePath,
- createdAt: 10,
- updatedAt: 20,
- migrationSource: "claudeCode",
- },
- messages: [
- { role: "user", content: "旧问题", timestamp: 10 },
- { role: "assistant", content: "旧回答", timestamp: 20 },
- ],
- }),
- "utf-8",
- );
- const emptySnapshot = {
- session: {
- sessionId,
- mode: "build",
- status: "idle",
- workspace: {
- workspacePath,
- workspaceKey: workspacePath,
- },
- },
- settings: {
- model: {
- current: { providerId: "glm", modelId: "glm-4.6" },
- available: [],
- },
- thoughtLevel: { enabled: true, current: "think", available: [] },
- mode: { current: "build" },
- },
- runtime: {
- eventSeq: 1,
- stateRevision: 2,
- pendingRequestIds: [],
- },
- messages: [],
- } as unknown as ZCodeSessionStateSnapshot;
- const repairedSnapshot = {
- ...emptySnapshot,
- messages: [
- { info: { role: "user" }, parts: [{ type: "text", text: "旧问题" }] },
- { info: { role: "assistant" }, parts: [{ type: "text", text: "旧回答" }] },
- ],
- } as unknown as ZCodeSessionStateSnapshot;
- const agentService = {
- resumeSession: vi.fn(async () => emptySnapshot),
- createSession: vi.fn(async () => repairedSnapshot),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(),
- } as unknown as ZCodeTaskIndexSyncer;
- const service = createZCodeSessionService({ agentService, taskIndexSyncer });
- await expect(service.resumeSession({ workspacePath, sessionId })).resolves.toBe(
- repairedSnapshot,
- );
- expect(agentService.createSession).toHaveBeenCalledWith({
- workspacePath,
- workspaceIdentity: undefined,
- sessionId,
- mode: "build",
- model: { providerId: "glm", modelId: "glm-4.6" },
- thoughtLevel: "think",
- persistence: "immediate",
- importedHistory: {
- source: "claudeCode",
- title: "Imported Claude",
- createdAt: 10,
- updatedAt: 20,
- messages: [
- { role: "user", content: "旧问题", timestamp: 10 },
- { role: "assistant", content: "旧回答", timestamp: 20 },
- ],
- },
- });
- expect(taskIndexSyncer.syncSnapshotAndBroadcast).toHaveBeenCalledWith(
- repairedSnapshot,
- // Bugfix: resume 收敛是状态同步,广播 reason 改为 task_status_changed,避免全局 membership 重拉。
- { broadcastReason: "task_status_changed" },
- );
- });
- it("repairs imported Claude protocol sessions with legacy fixed message ids", async () => {
- useTempDataBaseDir();
- const workspacePath = "/workspace/imported";
- const sessionId = "claude-import-corrupted-order";
- const legacyPath = getLegacyTaskSessionSnapshotPath(workspacePath, sessionId);
- mkdirSync(dirname(legacyPath), { recursive: true });
- writeFileSync(
- legacyPath,
- JSON.stringify({
- meta: {
- taskId: sessionId,
- traceId: "trace_imported_corrupted",
- title: "Imported Claude Corrupted",
- workspacePath,
- createdAt: 10,
- updatedAt: 20,
- migrationSource: "claudeCode",
- },
- messages: [
- { role: "user", content: "正确问题", timestamp: 10 },
- { role: "assistant", content: "正确回答", timestamp: 20 },
- ],
- }),
- "utf-8",
- );
- const corruptedSnapshot = {
- session: {
- sessionId,
- mode: "build",
- status: "idle",
- workspace: {
- workspacePath,
- workspaceKey: workspacePath,
- },
- },
- settings: {
- model: {
- current: { providerId: "glm", modelId: "glm-4.6" },
- available: [],
- },
- thoughtLevel: { enabled: true, current: "think", available: [] },
- mode: { current: "build" },
- },
- runtime: {
- eventSeq: 1,
- stateRevision: 2,
- pendingRequestIds: [],
- },
- messages: [
- { info: { messageId: "msg_import_1", role: "assistant" }, parts: [] },
- { info: { messageId: "msg_import_0", role: "user" }, parts: [] },
- ],
- } as unknown as ZCodeSessionStateSnapshot;
- const repairedSnapshot = {
- ...corruptedSnapshot,
- messages: [
- {
- info: { messageId: "msg_repaired_0", role: "user" },
- parts: [{ type: "text", text: "正确问题" }],
- },
- {
- info: { messageId: "msg_repaired_1", role: "assistant" },
- parts: [{ type: "text", text: "正确回答" }],
- },
- ],
- } as unknown as ZCodeSessionStateSnapshot;
- const agentService = {
- resumeSession: vi.fn(async () => corruptedSnapshot),
- createSession: vi.fn(async () => repairedSnapshot),
- } as unknown as IZCodeAgentService;
- const service = createZCodeSessionService({ agentService });
- await expect(service.resumeSession({ workspacePath, sessionId })).resolves.toBe(
- repairedSnapshot,
- );
- expect(agentService.createSession).toHaveBeenCalledWith(
- expect.objectContaining({
- workspacePath,
- sessionId,
- importedHistory: expect.objectContaining({
- messages: [
- { role: "user", content: "正确问题", timestamp: 10 },
- { role: "assistant", content: "正确回答", timestamp: 20 },
- ],
- }),
- }),
- );
- });
- it("does not repair ordinary user-only sessions from accidental Claude legacy files", async () => {
- useTempDataBaseDir();
- const workspacePath = "/workspace/ordinary";
- const sessionId = "ordinary-user-only-session";
- const legacyPath = getLegacyTaskSessionSnapshotPath(workspacePath, sessionId);
- mkdirSync(dirname(legacyPath), { recursive: true });
- writeFileSync(
- legacyPath,
- JSON.stringify({
- meta: {
- taskId: sessionId,
- traceId: "trace_accidental_legacy",
- title: "Accidental Claude backup",
- workspacePath,
- createdAt: 10,
- updatedAt: 20,
- migrationSource: "claudeCode",
- },
- messages: [
- { role: "user", content: "不应该回填的问题", timestamp: 10 },
- { role: "assistant", content: "不应该回填的回答", timestamp: 20 },
- ],
- }),
- "utf-8",
- );
- const ordinarySnapshot = {
- session: {
- sessionId,
- mode: "build",
- status: "idle",
- workspace: {
- workspacePath,
- workspaceKey: workspacePath,
- },
- },
- settings: {
- model: {
- current: { providerId: "glm", modelId: "glm-4.6" },
- available: [],
- },
- thoughtLevel: { enabled: true, current: "think", available: [] },
- mode: { current: "build" },
- },
- runtime: {
- eventSeq: 1,
- stateRevision: 2,
- pendingRequestIds: [],
- },
- messages: [{ info: { messageId: "msg_ordinary_0", role: "user" }, parts: [] }],
- } as unknown as ZCodeSessionStateSnapshot;
- const agentService = {
- resumeSession: vi.fn(async () => ordinarySnapshot),
- createSession: vi.fn(),
- } as unknown as IZCodeAgentService;
- const service = createZCodeSessionService({ agentService });
- await expect(service.resumeSession({ workspacePath, sessionId })).resolves.toBe(
- ordinarySnapshot,
- );
- expect(agentService.createSession).not.toHaveBeenCalled();
- });
- it("asks the task index syncer to place desktop-created sessions at the grouped top", async () => {
- const snapshot = {
- session: {
- sessionId: "session-1",
- workspace: {
- workspacePath: "/workspace/app",
- workspaceKey: "/workspace/app",
- },
- },
- };
- const agentService = {
- createSession: vi.fn(async () => snapshot),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(async () => ({
- taskId: "session-1",
- traceId: "trace-session-1",
- title: "New session",
- workspacePath: "/workspace/app",
- createdAt: 1,
- updatedAt: 1,
- mode: "build",
- status: "running",
- })),
- } as unknown as ZCodeTaskIndexSyncer;
- const service = createZCodeSessionService({ agentService, taskIndexSyncer });
- await service.createSession({ workspacePath: "/workspace/app" });
- expect(taskIndexSyncer.ensureSessionSubscription).toHaveBeenCalledWith(
- {
- workspacePath: "/workspace/app",
- workspaceIdentity: undefined,
- sessionId: "session-1",
- },
- undefined,
- );
- expect(taskIndexSyncer.syncSnapshotAndBroadcast).toHaveBeenCalledWith(snapshot, {
- moveGroupedTaskToTop: true,
- broadcastReason: "task_meta_changed",
- });
- });
- it("syncs resume model override into the task index", async () => {
- const snapshot = {
- protocol: { name: "ZCode Protocol", version: 1 },
- session: {
- sessionId: "session-1",
- mode: "build",
- status: "idle",
- createdAt: 1,
- updatedAt: 2,
- workspace: {
- workspacePath: "/workspace/app",
- workspaceKey: "/workspace/app",
- },
- },
- settings: {
- model: {
- current: { providerId: "current-provider", modelId: "current-model" },
- available: [],
- },
- thoughtLevel: { enabled: false, available: [] },
- mode: { current: "build" },
- },
- runtime: { eventSeq: 1, stateRevision: 1, pendingRequestIds: [] },
- projection: {},
- messages: [],
- } as unknown as ZCodeSessionStateSnapshot;
- const agentService = {
- resumeSession: vi.fn(async () => snapshot),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(),
- } as unknown as ZCodeTaskIndexSyncer;
- const service = createZCodeSessionService({ agentService, taskIndexSyncer });
- await service.resumeSession({
- workspacePath: "/workspace/app",
- sessionId: "session-1",
- model: { providerId: "current-provider", modelId: "current-model" },
- });
- expect(taskIndexSyncer.syncSnapshotAndBroadcast).toHaveBeenCalledWith(snapshot, {
- modelOverride: "current-provider/current-model",
- broadcastReason: "task_status_changed",
- });
- });
- it("replays requested thought level before broadcasting resume snapshot", async () => {
- const makeSnapshot = (thoughtLevel: string) =>
- ({
- protocol: { name: "ZCode Protocol", version: 1 },
- session: {
- sessionId: "session-1",
- mode: "build",
- status: "idle",
- createdAt: 1,
- updatedAt: 2,
- workspace: {
- workspacePath: "/workspace/app",
- workspaceKey: "/workspace/app",
- },
- },
- settings: {
- model: {
- current: { providerId: "bigmodel-api", modelId: "GLM-5.2" },
- available: [],
- },
- thoughtLevel: { enabled: true, current: thoughtLevel, available: ["high", "max"] },
- mode: { current: "build" },
- },
- runtime: {
- activeTurnId: undefined,
- eventSeq: 1,
- stateRevision: 1,
- pendingRequestIds: [],
- },
- projection: { target: null, lastError: null, pendingPermissions: [] },
- messages: [],
- }) as unknown as ZCodeSessionStateSnapshot;
- const pollutedSnapshot = makeSnapshot("high");
- const correctedSnapshot = makeSnapshot("max");
- const agentService = {
- resumeSession: vi.fn(async () => pollutedSnapshot),
- setThoughtLevel: vi.fn(async () => correctedSnapshot),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(),
- } as unknown as ZCodeTaskIndexSyncer;
- const service = createZCodeSessionService({ agentService, taskIndexSyncer });
- await expect(
- service.resumeSession({
- workspacePath: "/workspace/app",
- sessionId: "session-1",
- model: { providerId: "bigmodel-api", modelId: "GLM-5.2" },
- thoughtLevel: "max",
- }),
- ).resolves.toBe(correctedSnapshot);
- expect(agentService.setThoughtLevel).toHaveBeenCalledWith({
- workspacePath: "/workspace/app",
- workspaceIdentity: undefined,
- sessionId: "session-1",
- thoughtLevel: "max",
- });
- expect(taskIndexSyncer.syncSnapshotAndBroadcast).toHaveBeenCalledWith(correctedSnapshot, {
- modelOverride: "bigmodel-api/GLM-5.2",
- thoughtLevelOverride: "max",
- broadcastReason: "task_status_changed",
- });
- });
- it("skips resume thought replay when the restored model does not support the requested level", async () => {
- const snapshot = {
- protocol: { name: "ZCode Protocol", version: 1 },
- session: {
- sessionId: "session-1",
- mode: "build",
- status: "idle",
- createdAt: 1,
- updatedAt: 2,
- workspace: {
- workspacePath: "/workspace/app",
- workspaceKey: "/workspace/app",
- },
- },
- settings: {
- model: {
- current: {
- providerId: "account:bigmodel-individual-coding-plan",
- modelId: "GLM-5-Turbo",
- },
- available: [],
- },
- thoughtLevel: {
- enabled: true,
- current: "enabled",
- available: [
- { value: "enabled", label: "Enabled" },
- { value: "off", label: "Off" },
- ],
- },
- mode: { current: "build" },
- },
- runtime: {
- activeTurnId: undefined,
- eventSeq: 1,
- stateRevision: 1,
- pendingRequestIds: [],
- },
- projection: { target: null, lastError: null, pendingPermissions: [] },
- messages: [],
- } as unknown as ZCodeSessionStateSnapshot;
- const agentService = {
- resumeSession: vi.fn(async () => snapshot),
- setThoughtLevel: vi.fn(async () => {
- throw new Error("setThoughtLevel should not be called");
- }),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(),
- } as unknown as ZCodeTaskIndexSyncer;
- const service = createZCodeSessionService({ agentService, taskIndexSyncer });
- await expect(
- service.resumeSession({
- workspacePath: "/workspace/app",
- sessionId: "session-1",
- model: { providerId: "account:bigmodel-individual-coding-plan", modelId: "GLM-5-Turbo" },
- thoughtLevel: "max",
- }),
- ).resolves.toBe(snapshot);
- expect(agentService.setThoughtLevel).not.toHaveBeenCalled();
- expect(taskIndexSyncer.syncSnapshotAndBroadcast).toHaveBeenCalledWith(snapshot, {
- modelOverride: "account:bigmodel-individual-coding-plan/GLM-5-Turbo",
- broadcastReason: "task_status_changed",
- });
- });
- it("updates only task model when resume snapshot broadcast is disabled", async () => {
- const snapshot = {
- protocol: { name: "ZCode Protocol", version: 1 },
- session: {
- sessionId: "session-1",
- mode: "build",
- status: "idle",
- createdAt: 1,
- updatedAt: 2,
- workspace: {
- workspacePath: "/workspace/app",
- workspaceKey: "/workspace/app",
- },
- },
- settings: {
- model: {
- current: { providerId: "current-provider", modelId: "current-model" },
- available: [],
- },
- thoughtLevel: { enabled: false, available: [] },
- mode: { current: "build" },
- },
- runtime: { eventSeq: 1, stateRevision: 1, pendingRequestIds: [] },
- projection: {},
- messages: [],
- } as unknown as ZCodeSessionStateSnapshot;
- const agentService = {
- resumeSession: vi.fn(async () => snapshot),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(),
- syncTaskModel: vi.fn(),
- } as unknown as ZCodeTaskIndexSyncer;
- const service = createZCodeSessionService({ agentService, taskIndexSyncer });
- await service.resumeSession({
- workspacePath: "/workspace/app",
- sessionId: "session-1",
- model: { providerId: "current-provider", modelId: "current-model" },
- broadcastSnapshot: false,
- });
- expect(taskIndexSyncer.syncSnapshotAndBroadcast).not.toHaveBeenCalled();
- expect(taskIndexSyncer.syncTaskModel).toHaveBeenCalledWith(
- {
- workspacePath: "/workspace/app",
- workspaceIdentity: undefined,
- sessionId: "session-1",
- },
- "current-provider/current-model",
- );
- });
- it("syncs explicit setModel snapshots into the task index", async () => {
- const snapshot = {
- session: {
- sessionId: "session-1",
- workspace: {
- workspacePath: "/workspace/app",
- workspaceKey: "/workspace/app",
- },
- },
- } as unknown as ZCodeSessionStateSnapshot;
- const agentService = {
- setModel: vi.fn(async () => snapshot),
- } as unknown as IZCodeAgentService;
- const taskIndexSyncer = {
- ensureSessionSubscription: vi.fn(),
- syncSnapshotAndBroadcast: vi.fn(),
- } as unknown as ZCodeTaskIndexSyncer;
- const service = createZCodeSessionService({ agentService, taskIndexSyncer });
- await expect(
- service.setModel({
- workspacePath: "/workspace/app",
- sessionId: "session-1",
- model: { providerId: "current-provider", modelId: "current-model" },
- }),
- ).resolves.toBe(snapshot);
- expect(taskIndexSyncer.ensureSessionSubscription).toHaveBeenCalledWith(
- {
- workspacePath: "/workspace/app",
- workspaceIdentity: undefined,
- sessionId: "session-1",
- },
- undefined,
- );
- expect(taskIndexSyncer.syncSnapshotAndBroadcast).toHaveBeenCalledWith(snapshot, {
- modelOverride: "current-provider/current-model",
- broadcastReason: "task_model_changed",
- });
- });
- });
|