Skip to content

mailbox: recover a mailbox from an optional state journal - #88

Draft
Damon6666 wants to merge 1 commit into
DeepLinkNext:mainfrom
Damon6666:mailbox/recover-state
Draft

Damon6666 wants to merge 1 commit into
DeepLinkNext:mainfrom
Damon6666:mailbox/recover-state

Conversation

@Damon6666

Copy link
Copy Markdown

Draft for now: not meant to be merged before it has been looked over.

Why

The mailbox prototype is RAM-only by design, and its event log says so explicitly: "append-only observations are never recovery input". A crash means every letter, state, lease and idempotency key is gone. That makes one thing impossible that the agent-team example in #87 needs: a run that survives a restart of the process that hosts the mailboxes.

This adds the smallest thing that makes that possible, opt-in.

What it does

  • Mailbox(..., state_dir=...): every transition is appended to <state_dir>/<identity>.jsonl (the full private row, including the claim token) before the event is emitted and before RAM is published. A failed journal append publishes nothing.
  • On start the mailbox rebuilds its state from that journal: letters, states, leases, claim tokens, idempotency keys, byte accounting and counts. A worker that held a claim token before the restart can still finish its letter; one that did not, cannot.
  • The journal is compacted to one line per letter at startup, so it does not grow with reads.
  • A half-written last line (a crash in the middle of an append) is dropped. Damage anywhere else is reported as an error instead of being silently accepted.
  • MailboxDirectory(..., mailbox_state_dir=...) passes the directory to the mailboxes it creates, so the host that spawns them does not need to know each identity.

Why a second log

The actor event log stays what it is: an observation stream with the public view of a letter (no claim token), replayed offline by pulsing.mailbox replay. Recovery needs fields the observation stream deliberately omits, so it reads its own journal. The two logs have different contracts on purpose, and the existing document now says which is which.

Scope and limits

  • Covers a process restart, not a power loss (the journal is flushed, not fsynced).
  • One live mailbox per state directory is assumed.
  • Still no cross-machine replication, no HA, and no authentication.
  • The host and MCP commands do not wire the parameter yet, so nothing changes for current users; that is the follow-up, together with --resume in the example.
  • Related but separate: Write-review team example, its design page, and a new home page for the docs site #87 adds the write-review team example; it will point mailbox_state_dir at its run directory and resume a run from its ledger.

What was verified

  • tests/python/mailbox/test_recovery.py, 7 new tests: letters/states/leases/claim tokens come back; idempotency keys come back (and a conflicting re-send is still rejected); a half-written tail is dropped; the journal is compacted on start; mid-file damage is reported; nothing is written without a state_dir; the directory hands the state dir to its mailboxes.
  • The existing tests/python/mailbox/test_mailboxes.py (20 tests) still passes, and ruff check / ruff format --check pass on the changed files.
  • Not run here: the MCP stdio suite, because this environment has mcp 2.x installed while the suite needs mcp<2 (as ci.yml installs it). CI will cover it.

A Mailbox given a state_dir appends every transition to
<dir>/<identity>.jsonl before publishing it, and rebuilds letters, states,
leases, claim tokens and idempotency keys from that journal on start; the
journal is compacted to one line per letter at startup. A half-written last
line (a crash during a write) is dropped; damage anywhere else is reported.
MailboxDirectory takes an optional mailbox_state_dir and passes it to the
mailboxes it creates.

This covers a process restart, not power loss (the journal is flushed, not
fsynced), and assumes one live mailbox per state directory. The actor event
log stays an observation stream and is still never read back for recovery.
The host and MCP commands do not wire the parameter yet, so the change is
opt-in.

Tests: letters, states, leases and claim tokens come back; idempotency keys
come back; a half-written tail is dropped; the journal is compacted on start;
mid-file damage is reported; nothing is written without a state_dir; and the
directory hands the state dir to its mailboxes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant