MCPcopy Create free account
hub / github.com/ByteYellow/AgentProvenance / loadToolCallWindows

Function loadToolCallWindows

internal/intent/materialize.go:287–321  ·  view source on GitHub ↗

loadToolCallWindows loads each agent hook tool_call's real [start,end] window. Windows are only usable once hook events carry real timestamps (launch's hook-log stamps them); tool_calls left at the synthetic epoch are ignored so a bogus 2026-01-01 window can never bracket a real effect.

(db *sql.DB, runID string)

Source from the content-addressed store, hash-verified

285// hook-log stamps them); tool_calls left at the synthetic epoch are ignored so a
286// bogus 2026-01-01 window can never bracket a real effect.
287func loadToolCallWindows(db *sql.DB, runID string) toolCallWindows {
288 rows, err := db.Query(`SELECT id, agent_id, command, started_at, ended_at FROM tool_calls
289 WHERE run_id = ? AND agent_id != '' AND started_at != ''`, runID)
290 if err != nil {
291 return nil
292 }
293 defer rows.Close()
294 var out toolCallWindows
295 for rows.Next() {
296 var id, agent, command, startedAt, endedAt string
297 if err := rows.Scan(&id, &agent, &command, &startedAt, &endedAt); err != nil {
298 return out
299 }
300 start, err := time.Parse(time.RFC3339Nano, startedAt)
301 if err != nil || start.Year() < 2020 {
302 continue // synthetic-epoch or unparseable -> not a real window
303 }
304 end, err := time.Parse(time.RFC3339Nano, endedAt)
305 if err != nil {
306 end = start
307 }
308 // Grace: PostToolUse can fire a few seconds before the tool's subprocess
309 // actually performs its syscalls (observed ~3s on the lab VM), so a tight
310 // window misses the real effect. But it must stay SMALL: a truly async
311 // effect (e.g. an install that triggers a background exfil ~35s later) must
312 // NOT be pinned to a same-program tool call that merely ran nearby -- that
313 // is a wrong-tool attribution. Effects outside this grace fall to an honest
314 // coverage_gap; tying them back needs command-match, not a wider window.
315 out = append(out, toolCallWindow{
316 agent: agent, toolCall: id, program: programName(command),
317 start: start.Add(-grace), end: end.Add(grace),
318 })
319 }
320 return out
321}
322
323// containing returns the most specific (latest-starting) window that both
324// brackets ts AND whose program matches the effect's process comm. The comm gate

Callers 1

normalizeEffectsFunction · 0.85

Calls 3

programNameFunction · 0.85
AddMethod · 0.80
CloseMethod · 0.45

Tested by

no test coverage detected