Skip to content

PR-S2: switch served math to the Python engine and stop starting Clojure (held until the evidence bar and the backfill) - #2895

Closed
colinmegill wants to merge 9 commits into
edgefrom
pr-s2-math-python-flip
Closed

colinmegill wants to merge 9 commits into
edgefrom
pr-s2-math-python-flip

Conversation

@colinmegill

Copy link
Copy Markdown
Member

Status: DRAFT — do not merge yet

Held until (a) the light-shadow evidence bar is met (three covered UTC days of rolling hourly receipts, zero certified failures, ≥ 100 certified battery entries) and (b) the pre-switch backfill (P‑070) has computed every conversation the Clojure engine has results for, verified by an aggregate-only count. Reviewed design and revisions: design review, re-review of R1–R3, and approval of revisions A/B.

What it does after merge + deploy

  • The server and Delphi read math results under label python (shared MATH_ENV=python, a secret change made at deploy time — see the runbook; not hard-coded).
  • math-python keeps writing python (MATH_PYTHON_ENV); the single-writer lock key is unchanged and pinned by a test.
  • The deploy hook's math role resolves the effective Compose label with docker-compose config and requires a single matching .env assignment: exactly prod → starts the guarded Clojure and verifies it is running (one container, running, exit 0, zero restarts); exactly python → starts nothing and verifies no math container exists; anything else → fails closed. Unknown roles fail closed.
  • math/bin/run refuses any label but exactly prod (exit 78) as an independent second layer (dev/test opt in with MATH_CLOJURE_ALLOW_NONPROD_ENV=1).
  • The deploy workflow uploads an immutable per-run package (deployments/<sha>-<run>-<attempt>.zip, conditional put), re-reads its ETag, and passes ETag + VersionId to CodeDeploy; bindings are recorded as a job artifact. Hash assignments fail the job on any malformed value.
  • Includes the pca2 ETag change (also opened separately to land first).

Runbook (summary; full version in the program notes)

Stable merge → box deploy #1 (packages the new hook; Clojure keeps writing under prod) → verify Clojure writer progress → secret MATH_ENV=python → box deploy #2 (readers switch; the math role starts nothing and verifies retirement) → verification lines (pca2 tick, Delphi label, poller lock line, zero containers on the math box). Rollback: secret MATH_ENV=prod → redeploy (no hook edit; Clojure re-polls 10 days on start).

Tests

Delphi hook/compose/guard + report-label 107; hook + served-env guard 100; math/bin/test-run-guard.sh 11/11; server pca2 suites 90/90; new tests fail against the previous hook/handler.

🤖 Generated with Claude Code

https://claude.ai/code/session_018NVGBuYCk4EZmiz4csUv9s

colinmegill and others added 9 commits September 29, 2026 17:19
…eneration

math_tick is per (zid, math_env). When the served label changes (MATH_ENV
prod -> python at the switch to the Python engine, or back on a rollback),
the new label's tick for a conversation is usually far LOWER than the entity
tag a browser cached from the old label. The API sends Cache-Control:
no-cache, so browsers revalidate pca2 with If-None-Match on every load, and
the route treated the tag as a "newer than" floor: it answered 304 until the
new label's tick passed the old tag, so returning visitors kept seeing the
old label's cached result, possibly for the rest of a conversation's life.

The route now fetches the latest generation and answers 304 only when it
equals a tag the client holds; any other generation, lower or higher, is
served with its own ETag. While the label is unchanged ticks only grow, so
for the single tag a browser sends the answer is the same as before. `*` and
the explicit math_tick parameter keep their handling. Tests pin the switch
direction, the rollback direction, weak tags and the cached path; the two
switch-direction tests fail on the previous handler.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NVGBuYCk4EZmiz4csUv9s
…from the deploy

PR-S2 of the Clojure-to-Python cutover (delphi/docs/CUTOVER_RUNBOOK.md).
The readers move: one shared MATH_ENV in the production env secret sets the
label the server (env_file) and Delphi (compose interpolation) read, and at
the switch it becomes `python`, the label math-python has written since the
shadow started. The secret change is a deploy step, not part of this diff.

- scripts/after_install.sh: the math role starts nothing (the hook has
  already removed every container, so the Clojure box runs nothing until the
  math tier is deleted). The Delphi role line is unchanged (`delphi
  math-python`); its comment now describes the served writer, the MATH_ENV
  secret line, the switch order (stable, then secret, then box deploy) and
  the rollback order (secret MATH_ENV=prod first, then restore `up -d math`,
  redeploy). Stopping math-python now stops the served math, so the
  emergency stop points at the rollback.
- delphi/scripts/math_poller.py: no logic change. The guard refuses to
  WRITE `prod`, Clojure's label, which is served again after a rollback;
  `python` is admitted whatever the readers serve. The single-writer lock key
  stays polis-math-python:python.
- docker-compose.yml, example.env, poller docstring: comments only.
  math-python's label still comes only from MATH_PYTHON_ENV, so moving the
  readers never moves the writer.
- Tests: the math role starts nothing, no role starts `math`, the hook
  documents both orders, readers and writer agree on `python` under the
  post-switch secret, a rollback secret moves only the readers, math-python
  has no env_file; the guard admits `python`, refuses `prod` whatever the
  readers serve, and the lock key is unchanged.

Not safe to deploy on its own: conversations with no votes since the
shadow's 10-day boot window have `prod` rows and no `python` rows, so the
server would present them as empty. The switch waits for a backfill of
those conversations under `python`.

No schema change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NVGBuYCk4EZmiz4csUv9s
math_tick is allocated per (zid, math_env), so prod tick 57 and python tick
57 are unrelated bodies. The previous commit compared If-None-Match to the
served tick by equality, which fixed a lower or higher tick across a label
switch but still answered 304 at an EQUAL tick: review 1439 R2 reproduced a
stale 304 prod -> python, python -> prod and at tick 0.

pca2 now sends ETag "<math_env>-<math_tick>" (the label percent-encoded) and
compares held tags as opaque values, weakly (a GET). A legacy numeric tag,
which client-participation builds from the JSON math_tick, never matches, so
old pages get a 200 on every poll instead of a 304 that may belong to the
other label. A 304 carries the current tag. JSON math_tick stays numeric, and
`*` and the explicit math_tick cursor keep their handling.

The empty presentation synthesized for a conversation with no committed row
no longer carries a generation tag: its math_tick 0 is a placeholder, and
"<label>-0" would have answered 304 to the conversation's first real
publication, also generation 0. Express derives a weak validator from its
body instead. pca.ts exports isSynthesizedPca for this.

Tests: same tick across labels in both directions and at 0 gets 200 and the
new body; legacy numeric tags (quoted, bare, weak, listed) get 200; the
label's own tag (strong, weak, in a list, from a warm cache) gets 304 with
the tag; the synthesized body never carries "<label>-0". 17 of the pca2 HTTP
tests fail on the previous handler. The integration expectation moves to the
new tag and adds the legacy-tag case.

This commit touches only server/ and cherry-picks onto fd0325b as the
prerequisite ETag PR, ahead of the reader switch.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NVGBuYCk4EZmiz4csUv9s
…box runs no Clojure

Review 1439 R3. The unknown-role fallback ran an unnamed
`docker-compose up -d`, which starts every unprofiled service, `math`
included, under whatever MATH_ENV the secret holds: after the switch that is
Clojure writing `python` beside the Python poller. And the math role
announced retirement while both cleanup calls (`down`, `docker rm -f`) had
their failures ignored, so a surviving Clojure container left the deploy
green.

- Unknown or empty role: log, start nothing, exit 1. A missing role file
  already failed under `set -e`.
- Math role: after the shared cleanup, list containers of the compose
  service `math` (label com.docker.compose.service=math, running or
  stopped). Any left, or a failed `docker ps`, fails the deploy; otherwise
  it logs "retirement verified". An already empty box still passes (its
  ignored `docker rm -f` failure is the idempotent case).
- The switch-order comment now reads merge to stable, box deploy while the
  secret still says prod, secret to python, second box deploy, and says why:
  CodeDeploy runs the PACKAGED hook and gives ASG replacements the last
  successful revision (R1).

Tests execute the hook's role section under bash with inert sudo, docker,
docker-compose and aws: unknown/empty/near-miss roles and a missing role
file start nothing and fail; the math role verifies after cleanup and logs
it; a container surviving failed cleanup fails with its id; a failed
`docker ps` fails; an empty box passes; the server and delphi roles still
start exactly their services. Ten of these fail on the previous hook.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NVGBuYCk4EZmiz4csUv9s
… prod

Review 1439 R1. A `stable` merge does not replace the hook CodeDeploy has
packaged: an ASG replacement gets the last successful revision, and an old
revision's math role still runs `up -d math --build`. Run after the secret
says MATH_ENV=python, that hook builds math/ from the stable checkout with
the current compose, which hands Clojure ${MATH_ENV}: Clojure would write
`python` beside the Python poller.

The engine's entrypoint now checks its write label before the restart loop,
so before anything polls or writes:
- `python` is refused always (exit 78, EX_CONFIG);
- any other label but exactly `prod` (unset, empty, `dev`, `PROD`,
  `prod `) is refused unless MATH_CLOJURE_ALLOW_NONPROD_ENV=1.
Only the dev and test overlays set that opt-in (both run MATH_ENV=dev), and
math/example.env for runs outside docker. docker-compose.yml deliberately
does not forward it, so production Clojure runs `prod` or nothing, whatever
the secret carries. This protects `python` independently of which hook ran;
it does not show that Clojure stopped (the hook's retirement check does).

Tests: math/bin/test-run-guard.sh stubs `timeout` (the JVM's wrapper) and
runs bin/run under 11 environments; 8 fail without the guard. In
test_compose_math_env.py: the production compose with the post-switch
secret (with and without the opt-in in the secret) yields a math
environment bin/run refuses; the rollback secret starts; the test stack
opts in and starts under `dev`; the shell suite passes. python-ci copies
math/bin/run and the shell test beside the hook.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NVGBuYCk4EZmiz4csUv9s
…he label-bound ETag and the Clojure guard

Review 1439 R1-R3, as built on this branch: the four-step switch (stable,
box deploy under prod, secret to python, box deploy) and why the first
deploy is needed; pca2's "<math_env>-<math_tick>" tag and its legacy
handling; math/bin/run's write-label guard; the fail-closed hook and the
math role's retirement check; a rollback that restores only the math
role's `up -d math` line.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NVGBuYCk4EZmiz4csUv9s
…abel to prod

Review 1441 (answer (i), in place of "the math role runs nothing"). The math
role reads the label the way Compose will hand it to the `math` service,
`docker-compose config --format json` -> .services.math.environment.MATH_ENV,
with the same invocation as its `up`; the hook never exports MATH_ENV, and a
variable in Compose's environment beats .env while ${MATH_ENV:-prod} would
default a missing key silently. The written .env must also define MATH_ENV
exactly once with that same value.
- `prod`: `up -d math --build --force-recreate`, then after a wait exactly
  one `math` container, state running, exit code 0, no restart (the guard's
  exit 78 restarts it), else the deploy fails.
- `python`: start nothing; no container of the compose service `math` may
  remain after cleanup (unchanged check).
- unreadable config, no value, missing/duplicate key, an override from
  outside .env, or any other label: start nothing, fail the deploy.
math/bin/run's guard stays the independent second layer.

This removes the inter-deployment freeze: deploy #1 under `prod` keeps
Clojure writing, deploy #2 under `python` stops it and verifies retirement.
Rollback is MATH_ENV=prod in the secret and a redeploy, no hook edit.

Tests execute the hook's role section with stateful stand-ins (one state
directory per box): both branches, readiness failures (guard restarting or
exited, restarted, crashed, not started, up/ps/inspect failing, two
containers), 14 unresolved-label cases, the hook's own MATH_ENV ignored,
six precedence cases against the installed docker-compose on the real
compose file, a fresh replacement box under each label, deploy #1 -> #2 on
one box, and rollback python -> prod. 41 fail on the previous hook.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NVGBuYCk4EZmiz4csUv9s
… by ETag and version

Review 1441 R1b. The workflow uploaded every package to the one key
deployments/deployment.zip and passed CodeDeploy neither version nor eTag,
so the deployment group's last successful revision (what an ASG replacement
runs) named a key whose bytes a later upload could replace: omitting version
takes the object's latest version, omitting eTag skips validation.

Now each run uploads to deployments/<git sha>-<run id>-<run attempt>.zip
with `put-object --if-none-match '*'` (an existing key is refused), reads
the object back with head-object and requires the same ETag, and passes
`--s3-location bucket=…,key=…,bundleType=zip,eTag=<ETag>` plus
`,version=<VersionId>` when S3 returns one (the bucket reported versioning
Enabled on 2026-09-29; both cases are handled). The binding (deployment id,
bucket, key, eTag, version, git sha, ref, run id/attempt, archive sha256,
after_install.sh sha256) goes to the job summary, the log and a
deployment-binding-<run id>-<attempt> artifact. The `production`
environment gates on both jobs are unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NVGBuYCk4EZmiz4csUv9s
…ive or hook hash

`echo "archive_sha256=$(sha256sum ...)"` let a failed sha256sum through as
an empty value, so the step could succeed and record empty hashes (review
C1). Each hash is now assigned on its own line under set -euo pipefail,
before the upload, and must match ^[0-9a-f]{64}$ or the job fails. A
comment names s3:GetObjectVersion, which the versioned head-object needs
from the workflow's AWS identity; that policy is not in the repo, so the
runbook's step 0 carries the check.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018NVGBuYCk4EZmiz4csUv9s
@github-actions

Copy link
Copy Markdown
Contributor

Delphi Coverage Report

File Stmts Miss Cover
init.py 2 0 100%
benchmarks/bench_pca.py 128 107 16%
benchmarks/bench_repness.py 81 65 20%
benchmarks/bench_update_votes.py 38 28 26%
benchmarks/benchmark_utils.py 34 0 100%
components/init.py 1 0 100%
components/config.py 165 70 58%
conversation/init.py 2 0 100%
conversation/conversation.py 1290 264 80%
conversation/group_vote_counts.py 38 2 95%
conversation/manager.py 131 42 68%
database/init.py 1 0 100%
database/dynamodb.py 395 178 55%
database/postgres.py 323 109 66%
empty_output.py 29 2 93%
engine_adapter.py 316 264 16%
pca_kmeans_rep/init.py 5 0 100%
pca_kmeans_rep/clusters.py 257 38 85%
pca_kmeans_rep/corr.py 98 17 83%
pca_kmeans_rep/group_k_smoother.py 28 0 100%
pca_kmeans_rep/legacy_kmeans.py 197 8 96%
pca_kmeans_rep/pca.py 162 19 88%
pca_kmeans_rep/repness.py 226 5 98%
poller/init.py 4 0 100%
poller/coordinator_bridge.py 316 316 0%
poller/math_writer.py 105 2 98%
poller/service.py 307 36 88%
poller/worker_pool.py 83 0 100%
queue/executor.py 223 111 50%
regression/init.py 4 0 100%
regression/clojure_comparer.py 188 20 89%
regression/comparer.py 891 640 28%
regression/datasets.py 135 27 80%
regression/recorder.py 36 27 25%
regression/utils.py 130 85 35%
replay/init.py 2 0 100%
replay/attribution_capture.py 49 0 100%
replay/axis_continuity.py 347 24 93%
replay/certify.py 999 59 94%
replay/crosslang.py 128 2 98%
replay/decision_ties.py 55 8 85%
replay/diagnostics.py 50 7 86%
replay/driver.py 73 1 99%
replay/dsn_admission.py 36 1 97%
replay/equiv_query.py 10 0 100%
replay/event_ingress.py 95 1 99%
replay/fixture_bundle.py 815 62 92%
replay/fixture_config.py 266 11 96%
replay/fixture_extract.py 283 12 96%
replay/fixture_generate.py 103 1 99%
replay/fixture_samples.py 157 14 91%
replay/fixture_selection.py 119 0 100%
replay/fixture_survey.py 147 11 93%
replay/legacy_pca.py 29 17 41%
replay/polarity.py 319 25 92%
replay/poller_equiv.py 777 99 87%
replay/prodclone.py 193 2 99%
replay/real_data.py 146 10 93%
replay/schedule.py 205 10 95%
replay/served_math.py 159 4 97%
replay/shard_bench.py 189 65 66%
replay/stagecompare.py 890 161 82%
replay/stages.py 256 40 84%
replay/stepcompare.py 86 1 99%
replay/store.py 120 11 91%
replay/tie_capture.py 107 19 82%
replay/types.py 71 6 92%
run_math_pipeline.py 264 114 57%
types.py 22 0 100%
umap_narrative/501_calculate_comment_extremity.py 112 53 53%
umap_narrative/502_calculate_priorities.py 135 135 0%
umap_narrative/700_datamapplot_for_layer.py 502 502 0%
umap_narrative/701_static_datamapplot_for_layer.py 310 310 0%
umap_narrative/702_consensus_divisive_datamapplot.py 434 275 37%
umap_narrative/801_narrative_report_batch.py 790 726 8%
umap_narrative/803_check_batch_status.py 185 185 0%
umap_narrative/llm_factory_constructor/init.py 2 2 0%
umap_narrative/llm_factory_constructor/model_provider.py 234 234 0%
umap_narrative/polismath_commentgraph/init.py 1 0 100%
umap_narrative/polismath_commentgraph/cli.py 270 270 0%
umap_narrative/polismath_commentgraph/core/init.py 3 3 0%
umap_narrative/polismath_commentgraph/core/clustering.py 108 108 0%
umap_narrative/polismath_commentgraph/core/embedding.py 104 104 0%
umap_narrative/polismath_commentgraph/lambda_handler.py 219 219 0%
umap_narrative/polismath_commentgraph/schemas/init.py 2 0 100%
umap_narrative/polismath_commentgraph/schemas/dynamo_models.py 160 9 94%
umap_narrative/polismath_commentgraph/tests/conftest.py 17 17 0%
umap_narrative/polismath_commentgraph/tests/test_clustering.py 74 74 0%
umap_narrative/polismath_commentgraph/tests/test_embedding.py 55 55 0%
umap_narrative/polismath_commentgraph/tests/test_storage.py 87 87 0%
umap_narrative/polismath_commentgraph/utils/init.py 3 0 100%
umap_narrative/polismath_commentgraph/utils/converter.py 283 237 16%
umap_narrative/polismath_commentgraph/utils/group_data.py 331 289 13%
umap_narrative/polismath_commentgraph/utils/storage.py 584 470 20%
umap_narrative/reset_conversation.py 159 50 69%
umap_narrative/run_pipeline.py 374 230 38%
umap_narrative/topic_naming.py 148 148 0%
utils/clj_hash.py 44 0 100%
utils/env_flags.py 13 0 100%
utils/general.py 63 39 38%
utils/output_profile.py 66 2 97%
utils/serialization.py 10 5 50%
utils/vote_convention.py 29 2 93%
Total 19847 8120 59%

@colinmegill

Copy link
Copy Markdown
Member Author

superseded by #2911

@colinmegill colinmegill closed this Oct 2, 2026
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.

1 participant