← All Labs Lab 28 / 28 A beat lens in the chat sidebar
Lab 28 · Tools & UI · Learned

A beat lens in the chat sidebar

The chat example's Beats tab asks lens questions about a finished beat on one tool-less fork per beat, while the main chat keeps running; the lens never reaches a tool or changes the main transcript, and a running beat is refused.

Learned Needs: tool-calling Smoke: pnpm exec tsx examples/28-beat-lens-in-chat/run.ts --smoke Run: pnpm exec tsx examples/28-beat-lens-in-chat/run.ts Source: examples/28-beat-lens-in-chat

Lab 27 showed you can ask questions about a session on a fork made at a beat's answer with tools: []. This lab puts that in the chat example people actually use. The sidebar gets a second tab, Beats, next to Connected sources:

  • a list of the conversation's beats (one user message and everything the agent does until it waits again), each with how many questions it has had;
  • four lens chips for the selected beat: Intent, Is it over?, Decisions, Still open. A chip is disabled once asked;
  • the questions and answers so far, with "answering…" while the fork works, and a box for your own question;
  • a footer like fork #54 at answer #45 · tools: none.

The first question on a beat creates its fork; follow-ups go to the same fork, so they build on earlier answers. A running beat can't be asked about until the agent waits again, but earlier beats can, and their answers arrive while the main turn runs. Clicking a beat highlights it in the chat.

zsh
pnpm exec tsx examples/28-beat-lens-in-chat/run.ts            # agent :7782 + chat UI :7780, offline faux model, temp git workspace
pnpm exec tsx examples/28-beat-lens-in-chat/run.ts --smoke    # PASS/FAIL lines, exit 1 on any failure
MYCEL_URL=https://mycel.thefocus.ai pnpm exec tsx examples/28-beat-lens-in-chat/run.ts \
  --model mycel:deepseek/deepseek-v4.1-flash

The default mode makes a temp git init workspace and starts the _chat agent with --workspace (a coding agent with read, write, edit, bash) and --data (SQLite), plus the chat page. --delay <ms> slows the faux model's answer after each tool call so you can watch a beat run; --keep keeps the temp root. Open the page, send "Add a slugify(text) function … Write src/slug.ts.", then open Beats.

The same tab appears on any _chat agent: pnpm tsx examples/_chat/agent.ts --workspace ./some-repo --data ./.chat-data.

How it fits together

  • _chat/beats.ts lists beats from the conversation's entries and asks questions. ask checks the beat is finished, claims the beat's lens before any await (so two quick clicks make one fork), forks with habitat.fork(answerEntryId, { from, tools: [], instructions }), and runs habitat.ask on the fork in the background.
  • _chat/agent.ts adds state.beats (and state.beatsError if listing fails), state.ui.beats.lenses, and POST /api/beats/ask → 202 { forkId }. It records the main turn in flight (beatsBefore, prompt), so the right beat shows as running even before its user entry is committed.
  • _chat/ui/index.html shows the tab only when state.beats exists and wraps each beat's messages in the stream in a group it can highlight.

Checks

The offline model writes a file on every main beat. On questions it is adversarial: it calls write whenever write is offered, and answers as a reviewer only when it has no tools.

CheckWhat it proves
beats-listedAfter two beats, state.beats has both prompts, each beat's answer id equals the newest assistant entry read from the store after that turn, and each beat has its one write.
lens-answer-arrivesPOST /api/beats/ask returns 202 with a fork id, and the answer lands in state.beats[0].lens.qa, about beat 1 and not beat 2.
follow-up-reuses-forkA free question on the same beat goes to the same fork and its request carries the first question. Asking the same lens twice is refused.
lens-never-reaches-a-toolEvery lens request offered no tools and the model called none; the workspace's git state is unchanged.
running-beat-refusedWhile beat 3 is held after its tool call, it is running, and asking about it returns 400 "still running" and leaves no lens.
earlier-beat-askable-while-runningA lens on beat 2 is answered, on its own fork, while beat 3 is still running.
main-continues-unchangedstate.messages is identical across the lens questions, beat 3's requests carry no lens question, and no lens-leak.txt exists.

All requests go through the chat page's /api/* proxy, as the browser sends them.

Break it

In _chat/beats.ts, drop tools: [] from the habitat.fork(at, …) call. The fork keeps the parent's coding tools, the adversarial model writes lens-leak.txt, and four checks fail (lens-answer-arrives, lens-never-reaches-a-tool, earlier-beat-askable-while-running, main-continues-unchanged); the smoke exits 1.

Files

  • run.ts: the faux model, the temp workspace, the default mode and the smoke.
  • PROMPT.md: the prompt to rebuild this lab.
  • FINDINGS.md: what we learned.

Prompt

Coding-Agent Prompt · Ready to Paste
Build Lab 28, `examples/28-beat-lens-in-chat/`, and the Beats tab it tests in the shared chat example (`examples/_chat/`). The lab tests one thing: from the chat page's sidebar, lens questions about a finished beat run on one tool-less fork per beat while the main chat keeps working; they never reach a tool, never change the main transcript, and a running beat is refused.

Read first: Lab 27 (`examples/27-side-questions-on-a-fork/`: the fork mechanism and its adversarial faux model), `DurableHabitat.fork` and its `tools` option in `packages/habitat/src/durable/durable-habitat.ts`, `examples/_chat/agent.ts` (host API, `/api/chat`, `state()`), `examples/_chat/ui/index.html` (sidebar, drawer under 900px, stream rendering, MCP Apps host), and `examples/14-two-way-mcp-app/smoke.ts` (faux provider on `getPiModels()`, checks through the chat UI proxy).

1. `examples/_chat/beats.ts`: `createBeatLens(habitat)` with `beats(conv, running?)` and `ask(conv, running, answerEntryId, { lens? , question? })`. A beat is a user entry and everything until the next one; its answer is its latest assistant text entry, dropped if a tool result or a failed call comes after it (a running beat keeps it as a provisional id). Four lenses: Intent, Is it over?, Decisions, Still open. `ask` refuses running beats, unknown ids, a lens already asked, and a second question while one is answering; it claims the beat's lens before any await, forks with `habitat.fork(at, { from: conv, tools: [], instructions })` on the first question, and runs `habitat.ask` on the fork in the background. Page entries; no `any`.
2. `examples/_chat/agent.ts`: `--workspace <dir>` (env "local" + ownedRepoRoot) and `--data <dir>`; record the main turn in flight (beats before it, its prompt) so the right beat shows running before its entry commits; `state.beats`, `state.beatsError`, `state.ui.beats.lenses`, and `POST /api/beats/ask` → 202 `{ forkId }`.
3. `examples/_chat/ui/index.html`: a second sidebar tab, "Beats", shown only when `state.beats` exists: beat list with "N asked"/"running", lens chips disabled once asked, Q&A with "answering…", a free-question box that survives polling, a `fork #N at answer #M · tools: none` footer, a running-beat notice, and the selected beat highlighted in the stream. Keep the existing style, the drawer, and every existing behaviour.
4. `run.ts`: default mode starts the agent and chat UI on a temp `git init` workspace with an offline faux model; `--smoke` prints one `PASS/FAIL name: detail` line per check and exits 1 on any failure: beats-listed, lens-answer-arrives, follow-up-reuses-fork, lens-never-reaches-a-tool, running-beat-refused (hold beat 3 after its tool call with a gate in the faux model), earlier-beat-askable-while-running, main-continues-unchanged. The faux model writes on main beats and calls `write` on questions whenever offered. Derive expectations independently (answer ids from the store, git state, recorded requests).
5. README with frontmatter, a check table and a Break it section you have confirmed fails; FINDINGS.md with a live run through Mycel; screenshots of the tab with answers, a running beat, the sources tab, and the 390px drawer.
Works in Pi, Claude Code, Codex & Antigravity Offline mock first → --live opt-in

What we learned

The answer

Lab 27's tool-less fork works as a sidebar in the chat example. The Beats tab lists the conversation's beats, and each lens question goes through POST /api/beats/ask to one fork per beat, made at the beat's answer with tools: []. Answers land in state.beats, so the page shows them on its next poll. The main transcript didn't change, the lens requests offered no tools, and an earlier beat could be asked about while the agent was still working on a later one.

Live on DeepSeek V4.1 Flash through Mycel (2026-12-01), on a temp git workspace. Beat 1 ("Add a slugify(text) function … Write src/slug.ts.") took 9 tool calls: the agent wrote the file, typechecked it and ran a throwaway check. Beat 2 wrote a README. Then all four lenses and one free question went to beat 1 on fork #54 at answer #45:

  • Intent: "The user wanted a slugify(text) utility added that lowercases input and joins words with dashes, written to src/slug.ts. It was a small, self-contained implementation task in an otherwise empty repo."
  • Is it over?: "Finished. The requested deliverable … was written, typechecked with tsc --strict, and behavior-verified against six cases, all passing. … the only outstanding items are optional extras the agent offered (a test file, package/tsconfig scaffold) that the user never asked for."
  • Decisions: eight bullets, mostly choices the user didn't make: treat any non-alphanumeric run as a separator, fold diacritics (NFKD), trim dashes and return "" for punctuation-only input, export both named and default, add JSDoc, skip a test file, and fall back to the workspace tsc after npx tsc pulled an unrelated [email protected] package.
  • Still open: no committed test (the check was a deleted script in /tmp), no package.json/tsconfig, non-Latin input becomes "", the export shape is unconfirmed, nothing is committed.
  • Free question, "Given your answers so far, what should I check before moving on?": five items building on the earlier answers ("If items 1–2 match your intent, the beat is genuinely closed").

None of the answers mentioned beat 2's README, which the fork can't see. The workspace afterwards held only the files the two beats wrote.

Findings

  • busy alone marks the wrong beat. Between POST /api/chat and the user entry being committed, the newest beat in the store is the previous, finished one. The agent now records the turn in flight (beatsBefore and the prompt) when it submits, and beats() shows a placeholder running beat until the entry appears.
  • A running beat already has answer-like entries. The live agent and the faux model both write text before their tool calls ("Writing src/slug.ts."), so a running beat has a real assistant entry id that a client may hold. ask refuses by the beat's status, not by failing to find the id. A finished beat whose last text was followed by a tool result or a failed call has no answer, so it can't be asked about.
  • Claim the lens before awaiting the fork. Two quick clicks could both pass the "no lens yet" check while the first habitat.fork was still running, which would make two forks. ask now registers the lens, marked as answering, before its first await after the checks.
  • Lens forks run beside the main turn. With beat 3 held mid-turn, a lens question on beat 2 was answered on its own fork. The fork and the main conversation are separate conversations in one harness, and neither waited for the other.
  • Keep the question box out of the re-rendered HTML. The page re-renders on every state change (it polls every 700 ms), so a box inside the re-rendered region would lose what you're typing. The box and footer are static, and only enabled or disabled.
  • Live answers are long. "A short list" got eight bullets for Decisions. In a 320 px sidebar that is a lot of scrolling; a length limit in LENS_INSTRUCTIONS, or folding long answers, is the next step.
  • The tab row needed the space "+ Add server" had. With two tabs, the link clipped the Beats count, so it moved to the top of the Connected sources pane.
  • A faux factory can hold a turn. A FauxResponseFactory may return a promise. The smoke holds beat 3 after its tool call by awaiting a gate, and the factory re-appends itself before awaiting so the lens's concurrent requests still get a response.

Not tested

  • Lens state across a restart. Fork ids and Q&A live in the agent's memory. After a restart the forks stay in habitat.sqlite, but the tab starts empty and a new question makes a new fork.
  • "New chat" while a lens is answering. Lenses are keyed by entry id, so they should survive switching conversations, but this wasn't exercised.
  • An active MCP skill during a lens. tools: [] should remove skill tools too; the smoke has no skill server connected.
  • Prompt-cache hits on lens forks (as in Lab 27).

Smoke run

Visual Target Wireframe ASCII Layout
PASS beats-listed: 2 beats, answers #15 and #23 (the newest assistant entry after each turn), each with 1 write
PASS lens-answer-arrives: 202 fork #24; state.beats[0].lens.qa[0] = "The user wanted: “Add a slugify(text) function that lowercases and joins words w"
PASS follow-up-reuses-fork: fork #24 again; its request carries the Intent question; the same lens twice → 400
PASS lens-never-reaches-a-tool: 2 lens requests offered [] and called nothing; workspace git state unchanged
PASS running-beat-refused: beat 3 running at answer #42; ask → 400 "Beat 3 is still running; ask when the agent waits again."
PASS earlier-beat-askable-while-running: beat 2's "Is it over?" answered on fork #46 while beat 3 is still running
PASS main-continues-unchanged: state.messages identical across the lens questions; beat 3's 2 requests carry no lens question
all PASS

With tools: [] dropped from the fork (the README's Break it), the smoke printed 4 FAIL lines and exited 1.