fix: hc send text after --, history reads send args by name

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 12:08:13 +01:00
1 parent 2fd0234831
commit 3b7b4618a3
3 files changed
+71 -3

No files matched your search

+33 -1
View File
@@ -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<Result>}
*/
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.
+3 -2
View File
@@ -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;
}
+35
View File
@@ -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);
});