Skip to content

feat: make Madar worktree-aware so agents never use another worktree's graph #546

Description

@mohanagy

Problem

Agents commonly create an isolated Git worktree and branch before editing.

Madar currently binds its MCP server, hook, graph output, and watcher to one install-time checkout:

  • MCP config points at <projectDir>/out/graph.json
  • graph generation and madar watch write to <root>/out
  • graph freshness uses the graph's stored source root

If an agent moves into a new worktree while the graph artifact remains in the original checkout, Madar can serve the original branch's graph and evaluate freshness against the original checkout rather than the active worktree.

This is a correctness problem, not just a convenience gap:

  • wrong-branch code context and file paths
  • false-fresh graph status
  • incorrect impact, review, or edit guidance
  • branch/worktree information leaking across concurrent agents
  • a parent watcher does not see nested worktree changes because worktree directories are intentionally ignored

Expected behavior

A worktree must be treated as an independent source snapshot.

Madar should resolve the active worktree at MCP/session startup and select or build only that worktree's graph. Artifacts may live outside source worktrees, but must be namespaced per worktree rather than sharing one out/graph.json across branches.

Acceptance criteria

  • Two worktrees at different commits can run concurrently and each MCP session serves only its own graph.
  • Graph identity and freshness validate the canonical worktree root, HEAD, and dirty state; a graph from another worktree is rejected or clearly reported as mismatched.
  • Edits and watcher updates in worktree A never read, overwrite, or mark worktree B stale.
  • Per-worktree locks and atomic publishing prevent concurrent corruption.
  • A newly created worktree lazily creates or reuses its external artifact without manual copying, reinstalling, or placing out/ in the worktree.
  • Cleanup safely garbage-collects artifacts only after worktree removal or pruning.
  • Tests cover parent plus sibling/nested worktrees, divergent branches, concurrent updates, mismatch detection, and removal.

Current workaround

Run madar generate . and the relevant madar <agent> install from inside every worktree. This creates separate in-worktree out/ artifacts, but does not solve agent-created worktrees or external artifact storage.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions