Class: Ask::Agent::Extensions::AuditLog

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/agent/extensions/audit_log.rb,
lib/ask/agent/extensions/audit_log/active_record_writer.rb

Overview

Event-driven audit log for agent sessions.

Subscribes to all session events and writes them to a configurable adapter. Ships with an ActiveRecord adapter; custom adapters can implement the simple Adapter interface.

Examples:

Enable globally (ActiveRecord)

Ask::Agent.configure do |c|
  c.audit_log = { adapter: :active_record }
end

Per-session with custom adapter

Session.new(model: "gpt-4o", audit_log: { adapter: MyWriter.new })

Defined Under Namespace

Classes: ActiveRecordWriter, Adapter, FileAdapter

Constant Summary collapse

STORED_EVENTS =

Event types that get persisted (not every delta/stream event).

{
  Events::SessionStart       => "session_start",
  Events::SessionEnd         => "session_end",
  Events::TurnEnd            => "turn_end",
  Events::ToolExecutionStart => "tool_execution_start",
  Events::ToolExecutionEnd   => "tool_execution_end",
  Events::Error              => "error",
  Events::MaxTurnsExceeded   => "max_turns_exceeded",
  Events::LoopDetected       => "loop_detected",
  Events::CompactionEnd      => "compaction_end",
  Events::EvaluationBlocked  => "evaluation_blocked"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(session, adapter: nil) ⇒ AuditLog

Returns a new instance of AuditLog.



59
60
61
62
63
64
# File 'lib/ask/agent/extensions/audit_log.rb', line 59

def initialize(session, adapter: nil)
  @session = session
  @session_id = session.id
  @adapter = resolve(adapter)
  subscribe! if @adapter
end

Instance Method Details

#after_tool_call(tool_call, result, _context) ⇒ Object

Legacy hook interface — kept for backward compatibility. Called by the hooks system after each tool execution.



78
79
80
81
82
83
84
85
# File 'lib/ask/agent/extensions/audit_log.rb', line 78

def after_tool_call(tool_call, result, _context)
  write_entry("tool_call", {
    tool_name: tool_call.name,
    arguments: tool_call.arguments,
    result: result&.to_s&.to_s[0, 500],
    duration_ms: result[:duration_ms]
  })
end

#subscribe!Object

Subscribe to session events and log stored event types.



67
68
69
70
71
72
73
74
# File 'lib/ask/agent/extensions/audit_log.rb', line 67

def subscribe!
  @session.on_event do |event|
    type = STORED_EVENTS[event.class]
    next unless type

    write_entry(type, extract(event))
  end
end