|
| 1 | +/** |
| 2 | + * "I could not tell" must never read as "there is nothing there" — over the REAL |
| 3 | + * native discovery path. |
| 4 | + * |
| 5 | + * Three production behaviours each collapse an undecidable read into an empty |
| 6 | + * list, and any of them would let a second review agent open beside a live one: |
| 7 | + * |
| 8 | + * • `NativeTerminalBackend.readPaneSet` catches every recovery exception and |
| 9 | + * returns `null`; |
| 10 | + * • `nativeTaskPaneCommands` turns a `null` pane set into `[]`; |
| 11 | + * • `nativeTaskPaneCommandsOf` turns a pane whose own record is unreadable into |
| 12 | + * `command: []`, so it matches no marker. |
| 13 | + * |
| 14 | + * So nothing here mocks the module under test. The registry directory is real, the |
| 15 | + * records are real files, and the failures are injected where they actually happen: |
| 16 | + * in `recoverPaneSet` and in the per-pane record on disk. |
| 17 | + */ |
| 18 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 19 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 20 | +import { tmpdir } from "node:os"; |
| 21 | +import { join } from "node:path"; |
| 22 | +import { NATIVE_SESSIONS_DIR_ENV, recordFile, sessionDir } from "../native-terminal-registry/paths"; |
| 23 | +import { |
| 24 | + NATIVE_SESSION_SCHEMA_VERSION, |
| 25 | + writeRecordAtomic, |
| 26 | + type NativeSessionRecord, |
| 27 | +} from "../native-terminal-registry/record"; |
| 28 | +import type { Task } from "../../shared/types"; |
| 29 | + |
| 30 | +const TASK_ID = "dddddddd-0000-0000-0000-000000000004"; |
| 31 | +const SOCKET = "dev3-sock"; |
| 32 | +const nativeTask = { id: TASK_ID, seq: 909, terminalBackend: "native" } as unknown as Task; |
| 33 | + |
| 34 | +/** What the coordinator's recovery does when asked for this task's pane set. */ |
| 35 | +let recovery: () => Promise<{ panes: { paneId: string; sessionId: string }[] } | null>; |
| 36 | + |
| 37 | +vi.mock("../logger", () => ({ |
| 38 | + createLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }), |
| 39 | +})); |
| 40 | +vi.mock("../spawn", () => ({ spawn: vi.fn(), spawnSync: vi.fn() })); |
| 41 | +vi.mock("../tmux", () => ({ |
| 42 | + PANE_START_COMMAND_FORMAT: { formatString: "", parse: () => [] }, |
| 43 | + TmuxError: class extends Error {}, |
| 44 | + taskSessionName: (taskId: string) => `dev3-${taskId.slice(0, 8)}`, |
| 45 | + tmux: { |
| 46 | + listPanes: vi.fn(() => { |
| 47 | + throw new Error("a native task must not reach tmux"); |
| 48 | + }), |
| 49 | + splitWindow: vi.fn(() => { |
| 50 | + throw new Error("a native task must not reach tmux"); |
| 51 | + }), |
| 52 | + selectPane: vi.fn(), |
| 53 | + killPane: vi.fn(() => { |
| 54 | + throw new Error("a native task must not reach tmux"); |
| 55 | + }), |
| 56 | + }, |
| 57 | +})); |
| 58 | + |
| 59 | +// The ONE injection point: the coordinator's own recovery. Everything above it — |
| 60 | +// readPaneSet's catch, buildState's null handling, the per-pane record read — is |
| 61 | +// the real production code. |
| 62 | +vi.mock("../native-terminal-multipane/coordinator", async (importOriginal) => { |
| 63 | + const actual = await importOriginal<typeof import("../native-terminal-multipane/coordinator")>(); |
| 64 | + return { |
| 65 | + ...actual, |
| 66 | + NativeMultipaneCoordinator: class { |
| 67 | + static async recoverPaneSet() { |
| 68 | + const recovered = await recovery(); |
| 69 | + if (!recovered) return null; |
| 70 | + return { |
| 71 | + coordinator: { layout: { activePaneId: recovered.panes[0]?.paneId ?? "", root: null } }, |
| 72 | + panes: recovered.panes.map(({ paneId, sessionId }) => ({ |
| 73 | + paneId, |
| 74 | + sessionId, |
| 75 | + hostPid: 1, |
| 76 | + shellPid: 2, |
| 77 | + cols: 80, |
| 78 | + rows: 24, |
| 79 | + state: "alive", |
| 80 | + })), |
| 81 | + }; |
| 82 | + } |
| 83 | + }, |
| 84 | + }; |
| 85 | +}); |
| 86 | +vi.mock("../../shared/split-tree", async (importOriginal) => ({ |
| 87 | + ...(await importOriginal<typeof import("../../shared/split-tree")>()), |
| 88 | + serializeSplitTree: () => "layout", |
| 89 | +})); |
| 90 | + |
| 91 | +const { |
| 92 | + auxPaneMarker, |
| 93 | + findAuxPanes, |
| 94 | + openAuxPane, |
| 95 | + AuxPaneUndecidableError, |
| 96 | +} = await import("../task-aux-panes"); |
| 97 | +const { splitNativeTaskPane } = await import("../native-task-panes"); |
| 98 | +const nativePanes = await import("../native-task-panes"); |
| 99 | + |
| 100 | +let sessionsDir: string; |
| 101 | + |
| 102 | +function record(sessionId: string, paneId: string, command: string[]): NativeSessionRecord { |
| 103 | + return { |
| 104 | + schemaVersion: NATIVE_SESSION_SCHEMA_VERSION, |
| 105 | + sessionId, |
| 106 | + paneId, |
| 107 | + protocolVersion: 1, |
| 108 | + hostArtifactVersion: "1", |
| 109 | + runtimeVersion: "1.3.14", |
| 110 | + platform: "darwin", |
| 111 | + host: { pid: 1, executable: "/bin/bun", startSignature: "1@t0" }, |
| 112 | + shell: { pid: 2, command, startSignature: "2@t0" }, |
| 113 | + endpoint: { transport: "ws", address: "127.0.0.1", port: 51234 }, |
| 114 | + ownership: { evidenceKind: "posix-start-signature" }, |
| 115 | + cols: 80, |
| 116 | + rows: 24, |
| 117 | + createdAt: "2026-08-02T00:00:00.000Z", |
| 118 | + updatedAt: "2026-08-02T00:00:00.000Z", |
| 119 | + }; |
| 120 | +} |
| 121 | + |
| 122 | +function columnSpec() { |
| 123 | + const marker = auxPaneMarker(TASK_ID, "columnAgent"); |
| 124 | + return { |
| 125 | + task: nativeTask, |
| 126 | + purpose: "columnAgent" as const, |
| 127 | + placement: "right" as const, |
| 128 | + size: "40%", |
| 129 | + cwd: "/tmp/wt", |
| 130 | + socket: SOCKET, |
| 131 | + title: "AI Review", |
| 132 | + tmuxCommand: `bash "${marker}"`, |
| 133 | + nativeLaunch: { executable: "/bin/bash", argv: [marker] }, |
| 134 | + }; |
| 135 | +} |
| 136 | + |
| 137 | +beforeEach(() => { |
| 138 | + vi.clearAllMocks(); |
| 139 | + sessionsDir = mkdtempSync(join(tmpdir(), "dev3-strict-discovery-")); |
| 140 | + process.env[NATIVE_SESSIONS_DIR_ENV] = sessionsDir; |
| 141 | + nativePanes._resetBackendForTests(); |
| 142 | + recovery = async () => null; |
| 143 | +}); |
| 144 | + |
| 145 | +afterEach(() => { |
| 146 | + delete process.env[NATIVE_SESSIONS_DIR_ENV]; |
| 147 | + rmSync(sessionsDir, { recursive: true, force: true }); |
| 148 | +}); |
| 149 | + |
| 150 | +describe("strict native discovery for a proven replacement", () => { |
| 151 | + it("refuses when recovery throws, instead of reading it as an empty pane set", async () => { |
| 152 | + recovery = async () => { |
| 153 | + throw new Error("ownership sweep failed"); |
| 154 | + }; |
| 155 | + |
| 156 | + // The tolerant read is what production does elsewhere, and it hides this. |
| 157 | + await expect(nativePanes.nativeTaskPaneCommands(TASK_ID)).resolves.toEqual([]); |
| 158 | + // The replacement path must not accept that answer. |
| 159 | + await expect(findAuxPanes(nativeTask, "columnAgent", SOCKET, { strict: true })).rejects.toThrow(/ownership sweep/); |
| 160 | + await expect(openAuxPane(columnSpec())).rejects.toBeInstanceOf(AuxPaneUndecidableError); |
| 161 | + expect(splitNativeTaskPane).toBeDefined(); |
| 162 | + }); |
| 163 | + |
| 164 | + it("refuses when a pane's own launch command cannot be read", async () => { |
| 165 | + const marker = auxPaneMarker(TASK_ID, "columnAgent"); |
| 166 | + writeRecordAtomic(record("sess-agent", "pane-1", ["/bin/zsh"])); |
| 167 | + writeRecordAtomic(record("sess-review", "pane-9", ["/bin/bash", marker])); |
| 168 | + // A record that exists but cannot be parsed — the case that silently becomes |
| 169 | + // `command: []` and therefore matches no marker. |
| 170 | + writeFileSync(join(sessionDir("sess-review"), "record.json"), "{ not json"); |
| 171 | + recovery = async () => ({ |
| 172 | + panes: [ |
| 173 | + { paneId: "pane-1", sessionId: "sess-agent" }, |
| 174 | + { paneId: "pane-9", sessionId: "sess-review" }, |
| 175 | + ], |
| 176 | + }); |
| 177 | + |
| 178 | + // Tolerant read: the review pane looks like it is not there at all. |
| 179 | + const tolerant = await nativePanes.nativeTaskPaneCommands(TASK_ID); |
| 180 | + expect(tolerant.find((pane) => pane.paneId === "pane-9")?.command).toEqual([]); |
| 181 | + // Strict read refuses rather than opening a second agent beside it. |
| 182 | + await expect(openAuxPane(columnSpec())).rejects.toBeInstanceOf(AuxPaneUndecidableError); |
| 183 | + }); |
| 184 | + |
| 185 | + it("treats a genuinely absent pane set as owning nothing, not as undecidable", async () => { |
| 186 | + recovery = async () => null; |
| 187 | + |
| 188 | + await expect(findAuxPanes(nativeTask, "columnAgent", SOCKET, { strict: true })).resolves.toEqual([]); |
| 189 | + }); |
| 190 | + |
| 191 | + it("keeps the tolerant read tolerant for the best-effort purposes", async () => { |
| 192 | + recovery = async () => { |
| 193 | + throw new Error("ownership sweep failed"); |
| 194 | + }; |
| 195 | + |
| 196 | + await expect(findAuxPanes(nativeTask, "devServer", SOCKET)).resolves.toEqual([]); |
| 197 | + }); |
| 198 | + |
| 199 | + it("reads a healthy pane set through the real record files", async () => { |
| 200 | + const marker = auxPaneMarker(TASK_ID, "columnAgent"); |
| 201 | + writeRecordAtomic(record("sess-agent", "pane-1", ["/bin/zsh"])); |
| 202 | + writeRecordAtomic(record("sess-review", "pane-9", ["/bin/bash", marker])); |
| 203 | + recovery = async () => ({ |
| 204 | + panes: [ |
| 205 | + { paneId: "pane-1", sessionId: "sess-agent" }, |
| 206 | + { paneId: "pane-9", sessionId: "sess-review" }, |
| 207 | + ], |
| 208 | + }); |
| 209 | + |
| 210 | + await expect(findAuxPanes(nativeTask, "columnAgent", SOCKET, { strict: true })).resolves.toEqual([ |
| 211 | + { backend: "native", paneId: "pane-9" }, |
| 212 | + ]); |
| 213 | + expect(recordFile("sess-review")).toContain("sess-review"); |
| 214 | + }); |
| 215 | +}); |
0 commit comments