diff --git a/src/runner/adapters.ts b/src/runner/adapters.ts index 2f5456a..611573e 100644 --- a/src/runner/adapters.ts +++ b/src/runner/adapters.ts @@ -288,8 +288,10 @@ export interface AcpKept { /** * `end`: what it says once its process has ended, given the exit code, for a tool whose turn ends with the process rather than * with a line of its own (plainText). + * `stderr`: reads what it writes there, for a tool that says something about the turn on it, and gives back the part that is + * still worth reporting as its own words if the turn fails. */ -export type Parser = ((line: string) => AdapterOut[]) & { cancel?: () => void; steer?: (text: string) => Promise; end?: (code: number | null) => AdapterOut[] }; +export type Parser = ((line: string) => AdapterOut[]) & { cancel?: () => void; steer?: (text: string) => Promise; end?: (code: number | null) => AdapterOut[]; stderr?: (chunk: string) => string }; export interface Adapter { tool: EngineTool; @@ -3472,7 +3474,7 @@ export const aider: Adapter = { }, }; -/** Crush's conversation, as Friday keeps it: "its latest one in this folder", which `--continue` picks up. */ +/** A Crush conversation whose id wasn't read: "its latest one in this folder", which `--continue` picks up. */ export const CRUSH_LAST = "crush-latest"; /** Crush's own config folder, where it looks for it: CRUSH_GLOBAL_CONFIG, else $XDG_CONFIG_HOME/crush, else ~/.config/crush (on Windows too). */ @@ -3572,11 +3574,17 @@ export function crushConfig(t: TurnSpec, env: NodeJS.ProcessEnv = process.env, h return dir; } +/** The lines `--verbose` logs: a level, then the message, with a long value carried onto "│" lines. */ +const CRUSH_LOG = /^(?:(?:DEBU|INFO|WARN|ERRO)\b|\s+│)/; +/** The one that names the conversation the run is in, new or carried on. */ +const CRUSH_SESSION = /^INFO (?:Created|Continuing) session for non-interactive run session_id=(\S+)/; + /** * Crush (Charm's coding agent, `crush`), which speaks no ACP: one `crush run` a turn, the instruction on stdin and its answer * printed as plain text (`--quiet`, no spinner). A non-interactive run approves everything itself, so there is no Manual; Plan - * hides its editing and shell tools (crushConfig). It prints no conversation id, so the next turn carries on its most recent - * conversation in this folder (`--continue`). + * hides its editing and shell tools (crushConfig). Its logs (`--verbose`, on stderr) name the conversation's id, which the next + * turn hands back (`--session`), so two Friday sessions in one folder keep their own. Without an id it carries on the most recent + * conversation in the folder (`--continue`). */ export const crush: Adapter = { tool: "crush", @@ -3587,17 +3595,34 @@ export const crush: Adapter = { args: (t) => [ "run", "--quiet", - ...(t.resumeId && !t.ephemeral ? ["--continue"] : []), + ...(t.ephemeral ? [] : ["--verbose"]), + ...(!t.resumeId || t.ephemeral ? [] : t.resumeId === CRUSH_LAST || !offered(t, "--session") ? ["--continue"] : ["--session", t.resumeId]), ...(t.model ? ["--model", t.model] : []), ...(t.effort && offered(t, "--reasoning-effort") ? ["--reasoning-effort", lowMediumHigh(t.effort)] : []), ], - optional: ["--reasoning-effort"], + optional: ["--reasoning-effort", "--session"], env(t): Record { const dir = crushConfig(t); return dir ? { CRUSH_GLOBAL_CONFIG: dir } : {}; }, stdin: (t) => notedPrompt(t), - parser: (t) => plainText(undefined, (ok) => (ok && !t?.ephemeral ? [{ kind: "session", id: CRUSH_LAST }] : [])), + parser(t) { + let id = CRUSH_LAST; + let partial = ""; + const parse = plainText(undefined, (ok) => (ok && !t?.ephemeral ? [{ kind: "session", id }] : [])); + parse.stderr = (chunk) => { + const lines = (partial + chunk).split("\n"); + partial = lines.pop()!; + return lines + .filter((l) => { + id = CRUSH_SESSION.exec(l.trimEnd())?.[1] ?? id; + return !CRUSH_LOG.test(l); + }) + .map((l) => `${l}\n`) + .join(""); + }; + return parse; + }, explain(e) { if (/no providers? (?:configured|found)|not (?:logged|signed) in|unauthori[sz]ed|api key|401/i.test(e)) return "Crush has no model provider set up on the computer it runs on. Run `crush login` there (or set a provider's API key), then send the instruction again."; return e; diff --git a/src/runner/runner.ts b/src/runner/runner.ts index 1664f88..1a32d15 100644 --- a/src/runner/runner.ts +++ b/src/runner/runner.ts @@ -1969,7 +1969,7 @@ ${prompt}`; ); }; listen(); - child.stderr!.on("data", (d: Buffer) => (stderr = (stderr + d.toString()).slice(-4000))); + child.stderr!.on("data", (d: Buffer) => (stderr = (stderr + (parse.stderr?.(d.toString()) ?? d.toString())).slice(-4000))); const lines = createInterface({ input: child.stdout! }); lines.on("line", (line) => { heard.heard(); diff --git a/test/crush-adapter.test.ts b/test/crush-adapter.test.ts index 25d3aae..5bcf4a1 100644 --- a/test/crush-adapter.test.ts +++ b/test/crush-adapter.test.ts @@ -11,17 +11,29 @@ const said = (outs: AdapterOut[]) => outs.filter((o) => o.kind === "delta" && o. test("a Crush turn: the instruction on stdin, its answer as text, and the next turn continues its latest conversation", () => { const crush = ADAPTERS.crush; const turn: TurnSpec = { prompt: "Explain main.go", system: "Be brief.", resumeId: null, mode: "full", model: "gpt-5" }; - assert.deepEqual(crush.args(turn), ["run", "--quiet", "--model", "gpt-5"]); + assert.deepEqual(crush.args(turn), ["run", "--quiet", "--verbose", "--model", "gpt-5"]); assert.match(crush.stdin(turn), /Be brief\.[\s\S]*Explain main\.go$/); const outs = run(crush.parser(turn), ["main.go starts the server."]); assert.equal(said(outs), "main.go starts the server.\n"); assert.deepEqual(outs.slice(-2), [{ kind: "session", id: CRUSH_LAST }, { kind: "done" }]); - assert.deepEqual(crush.args({ ...turn, resumeId: CRUSH_LAST, model: undefined }), ["run", "--quiet", "--continue"]); + assert.deepEqual(crush.args({ ...turn, resumeId: CRUSH_LAST, model: undefined }), ["run", "--quiet", "--verbose", "--continue"]); assert.deepEqual(crush.modes, ["full", "plan"]); assert.deepEqual(crush.login, ["login"]); assert.match(crush.explain("no providers configured"), /crush login/); }); +test("Crush's conversation id is read from its logs and handed back, so the next turn stays in its own conversation", () => { + const crush = ADAPTERS.crush; + const turn: TurnSpec = { prompt: "Go on", system: "", resumeId: null, mode: "full" }; + const parse = crush.parser(turn); + // Split across chunks, as a pipe may deliver it; only what isn't a log line is kept as its own words. + assert.equal(parse.stderr!("INFO Running in non-interactive mode\nINFO Created session for non-inter"), ""); + assert.equal(parse.stderr!("active run session_id=1f0c-9a\nWARN api key expires soon\n │ detail\nError: boom\n"), "Error: boom\n"); + assert.deepEqual(run(parse, ["Done."]).slice(-2), [{ kind: "session", id: "1f0c-9a" }, { kind: "done" }]); + assert.deepEqual(crush.args({ ...turn, resumeId: "1f0c-9a" }), ["run", "--quiet", "--verbose", "--session", "1f0c-9a"]); + assert.deepEqual(crush.args({ ...turn, resumeId: "1f0c-9a", ephemeral: true }), ["run", "--quiet"]); +}); + test("Crush is offered by name with its npm install line on every system", () => { assert.ok(ENGINE_TOOLS.includes("crush")); assert.deepEqual(ENGINE_MODEL_INFO.crush, []);