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(cursor): drop the sandbox <img> tags Cursor streams (#6592)

Wolf Mermelsteinb6289fe65270
diff --git a/crates/cursor_cloud_agents/src/domain/inline_image.rs b/crates/cursor_cloud_agents/src/domain/inline_image.rs
new file mode 100644
index 0000000000..41a83176ff
--- /dev/null
+++ b/crates/cursor_cloud_agents/src/domain/inline_image.rs
@@ -0,0 +1,195 @@
+//! Drops the `<img>` tags Cursor streams for its walkthrough artifacts.
+//!
+//! Cursor's walkthrough skill tells the agent to reference screenshots with
+//! HTML tags inline, and Macro tells it to use the bare file name, so a turn
+//! that took a screenshot streams `<img src="shot.png" />` in its prose. That
+//! file lives in Cursor's sandbox: no reader can resolve the tag, and the
+//! transcript renders it as literal text. The image itself reaches the reader
+//! anyway, as the markdown [`super::artifact::artifact_markdown`] appends once
+//! the run's artifacts are re-hosted, so the tag is dropped here.
+//!
+//! The tag arrives a token at a time (`<img`, ` src`, `="`, `shot`, …), so a
+//! per-chunk match cannot see it. Text is held from a `<` while it can still
+//! become `<img`, then until the tag closes; anything that turns out to be
+//! prose is released unchanged.
+
+const TAG: &str = "<img";
+
+/// One run's view of its streamed text, tags withheld.
+#[derive(Debug, Default)]
+pub struct InlineImageFilter {
+ held: String,
+}
+
+impl InlineImageFilter {
+ /// The part of `text` to show, given what earlier chunks left held back.
+ pub fn push(&mut self, text: &str) -> String {
+ let mut shown = String::new();
+ for ch in text.chars() {
+ if self.held.is_empty() {
+ if ch == '<' {
+ self.held.push(ch);
+ } else {
+ shown.push(ch);
+ }
+ continue;
+ }
+ self.held.push(ch);
+ match self.classify() {
+ Held::Prose => shown.push_str(&self.flush()),
+ Held::Pending => {}
+ Held::Tag if ch == '>' => {
+ let tag = self.flush();
+ if keeps(&tag) {
+ shown.push_str(&tag);
+ }
+ }
+ Held::Tag => {}
+ }
+ }
+ shown
+ }
+
+ /// Whatever is still held, for when the stream ends mid-tag.
+ pub fn flush(&mut self) -> String {
+ std::mem::take(&mut self.held)
+ }
+
+ fn classify(&self) -> Held {
+ let held = self.held.as_str();
+ if held.len() < TAG.len() {
+ return if TAG.starts_with(held) {
+ Held::Pending
+ } else {
+ Held::Prose
+ };
+ }
+ let Some(name) = held.get(..TAG.len()) else {
+ return Held::Prose;
+ };
+ if !name.eq_ignore_ascii_case(TAG) {
+ return Held::Prose;
+ }
+ // `<imgs` or `<image` is a word, not this tag.
+ match held[TAG.len()..].chars().next() {
+ None => Held::Pending,
+ Some(next) if next.is_whitespace() || next == '/' || next == '>' => Held::Tag,
+ Some(_) => Held::Prose,
+ }
+ }
+}
+
+enum Held {
+ /// Could still become `<img`.
+ Pending,
+ /// Is an `<img` tag, open until `>`.
+ Tag,
+ /// Ordinary text, shown as it was.
+ Prose,
+}
+
+/// A tag pointing at something a reader can reach is not the sandbox
+/// reference this filter exists for, and is left alone.
+fn keeps(tag: &str) -> bool {
+ src(tag).is_some_and(|src| src.contains("://"))
+}
+
+fn src(tag: &str) -> Option<&str> {
+ let after = &tag[tag.find("src")? + "src".len()..];
+ let after = after.trim_start().strip_prefix('=')?.trim_start();
+ let quote = after.chars().next().filter(|c| matches!(c, '"' | '\''))?;
+ let value = &after[quote.len_utf8()..];
+ value.split(quote).next()
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ fn stream(chunks: &[&str]) -> String {
+ let mut filter = InlineImageFilter::default();
+ let mut shown: String = chunks.iter().map(|chunk| filter.push(chunk)).collect();
+ shown.push_str(&filter.flush());
+ shown
+ }
+
+ /// The delta sequence of dev session 01a0b4ed-29e1-7634-9a67-b87aa0d3efe8.
+ #[test]
+ fn a_tag_split_across_deltas_is_dropped() {
+ let deltas = [
+ "Here",
+ " is",
+ " the",
+ " screenshot",
+ ":\n\n",
+ "<img",
+ " src",
+ "=\"",
+ "hello",
+ "_",
+ "world",
+ "_",
+ "browser",
+ ".png",
+ "\"",
+ " alt",
+ "=\"",
+ "Hello",
+ " World",
+ " HTML",
+ " page",
+ " in",
+ " Chrome",
+ "\"",
+ " />",
+ "\n\n",
+ "Open",
+ " `",
+ "index",
+ ".html",
+ "`",
+ ];
+ assert_eq!(
+ stream(&deltas),
+ "Here is the screenshot:\n\n\n\nOpen `index.html`"
+ );
+ }
+
+ #[test]
+ fn a_sandbox_path_is_dropped_too() {
+ assert_eq!(
+ stream(&["Proof:\n<img src=\"/opt/cursor/artifacts/pr.webp\" />\nDone."]),
+ "Proof:\n\nDone."
+ );
+ }
+
+ #[test]
+ fn a_hosted_image_is_shown() {
+ let tag = "<img alt=\"CI\" src=\"https://github.com/macro-inc/macro/actions/workflows/ci.yml/badge.svg\">";
+ assert_eq!(stream(&["Status: ", tag]), format!("Status: {tag}"));
+ }
+
+ #[test]
+ fn prose_with_angle_brackets_is_untouched() {
+ for text in [
+ "a < b and b > c",
+ "use <image> here",
+ "<imgs are plural",
+ "generics like Vec<T>",
+ "an unfinished < at the end",
+ "<i>emphasis</i>",
+ ] {
+ assert_eq!(stream(&[text]), text);
+ let chars: Vec<String> = text.chars().map(String::from).collect();
+ let refs: Vec<&str> = chars.iter().map(String::as_str).collect();
+ assert_eq!(stream(&refs), text, "one char per delta");
+ }
+ }
+
+ #[test]
+ fn an_unterminated_tag_is_released_on_flush() {
+ let mut filter = InlineImageFilter::default();
+ assert_eq!(filter.push("Shipped.\n<img src=\"proof"), "Shipped.\n");
+ assert_eq!(filter.flush(), "<img src=\"proof");
+ }
+}
diff --git a/crates/cursor_cloud_agents/src/domain/journal.rs b/crates/cursor_cloud_agents/src/domain/journal.rs
index cff049f169..4f329869e6 100644
--- a/crates/cursor_cloud_agents/src/domain/journal.rs
+++ b/crates/cursor_cloud_agents/src/domain/journal.rs
@@ -1,6 +1,7 @@
//! Native capture contract and the shared live/load processing machine.
use super::artifact::{CollectedArtifact, artifact_markdown};
use super::event::{CursorEvent, InteractionUpdate};
+use super::inline_image::InlineImageFilter;
use super::model::{CursorRunId, RunOutcome, RunStatus};
use super::translate::TranslateMachine;
use agent_client_protocol::schema::v1::{
@@ -120,6 +121,9 @@ struct RunState {
prompt: bool,
text: String,
terminal: Option<RunStatus>,
+ /// What the reader sees of `text`: the same stream minus the `<img>`
+ /// tags Cursor writes for files only its sandbox can reach.
+ images: InlineImageFilter,
}
/// Complete live/replay state, including user prompts and terminal tool cleanup.
#[derive(Debug, Default)]
@@ -266,7 +270,11 @@ impl ReplayMachine {
// the rest; this is what happens when it does not.
CursorEvent::Status { status, .. } if status.is_terminal() => {
state.terminal = Some(status);
- Ok(self.translator.close_open_calls())
+ let mut updates = self.translator.push(CursorEvent::Assistant {
+ text: state.images.flush(),
+ });
+ updates.extend(self.translator.close_open_calls());
+ Ok(updates)
}
CursorEvent::Interaction(InteractionUpdate::Other { kind })
if kind == "step-started" =>
@@ -274,7 +282,9 @@ impl ReplayMachine {
// Cursor's final result contains the final step, not earlier
// commentary emitted before tool execution in the same run.
state.text.clear();
- Ok(Vec::new())
+ Ok(self.translator.push(CursorEvent::Assistant {
+ text: state.images.flush(),
+ }))
}
CursorEvent::Interaction(InteractionUpdate::UserMessage { text }) => {
if state.prompt {
@@ -287,6 +297,7 @@ impl ReplayMachine {
}
CursorEvent::Assistant { text } => {
state.text.push_str(&text);
+ let text = state.images.push(&text);
Ok(self.translator.push(CursorEvent::Assistant { text }))
}
CursorEvent::Result {
@@ -314,11 +325,9 @@ impl ReplayMachine {
// what the user watched arrive - stays as it is.
match text.strip_prefix(state.text.as_str()) {
Some(suffix) => {
- if !suffix.is_empty() {
- updates.extend(self.translator.push(CursorEvent::Assistant {
- text: suffix.to_owned(),
- }));
- }
+ updates.extend(self.translator.push(CursorEvent::Assistant {
+ text: state.images.push(suffix),
+ }));
state.text = text;
}
None => tracing::warn!(
@@ -329,6 +338,9 @@ impl ReplayMachine {
),
}
}
+ updates.extend(self.translator.push(CursorEvent::Assistant {
+ text: state.images.flush(),
+ }));
updates.extend(self.translator.push(CursorEvent::Result {
run_id,
status: status.clone(),
diff --git a/crates/cursor_cloud_agents/src/domain/journal/test.rs b/crates/cursor_cloud_agents/src/domain/journal/test.rs
index 9f3cb40c93..5740ec5a62 100644
--- a/crates/cursor_cloud_agents/src/domain/journal/test.rs
+++ b/crates/cursor_cloud_agents/src/domain/journal/test.rs
@@ -399,3 +399,104 @@ fn a_stream_interruption_projects_to_nothing() {
interruption
);
}
+
+/// The screenshot turn of dev session 01a0b4ed-29e1-7634-9a67-b87aa0d3efe8:
+/// Cursor streamed `<img src="hello_world_browser.png" ... />` a token at a
+/// time, the file only ever existed in its sandbox, and the same image then
+/// arrived re-hosted with the run's artifacts. The reader gets the prose and
+/// the hosted image, not the tag; the answer of record keeps the stream whole.
+#[test]
+fn a_streamed_sandbox_image_tag_is_not_shown() {
+ let run = CursorRunId::new("screenshot-run");
+ let mut machine = ReplayMachine::default();
+ let deltas = [
+ "Here is the screenshot:\n\n",
+ "<img",
+ " src",
+ "=\"",
+ "hello",
+ "_",
+ "world",
+ "_",
+ "browser",
+ ".png",
+ "\"",
+ " alt",
+ "=\"",
+ "Hello World",
+ "\"",
+ " />",
+ "\n\n",
+ "Open `index.html`.",
+ ];
+ let mut shown = String::new();
+ for delta in deltas {
+ for update in machine
+ .push(
+ Some(&run),
+ &JournalInput::Sse(crate::testing::raw_record(CursorEvent::Assistant {
+ text: delta.into(),
+ })),
+ )
+ .unwrap()
+ {
+ match update {
+ SessionUpdate::AgentMessageChunk(chunk) => match chunk.content {
+ ContentBlock::Text(text) => shown.push_str(&text.text),
+ other => panic!("unexpected content {other:?}"),
+ },
+ other => panic!("unexpected update {other:?}"),
+ }
+ }
+ }
+ assert_eq!(shown, "Here is the screenshot:\n\n\n\nOpen `index.html`.");
+ assert_eq!(machine.answer(&run), Some(deltas.concat().as_str()));
+
+ let result = JournalInput::Sse(NativeRecord {
+ event: "result".into(),
+ id: None,
+ data: serde_json::json!({
+ "runId": run.as_str(), "status": "FINISHED",
+ "text": "Here is the screenshot:\n\nOpen `index.html`.",
+ })
+ .to_string(),
+ });
+ assert!(
+ machine
+ .push(Some(&run), &result)
+ .unwrap()
+ .iter()
+ .all(|u| !matches!(u, SessionUpdate::AgentMessageChunk(_))),
+ "the restatement adds no text"
+ );
+}
+
+/// A tag still open when the run ends is prose after all, and is shown.
+#[test]
+fn an_unclosed_tag_is_released_when_the_run_ends() {
+ let run = CursorRunId::new("unclosed-run");
+ let mut machine = ReplayMachine::default();
+ let updates = machine
+ .push(
+ Some(&run),
+ &JournalInput::Sse(crate::testing::raw_record(CursorEvent::Assistant {
+ text: "Shipped. <img src=\"proof".into(),
+ })),
+ )
+ .unwrap();
+ assert!(
+ matches!(&updates[..], [SessionUpdate::AgentMessageChunk(c)] if matches!(&c.content, ContentBlock::Text(t) if t.text == "Shipped. "))
+ );
+ let updates = machine
+ .push(
+ Some(&run),
+ &JournalInput::Sse(crate::testing::raw_record(CursorEvent::Status {
+ run_id: run.clone(),
+ status: RunStatus::Finished,
+ })),
+ )
+ .unwrap();
+ assert!(
+ matches!(&updates[..], [SessionUpdate::AgentMessageChunk(c), ..] if matches!(&c.content, ContentBlock::Text(t) if t.text == "<img src=\"proof"))
+ );
+}
diff --git a/crates/cursor_cloud_agents/src/domain/mod.rs b/crates/cursor_cloud_agents/src/domain/mod.rs
index 251b4d27f6..3796c881d6 100644
--- a/crates/cursor_cloud_agents/src/domain/mod.rs
+++ b/crates/cursor_cloud_agents/src/domain/mod.rs
@@ -9,6 +9,9 @@ pub mod error;
/// The Cursor cloud event vocabulary the translation consumes.
pub mod event;
+/// The `<img>` tags Cursor streams for sandbox files, and dropping them.
+pub mod inline_image;
+
/// Identifiers and small value types.
pub mod model;
/// Cursor model catalog expressed as ACP configuration.