Skip to content

bt-daemon: expire stalled routes and reclaim session storage space - #287

Merged
Stephen Belanger (Qard) merged 2 commits into
mainfrom
design/expire-stuck-routes
Nov 17, 2026
Merged

Stephen Belanger (Qard) merged 2 commits into
mainfrom
design/expire-stuck-routes

Conversation

@Qard

@Qard Stephen Belanger (Qard) commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #276, the fourth in the stack: main ← #289 ← #291 ← #276 ← this PR. Part of the fix for SDK-830: pipeline/ SQLite files grew by tens of GB. Most of that space was in sessions whose destination kept rejecting spans with 413 or 400 errors, or whose span plugin failed.

Problem

With #276, a route waiting on a failure kept its head forever. A 413 or a missing-access 400 isn't fixed by new credentials, so the head never moved. Translation kept adding SpanOps behind it, and the session file was never removed. Separately, deleted rows never returned space to the file system:

  • Files created by main don't have auto-vacuum enabled, so freed pages stay inside the file.
  • The close-time incremental_vacuum(256) went through execute_batch, which steps a statement only once, so it freed about one page.

Changes

  • Stalled routes expire. A head that has owed output for the retention period without delivering any is stalled, whether it never subscribed or is held by a failure. GC retires its live delivery, deletes the head, and deletes the ops only it was owed, logging a warning with the failure. The next event starts the route again from current output. The heads.subscribed column is gone, since nothing needs it now.
  • Compaction. Each GC pass compacts the session file:
    • A file without auto-vacuum is rebuilt once with VACUUM to enable incremental auto-vacuum. The rebuild only copies the live data, so it is fast once expired rows are gone.
    • After that, incremental_vacuum is stepped until it finishes.
  • Configurable retention. Pass serve --retention, or set BT_TRACE_RETENTION (read through src/env.rs), to a human-readable duration such as 7d, 12h or 1h 30m, parsed by humantime; the default is 7 days. The flag wins over the env var. Zero and unitless values are rejected; an unusable env value logs a warning and the default applies. It applies to every retention-based cleanup the daemon does, which runs hourly. Hook-spawned daemons inherit the variable from the agent's environment.

Tests

  • A live daemon whose route is held by a 413 keeps the route within retention, and retires and expires it after.
  • The GC scenario test now expects storage to be removed when a route that never delivered has been stalled past retention.
  • Compaction converts a file without auto-vacuum and shrinks it, then shrinks it again incrementally.
  • All 725 tests pass (14 ignored), and clippy is clean with all features and with default features. doctor_reports_storage_open_failures_with_storage_retry_conditions once hit its 5 s timeout under full-suite load, then passed alone and in two full lib runs.

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T16:52:34.274516Z 31d689a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 31d689a716

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread bt-daemon/src/args.rs Outdated
/// when a destination keeps rejecting spans.
#[arg(
long,
env = "BRAINTRUST_DAEMON_RETENTION_HOURS",

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 Badge Read the retention environment setting through the shared helper

The new BRAINTRUST_DAEMON_RETENTION_HOURS setting is read directly through Clap's env attribute, bypassing bt-daemon/src/env.rs. This violates the explicit requirement in AGENTS.md that user-facing environment variables be read through that module and introduces a separate environment-resolution path. Resolve the environment default through crate::env while preserving explicit command-line precedence and range validation.

AGENTS.md reference: AGENTS.md:L57-L60

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 3c2e4a6: --retention-hours is now optional. It falls back to BT_TRACE_RETENTION_HOURS read through crate::env, then to the default, and keeps the flag's precedence and the range check.

@Qard
Stephen Belanger (Qard) force-pushed the design/expire-stuck-routes branch 2 times, most recently from bbf1156 to 08a7106 Compare November 17, 2026 14:59
Base automatically changed from design/sqlite-journal-collections to main November 17, 2026 15:21
A route held by a failure its settings never correct, such as a span too
large for its destination, kept its SpanOps forever while translation kept
adding more, so its session storage grew without bound. Any route that has
delivered nothing for the retention period now expires the same way a route
that never subscribed did: GC stops its delivery, deletes its head and the
output only it was owed. A later event starts it again from current output.
A detached head is never behind, so it is never stalled.

GC also compacts each session's storage. Files created before incremental
auto-vacuum was enabled are rebuilt once to enable it. Incremental vacuum
is now stepped to completion; execute_batch stepped it only once, so it
released at most one page.

Retention is an optional --retention-hours flag that falls back to
BT_TRACE_RETENTION_HOURS read through crate::env, then 168 hours. An
unusable value is reported and ignored. Daemon tuning variables are
declared in env.rs and AGENTS.md describes them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
serve --retention and BT_TRACE_RETENTION take a duration such as 7d, 12h
or 1h 30m, parsed by humantime, instead of a number of hours. Zero and
unitless values are rejected; an unusable environment value is reported
and the 7 day default applies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Qard
Stephen Belanger (Qard) merged commit ff09caa into main Nov 17, 2026
27 of 28 checks passed
@Qard
Stephen Belanger (Qard) deleted the design/expire-stuck-routes branch November 17, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants