Spar, for agents
This file is everything an agent needs to write a Spar app. It is put together by
bun run llms from this guide, the principles, the API reference, the recipes and the source of
the showcase app, so the examples are code that is built and tested, not sketches. When this file
and the code disagree, the code is right and this file is stale: run bun run llms.
What Spar is
Spar is a UI framework for people who are all in on Effect. It is not general purpose and does not try to be. A Spar app is an Effect program front to back: components are Effects, state lives in Schema-described stores, the backend is an Effect RPC group, and one trace follows a click from the page through the backend and back. The framework sees every state change and every backend call, logs each one, and shows them in devtools that people use in the page and agents use over MCP or from a shell.
If a thing can be done two ways, Spar picks one and refuses the other, at compile time or at build time. Do not look for the escape hatch. There is not meant to be one.
Read this first
"Routes, data and no waterfalls", the part right after this guide, is the one way to load data: declare a query, ask for it in the component that needs it, pass values down as reactive props. Never fetch in a component's Effect and never wait there.
Three rules that break the build most often:
- Never write a bare
(props) => ...as an enhancer insidecompose. Wrap it:withEffectProps((props) => Effect.gen(...))when it runs an Effect,withHandlers((props) => ({ ... }))when it returns plain values. A bare one makespropsunknownin every enhancer after it (TS18046), and the errors that follow (TS2339, anEffect<unknown, unknown, unknown>that is not aHandler) all go away with the wrap. - A UI write (
UiState.set,UiState.update) is not an Effect, soEffect.all,Effect.andThen,Effect.tapandEffect.forEachdo not take it (TS2739: Type 'UiWrite' is missing the following properties from type 'Effect'). Yield it insideEffect.gen, or give a handler a list of actions:onReset: [UiState.set(props.name, ""), UiState.set(props.open, false)]. - A handler (an event prop, a
withKeyaction) takes anEffect<void>with no error and no requirement, a UI write, or a list of them. A store command (store.commands.added(payload)) already fits. Something that fails or needs a service gets handled first:Effect.catch, or provide it. When what to run depends on a value, return the action from an Effect instead of yielding it: an action (a callback prop's result, a list) cannot be yielded, and Spar runs what you return.onEnter: Effect.gen(function* () { const now = yield* props.palette.get; return props.onRun(now.active) }). Returning nothing runs nothing.
The shape of an app
src/
api.ts the RPC contract shared with the backend: groups, payloads, errors
routes.ts Router.route(path, Schema) values
SomeStore.ts Store.Tag: state Schema, actions, selectors
Some.tsx components: compose(...enhancers)((props) => <jsx />)
main.ts App.mount with the layers
server/ what runs on the server, or stands in for it
server/ the backend's handlers
A component is one expression: compose(...enhancers)((props) => <jsx />). The enhancers add
props (a store's values and commands, UI state, a query, a key press); the view comes last,
inline, and its props are inferred from them. The view never runs an Effect; an enhancer never
writes markup. Child components are written in JSX inside the view. Your own behaviour, written
inside compose, is withEffectProps((props) => effect) for props made by an Effect or
withHandlers((props) => values) for plain values. Written as a named function, give its
parameter a type: const withDraft = (props: { readonly id: string }) => Effect.succeed({ ... }).
A group of enhancers is compose(...) without a view. compose takes up to 10 enhancers; group
some with compose(...) beyond that.
If TypeScript says 'props' is of type 'unknown' or that a property you added does not exist,
an untyped inline function among the enhancers is the cause (the build says so too, with
SPAR_ENHANCER_UNTYPED): wrap it in withEffectProps or withHandlers.
Three other errors to avoid:
ForandShoware imported from"spar"like everything else.- In a
.tsxfile a generic arrow is read as a JSX tag: write<A,>(item: A) => ...with the comma, or use afunction. exactOptionalPropertyTypesis on: never passundefinedto an optional prop or attribute. Leave it out ({...(title === undefined ? {} : { title })}) or give a value (title ?? "").
For reads its item type from each. When each is an inline expression such as
Reactive.map(props.rows, (all) => all.filter(...)), TypeScript cannot infer it; compute the list
in an enhancer and pass props.shown instead.
The one way to do each thing
| You want to | Do this | Not this |
|---|---|---|
| Make a component | compose(...enhancers)((props) => <jsx />) in a .tsx file | A plain function returning JSX |
| Take props from the parent | withProps({ name: Schema }) as the first enhancer | Untyped props |
| Make props with an Effect | withEffectProps(Effect.fnUntraced(function* (props) { ... return { ... } })) | (props) => Effect.gen(...) written inline among enhancers: every enhancer after it sees props as unknown |
| Read a store | withStore(Store, (store) => ({ count: store.select.count, onAdd: store.commands.added() })) | Reading a store inside the view |
| Hold state only the screen cares about | withUiState(name, Schema, initial) | Ref, SubscriptionRef, let, a module variable |
| Add a behaviour of your own | A named function with a typed parameter, (props: { readonly id: string }) => Effect.succeed({ ... }), or compose(a, b) with no view | Copying the same enhancers into every component; an untyped inline function among enhancers |
| Use a child component | Write it in JSX in the view: <Dropdown label="Sort" onPick={pick} /> | Passing elements down as props |
| Hold data the app cares about | A Store.Tag with actions and selectors | UI state seeded from a store, a service with a Ref |
| Put a store where one component uses it | SomeStore.with(Component), then mount the result | Wiring a layer at the root for a store one screen owns |
| Share a store across the app | SomeStore.layer in the layers given to App.mount | A global |
| Change app state | store.commands.<action>(payload) | Writing to state anywhere else |
| Change UI state | UiState.set or UiState.update in a handler | Writing UI state from a command, a stream or a service |
| Show a list | <For each={props.items} by={(item) => item.id}>{(item) => <li>...</li>}</For> | .map inside JSX |
| Show a tree (a component inside itself) | const Row: View.Component<Props> = compose(...)(... <Row node={child} /> ...), a Schema.suspend Schema | Flattening the tree by hand; leaving the type off |
| Show one thing or another | <Show when={props.open}>...</Show> | A ternary inside JSX |
| Load data from the server | A Query.Tag, and withQuery(name, TheQuery, (props) => input) where it is needed | Fetching in a component and passing the result down |
| Use loaded data in an action | An Effect that reads TheQuery.read(input) and then sends a command with the result | Props or arguments that carry server data |
| Stop a second run while one is going | Run the work with FiberHandle.run(handle, effect, { onlyIfMissing: true }) | Disabling the button and hoping nothing else starts it |
| Run a flow with a wait in it | One Effect: a command, Effect.sleep or the call, a command with the result | A lock, a queue, a flag in UI state |
| Call your backend | Backend.rpc(Group, url) and call the client | fetch, axios, tRPC, a hand-rolled client |
| Call someone else's HTTP API | An Effect HttpClient call, or an HttpApi client, with Backend.layer provided | fetch, FetchHttpClient.layer |
| Send something that must not be lost | Outbox.channel(name, schema, send): saved on the device, retried with an idempotency key | Calling the server from a handler and hoping |
| Keep a store across a reload | SomeStore.layerSaved, with a KeyValueStore; other tabs that save it are followed | Writing to localStorage by hand |
| Handle a failure a component can have | View.recover(ErrorSchema, FallbackView) | catchAll that hides it; leaving it (compile error) |
| Show something while a component loads | View.pending(LoadingView) | A loading flag in UI state |
| Navigate | Router.route(path, Schema), router.link(route, params), router.go(route, params) | location.href, string paths |
| Build a form | Form.make(Schema, initial), form.field(name), form.submit or form.submitThen | UI state for each input plus hand validation |
| React to a key press | withKey("Escape", (props) => UiState.set(props.open, false)) | addEventListener |
| Run something while mounted | withEffect((props) => effect); Events.onWindow / onDocument inside it | A module-level listener |
| Focus or measure an element | const input = yield* Handle.make (an Effect, not a function), passed as a Props.Handle | document.querySelector |
| Open a dialog, menu, popover or tooltip | The ones in @spar/ui, or View.portal with Overlay | A positioned div |
| Note something that happened, for the log | Timeline.note(kind, name, detail) | console.log |
| Test a store | Provide its layer and call commands in it.effect | Mounting a page to test a reducer |
| Test a component | spar/testing: Testing.mount, then find and click by role | Reaching into nodes by class name |
What is refused, and why
Compile errors (the proofs are the files in apps/demo/forbidden, each of which must fail to
compile):
- A component from a plain function, a view without a Schema for its props, UI state, a store, a selector or route params without a Schema. Everything declared has a Schema so the devtools and agents can read it as data.
- A component whose Effect needs a service nobody provided, or can fail with an error nobody recovers, mounted anyway. Needs and failures are settled at mount.
- A backend error from the shared contract that the component does not handle.
- A reducer returning an object literal with a literal-typed field needs its return type written
(
(state: State): State => ...), or TypeScript widens the literal and the action is rejected. - UI state made in a service, written from a command, a stream or a service listener, or seeded from a store. UI state belongs to one component and changes only in its handlers.
- A component that can still fail, written as a JSX tag (
TS2604/TS2786: "cannot be used as a JSX component"). Recover its error inside it withView.recover, or mount it withView.child(component, props)from an enhancer. If its error type shows asunknown, look for the first error in that component instead: something there was not typed. - A service passed into a view, a modal outside a portal,
View.pendingplaced beforeView.recover, a route mounted with the wrong page.
Build errors (the Vite plugin stops the build and names the file, line and rule):
views-need-a-schema: JSX outsideView.make.jsx-only-in-views: JSX inside an expression or a call, such as.mapor a ternary in a hole.no-effect-run-in-views:Effect.run*in a view file.state-only-through-spar:Ref.make,SubscriptionRef.make,SynchronizedRef.make,MutableRef.make,TxRef.make,let,var. State the framework did not make is state the log cannot show and the devtools cannot rewind.backend-only-through-spar:fetch,XMLHttpRequest,WebSocket,EventSource,sendBeacon,FetchHttpClient.layer, and imports of axios, ky, got, superagent, graphql-request, openapi-fetch,@trpc/client,@apollo/client, socket.io-client. A call the framework did not make is a call missing from the trace.
The two -through-spar rules look at app source. They skip node_modules, tests, config files,
scripts/ and any server/ folder (server code is not page state).
House rules the linter holds everywhere: no loops, no mutation, no comments, no casts, type
not interface, no nested ifs.
UI state and app state
UI state is what one component remembers about its own screen: a menu is open, what was typed,
which tab is showing. It is made with UiState.make in that component's Effect, lives in one tree
by component path, and goes away when the component unmounts. Nothing else can write it.
App state is what the app knows: the agents, the notes, the cart. It lives in a store. A store is
a Schema for the state, named actions with pure reducers, and named selectors. It changes only
through store.commands.<action>(payload), and every command is an entry in the store's log.
Keep in the state only what cannot be worked out from the rest. A total, a count, a "has any",
a filtered or sorted list is a selector over the state, never a field that every reducer has to
remember to update: total: Store.selector(Schema.Int, (state: State) => state.counts.reduce((sum, c) => sum + c.count, 0)).
A second copy of a value is a value that can disagree with the first.
A store does not have to be declared at the root. SomeStore.with(Component) makes one unit of a
store and the component that uses it. Mount that unit twice and there are two stores, each with
its own state, each registered in the same root registry the devtools read. So it is still model,
view, update: one registry, one log, every change an action. It only does not look like it,
because the store and its view sit in the same file.
Flows and server data
Read the "State, events that interleave, and server data" part below before writing any store with async work. The short version:
- A store accepts every command; there are no guards. Work that takes time is an Effect on a service (or an enhancer): it sends a command, does the work, and sends a command with the result. Every command is in the log and the trace.
- Server data is a
Query.Tag. Views never fetch and props never carry server data: an Effect that needs it readsTheQuery.read(input)itself. - When only the newest run matters (a search box, a filter), run it with
FiberHandle.run(handle, effect), which stops the one before; or load it with a Query. - When a second press must not start the work again (Pay, Save, Send), run it with
FiberHandle.run(handle, effect, { onlyIfMissing: true }), which ignores the repeat. - To change one row of a long list in a reducer, return
Store.withRow(state.rows, index, row): the list rewrites that row only. - A query's
valueis null while a new input loads. To keep the last rows on screen while the next page or filter loads, show the handle'slatest; for "load more", useTheQuery.pages. - A query with no input is asked for with no argument,
yield* TheQuery.ask(). Giving it an input, such as one made from another query's value, does not compile (SPAR_NEEDLESS_WAIT). - A draft the user is typing is never overwritten with a saved or loaded value. Keep both.
- Spar reports two timing mistakes while the app runs (
SPAR_DUPLICATE_CALL,SPAR_TYPING_REPLACED). After a change, run the app and read the log fornoticedandwaitedentries; each names its fix. - A store's layer needs no
SecureRandomfrom you. A UI write (UiState.set,update) can be yielded insideEffect.gen. Give every reducer its state type:(state: State, payload) => .... - A form field's problem is shown by
TextField,SelectandCheckboxwith the rolealert.
The backend
Declare the contract once, in a file both sides import:
export class Agents extends RpcGroup.make(
Rpc.make("Describe", {
payload: { name: Schema.String },
success: Started,
error: NoRecord,
}),
Rpc.make("Start", {
payload: { name: Schema.String, idempotencyKey: Schema.String },
error: ServerUnreachable,
}),
Rpc.make("Started", { success: Schema.Array(Schema.String) }),
Rpc.make("Users", { success: Schema.Array(User) }),
Rpc.make("Lookup", { payload: { kind: Schema.String, id: Schema.String }, success: Found }),
) {}
(apps/sidebar/src/api.ts)
On the page, Backend.rpc(Agents, url) gives a typed client. Its calls are spans in the trace of
whatever caused them, the trace context travels to the backend in the request, and the backend's
spans join the same trace. Provide Backend.layer once, where the app is mounted.
Devtools
sparDevtools({ backends }) from spar/devtools/vite adds the tools on the dev server only. In
the page they give the inspector, the one log with rewind, step and fork, the trace waterfall
across page and backend, and the stores with their outbox. No collector has to run to see a
trace.
An agent gets the same over MCP (bun run --filter @spar/devtools-mcp start, then connect to
http://127.0.0.1:4188/mcp) or from a shell
(bun run --filter @spar/devtools-mcp cli call <tool> '<json>'). A useful loop when debugging:
get_timelineto see what happened, in order.stepwithby: -1to walk back; each answer has the moment and the spans it caused.diff_statesto see what an action changed in a store.fork, thenrun_actionorset_ui_stateto try something else from that point.switch_branchwithbranch: 0to return to what really happened.
Working in this repo
bun run showcasestarts the showcase app, its backend and the devtools.bun run checkis everything that must pass: format, lint, types, tests with 100% coverage, the forbidden-code proofs, the production build check.bun run benchreruns the benchmarks and rewritesdocs/benchmarks.mdanddocs/benchmarks.html.- Browser suites are moon tasks:
bun run moon run sidebar:features sidebar:walkthrough sidebar:devtools sidebar:traced demo:browser stories:play ssr:browser --concurrency 1. - Only run tools through package scripts or moon tasks.