| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- import { Buffer } from "node:buffer";
- import { describe, expect, it, vi } from "vitest";
- import {
- SessionEventType,
- type EventId,
- type SessionEvent,
- type SessionEventType as SessionEventTypeUnion,
- type SessionId,
- type TraceId,
- type TurnId,
- } from "@zcode/contracts";
- import { PROTOCOL_V4_LIMITS, utf8JsonByteLength } from "@zcode/shared/zcode-protocol-v4";
- import {
- coldHydrationJsonByteLength,
- ConversationTopicPublisher,
- PROJECTION_TERMINAL_RESERVE_BYTES,
- } from "../src/zcode-protocol-v4/conversation-topic-publisher.js";
- const wireByteProbe = vi.hoisted(() => ({
- snapshotBytesOverride: null as ((value: unknown) => number | null) | null,
- inputs: [] as unknown[],
- snapshotSequences: [] as number[],
- }));
- vi.mock("@zcode/shared/zcode-protocol-v4", async (importOriginal) => {
- const actual = await importOriginal<typeof import("@zcode/shared/zcode-protocol-v4")>();
- return {
- ...actual,
- utf8JsonByteLength(value: unknown): number {
- wireByteProbe.inputs.push(value);
- if (
- typeof value === "object" &&
- value !== null &&
- "payload" in value &&
- typeof value.payload === "object" &&
- value.payload !== null &&
- "kind" in value.payload &&
- value.payload.kind === "snapshot" &&
- "snapshot" in value.payload &&
- typeof value.payload.snapshot === "object" &&
- value.payload.snapshot !== null &&
- "seq" in value.payload.snapshot &&
- typeof value.payload.snapshot.seq === "number"
- ) {
- wireByteProbe.snapshotSequences.push(value.payload.snapshot.seq);
- }
- const snapshotBytesOverride = wireByteProbe.snapshotBytesOverride?.(value);
- if (snapshotBytesOverride !== null && snapshotBytesOverride !== undefined) {
- return snapshotBytesOverride;
- }
- return actual.utf8JsonByteLength(value);
- },
- };
- });
- function sessionEvent(
- sequenceNumber: number,
- type: SessionEventTypeUnion,
- payload: unknown,
- turnId?: string,
- ): SessionEvent {
- return {
- id: `event-${sequenceNumber}` as EventId,
- sessionId: "session-1" as SessionId,
- ...(turnId ? { turnId: turnId as TurnId } : {}),
- type,
- timestamp: new Date(1_700_000_000_000 + sequenceNumber),
- traceId: "trace-1" as TraceId,
- sequenceNumber,
- payload,
- };
- }
- function checkpoint(sequenceNumber: number): SessionEvent {
- return sessionEvent(sequenceNumber, SessionEventType.CheckpointCreated, {
- checkpointId: `checkpoint-${sequenceNumber}`,
- messageId: `message-${sequenceNumber}`,
- targetMessageId: `message-${sequenceNumber}`,
- scope: "workspace",
- snapshotRef: `zcode-artifact://session-1/checkpoint-${sequenceNumber}`,
- fileCount: 1,
- });
- }
- function isRecord(value: unknown): value is Record<string, unknown> {
- return typeof value === "object" && value !== null && !Array.isArray(value);
- }
- function snapshotMeasurementCount(): number {
- return wireByteProbe.inputs.filter((value) => {
- if (!isRecord(value) || !isRecord(value.payload)) return false;
- return value.payload.kind === "snapshot";
- }).length;
- }
- function snapshotSequence(value: unknown): number | null {
- if (!isRecord(value) || !isRecord(value.payload) || value.payload.kind !== "snapshot") {
- return null;
- }
- if (!isRecord(value.payload.snapshot) || typeof value.payload.snapshot.seq !== "number") {
- return null;
- }
- return value.payload.snapshot.seq;
- }
- describe("cold hydration wire payload upper bound", () => {
- it.each([
- ["顶层 undefined", undefined],
- ["ASCII", { kind: "deltas", deltas: [{ text: "plain text" }] }],
- ["中文", { text: "冷启动恢复" }],
- ["emoji", { text: "ready 🚀" }],
- ["JSON 转义", { text: "quote: \\\" slash: \\\\ newline:\n tab:\t" }],
- ["未配对 surrogate", { text: "\ud800" }],
- ])("cold-only byte length 对 %s 保持 shared 精确结果", (_name, value) => {
- expect(coldHydrationJsonByteLength(value)).toBe(utf8JsonByteLength(value));
- });
- it("no-delta checkpoint 只在 candidate 初始化和最终事件精确测量", () => {
- const events = [
- sessionEvent(1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- ...Array.from({ length: 100 }, (_, index) => checkpoint(index + 2)),
- ];
- const strict = new ConversationTopicPublisher("session-1", "epoch-1", { now: () => 0 });
- for (const event of events) strict.ingest(event);
- const cold = new ConversationTopicPublisher("session-1", "epoch-1", { now: () => 0 });
- const byteLengthSpy = vi.spyOn(Buffer, "byteLength");
- const emptyDeltaJson = JSON.stringify({ kind: "deltas", deltas: [] });
- try {
- wireByteProbe.inputs.length = 0;
- cold.rehydrate(events);
- expect(cold.getSnapshot()).toEqual(strict.getSnapshot());
- expect(byteLengthSpy.mock.calls.some(([value]) => value === emptyDeltaJson)).toBe(false);
- expect(snapshotMeasurementCount()).toBe(2);
- } finally {
- byteLengthSpy.mockRestore();
- }
- });
- it("保守增长触及限额时先按当前 snapshot 精确重测,不误回退 strict replay", () => {
- const titleBody = "x".repeat(4 * 1024 * 1024);
- const events = [
- sessionEvent(1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- ...Array.from({ length: 5 }, (_, index) =>
- sessionEvent(index + 2, SessionEventType.SessionTitleUpdated, {
- previousTitle: index === 0 ? "" : `${index - 1}${titleBody}`,
- title: `${index}${titleBody}`,
- source: "custom",
- }),
- ),
- ];
- const publisher = new ConversationTopicPublisher("session-1", "epoch-1", { now: () => 0 });
- wireByteProbe.inputs.length = 0;
- publisher.rehydrate(events);
- expect(snapshotMeasurementCount()).toBeGreaterThan(2);
- const resumed = publisher.subscribe({
- connectionId: "mobile",
- deliveryProfile: "replayable",
- base: { logEpoch: "epoch-1", seq: 1 },
- });
- expect(resumed.ack.mode).toBe("snapshot");
- expect(resumed.frame?.payload.kind).toBe("snapshot");
- expect(utf8JsonByteLength(resumed.frame)).toBeLessThan(
- PROTOCOL_V4_LIMITS.logicalFrameAssemblyMaxBytes,
- );
- });
- it.each([
- { from: 9, to: 10, headroomBytes: 1 },
- { from: 99, to: 100, headroomBytes: 3 },
- ])("sequence $from→$to 位数增长计入上界,并在越界前精确重测", ({ from, to, headroomBytes }) => {
- const events = [checkpoint(from), checkpoint(to), checkpoint(to + 1)];
- const strict = new ConversationTopicPublisher("session-1", "epoch-1", { now: () => 0 });
- for (const event of events) strict.ingest(event);
- const publisher = new ConversationTopicPublisher("session-1", "epoch-1", { now: () => 0 });
- const runtimeLimit =
- PROTOCOL_V4_LIMITS.logicalFrameAssemblyMaxBytes - PROJECTION_TERMINAL_RESERVE_BYTES;
- wireByteProbe.inputs.length = 0;
- wireByteProbe.snapshotSequences.length = 0;
- wireByteProbe.snapshotBytesOverride = () => runtimeLimit - headroomBytes;
- try {
- publisher.rehydrate(events);
- } finally {
- wireByteProbe.snapshotBytesOverride = null;
- }
- expect(wireByteProbe.snapshotSequences).toEqual([0, to, to + 1]);
- expect(publisher.getSnapshot()).toEqual(strict.getSnapshot());
- expect(
- publisher.subscribe({
- connectionId: "mobile",
- deliveryProfile: "replayable",
- base: { logEpoch: "epoch-1", seq: from },
- }).ack.mode,
- ).toBe("snapshot");
- });
- it.each([
- {
- name: "TurnComplete",
- type: SessionEventType.TurnComplete,
- payload: {
- response: "",
- tokenCount: 0,
- toolCallCount: 0,
- duration: 1,
- resultType: "success",
- },
- },
- {
- name: "TurnError",
- type: SessionEventType.TurnError,
- payload: {
- error: { type: "test.error", message: "failed" },
- turnPhase: "model",
- },
- },
- ])("普通事件后紧接 $name 时可使用 terminal reserve", ({ type, payload }) => {
- const events = [
- sessionEvent(1, SessionEventType.SessionCreated, {
- mode: "default",
- contextWindow: 200_000,
- }),
- sessionEvent(
- 2,
- SessionEventType.TurnStarted,
- { turnNumber: 1, input: "第一问", messageId: "user-1" },
- "turn-1",
- ),
- sessionEvent(
- 3,
- SessionEventType.ModelStreaming,
- {
- kind: "text_start",
- delta: "",
- done: false,
- assistantMessageId: "assistant-1",
- },
- "turn-1",
- ),
- sessionEvent(
- 4,
- SessionEventType.ModelStreaming,
- { kind: "text_delta", delta: "第一答", done: false },
- "turn-1",
- ),
- checkpoint(5),
- sessionEvent(6, type, payload, "turn-1"),
- ];
- const strict = new ConversationTopicPublisher("session-1", "epoch-1", { now: () => 0 });
- for (const event of events) strict.ingest(event);
- const publisher = new ConversationTopicPublisher("session-1", "epoch-1", { now: () => 0 });
- const fullLimit = PROTOCOL_V4_LIMITS.logicalFrameAssemblyMaxBytes;
- const runtimeLimit = fullLimit - PROJECTION_TERMINAL_RESERVE_BYTES;
- wireByteProbe.snapshotSequences.length = 0;
- wireByteProbe.snapshotBytesOverride = (value) => {
- const sequenceNumber = snapshotSequence(value);
- if (sequenceNumber === null) return null;
- return sequenceNumber === 6 ? fullLimit : runtimeLimit - 1024 * 1024;
- };
- try {
- publisher.rehydrate(events);
- } finally {
- wireByteProbe.snapshotBytesOverride = null;
- }
- expect(wireByteProbe.snapshotSequences).toEqual([0, 6]);
- expect(publisher.getSnapshot()).toEqual(strict.getSnapshot());
- expect(
- publisher.subscribe({
- connectionId: "mobile",
- deliveryProfile: "replayable",
- base: { logEpoch: "epoch-1", seq: 1 },
- }).ack.mode,
- ).toBe("snapshot");
- });
- it("row.removed 立即重测,最终事件在 actions 收敛后再次重测", () => {
- const events = [
- sessionEvent(1, SessionEventType.SessionCreated, { mode: "default" }),
- sessionEvent(
- 2,
- SessionEventType.TurnStarted,
- { turnNumber: 1, input: "第一问", messageId: "user-1" },
- "turn-1",
- ),
- sessionEvent(
- 3,
- SessionEventType.ModelStreaming,
- {
- kind: "text_start",
- delta: "",
- done: false,
- assistantMessageId: "assistant-1",
- },
- "turn-1",
- ),
- sessionEvent(
- 4,
- SessionEventType.ModelStreaming,
- { kind: "text_delta", delta: "第一答", done: false },
- "turn-1",
- ),
- sessionEvent(
- 5,
- SessionEventType.TurnComplete,
- { response: "", tokenCount: 0, toolCallCount: 0, duration: 1, resultType: "success" },
- "turn-1",
- ),
- sessionEvent(
- 6,
- SessionEventType.TurnStarted,
- { turnNumber: 2, input: "第二问", messageId: "user-2" },
- "turn-2",
- ),
- sessionEvent(
- 7,
- SessionEventType.ModelStreaming,
- {
- kind: "text_start",
- delta: "",
- done: false,
- assistantMessageId: "assistant-2",
- },
- "turn-2",
- ),
- sessionEvent(
- 8,
- SessionEventType.ModelStreaming,
- { kind: "text_delta", delta: "第二答", done: false },
- "turn-2",
- ),
- sessionEvent(
- 9,
- SessionEventType.TurnComplete,
- { response: "", tokenCount: 0, toolCallCount: 0, duration: 1, resultType: "success" },
- "turn-2",
- ),
- sessionEvent(10, SessionEventType.RewindTriggered, {
- targetMessageId: "assistant-2",
- scope: "conversation",
- createdMessageId: "rewind-notice",
- }),
- checkpoint(11),
- ];
- const strict = new ConversationTopicPublisher("session-1", "epoch-1", { now: () => 0 });
- for (const event of events) strict.ingest(event);
- const publisher = new ConversationTopicPublisher("session-1", "epoch-1", { now: () => 0 });
- wireByteProbe.inputs.length = 0;
- wireByteProbe.snapshotSequences.length = 0;
- publisher.rehydrate(events);
- expect(snapshotMeasurementCount()).toBe(3);
- expect(wireByteProbe.snapshotSequences).toEqual([0, 10, 11]);
- expect(publisher.getSnapshot()).toEqual(strict.getSnapshot());
- expect(
- publisher
- .getSnapshot()
- .rows.window.filter((row) => row.kind === "userInput")
- .map((row) => row.text),
- ).toEqual(["第一问"]);
- });
- });
|