Skip to content

fix(test): give $PGDATA its own mode, and make the failure branch actually print - #679

Merged
sydevs-bot merged 3 commits into
mainfrom
claude/fix-ensure-test-db-mode
Sep 3, 2026
Merged

sydevs-bot merged 3 commits into
mainfrom
claude/fix-ensure-test-db-mode

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Closes #677.

scripts/ensure-test-db.sh could neither start a cluster on a warm container nor say why. Two independent bugs, one file, +15/−2.

1. $PGDATA was being chmodded to 0755 on every run. It shared an install -d line with two other directories, and install -d applies its default 0755 to every path it names — existing ones included. PostgreSQL refuses to start on a data directory that is not 0700 or 0750, so the second run in any container undid initdb's mode moments before the start attempt. Naming -m 0700 on its own line both preserves the mode and repairs a directory an earlier run already broke.

2. The failure branch discarded the log it exists to print. tail -20 … 2>/dev/null >&2 is applied left to right: fd2 goes to /dev/null, then >&2 points fd1 at it too. The postmaster's reason went to /dev/null on every failure — which is why "cluster did not come up" was the only thing anyone ever saw.

Proven by running it, not by reading it. A forced failure (PGPORT=5599, a $PGDATA holding a PG_VERSION but no postgresql.conf), same script, both redirects:

# after this PR
ensure-test-db: cluster did not come up — integration lane unavailable
postgres: could not access the server configuration file "/tmp/badpg/postgresql.conf": No such file or directory

# with the old redirect restored
ensure-test-db: cluster did not come up — integration lane unavailable
What the adversarial review changed, and the two claims I re-measured

The first version of this PR wrapped that tail in su postgres -c, on the theory that the 0600 log needed it. It did not: the branch is only reachable as root (the guard at line 59 exits unless id -u is 0), and root reads a 0600 postgres-owned file without ceremony. The su bought a subprocess, a quoting layer and a nologin failure mode that would print su's own refusal in place of the log. Dropped — the transcript above is with a bare tail.

It also carried 12 lines of comment for 2 lines of code, about half of it narrating the deleted redirect and the bash -x session that found it. That history is in #677 and in this body; the file keeps the standing invariants only.

Third change, from a consider thread: the log is now truncated before the start. pg_ctl -l /tmp/postgres.log appends and uses one path for every $PGDATA, so the newly-surfaced tail could show a previous run's lines — a diagnostic trusted for the wrong failure. The truncation has to run as postgres, which is the one place in this file where su earns its keep:

$ : > /tmp/roottest.log && ls -l /tmp/roottest.log
-rw-r--r-- 1 root root 0 …
$ su postgres -c "echo hi >> /tmp/roottest.log"
bash: /tmp/roottest.log: Permission denied

Root-truncating on a cold container would have left the postmaster unable to append to its own log — a worse bug than the one being fixed.

Deliberately not folded in: a scripts/AGENTS.md row for this script. That table is already stale in both directions (7 scripts with no row, 2 rows naming deleted files); reconciling it is its own ticket, and adding one row papers over the drift.

Gate. .claude/skills/pr-prep/check.sh → lint, typecheck, typecheck:tests, test:unit (119 files / 1541 specs), exit 0 in 124s. The script itself was run end to end on this container: cold start → PostgreSQL ready on 127.0.0.1:5432/payload_test, exit 0.

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


🤖 Generated with Claude Code

https://claude.ai/code/session_013zCsUruFTxhvz66CEANoAE

ensure-test-db.sh could not start a cluster that already existed — which is
every autonomous run after the first. It failed the exact case it was written
for, and it failed silently.

`install -d` applies its default 0755 to every directory it names, existing
ones included. initdb creates $PGDATA as 0700; the shared line reset it to 0755
before every start attempt, and PostgreSQL then refuses to start on a data
directory that is not 0700 or 0750. Because the reset ran ahead of each attempt,
a manual chmod followed by re-running the script did not recover.

Splitting $PGDATA onto its own `install -d -m 0700` both stops the damage and
repairs a container already carrying it: `install -d -m` chmods what it finds,
so the first run on a broken sandbox fixes it rather than needing a hand-edit.

The second change is why this took a bash -x trace to find. The failure branch
prints the postmaster log, and the log names the cause in two lines — but it
could never appear, for two independent reasons. The postmaster writes it as
postgres with mode 0600, so a bare tail reads nothing; and the redirect was
`2>/dev/null >&2`, which is applied left to right, so fd2 went to /dev/null and
`>&2` then pointed stdout at /dev/null as well. Reading it under `su` fixes the
first; moving the suppression inside the su fixes the second. The permissions
FATAL is now printed, which is how the reintroduction below was observed.

Verified rather than reasoned about, on a warm container holding a 0755 data
directory: two consecutive runs both leave a running cluster and the directory
at 0700; a first run against an empty $PGDATA still initdbs and starts; the
failure branch prints the postmaster log; and restoring the single `install -d`
line makes the run go red again with `data directory ... has invalid
permissions`, leaving the directory at 0755. tests/int/atlas-sitemap.int.spec.ts
then runs green against the restored cluster (17 tests), which is the lane this
unblocks.

Closes #677

Co-authored-by: Claude Opus 5 <[email protected]>
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-679 September 2, 2026 19:11 Destroyed
@railway-app

railway-app Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

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

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

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧐 Adversarial review

The scope is right: one file, +15/−2, both of the fixes #677 asked for and nothing smuggled in beside them. I re-derived both load-bearing claims from scratch rather than taking the measurements on trust — install -d -m 0700 does chmod an existing directory (coreutils 9.4), and tail … 2>/dev/null >&2 does discard its own stdout — so the mode fix, and the repair-an-already-broken-container claim that justifies its shape, both hold. That half I would merge as is.

The other half is where the shape slips. Of the 15 added lines, 2 are code and 12 are comment, and about half of that comment narrates the removed redirect and the bash -x session that found it — the same material already written out at length in #677 and the PR body. More substantively, the second fix adds a layer it does not need: line 95 is only reachable as root (the guard at line 59), and root reads the 0600 log without a su. The PR's own evidence — "fixing it alone still printed nothing" — is consistent with the redirect being the entire bug, and the su buys a subprocess, a quoting layer and a new way for this branch to print something other than the log.

On the open question in the body: don't fold the scripts/AGENTS.md row in. That table is already stale in both directions — 7 scripts have no row (ensure-test-db.sh, etl-d1-to-postgres.ts, generate-logo-pngs.ts, generate-status-config.ts, get-railway-preview-url.ts, mailpit-transport.ts, standalone-postbuild.mjs) and 2 rows name files that no longer exist (repair-r2-meditation-filenames.ts, create-sample-page.ts). Adding one row papers over a drifting table; reconciling it is its own small ticket.

Findings

  • should-fix — scripts/ensure-test-db.sh:95: the su postgres -c wrapper is unnecessary at a line only reachable as root; the redirect order was the whole bug. (thread)
  • should-fix — scripts/ensure-test-db.sh:88–94 (and 67–71): the comments describe the deleted code and the debugging session; keep the invariant, drop the archaeology. (thread)
  • consider — scripts/ensure-test-db.sh:87: pg_ctl -l /tmp/postgres.log appends and is shared across data directories, so the newly-surfaced tail -20 can show a previous run's lines. One line (: > /tmp/postgres.log) closes it. (thread)

What was checked

  • Ticket #677 read first and measured against the diff — it names exactly two fixes ($PGDATA mode, postmaster log surfacing) plus four acceptance criteria; the diff implements both and adds nothing else. Labelled ready-to-implement, Priority High / Effort Easy.
  • Diff via git fetch origin claude/fix-ensure-test-db-mode + git diff origin/main...FETCH_HEAD — 1 file, +15/−2. Head 9daee7a is a clean merge of origin/main (f0ec974, #675) into e14ef7a.
  • scripts/ensure-test-db.sh read whole at branch head (106 lines), not just the hunks — which is what surfaced the root guard at line 59 that makes the su at line 95 redundant, and the shared -l /tmp/postgres.log at line 84.
  • Claims re-verified locally rather than accepted: (a) chmod 0755 d && install -d -m 0700 d → drwx------, coreutils 9.4; (b) tail -1 f 2>/dev/null >&2 produced empty stderr while sh -c "tail -1 f 2>/dev/null" >&2 produced the line; (c) tail as root on a 0600 file owned by daemon returned its contents.
  • Callers and docs followed: .claude/skills/pr-prep/check.sh:37 (runs under set -u, not set -e, so the [ -x … ] && guard cannot abort the gate), .claude/workflow.json:21 (worktreeSetup), docs/rules/testing-reqs.md §"The integration lane needs a live PostgreSQL", and scripts/AGENTS.md — the last cross-checked against ls scripts/ to answer the body's open question with a count rather than an impression.
  • Cross-repo contracts: none touched. No generated types, no payload-types.ts, no embed/URL contract, no API surface — the file is a local test-infra script that ships nowhere. The smoke-lane skip noted in the body is therefore not load-bearing for this diff.
  • Reviewer profile read and applied (workflow/skills/adversarial-review/reviewer-profile.md); no findings raised in test-volume, performance or defensive-edge-case territory, per its "rarely flag" section.

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


Generated by Claude Code

Comment thread scripts/ensure-test-db.sh Resolved
Comment thread scripts/ensure-test-db.sh Resolved
Comment thread scripts/ensure-test-db.sh Resolved
@sydevs-bot sydevs-bot assigned sydevs-bot and unassigned Ardnived Sep 2, 2026
Adversarial review on #679 raised three threads; all three are adopted.

The `su postgres -c "tail …"` at the failure branch was never load-bearing.
That line is only reachable as root — the guard above it exits unless `id -u`
is 0 — and root reads the postmaster's 0600 log without ceremony. Measured
rather than reasoned: with a bare `tail … >&2` the branch prints
`could not access the server configuration file …`; restoring the old
left-to-right `2>/dev/null >&2` on the same forced failure prints nothing.
So the redirect order was the whole bug, and the `su` only added a subprocess,
a quoting layer, and a `nologin` failure mode that would print su's refusal in
place of the log.

The comments went with it. Twelve lines narrating the deleted redirect and the
`bash -x` session that found it are already written down in #677 and in the PR
body; what a future editor needs is the standing invariant, which is now one
line in each block.

`pg_ctl -l /tmp/postgres.log` appends and uses one path for every $PGDATA, so
the newly-surfaced tail could show a previous run's lines — the diagnostic
trusted for the wrong run. Truncating before the start closes it, but it has
to happen AS postgres: root creating that file on a cold container leaves it
root-owned, and the postmaster then cannot append to its own log (verified —
`su postgres -c "echo >> …"` on a root-owned file is Permission denied).

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013zCsUruFTxhvz66CEANoAE
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-679 September 2, 2026 23:10 Destroyed
@sydevs-bot sydevs-bot changed the title fix(test): give $PGDATA its own mode, and surface the postmaster log fix(test): give $PGDATA its own mode, and make the failure branch actually print Sep 2, 2026
@sydevs-bot sydevs-bot assigned Ardnived and unassigned sydevs-bot Sep 2, 2026
@sydevs-bot
sydevs-bot merged commit 974e0c5 into main Sep 3, 2026
2 checks passed
Ardnived pushed a commit that referenced this pull request Nov 5, 2026
Adversarial review on #679 raised three threads; all three are adopted.

The `su postgres -c "tail …"` at the failure branch was never load-bearing.
That line is only reachable as root — the guard above it exits unless `id -u`
is 0 — and root reads the postmaster's 0600 log without ceremony. Measured
rather than reasoned: with a bare `tail … >&2` the branch prints
`could not access the server configuration file …`; restoring the old
left-to-right `2>/dev/null >&2` on the same forced failure prints nothing.
So the redirect order was the whole bug, and the `su` only added a subprocess,
a quoting layer, and a `nologin` failure mode that would print su's refusal in
place of the log.

The comments went with it. Twelve lines narrating the deleted redirect and the
`bash -x` session that found it are already written down in #677 and in the PR
body; what a future editor needs is the standing invariant, which is now one
line in each block.

`pg_ctl -l /tmp/postgres.log` appends and uses one path for every $PGDATA, so
the newly-surfaced tail could show a previous run's lines — the diagnostic
trusted for the wrong run. Truncating before the start closes it, but it has
to happen AS postgres: root creating that file on a cold container leaves it
root-owned, and the postmaster then cannot append to its own log (verified —
`su postgres -c "echo >> …"` on a root-owned file is Permission denied).

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013zCsUruFTxhvz66CEANoAE

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-679 — 51fdbeb5 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.

fix(test): ensure-test-db resets the data directory to 0755, so PostgreSQL refuses to start on a warm container

3 participants