File size: 12,296 Bytes
9425aed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | -- βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
-- AgentMemory.hs β WORM-Sealed Observation History
-- bridges/haskell/AgentMemory.hs
--
-- PHASE 7 AGENT MEMORY. APPEND-ONLY. CRYPTOGRAPHICALLY SEALED.
--
-- Maintains agent observation history with WORM (Write-Once, Read-Many) integrity.
-- Every observation is chained via hash: obs_n+1 = hash(obs_n || obs_n+1_data)
--
-- Memory is immutable: agents can only READ prior observations and APPEND new ones.
-- No updates, no deletes, no rewrites. History is audit trail.
--
-- βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
{-# LANGUAGE DeriveGeneric #-}
module AgentMemory where
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC
import Data.List (intercalate)
import Data.Time (getCurrentTime, formatTime, defaultTimeLocale, UTCTime)
import GHC.Generics (Generic)
import Data.Hashable (hash)
import qualified Data.Map.Strict as Map
import Control.Exception (bracket)
import System.IO (withFile, IOMode(..), hPutStrLn, hGetContents)
-- Re-import agent types for memory
import SpacetimeAgent (Observation(..), AgentId, Frame, Agent(..))
-- ββ WORM Memory Entry βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
-- Each entry is sealed and immutable.
data WormMemoryEntry = WormMemoryEntry
{ entryIndex :: Int -- Position in chain
, entryTimestamp :: UTCTime -- When recorded
, entryAgentId :: AgentId -- Which agent
, entryData :: Observation -- The observation
, entryPriorHash :: ByteString -- Hash of previous entry
, entryCurrentHash :: ByteString -- WORM seal of this + prior
} deriving (Show, Eq, Generic)
-- ββ WORM Memory Log (the durable record) ββββββββββββββββββββββββββββββββββββββββββ
data WormMemoryLog = WormMemoryLog
{ logPath :: FilePath -- File location (append-only log)
, logEntries :: [WormMemoryEntry]
, logTailHash :: ByteString -- Hash of last entry (for chain integrity)
, logSize :: Int -- Number of entries
} deriving (Show, Eq, Generic)
-- ββ Encode entry to ByteString for hashing βββββββββββββββββββββββββββββββββββββββββ
encodeMemoryEntry :: WormMemoryEntry -> ByteString
encodeMemoryEntry entry =
let idxStr = show (entryIndex entry)
aidStr = entryAgentId entry
timeStr = show (entryTimestamp entry)
obsStr = show (entryData entry)
priorStr = BSC.unpack (entryPriorHash entry)
parts = [idxStr, aidStr, timeStr, obsStr, priorStr]
in BSC.pack (intercalate "|" parts)
-- ββ Hash chain: compute WORM seal ββββββββββββββββββββββββββββββββββββββββββββββββββ
-- WORM seal = hash(this_entry_data || prior_hash)
-- This creates an immutable chain: current entry's hash depends on prior
computeWormSeal :: WormMemoryEntry -> ByteString -> ByteString
computeWormSeal entry priorHash =
let thisData = encodeMemoryEntry entry
chainData = thisData <> priorHash
in BSC.pack $ "worm" ++ show (hash chainData)
-- ββ Create new WORM entry βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
createMemoryEntry :: Int -> UTCTime -> AgentId -> Observation -> ByteString -> WormMemoryEntry
createMemoryEntry idx ts aid obs priorHash =
let entry = WormMemoryEntry
{ entryIndex = idx
, entryTimestamp = ts
, entryAgentId = aid
, entryData = obs
, entryPriorHash = priorHash
, entryCurrentHash = BS.empty -- Placeholder, will be computed
}
seal = computeWormSeal entry priorHash
in entry { entryCurrentHash = seal }
-- ββ Initialize empty memory log ββββββββββββββββββββββββββββββββββββββββββββββββββββ
initializeMemoryLog :: FilePath -> IO WormMemoryLog
initializeMemoryLog path = do
return WormMemoryLog
{ logPath = path
, logEntries = []
, logTailHash = BS.empty -- Genesis: no prior hash
, logSize = 0
}
-- ββ Append observation to WORM log (main API) ββββββββββββββββββββββββββββββββββββββ
-- Returns updated log with new entry sealed and chained.
recordObservationWorm :: WormMemoryLog -> AgentId -> Observation -> IO WormMemoryLog
recordObservationWorm log aid obs = do
now <- getCurrentTime
let newIndex = logSize log
priorHash = logTailHash log -- Last entry's hash
newEntry = createMemoryEntry newIndex now aid obs priorHash
-- Append to in-memory log
let updatedEntries = logEntries log ++ [newEntry]
updatedLog = log
{ logEntries = updatedEntries
, logTailHash = entryCurrentHash newEntry
, logSize = newSize log + 1
}
-- Persist to disk (append mode)
persistEntry (logPath log) newEntry
return updatedLog
-- Write entry to WORM file (append-only, never update)
persistEntry :: FilePath -> WormMemoryEntry -> IO ()
persistEntry path entry =
let line = encodeWormEntryLine entry
in appendFile path (line ++ "\n")
-- Serialize entry to single line (pipe-delimited for durability)
encodeWormEntryLine :: WormMemoryEntry -> String
encodeWormEntryLine entry =
let parts =
[ show (entryIndex entry)
, entryAgentId entry
, formatTime defaultTimeLocale "%Y-%m-%d %H:%M:%S%z" (entryTimestamp entry)
, show (entryData entry)
, BSC.unpack (entryPriorHash entry)
, BSC.unpack (entryCurrentHash entry)
]
in intercalate "|" parts
-- ββ Verify WORM integrity ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
-- Check that chain is unbroken: each entry's hash matches expected value
verifyWormIntegrity :: WormMemoryLog -> Either String ()
verifyWormIntegrity log = verifyChain (logEntries log) BS.empty
where
verifyChain [] _ = Right ()
verifyChain (entry:rest) expectedPrior
| entryPriorHash entry /= expectedPrior =
Left $ "WORM chain broken at index " ++ show (entryIndex entry)
| otherwise = verifyChain rest (entryCurrentHash entry)
-- ββ Read memory (audit trail queries) ββββββββββββββββββββββββββββββββββββββββββββββ
-- Get all observations from a specific agent
agentMemoryLog :: WormMemoryLog -> AgentId -> [Observation]
agentMemoryLog log aid =
[ entryData entry
| entry <- logEntries log
, entryAgentId entry == aid
]
-- Get observations in time range
observationsInRange :: WormMemoryLog -> UTCTime -> UTCTime -> [Observation]
observationsInRange log startTime endTime =
[ entryData entry
| entry <- logEntries log
, let ts = entryTimestamp entry
, ts >= startTime && ts <= endTime
]
-- Get entry by index
getEntryAtIndex :: WormMemoryLog -> Int -> Maybe WormMemoryEntry
getEntryAtIndex log idx
| idx < 0 || idx >= length (logEntries log) = Nothing
| otherwise = Just (logEntries log !! idx)
-- ββ Export audit trail (for compliance/review) βββββββββββββββββββββββββββββββββββββ
exportAuditTrail :: WormMemoryLog -> String
exportAuditTrail log =
let header = "βββ WORM Memory Audit Trail βββ\n"
stats = "Entries: " ++ show (logSize log) ++ " | Tail Hash: " ++ BSC.unpack (logTailHash log) ++ "\n\n"
entries = intercalate "\n" [ exportEntry e | e <- logEntries log ]
in header ++ stats ++ entries
exportEntry :: WormMemoryEntry -> String
exportEntry entry =
intercalate " | "
[ "[" ++ show (entryIndex entry) ++ "]"
, formatTime defaultTimeLocale "%H:%M:%S" (entryTimestamp entry)
, "Agent:" ++ entryAgentId entry
, "PriorHash:" ++ take 12 (BSC.unpack (entryPriorHash entry))
, "CurrentHash:" ++ take 12 (BSC.unpack (entryCurrentHash entry))
]
-- ββ Memory statistics ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
data MemoryStats = MemoryStats
{ statsTotalEntries :: Int
, statsByAgent :: Map.Map AgentId Int -- Observations per agent
, statsOldestTime :: Maybe UTCTime
, statsNewestTime :: Maybe UTCTime
, statsChainIntegrity :: Bool
} deriving (Show, Eq, Generic)
computeMemoryStats :: WormMemoryLog -> MemoryStats
computeMemoryStats log =
let entries = logEntries log
byAgent = Map.fromListWith (+) [(entryAgentId e, 1) | e <- entries]
times = map entryTimestamp entries
oldest = if null times then Nothing else Just (minimum times)
newest = if null times then Nothing else Just (maximum times)
integrity = case verifyWormIntegrity log of
Right () -> True
Left _ -> False
in MemoryStats
{ statsTotalEntries = length entries
, statsByAgent = byAgent
, statsOldestTime = oldest
, statsNewestTime = newest
, statsChainIntegrity = integrity
}
-- ββ Snapshot (for checkpointing) βββββββββββββββββββββββββββββββββββββββββββββββββββ
-- Create immutable snapshot at a point in time
data MemorySnapshot = MemorySnapshot
{ snapshotTimestamp :: UTCTime
, snapshotIndex :: Int
, snapshotHash :: ByteString
, snapshotAgent :: AgentId
} deriving (Show, Eq, Generic)
createSnapshot :: WormMemoryLog -> IO (Maybe MemorySnapshot)
createSnapshot log = do
now <- getCurrentTime
case logEntries log of
[] -> return Nothing
entries ->
let lastEntry = last entries
snapshot = MemorySnapshot
{ snapshotTimestamp = now
, snapshotIndex = entryIndex lastEntry
, snapshotHash = entryCurrentHash lastEntry
, snapshotAgent = entryAgentId lastEntry
}
in return (Just snapshot)
-- ββ Restore from checkpoint ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
-- Verify integrity up to checkpoint index
verifyToCheckpoint :: WormMemoryLog -> MemorySnapshot -> Either String ()
verifyToCheckpoint log snapshot =
let checkIndex = snapshotIndex snapshot
entries = logEntries log
headEntries = take (checkIndex + 1) entries
in if null entries
then Left "Empty log"
else if length entries < checkIndex + 1
then Left "Checkpoint index out of range"
else verifyChain headEntries BS.empty
where
verifyChain [] _ = Right ()
verifyChain (entry:rest) expectedPrior
| entryPriorHash entry /= expectedPrior =
Left $ "Chain broken at checkpoint index " ++ show (entryIndex entry)
| otherwise = verifyChain rest (entryCurrentHash entry)
-- ββ Compact log (no-op for now; in production: archive old entries) ββββββββββββββββ
compactLog :: WormMemoryLog -> Int -> WormMemoryLog
compactLog log retentionDays =
-- In production: remove entries older than retentionDays
-- For now: return unchanged (append-only is immutable)
log
-- Helper for newSize
newSize :: WormMemoryLog -> Int
newSize log = logSize log
|