persisted-remote-session-path-repair.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. CoreErrorType,
  4. createProjectId,
  5. createSessionId,
  6. type SessionInfo,
  7. type SessionStorePort,
  8. type UpdateSessionInput,
  9. type WorkspaceId,
  10. } from "@zcode/contracts";
  11. import {
  12. REMOTE_SESSION_PATH_CORRUPTION_REASON,
  13. repairPersistedRemoteSessionPaths,
  14. } from "../src/runtime/helpers/persisted-remote-session-path-repair.js";
  15. function createSession(overrides: Partial<SessionInfo> = {}): SessionInfo {
  16. return {
  17. id: createSessionId("remote-path-repair"),
  18. projectID: createProjectId("remote-path-repair"),
  19. workspaceID: "remote:wsl:Debian:dev:/home/dev/project" as WorkspaceId,
  20. taskType: "interactive",
  21. slug: "remote-path-repair",
  22. directory: "/home/dev/project",
  23. path: "/home/dev/project",
  24. title: "Remote path repair",
  25. version: "0.1.0",
  26. time: { created: 1, updated: 1 },
  27. ...overrides,
  28. };
  29. }
  30. function createUpdater(session: SessionInfo) {
  31. let persisted = session;
  32. const updateSession = vi.fn(
  33. async (input: UpdateSessionInput): Promise<SessionInfo> => ({
  34. ...session,
  35. ...(input.directory !== undefined ? { directory: input.directory } : {}),
  36. ...(input.path !== undefined ? { path: input.path === null ? undefined : input.path } : {}),
  37. }),
  38. );
  39. const repairRemoteSessionPaths = vi.fn(
  40. async (input: {
  41. sessionID: SessionInfo["id"];
  42. workspaceID: WorkspaceId;
  43. expectedDirectory: string;
  44. expectedPath: string | null;
  45. directory: string;
  46. path: string | null;
  47. timeUpdated: number;
  48. }): Promise<boolean> => {
  49. persisted = {
  50. ...persisted,
  51. directory: input.directory,
  52. ...(input.path === null ? { path: undefined } : { path: input.path }),
  53. };
  54. return true;
  55. },
  56. );
  57. const getSession = vi.fn(async () => persisted);
  58. return {
  59. store: { getSession, repairRemoteSessionPaths, updateSession } as Pick<
  60. SessionStorePort,
  61. "getSession" | "updateSession"
  62. > & { repairRemoteSessionPaths: typeof repairRemoteSessionPaths },
  63. getSession,
  64. repairRemoteSessionPaths,
  65. updateSession,
  66. };
  67. }
  68. describe("repairPersistedRemoteSessionPaths", () => {
  69. it("repairs and persists the known WSL identity-appended paths", async () => {
  70. const identity = "remote:wsl:Debian:dev:/home/dev/project";
  71. const polluted = `/home/dev/project/${identity}`;
  72. const session = createSession({ directory: polluted, path: polluted });
  73. const { store, repairRemoteSessionPaths, updateSession } = createUpdater(session);
  74. const repaired = await repairPersistedRemoteSessionPaths(store, session);
  75. expect(repairRemoteSessionPaths).toHaveBeenCalledWith({
  76. sessionID: session.id,
  77. workspaceID: session.workspaceID,
  78. expectedDirectory: polluted,
  79. expectedPath: polluted,
  80. directory: "/home/dev/project",
  81. path: "/home/dev/project",
  82. timeUpdated: session.time.updated,
  83. });
  84. expect(updateSession).not.toHaveBeenCalled();
  85. expect(repaired).toMatchObject({ directory: "/home/dev/project", path: "/home/dev/project" });
  86. });
  87. it("repairs the identity-only cwd form for SSH sessions", async () => {
  88. const identity = "remote:ssh:dev.example.com:22:dev:/srv/project";
  89. const session = createSession({
  90. workspaceID: identity as WorkspaceId,
  91. directory: identity,
  92. path: "/srv/project",
  93. });
  94. const { store, repairRemoteSessionPaths, updateSession } = createUpdater(session);
  95. await repairPersistedRemoteSessionPaths(store, session);
  96. expect(repairRemoteSessionPaths).toHaveBeenCalledWith({
  97. sessionID: session.id,
  98. workspaceID: session.workspaceID,
  99. expectedDirectory: identity,
  100. expectedPath: "/srv/project",
  101. directory: "/srv/project",
  102. path: "/srv/project",
  103. timeUpdated: session.time.updated,
  104. });
  105. expect(updateSession).not.toHaveBeenCalled();
  106. });
  107. it("continues with the in-memory repair when persistence fails", async () => {
  108. const identity = "remote:wsl:Debian:dev:/home/dev/project";
  109. const polluted = `/home/dev/project/${identity}`;
  110. const session = createSession({ directory: polluted, path: polluted });
  111. const persistenceError = new Error("database is locked");
  112. const onPersistenceFailure = vi.fn();
  113. const updateSession = vi.fn(async (): Promise<SessionInfo> => session);
  114. const repairRemoteSessionPaths = vi.fn(async (): Promise<boolean> => {
  115. throw persistenceError;
  116. });
  117. const repaired = await repairPersistedRemoteSessionPaths(
  118. {
  119. getSession: vi.fn(async () => session),
  120. repairRemoteSessionPaths,
  121. updateSession,
  122. } as Pick<SessionStorePort, "getSession" | "updateSession"> & {
  123. repairRemoteSessionPaths: typeof repairRemoteSessionPaths;
  124. },
  125. session,
  126. { onPersistenceFailure },
  127. );
  128. expect(repaired).toMatchObject({ directory: "/home/dev/project", path: "/home/dev/project" });
  129. expect(onPersistenceFailure).toHaveBeenCalledWith(persistenceError);
  130. expect(updateSession).not.toHaveBeenCalled();
  131. });
  132. it("re-reads a CAS miss and keeps metadata from a concurrent successful repair", async () => {
  133. const identity = "remote:wsl:Debian:dev:/home/dev/project" as WorkspaceId;
  134. const session = createSession({ directory: identity, path: identity });
  135. const concurrent = createSession({
  136. directory: "/home/dev/project",
  137. path: "/home/dev/project",
  138. title: "并发更新后的标题",
  139. time: { ...session.time, updated: 99 },
  140. });
  141. const updateSession = vi.fn(async (): Promise<SessionInfo> => session);
  142. const repairRemoteSessionPaths = vi.fn(async () => false);
  143. const getSession = vi.fn(async () => concurrent);
  144. const repaired = await repairPersistedRemoteSessionPaths(
  145. { getSession, repairRemoteSessionPaths, updateSession } as Pick<
  146. SessionStorePort,
  147. "getSession" | "updateSession"
  148. > & { repairRemoteSessionPaths: typeof repairRemoteSessionPaths },
  149. session,
  150. );
  151. expect(repaired).toBe(concurrent);
  152. expect(repaired.title).toBe("并发更新后的标题");
  153. expect(getSession).toHaveBeenCalledWith(session.id);
  154. expect(updateSession).not.toHaveBeenCalled();
  155. });
  156. it("leaves clean, local, and unrelated remote subdirectory paths unchanged", async () => {
  157. const sessions = [
  158. createSession(),
  159. createSession({ workspaceID: undefined, directory: "C:\\project" }),
  160. createSession({
  161. directory: "/home/dev/project/packages/core",
  162. path: "/home/dev/project/packages/core",
  163. }),
  164. ];
  165. for (const session of sessions) {
  166. const { store, updateSession } = createUpdater(session);
  167. await expect(repairPersistedRemoteSessionPaths(store, session)).resolves.toBe(session);
  168. expect(updateSession).not.toHaveBeenCalled();
  169. }
  170. });
  171. it("reports ambiguous identity contamination as session corruption", async () => {
  172. const identity = "remote:wsl:Debian:dev:/home/dev/project";
  173. const session = createSession({ directory: `/unexpected/prefix/${identity}` });
  174. const { store, updateSession } = createUpdater(session);
  175. await expect(repairPersistedRemoteSessionPaths(store, session)).rejects.toMatchObject({
  176. type: CoreErrorType.SessionCorrupted,
  177. context: { reason: REMOTE_SESSION_PATH_CORRUPTION_REASON, sessionId: session.id },
  178. recoverable: true,
  179. });
  180. expect(updateSession).not.toHaveBeenCalled();
  181. });
  182. it("reports malformed persisted remote identity as session corruption", async () => {
  183. const session = createSession({
  184. workspaceID: "remote:wsl:Debian" as WorkspaceId,
  185. directory: "/home/dev/project",
  186. });
  187. const { store, updateSession } = createUpdater(session);
  188. await expect(repairPersistedRemoteSessionPaths(store, session)).rejects.toMatchObject({
  189. type: CoreErrorType.SessionCorrupted,
  190. context: { reason: REMOTE_SESSION_PATH_CORRUPTION_REASON, sessionId: session.id },
  191. recoverable: true,
  192. });
  193. expect(updateSession).not.toHaveBeenCalled();
  194. });
  195. });