Skip to content

fix(refs): ClearCache clears the cache section, never the per-host deploy config - #434

Merged
github-actions[bot] merged 1 commit into
mainfrom
fix/cache-clear-preserve
Nov 24, 2026
Merged

github-actions[bot] merged 1 commit into
mainfrom
fix/cache-clear-preserve

Conversation

@aitrawog

@aitrawog aitrawog commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

charly cache clear/refresh deleted the per-host deploy config
(~/.config/charly/charly.yml) to clear a cache: section — taking every deploy, provides,
ledger and system entry with it. The next resolution's save() (correctly a preserving
yaml.Node read-modify-write) then rebuilt the file holding nothing but cache:.

Measured on a real host on 2026-11-24: after a charly cache refresh the file went from ~94 KB
with seven deploys to 50 KB of cache, while every deployed container kept running — the config was
gone but the machines were not. Only the containers' systemd quadlets still described them.

Fix: ClearCache clears the section through the same writer save() uses (set the maps
empty, then save() → the cache is rewritten empty, every other key preserved) and never removes
the file. The stale comment claiming a removal goes with it.

How tested

# head f58ef991939de40adfd8ad73b62101eb2abc23c4 (git rev-parse HEAD)
$ GOTOOLCHAIN=go<go.mod> go test ./refs/
ok  	github.com/opencharly/spec/refs	13.686s
$ gofmt -l refs/git_client.go refs/git_client_clear_test.go
(no output)

R7 — the new test is red without the fix and green with it:

# with refs/git_client.go reverted to the previous head
--- FAIL: TestClearCachePreservesTheDeployConfig (0.00s)
    git_client_clear_test.go:55: the per-host config must still exist after a cache clear:
    open /tmp/…/charly.yml: no such file or directory

TestGitClientCacheSurface's old assertion that the file is REMOVED encoded the defect; it now
asserts the real contract (file survives, cache section empty). The observable
charly cache refresh behaviour changes once a spec release carrying this is pinned by charly;
the unit test is the gate here.

One reader/writer for the per-host config (R2)

The file is a SHARED document — deploys, the git-ref cache, the ledger and the system info are
written into it by different 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 (this PR's first commit; the bypass race in its second). spec/perhost is now
that contract stated once, using the CACHE writer's own approach:

  • ONE advisory lock, <file>.lock, bounded at 10 s (a stuck holder fails fast and is named);
  • the read happens INSIDE the lock — never a caller's snapshot;
  • the mutation touches ONLY the keys it owns, so every other section survives;
  • the write is a tempfile in the same directory + rename, mode 0600 (no torn reads);
  • a corrupt file is an ERROR and the mutation never runs — the bytes remain for recovery.

refs (load, save, ClearCache) runs through it now. A defect this migration introduced was
caught by the suite
: 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, and TestGitClientCacheAndPersist hung for the whole package
timeout (600 s). The helpers are lock-free, and TestSaveIsCallableWhileHoldingMu now pins the
invariant by asserting that save() RETURNS (a write-content test cannot see a deadlock).

New tests, all green (go test ./refs/ ./perhost/ → 16.1 s / 1.1 s):

  • perhost: every other section preserved; a corrupt file refused with the bytes untouched;
    24 concurrent Update callers each keeping their own key.
  • refs: ClearCache preservation; the bypass lost-update guard; the g.mu invariant.

The sdk's three writers (kit system, kit ledger, deploykit's MutateDeployConfig) follow the same
ORDER but each keeps its own copy; migrating them onto perhost is the next leg after this
release, filed as opencharly/sdk#631.

Rulebook compliance

  • R0 — loaded /charly-internals:root-cause-analyzer, /charly-internals:strict-policy,
    /charly-internals:go, /charly-internals:xdg, /charly-internals:git-workflow.
  • R1 — root cause traced to the exact call (os.Remove(g.cacheFile)) before any edit; the
    incident and its trigger are recorded here; the placement defect (a cache in a config document)
    is filed as fix(refs): the git-ref cache lives in the per-host deploy config; move it under the cache root (D-STORE-1) #433 rather than silently fixed by moving a file path.
  • R2 — clearing reuses save()'s one preserving writer instead of a second write path.
  • R5 — the destructive path and its comment are deleted, and the test that encoded it is
    corrected in the same change.
  • R6 — N/A — nothing destructive; the branch is a session worktree, pushed fast-forward.
  • R7 — the regression test fails without the change (pasted above).
  • R11 — no shape, field or mechanism added; a method body corrected to its own documented
    contract ("updates only the cache: key, preserving every other key").
  • R8/R9/R10 — N/A — no live service, no binary, no bed in this module.

Change classification

  • Class: library fix (one method + tests).
  • Impact: ClearCache no longer destroys the deploy config; every other key is preserved.
    Behaviour of charly cache refresh is otherwise unchanged.
  • Rollback: revert the commit.

Filed issues

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)

…ploy config

`charly cache clear/refresh` deleted `~/.config/charly/charly.yml` — the per-host
DEPLOY config — to clear a `cache:` section, taking every deploy, provides, ledger
and system entry with it. The next resolution's `save()` (correctly a preserving
yaml.Node read-modify-write) then rebuilt the file holding nothing but `cache:`.

Measured on a real host, 2026-11-24: after a `charly cache refresh` the file went
from ~94 KB with seven deploys to 50 KB of cache, while every deployed container
kept running — the config was gone but the machines were not, the worst shape of
this failure. The containers' systemd quadlets were the only surviving record.

ClearCache now clears the section through the SAME writer save() uses (empty maps
→ the cache is rewritten empty, every other key preserved), and never removes the
file. The stale comment claiming a removal is gone with it.

Regression proof (R7): `TestClearCachePreservesTheDeployConfig` is RED without the
fix — `the per-host config must still exist after a cache clear: … no such file or
directory` — and green with it; `TestGitClientCacheSurface`'s old assertion that the
file is removed is corrected to the real contract (file survives, cache empty). Full
`./refs/` suite green.

The observable `charly cache refresh` behaviour changes when a spec release carrying
this is pinned by charly; the unit test is the gate here.

Refs #433 (the cache's placement: move it under the cache root)

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)
@github-actions

Copy link
Copy Markdown

Review — PASS

Head SHA: b439c525fc461ddeec0930388fa6e76f745496e8 (branch: fix/cache-clear-preserve)

Security screen: PASS — diff is a minimal, in-scope library fix; no secrets, no egress, no guardrail weakening, no instruction-injection content.
Checklist: All applicable rules PASS or NA. Change class = library fix (no bed/binary in this module, declared N/A with reason); the new regression test is red-without-fix (pasted FAIL at git_client_clear_test.go:55, which matches the file's line 55), ClearCache reuses save() (R2), destructive path + comment removed in the same commit (R5), placement defect filed as #433, and the Agent:/Assisted-by: footers are well-formed. No comments on the thread to disposition.

Verdict: PASS


ollama-cloud/deepseek-v4.1-flash — action-review.

View action run

@github-actions
github-actions Bot enabled auto-merge (squash) November 24, 2026 09:19
@github-actions
github-actions Bot merged commit 1bf4138 into main Nov 24, 2026
4 checks passed
charly-auto-merge Bot added a commit that referenced this pull request Nov 24, 2026
aitrawog added a commit that referenced this pull request Nov 24, 2026
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)
github-actions Bot pushed a commit that referenced this pull request Nov 24, 2026
…(spec/perhost) (#435)

* refactor(refs): route the per-host config through ONE reader/writer (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)

* refactor(refs): delete the dead cacheLockPath helper; harden perhost.Set

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)

* refactor(refs): one persistCache write for save() and ClearCache; harden 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant