Skip to content

fix(learning): hold the partition publication lock across the full skill write - #1230

Merged
jhrozek merged 1 commit into
mainfrom
worktree-fix-learned-skills-generation-race
Sep 8, 2026
Merged

jhrozek merged 1 commit into
mainfrom
worktree-fix-learned-skills-generation-race

Conversation

@jhrozek

@jhrozek jhrozek commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixes a flaky test (TestUsableAutoSkillsStockBuildPolicyMatrix/untrusted_project_ignored, internal/app/usable_auto_skills_matrix_e2e_test.go), reproduced ~15-20% of the time via go test -run TestUsableAutoSkillsStockBuildPolicyMatrix -count=30 -race, surfacing as an opaque server: internal error: learned skill operation failed.
  • Root cause: buildProcedureProcessor (internal/app/learned_skills.go) only acquired the per-partition assets.skillPublication lock inside the publishable branch. skillmaterialize.Materialize (creates the draft version) and skilllifecycle.Pipeline.Process both write the shared assets.learnedSkills repository regardless of publishable, so a non-publishable write (e.g. an untrusted workspace, demoted to Review) ran fully unlocked — racing the foreground ListLearnedSkills RPC's own lock-guarded read (learnedSkillPublisher.publishPartition), which detects a mid-read generation change via a sentinel error (errLearnedSkillGenerationChanged) and surfaced it as a hard internal error.
  • Fix: acquire the lock unconditionally, before any partition-mutating call in the closure — closing the actual synchronization gap rather than tolerating the race.

Path not taken

An earlier version retried on the read side instead of fixing the lock. It was rejected in review because it silently changed the contract TestADR_0259_DelayedPublicationCannotRevokeNewerGeneration pins (a stale publisher must hard-error, never silently revive). This version leaves that test's contract untouched — confirmed still green.

New coverage

Adds TestBuildProcedureProcessorHoldsPublicationLockForNonPublishablePartition, a deterministic regression test (forces the write to pause mid-flight, asserts a concurrent lock() attempt on the same partition blocks until it finishes) — it caught an intermediate version of this fix that scoped the lock only around Pipeline.Process and missed Materialize's earlier write, so it's proven to fail if the lock's scope narrows again, not just probabilistically flake.

Review

This is a concurrency fix (not test-only), so it went through three rounds of independent review (go-architect + test-adequacy + standards + duplication + library-reuse across the iterations) before opening this PR — including one round that caught the rejected retry-based approach, and a final round that caught the Materialize gap in an earlier version of this same fix. Final pass: no blockers; one advisory addressed (the lock now also spans Pipeline.Process's Evaluator.Evaluate, a possible LLM call, and the lock isn't ctx-aware — noted inline as a ponytail:-flagged ceiling, not treated as a regression since correctness wins here).

Test plan

  • go test ./internal/app/... -run TestBuildProcedureProcessorHoldsPublicationLockForNonPublishablePartition -count=3 -race — passes; fails without the fix (proven during development)
  • go test ./internal/app/... -run 'TestADR_0259|TestLearnedSkillPublicationFailureQuarantinesOnlyOwnedPartitionsAndReconciles' -count=5 -race — all pass, no regression
  • go test ./internal/app/... -run TestUsableAutoSkillsStockBuildPolicyMatrix -count=30 -race — 30/30 pass (was ~15-20% failure rate before)
  • go test ./internal/app/... -run 'Skill|Learn|Reflection|Procedure' -count=3 — all pass
  • task lint — 0 issues
  • go build ./...

Co-Authored-By: Claude Sonnet 5 [email protected]

…ill write

buildProcedureProcessor only acquired the per-partition
assets.skillPublication lock inside the publishable branch, but
skillmaterialize.Materialize (which creates the draft version) and
skilllifecycle.Pipeline.Process both write assets.learnedSkills
regardless of publishable. A non-publishable write (e.g. an untrusted
workspace, demoted to Review) therefore ran fully unlocked, letting it
race the foreground ListLearnedSkills RPC's own lock-guarded read
(learnedSkillPublisher.publishPartition), which detects a mid-read
generation change via a sentinel error and previously surfaced it as
an opaque internal error to the caller.

Reproduced reliably (~15-20% of runs) via:
  go test ./internal/app/... -run TestUsableAutoSkillsStockBuildPolicyMatrix -count=30 -race

Fix: acquire the lock unconditionally, before any partition-mutating
call in the closure, closing the actual synchronization gap instead of
retrying past it. An earlier retry-based approach was reviewed and
rejected because it silently changed the behavior
TestADR_0259_DelayedPublicationCannotRevokeNewerGeneration pins; this
version leaves that contract untouched (confirmed still green).

Adds TestBuildProcedureProcessorHoldsPublicationLockForNonPublishablePartition,
a deterministic regression test that fails if the lock's scope is ever
narrowed again, rather than relying solely on the probabilistic e2e
reproduction.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@jhrozek
jhrozek merged commit a822313 into main Sep 8, 2026
25 checks passed
@jhrozek
jhrozek deleted the worktree-fix-learned-skills-generation-race branch September 8, 2026 05:44
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