8
Working notes
Jaroslav Beneš edited this page 2026-11-13 09:23:04 +00:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Working notes

The project's rules, how it is built, tested and released, and the lessons that became rules. Each rule carries its reason, because a rule without one is the first thing somebody "simplifies".

Rules — breaking one is a redesign, not a tweak

  1. Nothing is predefined. No server, model, endpoint or key ships in the binary. The models are the ones the server the CLI is logged in to serves, and what the CLI knows about one — its context window, efforts, capacity — is that server's word, never a table in the code.
  2. The CLI is LLeMbas-server-specific. It talks to the one LLeMbas server it is logged in to (lembas login <address>) and nothing else: there are no custom connections. ~/.config/lembas/connections.yaml holds that login and is global only — a project's .agent/config.yaml can never define or override it, otherwise a cloned repository could point it somewhere else and collect the key and the prompts. The same holds for every key in config/load.ts:GLOBAL_ONLY (hardline_extra, hardline_disable, voice, update, settings_tool, embedding, remote, library, instructions, personality_custom): a project that sets one is warned and ignored. 1.x on main differs: any number of connections the user wrote into connections.yaml, keys as {env:NAME}, {file:path} or key_cmd, and context from the config or discovery.
  3. The hardline is a floor. src/permission/hardline.ts (data: harness/permission/hardline.json) is refused in every mode, auto included. Only the global config may add (hardline_extra) or disable (hardline_disable, by id) a rule.
  4. Model output is untrusted. Tool results are data, not instructions. A project's own config, commands, agents, skills and MCP servers count only once the directory is trusted. Text from a third party — an MCP prompt, a skill, a server's instructions — goes through none of the user's conveniences: @path is expanded only in what the user typed.
  5. One thin native client, no AI SDK. openai-chat (src/provider/openai-chat.ts) to the server's /v1, producing the StreamEvents the engine reads. The quirks of llama.cpp, vLLM and friends reach the CLI through the server, and a generic SDK hides exactly them. 1.x on main has one client per dialect: openai-chat, responses, anthropic, gemini, ollama.
  6. One process, one bus. The engine emits events and knows nothing about who listens. The TUI, lembas run, the ACP agent and the tests are subscribers.
  7. No colour outside the theme. Every colour comes from src/tui/palette.ts (the palettes) and theme.ts; no hex literal anywhere else under src/tui/.
  8. Lifted code keeps its header, naming the upstream file and licence. src/tool/replace.ts is kept byte-for-byte with OpenCode (hence its @ts-nocheck) so it can be re-synced; a fix it needs lives beside it (replace-text.ts).
  9. The binary is the product. bun test cannot see what bun build --compile breaks, so every tag is preceded by bun run smoke, which drives the compiled binary — including the TUI in a real PTY at 120×40 and 80×24.
  10. The harness design is shared with LLeMbas. Tools, permissions, prompt texts, quirks, model metadata and the device link are one design, written as data in harness/ and implemented natively by both projects; LLeMbas vendors a pinned copy. A change goes into the spec first, then into both. A bug found here is searched for in LLeMbas, and the other way round. The two connect (login, /v1, /mcp, the outbound link) but never depend on each other. See Harness parity.

The shared harness, in practice

  • harness/README.md is the spec's own description. Bump harness/VERSION with the change: patch for a text or a new conformance case, minor for a new tool, rule or field, major for a rename or a removed tool.
  • A fix comes with a conformance case that fails without it. bun run harness record fills in the expect of a new case from this implementation; bun run harness record --all shows every case whose answer would change. tests/conformance.ts maps each area to the function that answers it; LLeMbas runs the same files through its Python.
  • A test keeps the tool definitions honest: the spec is the source of every tool's name, description and parameters as the model sees them, and tests/harness.test.ts keeps the zod schemas equal to it and harness/acp.md naming every _lembas method and capability the code handles or sends.
  • A version field a released client compares exactly is frozen within a major. Discovery said protocol: 1 beside protocols: [1, 2] because older clients compared protocol for equality: add a list beside it, never raise it. Across a major the refusal is the point — link protocol 3 (server and CLI 2.0) raises it on purpose, so a 1.x peer and a 2.0 one refuse each other and each says what to update.
  • Nothing in harness/ names a host, a path on a server or anybody's setup — both projects are public.

Versions, channels and LTS

X.Y.Z, and each number means one thing:

  • X — major. Big changes to the data structure, the design or core functionality. A major may break what came before; it says so and offers a way across.
  • Y — feature. New, changed or removed features with a smaller blast radius.
  • Z — patch. Bug and security fixes, and nothing else. Never a new, changed or removed feature: those always go up to the next Y or X.

LTS. A designated X.Y line keeps getting Z patches after a newer line ships. Its patches are made on a branch named after the line (1.7), because main has moved on. One exception, and only one: an LTS patch may also carry export tooling for the upgrade to the next major — nothing else that is new.

Channels. main is stable, tagged vX.Y.Z. dev is the edge, tagged vX.Y.Z-beta.N. A beta is installed by its tag, by hand; it is never offered by any update feed, so a stable installation cannot step onto one by itself.

Server and CLI move together across a major: CLI 2.x speaks to server 2.x, and each refuses the other's older major with a clear message.

Build, test, release

bun install                 # bunfig.toml's minimumReleaseAge refuses packages under 3 days old
bun test
bunx tsc --noEmit
bun run smoke               # build dist/lembas, drive it
bun run licenses --check    # the licences of everything the binary contains
bun run schema              # regenerate schema/*.json from the zod schemas
scripts/ci.sh               # all of it, plus shellcheck and gitleaks

scripts/ci.sh is the gate, here and on CI (.github/workflows/ci.yml runs it with CI=1, so a missing shellcheck or gitleaks fails instead of being skipped). .gitleaksignore holds the one false positive: the made-up credential the memory threat scanner's test must catch.

Versioning. package.json is the single source; src/version.ts is replaced at compile time. main is the stable channel, tagged vX.Y.Z, and pre-releases are vX.Y.Z-beta.N. The updater compares versions as SemVer (src/update/version.ts): git's own version sort would put v1.1.0-beta.1 above v1.1.0 and hand a stable install a pre-release.

A release, in order:

  1. CHANGELOG.md's [Unreleased] becomes [X.Y.Z] — date; package.json is bumped; one commit, files added by name.
  2. scripts/ci.sh passes.
  3. A signed annotated tag whose message is that changelog section: git tag -s vX.Y.Z --cleanup=verbatim -F notes.md. Without --cleanup=verbatim git deletes every line starting with # — every ### Fixed heading.
  4. Push, then from the tag: bun install --frozen-lockfile --os=linux --cpu=arm64 (OpenTUI's arm64 library), then bun run dist -- --sign <the release key>. dist builds the three binaries, writes SHA256SUMS — its first line # lembas X.Y.Z, signed with the rest so an old release's files cannot be served under a newer tag — signs it with ssh-keygen -Y sign in the lembas-release namespace, verifies its own signature, and says whether the key is the one built into the binary.
  5. The Release is made from the tag with the notes passed explicitly and every file SHA256SUMS lists — the three binaries, install.sh, get.sh, LICENSES.txt — plus SHA256SUMS.sig. get.sh and the updater refuse a release without a valid signature, so a Release missing its assets or its signature breaks every install and every update from then on.
  6. The public GitHub repository's Release is made by .github/workflows/release.yml: it waits for the canonical release's files, checks them exactly as get.sh would (signature, version line, every checksum), and publishes the same files and notes; a -beta.N tag becomes a prerelease. Nothing is built there.

Never gate a commit on a pipeline. bun test | tail -3 && git commit commits when tail succeeds. Take the run's status on its own: bun test > log; rc=$?.

Defaults, and why

  • MCP tools ask, whatever the server says about them, unless a permission: rule allows them. A read-only hint only changes the class (plan mode then asks instead of refusing).
  • A local MCP server gets a minimal environment, not the shell's. A server that needs a token names it under env:.
  • A server's instructions enter the system prompt only if the threat scan finds nothing. MCP prompts are commands the user runs, not tools.
  • memory is allowed without asking; skill_manage asks. Memory is capped, scanned and small; a skill is instructions later sessions will follow, so it is approved like an edit.
  • A long skill description is warned about, not refused — small models loop on a refusal.
  • Subagents get the skills list, not memory, custom instructions or the personality: those are the main agent's business.
  • Background work is one registry. A command in the background and a subagent in the background are both jobs of the session that started them (src/jobs/): one id space, one list, one wake. A job never belongs to the process: job_list in one session must not see another's, and a finished job's note goes to the session that owns it — stored for it when that session is not open. The lifecycle is harness/loop.json jobs.
  • A note from the harness always carries its mark (LOOP.jobs.marks), said rather than hidden, and starts a turn through Turns.prompt with machine: true when nothing runs — never engine.prompt directly, which would skip the task events the web UI tracks turns by. The idle wake once sent a bare user message to whatever session was open; the model took it as the person's words, and a /new in between sent it to the wrong session.
  • A command's timeout is when it stops blocking, not when it dies. At the timeout the running child is handed to the job registry with its buffer; only Esc, job_stop and the session's end kill it. A !cmd from the prompt passes timeline: false: it has no owner to tell.
  • What you say is a draft by default (voice.submit: draft), and voice is global only, so a cloned repository cannot point speech and its key elsewhere.
  • The step ceiling is a runaway backstop, not a budget. Budgets (harness/loop.json) are unset by default.
  • Nothing already sent to the model is rewritten. Tool results are kept short when they are inserted (session/results.ts, harness context.md): an id on each, a stub for an unchanged re-read or a repeated listing, head and tail of a long output, a diff for an edit — and recall shows any of them in full. Changing earlier context throws the server's prompt cache away from that point, which is why compaction.prune (cutting old outputs in place) is gone; compaction is the one deliberate break. An old config's prune is dropped with a warning.
  • Memory is the server's when the login's token may reach it: the memory tool goes to the server's /mcp, and the memory in the prompt is the resource lembas://memory, read once as a session starts. Notes, knowledge bases and skills stay on this machine.
  • 1.x on main: Gemini and Ollama are untested against real servers — written from the API references, tests/gemini-ollama.test.ts proves the shapes, not acceptance. 2.0 has neither client.

Lessons that are rules

Permissions: judge the thing that happens

  • A permission is about what the call does; when the program cannot see that, it asks. Every check that failed in review looked at a description of the action: the command's arguments but not its input redirection (cat < .env); a link's name but not its target; a URL's host but not its redirects or what the name resolved to; the mode as typed, not as applied; a release's tag, not the version inside its signed list.
  • Paths are judged as real paths. resolve does not follow symlinks; a repository shipping docs -> ~/.ssh would get reads there as in-project. realPath (the longest existing prefix, links followed — dangling ones by hand) is used for every path; protected paths are checked on both spellings.
  • A written deny is matched against the plain spelling too. sudo -u x git push, env A=1 git push, timeout 5 …, sh -c "…", /usr/bin/git push all walked past git push *: deny until each simple command was also looked up with wrappers, quotes and program paths taken off. Only the deny is taken from the plain spelling — an allow there would loosen (sudo git status is not git status).
  • A rule right per command can be wrong per line. "Always" on curl x | sh stored curl * and sh, so the next curl anything | sh ran unasked. No "always" is offered for a line where one command runs others (exact_only in harness/permission/arity.json; conformance always.json).
  • Read an allowed command's options, not its purpose. rg --pre runs a program, git diff --output writes anywhere, git branch -v -D deletes, tree -o writes. Ask rules follow the allows for those.
  • Edit mode's waiver needs paths. A write-class call that names no file (skill_manage, a read-only MCP tool) is not a file edit.
  • The project's rules come after the user's, and a project cannot loosen a global rule (last match would otherwise let a cloned repository do it).
  • Fail closed where a check cannot be done — including get.sh, which never falls back to an unsigned build.

Providers and streams

  • An SSE stream's last line may have no newline, and an error may be a bare {"error":…} line inside a 200. sse.ts processes an unterminated last line like any other and surfaces every error shape seen in the wild.
  • Never re-serialise a recorded stream. Fixtures are sanitised by in-place substitution only, byte-exact otherwise — a re-serialiser once added the very newline whose absence was the bug. Streamed strings are reassembled across frames before paths are replaced, then spread back over the same frames.
  • Only non-whitespace counts as output for the retry-before-output rule.
  • A frame with type: "server_error" or an internal… code is a 500 and gets the usual retry (a proxy unloading the model mid-reply says so this way).
  • A broken tool call must not end the session. Arguments that are not a JSON object go back into the history as {}; a call cut off at the output limit (finish: length, kept by the client) is not run, and the model is told to split it. Otherwise the server re-parses the broken arguments on every request.
  • The context meter leaves out thinking the dialect does not send back.
  • Reasoning arrives as reasoning_content, reasoning or inline <think> tags; all are read. A thinking model that ends with everything in the reasoning channel and no content is asked once for the reply.
  • A model told "carry on" retries a refused edit indefinitely. Tell it up front that nobody can approve, and withdraw the tool on a final refusal.
  • Small models drop the leading / of absolute paths (recovered only inside the project) and search code text as a regex (grep offers literal: true).
  • Compaction puts the prompt that started the turn back verbatim after an automatic summary, at most twice a turn; a summary alone loses the task. The summary also carries a table of the files read, so the model knows what to recall or read again.
  • A stub only ever points at a result still in the conversation. After a compaction, an /undo or a rewind the earlier read is gone, so the file is read in full again.
  • Bun's fetch does not use the OS certificate store unless run with --use-system-ca; the build passes it, and tls.ca covers the rest.
  • Bun.spawn without env passes the environment as it was at startup. Pass the live environment explicitly.
  • Bun decodes text dropping a UTF-8 BOM. Files are read with ignoreBOM: true so an edit does not strip one. Lifted code carries its runtime's assumptions: Bun is not Node.

The TUI

  • Solid props are live getters. A dialog that cleared its own spec and then called spec.onSelect threw inside OpenTUI's key handler, which logs and carries on — a picker that closed without choosing. Take the callback before closing; test dialogs inside the real Root.
  • One terminal-size subscription, in Root, passed through context — one per component leaked listeners until Node printed a warning across the screen.
  • Test the state a program leaves behind, not only what it shows. A throw after OpenTUI turned on mouse reporting left the shell receiving every mouse move as input. runTui destroys the renderer before printing any fatal error, and the smoke test reads the terminal's modes after it quits.
  • No model, or an unreadable config, is a screen with a retry, never an exception.
  • Markdown inside a <scrollbox> needs scrollX={false}, and is parsed off the main thread — wait for its text, not its box.
  • OpenTUI's truncate elides the middle — right for a path, wrong for a sentence.
  • Emoji with a variation selector are one or two columns depending on the terminal; plain emoji are two everywhere. icons: plain exists for fonts without emoji.
  • Streamed text is flushed at about 30 fps; re-parsing markdown per token stutters on a small machine.
  • A markdown box eats [t1] [ ] as link syntax. The plan card draws its lines as plain coloured text (plancard.tsx planLines), not through the markdown renderer, or the ids and marks vanish.
  • Offsets in the textarea are columns, not string indices (a CJK character or an emoji is two). Never slice plainText by cursorOffset: take text with getTextRange(from, to).
  • setText drops every extmark. A paste placeholder is an extmark, so anything that changes part of the box inserts or replaces a selection (setSelection + insertText); what must put the whole box back uses Placeholders.snapshot / restore (tui/paste.ts).
  • Where an insert went is read off the cursor after it, never before: with a selection the text goes in at the selection's start, wherever the cursor was. An extmark placed from the cursor-before landed on the wrong range and the placeholder's content was lost.
  • OpenTUI's input parser hands an unfinished escape sequence on as typed keys after about 20 ms. A long terminal reply that stalls (kitty's clipboard over SSH) loses that packet and would type it into the box: hold the box's input while such a reply is read, and treat any input during it as a lost packet.
  • A tool call has one title (session/display.ts callTitle, harness display.md), made when the call starts and carried on the tool_start event. Nothing else derives one: a second function is how the transcript, the progress line and the link came to say three things.
  • The default theme is Moria (since 1.3.0), the look the web UI, the desktop app and the website share; lembas, shire and the rest stay selectable.

Code and data

  • A field nothing reads is a promise. A store method with no caller, a config key accepted by the schema and read by nothing, a status-bar slot never filled — grep for the reader before shipping it, and test the stored row, not a screen with a fallback.
  • Validate after substitution. Files are validated before {env:}/{file:} are filled (so one entry's missing variable cannot stop the others); a URL field therefore accepts a placeholder, and what it becomes is checked after, turning off only the entry that is still wrong.
  • String.replaceAll reads $&, $$, $` and $' in the replacement. Escape $, or pass a function.
  • \w is ASCII in JavaScript. Patterns ported from Python use \p{L} with the u flag, or one accented word hides an injection.
  • A spec text's variables go through prompt/fill.ts, which drops a bullet whose variable is empty and throws on one that was never filled; fill() in assemble.ts is only for the CLI's own task texts. The harness test asserts no {{ and no empty bullet in the assembled prompt in every mode.
  • The plan is the engine's, not the tool's. plan_write and plan_update go through ToolContext.plan; the engine persists it in the session store's meta on every change and loads it on resume. A tool that kept its own copy would lose it on /new and on a restart, which is exactly what the todo list did.
  • A permission key may cover several tools: plan is plan_write and plan_update together, and a rule written for the former todo key is read as plan (PERMISSION_ALIASES, applied to the spec's defaults too).
  • bun run harness record pretty-prints every conformance file it touches. On a compact file that is a whole-file diff; check git diff --stat after a record and restore what did not change in substance.
  • zod 4's z.record(z.enum(…)) is exhaustive; a partial map is z.partialRecord.
  • Messages stored in one millisecond tie on time; order by id as well.
  • An rc wrapper replaces the shell's own startup order. The shell integration (src/acp/shellrc.ts) is tested by running real bash, zsh and fish against the startup files people really have. In bash, PS0 can set a variable in the shell itself only through an arithmetic array subscript.

Testing conventions

  • bun test runs unit tests and end-to-end tests against tests/fake-provider.ts, a scripted endpoint. Recorded real streams replay through the same path. tests/tui/ drives the real Root.
  • Bun's toMatchObject with asymmetric matchers writes the matchers into the object — use plain toMatch / toContain on an object that is looked at again.
  • The smoke test (scripts/smoke.ts, scripts/drive.ts) starts the compiled binary on a fresh home: first run, prompt, approval, answer and quit in a PTY at both sizes, nothing wider than the terminal, the status bar on the last row, and the terminal's modes restored.
  • MCP and OAuth are tested against the SDK's own servers, including its example OAuth server, so the whole sign-in runs without a public service.
  • Several suites may share a small machine. The per-test timeout is 20 s — passed as bun test --timeout 20000 by package.json and scripts/ci.sh, because Bun 1.4 does not read bunfig.toml's [test] timeout (found 2026-10-20: every test ran at Bun's 5 s default, and under load the ones near that edge timed out while their bodies kept running against the next test's fixtures). A test that must wait longer says so with its own timeout argument.