Skip to content

feat: a Redis schema-version key, and the config bundle's format version carried and checked - #868

Merged
webchang merged 6 commits into
rossoctl:mainfrom
webchang:pr/i509-schema-versions
Nov 17, 2026
Merged

webchang merged 6 commits into
rossoctl:mainfrom
webchang:pr/i509-schema-versions

Conversation

@webchang

Copy link
Copy Markdown
Member

Closes #509
Closes #510

Both follow-ups come out of the change policy (docs/reference/change-policy.md, decisions 6 and 7). There are three commits: one per issue, then review fixes to the schema-version check and its docs.

What changed and why

#510: the config bundle's format version survives upload and is checked on read

  • BUNDLE_FORMAT_VERSION used to live only in lockfile.json. The store drops that file (ADR-0038), so no reader ever saw the version. buildBundle now adds a content entry, format.json ({"formatVersion":1}), in packages/config-bundle/src/format.ts. It is content like any other: the digest covers it and the store keeps it. It records the format and never the digest, so the digest still does not depend on itself (the concern in the comment on contentDigest).
  • The entry sits at the bundle root, outside every prefix a reader acts on (skills/, prompts/, context/, prompt/, memory/). A harness from before this change ignores it, so the previous release can still read bundles built by this one.
  • assertBundleFormat runs on digest-verified content in two places. In prepareBundle it guards the control plane's upload: POST /v1/config-bundles answers 400 invalid_request with the reason, the earliest point the client that built the bundle can be told. In getBundle it guards the harness's read, which is the attach path through resolvePromotedConfig / attachPromotedConfig. A newer format, or a marker that is not a positive integer, throws BundleFormatError and nothing is unpacked.
  • A bundle without format.json reads as version 1. That covers bundles already stored and bundles built by older clients.
  • One side effect: the same workflow gets a new digest the first time it is promoted with this build, because the content now includes the marker. Bundles already stored keep their digests, and sessions that name them keep working. upgrades.md says so.

#509: a schema-version key in Redis

  • packages/control-plane/src/schema-version.ts adds sh:schema-version (prefix sh:, like sh:sandbox:records) with SCHEMA_VERSION = 1. checkSchemaVersion reads the key first and writes it with SET NX only when it is absent, so concurrent first starts agree. It accepts the same version or an older one, and throws SchemaVersionError on a newer version or on a value that is not a version. A key that is absent, even with other sh: data present, means version 1: every release before this key wrote version-1 data.
  • Reading first means a Redis that refuses writes still has its stamp checked. That covers a full Redis under noeviction (OOM command not allowed), a read-only replica (READONLY), and an ACL user without SET (NOPERM).
  • guardSchemaVersion runs that check on a short-lived client of its own once Redis answers. It keeps two kinds of failure apart:
    • Unreachable (the connect fails or the connection drops): it retries every 2 s.
    • An error reply (Redis answered but refused the command, for example SET NX on an absent key in a Redis that refuses writes): the version is unknown, not newer. The guard logs this once, naming the reply, keeps the process running, and retries with a back-off that doubles up to 60 s. The check lands once the refusal clears.
    • On a newer or malformed version it logs the reason and calls exit(1). It never logs the URL, which carries the password. The control plane (main.ts), each P6 worker (packages/server/src/worker.ts) and the relay (packages/sandbox-relay/src/main.ts) arm it at start. It does not block boot: each of these already starts before Redis and waits for it (P6 on Kubernetes, slice 1: supervisor, relay, Redis, control plane and container sandboxes as K8s workloads #423), and refusing to start while Redis is merely absent would be a new failure mode. The workers check too, not only the control plane, because a deployment without the control plane (the compose trial without its profile) still has workers writing sessions.
  • No new setting, so docs/reference/configuration.md is unchanged. REDIS_URL is the only input, read as each component already reads it.
  • Docs: in change-policy.md, the "contract today" bullets now describe both markers, a short "Version markers" section says when each is bumped and what readers do, and decisions 6 and 7 are marked done. In docs/operations/upgrades.md, the version-skew and rollback notes are updated.
  • Both pages also say three things about the key:
    • An absent key, with any sh: data, means version 1.
    • The key has no TTL, so Redis needs a noeviction or volatile-* eviction policy. Under allkeys-* the key can be evicted.
    • After a FLUSHALL, or a restore that lost the key, re-stamp it with the data's version. Otherwise the next process to start stamps its own version, which after a future bump could put an old version over newer data.

Choices the policy leaves open, made here and easy to change

  1. Who writes the key: any component that runs the check (control plane, worker, relay), whichever starts first. No start order is needed.
  2. What an older stored version does: it is accepted and left as it is. The policy requires a release to read what the previous one wrote. Whether a release that bumps SCHEMA_VERSION should raise the stored value, and so lock the previous release out, depends on whether that change is still readable by the previous release. Only that change knows, so it is left to it. The decision is the older branch of checkSchemaVersion.
  3. Malformed values (0, 1.0, " 1", 2x, an empty string) are refused like a newer version, rather than overwritten.
  4. The bundle upload's error code: a newer bundle format is 400 invalid_request with a message naming the version. I did not add a new error code, so the OpenAPI document and its version are unchanged.

Tests

New and extended tests:

  • packages/config-bundle/test/format.test.ts (17 tests). buildBundle writes format.json, and it survives prepareBundle (the lockfile does not). The digest covers the marker. For a bundle without the marker, with an older version, with the current version, with a newer version, or with a malformed marker: accepted, accepted, accepted, refused, refused. prepareBundle and getBundle both refuse a newer format and read a bundle without the marker.
  • packages/control-plane/test/handlers-config-bundles.test.ts: an upload in a newer format gets invalid_request, the message names the version, and nothing is stored.
  • harness/test/config-resolver.test.ts: resolvePromotedConfig rejects a stored bundle in a newer format and unpacks nothing. It applies a bundle without the marker and one in the current format, and the marker turns up in neither the context nor the prompt fragments.
  • packages/control-plane/test/schema-version.test.ts (28 tests).
    • Basics: the key name and version are pinned. An absent key is written. The same version is accepted without any write. A newer version is refused and not overwritten. An older version is accepted and not raised. Malformed values are refused. A concurrent first start that wins the SET NX has its version classified.
    • When Redis refuses writes (OOM, READONLY, NOPERM on SET), the current version is still checked by GET alone, a newer version is still refused, and a refused write on an absent key comes through as an error reply rather than a version error.
    • guardSchemaVersion: it retries until Redis answers and closes its client, reports the outage only once, exits 1 on a newer version (also when writes are refused), and runs on against an older one. On a refused write to an absent key it logs once, does not exit, and writes the key once the refusal clears. Its back-off doubles up to the cap (fake timers). It never logs the password.
    • Wiring: each entry point (the control plane's main.ts, the server's worker.ts, the relay's main.ts) calls guardSchemaVersion inside its main-module block and names its component.

Before the fix (source reverted, new tests kept, on 0eee39b0):

  • config-bundle format.test.ts: 10 failed, 7 passed. For example: expected undefined to be '{"formatVersion":1}\n'. The built bundle had no marker.
  • control-plane: 17 failed. The upload test failed with expected { status: 201, … } to match object { code: 'invalid_request' }, because a newer-format bundle was stored. Every schema-version test failed, because there is no key or check on main.
  • I also ran the new refusal tests against the first version of the check, which sent SET NX before GET: 8 of 28 failed. The "exits on a newer version even when Redis refuses writes" test hit the 5 s timeout, because that version retried forever without ever checking.
  • harness config-resolver.test.ts: 1 failed (promise resolved "{ …(7) }" instead of rejecting). A newer-format bundle was unpacked and applied. The two acceptance tests passed, as they should.

After the fix, all of these pass. Full suites (clean environment, pnpm 9):

Package Result
@moca/config-bundle 186 passed
@moca/control-plane 1967 passed, 3 skipped (after the review fixes)
@moca/harness 914 passed, 5 skipped
@moca/server (includes the env-name guard) 585 passed (after the review fixes)
@moca/sandbox-relay 297 passed (after the review fixes)

tsc --noEmit is clean for config-bundle, control-plane, harness, server, sandbox-relay and mocactl. python3 scripts/check-doc-links.py reports 0 broken links, and Prettier passes on all changed files.

The real Redis client was also checked against a disposable Valkey 9 container. Each line below is one run of the guard:

[probe] redis schema version 1 (written; this release knows 1)
[probe] redis schema version 1 (current; this release knows 1)
[probe] redis schema version 1 (older; this release knows 2)
[probe] refusing to run: Redis holds schema version 2 (sh:schema-version), newer than the 1 this release knows; run a release that knows it (a rollback past a schema change is not supported)

That last run exited 1. I then ran it against an ACL user allowed GET but not SET:

[ro-current] redis schema version 1 (current; this release knows 1)
[ro-newer] refusing to run: Redis holds schema version 2 (sh:schema-version), newer than the 1 this release knows; …
[ro-absent] redis schema version unknown: Redis refused the check (NOPERM User ro has no permissions to run the 'setnx' command); running on and retrying with back-off

The ro-newer run exited 1, and the ro-absent run was still running 2.5 s later. Against a closed port the guard logged redis schema version not checked yet (connect ECONNREFUSED …); retrying once and the process kept running.

Out of scope

  • Lease-mode state is unchanged. This PR only puts the marker in place before such changes land.
  • The provisioner does not run the check. It reads workload records with GETs only, and its Redis is optional. Writing the key from a read-only component seemed wrong. Adding it later is a single guardSchemaVersion call.
  • Session create (configRef) still checks only that the bundle exists. The format is enforced at upload and at the harness's read, so the control plane does not need to fetch the whole bundle at create time.
  • The layout allowlist in the isolation design (§7.3) is not implemented on main. When it is, format.json must be on its list.

Every commit is signed off (DCO).

Assisted-By: Claude Code

…eck it

BUNDLE_FORMAT_VERSION was written only into lockfile.json, which the
store drops (ADR-0038), so no reader ever saw it. buildBundle now adds
a content entry, format.json ({"formatVersion":1}). It is content like
any other: the digest covers it and the store keeps it, and it records
the format, never the digest, so the digest does not depend on itself.
It sits at the bundle root, outside every prefix a reader acts on, so a
harness from before this change ignores it.

assertBundleFormat runs on verified content in prepareBundle (the
control plane's upload, answered with 400 invalid_request and the
reason) and in getBundle (the harness's read and attach), and refuses a
newer or malformed format. A bundle without the entry, built before it
existed, reads as version 1, so stored bundles and older clients keep
working.

Change policy and upgrades page updated.

Closes rossoctl#510

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
Redis had no version marker, so a release rolled back past a stored-data
change, or pointed at a Redis restored from a newer one, would misread
keys it does not know. The key exists now, before the first change that
needs one (change policy decision 6).

sh:schema-version holds SCHEMA_VERSION (1). checkSchemaVersion writes it
with SET NX when absent, accepts the same or an older version, and
refuses a newer one or a value that is not a version. An older version
is left as it is: the policy requires a release to read what the
previous one wrote, and whether a bump should raise the stamp (locking
the previous release out) is for the change that first bumps it.

guardSchemaVersion runs the check on a short-lived client of its own
once Redis answers, retrying while it does not, and exits the process
on a refusal. The control plane, each worker and the relay arm it at
start. It does not block boot, because every one of them already starts
before Redis and waits for it. It never logs the URL.

Change policy and upgrades page updated.

Closes rossoctl#509

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
…uses writes

checkSchemaVersion sent SET NX first, so a Redis that refuses writes (full
under noeviction, a read-only replica, an ACL without SET) failed the
check outright. guardSchemaVersion treated that error as an unreachable
Redis and reconnected every 2 s forever, without ever checking the
stamp. The check now GETs first and sends SET NX only when the key is
absent.

guardSchemaVersion now tells an error reply apart from a lost
connection. A refusal leaves the version unknown, not newer, so the
guard logs it once, naming the reply, keeps the process running, and
retries with a back-off that doubles up to 60 s. A newer stamp is still
refused when writes are.

Docs: an absent key, with any sh: data, means version 1. The key has no
TTL, so Redis needs a noeviction or volatile-* eviction policy, or the
key must be re-stamped after a FLUSHALL or restore. A re-promoted
workflow gets a new bundle digest once. Tests pin each entry point's
guard call.

Refs rossoctl#509

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang requested a review from pdettori as a code owner November 17, 2026 19:17
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 17, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review at bb21603

This adds the Redis schema-version key (sh:schema-version, checked by the control plane, the P6 worker and the relay) and a format.json marker in config bundles, which upload and the harness read both check. I verified:

  • The GET-then-SETNX logic and the race branch, and the strict version parsing.
  • The guard's error-reply / unreachable split and its back-off.
  • That the URL is never logged.
  • That every entry point reads the same REDIS_URL as its stores.
  • That format.json sits outside every prefix the unpack and the overlay act on.
  • That the upload maps BundleFormatError to 400 before the redis_unavailable fallback.

No defects in the code. Two non-blocking items: the harness-side refusal surfaces as a generic 500, and two lines in upgrades.md overstate what the guard does today.

Author: webchang (MEMBER, maintainer)
Areas reviewed: TypeScript (config-bundle, control-plane, server, sandbox-relay, harness tests), Docs
Agent/IDE config (.claude/.vscode): none
Commits: 3, all signed off: yes
CI status: all checks passing at submit time

  • nit: the commit subjects carry no conventional prefix, and two are longer than 72 characters. The PR title is fine.

Drafted by an AI reviewer and approved by a human before posting.

if (actual !== digest) throw new BundleDigestMismatchError(digest, actual);
// A format newer than this reader knows throws rather than unpacking a layout it would misread:
// the same "never degrade" rule as above (#510). No marker reads as version 1.
assertBundleFormat(entries);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: When the harness reads a bundle in a newer format, this throws BundleFormatError. But turnErrorStatus (packages/server/src/server.ts:518) maps only BundleNotFoundError to 410. Everything else becomes a 500 internal_error, logged as [turn] unclassified error. So "fails loudly" reaches the caller as a generic 500, and the upgrade hint is only in the server log. Consider a stable status and code (for example 409 or 422 config_bundle_unsupported), plus a server-side test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7edb826: on /v1/turn (and its /turn alias), a bundle in a newer or unreadable format now answers 422 config_bundle_unsupported, with the reason and what to upgrade in message, on the sync path and before the first SSE frame; tests cover both. 422 rather than 409, because the 409s here are states a retry can outlive, and on this route a 409 is TurnConflict, whose turnId clients attach to. The harness contract goes to 1.5.0, shipped with control plane 1.28.0.

Comment thread docs/operations/upgrades.md Outdated
- **No schema version.** There is no schema-version key or migration tool for Redis. New fields are handled by
tolerant reads. The only versioned artefact at rest is the credential envelope (`v1.`).
- **Redis schema version.** Redis holds `sh:schema-version`, written by the first control plane, worker or relay to
start. A process that finds a version newer than it knows exits with the reason in its log, so a release rolled back

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: "a release rolled back past a schema change fails at start" overstates two things.

(a) The guard runs in the background after the listener is up. The process serves until Redis answers and then exits, and it keeps running indefinitely if the write on an absent key is refused.

(b) checkSchemaVersion leaves an older stamp as it is, so on an existing deployment a future bump does not raise the stamp. A rollback is then refused only if that bump PR chooses to raise it.

change-policy.md words this correctly ("not supported"). Suggest matching it here and saying "exits once Redis answers" rather than "at start".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 04de0fd: it now says the check runs in the background once the process is serving and exits once Redis answers, and that a rollback past a schema change is not supported, refused only if the newer release raised the stored version, matching change-policy.md.

Comment thread docs/operations/upgrades.md Outdated
and plain `kubectl rollout undo` also works. Redis data is not versioned, so a rollback across a data-format change
is untested.
and plain `kubectl rollout undo` also works. Redis data carries a schema version (above): a rollback
between releases that share it needs no step, and one past a schema change is refused at start. Rollback is otherwise

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Same point as above: "one past a schema change is refused at start" holds only if the bumping release raises the stored version, and this PR leaves that open. Suggest "is not supported (and is refused once a release raises the stamp)".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 04de0fd: it now reads "is not supported (and is refused once a release raises the stamp)".

A session whose promoted bundle is stored and intact but in a format
this harness does not read made getBundle throw BundleFormatError, which
turnErrorStatus did not classify: the caller got 500 internal_error and
the upgrade hint was only in the server log.

turnErrorStatus now maps it to 422 config_bundle_unsupported, with the
error's own sentence (versions found and known, what to upgrade) as the
body's message, on the sync path and before the first SSE frame. 422,
not 409: the 409s here are state a retry can outlive (turn_in_progress,
template_not_active), and on /v1/turn a 409 is TurnConflict, whose
turnId a client attaches to; 422 is what policy_refused and
workload_refused use for a refusal no retry fixes.

harness-openapi.yaml goes to 1.5.0 with the new 422, shipped with
control plane 1.29.0 (main is at 1.27.0 and 1.28.0 is claimed by an
open PR), whose section also records the upload's existing 400
invalid_request for a newer format.

Refs rossoctl#509

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
The guard runs in the background once a process serves: it retries
until Redis answers and then exits on a newer version, and it runs on if
Redis refuses the write on an absent key. checkSchemaVersion leaves an
older stamp as it is, so a rollback is refused only if the bumping
release raises the stamp. Say so, matching change-policy.md, instead of
"fails at start" and "is refused at start".

Refs rossoctl#509

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
Main took 1.27.0 (rossoctl#864, the base URL's page), so this PR's contract
version is 1.28.0, the next free one (it was 1.29.0 while rossoctl#874 held
1.28.0; whichever merges second renumbers). Its section goes above
1.27.0, and the change-policy list keeps both entries. rossoctl#863 made /turn a
documented alias of POST /v1/turn, response by response, so the alias
gets the new 422 too (the alias contract test caught it).

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang merged commit 5b768e8 into rossoctl:main Nov 17, 2026
17 checks passed
webchang added a commit to webchang/moca that referenced this pull request Nov 17, 2026
rossoctl#868 took 1.28.0 (the config bundle's format version, harness 1.5.0),
so this PR's per-source bound on login starts is 1.29.0: its CHANGELOG
section goes above 1.28.0, openapi.yaml and change-policy say 1.29.0,
and the change-policy list keeps both entries in order.

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
webchang added a commit to webchang/moca that referenced this pull request Nov 18, 2026
Brings 25 upstream merges since 1aa8dd6, among them the sbx OIDC
identity provider (rossoctl#851), the Redis schema-version key and config
bundle format marker (rossoctl#868), the provisioner Redis ACL user (rossoctl#869),
the relay keepalive env (rossoctl#866), the per-source OIDC pending bound with
the forwarded-headers Route pin (rossoctl#874), the egress refusal hints and
control-plane index (rossoctl#864), the empty-workspace sweep (rossoctl#872) and the
orphan-sweep log split (rossoctl#849). No conflicts.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
@pdettori pdettori added this to the v0.11.0 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

3 participants