Plugin management: config section, loading with hash checks, recording and the control API - #255
Merged
Merged
Conversation
…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>
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.
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.id[a-z0-9-]{1,40}, unique;orderandinspectare reservedfileplugins/<id>.js, relative to the config dirsha256enabledtrueon_errorreject|skiprejectscope{ clients, models, upstreams },*globs, no blank entries{}settings{}orderandinspectare reserved because/plugins/orderand/plugins/inspectare fixed paths. A test keeps this list in sync with the endpoint table.docs/config*.md(generated tables, both languages).Loading (tw-gateway
plugin::load)Plugins(onAppState, survives reloads) holds the engine, the config dir, per-pluginStatsandLogRing, and a compile cache keyed by SHA-256.sha256.changed. The approved copy's manifest is still shown, but it is never run.error.AppState::reload_plugins()re-reads the plugin files without a config change.plugin_failedevent is emitted without arequest_id.ConfigManager::newtells the gateway where the config dir is. Remote cores work the same way, because the files are next to their config.plugins/(tw-watch, 200 ms debounce; the directory is created 0700 at startup) reloads the plugins under the same lock that plugin writes hold.plugin::fake::FakeEngineserves the tests.Recording (tw-store)
SCHEMA24 adds theplugin_runstable: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_ranhands each run to the store through its own channel.{id}.after-plugins(BodyKind::AfterPlugins/Which::AfterPlugins).BodyRecord::for_diskredaction 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.CreatePlugin,ReplacePluginSourceandApprovePluginFileare documented in the endpoint table as not for the desktop webview.RequestDetailgainspluginsandrequest_after_plugins, and history rows gainplugin_changed.TrialPluginanswerscontrol.plugin.trial_unavailableuntil the data-plane trial lands. It refuses achangedplugin.CONTROL_API_VERSIONnote now covers all of this, under 33 (no number change).Checks
cargo fmt --all --checkcargo clippy --workspace --all-targets -- -D warnings, and--libcargo test --workspacetsfeature: clippy, tests, export,tsc --strict/pluginsand theplugins/mode are now covered🤖 Generated with Claude Code