|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| {-# LANGUAGE DeriveGeneric #-}
|
| {-# LANGUAGE OverloadedStrings #-}
|
|
|
| module AhmadBotAgent where
|
|
|
| import Control.Concurrent (MVar, newMVar, readMVar, modifyMVar_, threadDelay)
|
| import Control.Monad (forM_, when, unless)
|
| import Data.List (intercalate, foldl')
|
| import Data.Map.Strict (Map)
|
| import qualified Data.Map.Strict as Map
|
| import Data.Maybe (fromMaybe)
|
| import Data.Time (getCurrentTime, formatTime, defaultTimeLocale)
|
| import GHC.Generics (Generic)
|
| import System.Exit (exitFailure)
|
| import System.IO (hPutStrLn, stderr)
|
|
|
|
|
| import AToKio
|
| ( BotAgentState(..)
|
| , InvariantViolation(..)
|
| , AToKioRuntime(..)
|
| , checkAllInvariants
|
| , encodeWormEntry
|
| , initialState
|
| , initRuntime
|
| , enqueueTask
|
| , readWormLog
|
| )
|
|
|
|
|
|
|
|
|
| data Frame
|
| = Unknown
|
| | Gravity
|
| | Relativity
|
| | Quantum
|
| | Wormhole
|
| | Horizon
|
| deriving (Show, Eq, Ord, Generic)
|
|
|
| data BotGoal
|
| = ExploreFrame Frame
|
| | DeepInspect Frame
|
| | BridgeFrames Frame Frame
|
| | ReachConsensus
|
| | HaltAtBoundary
|
| deriving (Show, Eq, Generic)
|
|
|
|
|
|
|
| data AhmadBotAgent = AhmadBotAgent
|
| { abaId :: String
|
| , abaPosition :: (Double, Double)
|
| , abaFrame :: Frame
|
| , abaGoal :: BotGoal
|
| , abaBotState :: BotAgentState
|
| , abaObservations :: [BotObservation]
|
| , abaConfidence :: Double
|
| , abaGeneration :: Int
|
| } deriving (Show, Generic)
|
|
|
|
|
|
|
| data BotObservation = BotObservation
|
| { boStep :: Int
|
| , boAgentId :: String
|
| , boPosition :: (Double, Double)
|
| , boFrame :: Frame
|
| , boQuery :: String
|
| , boInsight :: String
|
| , boWormSeal :: String
|
| , boInvariants :: Bool
|
| } deriving (Show, Generic)
|
|
|
|
|
|
|
| detectFrameFromPosition :: (Double, Double) -> Frame
|
| detectFrameFromPosition (x, y) =
|
| let magnitude = sqrt (x*x + y*y)
|
| in if magnitude < 20.0 then Quantum
|
| else if magnitude < 50.0 then Gravity
|
| else if magnitude < 80.0 then Relativity
|
| else Wormhole
|
|
|
|
|
|
|
| frameMovement :: Frame -> (Double, Double)
|
| frameMovement Quantum = ( 5.0, 5.0)
|
| frameMovement Gravity = (-5.0, -5.0)
|
| frameMovement Relativity = ( 8.0, -3.0)
|
| frameMovement Wormhole = (10.0, 0.0)
|
| frameMovement Horizon = ( 0.0, 0.0)
|
| frameMovement Unknown = ( 2.0, 2.0)
|
|
|
| applyMovement :: (Double, Double) -> (Double, Double) -> (Double, Double)
|
| applyMovement (px, py) (dx, dy) =
|
| let stepSize = 1.5
|
| clamp v = max (-120.0) (min 120.0 v)
|
| in (clamp (px + stepSize * dx), clamp (py + stepSize * dy))
|
|
|
|
|
|
|
| updateGoal :: AhmadBotAgent -> Frame -> BotGoal
|
| updateGoal agent newFrame =
|
| case (abaGoal agent, newFrame) of
|
| (ExploreFrame f, f') | f == f' && abaConfidence agent > 0.8
|
| -> DeepInspect f
|
| (DeepInspect f, f') | f /= f' -> BridgeFrames f f'
|
| (_, Horizon) -> HaltAtBoundary
|
| (_, f) -> ExploreFrame f
|
|
|
|
|
|
|
| updateConfidence :: AhmadBotAgent -> Frame -> Double
|
| updateConfidence agent newFrame =
|
| let delta = if newFrame == abaFrame agent then 0.1 else -0.05
|
| in max 0.0 (min 1.0 (abaConfidence agent + delta))
|
|
|
|
|
|
|
|
|
|
|
| generateQuery :: Frame -> BotGoal -> Int -> String
|
| generateQuery Quantum _ step = "step " ++ show step ++ ": superposition β what are all possible answers?"
|
| generateQuery Gravity _ step = "step " ++ show step ++ ": convergence β what is the attractor?"
|
| generateQuery Relativity _ step = "step " ++ show step ++ ": relative β from which observer frame?"
|
| generateQuery Wormhole (BridgeFrames f1 f2) _ = "bridge: how does " ++ show f1 ++ " connect to " ++ show f2 ++ "?"
|
| generateQuery Wormhole _ step = "step " ++ show step ++ ": shortcut β what connects distant concepts?"
|
| generateQuery Horizon _ _ = "boundary: what is the edge of what I can know?"
|
| generateQuery Unknown _ step = "step " ++ show step ++ ": unknown β what frame am I in?"
|
|
|
|
|
|
|
| generateInsight :: Frame -> String -> Int -> String
|
| generateInsight Quantum query _ = "branch[" ++ query ++ "]: multiple valid answers coexist"
|
| generateInsight Gravity query _ = "converge[" ++ query ++ "]: single attractor found"
|
| generateInsight Relativity query _ = "relative[" ++ query ++ "]: answer depends on observer"
|
| generateInsight Wormhole query _ = "bridge[" ++ query ++ "]: shortcut path established"
|
| generateInsight Horizon query _ = "boundary[" ++ query ++ "]: limit of knowable reached"
|
| generateInsight Unknown query _ = "explore[" ++ query ++ "]: gathering frame data"
|
|
|
|
|
|
|
| advanceBotState :: BotAgentState -> Either InvariantViolation BotAgentState
|
| advanceBotState s =
|
| let s' = s { step = step s + 1
|
| , messageCount = messageCount s + 1
|
| , validProtocolSteps = validProtocolSteps s + 1
|
| , apiKeyUsage = apiKeyUsage s + 1
|
| , errorStatus = 0
|
| , stateValid = True
|
| }
|
| in case checkAllInvariants s' (step s') of
|
| Left err -> Left err
|
| Right () -> Right s'
|
|
|
|
|
|
|
| stepAhmadBot :: AhmadBotAgent -> IO (Either InvariantViolation AhmadBotAgent)
|
| stepAhmadBot agent = do
|
| let k = step (abaBotState agent)
|
| newPos = applyMovement (abaPosition agent) (frameMovement (abaFrame agent))
|
| newFrame = detectFrameFromPosition newPos
|
| newConf = updateConfidence agent newFrame
|
| newGoal = updateGoal agent newFrame
|
| query = generateQuery newFrame newGoal k
|
| insight = generateInsight newFrame query k
|
|
|
| case advanceBotState (abaBotState agent) of
|
| Left err -> return (Left err)
|
| Right newBotState -> do
|
|
|
| now <- getCurrentTime
|
| let timestamp = formatTime defaultTimeLocale "%Y-%m-%d %H:%M:%S" now
|
| seal = intercalate "|"
|
| [ "AHMAD_BOT"
|
| , timestamp
|
| , "id=" ++ abaId agent
|
| , "step=" ++ show k
|
| , "pos=" ++ show newPos
|
| , "frame=" ++ show newFrame
|
| , "query=" ++ take 60 query
|
| ]
|
| obs = BotObservation
|
| { boStep = k
|
| , boAgentId = abaId agent
|
| , boPosition = newPos
|
| , boFrame = newFrame
|
| , boQuery = query
|
| , boInsight = insight
|
| , boWormSeal = seal
|
| , boInvariants = True
|
| }
|
| newAgent = agent
|
| { abaPosition = newPos
|
| , abaFrame = newFrame
|
| , abaGoal = newGoal
|
| , abaBotState = newBotState
|
| , abaObservations = abaObservations agent ++ [obs]
|
| , abaConfidence = newConf
|
| , abaGeneration = abaGeneration agent + (if newFrame /= abaFrame agent then 1 else 0)
|
| }
|
| return (Right newAgent)
|
|
|
|
|
|
|
| runAhmadBot :: AhmadBotAgent -> Int -> IO (Either InvariantViolation AhmadBotAgent)
|
| runAhmadBot agent 0 = return (Right agent)
|
| runAhmadBot agent n = do
|
| result <- stepAhmadBot agent
|
| case result of
|
| Left err -> return (Left err)
|
| Right agent' -> runAhmadBot agent' (n - 1)
|
|
|
|
|
|
|
|
|
|
|
| data BotConsensus = BotConsensus
|
| { bcRound :: Int
|
| , bcAgreeingBots :: Int
|
| , bcTotalBots :: Int
|
| , bcWinningFrame :: Frame
|
| , bcAgreementRate :: Double
|
| } deriving (Show, Generic)
|
|
|
| consensusVote :: [AhmadBotAgent] -> Int -> BotConsensus
|
| consensusVote agents roundNum =
|
| let frames = map abaFrame agents
|
| frameCounts = foldl' (\m f -> Map.insertWith (+) f 1 m) Map.empty frames
|
| (winFrame, winCount) = Map.foldlWithKey'
|
| (\(bf, bc) f c -> if c > bc then (f, c) else (bf, bc))
|
| (Unknown, 0) frameCounts
|
| rate = fromIntegral winCount / fromIntegral (length agents)
|
| in BotConsensus
|
| { bcRound = roundNum
|
| , bcAgreeingBots = winCount
|
| , bcTotalBots = length agents
|
| , bcWinningFrame = winFrame
|
| , bcAgreementRate = rate
|
| }
|
|
|
|
|
|
|
| data BotSimResult = BotSimResult
|
| { bsrAgents :: [AhmadBotAgent]
|
| , bsrConsensusLog :: [BotConsensus]
|
| , bsrWormLog :: [String]
|
| , bsrTotalObs :: Int
|
| , bsrFrameVisits :: Map Frame Int
|
| } deriving (Show, Generic)
|
|
|
| runBotSimulation :: [AhmadBotAgent] -> Int -> IO BotSimResult
|
| runBotSimulation initialAgents totalSteps = go initialAgents [] [] 0
|
| where
|
| go agents consensusLog wormLog step
|
| | step >= totalSteps = do
|
| let allObs = concatMap abaObservations agents
|
| frameVisits = foldl' (\m obs -> Map.insertWith (+) (boFrame obs) 1 m)
|
| Map.empty allObs
|
| allSeals = map boWormSeal allObs
|
| return BotSimResult
|
| { bsrAgents = agents
|
| , bsrConsensusLog = consensusLog
|
| , bsrWormLog = allSeals
|
| , bsrTotalObs = length allObs
|
| , bsrFrameVisits = frameVisits
|
| }
|
| | otherwise = do
|
|
|
| results <- mapM stepAhmadBot agents
|
| let (errors, stepped) = foldr
|
| (\r (es, ss) -> case r of
|
| Left e -> (e:es, ss)
|
| Right a -> (es, a:ss))
|
| ([], []) results
|
|
|
|
|
| unless (null errors) $ do
|
| hPutStrLn stderr $ "INVARIANT HALT step=" ++ show step ++ ": " ++ show (head errors)
|
| exitFailure
|
|
|
|
|
| let newConsensus
|
| | step `mod` 10 == 0 =
|
| let c = consensusVote stepped (step `div` 10)
|
| in consensusLog ++ [c]
|
| | otherwise = consensusLog
|
|
|
|
|
| when (step `mod` 50 == 0) $ do
|
| let frames = map abaFrame stepped
|
| frameStr = intercalate "," (map show frames)
|
| putStrLn $ " step=" ++ show step
|
| ++ " frames=[" ++ frameStr ++ "]"
|
| ++ " obs=" ++ show (sum (map (length . abaObservations) stepped))
|
|
|
| go stepped newConsensus wormLog (step + 1)
|
|
|
|
|
|
|
| mkAhmadBotAgent :: String -> (Double, Double) -> AhmadBotAgent
|
| mkAhmadBotAgent agentId startPos =
|
| let frame = detectFrameFromPosition startPos
|
| in AhmadBotAgent
|
| { abaId = agentId
|
| , abaPosition = startPos
|
| , abaFrame = frame
|
| , abaGoal = ExploreFrame frame
|
| , abaBotState = initialState
|
| , abaObservations = []
|
| , abaConfidence = 0.5
|
| , abaGeneration = 0
|
| }
|
|
|
|
|
|
|
| printBotSimReport :: BotSimResult -> IO ()
|
| printBotSimReport result = do
|
| putStrLn ""
|
| putStrLn "ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
|
| putStrLn " AHMAD_BOT SPACETIME SIMULATION REPORT"
|
| putStrLn "ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
|
| putStrLn ""
|
| putStrLn $ " Total observations: " ++ show (bsrTotalObs result)
|
| putStrLn $ " WORM seals: " ++ show (length (bsrWormLog result))
|
| putStrLn $ " Consensus rounds: " ++ show (length (bsrConsensusLog result))
|
| putStrLn ""
|
| putStrLn " Frame visit distribution:"
|
| forM_ (Map.toList (bsrFrameVisits result)) $ \(frame, count) ->
|
| putStrLn $ " " ++ show frame ++ ": " ++ show count
|
| putStrLn ""
|
| putStrLn " Per-agent summary:"
|
| forM_ (bsrAgents result) $ \agent ->
|
| putStrLn $ " " ++ abaId agent
|
| ++ " | frame=" ++ show (abaFrame agent)
|
| ++ " | pos=" ++ show (abaPosition agent)
|
| ++ " | obs=" ++ show (length (abaObservations agent))
|
| ++ " | gen=" ++ show (abaGeneration agent)
|
| ++ " | conf=" ++ take 4 (show (abaConfidence agent))
|
| putStrLn ""
|
| putStrLn " Last consensus round:"
|
| case reverse (bsrConsensusLog result) of
|
| [] -> putStrLn " (none)"
|
| (c:_) -> do
|
| putStrLn $ " round=" ++ show (bcRound c)
|
| ++ " | frame=" ++ show (bcWinningFrame c)
|
| ++ " | agreement=" ++ take 4 (show (bcAgreementRate c))
|
| ++ " (" ++ show (bcAgreeingBots c) ++ "/" ++ show (bcTotalBots c) ++ " bots)"
|
| putStrLn ""
|
| putStrLn " Sample WORM seals (last 5):"
|
| mapM_ (\s -> putStrLn $ " " ++ s) (take 5 (reverse (bsrWormLog result)))
|
| putStrLn ""
|
| putStrLn "ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
|
|
|
|
|
|
|
| main :: IO ()
|
| main = do
|
| putStrLn "AhmadBotAgent v1.0 β Ahmad_bot in the Spacetime Manifold"
|
| putStrLn "7 Agda invariants enforced Β· WORM-sealed Β· Multi-bot consensus"
|
| putStrLn ""
|
|
|
|
|
|
|
| let agents =
|
| [ mkAhmadBotAgent "ahmad-1" ( 10.0, 5.0)
|
| , mkAhmadBotAgent "ahmad-2" ( 35.0, 15.0)
|
| , mkAhmadBotAgent "ahmad-3" ( 60.0, 30.0)
|
| , mkAhmadBotAgent "ahmad-4" ( 85.0, 5.0)
|
| , mkAhmadBotAgent "ahmad-5" ( 0.0, 10.0)
|
| ]
|
|
|
| putStrLn $ "Spawning " ++ show (length agents) ++ " Ahmad_bot agents..."
|
| putStrLn ""
|
| putStrLn "Initial frames:"
|
| forM_ agents $ \a ->
|
| putStrLn $ " " ++ abaId a ++ " @ " ++ show (abaPosition a)
|
| ++ " β " ++ show (abaFrame a)
|
| putStrLn ""
|
|
|
|
|
| putStrLn "Running 200 steps..."
|
| result <- runBotSimulation agents 200
|
|
|
|
|
| printBotSimReport result
|
|
|