Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(semantic): parse macOS Claude and Mail captures

Louis Beaumont6f1a4c7591d1
diff --git a/crates/screenpipe-semantic/src/parsers/claude.rs b/crates/screenpipe-semantic/src/parsers/claude.rs
index 808bba19c..8c05b1ad8 100644
--- a/crates/screenpipe-semantic/src/parsers/claude.rs
+++ b/crates/screenpipe-semantic/src/parsers/claude.rs
@@ -30,9 +30,9 @@ const ASSISTANT_ANNOUNCEMENT: &str = "Claude responded:";
const TURN_STOP_CLASSES: &[&str] = &["group/btn", "group/dd", "cds-reset"];
const CHROME_LINES: &[&str] = &["Chat mode", "Fast mode off", "Show message actions"];
-/// Exact Claude conversation parser. It requires actor-bearing DOM markers and
-/// abstains on older or incompatible layouts so the shared conversation and
-/// document parsers remain available.
+/// Exact Claude conversation parser. It requires actor-bearing DOM markers,
+/// accessible turn headings, or Windows live-region announcements and
+/// abstains on incompatible layouts so the shared parsers remain available.
pub struct ClaudeParser {
manifest: ParserManifest,
}
@@ -42,7 +42,7 @@ impl ClaudeParser {
Self {
manifest: ParserManifest {
id: "app.claude.message_dom".into(),
- parser_version: "1".into(),
+ parser_version: "2".into(),
schema_version: 1,
scope: ParserScope::App,
platforms: vec![Platform::Macos, Platform::Windows, Platform::Linux],
@@ -84,6 +84,8 @@ impl SemanticParser for ClaudeParser {
) -> Result<ParseOutcome, ProjectionError> {
let (messages, surface, key_from_url) = if let Some(messages) = dom_marker_messages(tree) {
(messages, "actor_dom_markers", false)
+ } else if let Some(messages) = accessible_heading_messages(tree) {
+ (messages, "accessible_heading_markers", false)
} else if let Some(messages) = announcement_messages(tree) {
(messages, "sr_announcements", true)
} else {
@@ -128,6 +130,119 @@ impl SemanticParser for ClaudeParser {
}
}
+/// Persisted macOS/Electron trees omit DOM classes but preserve one accessible
+/// heading per turn: `You said: ...` or `Claude responded: ...`. The visible
+/// body follows the heading and ends at the exact message-actions control.
+/// Requiring both actor headings keeps partial loading states and unrelated
+/// headings from being treated as conversations.
+fn accessible_heading_messages(tree: &SemanticTree) -> Option<Vec<(NodeId, &'static str, String)>> {
+ let order: Vec<NodeId> = all_nodes(tree).collect();
+ let mut markers: Vec<(usize, NodeId, &'static str, String)> = Vec::new();
+ for (position, &node) in order.iter().enumerate() {
+ if !tree.role(node).is_some_and(|role| {
+ role.eq_ignore_ascii_case("AXHeading") || role.eq_ignore_ascii_case("Heading")
+ }) {
+ continue;
+ }
+ let Some((actor, remainder)) = turn_marker(tree, node) else {
+ continue;
+ };
+ markers.push((position, node, actor, remainder.to_owned()));
+ if markers.len() == MAX_MESSAGES {
+ break;
+ }
+ }
+ let has_user = markers.iter().any(|(_, _, actor, _)| *actor == "[user]");
+ let has_assistant = markers.iter().any(|(_, _, actor, _)| *actor == "Claude");
+ if !has_user || !has_assistant {
+ return None;
+ }
+
+ let mut messages = Vec::new();
+ let mut retained_bytes = 0usize;
+ for (index, (position, node, actor, remainder)) in markers.iter().enumerate() {
+ let window_end = markers.get(index + 1).map_or(order.len(), |next| next.0);
+ let body = accessible_turn_body(
+ tree,
+ *node,
+ &order[position + 1..window_end],
+ &mut retained_bytes,
+ )
+ .or_else(|| (!remainder.is_empty()).then(|| remainder.clone()));
+ if let Some(body) = body {
+ messages.push((*node, *actor, body));
+ }
+ }
+ (!messages.is_empty()).then_some(messages)
+}
+
+fn accessible_turn_body(
+ tree: &SemanticTree,
+ marker: NodeId,
+ window: &[NodeId],
+ retained_bytes: &mut usize,
+) -> Option<String> {
+ let mut lines = Vec::<String>::new();
+ let mut skip_under: Option<NodeId> = None;
+ for &node in window {
+ if is_descendant(tree, node, marker) {
+ continue;
+ }
+ if let Some(anchor) = skip_under {
+ if is_descendant(tree, node, anchor) {
+ continue;
+ }
+ skip_under = None;
+ }
+ if is_button_role(tree.role(node)) {
+ if node_content(tree, node)
+ .is_some_and(|content| content.eq_ignore_ascii_case("Show message actions"))
+ {
+ break;
+ }
+ skip_under = Some(node);
+ continue;
+ }
+ if !is_message_text_role(tree.role(node)) {
+ continue;
+ }
+ let Some(content) = node_content(tree, node) else {
+ continue;
+ };
+ for line in content
+ .lines()
+ .map(str::trim)
+ .filter(|line| !line.is_empty())
+ {
+ if line.starts_with(USER_ANNOUNCEMENT)
+ || line.starts_with(ASSISTANT_ANNOUNCEMENT)
+ || CHROME_LINES
+ .iter()
+ .chain(IGNORED_ACTIONS.iter())
+ .any(|chrome| line.eq_ignore_ascii_case(chrome))
+ || lines.last().is_some_and(|previous| previous == line)
+ {
+ continue;
+ }
+ let separator = usize::from(!lines.is_empty());
+ let remaining = MAX_BODY_BYTES.saturating_sub(*retained_bytes + separator);
+ if remaining == 0 {
+ break;
+ }
+ let line = truncate_str(line, remaining);
+ if line.is_empty() {
+ break;
+ }
+ *retained_bytes += line.len() + separator;
+ lines.push(line.to_owned());
+ }
+ if *retained_bytes == MAX_BODY_BYTES {
+ break;
+ }
+ }
+ (!lines.is_empty()).then(|| lines.join("\n"))
+}
+
/// Web/macOS surface: actor-bearing DOM marker classes. Unchanged behavior.
fn dom_marker_messages(tree: &SemanticTree) -> Option<Vec<(NodeId, &'static str, String)>> {
let mut messages = Vec::new();
@@ -208,6 +323,10 @@ fn announcement(tree: &SemanticTree, node: NodeId) -> Option<(&'static str, &str
if !has_class(tree, node, "sr-only") {
return None;
}
+ turn_marker(tree, node)
+}
+
+fn turn_marker(tree: &SemanticTree, node: NodeId) -> Option<(&'static str, &str)> {
let text = node_content(tree, node)?;
text.strip_prefix(USER_ANNOUNCEMENT)
.map(|remainder| ("[user]", remainder.trim()))
@@ -235,10 +354,7 @@ fn assistant_body(
}
skip_under = None;
}
- if tree
- .role(node)
- .is_some_and(|role| role.eq_ignore_ascii_case("Button"))
- {
+ if is_button_role(tree.role(node)) {
if has_class(tree, node, "sr-only") {
break;
}
@@ -487,6 +603,12 @@ fn is_message_text_role(role: Option<&str>) -> bool {
})
}
+fn is_button_role(role: Option<&str>) -> bool {
+ role.is_some_and(|role| {
+ role.eq_ignore_ascii_case("AXButton") || role.eq_ignore_ascii_case("Button")
+ })
+}
+
fn contains_ascii_case_insensitive(value: &str, needle: &str) -> bool {
value
.as_bytes()
diff --git a/crates/screenpipe-semantic/src/parsers/native_macos.rs b/crates/screenpipe-semantic/src/parsers/native_macos.rs
index 0bb71c333..542d33369 100644
--- a/crates/screenpipe-semantic/src/parsers/native_macos.rs
+++ b/crates/screenpipe-semantic/src/parsers/native_macos.rs
@@ -49,7 +49,8 @@ impl NativeMacParser {
kinds: Vec<SemanticKind>,
) -> Self {
let parser_version = match kind {
- NativeKind::Discord | NativeKind::Mail => "3",
+ NativeKind::Mail => "4",
+ NativeKind::Discord => "3",
NativeKind::Gemini
| NativeKind::Messages
| NativeKind::Slack
@@ -338,25 +339,35 @@ fn parse_notes(tree: &SemanticTree) -> ParseOutcome {
}
fn parse_mail(context: &ParseContext<'_>, tree: &SemanticTree) -> ParseOutcome {
- let compose_field = find_identifier(tree, "Mail.subjectField")
- .or_else(|| find_identifier(tree, "Mail.toField"));
- if let (Some(subject_node), Some(web_area)) = (compose_field, find_role(tree, "AXWebArea")) {
- if let Some(body) = collect_text(tree, web_area, &[]) {
- let subject = node_content(tree, subject_node).unwrap_or("Draft");
- return conversation_with_messages(
- context,
- "mail",
- subject,
- vec![MessageData {
- node: web_area,
- actor: Some("[user]".into()),
- actor_evidence: Some("draft_state"),
- body,
- time_label: None,
- status: Some("draft".into()),
- native_message_id: None,
- }],
- );
+ let subject_field = find_identifier(tree, "Mail.subjectField");
+ let compose_marker = subject_field
+ .or_else(|| find_identifier(tree, "Mail.toField"))
+ .or_else(|| find_identifier(tree, "Mail.ccField"))
+ .or_else(|| find_identifier(tree, "lablel_to"))
+ .or_else(|| find_identifier(tree, "label_subject"));
+ if compose_marker.is_some() {
+ if let Some(web_area) = find_role(tree, "AXWebArea") {
+ if let Some(body) = collect_text(tree, web_area, &[])
+ .or_else(|| collect_flattened_mail_draft(tree, web_area))
+ {
+ let subject = subject_field
+ .and_then(|node| node_content(tree, node))
+ .unwrap_or("Draft");
+ return conversation_with_messages(
+ context,
+ "mail",
+ subject,
+ vec![MessageData {
+ node: web_area,
+ actor: Some("[user]".into()),
+ actor_evidence: Some("draft_state"),
+ body,
+ time_label: None,
+ status: Some("draft".into()),
+ native_message_id: None,
+ }],
+ );
+ }
}
return ParseOutcome::Empty;
}
@@ -401,6 +412,63 @@ fn parse_mail(context: &ParseContext<'_>, tree: &SemanticTree) -> ParseOutcome {
}
}
+/// Some persisted Mail compose captures flatten the web area's text into
+/// subsequent root-level static-text nodes. Recover only that narrow shape and
+/// stop at the first native compose control so unrelated window chrome cannot
+/// enter the draft body.
+fn collect_flattened_mail_draft(tree: &SemanticTree, web_area: NodeId) -> Option<String> {
+ const COMPOSE_CONTROLS: &[&str] = &["label_from", "popup_from", "_closeButton"];
+ let mut after_web_area = false;
+ let mut lines = Vec::<String>::new();
+ let mut bytes = 0usize;
+ for node in all_nodes(tree).take(4_000) {
+ if node == web_area {
+ after_web_area = true;
+ continue;
+ }
+ if !after_web_area {
+ continue;
+ }
+ if tree.identifier(node).is_some_and(|identifier| {
+ COMPOSE_CONTROLS
+ .iter()
+ .any(|control| identifier.eq_ignore_ascii_case(control))
+ }) {
+ break;
+ }
+ if tree.parent(node).is_some() || !role_is(tree, node, "AXStaticText") {
+ continue;
+ }
+ let Some(content) = node_content(tree, node) else {
+ continue;
+ };
+ for line in content
+ .lines()
+ .map(str::trim)
+ .filter(|line| !line.is_empty())
+ {
+ if lines.last().is_some_and(|previous| previous == line) {
+ continue;
+ }
+ let separator = usize::from(!lines.is_empty());
+ let remaining = MAX_BODY_BYTES.saturating_sub(bytes + separator);
+ if remaining == 0 {
+ break;
+ }
+ let line = truncate_str(line, remaining);
+ if line.is_empty() {
+ break;
+ }
+ bytes += line.len() + separator;
+ lines.push(line.to_owned());
+ }
+ if bytes == MAX_BODY_BYTES {
+ break;
+ }
+ }
+ (!lines.is_empty()).then(|| lines.join("\n"))
+}
+
fn parse_messages(context: &ParseContext<'_>, tree: &SemanticTree) -> ParseOutcome {
let transcript = find_identifier(tree, "TranscriptCollectionView");
let title_node = find_identifier(tree, "ConversationTitle")
diff --git a/crates/screenpipe-semantic/tests/claude_obsidian_parsers.rs b/crates/screenpipe-semantic/tests/claude_obsidian_parsers.rs
index 39e6e51b9..3f8662b1c 100644
--- a/crates/screenpipe-semantic/tests/claude_obsidian_parsers.rs
+++ b/crates/screenpipe-semantic/tests/claude_obsidian_parsers.rs
@@ -106,6 +106,27 @@ fn claude_extracts_actor_turns_from_exact_message_dom() {
);
}
+#[test]
+fn claude_extracts_macos_turns_from_accessible_headings() {
+ let items = handled(
+ include_str!("fixtures/apps/claude_macos_accessible_headings.json"),
+ "app.claude.message_dom",
+ );
+ assert_eq!(items.len(), 3);
+ assert_eq!(items[0].title.as_deref(), Some("Parser repair - Claude"));
+ assert_eq!(
+ items[0].metadata.get("surface").map(String::as_str),
+ Some("accessible_heading_markers")
+ );
+ assert_eq!(items[1].actor.as_deref(), Some("[user]"));
+ assert_eq!(
+ items[1].body.as_deref(),
+ Some("Reply with exactly SEMANTIC_PARSER_ASSISTANT")
+ );
+ assert_eq!(items[2].actor.as_deref(), Some("Claude"));
+ assert_eq!(items[2].body.as_deref(), Some("SEMANTIC_PARSER_ASSISTANT"));
+}
+
#[test]
fn claude_abstains_on_typography_outside_a_message_pair() {
let (app, tree) = fixture_tree(include_str!("fixtures/apps/claude_card_only.json"));
@@ -123,6 +144,25 @@ fn claude_abstains_on_typography_outside_a_message_pair() {
);
}
+#[test]
+fn claude_abstains_on_an_assistant_only_accessible_heading() {
+ let (app, tree) = fixture_tree(include_str!(
+ "fixtures/apps/claude_macos_assistant_only.json"
+ ));
+ let context = ParseContext {
+ frame_id: 7,
+ captured_at_unix_ms: 1_700_000_000_000,
+ utc_offset_minutes: None,
+ locale_hint: None,
+ app: &app,
+ input_content_hash: 9,
+ };
+ assert_eq!(
+ ClaudeParser::new().parse(&context, &tree).unwrap(),
+ ParseOutcome::NotHandled
+ );
+}
+
#[test]
fn obsidian_extracts_the_active_codemirror_note() {
let items = handled(
diff --git a/crates/screenpipe-semantic/tests/fixtures/apps/claude_macos_accessible_headings.json b/crates/screenpipe-semantic/tests/fixtures/apps/claude_macos_accessible_headings.json
new file mode 100644
index 000000000..3351221e6
--- /dev/null
+++ b/crates/screenpipe-semantic/tests/fixtures/apps/claude_macos_accessible_headings.json
@@ -0,0 +1,96 @@
+{
+ "app": {
+ "platform": "macos",
+ "app_id": "com.anthropic.claudefordesktop",
+ "executable": "Claude",
+ "display_name": "Claude",
+ "version": "1.32885.1",
+ "browser_url": "https://claude.ai/new"
+ },
+ "nodes": [
+ {
+ "parent": null,
+ "role": "AXWindow",
+ "title": "Parser repair - Claude"
+ },
+ {
+ "parent": 0,
+ "role": "AXWebArea",
+ "text": "Claude"
+ },
+ {
+ "parent": 1,
+ "role": "AXGroup",
+ "description": "Chat messages"
+ },
+ {
+ "parent": 2,
+ "role": "AXGroup",
+ "description": "Message 1"
+ },
+ {
+ "parent": 3,
+ "role": "AXHeading",
+ "text": "You said: Reply with exactly SEMANTICPARSERASSISTANT"
+ },
+ {
+ "parent": 4,
+ "role": "AXStaticText",
+ "text": "You said: Reply with exactly SEMANTICPARSERASSISTANT"
+ },
+ {
+ "parent": 3,
+ "role": "AXStaticText",
+ "text": "Reply with exactly SEMANTIC_PARSER_ASSISTANT"
+ },
+ {
+ "parent": 3,
+ "role": "AXButton",
+ "text": "Show message actions"
+ },
+ {
+ "parent": 2,
+ "role": "AXGroup",
+ "description": "Message 2"
+ },
+ {
+ "parent": 8,
+ "role": "AXGroup"
+ },
+ {
+ "parent": 9,
+ "role": "AXHeading",
+ "text": "Claude responded: SEMANTICPARSERASSISTANT"
+ },
+ {
+ "parent": 10,
+ "role": "AXStaticText",
+ "text": "Claude responded: SEMANTICPARSERASSISTANT"
+ },
+ {
+ "parent": 9,
+ "role": "AXStaticText",
+ "text": "SEMANTIC_PARSER_ASSISTANT"
+ },
+ {
+ "parent": 9,
+ "role": "AXButton",
+ "text": "Show message actions"
+ },
+ {
+ "parent": 1,
+ "role": "AXStaticText",
+ "text": "Chat mode"
+ },
+ {
+ "parent": 1,
+ "role": "AXTextArea",
+ "value": "composer contamination"
+ },
+ {
+ "parent": 1,
+ "role": "AXButton",
+ "text": "Send"
+ }
+ ]
+}
diff --git a/crates/screenpipe-semantic/tests/fixtures/apps/claude_macos_assistant_only.json b/crates/screenpipe-semantic/tests/fixtures/apps/claude_macos_assistant_only.json
new file mode 100644
index 000000000..e918880a7
--- /dev/null
+++ b/crates/screenpipe-semantic/tests/fixtures/apps/claude_macos_assistant_only.json
@@ -0,0 +1,32 @@
+{
+ "app": {
+ "platform": "macos",
+ "app_id": "com.anthropic.claudefordesktop",
+ "executable": "Claude",
+ "display_name": "Claude",
+ "version": "1.32885.1",
+ "browser_url": null
+ },
+ "nodes": [
+ {
+ "parent": null,
+ "role": "AXWindow",
+ "title": "Claude"
+ },
+ {
+ "parent": 0,
+ "role": "AXHeading",
+ "text": "Claude responded: A partial loading state"
+ },
+ {
+ "parent": 0,
+ "role": "AXStaticText",
+ "text": "A partial loading state"
+ },
+ {
+ "parent": 0,
+ "role": "AXButton",
+ "text": "Show message actions"
+ }
+ ]
+}
diff --git a/crates/screenpipe-semantic/tests/fixtures/persisted/mail_macos_flattened_draft.json b/crates/screenpipe-semantic/tests/fixtures/persisted/mail_macos_flattened_draft.json
new file mode 100644
index 000000000..3401365c4
--- /dev/null
+++ b/crates/screenpipe-semantic/tests/fixtures/persisted/mail_macos_flattened_draft.json
@@ -0,0 +1,56 @@
+{
+ "app": {
+ "platform": "macos",
+ "app_id": "com.apple.mail",
+ "executable": "Mail",
+ "display_name": "Mail",
+ "version": "16.0",
+ "browser_url": null
+ },
+ "nodes": [
+ {
+ "role": "AXWindow",
+ "text": "Release update",
+ "depth": 0,
+ "automation_id": "_NS:41"
+ },
+ {
+ "role": "AXTextField",
+ "text": "Release update",
+ "value": "Release update",
+ "depth": 1,
+ "automation_id": "Mail.subjectField"
+ },
+ {
+ "role": "AXScrollArea",
+ "text": "",
+ "depth": 1
+ },
+ {
+ "role": "AXWebArea",
+ "text": "",
+ "depth": 2
+ },
+ {
+ "role": "AXStaticText",
+ "text": "First draft line.",
+ "depth": 0
+ },
+ {
+ "role": "AXStaticText",
+ "text": "Second draft line.",
+ "depth": 0
+ },
+ {
+ "role": "AXStaticText",
+ "text": "From:",
+ "depth": 1,
+ "automation_id": "label_from"
+ },
+ {
+ "role": "AXStaticText",
+ "text": "must not leak into the draft",
+ "depth": 2
+ }
+ ]
+}
diff --git a/crates/screenpipe-semantic/tests/fixtures/persisted/mail_macos_partial_compose.json b/crates/screenpipe-semantic/tests/fixtures/persisted/mail_macos_partial_compose.json
new file mode 100644
index 000000000..8c5aa1d29
--- /dev/null
+++ b/crates/screenpipe-semantic/tests/fixtures/persisted/mail_macos_partial_compose.json
@@ -0,0 +1,24 @@
+{
+ "app": {
+ "platform": "macos",
+ "app_id": "com.apple.mail",
+ "executable": "Mail",
+ "display_name": "Mail",
+ "version": "16.0",
+ "browser_url": null
+ },
+ "nodes": [
+ {
+ "role": "AXWindow",
+ "text": "New Message",
+ "depth": 0,
+ "automation_id": "_NS:41"
+ },
+ {
+ "role": "AXStaticText",
+ "text": "To:",
+ "depth": 1,
+ "automation_id": "lablel_to"
+ }
+ ]
+}
diff --git a/crates/screenpipe-semantic/tests/persisted_tree_regressions.rs b/crates/screenpipe-semantic/tests/persisted_tree_regressions.rs
index e1d260f12..dc0e009d2 100644
--- a/crates/screenpipe-semantic/tests/persisted_tree_regressions.rs
+++ b/crates/screenpipe-semantic/tests/persisted_tree_regressions.rs
@@ -60,6 +60,37 @@ fn mail_empty_compose_is_a_valid_empty_surface() {
assert_eq!(outcome, ValidatedParseOutcome::Empty);
}
+#[test]
+fn mail_partial_compose_is_a_valid_empty_surface() {
+ let (parser, outcome) = parse_fixture(include_str!(
+ "fixtures/persisted/mail_macos_partial_compose.json"
+ ));
+ assert_eq!(parser.as_deref(), Some("app.macos.mail.message_view"));
+ assert_eq!(outcome, ValidatedParseOutcome::Empty);
+}
+
+#[test]
+fn mail_recovers_a_flattened_persisted_draft_body() {
+ let (parser, outcome) = parse_fixture(include_str!(
+ "fixtures/persisted/mail_macos_flattened_draft.json"
+ ));
+ assert_eq!(parser.as_deref(), Some("app.macos.mail.message_view"));
+ let ValidatedParseOutcome::Handled(projection) = outcome else {
+ panic!("expected handled Mail draft");
+ };
+ assert_eq!(projection.items().len(), 2);
+ assert_eq!(
+ projection.items()[0].title.as_deref(),
+ Some("Release update")
+ );
+ assert_eq!(projection.items()[1].actor.as_deref(), Some("[user]"));
+ assert_eq!(
+ projection.items()[1].body.as_deref(),
+ Some("First draft line.\nSecond draft line.")
+ );
+ assert_eq!(projection.items()[1].status.as_deref(), Some("draft"));
+}
+
#[test]
fn gmail_thread_uses_persisted_accessible_actions_without_dom_classes() {
let (parser, outcome) =