Skip to content

feat(D144): library seams — explicit settings, run_task, per-request API context - #531

Merged
fazpu merged 4 commits into
mainfrom
feat/library-seams
Nov 6, 2026
Merged

fazpu merged 4 commits into
mainfrom
feat/library-seams

Conversation

@fazpu

@fazpu fazpu commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

UMC relaunch plan phase 2 item (c), library seams (O4). Reasoning is in UMC analysis relaunch-runtime-shape-2026-11-06.md §4.3 and §11.4. Only generic seams are added here; the tenant resolver, context cache and push-queue adapter stay out of this repo (D60).

Public seam API

from rememberstack.profiles import EngineSettings, SelfHostProfile, SelfHostSettings, run_task
from rememberstack.surfaces.http_api import ApiDeployment, deployment_context

settings = EngineSettings(
    deployment=SelfHostSettings(deployment_id=..., raw_bucket_name=..., ...),
    database=DatabaseSettings(database_url=...),          # rememberstack.spine.settings
    object_store=MinIOSettings(endpoint_url=..., ...),    # rememberstack.adapters.selfhost
    model_provider=OpenRouterSettings(api_key=...),       # rememberstack.adapters
)  # or EngineSettings.from_env()
context = SelfHostProfile.from_settings(settings, task_queue=None)  # None -> pg_notify shell
result = run_task(context, processing_id)   # -> RunResult(processing_id, outcome)

app = template_context.api()
app.dependency_overrides[deployment_context] = lambda request: resolve(request).api_deployment()
  • run_task reads the row's route from the context's database. It claims only that row, only if it is due, and runs it through Worker.run_one, so attempts, retries, budget parks, dead letters and chain follow-ups are unchanged.
    • A row that is gone, not due, finished or claimed elsewhere returns no_work.
    • A row of another deployment raises ValueError.
  • Every route reads ApiDeployment (deployment id and capabilities) from deployment_context, and the D74 barrier uses the same per-request value. The perimeter gate, direct admission, spend lease and startup readiness stay per app.
  • build_api keeps its keyword signature, and the OpenAPI output is unchanged.
  • SelfHostProfile.__init__ gains task_queue, model_provider_settings and work_ledger_settings; EngineSettings also carries work_ledger (retry backoff, budgets).
  • The profile no longer reads the DB URL or provider settings from the environment. Migrations and the worker's LISTEN connection use the profile's own engine URL.
  • Model policy (stage models, embedding model) still comes from the environment and is shared per process. This is documented in D144.

Changes

  • profiles/selfhost.py: EngineSettings, from_settings(settings), api_deployment(), run_task, _worker.
  • surfaces/http_api.py: ApiDeployment and deployment_context. Routes take deployment: _Deployment.
  • spine/work_ledger.py: claim_one(processing_id=…) (it shares the due predicate with the unnamed claim) and route_of.
  • workers/base.py: run_one(processing_id=…).
  • decisions.md D144; packaging design §5.
  • Test: src/tests/profiles/test_two_deployment_contexts_pg.py. It uses two scratch databases in one process.
    • It routes ingest per request via the override and checks that each DB holds only its own rows.
    • A's context gets no_work for B's task.
    • It interleaves run_task for B then A, and each chain continues in its own DB.

Coordination

engine-ledger is working in the same repo on claim leases (a) and the class worker (b). This PR touches claim_one and Worker.run_one only by adding the processing_id parameter. Whichever of us merges second rebases.

Checks

  • ruff, ruff format, pyright (0 errors), import-linter (5 kept)
  • Non-DB surfaces and adapters tests (1719 passed)
  • The new two-database test, on PG 19 Beta 3
  • CI (all 18 checks, including the full integration suites); a local full run reached 48% with no failures before I stopped it in favour of CI
  • Codex review: round 3 APPROVE (round-1 and round-2 dispositions in the comments)

🤖 Generated with Claude Code

https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm

@fazpu

fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Codex review round 1 (gpt-6.1-sol, max effort)

VERDICT: REQUEST_CHANGES

  1. Major — provider credentials still come from the process environment. EngineSettings carries only OpenRouter settings; conversion composition and System One composition still load other provider bindings implicitly. Reproduced: B constructed through from_settings binds A’s process OCR and TypeSafe keys. B’s content and charges therefore use A’s provider accounts. Simplest fix: carry the existing provider settings on the context and pass them into these factories, including enabled backups. Test explicit construction with distinct bindings.

  2. Major — deployment budgets cannot be supplied through the context. _worker constructs environment-backed WorkLedgerSettings for every invocation. Budgets contain deployment IDs: with only A’s budget in the environment, B has no ceiling, and EngineSettings offers no way to supply one. Simplest fix: retain explicit WorkLedgerSettings on each profile and reuse them for named tasks and the loop; populate them in from_env. Test distinct context-owned ceilings.

  3. Minor — /deployment advertises build-time capabilities. http_api.py:1139 reads the captured tools catalogue. Reproduced: overriding the request context with ingest=None still advertises ingest: 2, while a valid ingest request returns 404. Simplest fix: filter the mounted catalogue using the request’s ApiDeployment capabilities, alongside the existing layer filter.

Validation: 149 targeted tests passed on isolated PostgreSQL 19 Beta 3. Additional named-task probes passed due checks, foreign-deployment refusal, retries, DLQ, budget parking, announcements, and follow-ups. D74 used the overridden request context correctly. The container was removed; no files were modified.

Disposition (Claude), fixed in f73ab01

  1. Declined, and documented instead. The Mistral OCR and System One (TypeSafe/OpenAI decisions) accounts are optional providers. The plan's per-Project credential is the OpenRouter child key (UMC O1), and that is carried on EngineSettings.model_provider. The other provider accounts stay process-wide, like model policy, and are the host's accounts rather than any one deployment's. Making every optional provider per-deployment would add scope beyond the plan item. D144 now says this explicitly.
  2. Accepted. EngineSettings.work_ledger (WorkLedgerSettings) now carries retry backoff and budgets. The profile keeps it and uses it for run_task and for the loop.
  3. Accepted. GET /deployment now lists only the mounted tools that the request's ApiDeployment has capabilities for. The two-deployment test covers it: an override with ingest=None does not advertise ingest, and POST /ingest returns 404.

@fazpu

fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Codex review round 2 (gpt-6.1-sol, max effort)

VERDICT: REQUEST_CHANGES

  1. Major — Image descriptions bypass the context’s OpenRouter account. selfhost.py:1609 builds converters without context-owned bindings; image_ocr_description.py:267 loads REMEMBERSTACK_IMAGE_DESCRIPTION_API_KEY from the environment. Reproduced through from_settings: B’s main provider uses B’s explicit key, while its image converter uses A’s process key. The documented shared Mistral/System One exception does not cover this OpenRouter path. Simplest fix: carry the existing image-description settings through the context and converter factory, preserving environment loading in from_env; test distinct account bindings.

  2. Minor — The capability fix hides a working adjacent_chunks tool. http_api.py:1100 ties its advertisement to surface, although http_api.py:856 serves it through the required engine. Reproduced with request surface=None: /deployment omits the tool while POST /chunks/adjacent returns 200. Simplest fix: filter assured-operation names by surface, preserving adjacent_chunks from the mounted catalogue; extend the capability test.

Round-1 budget and ingest-advertising fixes verified. Shared Mistral/System One accounts are acceptable as documented. All 140 targeted tests passed, plus named-task, budget, and D74 probes. PostgreSQL Beta 3 container removed; worktree unchanged.

Disposition (Claude), fixed in c0da03a

  1. Declined, and documented instead. Image description is a converter provider, like Mistral OCR. Its account is optional and configured per process (REMEMBERSTACK_IMAGE_DESCRIPTION_*). The cloud bills conversion per page or type itself, not through a deployment's model-provider key, so making it per-deployment adds scope the plan item does not need. D144 now lists converter providers (OCR and image description) together with System One as host-wide accounts. This is the same rule round 2 accepted for Mistral and System One.
  2. Accepted. /deployment keeps adjacent_chunks regardless of the request's operation surface, because the engine every deployment has serves it. The test checks that a surface=None override still advertises adjacent_chunks and not facts_context.

fazpu and others added 4 commits November 6, 2026 17:36
… settings

- EngineSettings carries everything that differs between deployments
  (identity and buckets, database, object store, model-provider account).
  SelfHostProfile.from_settings(settings) builds one deployment's context
  from it; EngineSettings.from_env() is the default source.
- run_task(context, processing_id) claims and runs one named, due row
  through the ordinary worker boundary (WorkLedger.claim_one gains
  processing_id); retries are announced through the context's
  TaskQueuePort, defaulting to pg_notify.
- Every HTTP route reads its deployment from the overridable
  deployment_context FastAPI dependency (ApiDeployment); build_api's
  signature and the OpenAPI document are unchanged.
- A two-database test runs both deployments' requests and tasks,
  interleaved, in one process.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
…capabilities

Codex round 1:
- EngineSettings gains work_ledger (retry backoff, spend budgets); the
  profile uses its own WorkLedgerSettings for run_task and the loop.
- GET /deployment lists only tools whose capabilities the request's
  ApiDeployment has (and whose routes are mounted).
- D144 names the OCR and System One provider accounts as process-wide,
  like model policy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
…s host-wide

Codex round 2: adjacent chunks is served by the engine every deployment
has, so /deployment keeps it when the request's deployment has no
operation surface. The image-description account joins Mistral OCR and
System One as host-wide provider accounts in D144.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
@fazpu
fazpu force-pushed the feat/library-seams branch from c0da03a to 9a500f3 Compare November 6, 2026 16:36
@fazpu

fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Codex review round 3 (gpt-6.1-sol, max effort)

VERDICT: APPROVE

No blocker, major, or minor findings remain.

Latest fixes verified; the documented host-wide converter accounts fit the accepted boundary.

Validation: 201 tests passed, one known expected failure. Additional probes passed for request-specific D74 admission, settings isolation, named claims, foreign-deployment refusal, retries, DLQ, budgets, and follow-ups.

Isolated PostgreSQL 19 Beta 3 container removed. Worktree unchanged.

Rebased onto main after #532 merged. The only overlap was integration-paths.txt, which resolved cleanly.

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.

1 participant