Cut the per-call cost of mail_peer_changes_v1 to meet the I02 search budget #167

Closed
opened 2026-11-03 10:51:14 +00:00 by svc-agent-writer · 1 comment

Problem

mail_peer_changes_v1 fails the ADR 0012 rule 14 search budget. On the 210k-row store, mail_search p95 under back-to-back feed polling is 1.6× to 2.04× its idle p95. The budget is 1.25×. The other I02 budgets pass: feed page p99 118 ms, health p99 77 ms, RSS growth 7.4 MiB (PR #166, ADR 0008).

The architect measured scripts/bench_feed.py on voyager with a pause added between feed calls:

Feed calls per poller Loaded ÷ idle mail_search p95
Back to back (the #166 benchmark) 1.6, 2.04
One every 0.5 s 1.33
About one in total (control) 1.03, 0.99

The control shows that the benchmark is fair: the slowdown comes from the feed calls, and it grows with their rate. Each feed call costs about 90 ms of server time for a short indexed read. Candidate costs, not yet proved:

  • a new read connection and its PRAGMAs on every call (Store.peer_changes);
  • Python CPU time in the single api process, shared with mail_search;
  • the benchmark client and server sharing one process.

Change

Profile one feed call. Cut its server cost until the unchanged budget passes with the #166 benchmark's back-to-back polling. Do not change the budget or the load model (ADR 0012 rule 14). Keep the dedicated read view and the invariants of #151 and #160: no shared reader lock, one read transaction per page, and bounded work.

Acceptance

  • scripts/bench_feed.py passes every I02 budget on the 210k-row store. The PR quotes the I02 line with host and commit.
  • A profile in the PR names the removed cost.
  • make check passes.

Blocks

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com

## Problem `mail_peer_changes_v1` fails the ADR 0012 rule 14 search budget. On the 210k-row store, `mail_search` p95 under back-to-back feed polling is 1.6× to 2.04× its idle p95. The budget is 1.25×. The other I02 budgets pass: feed page p99 118 ms, health p99 77 ms, RSS growth 7.4 MiB (PR #166, ADR 0008). The architect measured `scripts/bench_feed.py` on voyager with a pause added between feed calls: | Feed calls per poller | Loaded ÷ idle `mail_search` p95 | | --- | --- | | Back to back (the #166 benchmark) | 1.6, 2.04 | | One every 0.5 s | 1.33 | | About one in total (control) | 1.03, 0.99 | The control shows that the benchmark is fair: the slowdown comes from the feed calls, and it grows with their rate. Each feed call costs about 90 ms of server time for a short indexed read. Candidate costs, not yet proved: - a new read connection and its PRAGMAs on every call (`Store.peer_changes`); - Python CPU time in the single `api` process, shared with `mail_search`; - the benchmark client and server sharing one process. ## Change Profile one feed call. Cut its server cost until the unchanged budget passes with the #166 benchmark's back-to-back polling. Do not change the budget or the load model (ADR 0012 rule 14). Keep the dedicated read view and the invariants of #151 and #160: no shared reader lock, one read transaction per page, and bounded work. ## Acceptance - `scripts/bench_feed.py` passes every I02 budget on the 210k-row store. The PR quotes the `I02` line with host and commit. - A profile in the PR names the removed cost. - `make check` passes. ## Blocks - dev/mail-mind-mcp#161 — its snapshot pages run under the same budget. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator

⏸ SVD architect gate: dev/mail-mind-mcp#167 cut-the-per-call-cost-of-mail-peer-changes-v1-to-meet-the-i0

Status: CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity standard · dispatch effort medium

Run: 20261103T105845Z-c8879574

Held because: CLEAN but 3 open risk(s) (1 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim:

Open risks:

  1. Does moving the two feed pollers' MCP clients into a child process count as changing the load model (ADR 0012 rule 14; the issue says not to change it)?

Recommendation: no. The same pollers, arguments, back-to-back cadence, searcher, health probe, window and budgets apply, and the server receives more feed calls (73–74 per poller vs 30).

Evidence: the spec-time probe on voyager shows that no server-only cut passes while the pollers' client-side jsonschema validation (~20 ms per page) runs in the measured process. With the server cut alone the ratio was 1.53/1.63; with both cuts, 1.07/1.09.

If the architect rules the bench must stay single-process, this scope cannot meet the budget, and the budget or the contract page size needs a decision. [decision]
2. Is it acceptable to drop the MCP SDK's server-side output-schema re-validation for peer success results?

Recommendation: yes. The documents are correct by construction. Every served shape is proven by the contract validator (validate_exchange with FormatChecker) in tests. Consumers validate on receipt per MCP. Peer errors already bypass the check today.

The alternative, a cached server-side validator, still costs 13.1 ms per page on the GIL-holding job thread and was not shown to pass. [decision]
3. The I02 pass is proven only after the change, on the host. Check: run uv run python3 scripts/bench_feed.py on voyager at least twice. Each I02 line must show passes.search == true (loaded/idle search p95 <= 1.25) with feed, health and rss also true, and is quoted in the PR with host, base_commit and implementation_sha256. This spec's 1.07/1.09 came from a monkeypatched simulation, not the implemented code. [verification]

Change: The spec-time profile (Half-A sandbox, host voyager) found the cost is not in the store. Store.peer_changes plus render_changes take ~1.3 ms per page, and the connection with its PRAGMAs takes 0.05–0.24 ms. About 92% of each feed call is jsonschema.validate of the result's structuredContent, run twice per call: once server-side in mcp/server/lowlevel/server.py:566-575 and once client-side in mcp/client/session.py:417 (the bench client shares the server's process). Each run costs ~20 ms per 100-event page.

  1. peer.py: add a ToolResult subclass (suggested name PeerResult) whose only override, to_mcp_result, returns CallToolResult(content, structuredContent, isError). result() at peer.py:433 constructs it. FastMCP's to_mcp_result (fastmcp/tools/base.py:161-177) then hands the SDK a CallToolResult, which returns early at lowlevel/server.py:546-547 without per-call re-validation. Follow the shape of _PeerTool.to_mcp_tool at server.py:1016-1021. server.py and store.py are not touched.
  2. scripts/bench_feed.py: move the two feed pollers' MCP clients into a poll child process (same entry file, ready/go/stop over stdin, token over stdin). The load model, budgets, report keys and passes logic stay unchanged; one key, feed_pollers, is added. The server, searcher, health probe and RSS sampler stay in the bench process.
  3. tests/test_peer_changes.py: add a server-side jsonschema spy test with a positive control, and a raw JSON-RPC wire-shape test.
  4. ADR 0008: add a #167 observations section.

A simulation of both cuts gave ratios 1.07 and 1.09 (pass). Each cut alone failed: 1.53/1.63 and 1.63/1.50.

Files: src/mail_mind_mcp/peer.py · scripts/bench_feed.py · tests/test_peer_changes.py · docs/adr/0008-evidence-scope.md

Decision: accept the 3 open risk(s) above and dispatch this spec (svd:go), or park (svd:skip) / re-spec (svd:respec)?

Your call (when this issue is held at the gate via svd:hold):

  • add label svd:go — approve; Half-B dispatch accepts exactly this spec (open risks accepted)
  • add label svd:fire beside svd:go — approve AND authorise the Half-B fire from the phone; never on its own
  • add label svd:skip — park it; dispatch refuses it and the issue stays for a human
  • add label svd:respec — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context

Spec: /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-03-cut-the-per-call-cost-of-mail-peer-changes-v1-to-meet-the-i0.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-03-cut-the-per-call-cost-of-mail-peer-changes-v1-to-meet-the-i0.md

### ⏸ SVD architect gate: dev/mail-mind-mcp#167 cut-the-per-call-cost-of-mail-peer-changes-v1-to-meet-the-i0 **Status:** CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity `standard` · dispatch effort `medium` **Run:** `20261103T105845Z-c8879574` **Held because:** CLEAN but 3 open risk(s) (1 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim: **Open risks:** 1. Does moving the two feed pollers' MCP clients into a child process count as changing the load model (ADR 0012 rule 14; the issue says not to change it)? Recommendation: no. The same pollers, arguments, back-to-back cadence, searcher, health probe, window and budgets apply, and the server receives more feed calls (73–74 per poller vs 30). Evidence: the spec-time probe on voyager shows that no server-only cut passes while the pollers' client-side jsonschema validation (~20 ms per page) runs in the measured process. With the server cut alone the ratio was 1.53/1.63; with both cuts, 1.07/1.09. If the architect rules the bench must stay single-process, this scope cannot meet the budget, and the budget or the contract page size needs a decision. [decision] 2. Is it acceptable to drop the MCP SDK's server-side output-schema re-validation for peer success results? Recommendation: yes. The documents are correct by construction. Every served shape is proven by the contract validator (validate_exchange with FormatChecker) in tests. Consumers validate on receipt per MCP. Peer errors already bypass the check today. The alternative, a cached server-side validator, still costs 13.1 ms per page on the GIL-holding job thread and was not shown to pass. [decision] 3. The I02 pass is proven only after the change, on the host. Check: run `uv run python3 scripts/bench_feed.py` on voyager at least twice. Each I02 line must show passes.search == true (loaded/idle search p95 <= 1.25) with feed, health and rss also true, and is quoted in the PR with host, base_commit and implementation_sha256. This spec's 1.07/1.09 came from a monkeypatched simulation, not the implemented code. [verification] **Change:** The spec-time profile (Half-A sandbox, host voyager) found the cost is not in the store. Store.peer_changes plus render_changes take ~1.3 ms per page, and the connection with its PRAGMAs takes 0.05–0.24 ms. About 92% of each feed call is jsonschema.validate of the result's structuredContent, run twice per call: once server-side in mcp/server/lowlevel/server.py:566-575 and once client-side in mcp/client/session.py:417 (the bench client shares the server's process). Each run costs ~20 ms per 100-event page. 1. peer.py: add a ToolResult subclass (suggested name PeerResult) whose only override, to_mcp_result, returns CallToolResult(content, structuredContent, isError). result() at peer.py:433 constructs it. FastMCP's to_mcp_result (fastmcp/tools/base.py:161-177) then hands the SDK a CallToolResult, which returns early at lowlevel/server.py:546-547 without per-call re-validation. Follow the shape of _PeerTool.to_mcp_tool at server.py:1016-1021. server.py and store.py are not touched. 2. scripts/bench_feed.py: move the two feed pollers' MCP clients into a `poll` child process (same entry file, ready/go/stop over stdin, token over stdin). The load model, budgets, report keys and passes logic stay unchanged; one key, feed_pollers, is added. The server, searcher, health probe and RSS sampler stay in the bench process. 3. tests/test_peer_changes.py: add a server-side jsonschema spy test with a positive control, and a raw JSON-RPC wire-shape test. 4. ADR 0008: add a #167 observations section. A simulation of both cuts gave ratios 1.07 and 1.09 (pass). Each cut alone failed: 1.53/1.63 and 1.63/1.50. **Files:** `src/mail_mind_mcp/peer.py` · `scripts/bench_feed.py` · `tests/test_peer_changes.py` · `docs/adr/0008-evidence-scope.md` **Decision:** accept the 3 open risk(s) above and dispatch this spec (`svd:go`), or park (`svd:skip`) / re-spec (`svd:respec`)? **Your call** (when this issue is held at the gate via `svd:hold`): - add label `svd:go` — approve; Half-B dispatch accepts exactly this spec (open risks accepted) - add label `svd:fire` **beside** `svd:go` — approve AND authorise the Half-B fire from the phone; never on its own - add label `svd:skip` — park it; dispatch refuses it and the issue stays for a human - add label `svd:respec` — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context <sub>Spec: `/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-03-cut-the-per-call-cost-of-mail-peer-changes-v1-to-meet-the-i0.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-03-cut-the-per-call-cost-of-mail-peer-changes-v1-to-meet-the-i0.md`</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dev/mail-mind-mcp#167
No description provided.