Skip to content

python-math #12: feat(delphi): Python math poller service (phase 1) — Clojure math-container replacement - #2625

Draft
jucor wants to merge 1 commit into
spr/edge/6324b652from
spr/edge/18c7048f
Draft

python-math #12: feat(delphi): Python math poller service (phase 1) — Clojure math-container replacement#2625
jucor wants to merge 1 commit into
spr/edge/6324b652from
spr/edge/18c7048f

Conversation

@jucor

@jucor jucor commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

What

The poller half of MATH_POLLER_DESIGN.md phase 1: a Python service that replaces the Clojure math container's production duties — vote/moderation watermark polling (a watermark is the timestamp up to which rows have already been processed), per-zid serialized compute (a zid is a conversation ID), and the four-table Postgres writes.

Components

  • polismath/poller/service.py: MathPollerService — vote + moderation watermark loops mirroring poller.clj:12-37 (strict >, max-of-batch advancement, POLL_FROM_DAYS_AGO=10 boot window, allowlist/blocklist); per-zid engine chain update_votes -> update_moderation -> recompute() with load-or-init from math_main + full-history rebuild (conv_man.clj:188-207 analog); error path dump -> one retry -> park (analog of Clojure's errorconv).
  • polismath/poller/worker_pool.py: per-zid FIFO + single-owner flag (strict serialization), take-all!/split-batches coalescing (votes before moderation), bounded cross-zid ThreadPoolExecutor.
  • polismath/poller/math_writer.py: one math_tick per cycle, shared across math_main / math_bidtopid / math_ptptstats (conv_man.clj:158-169); bidToPid positionally aligned with base-cluster ids ascending (prep-bidToPid, conv_man.clj:35-40; server participants.ts:33-51).
  • scripts/math_poller.py CLI (--once / run-forever, SIGTERM/SIGINT) + docker-compose service delphi-math-poller (profile delphi-math, shadow MATH_ENV by default) + example.env documentation.

Testing

tests/poller/: 44 unit tests (watermark, coalescing, serialization thread-safety, writer SQL incl. the caching_tick MAX+1 subquery + shared tick, bidToPid shape, allow/block, engine-mode passthrough, error path, load-or-init restoration boundary) + 1 opt-in integration test (throwaway postgres:17 on port 5435): end-to-end poll -> compute -> write, shadow math_env isolation, shared math_tick, caching_tick=1 on first write, restart-resumes.

Cutover phasing

Per MATH_POLLER_DESIGN.md, this ships SHADOW mode only: writes land under a math_env value the server does not read (math_env is the column that keys each math result row; the server only reads rows matching its own setting). The flip to serving traffic is gated on the blob-shape alignment work — the deltas between Python's to_dict and Clojure's prep-main catalogued by the H-B cross-language comparison (H-B = the replay harness's Clojure-side driver and comparer, commit 8 in this series).

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

commit-id:18c7048f


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/18c7048f branch from 73b4cb9 to 4a152dd 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/18c7048f branch from 4a152dd to 4576fbb Compare July 18, 2026 13:32
@jucor
jucor force-pushed the spr/edge/6324b652 branch from 9b25900 to 68e41e5 Compare July 18, 2026 13:35
@jucor
jucor force-pushed the spr/edge/18c7048f branch from 4576fbb to 2ee5b01 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

Introduces a new Python-based “math poller” service under polismath.poller to replace the production responsibilities of the legacy Clojure math container (phase 1), including Postgres polling (votes + moderation), per-zid serialized processing, and writing math outputs back to Postgres under a configurable math_env (shadow-mode by default).

Changes:

  • Add MathPollerService (dual watermark poll loops + per-zid compute pipeline + error dump/retry/park) and a per-zid serialized ConversationWorkerPool with batch coalescing.
  • Add MathWriter orchestration for math_main, math_bidtopid, and math_ptptstats using a shared math_tick per cycle and Clojure-parity SQL in PostgresClient.
  • Add CLI entrypoint, docker-compose profile wiring, and a comprehensive unit + opt-in integration test suite.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
example.env Documents configuration knobs for running the new poller, including shadow-mode defaults.
docker-compose.yml Adds a profile-gated delphi-math-poller service that runs the poller CLI with shadow MATH_ENV.
delphi/pyproject.toml Registers a new integration pytest marker for Postgres-backed tests.
delphi/scripts/math_poller.py CLI wrapper to run the poller once or continuously with signal-based shutdown.
delphi/polismath/poller/init.py Package-level architecture + contract documentation and public exports.
delphi/polismath/poller/service.py Core poller service: watermark loops, dispatch, load-or-init, recompute/write, and error handling.
delphi/polismath/poller/worker_pool.py Per-zid serialized worker pool with batch coalescing and bounded cross-zid concurrency.
delphi/polismath/poller/math_writer.py Derives bidToPid/ptptstats envelopes and writes all tables with one shared math_tick.
delphi/tests/poller/init.py Declares the new poller test package.
delphi/tests/poller/test_watermark.py Unit tests for watermark advancement and allow/block filtering invariants.
delphi/tests/poller/test_service.py Tests service dispatch behavior, watermark advancement, env engine-mode passthrough, and LRU eviction.
delphi/tests/poller/test_serialization.py Thread-safety tests for per-zid serialization, cross-zid concurrency, and parking behavior.
delphi/tests/poller/test_coalescing.py Tests batch coalescing rules (votes before moderation, arrival-order flattening).
delphi/tests/poller/test_math_writer.py Tests bidToPid derivation, shared math_tick behavior, and SQL shape via a recorder client.
delphi/tests/poller/test_load_or_init.py Tests from_dict restoration limits and the required full-history rebuild behavior.
delphi/tests/poller/test_error_path.py Tests dump→retry→park circuit breaker behavior and “self-heal on new batch”.
delphi/tests/poller/test_integration_postgres.py Opt-in end-to-end integration test that provisions Postgres, seeds data, runs poll→compute→write, and verifies restart-resume semantics.

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

Comment thread delphi/polismath/poller/service.py Outdated
Comment thread delphi/polismath/poller/service.py
Comment thread example.env Outdated
@jucor
jucor force-pushed the spr/edge/18c7048f branch from 2ee5b01 to 6a9b51f Compare July 21, 2026 11:11
@jucor
jucor force-pushed the spr/edge/6324b652 branch 2 times, most recently from 1791ca3 to d05e53d 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 force-pushed the spr/edge/18c7048f branch from f8ab673 to c4ef767 Compare July 28, 2026 00:11
@jucor jucor changed the title feat(delphi): Python math poller service (phase 1) — Clojure math-container replacement python-math #12: feat(delphi): Python math poller service (phase 1) — Clojure math-container replacement Jul 28, 2026
… Clojure math-container replacement

## What

The poller half of `MATH_POLLER_DESIGN.md` phase 1: a Python service that replaces the Clojure math container's production duties — vote/moderation watermark polling (a watermark is the timestamp up to which rows have already been processed), per-zid serialized compute (a zid is a conversation ID), and the four-table Postgres writes.

## Components

- `polismath/poller/service.py`: `MathPollerService` — vote + moderation watermark loops mirroring `poller.clj:12-37` (strict `>`, max-of-batch advancement, `POLL_FROM_DAYS_AGO=10` boot window, allowlist/blocklist); per-zid engine chain `update_votes -> update_moderation -> recompute()` with load-or-init from `math_main` + full-history rebuild (`conv_man.clj:188-207` analog); error path dump -> one retry -> park (analog of Clojure's errorconv).
- `polismath/poller/worker_pool.py`: per-zid FIFO + single-owner flag (strict serialization), `take-all!`/`split-batches` coalescing (votes before moderation), bounded cross-zid `ThreadPoolExecutor`.
- `polismath/poller/math_writer.py`: one `math_tick` per cycle, shared across `math_main` / `math_bidtopid` / `math_ptptstats` (`conv_man.clj:158-169`); bidToPid positionally aligned with base-cluster ids ascending (`prep-bidToPid`, `conv_man.clj:35-40`; server `participants.ts:33-51`).
- `scripts/math_poller.py` CLI (`--once` / run-forever, SIGTERM/SIGINT) + docker-compose service `delphi-math-poller` (profile `delphi-math`, shadow `MATH_ENV` by default) + `example.env` documentation.

## Testing

`tests/poller/`: 44 unit tests (watermark, coalescing, serialization thread-safety, writer SQL incl. the `caching_tick` MAX+1 subquery + shared tick, bidToPid shape, allow/block, engine-mode passthrough, error path, load-or-init restoration boundary) + 1 opt-in integration test (throwaway `postgres:17` on port 5435): end-to-end poll -> compute -> write, shadow `math_env` isolation, shared `math_tick`, `caching_tick=1` on first write, restart-resumes.

## Cutover phasing

Per `MATH_POLLER_DESIGN.md`, this ships SHADOW mode only: writes land under a `math_env` value the server does not read (`math_env` is the column that keys each math result row; the server only reads rows matching its own setting). The flip to serving traffic is gated on the blob-shape alignment work — the deltas between Python's `to_dict` and Clojure's `prep-main` catalogued by the H-B cross-language comparison (H-B = the replay harness's Clojure-side driver and comparer, commit 8 in this series).

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

commit-id:18c7048f
@jucor
jucor force-pushed the spr/edge/18c7048f branch from c4ef767 to 6d41ef0 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