Commit 2fb94e8
perf+fix(daemon): cache getKnownRigs() per tick, atomic rigs.json writes (#3684)
Fixes #3463, #3464.
Cache getKnownRigs() per heartbeat tick (#3463)
d.getKnownRigs() re-read and re-parsed mayor/rigs.json on every call,
and ~10 call sites run per heartbeat tick. Memoize the result for the
duration of a tick via a new (knownRigsCache, knownRigsCacheValid) pair
on Daemon. Invalidated at the start of each heartbeat so changes
between ticks are picked up; accessed only from the heartbeat-loop
goroutine so no synchronization is needed.
The existing callers all already use d.getKnownRigs(), so no call-site
changes are required — they automatically benefit from the cache.
Atomic rigs.json writes (#3464)
SaveRigsConfig used os.WriteFile, which truncates the file before
writing. A concurrent reader (getKnownRigs on another tick, doctor
checks, LoadRigsConfig elsewhere) could observe a zero-byte or partial
file and fail with "unexpected end of JSON input".
- SaveRigsConfig now writes to a temp file in the same directory and
renames into place; the rename is atomic on POSIX.
- LoadRigsConfig retries once on read/parse failures as a
belt-and-suspenders recovery if an older non-atomic writer is still
in the process tree.
- doctor/workspace_check.go Fix paths (RigsRegistryExistsCheck and
RigsRegistryValidCheck) switch to the atomic write helper — same
atomicity guarantee, same rigs.json target.
Extract atomic-write helpers to internal/atomicfile
internal/config could not import internal/util's AtomicWriteFile
without a cycle (util → lock → tmux → config via orphan.go). Rather
than duplicate the helper, move the atomic-write primitives out of
util into a dedicated leaf package internal/atomicfile, which has no
internal deps and so can be imported from anywhere. 13 existing
callers are updated to atomicfile.WriteFile / WriteJSON /
WriteJSONWithPerm / EnsureDirAndWriteJSON; util retains its non-atomic
helpers. Net -569 lines.
Tests
- internal/atomicfile: full port of the existing util/atomic_test.go
suite (concurrency, integrity, permissions, read-only-dir,
large-file cases).
- TestGetKnownRigs_CachedBetweenInvalidations: populates cache, deletes
rigs.json, asserts d.getKnownRigs() still returns cached results, and
that invalidation surfaces disk state (including after rewrites).
- TestSaveRigsConfig_AtomicAgainstConcurrentReaders: 4 concurrent
writers × 2000 reader iterations; fails if any raw read observes a
zero-byte or parse-failing file.
- TestLoadRigsConfig_RetriesOnTruncatedRead: asserts the retry path
recovers a real payload after a seeded truncated state.
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>1 parent 66a8ff0 commit 2fb94e8
File tree
19 files changed
+542
-219
lines changed- internal
- agent
- atomicfile
- config
- crew
- daemon
- doctor
- dog
- doltserver
- polecat
- quota
- state
- util
- wisp
19 files changed
+542
-219
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
62 | 62 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
0 commit comments