Skip to content

getOwnershipCollisionMode()'s config fallback is fed "addApi" as collisionContext, which never matches config.collision's {initial, api} keys #367

Closed

Description

@Shinrai

Found during #365's investigation (PR #366).

The bug — TWO separate occurrences of the same mismatch

Config#normalizeCollision (src/lib/helpers/config.mjs) only ever produces a { initial, api } object. Two independent call sites read a config-driven collision-mode fallback keyed by collisionContext, and both are fed a third value, "addApi", which matches neither key:

1. src/lib/helpers/modes-utils.mjs's getOwnershipCollisionMode:

getOwnershipCollisionMode(config, collisionContext = "initial") {
	return config.collision?.[collisionContext] || "merge";
}

2. src/lib/builders/api-assignment.mjs:231 — a separate, direct inline read with the identical pattern:

const collisionMode = config.collision?.[collisionContext] || "merge";

Both silently resolve to the hardcoded "merge" whenever collisionContext === "addApi", regardless of the instance's actually-configured collision.api default.

Where "addApi" comes from: src/lib/handlers/api-manager.mjs's addApiComponent (the runtime api.slothlet.api.add() path) calls buildAPI({ collisionContext: "addApi", ... }); src/lib/handlers/api-cache-manager.mjs's reload-rebuild call does the same (collisionContext: entry.endpoint === "." ? "initial" : "addApi"). This one value threads down through buildAPI → eager.mjs/lazy.mjs → processFiles(), reaching both call sites above.

Root-caused (2026-09-10): which one actually matters, and why the "obvious" fix looked wrong

getOwnershipCollisionMode (occurrence 1) only affects ownership bookkeeping for a leaf — as of #366, modes-processor.mjs's per-leaf ownership.register() calls now prefer a threaded-through per-call override first, so this fallback rarely matters in practice.

api-assignment.mjs:231 (occurrence 2) is the one that actually decides the real file-vs-folder merge outcome for a same-name file+folder collision within a single module directory (e.g. a fixture with math.mjs at the top level colliding with a math/ subfolder, both exporting add) — not just ownership tracking.

This is exactly what made the "obvious" fix ("addApi" → "api") look broken during #366: renaming only api-manager.mjs's string caused tests/vitests/suites/core/core-reload-selective.test.vitest.mjs to fail (40/64 tests) — api.testComp.math.add(2,3) returned 5 instead of the expected 1005. That test's fixture (TEST_DIRS.API_TEST) has a deliberate, already-documented file-vs-folder collision (api_tests/api_test/math.mjs, whose own header comment says the root file wins "under default slothlet collision resolution"), and the test configures collision: { api: "replace" }.

Under genuine replace mode, the last-loaded source wins a file/folder collision — this is not a guess, it's already directly tested and passing elsewhere: tests/vitests/suites/config/collision-config.test.vitest.mjs's "replace mode: should replace with last loaded (folder or file)". The folder is scanned after the root file, so under real replace semantics the folder (plain a+b) correctly wins, not the file (a+b+1000).

So core-reload-selective.test.vitest.mjs's hardcoded 1005/1002/1020/1006 expectations were only ever correct by accident — the "addApi" bug silently suppressed replace mode from ever reaching this decision point for that test's api.add() calls, making it behave like merge (file wins) the entire time, even though the test explicitly configured replace.

Fix

  1. Rename "addApi" → "api" at both source sites (api-manager.mjs, api-cache-manager.mjs).
  2. Update core-reload-selective.test.vitest.mjs's hardcoded expectations to the now-genuinely-correct replace-mode values (folder wins → plain sum, no +1000 offset) — the test's actual purpose (reload mechanics: cache-busting, ownership-stack updates, custom-property persistence) is unaffected; only the incidental file/folder collision outcome baked into its shared fixture changes.
  3. Full suite must stay green after both changes land together.

Activity

  1. added theissue type on Sep 10, 2026
  2. added
    type: bug
    priority: medium
    area: core
    on Sep 10, 2026
  3. Shinrai commented on Sep 11, 2026

    @Shinrai
    ContributorAuthor

    Fixed in cf2e793 on PR #366.

    Root-caused via api-assignment.mjs:231's identical fallback (the one that actually decides file-vs-folder merge outcomes, not just ownership bookkeeping) — see that commit's message for the full trace. Renamed "addApi" → "api" at both source sites; also had to extend modes-processor.mjs's #resolveOwnershipCollisionMode to clamp the fallback result (not just the per-call override) to replace/merge-replace, since the now-correct fallback could otherwise resolve to "error"/"skip"/"warn" and reintroduce a leaf-level mid-build throw.

    Two existing tests (core-reload-selective.test.vitest.mjs, core-reload-path-multicache.test.vitest.mjs) had hardcoded expectations that were only correct because this bug silently suppressed replace mode — updated to the now-genuinely-correct values. Full suite green: 382 files, 13,530 tests.

  4. added
    status: verified
    on Sep 13, 2026
  5. cldmv-bot commented on Sep 14, 2026

    @cldmv-bot
    Contributor

    Closed in v3.16.0 — resolved by #366.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corepriority: mediumstatus: verifiedtype: bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions