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.

perf(local-dev): speed up `flox activate` and dev server startup (#58947)

Tom Piccirello8a89f2c1cfd9
diff --git a/.flox/env/on-activate.sh b/.flox/env/on-activate.sh
index f4459e8fac99..4ed59b693745 100755
--- a/.flox/env/on-activate.sh
+++ b/.flox/env/on-activate.sh
@@ -144,6 +144,54 @@ run_step() {
fi
}
+# Compute sha256 of a file. Portable across Linux (sha256sum) and macOS
+# (shasum). Returns empty string when the file is missing or no hasher
+# is available; safe to call inside `[[ ... ]]` under set -euo pipefail.
+_sha256_file() {
+ local file="$1"
+ [[ -f "$file" ]] || return 0
+ local result=""
+ if command -v sha256sum >/dev/null 2>&1; then
+ result=$(sha256sum "$file" 2>/dev/null | awk '{print $1}' || true)
+ elif command -v shasum >/dev/null 2>&1; then
+ result=$(shasum -a 256 "$file" 2>/dev/null | awk '{print $1}' || true)
+ fi
+ printf '%s' "$result"
+}
+
+# Read an AMI-bake stamp file, stripping whitespace. Returns empty
+# string when the stamp is missing -- which is the expected case on
+# laptops and on workspaces booted from a non-baked image. Callers
+# treat empty as "no match" and fall back to running the full step.
+_read_ami_stamp() {
+ local file="$1"
+ [[ -f "$file" ]] || return 0
+ tr -d '[:space:]' < "$file" 2>/dev/null || true
+}
+
+# Waits for a step that was started earlier as a background subshell and
+# reports its outcome. Mirrors run_step's final status line without a
+# spinner -- the work has usually completed by the time wait is called.
+# Usage: wait_bg_step "Label" <pid> <start_epoch> <logfile>
+wait_bg_step() {
+ local label="$1"
+ local pid="$2"
+ local start_time="$3"
+ local tmpfile="$4"
+
+ local exit_code=0
+ wait "$pid" 2>/dev/null || exit_code=$?
+ local elapsed=$(( $(date +%s) - start_time ))
+
+ if [[ $exit_code -eq 0 ]]; then
+ printf " ${C_GREEN}✓${C_RESET} %-42s %3ds\n" "$label" "$elapsed"
+ else
+ printf " ${C_RED}✗${C_RESET} %-42s %3ds\n" "$label" "$elapsed"
+ _save_failure_log "$label" "$tmpfile"
+ return $exit_code
+ fi
+}
+
# Inline step (instant, no spinner)
done_step() {
local label="$1"
@@ -220,8 +268,62 @@ echo -e "\n${C_CYAN}PostHog dev${C_RESET} ${C_DIM}── ${_branch}${C_RESET}\n"
_activation_start=$(date +%s)
+# ── Steps 1, 1b, 2 (kicked off in parallel, with AMI cache-skip) ───
+# uv sync, pnpm install, and `make phrocs build` are independent -- none
+# of them reads or writes the other's outputs. Kick the two non-spinner
+# ones off in the background BEFORE foregrounding uv sync, so the wall
+# clock is bounded by the slowest single step instead of the sum. uv sync
+# stays foregrounded because the completion + man-page steps that follow
+# depend on the venv it populates, and because its run_step spinner remains
+# the user-visible progress indicator for activate.
+#
+# Each step also checks an AMI-bake stamp file (sha256 of the source-of-
+# truth input recorded at bake time): when the on-disk hash matches the
+# baked hash, the workspace is in the same state as the bake and the
+# subprocess would be a no-op, so we skip it entirely. On laptops or
+# pre-stamp workspaces the stamps are missing and the checks fall back
+# to running normally -- no regression.
+_PNPM_LOCK="$FLOX_ENV_PROJECT/pnpm-lock.yaml"
+_UV_LOCK="$FLOX_ENV_PROJECT/uv.lock"
+_PHROCS_BIN="$FLOX_ENV_PROJECT/tools/phrocs/dist/phrocs"
+
+_PNPM_BAKED=$(_read_ami_stamp /etc/posthog-coder-ami-pnpm-stamp)
+_UV_BAKED=$(_read_ami_stamp /etc/posthog-coder-ami-uv-stamp)
+_PHROCS_BAKED=$(_read_ami_stamp /etc/posthog-coder-ami-phrocs-stamp)
+
+_PNPM_CURRENT=$(_sha256_file "$_PNPM_LOCK")
+_UV_CURRENT=$(_sha256_file "$_UV_LOCK")
+_PHROCS_CURRENT=$(_sha256_file "$_PHROCS_BIN")
+
+_PNPM_SKIP=0
+[[ -n "$_PNPM_BAKED" && -n "$_PNPM_CURRENT" && "$_PNPM_BAKED" == "$_PNPM_CURRENT" ]] && _PNPM_SKIP=1
+_UV_SKIP=0
+[[ -n "$_UV_BAKED" && -n "$_UV_CURRENT" && "$_UV_BAKED" == "$_UV_CURRENT" ]] && _UV_SKIP=1
+_PHROCS_SKIP=0
+[[ -n "$_PHROCS_BAKED" && -n "$_PHROCS_CURRENT" && "$_PHROCS_BAKED" == "$_PHROCS_CURRENT" ]] && _PHROCS_SKIP=1
+
+if [[ "$_PNPM_SKIP" -eq 0 ]]; then
+ _BG_PNPM_LOG=$(mktemp)
+ _ACTIVATION_TMPFILES+=("$_BG_PNPM_LOG")
+ ( pnpm install ) >"$_BG_PNPM_LOG" 2>&1 &
+ _BG_PNPM_PID=$!
+ _BG_PNPM_START=$(date +%s)
+fi
+
+if [[ "$_PHROCS_SKIP" -eq 0 ]]; then
+ _BG_PHROCS_LOG=$(mktemp)
+ _ACTIVATION_TMPFILES+=("$_BG_PHROCS_LOG")
+ ( make -C "$FLOX_ENV_PROJECT/tools/phrocs" build ) >"$_BG_PHROCS_LOG" 2>&1 &
+ _BG_PHROCS_PID=$!
+ _BG_PHROCS_START=$(date +%s)
+fi
+
# ── Step 1: Python packages (must run before hogli — it needs Click) ─
-run_step "Python packages" uv sync
+if [[ "$_UV_SKIP" -eq 1 ]]; then
+ done_step "Python packages (cached)"
+else
+ run_step "Python packages" uv sync
+fi
# Expose hogli on PATH via the uv-managed venv
if [[ -d "$UV_PROJECT_ENVIRONMENT/bin" ]]; then
@@ -250,13 +352,21 @@ if [[ -d "$UV_PROJECT_ENVIRONMENT/bin" ]]; then
fi
# ── Step 1b: Build phrocs from source ─────────────────────────────
-run_step "Build phrocs" make -C "$FLOX_ENV_PROJECT/tools/phrocs" build
+if [[ "$_PHROCS_SKIP" -eq 1 ]]; then
+ done_step "Build phrocs (cached)"
+else
+ wait_bg_step "Build phrocs" "$_BG_PHROCS_PID" "$_BG_PHROCS_START" "$_BG_PHROCS_LOG"
+fi
if [[ -f "$FLOX_ENV_PROJECT/tools/phrocs/dist/phrocs" && -d "$UV_PROJECT_ENVIRONMENT/bin" ]]; then
ln -sf "$FLOX_ENV_PROJECT/tools/phrocs/dist/phrocs" "$UV_PROJECT_ENVIRONMENT/bin/phrocs"
fi
# ── Step 2: Node packages ──────────────────────────────────────────
-run_step "Node packages" pnpm install
+if [[ "$_PNPM_SKIP" -eq 1 ]]; then
+ done_step "Node packages (cached)"
+else
+ wait_bg_step "Node packages" "$_BG_PNPM_PID" "$_BG_PNPM_START" "$_BG_PNPM_LOG"
+fi
# ── Step 3: /etc/hosts ──────────────────────────────────────────────
POSTHOG_HOSTS="127.0.0.1 db redis7 kafka clickhouse clickhouse-coordinator objectstorage seaweedfs temporal # posthog"
diff --git a/bin/mprocs.yaml b/bin/mprocs.yaml
index 6b3affa17819..bfe6ca39635a 100755
--- a/bin/mprocs.yaml
+++ b/bin/mprocs.yaml
@@ -35,7 +35,7 @@ procs:
capability: pinned
backend:
- shell: 'uv sync --active && bin/wait-for-docker && ./bin/start-backend'
+ shell: 'bin/wait-for-docker && ./bin/start-backend'
ready_pattern: 'Listening at'
groups:
layer: Application
@@ -43,7 +43,7 @@ procs:
# no capability: always_required via intent-map.yaml
celery-worker:
- shell: 'uv sync --active && bin/wait-for-docker && ./bin/start-celery worker'
+ shell: 'bin/wait-for-docker && ./bin/start-celery worker'
capability: celery_workers
ready_pattern: 'celery@'
groups:
@@ -51,7 +51,7 @@ procs:
tech: Python
celery-beat:
- shell: 'uv sync --active && bin/wait-for-docker && ./bin/start-celery beat'
+ shell: 'bin/wait-for-docker && ./bin/start-celery beat'
capability: celery_scheduler
ready_pattern: 'celery beat'
groups:
@@ -158,7 +158,6 @@ procs:
webhook-s3-sink:
shell: |-
- uv sync --active && \
bin/wait-for-docker && \
python manage.py run_webhook_s3_sink --consumer-group dwh-webhook-sink-dev --health-port 8091
capability: warehouse_webhook_sink
diff --git a/bin/wait-for-docker b/bin/wait-for-docker
index 3a6166820b98..092514dd8214 100755
--- a/bin/wait-for-docker
+++ b/bin/wait-for-docker
@@ -166,7 +166,13 @@ while true; do
last_progress_log=$SECONDS
fi
- sleep 1
+ # Poll faster than 1 Hz so the convergence cost summed across the
+ # ~10 phrocs procs that all block on this script is bounded by docker
+ # daemon health-check cadence, not the polling cadence. Five times
+ # the poll rate trims ~3-5s off the slowest proc's ready-pattern wait
+ # without measurably loading dockerd (each iteration is a handful of
+ # cheap docker compose ps + docker inspect calls).
+ sleep 0.2
done
if [ "${#timed_out_services[@]}" -gt 0 ]; then