Skip to content

Plugin management: config section, loading with hash checks, recording and the control API - #255

Merged
fylorn merged 1 commit into
feat/pluginsfrom
plugins-mgmt
Oct 2, 2026
Merged

fylorn merged 1 commit into
feat/pluginsfrom
plugins-mgmt

Conversation

@fylorn

@fylorn fylorn commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Management plane for script plugins (track 2a, step 2), on top of the scaffolding in #250.

Configuration (tw-config)

plugins: is a list, and its order is the run order.

Field Type Default
id [a-z0-9-]{1,40}, unique; order and inspect are reserved required
file must be plugins/<id>.js, relative to the config dir required
sha256 64 lowercase hex required
enabled bool true
on_error reject | skip reject
scope { clients, models, upstreams }, * globs, no blank entries {}
settings string | number | bool {}
  • order and inspect are reserved because /plugins/order and /plugins/inspect are fixed paths. A test keeps this list in sync with the endpoint table.
  • tw-config holds plain data only. Manifest checks (settings keys and types) happen in the gateway when a plugin loads.
  • Documented in docs/config*.md (generated tables, both languages).

Loading (tw-gateway plugin::load)

  • Plugins (on AppState, survives reloads) holds the engine, the config dir, per-plugin Stats and LogRing, and a compile cache keyed by SHA-256.
  • I9. Every build reads each file once, capped at 1 MiB + 1. It hashes those bytes and compiles those same bytes, and only when the hash matches the approved sha256.
    • A hash mismatch or a missing file gives status changed. The approved copy's manifest is still shown, but it is never run.
    • A load error, or a setting that is undeclared or has the wrong type, gives status error.
    • Neither ever fails a reload.
  • AppState::reload_plugins() re-reads the plugin files without a config change.
  • Runtime swaps are serialized, so a plugin reload cannot put back an older configuration.
  • When an enabled plugin newly stops running, a plugin_failed event is emitted without a request_id.
  • ConfigManager::new tells the gateway where the config dir is. Remote cores work the same way, because the files are next to their config.
  • File-change detection works two ways:
    • A watcher on plugins/ (tw-watch, 200 ms debounce; the directory is created 0700 at startup) reloads the plugins under the same lock that plugin writes hold.
    • Every config reload re-hashes all plugin files.
  • The engine is still the "engine unavailable" stand-in. plugin::fake::FakeEngine serves the tests.

Recording (tw-store)

  • SCHEMA 24 adds the plugin_runs table: request_id, seq, at_ms, plugin_id, plugin_name, hook, outcome, error, error_code, error_args, cpu_us, detail. Its rows are pruned along with the requests.
  • AppState::plugin_ran hands each run to the store through its own channel.
  • The post-plugin request body is stored as {id}.after-plugins (BodyKind::AfterPlugins / Which::AfterPlugins).
  • That body goes through the same BodyRecord::for_disk redaction as every other body (Store bodies with secrets replaced; keep 4 MiB of each answer; 5 GiB of bodies by default #252), and a test covers it.

Control API

Endpoints: GET /plugins, POST /plugins/inspect (no side effects), POST /plugins, PUT /plugins/order, PUT /plugins/{id}, DELETE /plugins/{id}, PUT /plugins/{id}/source, GET /plugins/{id}/source, POST /plugins/{id}/approve, POST /plugins/{id}/trial, GET /plugins/{id}/logs.

  • Create, replace and approve:
    • They compile the source first.
    • They write the plugin file and its approved copy (0600, atomic) before the configuration. If the config write fails, the files are restored.
    • Approve reads the file once and accepts it only if its hash equals the hash the caller reviewed.
    • Settings are checked against the manifest and written out in full. On replace and approve, settings that still fit are kept.
  • I12. CreatePlugin, ReplacePluginSource and ApprovePluginFile are documented in the endpoint table as not for the desktop webview.
  • RequestDetail gains plugins and request_after_plugins, and history rows gain plugin_changed.
  • TrialPlugin answers control.plugin.trial_unavailable until the data-plane trial lands. It refuses a changed plugin.
  • The CONTROL_API_VERSION note now covers all of this, under 33 (no number change).

Checks

  • cargo fmt --all --check
  • cargo clippy --workspace --all-targets -- -D warnings, and --lib
  • cargo test --workspace
  • the ts feature: clippy, tests, export, tsc --strict
  • smoke: /plugins and the plugins/ mode are now covered

🤖 Generated with Claude Code

…g and the control API

Track 2a of script plugins, on top of the scaffolding (#250).

Configuration (tw-config)
- A `plugins:` list, in run order: id, file, sha256, enabled (default true),
  on_error (reject | skip, default reject), scope (clients, models,
  upstreams globs) and settings (string | number | boolean). Validation:
  id pattern and uniqueness, `order`/`inspect` reserved (fixed paths under
  /plugins), file must be plugins/<id>.js, sha256 64 lowercase hex, no
  blank scope entry, scalar settings. Manifest checks stay in the gateway.
- Documented in docs/config*.md (generated tables).

Loading (tw-gateway)
- `Plugins` keeps the engine, the config directory, per-plugin stats and
  log rings across reloads, and a compile cache keyed by SHA-256.
- Every build reads each plugin file once (capped at 1 MiB + 1), hashes
  those bytes and compiles those bytes only when the hash equals the
  approved one (I9). A mismatch or a missing file is `changed`; a load
  error, an undeclared or mistyped setting is `error`. Neither fails the
  reload. Settings are merged over the manifest defaults.
- `reload_plugins` re-reads the files without a config change; runtime
  swaps are serialized so it never puts back an older configuration.
- An enabled plugin that newly stops running emits `plugin_failed`
  (no request id).
- `plugin_ran` also hands each run to the store (`RunRecord`).
- A fake engine (`plugin::fake`) for tests; the real runtime is still the
  "engine unavailable" stand-in.

Recording (tw-store)
- SCHEMA 24: `plugin_runs` (request_id, seq, at_ms, plugin_id,
  plugin_name, hook, outcome, error + code + args, cpu_us, detail), pruned
  with the requests.
- The post-plugin request body is stored as `{id}.after-plugins`.

Control API (tw-api, tw-control)
- GET /plugins, POST /plugins/inspect (no side effects), POST /plugins,
  PUT /plugins/order, PUT /plugins/{id}, DELETE /plugins/{id},
  PUT /plugins/{id}/source, GET /plugins/{id}/source,
  POST /plugins/{id}/approve, POST /plugins/{id}/trial,
  GET /plugins/{id}/logs.
- Create, replace and approve write the plugin file and the approved copy
  (0600, directories 0700) before the configuration and put them back if
  the configuration write fails. Approve accepts the file on disk only if
  its hash equals the one the caller reviewed. CreatePlugin,
  ReplacePluginSource and ApprovePluginFile are documented as not for the
  desktop webview (I12).
- RequestDetail gains `plugins` and `request_after_plugins`; history rows
  gain `plugin_changed`.
- A watcher on plugins/ reloads the plugins when a file changes.
- Trial runs answer "not available yet" until the data-plane trial lands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 6f82f27 into feat/plugins Oct 2, 2026
5 checks passed
@fylorn
fylorn deleted the plugins-mgmt branch October 2, 2026 10:49
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.

1 participant