@@ -122,57 +122,66 @@ export function rebuildLabProjection(configDir?: string): RebuildResult {
122122
123123 const db = new Database ( paths . sqlitePath ) ;
124124 let transactionOpen = false ;
125+ // Every statement prepared below, so they can be finalized before the close.
126+ //
127+ // Bun keeps a prepared statement alive until it is finalized or garbage collected,
128+ // and on Windows an unfinalized statement holds the database file open: `close()`
129+ // leaves the handle, and `close(true)` throws "database is locked". A second
130+ // rebuild then failed to unlink the previous projection with EBUSY, and the retry
131+ // loop in `wipeSqlite` could only turn that into a slower failure. POSIX allows
132+ // unlinking an open file, which is why this never surfaced there.
133+ const prepared : Array < { finalize ( ) : void } > = [ ] ;
134+ const prepare = ( sql : string ) => {
135+ const statement = db . prepare ( sql ) ;
136+ prepared . push ( statement ) ;
137+ return statement ;
138+ } ;
125139 try {
126140 db . exec ( "PRAGMA journal_mode=DELETE;" ) ;
127141 db . exec ( "PRAGMA foreign_keys=OFF;" ) ;
128142 db . exec ( "BEGIN IMMEDIATE;" ) ;
129143 transactionOpen = true ;
130144 resetProjectionSchema ( db ) ;
131145
132- db . prepare (
133- "INSERT OR REPLACE INTO schema_meta(key, value) VALUES (?, ?)" ,
134- ) . run ( "schema_version" , String ( LAB_SQLITE_SCHEMA_VERSION ) ) ;
135- db . prepare (
136- "INSERT OR REPLACE INTO schema_meta(key, value) VALUES (?, ?)" ,
137- ) . run ( "projection_spec_version" , LAB_PROJECTION_SPEC_VERSION ) ;
138- db . prepare (
139- "INSERT OR REPLACE INTO schema_meta(key, value) VALUES (?, ?)" ,
140- ) . run ( "built_at_ms" , String ( Date . now ( ) ) ) ;
146+ const insertMeta = prepare ( "INSERT OR REPLACE INTO schema_meta(key, value) VALUES (?, ?)" ) ;
147+ insertMeta . run ( "schema_version" , String ( LAB_SQLITE_SCHEMA_VERSION ) ) ;
148+ insertMeta . run ( "projection_spec_version" , LAB_PROJECTION_SPEC_VERSION ) ;
149+ insertMeta . run ( "built_at_ms" , String ( Date . now ( ) ) ) ;
141150
142- const insertCorruption = db . prepare (
151+ const insertCorruption = prepare (
143152 "INSERT INTO corruption(kind, line_number, event_id, detail) VALUES (?, ?, ?, ?)" ,
144153 ) ;
145154 for ( const c of corruptions ) {
146155 insertCorruption . run ( c . kind , c . lineNumber ?? null , c . eventId ?? null , c . detail ) ;
147156 }
148157
149- const insertEvent = db . prepare (
158+ const insertEvent = prepare (
150159 `INSERT INTO events(event_id, event_kind, recorded_at, producer, producer_version, payload_json, excluded, exclusion_reason)
151160 VALUES (?, ?, ?, ?, ?, ?, ?, ?)` ,
152161 ) ;
153- const insertSubject = db . prepare (
162+ const insertSubject = prepare (
154163 `INSERT OR IGNORE INTO subjects(subject_id, subject_kind, subject_json) VALUES (?, ?, ?)` ,
155164 ) ;
156- const insertObs = db . prepare (
165+ const insertObs = prepare (
157166 `INSERT INTO observations(
158167 event_id, subject_id, evidence_layer, suite_id, suite_version, suite_manifest_digest,
159168 scenario_id, scenario_version, scenario_manifest_digest, outcome, completed_at, execution_mode
160169 ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` ,
161170 ) ;
162- const insertClaim = db . prepare (
171+ const insertClaim = prepare (
163172 `INSERT INTO claims(
164173 event_id, subject_id, capability, polarity, source_manifest_digest,
165174 effective_at, recorded_at, supersedes_json, current, usable
166175 ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` ,
167176 ) ;
168- const insertInv = db . prepare (
177+ const insertInv = prepare (
169178 `INSERT INTO invalidations(event_id, reason, targets_json, recorded_at, applied) VALUES (?, ?, ?, ?, ?)` ,
170179 ) ;
171- const insertPurge = db . prepare (
180+ const insertPurge = prepare (
172181 `INSERT INTO purges(event_id, target_event_ids_json, target_artifact_digests_json, purge_actions_json, recorded_at)
173182 VALUES (?, ?, ?, ?, ?)` ,
174183 ) ;
175- const insertArtifact = db . prepare (
184+ const insertArtifact = prepare (
176185 `INSERT INTO artifacts(digest, artifact_class, media_type, byte_count, status, last_error)
177186 VALUES (?, ?, ?, ?, ?, ?)
178187 ON CONFLICT(digest) DO UPDATE SET
@@ -319,7 +328,7 @@ export function rebuildLabProjection(configDir?: string): RebuildResult {
319328 }
320329 }
321330
322- const insertVerdict = db . prepare (
331+ const insertVerdict = prepare (
323332 `INSERT INTO verdicts(
324333 projection_key, subject_id, evidence_layer, suite_id, suite_version, suite_manifest_digest,
325334 projection_spec_version, verdict, as_of, scenario_manifest_digests_json, claim_source_digest,
@@ -369,6 +378,15 @@ export function rebuildLabProjection(configDir?: string): RebuildResult {
369378 } catch {
370379 // Closing the disposable DB is still safe if pragma restoration fails.
371380 }
381+ // Finalize before closing: an outstanding statement keeps the file open on
382+ // Windows, and the next rebuild cannot unlink the projection it is replacing.
383+ for ( const statement of prepared ) {
384+ try {
385+ statement . finalize ( ) ;
386+ } catch {
387+ // A statement already finalized by an error path is not a rebuild failure.
388+ }
389+ }
372390 db . close ( ) ;
373391 artifactStore . close ( ) ;
374392 }
0 commit comments