#!/usr/bin/env node
const e={workingToEpisodicAccesses:2,episodicToSemanticReferences:3,semanticToProceduralVerifications:5},t={working:1,episodic:2,semantic:4,procedural:12};function n(t,n=e){switch(t.tier){case`working`:if(t.accessCount>=n.workingToEpisodicAccesses)return`episodic`;break;case`episodic`:if(t.accessCount>=n.workingToEpisodicAccesses+n.episodicToSemanticReferences)return`semantic`;break;case`semantic`:if(t.accessCount>=n.workingToEpisodicAccesses+n.episodicToSemanticReferences+n.semanticToProceduralVerifications)return`procedural`;break;case`procedural`:break;default:break}}function r(e,t,n){e.memoryMetaUpdateTier(t,n)}const i={stabilityHours:168,accessMultiplier:1.5,flagThreshold:.1,maxStabilityHours:8760};function a(e){return{...i,...e}}function o(e,t){if(!e)return t;let n=Date.parse(e);return Number.isNaN(n)?t:n}function s(e){return e&&e in t?e:`working`}function c(e,n,r,c=i,l){let u=typeof c==`string`?s(c):`working`,d=a(typeof c==`string`?l:c),f=o(e,o(r,Date.now())),p=Math.max(0,Date.now()-f)/(1e3*60*60),m=Math.min(d.stabilityHours*t[u]*d.accessMultiplier**Math.max(0,n),d.maxStabilityHours);return Math.exp(-p/m)}function l(e,t,n){let r=e.memoryMetaGet(t);if(!r)return;let i=c(r.lastAccessedAt,r.accessCount,r.createdAt,r.tier,n);return r.retentionScore!==i&&e.memoryMetaUpdateScore(t,i),e.memoryMetaGet(t)??{...r,retentionScore:i}}function u(e,t,n){e.memoryMetaGet(t)||e.memoryMetaCreate(t),e.memoryMetaTouch(t);let r=e.memoryMetaGet(t);if(!r)return 1;let i=c(r.lastAccessedAt,r.accessCount,r.createdAt,r.tier,n);return e.memoryMetaUpdateScore(t,i),i}function d(e,t){let n=a(t);return e.memoryMetaList().map(t=>l(e,t.entryId,n)??t).filter(e=>e.retentionScore<n.flagThreshold).sort((e,t)=>e.retentionScore-t.retentionScore)}export{e as a,u as i,d as n,n as o,l as r,r as s,i as t};