import{a as e,r as t}from"./bG-fsHMS.js";var n=90,r=.1;function i(e){return e.slice(0,5).map(e=>e.id??`unknown`).filter(e=>e!==`unknown`)}async function a(a){await t(a);let c=e(),l=[];if(!c)return{totalScanned:0,findings:[],isClean:!0,scannedAt:new Date().toISOString()};try{let e=[`brain_observations`,`brain_decisions`,`brain_patterns`,`brain_learnings`],t=0;for(let n of e)try{let e=c.prepare(`SELECT COUNT(*) as c FROM ${n}`).get();t+=e?.c??0}catch{}{let t=[];for(let n of e)try{let e=c.prepare(`SELECT id FROM ${n} WHERE type IS NULL OR type = '' LIMIT 20`).all();t.push(...e)}catch{}t.length>0&&l.push({pattern:`missing-type`,count:t.length,sampleIds:i(t),description:`Entries with NULL or empty type field — type is required for retrieval routing.`})}{let t=[];for(let n of e)try{let e=c.prepare(`SELECT id FROM ${n} WHERE provenance IS NULL LIMIT 20`).all();t.push(...e)}catch{}t.length>0&&l.push({pattern:`missing-provenance`,count:t.length,sampleIds:i(t),description:`Entries with NULL provenance — source cannot be verified for retrieval integrity.`})}{let t=[];for(let n of e)try{let e=c.prepare(`SELECT id FROM ${n} WHERE confidence IS NOT NULL AND confidence < ? LIMIT 20`).all(r);t.push(...e)}catch{}t.length>0&&l.push({pattern:`low-confidence`,count:t.length,sampleIds:i(t),description:`Entries with confidence < ${r} — near-zero signal, likely noise.`})}{let t=new Date;t.setDate(t.getDate()-n);let r=t.toISOString(),a=[];for(let t of e)try{let e=c.prepare(`SELECT id FROM ${t}
               WHERE verified = 0
                 AND created_at < ?
               LIMIT 20`).all(r);a.push(...e)}catch{}a.length>0&&l.push({pattern:`stale-unverified`,count:a.length,sampleIds:i(a),description:`Unverified entries older than ${n} days — likely outdated observations.`})}{let t=[];for(let n of e)try{let e=c.prepare(`SELECT id FROM ${n}
               WHERE content IN (
                 SELECT content FROM ${n}
                 WHERE content IS NOT NULL
                 GROUP BY content HAVING COUNT(*) > 1
               ) LIMIT 20`).all();t.push(...e)}catch{}t.length>0&&l.push({pattern:`duplicate-content`,count:t.length,sampleIds:i(t),description:`Entries sharing identical content — duplicates inflate retrieval scores.`})}try{let e=c.prepare(`SELECT e.id FROM brain_edges e
             LEFT JOIN brain_nodes src ON src.id = e.source_id
             LEFT JOIN brain_nodes tgt ON tgt.id = e.target_id
             WHERE src.id IS NULL OR tgt.id IS NULL
             LIMIT 20`).all();e.length>0&&l.push({pattern:`orphan-edge`,count:e.length,sampleIds:i(e),description:`Brain graph edges pointing to non-existent nodes — referential integrity violation.`})}catch{}let a=o(c),u=s(c);return{totalScanned:t,findings:l,isClean:l.length===0,scannedAt:new Date().toISOString(),autoExtractHealth:a,provenanceDistribution:u}}catch{return{totalScanned:0,findings:[],isClean:!0,scannedAt:new Date().toISOString()}}}function o(e){if(!e)return;let t=e;try{let e=t.prepare(`SELECT COUNT(*) AS cnt FROM brain_observations WHERE invalid_at IS NULL`).get()?.cnt??0,n=t.prepare(`SELECT COUNT(*) AS cnt FROM brain_learnings WHERE invalid_at IS NULL`).get()?.cnt??0,r=0,i=0;try{r=t.prepare(`SELECT COUNT(*) AS cnt FROM brain_promotion_log WHERE fulfilled_at IS NULL`).get()?.cnt??0,i=t.prepare(`SELECT COUNT(*) AS cnt FROM brain_promotion_log WHERE fulfilled_at IS NOT NULL`).get()?.cnt??0}catch{}let a=null;try{a=t.prepare(`SELECT started_at FROM brain_consolidation_events ORDER BY started_at DESC LIMIT 1`).get()?.started_at??null}catch{}let o=e>0?n/e:1;return{observationCount:e,learningCount:n,pendingPromotions:r,fulfilledPromotions:i,extractionRatio:o,lastConsolidationAt:a,healthy:e===0||o>=.01}}catch{return}}function s(e){if(!e)return;let t=e;try{let e=t.prepare(`SELECT COUNT(*) AS cnt FROM brain_observations WHERE invalid_at IS NULL`).get()?.cnt??0,n={};try{let e=t.prepare(`SELECT COALESCE(origin, '__null__') AS origin_val, COUNT(*) AS cnt
           FROM brain_observations WHERE invalid_at IS NULL
           GROUP BY origin_val`).all();for(let t of e)n[t.origin_val===`__null__`?`(unset)`:t.origin_val]=t.cnt}catch{}let r=0;try{r=t.prepare(`SELECT COUNT(*) AS cnt FROM brain_observations WHERE invalid_at IS NULL AND validated_at IS NOT NULL`).get()?.cnt??0}catch{}let i=0;try{i=t.prepare(`SELECT COUNT(*) AS cnt FROM brain_observations WHERE invalid_at IS NULL AND provenance_chain IS NOT NULL`).get()?.cnt??0}catch{}return{total:e,byOrigin:n,verifiedCount:r,derivedCount:i}}catch{return}}export{a as scanBrainNoise};