From 3b7b4618a3ab0bf613ec614e8cf3dfd5b3fd083d Mon Sep 17 00:00:00 2001 From: Factor Date: Thu, 19 Nov 2026 12:08:13 +0100 Subject: [PATCH] fix: hc send text after --, history reads send args by name Co-Authored-By: Ovis Fast (Claude) --- mod/ovis-composer/delivery.js | 34 +++++++++++++++++++++++++++++++++- mod/ovis-composer/main.js | 5 +++-- tests/send-args.test.mjs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 tests/send-args.test.mjs diff --git a/mod/ovis-composer/delivery.js b/mod/ovis-composer/delivery.js index f733f8c..7903ea3 100644 --- a/mod/ovis-composer/delivery.js +++ b/mod/ovis-composer/delivery.js @@ -144,6 +144,38 @@ export function gateAfter(status, seenWorking) { return status === "idle" || status === "done"; } +/** + * The arguments of `hc send`. + * + * The text goes after `--`: a message that starts with a dash ("- fix bug", + * "--help") would otherwise be read by hc as an option and fail with a usage + * error. Everything after `--` is the text, whatever it looks like. + * + * @param {string} terminal + * @param {string} text + * @returns {string[]} + */ +export function sendArgs(terminal, text) { + return ["send", "--json", terminal, "--", text]; +} + +/** + * The fields of the arguments `sendArgs` built - so a caller that watches + * the calls (the history) reads them by name, not by position. + * + * @param {unknown} args + * @returns {{terminal:string, text:string, force:boolean}|null} null when this is not a send + */ +export function parseSendArgs(args) { + if (!Array.isArray(args) || args[0] !== "send") return null; + const dashes = args.indexOf("--"); + if (dashes < 0 || args.length !== dashes + 2) return null; + + const terminal = args.slice(1, dashes).find((arg) => typeof arg === "string" && !arg.startsWith("-")); + if (typeof terminal !== "string" || typeof args[dashes + 1] !== "string") return null; + return { terminal, text: args[dashes + 1], force: args.slice(1, dashes).includes("--force") }; +} + /** * Paste the text into the terminal and press Enter. * @@ -156,7 +188,7 @@ export function gateAfter(status, seenWorking) { * @returns {Promise} */ export async function sendNow(ctx, text) { - const res = await callHc(ctx, ["send", ctx.terminal, text, "--json"]); + const res = await callHc(ctx, sendArgs(ctx.terminal, text)); if (res.failure) { // The runner itself failed - there is no exit code to report. diff --git a/mod/ovis-composer/main.js b/mod/ovis-composer/main.js index 6005937..5900ce9 100644 --- a/mod/ovis-composer/main.js +++ b/mod/ovis-composer/main.js @@ -39,7 +39,7 @@ import * as fs from "fs/promises"; import { createState, reduce } from "./state.js"; import { keyIntent } from "./keys.js"; -import { decide, shouldDrain, shouldStartCountdown, sendNow as sendToTerminal } from "./delivery.js"; +import { decide, shouldDrain, shouldStartCountdown, 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 } from "./attachments.js"; @@ -1896,7 +1896,8 @@ export default class Composer extends View { */ async runSend(args) { const res = await hc(args); - if (args[0] === "send" && hcOk(res)) this.recordSent(String(args[1]), String(args[2] ?? "")); + const sent = parseSendArgs(args); + if (sent && hcOk(res)) this.recordSent(sent.terminal, sent.text); return res; } diff --git a/tests/send-args.test.mjs b/tests/send-args.test.mjs new file mode 100644 index 0000000..9123edb --- /dev/null +++ b/tests/send-args.test.mjs @@ -0,0 +1,35 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { sendArgs, parseSendArgs, sendNow } from "../mod/ovis-composer/delivery.js"; + +const ok = { code: 0, stdout: "{}", stderr: "" }; + +test("sendArgs puts the text after `--`, so hc cannot read it as an option", () => { + assert.deepEqual(sendArgs("t2", "hello"), ["send", "--json", "t2", "--", "hello"]); +}); + +test("a text that looks like an option still goes through as the text", async () => { + for (const text of ["- fix", "--help", "--force x", "-", "--"]) { + const calls = []; + const result = await sendNow({ terminal: "t2", hc: async (args) => (calls.push(args), ok) }, text); + assert.equal(result.ok, true, text); + const args = calls[0]; + const at = args.indexOf("--"); + assert.equal(args[at + 1], text, text); + assert.equal(args.length, at + 2, text); + assert.deepEqual(args.slice(0, at), ["send", "--json", "t2"], text); + } +}); + +test("parseSendArgs reads the named fields back", () => { + for (const text of ["hello", "- fix", "--help", "--force x", "z \"q\" ęą"]) { + assert.deepEqual(parseSendArgs(sendArgs("t7", text)), { terminal: "t7", text, force: false }); + } +}); + +test("parseSendArgs: not a send, or a send of another shape, is nothing", () => { + assert.equal(parseSendArgs(["get", "t2", "--json"]), null); + assert.equal(parseSendArgs(["send", "t2", "text", "--json"]), null); + assert.equal(parseSendArgs(null), null); + assert.equal(parseSendArgs([]), null); +});