Repository navigation
fix(traffic): read a bot log file in one transaction, and prune bot rows in batches - #1223
Merged
gkhngyk merged 3 commits intoOct 20, 2026
Merged
Conversation
…d rate-limit unknown secrets by IP /bots/ingest read up to 20 MB into memory before the limiter and the secret check, so a caller without a secret could make the server take in the whole body. The route is now mounted before the app's body parsers and runs limiter, then the secret lookup, then the raw body read. The limiter keyed every Bearer string by its hash, so made-up secrets got a fresh bucket, and a lookup, per request. Only a secret the route has matched to a live connection now earns the connection's bucket (a cache like the dashboard's verified tokens, kept apart from it); an unknown one counts against the caller's IP. Follow-up to ansvisor#1179's review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ows in batches A Logpush file or a Vercel drain's delivery was read in steps, each its own transaction: the claim, the raw count, then the verified events 500 at a time. When a later step failed, the claim was given back and the sender's retry counted the raw records, and the chunks already written, again. ai_bot_ingest_file (00189) takes all three in one transaction, so a failure leaves nothing behind and the retry is read as new; a delivery of the same file meanwhile waits on the claim's key, then reads as a duplicate. A file holds at most 20,000 lines, so its events fit one call. A read-only check still answers a resent file before it is opened, and skips verifying files once the month's raw cap is reached (compared with today's cap, so a raised plan reads again). The daily prune was three unbatched DELETEs. ai_bot_prune deletes at most 5,000 rows a table per call, and the daily run calls it until a call deletes fewer (200 calls at most; the rest waits a day). Follow-up to ansvisor#1179's review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
Author
|
@gkhngyk This covers the other two #1179 notes:
It's stacked on #1222, so please merge that one first. Migration 00189 needs applying before the server deploy; it is on the dev DB already. On the raw cap: a capped file is still only counted, not verified, through a read-only pre-check. The transaction decides under the usage lock as before. |
This was referenced Oct 19, 2026
Merged
gkhngyk
reviewed
Oct 20, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
ai_bot_ingest_file makes one log file a single transaction:
- an
errorresult raises, so the claim, the usage upsert and the raw count roll back with it; - the same file arriving twice waits on the claim, then reads as a duplicate;
- the locks are taken in the same order as the Worker path.
I'll apply 00189 before the server deploy that carries this.
For a follow-up:
- Prune budget: 200 calls × 5,000 rows is 1M rows per table per day across all orgs. Enterprise's cap (20M events a month) is about 650k a day per org, so two Enterprise orgs at cap expire more than that once data is 90 days old. Bound the loop by time rather than by call count.
- Request size: a whole file now goes in one RPC body. With pages up to 2,048 characters, that can reach about 40 MB of JSON.
normalize_page_pathdrops the host, query and fragment anyway, so stripping them in Node first keeps it small.
4 of 12 tasks
gkhngyk
approved these changes
Oct 20, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
00189 is applied on prod: ai_bot_ingest_file and ai_bot_prune are service-role only. Server tests pass on main (1,832). The prune budget and request size notes can be a follow-up.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
These are the other two follow-ups from #1179's review: the partial-failure recount and the unbatched prunes.
1. A failed read recounted a log file.
ai_bot_ingested_files);ai_bot_count_raw);ai_bot_ingest).ai_bot_ingest_file(migration 00189) takes all three steps in one transaction:MAX_LOG_LINES), so its events fit one call.claimLogFile,releaseLogFile,countRawRecordsandWRITE_CHUNKare gone.logFileRead: a resent file is answered before it is gunzipped or parsed.rawCapReached: once the month'sraw_recordsreach the raw cap, a file is only counted, not verified, as before. It compares the count with today's cap, so a plan raised mid-month reads again.2. The prunes were unbatched.
pruneBotEventsran three plain DELETEs, which can exceed the statement timeout at Enterprise volume.ai_bot_prune(00189) deletes at mostp_limitrows from each table per call:ai_bot_events,ai_bot_dailyandai_bot_ingested_files.The Worker path is unchanged: one batch of at most 500 requests, one
ai_bot_ingestcall.Deploy: apply 00189 before the server. Without it, log senders' files fail with 500 (the sender retries), and the prune logs a warning.
00189 rather than 00188, which #1210 holds.
schema.sqlis rebuilt here without 00188; whichever of the two merges second needsbuild-schema.shagain.Related issue
Follow-up to #1179 (review of 2026-10-19 17:39 UTC). Stacked on #1222.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
I applied 00189 to the dev DB and called the functions with a temporary
vercel_drainconnection:raw5{duplicate: true}, nothing countedraw_recordsstill 5,stored_eventsstill 2raw8 (counted once){raw_capped: true}, raw counted (9), no events storedai_bot_prune(…, 2)with 3 old events and 2 old daily rows{events: 2, daily: 2}, then{events: 1, daily: 0}; recent rows untouchedThen I sent a Vercel drain delivery twice through the branch's route on :8081 against the dev DB:
duplicateand nothing changed.All temporary rows were deleted afterwards.
Unit tests (
src/lib/bot-ingest.test.js,src/routes/bot-ingest.test.js) cover:Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/): web not touchedyarn typecheckpasses (run fromweb/): web not touchedyarn format:checkpasses (run fromweb/): web not touchedAlso run:
npm run format,npm run lint,npm test(1829 passed);bash supabase/build-schema.sh.🤖 Generated with Claude Code