Cortex

Audit Trail

Bridge lifecycle events to an audit backend for compliance and debugging.

The audit_hook package bridges Cortex lifecycle events to an audit trail backend. It records structured audit events with action, resource, category, severity, and metadata.

Setup

import "github.com/xraph/cortex/audit_hook"

// With a Recorder implementation
auditExt := audithook.New(myRecorder)

// Or with a simple function
auditExt := audithook.New(audithook.RecorderFunc(func(ctx context.Context, event *audithook.AuditEvent) error {
    log.Printf("AUDIT: %s %s %s", event.Action, event.Resource, event.ResourceID)
    return nil
}))

// Register with engine
eng, _ := engine.New(
    engine.WithExtension(auditExt),
)

Recorder interface

type Recorder interface {
    Record(ctx context.Context, event *AuditEvent) error
}

The RecorderFunc adapter lets you use a plain function:

type RecorderFunc func(ctx context.Context, event *AuditEvent) error

AuditEvent

type AuditEvent struct {
    Action     string
    Resource   string
    Category   string
    ResourceID string
    Metadata   map[string]any
    Outcome    string
    Severity   string
    Reason     string
}

Actions

18 audit actions are defined:

ActionDescription
cortex.agent.run.startedAgent run initiated
cortex.agent.run.completedRun completed successfully
cortex.agent.run.failedRun failed
cortex.step.startedReasoning step started
cortex.step.completedReasoning step completed
cortex.tool.calledTool invocation initiated
cortex.tool.completedTool call completed
cortex.tool.failedTool call failed
cortex.persona.resolvedPersona resolved for run
cortex.skill.activatedSkill activated
cortex.behavior.triggeredBehavior triggered
cortex.cognitive.phase_changedCognitive phase changed
cortex.trait.appliedTrait applied
cortex.checkpoint.createdCheckpoint created
cortex.checkpoint.resolvedCheckpoint resolved
cortex.orchestration.startedOrchestration started
cortex.orchestration.completedOrchestration completed
cortex.agent.handoffAgent-to-agent handoff

Resources

ResourceDescription
agentAgent entity
runRun entity
toolTool entity
personaPersona entity
skillSkill entity
behaviorBehavior entity
checkpointCheckpoint entity
orchestrationOrchestration entity

Categories

CategoryDescription
agentAgent lifecycle events
toolTool invocation events
personaPersona and behavior events
checkpointCheckpoint events
orchestrationMulti-agent orchestration events

Severity levels

LevelUsage
infoNormal operations (run started, tool called)
warningUnusual but non-critical events
criticalFailures (run failed, tool failed)

Filtering

Use WithActions to limit which events are recorded:

auditExt := audithook.New(myRecorder,
    audithook.WithActions(
        audithook.ActionRunStarted,
        audithook.ActionRunFailed,
        audithook.ActionCheckpointCreated,
    ),
)

Only the specified actions will be recorded; all others are silently dropped.

On this page