Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(tasks): harden image specs and prewarm pytest database (#92148)

Alessandro Pogliaghi6fddfa414f83
diff --git a/docs/published/handbook/engineering/ai/sandboxed-agents.md b/docs/published/handbook/engineering/ai/sandboxed-agents.md
index bc4b940720b7..d726d990b3c5 100644
--- a/docs/published/handbook/engineering/ai/sandboxed-agents.md
+++ b/docs/published/handbook/engineering/ai/sandboxed-agents.md
@@ -305,7 +305,9 @@ images retain their separate ten-minute, batched refresh fanout.
`python manage.py bake_dev_stack_image` triggers a bake manually and bypasses the flag.
Pointing an org's `default_custom_image` payload key at that name gives its VM runs warm docker
state and already-migrated databases, so a task-time `hogli start` only applies the migrations
-that landed since the last bake. The pnpm store and Playwright's Chromium are prewarmed too:
+that landed since the last bake. The snapshot clones the migrated Postgres database into
+`test_posthog`, so pytest's default `--reuse-db` path also applies only newer migrations.
+The pnpm store and Playwright's Chromium are prewarmed too:
`pnpm install --frozen-lockfile --prefer-offline` is a fast linking pass and browser installs
are no-ops. Build outputs (node_modules, Storybook dist, Vite/Turbo caches) are deliberately
not baked — the bake's checkout is deleted before the snapshot — so frontend builds always run
diff --git a/products/tasks/backend/logic/services/image_builder.py b/products/tasks/backend/logic/services/image_builder.py
index 3df6578ba78e..9431f5fac3e7 100644
--- a/products/tasks/backend/logic/services/image_builder.py
+++ b/products/tasks/backend/logic/services/image_builder.py
@@ -40,22 +40,25 @@ SPEC FORMAT (YAML):
apt_packages: # Debian package names, installed via apt-get
- postgresql-client
run_commands: # shell commands executed in order at image BUILD time
- - curl -fsSL https://example.com/install.sh | bash
+ - >-
+ curl -fsSL https://example.com/install.sh | bash
repo_setup_commands: # only when a repository is linked: run inside a fresh checkout of it at BUILD time
- - pnpm install --frozen-lockfile
+ - >-
+ pnpm install --frozen-lockfile
env: # environment variables baked into the image
MY_TOOL_HOME: /opt/my-tool
RULES:
- Verify before you promise: actually run installs/commands here in the sandbox to confirm they work, then record the working steps in the spec.
- Build-time commands must be non-interactive and idempotent. No `sudo` needed — builds run as root.
-- Every run_commands / repo_setup_commands entry must be a SINGLE line: each becomes one Dockerfile RUN instruction, so multi-line strings (backslash continuations, heredocs) break the build. Chain steps with `&&`.
+- Write every run_commands / repo_setup_commands entry as a YAML folded block scalar (`>-`) containing a SINGLE logical line. Each becomes one Dockerfile RUN instruction, so multi-line commands (backslash continuations, heredocs) break the build. Chain steps with `&&`.
- Prefer apt_packages over run_commands when a Debian package exists.
- Keep the spec minimal: only what the user asked for, no speculative extras.
- Never put secrets, tokens, or credentials in the spec — it is scanned and rejected if it exfiltrates data or weakens sandbox security.
- Download-and-execute steps must be pinned and verifiable: fetch install scripts and binaries from a tagged release or commit hash (never a mutable branch) and verify a checksum when the vendor publishes one — unpinned `curl | sh` fails the security scan.
- Services (databases, daemons) cannot run "in" an image; you can install them and note that they start at task time.
- If the user points you at a repository, you may clone it here to verify their app runs, but do NOT bake repository code into the spec — only its runtime dependencies.
+- After writing the spec, validate its YAML syntax with `uv run --no-project --with PyYAML python -c "import pathlib, yaml; yaml.safe_load(pathlib.Path('{spec_path}').read_text())"`. Fix every syntax error before telling the user to save and build.
- After each spec update, show the user the current spec and briefly confirm what changed.
- When the user is happy, tell them to press "Save & build" (available right here in this conversation, and in the Environments settings) to scan, build, and publish the image.
diff --git a/products/tasks/backend/sandbox/images/bake-posthog-dev-stack.sh b/products/tasks/backend/sandbox/images/bake-posthog-dev-stack.sh
index a0b99f17dfc2..bb70e26c2b29 100644
--- a/products/tasks/backend/sandbox/images/bake-posthog-dev-stack.sh
+++ b/products/tasks/backend/sandbox/images/bake-posthog-dev-stack.sh
@@ -270,6 +270,11 @@ source .venv/bin/activate
# migrations have created posthog_instancesetting (the same race bin/mprocs.yaml gates
# with wait-for-postgres-tables).
bin/migrate --scope=postgres --scope=persons
+log "cloning migrated Postgres database for pytest"
+PGPASSWORD="${POSTGRES_PASSWORD:-posthog}" psql -h db -U posthog -d postgres -v ON_ERROR_STOP=1 \
+ -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'posthog' AND pid <> pg_backend_pid()" \
+ -c "DROP DATABASE IF EXISTS test_posthog WITH (FORCE)" \
+ -c "CREATE DATABASE test_posthog TEMPLATE posthog"
bin/migrate --scope=clickhouse
log "running rust-driven migrations"
diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.py
index 3460a0b484b8..8a13476bf0b5 100644
--- a/products/tasks/backend/tests/test_api.py
+++ b/products/tasks/backend/tests/test_api.py
@@ -12882,8 +12882,12 @@ class TestSandboxCustomImageAPI(BaseTaskAPITest):
self.assertEqual(state["runtime_adapter"], "claude")
self.assertEqual(state["model"], "zai-org/glm-5.3-flash")
self.assertEqual(state["reasoning_effort"], "high")
- self.assertIn("image-spec.yaml", state["pending_user_message"])
- self.assertIn("install pytorch and flox", state["pending_user_message"])
+ message = state["pending_user_message"]
+ self.assertIn("image-spec.yaml", message)
+ self.assertIn("run_commands: # shell commands executed in order at image BUILD time\n - >-", message)
+ self.assertIn("validate its YAML syntax", message)
+ self.assertIn("uv run --no-project --with PyYAML", message)
+ self.assertIn("install pytorch and flox", message)
@parameterized.expand(
[