@almadar/runtime 5.8.2 → 5.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,9 @@
1
1
  import { resolveBinding, evaluate, createMinimalContext, evaluateGuard } from '@almadar/evaluator';
2
2
  export { createMinimalContext } from '@almadar/evaluator';
3
3
  import { isKnownOperator } from '@almadar/std';
4
+ import * as nodeModule from 'module';
5
+ import { isInlineTrait, isEntityCall, OrbitalSchemaSchema, isEntityReference, parseEntityRef, parseImportedTraitRef, isPageReference, isPageReferenceString, isPageReferenceObject, parsePageRef } from '@almadar/core';
4
6
  import { faker } from '@faker-js/faker';
5
- import { OrbitalSchemaSchema, isEntityCall, isEntityReference, parseEntityRef, parseImportedTraitRef, isPageReference, isPageReferenceString, isPageReferenceObject, parsePageRef } from '@almadar/core';
6
7
 
7
8
  // src/EventBus.ts
8
9
  var EventBus = class {
@@ -175,6 +176,8 @@ function createLogger(namespace) {
175
176
  error: (msg, data) => log("ERROR", msg, data)
176
177
  };
177
178
  }
179
+
180
+ // src/BindingResolver.ts
178
181
  var bindLog = createLogger("almadar:runtime:bindings");
179
182
  var renderLog = createLogger("almadar:runtime:render-ui");
180
183
  var CLIENT_ONLY_BINDING_ROOTS = /* @__PURE__ */ new Set(["trait"]);
@@ -1984,509 +1987,822 @@ var MockPersistenceAdapter = class {
1984
1987
  function createMockPersistence(config) {
1985
1988
  return new MockPersistenceAdapter(config);
1986
1989
  }
1987
-
1988
- // src/loader/schema-loader.ts
1989
- function isElectron() {
1990
- return typeof process !== "undefined" && !!process.versions?.electron;
1991
- }
1992
- function isBrowser() {
1993
- return typeof window !== "undefined" && !isElectron();
1994
- }
1995
- function isNode() {
1996
- return typeof process !== "undefined" && !isBrowser();
1997
- }
1998
- var HttpImportChain = class _HttpImportChain {
1999
- chain = [];
2000
- /**
2001
- * Try to add a path to the chain.
2002
- * @returns Error message if circular, null if OK
2003
- */
2004
- push(absolutePath) {
2005
- if (this.chain.includes(absolutePath)) {
2006
- const cycle = [
2007
- ...this.chain.slice(this.chain.indexOf(absolutePath)),
2008
- absolutePath
2009
- ];
2010
- return `Circular import detected: ${cycle.join(" -> ")}`;
2011
- }
2012
- this.chain.push(absolutePath);
2013
- return null;
1990
+ var refResolverLog = createLogger("almadar:runtime:ref-resolver");
1991
+ function renameEventsInRenderUiConfig(node, rename) {
1992
+ if (node === null || node === void 0) return node;
1993
+ if (Array.isArray(node)) {
1994
+ return node.map((item) => renameEventsInRenderUiConfig(item, rename));
2014
1995
  }
2015
- /**
2016
- * Remove the last path from the chain.
2017
- */
2018
- pop() {
2019
- this.chain.pop();
1996
+ if (typeof node !== "object") return node;
1997
+ const obj = node;
1998
+ const next = { ...obj };
1999
+ for (const [key, value] of Object.entries(obj)) {
2000
+ if (key === "action" && typeof value === "string" && !value.startsWith("@")) {
2001
+ next[key] = rename(value) ?? value;
2002
+ continue;
2003
+ }
2004
+ if (/^on[A-Z]/.test(key) && typeof value === "string" && !value.startsWith("@")) {
2005
+ next[key] = rename(value) ?? value;
2006
+ continue;
2007
+ }
2008
+ if (key.endsWith("Event") && typeof value === "string" && !value.startsWith("@")) {
2009
+ next[key] = rename(value) ?? value;
2010
+ continue;
2011
+ }
2012
+ if ((key === "actions" || key === "itemActions") && Array.isArray(value)) {
2013
+ const rewrittenArray = value.map((entry) => {
2014
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) return entry;
2015
+ const action = entry;
2016
+ if (typeof action.event === "string" && !action.event.startsWith("@")) {
2017
+ return { ...action, event: rename(action.event) ?? action.event };
2018
+ }
2019
+ return action;
2020
+ });
2021
+ next[key] = rewrittenArray;
2022
+ continue;
2023
+ }
2024
+ next[key] = renameEventsInRenderUiConfig(value, rename);
2020
2025
  }
2021
- /**
2022
- * Clone the chain for nested loading.
2023
- */
2024
- clone() {
2025
- const newChain = new _HttpImportChain();
2026
- newChain.chain = [...this.chain];
2027
- return newChain;
2026
+ return next;
2027
+ }
2028
+ function renameEventsInEffects(effects, rename) {
2029
+ return effects.map((effect) => {
2030
+ if (!Array.isArray(effect)) return effect;
2031
+ if (effect[0] === "render-ui" && effect.length >= 3) {
2032
+ const slot = effect[1];
2033
+ const config = effect[2];
2034
+ const nextConfig = renameEventsInRenderUiConfig(config, rename);
2035
+ return [effect[0], slot, nextConfig, ...effect.slice(3)];
2036
+ }
2037
+ return effect;
2038
+ });
2039
+ }
2040
+ function renameEntityInRenderUiConfig(node, oldName, newName) {
2041
+ if (node === null || node === void 0) return node;
2042
+ if (Array.isArray(node)) {
2043
+ return node.map((item) => renameEntityInRenderUiConfig(item, oldName, newName));
2028
2044
  }
2029
- };
2030
- var HttpLoaderCache = class {
2031
- cache = /* @__PURE__ */ new Map();
2032
- get(url) {
2033
- return this.cache.get(url);
2045
+ if (typeof node !== "object") return node;
2046
+ const obj = node;
2047
+ const next = { ...obj };
2048
+ for (const [key, value] of Object.entries(obj)) {
2049
+ if (key === "entity" && value === oldName) {
2050
+ next[key] = newName;
2051
+ continue;
2052
+ }
2053
+ next[key] = renameEntityInRenderUiConfig(value, oldName, newName);
2034
2054
  }
2035
- set(url, schema) {
2036
- this.cache.set(url, schema);
2055
+ return next;
2056
+ }
2057
+ function renameEntityInEffects(effects, oldName, newName) {
2058
+ return effects.map((effect) => renameEntityInEffect(effect, oldName, newName));
2059
+ }
2060
+ var ENTITY_AT_POS_1 = /* @__PURE__ */ new Set(["fetch", "ref", "deref", "spawn"]);
2061
+ var ALL_ARGS_ARE_EFFECTS = /* @__PURE__ */ new Set([
2062
+ "do",
2063
+ "atomic",
2064
+ "async/race",
2065
+ "async/all",
2066
+ "async/sequence"
2067
+ ]);
2068
+ var ARGS_FROM_POS_2_ARE_EFFECTS = /* @__PURE__ */ new Set([
2069
+ "if",
2070
+ "when",
2071
+ "let",
2072
+ "async/delay",
2073
+ "async/debounce",
2074
+ "async/throttle",
2075
+ "async/interval"
2076
+ ]);
2077
+ function renameEntityInEffect(effect, oldName, newName) {
2078
+ if (!Array.isArray(effect) || effect.length === 0) return effect;
2079
+ const op = effect[0];
2080
+ if (typeof op !== "string") return effect;
2081
+ if (op === "render-ui" && effect.length >= 3) {
2082
+ const [, slot, config, ...rest] = effect;
2083
+ const nextConfig = renameEntityInRenderUiConfig(config, oldName, newName);
2084
+ return [op, slot, nextConfig, ...rest];
2037
2085
  }
2038
- has(url) {
2039
- return this.cache.has(url);
2086
+ if (op === "persist" && effect.length >= 3 && effect[2] === oldName) {
2087
+ return [op, effect[1], newName, ...effect.slice(3)];
2040
2088
  }
2041
- clear() {
2042
- this.cache.clear();
2089
+ if (ENTITY_AT_POS_1.has(op) && effect[1] === oldName) {
2090
+ return [op, newName, ...effect.slice(2)];
2043
2091
  }
2044
- get size() {
2045
- return this.cache.size;
2092
+ const skipFirstNonEffectArg = ARGS_FROM_POS_2_ARE_EFFECTS.has(op);
2093
+ const recurseAll = ALL_ARGS_ARE_EFFECTS.has(op);
2094
+ if (recurseAll || skipFirstNonEffectArg) {
2095
+ const startIndex = skipFirstNonEffectArg ? 2 : 1;
2096
+ return effect.map((arg, i) => {
2097
+ if (i < startIndex) return arg;
2098
+ if (Array.isArray(arg)) {
2099
+ return renameEntityInEffect(arg, oldName, newName);
2100
+ }
2101
+ return arg;
2102
+ });
2046
2103
  }
2047
- };
2048
- var HttpLoader = class {
2104
+ return effect;
2105
+ }
2106
+ function applyLinkedEntityRename(trait, linkedEntity) {
2107
+ const atomLinked = trait.linkedEntity;
2108
+ if (!linkedEntity || !atomLinked || linkedEntity === atomLinked) return trait;
2109
+ const sm = trait.stateMachine;
2110
+ if (!sm) return { ...trait, linkedEntity };
2111
+ const nextTransitions = (sm.transitions ?? []).map((t) => {
2112
+ const nextEffects = t.effects ? renameEntityInEffects(
2113
+ t.effects,
2114
+ atomLinked,
2115
+ linkedEntity
2116
+ ) : t.effects;
2117
+ return { ...t, effects: nextEffects };
2118
+ });
2119
+ refResolverLog.info("linkedEntity:rename", {
2120
+ trait: trait.name,
2121
+ from: atomLinked,
2122
+ to: linkedEntity,
2123
+ transitionCount: nextTransitions.length
2124
+ });
2125
+ return {
2126
+ ...trait,
2127
+ linkedEntity,
2128
+ stateMachine: { ...sm, transitions: nextTransitions }
2129
+ };
2130
+ }
2131
+ function applyEventRenames(trait, renames) {
2132
+ if (!renames || Object.keys(renames).length === 0) return trait;
2133
+ const rename = (k) => k !== void 0 && k in renames ? renames[k] : k;
2134
+ const sm = trait.stateMachine;
2135
+ if (!sm) return trait;
2136
+ const nextTransitions = (sm.transitions ?? []).map((t) => {
2137
+ const nextEvent = rename(t.event) ?? t.event;
2138
+ const nextEffects = t.effects ? renameEventsInEffects(t.effects, rename) : t.effects;
2139
+ return { ...t, event: nextEvent, effects: nextEffects };
2140
+ });
2141
+ const nextEvents = (sm.events ?? []).map((e) => {
2142
+ const newKey = rename(e.key);
2143
+ if (newKey === e.key) return e;
2144
+ return { ...e, key: newKey ?? e.key };
2145
+ });
2146
+ const nextEmits = (trait.emits ?? []).map((em) => {
2147
+ if (typeof em === "string") return rename(em) ?? em;
2148
+ const newEvent = rename(em.event);
2149
+ return newEvent === em.event ? em : { ...em, event: newEvent ?? em.event };
2150
+ });
2151
+ return {
2152
+ ...trait,
2153
+ stateMachine: {
2154
+ ...sm,
2155
+ transitions: nextTransitions,
2156
+ events: nextEvents
2157
+ },
2158
+ emits: nextEmits
2159
+ };
2160
+ }
2161
+ var ReferenceResolver = class {
2162
+ loader;
2049
2163
  options;
2050
- cache;
2164
+ localTraits;
2165
+ loaderInitialized = false;
2051
2166
  constructor(options) {
2052
- this.options = {
2053
- basePath: options.basePath,
2054
- stdLibPath: options.stdLibPath ?? "",
2055
- scopedPaths: options.scopedPaths ?? {},
2056
- fetchOptions: options.fetchOptions,
2057
- timeout: options.timeout ?? 3e4,
2058
- credentials: options.credentials ?? "same-origin"
2059
- };
2060
- this.cache = new HttpLoaderCache();
2167
+ this.options = options;
2168
+ this.loader = options.loader;
2169
+ this.localTraits = options.localTraits ?? /* @__PURE__ */ new Map();
2170
+ }
2171
+ async ensureLoader() {
2172
+ if (this.loader || this.loaderInitialized) return;
2173
+ this.loaderInitialized = true;
2174
+ try {
2175
+ const { ExternalOrbitalLoader } = await import('./external-loader-UHZQPCKW.js');
2176
+ this.loader = new ExternalOrbitalLoader(this.options);
2177
+ } catch {
2178
+ }
2061
2179
  }
2062
2180
  /**
2063
- * Load a schema from an import path.
2181
+ * Resolve all references in an orbital.
2064
2182
  */
2065
- async load(importPath, fromPath, chain) {
2066
- const importChain = chain ?? new HttpImportChain();
2067
- const resolveResult = this.resolvePath(importPath, fromPath);
2068
- if (!resolveResult.success) {
2069
- return resolveResult;
2070
- }
2071
- const absoluteUrl = resolveResult.data;
2072
- const circularError = importChain.push(absoluteUrl);
2073
- if (circularError) {
2074
- return { success: false, error: circularError };
2183
+ async resolve(orbital, sourcePath, chain) {
2184
+ const errors = [];
2185
+ const warnings = [];
2186
+ const importChain = chain ?? { push: () => null, pop: () => {
2187
+ }, clone() {
2188
+ return this;
2189
+ } };
2190
+ const importsResult = await this.resolveImports(
2191
+ orbital.uses ?? [],
2192
+ sourcePath,
2193
+ importChain
2194
+ );
2195
+ if (!importsResult.success) {
2196
+ return { success: false, errors: importsResult.errors };
2075
2197
  }
2076
- try {
2077
- const cached = this.cache.get(absoluteUrl);
2078
- if (cached) {
2079
- return { success: true, data: cached };
2080
- }
2081
- const loadResult = await this.fetchSchema(absoluteUrl);
2082
- if (!loadResult.success) {
2083
- return loadResult;
2084
- }
2085
- const loaded = {
2086
- schema: loadResult.data,
2087
- sourcePath: absoluteUrl,
2088
- importPath
2089
- };
2090
- this.cache.set(absoluteUrl, loaded);
2091
- return { success: true, data: loaded };
2092
- } finally {
2093
- importChain.pop();
2198
+ const imports = importsResult.data;
2199
+ const entityResult = this.resolveEntity(orbital.entity, imports);
2200
+ if (!entityResult.success) {
2201
+ errors.push(...entityResult.errors);
2094
2202
  }
2095
- }
2096
- /**
2097
- * Load a specific orbital from a schema by name.
2098
- */
2099
- async loadOrbital(importPath, orbitalName, fromPath, chain) {
2100
- const schemaResult = await this.load(importPath, fromPath, chain);
2101
- if (!schemaResult.success) {
2102
- return schemaResult;
2203
+ const traitsResult = this.resolveTraits(orbital.traits, imports);
2204
+ if (!traitsResult.success) {
2205
+ errors.push(...traitsResult.errors);
2103
2206
  }
2104
- const schema = schemaResult.data.schema;
2105
- let orbital;
2106
- if (orbitalName) {
2107
- const found = schema.orbitals.find(
2108
- (o) => o.name === orbitalName
2109
- );
2110
- if (!found) {
2111
- return {
2112
- success: false,
2113
- error: `Orbital "${orbitalName}" not found in ${importPath}. Available: ${schema.orbitals.map((o) => o.name).join(", ")}`
2114
- };
2115
- }
2116
- orbital = found;
2117
- } else {
2118
- if (schema.orbitals.length === 0) {
2119
- return {
2120
- success: false,
2121
- error: `No orbitals found in ${importPath}`
2122
- };
2123
- }
2124
- orbital = schema.orbitals[0];
2207
+ const pagesResult = this.resolvePages(orbital.pages, imports);
2208
+ if (!pagesResult.success) {
2209
+ errors.push(...pagesResult.errors);
2210
+ }
2211
+ if (errors.length > 0) {
2212
+ return { success: false, errors };
2213
+ }
2214
+ if (!entityResult.success || !traitsResult.success || !pagesResult.success) {
2215
+ return { success: false, errors: ["Internal error: unexpected failure state"] };
2125
2216
  }
2126
2217
  return {
2127
2218
  success: true,
2128
2219
  data: {
2129
- orbital,
2130
- sourcePath: schemaResult.data.sourcePath,
2131
- importPath
2132
- }
2220
+ name: orbital.name,
2221
+ entity: entityResult.data.entity,
2222
+ entitySource: entityResult.data.source,
2223
+ traits: traitsResult.data,
2224
+ pages: pagesResult.data,
2225
+ imports,
2226
+ original: orbital
2227
+ },
2228
+ warnings
2133
2229
  };
2134
2230
  }
2135
2231
  /**
2136
- * Resolve an import path to an absolute URL.
2232
+ * Resolve `uses` declarations to loaded orbitals.
2137
2233
  */
2138
- resolvePath(importPath, fromPath) {
2139
- if (importPath.startsWith("http://") || importPath.startsWith("https://")) {
2140
- return { success: true, data: importPath };
2141
- }
2142
- if (importPath.startsWith("std/")) {
2143
- return this.resolveStdPath(importPath);
2234
+ async resolveImports(uses, sourcePath, chain) {
2235
+ const errors = [];
2236
+ const orbitals = /* @__PURE__ */ new Map();
2237
+ if (this.options.skipExternalLoading) {
2238
+ return {
2239
+ success: true,
2240
+ data: { orbitals },
2241
+ warnings: ["External loading skipped"]
2242
+ };
2144
2243
  }
2145
- if (importPath.startsWith("@")) {
2146
- return this.resolveScopedPath(importPath);
2244
+ for (const use of uses) {
2245
+ if (orbitals.has(use.as)) {
2246
+ errors.push(`Duplicate import alias: ${use.as}`);
2247
+ continue;
2248
+ }
2249
+ await this.ensureLoader();
2250
+ if (!this.loader) {
2251
+ errors.push(`No loader available to resolve import: ${use.from}`);
2252
+ continue;
2253
+ }
2254
+ const loadResult = await this.loader.loadOrbital(
2255
+ use.from,
2256
+ void 0,
2257
+ sourcePath,
2258
+ chain
2259
+ );
2260
+ if (!loadResult.success) {
2261
+ errors.push(`Failed to load "${use.from}" as "${use.as}": ${loadResult.error}`);
2262
+ continue;
2263
+ }
2264
+ orbitals.set(use.as, {
2265
+ alias: use.as,
2266
+ from: use.from,
2267
+ orbital: loadResult.data.orbital,
2268
+ sourcePath: loadResult.data.sourcePath
2269
+ });
2147
2270
  }
2148
- if (importPath.startsWith("./") || importPath.startsWith("../")) {
2149
- return this.resolveRelativePath(importPath, fromPath);
2271
+ if (errors.length > 0) {
2272
+ return { success: false, errors };
2150
2273
  }
2151
- return this.resolveRelativePath(`./${importPath}`, fromPath);
2274
+ return { success: true, data: { orbitals }, warnings: [] };
2152
2275
  }
2153
2276
  /**
2154
- * Resolve a standard library path.
2277
+ * Resolve entity reference.
2155
2278
  */
2156
- resolveStdPath(importPath) {
2157
- if (!this.options.stdLibPath) {
2279
+ resolveEntity(entityRef, imports) {
2280
+ if (isEntityCall(entityRef)) {
2281
+ const fallbackName = entityRef.name ?? entityRef.extends.replace(/\.entity$/, "");
2158
2282
  return {
2159
- success: false,
2160
- error: `Standard library URL not configured. Cannot load: ${importPath}`
2283
+ success: true,
2284
+ data: {
2285
+ entity: {
2286
+ name: fallbackName,
2287
+ fields: entityRef.fields ?? [],
2288
+ ...entityRef.persistence ? { persistence: entityRef.persistence } : {},
2289
+ ...entityRef.collection ? { collection: entityRef.collection } : {}
2290
+ }
2291
+ },
2292
+ warnings: []
2161
2293
  };
2162
2294
  }
2163
- const relativePath = importPath.slice(4);
2164
- let absoluteUrl = this.joinUrl(this.options.stdLibPath, relativePath);
2165
- if (!absoluteUrl.endsWith(".orb")) {
2166
- absoluteUrl += ".orb";
2295
+ if (!isEntityReference(entityRef)) {
2296
+ return {
2297
+ success: true,
2298
+ data: { entity: entityRef },
2299
+ warnings: []
2300
+ };
2167
2301
  }
2168
- return { success: true, data: absoluteUrl };
2169
- }
2170
- /**
2171
- * Resolve a scoped package path.
2172
- */
2173
- resolveScopedPath(importPath) {
2174
- const match = importPath.match(/^(@[^/]+)/);
2175
- if (!match) {
2302
+ const parsed = parseEntityRef(entityRef);
2303
+ if (!parsed) {
2176
2304
  return {
2177
2305
  success: false,
2178
- error: `Invalid scoped package path: ${importPath}`
2306
+ errors: [`Invalid entity reference format: ${entityRef}. Expected "Alias.entity"`]
2179
2307
  };
2180
2308
  }
2181
- const scope = match[1];
2182
- const scopeRoot = this.options.scopedPaths[scope];
2183
- if (!scopeRoot) {
2309
+ const imported = imports.orbitals.get(parsed.alias);
2310
+ if (!imported) {
2184
2311
  return {
2185
2312
  success: false,
2186
- error: `Scoped package "${scope}" not configured. Available: ${Object.keys(this.options.scopedPaths).join(", ") || "none"}`
2313
+ errors: [
2314
+ `Unknown import alias in entity reference: ${parsed.alias}. Available aliases: ${Array.from(imports.orbitals.keys()).join(", ") || "none"}`
2315
+ ]
2187
2316
  };
2188
2317
  }
2189
- const relativePath = importPath.slice(scope.length + 1);
2190
- let absoluteUrl = this.joinUrl(scopeRoot, relativePath);
2191
- if (!absoluteUrl.endsWith(".orb")) {
2192
- absoluteUrl += ".orb";
2318
+ const importedEntity = this.getEntityFromOrbital(imported.orbital);
2319
+ if (!importedEntity) {
2320
+ return {
2321
+ success: false,
2322
+ errors: [
2323
+ `Imported orbital "${parsed.alias}" does not have an inline entity. Entity references cannot be chained.`
2324
+ ]
2325
+ };
2193
2326
  }
2194
- return { success: true, data: absoluteUrl };
2327
+ const persistence = importedEntity.persistence ?? "persistent";
2328
+ return {
2329
+ success: true,
2330
+ data: {
2331
+ entity: importedEntity,
2332
+ source: {
2333
+ alias: parsed.alias,
2334
+ persistence
2335
+ }
2336
+ },
2337
+ warnings: []
2338
+ };
2195
2339
  }
2196
2340
  /**
2197
- * Resolve a relative path.
2341
+ * Get the entity from an orbital (handling EntityRef).
2198
2342
  */
2199
- resolveRelativePath(importPath, fromPath) {
2200
- const baseUrl = fromPath ? this.getParentUrl(fromPath) : this.options.basePath;
2201
- let absoluteUrl = this.joinUrl(baseUrl, importPath);
2202
- if (!absoluteUrl.endsWith(".orb")) {
2203
- absoluteUrl += ".orb";
2343
+ getEntityFromOrbital(orbital) {
2344
+ const entityRef = orbital.entity;
2345
+ if (typeof entityRef === "string") {
2346
+ return null;
2204
2347
  }
2205
- return { success: true, data: absoluteUrl };
2348
+ if (isEntityCall(entityRef)) {
2349
+ const fallbackName = entityRef.name ?? entityRef.extends.replace(/\.entity$/, "");
2350
+ return {
2351
+ name: fallbackName,
2352
+ fields: entityRef.fields ?? [],
2353
+ ...entityRef.persistence ? { persistence: entityRef.persistence } : {},
2354
+ ...entityRef.collection ? { collection: entityRef.collection } : {}
2355
+ };
2356
+ }
2357
+ return entityRef;
2206
2358
  }
2207
2359
  /**
2208
- * Fetch and parse an OrbitalSchema from a URL.
2360
+ * Resolve trait references.
2209
2361
  */
2210
- async fetchSchema(url) {
2211
- try {
2212
- const controller = new AbortController();
2213
- const timeoutId = setTimeout(
2214
- () => controller.abort(),
2215
- this.options.timeout
2216
- );
2217
- try {
2218
- const response = await fetch(url, {
2219
- ...this.options.fetchOptions,
2220
- credentials: this.options.credentials,
2221
- signal: controller.signal,
2222
- headers: {
2223
- Accept: "application/json",
2224
- ...this.options.fetchOptions?.headers
2225
- }
2226
- });
2227
- if (!response.ok) {
2228
- return {
2229
- success: false,
2230
- error: `HTTP ${response.status}: ${response.statusText} for ${url}`
2231
- };
2232
- }
2233
- const text = await response.text();
2234
- let data;
2235
- try {
2236
- data = JSON.parse(text);
2237
- } catch (e) {
2238
- return {
2239
- success: false,
2240
- error: `Invalid JSON in ${url}: ${e instanceof Error ? e.message : String(e)}`
2241
- };
2242
- }
2243
- const parseResult = OrbitalSchemaSchema.safeParse(data);
2244
- if (!parseResult.success) {
2245
- const errors = parseResult.error.errors.map((e) => ` - ${e.path.join(".")}: ${e.message}`).join("\n");
2246
- return {
2247
- success: false,
2248
- error: `Invalid schema in ${url}:
2249
- ${errors}`
2250
- };
2251
- }
2252
- return { success: true, data: parseResult.data };
2253
- } finally {
2254
- clearTimeout(timeoutId);
2362
+ resolveTraits(traitRefs, imports) {
2363
+ const errors = [];
2364
+ const resolved = [];
2365
+ for (const traitRef of traitRefs) {
2366
+ const result = this.resolveTraitRef(traitRef, imports);
2367
+ if (!result.success) {
2368
+ errors.push(...result.errors);
2369
+ } else {
2370
+ resolved.push(result.data);
2255
2371
  }
2256
- } catch (e) {
2257
- if (e instanceof Error && e.name === "AbortError") {
2372
+ }
2373
+ if (errors.length > 0) {
2374
+ return { success: false, errors };
2375
+ }
2376
+ return { success: true, data: resolved, warnings: [] };
2377
+ }
2378
+ /**
2379
+ * Resolve a single trait reference.
2380
+ */
2381
+ resolveTraitRef(traitRef, imports) {
2382
+ if (typeof traitRef !== "string" && "stateMachine" in traitRef) {
2383
+ return {
2384
+ success: true,
2385
+ data: {
2386
+ trait: traitRef,
2387
+ source: { type: "inline" }
2388
+ },
2389
+ warnings: []
2390
+ };
2391
+ }
2392
+ if (typeof traitRef !== "string" && "ref" in traitRef) {
2393
+ const refObj = traitRef;
2394
+ return this.resolveTraitRefString(
2395
+ refObj.ref,
2396
+ imports,
2397
+ refObj.config,
2398
+ refObj.linkedEntity,
2399
+ refObj.name,
2400
+ refObj.events,
2401
+ refObj.listens
2402
+ );
2403
+ }
2404
+ if (typeof traitRef === "string") {
2405
+ return this.resolveTraitRefString(traitRef, imports);
2406
+ }
2407
+ return {
2408
+ success: false,
2409
+ errors: [`Unknown trait reference format: ${JSON.stringify(traitRef)}`]
2410
+ };
2411
+ }
2412
+ /**
2413
+ * Resolve a trait reference string.
2414
+ */
2415
+ resolveTraitRefString(ref, imports, config, linkedEntity, overrideName, eventRenames, listensOverride) {
2416
+ const parsed = parseImportedTraitRef(ref);
2417
+ if (parsed) {
2418
+ const imported = imports.orbitals.get(parsed.alias);
2419
+ if (!imported) {
2258
2420
  return {
2259
2421
  success: false,
2260
- error: `Request timeout for ${url} (${this.options.timeout}ms)`
2422
+ errors: [
2423
+ `Unknown import alias in trait reference: ${parsed.alias}. Available aliases: ${Array.from(imports.orbitals.keys()).join(", ") || "none"}`
2424
+ ]
2425
+ };
2426
+ }
2427
+ const trait = this.findTraitInOrbital(imported.orbital, parsed.traitName);
2428
+ if (!trait) {
2429
+ return {
2430
+ success: false,
2431
+ errors: [
2432
+ `Trait "${parsed.traitName}" not found in imported orbital "${parsed.alias}". Available traits: ${this.listTraitsInOrbital(imported.orbital).join(", ") || "none"}`
2433
+ ]
2261
2434
  };
2262
2435
  }
2436
+ const baseTrait = overrideName ? { ...trait, name: overrideName } : trait;
2437
+ const reboundTrait = applyLinkedEntityRename(baseTrait, linkedEntity);
2438
+ const renamedTrait = applyEventRenames(reboundTrait, eventRenames);
2439
+ const finalTrait = listensOverride !== void 0 ? { ...renamedTrait, listens: listensOverride } : renamedTrait;
2440
+ if (listensOverride !== void 0) {
2441
+ refResolverLog.info("listens-override:imported", {
2442
+ trait: finalTrait.name,
2443
+ ref,
2444
+ atomListens: trait.listens?.length ?? 0,
2445
+ callSiteListens: listensOverride.length
2446
+ });
2447
+ }
2263
2448
  return {
2264
- success: false,
2265
- error: `Failed to fetch ${url}: ${e instanceof Error ? e.message : String(e)}`
2449
+ success: true,
2450
+ data: {
2451
+ trait: finalTrait,
2452
+ source: { type: "imported", alias: parsed.alias, traitName: parsed.traitName },
2453
+ config,
2454
+ linkedEntity
2455
+ },
2456
+ warnings: []
2457
+ };
2458
+ }
2459
+ const localTrait = this.localTraits.get(ref);
2460
+ if (localTrait) {
2461
+ const baseLocal = overrideName ? { ...localTrait, name: overrideName } : localTrait;
2462
+ const reboundLocal = applyLinkedEntityRename(baseLocal, linkedEntity);
2463
+ const renamedLocalTrait = applyEventRenames(reboundLocal, eventRenames);
2464
+ const finalLocalTrait = listensOverride !== void 0 ? { ...renamedLocalTrait, listens: listensOverride } : renamedLocalTrait;
2465
+ if (listensOverride !== void 0) {
2466
+ refResolverLog.info("listens-override:local", {
2467
+ trait: finalLocalTrait.name,
2468
+ ref,
2469
+ atomListens: localTrait.listens?.length ?? 0,
2470
+ callSiteListens: listensOverride.length
2471
+ });
2472
+ }
2473
+ return {
2474
+ success: true,
2475
+ data: {
2476
+ trait: finalLocalTrait,
2477
+ source: { type: "local", name: ref },
2478
+ config,
2479
+ linkedEntity
2480
+ },
2481
+ warnings: []
2266
2482
  };
2267
2483
  }
2484
+ return {
2485
+ success: false,
2486
+ errors: [
2487
+ `Trait "${ref}" not found. For imported traits, use format "Alias.traits.TraitName". Local traits available: ${Array.from(this.localTraits.keys()).join(", ") || "none"}`
2488
+ ]
2489
+ };
2268
2490
  }
2269
2491
  /**
2270
- * Join URL parts, handling relative paths and trailing slashes.
2492
+ * Find a trait in an orbital by name.
2271
2493
  */
2272
- joinUrl(base, path) {
2273
- if (path.startsWith("http://") || path.startsWith("https://")) {
2274
- return path;
2275
- }
2276
- if (typeof URL !== "undefined") {
2277
- try {
2278
- const baseUrl = base.endsWith("/") ? base : base + "/";
2279
- return new URL(path, baseUrl).href;
2280
- } catch {
2494
+ findTraitInOrbital(orbital, traitName) {
2495
+ for (const traitRef of orbital.traits) {
2496
+ if (typeof traitRef !== "string" && "stateMachine" in traitRef) {
2497
+ if (traitRef.name === traitName) {
2498
+ return traitRef;
2499
+ }
2500
+ }
2501
+ if (typeof traitRef !== "string" && "ref" in traitRef) {
2502
+ const refObj = traitRef;
2503
+ if (refObj.ref === traitName || refObj.name === traitName) ;
2281
2504
  }
2282
2505
  }
2283
- const normalizedBase = base.endsWith("/") ? base.slice(0, -1) : base;
2284
- const normalizedPath = path.startsWith("/") ? path : "/" + path;
2285
- if (normalizedPath.startsWith("/./")) {
2286
- return normalizedBase + normalizedPath.slice(2);
2287
- }
2288
- if (normalizedPath.startsWith("/../")) {
2289
- const baseParts = normalizedBase.split("/");
2290
- baseParts.pop();
2291
- return baseParts.join("/") + normalizedPath.slice(3);
2506
+ return null;
2507
+ }
2508
+ /**
2509
+ * List trait names in an orbital.
2510
+ */
2511
+ listTraitsInOrbital(orbital) {
2512
+ const names = [];
2513
+ for (const traitRef of orbital.traits) {
2514
+ if (typeof traitRef !== "string" && "stateMachine" in traitRef) {
2515
+ names.push(traitRef.name);
2516
+ }
2292
2517
  }
2293
- return normalizedBase + normalizedPath;
2518
+ return names;
2294
2519
  }
2295
2520
  /**
2296
- * Get parent URL (directory) from a URL.
2521
+ * Resolve page references.
2297
2522
  */
2298
- getParentUrl(url) {
2299
- if (typeof URL !== "undefined") {
2300
- try {
2301
- const urlObj = new URL(url);
2302
- const pathParts = urlObj.pathname.split("/");
2303
- pathParts.pop();
2304
- urlObj.pathname = pathParts.join("/");
2305
- return urlObj.href;
2306
- } catch {
2523
+ resolvePages(pageRefs, imports) {
2524
+ const errors = [];
2525
+ const resolved = [];
2526
+ for (const pageRef of pageRefs) {
2527
+ const result = this.resolvePageRef(pageRef, imports);
2528
+ if (!result.success) {
2529
+ errors.push(...result.errors);
2530
+ } else {
2531
+ resolved.push(result.data);
2307
2532
  }
2308
2533
  }
2309
- const lastSlash = url.lastIndexOf("/");
2310
- if (lastSlash !== -1) {
2311
- return url.slice(0, lastSlash);
2534
+ if (errors.length > 0) {
2535
+ return { success: false, errors };
2312
2536
  }
2313
- return url;
2537
+ return { success: true, data: resolved, warnings: [] };
2314
2538
  }
2315
2539
  /**
2316
- * Clear the cache.
2540
+ * Resolve a single page reference.
2317
2541
  */
2318
- clearCache() {
2319
- this.cache.clear();
2542
+ resolvePageRef(pageRef, imports) {
2543
+ if (!isPageReference(pageRef)) {
2544
+ return {
2545
+ success: true,
2546
+ data: {
2547
+ page: pageRef,
2548
+ source: { type: "inline" },
2549
+ pathOverridden: false
2550
+ },
2551
+ warnings: []
2552
+ };
2553
+ }
2554
+ if (isPageReferenceString(pageRef)) {
2555
+ return this.resolvePageRefString(pageRef, imports);
2556
+ }
2557
+ if (isPageReferenceObject(pageRef)) {
2558
+ return this.resolvePageRefObject(pageRef, imports);
2559
+ }
2560
+ return {
2561
+ success: false,
2562
+ errors: [`Unknown page reference format: ${JSON.stringify(pageRef)}`]
2563
+ };
2320
2564
  }
2321
2565
  /**
2322
- * Get cache statistics.
2566
+ * Resolve a page reference string.
2323
2567
  */
2324
- getCacheStats() {
2325
- return { size: this.cache.size };
2326
- }
2327
- };
2328
-
2329
- // src/loader/unified-loader.ts
2330
- var externalLoaderModule = null;
2331
- async function getExternalLoaderModule() {
2332
- if (externalLoaderModule) {
2333
- return externalLoaderModule;
2568
+ resolvePageRefString(ref, imports) {
2569
+ const parsed = parsePageRef(ref);
2570
+ if (!parsed) {
2571
+ return {
2572
+ success: false,
2573
+ errors: [`Invalid page reference format: ${ref}. Expected "Alias.pages.PageName"`]
2574
+ };
2575
+ }
2576
+ const imported = imports.orbitals.get(parsed.alias);
2577
+ if (!imported) {
2578
+ return {
2579
+ success: false,
2580
+ errors: [
2581
+ `Unknown import alias in page reference: ${parsed.alias}. Available aliases: ${Array.from(imports.orbitals.keys()).join(", ") || "none"}`
2582
+ ]
2583
+ };
2584
+ }
2585
+ const page = this.findPageInOrbital(imported.orbital, parsed.pageName);
2586
+ if (!page) {
2587
+ return {
2588
+ success: false,
2589
+ errors: [
2590
+ `Page "${parsed.pageName}" not found in imported orbital "${parsed.alias}". Available pages: ${this.listPagesInOrbital(imported.orbital).join(", ") || "none"}`
2591
+ ]
2592
+ };
2593
+ }
2594
+ return {
2595
+ success: true,
2596
+ data: {
2597
+ page,
2598
+ source: { type: "imported", alias: parsed.alias, pageName: parsed.pageName },
2599
+ pathOverridden: false
2600
+ },
2601
+ warnings: []
2602
+ };
2334
2603
  }
2335
- if (isBrowser()) {
2336
- return null;
2604
+ /**
2605
+ * Resolve a page reference object with optional path override.
2606
+ */
2607
+ resolvePageRefObject(refObj, imports) {
2608
+ const baseResult = this.resolvePageRefString(refObj.ref, imports);
2609
+ if (!baseResult.success) {
2610
+ return baseResult;
2611
+ }
2612
+ const resolved = baseResult.data;
2613
+ if (refObj.path) {
2614
+ const originalPath = resolved.page.path;
2615
+ resolved.page = {
2616
+ ...resolved.page,
2617
+ path: refObj.path
2618
+ };
2619
+ resolved.pathOverridden = true;
2620
+ resolved.originalPath = originalPath;
2621
+ }
2622
+ return {
2623
+ success: true,
2624
+ data: resolved,
2625
+ warnings: baseResult.warnings
2626
+ };
2337
2627
  }
2338
- try {
2339
- externalLoaderModule = await import('./external-loader-UHZQPCKW.js');
2340
- return externalLoaderModule;
2341
- } catch {
2628
+ /**
2629
+ * Find a page in an orbital by name.
2630
+ */
2631
+ findPageInOrbital(orbital, pageName) {
2632
+ const pages = orbital.pages;
2633
+ if (!pages) return null;
2634
+ for (const pageRef of pages) {
2635
+ if (typeof pageRef !== "string" && !("ref" in pageRef)) {
2636
+ const page = pageRef;
2637
+ if (page.name === pageName) {
2638
+ return { ...page };
2639
+ }
2640
+ }
2641
+ }
2342
2642
  return null;
2343
2643
  }
2644
+ /**
2645
+ * List page names in an orbital.
2646
+ */
2647
+ listPagesInOrbital(orbital) {
2648
+ const pages = orbital.pages;
2649
+ if (!pages) return [];
2650
+ const names = [];
2651
+ for (const pageRef of pages) {
2652
+ if (typeof pageRef !== "string" && !("ref" in pageRef)) {
2653
+ names.push(pageRef.name);
2654
+ }
2655
+ }
2656
+ return names;
2657
+ }
2658
+ /**
2659
+ * Add local traits for resolution.
2660
+ */
2661
+ addLocalTraits(traits) {
2662
+ for (const trait of traits) {
2663
+ this.localTraits.set(trait.name, trait);
2664
+ }
2665
+ }
2666
+ /**
2667
+ * Clear loader cache.
2668
+ */
2669
+ clearCache() {
2670
+ this.loader?.clearCache();
2671
+ }
2672
+ };
2673
+ async function resolveSchema(schema, options) {
2674
+ const resolver = new ReferenceResolver(options);
2675
+ const errors = [];
2676
+ const warnings = [];
2677
+ const resolved = [];
2678
+ for (const orbital of schema.orbitals) {
2679
+ const inlineTraits = orbital.traits.filter(
2680
+ (t) => typeof t !== "string" && "stateMachine" in t
2681
+ );
2682
+ resolver.addLocalTraits(inlineTraits);
2683
+ }
2684
+ for (const orbital of schema.orbitals) {
2685
+ const result = await resolver.resolve(orbital);
2686
+ if (!result.success) {
2687
+ errors.push(`Orbital "${orbital.name}": ${result.errors.join(", ")}`);
2688
+ } else {
2689
+ resolved.push(result.data);
2690
+ warnings.push(...result.warnings.map((w) => `Orbital "${orbital.name}": ${w}`));
2691
+ }
2692
+ }
2693
+ if (errors.length > 0) {
2694
+ return { success: false, errors };
2695
+ }
2696
+ return { success: true, data: resolved, warnings };
2344
2697
  }
2345
- var UnifiedImportChain = class _UnifiedImportChain {
2698
+
2699
+ // src/loader/schema-loader.ts
2700
+ function isElectron() {
2701
+ return typeof process !== "undefined" && !!process.versions?.electron;
2702
+ }
2703
+ function isBrowser() {
2704
+ return typeof window !== "undefined" && !isElectron();
2705
+ }
2706
+ function isNode() {
2707
+ return typeof process !== "undefined" && !isBrowser();
2708
+ }
2709
+ var HttpImportChain = class _HttpImportChain {
2346
2710
  chain = [];
2347
- push(path) {
2348
- const normalized = this.normalizePath(path);
2349
- if (this.chain.includes(normalized)) {
2711
+ /**
2712
+ * Try to add a path to the chain.
2713
+ * @returns Error message if circular, null if OK
2714
+ */
2715
+ push(absolutePath) {
2716
+ if (this.chain.includes(absolutePath)) {
2350
2717
  const cycle = [
2351
- ...this.chain.slice(this.chain.indexOf(normalized)),
2352
- normalized
2718
+ ...this.chain.slice(this.chain.indexOf(absolutePath)),
2719
+ absolutePath
2353
2720
  ];
2354
2721
  return `Circular import detected: ${cycle.join(" -> ")}`;
2355
2722
  }
2356
- this.chain.push(normalized);
2723
+ this.chain.push(absolutePath);
2357
2724
  return null;
2358
2725
  }
2726
+ /**
2727
+ * Remove the last path from the chain.
2728
+ */
2359
2729
  pop() {
2360
2730
  this.chain.pop();
2361
2731
  }
2732
+ /**
2733
+ * Clone the chain for nested loading.
2734
+ */
2362
2735
  clone() {
2363
- const newChain = new _UnifiedImportChain();
2736
+ const newChain = new _HttpImportChain();
2364
2737
  newChain.chain = [...this.chain];
2365
2738
  return newChain;
2366
2739
  }
2367
- normalizePath(path) {
2368
- if (path.startsWith("http://") || path.startsWith("https://")) {
2369
- return path;
2370
- }
2371
- return path.replace(/\\/g, "/");
2740
+ };
2741
+ var HttpLoaderCache = class {
2742
+ cache = /* @__PURE__ */ new Map();
2743
+ get(url) {
2744
+ return this.cache.get(url);
2745
+ }
2746
+ set(url, schema) {
2747
+ this.cache.set(url, schema);
2748
+ }
2749
+ has(url) {
2750
+ return this.cache.has(url);
2751
+ }
2752
+ clear() {
2753
+ this.cache.clear();
2754
+ }
2755
+ get size() {
2756
+ return this.cache.size;
2372
2757
  }
2373
2758
  };
2374
- var UnifiedLoader = class {
2759
+ var HttpLoader = class {
2375
2760
  options;
2376
- httpLoader = null;
2377
- fsLoader = null;
2378
- fsLoaderInitialized = false;
2379
- cache = /* @__PURE__ */ new Map();
2761
+ cache;
2380
2762
  constructor(options) {
2381
- this.options = options;
2382
- this.httpLoader = new HttpLoader({
2763
+ this.options = {
2383
2764
  basePath: options.basePath,
2384
- stdLibPath: options.stdLibPath,
2385
- scopedPaths: options.scopedPaths,
2386
- ...options.http
2387
- });
2388
- }
2389
- /**
2390
- * Initialize the filesystem loader if available.
2391
- */
2392
- async initFsLoader() {
2393
- if (this.fsLoaderInitialized) {
2394
- return;
2395
- }
2396
- this.fsLoaderInitialized = true;
2397
- if (this.options.forceLoader === "http") {
2398
- return;
2399
- }
2400
- const module = await getExternalLoaderModule();
2401
- if (module) {
2402
- this.fsLoader = new module.ExternalOrbitalLoader({
2403
- basePath: this.options.basePath,
2404
- stdLibPath: this.options.stdLibPath,
2405
- scopedPaths: this.options.scopedPaths,
2406
- ...this.options.fileSystem
2407
- });
2408
- }
2765
+ stdLibPath: options.stdLibPath ?? "",
2766
+ scopedPaths: options.scopedPaths ?? {},
2767
+ fetchOptions: options.fetchOptions,
2768
+ timeout: options.timeout ?? 3e4,
2769
+ credentials: options.credentials ?? "same-origin"
2770
+ };
2771
+ this.cache = new HttpLoaderCache();
2409
2772
  }
2410
2773
  /**
2411
- * Determine which loader to use for an import path.
2774
+ * Load a schema from an import path.
2412
2775
  */
2413
- getLoaderForPath(importPath) {
2414
- if (this.options.forceLoader) {
2415
- return this.options.forceLoader;
2776
+ async load(importPath, fromPath, chain) {
2777
+ const importChain = chain ?? new HttpImportChain();
2778
+ const resolveResult = this.resolvePath(importPath, fromPath);
2779
+ if (!resolveResult.success) {
2780
+ return resolveResult;
2416
2781
  }
2417
- if (importPath.startsWith("http://") || importPath.startsWith("https://")) {
2418
- return "http";
2782
+ const absoluteUrl = resolveResult.data;
2783
+ const circularError = importChain.push(absoluteUrl);
2784
+ if (circularError) {
2785
+ return { success: false, error: circularError };
2419
2786
  }
2420
- if (importPath.startsWith("std/") && this.options.stdLibPath) {
2421
- if (this.options.stdLibPath.startsWith("http://") || this.options.stdLibPath.startsWith("https://")) {
2422
- return "http";
2423
- }
2424
- }
2425
- if (importPath.startsWith("@") && this.options.scopedPaths) {
2426
- const match = importPath.match(/^(@[^/]+)/);
2427
- if (match) {
2428
- const scopePath = this.options.scopedPaths[match[1]];
2429
- if (scopePath && (scopePath.startsWith("http://") || scopePath.startsWith("https://"))) {
2430
- return "http";
2431
- }
2432
- }
2433
- }
2434
- if (isBrowser()) {
2435
- return "http";
2436
- }
2437
- return "filesystem";
2438
- }
2439
- /**
2440
- * Load a schema from an import path.
2441
- *
2442
- * Note: We delegate chain management to the inner loader (HttpLoader or FsLoader).
2443
- * The inner loader handles circular import detection, so we don't push/pop here.
2444
- * We only use the unified cache to avoid duplicate loads across loaders.
2445
- */
2446
- async load(importPath, fromPath, chain) {
2447
- await this.initFsLoader();
2448
- const importChain = chain ?? new UnifiedImportChain();
2449
- const loaderType = this.getLoaderForPath(importPath);
2450
- const resolveResult = this.resolvePath(importPath, fromPath);
2451
- if (!resolveResult.success) {
2452
- return resolveResult;
2453
- }
2454
- const absolutePath = resolveResult.data;
2455
- const cached = this.cache.get(absolutePath);
2456
- if (cached) {
2457
- return { success: true, data: cached };
2458
- }
2459
- let result;
2460
- if (loaderType === "http") {
2461
- if (!this.httpLoader) {
2462
- return {
2463
- success: false,
2464
- error: "HTTP loader not available"
2465
- };
2787
+ try {
2788
+ const cached = this.cache.get(absoluteUrl);
2789
+ if (cached) {
2790
+ return { success: true, data: cached };
2466
2791
  }
2467
- result = await this.httpLoader.load(importPath, fromPath, importChain);
2468
- } else {
2469
- if (!this.fsLoader) {
2470
- if (this.httpLoader) {
2471
- result = await this.httpLoader.load(
2472
- importPath,
2473
- fromPath,
2474
- importChain
2475
- );
2476
- } else {
2477
- return {
2478
- success: false,
2479
- error: `Filesystem loader not available and import "${importPath}" cannot be loaded via HTTP. This typically happens when loading local files in a browser environment.`
2480
- };
2481
- }
2482
- } else {
2483
- result = await this.fsLoader.load(importPath, fromPath, importChain);
2792
+ const loadResult = await this.fetchSchema(absoluteUrl);
2793
+ if (!loadResult.success) {
2794
+ return loadResult;
2484
2795
  }
2796
+ const loaded = {
2797
+ schema: loadResult.data,
2798
+ sourcePath: absoluteUrl,
2799
+ importPath
2800
+ };
2801
+ this.cache.set(absoluteUrl, loaded);
2802
+ return { success: true, data: loaded };
2803
+ } finally {
2804
+ importChain.pop();
2485
2805
  }
2486
- if (result.success) {
2487
- this.cache.set(absolutePath, result.data);
2488
- }
2489
- return result;
2490
2806
  }
2491
2807
  /**
2492
2808
  * Load a specific orbital from a schema by name.
@@ -2497,988 +2813,2444 @@ var UnifiedLoader = class {
2497
2813
  return schemaResult;
2498
2814
  }
2499
2815
  const schema = schemaResult.data.schema;
2816
+ let orbital;
2500
2817
  if (orbitalName) {
2501
- const found = schema.orbitals.find((o) => o.name === orbitalName);
2818
+ const found = schema.orbitals.find(
2819
+ (o) => o.name === orbitalName
2820
+ );
2502
2821
  if (!found) {
2503
2822
  return {
2504
2823
  success: false,
2505
2824
  error: `Orbital "${orbitalName}" not found in ${importPath}. Available: ${schema.orbitals.map((o) => o.name).join(", ")}`
2506
2825
  };
2507
2826
  }
2508
- return {
2509
- success: true,
2510
- data: {
2511
- orbital: found,
2512
- sourcePath: schemaResult.data.sourcePath,
2513
- importPath
2514
- }
2515
- };
2516
- }
2517
- if (schema.orbitals.length === 0) {
2518
- return {
2519
- success: false,
2520
- error: `No orbitals found in ${importPath}`
2521
- };
2827
+ orbital = found;
2828
+ } else {
2829
+ if (schema.orbitals.length === 0) {
2830
+ return {
2831
+ success: false,
2832
+ error: `No orbitals found in ${importPath}`
2833
+ };
2834
+ }
2835
+ orbital = schema.orbitals[0];
2522
2836
  }
2523
2837
  return {
2524
2838
  success: true,
2525
2839
  data: {
2526
- orbital: schema.orbitals[0],
2840
+ orbital,
2527
2841
  sourcePath: schemaResult.data.sourcePath,
2528
2842
  importPath
2529
2843
  }
2530
2844
  };
2531
2845
  }
2532
2846
  /**
2533
- * Resolve an import path to an absolute path/URL.
2847
+ * Resolve an import path to an absolute URL.
2534
2848
  */
2535
2849
  resolvePath(importPath, fromPath) {
2536
- const loaderType = this.getLoaderForPath(importPath);
2537
- if (loaderType === "http" && this.httpLoader) {
2538
- return this.httpLoader.resolvePath(importPath, fromPath);
2850
+ if (importPath.startsWith("http://") || importPath.startsWith("https://")) {
2851
+ return { success: true, data: importPath };
2539
2852
  }
2540
- if (this.fsLoader) {
2541
- return this.fsLoader.resolvePath(importPath, fromPath);
2853
+ if (importPath.startsWith("std/")) {
2854
+ return this.resolveStdPath(importPath);
2542
2855
  }
2543
- if (this.httpLoader) {
2544
- return this.httpLoader.resolvePath(importPath, fromPath);
2856
+ if (importPath.startsWith("@")) {
2857
+ return this.resolveScopedPath(importPath);
2545
2858
  }
2546
- return {
2547
- success: false,
2548
- error: "No loader available for path resolution"
2549
- };
2859
+ if (importPath.startsWith("./") || importPath.startsWith("../")) {
2860
+ return this.resolveRelativePath(importPath, fromPath);
2861
+ }
2862
+ return this.resolveRelativePath(`./${importPath}`, fromPath);
2550
2863
  }
2551
2864
  /**
2552
- * Clear all caches.
2865
+ * Resolve a standard library path.
2553
2866
  */
2554
- clearCache() {
2555
- this.cache.clear();
2556
- this.httpLoader?.clearCache();
2557
- this.fsLoader?.clearCache();
2867
+ resolveStdPath(importPath) {
2868
+ if (!this.options.stdLibPath) {
2869
+ return {
2870
+ success: false,
2871
+ error: `Standard library URL not configured. Cannot load: ${importPath}`
2872
+ };
2873
+ }
2874
+ const relativePath = importPath.slice(4);
2875
+ let absoluteUrl = this.joinUrl(this.options.stdLibPath, relativePath);
2876
+ if (!absoluteUrl.endsWith(".orb")) {
2877
+ absoluteUrl += ".orb";
2878
+ }
2879
+ return { success: true, data: absoluteUrl };
2558
2880
  }
2559
2881
  /**
2560
- * Get combined cache statistics.
2882
+ * Resolve a scoped package path.
2561
2883
  */
2562
- getCacheStats() {
2563
- let size = this.cache.size;
2564
- if (this.httpLoader) {
2565
- size += this.httpLoader.getCacheStats().size;
2884
+ resolveScopedPath(importPath) {
2885
+ const match = importPath.match(/^(@[^/]+)/);
2886
+ if (!match) {
2887
+ return {
2888
+ success: false,
2889
+ error: `Invalid scoped package path: ${importPath}`
2890
+ };
2566
2891
  }
2567
- if (this.fsLoader) {
2568
- size += this.fsLoader.getCacheStats().size;
2892
+ const scope = match[1];
2893
+ const scopeRoot = this.options.scopedPaths[scope];
2894
+ if (!scopeRoot) {
2895
+ return {
2896
+ success: false,
2897
+ error: `Scoped package "${scope}" not configured. Available: ${Object.keys(this.options.scopedPaths).join(", ") || "none"}`
2898
+ };
2569
2899
  }
2570
- return { size };
2900
+ const relativePath = importPath.slice(scope.length + 1);
2901
+ let absoluteUrl = this.joinUrl(scopeRoot, relativePath);
2902
+ if (!absoluteUrl.endsWith(".orb")) {
2903
+ absoluteUrl += ".orb";
2904
+ }
2905
+ return { success: true, data: absoluteUrl };
2571
2906
  }
2572
2907
  /**
2573
- * Check if filesystem loading is available.
2908
+ * Resolve a relative path.
2574
2909
  */
2575
- async hasFilesystemAccess() {
2576
- await this.initFsLoader();
2577
- return this.fsLoader !== null;
2910
+ resolveRelativePath(importPath, fromPath) {
2911
+ const baseUrl = fromPath ? this.getParentUrl(fromPath) : this.options.basePath;
2912
+ let absoluteUrl = this.joinUrl(baseUrl, importPath);
2913
+ if (!absoluteUrl.endsWith(".orb")) {
2914
+ absoluteUrl += ".orb";
2915
+ }
2916
+ return { success: true, data: absoluteUrl };
2578
2917
  }
2579
2918
  /**
2580
- * Get current environment info.
2919
+ * Fetch and parse an OrbitalSchema from a URL.
2581
2920
  */
2582
- getEnvironmentInfo() {
2583
- return {
2584
- isElectron: isElectron(),
2585
- isBrowser: isBrowser(),
2586
- isNode: isNode(),
2587
- hasFilesystem: this.fsLoader !== null
2588
- };
2589
- }
2590
- };
2591
- function createUnifiedLoader(options) {
2592
- return new UnifiedLoader(options);
2593
- }
2594
- var refResolverLog = createLogger("almadar:runtime:ref-resolver");
2595
- function renameEventsInRenderUiConfig(node, rename) {
2596
- if (node === null || node === void 0) return node;
2597
- if (Array.isArray(node)) {
2598
- return node.map((item) => renameEventsInRenderUiConfig(item, rename));
2599
- }
2600
- if (typeof node !== "object") return node;
2601
- const obj = node;
2602
- const next = { ...obj };
2603
- for (const [key, value] of Object.entries(obj)) {
2604
- if (key === "action" && typeof value === "string" && !value.startsWith("@")) {
2605
- next[key] = rename(value) ?? value;
2606
- continue;
2607
- }
2608
- if (/^on[A-Z]/.test(key) && typeof value === "string" && !value.startsWith("@")) {
2609
- next[key] = rename(value) ?? value;
2610
- continue;
2611
- }
2612
- if (key.endsWith("Event") && typeof value === "string" && !value.startsWith("@")) {
2613
- next[key] = rename(value) ?? value;
2614
- continue;
2615
- }
2616
- if ((key === "actions" || key === "itemActions") && Array.isArray(value)) {
2617
- const rewrittenArray = value.map((entry) => {
2618
- if (!entry || typeof entry !== "object" || Array.isArray(entry)) return entry;
2619
- const action = entry;
2620
- if (typeof action.event === "string" && !action.event.startsWith("@")) {
2621
- return { ...action, event: rename(action.event) ?? action.event };
2622
- }
2623
- return action;
2624
- });
2625
- next[key] = rewrittenArray;
2626
- continue;
2627
- }
2628
- next[key] = renameEventsInRenderUiConfig(value, rename);
2629
- }
2630
- return next;
2631
- }
2632
- function renameEventsInEffects(effects, rename) {
2633
- return effects.map((effect) => {
2634
- if (!Array.isArray(effect)) return effect;
2635
- if (effect[0] === "render-ui" && effect.length >= 3) {
2636
- const slot = effect[1];
2637
- const config = effect[2];
2638
- const nextConfig = renameEventsInRenderUiConfig(config, rename);
2639
- return [effect[0], slot, nextConfig, ...effect.slice(3)];
2640
- }
2641
- return effect;
2642
- });
2921
+ async fetchSchema(url) {
2922
+ try {
2923
+ const controller = new AbortController();
2924
+ const timeoutId = setTimeout(
2925
+ () => controller.abort(),
2926
+ this.options.timeout
2927
+ );
2928
+ try {
2929
+ const response = await fetch(url, {
2930
+ ...this.options.fetchOptions,
2931
+ credentials: this.options.credentials,
2932
+ signal: controller.signal,
2933
+ headers: {
2934
+ Accept: "application/json",
2935
+ ...this.options.fetchOptions?.headers
2936
+ }
2937
+ });
2938
+ if (!response.ok) {
2939
+ return {
2940
+ success: false,
2941
+ error: `HTTP ${response.status}: ${response.statusText} for ${url}`
2942
+ };
2943
+ }
2944
+ const text = await response.text();
2945
+ let data;
2946
+ try {
2947
+ data = JSON.parse(text);
2948
+ } catch (e) {
2949
+ return {
2950
+ success: false,
2951
+ error: `Invalid JSON in ${url}: ${e instanceof Error ? e.message : String(e)}`
2952
+ };
2953
+ }
2954
+ const parseResult = OrbitalSchemaSchema.safeParse(data);
2955
+ if (!parseResult.success) {
2956
+ const errors = parseResult.error.errors.map((e) => ` - ${e.path.join(".")}: ${e.message}`).join("\n");
2957
+ return {
2958
+ success: false,
2959
+ error: `Invalid schema in ${url}:
2960
+ ${errors}`
2961
+ };
2962
+ }
2963
+ return { success: true, data: parseResult.data };
2964
+ } finally {
2965
+ clearTimeout(timeoutId);
2966
+ }
2967
+ } catch (e) {
2968
+ if (e instanceof Error && e.name === "AbortError") {
2969
+ return {
2970
+ success: false,
2971
+ error: `Request timeout for ${url} (${this.options.timeout}ms)`
2972
+ };
2973
+ }
2974
+ return {
2975
+ success: false,
2976
+ error: `Failed to fetch ${url}: ${e instanceof Error ? e.message : String(e)}`
2977
+ };
2978
+ }
2979
+ }
2980
+ /**
2981
+ * Join URL parts, handling relative paths and trailing slashes.
2982
+ */
2983
+ joinUrl(base, path) {
2984
+ if (path.startsWith("http://") || path.startsWith("https://")) {
2985
+ return path;
2986
+ }
2987
+ if (typeof URL !== "undefined") {
2988
+ try {
2989
+ const baseUrl = base.endsWith("/") ? base : base + "/";
2990
+ return new URL(path, baseUrl).href;
2991
+ } catch {
2992
+ }
2993
+ }
2994
+ const normalizedBase = base.endsWith("/") ? base.slice(0, -1) : base;
2995
+ const normalizedPath = path.startsWith("/") ? path : "/" + path;
2996
+ if (normalizedPath.startsWith("/./")) {
2997
+ return normalizedBase + normalizedPath.slice(2);
2998
+ }
2999
+ if (normalizedPath.startsWith("/../")) {
3000
+ const baseParts = normalizedBase.split("/");
3001
+ baseParts.pop();
3002
+ return baseParts.join("/") + normalizedPath.slice(3);
3003
+ }
3004
+ return normalizedBase + normalizedPath;
3005
+ }
3006
+ /**
3007
+ * Get parent URL (directory) from a URL.
3008
+ */
3009
+ getParentUrl(url) {
3010
+ if (typeof URL !== "undefined") {
3011
+ try {
3012
+ const urlObj = new URL(url);
3013
+ const pathParts = urlObj.pathname.split("/");
3014
+ pathParts.pop();
3015
+ urlObj.pathname = pathParts.join("/");
3016
+ return urlObj.href;
3017
+ } catch {
3018
+ }
3019
+ }
3020
+ const lastSlash = url.lastIndexOf("/");
3021
+ if (lastSlash !== -1) {
3022
+ return url.slice(0, lastSlash);
3023
+ }
3024
+ return url;
3025
+ }
3026
+ /**
3027
+ * Clear the cache.
3028
+ */
3029
+ clearCache() {
3030
+ this.cache.clear();
3031
+ }
3032
+ /**
3033
+ * Get cache statistics.
3034
+ */
3035
+ getCacheStats() {
3036
+ return { size: this.cache.size };
3037
+ }
3038
+ };
3039
+
3040
+ // src/loader/unified-loader.ts
3041
+ var externalLoaderModule = null;
3042
+ async function getExternalLoaderModule() {
3043
+ if (externalLoaderModule) {
3044
+ return externalLoaderModule;
3045
+ }
3046
+ if (isBrowser()) {
3047
+ return null;
3048
+ }
3049
+ try {
3050
+ externalLoaderModule = await import('./external-loader-UHZQPCKW.js');
3051
+ return externalLoaderModule;
3052
+ } catch {
3053
+ return null;
3054
+ }
2643
3055
  }
2644
- function renameEntityInRenderUiConfig(node, oldName, newName) {
2645
- if (node === null || node === void 0) return node;
2646
- if (Array.isArray(node)) {
2647
- return node.map((item) => renameEntityInRenderUiConfig(item, oldName, newName));
3056
+ var UnifiedImportChain = class _UnifiedImportChain {
3057
+ chain = [];
3058
+ push(path) {
3059
+ const normalized = this.normalizePath(path);
3060
+ if (this.chain.includes(normalized)) {
3061
+ const cycle = [
3062
+ ...this.chain.slice(this.chain.indexOf(normalized)),
3063
+ normalized
3064
+ ];
3065
+ return `Circular import detected: ${cycle.join(" -> ")}`;
3066
+ }
3067
+ this.chain.push(normalized);
3068
+ return null;
2648
3069
  }
2649
- if (typeof node !== "object") return node;
2650
- const obj = node;
2651
- const next = { ...obj };
2652
- for (const [key, value] of Object.entries(obj)) {
2653
- if (key === "entity" && value === oldName) {
2654
- next[key] = newName;
2655
- continue;
3070
+ pop() {
3071
+ this.chain.pop();
3072
+ }
3073
+ clone() {
3074
+ const newChain = new _UnifiedImportChain();
3075
+ newChain.chain = [...this.chain];
3076
+ return newChain;
3077
+ }
3078
+ normalizePath(path) {
3079
+ if (path.startsWith("http://") || path.startsWith("https://")) {
3080
+ return path;
3081
+ }
3082
+ return path.replace(/\\/g, "/");
3083
+ }
3084
+ };
3085
+ var UnifiedLoader = class {
3086
+ options;
3087
+ httpLoader = null;
3088
+ fsLoader = null;
3089
+ fsLoaderInitialized = false;
3090
+ cache = /* @__PURE__ */ new Map();
3091
+ constructor(options) {
3092
+ this.options = options;
3093
+ this.httpLoader = new HttpLoader({
3094
+ basePath: options.basePath,
3095
+ stdLibPath: options.stdLibPath,
3096
+ scopedPaths: options.scopedPaths,
3097
+ ...options.http
3098
+ });
3099
+ }
3100
+ /**
3101
+ * Initialize the filesystem loader if available.
3102
+ */
3103
+ async initFsLoader() {
3104
+ if (this.fsLoaderInitialized) {
3105
+ return;
3106
+ }
3107
+ this.fsLoaderInitialized = true;
3108
+ if (this.options.forceLoader === "http") {
3109
+ return;
3110
+ }
3111
+ const module = await getExternalLoaderModule();
3112
+ if (module) {
3113
+ this.fsLoader = new module.ExternalOrbitalLoader({
3114
+ basePath: this.options.basePath,
3115
+ stdLibPath: this.options.stdLibPath,
3116
+ scopedPaths: this.options.scopedPaths,
3117
+ ...this.options.fileSystem
3118
+ });
3119
+ }
3120
+ }
3121
+ /**
3122
+ * Determine which loader to use for an import path.
3123
+ */
3124
+ getLoaderForPath(importPath) {
3125
+ if (this.options.forceLoader) {
3126
+ return this.options.forceLoader;
3127
+ }
3128
+ if (importPath.startsWith("http://") || importPath.startsWith("https://")) {
3129
+ return "http";
3130
+ }
3131
+ if (importPath.startsWith("std/") && this.options.stdLibPath) {
3132
+ if (this.options.stdLibPath.startsWith("http://") || this.options.stdLibPath.startsWith("https://")) {
3133
+ return "http";
3134
+ }
3135
+ }
3136
+ if (importPath.startsWith("@") && this.options.scopedPaths) {
3137
+ const match = importPath.match(/^(@[^/]+)/);
3138
+ if (match) {
3139
+ const scopePath = this.options.scopedPaths[match[1]];
3140
+ if (scopePath && (scopePath.startsWith("http://") || scopePath.startsWith("https://"))) {
3141
+ return "http";
3142
+ }
3143
+ }
3144
+ }
3145
+ if (isBrowser()) {
3146
+ return "http";
3147
+ }
3148
+ return "filesystem";
3149
+ }
3150
+ /**
3151
+ * Load a schema from an import path.
3152
+ *
3153
+ * Note: We delegate chain management to the inner loader (HttpLoader or FsLoader).
3154
+ * The inner loader handles circular import detection, so we don't push/pop here.
3155
+ * We only use the unified cache to avoid duplicate loads across loaders.
3156
+ */
3157
+ async load(importPath, fromPath, chain) {
3158
+ await this.initFsLoader();
3159
+ const importChain = chain ?? new UnifiedImportChain();
3160
+ const loaderType = this.getLoaderForPath(importPath);
3161
+ const resolveResult = this.resolvePath(importPath, fromPath);
3162
+ if (!resolveResult.success) {
3163
+ return resolveResult;
3164
+ }
3165
+ const absolutePath = resolveResult.data;
3166
+ const cached = this.cache.get(absolutePath);
3167
+ if (cached) {
3168
+ return { success: true, data: cached };
3169
+ }
3170
+ let result;
3171
+ if (loaderType === "http") {
3172
+ if (!this.httpLoader) {
3173
+ return {
3174
+ success: false,
3175
+ error: "HTTP loader not available"
3176
+ };
3177
+ }
3178
+ result = await this.httpLoader.load(importPath, fromPath, importChain);
3179
+ } else {
3180
+ if (!this.fsLoader) {
3181
+ if (this.httpLoader) {
3182
+ result = await this.httpLoader.load(
3183
+ importPath,
3184
+ fromPath,
3185
+ importChain
3186
+ );
3187
+ } else {
3188
+ return {
3189
+ success: false,
3190
+ error: `Filesystem loader not available and import "${importPath}" cannot be loaded via HTTP. This typically happens when loading local files in a browser environment.`
3191
+ };
3192
+ }
3193
+ } else {
3194
+ result = await this.fsLoader.load(importPath, fromPath, importChain);
3195
+ }
3196
+ }
3197
+ if (result.success) {
3198
+ this.cache.set(absolutePath, result.data);
3199
+ }
3200
+ return result;
3201
+ }
3202
+ /**
3203
+ * Load a specific orbital from a schema by name.
3204
+ */
3205
+ async loadOrbital(importPath, orbitalName, fromPath, chain) {
3206
+ const schemaResult = await this.load(importPath, fromPath, chain);
3207
+ if (!schemaResult.success) {
3208
+ return schemaResult;
3209
+ }
3210
+ const schema = schemaResult.data.schema;
3211
+ if (orbitalName) {
3212
+ const found = schema.orbitals.find((o) => o.name === orbitalName);
3213
+ if (!found) {
3214
+ return {
3215
+ success: false,
3216
+ error: `Orbital "${orbitalName}" not found in ${importPath}. Available: ${schema.orbitals.map((o) => o.name).join(", ")}`
3217
+ };
3218
+ }
3219
+ return {
3220
+ success: true,
3221
+ data: {
3222
+ orbital: found,
3223
+ sourcePath: schemaResult.data.sourcePath,
3224
+ importPath
3225
+ }
3226
+ };
3227
+ }
3228
+ if (schema.orbitals.length === 0) {
3229
+ return {
3230
+ success: false,
3231
+ error: `No orbitals found in ${importPath}`
3232
+ };
3233
+ }
3234
+ return {
3235
+ success: true,
3236
+ data: {
3237
+ orbital: schema.orbitals[0],
3238
+ sourcePath: schemaResult.data.sourcePath,
3239
+ importPath
3240
+ }
3241
+ };
3242
+ }
3243
+ /**
3244
+ * Resolve an import path to an absolute path/URL.
3245
+ */
3246
+ resolvePath(importPath, fromPath) {
3247
+ const loaderType = this.getLoaderForPath(importPath);
3248
+ if (loaderType === "http" && this.httpLoader) {
3249
+ return this.httpLoader.resolvePath(importPath, fromPath);
3250
+ }
3251
+ if (this.fsLoader) {
3252
+ return this.fsLoader.resolvePath(importPath, fromPath);
3253
+ }
3254
+ if (this.httpLoader) {
3255
+ return this.httpLoader.resolvePath(importPath, fromPath);
3256
+ }
3257
+ return {
3258
+ success: false,
3259
+ error: "No loader available for path resolution"
3260
+ };
3261
+ }
3262
+ /**
3263
+ * Clear all caches.
3264
+ */
3265
+ clearCache() {
3266
+ this.cache.clear();
3267
+ this.httpLoader?.clearCache();
3268
+ this.fsLoader?.clearCache();
3269
+ }
3270
+ /**
3271
+ * Get combined cache statistics.
3272
+ */
3273
+ getCacheStats() {
3274
+ let size = this.cache.size;
3275
+ if (this.httpLoader) {
3276
+ size += this.httpLoader.getCacheStats().size;
2656
3277
  }
2657
- next[key] = renameEntityInRenderUiConfig(value, oldName, newName);
3278
+ if (this.fsLoader) {
3279
+ size += this.fsLoader.getCacheStats().size;
3280
+ }
3281
+ return { size };
2658
3282
  }
2659
- return next;
2660
- }
2661
- function renameEntityInEffects(effects, oldName, newName) {
2662
- return effects.map((effect) => renameEntityInEffect(effect, oldName, newName));
2663
- }
2664
- var ENTITY_AT_POS_1 = /* @__PURE__ */ new Set(["fetch", "ref", "deref", "spawn"]);
2665
- var ALL_ARGS_ARE_EFFECTS = /* @__PURE__ */ new Set([
2666
- "do",
2667
- "atomic",
2668
- "async/race",
2669
- "async/all",
2670
- "async/sequence"
2671
- ]);
2672
- var ARGS_FROM_POS_2_ARE_EFFECTS = /* @__PURE__ */ new Set([
2673
- "if",
2674
- "when",
2675
- "let",
2676
- "async/delay",
2677
- "async/debounce",
2678
- "async/throttle",
2679
- "async/interval"
2680
- ]);
2681
- function renameEntityInEffect(effect, oldName, newName) {
2682
- if (!Array.isArray(effect) || effect.length === 0) return effect;
2683
- const op = effect[0];
2684
- if (typeof op !== "string") return effect;
2685
- if (op === "render-ui" && effect.length >= 3) {
2686
- const [, slot, config, ...rest] = effect;
2687
- const nextConfig = renameEntityInRenderUiConfig(config, oldName, newName);
2688
- return [op, slot, nextConfig, ...rest];
3283
+ /**
3284
+ * Check if filesystem loading is available.
3285
+ */
3286
+ async hasFilesystemAccess() {
3287
+ await this.initFsLoader();
3288
+ return this.fsLoader !== null;
2689
3289
  }
2690
- if (op === "persist" && effect.length >= 3 && effect[2] === oldName) {
2691
- return [op, effect[1], newName, ...effect.slice(3)];
3290
+ /**
3291
+ * Get current environment info.
3292
+ */
3293
+ getEnvironmentInfo() {
3294
+ return {
3295
+ isElectron: isElectron(),
3296
+ isBrowser: isBrowser(),
3297
+ isNode: isNode(),
3298
+ hasFilesystem: this.fsLoader !== null
3299
+ };
2692
3300
  }
2693
- if (ENTITY_AT_POS_1.has(op) && effect[1] === oldName) {
2694
- return [op, newName, ...effect.slice(2)];
3301
+ };
3302
+ function createUnifiedLoader(options) {
3303
+ return new UnifiedLoader(options);
3304
+ }
3305
+
3306
+ // src/UsesIntegration.ts
3307
+ async function preprocessSchema(schema, options) {
3308
+ const namespaceEvents = options.namespaceEvents ?? true;
3309
+ const resolveResult = await resolveSchema(schema, options);
3310
+ if (!resolveResult.success) {
3311
+ return { success: false, errors: resolveResult.errors };
2695
3312
  }
2696
- const skipFirstNonEffectArg = ARGS_FROM_POS_2_ARE_EFFECTS.has(op);
2697
- const recurseAll = ALL_ARGS_ARE_EFFECTS.has(op);
2698
- if (recurseAll || skipFirstNonEffectArg) {
2699
- const startIndex = skipFirstNonEffectArg ? 2 : 1;
2700
- return effect.map((arg, i) => {
2701
- if (i < startIndex) return arg;
2702
- if (Array.isArray(arg)) {
2703
- return renameEntityInEffect(arg, oldName, newName);
3313
+ const resolved = resolveResult.data;
3314
+ const warnings = resolveResult.warnings;
3315
+ const preprocessedOrbitals = [];
3316
+ const entitySharing = {};
3317
+ const eventNamespaces = {};
3318
+ for (const resolvedOrbital of resolved) {
3319
+ const orbitalName = resolvedOrbital.name;
3320
+ const persistence = resolvedOrbital.entitySource?.persistence ?? resolvedOrbital.entity.persistence ?? "persistent";
3321
+ entitySharing[orbitalName] = {
3322
+ entityName: resolvedOrbital.entity.name,
3323
+ persistence,
3324
+ isShared: persistence !== "runtime",
3325
+ sourceAlias: resolvedOrbital.entitySource?.alias,
3326
+ collectionName: resolvedOrbital.entity.collection
3327
+ };
3328
+ eventNamespaces[orbitalName] = {};
3329
+ for (const resolvedTrait of resolvedOrbital.traits) {
3330
+ const traitName = resolvedTrait.trait.name;
3331
+ const namespace = {
3332
+ emits: {},
3333
+ listens: {}
3334
+ };
3335
+ if (namespaceEvents && resolvedTrait.source.type === "imported") {
3336
+ const emits = resolvedTrait.trait.emits ?? [];
3337
+ for (const emit of emits) {
3338
+ const eventName = typeof emit === "string" ? emit : emit.event;
3339
+ namespace.emits[eventName] = `${orbitalName}.${traitName}.${eventName}`;
3340
+ }
3341
+ const listens = resolvedTrait.trait.listens ?? [];
3342
+ for (const listen of listens) {
3343
+ namespace.listens[listen.event] = listen.event;
3344
+ }
2704
3345
  }
2705
- return arg;
2706
- });
3346
+ eventNamespaces[orbitalName][traitName] = namespace;
3347
+ }
3348
+ const preprocessedOrbital = {
3349
+ name: orbitalName,
3350
+ description: resolvedOrbital.original.description,
3351
+ visual_prompt: resolvedOrbital.original.visual_prompt,
3352
+ // Resolved entity (always inline now)
3353
+ entity: resolvedOrbital.entity,
3354
+ // Resolved traits (inline definitions)
3355
+ traits: (resolvedOrbital.traits || []).map((rt) => {
3356
+ if (rt.config || rt.linkedEntity) {
3357
+ return {
3358
+ ref: rt.trait.name,
3359
+ config: rt.config,
3360
+ linkedEntity: rt.linkedEntity,
3361
+ // Include the resolved trait definition for runtime
3362
+ _resolved: rt.trait
3363
+ };
3364
+ }
3365
+ return rt.trait;
3366
+ }),
3367
+ // Resolved pages (inline definitions with path overrides applied)
3368
+ pages: resolvedOrbital.pages.map((rp) => rp.page),
3369
+ // Preserve other fields
3370
+ exposes: resolvedOrbital.original.exposes,
3371
+ domainContext: resolvedOrbital.original.domainContext,
3372
+ design: resolvedOrbital.original.design
3373
+ };
3374
+ preprocessedOrbitals.push(preprocessedOrbital);
2707
3375
  }
2708
- return effect;
2709
- }
2710
- function applyLinkedEntityRename(trait, linkedEntity) {
2711
- const atomLinked = trait.linkedEntity;
2712
- if (!linkedEntity || !atomLinked || linkedEntity === atomLinked) return trait;
2713
- const sm = trait.stateMachine;
2714
- if (!sm) return { ...trait, linkedEntity };
2715
- const nextTransitions = (sm.transitions ?? []).map((t) => {
2716
- const nextEffects = t.effects ? renameEntityInEffects(
2717
- t.effects,
2718
- atomLinked,
2719
- linkedEntity
2720
- ) : t.effects;
2721
- return { ...t, effects: nextEffects };
2722
- });
2723
- refResolverLog.info("linkedEntity:rename", {
2724
- trait: trait.name,
2725
- from: atomLinked,
2726
- to: linkedEntity,
2727
- transitionCount: nextTransitions.length
2728
- });
2729
- return {
2730
- ...trait,
2731
- linkedEntity,
2732
- stateMachine: { ...sm, transitions: nextTransitions }
3376
+ const preprocessedSchema = {
3377
+ ...schema,
3378
+ orbitals: preprocessedOrbitals
2733
3379
  };
2734
- }
2735
- function applyEventRenames(trait, renames) {
2736
- if (!renames || Object.keys(renames).length === 0) return trait;
2737
- const rename = (k) => k !== void 0 && k in renames ? renames[k] : k;
2738
- const sm = trait.stateMachine;
2739
- if (!sm) return trait;
2740
- const nextTransitions = (sm.transitions ?? []).map((t) => {
2741
- const nextEvent = rename(t.event) ?? t.event;
2742
- const nextEffects = t.effects ? renameEventsInEffects(t.effects, rename) : t.effects;
2743
- return { ...t, event: nextEvent, effects: nextEffects };
2744
- });
2745
- const nextEvents = (sm.events ?? []).map((e) => {
2746
- const newKey = rename(e.key);
2747
- if (newKey === e.key) return e;
2748
- return { ...e, key: newKey ?? e.key };
2749
- });
2750
- const nextEmits = (trait.emits ?? []).map((em) => {
2751
- if (typeof em === "string") return rename(em) ?? em;
2752
- const newEvent = rename(em.event);
2753
- return newEvent === em.event ? em : { ...em, event: newEvent ?? em.event };
2754
- });
2755
3380
  return {
2756
- ...trait,
2757
- stateMachine: {
2758
- ...sm,
2759
- transitions: nextTransitions,
2760
- events: nextEvents
2761
- },
2762
- emits: nextEmits
3381
+ success: true,
3382
+ data: {
3383
+ schema: preprocessedSchema,
3384
+ entitySharing,
3385
+ eventNamespaces,
3386
+ warnings
3387
+ }
2763
3388
  };
2764
3389
  }
2765
- var ReferenceResolver = class {
2766
- loader;
2767
- options;
2768
- localTraits;
2769
- loaderInitialized = false;
2770
- constructor(options) {
2771
- this.options = options;
2772
- this.loader = options.loader;
2773
- this.localTraits = options.localTraits ?? /* @__PURE__ */ new Map();
3390
+ function getIsolatedCollectionName(orbitalName, entitySharing) {
3391
+ const info = entitySharing[orbitalName];
3392
+ if (!info) {
3393
+ throw new Error(`Unknown orbital: ${orbitalName}`);
3394
+ }
3395
+ if (info.persistence === "runtime") {
3396
+ return `${orbitalName}_${info.entityName}`;
3397
+ }
3398
+ return info.collectionName || info.entityName.toLowerCase() + "s";
3399
+ }
3400
+ function getNamespacedEvent(orbitalName, traitName, eventName, eventNamespaces) {
3401
+ const orbitalNs = eventNamespaces[orbitalName];
3402
+ if (!orbitalNs) return eventName;
3403
+ const traitNs = orbitalNs[traitName];
3404
+ if (!traitNs) return eventName;
3405
+ return traitNs.emits[eventName] || eventName;
3406
+ }
3407
+ function isNamespacedEvent(eventName) {
3408
+ return eventName.includes(".");
3409
+ }
3410
+ function parseNamespacedEvent(eventName) {
3411
+ const parts = eventName.split(".");
3412
+ if (parts.length === 3) {
3413
+ return { orbital: parts[0], trait: parts[1], event: parts[2] };
3414
+ }
3415
+ if (parts.length === 2) {
3416
+ return { trait: parts[0], event: parts[1] };
3417
+ }
3418
+ return { event: eventName };
3419
+ }
3420
+
3421
+ // src/PersistenceAdapter.ts
3422
+ var InMemoryPersistence = class {
3423
+ data = /* @__PURE__ */ new Map();
3424
+ idCounter = 0;
3425
+ /**
3426
+ * Seed the store with pre-existing rows.
3427
+ *
3428
+ * Accepts either a plain `Record<entityType, EntityRow[]>` or an iterable
3429
+ * of `[entityType, EntityRow[]]` entries. Rows without an `id` get one
3430
+ * generated at insert time; rows with an `id` keep it (so re-seeding
3431
+ * after a schema rebuild preserves identities used in render bindings).
3432
+ */
3433
+ seed(seedData) {
3434
+ const entries = Symbol.iterator in Object(seedData) ? seedData : Object.entries(seedData);
3435
+ for (const [entityType, rows] of entries) {
3436
+ if (!this.data.has(entityType)) {
3437
+ this.data.set(entityType, /* @__PURE__ */ new Map());
3438
+ }
3439
+ const collection = this.data.get(entityType);
3440
+ for (const row of rows) {
3441
+ const id = row.id || `${entityType}-${++this.idCounter}`;
3442
+ collection.set(id, { ...row, id });
3443
+ }
3444
+ }
2774
3445
  }
2775
- async ensureLoader() {
2776
- if (this.loader || this.loaderInitialized) return;
2777
- this.loaderInitialized = true;
2778
- try {
2779
- const { ExternalOrbitalLoader } = await import('./external-loader-UHZQPCKW.js');
2780
- this.loader = new ExternalOrbitalLoader(this.options);
2781
- } catch {
3446
+ async create(entityType, data) {
3447
+ const id = data.id || `${entityType}-${++this.idCounter}`;
3448
+ if (!this.data.has(entityType)) {
3449
+ this.data.set(entityType, /* @__PURE__ */ new Map());
3450
+ }
3451
+ this.data.get(entityType).set(id, { ...data, id });
3452
+ return { id };
3453
+ }
3454
+ async update(entityType, id, data) {
3455
+ const collection = this.data.get(entityType);
3456
+ if (collection?.has(id)) {
3457
+ const existing = collection.get(id);
3458
+ collection.set(id, { ...existing, ...data });
2782
3459
  }
2783
3460
  }
3461
+ async delete(entityType, id) {
3462
+ this.data.get(entityType)?.delete(id);
3463
+ }
3464
+ async getById(entityType, id) {
3465
+ return this.data.get(entityType)?.get(id) || null;
3466
+ }
3467
+ async list(entityType) {
3468
+ const collection = this.data.get(entityType);
3469
+ return collection ? Array.from(collection.values()) : [];
3470
+ }
2784
3471
  /**
2785
- * Resolve all references in an orbital.
3472
+ * Snapshot the entire store as a plain object (entityType → rows).
3473
+ * Useful for feeding a fresh render-time binding layer with the
3474
+ * current persistence view.
2786
3475
  */
2787
- async resolve(orbital, sourcePath, chain) {
2788
- const errors = [];
2789
- const warnings = [];
2790
- const importChain = chain ?? { push: () => null, pop: () => {
2791
- }, clone() {
2792
- return this;
2793
- } };
2794
- const importsResult = await this.resolveImports(
2795
- orbital.uses ?? [],
2796
- sourcePath,
2797
- importChain
2798
- );
2799
- if (!importsResult.success) {
2800
- return { success: false, errors: importsResult.errors };
3476
+ snapshot() {
3477
+ const out = {};
3478
+ for (const [entityType, collection] of this.data) {
3479
+ out[entityType] = Array.from(collection.values());
2801
3480
  }
2802
- const imports = importsResult.data;
2803
- const entityResult = this.resolveEntity(orbital.entity, imports);
2804
- if (!entityResult.success) {
2805
- errors.push(...entityResult.errors);
3481
+ return out;
3482
+ }
3483
+ };
3484
+
3485
+ // src/OrbitalServerRuntime.ts
3486
+ var _resolvedNodeRequire = null;
3487
+ function nodeRequire(modulePath) {
3488
+ if (!_resolvedNodeRequire) {
3489
+ const evalRequire = (0, eval)('typeof require !== "undefined" ? require : null');
3490
+ if (evalRequire) {
3491
+ _resolvedNodeRequire = evalRequire;
3492
+ } else {
3493
+ const createReq = nodeModule.createRequire;
3494
+ if (typeof createReq !== "function") {
3495
+ throw new Error(
3496
+ "[OrbitalServerRuntime] No synchronous require available. This branch is Node-only \u2014 invoking it from a browser indicates an isNodeEnv() guard regression upstream."
3497
+ );
3498
+ }
3499
+ _resolvedNodeRequire = createReq(import.meta.url);
2806
3500
  }
2807
- const traitsResult = this.resolveTraits(orbital.traits, imports);
2808
- if (!traitsResult.success) {
2809
- errors.push(...traitsResult.errors);
3501
+ }
3502
+ return _resolvedNodeRequire(modulePath);
3503
+ }
3504
+ var _nodeRequireExt = import.meta.url.endsWith(".ts") ? ".ts" : ".js";
3505
+ var effectLog2 = createLogger("almadar:runtime:effects");
3506
+ var busLog = createLogger("almadar:runtime:bus");
3507
+ var renderLog2 = createLogger("almadar:runtime:render-ui");
3508
+ var xOrbitalLog = createLogger("almadar:runtime:cross-orbital");
3509
+ function isNodeEnv() {
3510
+ return typeof process !== "undefined" && Boolean(process.versions?.node);
3511
+ }
3512
+ function collectDeclaredConfigDefaults(trait) {
3513
+ if (!trait) return void 0;
3514
+ const schema = trait.config;
3515
+ if (!schema || typeof schema !== "object") return void 0;
3516
+ const defaults = {};
3517
+ let hasAny = false;
3518
+ for (const [key, field] of Object.entries(schema)) {
3519
+ if (field && typeof field === "object" && !Array.isArray(field) && "default" in field) {
3520
+ const def = field.default;
3521
+ if (def !== void 0) {
3522
+ defaults[key] = def;
3523
+ hasAny = true;
3524
+ }
2810
3525
  }
2811
- const pagesResult = this.resolvePages(orbital.pages, imports);
2812
- if (!pagesResult.success) {
2813
- errors.push(...pagesResult.errors);
3526
+ }
3527
+ return hasAny ? defaults : void 0;
3528
+ }
3529
+ function needsPreprocessing(schema) {
3530
+ for (const orbital of schema.orbitals) {
3531
+ const uses = orbital.uses;
3532
+ if (Array.isArray(uses) && uses.length > 0) {
3533
+ return true;
3534
+ }
3535
+ const traits = orbital.traits ?? [];
3536
+ for (const t of traits) {
3537
+ if (!t || typeof t !== "object") continue;
3538
+ const obj = t;
3539
+ if (typeof obj.ref === "string" && obj.ref.includes(".") && !obj.stateMachine) {
3540
+ return true;
3541
+ }
2814
3542
  }
2815
- if (errors.length > 0) {
2816
- return { success: false, errors };
3543
+ }
3544
+ return false;
3545
+ }
3546
+ var OrbitalServerRuntime = class {
3547
+ orbitals = /* @__PURE__ */ new Map();
3548
+ eventBus;
3549
+ config;
3550
+ persistence;
3551
+ listenerCleanups = [];
3552
+ tickBindings = [];
3553
+ loader = null;
3554
+ preprocessedCache = /* @__PURE__ */ new Map();
3555
+ entitySharingMap = {};
3556
+ eventNamespaceMap = {};
3557
+ osHandlers = null;
3558
+ localPersistence = null;
3559
+ resolvedSchema = null;
3560
+ constructor(config = {}) {
3561
+ this.config = {
3562
+ mode: "mock",
3563
+ // Default to mock mode for preview
3564
+ autoPreprocess: false,
3565
+ namespaceEvents: true,
3566
+ ...config
3567
+ };
3568
+ this.eventBus = new EventBus();
3569
+ if (config.loaderConfig?.loader) {
3570
+ this.loader = config.loaderConfig.loader;
3571
+ } else if (config.loaderConfig?.stdLibPath) {
3572
+ this.loader = createUnifiedLoader({
3573
+ basePath: config.loaderConfig.basePath,
3574
+ stdLibPath: config.loaderConfig.stdLibPath,
3575
+ scopedPaths: config.loaderConfig.scopedPaths
3576
+ });
2817
3577
  }
2818
- if (!entityResult.success || !traitsResult.success || !pagesResult.success) {
2819
- return { success: false, errors: ["Internal error: unexpected failure state"] };
3578
+ if (this.config.mode === "mock" && !config.persistence) {
3579
+ this.persistence = new MockPersistenceAdapter({
3580
+ seed: config.mockSeed,
3581
+ defaultSeedCount: config.mockSeedCount ?? 6,
3582
+ debug: config.debug
3583
+ });
3584
+ if (config.debug) {
3585
+ console.log("[OrbitalRuntime] Using mock persistence with faker data");
3586
+ }
3587
+ } else {
3588
+ this.persistence = config.persistence || new InMemoryPersistence();
2820
3589
  }
2821
- return {
2822
- success: true,
2823
- data: {
2824
- name: orbital.name,
2825
- entity: entityResult.data.entity,
2826
- entitySource: entityResult.data.source,
2827
- traits: traitsResult.data,
2828
- pages: pagesResult.data,
2829
- imports,
2830
- original: orbital
2831
- },
2832
- warnings
3590
+ if (config.localStorageRoot && isNodeEnv()) {
3591
+ const { LocalPersistenceAdapter } = nodeRequire(`./LocalPersistenceAdapter${_nodeRequireExt}`);
3592
+ this.localPersistence = new LocalPersistenceAdapter(config.localStorageRoot);
3593
+ }
3594
+ if (isNodeEnv()) {
3595
+ const { createOsHandlers } = nodeRequire(`./createOsHandlers${_nodeRequireExt}`);
3596
+ this.osHandlers = createOsHandlers({
3597
+ emitEvent: (type, payload) => this.eventBus.emit(type, payload)
3598
+ });
3599
+ } else {
3600
+ this.osHandlers = { handlers: {}, cleanup: () => {
3601
+ } };
3602
+ }
3603
+ this.config.effectHandlers = {
3604
+ ...this.osHandlers.handlers,
3605
+ ...this.config.effectHandlers
2833
3606
  };
2834
3607
  }
2835
3608
  /**
2836
- * Resolve `uses` declarations to loaded orbitals.
3609
+ * Lazily construct a default loader when the caller didn't provide one
3610
+ * but `register()` needs to preprocess. Looks for `@almadar/std` in the
3611
+ * nearest `node_modules` so cross-orbital `std/behaviors/<name>` imports
3612
+ * resolve to the tiered registry on disk.
3613
+ *
3614
+ * Node only — browsers should receive already-preprocessed schemas from
3615
+ * their server.
2837
3616
  */
2838
- async resolveImports(uses, sourcePath, chain) {
2839
- const errors = [];
2840
- const orbitals = /* @__PURE__ */ new Map();
2841
- if (this.options.skipExternalLoading) {
2842
- return {
2843
- success: true,
2844
- data: { orbitals },
2845
- warnings: ["External loading skipped"]
2846
- };
3617
+ async ensureLoader() {
3618
+ if (this.loader) return;
3619
+ if (typeof process === "undefined" || !process.versions?.node) {
3620
+ return;
2847
3621
  }
2848
- for (const use of uses) {
2849
- if (orbitals.has(use.as)) {
2850
- errors.push(`Duplicate import alias: ${use.as}`);
2851
- continue;
3622
+ try {
3623
+ const [{ fileURLToPath }, path, fs] = await Promise.all([
3624
+ import('url'),
3625
+ import('path'),
3626
+ import('fs')
3627
+ ]);
3628
+ const mainEntryUrl = import.meta.resolve("@almadar/std");
3629
+ const mainEntry = fileURLToPath(mainEntryUrl);
3630
+ let stdLibPath = path.dirname(mainEntry);
3631
+ while (stdLibPath !== path.dirname(stdLibPath)) {
3632
+ if (fs.existsSync(path.join(stdLibPath, "package.json"))) {
3633
+ const pkg = JSON.parse(
3634
+ fs.readFileSync(path.join(stdLibPath, "package.json"), "utf-8")
3635
+ );
3636
+ if (pkg.name === "@almadar/std") break;
3637
+ }
3638
+ stdLibPath = path.dirname(stdLibPath);
2852
3639
  }
2853
- await this.ensureLoader();
2854
- if (!this.loader) {
2855
- errors.push(`No loader available to resolve import: ${use.from}`);
2856
- continue;
3640
+ const basePath = this.config.loaderConfig?.basePath ?? process.cwd();
3641
+ this.loader = createUnifiedLoader({
3642
+ basePath,
3643
+ stdLibPath,
3644
+ scopedPaths: this.config.loaderConfig?.scopedPaths
3645
+ });
3646
+ if (this.config.debug) {
3647
+ console.log(
3648
+ `[OrbitalRuntime] Default loader constructed: basePath=${basePath} stdLibPath=${stdLibPath}`
3649
+ );
2857
3650
  }
2858
- const loadResult = await this.loader.loadOrbital(
2859
- use.from,
2860
- void 0,
2861
- sourcePath,
2862
- chain
2863
- );
2864
- if (!loadResult.success) {
2865
- errors.push(`Failed to load "${use.from}" as "${use.as}": ${loadResult.error}`);
2866
- continue;
3651
+ } catch (err) {
3652
+ if (this.config.debug) {
3653
+ console.warn(
3654
+ `[OrbitalRuntime] Could not auto-construct loader: ${err instanceof Error ? err.message : String(err)}`
3655
+ );
2867
3656
  }
2868
- orbitals.set(use.as, {
2869
- alias: use.as,
2870
- from: use.from,
2871
- orbital: loadResult.data.orbital,
2872
- sourcePath: loadResult.data.sourcePath
2873
- });
2874
3657
  }
2875
- if (errors.length > 0) {
2876
- return { success: false, errors };
3658
+ }
3659
+ // ==========================================================================
3660
+ // Schema Registration
3661
+ // ==========================================================================
3662
+ /**
3663
+ * Register an OrbitalSchema for execution.
3664
+ *
3665
+ * Auto-preprocesses the schema when it contains `uses` declarations or
3666
+ * unresolved cross-orbital trait references (e.g. a trait with
3667
+ * `ref: "Modal.traits.ModalRecordModal"` and no inline `stateMachine`).
3668
+ * Without preprocessing, those refs arrive empty at the state machine and
3669
+ * button clicks silently do nothing — see Phase 9.5.H.
3670
+ *
3671
+ * Preprocessing needs a loader. If `loaderConfig` is set, that loader is
3672
+ * used. Otherwise, a default loader is constructed that points at
3673
+ * `<cwd>` (for `basePath`) and the nearest `node_modules/@almadar/std` (for
3674
+ * `stdLibPath`), which matches how every caller in this monorepo has the
3675
+ * std registry on disk.
3676
+ */
3677
+ async register(schema) {
3678
+ if (this.config.debug) {
3679
+ console.log(`[OrbitalRuntime] Registering schema: ${schema.name}`);
2877
3680
  }
2878
- return { success: true, data: { orbitals }, warnings: [] };
3681
+ if (needsPreprocessing(schema)) {
3682
+ await this.ensureLoader();
3683
+ if (this.loader) {
3684
+ if (this.config.debug) {
3685
+ console.log(`[OrbitalRuntime] Schema has uses/refs \u2014 auto-preprocessing`);
3686
+ }
3687
+ const result = await preprocessSchema(schema, {
3688
+ basePath: this.config.loaderConfig?.basePath || process.cwd(),
3689
+ stdLibPath: this.config.loaderConfig?.stdLibPath,
3690
+ scopedPaths: this.config.loaderConfig?.scopedPaths,
3691
+ loader: this.loader,
3692
+ namespaceEvents: this.config.namespaceEvents
3693
+ });
3694
+ if (!result.success) {
3695
+ throw new Error(
3696
+ `Schema preprocessing failed: ${result.errors.join("; ")}`
3697
+ );
3698
+ }
3699
+ schema = result.data.schema;
3700
+ this.entitySharingMap = {
3701
+ ...this.entitySharingMap,
3702
+ ...result.data.entitySharing
3703
+ };
3704
+ this.eventNamespaceMap = {
3705
+ ...this.eventNamespaceMap,
3706
+ ...result.data.eventNamespaces
3707
+ };
3708
+ } else if (this.config.debug) {
3709
+ console.warn(
3710
+ `[OrbitalRuntime] Schema has uses/refs but no loader available \u2014 proceeding without preprocessing. Cross-orbital trait refs will be empty.`
3711
+ );
3712
+ }
3713
+ }
3714
+ for (const orbital of schema.orbitals) {
3715
+ await this.registerOrbitalAsync(orbital);
3716
+ }
3717
+ this.setupEventListeners();
3718
+ this.setupTicks();
3719
+ this.resolvedSchema = schema;
2879
3720
  }
2880
3721
  /**
2881
- * Resolve entity reference.
3722
+ * Register an OrbitalSchema synchronously (for backward compatibility).
3723
+ * Note: This version doesn't wait for instance seeding to complete.
3724
+ * Use async register() for guaranteed instance seeding.
2882
3725
  */
2883
- resolveEntity(entityRef, imports) {
2884
- if (isEntityCall(entityRef)) {
2885
- const fallbackName = entityRef.name ?? entityRef.extends.replace(/\.entity$/, "");
2886
- return {
2887
- success: true,
2888
- data: {
2889
- entity: {
2890
- name: fallbackName,
2891
- fields: entityRef.fields ?? [],
2892
- ...entityRef.persistence ? { persistence: entityRef.persistence } : {},
2893
- ...entityRef.collection ? { collection: entityRef.collection } : {}
2894
- }
2895
- },
2896
- warnings: []
2897
- };
3726
+ registerSync(schema) {
3727
+ if (this.config.debug) {
3728
+ console.log(`[OrbitalRuntime] Registering schema (sync): ${schema.name}`);
2898
3729
  }
2899
- if (!isEntityReference(entityRef)) {
2900
- return {
2901
- success: true,
2902
- data: { entity: entityRef },
2903
- warnings: []
2904
- };
3730
+ for (const orbital of schema.orbitals) {
3731
+ this.registerOrbital(orbital);
2905
3732
  }
2906
- const parsed = parseEntityRef(entityRef);
2907
- if (!parsed) {
3733
+ this.setupEventListeners();
3734
+ this.setupTicks();
3735
+ this.resolvedSchema = schema;
3736
+ }
3737
+ /**
3738
+ * Returns the schema that this runtime is currently executing, post-
3739
+ * preprocessing. Safe to expose from an HTTP `/api/schema` endpoint — every
3740
+ * cross-orbital trait ref will have an inline `stateMachine` already, which
3741
+ * is what the browser's `schema-to-ir` resolver needs to wire button clicks
3742
+ * back to state transitions.
3743
+ *
3744
+ * Returns `null` if `register()` hasn't run yet.
3745
+ */
3746
+ getResolvedSchema() {
3747
+ return this.resolvedSchema;
3748
+ }
3749
+ /**
3750
+ * One-call entry point: read an `.orb` file from disk, parse it, preprocess
3751
+ * cross-orbital imports, and register the result. Callers never touch raw
3752
+ * `.orb` bytes — `register()` handles preprocessing internally.
3753
+ *
3754
+ * Node only. Browsers must receive already-resolved schemas from their
3755
+ * server (see `getResolvedSchema()`).
3756
+ */
3757
+ async registerFromFile(path) {
3758
+ if (typeof process === "undefined" || !process.versions?.node) {
3759
+ throw new Error(
3760
+ "registerFromFile is Node-only. Browsers should receive resolved schemas from their server."
3761
+ );
3762
+ }
3763
+ const { readFile } = await import('fs/promises');
3764
+ const raw = await readFile(path, "utf-8");
3765
+ let schema;
3766
+ try {
3767
+ schema = JSON.parse(raw);
3768
+ } catch (err) {
3769
+ const msg = err instanceof Error ? err.message : String(err);
3770
+ throw new Error(`registerFromFile: ${path} is not valid JSON: ${msg}`);
3771
+ }
3772
+ await this.register(schema);
3773
+ }
3774
+ /**
3775
+ * Register an OrbitalSchema with preprocessing to resolve `uses` imports.
3776
+ *
3777
+ * This method:
3778
+ * 1. Loads all external orbitals referenced in `uses` declarations
3779
+ * 2. Expands entity/trait/page references to inline definitions
3780
+ * 3. Builds entity sharing and event namespace maps
3781
+ * 4. Caches the preprocessed result
3782
+ * 5. Registers the resolved schema
3783
+ *
3784
+ * @param schema - Schema with potential `uses` declarations
3785
+ * @param options - Optional preprocessing options
3786
+ * @returns Preprocessing result with entity sharing info
3787
+ *
3788
+ * @example
3789
+ * ```typescript
3790
+ * const runtime = new OrbitalServerRuntime({
3791
+ * loaderConfig: {
3792
+ * basePath: '/schemas',
3793
+ * stdLibPath: '/std',
3794
+ * },
3795
+ * });
3796
+ *
3797
+ * const result = await runtime.registerWithPreprocess(schema);
3798
+ * if (result.success) {
3799
+ * console.log('Registered with', Object.keys(result.entitySharing).length, 'orbitals');
3800
+ * }
3801
+ * ```
3802
+ */
3803
+ async registerWithPreprocess(schema, options) {
3804
+ if (!this.loader && !this.config.loaderConfig) {
2908
3805
  return {
2909
3806
  success: false,
2910
- errors: [`Invalid entity reference format: ${entityRef}. Expected "Alias.entity"`]
3807
+ errors: ["Loader not configured. Set loaderConfig in OrbitalServerRuntimeConfig."]
2911
3808
  };
2912
3809
  }
2913
- const imported = imports.orbitals.get(parsed.alias);
2914
- if (!imported) {
3810
+ if (!this.loader && this.config.loaderConfig) {
3811
+ this.loader = this.config.loaderConfig.loader ?? createUnifiedLoader({
3812
+ basePath: this.config.loaderConfig.basePath,
3813
+ stdLibPath: this.config.loaderConfig.stdLibPath,
3814
+ scopedPaths: this.config.loaderConfig.scopedPaths
3815
+ });
3816
+ }
3817
+ const cacheKey = `${schema.name}:${schema.version || "1.0.0"}`;
3818
+ const cached = this.preprocessedCache.get(cacheKey);
3819
+ if (cached) {
3820
+ if (this.config.debug) {
3821
+ console.log(`[OrbitalRuntime] Using cached preprocessed schema: ${schema.name}`);
3822
+ }
3823
+ this.register(cached.schema);
3824
+ this.entitySharingMap = { ...this.entitySharingMap, ...cached.entitySharing };
3825
+ this.eventNamespaceMap = { ...this.eventNamespaceMap, ...cached.eventNamespaces };
2915
3826
  return {
2916
- success: false,
2917
- errors: [
2918
- `Unknown import alias in entity reference: ${parsed.alias}. Available aliases: ${Array.from(imports.orbitals.keys()).join(", ") || "none"}`
2919
- ]
3827
+ success: true,
3828
+ entitySharing: cached.entitySharing,
3829
+ eventNamespaces: cached.eventNamespaces,
3830
+ warnings: cached.warnings
2920
3831
  };
2921
3832
  }
2922
- const importedEntity = this.getEntityFromOrbital(imported.orbital);
2923
- if (!importedEntity) {
3833
+ if (this.config.debug) {
3834
+ console.log(`[OrbitalRuntime] Preprocessing schema: ${schema.name}`);
3835
+ }
3836
+ const result = await preprocessSchema(schema, {
3837
+ basePath: this.config.loaderConfig?.basePath || ".",
3838
+ stdLibPath: this.config.loaderConfig?.stdLibPath,
3839
+ scopedPaths: this.config.loaderConfig?.scopedPaths,
3840
+ loader: this.loader,
3841
+ namespaceEvents: this.config.namespaceEvents
3842
+ });
3843
+ if (!result.success) {
2924
3844
  return {
2925
- success: false,
2926
- errors: [
2927
- `Imported orbital "${parsed.alias}" does not have an inline entity. Entity references cannot be chained.`
2928
- ]
3845
+ success: false,
3846
+ errors: result.errors
2929
3847
  };
2930
3848
  }
2931
- const persistence = importedEntity.persistence ?? "persistent";
3849
+ this.preprocessedCache.set(cacheKey, result.data);
3850
+ this.entitySharingMap = { ...this.entitySharingMap, ...result.data.entitySharing };
3851
+ this.eventNamespaceMap = { ...this.eventNamespaceMap, ...result.data.eventNamespaces };
3852
+ this.register(result.data.schema);
2932
3853
  return {
2933
3854
  success: true,
2934
- data: {
2935
- entity: importedEntity,
2936
- source: {
2937
- alias: parsed.alias,
2938
- persistence
2939
- }
2940
- },
2941
- warnings: []
3855
+ entitySharing: result.data.entitySharing,
3856
+ eventNamespaces: result.data.eventNamespaces,
3857
+ warnings: result.data.warnings
2942
3858
  };
2943
3859
  }
2944
3860
  /**
2945
- * Get the entity from an orbital (handling EntityRef).
3861
+ * Get entity sharing information for registered orbitals.
3862
+ * Useful for determining entity isolation and collection names.
2946
3863
  */
2947
- getEntityFromOrbital(orbital) {
3864
+ getEntitySharing() {
3865
+ return { ...this.entitySharingMap };
3866
+ }
3867
+ /**
3868
+ * Get event namespace mapping for registered orbitals.
3869
+ * Useful for debugging cross-orbital event routing.
3870
+ */
3871
+ getEventNamespaces() {
3872
+ return { ...this.eventNamespaceMap };
3873
+ }
3874
+ /**
3875
+ * Clear the preprocessing cache.
3876
+ */
3877
+ clearPreprocessCache() {
3878
+ this.preprocessedCache.clear();
3879
+ }
3880
+ /**
3881
+ * Register a single orbital
3882
+ */
3883
+ async registerOrbitalAsync(orbital) {
3884
+ const configByTrait = /* @__PURE__ */ new Map();
3885
+ const unwrapped = (orbital.traits || []).map((t) => {
3886
+ if (t && typeof t === "object" && "ref" in t && "_resolved" in t) {
3887
+ const wrapper = t;
3888
+ const inner = wrapper._resolved;
3889
+ if (wrapper.config && inner?.name) {
3890
+ configByTrait.set(inner.name, wrapper.config);
3891
+ }
3892
+ return inner;
3893
+ }
3894
+ return t;
3895
+ });
3896
+ const inlineTraits = unwrapped.filter(isInlineTrait);
3897
+ const traitDefs = inlineTraits.map((t) => {
3898
+ const sm = t.stateMachine;
3899
+ const states = sm?.states || [];
3900
+ const transitions = sm?.transitions || [];
3901
+ return {
3902
+ name: t.name,
3903
+ states,
3904
+ transitions,
3905
+ listens: t.listens
3906
+ };
3907
+ });
3908
+ const manager = new StateMachineManager(traitDefs, {
3909
+ contextExtensions: this.config.contextExtensions
3910
+ });
3911
+ for (const [traitName, traitConfig] of configByTrait) {
3912
+ manager.setTraitConfig(traitName, traitConfig);
3913
+ }
2948
3914
  const entityRef = orbital.entity;
3915
+ let entity;
2949
3916
  if (typeof entityRef === "string") {
2950
- return null;
2951
- }
2952
- if (isEntityCall(entityRef)) {
3917
+ entity = { name: entityRef, fields: [] };
3918
+ } else if (isEntityCall(entityRef)) {
2953
3919
  const fallbackName = entityRef.name ?? entityRef.extends.replace(/\.entity$/, "");
2954
- return {
3920
+ entity = {
2955
3921
  name: fallbackName,
2956
3922
  fields: entityRef.fields ?? [],
2957
3923
  ...entityRef.persistence ? { persistence: entityRef.persistence } : {},
2958
3924
  ...entityRef.collection ? { collection: entityRef.collection } : {}
2959
3925
  };
3926
+ } else {
3927
+ entity = entityRef;
3928
+ }
3929
+ this.orbitals.set(orbital.name, {
3930
+ schema: orbital,
3931
+ entity,
3932
+ traits: inlineTraits,
3933
+ configByTrait,
3934
+ manager,
3935
+ entityData: /* @__PURE__ */ new Map()
3936
+ });
3937
+ if (entity?.name && entity.instances && Array.isArray(entity.instances)) {
3938
+ const instances = entity.instances;
3939
+ if (instances.length > 0) {
3940
+ console.log(`[OrbitalRuntime] Seeding ${instances.length} instances for ${entity.name} from schema`);
3941
+ const results = await Promise.all(
3942
+ instances.map(async (instance) => {
3943
+ try {
3944
+ const result = await this.persistence.create(entity.name, instance);
3945
+ console.log(`[OrbitalRuntime] Seeded instance: ${instance.id || "no-id"}`);
3946
+ return result;
3947
+ } catch (err) {
3948
+ console.error(`[OrbitalRuntime] Failed to seed instance ${instance.id}:`, err);
3949
+ return null;
3950
+ }
3951
+ })
3952
+ );
3953
+ const successCount = results.filter((r) => r !== null).length;
3954
+ console.log(`[OrbitalRuntime] Seeded ${successCount}/${instances.length} ${entity.name} instances from schema`);
3955
+ }
3956
+ } else if (this.config.mode === "mock" && this.persistence instanceof MockPersistenceAdapter) {
3957
+ if (this.config.debug) {
3958
+ console.log(`[OrbitalRuntime] No instances in schema, generating mock data for ${entity?.name}`);
3959
+ }
3960
+ if (entity?.name && entity.fields) {
3961
+ const fields = entity.fields.filter(
3962
+ (f) => typeof f.name === "string" && f.name.length > 0
3963
+ ).map((f) => ({
3964
+ name: f.name,
3965
+ type: f.type,
3966
+ required: f.required,
3967
+ values: f.values,
3968
+ default: f.default
3969
+ }));
3970
+ this.persistence.registerEntity({ name: entity.name, fields });
3971
+ if (this.config.debug) {
3972
+ console.log(`[OrbitalRuntime] Seeded mock data for entity: ${entity.name}, count: ${this.persistence.count(entity.name)}`);
3973
+ }
3974
+ }
3975
+ }
3976
+ if (this.config.debug) {
3977
+ console.log(
3978
+ `[OrbitalRuntime] Registered orbital: ${orbital.name} with ${(orbital.traits || []).length} trait(s)`
3979
+ );
2960
3980
  }
2961
- return entityRef;
2962
3981
  }
2963
3982
  /**
2964
- * Resolve trait references.
3983
+ * Register a single orbital (sync wrapper for backward compatibility)
2965
3984
  */
2966
- resolveTraits(traitRefs, imports) {
2967
- const errors = [];
2968
- const resolved = [];
2969
- for (const traitRef of traitRefs) {
2970
- const result = this.resolveTraitRef(traitRef, imports);
2971
- if (!result.success) {
2972
- errors.push(...result.errors);
2973
- } else {
2974
- resolved.push(result.data);
3985
+ registerOrbital(orbital) {
3986
+ this.registerOrbitalAsync(orbital).catch((err) => {
3987
+ console.error(`[OrbitalRuntime] Failed to register orbital:`, err);
3988
+ });
3989
+ }
3990
+ /**
3991
+ * Set up event listeners for cross-orbital communication
3992
+ */
3993
+ setupEventListeners() {
3994
+ for (const cleanup of this.listenerCleanups) {
3995
+ cleanup();
3996
+ }
3997
+ this.listenerCleanups = [];
3998
+ for (const [orbitalName, registered] of this.orbitals) {
3999
+ for (const trait of registered.traits) {
4000
+ if (!trait.listens) continue;
4001
+ for (const listener of trait.listens) {
4002
+ const { bareEvent, matcher } = parseListenSource(listener, orbitalName);
4003
+ const cleanup = this.eventBus.on(bareEvent, async (event) => {
4004
+ if (!matcher(event.source)) return;
4005
+ if (this.config.debug) {
4006
+ console.log(
4007
+ `[OrbitalRuntime] ${orbitalName}.${trait.name} received: ${listener.event} (from ${event.source?.orbital ?? "?"}.${event.source?.trait ?? "?"})`
4008
+ );
4009
+ }
4010
+ let mappedPayload = event.payload;
4011
+ if (listener.payloadMapping && event.payload) {
4012
+ mappedPayload = {};
4013
+ for (const [key, expr] of Object.entries(
4014
+ listener.payloadMapping
4015
+ )) {
4016
+ if (typeof expr === "string" && expr.startsWith("@payload.")) {
4017
+ const field = expr.slice("@payload.".length);
4018
+ mappedPayload[key] = event.payload[field];
4019
+ } else {
4020
+ mappedPayload[key] = expr;
4021
+ }
4022
+ }
4023
+ }
4024
+ const raw = event.payload;
4025
+ const mapped = mappedPayload;
4026
+ const pickId = (field) => mapped?.[field] ?? raw?.[field];
4027
+ const forwardedEntityId = pickId("entityId") ?? pickId("orbitalName");
4028
+ await this.processOrbitalEvent(orbitalName, {
4029
+ event: listener.triggers,
4030
+ payload: mappedPayload,
4031
+ entityId: forwardedEntityId
4032
+ });
4033
+ });
4034
+ this.listenerCleanups.push(cleanup);
4035
+ }
2975
4036
  }
2976
4037
  }
2977
- if (errors.length > 0) {
2978
- return { success: false, errors };
2979
- }
2980
- return { success: true, data: resolved, warnings: [] };
2981
4038
  }
2982
4039
  /**
2983
- * Resolve a single trait reference.
4040
+ * Set up scheduled ticks for all traits
2984
4041
  */
2985
- resolveTraitRef(traitRef, imports) {
2986
- if (typeof traitRef !== "string" && "stateMachine" in traitRef) {
2987
- return {
2988
- success: true,
2989
- data: {
2990
- trait: traitRef,
2991
- source: { type: "inline" }
2992
- },
2993
- warnings: []
2994
- };
4042
+ setupTicks() {
4043
+ this.cleanupTicks();
4044
+ for (const [orbitalName, registered] of this.orbitals) {
4045
+ for (const trait of registered.traits || []) {
4046
+ if (!trait.ticks || trait.ticks.length === 0) continue;
4047
+ for (const tick of trait.ticks) {
4048
+ this.registerTick(orbitalName, trait.name, tick, registered);
4049
+ }
4050
+ }
2995
4051
  }
2996
- if (typeof traitRef !== "string" && "ref" in traitRef) {
2997
- const refObj = traitRef;
2998
- return this.resolveTraitRefString(
2999
- refObj.ref,
3000
- imports,
3001
- refObj.config,
3002
- refObj.linkedEntity,
3003
- refObj.name,
3004
- refObj.events,
3005
- refObj.listens
4052
+ if (this.config.debug && this.tickBindings.length > 0) {
4053
+ console.log(
4054
+ `[OrbitalRuntime] Registered ${this.tickBindings.length} tick(s)`
3006
4055
  );
3007
4056
  }
3008
- if (typeof traitRef === "string") {
3009
- return this.resolveTraitRefString(traitRef, imports);
3010
- }
3011
- return {
3012
- success: false,
3013
- errors: [`Unknown trait reference format: ${JSON.stringify(traitRef)}`]
3014
- };
3015
4057
  }
3016
4058
  /**
3017
- * Resolve a trait reference string.
4059
+ * Register a single tick
3018
4060
  */
3019
- resolveTraitRefString(ref, imports, config, linkedEntity, overrideName, eventRenames, listensOverride) {
3020
- const parsed = parseImportedTraitRef(ref);
3021
- if (parsed) {
3022
- const imported = imports.orbitals.get(parsed.alias);
3023
- if (!imported) {
3024
- return {
3025
- success: false,
3026
- errors: [
3027
- `Unknown import alias in trait reference: ${parsed.alias}. Available aliases: ${Array.from(imports.orbitals.keys()).join(", ") || "none"}`
3028
- ]
3029
- };
3030
- }
3031
- const trait = this.findTraitInOrbital(imported.orbital, parsed.traitName);
3032
- if (!trait) {
3033
- return {
3034
- success: false,
3035
- errors: [
3036
- `Trait "${parsed.traitName}" not found in imported orbital "${parsed.alias}". Available traits: ${this.listTraitsInOrbital(imported.orbital).join(", ") || "none"}`
3037
- ]
3038
- };
3039
- }
3040
- const baseTrait = overrideName ? { ...trait, name: overrideName } : trait;
3041
- const reboundTrait = applyLinkedEntityRename(baseTrait, linkedEntity);
3042
- const renamedTrait = applyEventRenames(reboundTrait, eventRenames);
3043
- const finalTrait = listensOverride !== void 0 ? { ...renamedTrait, listens: listensOverride } : renamedTrait;
3044
- if (listensOverride !== void 0) {
3045
- refResolverLog.info("listens-override:imported", {
3046
- trait: finalTrait.name,
3047
- ref,
3048
- atomListens: trait.listens?.length ?? 0,
3049
- callSiteListens: listensOverride.length
3050
- });
3051
- }
3052
- return {
3053
- success: true,
3054
- data: {
3055
- trait: finalTrait,
3056
- source: { type: "imported", alias: parsed.alias, traitName: parsed.traitName },
3057
- config,
3058
- linkedEntity
3059
- },
3060
- warnings: []
3061
- };
4061
+ registerTick(orbitalName, traitName, tick, registered) {
4062
+ let intervalMs;
4063
+ if (typeof tick.interval === "number") {
4064
+ intervalMs = tick.interval;
4065
+ } else if (typeof tick.interval === "string") {
4066
+ intervalMs = this.parseIntervalString(tick.interval);
4067
+ } else {
4068
+ intervalMs = 1e3;
3062
4069
  }
3063
- const localTrait = this.localTraits.get(ref);
3064
- if (localTrait) {
3065
- const baseLocal = overrideName ? { ...localTrait, name: overrideName } : localTrait;
3066
- const reboundLocal = applyLinkedEntityRename(baseLocal, linkedEntity);
3067
- const renamedLocalTrait = applyEventRenames(reboundLocal, eventRenames);
3068
- const finalLocalTrait = listensOverride !== void 0 ? { ...renamedLocalTrait, listens: listensOverride } : renamedLocalTrait;
3069
- if (listensOverride !== void 0) {
3070
- refResolverLog.info("listens-override:local", {
3071
- trait: finalLocalTrait.name,
3072
- ref,
3073
- atomListens: localTrait.listens?.length ?? 0,
3074
- callSiteListens: listensOverride.length
3075
- });
3076
- }
3077
- return {
3078
- success: true,
3079
- data: {
3080
- trait: finalLocalTrait,
3081
- source: { type: "local", name: ref },
3082
- config,
3083
- linkedEntity
3084
- },
3085
- warnings: []
3086
- };
4070
+ if (this.config.debug) {
4071
+ console.log(
4072
+ `[OrbitalRuntime] Registering tick: ${orbitalName}.${traitName}.${tick.name} (${intervalMs}ms)`
4073
+ );
3087
4074
  }
3088
- return {
3089
- success: false,
3090
- errors: [
3091
- `Trait "${ref}" not found. For imported traits, use format "Alias.traits.TraitName". Local traits available: ${Array.from(this.localTraits.keys()).join(", ") || "none"}`
3092
- ]
3093
- };
4075
+ const timerId = setInterval(async () => {
4076
+ await this.executeTick(orbitalName, traitName, tick, registered);
4077
+ }, intervalMs);
4078
+ this.tickBindings.push({
4079
+ orbitalName,
4080
+ traitName,
4081
+ tick,
4082
+ timerId
4083
+ });
3094
4084
  }
3095
4085
  /**
3096
- * Find a trait in an orbital by name.
4086
+ * Parse interval string to milliseconds
4087
+ * Supports: '5s', '1m', '1h', '30000' (ms)
3097
4088
  */
3098
- findTraitInOrbital(orbital, traitName) {
3099
- for (const traitRef of orbital.traits) {
3100
- if (typeof traitRef !== "string" && "stateMachine" in traitRef) {
3101
- if (traitRef.name === traitName) {
3102
- return traitRef;
3103
- }
3104
- }
3105
- if (typeof traitRef !== "string" && "ref" in traitRef) {
3106
- const refObj = traitRef;
3107
- if (refObj.ref === traitName || refObj.name === traitName) ;
3108
- }
4089
+ parseIntervalString(interval) {
4090
+ const match = interval.match(/^(\d+)(ms|s|m|h)?$/);
4091
+ if (!match) {
4092
+ console.warn(
4093
+ `[OrbitalRuntime] Invalid interval format: ${interval}, defaulting to 1000ms`
4094
+ );
4095
+ return 1e3;
4096
+ }
4097
+ const value = parseInt(match[1], 10);
4098
+ const unit = match[2] || "ms";
4099
+ switch (unit) {
4100
+ case "ms":
4101
+ return value;
4102
+ case "s":
4103
+ return value * 1e3;
4104
+ case "m":
4105
+ return value * 60 * 1e3;
4106
+ case "h":
4107
+ return value * 60 * 60 * 1e3;
4108
+ default:
4109
+ return value;
3109
4110
  }
3110
- return null;
3111
4111
  }
3112
4112
  /**
3113
- * List trait names in an orbital.
4113
+ * Execute a tick for all applicable entities
3114
4114
  */
3115
- listTraitsInOrbital(orbital) {
3116
- const names = [];
3117
- for (const traitRef of orbital.traits) {
3118
- if (typeof traitRef !== "string" && "stateMachine" in traitRef) {
3119
- names.push(traitRef.name);
4115
+ async executeTick(orbitalName, traitName, tick, registered) {
4116
+ const entityType = registered.entity.name;
4117
+ const emittedEvents = [];
4118
+ try {
4119
+ let entities = await this.persistence.list(entityType);
4120
+ if (tick.appliesTo && tick.appliesTo.length > 0) {
4121
+ const appliesToSet = new Set(tick.appliesTo);
4122
+ entities = entities.filter((e) => appliesToSet.has(e.id));
4123
+ }
4124
+ if (this.config.debug && entities.length > 0) {
4125
+ console.log(
4126
+ `[OrbitalRuntime] Tick ${orbitalName}.${traitName}.${tick.name}: processing ${entities.length} entities`
4127
+ );
4128
+ }
4129
+ for (const entity of entities) {
4130
+ if (tick.guard) {
4131
+ try {
4132
+ const ctx = createContextFromBindings({
4133
+ entity,
4134
+ payload: {},
4135
+ state: registered.manager.getState(traitName)?.currentState || "unknown"
4136
+ }, false, this.config.contextExtensions);
4137
+ const guardPasses = evaluateGuard(
4138
+ tick.guard,
4139
+ ctx
4140
+ );
4141
+ if (!guardPasses) {
4142
+ if (this.config.debug) {
4143
+ console.log(
4144
+ `[OrbitalRuntime] Tick ${tick.name}: guard failed for entity ${entity.id}`
4145
+ );
4146
+ }
4147
+ continue;
4148
+ }
4149
+ } catch (error) {
4150
+ console.error(
4151
+ `[OrbitalRuntime] Tick ${tick.name}: guard evaluation error for entity ${entity.id}:`,
4152
+ error
4153
+ );
4154
+ continue;
4155
+ }
4156
+ }
4157
+ if (tick.effects && tick.effects.length > 0) {
4158
+ const fetchedData = {};
4159
+ const clientEffects = [];
4160
+ const tickEffectResults = [];
4161
+ await this.executeEffects(
4162
+ registered,
4163
+ traitName,
4164
+ tick.effects,
4165
+ {},
4166
+ // No payload for ticks
4167
+ entity,
4168
+ entity.id,
4169
+ emittedEvents,
4170
+ fetchedData,
4171
+ clientEffects,
4172
+ tickEffectResults
4173
+ );
4174
+ if (this.config.debug) {
4175
+ console.log(
4176
+ `[OrbitalRuntime] Tick ${tick.name}: executed effects for entity ${entity.id}`
4177
+ );
4178
+ }
4179
+ }
3120
4180
  }
4181
+ } catch (error) {
4182
+ console.error(
4183
+ `[OrbitalRuntime] Tick ${tick.name} execution error:`,
4184
+ error
4185
+ );
3121
4186
  }
3122
- return names;
3123
4187
  }
3124
4188
  /**
3125
- * Resolve page references.
4189
+ * Clean up all active ticks
3126
4190
  */
3127
- resolvePages(pageRefs, imports) {
3128
- const errors = [];
3129
- const resolved = [];
3130
- for (const pageRef of pageRefs) {
3131
- const result = this.resolvePageRef(pageRef, imports);
3132
- if (!result.success) {
3133
- errors.push(...result.errors);
3134
- } else {
3135
- resolved.push(result.data);
3136
- }
3137
- }
3138
- if (errors.length > 0) {
3139
- return { success: false, errors };
4191
+ cleanupTicks() {
4192
+ for (const binding of this.tickBindings) {
4193
+ clearInterval(binding.timerId);
3140
4194
  }
3141
- return { success: true, data: resolved, warnings: [] };
4195
+ this.tickBindings = [];
3142
4196
  }
3143
4197
  /**
3144
- * Resolve a single page reference.
4198
+ * Unregister all orbitals and clean up
3145
4199
  */
3146
- resolvePageRef(pageRef, imports) {
3147
- if (!isPageReference(pageRef)) {
3148
- return {
3149
- success: true,
3150
- data: {
3151
- page: pageRef,
3152
- source: { type: "inline" },
3153
- pathOverridden: false
3154
- },
3155
- warnings: []
3156
- };
4200
+ unregisterAll() {
4201
+ this.cleanupTicks();
4202
+ for (const cleanup of this.listenerCleanups) {
4203
+ cleanup();
3157
4204
  }
3158
- if (isPageReferenceString(pageRef)) {
3159
- return this.resolvePageRefString(pageRef, imports);
4205
+ this.listenerCleanups = [];
4206
+ this.orbitals.clear();
4207
+ this.eventBus.clear();
4208
+ if (this.persistence instanceof MockPersistenceAdapter) {
4209
+ this.persistence.clearAll();
3160
4210
  }
3161
- if (isPageReferenceObject(pageRef)) {
3162
- return this.resolvePageRefObject(pageRef, imports);
4211
+ if (this.osHandlers) {
4212
+ this.osHandlers.cleanup();
4213
+ this.osHandlers = null;
4214
+ }
4215
+ }
4216
+ /**
4217
+ * Reset the mock persistence store to a clean-slate re-seed without
4218
+ * unregistering orbitals. Exposed for verifier tools that want to
4219
+ * start each test with deterministic seeded rows, not the residue of
4220
+ * the previous walk's persist-creates. No-op when the persistence
4221
+ * layer is not MockPersistenceAdapter.
4222
+ */
4223
+ resetMockPersistence() {
4224
+ if (!(this.persistence instanceof MockPersistenceAdapter)) return;
4225
+ busLog.debug("mock:reset:enter", {
4226
+ orbitalCount: this.orbitals.size,
4227
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
4228
+ });
4229
+ this.persistence.clearAll();
4230
+ for (const registered of this.orbitals.values()) {
4231
+ const entity = registered.entity;
4232
+ if (entity?.name && entity.fields) {
4233
+ const fields = entity.fields.filter(
4234
+ (f) => typeof f.name === "string" && f.name.length > 0
4235
+ ).map((f) => ({
4236
+ name: f.name,
4237
+ type: f.type,
4238
+ required: f.required,
4239
+ values: f.values,
4240
+ default: f.default
4241
+ }));
4242
+ this.persistence.registerEntity({ name: entity.name, fields });
4243
+ }
3163
4244
  }
3164
- return {
3165
- success: false,
3166
- errors: [`Unknown page reference format: ${JSON.stringify(pageRef)}`]
3167
- };
3168
4245
  }
4246
+ // ==========================================================================
4247
+ // Event Processing
4248
+ // ==========================================================================
3169
4249
  /**
3170
- * Resolve a page reference string.
4250
+ * Process an event for an orbital
3171
4251
  */
3172
- resolvePageRefString(ref, imports) {
3173
- const parsed = parsePageRef(ref);
3174
- if (!parsed) {
4252
+ async processOrbitalEvent(orbitalName, request) {
4253
+ const registered = this.orbitals.get(orbitalName);
4254
+ if (!registered) {
3175
4255
  return {
3176
4256
  success: false,
3177
- errors: [`Invalid page reference format: ${ref}. Expected "Alias.pages.PageName"`]
4257
+ transitioned: false,
4258
+ states: {},
4259
+ emittedEvents: [],
4260
+ error: `Orbital not found: ${orbitalName}`
3178
4261
  };
3179
4262
  }
3180
- const imported = imports.orbitals.get(parsed.alias);
3181
- if (!imported) {
3182
- return {
3183
- success: false,
3184
- errors: [
3185
- `Unknown import alias in page reference: ${parsed.alias}. Available aliases: ${Array.from(imports.orbitals.keys()).join(", ") || "none"}`
3186
- ]
3187
- };
4263
+ const payloadRow = request.payload?.["row"];
4264
+ const payloadRowAsPayload = payloadRow !== null && typeof payloadRow === "object" && !Array.isArray(payloadRow) ? payloadRow : void 0;
4265
+ const payloadRowId = payloadRowAsPayload?.["id"];
4266
+ renderLog2.debug("processOrbitalEvent:enter", {
4267
+ orbital: orbitalName,
4268
+ event: request.event,
4269
+ hasPayloadRow: payloadRowAsPayload !== void 0,
4270
+ payloadRowId: typeof payloadRowId === "string" || typeof payloadRowId === "number" ? payloadRowId : void 0,
4271
+ entityId: request.entityId
4272
+ });
4273
+ busLog.debug("bus:incoming", {
4274
+ orbital: orbitalName,
4275
+ event: request.event,
4276
+ payload: JSON.stringify(request.payload ?? null),
4277
+ entityId: request.entityId,
4278
+ traitStates: JSON.stringify(
4279
+ Array.from(registered.manager.getAllStates().entries()).map(([traitName, state]) => ({
4280
+ traitName,
4281
+ currentState: state.currentState
4282
+ }))
4283
+ )
4284
+ });
4285
+ xOrbitalLog.info("processOrbitalEvent:enter", {
4286
+ orbital: orbitalName,
4287
+ event: request.event,
4288
+ traitsInOrbital: registered.traits.map((t) => t.name).join(","),
4289
+ payloadActiveTraits: JSON.stringify(
4290
+ request.payload?.["_activeTraits"] ?? null
4291
+ )
4292
+ });
4293
+ const { event, payload, entityId, user } = request;
4294
+ const validationFailures = [];
4295
+ for (const trait of registered.traits) {
4296
+ const eventSchema = trait.stateMachine?.events?.find((e) => e.key === event);
4297
+ if (eventSchema?.payloadSchema && eventSchema.payloadSchema.length > 0) {
4298
+ validationFailures.push(
4299
+ ...validateEventPayload(event, payload, eventSchema.payloadSchema)
4300
+ );
4301
+ }
3188
4302
  }
3189
- const page = this.findPageInOrbital(imported.orbital, parsed.pageName);
3190
- if (!page) {
4303
+ if (validationFailures.length > 0) {
3191
4304
  return {
3192
4305
  success: false,
3193
- errors: [
3194
- `Page "${parsed.pageName}" not found in imported orbital "${parsed.alias}". Available pages: ${this.listPagesInOrbital(imported.orbital).join(", ") || "none"}`
3195
- ]
4306
+ transitioned: false,
4307
+ states: {},
4308
+ emittedEvents: [],
4309
+ error: formatPayloadValidationError(validationFailures)
3196
4310
  };
3197
4311
  }
3198
- return {
4312
+ const emittedEvents = [];
4313
+ const fetchedData = {};
4314
+ const clientEffects = [];
4315
+ const clientEffectsByTrait = [];
4316
+ const effectResults = [];
4317
+ const activeTraits = payload?._activeTraits;
4318
+ const cleanPayload = payload ? { ...payload } : void 0;
4319
+ if (cleanPayload) {
4320
+ delete cleanPayload._activeTraits;
4321
+ }
4322
+ let entityData = {};
4323
+ if (entityId) {
4324
+ const stored = await this.persistence.getById(
4325
+ registered.entity.name,
4326
+ entityId
4327
+ );
4328
+ if (stored) {
4329
+ entityData = stored;
4330
+ }
4331
+ } else if (registered.entity?.name) {
4332
+ try {
4333
+ const all = await this.persistence.list(registered.entity.name);
4334
+ if (Array.isArray(all) && all.length > 0 && all[0]) {
4335
+ entityData = all[0];
4336
+ }
4337
+ } catch {
4338
+ }
4339
+ }
4340
+ const results = registered.manager.sendEvent(event, cleanPayload, entityData);
4341
+ const filteredResults = activeTraits && activeTraits.length > 0 ? results.filter(({ traitName }) => activeTraits.includes(traitName)) : results;
4342
+ if (this.config.debug && activeTraits) {
4343
+ console.log(`[OrbitalRuntime] Filtering traits: ${results.length} total, ${filteredResults.length} active (${activeTraits.join(", ")})`);
4344
+ }
4345
+ for (const { traitName, result } of filteredResults) {
4346
+ if (result.effects.length > 0) {
4347
+ await this.executeEffects(
4348
+ registered,
4349
+ traitName,
4350
+ result.effects,
4351
+ cleanPayload,
4352
+ entityData,
4353
+ entityId,
4354
+ emittedEvents,
4355
+ fetchedData,
4356
+ clientEffects,
4357
+ effectResults,
4358
+ user,
4359
+ clientEffectsByTrait
4360
+ );
4361
+ }
4362
+ }
4363
+ const states = {};
4364
+ for (const [name, state] of registered.manager.getAllStates()) {
4365
+ states[name] = state.currentState;
4366
+ }
4367
+ const response = {
3199
4368
  success: true,
3200
- data: {
3201
- page,
3202
- source: { type: "imported", alias: parsed.alias, pageName: parsed.pageName },
3203
- pathOverridden: false
4369
+ transitioned: results.length > 0,
4370
+ states,
4371
+ emittedEvents
4372
+ };
4373
+ if (clientEffects.length > 0) {
4374
+ response.clientEffects = clientEffects;
4375
+ }
4376
+ if (clientEffectsByTrait.length > 0) {
4377
+ response.clientEffectsByTrait = clientEffectsByTrait;
4378
+ }
4379
+ if (effectResults.length > 0) {
4380
+ response.effectResults = effectResults;
4381
+ }
4382
+ return response;
4383
+ }
4384
+ /**
4385
+ * Execute effects from a transition
4386
+ */
4387
+ async executeEffects(registered, traitName, effects, payload, entityData, entityId, emittedEvents, fetchedData, clientEffects, effectResults, user, clientEffectsByTrait) {
4388
+ const entityType = registered.entity.name;
4389
+ const pushClientEffect = (effect) => {
4390
+ clientEffects.push(effect);
4391
+ clientEffectsByTrait?.push({ traitName, effect });
4392
+ };
4393
+ let bindingsRef = null;
4394
+ let contextRef = null;
4395
+ const handlers = {
4396
+ emit: (event, eventPayload, source) => {
4397
+ if (this.config.debug) {
4398
+ console.log(`[OrbitalRuntime] Emitting: ${event}`, eventPayload, source);
4399
+ }
4400
+ const stamp = source ?? {
4401
+ orbital: registered.schema.name,
4402
+ trait: traitName
4403
+ };
4404
+ this.eventBus.emit(event, eventPayload, stamp);
4405
+ emittedEvents.push({ event, payload: eventPayload, source: stamp });
4406
+ effectLog2.debug("emit:push", {
4407
+ event,
4408
+ cumulativeEmittedCount: emittedEvents.length,
4409
+ sourceTrait: stamp.trait,
4410
+ sourceOrbital: stamp.orbital
4411
+ });
4412
+ xOrbitalLog.info("emit:server", {
4413
+ event,
4414
+ sourceOrbital: stamp.orbital,
4415
+ sourceTrait: stamp.trait,
4416
+ dispatchOrbital: registered.schema.name
4417
+ });
3204
4418
  },
3205
- warnings: []
4419
+ set: async (targetId, field, value) => {
4420
+ const id = targetId || entityId;
4421
+ if (id) {
4422
+ try {
4423
+ await this.persistence.update(entityType, id, { [field]: value });
4424
+ effectResults.push({
4425
+ effect: "set",
4426
+ entityType,
4427
+ data: { id, field, value },
4428
+ success: true
4429
+ });
4430
+ } catch (err) {
4431
+ effectResults.push({
4432
+ effect: "set",
4433
+ entityType,
4434
+ data: { id, field, value },
4435
+ success: false,
4436
+ error: err instanceof Error ? err.message : String(err)
4437
+ });
4438
+ }
4439
+ }
4440
+ },
4441
+ persist: async (action, targetEntityType, data) => {
4442
+ if (action === "batch") {
4443
+ const operations = data?.operations;
4444
+ if (!Array.isArray(operations) || operations.length === 0) {
4445
+ effectResults.push({
4446
+ effect: "persist",
4447
+ action: "batch",
4448
+ success: false,
4449
+ error: "Batch requires a non-empty operations array"
4450
+ });
4451
+ return;
4452
+ }
4453
+ const batchResults = [];
4454
+ const completed = [];
4455
+ let batchFailed = false;
4456
+ let batchError = "";
4457
+ for (const op of operations) {
4458
+ if (!Array.isArray(op) || op.length < 2) {
4459
+ batchFailed = true;
4460
+ batchError = `Invalid batch operation format: ${JSON.stringify(op)}`;
4461
+ break;
4462
+ }
4463
+ const [opAction, opEntityType, ...opRest] = op;
4464
+ try {
4465
+ switch (opAction) {
4466
+ case "create": {
4467
+ const createData = opRest[0] || {};
4468
+ const { id: newId } = await this.persistence.create(opEntityType, createData);
4469
+ batchResults.push({ action: "create", entityType: opEntityType, id: newId, ...createData });
4470
+ completed.push({ action: "create", entityType: opEntityType, id: newId });
4471
+ break;
4472
+ }
4473
+ case "update": {
4474
+ const updateId = opRest[0];
4475
+ const updateData = opRest[1] || {};
4476
+ await this.persistence.update(opEntityType, updateId, updateData);
4477
+ const updated = await this.persistence.getById(opEntityType, updateId);
4478
+ batchResults.push({ action: "update", entityType: opEntityType, id: updateId, ...updated || updateData });
4479
+ completed.push({ action: "update", entityType: opEntityType, id: updateId });
4480
+ break;
4481
+ }
4482
+ case "delete": {
4483
+ const deleteId = opRest[0];
4484
+ await this.persistence.delete(opEntityType, deleteId);
4485
+ batchResults.push({ action: "delete", entityType: opEntityType, id: deleteId, deleted: true });
4486
+ completed.push({ action: "delete", entityType: opEntityType, id: deleteId });
4487
+ break;
4488
+ }
4489
+ default:
4490
+ batchFailed = true;
4491
+ batchError = `Unknown batch operation action: ${opAction}`;
4492
+ break;
4493
+ }
4494
+ } catch (err) {
4495
+ batchFailed = true;
4496
+ batchError = `Batch operation [${opAction}, ${opEntityType}] failed: ${err instanceof Error ? err.message : String(err)}`;
4497
+ break;
4498
+ }
4499
+ if (batchFailed) break;
4500
+ }
4501
+ effectResults.push({
4502
+ effect: "persist",
4503
+ action: "batch",
4504
+ data: {
4505
+ operations: batchResults,
4506
+ completedCount: completed.length,
4507
+ totalCount: operations.length
4508
+ },
4509
+ success: !batchFailed,
4510
+ ...batchFailed ? { error: batchError } : {}
4511
+ });
4512
+ return;
4513
+ }
4514
+ const type = targetEntityType || entityType;
4515
+ let resultData;
4516
+ const sizeBefore = (await this.persistence.list(type)).length;
4517
+ try {
4518
+ if (action === "create" || action === "update") {
4519
+ this.validateRelationCardinality(type, data || {});
4520
+ }
4521
+ switch (action) {
4522
+ case "create": {
4523
+ const { id } = await this.persistence.create(type, data || {});
4524
+ resultData = { id, ...data || {} };
4525
+ break;
4526
+ }
4527
+ case "update":
4528
+ if (data?.id || entityId) {
4529
+ const updateId = data?.id || entityId;
4530
+ await this.persistence.update(type, updateId, data || {});
4531
+ const updated = await this.persistence.getById(type, updateId);
4532
+ resultData = updated || { id: updateId, ...data || {} };
4533
+ }
4534
+ break;
4535
+ case "delete": {
4536
+ const directId = typeof data === "string" ? data : void 0;
4537
+ const nestedId = typeof data === "object" && data !== null ? data.id : void 0;
4538
+ const deleteId = directId ?? nestedId ?? entityId;
4539
+ if (deleteId) {
4540
+ await this.enforceOnDeleteRules(type, deleteId);
4541
+ await this.persistence.delete(type, deleteId);
4542
+ resultData = { id: deleteId, deleted: true };
4543
+ }
4544
+ break;
4545
+ }
4546
+ }
4547
+ const sizeAfter = (await this.persistence.list(type)).length;
4548
+ effectLog2.debug("persist:store-mutate", {
4549
+ action,
4550
+ entityType: type,
4551
+ resultId: resultData?.id,
4552
+ sizeBefore,
4553
+ sizeAfter,
4554
+ delta: sizeAfter - sizeBefore
4555
+ });
4556
+ effectResults.push({
4557
+ effect: "persist",
4558
+ action,
4559
+ entityType: type,
4560
+ data: resultData,
4561
+ success: true
4562
+ });
4563
+ } catch (err) {
4564
+ effectLog2.error("persist:store-mutate-error", {
4565
+ action,
4566
+ entityType: type,
4567
+ error: err instanceof Error ? err.message : String(err)
4568
+ });
4569
+ effectResults.push({
4570
+ effect: "persist",
4571
+ action,
4572
+ entityType: type,
4573
+ success: false,
4574
+ error: err instanceof Error ? err.message : String(err)
4575
+ });
4576
+ }
4577
+ },
4578
+ callService: async (service, action, params) => {
4579
+ try {
4580
+ let result = null;
4581
+ if (this.config.effectHandlers?.callService) {
4582
+ result = await this.config.effectHandlers.callService(
4583
+ service,
4584
+ action,
4585
+ params
4586
+ );
4587
+ } else {
4588
+ console.warn(
4589
+ `[OrbitalRuntime] call-service not configured: ${service}.${action}`
4590
+ );
4591
+ }
4592
+ effectResults.push({
4593
+ effect: "call-service",
4594
+ action: `${service}.${action}`,
4595
+ data: result,
4596
+ success: true
4597
+ });
4598
+ return result;
4599
+ } catch (err) {
4600
+ effectResults.push({
4601
+ effect: "call-service",
4602
+ action: `${service}.${action}`,
4603
+ success: false,
4604
+ error: err instanceof Error ? err.message : String(err)
4605
+ });
4606
+ return null;
4607
+ }
4608
+ },
4609
+ fetch: async (fetchEntityType, options) => {
4610
+ try {
4611
+ let result = null;
4612
+ if (options?.id) {
4613
+ const entity = await this.persistence.getById(fetchEntityType, options.id);
4614
+ if (entity) {
4615
+ if (options?.include && options.include.length > 0) {
4616
+ await this.populateRelations([entity], fetchEntityType, options.include);
4617
+ }
4618
+ fetchedData[fetchEntityType] = [entity];
4619
+ result = entity;
4620
+ }
4621
+ } else {
4622
+ let entities = await this.persistence.list(fetchEntityType);
4623
+ if (options?.offset && options.offset > 0) {
4624
+ entities = entities.slice(options.offset);
4625
+ }
4626
+ if (options?.limit && options.limit > 0) {
4627
+ entities = entities.slice(0, options.limit);
4628
+ }
4629
+ if (options?.include && options.include.length > 0) {
4630
+ await this.populateRelations(entities, fetchEntityType, options.include);
4631
+ }
4632
+ fetchedData[fetchEntityType] = entities;
4633
+ result = entities;
4634
+ }
4635
+ if (bindingsRef && result) {
4636
+ const records = Array.isArray(result) ? result : [result];
4637
+ if (records.length > 0) {
4638
+ const merged = Object.assign([...records], records[0]);
4639
+ bindingsRef[fetchEntityType] = merged;
4640
+ if (fetchEntityType === entityType) {
4641
+ bindingsRef.entity = merged;
4642
+ }
4643
+ }
4644
+ }
4645
+ return result;
4646
+ } catch (error) {
4647
+ console.error(`[OrbitalRuntime] Fetch error for ${fetchEntityType}:`, error);
4648
+ return null;
4649
+ }
4650
+ },
4651
+ // Resource operators: ref, deref, swap, watch, atomic
4652
+ ref: async (refEntityType, options) => {
4653
+ try {
4654
+ return await handlers.fetch(refEntityType, options);
4655
+ } catch (error) {
4656
+ console.error(`[OrbitalRuntime] ref error for ${refEntityType}:`, error);
4657
+ return null;
4658
+ }
4659
+ },
4660
+ deref: async (derefEntityType, options) => {
4661
+ try {
4662
+ let result = null;
4663
+ if (options?.id) {
4664
+ const entity = await this.persistence.getById(derefEntityType, options.id);
4665
+ if (entity) {
4666
+ fetchedData[derefEntityType] = [entity];
4667
+ result = entity;
4668
+ }
4669
+ } else {
4670
+ const entities = await this.persistence.list(derefEntityType);
4671
+ fetchedData[derefEntityType] = entities;
4672
+ result = entities;
4673
+ }
4674
+ if (bindingsRef && result) {
4675
+ const records = Array.isArray(result) ? result : [result];
4676
+ if (records.length > 0) {
4677
+ const merged = Object.assign([...records], records[0]);
4678
+ bindingsRef[derefEntityType] = merged;
4679
+ if (derefEntityType === entityType) {
4680
+ bindingsRef.entity = merged;
4681
+ }
4682
+ }
4683
+ }
4684
+ effectResults.push({
4685
+ effect: "deref",
4686
+ entityType: derefEntityType,
4687
+ success: true
4688
+ });
4689
+ return result;
4690
+ } catch (error) {
4691
+ effectResults.push({
4692
+ effect: "deref",
4693
+ entityType: derefEntityType,
4694
+ success: false,
4695
+ error: error instanceof Error ? error.message : String(error)
4696
+ });
4697
+ return null;
4698
+ }
4699
+ },
4700
+ swap: async (swapEntityType, swapEntityId, transform) => {
4701
+ try {
4702
+ const current = await this.persistence.getById(swapEntityType, swapEntityId);
4703
+ if (!current) {
4704
+ effectResults.push({
4705
+ effect: "swap",
4706
+ entityType: swapEntityType,
4707
+ success: false,
4708
+ error: `Entity ${swapEntityType}/${swapEntityId} not found`
4709
+ });
4710
+ return null;
4711
+ }
4712
+ const ctx = createContextFromBindings({
4713
+ current,
4714
+ entity: entityData,
4715
+ payload
4716
+ }, false, this.config.contextExtensions);
4717
+ let newData;
4718
+ if (Array.isArray(transform)) {
4719
+ const result = evaluate(
4720
+ transform,
4721
+ ctx
4722
+ );
4723
+ if (result && typeof result === "object" && !Array.isArray(result)) {
4724
+ newData = result;
4725
+ } else {
4726
+ newData = current;
4727
+ }
4728
+ } else if (typeof transform === "object" && transform !== null) {
4729
+ newData = { ...current, ...transform };
4730
+ } else {
4731
+ effectResults.push({
4732
+ effect: "swap",
4733
+ entityType: swapEntityType,
4734
+ success: false,
4735
+ error: "swap! transform must be an S-expression or object"
4736
+ });
4737
+ return null;
4738
+ }
4739
+ await this.persistence.update(swapEntityType, swapEntityId, newData);
4740
+ effectResults.push({
4741
+ effect: "swap",
4742
+ entityType: swapEntityType,
4743
+ data: { id: swapEntityId, ...newData },
4744
+ success: true
4745
+ });
4746
+ return newData;
4747
+ } catch (error) {
4748
+ effectResults.push({
4749
+ effect: "swap",
4750
+ entityType: swapEntityType,
4751
+ success: false,
4752
+ error: error instanceof Error ? error.message : String(error)
4753
+ });
4754
+ return null;
4755
+ }
4756
+ },
4757
+ watch: (_watchEntityType, _watchOptions) => {
4758
+ if (this.config.debug) {
4759
+ console.log(`[OrbitalRuntime] watch is a no-op on server: ${_watchEntityType}`);
4760
+ }
4761
+ },
4762
+ atomic: async (atomicEffects) => {
4763
+ let atomicFailed = false;
4764
+ let atomicError = "";
4765
+ const atomicExecutor = new EffectExecutor({
4766
+ handlers,
4767
+ bindings: bindingsRef ?? {},
4768
+ context: contextRef ?? { traitName, orbitalName: registered.schema.name, state: "unknown", transition: "unknown" },
4769
+ debug: this.config.debug,
4770
+ contextExtensions: this.config.contextExtensions
4771
+ });
4772
+ for (const innerEffect of atomicEffects) {
4773
+ if (atomicFailed) break;
4774
+ try {
4775
+ await atomicExecutor.execute(innerEffect);
4776
+ } catch (err) {
4777
+ atomicFailed = true;
4778
+ atomicError = err instanceof Error ? err.message : String(err);
4779
+ }
4780
+ }
4781
+ if (atomicFailed) {
4782
+ effectResults.push({
4783
+ effect: "atomic",
4784
+ success: false,
4785
+ error: `Atomic block failed: ${atomicError}`
4786
+ });
4787
+ } else {
4788
+ effectResults.push({
4789
+ effect: "atomic",
4790
+ success: true,
4791
+ data: { innerCount: atomicEffects.length }
4792
+ });
4793
+ }
4794
+ },
4795
+ // Client-side effects - collect for forwarding to client
4796
+ renderUI: (slot, pattern, props, priority) => {
4797
+ const patternNode = pattern !== null && typeof pattern === "object" && !Array.isArray(pattern) ? pattern : null;
4798
+ const patternEntity = patternNode?.entity;
4799
+ const entityRow = patternEntity !== null && typeof patternEntity === "object" && !Array.isArray(patternEntity) ? patternEntity : null;
4800
+ const patternTypeRaw = patternNode?.["type"];
4801
+ renderLog2.debug("renderUI:push", {
4802
+ trait: traitName,
4803
+ slot,
4804
+ patternType: typeof patternTypeRaw === "string" ? patternTypeRaw : void 0,
4805
+ entityRowId: typeof entityRow?.id === "string" ? entityRow.id : void 0,
4806
+ entityIsObject: entityRow !== null
4807
+ });
4808
+ pushClientEffect(["render-ui", slot, pattern, props, priority]);
4809
+ },
4810
+ navigate: (path, params) => {
4811
+ pushClientEffect(["navigate", path, params]);
4812
+ },
4813
+ notify: (message, type) => {
4814
+ if (this.config.debug) {
4815
+ console.log(`[OrbitalRuntime] Notification (${type}): ${message}`);
4816
+ }
4817
+ pushClientEffect(["notify", message, { type }]);
4818
+ },
4819
+ log: (message, level) => {
4820
+ const logFn = level === "error" ? console.error : level === "warn" ? console.warn : console.log;
4821
+ logFn(`[OrbitalRuntime] ${message}`);
4822
+ },
4823
+ // Allow custom handlers to override
4824
+ ...this.config.effectHandlers
4825
+ };
4826
+ const state = registered.manager.getState(traitName);
4827
+ const bindings = {
4828
+ entity: entityData,
4829
+ payload,
4830
+ state: state?.currentState || "unknown",
4831
+ user
4832
+ // @user bindings from Firebase auth
4833
+ };
4834
+ const declaredDefaults = collectDeclaredConfigDefaults(
4835
+ registered.traits.find((t) => t.name === traitName)
4836
+ );
4837
+ const callSiteOverride = registered.configByTrait.get(traitName);
4838
+ if (declaredDefaults || callSiteOverride) {
4839
+ bindings.config = { ...declaredDefaults ?? {}, ...callSiteOverride ?? {} };
4840
+ }
4841
+ if (entityType) {
4842
+ bindings[entityType] = entityData;
4843
+ }
4844
+ bindingsRef = bindings;
4845
+ const context = {
4846
+ traitName,
4847
+ orbitalName: registered.schema.name,
4848
+ state: state?.currentState || "unknown",
4849
+ transition: "unknown",
4850
+ entityId
3206
4851
  };
4852
+ contextRef = context;
4853
+ const executor = new EffectExecutor({
4854
+ handlers,
4855
+ bindings,
4856
+ context,
4857
+ debug: this.config.debug,
4858
+ contextExtensions: this.config.contextExtensions
4859
+ });
4860
+ await executor.executeAll(effects);
3207
4861
  }
4862
+ // ==========================================================================
4863
+ // Relation Population
4864
+ // ==========================================================================
3208
4865
  /**
3209
- * Resolve a page reference object with optional path override.
4866
+ * Populate relation fields on entities
4867
+ *
4868
+ * For each field in `include`, find the relation field configuration and
4869
+ * fetch the related entity, attaching it to the parent entity.
4870
+ *
4871
+ * @param entities - Entities to populate
4872
+ * @param entityType - Entity type name
4873
+ * @param include - Relation field names to populate
3210
4874
  */
3211
- resolvePageRefObject(refObj, imports) {
3212
- const baseResult = this.resolvePageRefString(refObj.ref, imports);
3213
- if (!baseResult.success) {
3214
- return baseResult;
4875
+ /**
4876
+ * Validate that relation field values match their declared cardinality.
4877
+ * Called before create/update to ensure data integrity.
4878
+ */
4879
+ validateRelationCardinality(entityType, data) {
4880
+ for (const [, registered] of this.orbitals) {
4881
+ if (registered.entity.name !== entityType) continue;
4882
+ for (const field of registered.entity.fields ?? []) {
4883
+ if (field.type !== "relation") continue;
4884
+ if (field.name === void 0) continue;
4885
+ const fieldName = field.name;
4886
+ const value = data[fieldName];
4887
+ if (value === void 0 || value === null) continue;
4888
+ const cardinality = field.relation?.cardinality || "one";
4889
+ if (cardinality === "one" || cardinality === "many-to-one") {
4890
+ if (Array.isArray(value)) {
4891
+ throw new Error(
4892
+ `Cardinality violation: ${entityType}.${fieldName} has cardinality '${cardinality}' but received an array. Expected a single string ID.`
4893
+ );
4894
+ }
4895
+ } else if (cardinality === "many" || cardinality === "many-to-many" || cardinality === "one-to-many") {
4896
+ if (typeof value === "string") {
4897
+ data[fieldName] = [value];
4898
+ } else if (Array.isArray(value)) {
4899
+ const nonStrings = value.filter((v) => typeof v !== "string");
4900
+ if (nonStrings.length > 0) {
4901
+ throw new Error(
4902
+ `Cardinality violation: ${entityType}.${fieldName} has cardinality '${cardinality}' but array contains non-string values.`
4903
+ );
4904
+ }
4905
+ }
4906
+ }
4907
+ }
4908
+ break;
3215
4909
  }
3216
- const resolved = baseResult.data;
3217
- if (refObj.path) {
3218
- const originalPath = resolved.page.path;
3219
- resolved.page = {
3220
- ...resolved.page,
3221
- path: refObj.path
3222
- };
3223
- resolved.pathOverridden = true;
3224
- resolved.originalPath = originalPath;
4910
+ }
4911
+ /**
4912
+ * Enforce onDelete rules for relation fields pointing to the entity being deleted.
4913
+ * Scans all registered entities for relation fields targeting the given entity type,
4914
+ * finds records referencing the ID being deleted, and applies cascade/nullify/restrict.
4915
+ */
4916
+ async enforceOnDeleteRules(entityType, deletedId) {
4917
+ for (const [, registered] of this.orbitals) {
4918
+ const entity = registered.entity;
4919
+ const fields = entity.fields ?? [];
4920
+ for (const field of fields) {
4921
+ if (field.type !== "relation") continue;
4922
+ if (field.relation?.entity !== entityType) continue;
4923
+ if (field.name === void 0) continue;
4924
+ const fieldName = field.name;
4925
+ const onDelete = field.relation.onDelete || "restrict";
4926
+ const referringEntityType = entity.name;
4927
+ const allRecords = await this.persistence.list(referringEntityType);
4928
+ const affectedRecords = allRecords.filter((record) => {
4929
+ const fkValue = record[fieldName];
4930
+ if (typeof fkValue === "string") return fkValue === deletedId;
4931
+ if (Array.isArray(fkValue)) return fkValue.includes(deletedId);
4932
+ return false;
4933
+ });
4934
+ if (affectedRecords.length === 0) continue;
4935
+ switch (onDelete) {
4936
+ case "restrict":
4937
+ throw new Error(
4938
+ `Cannot delete ${entityType} ${deletedId}: ${affectedRecords.length} ${referringEntityType} record(s) reference it via ${field.name}. Rule: restrict.`
4939
+ );
4940
+ case "cascade":
4941
+ for (const record of affectedRecords) {
4942
+ const recordId = record.id;
4943
+ if (recordId) {
4944
+ await this.persistence.delete(referringEntityType, recordId);
4945
+ }
4946
+ }
4947
+ if (this.config.debug) {
4948
+ console.log(`[OrbitalRuntime] Cascade deleted ${affectedRecords.length} ${referringEntityType} records`);
4949
+ }
4950
+ break;
4951
+ case "nullify":
4952
+ for (const record of affectedRecords) {
4953
+ const recordId = record.id;
4954
+ if (recordId && field.name !== void 0) {
4955
+ const fieldName2 = field.name;
4956
+ const update = {};
4957
+ const fkValue = record[fieldName2];
4958
+ if (Array.isArray(fkValue)) {
4959
+ update[fieldName2] = fkValue.filter((id) => id !== deletedId);
4960
+ } else {
4961
+ update[fieldName2] = null;
4962
+ }
4963
+ await this.persistence.update(referringEntityType, recordId, update);
4964
+ }
4965
+ }
4966
+ if (this.config.debug) {
4967
+ console.log(`[OrbitalRuntime] Nullified ${field.name} on ${affectedRecords.length} ${referringEntityType} records`);
4968
+ }
4969
+ break;
4970
+ }
4971
+ }
4972
+ }
4973
+ }
4974
+ async populateRelations(entities, entityType, include, depth = 0, visited = /* @__PURE__ */ new Set()) {
4975
+ const maxDepth = 2;
4976
+ if (depth >= maxDepth || visited.has(entityType)) {
4977
+ if (this.config.debug) {
4978
+ console.log(`[OrbitalRuntime] Skipping populateRelations for ${entityType}: depth=${depth}, visited=${visited.has(entityType)}`);
4979
+ }
4980
+ return;
4981
+ }
4982
+ visited.add(entityType);
4983
+ let entityFields;
4984
+ for (const [, registered] of this.orbitals) {
4985
+ if (registered.entity.name === entityType) {
4986
+ entityFields = registered.entity.fields.filter(
4987
+ (f) => typeof f.name === "string" && f.name.length > 0
4988
+ );
4989
+ break;
4990
+ }
4991
+ }
4992
+ if (!entityFields) {
4993
+ if (this.config.debug) {
4994
+ console.warn(`[OrbitalRuntime] No entity definition found for ${entityType}`);
4995
+ }
4996
+ return;
4997
+ }
4998
+ for (const includeField of include) {
4999
+ const relationField = entityFields.find((f) => {
5000
+ if (f.type !== "relation") return false;
5001
+ return f.name === includeField || f.name === `${includeField}Id` || f.name.replace(/Id$/, "") === includeField;
5002
+ });
5003
+ if (!relationField?.relation?.entity) {
5004
+ if (this.config.debug) {
5005
+ console.warn(`[OrbitalRuntime] No relation field found for '${includeField}' on ${entityType}`);
5006
+ }
5007
+ continue;
5008
+ }
5009
+ const foreignKeyField = relationField.name;
5010
+ const relatedEntityType = relationField.relation.entity;
5011
+ const cardinality = relationField.relation.cardinality || "one";
5012
+ const foreignKeyIds = /* @__PURE__ */ new Set();
5013
+ for (const entity of entities) {
5014
+ const fkValue = entity[foreignKeyField];
5015
+ if (fkValue && typeof fkValue === "string") {
5016
+ foreignKeyIds.add(fkValue);
5017
+ } else if (Array.isArray(fkValue)) {
5018
+ for (const id of fkValue) {
5019
+ if (id && typeof id === "string") {
5020
+ foreignKeyIds.add(id);
5021
+ }
5022
+ }
5023
+ }
5024
+ }
5025
+ if (foreignKeyIds.size === 0) continue;
5026
+ const relatedEntities = /* @__PURE__ */ new Map();
5027
+ for (const fkId of foreignKeyIds) {
5028
+ try {
5029
+ const related = await this.persistence.getById(relatedEntityType, fkId);
5030
+ if (related) {
5031
+ relatedEntities.set(fkId, related);
5032
+ }
5033
+ } catch (error) {
5034
+ if (this.config.debug) {
5035
+ console.error(`[OrbitalRuntime] Error fetching related ${relatedEntityType}:`, error);
5036
+ }
5037
+ }
5038
+ }
5039
+ const populatedFieldName = includeField.endsWith("Id") ? includeField.slice(0, -2) : includeField;
5040
+ for (const entity of entities) {
5041
+ const fkValue = entity[foreignKeyField];
5042
+ if (cardinality === "one" || cardinality === "many-to-one") {
5043
+ if (typeof fkValue === "string" && relatedEntities.has(fkValue)) {
5044
+ Object.defineProperty(entity, populatedFieldName, {
5045
+ value: relatedEntities.get(fkValue),
5046
+ writable: true,
5047
+ enumerable: true,
5048
+ configurable: true
5049
+ });
5050
+ }
5051
+ } else {
5052
+ if (Array.isArray(fkValue)) {
5053
+ const fkIds = fkValue.filter((id) => typeof id === "string");
5054
+ Object.defineProperty(entity, populatedFieldName, {
5055
+ value: fkIds.map((id) => relatedEntities.get(id)).filter(Boolean),
5056
+ writable: true,
5057
+ enumerable: true,
5058
+ configurable: true
5059
+ });
5060
+ } else if (typeof fkValue === "string" && relatedEntities.has(fkValue)) {
5061
+ Object.defineProperty(entity, populatedFieldName, {
5062
+ value: [relatedEntities.get(fkValue)],
5063
+ writable: true,
5064
+ enumerable: true,
5065
+ configurable: true
5066
+ });
5067
+ }
5068
+ }
5069
+ }
5070
+ if (this.config.debug) {
5071
+ console.log(`[OrbitalRuntime] Populated '${populatedFieldName}' on ${entities.length} ${entityType} entities`);
5072
+ }
3225
5073
  }
3226
- return {
3227
- success: true,
3228
- data: resolved,
3229
- warnings: baseResult.warnings
3230
- };
3231
5074
  }
5075
+ // ==========================================================================
5076
+ // Express Router
5077
+ // ==========================================================================
3232
5078
  /**
3233
- * Find a page in an orbital by name.
5079
+ * Create Express router for orbital API endpoints
5080
+ *
5081
+ * All data access goes through trait events with guards.
5082
+ * No direct CRUD routes - use events with `fetch` effects.
5083
+ *
5084
+ * Routes:
5085
+ * - GET / - List registered orbitals
5086
+ * - GET /:orbital - Get orbital info and current states
5087
+ * - POST /:orbital/events - Send event to orbital (includes data from `fetch` effects)
3234
5088
  */
3235
- findPageInOrbital(orbital, pageName) {
3236
- const pages = orbital.pages;
3237
- if (!pages) return null;
3238
- for (const pageRef of pages) {
3239
- if (typeof pageRef !== "string" && !("ref" in pageRef)) {
3240
- const page = pageRef;
3241
- if (page.name === pageName) {
3242
- return { ...page };
5089
+ router() {
5090
+ if (!isNodeEnv()) {
5091
+ throw new Error(
5092
+ "OrbitalServerRuntime.router() is Node-only (uses Express). For in-browser use, mount <BrowserPlayground> from @almadar/ui instead."
5093
+ );
5094
+ }
5095
+ const { Router } = nodeRequire("express");
5096
+ const router = Router();
5097
+ router.get("/", (_req, res) => {
5098
+ const orbitals = Array.from(this.orbitals.entries()).map(
5099
+ ([name, reg]) => ({
5100
+ name,
5101
+ entity: reg.entity?.name,
5102
+ traits: (reg.traits || []).map((t) => t.name)
5103
+ })
5104
+ );
5105
+ res.json({ success: true, orbitals });
5106
+ });
5107
+ router.get("/:orbital", (req, res) => {
5108
+ const orbitalName = req.params.orbital;
5109
+ const registered = this.orbitals.get(orbitalName);
5110
+ if (!registered) {
5111
+ res.status(404).json({ success: false, error: "Orbital not found" });
5112
+ return;
5113
+ }
5114
+ const states = {};
5115
+ for (const [name, state] of registered.manager.getAllStates()) {
5116
+ states[name] = state.currentState;
5117
+ }
5118
+ res.json({
5119
+ success: true,
5120
+ orbital: {
5121
+ name: orbitalName,
5122
+ entity: registered.entity,
5123
+ traits: registered.traits.map((t) => ({
5124
+ name: t.name,
5125
+ currentState: states[t.name],
5126
+ states: (t.stateMachine?.states || []).map((s) => s.name),
5127
+ events: [...new Set((t.stateMachine?.transitions || []).map((tr) => tr.event))]
5128
+ }))
5129
+ }
5130
+ });
5131
+ });
5132
+ router.post(
5133
+ "/:orbital/events",
5134
+ async (req, res, next) => {
5135
+ try {
5136
+ const orbitalName = req.params.orbital;
5137
+ const firebaseUser = req.firebaseUser;
5138
+ const user = firebaseUser ? {
5139
+ ...firebaseUser,
5140
+ displayName: firebaseUser.name ?? firebaseUser.displayName
5141
+ } : void 0;
5142
+ const result = await this.processOrbitalEvent(orbitalName, {
5143
+ ...req.body,
5144
+ user
5145
+ });
5146
+ res.json(result);
5147
+ } catch (error) {
5148
+ next(error);
3243
5149
  }
3244
5150
  }
3245
- }
3246
- return null;
5151
+ );
5152
+ return router;
3247
5153
  }
5154
+ // ==========================================================================
5155
+ // Direct API (for programmatic use)
5156
+ // ==========================================================================
3248
5157
  /**
3249
- * List page names in an orbital.
5158
+ * Get the event bus for manual event emission
3250
5159
  */
3251
- listPagesInOrbital(orbital) {
3252
- const pages = orbital.pages;
3253
- if (!pages) return [];
3254
- const names = [];
3255
- for (const pageRef of pages) {
3256
- if (typeof pageRef !== "string" && !("ref" in pageRef)) {
3257
- names.push(pageRef.name);
3258
- }
3259
- }
3260
- return names;
5160
+ getEventBus() {
5161
+ return this.eventBus;
3261
5162
  }
3262
5163
  /**
3263
- * Add local traits for resolution.
5164
+ * Get state for a specific orbital/trait
3264
5165
  */
3265
- addLocalTraits(traits) {
3266
- for (const trait of traits) {
3267
- this.localTraits.set(trait.name, trait);
5166
+ getState(orbitalName, traitName) {
5167
+ const registered = this.orbitals.get(orbitalName);
5168
+ if (!registered) return void 0;
5169
+ if (traitName) {
5170
+ return registered.manager.getState(traitName);
5171
+ }
5172
+ const states = {};
5173
+ for (const [name, state] of registered.manager.getAllStates()) {
5174
+ states[name] = state;
3268
5175
  }
5176
+ return states;
3269
5177
  }
3270
5178
  /**
3271
- * Clear loader cache.
5179
+ * List registered orbitals
3272
5180
  */
3273
- clearCache() {
3274
- this.loader?.clearCache();
3275
- }
3276
- };
3277
- async function resolveSchema(schema, options) {
3278
- const resolver = new ReferenceResolver(options);
3279
- const errors = [];
3280
- const warnings = [];
3281
- const resolved = [];
3282
- for (const orbital of schema.orbitals) {
3283
- const inlineTraits = orbital.traits.filter(
3284
- (t) => typeof t !== "string" && "stateMachine" in t
3285
- );
3286
- resolver.addLocalTraits(inlineTraits);
5181
+ listOrbitals() {
5182
+ return Array.from(this.orbitals.keys());
3287
5183
  }
3288
- for (const orbital of schema.orbitals) {
3289
- const result = await resolver.resolve(orbital);
3290
- if (!result.success) {
3291
- errors.push(`Orbital "${orbital.name}": ${result.errors.join(", ")}`);
3292
- } else {
3293
- resolved.push(result.data);
3294
- warnings.push(...result.warnings.map((w) => `Orbital "${orbital.name}": ${w}`));
3295
- }
5184
+ /**
5185
+ * Check if an orbital is registered
5186
+ */
5187
+ hasOrbital(name) {
5188
+ return this.orbitals.has(name);
3296
5189
  }
3297
- if (errors.length > 0) {
3298
- return { success: false, errors };
5190
+ /**
5191
+ * Get information about active ticks
5192
+ */
5193
+ getActiveTicks() {
5194
+ return this.tickBindings.map((binding) => ({
5195
+ orbital: binding.orbitalName,
5196
+ trait: binding.traitName,
5197
+ tick: binding.tick.name,
5198
+ interval: binding.tick.interval,
5199
+ hasGuard: !!binding.tick.guard
5200
+ }));
3299
5201
  }
3300
- return { success: true, data: resolved, warnings };
5202
+ };
5203
+ function createOrbitalServerRuntime(config) {
5204
+ return new OrbitalServerRuntime(config);
3301
5205
  }
3302
-
3303
- // src/UsesIntegration.ts
3304
- async function preprocessSchema(schema, options) {
3305
- const namespaceEvents = options.namespaceEvents ?? true;
3306
- const resolveResult = await resolveSchema(schema, options);
3307
- if (!resolveResult.success) {
3308
- return { success: false, errors: resolveResult.errors };
3309
- }
3310
- const resolved = resolveResult.data;
3311
- const warnings = resolveResult.warnings;
3312
- const preprocessedOrbitals = [];
3313
- const entitySharing = {};
3314
- const eventNamespaces = {};
3315
- for (const resolvedOrbital of resolved) {
3316
- const orbitalName = resolvedOrbital.name;
3317
- const persistence = resolvedOrbital.entitySource?.persistence ?? resolvedOrbital.entity.persistence ?? "persistent";
3318
- entitySharing[orbitalName] = {
3319
- entityName: resolvedOrbital.entity.name,
3320
- persistence,
3321
- isShared: persistence !== "runtime",
3322
- sourceAlias: resolvedOrbital.entitySource?.alias,
3323
- collectionName: resolvedOrbital.entity.collection
3324
- };
3325
- eventNamespaces[orbitalName] = {};
3326
- for (const resolvedTrait of resolvedOrbital.traits) {
3327
- const traitName = resolvedTrait.trait.name;
3328
- const namespace = {
3329
- emits: {},
3330
- listens: {}
3331
- };
3332
- if (namespaceEvents && resolvedTrait.source.type === "imported") {
3333
- const emits = resolvedTrait.trait.emits ?? [];
3334
- for (const emit of emits) {
3335
- const eventName = typeof emit === "string" ? emit : emit.event;
3336
- namespace.emits[eventName] = `${orbitalName}.${traitName}.${eventName}`;
3337
- }
3338
- const listens = resolvedTrait.trait.listens ?? [];
3339
- for (const listen of listens) {
3340
- namespace.listens[listen.event] = listen.event;
3341
- }
3342
- }
3343
- eventNamespaces[orbitalName][traitName] = namespace;
3344
- }
3345
- const preprocessedOrbital = {
3346
- name: orbitalName,
3347
- description: resolvedOrbital.original.description,
3348
- visual_prompt: resolvedOrbital.original.visual_prompt,
3349
- // Resolved entity (always inline now)
3350
- entity: resolvedOrbital.entity,
3351
- // Resolved traits (inline definitions)
3352
- traits: (resolvedOrbital.traits || []).map((rt) => {
3353
- if (rt.config || rt.linkedEntity) {
3354
- return {
3355
- ref: rt.trait.name,
3356
- config: rt.config,
3357
- linkedEntity: rt.linkedEntity,
3358
- // Include the resolved trait definition for runtime
3359
- _resolved: rt.trait
3360
- };
3361
- }
3362
- return rt.trait;
3363
- }),
3364
- // Resolved pages (inline definitions with path overrides applied)
3365
- pages: resolvedOrbital.pages.map((rp) => rp.page),
3366
- // Preserve other fields
3367
- exposes: resolvedOrbital.original.exposes,
3368
- domainContext: resolvedOrbital.original.domainContext,
3369
- design: resolvedOrbital.original.design
5206
+ function parseListenSource(listener, listenerOrbital) {
5207
+ const explicit = listener.source;
5208
+ if (explicit && typeof explicit === "object") {
5209
+ return {
5210
+ bareEvent: listener.event,
5211
+ matcher: buildMatcher(explicit, listenerOrbital)
3370
5212
  };
3371
- preprocessedOrbitals.push(preprocessedOrbital);
3372
- }
3373
- const preprocessedSchema = {
3374
- ...schema,
3375
- orbitals: preprocessedOrbitals
3376
- };
3377
- return {
3378
- success: true,
3379
- data: {
3380
- schema: preprocessedSchema,
3381
- entitySharing,
3382
- eventNamespaces,
3383
- warnings
3384
- }
3385
- };
3386
- }
3387
- function getIsolatedCollectionName(orbitalName, entitySharing) {
3388
- const info = entitySharing[orbitalName];
3389
- if (!info) {
3390
- throw new Error(`Unknown orbital: ${orbitalName}`);
3391
- }
3392
- if (info.persistence === "runtime") {
3393
- return `${orbitalName}_${info.entityName}`;
3394
5213
  }
3395
- return info.collectionName || info.entityName.toLowerCase() + "s";
3396
- }
3397
- function getNamespacedEvent(orbitalName, traitName, eventName, eventNamespaces) {
3398
- const orbitalNs = eventNamespaces[orbitalName];
3399
- if (!orbitalNs) return eventName;
3400
- const traitNs = orbitalNs[traitName];
3401
- if (!traitNs) return eventName;
3402
- return traitNs.emits[eventName] || eventName;
3403
- }
3404
- function isNamespacedEvent(eventName) {
3405
- return eventName.includes(".");
3406
- }
3407
- function parseNamespacedEvent(eventName) {
3408
- const parts = eventName.split(".");
3409
- if (parts.length === 3) {
3410
- return { orbital: parts[0], trait: parts[1], event: parts[2] };
5214
+ const key = listener.event;
5215
+ const parts = key.split(".");
5216
+ if (parts.length === 1) {
5217
+ return { bareEvent: key, matcher: () => true };
3411
5218
  }
3412
5219
  if (parts.length === 2) {
3413
- return { trait: parts[0], event: parts[1] };
3414
- }
3415
- return { event: eventName };
3416
- }
3417
-
3418
- // src/PersistenceAdapter.ts
3419
- var InMemoryPersistence = class {
3420
- data = /* @__PURE__ */ new Map();
3421
- idCounter = 0;
3422
- /**
3423
- * Seed the store with pre-existing rows.
3424
- *
3425
- * Accepts either a plain `Record<entityType, EntityRow[]>` or an iterable
3426
- * of `[entityType, EntityRow[]]` entries. Rows without an `id` get one
3427
- * generated at insert time; rows with an `id` keep it (so re-seeding
3428
- * after a schema rebuild preserves identities used in render bindings).
3429
- */
3430
- seed(seedData) {
3431
- const entries = Symbol.iterator in Object(seedData) ? seedData : Object.entries(seedData);
3432
- for (const [entityType, rows] of entries) {
3433
- if (!this.data.has(entityType)) {
3434
- this.data.set(entityType, /* @__PURE__ */ new Map());
3435
- }
3436
- const collection = this.data.get(entityType);
3437
- for (const row of rows) {
3438
- const id = row.id || `${entityType}-${++this.idCounter}`;
3439
- collection.set(id, { ...row, id });
3440
- }
3441
- }
3442
- }
3443
- async create(entityType, data) {
3444
- const id = data.id || `${entityType}-${++this.idCounter}`;
3445
- if (!this.data.has(entityType)) {
3446
- this.data.set(entityType, /* @__PURE__ */ new Map());
3447
- }
3448
- this.data.get(entityType).set(id, { ...data, id });
3449
- return { id };
3450
- }
3451
- async update(entityType, id, data) {
3452
- const collection = this.data.get(entityType);
3453
- if (collection?.has(id)) {
3454
- const existing = collection.get(id);
3455
- collection.set(id, { ...existing, ...data });
5220
+ const [sourceOrStar, eventName] = parts;
5221
+ if (sourceOrStar === "*") {
5222
+ return { bareEvent: eventName, matcher: () => true };
3456
5223
  }
5224
+ return {
5225
+ bareEvent: eventName,
5226
+ matcher: buildMatcher(
5227
+ { kind: "trait", trait: sourceOrStar },
5228
+ listenerOrbital
5229
+ )
5230
+ };
3457
5231
  }
3458
- async delete(entityType, id) {
3459
- this.data.get(entityType)?.delete(id);
3460
- }
3461
- async getById(entityType, id) {
3462
- return this.data.get(entityType)?.get(id) || null;
3463
- }
3464
- async list(entityType) {
3465
- const collection = this.data.get(entityType);
3466
- return collection ? Array.from(collection.values()) : [];
3467
- }
3468
- /**
3469
- * Snapshot the entire store as a plain object (entityType → rows).
3470
- * Useful for feeding a fresh render-time binding layer with the
3471
- * current persistence view.
3472
- */
3473
- snapshot() {
3474
- const out = {};
3475
- for (const [entityType, collection] of this.data) {
3476
- out[entityType] = Array.from(collection.values());
3477
- }
3478
- return out;
5232
+ if (parts.length >= 3) {
5233
+ const eventName = parts[parts.length - 1];
5234
+ const trait = parts[parts.length - 2];
5235
+ const orbital = parts.slice(0, parts.length - 2).join(".");
5236
+ return {
5237
+ bareEvent: eventName,
5238
+ matcher: buildMatcher({ kind: "orbital", orbital, trait }, listenerOrbital)
5239
+ };
3479
5240
  }
3480
- };
5241
+ return { bareEvent: key, matcher: () => true };
5242
+ }
5243
+ function buildMatcher(src, listenerOrbital) {
5244
+ if (src.kind === "any") return () => true;
5245
+ if (src.kind === "trait") {
5246
+ const wantedTrait2 = src.trait;
5247
+ return (source) => !!source && source.orbital === listenerOrbital && source.trait === wantedTrait2;
5248
+ }
5249
+ const wantedOrbital = src.orbital;
5250
+ const wantedTrait = src.trait;
5251
+ return (source) => !!source && source.orbital === wantedOrbital && source.trait === wantedTrait;
5252
+ }
3481
5253
 
3482
- export { EffectExecutor, EventBus, HANDLER_MANIFEST, InMemoryPersistence, MockPersistenceAdapter, StateMachineManager, buildEmitsFromTraits, containsBindings, createContextFromBindings, createInitialTraitState, createLogger, createMockPersistence, createTestExecutor, createUnifiedLoader, extractBindings, findInitialState, findTransition, formatPayloadValidationError, getIsolatedCollectionName, getNamespacedEvent, interpolateProps, interpolateValue, isBrowser, isElectron, isNamespacedEvent, isNode, normalizeEventKey, parseNamespacedEvent, preprocessSchema, processEvent, validateEventPayload, validatePayloadShapes };
3483
- //# sourceMappingURL=chunk-6XUD4OHN.js.map
3484
- //# sourceMappingURL=chunk-6XUD4OHN.js.map
5254
+ export { EffectExecutor, EventBus, HANDLER_MANIFEST, InMemoryPersistence, MockPersistenceAdapter, OrbitalServerRuntime, StateMachineManager, buildEmitsFromTraits, collectDeclaredConfigDefaults, containsBindings, createContextFromBindings, createInitialTraitState, createLogger, createMockPersistence, createOrbitalServerRuntime, createTestExecutor, createUnifiedLoader, extractBindings, findInitialState, findTransition, formatPayloadValidationError, getIsolatedCollectionName, getNamespacedEvent, interpolateProps, interpolateValue, isBrowser, isElectron, isNamespacedEvent, isNode, normalizeEventKey, parseNamespacedEvent, preprocessSchema, processEvent, validateEventPayload, validatePayloadShapes };
5255
+ //# sourceMappingURL=chunk-JO2SIC3Q.js.map
5256
+ //# sourceMappingURL=chunk-JO2SIC3Q.js.map