# Self-programming AI agents

1. The agent writes a Workflow package in `~/.openprogram/programs/<name>/` with its normal file tools.
2. `openprogram programs check` tests it, commits it and records it as checked.
3. The worker loads it without restarting; later turns call its entries from the same registry as existing tools.

Writing to the package is not refused; the check decides what runs. An edit or a plain `git commit` stays out of use until the check passes.

## What the agent writes

```
from openprogram import Agent, Context

reviewer = Agent(instructions="You find concrete defects.", tools=[])
editor = Agent(instructions="You revise drafts.", tools=[])


def review_then_revise(draft: str) -> str:
    """Review a draft, then revise it against the review."""
    ctx = Context({"draft": draft})
    review = reviewer("Identify concrete defects in this draft", context=ctx)
    return editor("Revise the draft using the review",
                  context=ctx.derive({"review": review}))


PROGRAM_ENTRIES = [review_then_revise]
```

Python fixes the order; two roles do the semantic steps, each seeing only the blocks its Context holds.

## Ask for one

```
curl -fsSL https://fzkuji.github.io/OpenProgram/install | sh
openprogram
```

```
Create a Workflow named review_then_revise. It must review a draft, revise it
against the review, return only the final text, and include a behavior test.
```

## Controls that still apply

| Control | Where |
| --- | --- |
| Tool access | Each role's `tools` and the configured approval policy |
| Record | Each role call and its exact request, in the session graph |
| Limits, cancellation, structured output validation, provider accounting | The runtime |
| Source | Ordinary files with their own Git history: inspectable, testable, revertable. Only a checked commit loads, from an extracted copy that later edits cannot reach. If it fails to import, the version before it keeps running and the Program shows `error` with a note naming both. |

## When to use it

| Task | Use |
| --- | --- |
| Repeated, needs a reusable and inspectable process, semantic steps need judgment | Self-programming |
| Entirely deterministic | A plain function |
| Already has a stable implementation | An existing tool |

[Write and check a Workflow](https://fzkuji.github.io/OpenProgram/docs/capabilities/workflows/authoring.html) · [Agentic Programming guide](https://fzkuji.github.io/OpenProgram/docs/capabilities/agentic-programming/README.html) · [`Agent` reference](https://fzkuji.github.io/OpenProgram/docs/reference/api/agent.html) · [Design philosophy](https://fzkuji.github.io/OpenProgram/docs/capabilities/agentic-programming/philosophy.html)
