Visualizing Multi-Branch Coding Agent Sessions: Event-Sourced DAGs, Rollback Checkpoints, and Lightweight Harnesses
Why Linear Chat Transcripts Fail Modern Coding Agents
A single-threaded markdown chat log made sense when an assistant answered one question at a time. Today's coding harnesses—whether lean native UIs or multi-agent CLI runners—branch constantly:
- Parent orchestrator spawns 3 parallel subagents across separate git worktrees.
- One branch fails
pytest, rewinds to checkpointT-4, and tries an alternative patch. - Another branch compacts context mid-run.
Flattening that execution tree into a linear scroll hides where tokens were wasted and makes post-mortem debugging painful.
Event-Sourced Session DAG Schema
Instead of logging unstructured strings, emit immutable JSONL events keyed by (session_id, node_id, parent_node_id) with a Git tree snapshot on every file mutation:
pythonfrom dataclasses import dataclass, asdict import json import time import uuid @dataclass class AgentTurnNode: session_id: str node_id: str parent_node_id: str | None agent_role: str tool_name: str git_tree_sha: str | None tokens_in: int tokens_out: int status: str # "ok" | "error" | "reverted" ts_ms: int = 0 def to_jsonl(self) -> str: payload = asdict(self) payload["ts_ms"] = self.ts_ms or int(time.time() * 1000) return json.dumps(payload) def record_step(log_path: str, parent_id: str | None, role: str, tool: str, tree_sha: str | None) -> str: node_id = uuid.uuid4().hex[:8] node = AgentTurnNode( session_id="sess_91a", node_id=node_id, parent_node_id=parent_id, agent_role=role, tool_name=tool, git_tree_sha=tree_sha, tokens_in=1420, tokens_out=310, status="ok", ) with open(log_path, "a", encoding="utf-8") as f: f.write(node.to_jsonl() + "\n") return node_id
Takeaway
Once every agent turn carries parent_node_id and git_tree_sha, you get interactive session graphs, one-click branch rollbacks, and exact per-subtree cost attribution for free—even inside a sub-50MB native harness.