Repository navigation
refactor(deploykit,kit): every per-host config writer reads inside the shared lock - #632
Conversation
…e 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)
Review — BLOCKHead SHA: Change class: library refactor — deploykit/kit per-host config writers routed through Blocks
Comments considered: none — the thread is empty (0 comments). Verdict: BLOCK ollama-cloud/deepseek-v4.1-flash — action-review. |
…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)
|
STATUS Round-2 RCA and fixes — all four blocks fixed in this round. 1. R1 + T3 — the strict reader lost its strictness (real defect, fixed)Mechanism. Missed control. I checked only the one case the existing test pinned (unparseable YAML) and Fix. 2. CONC (d) — a described sweep is not a sweep (fixed)The body now pastes the command and its output verbatim; the transcript is in 3. R10 — the relocated fail-safe was unproven (fixed)Correct: Missed control. When code MOVES, the test that exercised it before the move must be re-aimed Fix. 4. CONC (b) — a surfaced finding was dismissed (fixed)Missed control. I decided the disposition of each finding by whether a narrative came to mind
Agent: Agent: |
Review — PASSHead SHA: Security screen: PASS — no secrets/egress, no weakened gate (the fail-safe and strict-reader refusals are preserved and now proven), spec pin is first-party. Verdict: PASS ollama-cloud/deepseek-v4.1-flash — action-review. |
…1265) * chore(deps): bump spec to v0.2026328.1115 and sdk to v0.2026328.1130 The shipped binary must carry the per-host config writer fixes; neither pin had them yet. - spec v0.2026328.1115 (opencharly/spec#437): spec/perhost is the ONE reader/writer of ~/.config/charly/charly.yml — one bounded lock, the read INSIDE it, own-key-only mutation, atomic replace — and exposes Lock/Apply for the wider typed overlay cycle. - sdk v0.2026328.1130 (opencharly/sdk#632): the three remaining SDK writers (deploy overlay, install ledger, host snapshot) run through it. It also fixes a measured lost update: every ledger writer read the ledger OUTSIDE the lock and replaced the whole section with that stale snapshot, so two concurrent ledger writes each dropped the other's record — 21 of 24 concurrently-added records lost, 5 of 5 runs, with the SDK's new cross-writer test against the pre-change tree. No charly source changes: charly has no per-host config writer of its own (grep: os.WriteFile only in internal/pluginsgen and one comment; the DeployConfigPath/DeployConfigEnv helpers are referenced only by tests). Gates on this tree: go mod tidy, go build ./..., go vet ./..., go test ./... (CORE_TEST_OK), and the R9 worktree-local build — ./scripts/bootstrap-charly.sh -> bin/charly 2026.328.1114. Live, with the pre-bump binary 2026.327.1010: `charly cache bypass --off` printed "git-ref cache bypass cleared — the cache resumes" and the file read `bypass: true` again seconds later, written by a concurrent long-lived charly process that had loaded the flag before the clear (the bypassOwned defect). With the rebuilt binary the clear is written and read back as `false`. 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(deps): propagate the spec/sdk pins to the tools modules Round-1 CI fix: the `go` job failed on tools/gomod-canonical's TestContractPinsMatchShared — tools/generate-packages, tools/golden-cloudinit and tools/golden-compile are separate GOWORK=off modules that mirror the contract pins, and the bump touched only charly/go.mod: gomod_canonical_test.go:123: ../../tools/generate-packages/go.mod: requires sdk v0.2026327.1021, but the shared sdk pin is v0.2026328.1130 — run `charly task mods-tidy` `charly task mods-tidy` propagated both pins into the three modules (go.mod + go.sum each). Missed control: the gate I designed reproduced the CI job I remembered, not the one that runs. ci.yml also carries `go test ./charly/...` from the repo root, the `tools/*` sweep, `gofmt -l charly tools` and a per-module golangci-lint — and this repo ships `mods-tidy` for exactly this propagation. The full job is now reproduced verbatim: CI_REPRO_OK (charly/ 241.9s, tools sweep ok, gofmt empty, golangci-lint 0 issues in charly/ and in all five tools modules). 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
Closes #631. The per-host
charly.yml(~/.config/charly/charly.yml) has foursection writers that run in different charly processes — the deploy overlay, the install ledger,
the host snapshot and the git-ref cache. opencharly/spec#437 published
spec/perhostas thatfile's ONE reader/writer (
Lock,Apply,Update,Read,Get,Set); this routes the threeSDK writers through it and fixes the lost update the ledger had kept.
The ledger read outside the lock (a real data loss, found by this migration).
WriteDeployRecord,WriteCandyRecord,DeleteDeployRecord,DeleteCandyRecordandAddCandyDeployment/RemoveCandyDeploymentread the ledger, mutated that snapshot, and handed itto
writeLedger, which locked and replaced the wholeledger:section with the snapshot it hadbeen given. Two concurrent ledger writes therefore each dropped the other's record — measured
against the pre-change tree with the new test: 21 of 24 concurrently-added candy records lost,
on 5 of 5 runs. They now call ONE
mutateLedger, which decodes theledger:section from thedocument
spec/perhost's lock just read and runs the mutation on THOSE maps: read inside thelock, own key only, atomic replace.
The deploy writer's second copy.
SaveDeployConfigkept its ownread/marshal/tempfile/chmod/rename. It is called INSIDE the deploy cycle
(
deploykit.MutateDeployConfig), which already holds the lock and must not be re-entered —flockis per-open-file-description, so a second acquire in one process waits for the first andfails at
perhost.LockWait. It now writes throughperhost.Apply(the lock-held half ofperhost.Update), and the cycle takes its lock fromperhost.Lock, sodeployConfigLockWait(a10 s copy of
perhost.LockWait) and its second lock path are gone. The fail-safe re-read movesINSIDE the locked mutation, so the check and the write are one critical section.
The reader.
readLedger,readLedgerStrictand every writer decode through oneledgerFromRootoverperhost.Read; the hand-rolledledgerDocdecode of the same section isdeleted.
ledgerFromRootREFUSES a wrong-shape section (ledger:/ledger.deploys:/ledger.candies:present but not a mapping) by name, so the strict reader keeps the strictness itexists for: the strict path propagates the refusal, the best-effort path degrades to empty, and a
writer refuses to rewrite a malformed ledger.
The remaining per-host writer is
kit.writeSystemInfo(system:), migrated in the same commit.How tested
Red proofs, measured, not assumed.
The concurrency test. With
kit/install_ledger.gorestored to its pre-change form and the newtest unchanged:
and with this branch's tree:
okon 5 of 5 consecutive runs.The strictness tests. Against this PR's round-1 head
1613fd2(same new test files):The fail-safe. With the
recheck()call deleted fromSaveDeployConfig(same test file):Test accounting, stated exactly:
deploykit/perhost_concurrency_test.go(TestConcurrentPerHostWritersPreserveEverySection):all four real writers at once — the deploy cycle (
MutateDeployConfig+SaveDeployConfig),eight concurrent
kit.AddCandyDeploymentinstalls released together per wave (threewaves),
refs.GitClient.SetBypass, andkit.PopulateSystemInfo— then asserts every deployentry, every ledger record and the
cache:/system:sections survive.deploykit/deploy_failsafe_test.go:TestSaveDeployConfigRefusesAnUnloadableConfig(refusal +bytes untouched) and
TestSaveDeployConfigWritesWhenTheConfigLoads(the refusal is not abroken save path).
kit/install_ledger_shape_test.go: four wrong-shape strict-reader cases, a writer refusal thatleaves the bytes untouched, and an absent-section case that must stay a non-error.
kitanddeploykitsuites,including
TestSaveDeployConfig_RoundTrip, the strict-ledger contract tests(
TestListStrict_MalformedLedgerIsAnError) and the ledger tx-lock tests.Scope of one claim, stated exactly: the fail-safe test proves the re-check GATES the write and the
bytes survive; that the check sits inside the locked mutation is structural — the mutation runs
while
perhost.Lockis held (pinned byspec/perhost'sTestApplyWritesUnderTheCallersLock) —which is what makes check-and-replace one critical section. It is not separately hookable from
outside.
Rulebook compliance
R0 —
/charly-internals:go,/charly-internals:plugin,/charly-internals:design,/charly-internals:git-workflow,/charly-internals:strict-policy.R1 — the ledger lost update is the anomaly; RCA: the missed control was that no test drove
two writers at once, so every copy of the mechanics was validated only against itself. The root
fix is both the read-inside-the-lock mutation and the cross-writer test that would have caught
it; the red proof above is that test failing on the pre-change code. The round-1 strictness
regression is RCA'd in this PR's round-2 comment (missed control: a body claim of "unchanged"
with no test that fails when it is not).
R2 — this PR is the de-duplication the issue asked for: one lock path, one bound, one read,
one decode, one atomic write.
R3 — no sleeps, retries or suppressions in production code; the test's channel gates are a
barrier, not a delay.
R5 —
writeLedger,ledgerDocanddeployConfigLockWaitare deleted, not deprecated, andthe stale comment naming
writeLedgeris corrected. Claim-keyed sweep, pasted verbatim:No live symbol remains: the matches are the pre-existing test helper
writeLedgerFixture(twofiles) and three comments that describe the change.
R6 — no destructive action; the worktree was clean (
git status) before the branch.R7 — every new guard fails without its fix (three red proofs above) and the changed paths
run live in
go test ./....R8/R9/R10 — N/A: library change, no live service, no binary, no bed. The consuming binary
rebuild is charly's own pin-bump PR.
R11 — no shape, field, kind, verb or capability changes; the SDK keeps rendering the shapes
DESIGN owns and adds no new one.
Change classification
SaveDeployConfig's contract is now stated: it isthe lock-held writer its cycle calls (
deploykit.MutateDeployConfig,AcquireDeployConfigLock),writing through
perhost.Apply. Every runtime caller is already inside that cycle. Onebehaviour change, deliberate and named: a malformed
ledger:section is now REFUSED by a writerand by the strict reader instead of being read as empty.
parse ledger <path>: …; an unreadable file nowreports
parse ledger <path>: per-host config <path>: …where the old code saidread ledger <path>: ….Filed issues
git ls-remotesweep spec#438 — the warm-up's cold check accepts an age-less persistedlatest_tagsentrybut requires a ≤1 h in-memory default-branch answer, so once the persisted answers age out every
project load pays a full 32-repo
git ls-remotesweep. Found while measuring the host this workstarted on (30:45 wall vs 4:00 CPU for one
charly config, network contention included). Theproposed remedy and its test are in the issue; it is NOT fixed here because it changes the
ref-cache freshness contract, which is its own decision.
Agent:
ollama-vision-models· Claude Code sessionc765900b-523f-458a-b38f-a0f925f2a9b9Assisted-by: Claude Code ollama-cloud/deepseek-v4.1-flash (fully tested and validated)