Repository navigation
fix(api): force-materialize both sides of a lazy collision before syncWrapper reads their child keys - #364
fix(api): force-materialize both sides of a lazy collision before syncWrapper reads their child keys#364cldmv-bot[bot] wants to merge 3 commits into
Conversation
…cWrapper reads their child keys syncWrapper() decided whether a colliding key already existed on the existing side via Object.prototype.hasOwnProperty against Object.keys(existingWrapper) — which, for an untouched lazy wrapper, reflects only what's been materialized so far, not its full eventual key set. An api.slothlet.api.add() collision against a same-named sibling that had never been touched therefore took the "key doesn't exist yet" fast path (a bare Object.defineProperty) and installed the incoming value directly, without ever reading or materializing the existing module — so its file was never even loaded, its lifecycle events never fired, and "merge" mode's documented "keep existing" contract was silently violated with no way to recover the discarded module afterward. Force-materializing both the existing and incoming wrapper (mirroring setValueAtPath's identical parent-materialization guard) before computing existingChildKeys/nextChildKeys makes every collision mode's existing/already-there check accurate regardless of which side is still lazy. This also fixes replace mode's shadow-recording (_recordReplaceShadows), which reads the same incomplete key list. This is a core lazy-mode correctness defect independent of the stackable-routines feature (#341) that originally surfaced it — discovered while investigating why a routine contributor could be silently dropped on collision (see #362).
There was a problem hiding this comment.
🔵 Needs a closer look
The change forces eager materialization inside syncWrapper() (a widely-used path), which may have broader lazy-mode performance/side-effect implications beyond the targeted collision case.
Pull request overview
This PR fixes a lazy-mode collision edge case where syncWrapper() could incorrectly decide that a colliding child key “doesn’t exist yet” because Object.keys() on an unmaterialized lazy wrapper only reflects already-touched keys, causing the existing module to be skipped entirely (including lifecycle events) during merge-mode collision handling.
Changes:
- Force-materialize both the existing and incoming wrappers in
ApiManager.syncWrapper()before enumerating child keys for collision handling. - Add a regression test ensuring merge-mode keeps the existing lazy leaf and that
impl:createdfires for both contributors during collision resolution. - Add a new lazy collision fixture module and update the generated collision docs to include it.
File summaries
| File | Description |
|---|---|
| tests/vitests/suites/api-manager/api-manager-lazy-collision-materialization.test.vitest.mjs | Adds regression coverage for untouched-lazy collision behavior and lifecycle events. |
| src/lib/handlers/api-manager.mjs | Materializes lazy wrappers before key enumeration to make collision decisions accurate. |
| docs/generated/api_tests/api_test_collisions.md | Updates generated docs to include the new lazybase fixture namespace. |
| api_tests/api_test_collisions/lazybase/sub/test-func.mjs | Adds a lazy-only fixture leaf used by the new regression test. |
Review details
- Files reviewed: 3/4 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…ation can't be narrowed Addresses PR review feedback on the force-materialization added for #364: both narrowing options considered (skip for "replace", or derive keys from __childFilePathsPreMaterialize instead of materializing) are unsafe — every collision mode reaching syncWrapper needs accurate keys from both sides, and the metadata map only ever holds keys copied in from a prior merge step, not a lazy wrapper's own undiscovered children.
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved synchronization and lazy-wrapper materialization issues remain.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (1)
src/lib/handlers/api-manager.mjs:679
- This can materialize a lazy wrapper whose implementation is a function (for example, a lazy single-file folder), but the final bookkeeping later in
syncWrapper()resetsstate.materializedtofalsewheneverimplis a function. The next call then runs the materializer again;_onWrapperMaterialized()decrements the global unmaterialized count a second time, so materialization stats/completion become incorrect. Keep a callable wrapper marked materialized after this forced load and prevent the second completion notification.
if (existingWrapper.____slothletInternal.mode === "lazy" && !existingWrapper.____slothletInternal.state.materialized) {
await existingWrapper._materialize();
}
if (nextWrapper.____slothletInternal.mode === "lazy" && !nextWrapper.____slothletInternal.state.materialized) {
await nextWrapper._materialize();
- Files reviewed: 3/4 changed files
- Comments generated: 2
- Review effort level: Lite
| if (existingWrapper.____slothletInternal.mode === "lazy" && !existingWrapper.____slothletInternal.state.materialized) { | ||
| await existingWrapper._materialize(); | ||
| } | ||
| if (nextWrapper.____slothletInternal.mode === "lazy" && !nextWrapper.____slothletInternal.state.materialized) { | ||
| await nextWrapper._materialize(); |
| if (nextWrapper.____slothletInternal.mode === "lazy" && !nextWrapper.____slothletInternal.state.materialized) { | ||
| await nextWrapper._materialize(); | ||
| } |
…able impl as unmaterialized A lazy wrapper whose real content is itself a function (e.g. a self-named single-file folder) had its state.materialized flag wrongly reset to false right after legitimately materializing, because the check used `typeof impl !== "function"` as a materialized signal. A later access would then re-run the materializer, double-firing _onWrapperMaterialized() and corrupting the global unmaterialized-lazy-wrapper count. Only "impl is still null/undefined" means unmaterialized; a callable impl is exactly as materialized as any other content type. Surfaced by PR #364 review; new regression fixture api_tests/api_test_collisions/lazyfuncbase/sub/sub.mjs and test.
|
Suppressed finding on Fixed in |
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved materialization bookkeeping, nested-child handling, and async propagation issues block approval.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (3)
src/lib/handlers/api-manager.mjs:679
- This only materializes the wrappers for the current syncWrapper call, not their shared child wrappers. The recursive merge branches later call Object.keys(resolveWrapper(nextChild)) without awaiting a lazy child's _materialize(), so a root collision with a nested lazy folder can see that child as empty, skip recursion, and lose the incoming grandchildren (or skip the merge-replace composition). Materialize each shared child before deciding whether it has grandchildren.
if (existingWrapper.____slothletInternal.mode === "lazy" && !existingWrapper.____slothletInternal.state.materialized) {
await existingWrapper._materialize();
}
if (nextWrapper.____slothletInternal.mode === "lazy" && !nextWrapper.____slothletInternal.state.materialized) {
await nextWrapper._materialize();
src/lib/handlers/api-manager.mjs:676
- The new awaits make syncWrapper yield before it has applied the merge, but one caller path does not await it: assignToApiPath invokes syncWrapper directly, and mergeApiObjects invokes assignToApiPath directly. A caller that awaits mergeApiObjects can therefore observe an incomplete nested collision while this materialization/merge is still running. Propagate the promise through that synchronous assignment path or await the sync operation before returning.
if (existingWrapper.____slothletInternal.mode === "lazy" && !existingWrapper.____slothletInternal.state.materialized) {
await existingWrapper._materialize();
src/lib/handlers/api-manager.mjs:679
state.materialized === falsedoes not guarantee that this wrapper is currently counted as unmaterialized:___resetLazy()sets the flag back to false without re-registering the wrapper, while_materialize()always calls_onWrapperMaterialized()and decrements the global count. After reload resets a previously materialized wrapper, a collision here can decrementmaterialize.get().remainingagain (and potentially make it negative), corrupting materialization statistics and completion tracking. Make reset/re-materialization accounting idempotent before using this forced-load path.
if (existingWrapper.____slothletInternal.mode === "lazy" && !existingWrapper.____slothletInternal.state.materialized) {
await existingWrapper._materialize();
}
if (nextWrapper.____slothletInternal.mode === "lazy" && !nextWrapper.____slothletInternal.state.materialized) {
await nextWrapper._materialize();
- Files reviewed: 4/5 changed files
- Comments generated: 1
- Review effort level: Lite
| if (existingWrapper.____slothletInternal.mode === "lazy" && !existingWrapper.____slothletInternal.state.materialized) { | ||
| await existingWrapper._materialize(); | ||
| } | ||
| if (nextWrapper.____slothletInternal.mode === "lazy" && !nextWrapper.____slothletInternal.state.materialized) { | ||
| await nextWrapper._materialize(); |
|
Closing without merging — every commit here is already an ancestor of #366, which will carry this fix (and its own review findings) into |
…ollisionModeOverride into assignToApiPath Two more findings from PR #366's review, against commit 9183ee4: - syncWrapper's final bookkeeping still inferred existingWrapper's materialized state from its impl's content/type. The earlier #364-review fix corrected this for a callable impl but the same class of bug applied to any other falsy-by-check impl (e.g. a legitimate null/undefined export) — the underlying flaw was re-deriving "materialized" from content at all. Replaced with an explicit flag set only in the one branch that genuinely needs existingWrapper marked unmaterialized (impl explicitly cleared to null, pending a future re-materialization); every other path now keeps whatever _materialize()/___setImpl() already correctly set. New regression test + api_tests/api_test_collisions/lazynullbase fixture (a self-named single-file lazy folder whose export is null). - assignToApiPath()'s collision-detection branch derived its mode purely from config.collision[collisionContext], ignoring any per-call override (api.add()'s forceOverwrite). modes-processor.mjs's lazy-subdirectory file/folder-collision call site already resolved the correct override-aware mode (modes_initialCollisionMode) for its OWN ownership registration and the lazy wrapper's later materialization, but never passed it to this assignToApiPath call — so the actual composed value for an internal collision inside a forceOverwrite-mounted directory could disagree with the mount's own top-level decision. Added a collisionModeOverride option to assignToApiPath, threaded through. Regression: 116 files / 4221 tests green across both rounds.
🚀 What's Changed
💥 Breaking Changes
No breaking changes
✨ Features
No new features
🐛 Bug Fixes
📦 Dependencies
No dependency updates
🔧 Other Changes
👥 Contributors