| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- import { describe, expect, it } from "vitest";
- import type { Logger } from "@zcode/contracts";
- import { conversationTopic } from "@zcode/shared/zcode-protocol-v4";
- import {
- activateSessionForResume,
- listSessionSubagents,
- readSession,
- } from "../src/zcode-protocol/server-operations.js";
- import type { ZCodeProtocolAgentServerContext } from "../src/zcode-protocol/server-types.js";
- import { ConversationV4Gateway } from "../src/zcode-protocol-v4/v4-gateway.js";
- import { createConversationV4Gateway } from "../src/zcode-protocol/v4-bridge.js";
- interface RecordedLog {
- context?: Record<string, unknown>;
- level: "debug" | "error" | "info" | "warn";
- message: string;
- }
- function createRecordingLogger(logs: RecordedLog[]): Logger {
- const logger = {
- child: () => logger,
- debug: (message: string, context?: Record<string, unknown>) =>
- logs.push({ level: "debug", message, context }),
- error: (message: string, _error?: Error, context?: Record<string, unknown>) =>
- logs.push({ level: "error", message, context }),
- info: (message: string, context?: Record<string, unknown>) =>
- logs.push({ level: "info", message, context }),
- warn: (message: string, context?: Record<string, unknown>) =>
- logs.push({ level: "warn", message, context }),
- } as Logger;
- return logger;
- }
- function contextWithLogger(
- logger: Logger,
- overrides: Record<string, unknown> = {},
- ): ZCodeProtocolAgentServerContext {
- return {
- deps: {},
- sessions: new Map(),
- logger,
- ...overrides,
- } as unknown as ZCodeProtocolAgentServerContext;
- }
- describe("session lifecycle diagnostics", () => {
- it("records the active-runtime miss before readSession reports Session is not active", async () => {
- const logs: RecordedLog[] = [];
- const context = contextWithLogger(createRecordingLogger(logs));
- await expect(
- readSession(context, {
- deliveryKind: "desktop-continuous",
- sessionId: "sess_missing",
- }),
- ).rejects.toThrow("Session is not active: sess_missing");
- expect(logs).toContainEqual(
- expect.objectContaining({
- level: "warn",
- context: expect.objectContaining({
- event: "zcode_protocol.session.require_missing",
- deliveryKind: "desktop-continuous",
- operation: "session_read",
- sessionId: "sess_missing",
- }),
- }),
- );
- });
- it("records the persisted-session miss when cold resume cannot find the id", async () => {
- const logs: RecordedLog[] = [];
- const context = contextWithLogger(createRecordingLogger(logs), {
- deps: {
- sessionStore: {
- getSession: async () => null,
- },
- },
- });
- await expect(activateSessionForResume(context, { sessionId: "sess_missing" })).rejects.toThrow(
- "Session not found: sess_missing",
- );
- expect(logs).toContainEqual(
- expect.objectContaining({
- level: "warn",
- context: expect.objectContaining({
- event: "zcode_protocol.session.resume_persisted_missing",
- sessionId: "sess_missing",
- }),
- }),
- );
- });
- it("records the durable parent miss that surfaces through v4 hydration", async () => {
- const logs: RecordedLog[] = [];
- const context = contextWithLogger(createRecordingLogger(logs), {
- deps: {
- sessionStore: {
- getSession: async () => null,
- },
- },
- });
- await expect(
- listSessionSubagents(context, { sessionId: "sess_missing", endedLimit: 1 }),
- ).rejects.toThrow("Session not found: sess_missing");
- expect(logs).toContainEqual(
- expect.objectContaining({
- level: "warn",
- context: expect.objectContaining({
- event: "zcode_protocol.session.persisted_missing",
- operation: "session_subagents",
- sessionId: "sess_missing",
- }),
- }),
- );
- });
- it("includes the session and phase when the gateway hydration source fails", async () => {
- const errors: Array<{
- context?: Record<string, unknown>;
- error: unknown;
- scope: string;
- }> = [];
- const gateway = new ConversationV4Gateway({
- sessionExists: () => true,
- executeCommand: async () => undefined,
- emitWireFrame: () => undefined,
- loadPersistedEvents: async () => {
- throw new Error("Session not found: sess_missing");
- },
- onError: (scope, error, context) => errors.push({ scope, error, context }),
- });
- await gateway.subscribe({
- topic: conversationTopic("sess_missing"),
- connectionId: "conn-test",
- clientMode: "desktop-continuous",
- });
- expect(errors).toContainEqual(
- expect.objectContaining({
- scope: "v4.hydrate",
- context: expect.objectContaining({
- phase: "loadPersistedEvents",
- sessionId: "sess_missing",
- }),
- }),
- );
- });
- it("records a missing persisted session before v4 cold resume returns notFound", async () => {
- const logs: RecordedLog[] = [];
- const context = contextWithLogger(createRecordingLogger(logs), {
- deps: {
- sessionStore: {
- getSession: async () => null,
- },
- },
- });
- const gateway = createConversationV4Gateway(context);
- await expect(
- gateway.subscribe({
- topic: conversationTopic("sess_missing"),
- connectionId: "conn-test",
- clientMode: "desktop-continuous",
- }),
- ).rejects.toThrow("Session is not active and not persisted: sess_missing");
- expect(logs).toContainEqual(
- expect.objectContaining({
- level: "warn",
- context: expect.objectContaining({
- event: "zcode_protocol.v4.resume_persisted_missing",
- sessionId: "sess_missing",
- }),
- }),
- );
- });
- it("includes the session and phase when cold resume itself fails", async () => {
- const errors: Array<{
- context?: Record<string, unknown>;
- error: unknown;
- scope: string;
- }> = [];
- const gateway = new ConversationV4Gateway({
- sessionExists: () => false,
- executeCommand: async () => undefined,
- emitWireFrame: () => undefined,
- resumePersistedSession: async () => {
- throw new Error("activation failed");
- },
- onError: (scope, error, context) => errors.push({ scope, error, context }),
- });
- await expect(
- gateway.subscribe({
- topic: conversationTopic("sess_resume_failed"),
- connectionId: "conn-test",
- clientMode: "desktop-continuous",
- }),
- ).rejects.toThrow("Failed to resume persisted session sess_resume_failed");
- expect(errors).toContainEqual(
- expect.objectContaining({
- scope: "v4.subscribe.resume",
- context: expect.objectContaining({
- phase: "resumePersistedSession",
- sessionId: "sess_resume_failed",
- }),
- }),
- );
- });
- });
|