Skip to content

python-math #11: fix(delphi): Clojure-parity math writers + committing write path in PostgresClient - #2624

Draft
jucor wants to merge 1 commit into
spr/edge/a861cd87from
spr/edge/6324b652
Draft

python-math #11: fix(delphi): Clojure-parity math writers + committing write path in PostgresClient#2624
jucor wants to merge 1 commit into
spr/edge/a861cd87from
spr/edge/6324b652

Conversation

@jucor

@jucor jucor commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

What

The four math writers in PostgresClient (the functions that write math results to the Postgres tables the server reads) had zero live callers (verified: only commented-out references in test_postgres_real_data.py) and did not match the Clojure SQL. This rewrites them to the verified Clojure statements and fixes the write path so INSERTs actually commit.

Writer fixes

  • write_math_main: caching_tick = COALESCE((select max(caching_tick)+1 from math_main where math_env=?), 1) upsert (postgres.clj:323-338). math_env is the column that keys each math result row — the server only reads rows matching its own setting. The production TypeScript prefetch (pca.ts:84-151) polls for caching_tick > last, so this is fidelity-critical.
  • increment_math_tick: atomic INSERT ... ON CONFLICT ... math_tick+1 RETURNING (postgres.clj:292-295), replacing the previous read-modify-write.
  • Add the MathBidToPid model + write_math_bidtopid (net-new; the server's participants.ts depends on it).
  • write_participant_stats: add the shared math_tick (Clojure writes all three data tables with one tick, conv_man.clj:158-169).

Committing write path

Add _write_returning (using engine.begin()) and route the four writers through it: query() uses engine.connect() (SQLAlchemy 2.0 commit-as-you-go), which rolls back INSERTs on close. Mocked unit tests could not catch this; the Postgres integration test did. execute() now commits too.

Watermark reads for the poll loops

Add poll_votes_since (WHERE created > wm ORDER BY zid,tid,pid,created, sign-flipped, postgres.clj:132-145) and poll_moderation_since (postgres.clj:148-161). Also add ORDER BY zid,tid,pid,created to poll_votes so the full-history rebuild reproduces Clojure's row-insertion order (base-cluster IDs seed k-means -> parity).

Co-Authored-By: Claude Fable 5 noreply@anthropic.com

commit-id:6324b652


Stack:


⚠️ Part of a stack created by spr. Do not merge manually using the UI - doing so may have unexpected results.

@jucor
jucor marked this pull request as draft July 18, 2026 02:43
@jucor
jucor force-pushed the spr/edge/6324b652 branch from b962042 to bb3b05d Compare July 18, 2026 13:28
@jucor
jucor force-pushed the spr/edge/a861cd87 branch from d65c2ef to 55b02d5 Compare July 18, 2026 13:28
@jucor
jucor force-pushed the spr/edge/6324b652 branch from bb3b05d to 9b25900 Compare July 18, 2026 13:32
@jucor
jucor force-pushed the spr/edge/a861cd87 branch 2 times, most recently from 7c607b1 to e5344f9 Compare July 18, 2026 13:35
@jucor
jucor force-pushed the spr/edge/6324b652 branch from 9b25900 to 68e41e5 Compare July 18, 2026 13:35
@jucor
jucor requested a review from Copilot July 21, 2026 08:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Delphi’s Postgres integration to better match the production Clojure SQL semantics and to ensure math writes actually commit under SQLAlchemy 2.x, while also hardening JSON serialization of math blobs that may contain NumPy scalar types.

Changes:

  • Add a committed write helper (_write_returning via engine.begin()) and route math upsert writers through it; execute() now commits as well.
  • Rewrite/extend math table writers for Clojure parity (including atomic increment_math_tick and a new math_bidtopid model/writer).
  • Add global poll helpers (poll_votes_since, poll_moderation_since) and enforce deterministic row ordering for vote polling; add shared NumPy-to-JSON serialization helper + regression test coverage.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
delphi/polismath/database/postgres.py Adds committed write path, Clojure-parity upsert SQL for math writers, atomic tick increment, deterministic vote ordering, and global watermark poll helpers.
delphi/polismath/utils/serialization.py Introduces shared convert_numpy_types helper for JSON serialization of NumPy scalars/arrays.
delphi/polismath/regression/utils.py Hoists and re-exports convert_numpy_types from the new shared serialization module for backward compatibility.
delphi/tests/test_math_writer_numpy_serialization.py Adds regression tests ensuring math writers can serialize real computed blobs containing NumPy integer scalars.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread delphi/polismath/database/postgres.py
@jucor
jucor force-pushed the spr/edge/6324b652 branch from 68e41e5 to 1791ca3 Compare July 21, 2026 11:11
@jucor
jucor force-pushed the spr/edge/a861cd87 branch 2 times, most recently from 81f5d9d to c9ec6ad Compare July 21, 2026 19:49
This was referenced Jul 25, 2026
@jucor
jucor force-pushed the spr/edge/6324b652 branch from 110e1bd to d9e4fdd Compare July 28, 2026 00:11
@jucor jucor changed the title fix(delphi): Clojure-parity math writers + committing write path in PostgresClient python-math #11: fix(delphi): Clojure-parity math writers + committing write path in PostgresClient Jul 28, 2026
…g write path in PostgresClient

## What

The four math writers in `PostgresClient` (the functions that write math results to the Postgres tables the server reads) had zero live callers (verified: only commented-out references in `test_postgres_real_data.py`) and did not match the Clojure SQL. This rewrites them to the verified Clojure statements and fixes the write path so INSERTs actually commit.

## Writer fixes

- `write_math_main`: `caching_tick = COALESCE((select max(caching_tick)+1 from math_main where math_env=?), 1)` upsert (`postgres.clj:323-338`). `math_env` is the column that keys each math result row — the server only reads rows matching its own setting. The production TypeScript prefetch (`pca.ts:84-151`) polls for `caching_tick > last`, so this is fidelity-critical.
- `increment_math_tick`: atomic `INSERT ... ON CONFLICT ... math_tick+1 RETURNING` (`postgres.clj:292-295`), replacing the previous read-modify-write.
- Add the `MathBidToPid` model + `write_math_bidtopid` (net-new; the server's `participants.ts` depends on it).
- `write_participant_stats`: add the shared `math_tick` (Clojure writes all three data tables with one tick, `conv_man.clj:158-169`).

## Committing write path

Add `_write_returning` (using `engine.begin()`) and route the four writers through it: `query()` uses `engine.connect()` (SQLAlchemy 2.0 commit-as-you-go), which rolls back INSERTs on close. Mocked unit tests could not catch this; the Postgres integration test did. `execute()` now commits too.

## Watermark reads for the poll loops

Add `poll_votes_since` (`WHERE created > wm ORDER BY zid,tid,pid,created`, sign-flipped, `postgres.clj:132-145`) and `poll_moderation_since` (`postgres.clj:148-161`). Also add `ORDER BY zid,tid,pid,created` to `poll_votes` so the full-history rebuild reproduces Clojure's row-insertion order (base-cluster IDs seed k-means -> parity).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

commit-id:6324b652
@jucor
jucor force-pushed the spr/edge/a861cd87 branch from 727f71c to afd1714 Compare July 28, 2026 01:10
@jucor
jucor force-pushed the spr/edge/6324b652 branch from d9e4fdd to 9e61b32 Compare July 28, 2026 01:10
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