Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

fix(agents): stop dropping and mangling `cumora reply --file` bodies (#11)

Xialie Zhuang87acdf7ac0c8
diff --git a/server/src/__tests__/agents-cli-parse.test.ts b/server/src/__tests__/agents-cli-parse.test.ts
index 6fea678..e1765d2 100644
--- a/server/src/__tests__/agents-cli-parse.test.ts
+++ b/server/src/__tests__/agents-cli-parse.test.ts
@@ -13,7 +13,7 @@
*/
import { test } from 'node:test'
import assert from 'node:assert/strict'
-import { parseArgs, tokenize, unescapeChat } from '../agents/cli-parse.js'
+import { joinBodyArgs, parseArgs, tokenize, unescapeChat } from '../agents/cli-parse.js'
// ── parseArgs — happy paths ──────────────────────────────────────────────
@@ -103,15 +103,13 @@ test('parseArgs: single dash stays positional; triple dash IS parsed as a flag (
assert.deepEqual(r.flags, { '-weird': true })
})
-test('parseArgs: bare `--` is parsed as a zero-length-key boolean flag', () => {
- // POSIX-ish convention would treat `--` as "end of flags". We don't
- // — `args[0].startsWith('--')` is true, key = ''. Lock in current
- // behaviour; no caller depends on `--` either way today.
+test('parseArgs: bare `--` ends flag parsing (POSIX end-of-flags)', () => {
+ // This used to parse as a zero-length-key flag (`flags[''] = <next token>`),
+ // which swallowed the token after it. The BYOA shim now uses `--` to hand
+ // over --file/--stdin content, so the POSIX meaning is the one that counts.
const r = parseArgs(['--', 'positional-arg'])
- // Current behaviour: key = '' (empty string), value comes from next
- // non-`--` token. So flags[''] = 'positional-arg'.
- assert.equal(r.flags[''], 'positional-arg')
- assert.deepEqual(r.positional, [])
+ assert.equal(r.flags[''], undefined)
+ assert.deepEqual(r.positional, ['positional-arg'])
})
test('parseArgs: --key value where value contains spaces is fine — argv is already split', () => {
@@ -248,3 +246,62 @@ test('tokenize: unterminated quote consumes everything to EOL into one token', (
test('tokenize: tab separator behaves like a space', () => {
assert.deepEqual(tokenize('a\tb\tc'), ['a', 'b', 'c'])
})
+
+// ── `--` end-of-flags (the BYOA --file / --stdin body path) ──────────────────
+// The shim reads --file/--stdin content locally and appends it after a POSIX
+// `--`. Before that marker existed the content was spliced in as a plain
+// positional, so a body starting with `---` parsed as a flag and the reply was
+// silently never posted.
+
+test('parseArgs: `--` ends flag parsing; a `--`-leading body stays positional', () => {
+ const r = parseArgs(['convo-1', '--', '--- a/src/foo.ts\n+++ b/src/foo.ts'])
+ assert.deepEqual(r.positional, ['convo-1', '--- a/src/foo.ts\n+++ b/src/foo.ts'])
+ assert.deepEqual(r.flags, {})
+ assert.equal(r.literalFrom, 1)
+})
+
+test('parseArgs: flags BEFORE `--` are still parsed', () => {
+ // Real shape: cumora reply <convo> --quote <id> -- <file body>
+ const r = parseArgs(['convo-1', '--quote', 'm-abc', '--', '---\ntitle: Status\n---'])
+ assert.deepEqual(r.positional, ['convo-1', '---\ntitle: Status\n---'])
+ assert.deepEqual(r.flags, { quote: 'm-abc' })
+ assert.equal(r.literalFrom, 1)
+})
+
+test('parseArgs: a flag-looking token AFTER `--` is not a flag', () => {
+ const r = parseArgs(['convo-1', '--', '--json'])
+ assert.deepEqual(r.positional, ['convo-1', '--json'])
+ assert.deepEqual(r.flags, {})
+})
+
+test('parseArgs: no `--` leaves literalFrom undefined', () => {
+ const r = parseArgs(['convo-1', 'hello'])
+ assert.equal(r.literalFrom, undefined)
+})
+
+// ── joinBodyArgs ─────────────────────────────────────────────────────────────
+
+test('joinBodyArgs: a shell-quoted body still gets escapes expanded', () => {
+ // The bash tool wraps the body in single quotes, so `\n` arrives literal.
+ const r = parseArgs(['convo-1', 'line one\\nline two'])
+ assert.equal(joinBodyArgs(r, 1), 'line one\nline two')
+})
+
+test('joinBodyArgs: a body after `--` is taken verbatim, escapes intact', () => {
+ // --file content never passed through a shell; expanding `\n` here would
+ // break the code snippets that flag exists to carry.
+ const src = 'console.log("a\\nb");'
+ const r = parseArgs(['convo-1', '--', src])
+ assert.equal(joinBodyArgs(r, 1), src)
+})
+
+test('joinBodyArgs: a verbatim body keeps Windows-style backslashes', () => {
+ const src = 'see C:\\\\Users\\\\dev for the build'
+ const r = parseArgs(['convo-1', '--', src])
+ assert.equal(joinBodyArgs(r, 1), src)
+})
+
+test('joinBodyArgs: joins multiple shell positionals with a space', () => {
+ const r = parseArgs(['convo-1', 'hello', 'world'])
+ assert.equal(joinBodyArgs(r, 1), 'hello world')
+})
diff --git a/server/src/agents/cli-parse.ts b/server/src/agents/cli-parse.ts
index 0aaa56d..b6164f6 100644
--- a/server/src/agents/cli-parse.ts
+++ b/server/src/agents/cli-parse.ts
@@ -16,6 +16,12 @@
export interface ParsedArgs {
positional: string[]
flags: Record<string, string | boolean>
+ /** Index into `positional` at which LITERAL args begin — everything the
+ * caller placed after a POSIX `--` end-of-flags marker. The BYOA shim puts
+ * `--file` / `--stdin` content there, so those args must be taken exactly as
+ * they arrived: not re-read as flags, and not escape-expanded. Absent when
+ * no `--` was present. */
+ literalFrom?: number
}
/**
@@ -31,12 +37,23 @@ export interface ParsedArgs {
* what the bash tool / CLI shim assumes.
*
* Anything not starting with `--` lands in `positional`.
+ *
+ * A bare `--` is the POSIX end-of-flags marker: every remaining token is a
+ * positional even if it starts with `--`. The BYOA shim uses it to hand over
+ * `--file` / `--stdin` content, which routinely begins with `---` (a markdown
+ * rule, a YAML front-matter fence, a `--- a/file.ts` diff header).
*/
export function parseArgs(args: string[]): ParsedArgs {
const positional: string[] = []
const flags: Record<string, string | boolean> = {}
+ let literalFrom: number | undefined
for (let i = 0; i < args.length; i++) {
const a = args[i]
+ if (a === '--') {
+ literalFrom = positional.length
+ for (let j = i + 1; j < args.length; j++) positional.push(args[j])
+ break
+ }
if (a.startsWith('--')) {
const key = a.slice(2)
const eq = key.indexOf('=')
@@ -55,7 +72,28 @@ export function parseArgs(args: string[]): ParsedArgs {
positional.push(a)
}
}
- return { positional, flags }
+ // Only carry `literalFrom` when a `--` was actually seen, so the parsed shape
+ // is byte-for-byte what it always was for every existing call.
+ return literalFrom === undefined ? { positional, flags } : { positional, flags, literalFrom }
+}
+
+/**
+ * Join the positional args from `start` onward into one body string.
+ *
+ * Args that came through a shell get `unescapeChat` (the bash tool wraps a
+ * body in single quotes, so `\n` arrives as two literal characters). Args
+ * after a POSIX `--` do NOT: the shim reads `--file` / `--stdin` content
+ * locally and it travels as JSON, never touched by a shell — that is the whole
+ * point of those flags. Re-expanding escapes there would corrupt exactly the
+ * code snippets the file path exists to carry verbatim, turning a `\n` inside
+ * a string literal into a real line break.
+ */
+export function joinBodyArgs(parsed: ParsedArgs, start: number): string {
+ const literalFrom = parsed.literalFrom ?? parsed.positional.length
+ return parsed.positional
+ .slice(start)
+ .map((part, i) => (start + i >= literalFrom ? part : unescapeChat(part)))
+ .join(' ')
}
/**
diff --git a/server/src/agents/cli.ts b/server/src/agents/cli.ts
index a006424..3f0b27b 100644
--- a/server/src/agents/cli.ts
+++ b/server/src/agents/cli.ts
@@ -46,7 +46,7 @@ async function agentCompany(agentId: string): Promise<string | null> {
/* ============== argv parsing ============== */
-import { parseArgs, unescapeChat, tokenize, type ParsedArgs } from './cli-parse.js'
+import { parseArgs, unescapeChat, joinBodyArgs, tokenize, type ParsedArgs } from './cli-parse.js'
export { tokenize }
// resolveAs lives in ./cli-identity for test-isolation (zero-side-effect
@@ -1506,7 +1506,7 @@ async function cmdReply(parsed: ParsedArgs): Promise<CliResult> {
const me = resolveAs(parsed)
const convoId = parsed.positional[0]
// Strip any hallucinated <tool_call> XML on the way in too — defense in depth.
- const body = unescapeChat(parsed.positional.slice(1).join(' '))
+ const body = joinBodyArgs(parsed, 1)
.replace(/<tool_call>[\s\S]*?<\/tool_call>/gi, '')
.replace(/<function_call>[\s\S]*?<\/function_call>/gi, '')
.trim()
diff --git a/server/src/agents/computer/daemon.ts b/server/src/agents/computer/daemon.ts
index a30619a..aab3aa1 100644
--- a/server/src/agents/computer/daemon.ts
+++ b/server/src/agents/computer/daemon.ts
@@ -589,20 +589,27 @@ const CUMORA_SHIM = `#!/usr/bin/env node
// is mangled by bash BEFORE this shim runs: backticks and $(...) get run as
// commands and collapse to empty, quotes get eaten. So --file <path> /
// --stdin let the body come from a file (written by the editor, no shell) or
- // a pipe; we read it LOCALLY and splice it in as one argument that travels as
+ // a pipe; we read it LOCALLY and pass it as one argument that travels as
// JSON and is never re-parsed by a shell, so code/quotes/$ survive verbatim.
+ //
+ // It goes LAST, behind a POSIX \`--\`, so the server takes it literally. Spliced
+ // in place it was still read as argv: a body starting with \`---\` (markdown
+ // rule, front-matter fence, diff header) parsed as a FLAG and the message was
+ // silently dropped, and escapes inside it were expanded a second time.
var fs = require('fs')
+ var body = null
var fi = argv.indexOf('--file')
if (fi >= 0 && argv[fi + 1] !== undefined) {
- try { argv.splice(fi, 2, fs.readFileSync(argv[fi + 1], 'utf8')) }
+ try { body = fs.readFileSync(argv[fi + 1], 'utf8') }
catch (e) { console.error('cumora: cannot read --file ' + argv[fi + 1]); process.exit(70) }
+ argv.splice(fi, 2)
}
var si = argv.indexOf('--stdin')
if (si >= 0) {
- var s = ''
- try { s = fs.readFileSync(0, 'utf8') } catch (e) {}
- argv.splice(si, 1, s)
+ argv.splice(si, 1)
+ if (body === null) { try { body = fs.readFileSync(0, 'utf8') } catch (e) { body = '' } }
}
+ if (body !== null) argv.push('--', body)
const res = await fetch(url + '/cli', {
method: 'POST',
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },