Skip to content

Reuse existing external keys for as-key references - #74

Merged
dschwartznyc merged 1 commit into
mainfrom
fix/as-key-external-key
Nov 12, 2026
Merged

dschwartznyc merged 1 commit into
mainfrom
fix/as-key-external-key

Conversation

@plamen-neykov

@plamen-neykov plamen-neykov commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

When a generated function applies as-key to an object with {"@key:external": "p1"}, Reference(target) currently selects an internal key, creating one if needed. The output contains {"@ref": "<uuid>"} and an externally keyed input unnecessarily gains an internal key.

Prefer the existing external key so the same assignment writes {"@ref:external": "p1"} without adding a key. This applies to scalar and list references, including targets reached through COW proxies.

Closes #73.

Key selection

  • With no explicit key or key type, prefer an existing external key, including when an internal key also exists.
  • Otherwise reuse the existing internal key or allocate one if missing.
  • Preserve explicit key_type=KeyType.INTERNAL selection.
  • Keep scoped-key selection explicit; automatic selection would need the receiving scope to be known.

Resolution of the keyless case

We do not adopt the no-key-creation behavior suggested in #73. Every object has an identity, and multiple keys can identify that same object. Allocating an additional key records another representation of its existing identity; it does not create a new object identity or change its ordinary data. Since as-key explicitly requests the target's identity, allocating an internal key when no external or internal key exists is intentional behavior.

The target's key metadata and registry may therefore gain an internal key. This is identity bookkeeping, rather than an ordinary data write requiring a COW copy. The key remains associated with the existing target. Java's different key-allocation behavior does not establish a defect in this Python contract.

These principles and the ownership rule are documented in Object identity and references. The document distinguishes implemented key selection from the agreed access rule requiring follow-up enforcement.

The defect in #73 is the unnecessary allocation or selection of an internal key when a usable external key already exists. This change fixes that defect, and both supplied reproduction tests pass. The keyless case is resolved by retaining automatic internal-key allocation under the identity principles above.

Protection against ordinary data writes through references into the caller's graph is separate follow-up work. The agreed rule for that work is that these references grant read access and identity access, while data writes require ownership.

Validation

  • Added 14 regression cases, including both generated-function reproduction tests supplied in as-key ignores the object's existing key and changes the function's input #73 as permanent tests in test/test_keys_and_references.py. Coverage includes external-only and dual-key targets, internal-key reuse, explicit internal selection, scalar/list serialization and COW wrappers.
  • Targeted tracked run: 156 passed, including both reproduction tests supplied in as-key ignores the object's existing key and changes the function's input #73.
  • Full tracked suite: 557 passed, 19 skipped, 1 failed. The sole failure is the pre-existing Windows module-resolution problem in test/test_cached_json_rune_object_deserializer.py::test_init_invalid_directory_or_module, also reproduced before the change.
  • git diff --check passed.

Prefer a target's existing external key when Reference receives neither an
explicit key nor a key type. Generated scalar and list as-key assignments
then serialize @ref:external without adding an unnecessary internal key.

Retain internal-key reuse and allocation and the explicit INTERNAL override.
Add regression coverage for plain targets, COW proxies and reference lists.
Include both generated-function reproduction tests from the issue in the
permanent test suite.
Document the identity, key-selection and ownership principles, distinguishing
current behavior from follow-up enforcement.

Internal-key allocation is intentional identity bookkeeping: an additional
key identifies the existing target without changing its ordinary data.

Fixes #73
@plamen-neykov plamen-neykov self-assigned this Nov 11, 2026
@plamen-neykov plamen-neykov added the bug Something isn't working label Nov 11, 2026
@github-actions

Copy link
Copy Markdown

Test Results

577 tests  +14   557 ✅ +14   2s ⏱️ ±0s
  1 suites ± 0    20 💤 ± 0 
  1 files   ± 0     0 ❌ ± 0 

Results for commit 7663811. ± Comparison against base commit ba623bb.

@dschwartznyc
dschwartznyc merged commit 272e179 into main Nov 12, 2026
6 checks passed
@plamen-neykov
plamen-neykov deleted the fix/as-key-external-key branch November 12, 2026 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

as-key ignores the object's existing key and changes the function's input

2 participants