Raven-1/evolver/tree/git_ops.py
Handsome-wzw 7b45527b5b fix(tools): an array sent as its own JSON is still an array
## Summary

Reported as "deliver_files is broken". It is not: **the tool never ran.**

Every `deliver_files` call on one install was refused before dispatch -- five in a row in a single session -- and nothing was ever delivered:

```
Error: Invalid parameters for tool 'deliver_files': files should be array
```

What the model sent was the array, spelled as a string:

```json
{"files": "[{\"path\": \"...poster.png\", \"title\": \"...\"}]", "message": "..."}
```

`cast_params` already forgives that spelling for every scalar type -- `"3"` for an integer, `"true"` for a boolean -- and did not for arrays or objects, which are exactly the types a model is most likely to stringify.

## Type

- [x] Fix
- [ ] Feature

## The edge of the forgiving is the design

When the value is a string, try `json.loads`, and take the result **only if it is the type the schema asked for**. A string that is not JSON, or is JSON of the wrong shape, is returned unchanged so the validator still names the real problem -- the point is to widen what is accepted, not to swallow a type error.

**Nothing here can raise.** Turning "your argument has the wrong type" into a traceback would be worse than the refusal it replaces, so the parse is wrapped and failure falls through.

**A `type: "string"` field is never parsed.** JSON is ordinary text in a message body or a file's content, and replacing it would hand the tool something it never asked for. Two independent things prevent it today (the string branch returns before the parse is reached, and the parse only wins on a type match), which is why the case pinning it is a tripwire rather than a load-bearing assertion -- said so in its own docstring rather than left to look like coverage.

## Not just deliver_files

Six built-in tools declare an array or object parameter and were all exposed:

| tool | parameter |
|---|---|
| `deliver_files` | `files` |
| `ask_user` | `questions` |
| `image_generate` | `images` |
| `message` | `media` |
| `create_playbook` | `skills` |
| `video_generate` | `params` |

The same failure was already seen from the other side: `acp_client/acp_agent.py` records `update_task_state` and `operations should be array` from a real dispatch.

## Verification

```
make lint-python    ruff check + ruff format --check, 1850 files
make lint-imports   9 contracts kept, 0 broken
pytest tests/       19,699 passed, 7 failed (all pre-existing, see below)
```

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots -- no user-facing string

**End to end, through the real tool**, with the exact payload that failed five times in production:

```
files sent as:            str -> [{"path": ".../poster.png", "title": "..."}]
returned:                 Delivered 1 file(s): poster.png (72 B).
still "should be array":  False
registered in the store:  True
```

Twelve cases, five mutations, each killing at least one:

| mutation | red |
|---|---|
| drop the whole branch (the old behaviour) | 3 |
| raise instead of falling through on a parse failure | 5 |
| accept the parse whatever type it yielded | 1 |
| do not re-cast the parsed value, so items are not cast | 1 |
| widen the tuple to include `string` | **survived -- unreachable** |

The last is worth stating rather than hiding: that mutation cannot change behaviour, because the `type == "string"` branch returns earlier and the type check would refuse the result anyway. I also tried moving the new block above that branch, alone and combined with widening the tuple, and neither reddens anything either. The invariant holds by construction; the case that documents it says so.

**Pre-existing failures, checked against the base.** 7 failed in the full run, all in `tests/test_security_network.py` (`test_a_redirect_to_a_dangerous_address_is_refused` and siblings). Running that file alone on this same tree gives 4 -- the count is order-dependent -- and the change touches only `raven/agent/tools/params.py` plus a new test file, neither of which `raven/security/network.py` imports. The failures are the box's resolver: it returns poisoned answers roughly half the time, which I measured separately (`en.wikipedia.org` came back as `2001::1` on 3 of 6 lookups). Not introduced here, and worth someone's attention on its own.

## Risk

- [x] Security impact considered -- widens what reaches a tool's own validation, not what bypasses it. A parsed value is still validated against the same schema, and a parse that yields the wrong type is refused exactly as before
- [x] Backward compatibility considered -- a call that worked before takes the identical path; the new branch is reached only by a `str` where an array or object is declared, which previously always failed
- [x] Rollback path is clear -- single-commit revert

## Related Issues

N/A

## Swept

`mr-review-patterns` pre-submit sweep, over `git diff origin/main...HEAD`.

- **1 (a write location moved)** -- nothing moved; a branch was added to an existing dispatch on `target_type`. Grepped every caller of `cast_params`: the registry is the only one, and it runs `validate_params` right after, which is the reader this affects.
- **2 (a value with several entry points)** -- the cast is the single funnel every tool argument passes through, which is why the fix belongs here and not in `deliver_files`.
- **3 (a guard)** -- checked both directions: what it wrongly lets through (JSON of the wrong type, non-JSON text -- both refused, with cases) and what it wrongly refuses (a real list, unaffected, with a case).
- **4 (tests)** -- five mutations above, one honestly reported as unreachable. Failing-ID sets diffed against the base rather than counts.
- **6 (stale text)** -- the module docstring enumerated what is coerced ("a string that should be an integer"); it now names arrays and objects too. Grepped the tracked tree for other statements of the rule: none.
- **9 (error handling)** -- the new path adds one way to end and it is caught: `json.loads` raises `ValueError` on bad text and `TypeError` on a non-string, and both fall through to the untouched value.
- **11** -- `git ls-files | grep -c node_modules` is 0; the commit-message gate was run, not eyeballed.

Not applicable, with reasons: **3b, 3c, 3d, 3e** (no tool schema produced, no state moved, one lane, no assertion lost its symbol); **4b, 4c, 4d** (no platform branch, no shell, no rendered number); **5, 5b** (no dependency; `json` is stdlib and already imported across this package); **6b, 6c** (no backup, no generated artifact); **7, 7b** (nothing on disk); **8** (no asset, no secret); **10** (no prompt text).

## Deliberately not fixed

The error text still ends with `[Analyze the error above and try a different approach.]`, and the model plainly did not -- it sent the same shape five times. A message naming the expected spelling (`files must be a JSON array, not a string containing one`) would give it something to act on. That is a change to the registry's error surface, affects every tool, and deserves its own argument.

## Moved from !594

Opened on a compliant source branch. AGENTS.md 2.1 asks for a 3-5 word snake_case description and the previous branch had seven; GitLab cannot change an existing merge request's source branch, so the change moves rather than the name.

Same commits, rebased onto `c2a9768f`. Both code findings from the review on !594 are already in them -- `RecursionError` caught with its regression case, and the implementation comment reduced to the two non-obvious reasons. Nothing else changed, so that review carries.
2026-09-09 15:42:36 +00:00

455 lines
16 KiB
Python

"""Low-level Git command wrappers for the evolver tree (C2.1).
These functions wrap ``subprocess`` calls to ``git`` so the evolver can
construct commits without depending on a particular Python Git library.
They are intentionally small, side-effect-explicit, and easy to mock in
tests via a custom ``repo_root``.
Two operational categories:
1. **Read** (cheap, no state mutation):
:func:`get_current_sha`, :func:`commit_exists`, :func:`get_tree_sha`,
:func:`get_commit_message`.
2. **Write that does NOT touch the working tree**:
:func:`apply_patch_as_commit` uses Git plumbing (``read-tree`` /
``apply --cached`` / ``write-tree`` / ``commit-tree``) so the
user's working directory is never modified. This is what
``EvolverTreeStore.create_child_node`` uses to spawn child nodes.
3. **Write that DOES create a working tree**:
:func:`create_worktree` / :func:`remove_worktree` use
``git worktree`` to materialise a checkout of a specific commit
at a separate path — used later for evaluation runs. Not used by
commit construction.
Why subprocess and not pygit2 / GitPython:
- Zero extra dependency
- Git plumbing semantics are stable and well-documented
- Easy to debug (we just print commands on failure)
Error model: any non-zero exit code raises :class:`GitOpError` with the
exact command and stderr. Callers should not catch this except at the
top of a logical operation.
"""
from __future__ import annotations
import os
import shutil
import subprocess
import tempfile
from contextlib import contextmanager
from pathlib import Path
from typing import Iterator, Optional
# ---------------------------------------------------------------------------
# Exceptions
# ---------------------------------------------------------------------------
_EPHEMERAL_ROOT: Optional[Path] = None
def set_ephemeral_root(path: Optional[Path]) -> None:
"""Route ephemeral worktree temp dirs under a run-scoped directory.
By default they land in the system temp dir; the context managers clean
them on any Python exit (including Ctrl-C), but a hard kill (SIGKILL,
power loss) leaves them behind — still registered as worktrees of the
subject repo. Parking them under the run's work_dir makes leftovers
discoverable and lets the next launch sweep exactly its own garbage
without touching concurrent runs. ``None`` restores the system default.
"""
global _EPHEMERAL_ROOT
_EPHEMERAL_ROOT = None if path is None else Path(path)
def _ephemeral_dir() -> Optional[str]:
if _EPHEMERAL_ROOT is None:
return None
_EPHEMERAL_ROOT.mkdir(parents=True, exist_ok=True)
return str(_EPHEMERAL_ROOT)
class GitOpError(RuntimeError):
"""Raised when a git command exits non-zero."""
def __init__(self, cmd: list[str], returncode: int, stderr: str, stdout: str = ""):
self.cmd = cmd
self.returncode = returncode
self.stderr = stderr
self.stdout = stdout
super().__init__(
f"git command failed (exit {returncode}): {' '.join(cmd)}\n"
f"stderr: {stderr.strip()}\n"
f"stdout: {stdout.strip()}"
)
# ---------------------------------------------------------------------------
# Core runner
# ---------------------------------------------------------------------------
def _run(
repo_root: Path,
*args: str,
env: Optional[dict[str, str]] = None,
input_text: Optional[str] = None,
check: bool = True,
) -> str:
"""Run ``git <args>`` in ``repo_root`` and return stdout.
:param env: extra env vars merged on top of os.environ
:param input_text: passed to stdin (used by ``git apply``)
:param check: raise :class:`GitOpError` on non-zero exit
"""
cmd = ["git", *args]
merged_env = {**os.environ, **(env or {})}
proc = subprocess.run(
cmd,
cwd=str(repo_root),
env=merged_env,
input=input_text,
capture_output=True,
text=True,
)
if check and proc.returncode != 0:
raise GitOpError(
cmd=cmd,
returncode=proc.returncode,
stderr=proc.stderr,
stdout=proc.stdout,
)
return proc.stdout
# ---------------------------------------------------------------------------
# Read operations
# ---------------------------------------------------------------------------
def get_current_sha(repo_root: Path) -> str:
"""Return the SHA of the current HEAD."""
return _run(repo_root, "rev-parse", "HEAD").strip()
def get_tree_sha(repo_root: Path, commit_sha: str) -> str:
"""Return the tree SHA referenced by ``commit_sha``."""
return _run(repo_root, "rev-parse", f"{commit_sha}^{{tree}}").strip()
def commit_exists(repo_root: Path, sha: str) -> bool:
"""Return True iff ``sha`` is a known commit object in this repo."""
try:
out = _run(repo_root, "cat-file", "-t", sha).strip()
except GitOpError:
return False
return out == "commit"
def get_commit_message(repo_root: Path, sha: str) -> str:
"""Return the full commit message (subject + body) for ``sha``."""
return _run(repo_root, "log", "-1", "--format=%B", sha)
def read_file_at(repo_root: Path, sha: str, rel_path: str) -> bytes:
"""Return the raw bytes of ``rel_path`` as stored in commit ``sha``.
Binary-safe (``_run`` decodes text; blobs may be arbitrary bytes). Raises
:class:`GitOpError` when the path does not exist in that commit.
"""
cmd = ["git", "cat-file", "blob", f"{sha}:{rel_path}"]
proc = subprocess.run(cmd, cwd=str(repo_root), capture_output=True)
if proc.returncode != 0:
raise GitOpError(
cmd=cmd,
returncode=proc.returncode,
stderr=proc.stderr.decode("utf-8", errors="replace"),
)
return proc.stdout
# ---------------------------------------------------------------------------
# Write — construct commits WITHOUT touching the working tree
# ---------------------------------------------------------------------------
@contextmanager
def _temp_index() -> Iterator[Path]:
"""Yield a path for a temporary git index file, cleaning up after."""
fd, name = tempfile.mkstemp(prefix="evolver-idx-", suffix=".tmp")
os.close(fd)
path = Path(name)
# ``git read-tree`` requires the file not to exist yet; remove it.
if path.exists():
path.unlink()
try:
yield path
finally:
if path.exists():
path.unlink()
def apply_patch_as_commit(
repo_root: Path,
parent_sha: str,
unified_diff: str,
message: str,
*,
author_name: str = "evolver-bot",
author_email: str = "evolver@raven.local",
) -> str:
"""Construct a child commit by applying ``unified_diff`` on top of
``parent_sha``. **Does not touch the working tree or move HEAD.**
Returns the SHA of the new commit.
Algorithm (Git plumbing):
1. ``git read-tree parent_sha`` into a temp index file
2. ``git apply --cached --index=<temp>`` the patch into that index
3. ``git write-tree`` from that index → tree_sha
4. ``git commit-tree tree_sha -p parent_sha -m message`` → child_sha
The new commit is reachable only via its returned SHA — no branch
ref is updated. Callers (typically :meth:`EvolverTreeStore.
create_child_node`) record the SHA in
:attr:`HarnessNode.git_commit_sha` and may later create a ref
via :func:`create_branch`.
If the patch fails to apply cleanly, raises :class:`GitOpError`
and no commit is created.
:raises GitOpError: if any sub-step fails
:raises ValueError: if ``parent_sha`` doesn't exist locally
"""
if not commit_exists(repo_root, parent_sha):
raise ValueError(f"parent_sha {parent_sha!r} is not a known commit in {repo_root}")
git_dir = (repo_root / ".git").resolve()
if not git_dir.exists():
# Worktrees have .git as a file pointing to gitdir; fall back to
# asking git itself.
git_dir_str = _run(repo_root, "rev-parse", "--git-dir").strip()
git_dir = (repo_root / git_dir_str).resolve()
with _temp_index() as idx:
# The custom index file is selected via GIT_INDEX_FILE env var
# for the duration of each plumbing call.
env = {"GIT_INDEX_FILE": str(idx)}
# Step 1: load parent tree into temp index
_run(repo_root, "read-tree", parent_sha, env=env)
# Step 2: apply the diff to the temp index (no working-tree touch)
_run(
repo_root,
"apply",
"--cached",
"--allow-empty",
"-", # read patch from stdin
env=env,
input_text=unified_diff,
)
# Step 3: write a tree object from the temp index
tree_sha = _run(repo_root, "write-tree", env=env).strip()
# Step 4: commit-tree to create the actual commit. Use environment
# variables for author / committer so the call doesn't depend on
# the global git config (which might not have user.name/email set
# on a CI box).
commit_env = {
"GIT_AUTHOR_NAME": author_name,
"GIT_AUTHOR_EMAIL": author_email,
"GIT_COMMITTER_NAME": author_name,
"GIT_COMMITTER_EMAIL": author_email,
}
child_sha = _run(
repo_root,
"commit-tree",
tree_sha,
"-p",
parent_sha,
"-m",
message,
env=commit_env,
).strip()
return child_sha
def commit_files_as_child(
repo_root: Path,
parent_sha: str,
files: dict[str, bytes],
message: str,
*,
deletions: tuple[str, ...] = (),
author_name: str = "evolver-bot",
author_email: str = "evolver@raven.local",
) -> tuple[str, list[str]]:
"""Commit a set of edited files as a child of ``parent_sha``.
The "edit-then-commit" apply path: the driver edits files directly (weak
models do this far more reliably than emitting a valid unified diff), and we
capture the result as a real git commit off the parent — giving the node a
reproducible SHA, git ancestry, and an immutable-guardable changed-file list,
instead of a "sandbox" placeholder + live-tree mutation.
``files`` maps repo-relative paths to their full new bytes; ``deletions``
lists repo-relative paths to remove. Materialises them into a detached
worktree at ``parent_sha``, stages everything, and ``commit-tree``s onto
``parent_sha``. **The main working tree is never touched.** Returns
``(child_sha, changed_paths)``.
"""
if not commit_exists(repo_root, parent_sha):
raise ValueError(f"parent_sha {parent_sha!r} is not a known commit in {repo_root}")
with tempfile.TemporaryDirectory(prefix="evolver-edit-", dir=_ephemeral_dir()) as tmp:
wt = Path(tmp) / "wt"
create_worktree(repo_root, wt, parent_sha)
try:
for rel, data in files.items():
dest = wt / rel
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_bytes(data if isinstance(data, bytes) else str(data).encode())
for rel in deletions:
fp = wt / rel
if fp.exists():
fp.unlink()
_run(wt, "add", "-A")
changed = [
line for line in _run(wt, "diff", "--cached", "--name-only", parent_sha).splitlines() if line.strip()
]
tree_sha = _run(wt, "write-tree").strip()
commit_env = {
"GIT_AUTHOR_NAME": author_name,
"GIT_AUTHOR_EMAIL": author_email,
"GIT_COMMITTER_NAME": author_name,
"GIT_COMMITTER_EMAIL": author_email,
}
child_sha = _run(
wt,
"commit-tree",
tree_sha,
"-p",
parent_sha,
"-m",
message,
env=commit_env,
).strip()
finally:
remove_worktree(repo_root, wt)
return child_sha, changed
@contextmanager
def worktree_at(repo_root: Path, sha: str) -> Iterator[Path]:
"""Yield an ephemeral detached worktree checked out at ``sha``, cleaned up
after. The eval-time counterpart of :func:`commit_files_as_child` — a scorer
runs against this checkout instead of the mutated live repo."""
with tempfile.TemporaryDirectory(prefix="evolver-wt-", dir=_ephemeral_dir()) as tmp:
wt = Path(tmp) / "wt"
create_worktree(repo_root, wt, sha)
try:
yield wt
finally:
remove_worktree(repo_root, wt)
# ---------------------------------------------------------------------------
# Branch / ref management (lightweight; tag-style use only)
# ---------------------------------------------------------------------------
def create_ref(repo_root: Path, ref_name: str, sha: str) -> None:
"""Point ``ref_name`` (e.g. ``refs/evolver/<node_id>``) at ``sha``.
Evolver-constructed commits are otherwise reachable only through SHAs
recorded in JSON (journal / node ledger), which ``git gc`` does not see —
an unreferenced candidate commit would be pruned after the default grace
period, breaking late worktree evals and the post-hoc sealed unseal. A
plain ref (not a branch: never checked out, invisible to ``git branch``)
anchors it. Overwrites if the ref already exists.
"""
_run(repo_root, "update-ref", ref_name, sha)
def delete_ref(repo_root: Path, ref_name: str) -> None:
"""Remove a ref created by :func:`create_ref` (no-op if absent)."""
_run(repo_root, "update-ref", "-d", ref_name, check=False)
def create_branch(repo_root: Path, branch_name: str, sha: str) -> None:
"""Create a branch named ``branch_name`` pointing at ``sha``.
Used to give an evolver-constructed commit a human-readable ref so
it isn't garbage-collected. Does **not** check out the branch —
HEAD remains where it was.
"""
_run(repo_root, "branch", branch_name, sha)
def branch_exists(repo_root: Path, branch_name: str) -> bool:
"""True iff a local branch with this name exists."""
proc = subprocess.run(
["git", "show-ref", "--verify", "--quiet", f"refs/heads/{branch_name}"],
cwd=str(repo_root),
capture_output=True,
text=True,
)
return proc.returncode == 0
def delete_branch(repo_root: Path, branch_name: str, *, force: bool = True) -> None:
"""Delete a local branch. Force-delete by default since evolver
branches are throwaway anchors, not main-line work."""
flag = "-D" if force else "-d"
_run(repo_root, "branch", flag, branch_name)
# ---------------------------------------------------------------------------
# Worktree management (for future eval runs; not used by commit construction)
# ---------------------------------------------------------------------------
def create_worktree(repo_root: Path, target_path: Path, sha: str) -> None:
"""Create a temporary worktree at ``target_path`` checked out at
``sha``. Caller must clean up with :func:`remove_worktree`.
Used by the evaluation driver (later C-series tasks) to get a clean
checkout of a specific harness version for running tests, without
disturbing the main working tree.
"""
_run(repo_root, "worktree", "add", "--detach", str(target_path), sha)
def remove_worktree(repo_root: Path, target_path: Path, *, force: bool = True) -> None:
"""Tear down a worktree previously created by :func:`create_worktree`."""
args = ["worktree", "remove", str(target_path)]
if force:
args.insert(2, "--force")
_run(repo_root, *args)
# On rare interrupt cases git may leave the directory; clean up.
if target_path.exists():
shutil.rmtree(target_path, ignore_errors=True)
__all__ = [
"GitOpError",
"apply_patch_as_commit",
"commit_files_as_child",
"worktree_at",
"branch_exists",
"commit_exists",
"create_branch",
"create_ref",
"create_worktree",
"delete_branch",
"delete_ref",
"get_commit_message",
"get_current_sha",
"get_tree_sha",
"read_file_at",
"remove_worktree",
]