Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

fix(hive): stop the inbox nudge firing at an inbox that has since been drained

Chaitanya Giri9829c94dc372
diff --git a/src/renderer/src/hooks/useHive.ts b/src/renderer/src/hooks/useHive.ts
index 24ab71fd..f237d332 100644
--- a/src/renderer/src/hooks/useHive.ts
+++ b/src/renderer/src/hooks/useHive.ts
@@ -678,8 +678,17 @@ export function useHive(config: HarnessConfig | null): void {
try {
const inbox = await window.cth.hiveInbox(a.id);
// Nudge on any id we have not nudged for yet (#130's per-id Set).
- // Draining shrinks the set and introduces nothing new, so it stays
- // quiet; a genuinely new message fires regardless of how its id sorts.
+ // Draining shrinks the set and introduces nothing new, so this POLL
+ // stays quiet; a genuinely new message fires regardless of how its id
+ // happens to sort.
+ //
+ // That reasoning covers the poll only — it does NOT survive the gap
+ // between enqueue and delivery, which is why the nudge carries an
+ // 'inbox-nonempty' precondition that the drain re-checks before typing.
+ // Without it: mail lands and queues a nudge, the already-awake agent
+ // drains the whole inbox in that same turn, and the nudge is typed into
+ // an empty inbox afterwards — a wasted turn, and the most expensive one
+ // on the floor when the agent is god.
const seen = nudged.current[a.id] ?? (nudged.current[a.id] = new Set());
const fresh = inbox.filter((m) => m.id && !seen.has(m.id));
if (fresh.length) {
@@ -690,7 +699,11 @@ export function useHive(config: HarnessConfig | null): void {
// one nudge pending per agent (see enqueueMessage), so a suppressed
// copy's ids stay unnamed — hence the text points at the pending
// inbox as the authority rather than at the list.
- useStore.getState().enqueueMessage(a.id, inboxNudgeText(fresh.map((m) => m.id)));
+ useStore.getState().enqueueMessage(
+ a.id,
+ inboxNudgeText(fresh.map((m) => m.id)),
+ { precondition: 'inbox-nonempty' }
+ );
for (const m of fresh) seen.add(m.id);
}
} catch { /* ignore */ }
@@ -801,6 +814,25 @@ export function useHive(config: HarnessConfig | null): void {
// erases the user's text and never closes the user's menu.
if (!isTerminalAutomationSafe(target.ptyId, now)) return { sent: false };
if (now - (lastFlush.current[target.id] ?? 0) < FLUSH_COOLDOWN_MS) return { sent: false };
+ // Last gate before we type: re-check the message's delivery-time
+ // precondition. A queue item is decided at enqueue time and delivered an
+ // arbitrary interval later, and the inbox nudge is only worth sending if
+ // there is still something in the inbox — the agent routinely drains it in
+ // the very turn the nudge was queued from. Stale ones are DROPPED, not
+ // deferred, so they cannot park at the queue head and starve the rest.
+ if (next.precondition === 'inbox-nonempty') {
+ let stillPending = true;
+ try {
+ stillPending = (await window.cth.hiveInbox(srcId)).length > 0;
+ } catch {
+ // Can't tell — let it through. A spurious nudge costs a turn; a
+ // swallowed one can leave real mail sitting unread indefinitely.
+ }
+ if (!stillPending) {
+ removeQueuedMessage(srcId, next.id);
+ return { sent: false };
+ }
+ }
const flightKey = `${srcId}:${next.id}`;
if (inFlight.has(flightKey)) return { sent: false };
inFlight.add(flightKey);
diff --git a/src/renderer/src/store/store.ts b/src/renderer/src/store/store.ts
index 0eb9bf60..f3d55c71 100644
--- a/src/renderer/src/store/store.ts
+++ b/src/renderer/src/store/store.ts
@@ -129,6 +129,18 @@ export interface QueuedMessage {
* ONLY the pause gate in the drain loop — idle/draft/picker safety still hold,
* so it delivers the moment the terminal is actually free. */
manual?: boolean;
+ /** Delivery-time precondition, re-checked by the drain immediately before the
+ * message is typed; a message whose precondition no longer holds is DROPPED
+ * rather than deferred (deferring would park it at the queue head forever and
+ * starve everything behind it).
+ *
+ * Exists because a queued message is evaluated at enqueue time but delivered
+ * an arbitrary interval later, and some messages are only worth sending if the
+ * world they described still exists. 'inbox-nonempty' is the inbox-wake nudge:
+ * the agent can drain its whole inbox in the same turn the nudge was queued
+ * from, and delivering it afterwards costs a full turn to discover nothing is
+ * there. Declarative (a string, not a closure) so it survives persistQueues. */
+ precondition?: 'inbox-nonempty';
}
// 'files' retired in v0.3.4 (the per-agent IDE button superseded it) — a
@@ -274,7 +286,7 @@ interface State {
/** Park a message for an agent. Returns nothing; the flush loop delivers it.
* `meta.instruction`, when set, is what gets typed into the PTY instead of
* `text` (UI/card surfaces still show `text`). */
- enqueueMessage: (agentId: string, text: string, meta?: { slack?: { channel: string; thread_ts: string }; instruction?: string }) => void;
+ enqueueMessage: (agentId: string, text: string, meta?: { slack?: { channel: string; thread_ts: string }; instruction?: string; precondition?: QueuedMessage['precondition'] }) => void;
/** Drop a single queued message (user removed it, or it was just delivered). */
removeQueuedMessage: (agentId: string, messageId: string) => void;
/** "Send now" while floor auto-delivery is paused: marks the message manual
@@ -843,7 +855,8 @@ export const useStore = create<State>((set) => ({
const msg: QueuedMessage = {
id: newQueuedId(), text: trimmed, ts: Date.now(),
...(meta?.slack ? { slack: meta.slack } : {}),
- ...(meta?.instruction ? { instruction: meta.instruction } : {})
+ ...(meta?.instruction ? { instruction: meta.instruction } : {}),
+ ...(meta?.precondition ? { precondition: meta.precondition } : {})
};
const messageQueues = { ...s.messageQueues, [agentId]: [...(s.messageQueues[agentId] ?? []), msg] };
persistQueues(messageQueues);