open static method

Future<AgentFileAuditLog> open(
  1. AgentAuditStore store, {
  2. Duration retention = AgentAuditLog.defaultRetention,
  3. DateTime clock()?,
})

Opens the stream over store: reads what is retained, deletes what is not, and continues numbering after the highest opId found.

A file that does not parse, or is of another format, is skipped rather than failing the open — the trail is written by this code alone, so an unreadable file is a torn write, and refusing to start the tool surface over it would turn one lost batch into none being recorded at all.

Implementation

static Future<AgentFileAuditLog> open(
  AgentAuditStore store, {
  Duration retention = AgentAuditLog.defaultRetention,
  DateTime Function()? clock,
}) async {
  final log = AgentFileAuditLog._(store, retention: retention, clock: clock);
  await log._deleteExpired();
  final loaded = <AgentAuditEntry>[];
  for (final path in await store.listFiles()) {
    if (!path.endsWith('.json')) continue;
    try {
      final raw = jsonDecode(await store.readString(path)) as Map;
      if (raw['version'] != formatVersion) continue;
      for (final map in raw['entries'] as List) {
        loaded.add(AgentAuditEntry.fromMap(map as Map));
      }
    } on Object {
      continue;
    }
  }
  log._adopt(loaded);
  return log;
}