The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-mode): re-schedule a push that races an in-flight drain + pin the watermark's fire-again contract
kshitijbb63e0c4c746
diff --git a/apps/desktop/src/plugins/hermes-bots/plugin.js b/apps/desktop/src/plugins/hermes-bots/plugin.jsindex 1090ac6260f..e02beec4131 100644--- a/apps/desktop/src/plugins/hermes-bots/plugin.js+++ b/apps/desktop/src/plugins/hermes-bots/plugin.js@@ -1388,6 +1388,10 @@ let relayRosterBusy = falselet relayDrainBusy = falselet relayPushUnsub = nulllet relayPushDebounceTimer = null+// A push landing while a drain is ALREADY running would be lost forever —+// the gateway signature is monotone (one event per new envelope, never+// re-broadcast) — so remember it and re-schedule after the drain finishes.+let relayDrainRerun = false/** One representative route per reachable connection id. */async function relayConnections() {@@ -1514,7 +1518,16 @@ async function syncRelayRosters() {* connection's own socket; the reply (or error) is posted back to the* sender gateway for its waiter. */async function drainRelayOutboxes() {- if (relayDisposed || relayDrainBusy) {+ if (relayDisposed) {+ return+ }++ if (relayDrainBusy) {+ // A push signal raced an in-flight drain. The gateway never re-sends it+ // (monotone signature), so without this flag the envelope would wait out+ // the full poll interval — exactly the latency the push path removes.+ relayDrainRerun = true+return}@@ -1582,6 +1595,13 @@ async function drainRelayOutboxes() {}} finally {relayDrainBusy = false++ if (relayDrainRerun && !relayDisposed) {+ // Envelopes signaled mid-drain: schedule one follow-up pass (debounced)+ // instead of leaving them to the interval poll.+ relayDrainRerun = false+ scheduleRelayPushDrain()+ }}}diff --git a/apps/desktop/src/plugins/hermes-bots/tests/relay-push-drain.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/relay-push-drain.test.mjsindex 63a63f37829..0314b02fb2b 100644--- a/apps/desktop/src/plugins/hermes-bots/tests/relay-push-drain.test.mjs+++ b/apps/desktop/src/plugins/hermes-bots/tests/relay-push-drain.test.mjs@@ -83,3 +83,20 @@ test('subscription is feature-detected, disposed, and the poll backstop stays',assert.match(pluginSource, /RELAY_DRAIN_INTERVAL_MS = 4_000/)assert.match(pluginSource, /setInterval\(\(\) => void drainRelayOutboxes\(\), RELAY_DRAIN_INTERVAL_MS\)/)})++test('a push racing an in-flight drain schedules a follow-up pass instead of dropping', () => {+ // The gateway signature is monotone — one event per new envelope, never+ // re-broadcast — so a push swallowed by the relayDrainBusy early-return+ // would strand its envelope until the poll. The rerun flag re-schedules.+ const drain = pluginSource.slice(+ pluginSource.indexOf('async function drainRelayOutboxes'),+ pluginSource.indexOf('function scheduleRelayPushDrain')+ )+ assert.match(drain, /if \(relayDrainBusy\) \{/)+ assert.match(drain, /relayDrainRerun = true/)+ // finally block: after busy clears, a remembered push re-schedules once.+ const finallyBlock = drain.slice(drain.indexOf('} finally {'))+ assert.match(finallyBlock, /relayDrainBusy = false/)+ assert.match(finallyBlock, /relayDrainRerun && !relayDisposed/)+ assert.match(finallyBlock, /scheduleRelayPushDrain\(\)/)+})diff --git a/tests/tui_gateway/test_change_watcher.py b/tests/tui_gateway/test_change_watcher.pyindex a5bff9e01ca..5ec73da7b35 100644--- a/tests/tui_gateway/test_change_watcher.py+++ b/tests/tui_gateway/test_change_watcher.py@@ -7,6 +7,7 @@ once, the sessions floor coalesces a write burst but keeps its trailing edge,and the pet signature only moves for a *renderable* pet."""+import osimport timeimport pytest@@ -210,6 +211,33 @@ def test_drained_outbox_does_not_rebroadcast_pending(watcher_home):assert not [e for e in events if e[0] == "bot_relay.outbox.pending"]+def test_new_envelope_after_drain_fires_pending_again(watcher_home):+ """The other half of the monotone contract: the watermark must not eat+ GENUINELY new envelopes. write → drain → write-newer fires twice."""+ home, events = watcher_home+ outbox = home / "bot_relay" / "outbox"+ outbox.mkdir(parents=True)+ first = outbox / ("c" * 32 + ".json")+ first.write_text("{}")+ server._broadcast_watched_changes(now=0.0)+ first.write_text("{}") # make the first sighting a change, not a seed+ server._broadcast_watched_changes(now=10.0)++ first.unlink() # the Desktop drained it+ server._broadcast_watched_changes(now=20.0)++ second = outbox / ("d" * 32 + ".json")+ second.write_text("{}")+ now_ns = time.time_ns()+ os.utime(second, ns=(now_ns, now_ns)) # strictly newer than the watermark+ server._broadcast_watched_changes(now=30.0)++ assert [e for e in events if e[0] == "bot_relay.outbox.pending"] == [+ ("bot_relay.outbox.pending", {}),+ ("bot_relay.outbox.pending", {}),+ ]++def test_no_outbox_dir_never_fires_pending(watcher_home):home, events = watcher_homeserver._broadcast_watched_changes(now=0.0)