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
seqequals the previous entry’sseq + 1(genesis:seq = 0). - Every entry’s
previous_hashequals the preceding entry’sentry_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
impl AuditLog
Sourcepub fn new(agent_id: AgentId, session_id: SessionId) -> Self
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).
Sourcepub fn entries(&self) -> &[AuditEntry]
pub fn entries(&self) -> &[AuditEntry]
Read-only view of all entries in append order.
Sourcepub fn session_id(&self) -> SessionId
pub fn session_id(&self) -> SessionId
The session identifier associated with this log.
Sourcepub fn push(&mut self, entry: AuditEntry) -> Result<(), AuditLogError>
pub fn push(&mut self, entry: AuditEntry) -> Result<(), AuditLogError>
Append a pre-built AuditEntry to the log, validating both invariants.
§Errors
AuditLogError::SequenceGapifentry.seq() != self.next_seq.AuditLogError::HashChainBrokenifentry.previous_hash() != &self.last_hash.
On error the log is not modified.
Sourcepub fn next_entry(
&mut self,
event_type: AuditEventType,
timestamp_ns: u64,
payload: String,
) -> &AuditEntry
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 forno_stdcompatibility).payload— pre-serialized UTF-8 string (JSON in practice).
Returns a reference to the newly appended entry.
Sourcepub fn next_entry_with_lineage(
&mut self,
event_type: AuditEventType,
timestamp_ns: u64,
payload: String,
lineage: Lineage,
) -> &AuditEntry
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 forno_stdcompatibility).payload— pre-serialized UTF-8 string (JSON in practice).lineage— optional agent-topology fields;Lineage::default()produces the same hash asAuditLog::next_entrywith the same base fields.
Returns a reference to the newly appended entry.
Sourcepub fn verify_chain(&self) -> bool
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
seqis exactly one greater than the previous entry’sseq(first entry must haveseq = 0). - Every entry’s
previous_hashmatches the preceding entry’sentry_hash(first entry must haveprevious_hash = [0u8; 32]).
Returns true for an empty log (vacuously valid).