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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
-- | Audit logging for Ava and subagents.
--
-- Persists all agent events to JSONL files for debugging and diagnosis.
-- Logs are stored in @AVA_DATA_ROOT/logs/@.
--
-- Structure:
-- - @logs/ava/YYYY-MM-DD.jsonl@ - Daily Ava conversation logs
-- - @logs/subagents/S-{id}.jsonl@ - Per-subagent traces
--
-- : out omni-agent-auditlog
-- : dep aeson
-- : dep bytestring
-- : dep directory
-- : dep time
-- : dep uuid
module Omni.Agent.AuditLog
( -- * Types
AuditLogEntry (..),
AuditEventType (..),
LogMetadata (..),
SubagentId (..),
SessionId (..),
AgentId (..),
-- * Writing logs
writeAvaLog,
writeSubagentLog,
-- * Reading logs
readAvaLogs,
readSubagentLogs,
getRecentAvaLogs,
-- * SubagentId
newSubagentId,
subagentLogPath,
-- * Paths
avaLogsDir,
subagentLogsDir,
-- * Helpers
mkLogEntry,
emptyMetadata,
-- * Testing
main,
test,
)
where
import Alpha
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.Time as Time
import qualified Data.UUID as UUID
import qualified Data.UUID.V4 as UUID
import qualified Omni.Agent.Paths as Paths
import qualified Omni.Test as Test
import qualified System.Directory as Dir
import System.FilePath ((</>))
main :: IO ()
main = Test.run test
test :: Test.Tree
test =
Test.group
"Omni.Agent.AuditLog"
[ Test.unit "SubagentId JSON roundtrip" <| do
let sid = SubagentId "abc123"
case Aeson.decode (Aeson.encode sid) of
Nothing -> Test.assertFailure "Failed to decode SubagentId"
Just decoded -> decoded Test.@=? sid,
Test.unit "AuditEventType JSON roundtrip" <| do
let types = [UserMessage, AssistantMessage, ToolCall, ToolResult, SubagentSpawn, SubagentComplete, ErrorOccurred]
forM_ types <| \t ->
case Aeson.decode (Aeson.encode t) of
Nothing -> Test.assertFailure ("Failed to decode: " <> show t)
Just decoded -> decoded Test.@=? t,
Test.unit "AuditLogEntry JSON roundtrip" <| do
now <- Time.getCurrentTime
let entry =
AuditLogEntry
{ logTimestamp = now,
logSessionId = SessionId "sess-123",
logAgentId = AgentId "ava",
logUserId = Just "ben",
logEventType = AssistantMessage,
logContent = Aeson.String "Hello",
logMetadata = emptyMetadata
}
case Aeson.decode (Aeson.encode entry) of
Nothing -> Test.assertFailure "Failed to decode AuditLogEntry"
Just decoded -> logEventType decoded Test.@=? AssistantMessage,
Test.unit "subagentLogPath constructs correct path" <| do
let sid = SubagentId "abc123"
let path = subagentLogPath sid
(Text.pack "abc123.jsonl" `Text.isInfixOf` Text.pack path) Test.@=? True
]
newtype SubagentId = SubagentId {unSubagentId :: Text}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON SubagentId where
toJSON (SubagentId sid) = Aeson.String sid
instance Aeson.FromJSON SubagentId where
parseJSON = Aeson.withText "SubagentId" (pure <. SubagentId)
newtype SessionId = SessionId {unSessionId :: Text}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON SessionId where
toJSON (SessionId sid) = Aeson.String sid
instance Aeson.FromJSON SessionId where
parseJSON = Aeson.withText "SessionId" (pure <. SessionId)
newtype AgentId = AgentId {unAgentId :: Text}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON AgentId where
toJSON (AgentId aid) = Aeson.String aid
instance Aeson.FromJSON AgentId where
parseJSON = Aeson.withText "AgentId" (pure <. AgentId)
data AuditEventType
= UserMessage
| AssistantMessage
| ToolCall
| ToolResult
| SubagentSpawn
| SubagentComplete
| ExtendedThinking
| CostUpdate
| ErrorOccurred
| SessionStart
| SessionEnd
deriving (Show, Eq, Generic)
instance Aeson.ToJSON AuditEventType where
toJSON UserMessage = Aeson.String "user_message"
toJSON AssistantMessage = Aeson.String "assistant_message"
toJSON ToolCall = Aeson.String "tool_call"
toJSON ToolResult = Aeson.String "tool_result"
toJSON SubagentSpawn = Aeson.String "subagent_spawn"
toJSON SubagentComplete = Aeson.String "subagent_complete"
toJSON ExtendedThinking = Aeson.String "extended_thinking"
toJSON CostUpdate = Aeson.String "cost_update"
toJSON ErrorOccurred = Aeson.String "error"
toJSON SessionStart = Aeson.String "session_start"
toJSON SessionEnd = Aeson.String "session_end"
instance Aeson.FromJSON AuditEventType where
parseJSON =
Aeson.withText "AuditEventType" <| \case
"user_message" -> pure UserMessage
"assistant_message" -> pure AssistantMessage
"tool_call" -> pure ToolCall
"tool_result" -> pure ToolResult
"subagent_spawn" -> pure SubagentSpawn
"subagent_complete" -> pure SubagentComplete
"extended_thinking" -> pure ExtendedThinking
"cost_update" -> pure CostUpdate
"error" -> pure ErrorOccurred
"session_start" -> pure SessionStart
"session_end" -> pure SessionEnd
_ -> empty
data LogMetadata = LogMetadata
{ metaInputTokens :: Maybe Int,
metaOutputTokens :: Maybe Int,
metaCostCents :: Maybe Double,
metaModelId :: Maybe Text,
metaParentAgentId :: Maybe AgentId,
metaDurationMs :: Maybe Int
}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON LogMetadata where
toJSON m =
Aeson.object
<| catMaybes
[ ("input_tokens" .=) </ metaInputTokens m,
("output_tokens" .=) </ metaOutputTokens m,
("cost_cents" .=) </ metaCostCents m,
("model_id" .=) </ metaModelId m,
("parent_agent_id" .=) </ metaParentAgentId m,
("duration_ms" .=) </ metaDurationMs m
]
instance Aeson.FromJSON LogMetadata where
parseJSON =
Aeson.withObject "LogMetadata" <| \v ->
LogMetadata
</ (v Aeson..:? "input_tokens")
<*> (v Aeson..:? "output_tokens")
<*> (v Aeson..:? "cost_cents")
<*> (v Aeson..:? "model_id")
<*> (v Aeson..:? "parent_agent_id")
<*> (v Aeson..:? "duration_ms")
emptyMetadata :: LogMetadata
emptyMetadata =
LogMetadata
{ metaInputTokens = Nothing,
metaOutputTokens = Nothing,
metaCostCents = Nothing,
metaModelId = Nothing,
metaParentAgentId = Nothing,
metaDurationMs = Nothing
}
data AuditLogEntry = AuditLogEntry
{ logTimestamp :: Time.UTCTime,
logSessionId :: SessionId,
logAgentId :: AgentId,
logUserId :: Maybe Text,
logEventType :: AuditEventType,
logContent :: Aeson.Value,
logMetadata :: LogMetadata
}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON AuditLogEntry where
toJSON e =
Aeson.object
[ "timestamp" .= logTimestamp e,
"session_id" .= logSessionId e,
"agent_id" .= logAgentId e,
"user_id" .= logUserId e,
"event_type" .= logEventType e,
"content" .= logContent e,
"metadata" .= logMetadata e
]
instance Aeson.FromJSON AuditLogEntry where
parseJSON =
Aeson.withObject "AuditLogEntry" <| \v ->
AuditLogEntry
</ (v Aeson..: "timestamp")
<*> (v Aeson..: "session_id")
<*> (v Aeson..: "agent_id")
<*> (v Aeson..:? "user_id")
<*> (v Aeson..: "event_type")
<*> (v Aeson..: "content")
<*> (v Aeson..:? "metadata" Aeson..!= emptyMetadata)
avaLogsDir :: FilePath
avaLogsDir = Paths.avaLogsDir
subagentLogsDir :: FilePath
subagentLogsDir = Paths.subagentLogsDir
newSubagentId :: IO SubagentId
newSubagentId = do
uuid <- UUID.nextRandom
pure <| SubagentId <| Text.take 6 <| UUID.toText uuid
subagentLogPath :: SubagentId -> FilePath
subagentLogPath (SubagentId sid) =
subagentLogsDir </> Text.unpack sid <> ".jsonl"
todayLogPath :: IO FilePath
todayLogPath = do
today <- Time.utctDay </ Time.getCurrentTime
let dateStr = Time.formatTime Time.defaultTimeLocale "%Y-%m-%d" today
pure (avaLogsDir </> dateStr <> ".jsonl")
mkLogEntry ::
SessionId ->
AgentId ->
Maybe Text ->
AuditEventType ->
Aeson.Value ->
LogMetadata ->
IO AuditLogEntry
mkLogEntry session agent user eventType content metadata = do
now <- Time.getCurrentTime
pure
AuditLogEntry
{ logTimestamp = now,
logSessionId = session,
logAgentId = agent,
logUserId = user,
logEventType = eventType,
logContent = content,
logMetadata = metadata
}
writeAvaLog :: AuditLogEntry -> IO ()
writeAvaLog entry = do
Dir.createDirectoryIfMissing True avaLogsDir
path <- todayLogPath
let line = Aeson.encode entry <> "\n"
LBS.appendFile path line
writeSubagentLog :: SubagentId -> AuditLogEntry -> IO ()
writeSubagentLog sid entry = do
Dir.createDirectoryIfMissing True subagentLogsDir
let path = subagentLogPath sid
let line = Aeson.encode entry <> "\n"
LBS.appendFile path line
readSubagentLogs :: SubagentId -> IO [AuditLogEntry]
readSubagentLogs sid = do
let path = subagentLogPath sid
exists <- Dir.doesFileExist path
if exists
then parseJsonlFile path
else pure []
readAvaLogs :: Time.Day -> IO [AuditLogEntry]
readAvaLogs day = do
let dateStr = Time.formatTime Time.defaultTimeLocale "%Y-%m-%d" day
let path = avaLogsDir </> dateStr <> ".jsonl"
exists <- Dir.doesFileExist path
if exists
then parseJsonlFile path
else pure []
getRecentAvaLogs :: Int -> IO [AuditLogEntry]
getRecentAvaLogs n = do
today <- Time.utctDay </ Time.getCurrentTime
entries <- readAvaLogs today
pure (take n (reverse entries))
parseJsonlFile :: FilePath -> IO [AuditLogEntry]
parseJsonlFile path = do
contents <- LBS.readFile path
let textLines = Text.lines <| Text.decodeUtf8 <| LBS.toStrict contents
pure <| mapMaybe (Aeson.decodeStrict <. Text.encodeUtf8) textLines
|