Skip to content

bug: REAL columns are float4 on PostgreSQL — execution cost loses precision vs SQLite, and an out-of-range cost leaves a loop run unclosed #3441

Open

Description

@vybe

Summary

Money and metric columns declared REAL in src/backend/db/schema.py are carried to PostgreSQL unchanged, where REAL is a 4-byte float (float4, ~7 significant digits, ceiling ~3.4e38). On SQLite the same keyword is an 8-byte double. So the two supported backends store execution cost with different precision, and a value SQLite accepts can raise on PostgreSQL.

The visible failure: closing a loop run whose cost is outside float4 range raises psycopg2.errors.NumericValueOutOfRange from db.finalize_loop_run (UPDATE agent_loop_runs SET … cost=…), inside LoopService._close_run. advance_on_terminal catches it ("never break a terminal write") and logs, so the run row is never closed and the loop never advances or reaches its stop reason.

Reproduction

Against a disposable PostgreSQL 16 (TEST_POSTGRES_URL set, alembic upgrade head applied):

cd tests && pytest "unit/test_ec_loop_service_edges.py::TestCostAccumulator::test_m16_infinite_budget_never_trips" -q
# [postgres] leg: FAILED — NumericValueOutOfRange: "1e300" is out of range for type real
# [sqlite] leg: passes

Deterministic (3 of 3) on a full-suite run of tests/run-full.sh on 2026-10-09 — the postgres tier's only failure (1 failed, 2330 passed).

Context

The test's 1e300 is an edge-case probe, not a realistic cost, but it exposes two real properties:

  • Precision drift between backends. schedule_executions.cost, agent_loop_runs.cost, the total_cost roll-ups, max_cost_usd on the original table DDL, and the monitoring/utilisation percentages are all float4 on PostgreSQL. Summing many small per-run costs in float4 drifts from the SQLite result, and budget comparisons (max_cost_usd) are made against a rounded stored value.
  • An unrepresentable value aborts a terminal write. A cost the column cannot hold leaves the loop run open rather than recording the run and stopping the loop.

The codebase already knows this class: agent_loops.max_cost_usd was added on the PostgreSQL track as DOUBLE PRECISION (Alembic 0008_agent_loops_max_cost), metric_values.value_numeric is DOUBLE PRECISION with a comment in db/tables.py that "REAL would be float4 on PG", and git_dir_bytes was widened to BigInteger for the integer twin of the same defect (#2800). The remaining REAL columns were never swept.

Acceptance Criteria

  • Cost columns (cost, total_cost, max_cost_usd) are DOUBLE PRECISION on PostgreSQL — fresh installs via the baseline DDL translation, existing installs via a new Alembic revision (SQLite needs no change; note that in the db/migrations.py entry per the dual-track rule)
  • The remaining REAL columns are either widened in the same revision or listed with a reason they are safe as float4
  • test_m16_infinite_budget_never_trips[postgres] passes, or — if 1e300 is judged out of contract — the cost is clamped/validated before the write and the test pins that behaviour on both backends
  • A loop run whose cost cannot be persisted still closes (the run is recorded as failed with a named error) rather than being left open
  • A schema-parity test asserts no REAL money column reaches PostgreSQL as float4

Technical Notes

  • src/backend/db/schema.py — to_postgres_table_ddl passes REAL through; ~24 REAL columns
  • src/backend/db/tables.py — the same columns are Float in the SQLAlchemy MetaData (Float is FLOAT → double precision on PG, so MetaData and the baseline DDL already disagree)
  • src/backend/services/loop_service.py — _close_run → db.finalize_loop_run; advance_on_terminal's broad except swallows the failure
  • Invariant Feature/vector log retention #3 / Fix git pushing bug #9 (dual-track migrations) apply

Activity

  1. added
    priority-p3
    type-bug
    theme-reliability
    status-ready
    on Oct 9, 2026
  2. added
    complexity-medium
    on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-mediumpriority-p3status-readytheme-reliabilitytype-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions