Make ExpectedConstants unregister the loaders it creates #1420
Labels
No labels
P0
P1
P2
P3
bug
category/code-change
category/cross-repo
category/eval-run
category/investigation
category/no-test-target
category/owner-ruling
category/prose-artifact
category/recurring-chore
category/structure
category/umbrella
crc-refactor
create
delete
enhancement
filed-by/agent
filed-by/user
frozen
lint-rule
needs-info
needs-triage
next
plugin/cc-architect
plugin/os
plugin/os-adr
plugin/os-aidd-lint
plugin/os-backlog
plugin/os-context
plugin/os-doc-hygiene
plugin/os-sdlc
plugin/os-vault
project/cc-os
ready-for-agent
ready-for-human
recurring
review
rule-proposal
update
waiting
wayfinder:grilling
wayfinder:map
wayfinder:map
wayfinder:research
wayfinder:task
wayfinder:task
wontfix
worklist/deviations
worklist/lint-rule
worklist/new-implement-build
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
jared/cc-os#1420
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
ExpectedConstantsleaves a Zeitwerk loader registered after every call.loadedatplugins/os-sdlc/lib/os_sdlc/expected_constants.rb:46runsload loader_pathon the target's loader file.Line 47 then collects every
Zeitwerk::LoaderfromObjectSpacewhose dirs sit underlib_root.Each run adds a new loader to
Zeitwerk::Registry, and a second run also returns the stale loaders from the first run.Callers:
crc/census/settings.rb:47,crc/score/run.rb:26, anddetector/{cycle_edges,error_lineage,holders}/signals.rb.Reproduce
n/a: call
ExpectedConstants.oftwice on a fixture project and compareZeitwerk::Registryloader counts.Approach
After
ExpectedConstantsreadsall_expected_cpathsand the root constant, it unloads and unregisters each loader it created.It must not unregister a loader that existed before the call. When the target is os-sdlc itself,
OsSdlc.loadermay already be set.Risk:
unregisteris marked @experimental in zeitwerk 2.8.3 (loader.rb:365).Required work:
ExpectedConstantscreated — check: presentunregisterinplugins/os-sdlc/lib/os_sdlc/expected_constants.rbAcceptance criteria
ExpectedConstants.ofreturns, then theZeitwerk::Registryloader count equals the count before the call.ExpectedConstants.ofruns twice in one process, then both runs return the same cpaths.The tests use a fixture project and are red before the fix.
AC 3 ("root constant is unchanged") was removed on 2026-10-27: it describes existing behavior, so its test cannot be red.
Owner sketch
all_expected_cpathsanddirs(namespaces: true)need no setup, so both still work on a disposed loader. A pre-existing loader (for exampleOsSdlc.loaderwhen the target is os-sdlc) is selected but never disposed.Origin
loadednever unregisters its loaders ← DESIGN (expected_constants.rb:46)References #1390.
VERDICT: CREATE
Zeitwerk 2.8.3 check (2026-10-27, read-only source review):
unregisteris safe to use under these conditions:loadcall created (snapshotObjectSpace.each_object(Zeitwerk::Loader)before the load, diff after). Never dispose of a pre-existing loader: when the target is os-sdlc,lib/os_sdlc_loader.rbguards withunless OsSdlc.loader, so the load creates nothing andOsSdlc.loadermust stay selected and registered.all_expected_cpathsanddirs(namespaces: true)need nosetup; compute both from the same memoized loader list. Both work after disposal.unloadraisesZeitwerk::SetupRequiredon a never-set-up loader: rescue it, then always callunregister.ObjectSpacestill returns unregistered loaders until GC; select from registered loaders plus the created list, never raw ObjectSpace.unregisteris@experimental; the regression test is the guard on each zeitwerk bump.Extra tests worth having: a second
ExpectedConstantson the same fixture project (no "already managed" error), and the os-sdlc-as-target case.Work started on branch ticket-1420 (os-sdlc pipeline, poodr-ticket-implementation).
Resolution
Done: ExpectedConstants#loaded snapshots ObjectSpace loaders before load, disposes (unload, rescue SetupRequired, ensure unregister) each loader the load created, and selects from pre-existing plus created loaders. Pre-existing loaders such as OsSdlc.loader are never disposed.
Evidence: Commit
9e1f144fon ticket-1420, implementation 188: 8 dispatches all pass; suite gate exit 0; new tests/expected_constants_test.rb (Registry loader count unchanged; two runs return same cpaths) red before, green after.Follow-ups: #1421 (InProcessBin without a fork) is now unblocked.