Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agent-fold): place a mid-turn compaction where it landed in the reply (#9010)

Wolf Mermelsteinb7413968886e
diff --git a/apps/web/src/features/block-agent/state/tool-groups.test.ts b/apps/web/src/features/block-agent/state/tool-groups.test.ts
index c7225ff422..05cbceb897 100644
--- a/apps/web/src/features/block-agent/state/tool-groups.test.ts
+++ b/apps/web/src/features/block-agent/state/tool-groups.test.ts
@@ -87,6 +87,21 @@ describe('segmentParts', () => {
]);
});
+ it('splits a running group around a compaction that landed inside it', () => {
+ const compaction: MessagePart = {
+ kind: 'control',
+ control: { kind: 'compact' },
+ outcome: { kind: 'accepted' },
+ };
+ expect(
+ segmentParts([tool({ status: 'running' }), compaction, tool()])
+ ).toEqual([
+ { kind: 'tools', start: 0, end: 1 },
+ { kind: 'part', start: 1, end: 2 },
+ { kind: 'tools', start: 2, end: 3 },
+ ]);
+ });
+
describe.each([
{
label: 'native',
diff --git a/crates/folds/agent_fold/src/domain/fold/control.rs b/crates/folds/agent_fold/src/domain/fold/control.rs
index 34cd2eaacc..d833323471 100644
--- a/crates/folds/agent_fold/src/domain/fold/control.rs
+++ b/crates/folds/agent_fold/src/domain/fold/control.rs
@@ -9,25 +9,49 @@ use non_empty::NonEmpty;
use super::state::{Changed, FoldState};
impl FoldState {
- /// Show that the agent summarized its own context. Nothing answers it,
- /// and it is no turn: it lands between turns, or beside one running.
+ /// Show that the agent summarized its own context. Nothing answers it.
+ ///
+ /// Between turns it is a message of its own. Within one it is a part of
+ /// the turn's agent message, after whatever the agent had produced when
+ /// the summary landed: as a message of its own it would sort after the
+ /// whole turn, below calls that started after it.
pub(super) fn record_compaction(&mut self) -> Option<Changed> {
+ let part = MessagePart::Control {
+ control: Control::Compact,
+ outcome: ControlOutcome::Accepted,
+ };
+ // Still consumes a turn id in a turn, so every later turn keeps the
+ // id a comms placeholder may have stored.
let id = TurnId(self.turns_opened);
self.turns_opened += 1;
let message = self.messages.len();
+ match self.turn.as_ref().map(|turn| (turn.id, turn.agent)) {
+ Some((_, Some(agent))) => {
+ self.messages.get_mut(agent)?.parts.push(part);
+ Some(Changed::updated(agent))
+ }
+ Some((turn_id, None)) => {
+ self.push_agent_message(turn_id, part);
+ self.open_turn().agent = Some(message);
+ Some(Changed::new(message))
+ }
+ None => {
+ self.push_agent_message(id, part);
+ Some(Changed::new(message))
+ }
+ }
+ }
+
+ fn push_agent_message(&mut self, id: TurnId, part: MessagePart) {
self.messages.push(FoldedMessage {
id,
author: Author::Agent,
request_id: None,
- parts: NonEmpty::one(MessagePart::Control {
- control: Control::Compact,
- outcome: ControlOutcome::Accepted,
- }),
+ parts: NonEmpty::one(part),
stop: None,
pending: self.speculative,
posted_at: None,
});
- Some(Changed::new(message))
}
/// Emit a standalone control message. A request-backed control starts
diff --git a/crates/folds/agent_fold/src/domain/test/fold.rs b/crates/folds/agent_fold/src/domain/test/fold.rs
index 433e526b13..29ccd7fd69 100644
--- a/crates/folds/agent_fold/src/domain/test/fold.rs
+++ b/crates/folds/agent_fold/src/domain/test/fold.rs
@@ -230,8 +230,8 @@ fn folds_session_controls_as_typed_parts() {
/// `/compact` is a message to every harness: the transcript shows what the
/// agent did, not what someone asked for. A compaction shows when the agent
-/// reports one, as a line of its own beside the turn it landed in, which
-/// carries on and ends as usual.
+/// reports one, at the point in the turn it landed, which carries on after it
+/// and ends as usual.
#[test]
fn compaction_shows_when_the_agent_reports_one_not_when_asked() {
let log = parse_log(concat!(
@@ -244,14 +244,12 @@ fn compaction_shows_when_the_agent_reports_one_not_when_asked() {
r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" Nothing yet."}}}}}"#,
"\n",
r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","id":"c","result":{"stopReason":"end_turn"}}}"#,
+ "\n",
+ r#"{"direction":"to_runtime","user_id":"macro|[email protected]","content":{"type":"acp","jsonrpc":"2.0","id":"n","method":"session/prompt","params":{"sessionId":"s","prompt":[{"type":"text","text":"next"}]}}}"#,
));
let messages = fold(log);
- assert_eq!(
- messages.len(),
- 3,
- "the message, its reply and the compaction"
- );
+ assert_eq!(messages.len(), 3, "the message, its reply and the next");
assert_eq!(
messages[0].parts.as_slice(),
&[MessagePart::Text {
@@ -261,21 +259,55 @@ fn compaction_shows_when_the_agent_reports_one_not_when_asked() {
assert!(matches!(messages[0].author, Author::User { .. }));
assert_eq!(
messages[1].parts.as_slice(),
- &[MessagePart::Text {
- text: "What should I compact? Nothing yet.".to_owned()
- }]
+ &[
+ MessagePart::Text {
+ text: "What should I compact?".to_owned()
+ },
+ MessagePart::Control {
+ control: Control::Compact,
+ outcome: ControlOutcome::Accepted,
+ },
+ MessagePart::Text {
+ text: " Nothing yet.".to_owned()
+ },
+ ]
);
assert_eq!(messages[1].author, Author::Agent);
assert_eq!(messages[1].stop, Some(StopReason::EndTurn));
+ // The compaction still takes a turn id, so later turns keep theirs.
+ assert_eq!(messages[2].id, TurnId(2));
+}
+
+/// A summary the agent waited for lands before it says anything: the divider
+/// opens the reply rather than trailing it.
+#[test]
+fn a_compaction_before_the_agent_speaks_opens_the_reply() {
+ let log = parse_log(concat!(
+ r#"{"direction":"to_runtime","user_id":"macro|[email protected]","content":{"type":"acp","jsonrpc":"2.0","id":"c","method":"session/prompt","params":{"sessionId":"s","prompt":[{"type":"text","text":"a long paste"}]}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"_macro/context_summary","params":{"sessionId":"s","text":"Earlier work.","through":"c","replaces":2}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"Read it."}}}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","id":"c","result":{"stopReason":"end_turn"}}}"#,
+ ));
+
+ let messages = fold(log);
+ assert_eq!(messages.len(), 2, "the paste and its reply");
+ assert_eq!(messages[1].id, TurnId(0));
assert_eq!(
- messages[2].parts.as_slice(),
- &[MessagePart::Control {
- control: Control::Compact,
- outcome: ControlOutcome::Accepted,
- }]
+ messages[1].parts.as_slice(),
+ &[
+ MessagePart::Control {
+ control: Control::Compact,
+ outcome: ControlOutcome::Accepted,
+ },
+ MessagePart::Text {
+ text: "Read it.".to_owned()
+ },
+ ]
);
- assert_eq!(messages[2].author, Author::Agent);
- assert_eq!(messages[2].stop, None);
+ assert_eq!(messages[1].stop, Some(StopReason::EndTurn));
}
/// A stop pressed before the agent's first chunk - a window over ten seconds
diff --git a/crates/folds/agent_fold/src/domain/test/machine.rs b/crates/folds/agent_fold/src/domain/test/machine.rs
index 99128cf8c5..06004d9331 100644
--- a/crates/folds/agent_fold/src/domain/test/machine.rs
+++ b/crates/folds/agent_fold/src/domain/test/machine.rs
@@ -9,7 +9,8 @@ use super::util::{TURN, parse_log};
use crate::domain::fold::{FoldMachineImpl, fold};
use crate::domain::log::AgentSessionLog;
use crate::domain::model::{
- Author, AuthorKind, FoldEvent, FoldedMessage, MessageId, MessagePart, StopReason, TurnId,
+ Author, AuthorKind, Control, ControlOutcome, FoldEvent, FoldedMessage, MessageId, MessagePart,
+ StopReason, ToolStatus, TurnId,
};
use crate::domain::ports::FoldMachine;
@@ -349,3 +350,50 @@ fn a_speculated_prompt_has_no_time() {
assert!(machine.messages()[0].pending);
assert_eq!(machine.messages()[0].posted_at, None);
}
+
+/// A summary can land while a tool runs. The divider sits where it landed:
+/// after the running call, before everything the turn does next - whether
+/// the transcript watched it happen or folded it on reload.
+#[test]
+fn a_compaction_during_a_running_tool_divides_the_turn_where_it_landed() {
+ let log = concat!(
+ r#"{"direction":"to_runtime","content":{"type":"acp","jsonrpc":"2.0","id":"p0","method":"session/prompt","params":{"sessionId":"s","prompt":[{"type":"text","text":"build it"}]}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s","update":{"sessionUpdate":"tool_call","toolCallId":"t1","title":"Bash","kind":"execute","status":"in_progress","rawInput":{"command":"cargo build"},"content":[],"locations":[]}}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"_macro/context_summary","params":{"sessionId":"s","text":"The user asked for a build.","through":"p0","replaces":2}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s","update":{"sessionUpdate":"tool_call_update","toolCallId":"t1","status":"completed"}}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s","update":{"sessionUpdate":"tool_call","toolCallId":"t2","title":"Bash","kind":"execute","status":"in_progress","rawInput":{"command":"cargo test"},"content":[],"locations":[]}}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"Built and tested."}}}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","id":"p0","result":{"stopReason":"end_turn"}}}"#,
+ );
+
+ let live = Consumer::drive(parse_log(log));
+ let reloaded = fold(parse_log(log));
+
+ assert_eq!(live.messages, reloaded, "live and reload agree");
+ assert_eq!(reloaded.len(), 2, "the prompt and one reply");
+ let reply = &reloaded[1];
+ assert_eq!(reply.id, TurnId(0));
+ assert_eq!(reply.stop, Some(StopReason::EndTurn));
+ assert!(
+ matches!(
+ reply.parts.as_slice(),
+ [
+ MessagePart::ToolUse { id: first, status: ToolStatus::Completed, .. },
+ MessagePart::Control {
+ control: Control::Compact,
+ outcome: ControlOutcome::Accepted,
+ },
+ MessagePart::ToolUse { id: second, status: ToolStatus::Running, .. },
+ MessagePart::Text { text },
+ ] if first.0 == "t1" && second.0 == "t2" && text == "Built and tested."
+ ),
+ "the divider sits between the call it interrupted and the next: {:?}",
+ reply.parts
+ );
+}
diff --git a/docs/agent-guide/ai-chat.md b/docs/agent-guide/ai-chat.md
index 956e9db9c2..141542c3a6 100644
--- a/docs/agent-guide/ai-chat.md
+++ b/docs/agent-guide/ai-chat.md
@@ -1380,7 +1380,9 @@ its own session. A message that alone takes more than half of the model's
window is refused with "This message is too long. Split it into smaller messages
or attach it as a file." `/compact` is an ordinary message to every agent: the
transcript shows it as sent, followed by the agent's reply; a "Context compacted"
-line appears only when the agent reports a compaction.
+line appears only when the agent reports a compaction, at the point in the reply
+where the summary landed: a summary that finishes while a tool runs splits the
+tool group, with the running call above the line and later steps below it.
Cursor walkthrough files the run re-hosts appear in the transcript after the
answer: screenshots as images, recordings as video players, and `.txt` / `.log`