building diaHistory with the macOS ax tree

i've been using diaBrowser for over a year now. its perfect for learning and asking follow up questions about a new topic i am learning.

but, one problem i have is that the conversation history is not saved. theres no way to track my learning overtime.

so, i built diaHistory, a tool that automatically archives your diaConversation history using the macOS accessibility API.

How it works

Screenshot 2026-04-20 at 3.52.50 PM.png
see the full excalidraw here

the architecture is simple:

  1. a while loop in chatWatcher polls to check if dia's is running(PID lookup)
    1. this changes every single time you launch the app
  2. once we have dia's PID, we run a discovery poll every 15 seconds to scan the ax tree to discover any new chat panels with a message.
  3. when a panel is discovered, we register an AXObserver to listen for new messages
  4. whenever a message is sent/received - it goes through the whole pipeline:
    1. accessibility reader gets the conversation that changed.
    2. chatparser turns it into the message object
    3. we append it OR create new convo in conversation tracker
    4. THEN we write markdown file and save to documents

macOS accessibility tree

initially i hoped this tool could be a browser extension. but, dia's databases are (of course) encrypted, and dia's chat UI is native, meaning its not part of the DOM at all.

but, hope was not lost, the macOS accessibility tree ended up being what we needed. macOS has an accessibility API that lets you read the UI elements of any app.

its the same system that the voiceover feature uses for screen readers - every text field and label gets an accessibility role that describes what it is and what it contains. for example:

AXSystemWide
  ├─ AXApplication "Dia"
  │  └─ AXWindow "Claude — claude.ai"
  │     ├─ AXToolbar
  │     │  └─ AXComboBox "Address"
  │     │      .value = "https://claude.ai/chat/abc-123"
  │     ├─ AXWebArea (webpage DOM — skipped by diaHistory)
  │     │  └─ ...
  │     └─ AXScrollArea (Dia's native chat panel)
  │        └─ AXList
  │           └─ AXList
  │              ├─ AXGroup
  │              │  └─ AXStaticText "what is 2+2?"
  │              ├─ AXGroup
  │              │  └─ AXStaticText "4"
  │              └─ AXGroup
  │                 └─ AXStaticText "how many r's in strawberry?"
  │
  ├─ AXApplication "Messages"
  │  └─ AXWindow "Sarah"
  │     └─ AXSplitGroup
  │        ├─ AXScrollArea
  │        │  └─ AXTable
  │        │     ├─ AXRow "Sarah"
  │        │     └─ AXRow "Mom"
  │        └─ AXScrollArea
  │           └─ AXList
  │              ├─ AXGroup
  │              │  └─ AXStaticText "hey i had a great time last night!"
  │              └─ AXGroup
  │                 └─ AXStaticText "i think we should just be friends"

to make sure the daemon ran anytime the computer restarted, or dia changed, we made the tool a macOS launch agent.

Problems faced and how we solved them

no "new panel" AX notification exists
1. the AX Observer only had generic "AXValueChanged" events that fire on any change in the application:
1. tab switch, keystroke in address bar, etc.
2. even if we did decide to filter all these specifically for new chat events, sometimes these notifications can fail silently.
3. fix: set up a poller to check for new populated chat panels every 15 seconds, THEN we register AXObserver to listen for new messages.

the findChatGroups function was doing a recursive walk of the whole AX tree, causing a stack overflow on deep parts of the tree:

old:

func findChatGroups(in element: AXUIElement) -> [AXUIElement]? {
      for child in children(of: element) {
          if role(child) == "AXScrollArea", let groups = checkForChat(child) {
              return groups
          }
          if let groups = findChatGroups(in: child) {  // recurse into everything
              return groups
          }
      }
      return nil
  }

here the old version recurses into every single node, including the rendered DOM.

to fix, i added a depth limit and skipped the DOM:

func findChatGroups(in root: AXUIElement) -> [AXUIElement]? {
	var stack = [(element: root, depth: 0)]

	while let (element, depth) = stack.popLast() {
		guard depth < 64 else { continue }          // depth limit
		for child in children(of: element) {
			if role(child) == "AXScrollArea", let groups = checkForChat(child) {
			  return groups
		  }
		  guard role(child) != "AXWebArea" else { continue }  // skip web DOM
		  stack.append((child, depth + 1))
	  }
  }
  return nil
}

every AXObserver notification triggered a full rewrite to a new file.

  1. this meant as a conversation continued, more and more files built up.
  2. fix: fingerprint each conversation with a SHA256 of the domain + first user message and stores the file path it was written to.

memory bloat

  1. a week after using the tool myself, i discovered that the daremon was eating 1.2GB of memory.
    1. since i had started the daemon, each conversation was being stored as state, with no way to way to evict.
  2. fix: added a lastUpdatedAt timestamp to each record and scheduled a repeating timer that prunes stale convos every 24 hours.

ive been using this tool as part of my learning loop and its been marvelous. a future where our agents have context on everything we do is coming - and maybe the AX tree will be a part of it.

i hope this post encourages one person to build a more ambitious project using macOS AX tree!