Skip to content

Add more setup for RealmResourceIdentifier migration#4425

Open
backspace wants to merge 7 commits intomainfrom
@cardstack/base-no-registerCardReferencePrefix-cs-10745
Open

Add more setup for RealmResourceIdentifier migration#4425
backspace wants to merge 7 commits intomainfrom
@cardstack/base-no-registerCardReferencePrefix-cs-10745

Conversation

@backspace
Copy link
Copy Markdown
Contributor

@backspace backspace commented Apr 16, 2026

This is another step toward @cardstack/base, more setup for RealmIdentifier and RealmResourceIdentifier:

  • VirtualNetwork#addRealmMapping to encompass addURLMapping, addImportMap, and registerCardReferencePrefix
    • this is called in host/app/services/network
    • and the realm server main.ts and worker.ts startup handlers
  • RRI support for VirtualNetwork#fetch
  • VirtualNetwork.knownRealms as a means to access the registered realms

The only changes to existing code are the subbullets, the next phase will be more significant. There’s a new test that shows that @cardstack/base/card-api resolves properly, but no code will actually be trying to do that yet.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 16, 2026

Realm Server Test Results

  1 files  ± 0    1 suites  ±0   13m 47s ⏱️ -34s
904 tests +10  904 ✅ +10  0 💤 ±0  0 ❌ ±0 
976 runs  +10  976 ✅ +10  0 💤 ±0  0 ❌ ±0 

Results for commit f03b021. ± Comparison against base commit eeb1d45.

♻️ This comment has been updated with latest results.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 16, 2026

Host Test Results

2 254 tests  +5   2 239 ✅ +5   2h 26m 25s ⏱️ - 1m 38s
    1 suites ±0      15 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit f03b021. ± Comparison against base commit eeb1d45.

♻️ This comment has been updated with latest results.

@backspace backspace force-pushed the @cardstack/base-no-registerCardReferencePrefix-cs-10745 branch from 592630d to 7df6350 Compare April 16, 2026 16:21
backspace and others added 4 commits April 16, 2026 14:54
VirtualNetwork.fetch now accepts scoped RRI strings (e.g.
"@cardstack/base/card-api") in addition to URLs and Requests. RRIs
are resolved to real URLs via realm mappings before creating the
Request, since new Request('@cardstack/base/...') would throw.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Returns the list of registered RealmIdentifiers from realm mappings.
Non-network code can use this to check realm membership without
needing to resolve RRIs to URLs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace registerCardReferencePrefix + addImportMap pairs with single
addRealmMapping calls for catalog, skills, and openrouter realms.
Add addRealmMapping for @cardstack/base/ scoped prefix (new).
Keep addURLMapping for the fake https://cardstack.com/base/ URL
mapping which is still needed during the transition.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace registerCardReferencePrefix + addImportMap pairs with single
addRealmMapping calls in main.ts and worker.ts for non-URL prefix
mappings (e.g. @cardstack/catalog/). URL-based mappings still use
addURLMapping.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@backspace backspace changed the title Add addRealmMapping to call registerCardReferencePrefix Add more setup for RealmResourceIdentifier migration Apr 16, 2026
@github-actions
Copy link
Copy Markdown

Preview deployments

backspace and others added 2 commits April 16, 2026 16:01
…chain

Verifies that addRealmMapping + VirtualNetwork.fetch resolves
@cardstack/base/card-api to http://localhost:4201/base/card-api
through the complete middleware chain.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@backspace backspace marked this pull request as ready for review April 16, 2026 21:52
@backspace backspace requested a review from a team April 16, 2026 22:34
@habdelra habdelra requested a review from Copilot April 16, 2026 22:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds additional infrastructure for migrating from URL-based realm addressing toward RealmIdentifier / RealmResourceIdentifier (RRI) support, centered around VirtualNetwork being able to register scoped realm prefixes and use them during fetch.

Changes:

  • Added VirtualNetwork.addRealmMapping() and VirtualNetwork.knownRealms() to register and introspect scoped realm prefix mappings (bridging to existing import-map and card-reference prefix registration).
  • Added support for VirtualNetwork.fetch() to accept scoped RRI strings (e.g. @cardstack/base/card-api) by resolving them to real URLs before constructing a Request.
  • Updated host + realm-server startup wiring to use addRealmMapping() instead of manually calling registerCardReferencePrefix() and addImportMap(), and added tests for the new behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/runtime-common/virtual-network.ts Introduces realm-prefix mapping registry, exposes known realms, and resolves RRI strings in fetch()
packages/realm-server/main.ts Switches non-URL realm prefix wiring to virtualNetwork.addRealmMapping()
packages/realm-server/worker.ts Switches non-URL realm prefix wiring to virtualNetwork.addRealmMapping()
packages/host/app/services/network.ts Registers @cardstack/* scoped realm prefixes via addRealmMapping() while keeping base URL mapping
packages/realm-server/tests/card-reference-resolver-test.ts Adds coverage for addRealmMapping(), knownRealms(), and RRI support in VirtualNetwork.fetch()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +74 to +86
addRealmMapping(realmIdentifier: string, targetURL: string): void {
let normalizedId = ensureTrailingSlash(realmIdentifier);
let normalizedTarget = ensureTrailingSlash(targetURL);
this.realmMappings.set(normalizedId, normalizedTarget);

// Backward compat bridge: populate both existing registration systems
// so that resolveImport and resolveCardReference continue to work
this.addImportMap(
normalizedId,
(rest) => new URL(rest, normalizedTarget).href,
);
registerCardReferencePrefix(normalizedId, normalizedTarget);
}
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addRealmMapping currently only registers the scoped prefix into importMap and the global prefixMappings, but it doesn't interact with urlMappings (so it doesn't affect mapURL()/handle() remapping). The name makes it easy to assume it replaces addURLMapping for URL-based realm mapping too; consider either documenting this limitation explicitly on the method or extending it to handle URL-like realmIdentifier inputs by delegating to addURLMapping where appropriate.

Copilot uses AI. Check for mistakes.
Comment on lines +666 to +669
assert.true(
realms.includes('@test/realm/' as any),
'contains the registered realm',
);
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions use as any to satisfy the RealmIdentifier[] type, which disables type checking in the test. Since RealmIdentifier is already imported in this file, prefer casting the string literal to RealmIdentifier (or otherwise comparing as string) so the test stays type-safe.

Copilot uses AI. Check for mistakes.
Comment on lines +675 to +678
assert.strictEqual(realms.length, 2);
assert.true(realms.includes('@test/realm/' as any));
assert.true(realms.includes('@test/other/' as any));
unregisterCardReferencePrefix('@test/other/');
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue here: using as any defeats the purpose of the branded RealmIdentifier type and can hide mistakes. Prefer as RealmIdentifier (already imported) or cast the array to string[] before calling includes.

Copilot uses AI. Check for mistakes.
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