diff --git a/mod/ovis-composer/delivery.js b/mod/ovis-composer/delivery.js index 7903ea3..b5f8e5f 100644 --- a/mod/ovis-composer/delivery.js +++ b/mod/ovis-composer/delivery.js @@ -151,12 +151,17 @@ export function gateAfter(status, seenWorking) { * "--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. * + * `force` is `--force`: "type even if the program is blocked" (hc 0.1.21). + * Only the person's own Enter, answering an agent that waits for them, asks + * for it - the queue never does. + * * @param {string} terminal * @param {string} text + * @param {{force?:boolean}} [options] * @returns {string[]} */ -export function sendArgs(terminal, text) { - return ["send", "--json", terminal, "--", text]; +export function sendArgs(terminal, text, { force = false } = {}) { + return ["send", "--json", ...(force ? ["--force"] : []), terminal, "--", text]; } /** @@ -185,10 +190,11 @@ export function parseSendArgs(args) { * * @param {Ctx} ctx * @param {string} text + * @param {{force?:boolean}} [options] - `force`: type even if the agent is blocked * @returns {Promise} */ -export async function sendNow(ctx, text) { - const res = await callHc(ctx, sendArgs(ctx.terminal, text)); +export async function sendNow(ctx, text, options = {}) { + const res = await callHc(ctx, sendArgs(ctx.terminal, text, options)); 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 5900ce9..6828e83 100644 --- a/mod/ovis-composer/main.js +++ b/mod/ovis-composer/main.js @@ -1021,7 +1021,10 @@ export default class Composer extends View { this.draining.has(key) || (mode !== "now" && (this.state.queue[key] ?? []).some((entry) => entry.state === "countdown")); if (decision.kind === "send" && allowed && !busy) { - this.sendNow(agent, message, cx); + // 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 + // from the queue. + this.sendNow(agent, message, cx, { force: this.statusOf(agent) === "blocked" }); return; } @@ -1061,7 +1064,7 @@ export default class Composer extends View { } /** The message goes to the terminal now. */ - async sendNow(agent, message, cx) { + async sendNow(agent, message, cx, options = {}) { const key = agentKey(agent); if (this.draining.has(key)) { this.notice("Ta wiadomość jest już w locie.", "error", cx); @@ -1086,7 +1089,9 @@ export default class Composer extends View { this.persistQueue(key); try { - const result = await sendToTerminal({ terminal: agent.address, hc: this.sendHc }, message); + const result = await sendToTerminal({ terminal: agent.address, hc: this.sendHc }, message, { + force: options.force === true, + }); if (result.ok) { this.state = reduce(this.state, { type: "settle", agent: key, id, outcome: "sent" }); this.persistQueue(key); diff --git a/tests/send-args.test.mjs b/tests/send-args.test.mjs index 9123edb..6350528 100644 --- a/tests/send-args.test.mjs +++ b/tests/send-args.test.mjs @@ -33,3 +33,31 @@ test("parseSendArgs: not a send, or a send of another shape, is nothing", () => assert.equal(parseSendArgs(null), null); assert.equal(parseSendArgs([]), null); }); + +test("--force is only there when asked for, and before the `--`", async () => { + assert.deepEqual(sendArgs("t2", "yes", { force: true }), ["send", "--json", "--force", "t2", "--", "yes"]); + assert.deepEqual(sendArgs("t2", "yes", { force: false }), sendArgs("t2", "yes")); + assert.deepEqual(parseSendArgs(sendArgs("t2", "--force x", { force: true })), { terminal: "t2", text: "--force x", force: true }); + assert.equal(parseSendArgs(sendArgs("t2", "--force")).force, false); + + const calls = []; + const ctx = { terminal: "t2", hc: async (args) => (calls.push(args), ok) }; + await sendNow(ctx, "yes", { force: true }); + await sendNow(ctx, "yes"); + assert.equal(calls[0].includes("--force"), true); + assert.equal(calls[1].includes("--force"), false); +}); + +test("the queue never forces: drain sends without --force", async () => { + const { createState, reduce } = await import("../mod/ovis-composer/state.js"); + const { drain } = await import("../mod/ovis-composer/delivery.js"); + const state = reduce(createState(), { type: "queue", agent: "t2", item: { id: "1", text: "q", createdAt: 1 } }); + const calls = []; + const result = await drain( + { agent: "t2", terminal: "t2", status: () => "idle", gate: () => true, hc: async (args) => (calls.push(args), ok) }, + state, + ); + assert.equal(result.sent, true); + assert.equal(calls.length, 1); + assert.equal(calls[0].includes("--force"), false); +});