# Channel specification (version 1)

This document is the normative contract for engram release channels. The Go package `neuroplast.io/engram` is the reference implementation; wherever the package disagrees with this specification, the package has a bug.

## Channel layout

An engram channel consists of three file types and one mirror directory under `<base>/<project>/<channel>/`:

```text
head                     newest live build (unsigned, polled, max-age=60)
journal                  append-only event log since channel creation (unsigned, max-age=60)
builds/<commit>/
  manifest               immutable build description (signed, immutable cache)
  manifest.sig           OpenSSH Ed25519 signature over manifest
  <artifact files>       release binaries or archives (immutable cache)
latest/                  copy of builds/<commit>/ for the newest live build (max-age=60)
```

## Grammar

All three text files (`journal`, `head`, `manifest`) use one grammar: a strict subset of logfmt readable by any logfmt parser or by splitting each line on ASCII spaces and each field on its first `=`.

- Encoding is UTF-8 with LF (`\n`) line endings. Each line is at most 4095 bytes (4096 bytes including LF).
- Blank lines and lines starting with `#` are ignored.
- Line 1 identifies the file: `engram version=1 kind=<kind>`, where `<kind>` is `journal`, `head`, or `manifest`. Readers must reject an unknown `version` or a `kind` other than the one requested. Incompatible format versions use new filenames while continuing to serve v1 files.
- Every subsequent non-comment line is a record: one record-type token followed by `key=value` fields separated by one or more spaces, in any order.
- Field values are bare tokens running to the next space and may contain `=`. Spaces, quotes (`"`), tabs, carriage returns, and control characters are forbidden; there is no escaping. Keys and record types may not contain `=`.
- Unknown keys and unknown record types must be ignored by readers and preserved unchanged by any tool that rewrites a file.
- A known record missing a required key makes the file invalid.
- Commits are full 40-character hexadecimal hashes. Timestamps are RFC 3339 in UTC.
- Nothing may be added to this grammar that requires more than splitting on spaces and `=`, so minimal long-lived binaries can parse it without third-party libraries.

## `journal`

```text
engram version=1 kind=journal
channel project=acme name=dev
publish seq=1 commit=1f3e9aa0... version=26.09.20-dev.1f3e9aa time=2026-09-20T18:02:11Z min_enboot=1
publish seq=2 commit=77aa01c3... version=26.09.21-dev.77aa01c time=2026-09-21T08:31:40Z min_enboot=1
scrap seq=3 commit=77aa01c3... time=2026-09-21T08:50:12Z reason=broken
publish seq=4 commit=a7072af3... version=26.09.21-dev.a7072af time=2026-09-21T09:10:55Z min_enboot=1
expire seq=5 commit=1f3e9aa0... time=2026-10-21T09:10:55Z
```

Events are only appended to the end of `journal`. `seq` starts at 1 and increments by exactly 1 per event; any gap makes the journal invalid.

| Record | Key | Meaning |
| --- | --- | --- |
| `channel` | `project`, `name` | Project and channel claimed by the file |
| `publish` | `seq` | Monotonic journal sequence number; signed identically into `manifest` |
| `publish` | `commit` | Full 40-character commit SHA identifying the build |
| `publish` | `version` | Display version string; never compared or sorted by clients |
| `publish` | `time` | Commit timestamp (RFC 3339 UTC) |
| `publish` | `min_enboot` | Lowest `enboot` protocol version compatible with the build |
| `scrap` | `seq`, `commit`, `time` | Build withdrawn; its files are deleted and running hosts should update |
| `scrap` | `reason` | Optional withdrawal reason: `broken`, `security`, or `mistake` (unknown values read as absent) |
| `expire` | `seq`, `commit`, `time` | Build removed after retention elapsed; its files are deleted |

A commit is live from its `publish` record until a `scrap` or `expire` record names it. A commit is published at most once; a scrapped or expired commit can never be re-published on the same channel. The newest live build is the live commit with the highest `publish` `seq`.

<!-- docs:figure text=docs/build-lifecycle.ans text-narrow=docs/build-lifecycle-tall.ans svg-narrow=docs/build-lifecycle-tall.svg -->
![Build lifecycle: publish enters live (published at most once; newest never expires), and scrap or expire transition terminally with files deleted and purged.](https://engram.neuroplast.io/docs/_files/docs/build-lifecycle.svg)

## `head`

```text
engram version=1 kind=head
channel project=acme name=dev seq=5 updated=2026-10-21T09:10:55Z
publish seq=4 commit=a7072af3... version=26.09.21-dev.a7072af time=2026-09-21T09:10:55Z min_enboot=1
```

In `head`, the `channel` record adds `seq` (the journal's latest sequence when `head` was written) and `updated` (UTC RFC 3339 timestamp). The `publish` record copies the newest live build's `publish` line verbatim from `journal`. A channel with no live builds omits the `publish` record.

## `latest/`

`latest/` mirrors every file of the newest live build (`manifest`, `manifest.sig`, and all artifacts) under stable URLs (`<base>/<project>/<channel>/latest/<artifact>`) with a 60-second cache TTL, purged whenever the newest live build changes.

Like `head`, `latest/` is untrusted convenience metadata. Because artifacts are copied before `manifest`, a reader verifying hashes against `latest/` during a publish may observe a newer artifact alongside the previous manifest and must retry. Automated clients should resolve `head` and fetch immutable `builds/<commit>/` paths instead. A channel with no live build removes `latest/`.

## `manifest`

```text
engram version=1 kind=manifest
build project=acme channel=dev seq=4 commit=a7072af3... version=26.09.21-dev.a7072af time=2026-09-21T09:10:55Z min_enboot=1
artifact name=acme os=linux arch=amd64 size=18300412 sha256=... path=acme_linux_amd64.tar.gz
artifact name=acme os=darwin arch=arm64 size=17911038 sha256=... path=acme_darwin_arm64.tar.gz
```

The `build` record includes all `publish` keys plus `project` and `channel`. Each `artifact` record requires `name`, `os`, `arch`, `size` (non-negative integer bytes), `sha256` (64 lowercase hex characters), and `path`. `path` must be a clean relative path inside `builds/<commit>/` with no leading `/` or `..` segments. Additional hash algorithm keys may be added alongside `sha256` in future revisions.

## Trust model

Only `manifest` is signed. `head`, `journal`, and `latest/` are unsigned pointers whose claims remain advisory until verified through a signed manifest.

`manifest.sig` is an OpenSSH signature (`PROTOCOL.sshsig`, created with `ssh-keygen -Y sign` or equivalent) over the exact bytes of `manifest`, using hash algorithm `sha512`, namespace `engram`, and an Ed25519 key.

Before accepting `builds/<commit>/manifest`, a client must:

1. Verify `manifest.sig` against Ed25519 public keys pinned inside the client (ignoring the public key embedded inside the signature as anything other than a lookup hint).
2. Confirm that `project`, `channel`, and `commit` in the signed `build` record equal the requested project, channel, and commit.
3. For automatic or forward updates, require the signed `seq` to be strictly greater than the highest `seq` previously accepted on this channel (only an explicit user-requested rollback may bypass this check).
4. Verify every downloaded artifact's exact byte `size` and `sha256` against the signed `manifest` before executing or unpacking it.

| Threat | Protected by engram | Notes |
| --- | --- | --- |
| Tampered or unsigned build | Yes | Rejected by pinned Ed25519 signature and SHA-256 checks |
| Cross-project or cross-channel substitution | Yes | `project`, `channel`, and `commit` are signed inside `manifest` |
| Replay / downgrade to an older signed build | Yes | Monotonic `seq` signed inside `manifest` (`ErrRollback`) |
| Freeze attack (withholding newer releases) | No (v1) | Storage attacker can freeze `head` or omit newer builds |
| Hiding or forging a `scrap` event | No | `journal` is unsigned; `scrap`/`expire` are advisory signals only |

Key rotation uses a list of pinned keys: ship a release that trusts both the old and new public keys before switching the publisher to the new key and retiring the old key.

## Automated client rules

An unattended updater such as `engram follow` must additionally:

- Compare only the signed `manifest` `seq` against its highest accepted `seq` (never the unsigned `head` `seq`).
- Record builds that failed startup health checks or were manually rolled back, and refuse to reinstall them automatically.
- Treat unsigned `scrap` and `expire` events as advisory without downgrading or uninstalling a running build.
- Support conditional polling of `head` with `If-None-Match`.

## Publish ordering, retention, and caching

Publishers must execute writes in this order so no pointer references an incomplete build:

1. Read `journal` and its version tag (`ETag`); assign the next sequential `seq`.
2. Upload all artifacts under `builds/<commit>/`, followed by `manifest` and `manifest.sig` (`Cache-Control: public, max-age=31536000, immutable`).
3. Append the `publish` event and an `expire` event for every superseded build past retention.
4. Write `journal` conditionally (`If-Match: <etag>`, or `If-None-Match: *` for a new channel). On HTTP `412`, restart from step 1.
5. Write `head` and mirror the newest live build into `latest/` (`Cache-Control: public, max-age=60`).
6. Delete expired build directories from storage and purge their prefixes from any CDN cache in front of storage.

Retention starts when a build is superseded by a newer live build, not when it was published: a live build expires once its successor's `time` is older than the retention duration. The newest live build never expires.

Because `builds/<commit>/` objects are served as immutable, removing a build (via `scrap` or `expire`) requires purging `builds/<commit>/` from any CDN cache after deleting the backing objects.

## Private channels

A private channel restricts HTTP reads using a bearer token enforced by the serving layer without changing any file format.

- Clients send `Authorization: Bearer <token>` on every channel request and never place tokens in URLs (where they leak into logs, errors, and shell history). Servers may optionally accept the same token as the password in HTTP Basic authentication for browsers or `curl -u`.
- Servers must return HTTP `401` when a valid token is absent or rejected. Do not return `403` or `404` for authentication failures: clients interpret `403` and `404` as `ErrNotLive` because private S3 buckets behind CDNs return `403` for missing keys.
- Access control must apply to the entire channel (`head`, `journal`, `builds/`, and `latest/`).
- Any CDN or reverse proxy must validate the token on every incoming request before cache lookup (for example in a viewer-request function). Validating only at the origin on cache misses allows one authenticated reader to populate the cache for unauthenticated callers.
- A bearer token governs read access only and never replaces pinned-key signature verification.

## Deferred beyond version 1

- Signed freshness expiry (`expires=`) to bound freeze attacks without forcing unsigned `head` to be trusted.
- Journal segmentation (`journal.NNNN` archives referenced by `prev=` and `prev_sha256=` keys on the active `journal`) to bound unbounded journal growth while remaining backward-compatible with v1 readers that ignore unknown keys.
