# Self-update

An Agent can change OpenProgram itself: its tools, its interface, even the code that does the updating. Writing is never restricted; only going live is checked.

1. **Build and trial** copy of your data on failure, nothing live changed
2. **Switch** run by the old version
3. **Verify** real turn, then 120 s on failure, goes back

This page covers an installation that runs from sealed versions, under `<state>/runtime`. For a source checkout see [Developer mode](#developer-mode) and [Upgrade](https://fzkuji.github.io/OpenProgram/docs/install/upgrade.html).

Windows is implemented but has not run on a Windows machine. A Windows trial has no OS sandbox: a temporary data directory, a separate port and a sanitized copy are its only isolation, and every Windows result says so.

## How the Agent changes itself

Ask for the change in a conversation. The Agent uses one tool, `self_update`, not available in plan mode.

| Action | Effect |
| --- | --- |
| `begin(task)` | Opens a private copy of the code at `work/<task>` as the turn's working directory |
| `prepare(task, goal, assertions)` | Hands committed work over for building, checking and switching |
| `status` | Progress and the queue. Default. |
| `retry(update_id)` | Next attempt after a failure, at most three in one chain |
| `cancel(update_id)` | Stops an update before the switch, or removes a queued one |
| `versions` | Lists the versions kept |
| `undo(target)` | [Goes back](#undo) |

`prepare` needs a clean tree with commits beyond the running code; otherwise it says what is missing. A change based on an older commit is rebased.

## After `prepare`

| Phase | What happens | On failure |
| --- | --- | --- |
| `building` | Builds a sealed version from the commit | Stops; the worktree stays |
| `trial` | Runs on a copy of your data, on its own port, with no credentials and a fake model. An independent verifier checks the assertions. | Stops; nothing live touched |
| `ready` | If the running code moved, rebases and tries again, at most three times | Stops and names the conflicting files |
| `quiescing` | Waits up to 10 minutes for a quiet moment, then checkpoints what is running. Gives up at 30 minutes. | Stops; nothing closed |
| `activating` | Stops the worker, moves the `current` pointer, starts the new version | Goes back |
| `verifying` | Probes, runs one real Agent turn, watches for 120 seconds | Goes back |

**During and after the switch**

| Case | Behavior |
| --- | --- |
| Who switches | The old version, from outside the worker. A new updater takes effect from the next switch. |
| Several requests | One update runs at a time; others queue. |
| Messages during the switch | Messages, scheduled turns and always-on wakes are saved, shown as "Switching versions; this runs as soon as that finishes.", and run right after, on whichever version came up. |
| New version fails | The conversation says why, and a read-only diagnosis starts. |
| Old version does not return | Ends in `needs_manual_recovery`; the reason is in `<state>/runtime/recovery.log`. |

## Undo

| Target | Effect |
| --- | --- |
| A version id still kept | Tries that version and switches to it. Later commits are saved. |
| One commit | Reverted alone, through the normal path. A conflict stops the undo and names the files. |
| Terminal | No trial; works even when the running version is broken: |

```
openprogram versions list
openprogram versions use <id>
```

## Versions

Each version is a sealed directory under `versions/`, and the file `current` says which runs. OpenProgram keeps the current version, the last good one, the one before it and recent ones. A version that ends abnormally twice on probation, or three times in ten minutes later, is replaced by the last good one, and the next start says so.

## Your data

| Change | Rule |
| --- | --- |
| Going back | Code only; conversations, settings and files stay. |
| Schema and config | Additive only, so older versions can read your data. The trial enforces this. |
| Destructive migration | Declared in its commit. Databases, conversations, memory and `config.json` are backed up at the switch, and the result names the backup: |

```
openprogram versions restore-data <backup>
```

**Editing a running version directly**

Not refused. At the next start the launcher saves the changed files and a patch under `unapplied/`, restores the version, and says once how many files were set aside. The Agent can take that patch through the normal path.

**Developer mode**

`<state>/runtime/developer.json`, holding `{"checkout", "python"}`, runs from a source checkout: no pointer, trial or automatic return, and `self_update` activates nothing. Commit in the checkout and restart the worker.

[Upgrade](https://fzkuji.github.io/OpenProgram/docs/install/upgrade.html)
