Skip to content

BrainAPI is a knowledge graph–powered AI memory layer that transforms unstructured data into structured knowledge, enabling intelligent search, recommendations, and contextual memory for AI agents and applications.

Gate production releases on profile validation

Christian87aa0367e208
diff --git a/.github/workflows/heavy-validation.yaml b/.github/workflows/heavy-validation.yaml
index 447a188..39db041 100644
--- a/.github/workflows/heavy-validation.yaml
+++ b/.github/workflows/heavy-validation.yaml
@@ -8,13 +8,14 @@ on:
permissions:
contents: read
+ actions: read
jobs:
heavy-smoke-and-restore:
runs-on: [self-hosted, linux, x64, brainapi-heavy]
timeout-minutes: 90
env:
- BRAINAPI_IMAGE: brainapi:heavy-ci
+ BRAINAPI_IMAGE: brainapi:ci
BRAINAPI_ENV_FILE: env.heavy.example
REDIS_PASSWORD: ci-redis-password
NEO4J_PASSWORD: ci-neo4j-password
@@ -25,31 +26,110 @@ jobs:
MILVUS_TOKEN: root:ci-milvus-password
BRAINPAT_TOKEN: ci-system-token
BACKUP_DIR: ${{ github.workspace }}/.backup-artifacts
+ RELEASE_ARTIFACT_DIR: ${{ github.workspace }}/release-artifacts
steps:
- uses: actions/checkout@v4
+ - name: Download the exact audited candidate image
+ timeout-minutes: 45
+ uses: actions/github-script@v7
+ with:
+ script: |
+ const fs = require("fs");
+ const shas = [...new Set([
+ context.payload.pull_request?.head?.sha,
+ context.sha,
+ ].filter(Boolean))];
+ const deadline = Date.now() + 40 * 60 * 1000;
+ while (Date.now() < deadline) {
+ let run;
+ for (const sha of shas) {
+ const response = await github.rest.actions.listWorkflowRuns({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ workflow_id: "quality.yaml",
+ head_sha: sha,
+ status: "completed",
+ per_page: 20,
+ });
+ run = response.data.workflow_runs[0];
+ if (run) break;
+ }
+ if (run) {
+ if (run.conclusion !== "success") {
+ core.setFailed(`Quality workflow ${run.id} concluded ${run.conclusion}`);
+ return;
+ }
+ const artifacts = await github.paginate(
+ github.rest.actions.listWorkflowRunArtifacts,
+ {
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ run_id: run.id,
+ per_page: 100,
+ }
+ );
+ const artifact = artifacts.find(
+ (item) => item.name === "light-production-validation" && !item.expired
+ );
+ if (!artifact) {
+ core.setFailed(`Quality workflow ${run.id} has no candidate image artifact`);
+ return;
+ }
+ const archive = await github.rest.actions.downloadArtifact({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ artifact_id: artifact.id,
+ archive_format: "zip",
+ });
+ fs.writeFileSync("quality-artifacts.zip", Buffer.from(archive.data));
+ return;
+ }
+ await new Promise((resolve) => setTimeout(resolve, 30000));
+ }
+ core.setFailed(`Timed out waiting for successful Quality artifacts for ${shas.join(" or ")}`);
+ - name: Load and verify the shared candidate image
+ run: |
+ unzip -q quality-artifacts.zip -d quality-artifacts
+ image_archive=$(find quality-artifacts -name brainapi-image.tar.gz -print -quit)
+ security_file=$(find quality-artifacts -name security.json -print -quit)
+ test -n "$image_archive"
+ test -n "$security_file"
+ gzip -dc "$image_archive" | docker load
+ expected=$(python -c 'import json,sys; print(json.load(open(sys.argv[1]))["image_digest"])' "$security_file")
+ actual=$(docker image inspect "$BRAINAPI_IMAGE" --format '{{.Id}}')
+ test "$actual" = "$expected"
- name: Require runner capacity
run: |
awk '/MemTotal/ { exit !($2 >= 16000000) }' /proc/meminfo
test "$(uname -m)" = x86_64
- run: docker compose -f deploy/docker-compose.heavy.yaml config -q
- - run: docker build --platform linux/amd64 -t "$BRAINAPI_IMAGE" .
- name: Clean heavy startup
- run: docker compose --project-name brainapi-heavy-ci -f deploy/docker-compose.heavy.yaml up -d --wait
- - run: curl --fail --retry 20 --retry-delay 5 http://localhost/health
+ run: docker compose --project-name brainapi-heavy-ci -f deploy/docker-compose.heavy.yaml -f deploy/docker-compose.ci.yaml up -d --wait
+ - name: Heavy product smoke
+ run: |
+ curl --fail --retry 20 --retry-delay 5 http://localhost/health
+ python scripts/production_smoke.py exercise --profile heavy --system-token "$BRAINPAT_TOKEN" --artifact-dir "$RELEASE_ARTIFACT_DIR"
- name: Backup and verify
id: backup
run: |
- archive=$(deploy/brainapi-backup backup --profile heavy --project-name brainapi-heavy-ci --env-file deploy/env.heavy.example --backup-dir "$BACKUP_DIR" | tail -1)
+ archive=$(deploy/brainapi-backup backup --profile heavy --project-name brainapi-heavy-ci --env-file deploy/env.heavy.example --compose-file deploy/docker-compose.ci.yaml --backup-dir "$BACKUP_DIR" | tail -1)
deploy/brainapi-backup verify --profile heavy --env-file deploy/env.heavy.example --archive "$archive"
+ cp "$archive/manifest.json" "$RELEASE_ARTIFACT_DIR/backup-heavy-manifest.json"
echo "archive=$archive" >> "$GITHUB_OUTPUT"
- name: Restore into clean volumes
run: |
- docker compose --project-name brainapi-heavy-ci -f deploy/docker-compose.heavy.yaml down --volumes
- deploy/brainapi-backup restore --profile heavy --project-name brainapi-heavy-ci --env-file deploy/env.heavy.example --archive "${{ steps.backup.outputs.archive }}"
+ docker compose --project-name brainapi-heavy-ci -f deploy/docker-compose.heavy.yaml -f deploy/docker-compose.ci.yaml down --volumes
+ deploy/brainapi-backup restore --profile heavy --project-name brainapi-heavy-ci --env-file deploy/env.heavy.example --compose-file deploy/docker-compose.ci.yaml --archive "${{ steps.backup.outputs.archive }}"
curl --fail --retry 20 --retry-delay 5 http://localhost/health
+ python scripts/production_smoke.py verify-restore --profile heavy --system-token "$BRAINPAT_TOKEN" --artifact-dir "$RELEASE_ARTIFACT_DIR"
- name: Logs on failure
if: failure()
- run: docker compose --project-name brainapi-heavy-ci -f deploy/docker-compose.heavy.yaml logs --no-color
+ run: docker compose --project-name brainapi-heavy-ci -f deploy/docker-compose.heavy.yaml -f deploy/docker-compose.ci.yaml logs --no-color
- name: Clean heavy volumes
if: always()
- run: docker compose --project-name brainapi-heavy-ci -f deploy/docker-compose.heavy.yaml down --volumes --remove-orphans
+ run: docker compose --project-name brainapi-heavy-ci -f deploy/docker-compose.heavy.yaml -f deploy/docker-compose.ci.yaml down --volumes --remove-orphans
+ - uses: actions/upload-artifact@v4
+ if: always()
+ with:
+ name: heavy-production-validation
+ path: release-artifacts/
diff --git a/.github/workflows/quality.yaml b/.github/workflows/quality.yaml
index 4f2ed56..83dda0a 100644
--- a/.github/workflows/quality.yaml
+++ b/.github/workflows/quality.yaml
@@ -87,6 +87,7 @@ jobs:
POSTGRES_USERNAME: brainapi
BRAINPAT_TOKEN: ci-system-token
BACKUP_DIR: ${{ github.workspace }}/.backup-artifacts
+ RELEASE_ARTIFACT_DIR: ${{ github.workspace }}/release-artifacts
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
@@ -96,6 +97,10 @@ jobs:
if env -u REDIS_PASSWORD docker compose -f deploy/docker-compose.light.yaml config -q; then exit 1; fi
- name: Build exact test image
run: docker build --platform linux/amd64 --load -t "$BRAINAPI_IMAGE" .
+ - name: Export exact candidate image
+ run: |
+ mkdir -p "$RELEASE_ARTIFACT_DIR"
+ docker save "$BRAINAPI_IMAGE" | gzip -1 > "$RELEASE_ARTIFACT_DIR/brainapi-image.tar.gz"
- name: Generate image SBOM
uses: anchore/sbom-action@v0
with:
@@ -109,24 +114,36 @@ jobs:
severity: HIGH,CRITICAL
ignore-unfixed: true
exit-code: "1"
+ format: json
+ output: trivy.json
- name: Light profile smoke
run: |
- docker compose --project-name brainapi-ci -f deploy/docker-compose.light.yaml up -d --wait
+ docker compose --project-name brainapi-ci -f deploy/docker-compose.light.yaml -f deploy/docker-compose.ci.yaml up -d --wait
curl --fail --retry 12 --retry-delay 5 http://localhost/health
+ python scripts/production_smoke.py exercise --profile light --system-token "$BRAINPAT_TOKEN" --artifact-dir "$RELEASE_ARTIFACT_DIR"
- name: Light backup and clean restore
run: |
- archive=$(deploy/brainapi-backup backup --profile light --project-name brainapi-ci --env-file deploy/env.light.example --backup-dir "$BACKUP_DIR" | tail -1)
+ archive=$(deploy/brainapi-backup backup --profile light --project-name brainapi-ci --env-file deploy/env.light.example --compose-file deploy/docker-compose.ci.yaml --backup-dir "$BACKUP_DIR" | tail -1)
deploy/brainapi-backup verify --profile light --env-file deploy/env.light.example --archive "$archive"
- docker compose --project-name brainapi-ci -f deploy/docker-compose.light.yaml down --volumes
- deploy/brainapi-backup restore --profile light --project-name brainapi-ci --env-file deploy/env.light.example --archive "$archive"
+ cp "$archive/manifest.json" "$RELEASE_ARTIFACT_DIR/backup-light-manifest.json"
+ docker compose --project-name brainapi-ci -f deploy/docker-compose.light.yaml -f deploy/docker-compose.ci.yaml down --volumes
+ deploy/brainapi-backup restore --profile light --project-name brainapi-ci --env-file deploy/env.light.example --compose-file deploy/docker-compose.ci.yaml --archive "$archive"
curl --fail --retry 12 --retry-delay 5 http://localhost/health
+ python scripts/production_smoke.py verify-restore --profile light --system-token "$BRAINPAT_TOKEN" --artifact-dir "$RELEASE_ARTIFACT_DIR"
+ - name: Record exact-image security evidence
+ run: |
+ cp brainapi.spdx.json "$RELEASE_ARTIFACT_DIR/brainapi.spdx.json"
+ python scripts/write_security_artifact.py --trivy trivy.json --sbom "$RELEASE_ARTIFACT_DIR/brainapi.spdx.json" --image "$BRAINAPI_IMAGE" --output "$RELEASE_ARTIFACT_DIR/security.json"
- name: Logs on failure
if: failure()
- run: docker compose --project-name brainapi-ci -f deploy/docker-compose.light.yaml logs --no-color
+ run: docker compose --project-name brainapi-ci -f deploy/docker-compose.light.yaml -f deploy/docker-compose.ci.yaml logs --no-color
- name: Clean CI volumes
if: always()
- run: docker compose --project-name brainapi-ci -f deploy/docker-compose.light.yaml down --volumes --remove-orphans
+ run: docker compose --project-name brainapi-ci -f deploy/docker-compose.light.yaml -f deploy/docker-compose.ci.yaml down --volumes --remove-orphans
- uses: actions/upload-artifact@v4
+ if: always()
with:
- name: light-image-sbom
- path: brainapi.spdx.json
+ name: light-production-validation
+ path: |
+ release-artifacts/
+ trivy.json
diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml
index 9bfda36..13601e3 100644
--- a/.github/workflows/release.yaml
+++ b/.github/workflows/release.yaml
@@ -6,6 +6,8 @@ on:
permissions:
contents: read
+ checks: read
+ actions: read
packages: write
id-token: write
@@ -16,6 +18,8 @@ jobs:
version: ${{ steps.tag.outputs.version }}
steps:
- uses: actions/checkout@v4
+ with:
+ fetch-depth: 0
- id: tag
shell: bash
run: |
@@ -31,41 +35,128 @@ jobs:
tag_version="${{ steps.tag.outputs.version }}"
normalized_version="${tag_version/-rc./rc}"
test "$project_version" = "$normalized_version"
+ - name: Require the tagged commit to be on main
+ run: git merge-base --is-ancestor "$GITHUB_SHA" origin/main
+
+ verify-required-checks:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+ - name: Require successful quality and heavy validation for this commit
+ uses: actions/github-script@v7
+ with:
+ script: |
+ const required = [
+ "python",
+ "benchmark-helpers",
+ "frontend (console)",
+ "frontend (tui)",
+ "rust",
+ "image-light-smoke",
+ "heavy-smoke-and-restore",
+ ];
+ const runs = await github.paginate(github.rest.checks.listForRef, {
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ ref: context.sha,
+ per_page: 100,
+ });
+ const failed = required.filter((name) =>
+ !runs.some((run) => run.name === name && run.conclusion === "success")
+ );
+ if (failed.length) {
+ core.setFailed(`Missing successful checks for ${context.sha}: ${failed.join(", ")}`);
+ }
+ - name: Download exact-commit validation artifacts
+ uses: actions/github-script@v7
+ with:
+ script: |
+ const fs = require("fs");
+ const required = [
+ ["quality.yaml", "light-production-validation", "light.zip"],
+ ["heavy-validation.yaml", "heavy-production-validation", "heavy.zip"],
+ ];
+ for (const [workflow, artifactName, output] of required) {
+ const response = await github.rest.actions.listWorkflowRuns({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ workflow_id: workflow,
+ head_sha: context.sha,
+ status: "completed",
+ per_page: 100,
+ });
+ const run = response.data.workflow_runs.find(
+ (item) => item.conclusion === "success"
+ );
+ if (!run) {
+ core.setFailed(`No successful ${workflow} run found for ${context.sha}`);
+ return;
+ }
+ const artifacts = await github.paginate(
+ github.rest.actions.listWorkflowRunArtifacts,
+ {
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ run_id: run.id,
+ per_page: 100,
+ }
+ );
+ const artifact = artifacts.find(
+ (item) => item.name === artifactName && !item.expired
+ );
+ if (!artifact) {
+ core.setFailed(`Missing ${artifactName} on workflow run ${run.id}`);
+ return;
+ }
+ const archive = await github.rest.actions.downloadArtifact({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ artifact_id: artifact.id,
+ archive_format: "zip",
+ });
+ fs.writeFileSync(output, Buffer.from(archive.data));
+ }
+ - name: Enforce recorded product, restore, security, and benchmark gates
+ run: |
+ mkdir -p gate-input/light gate-input/heavy gate-artifacts
+ unzip -q light.zip -d gate-input/light
+ unzip -q heavy.zip -d gate-input/heavy
+ python scripts/assemble_release_artifacts.py --light gate-input/light --heavy gate-input/heavy --output gate-artifacts
+ python scripts/check_release_readiness.py gate-artifacts
+ - uses: actions/upload-artifact@v4
+ with:
+ name: release-evidence-${{ github.ref_name }}
+ path: gate-artifacts/
publish:
runs-on: ubuntu-latest
- needs: validate-tag
+ needs: [validate-tag, verify-required-checks]
steps:
- uses: actions/checkout@v4
- - uses: docker/setup-qemu-action@v3
- - uses: docker/setup-buildx-action@v3
+ - uses: actions/download-artifact@v4
+ with:
+ name: release-evidence-${{ github.ref_name }}
+ path: gate-artifacts
+ - name: Load and verify the gated image
+ run: |
+ gzip -dc gate-artifacts/brainapi-image.tar.gz | docker load
+ expected=$(python -c 'import json; print(json.load(open("gate-artifacts/security.json"))["image_digest"])')
+ actual=$(docker image inspect brainapi:ci --format '{{.Id}}')
+ test "$actual" = "$expected"
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- - id: meta
- uses: docker/metadata-action@v5
- with:
- images: ghcr.io/lumen-labs/brainapi
- tags: |
- type=ref,event=tag
- - id: build
- uses: docker/build-push-action@v6
- with:
- context: .
- platforms: linux/amd64
- push: true
- tags: ${{ steps.meta.outputs.tags }}
- labels: ${{ steps.meta.outputs.labels }}
- build-args: |
- BUILD_DATE=${{ github.event.head_commit.timestamp }}
- BUILD_SHA=${{ github.sha }}
- provenance: mode=max
- sbom: true
- cache-from: type=gha
- cache-to: type=gha,mode=max
- - run: echo '${{ steps.build.outputs.digest }}' > image-digest.txt
+ - id: publish
+ name: Publish the unchanged validated image
+ run: |
+ target="ghcr.io/lumen-labs/brainapi:$GITHUB_REF_NAME"
+ docker tag brainapi:ci "$target"
+ docker push "$target"
+ digest=$(docker image inspect "$target" --format '{{index .RepoDigests 0}}' | cut -d@ -f2)
+ test -n "$digest"
+ echo "$digest" > image-digest.txt
- uses: actions/upload-artifact@v4
with:
name: release-image-digest-${{ needs.validate-tag.outputs.version }}
diff --git a/deploy/README.md b/deploy/README.md
index 212997e..a9d3d57 100644
--- a/deploy/README.md
+++ b/deploy/README.md
@@ -60,7 +60,10 @@ nginx server file; do not expose port 80 beyond a trusted proxy without HTTPS.
## Backup and restore
Backups default to `/srv/brainapi/backups` and are created with mode `0700`.
-They contain checksummed dumps and stopped-volume archives, but never secrets.
+They contain sensitive application data (including stored brain credentials),
+so encrypt and access-control the backup directory. The generated manifest
+contains only profile/version metadata, service names, restore order, and
+checksums; it never contains secrets.
```bash
deploy/brainapi-backup backup --profile light \
@@ -74,7 +77,29 @@ deploy/brainapi-backup restore --profile light \
Backup stops nginx, API, MCP, and workers before capture. Restore refuses a
running stack, a profile/image mismatch, a checksum mismatch, or any non-empty
target volume. Use a documented maintenance window and retain the generated
-manifest with the release artifacts.
+manifest with the release artifacts. Light backups include the system registry
+plus an individual custom-format dump for every `brain_*` PostgreSQL database.
+
+## Production validation
+
+The CI-only Compose override supplies a deterministic OpenAI-compatible model
+stub; it is not a production deployment file. It lets both profiles exercise
+the complete storage and gateway path without external model credentials:
+
+```bash
+docker compose -f deploy/docker-compose.light.yaml \
+ -f deploy/docker-compose.ci.yaml up -d --wait
+python scripts/production_smoke.py exercise --profile light \
+ --system-token "$BRAINPAT_TOKEN" --artifact-dir release-artifacts
+```
+
+After backup, clean-volume restore, and restart, run the same tool with
+`verify-restore`. CI retains the profile smoke, latency, restore, backup
+manifest, image audit, and SBOM evidence. The tag workflow downloads the exact
+commit's successful light and heavy artifacts and runs the release-readiness
+gate before any image can be published. Quality CI exports one `linux/amd64`
+candidate archive; the heavy runner loads that same archive, and the tag
+workflow publishes it unchanged after verifying its recorded image ID.
## Public documentation sandbox
diff --git a/deploy/backup_restore.py b/deploy/backup_restore.py
index 09ca16d..e371dd7 100755
--- a/deploy/backup_restore.py
+++ b/deploy/backup_restore.py
@@ -73,12 +73,20 @@ def _load_env(path: Path) -> dict[str, str]:
class Stack:
- def __init__(self, profile: str, env_file: Path, project: str, dry_run: bool):
+ def __init__(
+ self,
+ profile: str,
+ env_file: Path,
+ project: str,
+ dry_run: bool,
+ compose_files: list[Path] | None = None,
+ ):
self.profile = profile
self.env_file = env_file.resolve()
self.project = project
self.dry_run = dry_run
- self.compose_file = DEPLOY / f"docker-compose.{profile}.yaml"
+ self.compose_files = [DEPLOY / f"docker-compose.{profile}.yaml"]
+ self.compose_files.extend(path.resolve() for path in (compose_files or []))
self.env = _load_env(self.env_file)
self.env["BRAINAPI_ENV_FILE"] = str(self.env_file)
self.base = [
@@ -88,9 +96,9 @@ class Stack:
project,
"--env-file",
str(self.env_file),
- "-f",
- str(self.compose_file),
]
+ for compose_file in self.compose_files:
+ self.base.extend(["-f", str(compose_file)])
def run(
self,
@@ -159,6 +167,82 @@ def _write_stream(stack: Stack, args: list[str], destination: Path) -> None:
stack.run(args, stdout=output)
+def _dump_postgres_brain_databases(stack: Stack, destination: Path) -> None:
+ script = r'''
+set -eu
+tmp="$(mktemp -d)"
+trap 'rm -rf "$tmp"' EXIT
+psql -U "$POSTGRES_USER" -d postgres -Atc \
+ "SELECT datname FROM pg_database WHERE datistemplate = false AND left(datname, 6) = 'brain_' ORDER BY datname" |
+while IFS= read -r database; do
+ test -n "$database" || continue
+ pg_dump -U "$POSTGRES_USER" -d "$database" --format=custom --file="$tmp/$database.dump"
+done
+tar -C "$tmp" -czf - .
+'''.strip()
+ _write_stream(
+ stack,
+ ["exec", "-T", "postgres", "sh", "-c", script],
+ destination,
+ )
+
+
+def _restore_postgres_brain_databases(
+ stack: Stack, archive: Path, pg_user: str
+) -> None:
+ with tempfile.TemporaryDirectory(prefix="brainapi-postgres-restore-") as tmp:
+ tmp_path = Path(tmp)
+ if not stack.dry_run:
+ subprocess.run(
+ ["tar", "-C", str(tmp_path), "-xzf", str(archive)], check=True
+ )
+ dumps = sorted(tmp_path.glob("brain_*.dump"))
+ if stack.dry_run:
+ dumps = [tmp_path / "brain_example.dump"]
+ for dump in dumps:
+ database = dump.stem
+ if not database.replace("_", "").isalnum() or not database.startswith(
+ "brain_"
+ ):
+ raise RuntimeError(f"Invalid PostgreSQL brain dump name: {dump.name}")
+ stack.run(
+ ["exec", "-T", "postgres", "createdb", "-U", pg_user, database],
+ check=False,
+ )
+ if stack.dry_run:
+ stack.run(
+ [
+ "exec",
+ "-T",
+ "postgres",
+ "pg_restore",
+ "-U",
+ pg_user,
+ "-d",
+ database,
+ "--clean",
+ "--if-exists",
+ ]
+ )
+ else:
+ with dump.open("rb") as source:
+ stack.run(
+ [
+ "exec",
+ "-T",
+ "postgres",
+ "pg_restore",
+ "-U",
+ pg_user,
+ "-d",
+ database,
+ "--clean",
+ "--if-exists",
+ ],
+ stdin=source,
+ )
+
+
def _archive_volume(stack: Stack, key: str, destination: Path) -> None:
if stack.dry_run:
stack.docker(["run", "--rm", "-v", f"{stack.volume_name(key)}:/source:ro", ARCHIVER_IMAGE, "tar", "-C", "/source", "-czf", "-", "."])
@@ -261,6 +345,9 @@ def backup(stack: Stack, backup_root: Path) -> Path:
["exec", "-T", "postgres", "pg_dump", "-U", pg_user, "-d", pg_db, "--format=custom"],
destination / "postgres.dump",
)
+ _dump_postgres_brain_databases(
+ stack, destination / "postgres-brains.tar.gz"
+ )
stack.run(["exec", "-T", "redis", "sh", "-c", 'redis-cli -a "$REDIS_PASSWORD" SAVE'])
stack.run(["stop", "redis", "postgres"])
else:
@@ -347,6 +434,9 @@ def restore(stack: Stack, backup_dir: Path) -> None:
else:
with (backup_dir / "postgres.dump").open("rb") as source:
stack.run(["exec", "-T", "postgres", "pg_restore", "-U", pg_user, "-d", pg_db, "--clean", "--if-exists"], stdin=source)
+ _restore_postgres_brain_databases(
+ stack, backup_dir / "postgres-brains.tar.gz", pg_user
+ )
else:
with tempfile.TemporaryDirectory(prefix="brainapi-neo4j-restore-") as tmp:
tmp_path = Path(tmp)
@@ -372,6 +462,13 @@ def _parser() -> argparse.ArgumentParser:
parser.add_argument("--project-name", default="brainapi")
parser.add_argument("--backup-dir", type=Path, default=Path(os.getenv("BACKUP_DIR", "/srv/brainapi/backups")))
parser.add_argument("--archive", type=Path, help="Backup directory for verify/restore")
+ parser.add_argument(
+ "--compose-file",
+ action="append",
+ default=[],
+ type=Path,
+ help="Additional Compose override file; may be repeated.",
+ )
parser.add_argument("--dry-run", action="store_true")
return parser
@@ -385,7 +482,13 @@ def main() -> int:
verify_backup(args.archive.resolve(), args.profile)
print(f"Verified {args.archive}")
return 0
- stack = Stack(args.profile, args.env_file, args.project_name, args.dry_run)
+ stack = Stack(
+ args.profile,
+ args.env_file,
+ args.project_name,
+ args.dry_run,
+ args.compose_file,
+ )
if args.command == "backup":
destination = backup(stack, args.backup_dir)
print(destination)
diff --git a/deploy/docker-compose.ci.yaml b/deploy/docker-compose.ci.yaml
new file mode 100644
index 0000000..253bb58
--- /dev/null
+++ b/deploy/docker-compose.ci.yaml
@@ -0,0 +1,42 @@
+# CI-only provider override. This file is never part of a production deployment.
+x-ci-model-environment: &ci-model-environment
+ BRAINPAT_TOKEN: ${BRAINPAT_TOKEN:?BRAINPAT_TOKEN is required}
+ LLM_SMALL_PROVIDER: openai
+ LLM_LARGE_PROVIDER: openai
+ EMBEDDINGS_PROVIDER: openai
+ OPENAI_API_KEY: ci-isolated-token
+ OPENAI_BASE_URL: http://embedding-stub:8080/v1
+ SEARCH_ENABLED: "true"
+ SEARCH_USE_DENSE: "true"
+ SEARCH_USE_BM25: "false"
+ CONTEXT_PASSAGE_MODE: dense
+
+services:
+ embedding-stub:
+ image: python:3.11.14-slim-bookworm
+ command: [python, /stub.py]
+ volumes:
+ - ../scripts/openai_ci_stub.py:/stub.py:ro
+ networks: [appnet]
+ read_only: true
+ tmpfs: [/tmp]
+ healthcheck:
+ test: [CMD, python, -c, "import urllib.request; urllib.request.urlopen('http://localhost:8080/health')"]
+ interval: 2s
+ timeout: 2s
+ retries: 10
+
+ brainapi:
+ environment: *ci-model-environment
+ depends_on:
+ embedding-stub: {condition: service_healthy}
+
+ brainapi-worker:
+ environment: *ci-model-environment
+ depends_on:
+ embedding-stub: {condition: service_healthy}
+
+ brainapi-mcp:
+ environment: *ci-model-environment
+ depends_on:
+ embedding-stub: {condition: service_healthy}
diff --git a/deploy/release-artifacts/README.md b/deploy/release-artifacts/README.md
index ada05c5..c743f56 100644
--- a/deploy/release-artifacts/README.md
+++ b/deploy/release-artifacts/README.md
@@ -17,3 +17,10 @@ must contain the following files produced against the exact candidate image:
The public ledger must also contain a representative LongMemEval result and a
WANDS representative Search row. LoCoMo and BEAM rows retain their explicit
sample scopes and are not promoted into broader claims by this gate.
+
+`scripts/production_smoke.py exercise` produces profile-scoped smoke, latency,
+and pre-restore state evidence; `verify-restore` produces the matching restore
+report. `scripts/assemble_release_artifacts.py` combines successful light and
+heavy workflow artifacts. The tag-only publish workflow performs this assembly
+and runs the gate itself, in addition to requiring all quality and dedicated
+heavy-runner checks on the tagged commit.
diff --git a/scripts/assemble_release_artifacts.py b/scripts/assemble_release_artifacts.py
new file mode 100644
index 0000000..5de33c8
--- /dev/null
+++ b/scripts/assemble_release_artifacts.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+"""Combine light and heavy workflow evidence into one release-gate directory."""
+
+from __future__ import annotations
+
+import argparse
+import json
+import shutil
+from pathlib import Path
+
+
+def _find(root: Path, name: str) -> Path:
+ matches = list(root.rglob(name))
+ if len(matches) != 1:
+ raise RuntimeError(f"Expected one {name} under {root}, found {len(matches)}")
+ return matches[0]
+
+
+def _merge_profiles(light: Path, heavy: Path, name: str) -> dict:
+ left = json.loads(_find(light, name).read_text(encoding="utf-8"))
+ right = json.loads(_find(heavy, name).read_text(encoding="utf-8"))
+ if set(left) != {"light"} or set(right) != {"heavy"}:
+ raise RuntimeError(f"Unexpected profile keys in {name}")
+ return {**left, **right}
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--light", type=Path, required=True)
+ parser.add_argument("--heavy", type=Path, required=True)
+ parser.add_argument("--output", type=Path, required=True)
+ args = parser.parse_args()
+ output = args.output.resolve()
+ output.mkdir(parents=True, exist_ok=True)
+ for name in ("smoke.json", "latency.json"):
+ merged = _merge_profiles(args.light, args.heavy, name)
+ (output / name).write_text(
+ json.dumps(merged, indent=2, sort_keys=True) + "\n", encoding="utf-8"
+ )
+ for root, names in (
+ (
+ args.light,
+ (
+ "restore-light.json",
+ "security.json",
+ "brainapi.spdx.json",
+ "backup-light-manifest.json",
+ "brainapi-image.tar.gz",
+ ),
+ ),
+ (args.heavy, ("restore-heavy.json", "backup-heavy-manifest.json")),
+ ):
+ for name in names:
+ shutil.copy2(_find(root, name), output / name)
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/scripts/check_release_readiness.py b/scripts/check_release_readiness.py
index bb804b1..9d23b52 100644
--- a/scripts/check_release_readiness.py
+++ b/scripts/check_release_readiness.py
@@ -57,19 +57,31 @@ def _check_smoke(data: dict) -> None:
def _check_latency(data: dict) -> None:
+ if "light" in data or "heavy" in data:
+ for profile in ("light", "heavy"):
+ if profile not in data:
+ raise RuntimeError(f"Latency results are missing the {profile} profile")
+ _check_latency_result(profile, data[profile])
+ return
+ _check_latency_result("release", data)
+
+
+def _check_latency_result(profile: str, data: dict) -> None:
context = data.get("context", {})
search = data.get("search", {})
if float(context.get("p50_ms", float("inf"))) >= 1000:
- raise RuntimeError("/retrieve/context p50 must be below 1000 ms")
+ raise RuntimeError(f"{profile} /retrieve/context p50 must be below 1000 ms")
if int(context.get("online_llm_retrieval_loops", -1)) != 0:
- raise RuntimeError("Context retrieval must not run an online LLM loop")
+ raise RuntimeError(f"{profile} context retrieval must not run an online LLM loop")
if float(search.get("p50_ms", float("inf"))) >= 200:
- raise RuntimeError("Default search p50 must be below 200 ms")
+ raise RuntimeError(f"{profile} default search p50 must be below 200 ms")
if search.get("excludes_embed_query") is not True:
- raise RuntimeError("Search latency must explicitly exclude embed.query")
+ raise RuntimeError(f"{profile} search latency must exclude embed.query")
for surface, result in (("context", context), ("search", search)):
if "p95_ms" not in result or "p99_ms" not in result:
- raise RuntimeError(f"{surface} must record non-blocking p95 and p99")
+ raise RuntimeError(
+ f"{profile} {surface} must record non-blocking p95 and p99"
+ )
def _check_restore(profile: str, data: dict) -> None:
diff --git a/scripts/openai_ci_stub.py b/scripts/openai_ci_stub.py
new file mode 100644
index 0000000..55e3154
--- /dev/null
+++ b/scripts/openai_ci_stub.py
@@ -0,0 +1,64 @@
+#!/usr/bin/env python3
+"""Minimal deterministic OpenAI-compatible server for isolated CI smoke tests."""
+
+from __future__ import annotations
+
+import hashlib
+import json
+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+
+
+def _embedding(text: str, dimensions: int = 3072) -> list[float]:
+ digest = hashlib.sha256(text.encode("utf-8")).digest()
+ values = [((digest[index % len(digest)] / 255.0) * 2.0) - 1.0 for index in range(dimensions)]
+ norm = sum(value * value for value in values) ** 0.5 or 1.0
+ return [value / norm for value in values]
+
+
+class Handler(BaseHTTPRequestHandler):
+ protocol_version = "HTTP/1.1"
+
+ def _json(self, status: int, body: dict) -> None:
+ encoded = json.dumps(body).encode("utf-8")
+ self.send_response(status)
+ self.send_header("Content-Type", "application/json")
+ self.send_header("Content-Length", str(len(encoded)))
+ self.end_headers()
+ self.wfile.write(encoded)
+
+ def do_GET(self) -> None: # noqa: N802
+ if self.path == "/health":
+ self._json(200, {"status": "ok"})
+ else:
+ self._json(404, {"error": "not_found"})
+
+ def do_POST(self) -> None: # noqa: N802
+ length = int(self.headers.get("Content-Length", "0"))
+ request = json.loads(self.rfile.read(length) or b"{}")
+ if self.path.rstrip("/").endswith("/embeddings"):
+ inputs = request.get("input", [])
+ if isinstance(inputs, str):
+ inputs = [inputs]
+ dimensions = int(request.get("dimensions") or 3072)
+ data = [
+ {"object": "embedding", "index": index, "embedding": _embedding(str(text), dimensions)}
+ for index, text in enumerate(inputs)
+ ]
+ self._json(
+ 200,
+ {
+ "object": "list",
+ "model": request.get("model", "ci-embedding"),
+ "data": data,
+ "usage": {"prompt_tokens": 0, "total_tokens": 0},
+ },
+ )
+ return
+ self._json(404, {"error": {"message": "unsupported CI stub endpoint"}})
+
+ def log_message(self, format: str, *args) -> None:
+ return
+
+
+if __name__ == "__main__":
+ ThreadingHTTPServer(("0.0.0.0", 8080), Handler).serve_forever()
diff --git a/scripts/production_smoke.py b/scripts/production_smoke.py
new file mode 100644
index 0000000..6be9756
--- /dev/null
+++ b/scripts/production_smoke.py
@@ -0,0 +1,375 @@
+#!/usr/bin/env python3
+"""Exercise a running production profile and record release-gate artifacts."""
+
+from __future__ import annotations
+
+import argparse
+import json
+import math
+import time
+import urllib.error
+import urllib.request
+from pathlib import Path
+from typing import Any
+
+
+TERMINAL_TASK_STATES = {"completed", "failed", "partial_failed"}
+COUNT_PATHS = {
+ "nodes": "/retrieve/entities?limit=1",
+ "edges": "/retrieve/relationships?limit=1",
+ "chunks": "/retrieve/text-chunks?limit=1",
+ "observations": "/retrieve/observations?limit=1",
+}
+
+
+class Client:
+ def __init__(self, base_url: str, system_token: str, timeout: float = 60.0):
+ self.base_url = base_url.rstrip("/")
+ self.system_token = system_token
+ self.timeout = timeout
+ self.unexpected_5xx = 0
+
+ def request(
+ self,
+ method: str,
+ path: str,
+ *,
+ token: str | None = None,
+ brain_id: str | None = None,
+ body: dict[str, Any] | None = None,
+ expected: set[int] | None = None,
+ ) -> tuple[int, Any, float]:
+ headers = {"Accept": "application/json"}
+ if token:
+ headers["Authorization"] = f"Bearer {token}"
+ if brain_id:
+ headers["X-Brain-ID"] = brain_id
+ payload = None
+ if body is not None:
+ payload = json.dumps(body).encode("utf-8")
+ headers["Content-Type"] = "application/json"
+ request = urllib.request.Request(
+ f"{self.base_url}{path}", data=payload, headers=headers, method=method
+ )
+ started = time.perf_counter()
+ try:
+ with urllib.request.urlopen(request, timeout=self.timeout) as response:
+ status = response.status
+ raw = response.read()
+ content_type = response.headers.get("Content-Type", "")
+ except urllib.error.HTTPError as exc:
+ status = exc.code
+ raw = exc.read()
+ content_type = exc.headers.get("Content-Type", "")
+ elapsed_ms = (time.perf_counter() - started) * 1000
+ if status >= 500 and (expected is None or status not in expected):
+ self.unexpected_5xx += 1
+ if expected is not None and status not in expected:
+ detail = raw.decode("utf-8", errors="replace")[:1000]
+ raise RuntimeError(f"{method} {path} returned {status}: {detail}")
+ if "json" in content_type:
+ result = json.loads(raw or b"null")
+ else:
+ result = raw.decode("utf-8", errors="replace")
+ return status, result, elapsed_ms
+
+
+def _percentile(values: list[float], percentile: float) -> float:
+ ordered = sorted(values)
+ if not ordered:
+ raise RuntimeError("Cannot calculate a percentile without samples")
+ position = (len(ordered) - 1) * percentile
+ lower = math.floor(position)
+ upper = math.ceil(position)
+ if lower == upper:
+ return round(ordered[lower], 3)
+ value = ordered[lower] + (ordered[upper] - ordered[lower]) * (position - lower)
+ return round(value, 3)
+
+
+def _latency_summary(samples: list[float]) -> dict[str, float | int]:
+ return {
+ "samples": len(samples),
+ "p50_ms": _percentile(samples, 0.50),
+ "p95_ms": _percentile(samples, 0.95),
+ "p99_ms": _percentile(samples, 0.99),
+ }
+
+
+def _write_merged(path: Path, key: str, value: dict[str, Any]) -> None:
+ existing = json.loads(path.read_text()) if path.is_file() else {}
+ existing[key] = value
+ path.write_text(json.dumps(existing, indent=2, sort_keys=True) + "\n")
+
+
+def _wait_for_task(
+ client: Client, task_id: str, brain_id: str, token: str, timeout: float
+) -> dict[str, Any]:
+ deadline = time.monotonic() + timeout
+ while time.monotonic() < deadline:
+ status, result, _ = client.request(
+ "GET",
+ f"/tasks/{task_id}",
+ token=token,
+ brain_id=brain_id,
+ expected={200, 404},
+ )
+ if status == 200 and result.get("status") in TERMINAL_TASK_STATES:
+ if result["status"] != "completed":
+ raise RuntimeError(f"Task {task_id} ended as {result['status']}: {result}")
+ return result
+ time.sleep(1)
+ raise RuntimeError(f"Task {task_id} did not complete within {timeout:g}s")
+
+
+def _stage_ms(report: dict[str, Any] | None, name: str) -> float:
+ for stage in (report or {}).get("stages", []):
+ if stage.get("stage") == name:
+ return float(stage.get("wall_ms", 0))
+ raise RuntimeError(f"Profiled response did not include {name}")
+
+
+def _online_llm_stages(report: dict[str, Any] | None) -> int:
+ return sum(
+ 1
+ for stage in (report or {}).get("stages", [])
+ if "llm" in str(stage.get("stage", "")).lower()
+ )
+
+
+def _capture_counts(client: Client, brain_id: str, token: str) -> dict[str, int]:
+ _, brains, _ = client.request(
+ "GET", "/system/brains-list", token=client.system_token, expected={200}
+ )
+ counts = {"brains": len(brains)}
+ for key, path in COUNT_PATHS.items():
+ _, result, _ = client.request(
+ "GET", path, token=token, brain_id=brain_id, expected={200}
+ )
+ counts[key] = int(result.get("total", result.get("count", 0)))
+ _, stores, _ = client.request(
+ "GET", "/retrieve/vectors/stores", token=token, brain_id=brain_id, expected={200}
+ )
+ vector_count = 0
+ for store in stores.get("stores", []):
+ name = store["name"]
+ _, vectors, _ = client.request(
+ "GET",
+ f"/retrieve/vectors/{name}?limit=1",
+ token=token,
+ brain_id=brain_id,
+ expected={200},
+ )
+ vector_count += int(vectors.get("total", 0))
+ counts["vectors"] = vector_count
+ return counts
+
+
+def _search_check(client: Client, brain_id: str, token: str, query: str) -> dict[str, Any]:
+ _, response, _ = client.request(
+ "POST",
+ "/retrieve/search",
+ token=token,
+ brain_id=brain_id,
+ body={
+ "query": query,
+ "brain_id": brain_id,
+ "k": 5,
+ "channels": ["passages"],
+ "profile_stages": True,
+ },
+ expected={200},
+ )
+ snippets = [str(hit.get("snippet", "")) for hit in response.get("hits", [])]
+ return {"query": query, "match": any(query.lower() in item.lower() for item in snippets)}
+
+
+def exercise(args: argparse.Namespace) -> None:
+ artifacts = args.artifact_dir.resolve()
+ artifacts.mkdir(parents=True, exist_ok=True)
+ client = Client(args.base_url, args.system_token, args.request_timeout)
+ brain_id = args.brain_id or f"productiongate{int(time.time())}"
+ markers = [f"releaseproof{i:02d}x" for i in range(10)]
+ flows: dict[str, bool] = {}
+
+ client.request(
+ "GET",
+ "/system/brains-list",
+ token="malformed-token",
+ expected={401},
+ )
+ flows["authentication"] = True
+
+ _, brain, _ = client.request(
+ "POST",
+ "/system/brains",
+ token=args.system_token,
+ body={"brain_id": brain_id},
+ expected={200},
+ )
+ brain_token = brain["pat"]
+ flows["brain_creation"] = brain.get("name_key") == brain_id
+
+ source_text = "BrainAPI deterministic production proof: " + " ".join(markers)
+ _, accepted, _ = client.request(
+ "POST",
+ "/ingest/",
+ token=brain_token,
+ brain_id=brain_id,
+ body={
+ "brain_id": brain_id,
+ "data": {"data_type": "text", "text_data": source_text},
+ "skip_enrichment": True,
+ },
+ expected={202},
+ )
+ _wait_for_task(client, accepted["task_id"], brain_id, brain_token, args.task_timeout)
+ flows["plain_ingestion"] = True
+
+ _, accepted, _ = client.request(
+ "POST",
+ "/ingest/structured",
+ token=brain_token,
+ brain_id=brain_id,
+ body={
+ "brain_id": brain_id,
+ "mode": "deterministic",
+ "data": [
+ {
+ "subject": {"name": "Release runner", "type": "AGENT"},
+ "subj_event": {
+ "name": "VALIDATED",
+ "description": "validated the release profile",
+ },
+ "object": {"name": "BrainAPI", "type": "PRODUCT"},
+ }
+ ],
+ },
+ expected={202},
+ )
+ _wait_for_task(client, accepted["task_id"], brain_id, brain_token, args.task_timeout)
+ flows["structured_ingestion"] = True
+ flows["task_completion"] = True
+
+ context_samples: list[float] = []
+ search_samples: list[float] = []
+ for index in range(args.warmups + args.samples):
+ _, context, context_ms = client.request(
+ "POST",
+ "/retrieve/context",
+ token=brain_token,
+ brain_id=brain_id,
+ body={
+ "text": markers[index % len(markers)],
+ "brain_id": brain_id,
+ "profile_stages": True,
+ "apply_fact_filter": False,
+ },
+ expected={200},
+ )
+ _, search, search_ms = client.request(
+ "POST",
+ "/retrieve/search",
+ token=brain_token,
+ brain_id=brain_id,
+ body={
+ "query": markers[index % len(markers)],
+ "brain_id": brain_id,
+ "k": 5,
+ "channels": ["passages"],
+ "profile_stages": True,
+ },
+ expected={200},
+ )
+ embed_ms = _stage_ms(search.get("stage_timings"), "embed.query")
+ if index >= args.warmups:
+ context_samples.append(context_ms)
+ search_samples.append(max(0.0, search_ms - embed_ms))
+ flows["context_retrieval"] = bool(context.get("text_context") is not None)
+ flows["search"] = bool(search.get("hits"))
+
+ _, console, _ = client.request("GET", "/console/", expected={200})
+ flows["console"] = "<html" in console.lower() or "<!doctype" in console.lower()
+ _, mcp, _ = client.request("GET", "/mcp/info", expected={200})
+ flows["mcp"] = mcp.get("service") == "brainapi-mcp"
+
+ checks = [_search_check(client, brain_id, brain_token, marker) for marker in markers]
+ if not all(check["match"] for check in checks):
+ raise RuntimeError("One or more deterministic retrieval spot checks failed")
+ if not all(flows.values()):
+ raise RuntimeError(f"Incomplete product flows: {flows}")
+ if client.unexpected_5xx:
+ raise RuntimeError(f"Smoke observed {client.unexpected_5xx} unexpected 5xx responses")
+
+ _write_merged(
+ artifacts / "smoke.json",
+ args.profile,
+ {"flows": flows, "unexpected_5xx": client.unexpected_5xx},
+ )
+ latency = {
+ "context": {
+ **_latency_summary(context_samples),
+ "online_llm_retrieval_loops": _online_llm_stages(
+ context.get("stage_timings")
+ ),
+ },
+ "search": {**_latency_summary(search_samples), "excludes_embed_query": True},
+ }
+ _write_merged(artifacts / "latency.json", args.profile, latency)
+ state = {
+ "profile": args.profile,
+ "brain_id": brain_id,
+ "before": _capture_counts(client, brain_id, brain_token),
+ "retrieval_spot_checks": checks,
+ }
+ (artifacts / f"state-{args.profile}.json").write_text(
+ json.dumps(state, indent=2, sort_keys=True) + "\n"
+ )
+ print(brain_id)
+
+
+def verify_restore(args: argparse.Namespace) -> None:
+ artifacts = args.artifact_dir.resolve()
+ state_path = artifacts / f"state-{args.profile}.json"
+ state = json.loads(state_path.read_text())
+ client = Client(args.base_url, args.system_token, args.request_timeout)
+ brain_id = state["brain_id"]
+ brain_token = args.system_token
+ checks = [
+ _search_check(client, brain_id, brain_token, item["query"])
+ for item in state["retrieval_spot_checks"][:10]
+ ]
+ result = {
+ "before": state["before"],
+ "after": _capture_counts(client, brain_id, brain_token),
+ "retrieval_spot_checks": checks,
+ }
+ output = artifacts / f"restore-{args.profile}.json"
+ output.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n")
+ if result["before"] != result["after"] or not all(item["match"] for item in checks):
+ raise RuntimeError(f"{args.profile} restore verification failed; see {output}")
+ print(output)
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("command", choices=("exercise", "verify-restore"))
+ parser.add_argument("--profile", required=True, choices=("light", "heavy"))
+ parser.add_argument("--base-url", default="http://localhost")
+ parser.add_argument("--system-token", required=True)
+ parser.add_argument("--artifact-dir", type=Path, required=True)
+ parser.add_argument("--brain-id")
+ parser.add_argument("--samples", type=int, default=10)
+ parser.add_argument("--warmups", type=int, default=2)
+ parser.add_argument("--task-timeout", type=float, default=300)
+ parser.add_argument("--request-timeout", type=float, default=60)
+ args = parser.parse_args()
+ if args.command == "exercise":
+ exercise(args)
+ else:
+ verify_restore(args)
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/scripts/write_security_artifact.py b/scripts/write_security_artifact.py
new file mode 100644
index 0000000..993b506
--- /dev/null
+++ b/scripts/write_security_artifact.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+"""Summarize an exact image's Trivy scan, digest, and SBOM for release gates."""
+
+from __future__ import annotations
+
+import argparse
+import json
+import subprocess
+from pathlib import Path
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--trivy", type=Path, required=True)
+ parser.add_argument("--sbom", type=Path, required=True)
+ parser.add_argument("--image", required=True)
+ parser.add_argument("--output", type=Path, required=True)
+ args = parser.parse_args()
+
+ scan = json.loads(args.trivy.read_text(encoding="utf-8"))
+ counts = {"HIGH": 0, "CRITICAL": 0}
+ for result in scan.get("Results") or []:
+ for vulnerability in result.get("Vulnerabilities") or []:
+ severity = str(vulnerability.get("Severity", "")).upper()
+ if severity in counts:
+ counts[severity] += 1
+ digest = subprocess.run(
+ ["docker", "image", "inspect", args.image, "--format", "{{.Id}}"],
+ check=True,
+ capture_output=True,
+ text=True,
+ ).stdout.strip()
+ result = {
+ "high": counts["HIGH"],
+ "critical": counts["CRITICAL"],
+ "image_digest": digest,
+ "sbom": args.sbom.name,
+ }
+ args.output.parent.mkdir(parents=True, exist_ok=True)
+ args.output.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n")
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/tests/test_deploy_production_contract.py b/tests/test_deploy_production_contract.py
index 4380568..b2bcfa0 100644
--- a/tests/test_deploy_production_contract.py
+++ b/tests/test_deploy_production_contract.py
@@ -11,6 +11,9 @@ import pytest
import yaml
from deploy.backup_restore import verify_backup
+from scripts.check_release_readiness import _check_latency
+from scripts.openai_ci_stub import _embedding
+from scripts.production_smoke import _latency_summary
ROOT = Path(__file__).resolve().parents[1]
@@ -120,9 +123,81 @@ def test_backup_manifest_verification_detects_tampering(tmp_path):
verify_backup(tmp_path, "light")
+def test_light_backup_includes_per_brain_postgres_databases():
+ source = (DEPLOY / "backup_restore.py").read_text(encoding="utf-8")
+ assert "postgres-brains.tar.gz" in source
+ assert "brain_*.dump" in source
+
+
def test_recommendation_openapi_surface_is_preview():
from src.services.api.app import app
schema = app.openapi()
assert schema["paths"]["/retrieve/recommend"]["get"]["x-stability"] == "preview"
assert schema["paths"]["/retrieve/recommend"]["post"]["x-stability"] == "preview"
+
+
+def test_production_smoke_percentiles_are_deterministic():
+ assert _latency_summary([1, 2, 3, 4, 5]) == {
+ "samples": 5,
+ "p50_ms": 3,
+ "p95_ms": 4.8,
+ "p99_ms": 4.96,
+ }
+
+
+def test_product_state_artifact_never_persists_a_brain_token():
+ source = (ROOT / "scripts" / "production_smoke.py").read_text(encoding="utf-8")
+ state_block = source.split('state = {', 1)[1].split('}', 1)[0]
+ assert '"brain_token"' not in state_block
+
+
+def test_release_latency_gate_requires_both_profiles():
+ result = {
+ "context": {
+ "p50_ms": 999,
+ "p95_ms": 1200,
+ "p99_ms": 1500,
+ "online_llm_retrieval_loops": 0,
+ },
+ "search": {
+ "p50_ms": 199,
+ "p95_ms": 250,
+ "p99_ms": 300,
+ "excludes_embed_query": True,
+ },
+ }
+ _check_latency({"light": result, "heavy": result})
+ with pytest.raises(RuntimeError, match="missing the heavy"):
+ _check_latency({"light": result})
+
+
+def test_ci_embedding_stub_is_deterministic_and_normalized():
+ first = _embedding("same input", dimensions=8)
+ assert first == _embedding("same input", dimensions=8)
+ assert first != _embedding("different input", dimensions=8)
+ assert sum(value * value for value in first) == pytest.approx(1.0)
+
+
+def test_release_workflow_enforces_artifact_gate_before_publish():
+ workflow = (ROOT / ".github" / "workflows" / "release.yaml").read_text(
+ encoding="utf-8"
+ )
+ assert "check_release_readiness.py gate-artifacts" in workflow
+ assert "needs: [validate-tag, verify-required-checks]" in workflow
+
+
+def test_mcp_has_only_one_lifespan_definition():
+ source = (ROOT / "src" / "services" / "mcp" / "app.py").read_text(
+ encoding="utf-8"
+ )
+ assert source.count("async def _lifespan") == 1
+
+
+def test_ci_profiles_enable_dense_search_without_online_llm():
+ override = yaml.safe_load((DEPLOY / "docker-compose.ci.yaml").read_text())
+ environment = override["x-ci-model-environment"]
+ assert environment["SEARCH_ENABLED"] == "true"
+ assert environment["SEARCH_USE_DENSE"] == "true"
+ assert environment["SEARCH_USE_BM25"] == "false"
+ assert environment["CONTEXT_PASSAGE_MODE"] == "dense"