Skip to content

[0.0.28] feat(plugin): per-plugin execution budgets (#2304) - #2622

Merged
Xuepoo merged 1 commit into
mainfrom
ctx-1501/feat-plugin-budgets
Oct 24, 2026
Merged

Xuepoo merged 1 commit into
mainfrom
ctx-1501/feat-plugin-budgets

Conversation

@Xuepoo

@Xuepoo Xuepoo commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements bitty#2304: per-plugin execution budgets that can only tighten the
compiled RC-1/RC-2 ceilings.

  • bitty-plugin.toml [limits] accepts optional fuel_instructions,
    wall_ms, memory_bytes, and warning_ms. Values validate in
    1..=host_ceiling; zero, above-ceiling, negative, wrong-typed, duplicate,
    unknown, and warning_ms >= wall_ms declarations fail closed with the
    manifest field path.
  • Optional user layer at <config-dir>/plugin-budgets.lua: a global default
    table plus per-plugin plugins["owner.name"] entries. Data-only Lua parsed
    without a VM or host access (16 KiB, 256 lines, 512 B/line, 256 plugins,
    UTF-8 only, duplicate/unknown keys fail, symlinks/special files refused).
  • Production activation now builds VmBudgets from the resolved values
    instead of always VmBudgets::default(). Absent declarations keep the
    existing defaults exactly (canonical manifest bytes are unchanged when no
    budget is declared, so existing manifest hashes and grant pins stay valid).
  • bitty plugin config show <id> prints host ceiling, manifest value, user
    layers, effective value, and the deciding source layer, in table and JSON.

Design choice: layers are upper bounds, effective = minimum

All four layers are upper bounds; the effective value is the per-dimension
minimum across host ceiling, manifest, user global, and user per-plugin. This
is monotone by construction: a later layer that declares a larger number is
inert, never a widening, even when it is inside the host ceiling. It also
avoids a global user default being rejected because some other plugin declared
a tighter manifest value. The advisory warning_ms is capped at wall_ms - 1
when a tighter wall leaves no room; it never suspends.

Failure posture: a malformed user file fails generation activation closed with
a typed budget error (no silent fallback). --safe ignores both tightening
layers and runs host defaults, so the recovery path cannot be blocked by a bad
local file. Safe mode also ignores manifest declarations for bundled plugins.

Tests

  • crates/bitty-plugin-host: budget resolution (accepted tightening, absent =
    defaults, min precedence, user cannot widen, zero/above-ceiling/warning
    rejection), user-file parser (happy path, wrong types, negatives, unknown
    and duplicate keys, oversize/non-UTF-8), manifest validation and
    hash coverage.
  • crates/bitty-runtime/tests/plugin_budgets.rs: production activation applies
    manifest budgets to VmBudgets; absent keys keep host defaults; user layers
    tighten and cannot widen; safe mode ignores both layers; rejected file fails
    closed while safe mode still activates; deterministic VM-level proof that a
    tighter fuel suspends where the default completes, plus wall/memory
    observation.
  • crates/bitty-terminal: loader tests (absent/valid/malformed/oversize/
    symlink) and plugin config show budget rows, precedence, and rejected-file
    exit behavior.

Gates run

  • cargo fmt --all -- --check
  • cargo clippy -p bitty-plugin-host -p bitty-runtime -p bitty-terminal --all-targets -- -D warnings
  • cargo check --workspace --all-targets
  • cargo test -p bitty-plugin-host
  • cargo test -p bitty-config
  • cargo test -p bitty-runtime --test plugin_budgets
  • cargo test -p bitty-runtime --test plugin_runtime
  • cargo test -p bitty-runtime --test plugin_settings_file
  • cargo test -p bitty-terminal --test cli_plugin plugin_config_show
  • docs: just fmt-check + just verify-schema + targeted markdownlint
    (bitty-manual), just fmt-check + targeted markdownlint + just language
    (bitty-terminal-docs); just markdownlint/just links still report
    pre-existing recording/ issues unrelated to this PR.

Docs: bitty-manual plugin manifest/config pages, bitty-terminal-docs
configuration options, bitty-plugins-docs isolation RFC amendment, and the
bitty-docs OQ-014 note are updated in their own repositories.

Closes #2304

@Xuepoo Xuepoo added this to the v0.0.28 milestone Oct 24, 2026
@Xuepoo Xuepoo added feat Feature area:runtime Area: runtime orchestration P2 Priority: medium labels Oct 24, 2026
@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 17 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e179f937-baed-46f5-b2b8-e0c158a6f77e
Commits

Reviewing files that changed from the base of the PR and between 6d82a69 and f159076.

Files selected for processing: 12
  • CHANGELOG.md
  • crates/bitty-plugin-host/src/budgets.rs
  • crates/bitty-plugin-host/src/error.rs
  • crates/bitty-plugin-host/src/event.rs
  • crates/bitty-plugin-host/src/lib.rs
  • crates/bitty-plugin-host/src/manifest.rs
  • crates/bitty-runtime/src/plugin_runtime/manifest_toml.rs
  • crates/bitty-runtime/src/plugin_runtime/mod.rs
  • crates/bitty-runtime/tests/plugin_budgets.rs
  • crates/bitty-terminal/src/plugin.rs
  • crates/bitty-terminal/src/plugin_runtime.rs
  • crates/bitty-terminal/tests/cli_plugin.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Add optional [limits] fuel_instructions, wall_ms, memory_bytes, and warning_ms to bitty-plugin.toml, validated in 1..=the compiled RC-1/RC-2 ceilings with typed field-path errors. Add an optional data-only $XDG_CONFIG_HOME/bitty/plugin-budgets.lua with a global default and per-plugin overrides; every layer is an upper bound and the effective budget is the per-dimension minimum, so no manifest or user file can widen another layer.

Production activation now builds VmBudgets from the resolved values instead of always VmBudgets::default(); absent declarations keep the existing defaults. Safe mode ignores both tightening layers and runs host defaults. A rejected user file fails activation closed rather than silently falling back. bitty plugin config show prints host, manifest, user, effective values and the deciding source layer.

Closes #2304

@Xuepoo Xuepoo left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

INDEPENDENT REVIEW (guard tick, agent: guard-1024) — head f1590761c8a5c7777e81a01908da50859449fc37

Scope: independent review of the 2518-line diff across bitty-plugin-host (budgets.rs, manifest.rs, event.rs, error.rs), bitty-runtime (manifest_toml.rs, plugin_runtime/mod.rs), bitty-terminal (plugin.rs, plugin_runtime.rs) + tests/CHANGELOG. Areas: fail-closed budget validation, user-layer plugin-budgets.lua safety, production VmBudgets wiring, manifest-hash stability, security invariants, test honesty. All findings below verified with fresh runs on the head, not the implementer's artifacts. Note: base drifted (base 6d82a69, earlier than main head) — mergeable=MERGEABLE at review time.

(a) Fail-closed validation — PASS

  • budgets.rs:143-186 (BudgetValues::validate) + budgets.rs:188-211 (check_range_u64): every present value must be 1..=ceiling; zero and above-ceiling rejected with PluginError::Budget carrying the exact field path (limits.fuel_instructions, plugin-budgets.default.wall_ms, cross-layer plugin-budgets.plugins["id"].warning_ms). No clamping, no silent ignore.
  • warning_ms >= wall_ms rejected within the same layer (budgets.rs:174-181); the cross-layer case is handled by the advisory cap (warning = min(warning, wall-1), resolve_budgets at budgets.rs:278-287) with warning_capped_by_wall reported.
  • Manifest dimension: manifest.rs:844-867 adds four Option fields; manifest.rs:927-928 re-runs budget validation inside PluginLimits::validate, so parse_manifest fails closed even before acceptance checks (manifest_toml.rs:584 calls validate()).
  • TOML subset: parse_limit_u64/parse_limit_uint (manifest_toml.rs:712-741) accept bare ASCII digits only — quoted strings, -1, 4.0, true, empty, >u64 all rejected; duplicates caught by seen_keys.insert before the key match (manifest_toml.rs:406-410), so the early continue arms of the new budget keys do not bypass duplicate detection (verified in code; limit_budgets_parse_and_reject_escalation covers it).
  • Tests: budgets.rs:781-811 sweeps zero/above-ceiling for all four dimensions through resolve_budgets; manifest.rs:4589-4685 covers zero/escalation/typed-error/warning-vs-wall at the manifest layer; manifest_toml.rs tests cover wrong types, signs, floats, empty, duplicate keys.

(b) User-layer Lua file — PASS

  • Loader (bitty-terminal/src/plugin_runtime.rs:621-654): absent file → Ok(None); symlink_metadata + !file_type().is_file() refuses symlinks, directories, and special files; size checked pre-read and re-checked post-read by parse_budget_file, closing the widen TOCTOU window.
  • Parser (budgets.rs:243-255): UTF-8 only, ≤16 KiB / 256 lines / 512 B per line / ≤256 plugin records; hand-rolled bounded byte parser — accepts only return { default = {...}, plugins = { ["id"] = {...} } } with bare non-negative integers and -- comments; fails closed on strings, floats, negatives, booleans, function calls, local x, duplicate keys/values, unknown keys, trailing content (budgets.rs:560-592 tests). No Lua VM is constructed anywhere; no host/IO access from file content.
  • Malformed file ⇒ set_budget_file_error (mod.rs:1171-1180) ⇒ activation refuses with typed PluginRuntimeError::Budget (mod.rs:1226-1231); --safe checks safe_mode first (mod.rs:1221-1226), so recovery always runs host defaults. Tests: rejected_user_file_fails_closed_but_safe_mode_still_works, budget_file_loader_refuses_symlinks_and_special_files (unix-only, symlink exercised directly — nit below on the FIFO case).
  • Minor: parse_string (budgets.rs:496-532) maps non-ASCII bytes via char::from(byte) (Latin-1), mangling multibyte plugin ids before PluginId::new validation; since PluginId is ASCII-only (manifest.rs:86-141), this fails closed — cosmetic diagnostic staleness only.

(c) Production wiring — PASS

  • Activation actually constructs VmBudgets from resolved values (mod.rs:2966-2971): instruction_budget/wall/warning/memory_limit all from effective_budgets; the old VmBudgets::default() fallback path is gone; the test-only harness override touches only the wall dimension.
  • No layer can raise: per-dimension minimum across manifest/user layers (budgets.rs:245-275), range capped at 1..=ceiling, and ceilings are derived from the actual VmBudgets::default() (budget_ceilings, mod.rs:2180-2189) so host/VM constant drift fails closed. host_ceilings_match_the_vm_defaults pins RC-1 (10^7 fuel, 50 ms wall) / RC-2 (32 MiB) parity in the runtime crate.
  • Constants re-pointed to the single authority: event.rs:1063-1087 now aliases budgets::HOST_* (no duplicate ownership).
  • Manifest bytes/hash: budget keys appended to canonical bytes only when at least one is declared (manifest.rs:2715-2739), so absent declarations keep pre-existing bytes — manifest_hash_covers_budgets_only_when_declared asserts no budget keys in the canonical string when nothing is declared, and distinct values change the hash. Absent-declaration grant pins stay valid.
  • Effective budgets recorded per entry, cleared on teardown and on failure (entry.budgets = None hunks); rollback happens before any VM exists when resolution fails (mod.rs:2944-2955).

(d) Security invariants — PASS

  • Tighten-only monotonic min across layers; user layers cannot widen manifest/host (explicit tests user_layers_cannot_widen_manifest_or_host at budgets.rs:860-878, user_layers_tighten_and_cannot_widen).
  • No plugin execution on render/input hot path: the diff touches only startup discovery (discover_with_roots — file read per process start, no VM), activation, and the plugin config show CLI report path. The budget file is data-only and never instantiated as code, so it cannot alter Terminal Truth; no new Terminal Truth writes anywhere in the diff.

(e) Test honesty — PASS

My independent runs on the PR head: bitty-plugin-host full suite ok (incl. 12 budgets::tests + 2 manifest budget tests), --test plugin_budgets 8/8 ok (end-to-end: manifest values reach the production VM; runaway command terminated by tight fuel, not default; rejected-file fail-closed with safe-mode recovery; VM-level fuel/wall/memory suspend contrast), bitty-runtime manifest_toml incl. limit_budgets_parse_and_reject_escalation ok, bitty-terminal --bins 4 budget tests ok, cli_plugin malformed-budget-file fail-closed (exit 4, BudgetRejected on stderr) ok. GitHub CI green on this exact head (Linux Wayland/X11, Windows, macOS, MSRV 1.85, CodeQL, supply chain).

Nits (non-blocking)

  1. crates/bitty-terminal/src/plugin_runtime.rs:655 — replaced doc comment leaves a stray /// glued to the first line (...(CTX-1115 test seam).///), merging two rustdoc paragraphs; formatting regression only.
  2. budget_rows (bitty-terminal/src/plugin.rs:4511) displays BudgetCeilings::HOST constants while the runtime derives ceilings from VmBudgets::default(); parity is pinned by tests, but on hypothetical drift the config show report could disagree with effective enforcement.
  3. budget_file_loader_refuses_symlinks_and_special_files exercises only the symlink case on unix; a FIFO/named-pipe case would close the special-files evidence gap directly.
  4. parse_budget_table's unknown-key path reports the input-typed error ("expected a bare non-negative integer") instead of "unknown budget key" when a string follows an unknown key; fail-closed either way, message quality only.

VERDICT: PASS-WITH-NITS — fail-closed validation, data-only user file, resolved VmBudgets wiring, hash stability, hot-path neutrality, and genuine fail-path tests all verified against head f159076; four cosmetic nits only.

@Xuepoo
Xuepoo merged commit 47b4901 into main Oct 24, 2026
20 checks passed
@Xuepoo
Xuepoo deleted the ctx-1501/feat-plugin-budgets branch October 24, 2026 09:15
@Xuepoo

Xuepoo commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Independent review verdict recorded by blitz guard tick (agent guard-1024, completed 2026-10-24 17:13 CST).

INDEPENDENT REVIEW ✓ (agent: guard-1024)

PR: #2622 "[0.0.28] feat(plugin): per-plugin execution budgets (#2304)" · Head SHA (fetched fresh): f1590761c8a5c7777e81a01908da50859449fc37 · Base 6d82a69bdc6eb885cf934b4bc9f531c5e614e957 · mergeable MERGEABLE · 2518-line diff across bitty-plugin-host (budgets.rs, manifest.rs, event.rs, error.rs), bitty-runtime (manifest_toml.rs, plugin_runtime/mod.rs), bitty-terminal (plugin.rs, plugin_runtime.rs) + tests/CHANGELOG.

Scope: independent review of fail-closed budget validation, user-layer plugin-budgets.lua safety, production VmBudgets wiring, manifest-hash stability, security invariants, and test honesty. Verified with my own re-runs, not the implementer's artifacts.

(a) Fail-closed validation — PASS

  • budgets.rs:143-186 (BudgetValues::validate) + budgets.rs:188-211 (check_range_u64): every present value must be 1..=ceiling; zero and above-ceiling rejected with PluginError::Budget carrying the exact field path (limits.fuel_instructions, plugin-budgets.default.wall_ms, cross-layer plugin-budgets.plugins["id"].warning_ms). No clamping, no silent ignore.
  • warning_ms >= wall_ms rejected within the same layer (budgets.rs:174-181), and the cross-layer case is handled by the advisory cap (warning = min(warning, wall-1), resolve_budgets budgets.rs:278-287) with warning_capped_by_wall reported.
  • Manifest dimension: manifest.rs:844-867 adds four Option fields; manifest.rs:927-928 re-runs budget validation inside PluginLimits::validate, so parse_manifest fails closed even before acceptance checks (manifest_toml.rs:584 calls validate()).
  • TOML subset: parse_limit_u64/parse_limit_uint (manifest_toml.rs:712-741) accept bare ASCII digits only — quoted strings, -1, 4.0, true, empty, >u64 all rejected; duplicates caught by seen_keys.insert before the key match (manifest_toml.rs:406-410), so the early continue arms of the new budget keys do not bypass duplicate detection (verified in code, and limit_budgets_parse_and_reject_escalation covers it).
  • Tests: budgets.rs:781-811 sweeps zero/above-ceiling for all four dims through resolve_budgets; manifest.rs:4589-4685 covers zero/escalation/typed-error/warning-vs-wall at the manifest layer; manifest_toml.rs tests cover wrong types, signs, floats, empty, duplicate keys.

(b) User-layer Lua file — PASS

  • Loader (bitty-terminal/src/plugin_runtime.rs:621-654): absent file → Ok(None); symlink_metadata + !file_type().is_file() refuses symlinks, directories, and special files (plugin_runtime.rs:636-639); size checked pre-read and re-checked post-read by parse_budget_file, closing the widen TOCTOU window.
  • Parser (budgets.rs:243-255): UTF-8 only, ≤16 KiB / 256 lines / 512 B per line / ≤256 plugin records; hand-rolled bounded byte parser — accepts only return { default = {...}, plugins = { ["id"] = {...} } } with bare non-negative integers and -- comments; fails closed on strings, floats, negatives, booleans, function calls, local x, duplicate keys/values, unknown keys, trailing content (budgets.rs:560-592 tests). No Lua VM is constructed anywhere; no host/IO access from the file content.
  • Malformed file ⇒ set_budget_file_error (mod.rs:1171-1180) ⇒ activation refuses with typed PluginRuntimeError::Budget (mod.rs:1226-1231); --safe checks safe_mode first (mod.rs:1221-1226), so recovery always runs host defaults. Tests: rejected_user_file_fails_closed_but_safe_mode_still_works, budget_file_loader_refuses_symlinks_and_special_files (unix-only symlink test, the special-file/named-pipe case is covered by !is_file() but only symlink exercised directly — nit).
  • Minor: parse_string (budgets.rs:496-532) maps non-ASCII bytes via char::from(byte) (Latin-1), mangling multibyte plugin ids before PluginId::new validation; since PluginId is ASCII-only (manifest.rs:86-141), this fails closed — cosmetic diagnostic staleness only.

(c) Production wiring — PASS

  • Activation actually constructs VmBudgets from resolved values (mod.rs:2966-2971): instruction_budget/wall/warning/memory_limit all from effective_budgets; the old VmBudgets::default() fallback path is gone; the test-only harness override touches only the wall dimension and yields to the resolved value otherwise (mod.rs:2968).
  • No layer can raise: per-dimension minimum across manifest/user layers (budgets.rs:245-275), value range capped at 1..=ceiling, and ceilings are derived from the actual VmBudgets::default() (budget_ceilings, mod.rs:2180-2189) so host/VM constant drift fails closed rather than widening. host_ceilings_match_the_vm_defaults pins RC-1 (10^7 fuel, 50 ms wall) / RC-2 (32 MiB) parity in the runtime crate.
  • Constants re-pointed to the single authority: event.rs:1063-1087 now aliases budgets::HOST_* (no duplicate ownership).
  • Manifest bytes/hash: budget keys appended to canonical bytes only when at least one is declared (manifest.rs:2715-2739), so absent declarations keep pre-existing bytes — manifest_hash_covers_budgets_only_when_declared asserts the canonical string contains no budget keys when nothing is declared, and distinct values change the hash. Absent-declaration grant pins stay valid.
  • Effective budgets are recorded per entry, cleared on teardown and on failure (mod.rs hunks at entry.budgets = None), and rollback happens before any VM exists when resolution fails (mod.rs:2944-2955).

(d) Security invariants — PASS

  • Tighten-only monotonic min across layers; user layers cannot widen manifest/host (explicit tests user_layers_cannot_widen_manifest_or_host, budgets.rs:860-878, runtime user_layers_tighten_and_cannot_widen).
  • No plugin execution on render/input hot path: the diff touches only startup discovery (discover_with_roots — file read per process start, no VM), activation, and the plugin config show CLI report path. The budget file is data-only and never instantiated as code, so it cannot alter Terminal Truth; no new Terminal Truth writes anywhere in the diff.

(e) Test honesty — PASS

My independent runs on the PR head: bitty-plugin-host full suite ok (incl. 12 budgets::tests + 2 manifest budget tests), --test plugin_budgets 8/8 ok (ends-to-end: manifest values reach the production VM; runaway command terminated by tight fuel, not default; rejected-file fail-closed with safe-mode recovery; VM-level fuel/wall/memory suspend contrast), bitty-runtime manifest_toml incl. limit_budgets_parse_and_reject_escalation ok, bitty-terminal --bins 4 budget tests ok, cli_plugin malformed-budget-file fail-closed (exit 4, BudgetRejected on stderr) ok. Fail paths genuinely asserted, not just plumbing. GitHub CI is green on the head (Linux Wayland/X11, Windows/macOS, MSRV 1.85, CodeQL).

Nits (non-blocking)

  1. crates/bitty-terminal/src/plugin_runtime.rs:655 — the replaced doc comment leaves a stray /// glued to the first line (...(CTX-1115 test seam).///), merging two rustdoc paragraphs; formatting regression only.
  2. budget_rows (bitty-terminal/src/plugin.rs:4511) displays BudgetCeilings::HOST constants while the runtime derives ceilings from VmBudgets::default(); parity is pinned by tests, but on hypothetical drift the config show report could disagree with effective enforcement.
  3. budget_file_loader_refuses_symlinks_and_special_files exercises only the symlink case on unix; a FIFO/named-pipe case would close the special-files evidence gap directly.
  4. parse_budget_table's unknown-key path reports the input-typed error ("expected a bare non-negative integer") instead of "unknown budget key" when a string follows an unknown key; field is always plugin-budgets.lua (line-prefixed) — fail-closed either way, message quality only.

I made no changes to GitHub (no approval, comment, merge, push). Housekeeping leftovers from my verification: /tmp/bitty/guard-1024/2622-review.diff (fresh fetch; pre-existing 2622.diff untouched), branch pr-2622-review (refetch; force-delete blocked by policy), and a build target dir at /mnt/data/bitty-guard-target (~7 GB, could not rm -rf due to command guard) — safe to delete.

VERDICT: PASS-WITH-NITS — fail-closed validation, data-only user file, resolved VmBudgets wiring, hash stability, hot-path neutrality, and genuine fail-path tests all verified against head f159076; four cosmetic nits only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:runtime Area: runtime orchestration feat Feature P2 Priority: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[0.2.0] Per-plugin configurable execution budgets (fuel, wall, memory)

1 participant