packages feed

praxo-core-0.12.1 / src / Praxo / PluginProcess.hs

{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE CPP #-}{-# LANGUAGE ForeignFunctionInterface #-}module Praxo.PluginProcess    ( PluginProcessConfig(..)    , PluginProcessError(..)    , PluginProcessHost    , startPluginProcess    , invokePluginProcess    , stopPluginProcess    ) whereimport Control.Concurrent (forkIO)import Control.Exception (SomeException, try)import Control.Monad (when)import Data.Char (chr, digitToInt, intToDigit, isSpace)import Data.IORefimport Data.List (dropWhileEnd)import Data.Word (Word8)import Foreign.C.Types (CInt(..))import Foreign.Marshal.Alloc (allocaBytes)import Foreign.Marshal.Array (peekArray)import Foreign.Ptr (Ptr, plusPtr)import System.Environment (getEnvironment)import System.IOimport System.Processimport System.Timeout (timeout)import Praxo.Agent (AgentError(..))import Praxo.Json (Json(..), asString, encodeJson, field, parseJson)#ifdef mingw32_HOST_OSforeign import stdcall unsafe "SystemFunction036"    c_SystemFunction036 :: Ptr Word8 -> CInt -> IO Word8#endifrandomToken :: IO StringrandomToken = allocaBytes tokenBytes $ \buffer -> do    fillRandom buffer    bytes <- peekArray tokenBytes buffer    pure (concatMap hexPair (bytes :: [Word8]))  where    tokenBytes = 32    fillRandom buffer =#ifdef mingw32_HOST_OS        do            ok <- c_SystemFunction036 buffer (fromIntegral tokenBytes)            when (ok == 0) $ ioError (userError "plugin: secure random unavailable")#else        do            handle <- openBinaryFile "/dev/urandom" ReadMode            let loop offset                    | offset >= tokenBytes = pure ()                    | otherwise = do                        got <- hGetBuf handle (buffer `plusPtr` offset) (tokenBytes - offset)                        if got <= 0                            then ioError (userError "plugin: secure random unavailable")                            else loop (offset + got)            loop 0            hClose handle#endif    hexPair byte = [hexDigit (fromIntegral byte `div` 16), hexDigit (fromIntegral byte `mod` 16)]    hexDigit value        | value < 10 = chr (fromEnum '0' + value)        | otherwise = chr (fromEnum 'a' + value - 10)data PluginProcessConfig = PluginProcessConfig    { pluginId :: String    , pluginPath :: String    , pluginArgs :: [String]    , pluginDir :: Maybe FilePath    , pluginEnv :: Maybe [(String, String)]    , pluginToken :: String    , pluginTokenEnvVar :: String    , pluginStartTimeout :: Int    , pluginInvokeTimeout :: Int    , pluginGracePeriod :: Int    } deriving (Eq, Show)data PluginProcessError = PluginProcessError    { processCode :: String    , processMessage :: String    } deriving (Eq, Show)data PluginProcessHost = PluginProcessHost    { hostId :: String    , hostToken :: String    , hostInput :: Handle    , hostOutput :: Handle    , hostProcess :: ProcessHandle    , hostSeq :: IORef Int    , hostActive :: IORef Bool    }startPluginProcess :: PluginProcessConfig -> IO (Either PluginProcessError PluginProcessHost)startPluginProcess config    | null (pluginId config) = pure (Left (err "INVALID_REQUEST" "plugin: PluginID is required"))    | null (pluginPath config) = pure (Left (err "INVALID_REQUEST" "plugin: path is required"))    | otherwise = do        env0 <- maybe getEnvironment pure (pluginEnv config)        token <- if null (pluginToken config) then randomToken else pure (pluginToken config)        let tokenEnv = if null (pluginTokenEnvVar config) then "PRAXOVELA_PLUGIN_TOKEN" else pluginTokenEnvVar config            env = (tokenEnv, token) : filter ((/= tokenEnv) . fst) env0        result <- try $ createProcess (proc (pluginPath config) (pluginArgs config))            { cwd = pluginDir config            , env = Just env            , std_in = CreatePipe            , std_out = CreatePipe            , std_err = CreatePipe            }        case result of            Left (_ :: SomeException) -> pure (Left (err "INTERNAL" "plugin: spawn failed"))            Right (Just input, Just output, Just stderrHandle, process) -> do                _ <- forkIO (drainHandle stderrHandle)                seqRef <- newIORef 0                activeRef <- newIORef False                let host = PluginProcessHost (pluginId config) token input output process seqRef activeRef                health <- callHost config host "host.control" "health" "" (pluginStartTimeout config)                case health of                    Left errorValue -> do                        terminateProcess process                        pure (Left errorValue)                    Right _ -> do                        writeIORef activeRef True                        pure (Right host)            Right _ -> pure (Left (err "INTERNAL" "plugin: pipe setup failed"))invokePluginProcess :: PluginProcessConfig -> PluginProcessHost -> String -> String -> String -> IO (Either PluginProcessError String)invokePluginProcess config host service method payload = do    active <- readIORef (hostActive host)    if not active        then pure (Left (err "NOT_ACTIVE" "plugin: host is not active"))        else callHost config host service method payload (pluginInvokeTimeout config)stopPluginProcess :: PluginProcessConfig -> PluginProcessHost -> IO ()stopPluginProcess config host = do    _ <- callHost config host "host.control" "shutdown" "" (pluginGracePeriod config)    writeIORef (hostActive host) False    _ <- try (terminateProcess (hostProcess host)) :: IO (Either SomeException ())    pure ()callHost :: PluginProcessConfig -> PluginProcessHost -> String -> String -> String -> Int -> IO (Either PluginProcessError String)callHost config host service method payload timeoutMs = do    seqValue <- atomicModifyIORef' (hostSeq host) (\value -> (value + 1, value + 1))    let requestId = "req-" ++ show seqValue        frame = JObject            [ ("id", JString requestId)            , ("version", JNumber 1)            , ("plugin_id", JString (hostId host))            , ("service", JString service)            , ("method", JString method)            , ("token", JString (hostToken host))            , ("payload", JString (base64Encode payload))            ]    case encodeJson frame of        Left detail -> pure (Left (err "INVALID_REQUEST" detail))        Right encoded -> do            writeResult <- try $ do                hPutStrLn (hostInput host) encoded                hFlush (hostInput host)            case writeResult of                Left (_ :: SomeException) -> pure (Left (err "INTERNAL" "plugin: request write failed"))                Right () -> readResponse (hostOutput host) requestId timeoutMsreadResponse :: Handle -> String -> Int -> IO (Either PluginProcessError String)readResponse output requestId timeoutMs = do    line <- timeout (timeoutMs * 1000) (hGetLine output)    case line of        Nothing -> pure (Left (err "TIMEOUT" "plugin: invocation timed out"))        Just raw -> case parseJson raw of            Left detail -> pure (Left (err "INTERNAL" ("plugin: invalid response: " ++ detail)))            Right value                | asString (field "id" value) /= requestId -> readResponse output requestId timeoutMs                | field "ok" value /= JBool True ->                    pure (Left (PluginProcessError                        (normalizeCode (asString (field "code" value)))                        (asString (field "message" value))))                | otherwise -> pure $ case base64Decode (asString (field "payload" value)) of                    Nothing -> Left (err "INTERNAL" "plugin: invalid payload")                    Just decoded -> Right decoded-- A plugin that logs heavily to stderr must not block on a full pipe, so a-- dedicated thread drains and discards stderr for the process lifetime.drainHandle :: Handle -> IO ()drainHandle handle = do    _ <- try (hSetBinaryMode handle True >> loop) :: IO (Either SomeException ())    pure ()  where    loop = do        eof <- hIsEOF handle        if eof            then pure ()            else hGetChar handle >> loopnormalizeCode :: String -> StringnormalizeCode code    | code `elem` known = code    | otherwise = "INTERNAL"  where    known = ["CAPABILITY_DENIED", "NOT_ACTIVE", "NOT_FOUND", "INVALID_REQUEST", "TIMEOUT", "INTERNAL", "CANCELED"]err :: String -> String -> PluginProcessErrorerr code message = PluginProcessError code messagebase64Encode :: String -> Stringbase64Encode = concatMap encodeChunk . chunksOf3  where    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"    encodeChunk [a] =        [ alphabet !! (fromEnum a `div` 4)        , alphabet !! ((fromEnum a `mod` 4) * 16)        , '=', '=']    encodeChunk [a, b] =        [ alphabet !! (fromEnum a `div` 4)        , alphabet !! (((fromEnum a `mod` 4) * 16) + (fromEnum b `div` 16))        , alphabet !! ((fromEnum b `mod` 16) * 4)        , '=']    encodeChunk (a:b:c:_) =        [ alphabet !! (fromEnum a `div` 4)        , alphabet !! (((fromEnum a `mod` 4) * 16) + (fromEnum b `div` 16))        , alphabet !! (((fromEnum b `mod` 16) * 4) + (fromEnum c `div` 64))        , alphabet !! (fromEnum c `mod` 64)]    encodeChunk [] = []    chunksOf3 [] = []    chunksOf3 value = take 3 value : chunksOf3 (drop 3 value)base64Decode :: String -> Maybe Stringbase64Decode text    | any (not . valid) filtered = Nothing    | otherwise = Just (go filtered)  where    filtered = takeWhile (/= '=') (filter (not . isSpace) text)    valid c = c `elem` alphabet    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"    value c = case lookup c (zip alphabet [0 ..]) of        Just index -> index        Nothing -> 0    go = goBits 0 0 []    goBits _ _ acc [] = reverse acc    goBits buffer bits acc (c:rest) =        let next = buffer * 64 + value c            bits2 = bits + 6        in if bits2 >= 8            then let output = (next `div` (2 ^ (bits2 - 8))) `mod` 256                 in goBits next (bits2 - 8) (chr output : acc) rest            else goBits next bits2 acc rest