How it worksCách hoạt động

One verdict for a machine shared by many agents.Một kết luận cho chiếc máy nhiều agent dùng chung.

host-watcher measures CPU, memory, disk and free space, tells every coding agent the same thing, and makes heavy commands wait their turn. Local only.host-watcher đo CPU, bộ nhớ, ổ đĩa và dung lượng trống, báo cùng một thông tin cho mọi coding agent, và cho lệnh nặng chờ đến lượt. Chạy hoàn toàn cục bộ.

$ host_status.pysampled

throttle

cpu
62%
memory
71%
disk active
78%
free
182G

Illustrative values, not a real machine.Số liệu minh hoạ, không phải máy thật.

01

The problemVấn đề

Run five agents in five worktrees and they all start npm install, test suites and builds at the same moment. The disk saturates, everything slows down, and nobody can tell which job did it. host-watcher measures the machine, tells every agent the same thing, and lets heavy commands wait their turn. It runs locally: no network, no telemetry, no database, no required daemon.

Chạy năm agent trong năm worktree, cả năm cùng lúc bắt đầu npm install, test và build. Ổ đĩa quá tải, mọi thứ chậm lại, và không ai biết job nào gây ra. host-watcher đo tình trạng máy, báo cùng một thông tin cho mọi agent, và cho các lệnh nặng chờ đến lượt. Mọi thứ chạy cục bộ: không gọi mạng, không telemetry, không database, không bắt buộc chạy daemon.

Windows Task Manager: CPU 20%, memory 66%, Disk 0 active time 100%, average response time 664 ms, write 39.4 MB/s

Real host · 18 parallel sessionsMáy thật · 18 session song song

CPU sits at 20% and memory at 66%, so a CPU-only check says "fine". Meanwhile the NVMe disk is 100% active with a 664 ms average response time, while writing only 39 MB/s. Every agent waits on I/O. This is the contention host-watcher is built to catch: it reads active time and latency, not throughput, and this machine would get hold.

CPU chỉ 20%, bộ nhớ 66%, nên nếu chỉ nhìn CPU thì tưởng máy vẫn ổn. Trong khi đó ổ NVMe bận 100%, thời gian phản hồi trung bình 664 ms, dù chỉ ghi 39 MB/s. Mọi agent đều đứng chờ I/O. Đây chính là kiểu tranh chấp host-watcher được làm ra để bắt: nó đọc thời gian bận và độ trễ chứ không đọc throughput, và máy này sẽ nhận kết luận hold.

02

ArchitectureKiến trúc

OS counters /proc · vm_stat · PDH CPU · memory disk active % · latency free space · per-pid I/O watcher host_watch.py (or Go binary) samples every few seconds median of samples → verdict one instance (watch.lock) ~/.host-watcher/ status.json (≤15 s fresh) inbox/inbox/<worktree-hash>.jsonlt;hashinbox/<worktree-hash>.jsongt;.json config.json (thresholds) watch.lock · watch.pid shared by Python + Go host_status.py read snapshot, or sample once if stale host_gate.py -- cmd wait on hold / clean then run at low priority agent hooks Claude Code · Pi · Codex read-only, always exit 0 cache_cleanup.py inventory + dry-run --apply only when safe

One optional watcher samples the OS and writes a snapshot. Everything else reads that snapshot, so ten agents asking "is the machine busy?" cost nothing. Without a watcher, host_status.py and the gate sample on their own.

Một watcher (tuỳ chọn) đọc số liệu từ hệ điều hành và ghi ra một snapshot. Mọi thành phần khác chỉ đọc snapshot đó, nên mười agent cùng hỏi "máy có đang bận không?" cũng gần như không tốn gì. Khi không có watcher, host_status.py và gate sẽ tự đo.

03

Five verdictsNăm kết luận

goPlenty of headroom. Run anything.Máy còn dư sức. Chạy gì cũng được.
throttleBusy. Run, but avoid starting extra parallel jobs.Máy đang bận. Vẫn chạy, nhưng đừng mở thêm job song song.
unknownNothing could be measured. Never blocks.Không đo được gì. Không bao giờ chặn lệnh.
cleanA watched volume is low on free space. Free some first.Một ổ đang theo dõi sắp hết chỗ. Hãy dọn bớt trước.
holdSaturated. Heavy commands wait.Máy đã quá tải. Lệnh nặng phải chờ.

Severity order: go < throttle < unknown < clean < hold. The worst signal wins. Every verdict exits 0, so scripts read the verdict, not the exit code.

Thứ tự mức độ: go < throttle < unknown < clean < hold. Tín hiệu tệ nhất quyết định kết luận. Mọi kết luận đều trả exit code 0, nên script cần đọc kết luận chứ không dựa vào exit code.

04

How a verdict is decidedKết luận được quyết định thế nào

3 samples take the median Disk active ≥90% or ≥100 ms → hold · ≥70% / ≥50 ms → throttle CPU · Memory CPU ≥95% / mem ≥95% → hold · ≥85% / ≥88% → throttle Free space any watched volume < 25 GB free → clean Nothing measured no CPU, memory or disk sample → unknown worst wins max severity verdict

Only disk active time and latency count. A fast sequential copy at high throughput is not contention; queued I/O waiting on the disk is. All thresholds live in config.json (see configuration).

Với ổ đĩa, chỉ thời gian bận (active time) và độ trễ (latency) được tính. Một lệnh copy tuần tự nhanh, throughput cao, không phải là tranh chấp; I/O phải xếp hàng chờ ổ đĩa mới là tranh chấp. Mọi ngưỡng nằm trong config.json (xem configuration).

05

The gateGate

holdwait 15 s holdwait 30 s cleanwait 60 s (then 60 s …) gostart npm run buildlow CPU + I/O priority max wait 30 min → run anyway with a warning · --max-wait 0 skips waiting exit code = child's exit code · 5 = could not start · 2 = bad arguments nice + ionice (Linux) · taskpolicy -b (macOS) · BELOW_NORMAL (Windows)
$ python scripts/host_gate.py -- npm run build

The gate never refuses to run. It waits only on hold and clean, backs off 15 → 30 → 60 seconds, then starts the command at low priority and passes its exit code and signals through. If the status cannot be read, it runs immediately.

Gate không bao giờ từ chối chạy lệnh. Nó chỉ chờ khi kết luận là hold hoặc clean, giãn thời gian chờ 15 → 30 → 60 giây, sau đó chạy lệnh ở mức ưu tiên thấp và chuyển tiếp nguyên exit code và tín hiệu. Nếu không đọc được trạng thái máy, nó chạy ngay.

06

Worktree inboxInbox theo worktree

watcher sees top I/O pid in worktree B inbox/sha256(B)[:16].json expires after 10 min agent in worktree B hook/gate: "your job is the top I/O user" agents in A, C, D see only the shared verdict

The watcher maps the heaviest I/O process to its git worktree and leaves a notice only for that worktree, so the agent that caused the pressure is the one asked to slow down. Needs per-process I/O: Linux, or Windows with the Go watcher.

Watcher tìm process đang ghi/đọc đĩa nhiều nhất, xác định nó thuộc git worktree nào, rồi chỉ để lại thông báo cho đúng worktree đó. Nhờ vậy agent gây ra áp lực là agent được nhắc chậm lại. Tính năng này cần số liệu I/O theo từng process: có trên Linux, và trên Windows khi dùng watcher bản Go.

07

Safe cache cleanupDọn cache an toàn

--glob match real dir owned by you idle ≥ 60 min no live process candidate no symlinks · no junctions · process list unreadable → nothing deleted · dry-run unless --apply

Shared package caches (npm, pip, Go, Cargo…) are only listed with their own prune command; host-watcher never deletes them. See cleanup safety.

Các cache dùng chung của trình quản lý gói (npm, pip, Go, Cargo…) chỉ được liệt kê kèm lệnh dọn riêng của chúng; host-watcher không bao giờ tự xoá. Xem cleanup safety.

08

Platform supportNền tảng hỗ trợ

LinuxWSLWindowsmacOS
CPU · memory✓✓✓✓
Disk active / latencyĐộ bận / độ trễ ổ đĩa✓virtual diskổ ảo✓—
Free spaceDung lượng trống✓✓✓✓
Worktree inboxInbox theo worktree✓✓Go watcherwatcher bản Go—

Details in platform support.

Chi tiết trong platform support.

09

Quick startBắt đầu nhanh

$ git clone https://github.com/bestagentkits/host-watcher
$ cd host-watcher
$ python scripts/host_status.py
$ python scripts/host_watch.py --detach
$ python scripts/host_gate.py -- npm run build
$ python scripts/cache_cleanup.py --dry-run

Hooks for Claude Code, Pi, Codex and others: integrations. Prebuilt Go binaries: releases.

Hook cho Claude Code, Pi, Codex và các agent khác: integrations. Binary Go dựng sẵn: releases.