| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192 |
- // ConversationV4Gateway 的 sessions-index 集成测试(M5 ①,14-sessions-index):
- // 1. subscribeSessionsIndex:store 摘要种子 + 已加载会话 live 投影覆盖 → snapshot 帧
- // 2. ingest 非流式事件 → 列表订阅者收到 session.upserted 增量帧(流式 ModelStreaming 不触发)
- // 3. disposeSession → session.removed 增量帧
- // 4. 无 getSessionWorkspaceId 的旧宿主 → sessions-index 路径整体 no-op,不影响 conversation
- // 5. unsubscribeSessionsIndex 后不再收帧
- // 6. DynamicWorkflowRunProgress 按 leading + trailing 窗口节流(14-sessions-index「事件 fan-out 节奏」)
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
- import type {
- EventId,
- SessionEvent,
- SessionEventType as SessionEventTypeUnion,
- SessionId,
- TraceId,
- TurnId,
- } from "@zcode/contracts";
- import { SessionEventType } from "@zcode/contracts";
- import type {
- ConversationTopicFrame,
- SessionSummary,
- SessionsIndexTopicFrame,
- } from "@zcode/shared/zcode-protocol-v4";
- import { ConversationV4Gateway } from "../src/zcode-protocol-v4/index.js";
- type AnyFrame = ConversationTopicFrame | SessionsIndexTopicFrame;
- function event(
- sessionId: string,
- seq: number,
- type: SessionEventTypeUnion,
- payload: unknown,
- turnId?: string,
- ): SessionEvent {
- return {
- id: `event-${sessionId}-${seq}` as EventId,
- sessionId: sessionId as SessionId,
- turnId: turnId as TurnId | undefined,
- type,
- timestamp: new Date(1_700_000_000_000 + seq * 1000),
- traceId: "trace-1" as TraceId,
- sequenceNumber: seq,
- payload,
- };
- }
- function storedSummary(sessionId: string): SessionSummary {
- return {
- sessionId,
- workspaceId: "ws-1",
- title: `stored-${sessionId}`,
- phase: "completedSuccess",
- sessionEnded: true,
- hasBackgroundWork: false,
- lastActivityAt: 50,
- createdAt: 10,
- };
- }
- interface Stub {
- frames: AnyFrame[];
- sessions: Set<string>;
- sessionWorkspace: Map<string, string>;
- stored: SessionSummary[];
- /** 模拟 record.updatedAt:每条活动事件都会 bump,摘要因此条条有变化。 */
- lastActivityAt?: number;
- drafts?: Set<string>;
- executeCommand?: ConstructorParameters<typeof ConversationV4Gateway>[0]["executeCommand"];
- loadPersistedEvents?: (sessionId: string) => Promise<{
- events: SessionEvent[];
- synthesized: boolean;
- sourceEventSeq?: number;
- }>;
- loadStored?: (
- workspaceId: string,
- legacyTaskIds?: readonly string[],
- ) => Promise<SessionSummary[]> | SessionSummary[];
- }
- function makeGateway(stub: Stub, opts: { withIndexHooks: boolean }) {
- return new ConversationV4Gateway(
- {
- sessionExists: (sessionId) => stub.sessions.has(sessionId),
- emitWireFrame: (wire) => {
- if (wire.kind !== "complete") {
- throw new Error("sessions-index test adapter only accepts complete wires");
- }
- stub.frames.push(wire.frame);
- },
- executeCommand: (...args) => stub.executeCommand?.(...args) ?? Promise.resolve(undefined),
- loadPersistedEvents: async (sessionId) =>
- stub.loadPersistedEvents?.(sessionId) ?? { events: [], synthesized: false },
- ...(opts.withIndexHooks
- ? {
- getSessionWorkspaceId: (sessionId: string) =>
- stub.sessionWorkspace.get(sessionId) ?? null,
- getSessionIndexMeta: () => ({
- createdAt: 10,
- lastActivityAt: stub.lastActivityAt ?? 99,
- }),
- listWorkspaceSessionIds: (workspaceId: string) =>
- [...stub.sessionWorkspace.entries()]
- .filter(([, ws]) => ws === workspaceId)
- .map(([id]) => id),
- getStoredSessionSummaries: (workspaceId: string) =>
- stub.loadStored?.(workspaceId) ?? stub.stored,
- refreshLegacySessionSummaries: (
- workspaceId: string,
- legacyTaskIds: readonly string[],
- ) => stub.loadStored?.(workspaceId, legacyTaskIds) ?? stub.stored,
- isDraftSession: (sessionId: string) => stub.drafts?.has(sessionId) ?? false,
- }
- : {}),
- },
- { now: () => 1_700_000_999_000, createLogEpoch: () => "epoch-1" },
- );
- }
- function indexFrames(stub: Stub): SessionsIndexTopicFrame[] {
- return stub.frames.filter((f) =>
- f.topic.startsWith("sessions-index/"),
- ) as SessionsIndexTopicFrame[];
- }
- describe("ConversationV4Gateway sessions-index 集成", () => {
- it.each(["sendText", "createSession"] as const)(
- "FSV01:%s 首发提升后不等待下一条 runtime 事件发布摘要",
- async (type) => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- drafts: new Set(["live-1"]),
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "desktop",
- clientMode: "desktop-continuous",
- });
- gateway.ingest(
- "live-1",
- event("live-1", 2, SessionEventType.SessionTitleUpdated, {
- previousTitle: "",
- source: "first_input",
- title: "first",
- }),
- );
- expect(indexFrames(stub)).toHaveLength(0);
- stub.executeCommand = async () => {
- stub.drafts!.delete("live-1");
- return type === "createSession"
- ? { type: "createSession", sessionId: "live-1" }
- : { type: "inputAccepted", inputId: "first", delivery: "startNow" };
- };
- const command = {
- commandId: "first",
- clientId: "client",
- sessionId: type === "createSession" ? null : "live-1",
- type,
- payload:
- type === "createSession"
- ? { workspaceId: "ws-1", firstInput: { text: "first" } }
- : { text: "first" },
- issuedAt: 1,
- };
- const ack = await gateway.handleCommand(command);
- expect(ack.status).toBe("accepted");
- expect(indexFrames(stub)).toMatchObject([
- {
- payload: {
- kind: "deltas",
- deltas: [
- {
- op: "session.upserted",
- session: { sessionId: "live-1", title: "first", phase: "draft" },
- },
- ],
- },
- },
- ]);
- await gateway.handleCommand(command);
- expect(indexFrames(stub)).toHaveLength(1);
- gateway.dispose();
- },
- );
- it("saturated mobile index subscriber does not block desktop and drains only itself", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- const desktop = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "desktop-index",
- clientMode: "desktop-continuous",
- });
- const mobile = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "mobile-index",
- clientMode: "web-remote-replayable",
- });
- gateway.setConnectionFlowState({ connectionId: "mobile-index", state: "saturated" });
- gateway.ingest(
- "live-1",
- event("live-1", 2, SessionEventType.SessionTitleUpdated, {
- previousTitle: "",
- source: "custom",
- title: "changed-while-paused",
- }),
- );
- expect(indexFrames(stub).map((frame) => frame.subscriptionId)).toEqual([
- desktop.ack.subscriptionId,
- ]);
- gateway.setConnectionFlowState({ connectionId: "mobile-index", state: "drained" });
- expect(indexFrames(stub).map((frame) => frame.subscriptionId)).toEqual([
- desktop.ack.subscriptionId,
- mobile.ack.subscriptionId,
- ]);
- });
- it("sessions-index recovery commit 后无新 ingest 也会 flush 在途期间增量", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- const subscribed = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- const recovery = gateway.resyncReserved({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- subscriptionId: subscribed.ack.subscriptionId,
- base: {
- logEpoch: subscribed.ack.logEpoch,
- seq: subscribed.initialFrame?.toSeq ?? 0,
- },
- });
- gateway.ingest(
- "live-1",
- event("live-1", 2, SessionEventType.SessionTitleUpdated, {
- previousTitle: "",
- source: "custom",
- title: "during-recovery",
- }),
- );
- expect(indexFrames(stub)).toEqual([]);
- expect(recovery.commit()).toBe(true);
- expect(indexFrames(stub)).toHaveLength(1);
- expect(indexFrames(stub)[0]).toMatchObject({
- payload: { kind: "deltas" },
- });
- });
- it("initial physical encode 失败会回滚 sessions-index subscription", async () => {
- const summary = storedSummary("oversized");
- summary.title = "x".repeat(17 * 1024 * 1024);
- const stub: Stub = {
- frames: [],
- sessions: new Set(),
- sessionWorkspace: new Map(),
- stored: [summary],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- await expect(
- gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- }),
- ).rejects.toThrow("proto.frameAssemblyTooLarge");
- const publishers = (
- gateway as unknown as {
- indexPublishers: Map<string, { hasSubscribers(): boolean }>;
- }
- ).indexPublishers;
- expect(publishers.get("ws-1")?.hasSubscribers()).toBe(false);
- });
- it("subscribe:store 种子 + live 覆盖 → snapshot 含两会话", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [storedSummary("cold-1")],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- // 先让 live-1 有 conversation 投影
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- const res = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- expect(res.ack.mode).toBe("snapshot");
- expect(res.initialFrame?.payload.kind).toBe("snapshot");
- if (res.initialFrame?.payload.kind === "snapshot") {
- const ids = res.initialFrame.payload.snapshot.sessions.map((s) => s.sessionId).sort();
- expect(ids).toEqual(["cold-1", "live-1"]);
- }
- });
- it("首次无 allowlist 后可用 allowlist 重订阅:重读 store 并向已有订阅增量补齐", async () => {
- const storedCurrent = storedSummary("current-1");
- const storedLegacy = storedSummary("legacy-1");
- const calls: Array<readonly string[] | undefined> = [];
- const stub: Stub = {
- frames: [],
- sessions: new Set(),
- sessionWorkspace: new Map(),
- stored: [],
- loadStored: (_workspaceId, legacyTaskIds) => {
- calls.push(legacyTaskIds);
- return legacyTaskIds?.length
- ? [{ ...storedCurrent, title: "不能覆盖已有摘要" }, storedLegacy]
- : [storedCurrent];
- },
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- const first = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-old",
- clientMode: "desktop-continuous",
- });
- const before = indexFrames(stub).length;
- const retried = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-new",
- clientMode: "desktop-continuous",
- legacyTaskIds: ["legacy-1"],
- });
- expect(calls).toEqual([undefined, ["legacy-1"]]);
- expect(first.initialFrame?.payload).toMatchObject({
- kind: "snapshot",
- snapshot: { sessions: [expect.objectContaining({ sessionId: "current-1" })] },
- });
- if (retried.initialFrame?.payload.kind === "snapshot") {
- expect(retried.initialFrame.payload.snapshot.sessions).toEqual([
- expect.objectContaining({ sessionId: "current-1", title: "stored-current-1" }),
- expect.objectContaining({ sessionId: "legacy-1" }),
- ]);
- } else {
- throw new Error("Expected retried sessions-index snapshot");
- }
- const emitted = indexFrames(stub).slice(before);
- expect(emitted).toEqual([
- expect.objectContaining({
- subscriptionId: first.ack.subscriptionId,
- payload: {
- kind: "deltas",
- deltas: [
- expect.objectContaining({
- op: "session.upserted",
- session: expect.objectContaining({ sessionId: "legacy-1" }),
- }),
- ],
- },
- }),
- ]);
- });
- it("携带 allowlist 的重读失败后不会封死后续重试", async () => {
- let allowlistAttempts = 0;
- const stub: Stub = {
- frames: [],
- sessions: new Set(),
- sessionWorkspace: new Map(),
- stored: [],
- loadStored: (_workspaceId, legacyTaskIds) => {
- if (!legacyTaskIds?.length) return [];
- allowlistAttempts += 1;
- // 模拟 bridge 将 claim/list 失败降级为空摘要;下一次订阅必须再次调用宿主钩子。
- return allowlistAttempts === 1 ? [] : [storedSummary("legacy-1")];
- },
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-initial",
- clientMode: "desktop-continuous",
- });
- await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-failed",
- clientMode: "desktop-continuous",
- legacyTaskIds: ["legacy-1"],
- });
- const recovered = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-recovered",
- clientMode: "desktop-continuous",
- legacyTaskIds: ["legacy-1"],
- });
- expect(allowlistAttempts).toBe(2);
- if (recovered.initialFrame?.payload.kind === "snapshot") {
- expect(recovered.initialFrame.payload.snapshot.sessions).toEqual([
- expect.objectContaining({ sessionId: "legacy-1" }),
- ]);
- } else {
- throw new Error("Expected recovered sessions-index snapshot");
- }
- });
- it("同 workspace 并发初始化按顺序执行,迁移后快照不会被较早的空种子赢走", async () => {
- let releaseFirst!: (summaries: SessionSummary[]) => void;
- let secondLoadStarted = false;
- let loadCount = 0;
- const stub: Stub = {
- frames: [],
- sessions: new Set(),
- sessionWorkspace: new Map(),
- stored: [],
- loadStored: () => {
- loadCount += 1;
- if (loadCount === 1) {
- return new Promise<SessionSummary[]>((resolve) => {
- releaseFirst = resolve;
- });
- }
- secondLoadStarted = true;
- return [storedSummary("legacy-1")];
- },
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- const first = gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-first",
- clientMode: "desktop-continuous",
- });
- const second = gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-second",
- clientMode: "desktop-continuous",
- legacyTaskIds: ["legacy-1"],
- });
- expect(secondLoadStarted).toBe(false);
- releaseFirst([]);
- await first;
- const migrated = await second;
- expect(secondLoadStarted).toBe(true);
- if (migrated.initialFrame?.payload.kind === "snapshot") {
- expect(migrated.initialFrame.payload.snapshot.sessions).toEqual([
- expect.objectContaining({ sessionId: "legacy-1" }),
- ]);
- } else {
- throw new Error("Expected migrated sessions-index snapshot");
- }
- });
- it("异步冷种子未完成时 dispose:不得在销毁后重新注册 publisher 或完成订阅", async () => {
- let releaseLoad!: (summaries: SessionSummary[]) => void;
- const stub: Stub = {
- frames: [],
- sessions: new Set(),
- sessionWorkspace: new Map(),
- stored: [],
- loadStored: () =>
- new Promise<SessionSummary[]>((resolve) => {
- releaseLoad = resolve;
- }),
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- const subscribing = gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-disposed",
- clientMode: "desktop-continuous",
- });
- gateway.dispose();
- releaseLoad([storedSummary("late-session")]);
- await expect(subscribing).rejects.toThrow("fault.gateway.disposed");
- const publishers = (
- gateway as unknown as {
- indexPublishers: { get(workspaceId: string): unknown };
- }
- ).indexPublishers;
- expect(publishers.get("ws-1")).toBeUndefined();
- expect(indexFrames(stub)).toEqual([]);
- });
- it("draft(deferred 未发首条)不进列表:种子跳过 + ingest 跳过,提升后入列", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["draft-1"]),
- sessionWorkspace: new Map([["draft-1", "ws-1"]]),
- stored: [],
- drafts: new Set(["draft-1"]),
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "draft-1",
- event("draft-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- // 订阅种子:draft 不出现在 snapshot。
- const res = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- if (res.initialFrame?.payload.kind === "snapshot") {
- expect(res.initialFrame.payload.snapshot.sessions).toEqual([]);
- }
- // draft 期间的事件也不产列表帧。
- const framesBefore = indexFrames(stub).length;
- gateway.ingest(
- "draft-1",
- event("draft-1", 2, SessionEventType.SessionTitleUpdated, {
- previousTitle: "",
- source: "custom",
- title: "draft 期标题",
- }),
- );
- expect(indexFrames(stub).length).toBe(framesBefore);
- // 首发提升 persistence 后(draft 标记消失),事件驱动入列。
- stub.drafts?.delete("draft-1");
- gateway.ingest(
- "draft-1",
- event("draft-1", 3, SessionEventType.SessionTitleUpdated, {
- previousTitle: "draft 期标题",
- source: "custom",
- title: "已提升",
- }),
- );
- const upserts = indexFrames(stub).filter(
- (f) =>
- f.payload.kind === "deltas" &&
- f.payload.deltas.some(
- (d) => d.op === "session.upserted" && d.session.sessionId === "draft-1",
- ),
- );
- expect(upserts.length).toBeGreaterThan(0);
- });
- it("fork 暂态 publisher 在 hydration 后无后续事件也会发布 visible summary", async () => {
- const childId = "fork-child";
- const persistedEvents = [
- event(childId, 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- event(
- childId,
- 2,
- SessionEventType.TurnStarted,
- { turnNumber: 1, input: "继承问题" },
- "turn-1",
- ),
- event(
- childId,
- 3,
- SessionEventType.ModelStreaming,
- { kind: "text_delta", delta: "继承回答", done: false },
- "turn-1",
- ),
- event(
- childId,
- 4,
- SessionEventType.TurnComplete,
- {
- response: "继承回答",
- tokenCount: 1,
- toolCallCount: 0,
- duration: 1,
- resultType: "success",
- },
- "turn-1",
- ),
- ];
- const stub: Stub = {
- frames: [],
- sessions: new Set([childId]),
- sessionWorkspace: new Map([[childId, "ws-1"]]),
- stored: [],
- loadPersistedEvents: async () => ({
- events: persistedEvents,
- synthesized: true,
- sourceEventSeq: persistedEvents.length,
- }),
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- // fork resume 先建立只有 SessionCreated 的暂态 publisher;此时列表基线是 draft。
- gateway.ingest(
- childId,
- event(childId, 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- const indexSubscription = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "index-conn",
- clientMode: "desktop-continuous",
- });
- expect(indexSubscription.initialFrame?.payload).toMatchObject({
- kind: "snapshot",
- snapshot: {
- sessions: [expect.objectContaining({ sessionId: childId, phase: "draft" })],
- },
- });
- const framesBeforeHydration = indexFrames(stub).length;
- await gateway.subscribe({
- topic: `conversation/${childId}`,
- connectionId: "conversation-conn",
- clientMode: "desktop-continuous",
- });
- const emitted = indexFrames(stub).slice(framesBeforeHydration);
- expect(emitted).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- subscriptionId: indexSubscription.ack.subscriptionId,
- payload: {
- kind: "deltas",
- deltas: [
- expect.objectContaining({
- op: "session.upserted",
- session: expect.objectContaining({
- sessionId: childId,
- phase: "completedSuccess",
- }),
- }),
- ],
- },
- }),
- ]),
- );
- });
- it("ingest 非流式事件 → 列表订阅者收到 upserted 增量帧", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- const before = indexFrames(stub).length;
- // 标题变化(非流式)→ 应产 upserted
- gateway.ingest(
- "live-1",
- event("live-1", 2, SessionEventType.SessionTitleUpdated, {
- previousTitle: "",
- source: "custom",
- title: "新标题",
- }),
- );
- const emitted = indexFrames(stub).slice(before);
- const deltaFrame = emitted.find((f) => f.payload.kind === "deltas");
- expect(deltaFrame).toBeDefined();
- if (deltaFrame?.payload.kind === "deltas") {
- expect(deltaFrame.payload.deltas[0]).toMatchObject({
- op: "session.upserted",
- });
- }
- });
- it("ModelStreaming 高频增量不触发列表帧", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- const before = indexFrames(stub).length;
- gateway.ingest(
- "live-1",
- event("live-1", 2, SessionEventType.TurnStarted, { turnNumber: 1, input: "hi" }, "t1"),
- );
- gateway.ingest(
- "live-1",
- event(
- "live-1",
- 3,
- SessionEventType.ModelStreaming,
- { kind: "text_start", delta: "", done: false },
- "t1",
- ),
- );
- gateway.ingest(
- "live-1",
- event(
- "live-1",
- 4,
- SessionEventType.ModelStreaming,
- { kind: "text_delta", delta: "abc", done: false },
- "t1",
- ),
- );
- const emitted = indexFrames(stub).slice(before);
- // TurnStarted 触发一帧(phase→running),两条 ModelStreaming 不再触发
- expect(emitted.length).toBe(1);
- });
- it("关闭运行实例不发删除事件,列表仍保留持久化会话", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- const before = indexFrames(stub).length;
- gateway.disposeSession("live-1");
- expect(indexFrames(stub).slice(before)).toEqual([]);
- const next = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-2",
- clientMode: "web-remote-replayable",
- });
- expect(next.initialFrame?.payload).toMatchObject({
- kind: "snapshot",
- snapshot: { sessions: [expect.objectContaining({ sessionId: "live-1" })] },
- });
- });
- it("disposeSession → session.removed 增量帧", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- const before = indexFrames(stub).length;
- gateway.disposeSession("live-1", { deleted: true });
- const emitted = indexFrames(stub).slice(before);
- const removed = emitted.find(
- (f) =>
- f.payload.kind === "deltas" && f.payload.deltas.some((d) => d.op === "session.removed"),
- );
- expect(removed).toBeDefined();
- });
- it("无订阅者时 disposeSession 仍移除 index projection,重订阅 snapshot 不复活", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- const first = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- gateway.unsubscribe({
- topic: "sessions-index/ws-1",
- subscriptionId: first.ack.subscriptionId,
- connectionId: "conn-1",
- });
- // disposeSession 的删除必须更新无订阅者 publisher;否则已有 publisher 会把已删除会话
- // 带进下一次 snapshot,造成任务复活。
- gateway.disposeSession("live-1", { deleted: true });
- const second = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-2",
- clientMode: "desktop-continuous",
- });
- if (second.initialFrame?.payload.kind === "snapshot") {
- expect(second.initialFrame.payload.snapshot.sessions).toEqual([]);
- } else {
- throw new Error("Expected sessions-index snapshot");
- }
- });
- it("旧宿主(无 index hooks)→ sessions-index 路径 no-op,conversation ingest 不受影响", () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map(),
- stored: [],
- };
- const gateway = makeGateway(stub, { withIndexHooks: false });
- expect(() =>
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- ),
- ).not.toThrow();
- expect(indexFrames(stub)).toHaveLength(0);
- });
- it("unsubscribeSessionsIndex 后 ingest 不再产列表帧", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- const res = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- gateway.unsubscribe({
- topic: "sessions-index/ws-1",
- subscriptionId: res.ack.subscriptionId,
- connectionId: "conn-1",
- });
- const before = indexFrames(stub).length;
- gateway.ingest(
- "live-1",
- event("live-1", 2, SessionEventType.SessionTitleUpdated, {
- previousTitle: "",
- source: "custom",
- title: "退订后",
- }),
- );
- expect(indexFrames(stub).length).toBe(before);
- });
- it("无订阅者窗口仍推进 index projection,重订阅 snapshot 返回当前 phase", async () => {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- const first = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- expect(first.initialFrame?.payload).toMatchObject({
- kind: "snapshot",
- snapshot: { sessions: [expect.objectContaining({ sessionId: "live-1" })] },
- });
- gateway.unsubscribe({
- topic: "sessions-index/ws-1",
- subscriptionId: first.ack.subscriptionId,
- connectionId: "conn-1",
- });
- // 退订窗口内没有任何列表消费者;修复前 publishCurrentSummaryToIndex 会直接
- // return,导致之后的 snapshot 仍停留在窗口前的 phase。
- gateway.ingest(
- "live-1",
- event("live-1", 2, SessionEventType.TurnStarted, { turnNumber: 1, input: "hi" }, "t1"),
- );
- const second = await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-2",
- clientMode: "desktop-continuous",
- });
- expect(second.initialFrame?.payload).toMatchObject({
- kind: "snapshot",
- snapshot: {
- sessions: [expect.objectContaining({ sessionId: "live-1", phase: "running" })],
- },
- });
- });
- });
- describe("sessions-index reconciliation", () => {
- it("validates and delegates a bounded read without creating a conversation runtime", async () => {
- let received: unknown;
- const gateway = new ConversationV4Gateway({
- sessionExists: () => false,
- emitWireFrame: () => {},
- executeCommand: (...args) => stub.executeCommand?.(...args) ?? Promise.resolve(undefined),
- reconcileSessions: async (params) => {
- received = params;
- return {
- sessions: [
- {
- sessionId: "deleted",
- exists: true,
- deleted: true,
- title: "removed",
- titleSource: "custom",
- createdAt: 1,
- updatedAt: 2,
- lastOutcome: "error",
- },
- { sessionId: "missing", exists: false, deleted: false },
- ],
- };
- },
- });
- expect(
- await gateway.reconcileSessions({ workspaceId: "/w", sessionIds: ["deleted", "missing"] }),
- ).toMatchObject({ sessions: [{ deleted: true }, { exists: false }] });
- expect(received).toEqual({ workspaceId: "/w", sessionIds: ["deleted", "missing"] });
- await expect(
- gateway.reconcileSessions({ workspaceId: "/w", sessionIds: Array(257).fill("s") }),
- ).rejects.toThrow();
- gateway.dispose();
- });
- });
- // ── 工作流进度事件的 fan-out 节流(14-sessions-index「事件 fan-out 节奏」)──
- // 现场事故:一次 run 在 8s 内发出 4000 条 DynamicWorkflowRunProgress,每条都 bump
- // record.updatedAt,旧实现于是条条 flush 一帧 sessions-index 增量 → 宿主重算 task 行 →
- // renderer 按事件频率重拉三份任务列表。节流只改帧节奏,终态仍在一个窗口内送达。
- describe("ConversationV4Gateway sessions-index 工作流进度节流", () => {
- beforeEach(() => vi.useFakeTimers());
- afterEach(() => vi.useRealTimers());
- /** 与 WORKFLOW_PROGRESS_INDEX_FANOUT_MS 对齐;测试不导入常量,窗口值本身也是被测事实。 */
- const WINDOW_MS = 250;
- const RUN_ID = "dwfrun-1";
- function progress(
- seq: number,
- eventType: string,
- payload: Record<string, unknown>,
- ): SessionEvent {
- return event("live-1", seq, SessionEventType.DynamicWorkflowRunProgress, {
- runId: RUN_ID,
- toolCallId: "tc-1",
- sequence: seq,
- eventType,
- payload,
- });
- }
- /** 建好一个在跑的 run,并把建 run 时开的那扇窗关掉:断言从静默态开始。 */
- function setup(): { stub: Stub; gateway: ReturnType<typeof makeGateway> } {
- const stub: Stub = {
- frames: [],
- sessions: new Set(["live-1"]),
- sessionWorkspace: new Map([["live-1", "ws-1"]]),
- stored: [],
- lastActivityAt: 1000,
- };
- const gateway = makeGateway(stub, { withIndexHooks: true });
- gateway.ingest(
- "live-1",
- event("live-1", 1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- );
- gateway.ingest(
- "live-1",
- progress(2, "run-started", { runId: RUN_ID, caps: { maxConcurrency: 4 } }),
- );
- vi.advanceTimersByTime(WINDOW_MS);
- return { stub, gateway };
- }
- /** 模拟 onSessionEvent 的 record.updatedAt bump,再喂事件。 */
- function ingestProgress(
- stub: Stub,
- gateway: ReturnType<typeof makeGateway>,
- seq: number,
- eventType: string,
- payload: Record<string, unknown>,
- ): void {
- stub.lastActivityAt = (stub.lastActivityAt ?? 0) + 1;
- gateway.ingest("live-1", progress(seq, eventType, payload));
- }
- function lastUpsertedSummary(stub: Stub): SessionSummary | undefined {
- for (const frame of [...indexFrames(stub)].reverse()) {
- if (frame.payload.kind !== "deltas") continue;
- for (const delta of [...frame.payload.deltas].reverse()) {
- if (delta.op === "session.upserted") return delta.session;
- }
- }
- return undefined;
- }
- async function subscribed(stub: Stub, gateway: ReturnType<typeof makeGateway>): Promise<number> {
- await gateway.subscribeSessionsIndex({
- topic: "sessions-index/ws-1",
- connectionId: "conn-1",
- clientMode: "desktop-continuous",
- });
- return indexFrames(stub).length;
- }
- it("一窗内的 N 条进度只出 1 帧立即 + 1 帧窗末,窗末帧带最终摘要", async () => {
- const { stub, gateway } = setup();
- const before = await subscribed(stub, gateway);
- for (let index = 1; index <= 6; index += 1) {
- ingestProgress(stub, gateway, 2 + index, "phase-entered", { name: `phase-${index}` });
- vi.advanceTimersByTime(10);
- }
- // leading edge:静默后的第一条立即到;其余 5 条合并,窗内不再发帧。
- expect(indexFrames(stub).length).toBe(before + 1);
- expect(lastUpsertedSummary(stub)?.workflowActivity?.runs[0]?.currentPhase).toBe("phase-1");
- vi.advanceTimersByTime(WINDOW_MS);
- expect(indexFrames(stub).length).toBe(before + 2);
- const trailing = lastUpsertedSummary(stub);
- expect(trailing?.workflowActivity?.runs[0]?.currentPhase).toBe("phase-6");
- expect(trailing?.lastActivityAt).toBe(1006);
- // 持续高频:窗末补发后窗口续开,下一窗同样只出 1 帧 → 侧栏运行行不超过 4Hz。
- for (let index = 7; index <= 12; index += 1) {
- ingestProgress(stub, gateway, 2 + index, "phase-entered", { name: `phase-${index}` });
- vi.advanceTimersByTime(10);
- }
- expect(indexFrames(stub).length).toBe(before + 2);
- vi.advanceTimersByTime(WINDOW_MS);
- expect(indexFrames(stub).length).toBe(before + 3);
- expect(lastUpsertedSummary(stub)?.workflowActivity?.runs[0]?.currentPhase).toBe("phase-12");
- });
- it("窗内的非进度事件立即发帧,并满足/取消待发的窗末帧", async () => {
- const { stub, gateway } = setup();
- const before = await subscribed(stub, gateway);
- ingestProgress(stub, gateway, 3, "phase-entered", { name: "phase-1" });
- vi.advanceTimersByTime(10);
- ingestProgress(stub, gateway, 4, "phase-entered", { name: "phase-2" });
- vi.advanceTimersByTime(10);
- expect(indexFrames(stub).length).toBe(before + 1);
- stub.lastActivityAt = (stub.lastActivityAt ?? 0) + 1;
- gateway.ingest(
- "live-1",
- event("live-1", 5, SessionEventType.SessionTitleUpdated, {
- previousTitle: "",
- source: "custom",
- title: "窗内改名",
- }),
- );
- // 非进度事件照旧即时 publish,且捎带了窗内合并的进度。
- expect(indexFrames(stub).length).toBe(before + 2);
- const immediate = lastUpsertedSummary(stub);
- expect(immediate?.title).toBe("窗内改名");
- expect(immediate?.workflowActivity?.runs[0]?.currentPhase).toBe("phase-2");
- // 待发的窗末帧已被它满足:窗口走完不再补一帧空增量。
- vi.advanceTimersByTime(WINDOW_MS * 2);
- expect(indexFrames(stub).length).toBe(before + 2);
- });
- it("dispose 清掉窗口定时器:不再发帧,也不拖住进程", async () => {
- const { stub, gateway } = setup();
- const before = await subscribed(stub, gateway);
- ingestProgress(stub, gateway, 3, "phase-entered", { name: "phase-1" });
- ingestProgress(stub, gateway, 4, "phase-entered", { name: "phase-2" });
- expect(indexFrames(stub).length).toBe(before + 1);
- gateway.dispose();
- vi.advanceTimersByTime(10_000);
- expect(indexFrames(stub).length).toBe(before + 1);
- // 网关自己的定时器(attachment prune、flush、fan-out 窗口)必须一个不剩。
- expect(vi.getTimerCount()).toBe(0);
- });
- it("窗内的 run-settled 在一个窗口内送达终态", async () => {
- const { stub, gateway } = setup();
- const before = await subscribed(stub, gateway);
- ingestProgress(stub, gateway, 3, "phase-entered", { name: "phase-1" });
- vi.advanceTimersByTime(10);
- ingestProgress(stub, gateway, 4, "run-settled", { status: "completed" });
- // 终态也在窗内被合并,所以此刻列表还停在 leading 那一帧。
- expect(indexFrames(stub).length).toBe(before + 1);
- expect(lastUpsertedSummary(stub)?.workflowActivity?.runs[0]?.status).toBe("running");
- vi.advanceTimersByTime(WINDOW_MS);
- expect(indexFrames(stub).length).toBe(before + 2);
- expect(lastUpsertedSummary(stub)?.workflowActivity?.runs[0]?.status).toBe("completed");
- });
- });
|