Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/runner/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>; end?: (code: number | null) => AdapterOut[] };
export type Parser = ((line: string) => AdapterOut[]) & { cancel?: () => void; steer?: (text: string) => Promise<boolean>; end?: (code: number | null) => AdapterOut[]; stderr?: (chunk: string) => string };

export interface Adapter {
tool: EngineTool;
Expand Down Expand Up @@ -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). */
Expand Down Expand Up @@ -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",
Expand All @@ -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<string, string> {
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;
Expand Down
2 changes: 1 addition & 1 deletion src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 14 additions & 2 deletions test/crush-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, []);
Expand Down
Loading