Skip to content

fix(traffic): read a bot log file in one transaction, and prune bot rows in batches - #1223

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:fix/bot-ingest-file-transaction
Oct 20, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:fix/bot-ingest-file-transaction

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

These are the other two follow-ups from #1179's review: the partial-failure recount and the unbatched prunes.

Stacked on #1222. Merge that one first. Until then, this PR also shows #1222's commit; this PR's own change is b4c7de52.

1. A failed read recounted a log file.

  • A Logpush file or a Vercel drain's delivery was read in steps, each its own transaction:
    1. the claim (ai_bot_ingested_files);
    2. the raw count (ai_bot_count_raw);
    3. the verified events, 500 at a time (ai_bot_ingest).
  • When a later step failed, the claim was given back. The sender's retry then counted the raw records, and the chunks already written, a second time.
  • ai_bot_ingest_file (migration 00189) takes all three steps in one transaction:
    • a failure leaves nothing behind, so the retry is read as new;
    • a delivery of the same file that arrives meanwhile waits on the claim's key, then reads as a duplicate.
  • A file holds at most 20,000 lines (MAX_LOG_LINES), so its events fit one call.
  • The route no longer claims or releases anything itself. claimLogFile, releaseLogFile, countRawRecords and WRITE_CHUNK are gone.
  • Two read-only shortcuts keep the old costs. Both are re-decided inside the transaction:
    • logFileRead: a resent file is answered before it is gunzipped or parsed.
    • rawCapReached: once the month's raw_records reach 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.

  • pruneBotEvents ran three plain DELETEs, which can exceed the statement timeout at Enterprise volume.
  • ai_bot_prune (00189) deletes at most p_limit rows from each table per call: ai_bot_events, ai_bot_daily and ai_bot_ingested_files.
  • The daily run calls it with 5,000 until a call deletes fewer from every table. It stops at 200 calls; the rest waits a day, with a warning.

The Worker path is unchanged: one batch of at most 500 requests, one ai_bot_ingest call.

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.sql is rebuilt here without 00188; whichever of the two merges second needs build-schema.sh again.

Related issue

Follow-up to #1179 (review of 2026-10-19 17:39 UTC). Stacked on #1222.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

I applied 00189 to the dev DB and called the functions with a temporary vercel_drain connection:

Case Result
File with 5 records and 2 events stored 2, raw 5
Same hash again {duplicate: true}, nothing counted
File whose events fail to write (a bad timestamp) Error. Afterwards: no claim row, raw_records still 5, stored_events still 2
The retry of that file stored 1, raw 8 (counted once)
File past a raw cap of 8 {raw_capped: true}, raw counted (9), no events stored
ai_bot_prune(…, 2) with 3 old events and 2 old daily rows {events: 2, daily: 2}, then {events: 1, daily: 0}; recent rows untouched

Then I sent a Vercel drain delivery twice through the branch's route on :8081 against the dev DB:

  • the first was stored (raw +2, stored +1);
  • the second answered duplicate and nothing changed.

All temporary rows were deleted afterwards.

Unit tests (src/lib/bot-ingest.test.js, src/routes/bot-ingest.test.js) cover:

  • the single call;
  • duplicates;
  • capped files skipping verification;
  • a raised plan reading again;
  • locked plans;
  • the 80% alert;
  • the batched prune and its call budget;
  • the route without a claim release.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): web not touched
  • yarn typecheck passes (run from web/): web not touched
  • yarn format:check passes (run from web/): web not touched
  • Changes are focused — one concern per PR

Also run:

  • server: npm run format, npm run lint, npm test (1829 passed);
  • bash supabase/build-schema.sh.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 20, 2026 00:49
…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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk This covers the other two #1179 notes:

  • The partial-failure recount: one transaction per log file (ai_bot_ingest_file).
  • The unbatched prunes: ai_bot_prune, 5,000 rows a table per call.

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.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ai_bot_ingest_file makes one log file a single transaction:

  • an error result 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_path drops the host, query and fragment anyway, so stripping them in Node first keeps it small.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@gkhngyk
gkhngyk merged commit a2067b4 into ansvisor:main Oct 20, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants