Skip to content

Reference

Transformation registry

Patch plans describe what could change. A transformation pass goes one step further: it produces the exact change as a diff, under rules strict enough that the change can be tried safely.

from agentloop.transformations import default_registry, execute_candidate, propose

registry = default_registry()
candidate = propose(
    registry,
    "config.json_set",
    {"file": "config/agent.json", "pointer": "/model", "expected": "large-model", "value": "small-model"},
    repo=".",
)
print(candidate["status"], candidate["diff"])

propose is a dry run: it reads the checkout and writes nothing. Trying the candidate is a separate, explicit step:

result = execute_candidate(
    candidate,
    repo=".",
    validation=["python", "-m", "pytest", "-q", "tests/test_agent.py"],
    allow_execution=True,
)
result["status"]   # "accepted", "rejected" or "error"

The pass contract

Every pass declares, before it runs, what it applies to and what it needs. registry.contracts() lists them:

Field Meaning
applies_to Finding types, or host_request, that the pass can address
required_evidence What must hold before it proposes anything
allowed_effects The operation effects, from the execution IR, that it tolerates
objective The resource it aims to reduce
validation How to check a candidate
rollback How to undo it

A pass either proposes a candidate or abstains with reasons. It never guesses. Registering a second pass with an existing id is an error, not an override.

Built-in passes

config.json_set

Change one value in a JSON file inside the checkout, at a JSON pointer such as /model/name. The request names the value you expect to find there, and the pass abstains if the file says anything else. Type counts too: 3 and 3.0 are different values. It also abstains for: - paths outside the checkout, or absolute paths; - files that aren't .json; - missing pointers; - a new value equal to the current one.

The file keeps its indentation and line endings.

ast.memoize_pure_function

Add a bounded @functools.lru_cache(maxsize=N) to one module-level function, and import functools if the module lacks it. Version 2 of the pass. It's deliberately narrow: "pure and deterministic" isn't enough to make a cache safe, so the request also declares the cache it wants:

{
    "span_id": "...",
    "cache": {"maxsize": 256, "scope": "process", "freshness": "static"},
    "arguments": "hashable",
    "returns": "immutable",
}

The pass proposes only when all of these hold:

  • The span has a source receipt that resolves exactly against the checkout.
  • The span declares effect: "none" and deterministic: true in agentloop.effects.
  • Size: maxsize is from 1 to 1,000,000. An unbounded cache that lives as long as the process can grow without limit, so the pass never proposes one.
  • Scope: process. A module-level cache shares results between every caller and tenant in the process. For results that must stay per request or per tenant, use request-local reuse instead.
  • Freshness: static. Results depend only on the arguments, never on time, files, configuration or remote state. Results that can go stale need invalidation that this pass doesn't provide.
  • Arguments are declared hashable, and no parameter is annotated with an unhashable type such as list or dict[str, int].
  • Returns are declared immutable. The pass also refuses a function that is annotated to return a mutable type, or that returns a list, dict or set literal: one cached object would be shared by every caller.
  • The function is a plain, synchronous, module-level function that isn't already cached and doesn't use global or nonlocal state. Async functions, generators and methods are refused.

When anything is missing, the pass abstains and lists every reason. The receipt records the declared cache. A call that raises isn't cached, so it runs again next time. The decorator goes directly above def, so tracing decorators still record each call.

Candidates and receipts

A candidate records: - the pass and its version, and the request; - the evidence it relied on; - each changed file's path, its digest before and after, and the changed lines; - a unified diff.

receipt_sha256 is a digest of all of that. The same request against the same files gives the same receipt, wherever the checkout lives. Receipts contain no absolute paths.

conflicts(candidates) lists pairs of candidates that change the same or adjacent lines of a file, or that were proposed against different versions of it. Apply at most one of each pair.

Executing a candidate

execute_candidate refuses to run unless you pass allow_execution=True. When you do, it:

  1. Creates a temporary git worktree from HEAD, outside your checkout.
  2. Proposes the candidate again in that worktree, and rejects it unless the receipt is identical. This catches a candidate proposed against uncommitted edits, or a stale receipt. Commit first, or propose again.
  3. Writes the change and records git diff.
  4. Runs your validation command in the worktree. It's an argv list with no shell. Exit code 0 passes. Anything else, or a timeout, rejects the candidate. At the timeout, or on cancel, the command and every process it started are stopped (termination says how).
  5. Removes the worktree, then checks that your checkout's HEAD and git status are unchanged.

Rejected candidates are returned with their exit code and the last part of the validation output, so failed attempts can be kept as evidence.

Where the command runs

Setting What happens
default The command runs on this machine as you (runner.kind: trusted_host). The worktree keeps your checkout tidy; it is not a security boundary.
isolation=provider An external sandbox you configure runs the command (runner.kind: external). AgentLoop provides no sandbox of its own.
untrusted=True Refuses to run unless an isolation provider is given.

An isolation provider is any object with a name, a describe() method that says what it isolates, and run(argv, *, workspace, env, timeout, cancel) returning the same fields as agentloop.process_tree.run_process_tree. A provider that raises or returns no valid outcome gives status: "error", never a pass.

Environment

The command gets a minimal environment by default (BASE_ENVIRONMENT): PATH, temporary directories, the home directory, locale and the Windows system variables. Your API keys and other credentials aren't passed on. Add what the command needs with env={"NAME": "value"}, or pass inherit_env=True for the whole environment, which was the behaviour before this change. The receipt lists the variable names (runner.environment), never their values.

What the result means

Field Meaning
status accepted, rejected, cancelled, error, or invalid when your checkout changed while the candidate ran. The values from earlier versions are unchanged.
validation_passed Only that your command exited 0
verified_improvement Always false. Execution doesn't measure quality or performance. A recommendation needs measured evidence, such as an acceptance contract.
original_checkout_unchanged, original_head_unchanged Your checkout's status and HEAD after the run
cleanup Whether the worktree and temporary directory were removed, and git's exit codes

What it won't do

  • Edit anything it can't identify exactly, or edit outside the checkout.
  • Run without allow_execution=True, or run commands through a shell.
  • Touch your working tree, commit, push or deploy. Applying an accepted diff is your decision.
  • Judge whether a change is worth keeping. Validation and replayed quality gates do that.

The existing agentloop patch-plan command is unchanged.