gRPC parity check #
Classification: Internal-Only
INTERNAL - DO NOT FORWARD
Proves the moved MCP tools answer the same on the new gRPC reads as on the old REST path before any public release (ENG-10053). Two parts share one diff engine and one list of expected differences (apps/tofu-external-mcp/src/parity/):
- The comparison job,
mise run tofu-external-mcp-parity [report-file]. It replays recorded calls against a running MCP and fails on any difference not listed below. - Shadow mode, in the MCP itself. The caller keeps the REST reply; the same read also runs on gRPC and one
grpc_parity_shadowlog line records the difference.
Neither one ever logs or reports a value: only field paths, difference kinds and counts. metadata is compared whole and never broken into its keys, because those keys are stored data.
What “old” and “new” mean #
A call with only the old inputs takes REST, with one exception: with the flag on, get_invoice and get_direct_expense_by_extraction are served over gRPC in REST’s shape (ENG-10194), with REST as fallback. For those two the job compares that REST-shaped reply with the fields reply. A call with a new input (fields, page_token, and for get_bank_statement also version_view, transactions_of and n_per_page) takes gRPC, and fails while the flag (TOFU_EXTERNAL_MCP_GRPC_READS) is off. So the job does not compare “flag off” with “flag on”. It compares the recorded old-input call (REST) with the same call plus fields: ["*"] (gRPC), with the flag on.
Some calls need more than one gRPC call to match one REST reply:
get_bank_statement: REST returns each account’s latest version with its transactions, as gRPC’s defaultversion_view: "LATEST"does. On gRPC the job reads that view page by page, then each version’s transactions withtransactions_of.search_*: on gRPC the job followsnext_page_token.
Running the job #
- Start the stack (
mise run dev), or pointMCP_E2E_URLat a deployed dev or staging and setMCP_E2E_ALLOW_REMOTE=1andPARITY_DOPPLER_CONFIG. - Turn the gRPC reads on for that MCP:
TOFU_EXTERNAL_MCP_GRPC_READS=true(on in the dev overlays; locally in Dopplerbonsai/dev_local, thenmise run dev). - Run
mise run tofu-external-mcp-parity report.md. It exits non-zero on any FAIL. On the local stack,PARITY_LOGIN=browsersigns in as you instead of the e2e user: it prints a sign-in link and waits for the redirect on the.mcp.jsoncallback port.
The job logs in as the e2e user and finds its test records in up to 5 of that org’s entities, reading up to 10 records of each type (PARITY_ENTITY_IDS pins the entities). For each edge case it picks one record:
- invoices and direct expenses: the first one, the one with the most versions (at least 3), one in an uncommon currency, and the one with the fewest filled fields;
- an expense claim;
- bank statements: one with several accounts, and one with over 100 transactions, so transaction paging is exercised;
- a document and an extraction in
ERROR; - each record’s extraction.
Credit notes have no moved tool, so they are not in the corpus. A case with no matching record shows as NO FIXTURE and fails the run, so add that record to the e2e org rather than drop the case. The recorded calls are inputs only, in src/parity/corpus.ts.
The report holds the per-tool PASS/FAIL table, every unexplained difference (tool, call, path, kind, count), and the expected differences seen with their reasons. Failed calls are named on stderr by kind only (tool error, HTTP 500), never with their message.
The MCP allows 60 tool calls a minute per user. The job waits out a 429 and goes on. The e2e token is not refreshed, so when it expires the job stops with exit code 2 and says so; run again, or pin fewer entities.
Shadow mode #
Shadow mode has its own Redis key, so it can be sampled in production and turned off without a redeploy. org_ids (Clerk organization ids) are always shadowed, percent samples the other calls, and tools limits which tools. It runs whether TOFU_EXTERNAL_MCP_GRPC_READS is on or off:
SET tofu-external-mcp:grpc-shadow '{"org_ids":["org_…"],"percent":5}'
- Only reads are shadowed. The writes (
create_direct_expense_data,upsert_bank_statement_data) never are, because a second run would save a second version. - The reply is never affected. The shadow run starts after the REST reply is ready and is not awaited, and any failure on it is caught.
- What is logged. One
grpc_parity_shadowevent per shadowed call, with:unexplained: path, kind and count for each difference;unexplained_count;expected: the id and count of each rule that applied;parity_failure: when the new path failed, its reason code (e.g.UNAVAILABLE,http_404), never the message.
- Calls not shadowed. A call that already uses new inputs, an
action_typescall, anupdated_aftercall onlist_documentsorlist_extractions, a REST page past the first, or any call while 8 shadow runs are already in flight on that pod. The cap bounds the extra bonsapi load at anypercent. - Read quota. Shadow reads run with the caller’s token, so they count against the organization’s gRPC read quota (600 units a minute per service,
apps/bonsapi/src/grpc/quota.rs) like its ownfieldsreads, up to 10 calls per shadowed call. Keeppercentlow, and narroworg_idsortoolsfor busy organizations, so shadowing cannot starve their real reads. - Call budget. A shadow run makes at most 10 gRPC calls. A bank statement needs one call per version and per page of transactions, so a large one stops there and logs
not_compared: shadow_call_budgetwith resultsuccess; it is not a parity failure. Use the comparison job for those.
To count shadowed calls, create this Datadog log-based metric (it needs log-metric write access, which the Datadog connector does not have):
- id:
tofu_external_mcp.grpc_parity_shadow.count, compute: count; - filter:
@service:tofu-external-mcp @event_name:grpc_parity_shadow; - group by
@tool_name,@resultand@parity_failure(the logger writes these fields at the top level).
Rolling back #
None of it changes stored data or the schema.
| What | Turn off | Takes effect |
|---|---|---|
| gRPC reads and writes (0.8) | Unset TOFU_EXTERNAL_MCP_GRPC_READS in the overlay or Doppler |
When the pods restart |
| Shadow mode only | DEL tofu-external-mcp:grpc-shadow, or set its org_ids to [] and percent to 0. The gRPC reads flag is untouched |
Within 15 seconds on each pod (flag cache) |
| The comparison job | Nothing runs unless started by hand. It only reads | Immediately |
The grpc_parity_shadow metric |
Delete the log-based metric | Immediately |
| The code | Revert the commit. It is additive: a log event name, the parity/ module, a wrapper on 11 read handlers |
Next deploy |
Known limits:
- During a Redis outage the shadow flag cannot change. Each pod keeps the last shadow flag it read, so a
DELonly applies once Redis is back. To stop shadowing during an outage, redeploy the previous image. - After a rollback, calls with new inputs are refused.
fields,page_tokenand the other new inputs answer 400 with “call again without it”, and a caller paging withpage_tokenmust start again from the first page. action_typesstops working on rollback. With the flag on,get_invoice_dataandget_direct_expense_datawithaction_typesuse gRPC. With it off they fall back to REST, which answers “Not found” for that filter. The REST bug predates this work.
Expected differences #
These are the reviewed list (0.10.1). The code is src/parity/expected-differences.ts; a difference matched by none of them fails.
Mappings, not differences. Before comparing, REST names are mapped to gRPC names and the reply envelopes are left out:
invoice_data→data, andinvoice_pages→pages;- the paging and size fields REST added (
page_number,total_count,truncated,line_items_truncated,over_size_budget, and on gRPCnext_page_token,has_more,read_time,resume_time,not_found_data_ids).
Each get_invoice_data and get_direct_expense_data row on gRPC is the invoice or direct expense with that version under data; only the data part is compared. Values are compared as values:
- numbers are compared as decimals, to the precision a float holds (gRPC amounts reach the caller as numbers, as REST’s do); a number and a decimal string are equal when they are the same decimal; two strings are compared as text;
- null, absent and
[]are equal; - rows with ids are matched by id, so order does not matter;
- gRPC
metadata(other,traces,bboxes) is flattened back to REST’s shape, and metadata stored as a non-object ({ value }on gRPC) is compared as that value.
| Id | Kind | Tools | Reason |
|---|---|---|---|
new_concurrency_fields |
added | all | gRPC returns the etag a write needs, omitted_mask, latest_data_id, is_unfinished, extraction_status and is_extraction_processing (tofu still reading the document). REST had none. |
new_link_fields |
added | all | Links and counts so a caller reaches the related record without a second lookup: an extraction’s document, file name, and invoice, direct expense, bank statement or credit note; a document’s extraction ids and types and page count; a bank statement version’s transaction count; a transaction’s external id. |
list_rows_complete |
added | list_documents, list_extractions |
REST projected a fixed summary per row; fields: ["*"] returns the whole row. |
redundant_parent_ids |
missing | all | Rows, sync activities and pages repeated their parent’s ids (entity_id, invoice_id, invoice_data_id, direct_expense_id, direct_expense_data_id, bank_statement_id); the parent in the same reply carries them. |
rest_search_summary |
added | search_invoice_data, search_direct_expense_data |
REST search returned a summary without source, publish or import state, sync activities, creator, is_extraction_updating or pages; gRPC returns them as a single read does. |
grpc_caller |
added | all | caller, the user making the call (ENG-10194). REST never returned it on its own. |
grpc_line_created_at |
added | all | A line’s created_at: REST’s spec lists it, but REST never sends it. |
reread_previous_version |
conditional | get_invoice, get_direct_expense_by_extraction |
While tofu re-reads a document, gRPC returns the version before the unfinished one; data is not compared on such a call. |
search_exact_version |
conditional | search_invoice_data, search_direct_expense_data |
gRPC returns each version asked for; REST returned one version per invoice or direct expense. A row is compared only when both sides hold the same version; an invoice REST found and gRPC did not still fails. |
rest_bank_statement_creator_unloaded |
conditional | get_bank_statement |
REST v2 never loaded a version’s creator, only its creator_id. A null REST creator is compared as gRPC’s creator when that creator’s id is the REST creator_id. |
rest_line_item_cap |
conditional | get_invoice, get_direct_expense_by_extraction |
REST cut long line lists; lines past its cut are not compared. |
list_page_boundary |
conditional | list_documents, list_extractions |
When either side has more pages, the two first pages can hold different rows because their order differs; only rows both hold are compared. |
proto_zero_value |
conditional | all | gRPC leaves out a field holding false, 0 or ""; absent on gRPC equals that value on REST, but only for a field gRPC’s field catalog defines. |
rest_byte_cap |
conditional | all | REST dropped rows over its byte budget; those rows are not compared. |
get_bank_statement with version_view: "ALL" returns every version, which REST never offered; it is not compared.
Fields only REST has #
The gRPC types do not have these fields. A call without fields still gets them in REST’s shape; only a caller who asks for fields goes without. Each is an expected difference (rest_only_*, kind missing):
| Id | Resource | Only in REST |
|---|---|---|
rest_only_invoice |
Invoice | publish_status, import_status (derived) |
rest_only_document |
Document | checksum, document_pages (the MCP summarised them), error_description, extraction_statuses, source_submitter_id, source_extraction_type, updated_at, was_extracted |
rest_only_extraction |
Extraction | data, pages, is_ai_page_classified, is_awaiting_page_review, is_sample, updated_at |
rest_only_bank_statement_version |
Bank statement version | sync_status, sync_error_type, sync_error_messages, synced_at (gRPC has sync_activities), creator_id (gRPC has creator) |
Reviewed and approved for ENG-10053 on 2026-11-30.
Not compared #
create_direct_expense_data: a write. It is neither shadowed nor replayed, and its reply is the direct expense thatget_direct_expense_by_extractioncompares.upsert_bank_statement_data: a write, moved to gRPC in ENG-10191. Same as above; its reply is the bank statement thatget_bank_statementcompares.create_invoice_data: not moved yet (ENG-10193). Add it tosrc/parity/when it moves.- The accuracy eval and its pass bar: owned by ENG-10064; a gate below.
Before the flag goes public #
TOFU_EXTERNAL_MCP_GRPC_READS stays off in production until each of these is done and recorded on ENG-10053:
- The job passes on deployed dev and on staging, with the report posted on the ticket.
- Those runs compare every case: the e2e org holds an expense claim, a document in
ERRORand an extraction inERROR, so no case isNO FIXTURE. A local run cannot make these records. - The 0.12 accuracy eval (ENG-10064) meets its pass bar.