session-lifecycle-logging.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { describe, expect, it } from "vitest";
  2. import type { Logger } from "@zcode/contracts";
  3. import { conversationTopic } from "@zcode/shared/zcode-protocol-v4";
  4. import {
  5. activateSessionForResume,
  6. listSessionSubagents,
  7. readSession,
  8. } from "../src/zcode-protocol/server-operations.js";
  9. import type { ZCodeProtocolAgentServerContext } from "../src/zcode-protocol/server-types.js";
  10. import { ConversationV4Gateway } from "../src/zcode-protocol-v4/v4-gateway.js";
  11. import { createConversationV4Gateway } from "../src/zcode-protocol/v4-bridge.js";
  12. interface RecordedLog {
  13. context?: Record<string, unknown>;
  14. level: "debug" | "error" | "info" | "warn";
  15. message: string;
  16. }
  17. function createRecordingLogger(logs: RecordedLog[]): Logger {
  18. const logger = {
  19. child: () => logger,
  20. debug: (message: string, context?: Record<string, unknown>) =>
  21. logs.push({ level: "debug", message, context }),
  22. error: (message: string, _error?: Error, context?: Record<string, unknown>) =>
  23. logs.push({ level: "error", message, context }),
  24. info: (message: string, context?: Record<string, unknown>) =>
  25. logs.push({ level: "info", message, context }),
  26. warn: (message: string, context?: Record<string, unknown>) =>
  27. logs.push({ level: "warn", message, context }),
  28. } as Logger;
  29. return logger;
  30. }
  31. function contextWithLogger(
  32. logger: Logger,
  33. overrides: Record<string, unknown> = {},
  34. ): ZCodeProtocolAgentServerContext {
  35. return {
  36. deps: {},
  37. sessions: new Map(),
  38. logger,
  39. ...overrides,
  40. } as unknown as ZCodeProtocolAgentServerContext;
  41. }
  42. describe("session lifecycle diagnostics", () => {
  43. it("records the active-runtime miss before readSession reports Session is not active", async () => {
  44. const logs: RecordedLog[] = [];
  45. const context = contextWithLogger(createRecordingLogger(logs));
  46. await expect(
  47. readSession(context, {
  48. deliveryKind: "desktop-continuous",
  49. sessionId: "sess_missing",
  50. }),
  51. ).rejects.toThrow("Session is not active: sess_missing");
  52. expect(logs).toContainEqual(
  53. expect.objectContaining({
  54. level: "warn",
  55. context: expect.objectContaining({
  56. event: "zcode_protocol.session.require_missing",
  57. deliveryKind: "desktop-continuous",
  58. operation: "session_read",
  59. sessionId: "sess_missing",
  60. }),
  61. }),
  62. );
  63. });
  64. it("records the persisted-session miss when cold resume cannot find the id", async () => {
  65. const logs: RecordedLog[] = [];
  66. const context = contextWithLogger(createRecordingLogger(logs), {
  67. deps: {
  68. sessionStore: {
  69. getSession: async () => null,
  70. },
  71. },
  72. });
  73. await expect(activateSessionForResume(context, { sessionId: "sess_missing" })).rejects.toThrow(
  74. "Session not found: sess_missing",
  75. );
  76. expect(logs).toContainEqual(
  77. expect.objectContaining({
  78. level: "warn",
  79. context: expect.objectContaining({
  80. event: "zcode_protocol.session.resume_persisted_missing",
  81. sessionId: "sess_missing",
  82. }),
  83. }),
  84. );
  85. });
  86. it("records the durable parent miss that surfaces through v4 hydration", async () => {
  87. const logs: RecordedLog[] = [];
  88. const context = contextWithLogger(createRecordingLogger(logs), {
  89. deps: {
  90. sessionStore: {
  91. getSession: async () => null,
  92. },
  93. },
  94. });
  95. await expect(
  96. listSessionSubagents(context, { sessionId: "sess_missing", endedLimit: 1 }),
  97. ).rejects.toThrow("Session not found: sess_missing");
  98. expect(logs).toContainEqual(
  99. expect.objectContaining({
  100. level: "warn",
  101. context: expect.objectContaining({
  102. event: "zcode_protocol.session.persisted_missing",
  103. operation: "session_subagents",
  104. sessionId: "sess_missing",
  105. }),
  106. }),
  107. );
  108. });
  109. it("includes the session and phase when the gateway hydration source fails", async () => {
  110. const errors: Array<{
  111. context?: Record<string, unknown>;
  112. error: unknown;
  113. scope: string;
  114. }> = [];
  115. const gateway = new ConversationV4Gateway({
  116. sessionExists: () => true,
  117. executeCommand: async () => undefined,
  118. emitWireFrame: () => undefined,
  119. loadPersistedEvents: async () => {
  120. throw new Error("Session not found: sess_missing");
  121. },
  122. onError: (scope, error, context) => errors.push({ scope, error, context }),
  123. });
  124. await gateway.subscribe({
  125. topic: conversationTopic("sess_missing"),
  126. connectionId: "conn-test",
  127. clientMode: "desktop-continuous",
  128. });
  129. expect(errors).toContainEqual(
  130. expect.objectContaining({
  131. scope: "v4.hydrate",
  132. context: expect.objectContaining({
  133. phase: "loadPersistedEvents",
  134. sessionId: "sess_missing",
  135. }),
  136. }),
  137. );
  138. });
  139. it("records a missing persisted session before v4 cold resume returns notFound", async () => {
  140. const logs: RecordedLog[] = [];
  141. const context = contextWithLogger(createRecordingLogger(logs), {
  142. deps: {
  143. sessionStore: {
  144. getSession: async () => null,
  145. },
  146. },
  147. });
  148. const gateway = createConversationV4Gateway(context);
  149. await expect(
  150. gateway.subscribe({
  151. topic: conversationTopic("sess_missing"),
  152. connectionId: "conn-test",
  153. clientMode: "desktop-continuous",
  154. }),
  155. ).rejects.toThrow("Session is not active and not persisted: sess_missing");
  156. expect(logs).toContainEqual(
  157. expect.objectContaining({
  158. level: "warn",
  159. context: expect.objectContaining({
  160. event: "zcode_protocol.v4.resume_persisted_missing",
  161. sessionId: "sess_missing",
  162. }),
  163. }),
  164. );
  165. });
  166. it("includes the session and phase when cold resume itself fails", async () => {
  167. const errors: Array<{
  168. context?: Record<string, unknown>;
  169. error: unknown;
  170. scope: string;
  171. }> = [];
  172. const gateway = new ConversationV4Gateway({
  173. sessionExists: () => false,
  174. executeCommand: async () => undefined,
  175. emitWireFrame: () => undefined,
  176. resumePersistedSession: async () => {
  177. throw new Error("activation failed");
  178. },
  179. onError: (scope, error, context) => errors.push({ scope, error, context }),
  180. });
  181. await expect(
  182. gateway.subscribe({
  183. topic: conversationTopic("sess_resume_failed"),
  184. connectionId: "conn-test",
  185. clientMode: "desktop-continuous",
  186. }),
  187. ).rejects.toThrow("Failed to resume persisted session sess_resume_failed");
  188. expect(errors).toContainEqual(
  189. expect.objectContaining({
  190. scope: "v4.subscribe.resume",
  191. context: expect.objectContaining({
  192. phase: "resumePersistedSession",
  193. sessionId: "sess_resume_failed",
  194. }),
  195. }),
  196. );
  197. });
  198. });