Skip to content

feat(rap-agent-mail): let agents message agents in other Infinity daemons - #195

Open
akainth015 wants to merge 4 commits into
mainfrom
pr/agent-mail
Open

akainth015 wants to merge 4 commits into
mainfrom
pr/agent-mail

Conversation

@akainth015

Copy link
Copy Markdown
Member

Agents can now message agents in other Infinity daemons: across the hosts in ~/.infinity/remotes.json, or between daemons on one machine. The work comes as a new daemon-lifetime RAP server, rap-agent-mail, plus the daemon and runtime support it needs.

sequenceDiagram
    participant LA as laptop agent
    participant LM as laptop mailbox
    participant CM as devbox mailbox
    participant CC as devbox child thread
    LM->>CM: ssh -L forward of its peer socket (only the laptop dials)
    LA->>LM: ask_agent(devbox/reviewer)
    LM->>CM: ask (durable outbox until acked)
    CM->>CC: subscription event → child thread
    CC->>CM: reply(message_id)
    CM-->>LM: reply over the same link
    LM->>LA: tool result
Loading

Commits

The commits are split for review; each builds on the previous one.

  1. fix(infinity-agent-core): don't defer subscription events that only spawn a child thread. While a thread waited on an active tool call, every synthetic event was deferred, non-associative subscription events included. Those never touch the parent's history: they only spawn a child at a spawn point that excludes the pending call. Deferring them meant two agents asking each other questions deadlocked, and the in-memory defer queue lost them on restart. Associative events, thread reports, and parent messages are still deferred. A regression test is included. The Lambda runtime (NoDeferral) is unaffected.

  2. fix(infinity-daemon): clear a session's stopped flag when user text resumes it. If a session was stopped while its driver stayed live (parked in sleep_until_event_or_input), resuming it produced no Live transition. The session stayed marked stopped, and the router silently dropped every later tool result and event for it. I found this on real hosts; a regression test is included.

  3. feat(infinity-daemon)!: daemon-lifetime RAP servers. A toolset_command entry with "lifetime": "daemon" (written by infinity rap install --daemon-lifetime) behaves as follows:

    • It runs as one shared process, in ~/.infinity, with RAP_SERVER_LIFETIME=daemon and RAP_CALLBACK_URL set.
    • It boots at daemon start, is restarted if it exits, and is never stopped on session idle. Migrations skip it.
    • Its stdin is a pipe that hits EOF when the daemon dies, so it exits rather than lingering as an orphan.
    • When a session is stopped, archived, or migrated, the server gets /close_thread for the session root.

    This commit also makes three changes that apply to every command server:

    • A dead child process is restarted on next use.
    • Startup and manifest fetches time out.
    • RapNotifier::new_live looks up server URLs when it sends, so a restarted server is still reached.

    It has breaking API changes (footers in the commit).

  4. feat(rap-agent-mail): the mailbox. It provides five tools:

    • open_inbox, a subscription delivering each message to a per-message child thread (default) or inline
    • list_agents
    • send_message
    • ask_agent, whose call stays pending until reply
    • reply

    It also includes:

    • Peer links: a Unix socket forwarded over SSH, carrying traffic both ways, so a host that can't dial back still sends and receives.
    • Delivery: a durable outbox with acks, retransmission, deduplication, and expiry.
    • Withdraw and decline: interrupting an ask withdraws it, and a closed session's unanswered questions fail.
    • Identity: host IDs and aliases, so two hosts that dial each other under different names line up.
    • Safeguards: per-session rate limits, validation of inbound messages, and heartbeats.
    • Docs: docs/docs/infinity-code/agent-mail.md.

Testing

  • Rust tests: cargo test -p rap-agent-mail (11 end-to-end tests: real mailboxes linked over Unix sockets, with RAP callbacks captured by a real callback server). Also infinity-daemon, infinity-agent-core and rap-client, plus workspace-wide cargo clippy --all-targets -D warnings and cargo fmt --check.
  • Two real hosts: a laptop daemon and a cloud-desktop daemon, each with an isolated HOME and real Bedrock agents:
Scenario Result
Laptop agent asks a cloud-desktop agent Correct answer as the ask_agent result
Mutual asks (devbox asks a clarifying question while the laptop waits) Works (deadlocked before commit 1)
Ask while the remote daemon is down, then it restarts on a new callback port Delivered and answered
Asker's daemon restarted while waiting Reply still arrives
SSH tunnel killed Reconnects in ~3s, queue flushed
Remote mailbox frozen (SIGSTOP) Heartbeat drops the link in ~80s; drains after SIGCONT
Daemon kill -9 Mailbox exits via the stdin lifeline
Mailbox kill -9 Supervisor restarts it within 30s; orphaned SSH tunnel reaped
Recipient session stopped mid-question Asker told "will not be answered" in ~100ms; after resume, reopening the inbox ends the old subscription

Seven infinity-agent-cli TUI snapshot tests fail locally (tui_reflow_snapshots, tui_submit_wrap_snapshots). They fail the same way on main (e1a3e4b), so they're unrelated to this PR.

Known limitations (documented)

  • No authentication on the local port. Like every RAP server and the daemon's callback endpoint, the mailbox's tool endpoint is unauthenticated on 127.0.0.1. It isn't meant for machines shared with other users.
  • Inline inboxes have weaker guarantees. Messages waiting for an inline inbox are held only in memory, and a mutual ask can still deadlock there. Thread delivery, the default, has neither problem.
  • Reopen after resume. A resumed session must reopen its inbox.
  • No relaying. Messages are not relayed through a third host.

…pawn a child thread

While a thread waited on an active tool call, every synthetic event was
deferred, including non-associative subscription events. Those never touch
the parent's history: they only spawn a child thread at a spawn point that
excludes the pending call. Deferring them held independent work hostage to
an unrelated call (two agents waiting on answers from each other
deadlocked) and lost it on restart, since the defer queue is in memory.

* `is_deferrable_synthetic_event` now defers only associative subscription
  events, thread reports, and parent messages
* Add `threaded_subscription_event_not_deferred_during_async_tool_wait`
…esumes it

A session stopped while its thread driver stayed live (parked in
`sleep_until_event_or_input`) got no new `Live` lifecycle transition when
the user resumed it, so it stayed marked stopped and the router silently
dropped every later tool result and subscription event for it.

* `SessionManager::send_input` clears the root session's stopped flag for
  user text
* Export `infinity_agent_core::system::is_user_text_input`
* Add `user_input_restarts_session_stopped_while_live`
Command RAP servers run per session and stop whenever their session goes
idle, so none can keep receiving events for idle sessions. A
`toolset_command` entry can now declare `"lifetime": "daemon"`:

* One shared process per command (`DaemonServers`), spawned in the daemon's
  state dir with `RAP_SERVER_LIFETIME=daemon` and `RAP_CALLBACK_URL` (the
  callback port changes on every daemon restart)
* Booted at daemon start for user-level entries; `supervise` restarts it if
  it exits; session idles never stop it; migrations skip it
* Its stdin is a pipe that hits EOF when the daemon dies, so it can exit
  instead of lingering as an orphan
* Stopping, archiving, or migrating a session sends `/close_thread` for the
  session root to daemon-lifetime servers (in the background, booting them
  first)
* `infinity rap install --daemon-lifetime`; re-installing keeps an entry's
  lifetime

Also, for every command server:

* `ensure_up` restarts a server whose process exited
* Startup-line (60s) and manifest-fetch (30s) timeouts, so a hung server
  cannot hold its boot lock forever
* Thread lifecycle notifications look up server URLs when sent
  (`RapNotifier::new_live`), so a restarted server is still reached

BREAKING CHANGE: `ToolSetConfig::ToolsetCommand` has a new `lifetime` field.
BREAKING CHANGE: `ToolsConfig::add_installed_command` takes a `ServerLifetime`.
BREAKING CHANGE: `SessionRapManager::new` takes a `DaemonServers`.
BREAKING CHANGE: `ManagedRapServer::current_base_url` is no longer async.
BREAKING CHANGE: `infinity_agent_cli::install::InstallArgs` has a new `lifetime` field.
@akainth015
akainth015 requested a review from a team November 19, 2026 22:42
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Deploying infinity with  Cloudflare Pages  Cloudflare Pages

Latest commit: cb7781a
Status: ✅  Deploy successful!
Preview URL: https://bb1b1723.infinity-dc7.pages.dev
Branch Preview URL: https://pr-agent-mail.infinity-dc7.pages.dev

View logs

…mons

A new daemon-lifetime RAP server. Each daemon runs one mailbox; agents get:

* `open_inbox`: a subscription that makes the thread reachable as
  `host/name`; each message is handled in its own child thread (default)
  or inline
* `list_agents`: open inboxes on this host and every linked host
* `send_message`: one-way message
* `ask_agent`: a question whose tool call stays pending until the
  recipient calls `reply`
* `reply`: answer a received question

Mailboxes dial the peers in `~/.infinity/remotes.json` by forwarding a Unix
socket over SSH; each link carries traffic both ways, so a host that cannot
dial back (cloud desktop to laptop) still sends and receives. Messages wait
in a durable outbox until acknowledged, are re-sent on reconnect and every
minute, and receivers deduplicate them. Also:

* Interrupting a waiting `ask_agent` withdraws the question; a stopped,
  archived, or migrated session closes its inboxes and fails the questions
  they had not answered
* Stable host IDs and announced-name aliases, so hosts that dial each other
  under different names match up
* Per-session rate limits, inbound validation, ping heartbeats, orphaned
  SSH tunnel cleanup, and a stdin lifeline to its daemon
* Docs: `docs/docs/infinity-code/agent-mail.md`
* Added to the release crate lists and THIRD-PARTY
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