Skip to content

feat(perhost): split Update into the shared Lock and the lock-held Apply - #437

Merged
github-actions[bot] merged 1 commit into
mainfrom
feat/perhost-lock
Nov 24, 2026
Merged

github-actions[bot] merged 1 commit into
mainfrom
feat/perhost-lock

Conversation

@aitrawog

Copy link
Copy Markdown
Contributor

Summary

#435 made spec/perhost the ONE reader/writer of the per-host
charly.yml and moved the git-ref cache onto it. One writer could not follow: the typed
deploy-overlay cycle (deploykit.MutateDeployConfig) has to hold the lock across a
typed read-modify-write — the caller's mutation runs against a freshly re-read
*DeployConfig, and charly deploy reset decides save-versus-remove under the same hold.
Neither can be expressed as one perhost.Update, so both re-derived the lock themselves:

  • deploykit/deploy_config_cycle.go acquired kit.AcquireFileLockWithin(path+".lock", true, deployConfigLockWait) with its own deployConfigLockWait = 10 * time.Second — a copy of
    perhost.LockWait and a second statement of the lock path;
  • deploykit.SaveDeployConfig re-implemented the whole document mechanics (read the file,
    set keys, marshal, tempfile, chmod, rename) because it is called inside that cycle and
    must not take the lock again.

This PR splits perhost.Update into the two halves it is already made of, and exports both:

Function Takes the lock Does
perhost.Lock(path) — acquires <path>.lock, bounded by LockWait (the ONE definition of the lock path and bound)
perhost.Apply(path, mutate) no — the caller holds it read the CURRENT file, mutate its root, marshal, atomic replace
perhost.Update(path, mutate) yes Lock + Apply; unchanged behaviour

Apply must not re-acquire: flock is per-open-file-description, so a second acquire inside
the same process waits for the first and fails at LockWait. That is exactly the trap the
next SDK change walks into (routing SaveDeployConfig and the cycle through these two), so
it is pinned here rather than in the consumer: perhost_lock_test.go proves that Apply
under a held Lock writes and returns inside 5 s, that an Update started while Lock
is held cannot complete and completes once it is released (i.e. the two share ONE lock),
and that a nil mutate is refused by both.

This closes #436. The consumer change (SDK: SaveDeployConfig →
perhost.Apply, AcquireDeployConfigLock → perhost.Lock, the duplicated constant deleted,
plus a cross-writer concurrency test) is opencharly/sdk#631.

How tested

$ git rev-parse HEAD
1f08004aedfc5643b51d170119e0e2fb08d8b7c9

$ GOWORK=off go build ./... && GOWORK=off go vet ./perhost/ ./refs/ \
    && GOWORK=off go test ./... -timeout 900s && test -z "$(gofmt -l .)" && echo GATE_OK
…
ok  	github.com/opencharly/spec/perhost	1.308s
ok  	github.com/opencharly/spec/refs	16.812s
…
GATE_OK

$ GOTOOLCHAIN=go1.26.0 golangci-lint run ./perhost/
0 issues.

Test accounting, stated exactly:

  • NEW in this PR — perhost_lock_test.go: TestApplyWritesUnderTheCallersLock (fails if
    Apply acquires: it would block, and the 5 s bound reports it),
    TestUpdateWaitsForTheCallersLock (fails if Lock and Update do not share one lock),
    TestApplyRefusesANilMutate.
  • PRE-EXISTING on main, re-run here — the whole perhost and refs suites, including the
    24-concurrent-Update preservation test and the corrupt-file refusal.

Rulebook compliance

  • R0 — /charly-internals:go, /charly-internals:design, /charly-internals:git-workflow,
    /charly-internals:strict-policy.
  • R1 — no anomaly; the self-deadlock hazard of a lock-held writer is named in the code and
    pinned by a test instead of being discovered at runtime.
  • R2 — this is de-duplication: one lock path, one bound, one document mechanics. The
    consumer PR deletes the copied constant and the copied write.
  • R3 — no sleep, retry or suppression; the 5 s / 300 ms bounds are test deadlines, not
    production waits.
  • R5 — nothing is left as a shim: Update keeps its exact signature and behaviour as
    Lock + Apply.
  • R6 — no destructive action.
  • R7 — each new test fails without its guard (an Apply that locks hangs into its 5 s
    bound; a Lock on a different path lets Update complete early and fails the second test).
  • R8/R9/R10 — N/A: library change, no live service, no binary, no bed.
  • R11 — no shape, field, kind, verb or capability is added to the contract; this is a
    mechanism-neutral helper package, the same posture as refactor(refs)!: route the per-host config through ONE reader/writer (spec/perhost) #435.

Change classification

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)

perhost.Update is the ONE reader/writer of the per-host charly.yml, but a
caller whose critical section is WIDER than one document update cannot use it:
deploykit's typed overlay cycle (deploykit.MutateDeployConfig) must hold the
lock across a typed read-modify-write, and `charly deploy reset` must decide
save-versus-remove under the same hold. Both therefore re-derived the lock path
and re-invented the bound (`deployConfigLockWait = 10 * time.Second`, a copy of
perhost.LockWait) instead of sharing perhost's.

This splits the shell into the two halves Update is made of, and exports both:

  - Lock(path) — THE definition of the lock: `<path>.lock`, bounded by LockWait.
  - Apply(path, mutate) — the document mechanics (read the CURRENT file, mutate
    its root, marshal, atomic replace), for a caller that ALREADY holds Lock.

Update is now Lock + Apply, unchanged in behaviour. Apply must NOT re-acquire:
flock is per-open-file-description, so the second acquire in the same process
waits for the first and fails at LockWait — a self-deadlock. Both facts are
pinned by perhost_lock_test.go (Apply under a held Lock writes and returns
inside 5 s; an Update started while Lock is held cannot complete and completes
once it is released).

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: 1f08004aedfc5643b51d170119e0e2fb08d8b7c9 (branch: feat/perhost-lock)

Security screen: PASS — pure library refactor; no secrets, no guardrail weakening, no supply-chain/CI-script change, no embedded directives.
Placement: CORRECT — Lock/Apply are the reusable halves of the existing perhost lock + document-write cycle, already shared by Update and consumed next by deploykit/SDK; the shared perhost mechanism package is the right layer, not core.
Checklist: All applicable rules PASS or NA.

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 11:14
@github-actions
github-actions Bot merged commit eb6aa04 into main Nov 24, 2026
4 checks passed
charly-auto-merge Bot added a commit that referenced this pull request Nov 24, 2026
github-actions Bot pushed a commit to opencharly/charly that referenced this pull request Nov 24, 2026
…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)
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.

perhost exposes Update only, so a wider cycle re-derives the per-host lock path and bound

1 participant