Metadata-Version: 2.5
Name: gcae
Version: 0.8.0
Summary: Git-Checkpointed Adaptive Execution runtime
Project-URL: Homepage, https://github.com/mberkanbicer/gcae
Project-URL: Documentation, https://github.com/mberkanbicer/gcae/tree/main/docs
Project-URL: Changelog, https://github.com/mberkanbicer/gcae/blob/main/CHANGELOG.md
Author: mberkanbicer
License-Expression: MIT
License-File: LICENSE
Keywords: agents,checkpointing,code-generation,git,llm,tui
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Version Control :: Git
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: httpx>=0.27
Requires-Dist: pydantic>=2.0
Requires-Dist: textual>=1.0
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: mypy>=1.11; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Description-Content-Type: text/markdown

<p align="center">
  <img src="./assets/readme/hero.svg" width="100%"
       alt="GCAE — a single-agent coding runtime that keeps only what it can verify, shown with a real finished-run summary and a checkpoint rail">
</p>

<p align="center">
  <a href="https://github.com/mberkanbicer/gcae/actions/workflows/ci.yml"><img src="https://github.com/mberkanbicer/gcae/actions/workflows/ci.yml/badge.svg" alt="CI status"></a>
  <img src="https://img.shields.io/badge/python-3.12%20%7C%203.13-3776AB?logo=python&logoColor=white" alt="Python 3.12 and 3.13">
  <img src="https://img.shields.io/badge/dependencies-pydantic%20%C2%B7%20httpx%20%C2%B7%20textual-2F81F7" alt="Runtime dependencies: pydantic, httpx, textual">
  <a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-6E7681" alt="MIT license"></a>
</p>

<p align="center">
  <b>Git-Checkpointed Adaptive Execution</b> — an agent runtime that works in an isolated Git
  worktree, commits only what deterministic validation and evaluation accept, rolls back the rest,
  and keeps the failure knowledge that the rollback would otherwise destroy.
</p>

---

## What it is

GCAE runs one coding task at a time against a Git repository. The agent plans the task as
**semantic steps**, executes tools inside a dedicated worktree, validates each step with
deterministic checks, asks a model whether the step actually advanced the objective, and then
either **checkpoints** the step as a commit or **rolls it back** and replans. A final verification
gate checks every success criterion before the run is declared complete. The verified branch is then
merged into your checkout — and that merge is reversible.

It ships as a CLI plus a full-screen terminal dashboard. The engine has no dependency on the TUI and
runs headless in CI.

## What a finished run looks like

```console
$ gcae run ~/csv-parser "fix quoted records across chunk boundaries" --headless
...
gcae: full result in result.json
run d1ae6b3af14d: complete
  accepted steps: 1, commit: d176e5f2effb87af4108bafb4ea0b5d699fecc59
  verification  : 1/1 criteria passed
  worktree      : ~/.local/state/gcae/worktrees/d1ae6b3af14d
  branch        : gcae/d1ae6b3af14d merged into main (undo: gcae undo ~/csv-parser d1ae6b3af14d)
  files         : parser.py
  documents     : ~/csv-parser (in your working tree now)
```

Every line above is production output, not a mock: the run merged its branch, removed its own
worktree, and named the folder holding the file. `gcae undo` puts the branch back. The full
state JSON lands in `./result.json`; stdout stays empty.

A step that fails validation or evaluation never reaches your branch:

```console
$ gcae run ~/project "rework the parser" --headless
gcae WARNING: rollback to 81aac02ef739
...
run 3f81c2d4: failed: step budget exhausted after 8 iterations (raise [runtime] max_steps ...)
  accepted steps: 0, commit: none
  verification  : 0/1 criteria passed
  worktree      : ~/.local/state/gcae/worktrees/3f81c2d4
  branch        : gcae/3f81c2d4 (no file changes; nothing to merge)
  documents     : none — the run produced no files
```

## Why it is different

| Property | How GCAE does it |
| --- | --- |
| Trusted state is explicit | Only an accepted evaluation creates a commit. `accepted_commit` is always a verified tree. |
| Failure is cheap | Rejection runs `reset --hard` plus cleanup **inside the worktree only**; your repository is untouched. |
| Rollback is not amnesia | Facts, decisions and failure lessons live in SQLite and survive every rollback, so the replanned step starts smarter. |
| Work is verified, not assumed | Deterministic criteria (`file exists:`, `file contains:`, `file contains exactly:`, `command succeeds:`) run first; a model judge is opt-in and fails closed. |
| Every git operation is the loop's job | Base commit, commit identity, run branch, worktree, merge, conflict resolution and cleanup are automatic. The model is never allowed to run `git`. |
| Your working tree is never the workshop | The agent edits an isolated worktree under the state directory, one worktree per run. |
| A slow model is never a frozen screen | Model calls stream and report progress: elapsed time, first-token latency, content and reasoning character counts, plus a heartbeat every 10s while the model deliberates. A call that produces nothing for `stall_timeout` fails honestly instead of hanging the run. |
| Self-recovery is the default | Every failure that could end a run — stagnation, unusable model output, a stall, an exhausted budget, a planner outage, a transient network error, even an unexpected exception — is diagnosed from the run's own trace before it is allowed to stop anything. Bounded, and never a substitute for evidence. |
| A broken model is recovered too | A dead or misconfigured model is the one failure self-diagnosis cannot fix, so the role moves to `[models.escalation]` instead: a provider outage, or the same rejection three times, switches the controller for the rest of the run. Transient errors (429, 5xx, timeouts) are retried with backoff first. Verified live: an invalid controller model id still finished the task in 11 seconds. |
| One run per repository | `run`, `resume`, `merge` and `undo` take a per-repository lock, so two runs can never interleave two merges into the same branch. The lock lives in the state directory and dies with the process. |
| A blocked run fixes itself first | Before asking you, a failing run (stagnation, unusable model output, exhausted budget) reads its own trace — events, validation evidence, failure memories — and diagnoses the root cause, then retries with the correction. Bounded, and always outranked by the verification gate. |
| The attempt is the unit, not the tool call | Each semantic attempt is a typed **trajectory step** — goal, expectation, expected evidence, failure signals, verdict, knowledge gained — persisted in `state.json` and visible on the dashboard timeline, with no chat transcript anywhere. |
| Every claim needs evidence | An append-only **evidence ledger** records each command result, validation, interactive session and criterion verdict (supports / contradicts, never scored). Completion requires PASS for every criterion mapped to evidence: no evidence is INSUFFICIENT, contradiction is FAIL. |
| Repair is not rollback | The evaluator can `repair` a valid direction with a broken implementation: the candidate is kept and the next attempt is told what to fix, instead of throwing the work away. |
| Old runs are pruned, lessons stay | `gcae prune` deletes run records beyond `--keep` or older than `--older-than` / `[runtime] run_retention_days`, never touching live runs, recorded merges, repositories or worktrees. Failure lessons stay cumulative; a pruned run's raw evidence rows go with it — nothing reads evidence of an inactive run. |
| Memory is scoped to the repository | Lessons accumulate across runs of one project; retrieval never leaks another project's failures into your context. |

## The execution loop

<p align="center">
  <img src="./assets/readme/loop.svg" width="100%"
       alt="Checkpoint rail: accepted commits a31fc42 and d91f220, a rolled back candidate, the verified branch merged into main, and the failure lesson retained in memory">
</p>

```
PLAN → SEMANTIC STEP → EXECUTE → OBSERVE → VALIDATE → EVALUATE → ACCEPT | ROLLBACK | REPLAN → VERIFY
```

- **PLAN** — a model-backed planner derives the objective, the success criteria, the constraints and
  an ordered step list from your request (or a deterministic planner writes a single step when you
  prefer that).
- **SEMANTIC STEP** — tools run freely *inside* a step; the unit of progress is a verified step, not
  a tool call. A per-step tool budget and a repetition guard keep a confused model bounded.
- **VALIDATE** — deterministic checks: configured commands, `git diff --check`, scope violations,
  dependency-manifest changes, workspace hygiene.
- **EVALUATE** — a structured decision (`accept`, `repair`, `rollback`, `replan`, `continue`,
  `finish_candidate`) with a reason, a progress score and memories to promote.
- **ACCEPT** — checkpoint commit `gcae: <goal>`, working memory settled into durable memory.
- **ROLLBACK** — the candidate is discarded, the lesson is stored as immutable memory, the step is
  replaced by a replanned one.
- **VERIFY** — every success criterion is checked again on the final tree and mapped to ledger
  evidence before the run may complete: PASS needs support, no evidence is INSUFFICIENT,
  contradiction is FAIL.
- **RECOVER** — if the run is about to give up (stagnation, unusable provider output, exhausted
  budget, or an unexpected exception), the recovery advisor reads the run's own trace and returns a `Diagnosis`: root cause,
  one corrective instruction, and a strategy. `replan` queues the correction as the next step and
  grants bounded extra iterations; `ask_user` and `stop` end the run honestly.

## Install

Requires Python 3.12+ and Git. Tags (`v*`) build sdist and wheel in CI and publish them to
[PyPI](https://pypi.org/project/gcae/) via trusted publishing, so once the first release is out:

```bash
python -m pip install gcae
```

Until then, install from the repository:

```bash
git clone https://github.com/mberkanbicer/gcae.git
cd gcae
python -m venv .venv
.venv/bin/python -m pip install -e ".[dev]"     # runtime + test/lint tooling
.venv/bin/gcae --version
```

## First run

```bash
cp config.example.toml config.toml      # config.toml is gitignored, it holds your API key
```

Point it at a local Ollama endpoint (no key needed) or an OpenAI-compatible provider:

```toml
[provider]
kind = "http"                                  # or "openrouter"
base_url = "http://localhost:11434/v1"         # local models need no API key
model = "qwen2.5-coder:14b"

[validation]
commands = ["pytest -q"]                       # deterministic evidence for every step
```

```bash
# interactive dashboard: it asks for the task and the planner derives the criteria
gcae run ~/src/project --config config.toml

# or fully specified
gcae run ~/src/project "add a --dry-run flag to the importer" \
  --criterion "command succeeds: pytest -q" \
  --config config.toml

# non-interactive: log on stderr, full state JSON in ./result.json, stdout stays empty
gcae run ~/src/project "add a --dry-run flag" --headless --config config.toml
```

The repository does not need to be pristine: GCAE creates the base commit it needs, commits your
pending edits as that base (never discarding them), and uses `GCAE <gcae@localhost>` when no Git
identity is configured.

## The dashboard

```
GCAE │ RUNNING │ parser-project │ 7c21a9 │ openrouter · qwen3-14b · ACT │ 12m43s
───────────────────────────────────────────────────────────────────────────────────────
OBJECTIVE  fix quoted records across chunk boundaries   ACTIVE       pytest -q tests/
PLAN       3/5 steps  ✓ inspect  ✓ reproduce  ● fix     CHECKPOINT   trusted a31fc42
NOW        carry quote state across the boundary        VALIDATION   ✓ diff-check  … pytest
CTX ██████░░░░ 9.8k/32k   MEM 18 facts · 6 decisions   ITER 12
EVENTS  20:31:04 ✓ checkpoint a31fc42 · 20:30:57 ↻ replan · 20:29:29 ↩ rollback
[p] Pause  [s] Stop  [d] Diff  [l] Logs  [m] Memory  [c] Context  [i] Instruct  [?] Help  [q] Quit
```

| Key | Action | Key | Action |
| --- | --- | --- | --- |
| `p` / `r` | pause / resume | `d` | candidate diff, per file |
| `s` | stop (asks first) | `l` | full event log with filters |
| `i` | inject an instruction / new task | `m` | memory inspector with provenance |
| `Enter` | open the focused panel's detail | `c` | context inspector (what the model sees) |
| `M` | merge accepted work | `e` / `t` | evaluation / plan detail |

The dashboard renders at any terminal width from 160 columns down to 80, stays responsive while
models and tools run, and never blocks on Git, SQLite or the provider.

## CLI

| Command | Purpose |
| --- | --- |
| `gcae run <repo> [request]` | start a run (`--criterion`, `--constraint`, `--merge/--no-merge`, `--no-auto-bootstrap`, `--tui/--headless`) |
| `gcae resume <repo> <run-id>` | continue a stopped or failed run from its persisted state |
| `gcae list` | runs with status, accepted steps and merge state |
| `gcae inspect <run-id>` | objective, plan, verification, merge state (`--json` for the raw state) |
| `gcae merge <repo> <run-id>` | merge a run branch later, resolving conflicts through the agent |
| `gcae undo <repo> <run-id>` | reverse a recorded merge |
| `gcae input <repo> <run-id> <text>` | answer a live process waiting for input |
| `gcae prune [--keep N] [--older-than DAYS] [--dry-run] [--force]` | delete old run records; never live runs, recorded merges, repos or worktrees |

Exit codes: `0` only when a run completed **and** its work reached your checkout; `1` for handled
errors, failed runs, or a merge that did not happen; `2` for usage errors.

## Use from Python

The supported interface is the `gcae` command; from Python, call it through the entry point:

```python
from gcae.cli import main

main([
    "run", "~/src/project", "add a --dry-run flag to the importer",
    "--criterion", "command succeeds: pytest -q",
    "--headless", "--config", "config.toml",
])
```

For deeper embedding, the runtime is importable directly. One `Runtime` drives one run
(reconstruct → act → observe → judge → commit or revert), and everything it does is
observable as events:

```python
from gcae.http_provider import OpenAICompatibleProvider
from gcae.runtime import Runtime

runtime = Runtime(
    "~/src/project",            # the repository to work on (never modified outside a worktree)
    "~/.local/state/gcae",      # where runs, checkpoints and memory live
    provider=OpenAICompatibleProvider(
        base_url="http://localhost:11434/v1",   # any OpenAI-compatible endpoint
        model="qwen2.5-coder:14b",              # add api_key_env="OPENAI_API_KEY" for hosted ones
    ),
)
runtime.subscribe(print)        # the whole run, as events
runtime.start(
    "add a --dry-run flag to the importer",
    success_criteria=["command succeeds: pytest -q"],
)
state = runtime.run()
print(state.status, state.accepted_commit)
```

## Configuration essentials

| Key | Default | Effect |
| --- | --- | --- |
| `provider.context_limit` | `8192` | token budget for the reconstructed context |
| `provider.json_mode` | `true` | send `response_format=json_object`; disable for reasoning models |
| `provider.generation.max_tokens` | – | output budget; raise it when a planner schema is truncated |
| `validation.commands` | `[]` | deterministic commands run at every evaluation |
| `runtime.max_steps` | `20` | iterations before a run gives up |
| `runtime.auto_bootstrap` | `true` | create the base commit a run needs |
| `runtime.auto_merge` | `true` | merge the verified branch on completion |
| `runtime.merge_accepted_on_failure` | `true` | hand over checkpoints a failed run accepted (labelled unverified) |
| `runtime.resolve_merge_conflicts` | `true` | resolve merge conflicts through the agent |
| `runtime.cleanup_after_merge` | `true` | remove GCAE's worktree once merged |
| `runtime.require_execution_evidence` | `true` | a step that changed code must run something before acceptance |
| `runtime.run_retention_days` | – | `gcae prune` deletes runs older than this (CLI `--older-than` overrides) |

See [`config.example.toml`](./config.example.toml) and
[`docs/CONFIGURATION.md`](./docs/CONFIGURATION.md).

## What GCAE does not do

- **It is not a sandbox.** `run_command` enforces a blocklist and workspace confinement, not
  OS-level isolation. An opt-in `[sandbox] command_prefix` can route every command through a
  wrapper such as bubblewrap or docker; making that wrapper actually isolating stays the
  operator's job. Run it against repositories you can restore.
- **It is not a multi-agent framework.** One agent, one run, no orchestration graph.
- **It does not decide product questions for you.** When the task is ambiguous the agent asks, and
  `ask_user` pauses the run rather than guessing.
- **It cannot verify what you never specified.** Without criteria it can only check what the
  planner inferred; pass `--criterion` for the things that matter.
- **Token figures are estimates** (`chars / 3`), labelled `(est)` everywhere they appear.
- **Parallel runs in one repository are refused**; each run owns one worktree and a per-repository
  lock dies with its process. Parallel runs across different repositories are fine.

## Development

```bash
.venv/bin/pytest -q
.venv/bin/ruff check .                   # lint
.venv/bin/mypy src/gcae                  # strict typing
.venv/bin/python tools/tui_demo.py       # dashboard with real git operations, no model calls
```

CI (`.github/workflows/ci.yml`) runs the same commands on Python 3.12 and 3.13, builds the sdist and
wheel, installs the wheel in a clean environment, and checks that `gcae --version` works. Tags
matching `v*` publish a GitHub release with the build artifacts and publish to PyPI via trusted
publishing — a one-time pending-publisher entry for this repository, workflow (`ci.yml`),
environment (`pypi`) on [pypi.org](https://pypi.org/manage/account/publishing/) is required first.

## Documentation

| Document | Contents |
| --- | --- |
| [`docs/ARCHITECTURE.md`](./docs/ARCHITECTURE.md) | module map, state vs knowledge, data flow |
| [`docs/TRAJECTORY_MODEL.md`](./docs/TRAJECTORY_MODEL.md) | the trajectory step: attempt as primary entity |
| [`docs/EVIDENCE_LEDGER.md`](./docs/EVIDENCE_LEDGER.md) | evidence records, supports/contradicts, no scoring |
| [`docs/VERIFICATION.md`](./docs/VERIFICATION.md) | evidence-backed completion: PASS / FAIL / INSUFFICIENT |
| [`docs/EXECUTION_KNOWLEDGE_SPLIT.md`](./docs/EXECUTION_KNOWLEDGE_SPLIT.md) | reversible execution, cumulative knowledge |
| [`docs/STATE_MACHINE.md`](./docs/STATE_MACHINE.md) | phases, allowed transitions, failure routing |
| [`docs/GIT_EXECUTION.md`](./docs/GIT_EXECUTION.md) | worktrees, checkpoints, merges, conflicts, bootstrap |
| [`docs/MEMORY_CONTEXT.md`](./docs/MEMORY_CONTEXT.md) | memory kinds, retrieval, context budgeting |
| [`docs/CONTEXT_RECONSTRUCTION.md`](./docs/CONTEXT_RECONSTRUCTION.md) | projection priorities, pinned data, no summaries |
| [`docs/FAILURE_RECOVERY.md`](./docs/FAILURE_RECOVERY.md) | failure ladder, escalation, blocked vs failed |
| [`docs/INTERACTIVE_EXECUTION.md`](./docs/INTERACTIVE_EXECUTION.md) | batch / scripted / PTY, timeouts, waiting-for-user |
| [`docs/TOOLS.md`](./docs/TOOLS.md) | tool contracts and the command guardrail |
| [`docs/PROVIDERS.md`](./docs/PROVIDERS.md) | OpenAI-compatible providers, reasoning models, structured output |
| [`docs/TUI.md`](./docs/TUI.md) | dashboard layout, keys, screens, responsive behavior |
| [`docs/CLI.md`](./docs/CLI.md) · [`docs/CONFIGURATION.md`](./docs/CONFIGURATION.md) | commands and every setting |
| [`docs/TEST_PLAN.md`](./docs/TEST_PLAN.md) | suites, mandatory end-to-end scenarios, commands |
| [Wiki](https://github.com/mberkanbicer/gcae/wiki) | task-oriented guides built from these documents; sources live in [`wiki/`](./wiki) and publish with `tools/publish_wiki.sh` |
| [`CHANGELOG.md`](./CHANGELOG.md) | released features and fixes |

## License

MIT — see [`LICENSE`](./LICENSE).
