Skip to main content

AuditLog

Struct AuditLog 

Source
pub struct AuditLog { /* private fields */ }
Expand description

A session-scoped, append-only sequence of AuditEntry records that enforces monotonic sequence numbers and hash-chain continuity on every append.

§Invariants

  • Every entry’s seq equals the previous entry’s seq + 1 (genesis: seq = 0).
  • Every entry’s previous_hash equals the preceding entry’s entry_hash (genesis entry uses [0u8; 32]).

Both invariants are checked by AuditLog::push at append time. AuditLog::verify_chain re-validates them across the entire stored log.

Implementations§

Source§

impl AuditLog

Source

pub fn new(agent_id: AgentId, session_id: SessionId) -> Self

Create a new, empty AuditLog for the given agent and session.

The log starts with next_seq = 0 and last_hash = [0u8; 32] (the genesis previous-hash sentinel).

Source

pub fn entries(&self) -> &[AuditEntry]

Read-only view of all entries in append order.

Source

pub fn len(&self) -> usize

Number of entries currently stored in the log.

Source

pub fn is_empty(&self) -> bool

Returns true if the log contains no entries.

Source

pub fn agent_id(&self) -> AgentId

The agent identifier associated with this log.

Source

pub fn session_id(&self) -> SessionId

The session identifier associated with this log.

Source

pub fn push(&mut self, entry: AuditEntry) -> Result<(), AuditLogError>

Append a pre-built AuditEntry to the log, validating both invariants.

§Errors

On error the log is not modified.

Source

pub fn next_entry( &mut self, event_type: AuditEventType, timestamp_ns: u64, payload: String, ) -> &AuditEntry

Build and append the next AuditEntry in one atomic step.

seq and previous_hash are derived automatically from the log’s current state, eliminating the risk of caller-side sequencing errors.

§Parameters
  • event_type — category of the governance event.
  • timestamp_ns — nanoseconds since Unix epoch (caller-supplied for no_std compatibility).
  • payload — pre-serialized UTF-8 string (JSON in practice).

Returns a reference to the newly appended entry.

Source

pub fn next_entry_with_lineage( &mut self, event_type: AuditEventType, timestamp_ns: u64, payload: String, lineage: Lineage, ) -> &AuditEntry

Build and append the next AuditEntry with lineage fields in one atomic step.

Equivalent to AuditLog::next_entry but attaches agent-topology metadata. seq and previous_hash are derived automatically from the log’s current state.

§Parameters
  • event_type — category of the governance event.
  • timestamp_ns — nanoseconds since Unix epoch (caller-supplied for no_std compatibility).
  • payload — pre-serialized UTF-8 string (JSON in practice).
  • lineage — optional agent-topology fields; Lineage::default() produces the same hash as AuditLog::next_entry with the same base fields.

Returns a reference to the newly appended entry.

Source

pub fn verify_chain(&self) -> bool

Re-validate the entire log in O(n), checking both invariants for every entry.

Returns true if:

  • Every entry passes AuditEntry::verify_integrity (SHA-256 matches stored hash).
  • Every entry’s seq is exactly one greater than the previous entry’s seq (first entry must have seq = 0).
  • Every entry’s previous_hash matches the preceding entry’s entry_hash (first entry must have previous_hash = [0u8; 32]).

Returns true for an empty log (vacuously valid).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.