# enboot

Replace a running Unix program's executable image with `execve` without closing open file descriptors or interrupting child processes.

```sh
go get neuroplast.io/engram/enboot
```

`neuroplast.io/engram/enboot` is a standalone Go module with zero external dependencies.

## How it works

When a program starts, `enboot.Wrap()` turns the first process into a minimal resident parent (`enboot`) and spawns a second copy of the same executable as the active process (the tenant) connected by one Unix socket pair. Because both halves are compiled into one executable, there is no separate supervisor binary to install or version-match.

As the tenant opens PTY masters, listening sockets, or child socket pairs, it deposits duplicates with the resident. To update, the tenant writes the new binary beside the old one, stops reading from shared descriptors, and calls `Exec` (`execve`). The PID and child processes remain unchanged, and the new image retrieves duplicates of its descriptors from the resident. If the new image exits before calling `Ready`, the resident restarts the last binary that called `Ready` with all descriptors still open.

```go
import "neuroplast.io/engram/enboot"

func main() {
	b := enboot.Wrap() // the first process becomes the resident and never returns
	b.Hello()

	b.Put("pty-3", fd) // deposit each descriptor when created, not at update time
	b.Ready(self)      // mark this path as the rollback target

	// Later, after writing the new binary beside the old one:
	b.Exec(newPath, os.Args)
}
```

<!-- docs:figure text=../docs/enboot-lifecycle.ans text-narrow=../docs/enboot-lifecycle-tall.ans svg-narrow=../docs/enboot-lifecycle-tall.svg -->
![enboot update sequence: deposit descriptors with resident, replace build 1 with build 2 via execve, retrieve duplicates, and either promote on ready or roll back on early exit.](https://engram.neuroplast.io/docs/_files/docs/enboot-lifecycle.svg)

For programs not written in Go, `cmd/enboot` provides the resident half as a standalone executable (`enboot <program> [args...]`). The tenant half implements the eight-verb lockstep protocol specified in [enboot protocol](https://engram.neuroplast.io/docs/enboot-protocol).

## Minimal resident design

A program can only update itself freely if the resident underneath it rarely needs restarting. The resident therefore performs no downloads, no signature verification, and no version selection: it receives paths from the tenant and keeps open descriptor duplicates without reading or writing their bytes. It runs the original code it started with across any number of tenant updates.

Each engram manifest records `min_enboot`, the lowest protocol version that build requires. `Hello()` returns the protocol version supported by the running resident; only an update that raises `min_enboot` above that number requires restarting the whole process tree.

## Tenant requirements

| Requirement | Reason |
| --- | --- |
| Call `Put` when each descriptor is created | Ensures crash recovery has the same descriptor set as planned updates |
| Call `Hello` first, `Ready` only after startup succeeds | `Hello` clears the ready flag; `Ready` sets the rollback target path |
| Keep the previous binary on disk during update | Rollback restarts the last path passed to `Ready`; overwriting it removes the fallback |
| Stop reading from deposited descriptors before `Exec` | Kernel buffers persist across `execve`; concurrent readers in the old image can consume bytes meant for the new image |

Child processes never inherit the control socket: `Dial()` sets close-on-exec on the socket and unsets `ENBOOT_FD` and `ENBOOT_ROLLBACK` from the environment. `Exec()` temporarily restores inheritance and `ENBOOT_FD` for the single `execve` transition. Check `b.RolledBack()` on startup to detect whether the resident restarted the previous build after a failed update.

## Platform notes

- macOS: Write new binaries to a temporary path beside the old binary and rename into place; overwriting a code-signed binary in place causes the kernel to kill the next launch.
- macOS: An exiting child process with unread PTY output blocks until the output drains as long as any copy of its PTY master remains open, including the resident's copy. The resident exits whenever the tenant exits normally so it never outlives its tenant.
- Linux: The resident registers as a child sub-reaper (`PR_SET_CHILD_SUBREAPER`), reaping processes orphaned by a tenant exit and returning their wait statuses through `Reaped()`.

## Measured overhead

Benchmarks on Linux and macOS show no measurable throughput cost for keeping duplicate descriptors open in the resident (proxying bytes through a supervisor process reduces throughput by roughly 20%). No bytes are lost across `execve` once the old image stops reading, connections arriving during the swap wait in the kernel listen backlog until the new image accepts them, and rollback completes in milliseconds with child processes still running.
