Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(cli): strip malformed Alook heredoc tail (#537)

Gustavo Ye9f744b0625cb
diff --git a/src/daemon/src/cli/index.test.ts b/src/daemon/src/cli/index.test.ts
index 8c6f682be..6aba48832 100644
--- a/src/daemon/src/cli/index.test.ts
+++ b/src/daemon/src/cli/index.test.ts
@@ -1763,6 +1763,47 @@ describe("message send — literal stdin/file body contract", () => {
expect(fs.existsSync(marker)).toBe(false);
});
+ it.each([
+ ["body\nALOOK_MESSAGE_5211_MERGE_POLICY_2'\n", "body\n"],
+ ['body\r\nALOOK_MESSAGE_7F3C"\r\n', "body\r\n"],
+ ["body\n'ALOOK_MESSAGE_7F3C'\n", "body\n"],
+ ])("removes a malformed final Alook heredoc marker from stdin", async (input, expected) => {
+ let sentText: string | undefined;
+ setApiForTesting(stubApi({
+ send: async (request: Parameters<ServerApi["send"]>[0]) => {
+ sentText = request.content.text;
+ return { state: "sent", message: { seq: "#1", channel: "/s/c", sender: "@a", content: { text: "" }, time: "" } };
+ },
+ }));
+
+ await mainWithStdin(
+ ["message", "send", "--target", "/s#0042/general", "--stdin", "--remind-after", "0"],
+ input,
+ );
+
+ expect(sentText).toBe(expected);
+ });
+
+ it.each([
+ ["body\nALOOK_MESSAGE_7F3C\n"],
+ ["body\nALOOK_MESSAGE_7F3C'\nmore\n"],
+ ])("preserves marker-like stdin outside the malformed terminal boundary", async (input) => {
+ let sentText: string | undefined;
+ setApiForTesting(stubApi({
+ send: async (request: Parameters<ServerApi["send"]>[0]) => {
+ sentText = request.content.text;
+ return { state: "sent", message: { seq: "#1", channel: "/s/c", sender: "@a", content: { text: "" }, time: "" } };
+ },
+ }));
+
+ await mainWithStdin(
+ ["message", "send", "--target", "/s#0042/general", "--stdin", "--remind-after", "0"],
+ input,
+ );
+
+ expect(sentText).toBe(input);
+ });
+
it("decodes UTF-8 once after joining split stdin byte chunks", async () => {
const literal = "中文 🎉";
const bytes = Buffer.from(literal, "utf8");
@@ -1784,7 +1825,7 @@ describe("message send — literal stdin/file body contract", () => {
const path = await import("path");
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cli-esc-"));
const file = path.join(dir, "body.txt");
- const literal = " a\\\\nb\n总部 🎉 \n";
+ const literal = " a\\\\nb\n总部 🎉\nALOOK_MESSAGE_FILE'\n";
fs.writeFileSync(file, literal);
let sentText: string | undefined;
setApiForTesting(
@@ -1834,6 +1875,31 @@ describe("message send — literal stdin/file body contract", () => {
expect(sendSpy).toHaveBeenCalledWith(expect.objectContaining({ content: { text: " \n" } }));
});
+ it("validates the repaired body before sending", async () => {
+ const sendSpy = vi.fn(async () => ({
+ state: "sent" as const,
+ message: { seq: "#1", channel: "/s/c", sender: "@a", content: { text: "" }, time: "" },
+ }));
+ setApiForTesting(stubApi({ send: sendSpy }));
+
+ await mainWithStdin(
+ ["message", "send", "--target", "/s#0042/general", "--stdin", "--remind-after", "0"],
+ "ALOOK_MESSAGE_ONLY'\n",
+ );
+ expect(parseEnvelope(cap.lines()).error).toContain("--stdin");
+ expect(sendSpy).not.toHaveBeenCalled();
+
+ cap.lines().length = 0;
+ await mainWithStdin(
+ [
+ "message", "send", "--target", "/s#0042/general", "--stdin", "--remind-after", "0",
+ "--attachment", "att_1",
+ ],
+ "ALOOK_MESSAGE_ONLY'\n",
+ );
+ expect(sendSpy).toHaveBeenCalledWith(expect.objectContaining({ content: { text: "" } }));
+ });
+
it("keeps attachment-only sends valid without reading stdin", async () => {
const sendSpy = vi.fn(async () => ({
state: "sent" as const,
diff --git a/src/daemon/src/cli/index.ts b/src/daemon/src/cli/index.ts
index b7ad03be1..5606907a3 100755
--- a/src/daemon/src/cli/index.ts
+++ b/src/daemon/src/cli/index.ts
@@ -196,6 +196,13 @@ async function readLiteralInput(args: {
return undefined;
}
+const MALFORMED_ALOOK_HEREDOC_TAIL =
+ /(^|\r?\n)(?:["']ALOOK_MESSAGE_[A-Z0-9_]+["']?|ALOOK_MESSAGE_[A-Z0-9_]+["'])(?:\r?\n)?$/;
+
+function stripMalformedAlookHeredocTail(input: string): string {
+ return input.replace(MALFORMED_ALOOK_HEREDOC_TAIL, "$1");
+}
+
/* ------------------------------------------------------------------ */
/* Commands */
/* ------------------------------------------------------------------ */
@@ -302,13 +309,16 @@ async function cmdMessageSend(opts: Record<string, unknown>, stdin: CliInputStre
if (!channel) throw new CliError("message send: --target <ref> is required (e.g. /demo-workspace#1234/general)");
const fileFlag = opts.file as string | undefined;
- const text = await readLiteralInput({
+ const literalText = await readLiteralInput({
command: "message send",
stdinSelected: opts.stdin === true,
stdin,
filePath: fileFlag,
fileOption: "--file <path>",
});
+ const text = opts.stdin === true && literalText !== undefined
+ ? stripMalformedAlookHeredocTail(literalText)
+ : literalText;
// `--attachment` may repeat. Commander wires this via `.option(..., collect, [])`
// below; treat a missing flag as an empty list.