Skip to content

bug(db): seven more agent_sync_state columns accept a 63-bit agent value into an int4 column on PostgreSQL #2827

Closed

Description

@dolho

Summary

#2800 widened agent_sync_state.git_dir_bytes to BIGINT because _coerce_nonneg_int admits any value < 2**63 while the PostgreSQL column was int4. The same mismatch remains on seven sibling columns of the same upsert, so the identical NumericValueOutOfRange stays reachable — surfaced by the merge-train review of #2805.

Where

src/backend/services/sync_health_service.py:

  • :400-403 — pack_count, loose_objects, maintenance_failures go through _coerce_nonneg_int (bound < 2**63) into Integer columns (int4 on PG).
  • :414-417 — ahead_main / behind_main / ahead_working / behind_working are passed uncoerced from the agent payload (payload.get(...) or 0) into int4 columns.

_coerce_nonneg_int's own docstring names the threat model — "a compromised/prompt-injected agent can persist arbitrary JSON into sync-state.json" — so an agent that writes 2**40 into any of these makes every SyncHealthService upsert for it raise on PostgreSQL, and the agent's sync health goes dark exactly as #2800 described for git_dir_bytes.

Rule (from the #2800 learnings entry)

Audit the boundary guard beside the column: if the coercion allows < 2**63 the column must too.

Either widen the seven columns to BigInteger (dual-track: schema.py + tables.py + one Alembic revision; the SQLite track needs nothing — see the note beside _migrate_agent_sync_state_git_dir_bytes in db/migrations.py), or bound the coercion to int4 for the counters that are genuinely small (pack_count, loose_objects, maintenance_failures, ahead/behind counts cannot plausibly exceed 2**31) and route the four ahead/behind fields through it. The second is the smaller, more honest change: a 2-billion-commit divergence is not a value to store, it is a payload to reject.

Acceptance criteria

Related

Activity

  1. added
    priority-p2
    type-bug
    theme-reliability
    on Sep 15, 2026
  2. added 2 commits that reference this issue on Sep 15, 2026
  3. dolho commented on Sep 16, 2026

    @dolho
    ContributorAuthor

    /claim

  4. added
    status-in-progress
    on Sep 16, 2026
  5. github-actions commented on Sep 16, 2026

    @github-actions

    Claimed by @dolho. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/2827-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #2827
  6. dolho commented on Sep 16, 2026

    @dolho
    ContributorAuthor

    Folded into #2805 (the git_dir_bytes BIGINT PR) rather than a separate PR — same boundary function, same test harness, and the two fixes are one story: #2800 widened the one column that needed 64 bits; this makes the boundary stop admitting 64-bit values for the seven columns that are still int4.

    Commit: 2d711a487

    • _coerce_nonneg_int default ceiling → INT4_MAX; git_dir_bytes + the lock-report ints opt into INT8_MAX explicitly
    • new _coerce_counter bounds ahead_main / behind_main / ahead_working / behind_working (and legacy ahead/behind) — previously written raw
    • tests: INT4_MAX + 1 per column through _sync_agent (int4 → NULL/0, BIGINT admitted), INT4_MAX itself lands, and a requires_postgres round-trip of every column's admitted maximum — the proof boundary and column agree
    • mutation (default back to INT8_MAX, counter left raw) → 3 red

    PR body carries Fixes #2827 so the status promoter picks it up on merge.

  7. added
    status-in-dev
    and removed
    status-in-progress
    on Sep 16, 2026
  8. removed
    status-in-dev
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority-p2theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions