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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ opt-level = 2
# (`handle_completion_trigger` in src/input/editor/lsp/completions.rs). It also makes `has_multiple_cursors`, `select_range`
# and `replace_range` public, which the line actions (src/app/line_actions.rs) edit through, and makes a drag that began
# with a double click select whole words (`snap_to_word_edge` in src/input/base/selection.rs). `SelectableText::runs` lets
# a card's text keep its styling while selectable.
# a card's text keep its styling while selectable. `code_background` (src/text/inline_flow.rs) covers inline code's
# whole glyph box rather than its capital height, so descenders stay inside it.
[patch.crates-io]
gpui-base = { path = "vendor/gpui-base" }

Expand Down
2 changes: 1 addition & 1 deletion README.md

Large diffs are not rendered by default.

162 changes: 152 additions & 10 deletions bridge/claude.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Only the part of ACP m4code uses is here: initialize, session/new, session/load, session/list, session/prompt,
// session/cancel, session/set_mode and session/set_config_option, and session/request_permission back to m4code.
// `_m4code/steer` is m4code's own: a message for the turn that is running, which ACP has no request for.
// stdout carries the protocol only, so anything logged goes to stderr.

import { randomUUID } from "node:crypto";
Expand Down Expand Up @@ -213,11 +214,16 @@ async function permission(session, name, input, options) {
return { behavior: "deny", message: "The user declined this.", interrupt: chosen === null };
}

/** Claude's questions to the user, one at a time, each with its options. A multiple-choice question takes one answer. */
/**
* Claude's questions to the user, one at a time, each with its options. A multiple-choice question takes one answer.
* ACP has no free-text answer, so `_meta.m4code.freeText` tells m4code the user may type their own instead, and
* m4code sends those words back in the answer's `_meta.m4code.text`.
*/
async function askQuestions(session, input, options) {
const answers = {};
for (const q of input?.questions ?? []) {
const answer = await ask("session/request_permission", {
_meta: { m4code: { freeText: true } },
sessionId: session.id,
toolCall: { toolCallId: `${options.toolUseID}-${q.question}`, title: q.question, kind: "other", status: "pending" },
options: [
Expand All @@ -227,7 +233,9 @@ async function askQuestions(session, input, options) {
});
const chosen = answer?.outcome?.outcome === "selected" ? answer.outcome.optionId : null;
if (chosen === null) return { behavior: "deny", message: "The user dismissed the question.", interrupt: true };
if (chosen !== "skip") answers[q.question] = q.options[Number(chosen)].label;
const own = answer?._meta?.m4code?.text;
if (typeof own === "string" && own.trim()) answers[q.question] = own.trim();
else if (chosen !== "skip" && q.options[Number(chosen)]) answers[q.question] = q.options[Number(chosen)].label;
}
return { behavior: "allow", updatedInput: { ...input, answers } };
}
Expand All @@ -240,13 +248,38 @@ function stopReason(session, result) {
return "end_turn";
}

function finishTurn(session, result) {
/** The uuids of the messages a result answered: every one Claude took into that turn. */
function answeredBy(result) {
if (Array.isArray(result.user_message_uuids)) return result.user_message_uuids;
return result.user_message_uuid ? [result.user_message_uuid] : null;
}

/**
* A result ends the turn m4code sees once it has answered every message sent in it. A steer Claude takes between tool
* calls is answered by the same result; one it takes after the turn gets a result of its own, so the turn stays open
* for it. A Claude Code too old to say which messages a result answered ends the turn at its first result, as before.
*/
async function finishTurn(session, result) {
const turn = session.turn;
if (turn && !session.cancelled && !result.is_error) {
const answered = answeredBy(result);
// A Claude Code that never said it started a steer has answered it by now, so it shows before the turn ends.
showWaitingSteers(session, answered);
if (answered) for (const uuid of answered) turn.waiting.delete(uuid);
else turn.waiting.clear();
if (turn.waiting.size) {
turn.held = result;
return;
}
}
session.turn = null;
// The receipt lists the messages Claude Code still means to run, which is how `unreadSteers` tells a steer it
// dropped from one it already answered. An older Claude Code sends none, and its steers are left to run.
const receipt = session.cancelled ? await session.interrupted : undefined;
if (turn) {
const failed = result.is_error && !session.cancelled;
if (failed) turn.reject(new Error(result.result || (result.errors ?? []).join("\n") || "Claude Code stopped with an error."));
else turn.resolve({ stopReason: stopReason(session, result) });
else turn.resolve({ stopReason: stopReason(session, result), ...steersBack(session, turn, receipt) });
}
// The context size is only shown, so the turn ends without waiting for it.
session.query
Expand All @@ -255,15 +288,69 @@ function finishTurn(session, result) {
.catch(() => {});
}

/**
* The steers the turn ends without having answered, in the reply's `_meta.m4code`. `unread` are those Claude had not
* read when the user stopped the turn: Claude Code keeps them and would run them as a turn of their own, which the
* user just asked to stop, so the bridge stops that turn too (`dropping`) and m4code puts their text back in the
* message box. `resend` are those Claude dropped unread on its own, which m4code sends as the next turn.
*/
function steersBack(session, turn, receipt) {
const stillQueued = new Set(receipt?.still_queued ?? []);
const unread = [...turn.waiting].filter((uuid) => turn.steers.has(uuid) && !turn.shown.has(uuid) && stillQueued.has(uuid));
for (const uuid of unread) session.dropping.add(uuid);
const back = {};
if (unread.length) back.unread = unread.map((uuid) => turn.steers.get(uuid));
if (turn.dropped.length) back.resend = turn.dropped;
return Object.keys(back).length ? { _meta: { m4code: back } } : {};
}

/** Claude Code says it took a steer off its queue without reading it (the states t3code treats the same way). */
function steerDropped(session, m) {
const turn = session.turn;
if (!turn || session.cancelled || !["cancelled", "discarded", "refused"].includes(m.state) || !turn.steers.has(m.command_uuid)) return;
if (!turn.waiting.delete(m.command_uuid)) return;
turn.dropped.push(turn.steers.get(m.command_uuid));
if (turn.waiting.size === 0 && turn.held) return finishTurn(session, turn.held);
}

/** The session's messages from the SDK, as updates to m4code, until the session ends. */
async function pump(session) {
try {
for await (const m of session.query) {
if (session.dropping.size && !session.turn) {
if (m.type === "result") {
for (const uuid of answeredBy(m) ?? [...session.dropping]) session.dropping.delete(uuid);
session.dropInterrupted = false;
} else if (m.type === "command_lifecycle" && ["cancelled", "discarded", "refused"].includes(m.state)) {
session.dropping.delete(m.command_uuid);
} else if (!session.dropInterrupted) {
session.dropInterrupted = true;
session.query.interrupt().catch(() => {});
}
if (!session.dropping.size) session.dropsDone?.();
continue;
}
// Not in the SDK's types yet; t3code reads it the same way.
if (m.type === "command_lifecycle") {
if (m.state === "started") showSteer(session, m.command_uuid);
await steerDropped(session, m);
continue;
}
// A subagent's own steps stay inside its tool call.
if (m.parent_tool_use_id) continue;
if (m.type === "stream_event") {
const ev = m.event;
if (ev.type === "message_start") session.streamed.add(ev.message.id);
if (ev.type === "message_start") {
session.streamed.add(ev.message.id);
// After a result the turn stayed open for, what Claude writes next answers the oldest steer it still
// owed; a later one may still be waiting, so it shows when Claude starts or answers it.
const turn = session.turn;
if (turn?.held) {
turn.held = null;
const oldest = [...turn.waiting].find((uuid) => turn.steers.has(uuid) && !turn.shown.has(uuid));
if (oldest) showSteer(session, oldest);
}
}
if (ev.type === "content_block_delta" && ev.delta.type === "text_delta") update(session.id, { sessionUpdate: "agent_message_chunk", content: { type: "text", text: ev.delta.text } });
if (ev.type === "content_block_delta" && ev.delta.type === "thinking_delta") update(session.id, { sessionUpdate: "agent_thought_chunk", content: { type: "text", text: ev.delta.thinking } });
} else if (m.type === "assistant") {
Expand All @@ -280,20 +367,23 @@ async function pump(session) {
update(session.id, { sessionUpdate: "current_mode_update", currentModeId: session.mode });
}
} else if (m.type === "result") {
finishTurn(session, m);
await finishTurn(session, m);
}
}
} catch (error) {
session.turn?.reject(error);
session.turn = null;
}
// Claude Code ended with a turn still open, such as one held for a steer it never answered.
session.turn?.reject(new Error("Claude Code stopped before it finished the turn."));
session.turn = null;
sessions.delete(session.id);
}

async function open(params, resume) {
const id = resume ?? randomUUID();
const extra = params._meta?.claudeCode?.options ?? {};
const session = { id, cwd: params.cwd, inbox: new Inbox(), tools: new Map(), streamed: new Set(), turn: null, cancelled: false, mode: "default", model: "", effort: "medium", models: [] };
const session = { id, cwd: params.cwd, inbox: new Inbox(), tools: new Map(), streamed: new Set(), turn: null, cancelled: false, interrupted: null, dropping: new Set(), dropInterrupted: false, dropsDone: null, mode: "default", model: "", effort: "medium", models: [] };
session.query = query({
prompt: session.inbox,
options: {
Expand Down Expand Up @@ -369,11 +459,26 @@ const handlers = {
if (!s) throw new Error("That session has ended.");
if (s.turn) throw new Error("A turn is already running.");
s.cancelled = false;
// Steers the user stopped still run in Claude Code first; this message waits until they are gone, so their
// output and result can't be taken for its own.
await afterDropped(s);
const text = (p.prompt ?? []).filter((c) => c.type === "text").map((c) => c.text).join("\n");
const done = new Promise((resolve, reject) => (s.turn = { resolve, reject }));
s.inbox.push({ type: "user", message: { role: "user", content: [{ type: "text", text }] }, parent_tool_use_id: null, session_id: s.id, origin: { kind: "human" } });
const uuid = randomUUID();
const done = new Promise((resolve, reject) => (s.turn = { resolve, reject, waiting: new Set([uuid]), steers: new Map(), shown: new Set(), dropped: [], held: null }));
s.inbox.push(userMessage(s, text, uuid));
return done;
},
"_m4code/steer": async (p) => {
const s = sessions.get(p.sessionId);
if (!s) throw new Error("That session has ended.");
if (!s.turn || s.cancelled) throw new Error("No turn is running.");
const uuid = randomUUID();
s.turn.waiting.add(uuid);
s.turn.steers.set(uuid, p.text);
// "next" waits for the tool calls running to finish; "now" would cancel them, as t3code found.
s.inbox.push(userMessage(s, p.text, uuid, "next"));
return {};
},
"session/set_mode": async (p) => {
const s = sessions.get(p.sessionId);
await s.query.setPermissionMode(p.modeId);
Expand All @@ -396,11 +501,48 @@ const handlers = {
},
};

/**
* Resolves once the stopped steers (`dropping`) have run and been stopped, or after 10 s: Claude Code's receipt said
* it would run them, and one that hasn't started by then is taken as gone, so a new message never waits forever.
*/
async function afterDropped(session) {
if (session.dropping.size) {
await new Promise((resolve) => {
session.dropsDone = resolve;
setTimeout(resolve, 10_000);
});
}
session.dropsDone = null;
session.dropping.clear();
session.dropInterrupted = false;
}

function userMessage(session, text, uuid, priority) {
return { type: "user", uuid, ...(priority ? { priority } : {}), message: { role: "user", content: [{ type: "text", text }] }, parent_tool_use_id: null, session_id: session.id, origin: { kind: "human" } };
}

/**
* Show a steer in the conversation once Claude reads it, not when it was sent: what Claude was still writing then
* comes first. m4code knows it by `_meta.m4code.steer` and takes it off the list of steers waiting to be read.
*/
function showSteer(session, uuid) {
const turn = session.turn;
const text = turn?.steers.get(uuid);
if (text === undefined || turn.shown.has(uuid)) return;
turn.shown.add(uuid);
update(session.id, { sessionUpdate: "user_message_chunk", content: { type: "text", text }, _meta: { m4code: { steer: true } } });
}

/** Every steer not shown yet, oldest first: Claude is about to answer them, or just did. */
function showWaitingSteers(session, only) {
for (const uuid of session.turn?.steers.keys() ?? []) if (!only || only.includes(uuid)) showSteer(session, uuid);
}

function cancel(p) {
const s = sessions.get(p.sessionId);
if (!s?.turn) return;
s.cancelled = true;
s.query.interrupt().catch(() => {});
s.interrupted = s.query.interrupt().catch(() => undefined);
}

async function handle(m) {
Expand Down
3 changes: 2 additions & 1 deletion src/app/act.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ impl Workspace {
);
let updates = s.update_url.as_deref() != Some("");
let chrome = s.chrome_default;
let s_follow_up = s.follow_up;
let default_model = s.default_model();
let claude_hint = self.claude_exe.as_ref().map_or("not found".to_string(), |p| p.to_string_lossy().into_owned());
let defaults = s.new_session.clone();
Expand All @@ -356,7 +357,7 @@ impl Workspace {
.filter(|id| modes.iter().any(|(mid, _)| mid == id))
.or_else(|| modes.first().map(|(id, _)| id.clone()))
.unwrap_or_else(|| "default".into());
self.modal = Some(Modal::Settings { shell, claude, default_model, effort: defaults.effort.unwrap_or_default(), permission: initial_perm, updates, chrome, scale: crate::panels::scale::percent(), m4code_off: self.store.data.settings.m4code_tools_off.clone(), skills_field, skill_search, costly: claude::costly::scan(), list_focus, renaming: None, subagents });
self.modal = Some(Modal::Settings { shell, claude, default_model, effort: defaults.effort.unwrap_or_default(), permission: initial_perm, updates, chrome, scale: crate::panels::scale::percent(), follow_up: s_follow_up, m4code_off: self.store.data.settings.m4code_tools_off.clone(), skills_field, skill_search, costly: claude::costly::scan(), list_focus, renaming: None, subagents });
// After the modal exists: a page that loads something reads the dialog's fields.
self.set_settings_page(self.settings_page, cx);
}
Expand Down
8 changes: 5 additions & 3 deletions src/app/headless/agents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,10 @@ fn a_missing_claude_offers_the_setup_window() {
/// exit is a toast, and the worktree stays: Claude Code already asked on `/exit`, and the X is the path that offers.
#[test]
fn a_process_that_ends_closes_the_session_and_leaves_its_worktree() {
let data = tempfile::tempdir().unwrap();
unsafe { std::env::set_var("M4CODE_DATA_DIR", data.path()) };
// Not `#[ignore]`d, so it runs beside the unit tests: it takes the run's one data folder rather than setting
// `M4CODE_DATA_DIR` to its own, which would move theirs while they read it (#149). Its repository is new, so
// its worktree folder and project are its own there.
crate::datadir::use_throwaway_folders();
let work = tempfile::tempdir().unwrap();
let repo_dir = dunce::canonicalize(work.path()).unwrap().join("proj");
let git = |args: &[&str]| std::process::Command::new("git").args(args).current_dir(&repo_dir).output().unwrap();
Expand All @@ -360,7 +362,7 @@ fn a_process_that_ends_closes_the_session_and_leaves_its_worktree() {
let (window, ws) = cx
.update(|app| gpui_kit::open_window(Default::default(), app, |window, app| app.new(|cx| Workspace::new(window, vec![repo_dir.clone()], cx))))
.unwrap();
let project = ws.read_with(&cx, |ws, _| ws.store.data.projects[0].id.clone());
let project = ws.read_with(&cx, |ws, _| ws.store.data.projects.iter().find(|p| p.path == repo_dir).expect("the project opened").id.clone());
let step = |cx: &mut TestAppContext, f: &dyn Fn(&mut Workspace, &mut Window, &mut Context<Workspace>)| {
cx.update_window(window, |_, window, app| ws.update(app, |ws, cx| f(ws, window, cx))).unwrap();
cx.update_window(window, |_, window, app| window.draw(app).clear(app)).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions src/app/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub fn shortcuts() -> Vec<Shortcut> {
row("Sessions", &["secondary-shift-w"], "Close the session"),
row("Sessions", &["secondary-z"], "Reopen the last closed session"),
row("Chat", &["enter"], "Send the message"),
row("Chat", &["shift-enter"], "Start a new line"),
row("Chat", &["secondary-enter"], "While the agent works, queue instead of steering, or steer instead of queueing"),
row("Chat", &["up", "down", "enter"], "Pick an answer to the agent's question"),
row("Chat", &["escape", "ctrl-c"], "Stop the agent's turn"),
row("Chat", &["escape escape"], "Edit your last message, or clear the one you're typing"),
row("Chat", &["shift-tab"], "Next permission mode"),
Expand Down
6 changes: 5 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ pub enum Modal {
updates: bool,
chrome: bool,
scale: u32,
follow_up: crate::store::FollowUp,
/// m4code tools turned off (m4code tools page).
m4code_off: Vec<crate::claude::mcp::Feature>,
/// Skills to turn off, separated by commas (Claude Code tools page).
Expand Down Expand Up @@ -592,6 +593,8 @@ pub struct Workspace {
pub window_title: String,
/// The session being dragged in the sidebar (lifted in place while its ghost follows the cursor).
pub dragging: Option<Id>,
/// The right sidebar card being dragged, lifted in place like a session row.
pub dragging_card: Option<crate::store::SideCard>,
/// Pointer position at the last drag-over check, see `pointer_moved`.
/// Sidebar reorder animation: last drawn order of the running rows, and the slides in flight.
pub prev_current: Vec<Id>,
Expand Down Expand Up @@ -668,7 +671,7 @@ impl Workspace {
let home_prompt = cx.new(|cx| TextareaState::new(window, cx).auto_grow(2, 8).submit_on_enter(true).placeholder("Ask anything, or describe a task…"));
cx.subscribe_in(&home_prompt, window, |ws: &mut Self, _, ev: &InputEvent, window, cx| {
match ev {
InputEvent::PressEnter { .. } => ws.act(Act::HomeLaunch, window, cx),
ev if crate::panels::composer::sends(ev) => ws.act(Act::HomeLaunch, window, cx),
InputEvent::Change => cx.notify(),
_ => {}
}
Expand Down Expand Up @@ -743,6 +746,7 @@ impl Workspace {
.collect(),
window_title: String::new(),
dragging: None,
dragging_card: None,
drag_pos: None,
prev_current: Vec::new(),
slides: HashMap::new(),
Expand Down
Loading
Loading