buzz icon indicating copy to clipboard operation
buzz copied to clipboard

buzz-acp: a message sent while the bridge is not running is never delivered (each start reads from its own start time)

Open troyhoffman-oss opened this issue 1 week ago • 3 comments

What happens

A message sent to an agent while its buzz-acp process is not running (a restart for an upgrade, a crash and respawn, a host reboot) is never delivered. When buzz-acp starts again it reads from its own start time, so the message stays unanswered in the DM or channel and nothing tells the sender or the agent.

Reproduce

  1. Run buzz-acp with any ACP agent and exchange one DM with it, so the DM is known to work.
  2. Stop the buzz-acp process.
  3. Send the agent a DM.
  4. Start buzz-acp again with the same key and no --replay-floor.

Seen (buzz-acp from the image built at commit 44e97e9, a throwaway relay at the same commit): the agent received no prompt for that message in the 120 seconds after the start. A message sent after the start was delivered as usual.

Expected: a long-running agent picks up what was sent to it while its bridge was down, at least for a short gap such as a restart.

Where it comes from (read on main at d1236080eb)

  • crates/buzz-acp/src/lib.rs:2925-2929 sets the startup watermark to the process's start time, and lib.rs:2951-2955 has the first REQ use since = watermark - 5s.
  • The per-channel position (last_seen, crates/buzz-acp/src/relay.rs:1164) lives in memory only and starts empty (relay.rs:1278), so resume_since (relay.rs:3692) falls back to that watermark on every start.
  • --replay-floor / BUZZ_ACP_REPLAY_FLOOR can move the watermark back, but it is clamped to 15 minutes (lib.rs:2703, lib.rs:2714-2716) and is documented for a publish-first mention send, where the caller knows the one message it is waiting for.

So nothing carries a position from one run of the process to the next.

Possible fix

Keep the newest delivered created_at per channel (and the membership one) in a small state file next to the agent's other state, and use it as the startup watermark when it is present, with event ids already handled dropped as on a reconnect. Or, if a host is meant to supply this itself, say so in the docs for --replay-floor and let the floor reach back further than 15 minutes for a host that asks.

Workaround in use

The host passes BUZZ_ACP_REPLAY_FLOOR on every start, set to the send time of the last message it took from that person, and drops repeats by event id on its side. Measured on the same setup: the message from step 3 is then delivered after the start. It covers gaps up to 15 minutes; a longer outage still loses messages.


Found and written up by an AI coding agent while building a custom ACP agent; the "Seen" line and the workaround were measured, the source pointers were read and not run.

troyhoffman-oss avatar Nov 21 '26 15:11 troyhoffman-oss

I'd like to pick this up, but the two fixes in the issue pull in different directions, so I'd like to agree on the approach first.

On main the 15-minute bound looks deliberate. REPLAY_FLOOR_MAX_AGE_SECS "bounds the stale-event burst when a spawn request sat around" (crates/buzz-acp/src/lib.rs). A persisted position without a bound would undo that: after a week offline, an agent would replay and answer a week of messages. So I'd propose a persisted cursor that keeps a bound:

  1. Persist the cursor. buzz-acp writes the newest delivered created_at per channel (BgState::last_seen) and membership_last_seen to a small state file keyed by the agent's pubkey. It writes after an event is enqueued, at most once per second or so.
  2. Resume from it, bounded. On start, a channel with a saved cursor uses it as its since, the same way resume_since already prefers last_seen over the startup watermark on a reconnect. The cursor is clamped to a maximum age. Channels without a cursor keep today's startup watermark. --replay-floor still applies, and the older of the two wins.
  3. Dedup across the restart. Events at the cursor second can be delivered twice, so the file also keeps the ids delivered at that created_at and preloads them into seen_ids.
  4. Tests: unit tests for loading and clamping the cursor and for the since it produces, plus an integration test that stops the bridge, sends a DM and checks it is delivered after the restart.

Questions before I build it:

  • Is persisting a cursor in buzz-acp the direction you want? Or should the host own this, so that the fix is to document --replay-floor and lift its cap when the host asks?
  • What maximum catch-up age would you accept, and should it be a flag (for example --max-catch-up) defaulting to the current 15 minutes or something longer?
  • Is there an existing per-agent state directory buzz-acp should write to, or should the path be a flag or env var?

Once you confirm, I'll open a draft PR and follow the AGENTS.md checklist.

srednoff888-art avatar Nov 21 '26 20:11 srednoff888-art

We run buzz-acp behind planned restarts, with a drain patch (section 3). Three details next to this issue may help choose a direction. Line references are at 5511b56. The buzz-acp queue, dispatch and shutdown behaviour described below also holds on main at dfda5fc.

1. Events already received are lost too, not only those sent while the process is down.

The queue lives only in memory (queue.rs:199-200). On SIGTERM the main loop exits (lib.rs:2906-2923, lib.rs:3713-3716). Shutdown waits up to 30 s for in-flight prompts, then aborts any still running (lib.rs:4044-4079). Work left in the queue is dropped.

A cursor saved once an event is enqueued, as in the plan in the comment above, can then skip that unfinished work or suppress it as already delivered. Either the cursor stays behind the oldest unfinished event (even when a newer turn finishes first), or the queue itself persists. #4903 asks for the latter. The open PRs #8304 (opt-in delivery journal) and #5940 (opt-in wake tickets for unconsumed mentions) work in that direction.

2. A replay window also replays an owner's !cancel.

!cancel is handled before the queue and discards whatever turn is in flight in its scope (lib.rs:3372-3415, pool.rs:4738-4748). The relay returns history newest-first (buzz-db/src/store/event.rs:438), and when the pool is ready the loop tries to dispatch a queued event right after it is pushed (lib.rs:3556-3561).

So if the replay window reaches back past a !cancel that was already applied, and an agent is free, a newer restored mention in the same scope can start before the old !cancel is read. If that turn is still running when the !cancel is handled, it is discarded. Dedup across the restart needs the ids of everything handled in the window, control commands included, and not only the ids at the cursor second. #8304 suppresses replayed ids of journaled work (delivery_journal.rs:258). A !cancel never enters the queue, so as far as I can tell from the diff it isn't in that set.

3. For planned restarts, a drain mode as the complement.

Persistence is what covers a crash. For a planned restart (an upgrade or a config change) it's simpler not to need recovery: stop taking new work, finish what was already accepted, and tell the host when it's safe to stop. Today the host can't see that, and the fixed 30 s grace cuts long turns (#8392).

Our patch is opt-in, and with its settings unset nothing changes. While a lease file is valid, it:

  • defers new events in memory and keeps dispatching work already queued (owner !cancel / !shutdown / !rotate and membership changes are still handled);
  • reports drained in a status file once deferral is active, the replay window is saved, nothing is queued or running, and a fresh relay barrier is ready;
  • saves a replay floor plus the ids handled from 5 s before it. A process restarted under the same lease seeds those ids into dedup, holds deferred events until its replay has finished and the lease has ended, then feeds them back oldest-first.

The barrier answers once every channel subscription on the current connection has had its EOSE. It is only as good as that EOSE: on 5511b56 the relay still sends EOSE after a failed history read (req.rs:393-401), while main sends CLOSED since #8692.

That is also #8688's ground. The relay registers the live subscription before it reads history (req.rs:291-324, then req.rs:385), so a live event can move last_seen past history that hasn't arrived (relay.rs:1256-1270). If that first replay is interrupted, a cursor persisted from last_seen can carry the gap into the next start. The next request needs that subscription's unconfirmed since, which #8688 kept until EOSE.

The drain doesn't help after a crash, or for an agent already offline when mentioned (#1743); that stays with this issue. We'd be glad to contribute it as a PR, either on its own or alongside whichever persistence direction you pick here.

mlamp avatar Dec 01 '26 01:12 mlamp

Thanks, Margus — this is very helpful.

You're right that persisting the cursor at enqueue time would introduce a loss window, and deduplicating only events at the cursor boundary would not protect against replayed control commands.

I also checked #8304, #5940, #2898, and the EOSE handling discussed in #8688 and #8692. I'd prefer not to introduce another competing persistence mechanism.

Your drain approach looks complementary to crash recovery rather than a replacement for it.

Would it be useful if I focused on deterministic regression coverage for the remaining recovery gaps, particularly:

  • Interrupted history replay before EOSE.
  • Live events arriving ahead of historical events.
  • Replayed control commands affecting newer turns.
  • Out-of-order completion advancing the recovery cursor past unfinished work.

I'd first check the coverage in the existing PRs and only implement missing scenarios.

Happy to align with whichever persistence direction the maintainers prefer before introducing production changes.

srednoff888-art avatar Dec 01 '26 06:12 srednoff888-art