Skip to content

Fix cache navigation through collapsing/re-mount renames - #681

Closed
Butanium wants to merge 2 commits into
mainfrom
claude/verify-issue-535-7sra91
Closed

Fix cache navigation through collapsing/re-mount renames#681
Butanium wants to merge 2 commits into
mainfrom
claude/verify-issue-535-7sra91

Conversation

@Butanium

@Butanium Butanium commented Jul 8, 2026

Copy link
Copy Markdown
Member

Summary

Fixes cache attribute/index navigation when using collapsing or re-mount renames (e.g., rename={"model.layers": "layers"}). These renames re-mount nested modules at the root, creating alias paths that don't exist in real storage keys. The cache navigator now resolves these paths correctly through an alias-path map.

Key Changes

  • Added _resolved_path() method: Translates aliased paths (from collapsing renames) back to their real storage keys using the _alias_paths map.

  • Updated property accessors (.output, .inputs, .input): Now use _resolved_path() to look up cache entries, enabling access through renamed paths.

  • Added _alias_path_prefix() helper: Checks if a path is an alias path or a prefix of one, allowing segment-by-segment navigation through the alias-path keyspace.

  • Refactored _add_alias_path(): Protects the root cache component from substring replacements. A rename like {"model": "foo"} now correctly produces model.foo.layers.0 (not foo.foo.layers.0), since cache.model is the fixed root.

  • Enhanced __getitem__() (integer indexing): Now checks both real keys and alias-path prefixes when validating numeric indices, raising IndexError instead of leaking KeyError for out-of-bounds access on renamed module lists.

  • Refactored __getattr__() (attribute access): Implements a three-route resolution strategy:

    1. Direct route: path prefixes a real storage key
    2. Leaf-alias route: translate aliased segment and continue in real-path space
    3. Alias-path route: path prefixes an authoritative alias path (handles collapsing renames)
  • Added _child() helper: Centralizes sub-view creation to reduce duplication.

Tests

Added two new test cases:

  • test_cache_collapsing_rename: Validates attribute/index access through collapsing renames, including .output, .input, and out-of-bounds indexing.
  • test_cache_rename_preserves_root: Ensures renames matching the root component name don't corrupt the fixed cache.model root.

Implementation Details

The core issue: when a rename like {"model.layers": "layers"} is applied, the user-facing access path cache.model.layers[0] doesn't match any real storage key (which is model.transformer.h.0). The fix maintains an _alias_paths map that records these mappings and uses it during navigation to resolve paths correctly. The three-route resolution in __getattr__() ensures both simple renames and complex collapsing renames work seamlessly.

https://claude.ai/code/session_01CGecSNRudbfxSX14gsBco5

claude added 2 commits July 3, 2026 08:05
Attribute-style access into a cache (e.g. `cache.model.layers[0].output`)
raised `AttributeError` when a rename re-mounts a nested module at the root
via a dotted rename key (e.g. `{"model.layers": "layers"}`). Two root causes:

- `CacheDict.__getattr__`/int-indexing only matched navigation paths against
  the real storage keys and the lossy inverted leaf-alias map, never the
  authoritative `_alias_paths` (alias-path -> real-key) map. A collapsing
  rename's alias path is not a prefix of any real key, so navigation dead-ended.
- `_add_alias_path` built alias paths with naive substring replacement, which
  also rewrote the fixed root component (`model.model.layers.0` ->
  `foo.foo.layers.0` instead of `model.foo.layers.0`).

Fixes:
- Navigate the `_alias_paths` keyspace as a fallback route in `__getattr__`
  and int-indexing; resolve a landed `_path` back to its real storage key in
  `output`/`inputs`/`input`.
- Protect the root component in `_add_alias_path`.
- Copy underlying storage into sub-views based on raw dict length, so an
  alias-space sub-view (scoped length 0) still propagates storage to children.

Adds regression tests for both the collapsing-rename and root-preservation
cases from the issue.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CGecSNRudbfxSX14gsBco5
…ame cache tests

Follow-up to the #535 fix. Out-of-bounds indexing on a renamed/collapsed
modulelist (e.g. `cache.model.layers[999]`) leaked a `KeyError` instead of the
`IndexError` contract honored by non-renamed access, because the bounds check
only consulted the real storage keys. Extend it to the alias-path keyspace.

Broaden the collapsing-rename regression test to also cover `.input` via a
renamed path, out-of-bounds `IndexError`, and navigating into a submodule of a
re-mounted block under a full (no-`modules=`) cache.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CGecSNRudbfxSX14gsBco5
@Butanium

Butanium commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

#682 for fable less sloppy version

@Butanium Butanium closed this Jul 8, 2026
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.

2 participants