|
1 | 1 | import path from "node:path"; |
| 2 | +import { Worker } from "node:worker_threads"; |
2 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 4 | import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js"; |
4 | 5 | import { |
@@ -148,6 +149,48 @@ describe("SQLite session handle lifecycle", () => { |
148 | 149 | } |
149 | 150 | }); |
150 | 151 |
|
| 152 | + it("cancels a projection wait while its worker is stalled", async () => { |
| 153 | + await persistSessionTranscriptTurn(scope, { |
| 154 | + messages: [{ eventId: "target", message: { role: "user", content: "target" } }], |
| 155 | + touchSessionEntry: false, |
| 156 | + }); |
| 157 | + const databaseOptions = { agentId: "main", path: databasePath }; |
| 158 | + const database = openOpenClawAgentDatabase(databaseOptions); |
| 159 | + database.db.prepare("UPDATE session_transcript_index_state SET needs_rebuild = 1").run(); |
| 160 | + let stalledWorker: Worker | undefined; |
| 161 | + startSessionTranscriptIndexReconcile({ |
| 162 | + ...databaseOptions, |
| 163 | + createWorker: () => { |
| 164 | + stalledWorker = new Worker("setInterval(() => {}, 1_000)", { eval: true }); |
| 165 | + return stalledWorker; |
| 166 | + }, |
| 167 | + }); |
| 168 | + const controller = new AbortController(); |
| 169 | + const abortReason = new Error("cancel stalled projection wait"); |
| 170 | + |
| 171 | + try { |
| 172 | + const ready = waitForSessionTranscriptProjection(scope, controller.signal); |
| 173 | + await vi.waitFor(() => expect(stalledWorker).toBeDefined()); |
| 174 | + controller.abort(abortReason); |
| 175 | + const outcome = await Promise.race([ |
| 176 | + ready.then( |
| 177 | + () => ({ kind: "resolved" as const }), |
| 178 | + (error: unknown) => ({ kind: "rejected" as const, error }), |
| 179 | + ), |
| 180 | + new Promise<{ kind: "still-waiting" }>((resolve) => { |
| 181 | + setTimeout(() => resolve({ kind: "still-waiting" }), 250); |
| 182 | + }), |
| 183 | + ]); |
| 184 | + expect(outcome).toMatchObject({ |
| 185 | + kind: "rejected", |
| 186 | + error: { name: "AbortError", cause: abortReason }, |
| 187 | + }); |
| 188 | + } finally { |
| 189 | + await stalledWorker?.terminate(); |
| 190 | + await waitForSessionTranscriptIndexReconcile(databaseOptions); |
| 191 | + } |
| 192 | + }); |
| 193 | + |
151 | 194 | it("completes a disk-budget sweep after its handle closes during archive materialization", async () => { |
152 | 195 | const { sessionKey, sessionId, storePath } = scope; |
153 | 196 | await replaceTranscriptEvents({ sessionKey, sessionId, storePath }, [ |
|
0 commit comments