import{s as e}from"./DrKWWDpL.js";import{t}from"./DYP3EJMY.js";import{t as n}from"./C0nwgWmB.js";var r=e(t(),1),i=(0,r.createRequire)(import.meta.url),a=null;function o(){return a===null&&(a=i(`node:sqlite`).DatabaseSync),a}function s(e){let t=(0,r.isAbsolute)(e)?e:(0,r.resolve)(e);try{return(0,r.realpathSync)(t)}catch{let e=[],n=t;for(;;){let i=(0,r.dirname)(n);if(i===n)return t;e.unshift(n.slice(i.length+ +!i.endsWith(r.sep))),n=i;try{return(0,r.join)((0,r.realpathSync)(n),...e)}catch{}}}}function c(e,t){if(e===t)return!0;let n=t.endsWith(r.sep)?t:t+r.sep;return e.startsWith(n)}function l(e){if({}.VITEST!==`true`||{}.CLEO_TEST_ALLOW_PROJECT_DB===`true`||e===`:memory:`||e===``||e===`/dev/null`)return;let t=s(e);if(c(t,s((0,r.tmpdir)())))return;let n=({}.CLEO_TEST_ALLOWED_DB_ROOTS??``).split(r.delimiter).map(e=>e.trim()).filter(Boolean);for(let e of n)if(c(t,s(e)))return;throw Error(`[CLEO test isolation guard] Refusing to open SQLite at "${t}" during a vitest run. Tests MUST use an isolated database under os.tmpdir() (e.g. via fs.mkdtemp(os.tmpdir(), 'cleo-test-')). If this open is intentional (integration/e2e suite), set CLEO_TEST_ALLOW_PROJECT_DB=true or extend CLEO_TEST_ALLOWED_DB_ROOTS=<absPath>:<absPath>. Prevents the T9001-style production-fixture leak (2026-05-06).`)}function u(e,t){l(e);let r=new(o())(e,{enableForeignKeyConstraints:!0,readOnly:t?.readonly??!1,timeout:t?.timeout??3e4,allowExtension:t?.allowExtension??!1});if(n(r,{enableWal:!1,busyTimeoutMs:t?.timeout??3e4}),t?.enableWal!==!1){let t=!1;for(let e=1;e<=3;e++){if(r.exec(`PRAGMA journal_mode=WAL`),(r.prepare(`PRAGMA journal_mode`).get()?.journal_mode?.toLowerCase?.()??`unknown`)===`wal`){t=!0;break}if(e<3){let t=new SharedArrayBuffer(4);Atomics.wait(new Int32Array(t),0,0,200*e)}}if(!t){let t=r.prepare(`PRAGMA journal_mode`).get()?.journal_mode?.toLowerCase?.()??`unknown`;if(t!==`wal`)throw r.close(),Error(`CRITICAL: Failed to set WAL journal mode after 3 attempts. Database is in '${t}' mode. Another process likely holds an EXCLUSIVE lock on ${e}. Refusing to open — concurrent writes in DELETE mode cause data loss. Kill other cleo processes and retry. (T5173)`)}}return{}.VITEST||r.exec(`PRAGMA foreign_keys=ON`),r}export{u as n,o as t};