Skip to content

Run database maintenance ​

Use the database maintenance runner to apply the reviewed PostgreSQL migration plan and, when selected, update the Environment, Orchestrator, and Events & Sessions workloads to matching images. The runner is available locally and through a manual GitHub Actions workflow.

This is a write operation. An execute run can apply migrations, change schema ownership and grants, create or update the migration identity and Kubernetes resources, and roll Deployments. Obtain approval for the exact source, target, and image digests before using --execute. A preflight without --execute does not apply those changes.

After a failure, inspect the receipt and confirm which effects completed before you retry. A failure does not roll back earlier committed migrations or workload updates. Do not retry an uncertain effect.

The runner applies these migrations, in this order:

  1. Identity Broker EF Core migrations, when you pass a Broker digest.
  2. Environment EF Core migrations 1 to 13.
  3. Orchestrator (Core) migrations 1 to 16, 18 to 33, and 35 to 51. Reserved gaps are not applied.
  4. Events & Sessions migrations 1 to 11.

The reviewed plan fixes these versions by hash. Core migration 017 is not in the plan, while 032 is included with its exact runtime table grants. Migration 035 adds backlog queue positions, 036 adds append-only coordinator-root model-turn admissions, 037 adds append-only child-spawn authorization receipts and consumption records, and 038 adds the Source Control pull-request operation ledger. Migration 038 requires migration 012. Reserved migration 034 remains outside the plan. Migrations 036 and 037 depend on the immutable-row trigger function from 021. Migration 037 also references run, session, grant, and policy-receipt records already owned by Orchestrator. An image that would apply a version outside the plan fails before it connects. Readiness accepts 036 only when its ledger row and table are both absent, or when the migration is recorded and the table has every admission column, its request primary key, and an enabled unconditional immutability trigger. Readiness accepts 037 only when its ledger row and both tables are absent, or when the migration is recorded and both evidence tables have their expected columns, keys, foreign keys, and enabled immutable-row triggers. The runtime role receives exactly SELECT, INSERT on both 037 tables. Readiness accepts 038 only when its ledger row and operation table are both absent, or when the migration is recorded and the operation table has its expected columns and constraints. The runtime role receives exactly SELECT, INSERT, UPDATE on the 038 table. Readiness ordering retains the exact supported Core set so 034 is never treated as compatible between 033 and 035. Migration 042 adds runtime_run_start_intents. The runtime role receives exactly SELECT, INSERT, UPDATE on that table. Readiness accepts 042 only when the migration row and table are both absent, or when the migration is recorded and the table has all 20 columns and the expected constraints. The column check uses PostgreSQL catalog metadata and does not require access to the table.

Migrations 043 and 045 add the native root-turn completion and replay evidence kinds to maf_runtime_evidence_receipts. The existing runtime role keeps its SELECT, INSERT access to that append-only table. Migration 044 adds runtime_replacement_operations; the runtime role receives exactly SELECT, INSERT, UPDATE on that table. Readiness checks each migration and its complete table or constraint shape before it accepts the schema.

What one run does ​

StepWhat happensRepeat behavior and limits
Source checkRequires a clean checkout whose commit is on origin/v1.Read-only.
Locked restoreRestores the persistence, Broker, and relay projects with --locked-mode.Read-only.
Plan checkRequires the committed plan file, the plan generator, and the pinned hash to agree.Read-only.
Cluster accessChecks the Azure account, gets a private kubeconfig for agentweaver-aks, and checks Kubernetes permissions.Uses a temporary file that the runner removes during cleanup.
Broker egress checkCompares the live Broker migration egress policy with the repository and reports any DNS name that differs.Read-only unless you explicitly add --reconcile-broker-egress to an execute run.
Setup checkReads the migration identity, its federated credential, and the recorded handover. A workload identity that would need either one stops here and names the setup command.Read-only.
Service image checkReads running and target schema ranges and chooses deployment order in normal maintenance mode. The check is skipped by --migrations-only. If the plan includes Core 047, the runner still checks Orchestrator and Environment Deployment images and guard-aware pods before the migration Jobs.Read-only.
LockCreates the Lease p1-db-maintenance-lock. A second run fails here.The runner releases a normal run's Lease during cleanup. A settled owner installer can mark its own Lease ready for takeover; a crash leaves the timeout in place.
Migration identityUses the existing identity when it and its federated credential match. Otherwise a run signed in as the PostgreSQL Entra administrator deploys infra/bicep/p1-postgres-migration-identity.bicep.Check the receipt and identity readback before another execute run.
ServiceAccount and egressApplies p1-postgres-migration and its egress policy with server-side apply.Check the receipt and live state before retrying an uncertain update.
Ownership handoverMakes the migration identity the owner of the environment, orchestrator, and events_sessions schemas.Recorded in the ConfigMap p1-db-maintenance-state; check the recorded handover before retrying.
Connectivity probesA short Job under each migration egress policy resolves the PostgreSQL name, including its private link name, and opens port 5432. It requests at least 25m CPU and 32Mi memory to satisfy the namespace resource minimum.The runner removes its uniquely named probe Jobs during cleanup. Check the receipt before starting another run.
Migration JobsRuns the Broker, Environment, Orchestrator, and Events & Sessions Jobs on the pinned digests, one at a time.Migrators skip applied versions. A later failure does not roll back completed migrations.
Grants checkRequires one grants marker from each Environment, Orchestrator, and Events & Sessions migration Job, with versions that match the plan exactly. The Identity Broker Job is exempt; successful completion indicates that its migration succeeded.Read-only.
Environment Policy ledger cutoverFor the schema-18 cutover that revokes INSERT on the three Environment Policy ledger tables, verify the Linux/AMD64 platform digest and reviewed v1 source commit for all three P1 migration images before changing grants. On fresh installations (deployment/environment absent), require zero non-terminal environment pods; deploy:kustomize blocks environment-migration if deployment/environment or non-terminal environment pods exist and routes to db:maintenance. On existing installations, suspend competing release pipelines, controllers, and manual scaling on deployment/environment for the maintenance window, require --deploy-apps (even when environment's image already matches), validate Orchestrator Core 047 compatibility first, persist rollForward.planSha256, all three rollForward.<job> digests, and rollForward.environmentPriorReplicas (including 0) before patching deployment/environment spec.replicas to 0 and draining all pods, recheck spec.replicas === 0 and zero non-terminal pods immediately before environment-migration, roll to the approved image, restore environmentPriorReplicas, and verify readiness before clearing recovery.Retry interrupted cutovers (including after image write) with normal maintenance (--execute --deploy-apps) using the same plan and digests; --migrations-only is blocked when deployment/environment or non-terminal pods exist, and --apps-only verifies all three image revisions and cannot change environment or clear pending recovery. Do not roll back to an older Environment image or restore INSERT.
ServicesNormal maintenance mode can roll mismatched Deployments to the same digests when asked, then waits for readiness. --migrations-only does not change application Deployments.Matching images are not changed. Check the receipt and Deployment state after a failure.
Cleanup and receiptDeletes the run's Jobs and pods, releases the lock, removes temporary files, and writes a receipt.The runner attempts cleanup on success and failure. Verify the receipt; do not assume cleanup succeeded.

Without --execute, the runner stops after its mode-specific preflight. It prints the steps an execute run would take and applies no migrations, grants, identity, Kubernetes resources, or workload changes.

Before you begin ​

You need:

  • A clean checkout of v1 at a commit that is already on origin/v1.
  • Node.js 24, the .NET SDK from global.json, Azure CLI, kubectl, and kubelogin.
  • An Azure CLI sign-in on the Agentweaver subscription and tenant with access to the agentweaver-aks cluster.
  • The image digests of a published release for Orchestrator and Environment, Events & Sessions, and for the Identity Broker if it has new migrations. Read them from the release's runtime manifest.

The Environment, Orchestrator, and Events & Sessions migration Jobs require images built from source that prints the grants marker. Older images fail the grants check after they migrate. The Identity Broker Job does not print that marker; successful completion indicates that its migration succeeded. Use --apps-only to move services onto an older release.

The Events & Sessions Job also sets the full Sessions and Messaging provider configuration from its runtime map. The host validates those settings before it enters --migrate. Orchestrator handles --migrate before loading normal runtime provider settings.

One-time setup ​

Run the check-only command to see which setup parts are missing. Review that list before using --execute to create the missing parts:

powershell
npm run db:maintenance:setup            # check only; exits 1 and lists what is missing
npm run db:maintenance:setup -- --execute

With --execute it creates only what is missing, reads everything back, and writes a receipt:

PartWhat it creates
PostgreSQL migration identityThe identity, its federated credential, its ServiceAccount and egress policy, and the schema ownership handover. It reuses the runner in --prepare-only mode, with the same preflight, lock, cleanup, and receipt.
Workflow identityinfra/bicep/db-maintenance-ci-identity.bicep creates agentweaver-id-db-maintenance, its federated credential for v1-db-maintenance, Reader on the resource group, and AKS Cluster User on the cluster. Setup applies a Kubernetes Role and RoleBinding in the agentweaver namespace.
GitHub environmentThe environment v1-db-maintenance, limited to the v1 branch, and its variable AZURE_DB_MAINTENANCE_CLIENT_ID.

The AKS template enables both Azure RBAC and Kubernetes RBAC, and setup checks that both are enabled before it continues. The built-in AKS RBAC Writer role covers standard resources such as Jobs, Deployments, pod logs, and ConfigMaps, but it also grants access to Secrets and lets the identity run Pods as any ServiceAccount in the namespace. It also does not grant access to the Cilium network-policy resource that the runner checks. Instead, setup applies a Kubernetes Role with only the verbs the runner needs, including access to that Cilium resource. Deployment access is limited by name to environment, orchestrator, and events-and-sessions. The RoleBinding names the workflow identity's object ID. This uses both AKS authorization models without creating an Azure custom role definition. Setup reads the Role and RoleBinding back to verify their exact permissions and identity. It never removes the workflow identity, Azure assignments, Role, or RoleBinding, so a failed setup can be checked and safely rerun.

Run the setup only after the required Azure and GitHub approvals are in place, with the Azure CLI signed in as the PostgreSQL Entra administrator, with the right to assign roles in the agentweaver resource group, and with the GitHub CLI signed in as a repository admin. This is the same sign-in that runs the PostgreSQL bootstrap. The command needs no prompt or decision, so it runs the same from a terminal, a script, or an agent session that already has that sign-in.

The setup needs that sign-in because of the ownership handover. The migration schemas start out owned by the Entra administrator's database role. PostgreSQL lets only a member of the owning role give objects away, and Azure Database for PostgreSQL has no superuser. Only the Entra administrator can also create the database principal for the migration identity. After the handover, the migration identity owns the schemas, and later runs, including workflow runs, need neither that sign-in nor the setup command.

The workflow identity cannot deploy Azure resources or assign roles. If the migration identity or the handover is missing, a workflow run stops before it changes anything and names the setup command. Role assignments can take a few minutes to reach AKS. Run the workflow once without execute to confirm the workflow identity has every permission the runner checks.

Run it locally ​

Check preconditions first. This changes nothing:

powershell
npm run db:maintenance -- `
  --environment-digest sha256:<environment> `
  --orchestrator-digest sha256:<orchestrator> `
  --events-digest sha256:<events-and-sessions> `
  --broker-digest sha256:<identity-broker>

Then apply, and roll the services to the same digests:

powershell
npm run db:maintenance -- --execute --deploy-apps `
  --environment-digest sha256:<environment> `
  --orchestrator-digest sha256:<orchestrator> `
  --events-digest sha256:<events-and-sessions> `
  --broker-digest sha256:<identity-broker>

Leave out --broker-digest when the Broker has no new migrations.

Upgrade existing core owners across a readiness gap ​

Use --core-owner-upgrade with the exact-source deployment command when the new Environment or Orchestrator image cannot start on the live database schema. Run the command from a clean, trusted operator checkout. The command creates a clean detached worktree at the named candidate commit and verifies the candidate tree and all ten image identities.

Check the full handoff before the outage:

powershell
npm run azure:deploy-from-commit -- `
  <candidate-sha> `
  --core-owner-upgrade `
  --resource-group <approved-target> `
  --agenthost-egress-audit off `
  --dry-run

The dry run verifies the target, plan, exact migration image digests, candidate owner Deployments, original replica counts, immutable runtime ConfigMaps, and final workload inputs. It does not change the cluster.

A --core-owner-upgrade --dry-run run (fresh or resume) can run from a clean local operator commit that is not yet on origin/v1. The candidate commit and any --resume-core-owner-upgrade-from original commit must still be on origin/v1. The dry run still fetches origin/v1, rejects dirty checkouts and Git errors, and records the operator commit in the receipt. Live --core-owner-upgrade --execute runs and standalone npm run db:maintenance runs still require the operator commit to be on origin/v1.

Run the same command with --execute to apply it:

powershell
npm run azure:deploy-from-commit -- `
  <candidate-sha> `
  --core-owner-upgrade `
  --resource-group <approved-target> `
  --agenthost-egress-audit off `
  --execute

Replace <candidate-sha> with the reviewed lowercase 40-character commit. Replace <approved-target> with the approved registered target. The core owner path uses the default ghcr.io/sabbour registry. Keep every argument identical between preflight and execution.

The execute path takes the shared maintenance Lease and records the candidate, target, plan, image digests, Deployment UIDs, and original replica counts in p1-db-maintenance-state. It stops Environment and Orchestrator, waits for all non-terminal owner pods to exit, and—when the rollout changes the effective Environment__Sandbox__AgentSandbox__StartupBudgets__ScheduledSeconds value—runs Agentweaver.Orchestrator.dll --check-startup-cutover under the p1-postgres-migration ServiceAccount. That Job reads both the orchestrator and environment schemas in one REPEATABLE READ READ ONLY snapshot and prints aggregate counts only. If any unresolved root or child start (including one with an Active lease), startup provider attempt, or startup cleanup remains, the runner stops before staging candidate templates or changing runtime ConfigMaps: it removes the check Job, verifies zero active Job pods and unchanged owner Deployments, restores and reads back original replicas, closes only that operation in p1-db-maintenance-state, and releases p1-db-maintenance-lock (keeping state and lock if any recovery check is uncertain). Once the current owners settle the unfinished work, start a fresh run. When the check passes, the runner stages the three candidate owner templates at zero replicas. It then verifies or repairs the approved immutable AgentHost, Environment, and Orchestrator runtime ConfigMaps while their owners are stopped. Orchestrator replacement requires zero replicas, no non-terminal consumer Pod, and a second check after the 70-second cache wait. The installer uses the live map UID and resource version as delete preconditions and checks the exact replacement data before any migration Job or owner restoration. Changed and missing maps use create admission during preflight. Projects & Config uses its existing final-phase guarded replacement. If its map needs replacement, deployment/projects-config must exist before the installer writes. A core-owner retry preserves the original Projects & Config replica count until final smoke verification succeeds.

Each migration Job identity is recorded before submission. A retry adopts an existing Job only when its operation label and image match, waits for the Job, and checks its grants marker. The owner images stay at the candidate digest after any migration Job has been submitted. The command restores the recorded replica counts and checks owner readiness before the stock partial workload handoff. The handoff inherits the three verified owner images and omits their migration Jobs. It still applies and verifies the other candidate workloads, settings, and the separate AgentHost image.

If execution stops, fix the reported condition. A bound installer that has stopped its own work and proved that all owner migration Jobs and Pods are terminal marks its existing Lease ready for takeover before it exits. Confirm that the prior operator process stopped, then rerun the same exact command with --resume-core-owner-upgrade:

powershell
npm run azure:deploy-from-commit -- `
  <candidate-sha> `
  --core-owner-upgrade `
  --resume-core-owner-upgrade `
  --resource-group <approved-target> `
  --agenthost-egress-audit off `
  --execute

The takeover uses compare-and-swap tests on the Lease resource version and holder. It accepts only an expired Lease with no active migration Jobs or Pods. If the installer crashes, loses ownership, or cannot prove settlement, the original timeout remains. Keep the candidate, target, image digests, audit mode, and approved settings unchanged. A different input is a different operation. Do not scale an old owner image up or roll an owner image back after a migration Job has been submitted.

Before candidate admission or the maintenance Lease, the guarded dry run and execute paths read ResourceQuota/agentweaver-quota. A fresh core-owner upgrade, a same-original resume, and a successor resume must all have room for one additional persistentvolumeclaim. The runner compares current live usage plus one with the live hard limit and the rendered workload hard limit. It does not predict cleanup or a prospective quota increase. Missing, denied, malformed, incomplete, or scoped quota evidence stops the operation. Retained workspace claims remain counted until their owner removes them. On awwus2np01 in namespace agentweaver, two guarded core-owner paths preserve a raised live persistentvolumeclaims limit when all other quota fields and labels match: a fresh core-owner upgrade when the Lease is absent, any previous owner upgrade is absent or closed with no pending recovery, and both identity-broker and projects-config have observed their current generation with all desired replicas ready and available; and an explicit same-original resume at owners-ready with no successor binding and exact matching candidate evidence. Both paths omit the quota from server admission and final apply. Under the maintenance Lease, the fresh path rechecks the state, service readiness, and live quota before writing operation state, mutating owners, or running migrations, and both paths recheck the live quota again before quiescing and before final apply.

The owner upgrade holds its Lease for the final handoff, not only for database maintenance. The wrapper renews and checks the holder with the Lease resource version before final mutations and each rollout. If final rollout or smoke checks fail after cleanup, the wrapper uses the same guarded Lease handoff. The check covers resources labeled with the owner operation, the database runner's Lease holder and run ID, and the prior holder from a takeover. A normal final handoff continues under that Lease. The command closes the owner operation after final readback and smoke checks.

Resume a settled core owner upgrade from a successor commit ​

If all three owner migration Jobs completed and a service cannot start, resume with a corrected commit on origin/v1 that descends from the original. After the stopped installer marks the existing Lease ready, or after the original timeout expires, run a dry run:

powershell
npm run azure:deploy-from-commit -- `
  <successor-sha> `
  --core-owner-upgrade `
  --resume-core-owner-upgrade-from <original-sha> `
  --resource-group <approved-target> `
  --agenthost-egress-audit off `
  --dry-run

Use the same approved deployment settings as the original run, including any model bindings. Run the same command with --execute after the dry run passes.

If a bound successor fails owner readiness before owners-ready (for example, deployment/environment hits ProgressDeadlineExceeded while orchestrator, events-and-sessions, projects-config, and identity-broker remain healthy with no cutover), replace the failed successor with a newer origin/v1 descendant of both commits using its failed execution receipt:

powershell
npm run azure:deploy-from-commit -- `
  <new-successor-sha> `
  --core-owner-upgrade `
  --resume-core-owner-upgrade-from <original-sha> `
  --replace-failed-successor <maintenance-receipt-path> `
  --resource-group <approved-target> `
  --agenthost-egress-audit off `
  --dry-run

Run the same command with --execute after the dry run passes. The new candidate must use a different agentweaver.environment image digest. At both preflight and under the Lease, the runner checks the failed receipt, original rollForward.* records, absent closure keys, owner UIDs, replica counts, failed images, environment ProgressDeadlineExceeded, healthy orchestrator, events-and-sessions, projects-config, and identity-broker, and active work across both the receipt holder and any expired same-operation Lease holder. If projectsConfig.consumeGuardPending is true without projectsConfig.consumeGuardRequired=true, replacement stops. When both projectsConfig.consumeGuardPending and projectsConfig.consumeGuardRequired are true, the runner also verifies before and under the Lease that no non-terminal Projects & Config migration or credential-settlement Job or Job Pod exists in the namespace across any run ID, rejects unreadable or malformed inventory, and preserves both keys. It rebinds the seven ownerUpgrade.successor* fields and ownerUpgrade.holder atomically with a resourceVersion check before any workload write, preserves rollForward.* and retained Job proofs, and replaces owner Deployments with their validated uid and resourceVersion. If the replacement run fails or its outcome is uncertain, read back p1-db-maintenance-state before retrying: if the new successor is already bound, retry that exact commit without --replace-failed-successor; if the old failed successor is still bound, retry with --replace-failed-successor.

This mode is mutually exclusive with --resume-core-owner-upgrade. It verifies the original commit, tree, pinned images, schema versions, Deployment UIDs, recorded replica counts, and all three retained migration Jobs and grants markers. The successor's rendered settings must match the original operation except for verified image references and values derived directly from those images, including Identity Broker settings.

On --execute, the installer takes over the expired Lease and records the exact successor before changing workloads. If a prior complete successor binding has matching closure fields (ownerUpgrade.finalSourceSha and ownerUpgrade.finalSourceTree) and ordered timestamps (ownerUpgrade.successorBoundAt <= ownerUpgrade.completedAt < ownerUpgrade.startedAt), the installer treats that binding as stale, replaces all successor fields atomically with a resource-version check, and removes the old closure fields. Incomplete, malformed, or conflicting successor or closure evidence does not authorize replacement, and a stale binding does not qualify for stopped Identity Broker recovery. A genuinely new core owner upgrade clears prior successor and closure fields when it records the new operation. It checks the runtime ConfigMaps before owner restoration. An unchanged map creates no outage. A changed or missing Environment or Orchestrator map is repaired while its owner is stopped. The installer never scales Orchestrator down for this repair; a drifted replica count stops the run before map mutation. The replacement uses the same drain, cache wait, identity-preconditioned replacement, and readback checks as the original operation. It then restores the owners at their recorded replica counts. It does not rerun completed owner migration Jobs or change database permissions. If the run stops again, use the same successor. The installer deletes the retained migration Jobs after it verifies the deployment and closes the operation.

When a bound successor run stops identity-broker and projects-config under the maintenance Lease and runs credential settlement before a selected Projects & Config migration, it keeps the normal migration order. If that first settlement Job ends in a terminal Failed=True state because projects_config.model_provider_credential_operations does not exist yet (Npgsql.PostgresException (0x80004005): 42P01: relation "projects_config.model_provider_credential_operations" does not exist), the installer verifies the Job status and container log, deletes the failed Job with foreground cleanup, renews the Lease, runs the Projects & Config migration, and then runs one new settlement Job. Both workloads remain at zero replicas with their restore annotations until that settlement retry passes. A timeout, any other error, or a failure during cleanup, Lease renewal, the Projects & Config migration, or the settlement retry stops the run, skips the Projects & Config Pair A restore path, keeps both workloads stopped, and retains the recovery record.

When you retry that stopped bound successor from the same commit and tree with ownerUpgrade.phase=owners-ready, identityBroker.disableFenceRequired=true, and deployment/identity-broker at 0 replicas with a valid agentweaver.io/local-deploy-restore-replicas annotation matching the rendered replica count, the installer projects a cloned Deployment at the saved replica count for the provider setup check without changing the live spec. Database maintenance requires that exact bound successor, owners-ready phase, and Broker fence both on its preflight read and under the maintenance Lease, with no first-time bind fallback. At the final credential cutover, the installer verifies the stopped Broker's UID, resourceVersion, zero replicas, and unchanged restore annotation against preflight before continuing, and uses that preflight resourceVersion as the write fence for the final Broker patch.

When you resume the original core owner upgrade with --resume-core-owner-upgrade from the same commit, tree, target, plan, image digests, and settings while ownerUpgrade.phase=owners-ready, identityBroker.disableFenceRequired=true, no successor binding or closure or refusal key exists, and both deployment/identity-broker and deployment/projects-config remain at 0 replicas with valid agentweaver.io/local-deploy-restore-replicas annotations:

  1. The installer projects a cloned deployment/identity-broker at its saved replica count solely for the provider setup check without changing the live spec, and verifies that all four runtime ConfigMaps match without writes.
  2. Database maintenance requires an existing expired p1-db-maintenance-lock Lease held by the same original holder, verifies the retained core migration Jobs, grants markers, and healthy core owner Deployments, takes over the Lease and updates ownerUpgrade.holder with compare-and-swap checks, and skips connectivity probes, core owner restaging, core migrations, and runtime ConfigMap writes.
  3. The final handoff keeps both identity-broker and projects-config stopped through credential settlement and migration checks, re-verifies both stopped workloads and p1-db-maintenance-state before applying remaining workloads and the guarded identity-broker patch, omits settled core owner Deployments and verified runtime ConfigMaps from kubectl apply, verifies rollout readiness and smoke checks, and closes the original operation without a successor. Any mismatch or drift stops before writes and retains the open operation and lock.

Run it from GitHub Actions ​

Open the v1 database maintenance workflow and choose Run workflow on the v1 branch. Fill in the digests. Leave execute off for a preflight, and turn it on to apply. The workflow runs the same command and uploads the receipt as an artifact, even when the run fails.

From the GitHub CLI:

powershell
gh workflow run v1-db-maintenance.yml --ref v1 `
  -f orchestrator_digest=sha256:<orchestrator> `
  -f environment_digest=sha256:<environment> `
  -f events_digest=sha256:<events-and-sessions> `
  -f services=deploy `
  -f execute=true

Only one workflow run executes at a time.

Migrations during workload deployment ​

The local workload deployment command runs the selected migration Jobs and Projects & Config runtime-grant reconciliation before it applies the namespaced workload overlay:

powershell
npm run deploy:kustomize -- --image-manifest <path-to-image-manifest.json> --log-analytics-workspace-id <monitorWorkspaceId> --execute

Use the monitorWorkspaceId customer ID from the existing foundation receipt when the manifest selects Environment. Omit the option for a partial manifest that does not select Environment; that deploy preserves the live audit configuration.

The command excludes Namespace objects and checks all nine Deployment rollouts. A migration failure stops before the root workload apply. The one-time Projects & Config identity and ownership setup is a separate prerequisite. On a guard failure, stop for authorized role inspection; do not loosen the guard or broaden the role. A migration success does not prove a deployment success. Use the canonical release runbook for the deployment procedure.

Keep services ready ​

The Environment, Orchestrator, and EventsAndSessions services check their schema version at startup and on /health/ready. Each image accepts only a range of schema versions. If the database moves outside that range, the service becomes unready.

The runner reads the running images before it changes anything. It compares each image's accepted range with the current schema and the target schema, and picks one order:

OrderWhenWhat the runner does
Migrate firstThe running image accepts the target schema.Migrates. With --deploy-apps, then sets the new image.
Deploy firstThe new image accepts the current schema.Sets the new image and waits until it is ready, then migrates. Needs --deploy-apps.
Planned readiness gapNeither image accepts the other's schema.Migrates, then sets the new image in the same run and waits until it is ready. Needs --deploy-apps.

The options are:

OptionWorkflow servicesBehavior
NoneblockStops before any change when a service would need a new image to stay ready.
--deploy-appsdeploySets the given digests in the order above and waits until all three services are ready.
--apps-onlyapps-onlySkips every database step. Only rolls the three Deployments to the given digests under the lock and waits until they are ready.

Schema ranges ​

The runner reads each image's accepted range from the image itself. There is no list to update when you publish an image. A schema version is the highest applied migration of that service.

For every target image and each different running image, the runner:

  1. Reads the image's org.opencontainers.image.revision label from the registry. Every published service image has this label. It holds the commit the image was built from.
  2. Reads the service's migrator source at that commit with git. It fetches the commit from origin if the clone does not have it.
  3. Reads the range from the readiness check in that source. The highest version the check requires is the lowest accepted version. The highest version it allows is the highest accepted version. For example, the current Orchestrator image accepts schema versions 31 through 38. It accepts the complete schema before and after migrations 032, 033, 035, 036, 037, and 038. The root model-turn admissions table is optional until 036 is applied, and the child-spawn authorization tables are optional until 037 is applied. The pull-request operation ledger is optional until 038 is applied. Reserved migration 034 is not accepted by this source revision.

Environment uses EF Core timestamp migration IDs. The source assigns their catalog order to schema versions 1 through 13, and startup rejects pending migrations, so the runner treats an Environment image as accepting its exact catalog version.

If the runner cannot read the range of a new image, the run stops before any change. If the new image does not accept the target schema of the reviewed plan, the digest was built from other source, and the run also stops. This check also applies when the Deployment already runs that digest or does not exist. If it cannot read the range of a running image, for example a tag or an image from another registry, it assumes nothing about that image, so the run takes the planned-gap path. The runner records each schema version in its state after a run, and uses it to decide whether a new image accepts the current schema.

For the Core 031 to 038 upgrade, the old owner accepts only 31 to 32. The new owner accepts 31 to 38, so it can become ready before 032, 033, and 035 to 038 are applied. When the state records schema 31, --deploy-apps deploys the new owner first, waits for readiness, then applies the planned migrations. If no schema version is recorded, the runner cannot prove that order is safe and uses a planned readiness gap instead. Both paths keep 017 out of the migration plan.

Planned readiness gap ​

Some migrations have no order that keeps a service ready. For example, when the current images reject the new schema and the new images reject the old one, the service is unready for a short time. This is expected, not an error. The runner:

  1. Logs the planned gap and records it in the receipt before it changes anything.
  2. Refuses to migrate without --deploy-apps, because the matching images must follow in the same run.
  3. Records the images it will deploy in its state, then runs the migration Jobs.
  4. Sets the matching images right away and waits until each service is ready, within --readiness-timeout-seconds (default 600).
  5. Writes the gap's start, end, and length in seconds to readinessGap in the receipt.

Roll forward only ​

After the schema has moved, the old images cannot run on it. The runner never sets an older image. If the deploy or the readiness wait fails, the run stops, reports the failure, and keeps the gap open in the receipt (no end time). The state still holds the images it was deploying.

Run the same command again to finish. The next run accepts only those same digests. It refuses other digests, including the old ones, before it takes the lock. When the services are ready, it clears the record.

When migrations have already been applied and services are unready, roll the services to the release that matches the schema:

powershell
npm run db:maintenance -- --execute --apps-only `
  --environment-digest sha256:<environment> `
  --orchestrator-digest sha256:<orchestrator> `
  --events-digest sha256:<events-and-sessions>

Only the container image changes. The runner never creates a Deployment, and it fails if one is missing.

Apps-only maintenance updates only the selected application Deployments. For the full workload set, use npm run deploy:kustomize; it runs migrations first and then applies the root Kustomize overlay without a deployment receipt gate.

Reconcile Broker egress drift ​

Every run compares the live identity-broker-migration-egress policy with the repository and prints the DNS names that differ. To restore the repository policy, obtain approval for that exact policy change, then add --reconcile-broker-egress to an execute run. The connectivity probe that follows checks whether the policy reaches PostgreSQL before the Broker Job starts. It does not prove broader network access.

Read the receipt ​

Every run writes a JSON receipt, even when it fails. Locally it goes to ~/.agentweaver/db-maintenance/<run-id>.json, outside the checkout, so the next run's clean-tree check still passes. Use --receipt-dir to choose another folder.

The receipt records the source commit, the operator, each step with its time, the resolved private link name, each Job's grants marker, the service images before and after, the chosen service order, any planned readiness gap, the cleanup result, and the error when the run fails.

Recover from a failed run ​

The runner fails closed. The first failed check stops the run, and the runner attempts cleanup. Read the error and receipt. Confirm which steps completed and whether cleanup succeeded. If an external effect is uncertain, reconcile its state with the owning service before you retry. After the cause and state are clear, use the same approved image digests; migrators skip versions already applied.

If a run stops without cleanup, for example because the machine lost power, its lock keeps the original timeout. The next run prints the holder's run ID. After you confirm that run is not active, release its lock:

powershell
npm run db:maintenance -- --release-lock <run-id>

What the runner never does ​

  • Apply Core migration 017 or any version outside the reviewed plan.
  • Reset, reinitialize, or drop a database, schema, or role.
  • Create Azure role assignments or change identities other than the PostgreSQL migration identity. Only the setup command assigns roles, and only the ones in its template.
  • Change any Deployment setting other than the service image.

These changes need their own review.

After the ownership handover, the operator no longer owns the migration schemas. --apply-pending-migrations of bootstrap-p1-postgres.mjs is limited to operator-owned migration schemas, so use this runner for later migrations instead. Its read-only --inspect mode reports live ownership and grants without changing them. Projects & Config runtime grants are reconciled by its dedicated-owner migration Job after EF migrations; the PostgreSQL operator initializer does not grant access in that schema.