Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

ADR-26041: Client-Side Logic with Server-Side Retrieval


title: Client-Side Logic with Server-Side Retrieval date: 2026-03-09 tags:


ADR-26041: Client-Side Logic with Server-Side Retrieval

Date

2026-03-09

Status

proposed

Context

ADR-26039: pgvector as Ecosystem Database Standard establishes PostgreSQL with pgvector as the single database for all ecosystem storage. ADR-26038: Context Engineering as Core Design Principle defines the agent as a single-agent context manager that orchestrates retrieval, reasoning, and action.

With the database chosen, the next architectural question is: where should the agent’s logic execute? Three options exist:

  1. Fat-Client / Thin-DB: Python handles everything — query construction, retrieval orchestration, scoring, and ranking. The database is passive storage.

  2. Thin-Client / Fat-DB: PL/pgSQL stored procedures handle context operations — hybrid search, scoring, ranking, session management. Python is a thin wrapper.

  3. Hybrid (Logic-in-View): Python handles orchestration (agent loop, LLM calls, context budgeting). SQL stored functions handle computationally expensive retrieval (vector search, hybrid scoring).

This decision impacts every agent in the ecosystem, starting with the mentor_generator application.

Existing Infrastructure

The ecosystem already has a postgres_connector module — a psycopg-based Python library used across database projects. It provides connection management, safe query construction via psycopg.sql, table lifecycle operations (create, drop, dump), and CRUD helpers with SQL injection prevention. The module operates on the public schema and does not yet support schema-per-project isolation or pgvector operations, but it validates the client-side Python pattern as the ecosystem’s established approach to database interaction.

Evidence

A-26007: pgvector Viability Assessment and Logic Locality Decision provides a structured comparison based on S-26011 (Gemini 3 Flash Thinking analysis):

The critical falsification from S-26011: if the agent loop is “read once, think for 30 seconds, write once” — which matches the LLM call pattern — the PL/pgSQL latency advantage vanishes. LLM inference time dominates by orders of magnitude over database round-trip savings.

A-26006: Agent Runtime Architecture and RAG Infrastructure Decisions establishes the master loop pattern where the agent’s cycle is: retrieve context → inject into prompt → call LLM → act on response. The retrieval step is the only database-intensive operation; the rest is Python orchestration.

Decision

We adopt the Logic-in-View pattern: Python handles all agent orchestration, and SQL stored functions encapsulate computationally expensive retrieval operations.

1. Python Owns the Agent Loop

All orchestration logic — context budgeting, LLM calls, skill dispatch, session management, error handling — lives in Python application code. This code is version-controlled in Git, tested with pytest, and follows ecosystem conventions (pathlib, top-down design, TDD).

2. SQL Functions Encapsulate Retrieval

Database-intensive operations — hybrid vector+keyword search, cross-schema queries, scored ranking — are defined as SQL stored functions. These functions are:

3. Boundary Rule

The boundary between Python and SQL follows one principle: if the operation benefits from set-based processing close to the data, it belongs in a SQL function; everything else stays in Python. Concretely:

Consequences

Positive

Negative / Risks

Alternatives

References

Participants

  1. Vadim Rudakov

  2. Claude Opus 4.6