Realms in TypeScript
realm.ts is the preferred realm definition format. YAML realms remain supported as legacy,
without synthesis from TypeScript. realm.ts exports one default object,
assembled from as many definition files as the author needs. The host reads and evaluates it
directly. Nothing converts it to YAML, manifests or other generated declaration files, and
a realm repository commits no generated declaration files.
This document specifies the observable declaration contract. The composition guide teaches how to assemble it. A realm’s version is its own compatibility label, not a host contract version or a module digest. This specification, not an SDK’s acceptance rules, decides what a declaration means. Source and dependency terms follow the glossary.
Most examples below use execution: "captured". A captured realm runs from a verified copy
the owner has admitted, with separately approved capabilities. A conventional realm omits
execution or sets it to "conventional"; its capabilities follow the conventional sections
of the main specification. triggers, queries, apps, credentials,
dataPipes, capturedLenses, watches, graphql, views, skills and capabilities use
the captured profile here. goals, producers, apis and channels have different forms
in the two profiles. A host must refuse a declaration in the wrong profile, naming the field.
Source selection and legacy compatibility
Section titled “Source selection and legacy compatibility”| Files present | Host behavior |
|---|---|
realm.ts only |
Evaluate it and validate its resulting declaration. |
realm.ts plus realm.yml or legacy declaration folders |
Use only the evaluated TypeScript declaration. Report the ignored declaration paths, whether they agree or conflict. Do not merge them or fill absent TypeScript fields from them. |
Invalid or unsupported realm.ts plus valid YAML |
Refuse the TypeScript declaration with its diagnostic. Never fall back to YAML. |
No realm.ts |
Read the legacy realm.yml and its folders, including committed dist/ and dependencies/manifest.json, subject to capability-specific migrations such as the retired source producer kind. No TypeScript migration is required. |
The precedence rule includes adjacent handler and dependency manifests and stale generated
declarations. It does not discard resources explicitly referenced by realm.ts, such as SQL,
handler source, an icon or a vendored API document. YAML is legacy and receives no new features.
Legacy decorator and YAML type declarations retain their existing duplicate-type checks;
neither contributes additional types when realm.ts is present.
Definition evaluation
Section titled “Definition evaluation”realm.ts and its transitive imports are definition code evaluated by the host, separate
from guest handler code. Only the evaluated default object’s value counts. Object spread
therefore has ordinary TypeScript semantics: a later property replaces an earlier property
with the same key, and the host cannot recover the overwritten declaration.
Definition imports may reference captured, realm-relative TypeScript or JavaScript modules
and exact # subpath mappings in the realm’s package.json imports object. These mappings
must resolve to relative files inside the realm. A tsconfig.json path alias alone does not
declare a runtime mapping. No import may escape the captured realm, including through a
symlink. Type-only imports are erased and confer no runtime capability.
The host provides @embabel/realm-types version 0.1.0 for definition imports without a
package.json or a declared package dependency. The bundled version guarantee is exactly
0.1.0; it does not imply support for other versions. Authors may import the package root
or an existing source file as @embabel/realm-types/<file>.ts, for example
@embabel/realm-types/handlers.ts or @embabel/realm-types/types.ts. Subpaths must stay
inside that package; an absent source file is refused. An explicit package.json
dependencies entry for @embabel/realm-types takes precedence for the root and its
subpaths. Its version comes from package-lock.json, and it passes the same package lock
and integrity checks as other declared packages; failure never falls back to the bundled
helper. Editor checks use the matching SDK version.
A host may offer other documented, versioned authoring helpers and locked package imports. It must refuse unsupported bare package imports, host built-ins, remote imports and unresolved imports. Package declarations do not grant arbitrary package installation or network access during definition evaluation. An authoring helper or imported package has the same evaluation restrictions as local definition code.
Evaluation must be deterministic and free of external side effects. It may construct values with functions, loops and object spread. It may not read files except by the permitted import resolver, write files, read environment variables, use the clock or randomness, open a network connection, start a process or access host capabilities. Evaluation failure, an unsupported import, a prohibited operation or a resource limit refuses the definition before admission, with the offending file or operation and the reason. A host must enforce these restrictions before effects occur; detecting an escape after executing it is not confinement.
The result is finite JSON-like data: strings, booleans, null, finite numbers, arrays and plain records. Functions may build that data but cannot remain in it. Functions, symbols, dates, big integers, maps, sets, cycles and undefined array entries in the result are refused. Undefined object members are absent. The host validates the result regardless of SDK checks. Schema helper values must be converted by a permitted pure authoring helper to the host’s supported JSON Schema subset before the host accepts the definition.
In the captured profile, the host must refuse control characters, including newlines, and unpaired surrogates in the text fields of goals, triggers, producers, lenses, watches, data pipes, view references, apps, credentials and API entries. This validation applies regardless of SDK checks.
A host that cannot safely evaluate a definition must refuse it. Definition evaluation uses only permitted imports and explicitly referenced resources. Adjacent legacy declarations cannot supply missing fields or override the evaluated object. Arbitrary npm validators and unrestricted package resolution are outside this contract.
Handler imports
Section titled “Handler imports”Handler source the host builds from entry is guest code, and its imports follow these rules,
separate from definition evaluation:
- A relative import of named values, such as
import { format } from './lib/format.ts', is accepted when it resolves to a regular file inside the handler folder: the folder holding the entry,wasm/by default. - Refused before the handler is bound, naming the importing file and the specifier: an import
that resolves outside the handler folder (
../types/index.ts, an absolute path, or a symbolic link that leaves it), a#subpath mapping, a bare package specifier, a remote URL and a host built-in. An editor or atsconfig.jsonalias resolving one does not make it acceptable. - Other relative import forms (a default, namespace or side-effect-only import, or a dynamic
import()) are not portable; a host may refuse them, naming the file. - Type-only imports are erased. They confer nothing and do not make a value import acceptable.
- A handler reaches a dependency module only through
ctx.deps, never by importing it.
Legacy src/ projects ship their built artifacts and keep their existing loading; see
single file, or a typed project.
SDK assistance (non-normative)
Section titled “SDK assistance (non-normative)”The TypeScript SDK checks a definition as you write it. satisfies Realm checks its fields;
defineRealm can also check handler references. It accepts Standard Schema validators for
handler schemas and can derive input and output types. Those are SDK conveniences, not a
different host contract. The host re-checks everything regardless, including the resulting
JSON Schema and every handler input and output. SDK lint may be stricter than portable
metadata rules, but it cannot change the host’s interpretation.

