Add bounded AI Engine contracts and shared adapter test suite (E01, #15) #57

Merged
amiryahaya merged 11 commits from feat/15-engine-contracts into main 2026-10-25 17:46:30 +01:00
Owner

Refs #15 (E01).

What this adds

A new rag-ai-engine crate: the app-owned contract between the RAG application and the AI Engine. It depends on rag-domain only; a test enforces that.

  • Bounded, scoped requests: EmbedRequest, RerankRequest, CountRequest, GenerateRequest. Each carries a RequestScope (request ID, organization, policy version, optional marking, deadline, cancellation) and a permitted profile ID, and validates bounds in its constructor. No conversation, message, document or user identifiers. Debug on requests, scope, embeddings and stream tokens never prints text, vectors or marking values (sentinel tests).
  • Profiles and compatibility: EmbeddingContract (profile, dimensions, tokenizer) fails with IncompatibleProfile or UnknownProfile before any work. GenerationProfile::fits fails with ContextTooLarge before generation starts, with checked arithmetic. Embed inputs and rerank passages over the advertised token limit also fail before work; nothing is silently truncated.
  • AiEngineClient: object-safe trait with boxed futures (embed, rerank, count_tokens, streamed generate, capabilities), a versioned contract (1.0) and Capabilities::require (streaming, cancellation, session isolation).
  • Shared contract suite: 14 named checks run against two deliberately different fake adapters (different dimensions, tokenizer, chunking, slot behavior). The checks cover stream order with a single terminal event, cancellation, deadlines, no work on rejected requests, request isolation, concurrent streams, and release of unpolled streams at their deadline. A third, leaky fake proves the suite fails when state leaks between requests.
  • ProfileId in rag-domain; docs/operations/ai-engine-contract.md (contract, bounds, semantics, how a real adapter plugs in, deferrals); README link; the implementation plan.

Not in this PR

  • A real adapter, or an HTTP/gRPC transport. The trait is transport-neutral; Instant and Cancellation stay on the client side.
  • Admission control and scheduling (#30), health registration (when an Engine exists), and the Embedder / Reranker / Generator application traits (first consumers define them).
  • Numeric bounds are defaults awaiting OD-06. An awaitable Cancellation::cancelled(), Usage on errored streams, and per-message template overhead in GenerationProfile are listed as deferred in the docs.
  • The suite is test-only (crates/ai-engine/tests/support); promote it to a shared crate when the first real adapter lands. Suite checks 6, 9, 10, 11 and 12 assume output derived from the prompt, so a real model adapter needs an echo or deterministic profile.

Verification

  • bash scripts/coverage.sh rust (workspace tests plus the Postgres suites): exit 0, 93.83% line coverage (minimum 80%). New crate sources: 93-100%.
  • cargo fmt --check and cargo clippy --workspace --all-targets --locked -- -D warnings: clean.
  • Reviews: four per-task reviews; Pass 1 (complexity) and Pass 2 (correctness) on the whole branch. Pass 2 found three gaps (over-long inputs unspecified, unpolled streams holding resources, ambiguous CountRequest.profile); all fixed and re-reviewed clean.
  • One rag-api authz unit test failed once in a workspace run and passed on three reruns; it is outside this change.

🤖 Generated with Claude Code

Refs #15 (E01). ## What this adds A new `rag-ai-engine` crate: the app-owned contract between the RAG application and the AI Engine. It depends on `rag-domain` only; a test enforces that. - **Bounded, scoped requests:** `EmbedRequest`, `RerankRequest`, `CountRequest`, `GenerateRequest`. Each carries a `RequestScope` (request ID, organization, policy version, optional marking, deadline, cancellation) and a permitted profile ID, and validates bounds in its constructor. No conversation, message, document or user identifiers. `Debug` on requests, scope, embeddings and stream tokens never prints text, vectors or marking values (sentinel tests). - **Profiles and compatibility:** `EmbeddingContract` (profile, dimensions, tokenizer) fails with `IncompatibleProfile` or `UnknownProfile` before any work. `GenerationProfile::fits` fails with `ContextTooLarge` before generation starts, with checked arithmetic. Embed inputs and rerank passages over the advertised token limit also fail before work; nothing is silently truncated. - **`AiEngineClient`:** object-safe trait with boxed futures (embed, rerank, count_tokens, streamed generate, capabilities), a versioned contract (`1.0`) and `Capabilities::require` (streaming, cancellation, session isolation). - **Shared contract suite:** 14 named checks run against two deliberately different fake adapters (different dimensions, tokenizer, chunking, slot behavior). The checks cover stream order with a single terminal event, cancellation, deadlines, no work on rejected requests, request isolation, concurrent streams, and release of unpolled streams at their deadline. A third, leaky fake proves the suite fails when state leaks between requests. - `ProfileId` in `rag-domain`; `docs/operations/ai-engine-contract.md` (contract, bounds, semantics, how a real adapter plugs in, deferrals); README link; the implementation plan. ## Not in this PR - A real adapter, or an HTTP/gRPC transport. The trait is transport-neutral; `Instant` and `Cancellation` stay on the client side. - Admission control and scheduling (#30), health registration (when an Engine exists), and the `Embedder` / `Reranker` / `Generator` application traits (first consumers define them). - Numeric bounds are defaults awaiting OD-06. An awaitable `Cancellation::cancelled()`, `Usage` on errored streams, and per-message template overhead in `GenerationProfile` are listed as deferred in the docs. - The suite is test-only (`crates/ai-engine/tests/support`); promote it to a shared crate when the first real adapter lands. Suite checks 6, 9, 10, 11 and 12 assume output derived from the prompt, so a real model adapter needs an echo or deterministic profile. ## Verification - `bash scripts/coverage.sh rust` (workspace tests plus the Postgres suites): exit 0, **93.83%** line coverage (minimum 80%). New crate sources: 93-100%. - `cargo fmt --check` and `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean. - Reviews: four per-task reviews; Pass 1 (complexity) and Pass 2 (correctness) on the whole branch. Pass 2 found three gaps (over-long inputs unspecified, unpolled streams holding resources, ambiguous `CountRequest.profile`); all fixed and re-reviewed clean. - One `rag-api` authz unit test failed once in a workspace run and passed on three reruns; it is outside this change. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Adds ScriptedEngine, SlotEngine and a deliberately leaky engine, one
adapter-neutral suite of 12 named checks, a Cargo.toml boundary test and
docs/operations/ai-engine-contract.md. Also tightens client.rs tests
(exact Debug output, object safety, major-0 compatibility), documents the
major-version check in Capabilities::require and the three profile types.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
test(ai-engine): wider deadline margin in the unpolled-stream check; note the over-long check's limit (#15)
Some checks failed
CI / Web checks (pull_request) Successful in 41s
CI / Web checks (push) Successful in 38s
CI / Rust checks (push) Successful in 15m27s
CI / Rust checks (pull_request) Failing after 24m45s
99b39b1041
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
test(storage-postgres): give the pool-exhaustion test a connect budget that survives a loaded runner
Some checks failed
CI / Web checks (push) Successful in 48s
CI / Web checks (pull_request) Successful in 51s
CI / Rust checks (pull_request) Failing after 7m57s
CI / Rust checks (push) Has been cancelled
eb7a9168d5
The test's 250 ms acquire_timeout also bounds PostgresStore::connect's first
connection, so a slow CI connect failed it with Err(Connection) before any
assertion ran. Raise it to 1 s; the 2 s outer timeout still bounds the wait.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
test(storage-postgres): same connect budget for the readiness loss/recovery test
All checks were successful
CI / Web checks (push) Successful in 41s
CI / Web checks (pull_request) Successful in 2m2s
CI / Rust checks (push) Successful in 18m59s
CI / Rust checks (pull_request) Successful in 19m0s
2fb1458f0c
Second test with a 250 ms acquire_timeout that also bounds connect's first
connection; it failed the same way on a loaded runner.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.