Heuristic:1ay1 Agentty Cross Process Locking
| Knowledge Sources | |
|---|---|
| Domains | Concurrency, Security |
| Last Updated | 2026-10-28 14:00 GMT |
Overview
Protect shared credential and configuration state with cross-process locks so that concurrent agentty instances cannot corrupt each other's writes.
Description
The Cross_Process_Locking heuristic requires that any file-based shared state (credential stores, configuration files, thread logs) is protected by a lock that works across processes, not just threads. Multiple agentty instances may run simultaneously (e.g., multiple terminal sessions), and without cross-process locking, concurrent writes to the credential store can corrupt the file or interleave entries. The lock is acquired via `flock` (or equivalent) on a dedicated lock file, with bounded retry and clear error reporting.
In agentty this appears in the credential domain (`auth.hpp`) which coordinates access to the shared credential store across concurrent sessions.
Usage
Apply this heuristic whenever a file is written by a process that may have multiple concurrent instances. Thread-only locks (`std::mutex`) are insufficient — they protect within one process but not across processes. Use `flock` on a lock file, with a bounded timeout to avoid permanent deadlock.
The Insight (Rule of Thumb)
- Action: Use `flock` (or platform equivalent) on a dedicated lock file for any multi-process shared state.
- Value: Exclusive lock for writes, shared lock for reads; bounded timeout.
- Trade-off: Locking overhead and potential wait; but prevents file corruption from concurrent writes.
- Compatibility: Pairs with Heuristic:1ay1_Agentty_Bounded_Never_Fatal — lock acquisition failure must degrade, not crash.
Reasoning
A `std::mutex` only synchronizes threads within one process. When two terminal windows run agentty simultaneously, they are two processes sharing the same `~/.agentty` directory. Without cross-process locking, their concurrent writes to the credential store interleave and corrupt the file. `flock` on a dedicated lock file is the standard Unix mechanism for this: it works across processes, is automatically released on process exit, and supports both shared (read) and exclusive (write) modes.