> ## Documentation Index
> Fetch the complete documentation index at: https://axe-engine.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Watch directories

> Stream changes under a directory, recursively or not, and handle event kinds, overflow, rescans and the shared watch limit.

## Start a watch

> [tab] axectl
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> axectl watch                 # direct children of the root
> axectl watch -r src          # src and every subdirectory
> ```
>
> Each change prints as one line, such as `CREATE src/main.rs`. Errors print to stderr as `axectl: watch: <message>`. An error that ends the watch makes `axectl` exit with `1`; other errors are reported and watching continues. Ctrl-C stops the watch.

> [tab] gRPC
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> grpcurl -plaintext -d '{"path": "src", "recursive": true}' \
>   localhost:9090 axe.v1.Files/Watch
> ```
>
> The first event is `started`, sent once the watch is armed. axed reports changes made after it. Then come `change` events with `kind` and `path`, and `error` events.

The path must be a directory inside the root. A missing path fails with `NOT_FOUND`, a file with `INVALID_ARGUMENT`, and a path outside the root with `PERMISSION_DENIED`.

> [!NOTE]
>
> The Agent Sandbox equivalent is `watcher.v1.FileWatcherService/WatchDir` with `path` and `recursive`. It sends `FileEvent`s with a `type` of `CREATE`, `WRITE`, `REMOVE`, `RENAME`, `CHMOD` or `ERROR`. It has no started event, and its errors do not say whether they end the watch.

## Event kinds

Event paths are relative to the sandbox root, not to the watched directory.

| Kind | Reported when |
| - | - |
| `CREATE` | A file or directory is created, or moved into a watched directory. |
| `WRITE` | A file's contents change. |
| `REMOVE` | A file or directory is deleted. |
| `RENAME` | An entry is moved away. The event names the old path; the new path arrives as a `CREATE` if it lands inside the watched tree. |
| `CHMOD` | Metadata changes: permissions, ownership or timestamps. `touch` on an existing file reports `CHMOD`. |

A session in a watched directory looks like this:

```text theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
$ echo a > f.txt; chmod 600 f.txt; mv f.txt g.txt; rm g.txt
CREATE w/f.txt
WRITE w/f.txt
CHMOD w/f.txt
RENAME w/f.txt
CREATE w/g.txt
REMOVE w/g.txt
```

Uploads through axed stay invisible until they commit, so a watcher never sees their temporary files. axed reports a committed upload as a `CREATE` of its path, even when it replaced an existing file.

Events arrive in order, in small batches: the native API gathers events for 50 ms and the Agent Sandbox API for 250 ms after the first one. A batch holds about 4,096 events at most. During a flood axed stops reading until the batch is sent, so the backlog waits in the kernel's queue, and if that fills you get the overflow error below.

## Recursive watches

With `recursive`, axed watches the directory and every subdirectory below it. It does not follow symlinks. When a directory is created inside the tree, axed starts watching it; when one is removed or moved away, axed stops. A new directory is watched before its event is reported, so changes made in it afterwards are seen. Its existing contents, such as a tree moved in, are covered in the background.

A file created inside a new directory in the instant before axed starts watching that directory is not reported. If you need every file, list a new directory when you see its `CREATE`.

If some subdirectories cannot be watched, usually because the kernel's inotify watch limit (`fs.inotify.max_user_watches`) is reached, or because their path from the sandbox root is longer than 4,096 bytes (`PATH_MAX`), you get one non-terminal error that counts them and names the first few. `axectl watch` prints it as:

```text theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl: watch: recursive watch incomplete: 1200 subdirectories could not be watched: ...
```

The watch keeps running for the directories it covers.

## Overflow and rescan

The kernel queues events for each watch. If a burst fills the queue, the kernel drops events and you get a non-terminal error:

```text theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl: watch: event queue overflowed; some changes were not reported
```

The watch keeps running. When you get this error, rescan. List the directory (`Files.List`) and compare it with what you knew.

## When a watch ends

An error event's `terminal` field says whether it ends the watch:

* **Terminal.** The watched directory itself was removed or renamed. This is the last event.
* **Not terminal.** Queue overflow and incomplete recursive coverage. The watch continues.

A watch also ends when the client disconnects or axed shuts down. If reading kernel events fails, the stream ends with an `INTERNAL` status.

## The shared watch limit

axed allows at most 64 concurrent watches, shared by the native API and the Agent Sandbox API. A watch past the limit fails with `RESOURCE_EXHAUSTED`:

```text theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
too many concurrent file watches (limit 64)
```

axed checks the limit before it sets up the watch, so while all 64 slots are taken, even a watch on a path that does not exist fails with `RESOURCE_EXHAUSTED` rather than `NOT_FOUND`. A refused watch never creates an inotify instance.

Each watch holds its own inotify instance, so a burst in one client's tree cannot overflow another watch's queue. The kernel limits how many inotify instances one user may hold (`fs.inotify.max_user_instances`), and that limit is shared with every other program in the sandbox running as the same user. The cap of 64 bounds how much of that limit axed can take.

Close watches you no longer need. Watching needs inotify; on a kernel without it, watch calls fail and everything else works.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.