Metadata-Version: 2.5
Name: graphtory
Version: 0.1.1
Summary: Agent task graphs you can trust: prep, work, verify, loop. A validated graph, not the agent, decides what runs next, what must pass and who approves.
Project-URL: Homepage, https://adityakarnam.com/graphtory/
Project-URL: Repository, https://github.com/adityak74/graphtory
Project-URL: Issues, https://github.com/adityak74/graphtory/issues
Author: Aditya Karnam Gururaj Rao
License-Expression: MIT
License-File: LICENSE
Keywords: agents,coding-agents,llm,mcp,temporal,verification,workflow
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Quality Assurance
Requires-Python: >=3.12
Requires-Dist: claude-agent-sdk
Requires-Dist: mcp>=1.2
Requires-Dist: pydantic>=2.7
Requires-Dist: temporalio>=1.7
Description-Content-Type: text/markdown

<div align="center">

# Graphtory

**Agent task graphs you can trust: prep, work, verify, loop.**

Write the plan as code. Run it on [Temporal](https://temporal.io). Every agent step is cached, every gate is a human, and nothing passes until its signals say so.

[Spec](SPEC.md) · [Architecture](#architecture) · [Quickstart](#quickstart) · [Known limits](#known-limits)

</div>

<p align="center"><img src="docs/assets/demo-claude.gif" alt="A Graphtory bug-fix run with real Claude: failing test, three gates approved, merged, verify passes" width="820"></p>

<p align="center"><sub>A real run with real Claude on a toy repo: the agent fixes <code>a - b</code> to <code>a + b</code>, three gates are approved, the branch merges to <code>main</code> and verify passes (about a minute of wall clock). The GIF is the captured output. Regenerate with <code>uv run --with pillow python docs/demo/record_real.py</code>; <code>docs/demo/record.py</code> does the same with the scripted <code>fake</code> agent.</sub></p>

![Graphtory architecture](docs/assets/architecture.png)

## Why

Coding agents are good at writing code and bad at knowing when they are done. Graphtory puts the agent inside a graph that decides that for it:

- **A plan is a contract.** A planner writes the graph in a small Python DSL. It compiles to a JSON plan with per-task PASS signals, is validated, and is approved by a human before anything runs.
- **Verification is not the agent's opinion.** A verify node runs real code (tests, lint, types) in the run's worktree, reruns to catch flaky tests, and only a clean pass counts.
- **Loops are capped, not infinite.** Hit a loop cap or a model credit cap and the run pauses and notifies. `resume` continues it.
- **Durable by default.** Temporal events are the source of truth. LLM steps are cached by run, node and visit, so a worker restart never pays for the same step twice.
- **Any agent, per node.** Agents are plugins behind one small contract. Claude ships today; others are an entry point away.
- **You can stop it.** `graphtory kill` signals every run and revokes deploy credentials first, even if Temporal is down.

## How it works

```
Task → Proposal → Context → Plan (as code) → Validate → ✋ Approve plan
   → Build → Review → Adversarial review → PR → Team review ─┐
   ▲                                                   changes? ┘ (capped)
   → ✋ Approve merge → Merge → ✋ Approve deploy → Deploy → Monitor → Verify → PASS
```

| Piece | What it does |
|---|---|
| `plan` / `dsl` / `validate` | The JSON plan contract, the Python DSL that compiles to it, and the validator (schema, locked judge nodes, signals set) |
| `templates.bugfix` | The first milestone: a ready-made bug-fix graph |
| `runner.workflow` | The Temporal workflow: gates, loop caps, pause and resume, runtime `add_node`, kill |
| `runner.activities` | All I/O: agent calls with an idempotent cache, a git worktree sandbox per run, verification |
| `projection` | A rebuildable SQLite projection of workflow events (the UI reads this) |
| `ui` | A React board (TanStack Query, Router, Table) over the projection: a live dashboard of every run's graph, per-run node states, loop-backs, outputs, a sortable event table, and approve/reject for waiting gates |
| `mcp_server` | Shared run state and per-agent notes, over MCP |
| `adapters` | Agent plugins (`claude`, plus a scripted `fake` for tests) |
| `cli` | `graphtory worker / mcp / ui / plan / run / show / approve / reject / resume / kill / approver-add / dream` |

### The board

<p align="center"><img src="docs/assets/demo-ui.gif" alt="The board live: a run starts, its graph fills in, and each gate is approved from the page" width="820"></p>

<p align="center"><sub>A scripted-agent run, recorded from the real board in a real browser: the run appears, the graph fills in, and each of the three gates is approved from the page. Regenerate with <code>uv run --with playwright --with pillow python docs/demo/record_ui.py REPO</code> (needs the stack running).</sub></p>

![The dashboard: three runs live, each with its own graph](docs/assets/dashboard.png)

![The Graphtory board showing a finished run with a review failure and a loop back to build](docs/assets/ui.png)

<sub>`/` is a live overview of every run (live ones as full graphs, finished ones as a list); a run opens to its graph, where `?node=review` deep-links a selection. A waiting gate shows Approve and Reject. With gate authentication on, the browser computes the HMAC proof itself and the token never reaches the server. The screenshot is the real forced-review-fail run: the review failed once, the run looped back to `build`, then merged and verified. The built app is committed under `src/graphtory/ui_dist`; to change it, `cd ui && npm ci && npm run dev` (proxies `/api` to `graphtory ui`), then `npm run build`. The interface direction follows [unslop](https://github.com/heydamir/unslop) by Damir Sokolovsky (CC BY 4.0): quiet graphite chrome, one status dot, color only where something is live or failed.</sub>

## Quickstart

Requires Python 3.12+, [uv](https://docs.astral.sh/uv/) and the [Temporal CLI](https://docs.temporal.io/cli).

```bash
git clone https://github.com/adityak74/graphtory && cd graphtory
uv sync
uv run pytest -q                              # the whole suite, no services needed

temporal server start-dev                     # Temporal on localhost:7233
uv run graphtory worker                       # workflows + activities
uv run graphtory mcp                          # MCP server on 127.0.0.1:7801
uv run graphtory ui                           # live board on http://127.0.0.1:7802

uv run graphtory run --repo PATH --title "fix the failing test" \
    --test-cmd "pytest -q" --agent claude --solo
uv run graphtory plan --repo PATH --title "add feature X" --test-cmd "pytest -q" --out plan.json   # an agent designs the graph
uv run graphtory run --repo PATH --plan plan.json --solo                                              # run the plan you reviewed
uv run graphtory run --repo PATH --prompt "add feature X; it must ..." --test-cmd "pytest -q"           # free-text task: plan it and start (use - to read stdin)
uv run graphtory show RUN_ID                  # state, plan, PASS signals
uv run graphtory approve RUN_ID plan_gate --user bob
uv run graphtory approver-add bob            # optional: require a per-approver token on gates, resume and kill
uv run graphtory dream                       # morning report from the run history (read-only, proposes only)
```

- Use `--agent fake` to walk the whole graph without calling a model.
- The worker and MCP server must share the same `GRAPHTORY_HOME` / `GRAPHTORY_DB` (default `~/.graphtory`, `shared.db`), or agents will see an empty projection.
- The worker is versioned (deployment `graphtory`, build id = package version). On a fresh namespace, set that version as current before workflows run, for example `temporal worker deployment set-current-version --deployment-name graphtory --build-id <version> --yes`. Verified against Temporal CLI 1.10.

## Writing a plan

Plans are code, so they diff, review and test like code. The bug-fix template is the reference: it wires the nodes, the gates, the loop caps and the PASS signals, then the validator refuses to start anything malformed (`graphtory run` exits 2 before touching Temporal).

For feature work, `graphtory plan` has an agent design the graph in a throwaway clone of your repo, retries with the validator's complaints, and writes `plan.json` for you to read before `graphtory run --plan`. The planner chooses the shape and the prompts, never what runs on your machine: verify always runs only your `--test-cmd`, every node uses the agent you chose, and loop caps are clamped to 5.

## Status

**v0.1, early.** The bug-fix flow runs end to end against real activities, and the workflow, projection, activities, validator and CLI are covered by 126 tests. It has passed with real Claude on toy repos. Proven with a real model: a forced review failure; an unforced adversarial-review failure that looped back to build; an unforced verify failure (strict tests hidden from the agent) that looped back to build and then passed; and a planner-designed two-build plan run end to end. Each run used a live dev server and the versioned worker, with gates approved by hand or from the board. Not yet proven: loop caps and `resume` against a real model, three or more builds, and a long run. Feature work has started (`graphtory plan`); a board ships (`graphtory ui`): a live dashboard, and gates can be approved from it or the CLI. Dream mode has its first slice (`graphtory dream`, a read-only report); the optimizer, replay and auto-revert, and full apps, are next. See [SPEC.md](SPEC.md) for the design and the open gaps.

## Known limits

- Gate authentication is opt-in. `graphtory approver-add NAME` turns it on: approvals then carry an HMAC of the approver's token (per run, node and decision), and forged, borrowed or missing proofs are ignored. With no `approvers.json`, `--user` is honour-system. `resume` and `kill` carry the same proof (per run and per action), so with authentication on a forged or missing proof is ignored and logged. A `kill` from someone who is not an approver does nothing, so to stop everything without a token, stop the worker. Anyone who can reach Temporal can still terminate a workflow through its own API; put Temporal's auth in front of it.
- The Claude adapter runs Bash inside the SDK sandbox (writes only to the worktree, no network, no host reads), denies the file tools outside the worktree, and blanks host secrets (names containing TOKEN, KEY, SECRET, PASSW or CREDENTIAL, except the agent's own login) in the agent's environment. `uv run python scripts/sandbox_drill.py` has the agent try ten routes out and judges by what lands on disk; it passes on macOS (the first run found the environment leak, now fixed). Not tested on Linux: run the script there. The sandbox's own proxy credential (`CLOUDSDK_PROXY_PASSWORD`) is still visible inside the agent's shell. Other adapters bring their own isolation.
- `git add -A` commits whatever the agent writes to the worktree branch.
- `kill` stops workflows at the next node boundary (stop the worker to hard-stop) but revokes creds immediately.
- The local MCP server (127.0.0.1:7801) and the board (127.0.0.1:7802) have no login. The board refuses any `Host` or `Origin` other than itself and its only write is approving or rejecting a waiting gate, which the workflow still checks (requester, and the HMAC proof when gate authentication is on). With it off, `--user` is honour-system on the board just as on the CLI. Anything that outputs say is shown to whoever can reach it.
- The projection is auxiliary; Temporal is authoritative.

## Contributing

Issues and PRs are welcome. Run `uv run pytest -q` before you push; every behaviour in the workflow has a test, and new ones should too.

The architecture diagram was drawn from the [Graphtory whiteboard](https://claude.ai/artifact/SSWyLKELi2GtZ21A1uQwtH); a vector copy is at [docs/assets/architecture.svg](docs/assets/architecture.svg).

## License

[MIT](LICENSE)

## Running on more than one machine

What works: Temporal is the shared coordinator, so workers on other hosts can poll the same namespace.
`--repo` may be a git URL (https, ssh, `file://`, or `user@host:path`); each worker keeps its own cached clone under
`$GRAPHTORY_HOME/repos/<hash>` (cloned on first use, `git fetch --prune` after). Each worker host sets `GRAPHTORY_MCP_URL`
to an MCP endpoint reachable from that host (the `mcp_url` run argument is only a fallback). Skills and SOUL are stored
content-addressed, and `graphtory skill-export DIR` / `skill-import DIR` copy them between hosts so a pinned plan finds
the same bytes everywhere.

What is not built: a networked projection store. The SQLite projection (`GRAPHTORY_DB`, default
`$GRAPHTORY_HOME/shared.db`) is per host. Pointing `GRAPHTORY_DB` at a shared path only works with WAL-safe file
semantics, which NFS does not provide, so do not. Recommended: one projection-writer host, with the other workers
sending events there through `record_event` on the control queue (the `--control-queue` run argument already exists).
The per-run MCP token secret (`mcp_secret`) and the approvers file are also per-host; the MCP server and workers that
talk to it must share the same `$GRAPHTORY_HOME/mcp_secret`. Merging still happens in whatever checkout the merging worker has.
