Skip to content

fix(test): bring up the test database from the repo, not the environment - #674

Merged
Ardnived merged 1 commit into
mainfrom
claude/ensure-test-db
Sep 2, 2026
Merged

Ardnived merged 1 commit into
mainfrom
claude/ensure-test-db

Conversation

@Ardnived

@Ardnived Ardnived commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • scripts/ensure-test-db.sh brings up the integration lane's PostgreSQL from the repo, not the Claude routine environment
  • Wired into worktreeSetup and the top of the lean gate; idempotent and best-effort, so it is a no-op locally and can never fail a session

Why the repo and not the environment

The integration lane has been unavailable to autonomous runs for three days, and each investigation found a different cause:

Attempt Cause
1 Setup script exited non-zero — the session aborted at zero turns, no journal, nothing
2 Cluster was healthy; psql as root answers FATAL: role "root" does not exist — an auth error indistinguishable from a refused server, misread as "no PostgreSQL"
3 Data directory present, socket present, no server process — the environment's setup script is cached between containers and had not run

The third is why this moves into git: an environment setup script cannot be relied on to reflect its own latest edit. A script in the repo runs at whatever version the session checked out.

Behaviour

  • Already serving → exits immediately. Every macOS developer with Postgres.app, Homebrew or Docker is untouched
  • Not a root Linux sandbox → prints what to do and exits 0. No bring-up attempted
  • Stale socket whose lock names a dead PID → cleared. A warm container leaves this behind, and it makes a fresh start look like a live server
  • Creates a root superuser role, removing the auth error behind misdiagnosis Ticket #2: Narrator Collection Implementation #2
  • Never fails its caller. A database it cannot start degrades the gate; it does not abort the run

Test results

  • Lean gate green with the hook in place: lint ✓ typecheck ✓ typecheck:tests ✓ 118 files / 1539 unit tests ✓, 36s
  • bash -n clean on both changed scripts
  • Verified the no-op path on macOS: exits 0 silently against a running local Postgres

Notes for reviewer

docs/rules/testing-reqs.md records all three misdiagnoses, because the lesson generalises: when the lane is unavailable, name the layer that refused — process, socket, auth, or database — rather than reasoning about why. Each of the three looked like "no database" and only one was.

The integration lane has been unavailable to autonomous runs for three days,
and each investigation found a different cause: a setup script that exited
non-zero and killed the session at zero turns; a healthy cluster misdiagnosed
as absent because psql as root answers FATAL: role "root" does not exist, an
auth error indistinguishable from a refused server; and finally a container
holding the data directory and a stale socket with no server process, because
the environment's setup script is cached between containers and had not run.

That last one is the reason this moves into the repo. An environment setup
script cannot be relied on to reflect its own latest edit, whereas a script in
git runs at whatever version the session checked out.

scripts/ensure-test-db.sh is idempotent and best-effort. It exits immediately
when 5432 already answers, so a developer's own PostgreSQL is untouched and
macOS is unaffected; the bring-up path is guarded to a root Linux sandbox. It
clears a stale socket whose lock names a dead PID, which a warm container
leaves behind and which makes a fresh start look like a live server. It also
creates a root superuser role, removing the auth error that caused the second
misdiagnosis.

Wired into worktreeSetup and the top of the lean gate. The cost when the
database is already up is one pg_isready.

Co-Authored-By: Claude Opus 5 <[email protected]>
@railway-app

railway-app Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-674 environment in sahajcloud

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Sep 2, 2026 at 3:01 pm UTC
1 service not affected by this PR
  • mailpit

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-674 September 2, 2026 14:58 Destroyed
@sydevs-bot

Copy link
Copy Markdown
Contributor

ensure-test-db.sh breaks the cluster it is about to start, in exactly the warm-sandbox case it was written for. One-line fix: drop "$PGDATA" from the install -d argument list, or follow it with chmod 0700 "$PGDATA".

Line 68 runs unconditionally, before the PG_VERSION check:

install -d -o postgres -g postgres /var/run/postgresql "$(dirname "$PGDATA")" "$PGDATA"

install -d applies its mode (default 0755) to a directory that already exists. PostgreSQL refuses to start on a data directory that is not 0700 or 0750, so on a container where initdb has already run — the third of the three diagnoses your header comment names — the script sets 0755, pg_ctl start fails, and it reports "cluster did not come up" for a cluster that was fine until line 68 touched it.

I hit this on today's loop container and the fix was chmod 0700 alone; the lane is up now.

Measurements

The permission flip, isolated from Postgres entirely:

$ T=$(mktemp -d)/pgd; mkdir -p "$T"; chmod 0700 "$T"; stat -c %a "$T"
700
$ install -d -o postgres -g postgres "$T"; stat -c %a "$T"
755

The refusal, from /tmp/postgres.log on this container. The first four lines are a previous container's healthy start at 00:27Z; the last two are the start attempt after I ran this PR's install -d line at 16:06Z:

2026-09-02 00:27:18.559 UTC [689] LOG:  database system is ready to accept connections
2026-09-02 00:27:18.594 UTC [697] FATAL:  role "root" does not exist
2026-09-02 16:06:43.900 UTC [2032] FATAL:  data directory "/var/lib/postgresql/loop" has invalid permissions
2026-09-02 16:06:43.900 UTC [2032] DETAIL:  Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).

Recovery, with no other change: chmod 0700 /var/lib/postgresql/loop then pg_ctl start -w → "server started"; pg_isready → "accepting connections", exit 0; psql -h 127.0.0.1 -U postgres -tAc 'select 1' → 1.

Why the script's own guards do not catch it. The stale-socket branch at line 74 keys on a socket file existing; here the socket was already gone, so it did not fire and nothing else re-checks the mode. initdb is skipped because PG_VERSION is non-empty — correctly — which is precisely why the 0755 is never repaired. And because the failure path is best-effort by design, the script exits 0 and the caller sees a degraded gate rather than a fixable error.

Scope of the report. I have not pushed anything — this is your branch. The rest of the PR did what it says on this container: the createuser -s root line matters (role "root" does not exist is in the log above from before it existed), and the versioned pg_bin lookup resolved /usr/lib/postgresql/16/bin correctly.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-674 — cd00dc98 Deployed Sep 2, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants