Skip to content

fix: enable production PostgreSQL persistence - #142

Merged
franciscojavierarceo merged 7 commits into
mainfrom
codex/issue-102-postgres-pool-config
Aug 3, 2026
Merged

franciscojavierarceo merged 7 commits into
mainfrom
codex/issue-102-postgres-pool-config

Conversation

@franciscojavierarceo

@franciscojavierarceo franciscojavierarceo commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • make the existing persistence layer work with PostgreSQL through portable SQL, configurable pool/timeouts, per-conversation row locking, and a database-unique conversation sequence index
  • pin every PostgreSQL pool connection to the validated persistence schema, preserve existing data during the INTEGER-to-BIGINT upgrade, and fail closed on split or incompatible schemas
  • verify real writes once at startup, keep readiness probes read-only and privilege-aware, and return consistent machine-readable streaming persistence errors
  • add PostgreSQL migration/concurrency/readiness coverage and document production rollout, TLS, pooling, schema, and duplicate-sequence guidance

This is the PostgreSQL persistence slice of #102.

Closes #103.
Refs #102.

Test Plan

  • cargo fmt --all -- --check
  • cargo clippy --all-targets -- -D warnings
  • cargo test
  • uvx pre-commit run --all-files
  • TEST_POSTGRES_URL=... cargo test -p agentic-server-core --lib -- --ignored --nocapture
  • TEST_POSTGRES_URL=... cargo test -p agentic-server-core --test postgres_storage_integration -- --ignored --nocapture
  • read-only Claude review: no actionable findings
  • gstack pre-landing specialist, red-team, and adversarial reviews: no remaining findings
  • uv run --with-requirements docs/requirements.txt mkdocs build --strict (reaches the pre-existing broken docs/design/claude-code-integration.md link to ROADMAP.md)

@maralbahari maralbahari left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the work. I left some inline comments

@franciscojavierarceo
franciscojavierarceo force-pushed the codex/issue-102-postgres-pool-config branch from 2c1ba13 to b56cf79 Compare July 29, 2026 13:54

@ashwing ashwing 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.

The earlier fixes hold up on 2c1ba13 and CI genuinely exercises the Postgres path via the --ignored integration run. One schema-validation portability issue I reproduced against a stock Postgres 17, plus an accuracy nit inline — none blocking.

Two more:

  • [nit] storage/response.rs — the row-lock hardening is conversation-scoped only. previous_response_id continuations read get(prev_id).history_item_ids outside any lock, so two concurrent continuations of the same response read the same history and fork. That's legitimate under OpenAI response-chaining (forks are allowed) and each row is independent — not a bug — but worth stating so nobody assumes response chains are serialized the way conversations now are.
  • The two docs/superpowers/...pr-142-review-followups*.md files look like PR-process planning docs rather than user-facing deployment docs, and they're not in the mkdocs nav. There's precedent (a websocket-shutdown pair already on main), so if this is a deliberate planning archive that's fine — just checking they're meant to ship, and if so maybe exclude docs/superpowers/ from the mkdocs build so it doesn't rot in the published docs.

WHERE table_namespace.nspname = current_schema() \
AND table_relation.relname = required.table_name \
AND constraint_metadata.contype::text = required.constraint_type \
AND pg_get_constraintdef(constraint_metadata.oid) = required.definition \

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.

[concern] current_schema()-pinned constraint validation fails to start under a non-public search_path.

postgres_required_constraint_count matches each constraint by exact string pg_get_constraintdef(oid) = definition and pins table_namespace.nspname = current_schema(), and it runs unconditionally on the auto-migrate path (widen_postgres_integer_columns -> validate_required_postgres_schema) — i.e. right after sqlx applies our own migrations into public.

I reproduced this on postgres:17-alpine: an app role with a role-named schema and the default managed-Postgres search_path = "$user", public makes current_schema() return the role's schema, not public. The validation then counts 0 of 6 required constraints and returns Err, so every replica fails to start even though the schema is correct:

as role 'agentic' (search_path="$user",public): current_schema() = agentic
constraint count found under current_schema(): 0    # expected 6

(I also checked the PG-version angle — 16 and 17 emit byte-identical pg_get_constraintdef output, so formatting drift isn't the issue; the schema-resolution is.)

It fails loud, not silent, so no corruption — but "app deployed with a per-role schema" is a common managed-PG default, not an edge case. Two options: resolve the tables' actual namespace instead of current_schema(), or scope the strict constraint-string check to the supervisor/AGENTIC_API_SCHEMA_READY path and trust sqlx's migration tracking on auto-migrate.


/// Locks an existing conversation for the lifetime of the transaction.
///
/// `PostgreSQL` takes a row lock without writing the row. `SQLite` uses a no-op

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.

[nit] the doc comment says both backends "serialize per-conversation sequence allocation." Postgres does (SELECT id ... FOR UPDATE). But the SQLite no-op UPDATE ... WHERE id = $1 takes SQLite's database-level write lock, so it serializes all conversation persists globally, not per-conversation. Correct for safety — the granularity claim is just off, and it hides a SQLite write-throughput ceiling worth naming.

Signed-off-by: Francisco Javier Arceo <[email protected]>
@franciscojavierarceo
franciscojavierarceo merged commit cb815f1 into main Aug 3, 2026
5 checks passed
maralbahari pushed a commit that referenced this pull request Aug 4, 2026
## Summary

- capture a typed conversation version alongside rehydrated history
- reject stale explicit-conversation turns under the existing row lock
before persistence
- return a consistent `conversation_locked` error over HTTP, SSE, and
WebSocket, including `generate: false`
- preserve `previous_response_id` fork behavior and the existing
unchecked storage API
- add SQLite, transport, and two-pool PostgreSQL race coverage plus
deployment documentation

This builds on the PostgreSQL persistence foundation merged in #142.

This implements optimistic conflict detection at persistence time. A
pre-inference distributed lease or queue that also suppresses external
tool side effects remains separate policy work.

## Test Plan

- `cargo fmt --all -- --check`
- `cargo clippy --all-targets -- -D warnings`
- `cargo test`
- `pre-commit run --all-files`
- `postgres_concurrent_conversation_writes_have_contiguous_sequences`
against PostgreSQL 17
- `postgres_optimistic_conversation_conflict` against PostgreSQL 17
- `postgres_lock_wait_is_bounded_without_blocking_other_conversations`
against PostgreSQL 17
- read-only Claude review: no actionable findings
- gstack pre-landing review: all in-scope actionable findings resolved

Signed-off-by: Francisco Javier Arceo <[email protected]>
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.

Make PostgreSQL persistence production-ready

4 participants