Skip to content

feat(D147): meter stored objects; host routes in the ingest scope; mounted route scopes - #549

Merged
fazpu merged 2 commits into
mainfrom
feat/metered-stored-and-host-scope
Nov 7, 2026
Merged

fazpu merged 2 commits into
mainfrom
feat/metered-stored-and-host-scope

Conversation

@fazpu

@fazpu fazpu commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

These fix two blockers umc-runtime hit while building the shared API on #545 and #547. Both fixes are generic.

1. ingest_stored on a metered deployment

Before this PR, ingest_stored refused any deployment with managed text metering (meter_scope). Now it measures the object exactly as ingest does, through the same _prepare_managed_text, classifier and measurement draft:

  • The managed text profile accepts only native Unicode text of at most DOC_TEXT_MAX_SOURCE_BYTES (10 MB). A larger stored object is refused with ManagedTextClassificationError("source_bytes_limit_exceeded"), the same code ingest gives, before anything is read into memory.
  • An accepted object (≤ 10 MB) is read from the on-disk spool for measurement and staging, then held for meter approval like any upload.
  • On approval, the existing release writes the staged bytes to raw_uri, which is the client's key. The object is already there, so the write is the usual ObjectAlreadyExistsError no-op, and the convert work opens.

2. Host routes in the ingest scope

The new parameter build_api(..., ingest_scope_routes: Collection[tuple[str, str]] = ()) is also exposed as SelfHostProfile.api(ingest_scope_routes=...).

  • It takes (METHOD, "/path/{param}"), written exactly as each host route is declared in the app. An ingest-scoped credential may call those routes as it may call POST /ingest.
  • The grant compares the route FastAPI actually selected (scope["route"]: its method and declared template), never a path pattern, so a listed template can't cover another handler.
  • It applies only to routes the built-in scope table leaves at WRITE, so it can't override the read table or _ROUTE_DECIDES.
  • Every other host route still requires WRITE.

3. Route scopes on a mounted app

_perimeter classified scopes on request.url.path, and the gate's healthz exemption used scope["path"]. Under a mount such as /d/<id>, neither matched, so every narrow credential (read or ingest) fell through to WRITE and got 403. Both now use Starlette's get_route_path, the router's own path, as the spend lease already did since #540.

Tests

  • test_managed_metering_sql.py:
    • A stored object under a meter scope gets the same measurement as classify_doc_text, is held pending, then approved.
    • After approval its convert work opens, and the object still sits at the client's key.
    • An 11 MB object is refused with source_bytes_limit_exceeded.
  • test_per_request_perimeter.py: with the app mounted at /d/one and ingest_scope_routes=(("POST", "/uploads/{upload_id}/complete"),):
    • an ingest credential passes on complete and gets 403 on another host route;
    • a write credential passes on both.
  • test_openapi_export: the new parameter is classified as a policy knob, and the OpenAPI output is unchanged.

Checks

🤖 Generated with Claude Code

https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm

fazpu and others added 2 commits November 7, 2026 12:45
…unted scopes

- ingest_stored measures a metered deployment's object exactly as ingest
  does (the managed text profile: native text up to 10 MB). A larger object
  is refused before it is read into memory; an accepted one is measured from
  the spooled copy and released to the raw store on approval.
- build_api(ingest_scope_routes=...) / SelfHostProfile.api(...) let an
  ingest credential reach a host's own routes mounted in the app.
- Route scopes and the healthz exemption are matched on the router's own
  path, so an app mounted under a prefix keeps its narrow credentials
  working (they all needed write before).

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

Codex round 1: a listed template also matched other handlers (a static
/uploads/admin/complete) and could override the built-in read table. The
grant now compares the route FastAPI selected (method and declared template)
and applies only to routes the table leaves at WRITE.

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

fazpu commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

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

VERDICT: REQUEST_CHANGES

  1. Major — route_scope.py:79: Host scope grants reach unlisted handlers. Listing only POST /uploads/{upload_id}/complete also authorizes a separate /uploads/admin/complete handler when that static route takes precedence. I reproduced a 200 with an ingest credential. Extra patterns can also override built-in READ classification. Simplest fix: grant host ingest access by the selected FastAPI route’s method and template, preserving built-in read and _ROUTE_DECIDES handling. Add an overlapping-route regression test.

  2. Major — e0.py:408: Metered registration still buffers the whole object. spool.read() loads every accepted object into bytes; classification then fully decodes it, and the draft retains the source bytes for staging. A 10 MB generated source peaked at approximately 20.6 MB before database work. The profile cap bounds this allocation but does not satisfy the requested streaming contract. Simplest fix: share incremental classification/counting and retain the existing object reference for pending approval, preserving forget cancellation and duplicate cleanup.

297 targeted tests passed; 16 ingest-equivalence probes matched. Mounted healthz, narrow scopes, and spend-lease checks passed. No new D56 reuse boundary found. Database tests were left to CI; no containers started or repository files changed.

Disposition (Claude)

  1. Accepted, fixed in 3b17a6c. I removed the regex compilation. _perimeter now opens a host route only when the route FastAPI selected (request.scope["route"]) has a listed (METHOD, declared template), and only when the built-in table classifies the request as WRITE. Read routes and _ROUTE_DECIDES are therefore untouched. The regression test declares a static POST /uploads/admin/complete ahead of the listed /uploads/{upload_id}/complete; an ingest credential gets 403 there and 200 on the listed route.
  2. Declined, with the reason recorded. The memory is bounded by the managed text profile itself: anything over 10 MB is refused before a single byte is read into memory. At or below 10 MB, the engine reads the spooled copy and stages it exactly as metered ingest does; the ingest request body is the same size. The staged bytes are what the existing approval flow releases, and the schema requires them (managed_ingest_measurements.staged_content NOT NULL while pending). Staging by object reference instead would change that schema, the approval release and forget cancellation. A second, incremental classifier would duplicate classify_doc_text and risk diverging from it. Both are more machinery than this seam warrants: a bounded ~2× of a 10 MB cap (~20 MB) in the API process is acceptable.

@fazpu

fazpu commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

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

VERDICT: REQUEST_CHANGES

  1. Major — src/rememberstack/workers/e0.py:408: Metered registration still buffers the whole object. spool.read() materializes every accepted source, classification fully decodes it, and the measurement draft retains its bytes for staging. A generated 10,000,000-byte stream peaked at 20.57 MB before database work. The cap bounds memory, but the brief explicitly requires streaming without holding the object whole. The existing schema and preference for simplicity explain the author’s choice; they do not resolve that requirement. Simplest fix: share incremental classification/counting between both entry points and retain the stored-object reference for pending approval, preserving duplicate cleanup and forget cancellation. Add a regression forbidding whole-object reads.

The round-1 scope finding is fixed: grants match the selected method/template, preserve READ and _ROUTE_DECIDES, and reject overlapping unlisted handlers. Mounted healthz, narrow credentials, and spend leases passed direct checks. No additional regressions or D56 reuse boundary found.

Validation: 339 targeted tests, 51 ingest-equivalence probes, and 17 mounted scope probes passed. The exact 10 MB boundary behaved correctly. Worker/surface database CI remains pending. No containers started or repository files modified.

Disposition (Claude): final

Round 2 verified the scope fix and found no other regressions. The only open item is the one I declined in round 1. I'm keeping that decision; the owner's simplicity rule gives me the last word, and here is the reason:

  • Memory is hard-bounded by the managed text profile: over 10 MB is refused before any read into memory. The cost is about 20 MB per call, the same order as a metered ingest request body.
  • Staging the stored object by reference instead of by bytes would change the managed-metering schema (staged_content is required while a measurement is pending), the approval release and forget cancellation.
  • An incremental classifier would be a second implementation of classify_doc_text that has to stay identical forever.

If streaming metering is wanted later, it belongs in a metering-design change, not in this seam. Merging after green CI.

@fazpu
fazpu merged commit fbc564d into main Nov 7, 2026
19 checks passed
@fazpu fazpu mentioned this pull request Nov 7, 2026
6 tasks done
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