Repository navigation
fix(test): give $PGDATA its own mode, and make the failure branch actually print - #679
Conversation
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]>
|
🚅 Deployed to the SahajCloud-pr-679 environment in sahajcloud
1 service not affected by this PR
|
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 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: thesu postgres -cwrapper 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.logappends and is shared across data directories, so the newly-surfacedtail -20can 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 (
$PGDATAmode, postmaster log surfacing) plus four acceptance criteria; the diff implements both and adds nothing else. Labelledready-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. Head9daee7ais a clean merge oforigin/main(f0ec974, #675) intoe14ef7a. scripts/ensure-test-db.shread whole at branch head (106 lines), not just the hunks — which is what surfaced the root guard at line 59 that makes thesuat line 95 redundant, and the shared-l /tmp/postgres.logat 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 >&2produced empty stderr whilesh -c "tail -1 f 2>/dev/null" >&2produced the line; (c)tailas root on a0600file owned bydaemonreturned its contents. - Callers and docs followed:
.claude/skills/pr-prep/check.sh:37(runs underset -u, notset -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", andscripts/AGENTS.md— the last cross-checked againstls 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
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
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
Closes #677.
scripts/ensure-test-db.shcould neither start a cluster on a warm container nor say why. Two independent bugs, one file, +15/−2.1.
$PGDATAwas being chmodded to 0755 on every run. It shared aninstall -dline with two other directories, andinstall -dapplies 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 undidinitdb's mode moments before the start attempt. Naming-m 0700on 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 >&2is applied left to right: fd2 goes to/dev/null, then>&2points fd1 at it too. The postmaster's reason went to/dev/nullon 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$PGDATAholding aPG_VERSIONbut nopostgresql.conf), same script, both redirects:What the adversarial review changed, and the two claims I re-measured
The first version of this PR wrapped that
tailinsu 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 unlessid -uis 0), and root reads a 0600postgres-owned file without ceremony. Thesubought a subprocess, a quoting layer and anologinfailure mode that would print su's own refusal in place of the log. Dropped — the transcript above is with a baretail.It also carried 12 lines of comment for 2 lines of code, about half of it narrating the deleted redirect and the
bash -xsession that found it. That history is in #677 and in this body; the file keeps the standing invariants only.Third change, from a
considerthread: the log is now truncated before the start.pg_ctl -l /tmp/postgres.logappends and uses one path for every$PGDATA, so the newly-surfacedtailcould show a previous run's lines — a diagnostic trusted for the wrong failure. The truncation has to run aspostgres, which is the one place in this file wheresuearns its keep: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.mdrow 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