Repository navigation
Description
Problem Statement
The captain's dashboards can only see firstmate by reaching into its private files. Every dashboard must know which file holds what, and a file-format change breaks them silently. Nothing outside firstmate can safely observe the fleet or act on it, and there is no way for firstmate to push "something changed" to a screen. Today one dashboard carries its own file-watching workaround; every future consumer would have to rebuild the same thing.
Solution
Firstmate itself answers at a front door: a small HTTP API on localhost that starts and stops with firstmate. It serves JSON answers about the fleet, accepts a small set of captain actions guarded by a token, and pushes typed live events over a server-sent event stream. Consumers stop reading firstmate's files; the file-format knowledge lives in one place, inside firstmate. The captain's dashboard becomes the API's first full customer and proves it complete.
User Stories
- As the captain, I want my dashboard to show the fleet snapshot the moment it changes, so that I never act on a stale picture.
- As the captain, I want to see one task's detail and timeline through the API, so that any screen can show a task without knowing firstmate's file layout.
- As the captain, I want to read the captain queue through the API, so that parked decisions reach whatever screen I am looking at.
- As the captain, I want to read the blocked list through the API, so that stuck work is visible everywhere the fleet is.
- As the captain, I want to read the rig ladders and their rungs through the API, so that dispatch behavior is inspectable without opening config files.
- As the captain, I want to send a captain note about a task through the API, so that firstmate relays my guidance without me touching a terminal.
- As the captain, I want to answer a parked decision through the API, so that waiting work resumes from any consumer I choose.
- As the captain, I want to toggle a rig rung through the API, so that I can steer dispatch without editing config by hand.
- As the captain, I want writes to require a token that only lives on my machine, so that a misbehaving crewmate cannot answer its own parked decision with one HTTP call.
- As the captain, I want the API to answer only on this machine, so that no write endpoint is ever one leaked URL away from a stranger.
- As a dashboard builder, I want typed events that name what changed and for which task, so that I can update one card instead of refetching the world.
- As a dashboard builder, I want a stable JSON contract for every read, so that a firstmate-internal file change never breaks my screen.
- As a dashboard builder, I want the event stream to survive quiet periods, so that a proxy in front of my consumer never cuts an idle connection.
- As a dashboard builder, I want a health endpoint that says the API is up and which firstmate home it serves, so that I can tell "no news" from "not running".
- As a script author on the captain's machine, I want to query the fleet with plain HTTP and no auth for reads, so that a one-line curl can answer "what is running".
- As someone running firstmate on another machine, I want the API to find its home, port, and token from firstmate's config with no hardcoded paths, so that it works on a setup that is not the author's.
- As someone running firstmate, I want the API to start when firstmate starts and die when it stops, so that it never serves confident answers about a supervisor that is not there.
- As the captain, I want the dashboard to stop reading firstmate's files entirely once migrated, so that there is exactly one place that knows the file formats.
- As a firstmate maintainer, I want every endpoint exercised by tests against a throwaway firstmate home, so that a contract break fails a test instead of a dashboard.
- As a firstmate maintainer, I want the API server to use only what ships with Node, so that firstmate gains no dependency tree to keep patched.
Implementation Decisions
- The API is a Node server inside firstmate's script collection, using only Node built-ins. Firstmate already ships Node scripts, so this adds no new runtime.
- Firstmate owns the server's lifecycle the same way it owns its watcher daemons: started on firstmate start, stopped on teardown. It binds to localhost only.
- Port and write token live in firstmate's config. The token is generated on first start if absent. Reads and the event stream need no auth; the three writes require the token as a bearer header.
- Reads mirror what the captain's dashboard consumes today, and nothing more: fleet snapshot, single task detail with timeline, captain queue, blocked list, rig ladders. Each read assembles its answer through firstmate's existing scripts and files; the API is the single holder of that knowledge.
- Writes are thin wrappers over existing firstmate entry points: a captain note about a task and a parked-decision answer both go through the operational-input relay and the wake queue (a captain note goes to firstmate, never directly to a crewmate); the rung toggle applies the same invariant the config already enforces, a ladder can never have every rung off.
- The event stream is server-sent events. A watcher inside the API maps file changes in the firstmate home to typed events carrying an event type, the task id where one applies, and a timestamp. Types cover at least: task status changed, task created, captain queue changed, rig config changed. A change the mapper cannot classify still emits a generic changed event, so no consumer ever misses news. Bursts are coalesced with a quiet window and a deadline, and hidden bookkeeping files are ignored. This design is lifted from the proven watcher in the captain's dashboard.
- The stream sends a heartbeat comment on an interval so idle connections survive proxies.
- A health endpoint reports the API version and the firstmate home it serves.
- The captain's dashboard migrates read path by read path onto the API, then its writes, then replaces its own file-watching doorbell by proxying the API's event stream. Migration is complete when the dashboard performs no direct firstmate file access. Its server-side proxy is what keeps the public site working with a localhost-only API.
Testing Decisions
- A good test talks to the API exactly as a consumer would and asserts only external behavior: the HTTP response, the event frames, and the visible file side effects in the firstmate home. No test reaches into the server's internals.
- One new seam: tests build a throwaway firstmate home (state, data, and config folders with fixture files), start the API against it on an ephemeral port, and speak real HTTP. Reads assert JSON bodies. Writes assert the side effects land in the throwaway home, including the wake queue entry. Event tests touch a fixture file and assert the typed event arrives. Auth tests send writes without the token and expect refusal.
- Prior art: firstmate's existing test scripts already build throwaway homes in temp directories and assert on the files firstmate leaves behind; the API tests follow that pattern and run under the same test entry point as the rest of the suite.
- The dashboard's migration reuses its existing test seams: its modules take injected dependencies, so migrated read and write paths are tested by pointing the dashboard's API client at the same fixture-backed server.
Out of Scope
- Spawning tasks through the API.
- Public exposure of the API (tunnels, remote access) and the real auth that would demand.
- Any conversation with or PR to the upstream firstmate project; this lives in the fork for now.
- Consumers beyond the captain's dashboard; they are welcome later but nothing is built speculatively for them.
- Write-conflict handling beyond what firstmate's existing entry points already provide.
Further Notes
The project glossary in CONTEXT.md fixes the vocabulary used here: captain, crewmate, task, captain note, parked decision, wake, fleet snapshot, rig and rung. Endpoint and event naming should use these words. The dashboard's existing file-watching doorbell (its live-update feature shipped 2026-08-25) is the prototype for the API's event watcher and gets retired by the migration.
Activity
EvanAgee commentedon Aug 27, 2026
Done. The localhost API is built with live events, and the dashboard is its first full customer: every read and write goes through it, and the dashboard no longer touches firstmate's files (except the write token). All child tickets closed.