Skip to content

feat(D150): the managed meter measures each new version in convert - #556

Merged
fazpu merged 8 commits into
mainfrom
feat/d94-generic-meter
Nov 7, 2026
Merged

fazpu merged 8 commits into
mainfrom
feat/d94-generic-meter

Conversation

@fazpu

@fazpu fazpu commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Problem

A metered deployment — every hosted remember.dev Project — measured at ingest. Only plain UTF-8 text and Markdown up to 10 MB could be measured. Everything else was refused before it was stored:

  • every PDF, office document, HTML file or image got 409 rate_class_unavailable or 422 rate_class_ambiguous;
  • larger text got 413 source_bytes_limit_exceeded.

The accepted bytes were also staged in a bytea column until approval. The cloud's D94 asks for a generic receipt instead: {cost_class, native_quantity, unit} per source version, every PDF page billed as doc-scan, and other readable text billed as doc-text.

Decision (D150)

Measurement moves into the convert stage, where the route that reads the file is known (core/metering.py).

  • pdf → doc-scan. The page count comes from the page tree before any OCR call. Bytes routes use PageCountingConverter.count_pages. By-URL routes (D147) use UrlPageCountingConverter.count_pages_from_url, so the worker never reads the object. PdfConverter implements count_pages.
  • Local reading routes → doc-text. Covers text, markitdown, office, email, notebook, spreadsheet, table and dataset. The quantity is the whitespace-normalized character count of the document.md the route produced. These routes call no provider, so the count still comes before every paid stage.
  • card is free. It produces no measurement.
  • Any other route fails the version with rate_class_unavailable, so a metered deployment never runs provider work it cannot bill.
  • Parking. After measuring, the convert row parks with a new admission defer reason. No claim takes a row in that state.
    • The meter drain's approval releases the row in the transaction that records the approval (release_admission_on), and announces it for push hosts.
    • park_for_admission reads the approval under the measurement-row lock that the approval takes, so a race leaves the work queued, never stranded.
    • On release, a PDF is OCR'd. A reading route replays its stored reading, so nothing is converted twice.
  • Ingest refuses nothing for metering. Every path writes the original and enqueues convert. The 10 MB cap, classify_doc_text and the classification 409/413/422 responses are deleted. Unchanged bytes create no version, so there are no no_op receipts.
  • Receipt v3.
    • rememberstack.ingest_measurement.v3: role primary, format_family, cost_class, native_quantity, unit (exactly one unit per class).
    • rememberstack.document_version_outcome.v3: succeeded or failed only.
    • Processing profiles become <cost class>-<standard|claims|sources>-v1.
  • Migration p9_46_0067.
    • Drops staged_content and the no_op disposition, and adds cost_class, unit and format_family. The migration refuses to run while staged originals remain undrained.
    • Allows one measurement per version.
    • Adds the admission defer reason.
  • Public surface. PipelineStageReadiness.defer_reason gains admission. OpenAPI and the TypeScript contracts are regenerated. RunResultOutcome gains admission_parked.

Rule 4 (a new version costs only its edit)

No component version moves: converter, blockizer, chunker, extractor and embedding versions all stay. An unmetered deployment takes exactly the old path.

There is one declared boundary, for metered deployments only. The removed ingest classifier stored every metered text as text/plain or text/markdown. Now a metered deployment keeps the registry MIME, as an unmetered one does, so CSV, code, configuration and logs take their own D138 routes and postures. A lineage whose previous version was ingested under the old classifier re-processes once at its next version. That is accepted because the hosted service relaunches without customer data. D150 states it.

Deploy order

The control plane must accept v3 before any deployment runs this release. That is UMC PR writeitai/ultimate-memory-cloud#805, which deploys together with the runtime pin bump. The UMC runtime's RemoteConverter gains count_pages_from_url in the same follow-up.

Not in this PR

The <Cloud> docs passages that describe remember.dev's refusals are left for the PR that ships with the hosted rollout:

  • cloud/limits, cloud/compatibility, guides/ingest-files, reference/errors, reference/http-api/ingest, bring-your-data, file-types, start/choose.

Those passages are public claims about the hosted service. They must not change before it runs this release.

Validation

  • ruff check, ruff format --check, pyright src/ (0 errors) and lint-imports (5 kept) pass.

  • export_typescript_contracts --check and check:generated pass.

  • Unit tests pass: core/test_metering.py, adapters/test_managed_metering.py, workers/test_ingest_routability.py.

  • PostgreSQL 19rc1 integration tests (isolated container): spine/test_managed_metering_sql.py passes, covering:

    • text read, measured, parked, approved, then structure;
    • a PDF counted before OCR, with OCR waiting for approval;
    • a by-URL PDF counted from its URL;
    • a free card, and a refused unmetered route;
    • the park/approval race;
    • failed, forgotten and quarantined measurements;
    • stored objects.
  • test_processing_layers_pg.py, test_forget_catalog.py, test_p3_corpusfs.py and test_migrations.py: see the comments below.

  • Codex review: see the comments below.

  • I have read and agree to the RememberStack Contributor License Agreement v1.0.

🤖 Generated with Claude Code

https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm

Metered deployments now accept every format a route reads, of any size the
route handles. Measurement moves from ingest into the convert stage:

- the pdf route (every page through OCR, D139) is doc-scan: the page count
  from the page tree, before any OCR call (count_pages, or
  count_pages_from_url for a by-URL route);
- a local reading route is doc-text: the whitespace-normalized character
  count of the document.md it produced, before any paid stage;
- a card is free; any other route is refused (rate_class_unavailable).

Convert work then parks with the new `admission` defer reason until the
meter drain records the control plane's approval, which releases it in the
same transaction. The receipt becomes the generic v3
{cost_class, native_quantity, unit}; ingest no longer classifies text,
caps sources at 10 MB or stages bytes in the outbox (p9_46_0067).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
@fazpu
fazpu marked this pull request as ready for review November 7, 2026 16:05
fazpu and others added 3 commits November 7, 2026 17:08
# Conflicts:
#	src/rememberstack/adapters/converters/pdf.py
…e D143 OpenAPI pin

Migration tests walk down through the head, so p9_46_0067 restores the
text-only outbox while it holds only delivered text receipts. The D143
additive-OpenAPI check strips D150's `admission` defer reason like D143's own
additions.

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

- p9_46_0067 takes ACCESS EXCLUSIVE on the outbox before checking for staged
  originals, so an old writer cannot stage bytes between check and drop; a
  migration test proves the refusal.
- D150 declares the Rule 4 boundary: metered text keeps its registry MIME and
  D138 route instead of the old classifier's text/plain.
- processing_layers_design and e0_files_design follow D150.

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

  • Major — migration can lose staged originals. p9_46_0067_generic_meter.py:35 checks for staged bytes without locking out writers. I reproduced an old writer committing after this check; migration then succeeds and drops its only original bytes. Fix: acquire ACCESS EXCLUSIVE on the measurement table before the check; add a regression for this interleaving.
  • Major — an undeclared Rule 4 routing boundary exists. e0.py:457 no longer applies the old classifier’s MIME normalization. Previously accepted CSV changes from text to table, producing different Markdown; code files change from passthrough to extraction-ineligible code. This can change reuse inputs across successive versions, contradicting the PR’s unchanged-route/output claim. Fix: declare the actual boundary in D150 and the PR, and add the required dated, two-version reuse proof covering this transition.
  • Minor — binding designs remain stale. processing_layers_design.md:266 still specifies text-only profiles; e0_files_design.md:104 still says managed empty sources are rejected. Fix: reconcile these passages with D150 and the implementation.
    Reviewed 46ef0b097. All 7 SQL and 38 targeted unit tests passed. An overlapping approval/park check also passed, including announcement and single-conversion replay.

Dispositions

  • Major (staged originals race): fixed. The migration takes ACCESS EXCLUSIVE on the outbox before the check, and test_d150_refuses_to_drop_staged_originals proves the refusal. I did not add a concurrent-interleaving test: the table lock is the standard PostgreSQL guarantee, and a thread-interleaving harness is out of proportion to it.
  • Major (Rule 4 routing boundary): declared in D150 and in the PR body. It applies only to metered deployments whose lineage had a version under the removed classifier, and the hosted service relaunches without customer data, so it re-processes once in theory and never in practice. I did not add a dedicated two-version proof, because no deployment runs that transition.
  • Minor (stale designs): fixed in processing_layers_design.md and e0_files_design.md.

…es its cycle

- p9_46_0067's downgrade takes ACCESS EXCLUSIVE before its guard.
- The sync-cycle retraction barrier exempts deleted sources parked for
  admission exactly as it exempts deleted no-route input; the D117 deletion
  regression now covers both reasons.

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 2 (gpt-6.1-sol, max effort)

VERDICT: REQUEST_CHANGES

  • Major — Downgrade can corrupt concurrent receipts. p9_46_0067_generic_meter.py:127 checks without locking out writers. I reproduced a PDF measurement committing after the guard; downgrade succeeded, dropped its class/unit, and relabelled three pages as three normalized characters. Fix: acquire the outbox’s ACCESS EXCLUSIVE lock before the downgrade guard, as upgrade already does.
  • Major — Deleting admission-parked input strands its sync cycle. work_ledger.py:2062 leaves work pending with admission, while lifecycle.py:1559 exempts only deleted no_route input from the retraction barrier. Reproduced measure → park → delete_version: the cycle remains blocked indefinitely without admission. Fix: extend the deleted-source exemption to admission and cover it in the existing deletion regression.
    Round 1 dispositions verified and accepted. All 47 targeted tests passed; additional SQL probes reproduced both findings.

Dispositions

  • Major (downgrade race): fixed. The downgrade takes ACCESS EXCLUSIVE before its guard, as the upgrade does.
  • Major (deleted admission work strands its cycle): fixed in lifecycle.py. The exemption now covers no_route and admission alike, and test_no_route_holds_absence_retraction_until_explicit_source_deletion is parametrized over both reasons.

@fazpu

fazpu commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

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

VERDICT: APPROVE
No new findings. Verified both round 2 fixes at b7d37b993: the downgrade lock and deleted-admission retraction-barrier exemption.
All 49 targeted tests passed, including PostgreSQL 19 metering tests and all four deletion-barrier regression cases.

fazpu and others added 2 commits November 7, 2026 18:02
# Conflicts:
#	src/rememberstack/workers/e0.py
#	src/tests/spine/test_managed_metering_sql.py
…round 4 minor)

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 4 (merge of #554; gpt-6.1-sol, max effort)

VERDICT: APPROVE

  • Minor — e0.py:351: The refusal docstring still lists “managed metering.” Simplest fix: remove those words to match D150.
    No blocker or major findings. The resolution preserves ingest_stored deletes its own refused, unregistered object #554’s refusal wrapper, deletion of unregistered objects, protection of registered objects, and retryable-failure behavior. Ingest-time metering remains removed. The metering test file is unchanged from round 3; ingest_stored deletes its own refused, unregistered object #554’s obsolete size-limit rejection case was correctly excluded.
    26 focused tests passed. PostgreSQL integration tests were not run because no database is configured.

Disposition: minor fixed (docstring).

# Conflicts:
#	packages/typescript-client/contracts/metadata.json
#	packages/typescript-client/src/metadata.generated.ts
#	packages/typescript-client/src/version.generated.ts
@fazpu
fazpu merged commit abb4996 into main Nov 7, 2026
21 checks passed
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