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
HTML agent-harnessagent-orchestrationagentsai-agentsautonomous-agentsclaude-codecodexcoding-agentsdesktop-appdeveloper-toolselectrongemini-cliharnessharness-engineeringlocal-firstmemorymulti-agentopencodeorchestrationtypescript
fix: don't enqueue /compact for an undeliverable agent, latch at delivery not enqueue
Sachin Singhee806756c732
diff --git a/src/renderer/src/hooks/useHive.ts b/src/renderer/src/hooks/useHive.tsindex 5e10fbca..8be658ca 100644--- a/src/renderer/src/hooks/useHive.ts+++ b/src/renderer/src/hooks/useHive.ts@@ -337,6 +337,15 @@ export function useHive(config: HarnessConfig | null): void {// listPtys on exactly the cadence the drain needs, and one reading keeps the// two loops from disagreeing about whether an agent is quiet.const ptyLastOutput = useRef<Record<string, number>>({});+ /** How long this terminal has been silent, or null when we have no reading+ * (never polled, PTY gone, or it has emitted nothing at all). canDeliverToAgent+ * fails closed on null — unmeasured silence is not evidence of silence.+ * Hook-level (not effect-local) because both the drain effect and the+ * context-trigger effect gate delivery through the same check. */+ const ptyQuietMs = (ptyId: string, now: number): number | null => {+ const last = ptyLastOutput.current[ptyId];+ return typeof last === 'number' && last > 0 ? now - last : null;+ };// #5C/#7C.4 — latest circuit-breaker level per agent. When 'constrained'/// 'stopped' the avatar is pinned to 'looping' and hook events must NOT flip it// back to 'working' (the flicker the spec calls out); only a genuine Stop clears it.@@ -772,13 +781,6 @@ export function useHive(config: HarnessConfig | null): void {const inFlight = new Set<string>();const sendFailures: Record<string, number> = {};- /** How long this terminal has been silent, or null when we have no reading- * (never polled, PTY gone, or it has emitted nothing at all). canDeliverToAgent- * fails closed on null — unmeasured silence is not evidence of silence. */- const ptyQuietMs = (ptyId: string, now: number): number | null => {- const last = ptyLastOutput.current[ptyId];- return typeof last === 'number' && last > 0 ? now - last : null;- };// Send the front of `srcId`'s queue into `target`'s pty (verbatim or wrapped),// gated on the target being idle, free of interactive menus, and off@@ -920,8 +922,13 @@ export function useHive(config: HarnessConfig | null): void {// the one inside dispatch would have changed nothing.if (!a.ptyId || !canDeliverToAgent(a.status, ptyQuietMs(a.ptyId, now), QUIESCE_IDLE_MS)) continue;if (!messageQueues[a.id]?.length) continue;- void dispatch(a.id, a).then(({ sent, message }) => {+ void dispatch(a.id, a).then(({ sent, message }) => {if (sent && message?.slack) void ensureSlackCard(message);+ // Write the compact latch only once delivery genuinely happened — see+ // the comment in fire() above.+ if (sent && message?.compactUsed !== undefined) {+ lastCompactUsed.current[a.id] = message.compactUsed;+ }});}};@@ -1076,10 +1083,18 @@ export function useHive(config: HarnessConfig | null): void {useEffect(() => {if (!config?.onboardingComplete) return;- const fire = (action: 'compact' | 'clear', rule: ContextRule): void => {+ const fire = (action: 'compact' | 'clear', rule: ContextRule): void => {const { agents, messageQueues, enqueueMessage } = useStore.getState();+ const now = Date.now();for (const a of agents) {if (!a.ptyId) continue;+ // Gate #109-2: don't enqueue a context command for an agent that cannot+ // currently receive one (e.g. god 'blocked' on a human prompt). Enqueuing+ // anyway left a stuck /compact at the head of the queue that dedupe then+ // collapsed every subsequent hourly attempt against, forever — the exact+ // same check the drain itself uses immediately before typing, so a+ // command is never queued in a state the drain would refuse to deliver.+ if (!canDeliverToAgent(a.status, ptyQuietMs(a.ptyId, now), QUIESCE_IDLE_MS)) continue;const provider = inferAgentProvider(a.command, a.provider);const command = action === 'clear'? clearCommandForProvider(provider, rule.message)@@ -1108,12 +1123,16 @@ export function useHive(config: HarnessConfig | null): void {// state those thresholds cannot reason about, because nothing they could do// would ever change it. /clear needs no equivalent — the queue drain zeroes// the store reading when it lands.- const used = a.contextTokens ?? 0;- if (action === 'compact') {- if (lastCompactUsed.current[a.id] === used) continue;- lastCompactUsed.current[a.id] = used;- }- enqueueMessage(a.id, command);+ const used = a.contextTokens ?? 0;+ if (action === 'compact' && lastCompactUsed.current[a.id] === used) continue;+ // The latch is written at successful DELIVERY (see the flush() dispatch+ // callback below), not here. Writing it at enqueue time recorded+ // "already compacted at N tokens" for a compaction that might never+ // actually happen — e.g. blocked by the gate just above, or a failed+ // send — silently latching out every future attempt at that count.+ // compactUsed rides on the queued message so the delivery site knows+ // which count to latch.+ enqueueMessage(a.id, command, action === 'compact' ? { compactUsed: used } : undefined);}};diff --git a/src/renderer/src/store/store.ts b/src/renderer/src/store/store.tsindex 7fb1e6a4..c003bee4 100644--- a/src/renderer/src/store/store.ts+++ b/src/renderer/src/store/store.ts@@ -146,6 +146,13 @@ export interface QueuedMessage {* 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';+ /** Token count captured when a /compact was enqueued for this agent, carried+ * through to the point of successful delivery so the "already compacted at+ * N tokens" latch (see useHive) can be written there instead of at enqueue+ * time. Enqueue time is too early: a /compact stuck behind an undeliverable+ * status would otherwise latch out every future compact attempt for a+ * compaction that never actually happened. */+ compactUsed?: number;}// 'files' retired in v0.3.4 (the per-agent IDE button superseded it) — a@@ -292,7 +299,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; precondition?: QueuedMessage['precondition'] }) => void;+ enqueueMessage: (agentId: string, text: string, meta?: { slack?: { channel: string; thread_ts: string }; instruction?: string; precondition?: QueuedMessage['precondition']; compactUsed?: number }) => 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@@ -888,11 +895,12 @@ export const useStore = create<State>((set, get) => ({if (isInboxNudge(trimmed) && queued.some((m) => isInboxNudge(m.text))) {return s;}- const msg: QueuedMessage = {+ const msg: QueuedMessage = {id: newQueuedId(), text: trimmed, ts: Date.now(),...(meta?.slack ? { slack: meta.slack } : {}),...(meta?.instruction ? { instruction: meta.instruction } : {}),- ...(meta?.precondition ? { precondition: meta.precondition } : {})+ ...(meta?.precondition ? { precondition: meta.precondition } : {}),+ ...(meta?.compactUsed !== undefined ? { compactUsed: meta.compactUsed } : {})};const messageQueues = { ...s.messageQueues, [agentId]: [...(s.messageQueues[agentId] ?? []), msg] };persistQueues(messageQueues);