underlay v0.3.0 · preview

Guide

How the CLI works: your home, script folders, what is kept on your machine, scripts and imports, versions, pull and push, the terminal UI, and agents. For every command and option, see Commands.

Home and script folders

Your collections live in your home, ~/Underlay by default. It is yours and isn't shared: the collections you follow, their schemas, the files you add, and your own scripts. underlay init makes it.

collections/A folder for each collection at <server>/<org>/<name>/, named by its authority: collection.toml (its id, authority, and what is kept), README.md, and a JSON Schema for each type in schemas/. Schemas you edit here go up with the next version.
scripts/Your own scripts and views, and saved imports in scripts/imports/.
inputs/Files you add, rows an import held back, and choices a view saves.
AGENTS.mdHow your home works, for an agent working in it.
.underlay/Run history and locks.

Scripts you share live in script folders: a git repo of scripts with its own dependencies, such as one your team keeps. Add one, and its scripts appear beside yours, named after the folder (team/monthly). Running underlay inside a folder that has an underlay.toml picks it up too. A script folder has no collections or schemas: each person pulls those into their own home.

underlay sources add ~/code/team-scripts --name team
underlay sources              # home, and each script folder

A script folder's underlay.toml is optional and says only what belongs to its code:

scripts = ["sources"]          # where to look (default: the whole folder)
authority = "underlay.org"     # for collections its scripts name as org/name

[runners]                      # how to run a file, when the defaults don't fit
".ts" = "pnpm exec tsx"

[env]                          # secrets its scripts need: names only
need = ["ORCID_TOKEN"]

Each person keeps a secret's value with underlay secret set ORCID_TOKEN (in the keychain on a Mac) or in their environment. A run that lacks one stops before it starts.

What is kept on your machine

Records are not kept in your home. They live in the store, ~/.local/share/underlay by default, with versions not yet pushed and run logs. A record file is kept once however many collections name it.

underlay add keeps everything: a collection's records and its assets (the PDFs, images, and other bytes its records attach, at assets/ in the store). With --keep records, its records but not its assets; with --keep none, only its schemas and history, and records are fetched from its authority when something reads them; --keep some --types A,B keeps some types. underlay keep (with --records or --types) and underlay forget change this later, and k in the terminal UI asks before it deletes anything here. underlay remove takes a collection off your machine altogether; it stays on its server.

underlay asset add <collection> <file> adds a local file as an asset: a version that names it, and its bytes are sent before that version on underlay push. underlay asset <collection> lists a collection's assets and which are here.

A collection with everything here has no mark.

◐Some of it is here: its records but not its assets, or some types
○Its schemas and history only
↑2Two versions here that are not pushed
↓1One version on the authority not yet pulled (in underlay status, with --fetch)
newNot on its authority yet: the first push creates it
▶A script is writing it now

Settings

Your settings are in ~/.config/underlay/config.toml: where your home is, the store, your remotes, your script folders, where keys are kept. underlay config lists every setting and whether it is set. UNDERLAY_HOME points at another home for one command.

underlay config
underlay config set defaults.remote underlay.org
underlay config set store.path /Volumes/Data/underlay

The woodpecker from the Underlay logo is drawn in the terminal on the keys screen, beside a long push or pull, and after underlay init. It is never drawn when output goes to a file or another program, or when NO_COLOR is set. underlay config set mascot off turns it off.

A store rule sends some collections' records somewhere else, or sets how much of them is kept. The most specific match wins.

underlay config rule add 'underlay.org/your-org/*' --keep all
underlay config rule add 'underlay.org/your-org/private' --path ~/Private/underlay

Scripts

A script is a .ts, .js, .mjs, .py, or .sh file, or any executable, in your home's scripts/ or a script folder. Its first lines say what it does; files without them, such as helpers, aren't scripts. It runs in its own folder, so its dependencies resolve. When it runs, UNDERLAY_OUT is a folder to write to: one NDJSON file for each collection, at <server>/<org>/<name>.ndjson, one record a line. UNDERLAY_HOME is your home, and underlay schema <collection> prints a collection's schemas for a script that needs them.

// ~/Underlay/scripts/scratch.mjs
// underlay: writes underlay.org/your-org/scratch
import { mkdirSync, writeFileSync } from 'node:fs'

const dir = process.env.UNDERLAY_OUT + '/underlay.org/your-org'
mkdirSync(dir, { recursive: true })

const records = [
  { id: 'a', type: 'Thing', data: { name: 'First' } },
  { id: 'b', type: 'Thing', data: { name: 'Second' } },
]
writeFileSync(dir + '/scratch.ndjson', records.map((r) => JSON.stringify(r)).join('\n'))

Assets

A script attaches an asset (a PDF, an image) by writing its bytes into $UNDERLAY_OUT/assets/, under any name and folder, and attaching it to a record with {"$asset": "ul:asset:<hex>"}, the SHA-256 of the bytes in lowercase hex. Give it a mediaType (application/pdf) and a fileName (paper.pdf) too: the collection serves the asset as that type, and readers save it under that name. Any other member is yours. After the run, each asset is stored here once, named in the version, and sent before that version on push. A field that holds an asset has the schema {"type": "object", "required": ["$asset"]}. A record attaching an asset the run didn't write, and that isn't here already, stops the save and names the record, and so does a record with a $file.

const sha = createHash('sha256').update(pdf).digest('hex')
writeFileSync(`${process.env.UNDERLAY_OUT}/assets/${sha}.pdf`, pdf)
record.data.pdf = { $asset: `ul:asset:${sha}`, mediaType: 'application/pdf', fileName: 'paper.pdf' }

To point at an exact record elsewhere (where a record came from, what it cites), write a link: {"$link": "ul:rec:<hash>", "collection": "<id>", "type": "...", "id": "..."}, with the record's hash and its collection's id. A $link that isn't a ul: id (a bare hash, say), or a $asset that isn't a ul:asset: id, stops the save and names the record. See Links.

What a script declares

writesThe collections it writes, as server/org/name or org/name.
checkIt writes nothing: it passes or fails.
view, servesA view: an .html page, or a script that serves a page (see Views).
readsCollections it reads, noted with each run.
takesThe files it takes, as a pattern such as *.xlsx. Files you add offer the scripts that take them.
modeset (the default) or additions; see below.
typesWith set, the types the script writes the whole of.

Each goes on a line of its own, as // underlay: mode additions (or with # in Python and shell, and <!-- --> in HTML), or several on one line with commas: // underlay: check, reads org/units. They can also go in a .toml file beside the script with the same name, which may set run, the command that runs it.

Runs

underlay run scratch [args]
underlay runs                 # every run, newest first
underlay runs <id> --follow   # one run's output
  • A script writes the whole collection. When it exits, the CLI compares what it wrote with the newest version and saves a new version with what changed. Records it left out are removed. With mode additions, its output is additions and changes only; a record it writes as {"id", "type", "removed": true} is removed.
  • Every record is checked against its type's schema before the version is saved, as the server will check it. A script that exits non-zero saves nothing.
  • A run records the script's git commit, the files it read, its output, and the versions it saved. Two runs never write one collection at once.
  • A collection a script writes that isn't here yet keeps its records with the run. underlay new <collection> --from-run <id>, or n on the run in the terminal UI, makes it from them: its schemas drafted from the records, and the records as its first version.
  • A run started from the terminal UI keeps running when you close it. underlay pause, resume, and cancel take a run's id.

Imports

A spreadsheet, CSV, TSV, JSON, Parquet, or SQLite file can be imported without a script. underlay add-file copies it into your home's inputs/, shows its sheets and columns, and finds the collection whose ids it shares.

underlay add-file ~/Downloads/roster.xlsx
underlay import inputs/roster.xlsx --collection underlay.org/your-org/staff \
    --type Person --id staff_id
  • underlay import saves its options as scripts/imports/roster.toml in your home and runs it. Next month, underlay run imports/roster inputs/roster-2026-12.xlsx. Move the file into a script folder to share it.
  • Columns are matched to the schema's fields by name. Columns that look personal, such as email or phone, are left out unless you pass --personal; --column name=field maps one by hand.
  • Rows without an id are held in inputs/<import>-held.ndjson rather than guessed at, and nothing is removed while rows are held. Choices for them go in inputs/<import>-decisions.json, which the next run reads.
  • underlay eject turns an import into a script you can edit, with its options written in. underlay read prints a file's rows as NDJSON.

In the terminal UI, a file whose ids match nothing here offers Make a new collection from this file (n), with its name and type taken from the file. When the type has no schema yet, the file screen drafts one from the columns: a field for each column but the id, its type guessed from every value, and required where no row leaves it blank. Columns that look personal start left out. space keeps or leaves out a column, ←→ changes its type, e opens the draft as JSON, A hands it to an agent to shape, and enter saves it to the collection's schemas/ and imports.

Views

A view is a page for looking at results, kept beside the scripts it goes with. An .html file with <!-- underlay: view --> opens in your browser as it is. A script that declares serves is given a free port as PORT; the CLI waits until it listens, opens it, and stops it when you are done. A view never writes records: it saves choices to a file in inputs/ that a script reads on its next run.

underlay view held-rows      # opens it; Ctrl-C stops one that serves

Versions

A version is saved on your machine by a run, an import, or underlay save. It is a real version of the collection, with its number and id, but nobody else sees it until you push it.

underlay log scratch      # versions, and which are pushed
underlay diff scratch     # what a push would send
underlay undo scratch     # drop the newest version not yet pushed
underlay squash scratch   # merge versions not yet pushed into one
underlay reword scratch "A better message"

Squash is useful before a push: the states in between are never uploaded. underlay save saves a version by hand, from schema edits or an NDJSON file, and underlay export prints a version's records as NDJSON.

Pull

underlay pull             # every collection, or name some

Pull follows each collection's authority and brings in versions others pushed, with the files of what you keep. Your versions not yet pushed are rebuilt on top of them and renumbered.

Push

underlay push             # every collection, or name some
  • Push lists each collection and version it will send, and checks that nothing newer is on the authority. A public collection, or one that would lose many records, needs push typed out. Otherwise y is enough, and --yes answers it.
  • Versions arrive as you saved them: the same numbers, contents, and ids, so a version you cite from your machine is the version on the server. The server signs a receipt for each one (Push and pull).
  • If someone pushed first, the server refuses and your versions are rebuilt on top of theirs, renumbered, and shown to you again before you push.
  • A collection made with underlay new org/name is created on its authority by its first push. One made with a name alone stays on this machine until underlay publish gives it a server; its first push then sends the versions made here, unchanged.

Terminal UI

Plain underlay in a terminal opens it on your home and script folders. It calls the same code as the commands and watches those folders, so what a command, another terminal, or an agent does shows up as it happens.

The terminal UI: a collection
The terminal UI, drawn by this version of the CLI when the site was built. The organizations in it are made up.
  • Servers (s): the servers you can use, whether you are signed in, and how many of your organizations' collections there are on your machine.
  • Collections on this machine (c): your collections, by authority and organization, with what is kept and how each stands against its authority. The UI opens here, and esc comes back here.
  • Scripts and views (r): each script with its last result, and your views, under the name of the folder they come from. v opens a view.
  • The main pane shows what is selected: a collection (Overview, Records, Schemas, Assets, Versions, where choosing a version shows what it changed), a script's output as it runs, a finished run, the push screen, a remote's list of collections to add, or a file you added.
  • Settings (,) and Keys (?) take the whole window. Settings lists every setting by section, with what the selected one does beside it: enter edits it, or steps through its choices; d puts it back to its default; o opens config.toml.

Keys

The keys for the screen you are on are along the bottom, and ? shows them all. Click a card, row, tab, or key to use it, and the wheel scrolls what is under the pointer. To select text, hold Option (iTerm) or Shift.

cCollections on this machine: where you start, and where esc takes you back to
sServers
rScripts and views
↑ ↓Move through the card that has the keys
tabMove between that card and the main pane
← →Switch a collection’s tabs
↑ ↓ (in the pane)Scroll, or choose a version, record, or asset; enter opens a record or an asset, w saves an asset to Downloads
RRun the selected script, or one that writes the selected collection
X, space, RWhile a script runs: X cancels it, space pauses or resumes it, and R restarts it
uUndo the newest version not yet pushed, or a finished run’s versions
lPull the selected collection
kKeep everything, data without assets, or metadata only; asks before deleting anything here
p, PPush the selected collection; push every collection
DRemove the selected collection from this machine; it asks first
+A new collection (Servers, then enter on one, adds one from it)
fAdd a file (or drop one on the terminal)
hEvery run
,Settings
?Every key, with the ones for where you are lit
a, ACopy a prompt for an agent; send it to the handoff command
oOpen the collection on its server
x xQuit (the first x says so)

Agents

An agent works in your home or a script folder with the same commands you use, and reads AGENTS.md to learn how. underlay status --json gives it the state of everything. Agents save versions; pushing is yours to do.

To hand work to an agent from the terminal UI, a copies a prompt about what you are looking at, and A sends it to the command set in handoff.send, which takes the prompt on its standard input.

underlay config set handoff.send 'your-agent-command -'