WeaveDocs
Agentfs Watch

Usage

Using `agentfs-watch` — register watchers, consume event batches, and handle backpressure under sustained filesystem churn.

Purpose

Restartable filesystem watcher with event batches, backpressure policy, debouncing, and overflow handling.

This page follows the real source shape for Agentfs Watch and explains the workflow a developer is likely to use first.

Developer workflow

Start from the smallest constructor or builder, perform one meaningful operation, inspect the returned state, then add the relevant policy, storage, or network integration. The examples below should be expanded whenever the crate API changes.

Primary types to know

  • EventBatch — local/agentfs-watch/src/lib.rs
  • RestartConfig — local/agentfs-watch/src/lib.rs
  • WatchConfig — local/agentfs-watch/src/lib.rs
  • Watcher — local/agentfs-watch/src/lib.rs
  • WatcherBuilder — local/agentfs-watch/src/lib.rs
  • WatchEvent — local/agentfs-watch/src/lib.rs
  • BackpressurePolicy — local/agentfs-watch/src/lib.rs
  • EventType — local/agentfs-watch/src/lib.rs
  • WatchError — local/agentfs-watch/src/lib.rs

Example shape

use agentfs_watch::{BackpressurePolicy, EventBatch, WatcherBuilder};
use futures::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Builder configures roots, glob filters, debounce, batching, and queueing.
    let watcher = WatcherBuilder::new()
        .roots(["/tmp/observed"])
        .include_globs(["**/*.rs", "**/*.toml"])
        .exclude_globs(["**/target/**"])
        .debounce_ms(50)
        .coalesce_window_ms(200)
        .batch_max_items(64)
        .batch_max_ms(500)
        .queue_capacity(1024)
        .backpressure(BackpressurePolicy::DropOldest)
        .recursive(true)
        .build()?;

    // Each yielded EventBatch is a coalesced burst of WatchEvent values.
    let mut stream = std::pin::pin!(watcher.into_stream());
    if let Some(batch) = stream.next().await {
        let EventBatch { events, .. } = batch;
        println!("observed batch of {} events", events.len());
    }
    Ok(())
}