Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Console: managed servers turn agent management on; canary gets its own switch-core pin (#764)

Louis Amaudruza12767dc2677
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9f6d65992..370a3a54b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1428,6 +1428,17 @@ version of their own to them without also giving them a release of their own.
### [Unreleased]
#### Added
+- **Managed servers turn agent management on.** A server Console runs, on
+ this computer or on an SSH host, now starts with agent management enabled
+ and the controller token secret it needs, generated once and shared by every
+ Console using that server. On a switch-core release that offers agent
+ management, Console then moves your agents onto managed machines by itself.
+ Releases from before agent management ignore both settings.
+- **Canary has its own switch-core pin.** The canary build bundles and runs
+ switch-core 0.30.0-rc.11 for the servers it manages, while stable stays on
+ 0.29.0. A canary pin can never be older than the stable one. Canary and
+ stable share their managed servers on one computer, so once canary has
+ updated one, stable will not start it again until stable's pin catches up.
- **Switch cloud on the agents controller.** On a server whose cloud machines
run the agents controller, choosing Switch cloud in the New agent form starts
your cloud machine and waits for it to come online (its first start takes a
diff --git a/artifacts.yaml b/artifacts.yaml
index c133f8d03..2a2fe580c 100644
--- a/artifacts.yaml
+++ b/artifacts.yaml
@@ -79,6 +79,13 @@ artifacts:
# in lockstep with the bundled compose.
pins:
switch-core: 0.29.0
+ # What the canary build bundles instead, so a switch-core pre-release can be
+ # tried on canary without moving stable. Never older than the stable pin
+ # (`just artifacts` refuses it). When a stable switch-core release ships,
+ # move `pins` to it; drop the canary pin back to it too unless a newer
+ # pre-release is being tried.
+ canary_pins:
+ switch-core: 0.30.0-rc.11
agent-runtime:
description: >-
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.test.ts
index 07bf2b988..c71847462 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.test.ts
@@ -11,6 +11,7 @@ const secrets: LocalServerSecrets = {
agentRegistrationToken: 'agent-token',
jwtSecretKey: 'jwt-key',
secretKeys: 'console:secret-keys-value',
+ controllerTokenSecret: 'controller-token-secret-value',
gatewayAdminPassword: 'gw-admin',
mattermostAdminPassword: 'mm-admin',
mattermostUserPassword: 'mm-user',
@@ -68,11 +69,16 @@ describe('buildEnvFile', () => {
expect(vars.AGENT_REGISTRATION_TOKEN).toBe('agent-token');
expect(vars.JWT_SECRET_KEY).toBe('jwt-key');
expect(vars.SECRET_KEYS).toBe('console:secret-keys-value');
+ expect(vars.CONTROLLER_TOKEN_SECRET).toBe('controller-token-secret-value');
expect(vars.GATEWAY_ADMIN_PASSWORD).toBe('gw-admin');
expect(vars.MATTERMOST_ADMIN_PASSWORD).toBe('mm-admin');
expect(vars.MATTERMOST_USER_PASSWORD).toBe('mm-user');
});
+ it('turns agent management on', () => {
+ expect(vars.FEATURE_FLAGS_ENABLED).toBe('agent_management');
+ });
+
it('runs switch-core as the restricted runtime role, not the schema owner', () => {
// The runtime role name is fixed (switch_app), not derived from the
// secrets bundle: init-db creates exactly this role, so drifting the name
@@ -127,16 +133,9 @@ describe('buildEnvFile', () => {
// loopback stack cannot offer (see above).
'TELEGRAM_APP_BOT_TOKEN',
'TELEGRAM_APP_WEBHOOK_SECRET',
- // Agent management is off unless a server opts in, and the compose file
- // defaults it off. A managed stack does not opt in yet: the pinned
- // switch-core release predates it, and turning it on also needs a
- // controller token secret generated for the stack.
+ // The variable the agent_management feature flag replaced. switch-core
+ // refuses to start with it on, so it is left to the compose default.
'AGENT_MANAGEMENT_ENABLED',
- 'CONTROLLER_TOKEN_SECRET',
- // Every feature flag is off unless a deployment lists it, and a managed
- // stack lists none: agent management and cloud agents, the flags a
- // server would turn on, are not offered from a loopback stack yet.
- 'FEATURE_FLAGS_ENABLED',
// Private hosts Switch may reach at a tenant- or agent-supplied URL. The
// compose file always allows the bundled Mattermost; a managed stack
// allows nothing more until its operator says so.
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.ts
index 7da8ed79d..c7905471b 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.ts
@@ -33,6 +33,9 @@ export type LocalServerEnvParams = {
telemetryInternal: boolean;
};
+/** The feature flags every managed stack is started with. */
+export const MANAGED_SERVER_FEATURE_FLAGS = ['agent_management'] as const;
+
/** The variable the server reads its telemetry gate from. Named here because
* reading the choice back off a host has to look for the same key. */
export const TELEMETRY_ENABLED_KEY = 'TELEMETRY_ENABLED';
@@ -85,6 +88,11 @@ export function buildEnvFile(params: LocalServerEnvParams): string {
`AGENT_REGISTRATION_TOKEN=${secrets.agentRegistrationToken}`,
`JWT_SECRET_KEY=${secrets.jwtSecretKey}`,
`SECRET_KEYS=${secrets.secretKeys}`,
+ // The flags a managed stack runs with. Agent management is the one it
+ // turns on: Console moves this machine's agents onto controllers once the
+ // server offers it, and a release that does not know the variable ignores it.
+ `FEATURE_FLAGS_ENABLED=${MANAGED_SERVER_FEATURE_FLAGS.join(',')}`,
+ `CONTROLLER_TOKEN_SECRET=${secrets.controllerTokenSecret}`,
`GATEWAY_ADMIN_EMAIL=${LOCAL_SERVER_ADMIN_EMAIL}`,
`GATEWAY_ADMIN_PASSWORD=${secrets.gatewayAdminPassword}`,
`FRONTEND_BASE_URL=${gatewayUrlFor(ports)}`,
@@ -139,15 +147,20 @@ export function readEnvValue(env: string, key: string): string | null {
/**
* What another Console needs to run the same stack. `dbRuntimePassword` is
- * null for a `.env` from before switch-core split its database roles, and
- * `secretKeys` for one from before `SECRET_KEYS`; the caller fills them in
+ * null for a `.env` from before switch-core split its database roles,
+ * `secretKeys` for one from before `SECRET_KEYS`, and `controllerTokenSecret`
+ * for one from before agent management; the caller fills them in
* (see `withNewerSecrets` in `secret-values.ts`).
*/
export type StackEnv = {
ports: LocalServerPorts;
- secrets: Omit<LocalServerSecrets, 'dbRuntimePassword' | 'secretKeys'> & {
+ secrets: Omit<
+ LocalServerSecrets,
+ 'dbRuntimePassword' | 'secretKeys' | 'controllerTokenSecret'
+ > & {
dbRuntimePassword: string | null;
secretKeys: string | null;
+ controllerTokenSecret: string | null;
};
version: string | null;
};
@@ -226,6 +239,7 @@ export function keysDisagreeing(
for (const [field, key] of PORT_KEYS) expected.set(key, String(copy.ports[field]));
for (const [field, key] of SECRET_KEYS) expected.set(key, copy.secrets[field]);
expected.set('SECRET_KEYS', copy.secrets.secretKeys);
+ expected.set('CONTROLLER_TOKEN_SECRET', copy.secrets.controllerTokenSecret);
// Which password `DB_PASSWORD` is depends on the layout that wrote the file.
if (readEnvValue(env, 'DB_OWNER_PASSWORD') !== null) {
expected.set('DB_OWNER_PASSWORD', copy.secrets.dbPassword);
@@ -276,6 +290,7 @@ export function readStackEnv(env: string): StackEnvReading {
...(plain as Record<PlainSecret, string>),
...database,
secretKeys: readEnvValue(env, 'SECRET_KEYS'),
+ controllerTokenSecret: readEnvValue(env, 'CONTROLLER_TOKEN_SECRET'),
},
version: readEnvValue(env, 'SWITCH_VERSION'),
},
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline-shared-host.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline-shared-host.test.ts
index c9fa83b98..b692b0366 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline-shared-host.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline-shared-host.test.ts
@@ -115,6 +115,7 @@ const hostSecrets: LocalServerSecrets = {
agentRegistrationToken: 'host-agent-token',
jwtSecretKey: 'host-jwt',
secretKeys: 'console:host-secret-keys',
+ controllerTokenSecret: 'controller-token-secret-value',
gatewayAdminPassword: 'host-admin-pw',
mattermostAdminPassword: 'host-mm-admin',
mattermostUserPassword: 'host-mm-user',
@@ -400,6 +401,31 @@ describe('starting a shared stack', () => {
expect(secrets.secretKeys).toMatch(/^console:[0-9a-f]{64}$/);
});
+ it('keeps the host’s controller token secret so every Console signs with one', async () => {
+ inspectStackMock.mockResolvedValue(
+ present({}, { secrets: { ...hostSecrets, controllerTokenSecret: 'host-controller-secret' } })
+ );
+ readSecretsMock.mockResolvedValue({ ...hostSecrets, controllerTokenSecret: 'desktop-secret' });
+ const { host } = sharedHost();
+
+ await startStack(startOptions(host));
+
+ const [{ secrets }] = buildEnvFileMock.mock.calls[0] as [{ secrets: LocalServerSecrets }];
+ expect(secrets.controllerTokenSecret).toBe('host-controller-secret');
+ });
+
+ it('makes a controller token secret for a stack whose .env predates agent management', async () => {
+ inspectStackMock.mockResolvedValue(
+ present({}, { secrets: { ...hostSecrets, controllerTokenSecret: null } })
+ );
+ const { host } = sharedHost();
+
+ await startStack(startOptions(host));
+
+ const [{ secrets }] = buildEnvFileMock.mock.calls[0] as [{ secrets: LocalServerSecrets }];
+ expect(secrets.controllerTokenSecret).toMatch(/^[0-9a-f]{64}$/);
+ });
+
it('makes new credentials only on a host with nothing of the stack', async () => {
inspectStackMock.mockResolvedValue({ kind: 'absent' });
const { host } = sharedHost();
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts
index 8166c9928..f3d8b1972 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts
@@ -306,11 +306,15 @@ async function adoptSettings(
// A `.env` from before the database role split has no runtime password; one
// is made here, and the next start gives the role it. One from before
// SECRET_KEYS takes this desktop's, if it has one — a server may already
- // have stored credentials under it — and a fresh one otherwise.
+ // have stored credentials under it — and a fresh one otherwise. One from
+ // before agent management gets a controller token secret the same way.
+ const desktop = await readSecrets(host);
const { secrets } = withNewerSecrets({
...stack.env.secrets,
dbRuntimePassword: stack.env.secrets.dbRuntimePassword ?? '',
- secretKeys: stack.env.secrets.secretKeys ?? (await readSecrets(host))?.secretKeys ?? '',
+ secretKeys: stack.env.secrets.secretKeys ?? desktop?.secretKeys ?? '',
+ controllerTokenSecret:
+ stack.env.secrets.controllerTokenSecret ?? desktop?.controllerTokenSecret ?? '',
});
await storeSecrets(host, secrets);
await rememberPorts(host, stack.env.ports);
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/remote-server-service.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/remote-server-service.test.ts
index 4b85a394c..fca702953 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/remote-server-service.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/remote-server-service.test.ts
@@ -155,6 +155,7 @@ const secrets = {
agentRegistrationToken: 'agent-token',
jwtSecretKey: 'jwt',
secretKeys: 'console:secret-keys',
+ controllerTokenSecret: 'controller-token-secret-value',
gatewayAdminPassword: 'admin-pw',
mattermostAdminPassword: 'mm-admin',
mattermostUserPassword: 'mm-user',
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/secret-values.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/secret-values.ts
index 6b466112d..5429f813f 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/secret-values.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/secret-values.ts
@@ -14,13 +14,18 @@ import { randomBytes } from 'node:crypto';
*
* `secretKeys` is switch-core's `SECRET_KEYS`: what it signs sessions and
* encrypts stored credentials with. `jwtSecretKey` is the key that did both
- * before it, still passed so the server can read what it wrote back then. */
+ * before it, still passed so the server can read what it wrote back then.
+ *
+ * `controllerTokenSecret` is switch-core's `CONTROLLER_TOKEN_SECRET`: what it
+ * signs agents controllers' hour-long access tokens with. Agent management
+ * refuses to start without one. */
export type LocalServerSecrets = {
dbPassword: string;
dbRuntimePassword: string;
agentRegistrationToken: string;
jwtSecretKey: string;
secretKeys: string;
+ controllerTokenSecret: string;
gatewayAdminPassword: string;
mattermostAdminPassword: string;
mattermostUserPassword: string;
@@ -39,6 +44,7 @@ export function generateSecrets(): LocalServerSecrets {
agentRegistrationToken: token(),
jwtSecretKey: token(32),
secretKeys: `console:${randomBytes(32).toString('hex')}`,
+ controllerTokenSecret: randomBytes(32).toString('hex'),
gatewayAdminPassword: token(),
mattermostAdminPassword: token(),
mattermostUserPassword: token(),
@@ -56,6 +62,9 @@ export function generateSecrets(): LocalServerSecrets {
* still reads what it stored through `jwtSecretKey`, and moves it onto the
* new key on its first start. Once a server has started with one it must
* never be replaced, which is why only an empty value is filled.
+ * - `controllerTokenSecret`, from before agent management: it signs only
+ * access tokens that expire within the hour and that controllers mint again
+ * from their own credential, so a new one costs nothing.
*/
export function withNewerSecrets(secrets: LocalServerSecrets): {
secrets: LocalServerSecrets;
@@ -64,6 +73,8 @@ export function withNewerSecrets(secrets: LocalServerSecrets): {
const missing: Partial<LocalServerSecrets> = {};
if (!secrets.dbRuntimePassword) missing.dbRuntimePassword = generateSecrets().dbRuntimePassword;
if (!secrets.secretKeys) missing.secretKeys = generateSecrets().secretKeys;
+ if (!secrets.controllerTokenSecret)
+ missing.controllerTokenSecret = generateSecrets().controllerTokenSecret;
if (Object.keys(missing).length === 0) return { secrets, migrated: false };
return { secrets: { ...secrets, ...missing }, migrated: true };
}
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/secrets.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/secrets.test.ts
index 8a4f851ec..04a95f081 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/secrets.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/secrets.test.ts
@@ -88,6 +88,18 @@ describe('loadOrCreateSecrets', () => {
expect(setSecret).toHaveBeenCalledWith('host-a', JSON.stringify(secrets));
});
+ it('backfills a controller token secret on an install predating agent management', async () => {
+ const legacy = generateSecrets() as Partial<ReturnType<typeof generateSecrets>>;
+ delete legacy.controllerTokenSecret;
+ getSecret.mockResolvedValue(JSON.stringify(legacy));
+
+ const secrets = await loadOrCreateSecrets({ secretsKey: 'host-a' });
+
+ expect(secrets).toMatchObject(legacy);
+ expect(secrets.controllerTokenSecret).toMatch(/^[0-9a-f]{64}$/);
+ expect(setSecret).toHaveBeenCalledWith('host-a', JSON.stringify(secrets));
+ });
+
it('never replaces a key ring the stored secrets were encrypted with', async () => {
const current = generateSecrets();
getSecret.mockResolvedValue(JSON.stringify(current));
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/stack-state.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/stack-state.test.ts
index 0c1c69b83..c2e76bb91 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/stack-state.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/stack-state.test.ts
@@ -37,6 +37,7 @@ const secrets: LocalServerSecrets = {
agentRegistrationToken: 'agent-token',
jwtSecretKey: 'jwt-key',
secretKeys: 'console:secret-keys-value',
+ controllerTokenSecret: 'controller-token-secret-value',
gatewayAdminPassword: 'gw-admin',
mattermostAdminPassword: 'mm-admin',
mattermostUserPassword: 'mm-user',
diff --git a/console/apps/switch-console-desktop/src/shared/app-identity.canary.ts b/console/apps/switch-console-desktop/src/shared/app-identity.canary.ts
index 1d959f8c8..f59892675 100644
--- a/console/apps/switch-console-desktop/src/shared/app-identity.canary.ts
+++ b/console/apps/switch-console-desktop/src/shared/app-identity.canary.ts
@@ -13,8 +13,8 @@ export const ARTIFACT_PREFIX = 'switch-console-canary';
export const RELEASE_REPO_OWNER = 'sandbox-quantum';
export const RELEASE_REPO_NAME = 'switch';
-// Mirrors COMPATIBLE_SWITCH_VERSION in ./app-identity.ts. Both are checked
-// against `switch-console.pins.switch-core` in artifacts.yaml by `just artifacts`,
+// Mirrors CANARY_SWITCH_VERSION in ./app-identity.ts. Both are checked
+// against `switch-console.canary_pins.switch-core` in artifacts.yaml by `just artifacts`,
// so they can no longer drift apart — this used to say "keep in sync" and rely
// on whoever edited one remembering the other (CHOO-1865).
-export const COMPATIBLE_SWITCH_VERSION = '0.29.0';
+export const COMPATIBLE_SWITCH_VERSION = '0.30.0-rc.11';
diff --git a/console/apps/switch-console-desktop/src/shared/app-identity.ts b/console/apps/switch-console-desktop/src/shared/app-identity.ts
index 642062734..76e4d6230 100644
--- a/console/apps/switch-console-desktop/src/shared/app-identity.ts
+++ b/console/apps/switch-console-desktop/src/shared/app-identity.ts
@@ -63,14 +63,20 @@ export const RELEASE_REPO_NAME = 'switch';
// against it by `just artifacts` (CHOO-1865). A literal rather than an import
// because electron-builder loads this module under bare Node, where
// extensionless ESM specifiers do not resolve — so it cannot import anything.
-// The check is what makes the literal safe; it is also mirrored in
+// The check is what makes the literal safe; the canary pin is also mirrored in
// app-identity.canary.ts, which is checked the same way.
//
// Deliberately NOT switch-core's own version from that file: bumping
// core/pyproject.toml is the first step of cutting a switch-core release, and a
// derived pin would immediately point local-server mode at images that are not
// on the registry yet.
-export const COMPATIBLE_SWITCH_VERSION = '0.29.0';
+//
+// Two pins: a canary build can run a switch-core pre-release while stable
+// stays on the last release. `switch-console.pins.switch-core` and
+// `switch-console.canary_pins.switch-core` respectively, each checked.
+export const STABLE_SWITCH_VERSION = '0.29.0';
+export const CANARY_SWITCH_VERSION = '0.30.0-rc.11';
+export const COMPATIBLE_SWITCH_VERSION = isCanary ? CANARY_SWITCH_VERSION : STABLE_SWITCH_VERSION;
// The last switch-core release that can still read a Matrix homeserver.
//
diff --git a/core/tests/test_gen_artifacts.py b/core/tests/test_gen_artifacts.py
index 0fdc169f8..02698e911 100644
--- a/core/tests/test_gen_artifacts.py
+++ b/core/tests/test_gen_artifacts.py
@@ -295,6 +295,61 @@ def test_registry_rejects_a_pin_of_something_it_does_not_know(
gen.load_registry()
+def test_registry_rejects_a_canary_pin_older_than_stable(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """A canary must never run an older release than stable."""
+ _write_registry(
+ tmp_path,
+ monkeypatch,
+ artifacts={
+ "dep": {"description": "d", "version": "1.0.0"},
+ "demo": {
+ "description": "d",
+ "version": "1.0.0",
+ "pins": {"dep": "1.1.0"},
+ "canary_pins": {"dep": "1.1.0-rc.3"},
+ },
+ },
+ )
+ with pytest.raises(ValueError, match="older than its stable pin"):
+ gen.load_registry()
+
+
+def test_registry_rejects_a_canary_pin_without_a_stable_one(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _write_registry(
+ tmp_path,
+ monkeypatch,
+ artifacts={
+ "dep": {"description": "d", "version": "1.0.0"},
+ "demo": {
+ "description": "d",
+ "version": "1.0.0",
+ "canary_pins": {"dep": "1.1.0"},
+ },
+ },
+ )
+ with pytest.raises(ValueError, match="no stable one"):
+ gen.load_registry()
+
+
+ ("older", "newer"),
+ [
+ ("0.29.0", "0.30.0-rc.11"),
+ ("0.30.0-rc.2", "0.30.0-rc.11"),
+ ("0.30.0-rc.11", "0.30.0"),
+ ("0.30.0-alpha", "0.30.0-beta"),
+ ],
+)
+def test_semver_ordering_puts_pre_releases_before_their_release(
+ older: str, newer: str
+) -> None:
+ assert gen._semver_key(older) < gen._semver_key(newer)
+
+
def test_registry_rejects_an_empty_range(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
diff --git a/scripts/gen_artifacts.py b/scripts/gen_artifacts.py
index f0a82d2af..ccaf76bfe 100644
--- a/scripts/gen_artifacts.py
+++ b/scripts/gen_artifacts.py
@@ -74,6 +74,8 @@ class Artifact:
#: Where it lands on the registry, for anything we publish.
published_as: str | None
pins: dict[str, str]
+ #: What a canary build pins instead, where it differs. Never older than `pins`.
+ canary_pins: dict[str, str]
@dataclass(frozen=True)
@@ -110,12 +112,25 @@ def load_registry() -> Registry:
artifacts = _resolve_inherited_versions(artifacts)
known = {artifact.name for artifact in artifacts}
for artifact in artifacts:
- for pinned in artifact.pins:
+ for pinned in [*artifact.pins, *artifact.canary_pins]:
if pinned not in known:
raise ValueError(
f"artifact {artifact.name!r} pins {pinned!r}, which is not "
"an artifact in this registry"
)
+ for pinned, canary in artifact.canary_pins.items():
+ stable = artifact.pins.get(pinned)
+ if stable is None:
+ raise ValueError(
+ f"artifact {artifact.name!r} has a canary pin of {pinned!r} "
+ "but no stable one to fall back to"
+ )
+ if _semver_key(canary) < _semver_key(stable):
+ raise ValueError(
+ f"artifact {artifact.name!r}: canary pin of {pinned!r} ({canary}) "
+ f"is older than its stable pin ({stable}); a canary must never "
+ "run an older release than stable"
+ )
contracts = [_parse_contract(name, body) for name, body in raw["contracts"].items()]
@@ -201,17 +216,8 @@ def _parse_artifact(name: str, body: object) -> Artifact:
if published_as is not None and not isinstance(published_as, str):
raise ValueError(f"artifact {name!r}: published_as must be a string")
- pins_raw = body.get("pins") or {}
- if not isinstance(pins_raw, dict):
- raise ValueError(f"artifact {name!r}: pins must be a mapping")
- pins: dict[str, str] = {}
- for pinned, pinned_version in pins_raw.items():
- if not isinstance(pinned_version, str) or not SEMVER.fullmatch(pinned_version):
- raise ValueError(
- f"artifact {name!r}: pin of {pinned!r} must be three-part semver, "
- f"got {pinned_version!r}"
- )
- pins[pinned] = pinned_version
+ pins = _parse_pins(name, body, "pins")
+ canary_pins = _parse_pins(name, body, "canary_pins")
return Artifact(
name=name,
@@ -221,9 +227,40 @@ def _parse_artifact(name: str, body: object) -> Artifact:
version_from=version_from,
published_as=published_as,
pins=pins,
+ canary_pins=canary_pins,
)
+def _parse_pins(name: str, body: dict[str, Any], key: str) -> dict[str, str]:
+ raw = body.get(key) or {}
+ if not isinstance(raw, dict):
+ raise ValueError(f"artifact {name!r}: {key} must be a mapping")
+ pins: dict[str, str] = {}
+ for pinned, pinned_version in raw.items():
+ if not isinstance(pinned_version, str) or not SEMVER.fullmatch(pinned_version):
+ raise ValueError(
+ f"artifact {name!r}: {key} entry for {pinned!r} must be three-part "
+ f"semver, got {pinned_version!r}"
+ )
+ pins[pinned] = pinned_version
+ return pins
+
+
+def _semver_key(
+ version: str,
+) -> tuple[int, int, int, int, tuple[tuple[int, int | str], ...]]:
+ """Orders versions as semver does: a pre-release sorts before its release,
+ and numeric pre-release identifiers compare as numbers."""
+ core, _, rest = version.partition("+")[0].partition("-")
+ major, minor, patch = (int(part) for part in core.split("."))
+ if not rest:
+ return (major, minor, patch, 1, ())
+ identifiers = tuple(
+ (0, int(part)) if part.isdigit() else (1, part) for part in rest.split(".")
+ )
+ return (major, minor, patch, 0, identifiers)
+
+
def _parse_contract(name: str, body: object) -> Contract:
if not isinstance(body, dict):
raise ValueError(f"contract {name!r}: expected a mapping")
@@ -282,13 +319,18 @@ def read_declared_version(relative_path: str) -> str:
LITERAL_CHECKS: list[tuple[str, str, str]] = [
(
"console/apps/switch-console-desktop/src/shared/app-identity.ts",
- "COMPATIBLE_SWITCH_VERSION",
+ "STABLE_SWITCH_VERSION",
"switch-console.pins.switch-core",
),
+ (
+ "console/apps/switch-console-desktop/src/shared/app-identity.ts",
+ "CANARY_SWITCH_VERSION",
+ "switch-console.canary_pins.switch-core",
+ ),
(
"console/apps/switch-console-desktop/src/shared/app-identity.canary.ts",
"COMPATIBLE_SWITCH_VERSION",
- "switch-console.pins.switch-core",
+ "switch-console.canary_pins.switch-core",
),
]
@@ -497,8 +539,11 @@ def check_declared_versions(registry: Registry) -> list[str]:
by_name = {artifact.name: artifact for artifact in registry.artifacts}
for relative_path, constant, source in LITERAL_CHECKS:
- owner, _, pinned = source.partition(".pins.")
- expected = by_name[owner].pins[pinned]
+ owner, kind, pinned = source.split(".")
+ artifact = by_name[owner]
+ expected = (artifact.canary_pins if kind == "canary_pins" else artifact.pins)[
+ pinned
+ ]
try:
actual = read_ts_literal(relative_path, constant)
except ValueError as exc: