Macula · Martha · docs from Martha v0.4.1
The host contract and the ledger
This exists so that a member refreshed in the middle of a package can see which of its predecessor's pushes, handoffs, room messages and tags were sent and never confirmed, and check them instead of sending them twice; and so that the same code does this on every coding tool Martha runs in.
Both are the first step of the port map, as designed on issue #1.
The host contract
src/host_contract/host_contract.ts
is everything the crew core may ask of a coding tool. It has five parts, and a
test fails by name if a sixth appears:
| Part | What a host gives |
|---|---|
clock |
the time |
stores |
rows: streams that are only ever appended to; documents: files read as text; launches: the launcher's records, one row per key, which slices only read; the writable store is the launcher's own seam, outside the contract (#42) |
processes |
running a command and getting its exit status and output; identityOf(pid): which process holds a pid, boot included (#42) |
delivery |
handing a turn to the model, and the turns that arrive, with where each came from as far as the tool can say |
tools |
registering a tool the model can call |
Nothing in it decides anything, and settings are not in it: they come from the crew's declaration. An append can name the length it expects the stream to have, so two writers that read the same stream cannot both act on what they read. A stream name is an opaque key: an adapter encodes it before it touches a path, a table or a URL.
The contract also holds the words a host uses to say where a turn came from
(Arrival, TurnSource, TurnOrigin). The publish gate's slices read them
from here. The contract file imports nothing, and a test holds it to that.
It also holds the words a host's adapter hands the core what its host reported
in, so that one entry answers every host
(src/answer_what_a_host_reports,
541):
| Word | What it carries | Answered with |
|---|---|---|
AStartingSession |
the session, the outcome its host's own table gives the start event (SessionStartOutcome: fresh, same_id or inherits; none for an event the table does not declare, which has lost its record), and the predecessor the host names |
TheAnswerToAStart: refused and why, or taken with what the session is handed as it starts |
ArrivingTurn |
a turn's text, and its Arrival |
TheAnswerToATurn: kept, or refused and why; a refused turn is never raised |
AReviewTheHostSawFail |
the session, the turn's text, and the origin kind the engine stamped on it | nothing: a review the host reports failed is named on the owner's row and to the member, only for a session the entry holds |
ACalledTool |
the session, the tool by the name the host called it, its input, the host's id for the call when it gives one, and the agent that made the call when an agent of the session made it | TheAnswerToACall: allowed, or denied and why; only the adapter writes its host's own yes |
AResultOfACall |
the called tool as above, after it ran: the host's answer passed through untouched, whether the called name is a tool server's, what the answer holds where it names an agent, and the model a spawned agent resolved to | TheAnswerToAResult: taken, with the end of the turn it carries when it was the last call in flight under a held end, or the member stops and why |
AnAgentOfASession |
the session, and the agent the host says started or stopped under it | TheAnswerToAnAgent: taken, or refused and why; an agent that stops unvouched for has its answer recorded as a subagent's text |
ACrewToolAsked |
the session, the crew tool's own name, and its arguments | the crew's own answer, or refused and why; a session the crew refreshed is taken in first |
The entry never reads a host's own table. The adapter turns the host's event
into its outcome and its prompt origin into an Arrival, and it hands the
envelope its host writes for a failed agent to the launch as a value. For a
result it reads the three facts the core needs out of its own host's answer,
and hands its table of result rows and its decisions over its own files to the
launch as values and closures (WhereAResultIsRecorded).
The core imports nothing but its own slices and Node's standard library. A
test reads every script file under src/ (TypeScript and JavaScript, every
extension) and names any other import, including a dynamic import whose name is
computed, so a coding tool's API cannot creep in past the contract.
Every slice is tested against
host_for_tests.ts, a host that
lives in memory and keeps the same promises.
On Claude Code
src/claude_code_host/ implements the contract on
Claude Code (#41). It is a host adapter, declared in package.json
martha.hostAdapters with the relay mod
(src/claude_code_relay/) and the launcher
(src/launch_a_claude_code_member/).
None of the three is a slice, and an adapter may import the contract, node:*,
host glue and slices, never a test or a program. It decides nothing: it composes the engine's clock, prompt submission and
tool registration with two pieces of glue any POSIX host can use,
src/row_streams_directory/ for the rows and
src/linux_process_identity/ for the
launcher's records and which process holds a pid.
One file inside an adapter is a decision rather than the adapter's own
machinery: martha.decisionFiles names it, and the slice rule's Node modules
apply to it wherever it sits, so it reads the machine through the contract and
imports no Node module but pure computation (node:path, node:crypto).
Declaring a file this way says which other rules it is still outside of; today
that is one file, the one that reads a member's own write back
(src/launch_a_claude_code_member/remember_own_write_of_a_call.ts,
martha#158).
A program a script runs, such as the launch probe (npm run probe:launch), is
not a slice either, and it may be cut into several directories to stay under the
slice cap. martha.programParts names each such directory and the program it
belongs to. The core's rules treat a part like its program, and the part is still
held to the slice cap. In exchange, only its program and that program's other
parts may import it. prove_the_core/what_the_core_may_reach/a_program_part_is_loaded_by_its_program_alone.ts
names any other file that does (martha#443).
- Rows live one directory per stream, the name encoded so it is never a
path, one file per row named by its position. A row is staged, then linked to
its position, and a link never replaces a file, so two writers cannot both
land at one position. An append resolves only once the row's file, its
position, the stream's directory and the rows directory with its parent are
fsynced, so a crash cannot lose the row of an act that then ran. A flush that
fails after the link rejects with
ROW_NOT_DURABLEand the row's position, and the stream takes no more appends in that process, so a retry cannot keep the row twice. A read refuses a gap, a link, an entry it never writes, a row that is not a JSON object, and a file anyone else may read or write; it never skips one. - The launcher's records reach slices only as
readingOnly(store). - A process runs directly with no shell, so a signal reads as a null exit code.
- Where a turn came from is the engine's
origin, which it stamps where it queued the prompt and no hook may set. Its kind maps to a turn source through the declaration insrc/name_the_source_of_a_prompt/: only the owner's own gestures (Enter at the terminal, a follow-up to the owner's click) areowner_paste; a kind that can carry anyone's text (Remote Control's bridge, a Slack ping, the SDK driver's input, an unpromoted task notification such as a GitHub webhook) and another session's message arecross_session; an observer's report or a schedule's stored text issubagent; a channel server's prompt and every plugin's injected prompt, a crew mod's included, aremcp_result. A kind no declaration lists is another session's message. The kinds are parsed from the engine's own origin code as recorded from its binary, andgapsInPromptOriginsfails CI on a kind the declaration does not map. Text never sets a source. - No sender yet. The host knows which process sent a cross-session message
and does not pass it to plugins (anthropics/claude-code#107216), so the
adapter's binder is
bindNothingand every cross-session message stays unattributed. The adapter reads no transcript. - A session start maps the hook input's
sourceandsession_id, and nothing else. A source the host does not send, the crew's refresh among them, maps to an event no declaration lists and fails closed. The emitted events are parsed from the host's SessionStart schema as recorded from its binary (recorded_claude_code_host.json;npm run record:claude-code-host, the program insrc/record_claude_code_host/, takes it again after an upgrade), plus the crew's refresh. - A tool name is refused a second time before the engine sees it.
The launcher's wiring (who writes the records) waits for #16's launch gate.
The ledger
src/log_ledger/log_ledger.ts keeps one
append-only stream per work package:
- Milestones: a checkpoint reached, a review round closed, a release shipped, a fix shipped after a release.
- Outward acts:
sendOutwardActwrites the act's row first. If the store does not keep it, the act does not run. After the act, its outcome is a second row, success or failure. A caller can tell the failures apart:
| Failure | What happened | May the caller send it again? |
|---|---|---|
ACT_NOT_SENT |
the row was not kept, so the act never ran | yes, unless row_may_stand_at is set: a row on that stream was linked and could not be made durable (ROW_NOT_DURABLE), this act's or another act's whose append failed earlier, so it may stand as an act left awaiting, and the store takes no more rows on that stream in this process |
| the act's own error | the act failed, and that is its kept outcome | as for any failed act |
OUTCOME_NOT_KEPT |
the act ran, and its outcome is not in the ledger | never: check it first |
Every act sent through sendOutwardAct has its row first. Room messages go
through it in send_a_crew_message, handoffs in read_a_crew_line, and the
deletion of a worktree's branch on origin in open_and_finish_a_worktree. No
other slice sends one: a member pushes with git in its own shell and a tag
goes out from CI, so a push or a tag of Martha's own gets a row the day
Martha has a sender of one.
- What is unresolved: actsAwaitingOutcome returns every act with no
outcome. A successor checks each one (is the push on the remote? did the
handoff arrive?) and records what it found with concludeAct. An outcome for
an act never announced, or a second outcome for the same act, is refused.
A package is an issue, owner/repo#n; any other name is refused before it
names a stream. A row the ledger cannot read is refused rather than skipped,
because skipping it could hide an act that was sent, and so is a row filed under
another package's stream.
A handover as a ledger row, naming the last row it covers, comes with the
hand_over_at_limit slice.
Martha's docs, from release v0.4.1: Getting started: a crew of two on your own project · @macula-io/martha · Martha · The agent control loop · claw-code: what it is, where it comes from, what Martha can learn · The host contract and the ledger · How a release is made · Nothing private gets in · The port map, in summary · The publish gate · The crew's declaration