Skip to content

feat(mcp): require approval for destructive batch record writes - #507

Merged
alosec merged 4 commits into
mainfrom
alex/dev-14372-confirmed-batch-writes
Nov 6, 2026
Merged

alosec merged 4 commits into
mainfrom
alex/dev-14372-confirmed-batch-writes

Conversation

@alosec

@alosec alosec commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Destructive set_party_role and set_custom_field calls now preview every planned change and require approval. Purely additive calls still run immediately, including without a confirmation secret. Mixed batches wait as a whole; approvals bind every record read, including unchanged records. Preserve the existing per-record result shape and gateway deadline.

Second part of #451 / DEV-14372, based on #506. Includes token and form-elicitation wiring, party-ID validation, custom-field updated_at and role-assignment replay protection, tracking-create auditing, and fixture-backed transport tests. Original author: Akshay Dodeja.

Also resolves the outstanding #451 batch-429 review finding: stop starting further writes, retain Retry-After and partial results, release approval only if no write succeeded or may have applied. Five regression tests cover read limits, fully refused retries, partial success, untouched records, and an attach succeeding before a rate-limited detach.

Deployment: set T49_MCP_CONFIRMATION_SECRET (at least 32 characters) on the HTTP gateway before deploying. Destructive writes fail closed without it; stdio generates a process-local secret when none is configured.

Validation: full npm run agent-verify; separately, 37 real gateway-handler tests pass. No live account writes performed. Lifecycle tools remain in the next PR.


Proof this PR
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Review order: #506 → #507 → #508 → #509. Each diff is against its predecessor; #509 is the combined QA target.

Follow-up review: Starting tracking now explicitly warns about tracking quota and possible pay-as-you-go charges, and advertises client interaction despite remaining additive/non-destructive. Clients own approval UI; metadata does not guarantee a prompt. Concurrent record edits between preview/read and save remain possible because backend writes are unconditional.

RetriggerView in GreptileConfidence Score: 2/5

Fix the batch confirmation checks, unbounded identity reads, and hidden read limits before merging.

Fix All in CodexFindings

  1. P1 Changed batches skip approval checks ▶
  2. P1 Identity reads exceed the deadline ▶
  3. P1 Read limits still allow writes ▶
Fix with agent prompt
### Issue 1
packages/mcp/src/tools/record-writes.ts:626
On a mixed batch's confirmation call, `counts.losing` can become zero because the destructive record now fails to read or another user cleared it. The additive branch then writes without checking the token or comparing the approved state. A record that matched during preview can become empty and get filled without returning `stale_preview`.

Keep checking the full batch state when `confirmation_token` or `requestState` is present. Only fresh additive calls should skip approval.

### Issue 2
packages/mcp/src/tools/write-kit.ts:828-834
The new calls to `accountId()` and `credentialKey()` run outside the batch deadline. API-key calls without a gateway account need `/me` for `accountId()`, and user or agent calls need it for `credentialKey()`. A slow `/me` can hold a destructive preview or confirmation past the gateway's 20-second budget and 30-second function limit, leaving the caller with a timeout instead of a result.

Bound these required lookups by the remaining request budget and return without writing when it expires.

### Issue 3
packages/mcp/src/tools/record-writes.ts:568-576
This branch only sees a 429 after the SDK finishes its automatic GET retries, which the real server leaves enabled. If a record read receives `Retry-After: 30`, the roughly 10-second read budget expires first. The record becomes `not_attempted`, `read.rateLimit` stays unset, and other records can still be written. The response also loses the API's requested wait.

Make batch reads expose 429 immediately, or pass the rate-limit signal through before retrying, so the batch stops writes and preserves `Retry-After`.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds approval previews for destructive set_party_role and set_custom_field batches while keeping additive calls immediate.

  • Destructive record batches show every planned change and wait for approval.
  • Tracking calls warn about charges and record their outcomes.
  • MCP logs hide approval tokens and signed URLs.
  • Rate-limit errors expose the wait requested by the API.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Read batch records] --> B{Any destructive change?}
  B -->|No| C[Write additive changes]
  B -->|Yes| D[Build account and credential binding]
  D --> E{Approval supplied?}
  E -->|No| F[Return preview or ask client]
  F --> A
  E -->|Yes| G[Check token and batch state]
  G -->|Changed| F
  G -->|Matches| H[Reserve approval and write]
  H --> I[Return per-record results and audit]
Loading

Reviews (1) · Last reviewed commit: "fix(mcp): flag tracking quota and billin..." · Reviewed by Greptile

@vercel

vercel Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
api Ready Ready Preview Nov 6, 2026 8:29pm UTC

Request Review

@linear-code

linear-code Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

DEV-14372

@github-actions github-actions Bot added the risk-shadow:high Shadow-only risk signal: high label Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Shadow risk calibration

Signal Result Authority
Path classifier risk:high Deterministic advisory policy
heuristic fallback risk-shadow:high Shadow only

Calibration result: agreement.

Shadow risk never blocks or enables merge. Keep it in observation mode until roughly 50 pull requests have human-reviewed labels, then calibrate before considering any policy use.

@alosec
alosec changed the base branch from alex/dev-14372-write-safety-foundation to main November 6, 2026 20:52
@mintlify

mintlify Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
terminal49 🟢 Ready View Preview Nov 6, 2026, 8:53 PM

💡 Tip: Enable Automations to automatically generate PRs for you.

@github-actions github-actions Bot added the risk:unknown Advisory evidence unavailable or changed; no merge permission label Nov 6, 2026
@alosec
alosec marked this pull request as ready for review November 6, 2026 20:52
@alosec
alosec merged commit 793ff4f into main Nov 6, 2026
33 checks passed
@alosec
alosec deleted the alex/dev-14372-confirmed-batch-writes branch November 6, 2026 20:54
failed: unreadable,
not_attempted: notRead,
};
if (counts.losing === 0) return { additive: preview };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Changed batches skip approval checks

On a mixed batch's confirmation call, counts.losing can become zero because the destructive record now fails to read or another user cleared it. The additive branch then writes without checking the token or comparing the approved state. A record that matched during preview can become empty and get filled without returning stale_preview.

Keep checking the full batch state when confirmation_token or requestState is present. Only fresh additive calls should skip approval.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mcp/src/tools/record-writes.ts
Line: 626

Comment:
**Changed batches skip approval checks**

On a mixed batch's confirmation call, `counts.losing` can become zero because the destructive record now fails to read or another user cleared it. The additive branch then writes without checking the token or comparing the approved state. A record that matched during preview can become empty and get filled without returning `stale_preview`.

Keep checking the full batch state when `confirmation_token` or `requestState` is present. Only fresh additive calls should skip approval.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Codex Fix in Claude Code

Comment on lines +828 to +834
const binding: ConfirmationBinding = {
tool: plan.tool,
args,
accountId: await context.accountId(),
credentialHash: await context.credentialKey(),
channel: channel.kind,
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Identity reads exceed the deadline

The new calls to accountId() and credentialKey() run outside the batch deadline. API-key calls without a gateway account need /me for accountId(), and user or agent calls need it for credentialKey(). A slow /me can hold a destructive preview or confirmation past the gateway's 20-second budget and 30-second function limit, leaving the caller with a timeout instead of a result.

Bound these required lookups by the remaining request budget and return without writing when it expires.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mcp/src/tools/write-kit.ts
Line: 828-834

Comment:
**Identity reads exceed the deadline**

The new calls to `accountId()` and `credentialKey()` run outside the batch deadline. API-key calls without a gateway account need `/me` for `accountId()`, and user or agent calls need it for `credentialKey()`. A slow `/me` can hold a destructive preview or confirmation past the gateway's 20-second budget and 30-second function limit, leaving the caller with a timeout instead of a result.

Bound these required lookups by the remaining request budget and return without writing when it expires.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Codex Fix in Claude Code

Comment on lines +568 to +576
const read = await forEachRecord(
plan.ids,
(id) => plan.planRecord(id),
readDeadlineAt,
);
planned = read.done;
unreadable = read.failed;
notRead = read.not_attempted;
if (read.rateLimit !== undefined) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Read limits still allow writes

This branch only sees a 429 after the SDK finishes its automatic GET retries, which the real server leaves enabled. If a record read receives Retry-After: 30, the roughly 10-second read budget expires first. The record becomes not_attempted, read.rateLimit stays unset, and other records can still be written. The response also loses the API's requested wait.

Make batch reads expose 429 immediately, or pass the rate-limit signal through before retrying, so the batch stops writes and preserves Retry-After.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/mcp/src/tools/record-writes.ts
Line: 568-576

Comment:
**Read limits still allow writes**

This branch only sees a 429 after the SDK finishes its automatic GET retries, which the real server leaves enabled. If a record read receives `Retry-After: 30`, the roughly 10-second read budget expires first. The record becomes `not_attempted`, `read.rateLimit` stays unset, and other records can still be written. The response also loses the API's requested wait.

Make batch reads expose 429 immediately, or pass the rate-limit signal through before retrying, so the batch stops writes and preserves `Retry-After`.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Codex Fix in Claude Code

This branch was successfully deployed

2 active deployments
staging - docs — 03880df3 Deployed Nov 6, 2026 by mintlify[bot]
Preview — 03880df3 Deployed Nov 6, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:unknown Advisory evidence unavailable or changed; no merge permission risk-shadow:high Shadow-only risk signal: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant