Plugin runtime and adapters
The plugin runtime lets Sofka run external tools against a selected resource or cluster context and return safe, searchable results to the application. It joins command selection, safety checks, process control, live activity, report rendering, and optional port forwarding. The catalog and install lifecycle is described in Plugin catalog and installation, while the wider model is described in Plugin ecosystem.
Execution model
Plugin targets and jobs
The runtime turns a command into one or more jobs. A selection-targeted command runs once for each marked resource, or for the current row when no rows are marked. A context-targeted command runs once without an object. Resource scope, input values, executable availability, and safety declarations are checked before any job starts.
The application uses the same job model for local packages and bundled adapters. This keeps external tools and built-in integrations subject to the same confirmation, read-only, timeout, and output rules.
flowchart LR
A[Command or key] --> B[Resolve scope and inputs]
B --> C[Apply safety checks]
C --> D[Create jobs]
D --> E[Run adapter]
E --> F[Activity and result]
F --> G[Popup, report, or status]
References: src/…/plugins.rs• L168-225 src/…/plugins.rs• L247-265 src/plugins.rs• L61-121 src/config.rs• L958-1015
Adapter process boundary
An adapter is the program that the runtime starts for a plugin. Sofka sends a request as JSON on standard input and closes that stream. The adapter reads the request, performs the tool work, and writes its final result to standard output. Standard error is for progress and diagnostic text.
Packaged adapters run from their package directory, so they can use files shipped with the package. The runtime passes each command argument as a separate argument. It does not build a shell command for packaged plugins, which keeps user inputs and resource values separate from command syntax.
References: src/plugins.rs• L351-375 src/plugins.rs• L864-937 docs/plugin-authoring.md• L306-345
Output modes
The output mode defines how the application presents the adapter result. A terminal command is an interactive command for inline configuration. Packaged commands use captured modes: popup activity, background completion status, or a structured report view.
The runtime runs bulk jobs with bounded concurrency and keeps their result order. A report can become a searchable document. A background run instead reports how many jobs succeeded and which targets failed.
| Mode | Main use | Application result |
|---|---|---|
popup | A captured result with live diagnostics | Activity view and retained output |
background | Work that should not take over the view | Completion status and failure summary |
report | Structured adapter output | Validated searchable document |
terminal | Interactive inline command use | Suspended terminal session |
References: src/…/plugins.rs• L234-265 src/…/plugins.rs• L452-607 src/store.rs• L184-204 src/plugins.rs• L605-718
Application integration
Safety and lifecycle
The application treats a plugin as an action that may affect the cluster or create network load. Guardrails and confirmation run before adapter execution and before a managed port-forward starts. Read-only mode blocks commands that can mutate resources or generate load. A plugin author must declare these properties accurately because the declarations are not a security sandbox.
Only one plugin run is active at a time. A new run, navigation to a new resource view, context changes, program exit, or manual cancellation stops the active process and its child processes. Each target has a timeout that covers setup, request transfer, and output collection.
References: src/…/plugins.rs• L191-225 src/…/plugins.rs• L392-449 src/…/plugins.rs• L712-719 src/plugins.rs• L348-364 docs/plugin-authoring.md• L430-484
Activity tracking
Activity tracking gives the user progress before an adapter finishes. The runtime reads standard error while it captures the final output, then publishes a bounded activity tail to the application. The view can follow new lines, scroll through prior lines, or retain the completed result.
The activity stream is shared by bulk jobs and labels each stream so progress from different targets remains understandable. A bounded tail prevents a noisy tool from growing the UI without limit. Background commands use completion status instead of an activity panel.
References: src/…/activity.rs• L11-59 src/…/activity.rs• L61-111 src/…/plugins.rs• L503-607 src/…/plugins.rs• L609-718
Managed port-forwarding
An adapter can request a remote port for a selected pod or service. Sofka reuses a matching existing forward when possible. Otherwise, it starts a temporary local forward, waits until it is ready, and adds the local and remote port details to the request.
The adapter still chooses the URL scheme, path, and request behavior. Sofka removes a temporary forward when the job ends or is cancelled. A plugin cannot ask for a new forward after execution starts.
References: src/…/plugins.rs• L280-388 src/…/plugins.rs• L421-424 src/plugins.rs• L808-845 docs/plugin-authoring.md• L486-535
Sanitization support
Activity text sanitization
Activity text is display data, not terminal output. Before it reaches the activity view, Sofka converts byte streams into text, removes terminal escape sequences and unsafe control characters, and keeps line redraws as progress updates instead of adding endless history. Invalid byte sequences do not stop the activity stream.
This protection applies to adapter diagnostics and failure summaries. It is separate from the Sanitize pods plugin, which is a cluster cleanup action.
References: src/…/activity.rs• L123-212 src/plugins.rs• L783-805 src/…/plugins.rs• L756-797
Bundled cleanup adapter
Sanitize pods is a bundled adapter that uses the same request and report boundary as an external plugin. It finds pods that match a chosen state set and deletes them through the Kubernetes API. Its manifest marks it as context-wide, mutating, and dangerous, so confirmation, guardrails, and read-only mode apply before the cleanup begins.
The adapter accepts state and dry-run inputs. It reads the active context and namespace from the request, then returns a report that explains the action. Because the adapter is part of the Sofka executable, users do not need to install a separate runtime for it.
References: plugins/…/plugin.toml• L1-24 src/sanitize.rs• L1-7 src/sanitize.rs• L117-180 src/main.rs• L223-232
External adapter contract
Package command contract
An external package declares its adapter command, input schema, target, output mode, resource scopes, and runtime requirements in plugin.toml. The runtime validates the manifest before it loads the command. Package commands must use a captured output mode and an executable adapter. They cannot rely on shell = true or an interactive terminal mode.
The package contract separates user-facing command metadata from adapter behavior. A package can contain more than one command, and each command can select a different action in the same adapter by using arguments.
References: src/plugins.rs• L157-204 src/plugins.rs• L377-408 src/plugins.rs• L447-533 docs/plugin-authoring.md• L211-245
Request and report schemas
The request schema gives the adapter the context needed to act without a direct application API. It includes the cluster context, namespace, resource identity, active filter, validated inputs, and the selected object when the target is a selection. A context target receives no selected object and keeps the current namespace; an empty namespace represents all namespaces.
The report schema gives Sofka a stable display shape. It contains a title and optional sections with text lines or tables. Every table row must match its column count. A successful report may also request a kubeconfig reload, but Sofka accepts that action only after the complete run and report pass validation.
References: src/…/plugins.rs• L267-388 src/…/plugins.rs• L801-825 src/plugins.rs• L576-665 docs/plugin-authoring.md• L306-345 docs/plugin-authoring.md• L346-429
Example adapter behavior
The reference adapter in examples/…/adapter.py shows the smallest useful contract. It reads one request from standard input, checks the request schema version, reads the selected object and validated inputs, and writes one report to standard output. Its optional detail input adds a second table from the object's labels.
The example does not call the Kubernetes API. It uses data supplied by Sofka, which makes it a useful model for adapters that only need the selected resource. Adapters that need cluster access can use the context and inherited environment, or use the managed forward data in the request.
References: examples/…/adapter.py• L1-28 examples/…/plugin.toml• L1-16 examples/…/request.json• L1-20
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the request and report schema versions aligned between the runtime, bundled adapters, examples, and external author guidance.
- When output modes or report actions change, update the application result handling and the adapter validation rules together.
- When target, input, or placeholder rules change, check both single-resource and context-wide jobs because they produce different request data.
- When activity limits or text cleaning change, keep live stderr display, failure summaries, and retained reports within the same safety expectations.
- When safety flags or port-forward rules change, update both the pre-run guard checks and the request data given to adapters.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Adapter writes malformed JSON or an unsupported report schema | The report is rejected and the run is shown as failed | Validate report shape and schema version before presenting it |
| Adapter writes too much to stdout or stderr | The job stops or its displayed output is truncated | Keep tool output bounded and emit a compact report |
| Adapter exceeds its per-target timeout | The job is cancelled and the target is counted as failed | Set a suitable timeout and avoid unbounded waits |
| A package declares a missing executable | The command cannot run on that system | Declare required executables and provide install guidance |
| A mutating or load-generating command is run in read-only mode | The runtime blocks it before execution | Set mutating and network_load to match the real behavior |
| A terminal command is launched for a marked set | The runtime refuses to run the command because it cannot safely compose interactive output | Use popup or background for bulk jobs |
| A cleanup adapter selects a wider scope than the user expects | It can affect many resources in a context or all namespaces | Use context and namespace values from the request and require confirmation |
| Adapter diagnostics contain terminal control data | The activity view could display misleading or unsafe text | Send diagnostics to stderr and rely on runtime text sanitization |
Key files
| File | Why to read it |
|---|---|
| src/…/plugins.rs | Shows how the application resolves targets, applies safety checks, launches jobs, tracks activity, and handles results. |
| src/plugins.rs | Defines the process boundary, request creation, output capture, report validation, and managed forwarding. |
| src/…/activity.rs | Explains the bounded activity tail and the cleaning of adapter diagnostics. |
| src/config.rs | Defines the plugin command model, inputs, targets, safety flags, and output modes. |
| src/sanitize.rs | Shows how a bundled cluster cleanup adapter uses the common request/report contract. |
| plugins/…/plugin.toml | Declares the bundled cleanup command and its safety-facing user inputs. |
| examples/…/adapter.py | Provides a small external adapter example for the request and report contract. |
| examples/…/plugin.toml | Shows the package command declaration and input definition used by the example. |
| docs/plugin-authoring.md | Gives the external author-facing rules for manifests, schemas, limits, safety, and testing. |