Repository navigation
refactor(refs)!: route the per-host config through ONE reader/writer (spec/perhost) - #435
Conversation
…spec/perhost) The per-host config (~/.config/charly/charly.yml) is a SHARED document: deploys, the git-ref cache, the ledger and the system info are written into it by different charly commands that can run concurrently. Four independent implementations of "take <file>.lock, read the yaml.Node, find-or-create my key, marshal, tempfile+rename" had grown, and the copies disagreed in ways that destroyed real data on 2026-11-24: one cleared a section by DELETING the whole file, and one wrote a stale in-memory value over a value another process had just changed. spec/perhost is that contract stated once — the cache writer's own approach, extracted: - ONE advisory lock, <file>.lock, bounded (10 s: a stuck holder fails fast, named); - the read happens INSIDE the lock (never a caller's snapshot); - the mutation touches ONLY the keys it owns (every other section survives); - the write is a tempfile in the same directory + rename (no torn reads), mode 0600; - a corrupt file is an ERROR and the mutation never runs — the bytes stay for recovery. refs (load, save, ClearCache) now runs through it; each caller supplies only its section logic. The two data-loss fixes of the previous commits become consequences of the shared writer rather than special cases. A defect this migration introduced and the suite caught: the helpers save() called took g.mu, but save() is documented to stay LOCK-FREE because LatestTag/DefaultBranch and the cache tests call it while holding g.mu — sync.Mutex is not reentrant, so TestGitClientCacheAndPersist hung for the package timeout. The helpers are lock-free now, and TestSaveIsCallableWhileHoldingMu pins the invariant by asserting that save() RETURNS. Tests: perhost — preservation of every other section, corrupt-file refusal (mutation never runs, bytes untouched), and 24 concurrent Update callers each keeping their own key; refs — the ClearCache preservation guard, the bypass lost-update guard, the mu invariant, and the existing suite. `go test ./refs/ ./perhost/` green (16.1 s / 1.1 s). The sdk's own three writers (kit system, kit ledger, deploykit's MutateDeployConfig) follow the same ORDER but each keeps its own copy of the mechanics; migrating them onto perhost is the next leg, after this release, tracked as opencharly/sdk#631. Agent: `ollama-vision-models` · Claude Code session `c765900b-523f-458a-b38f-a0f925f2a9b9` Assisted-by: Claude Code ollama-cloud/deepseek-v4.1-flash (fully tested and validated)
Review — BLOCKHead SHA: Blocks
Non-blocking note
Comment intake
Verdict: BLOCK ollama-cloud/deepseek-v4.1-flash — action-review. |
Round-1 review fixes on #435. - Block 1 (CONC(a)/R5): `cacheLockPath()` lost its only caller when save() moved onto perhost.Update, and the `lock` import went with it. Deleted — `grep -rn cacheLockPath` returns nothing, and perhost.Update's `<path>.lock` is the ONE lock path for this file, which is what makes the body's "the cache's own copy of the mechanics is deleted here" true. - Block 2 (A1): the ClearCache preservation guard is PRE-EXISTING on main (it landed in #434); the body called it new. The body is corrected; no test accounting changes here. - Non-blocking note, fixed because this package becomes THE writer for three more sections (sdk#631): perhost.Set no longer APPENDS a second key when an intermediate path exists as a non-mapping node — it REPLACES that node with a mapping in place. A duplicate YAML key resolves differently per parser, so the latent path is worth closing now. TestSetReplacesANonMappingIntermediateInsteadOfDuplicatingTheKey pins it. Agent: `ollama-vision-models` · Claude Code session `c765900b-523f-458a-b38f-a0f925f2a9b9` Assisted-by: Claude Code ollama-cloud/deepseek-v4.1-flash (fully tested and validated)
Review — BLOCKHead SHA: The prior bot BLOCK (7128081097, at
That prior comment is not authoritative; it carries nothing forward. Blocks
Non-blocking notes
Comment intake
Verdict: BLOCK ollama-cloud/deepseek-v4.1-flash — action-review. |
…den the shared writer Round-2 review fixes on #435. - Block 1 (R2): save() and ClearCache() carried the IDENTICAL perhost.Update lambda while this PR's premise is de-duplication. Extracted `persistCache()`; both call it. warmUpSaveFn() moved INTO it, at the single place a write happens, so the seam counts WRITES (the coalescing test's contract) and ClearCache's write is now counted too — it was not when ClearCache carried its own copy. ClearCache stays un-gated by suppressSave, and says why: a clear is an operator action, never a coalesced prefetch write. - Note: perhost.Set now zeroes Style and Anchor on a node it replaces in place (a styled scalar would otherwise keep its style on the fresh mapping). - Note: SetBypass releases bypassOwned after the persisting write, so a long-lived client cannot re-assert a stale flag over a later operator clear. The missed-control RCA for the ROUND-1 findings is posted as this round's PR comment (the rulebook asks for it there): the orphaned cacheLockPath had no gate that can see an unused METHOD, and the duplicate-key path had no adversarial-input test. Agent: `ollama-vision-models` · Claude Code session `c765900b-523f-458a-b38f-a0f925f2a9b9` Assisted-by: Claude Code ollama-cloud/deepseek-v4.1-flash (fully tested and validated)
|
STATUS STATUS — round-2 fixes, with the missed-control RCA the round is required to carry. Missed control for each round-1 finding (R1):
Round-2 fixes (head
No comment remains unaddressed. Agent: |
Review — PASSHead SHA: Security screen: PASS — no scope mismatch, no secrets/egress, no weakened gate (the shared writer tightens the lock with a bounded Checklist: All applicable rules PASS or NA. Code path: library change gated by Comment intake:
Non-blocking notes (no BLOCK):
Verdict: PASS ollama-cloud/deepseek-v4.1-flash — action-review. |
…e shared lock (#632) * refactor(deploykit,kit): every per-host config writer reads inside the shared lock The per-host charly.yml has FOUR writers that run in different charly processes — the deploy overlay, the install ledger, the host snapshot and the git-ref cache. opencharly/spec#435 put the cache on spec/perhost and made it that file's one reader/writer; this routes the other three through it and fixes the lost update the ledger had kept. THE LEDGER READ OUTSIDE THE LOCK (the data-loss bug). WriteDeployRecord, WriteCandyRecord, DeleteDeployRecord, DeleteCandyRecord and AddCandyDeployment/RemoveCandyDeployment all read the ledger, mutated the snapshot, and then handed it to writeLedger, which locked and replaced the whole `ledger:` section with that stale snapshot. Two concurrent ledger writes therefore each dropped the other's record. Measured with the new cross-writer test against the pre-change code: 21 of 24 concurrently-added candy records lost, on 5 of 5 runs. They now call ONE mutateLedger, which decodes the `ledger:` section from the document spec/perhost's lock just read and lets the mutation run on THOSE maps — read inside the lock, own key only, atomic replace. THE DEPLOY WRITER'S SECOND COPY. SaveDeployConfig kept its own read/marshal/tempfile/chmod/rename because it is called INSIDE the deploy cycle (deploykit.MutateDeployConfig), which already holds the lock and must not be re-entered — flock is per-open-file-description, so a second acquire in one process waits for the first and fails at perhost.LockWait. It now writes through perhost.Apply (the lock-held half of perhost.Update, spec v0.2026328.1115) and the cycle takes its lock from perhost.Lock, so deployConfigLockWait — a 10 s copy of perhost.LockWait — and the second lock path are deleted. The fail-safe re-read moves INSIDE the locked section, so the check and the write now see the same bytes. THE READER. readLedgerStrict and readLedger both decode through one ledgerFromRoot over perhost.Read; ledgerDoc (a second, hand-rolled decode of the same section) is deleted. Coverage: deploykit/perhost_concurrency_test.go drives all four real writers at once (the deploy cycle, eight concurrent ledger installs released together per wave, SetBypass, and PopulateSystemInfo) and asserts every deploy entry, every ledger record and the cache/system sections survive. It is red on the pre-change tree (21/24 records lost) and green here. Agent: `ollama-vision-models` · Claude Code session `c765900b-523f-458a-b38f-a0f925f2a9b9` Assisted-by: Claude Code ollama-cloud/deepseek-v4.1-flash (fully tested and validated) * fix(kit,deploykit): refuse a wrong-shape ledger, prove the relocated fail-safe Round-1 review fixes. - Block 1 (R1): ledgerFromRoot discarded Decode's error and read a present-but-non-mapping `ledger:`/`ledger.deploys`/`ledger.candies` as an EMPTY ledger. The decode this replaced (yaml.Unmarshal into ledgerDoc) errored on exactly those, so readLedgerStrict had silently lost the strictness it exists for. It now returns (maps, error), refuses a wrong-shape section by name, mutateLedger propagates the refusal (bytes stay), readLedger keeps its best-effort degradation, readLedgerStrict propagates. Five new tests (kit/install_ledger_shape_test.go) — four wrong-shape cases and a writer refusal — all fail against 1613fd2 and pass here. - Block 2 (CONC d): the dead-symbol sweep is now pasted, not summarized. - Block 3 (R10): the fail-safe re-read moved into perhost.Apply's mutation and no test executed it (the concurrency test nulls DeployStateHost). deploykit/deploy_failsafe_test.go drives it: an erroring LoadUnifiedDeployConfig makes SaveDeployConfig refuse and leaves the file's bytes byte-identical, while the same call with a loadable config writes. Deleting the recheck() call makes the test fail ("wrote over a present-but-unloadable config"). - Block 4 (CONC b): the warm-up sweep finding is filed (opencharly/spec#438) instead of being explained away; the bypass disposition names the spec pin this PR carries. Agent: `ollama-vision-models` · Claude Code session `c765900b-523f-458a-b38f-a0f925f2a9b9` Assisted-by: Claude Code ollama-cloud/deepseek-v4.1-flash (fully tested and validated)
Summary
The per-host config (
~/.config/charly/charly.yml) is a SHARED document — deploys, the git-refcache, the ledger and the system info are written into it by different charly commands that can run
concurrently. Four independent copies of "lock, read the yaml.Node, set my key, marshal,
tempfile+rename" had grown, and the copies have already destroyed real data on a host
(2026-11-24): one cleared a section by DELETING the whole file, and one wrote a stale in-memory
value over a value another process had just changed.
spec/perhostis that contract stated once — the cache writer's own approach, extracted:<file>.lock, bounded at 10 s (a stuck holder fails fast and is named);rename, mode 0600 (no torn reads);refs(load, save, ClearCache) now runs through it; the two data-loss fixes already on main becomeconsequences of the shared writer rather than special cases.
This PR re-lands a commit the previous merge missed. #434 squashed only the
first commit of its branch —
git ls-tree -r v0.2026328.920 | grep perhost→ 0. The auto-mergefired on the earlier head before the second push was validated, and a
MERGEDsignal says a PRlanded, not what it carried. This branch is that commit cherry-picked onto current
main.A defect the migration introduced and the suite caught: the helpers
save()called tookg.mu,but
save()must stay LOCK-FREE —LatestTag/DefaultBranchand the cache tests call it whileholding
g.mu, andsync.Mutexis not reentrant, soTestGitClientCacheAndPersisthung for thefull 600 s package timeout. The helpers are lock-free; a content-only test could never have seen it.
How tested
Test accounting, stated exactly:
perhost: every other section preserved; a corrupt file refused with itsbytes untouched; 24 concurrent
Updatecallers each keeping their own key; andSetreplacing a non-mapping intermediate instead of emitting a duplicate key.refs:TestSaveIsCallableWhileHoldingMu(assertssave()RETURNS) andTestConcurrentSaveDoesNotResurrectAClearedBypass(the bypass lost-update guard).ClearCachepreservationguard,
TestClearCachePreservesTheDeployConfig, and the correctedTestGitClientCacheSurface.Review-round fixes carried by this branch:
cacheLockPath()— dead oncesave()moved ontoperhost.Update— is deleted (grep -rn cacheLockPath→ no output), so the shared writer's<path>.lockis the ONE lock path for this file;perhost.Setreplaces a non-mapping intermediaterather than appending a duplicate key, and zeroes
Style/Anchoron a node it replaces;save()and
ClearCache()now share ONEpersistCache()write (the round-2 R2 finding), withwarmUpSaveFn()inside it so every write — ClearCache's included — is counted; andSetBypassreleases
bypassOwnedafter the persisting write, so a long-lived client cannot re-assert a staleflag over a later operator clear. The round's missed-control RCA is posted as a PR comment.
Rulebook compliance
/charly-internals:root-cause-analyzer,/charly-internals:strict-policy,/charly-internals:go,/charly-internals:xdg,/charly-internals:git-workflow.other three.
Change classification
Filed issues
deploykit) through
spec/perhost, with the cross-writer concurrency test.Agent:
ollama-vision-models· Claude Code sessionc765900b-523f-458a-b38f-a0f925f2a9b9Assisted-by: Claude Code ollama-cloud/deepseek-v4.1-flash (fully tested and validated)