Commit ae05058
fix(mcp): sweep terminal approval rows past a retention window
Review finding on PR open-legal-products#247: terminal ledger rows (executed / failed /
denied / expired) were retained forever, and each one carries the full
tool-argument jsonb the model proposed — in a legal product that can be
privileged matter data (case ids, document excerpts, party names)
sitting in a table with no cleanup path.
WHY THIS MATTERS
Data-minimization is part of the security posture: every copy of
sensitive data is another thing a breach, a misconfigured export, or an
over-broad query can leak. The ledger's arguments payload exists for
exactly one purpose — showing the user what they are approving and
executing precisely that — and that purpose is over within minutes.
Terminal rows keep only short-term forensic value ("what ran today?"),
so they get a bounded lifetime (24h) instead of an unbounded one.
WHAT IS AN OPPORTUNISTIC SWEEP
Instead of standing up cron/job infrastructure for a low-traffic
table, cleanup piggybacks on an operation that already touches the
table: every new pending-call INSERT first deletes terminal rows older
than the retention window. The repo already uses insert-time cleanup
for the OAuth state store (saveCodeVerifier deletes the stale state
row before inserting the new one); this follows the same shape. The
table only grows while the approval flow is being used — which is
precisely when the sweep runs.
HOW THE FIX WORKS
await db.from("user_mcp_pending_tool_calls")
.delete()
.in("status", ["executed", "failed", "denied", "expired"])
.lt("created_at", cutoff); // now - 24h
- Only TERMINAL statuses are eligible: pending/approved/executing rows
are the approval flow's live state and are never deleted, whatever
their age (they are bounded anyway — expires_at retires them to a
terminal state within minutes).
- Best-effort: a sweep failure is logged and swallowed, because the
user is waiting on the approval prompt the insert serves; the next
insert retries the sweep.
- The sweep is deliberately global (not per-user): the service-role
backend is the only writer, and an active user's insert also clears
other users' aged-out rows, so retention holds even for users who
never return.
Test: an insert with aged executed/expired rows, a recent denied row,
and an old-but-live pending row on the table deletes exactly the aged
terminal rows and nothing else.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent 52f3f2f commit ae05058
3 files changed
Lines changed: 102 additions & 1 deletion
File tree
- backend
- migrations
- src/lib/mcp
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
18 | 24 | | |
19 | 25 | | |
20 | 26 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | | - | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
42 | 47 | | |
43 | 48 | | |
44 | 49 | | |
| |||
51 | 56 | | |
52 | 57 | | |
53 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
54 | 67 | | |
55 | 68 | | |
56 | 69 | | |
| |||
77 | 90 | | |
78 | 91 | | |
79 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
80 | 97 | | |
81 | 98 | | |
82 | 99 | | |
| |||
123 | 140 | | |
124 | 141 | | |
125 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
126 | 187 | | |
127 | 188 | | |
128 | 189 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
15 | 22 | | |
16 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
17 | 50 | | |
18 | 51 | | |
19 | 52 | | |
| |||
26 | 59 | | |
27 | 60 | | |
28 | 61 | | |
| 62 | + | |
29 | 63 | | |
30 | 64 | | |
31 | 65 | | |
| |||
0 commit comments