From 2dbb3fe23e37813212ab8ceaf29e940e5aab27ab Mon Sep 17 00:00:00 2001 From: Factor Date: Thu, 19 Nov 2026 12:10:07 +0100 Subject: [PATCH] fix: a new message goes behind queued entries, not ahead of them Co-Authored-By: Ovis Fast (Claude) --- mod/ovis-composer/delivery.js | 11 +++++++++++ mod/ovis-composer/main.js | 14 ++++++++++---- tests/delivery.test.mjs | 11 +++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/mod/ovis-composer/delivery.js b/mod/ovis-composer/delivery.js index b5f8e5f..52fb572 100644 --- a/mod/ovis-composer/delivery.js +++ b/mod/ovis-composer/delivery.js @@ -130,6 +130,17 @@ export function shouldStartCountdown({ status, queue, gateOpen, stopped }) { return queue.some((item) => item.state === "queued"); } +/** + * Does a message wait in the queue (`queued`, or counting down)? Then a new + * message does not go out ahead of it - it takes its place at the end. + * + * @param {QueueItem[]|undefined} queue + * @returns {boolean} + */ +export function queueBlocksDirectSend(queue) { + return (Array.isArray(queue) ? queue : []).some((item) => item.state === "queued" || item.state === "countdown"); +} + /** * The turn gate: the next message goes out only after a whole turn was seen - * the agent was working and then came back to idle/done. Without that diff --git a/mod/ovis-composer/main.js b/mod/ovis-composer/main.js index 16f1817..69643c5 100644 --- a/mod/ovis-composer/main.js +++ b/mod/ovis-composer/main.js @@ -39,7 +39,14 @@ import * as fs from "fs/promises"; import { createState, reduce } from "./state.js"; import { keyIntent } from "./keys.js"; -import { decide, shouldDrain, shouldStartCountdown, parseSendArgs, sendNow as sendToTerminal } from "./delivery.js"; +import { + decide, + shouldDrain, + shouldStartCountdown, + queueBlocksDirectSend, + parseSendArgs, + sendNow as sendToTerminal, +} from "./delivery.js"; import { detect as detectTrigger, rank, merge, startsLine } from "./suggest.js"; import * as catalog from "./catalog.js"; import { LIMITS, validate, mention, removeMention, kindOf, formatBytes, relativeTo } from "./attachments.js"; @@ -1055,12 +1062,11 @@ export default class Composer extends View { // Not before the draft and the identity of the agent were checked - the // person's own "send now" (Ctrl+Enter) is the exception. const allowed = mode === "now" || canAutoSend(this.draftReady, key); - // A message that is on the wire, or one that counts down, goes first: the + // A message that is on the wire, waits in the queue or counts down goes first: the // new one waits behind it in the queue instead of vanishing or jumping // the line (Ctrl+Enter is the person's own order and does jump). const busy = - this.draining.has(key) || - (mode !== "now" && (this.state.queue[key] ?? []).some((entry) => entry.state === "countdown")); + this.draining.has(key) || (mode !== "now" && queueBlocksDirectSend(this.state.queue[key])); if (decision.kind === "send" && allowed && !busy) { // The person's own Enter answers an agent that waits for them: it goes // through even though the agent is blocked (`hc send --force`). Never diff --git a/tests/delivery.test.mjs b/tests/delivery.test.mjs index 63b3022..d52548c 100644 --- a/tests/delivery.test.mjs +++ b/tests/delivery.test.mjs @@ -105,3 +105,14 @@ test("shouldStartCountdown: stopped or closed gate blocks", () => { test("shouldStartCountdown tolerates a non-array queue", () => { assert.equal(shouldStartCountdown(sc({ queue: null })), false); }); + +import { queueBlocksDirectSend } from "../mod/ovis-composer/delivery.js"; + +test("queueBlocksDirectSend: a message that waits (queued or counting down) goes first", () => { + assert.equal(queueBlocksDirectSend([{ state: "queued" }]), true); + assert.equal(queueBlocksDirectSend([{ state: "countdown" }]), true); + assert.equal(queueBlocksDirectSend([{ state: "held" }, { state: "uncertain" }]), false); + assert.equal(queueBlocksDirectSend([{ state: "sending" }]), false); + assert.equal(queueBlocksDirectSend([]), false); + assert.equal(queueBlocksDirectSend(undefined), false); +});