The Runtime Loop
The runtime coordinates component interactions with application capabilities. The existing DOM bridge carries meaning and context through this five-stage loop:
1. User interacts with a UI element2. The element exposes meaning (an event type or command type, with payload)3. The boundary enriches with context (surface, feature, entity)4. The runtime emits the event or dispatches the command5. Modules observe events or handle commandsThen the loop continues, because module behavior typically updates state, which updates the UI, which becomes the next interaction.
Runtime and component integration
Section titled “Runtime and component integration”Kitsune’s next architecture phase connects an opinionated application runtime with a component pattern that agents can use, compose, and generate. The maintained library uses TypeScript/Lit; the artifact contract also allows browser-ready JavaScript components, including plain-JavaScript Lit, to load without rebuilding the application. Both follow one attachment/binding protocol. The target is that adding a conforming component to a configured host resolves its runtime connection, capability requirements, and object bindings through that protocol. The app /delivery entry now validates full envelopes, preflights host requirements before import, resolves exact retained versions, and stages replacement. All eleven supplied renderers now have registration-free participation entries; live publication transport remains separate.
That protocol includes more than event delivery. Queries provide current state; commands request changes and return outcomes; events describe interactions or committed domain facts. Components render accessible interaction and local state. Domain modules or services enforce invariants, authority, and persistence. The runtime understands these integration contracts without needing to model every domain’s meaning.
Core queries, contract discovery, and local/remote connection adapters are implemented. Version 1 defines connection ownership, readiness generations, restoration, observation reconciliation, and boundary lifecycle; core exports these contracts and scoped resume/draft readers. Local, deterministic remote, and shared conformance tests prove readiness, cleanup, generations, snapshot reconciliation, scoped cursor replay/gaps, and receipts after lost replies. Headless scoped surface restoration is implemented through explicit record/artifact/query adapters, with tests for fresh reconstruction, route/reference scope, draft conflicts, and pending recovery without synthetic saves. The initial app bridge now supplies composed host selection, declared sessions/bindings, Lit/native attachment, and silent eligible draft initialization. Browser acceptance loads an unchanged JavaScript module and reloads explicit local state. Full envelope validation, exact-version delivery, immutable registration, and staged replacement are now browser tested. All eleven supplied renderers now have participation entries; production integrations and live publication remain separate work. They should let an inserted or generated component read state, invoke declared operations, react to changes, and release its subscriptions on removal without new application-specific wiring. Quill Notes supplies note/conflict regression scenarios alongside the generic acceptance fixtures.
The five stages, with code
Section titled “The five stages, with code”Take this markup:
<kit-shell name="quill"> <kit-boundary surface="note-editor" feature="notes" entity-type="note" entity-id="42"> <kit-button meta-event="note.save-clicked" meta-command="notification.show" meta-prop-message="Save requested" meta-prop-tone="info" > Save </kit-button> </kit-boundary></kit-shell>When the user clicks Save:
Stage 1 — Interaction
Section titled “Stage 1 — Interaction”The user clicks <kit-button>. The native <button> inside fires a click. The click bubbles. Because click events are composed, they cross shadow boundaries.
Stage 2 — Meaning
Section titled “Stage 2 — Meaning”The <kit-boundary> parent has a delegated click listener. It walks the click’s composedPath() looking for the nearest element with metadata. It finds <kit-button> and reads:
meta-event="note.save-clicked"meta-command="notification.show"meta-prop-message="Save requested"(becomes payload{ message: "Save requested" })meta-prop-tone="info"(adds payload{ tone: "info" })
Stage 3 — Context
Section titled “Stage 3 — Context”The boundary computes its context by walking up through any parent boundaries:
{ surface: 'note-editor', feature: 'notes', entity: { type: 'note', id: '42' }}If there were nested boundaries, surfaces would be an ordered list.
Stage 4 — Runtime
Section titled “Stage 4 — Runtime”The boundary calls the shell’s runtime:
runtime.emit({ type: 'note.save-clicked', context: { surface: 'note-editor', feature: 'notes', entity: { type: 'note', id: '42' } }, payload: { message: 'Save requested', tone: 'info' },})
runtime.command({ type: 'notification.show', context: { surface: 'note-editor', feature: 'notes', entity: { type: 'note', id: '42' } }, payload: { message: 'Save requested', tone: 'info' },})The runtime stamps each one with id (UUID) and timestamp, and emits diagnostics.
Stage 5 — Modules
Section titled “Stage 5 — Modules”Every module subscribed to note.save-clicked observes the interaction event. Every module subscribed to * observes the event. The command handler registered for notification.show runs and (if a <kit-toast-region> exists) appends an informational toast. This example demonstrates the metadata bridge; it does not persist a note. A real save uses note.save, and the domain publishes note.updated after committing the change. A success notification should follow that outcome.
Suppose the shell installed three modules:
shell.modules = [ notificationModule(), // handles notification.show auditModule, // observes interaction events, writes to localStorage analyticsModule, // observes *, batches sends to PostHog]After one click:
- A toast appears.
- An audit entry is appended.
- An analytics event is queued.
- The button has no idea any of this happened.
Why this shape, not direct calls
Section titled “Why this shape, not direct calls”You could argue: just give the button onSave={() => { showToast(); track(); audit() }}. Why the indirection?
Three reasons:
Modules are observed, not imported. The button doesn’t know which modules exist. You can add or remove them without touching UI. Swap analytics vendors by swapping a single module file. Disable audit in dev by not installing it.
Capabilities compose without coupling. Five product systems can react to one event without conspiring. The audit module doesn’t know analytics exists. The notification module doesn’t know audit exists. Adding observability is one new module; nothing else changes.
Context is structural, not stuffed into props. The button doesn’t pass surface, feature, or entity to anything. The DOM tree carries them. If you move the button to a different boundary, its context changes automatically.
Events vs commands — why both
Section titled “Events vs commands — why both”Events are facts: X happened. Commands are requests: please do Y.
Many modules can observe one event. Exactly one handler runs for one command. Commands return a result. Events don’t.
You usually want events for what just happened (analytics, audit, observability) and commands for make something happen now (open a dialog, show a toast, save a draft, validate a form).
The same interaction can declare both. Kitsune emits the event first, then dispatches the command — so observers see the fact before the imperative behavior runs.
That first event describes the interaction, not the command’s success. Domain facts such as note.updated follow successful persistence. Queries such as note.get obtain the current snapshot, and a binding can refetch it after a relevant committed event. This query/command/event contract connects changing UI to the environment.
What you do not see in the runtime
Section titled “What you do not see in the runtime”The runtime is small on purpose. There is no:
- State container
- Router
- Data-fetching abstraction
- Permission system
- Theme manager
Each of those, if you want it, is a module. The runtime is the substrate they install onto.
The runtime in code
Section titled “The runtime in code”The whole shape:
type KitRuntime = { emit(event: KitEventInput): KitEvent on(eventType: string, handler: EventHandler): Unsubscribe
command<T>(command: KitCommandInput): Promise<CommandResult<T>> handleCommand<T>(commandType: string, handler: CommandHandler<T>): Unsubscribe
query<T>(query: KitQueryInput): Promise<QueryResult<T>> handleQuery<T>(queryType: string, handler: QueryHandler<T>): Unsubscribe discover(): KitDiscovery
install(module: KitModule): Promise<void> start(): Promise<void> stop(): Promise<void>
provide<T>(token: ProviderToken<T>, value: T): void inject<T>(token: ProviderToken<T>): T | undefined
createBoundary(options: BoundaryOptions): BoundaryHandle onDiagnostic(handler: DiagnosticHandler): Unsubscribe}This is a simplified API sketch. Query and command registrations can also carry operation descriptors. See the runtime API for the implemented contract, or read the core source.
Read next
Section titled “Read next”- Boundaries — the context graph in detail
- Events and Commands — the protocol
- Modules — how to author one
- The Metadata Protocol — how DOM meaning becomes runtime input