Plugin SDK — graph.sdk
The consumption SDK (ADR 0043): a stable, importable surface onto agent capability, so a plugin can run a subagent, search knowledge, schedule work, or record a metric without reaching into core internals. The distinction matters — internals are free to change underneath you between releases; this module and registry.host are the two surfaces that aren't.
from graph import sdk
answer = await sdk.complete('Summarize the changelog')Import it lazily (inside the function that uses it) if your plugin must also run host-free in its own test suite. Generated from src/graph/sdk.py; the sections below are the module's own.
Contents
Plugin services (the plugin↔plugin call seam, ADR 0116) — record_friction(), service()
Agent + model access (the plugin↔agent channel, ADR 0043) — capture_tool_receipts(), complete(), config(), gateway_client(), renders_ui(), run_subagent(), subagent_types(), turn_stop_reason()
Delivering a file with the answer (#4252) — attach_file()
The agent's own console URL + plugin-view deep links (#4256) — console_url(), view_url()
Model in-flight priority (the plugin↔limiter channel, ADR 0115 D6) — llm_lanes(), llm_priority()
Knowledge graph (the plugin↔knowledge channel, ADR 0043 — "shared knowledge") — knowledge_add(), knowledge_purge(), knowledge_search()
Durable memory (ADR 0125) — memory_save(), memory_search()
Goal-driven recurring loop (the OODA pattern) — clear_watch(), create_watch(), list_watches(), run_in_session(), start_goal_loop(), stop_goal_loop(), update_watch()
Background jobs (ADR 0050 spawn + ADR 0070 results pipeline) — background_status(), spawn_background()
Reactive rules (ADR 0039 events → one-shot turns) — react_on()
Plugin-owned recurring jobs (#1642) — cancel_plugin_jobs(), cancel_scheduled(), plugin_job_prefix(), schedule_recurring()
Plugin state (own the file, never core's) — deleted_chat_sessions(), plugin_store()
Plugin metric timeseries (#1632) — metric_history(), metric_last(), record_metric()
Run tracing (one Langfuse trace per multi-step plugin run) — trace_attributes(), trace_run()
Delegation ledger (who handed what work to whom) — current_run_id(), delegation(), record_delegation(), run_scope()
Subscription usage (the plugin↔quota channel, ADR 0122 D6/D7/D8) — provider_usage()
Managed Python runtime (ADR 0094 — the provisioned child interpreter) — managed_python_exe()
Node.js runtime (ADR 0085, amended 2026-10-15 — one resolver for every Node consumer) — node_executable()
Per-plugin Python venv + worker (ADR 0085 amendment 2026-11-08, #4807 part 2) — aworker(), plugin_python(), worker()
Plugin services (the plugin↔plugin call seam, ADR 0116)
sdk.service
sdk.service(name: str) -> Callable | NoneThe callable another plugin offers as name ("<plugin_id>.<name>"), or None.
The way one plugin CALLS another without importing it: the provider registers it with registry.register_service and you resolve it here, at call time — never at register() time, since plugins load in no guaranteed order and a reload can swap the provider in or out. None means no loaded plugin provides it (disabled, not installed, or a core older than the provider needs): degrade with an actionable message rather than fail. Guard the lookup itself with getattr(sdk, "service", None) on cores older than this seam. The callable's signature and return shape are the PROVIDER's documented API — read its docstring (help(sdk.service("artifact.show"))). The reference provider is the artifact plugin's artifact.show::
show = sdk.service("artifact.show")
if show is None:
return "The Artifact plugin is off — enable it to see charts."
r = show(kind="vega-lite", code=spec_json, title="Sales by weekday")
return r["message"] + r["ref"] # the artifact-ref chip tail goes LAST
Its sibling artifact.save_file registers a FILE your plugin wrote (inside its plugin_store) as a versioned file artifact from background code, with no agent turn — save(path, title=…, session_id=…, plugin_id=registry.plugin_id).
sdk.record_friction
sdk.record_friction(kind: str, summary: str, detail: str = '', severity: str = 'minor', *, source: str) -> str | NoneRecord a friction point in THIS instance's friction ledger from plugin code.
The agent's record_friction tool only captures what goes wrong inside a turn. A plugin's background work (a loop, a poller, a scheduled job) fails outside any turn, so without this its failures never reach the agent's friction review. Writes through the friction plugin's friction.record service (ADR 0116): the same ledger, grouping and review as the tool.
kind:"harness"(tooling/infrastructure got in the way) or"model".summary: one line, kept STABLE across occurrences, because the ledger groups by(kind, summary): no ids, timestamps or counts in it. Put those indetail.severity:"minor"or"major"(anything else records as minor).source: your plugin id; the record is stampedplugin:<source>.
Returns the grouped row's id, "" when the provider refused the input or the write failed, and None when the friction plugin is off (or the core is older than the services seam). It never raises, and it is synchronous (one small file append), so it is safe from a worker thread or a background loop. It does not rate-limit: a caller whose failure can repeat every tick should dedupe before calling. Guard the call itself with getattr(sdk, "record_friction", None) on cores older than this seam.
Agent + model access (the plugin↔agent channel, ADR 0043)
sdk.config
sdk.config() -> AnyThe live runtime LangGraphConfig.
sdk.gateway_client
sdk.gateway_client(*, timeout: float | None = None) -> AnyAn httpx.AsyncClient pre-configured for the model gateway (#1931): base_url = the gateway connection the chat model runs on (its key as the bearer — graph.llm.resolve_gateway_route), the allowlisted User-Agent (the gateway's WAF 403s default SDK UAs), and a sane timeout.
For OpenAI-compatible endpoints the chat model doesn't cover — /images/generations, /images/edits, /audio/* (core's own transcription rides the same client). Request relative paths and use it per call::
async with sdk.gateway_client(timeout=300) as client:
resp = await client.post("/images/generations", json={...})
resp.raise_for_status()
Call gateway endpoints through this — never a provider backend directly: the api_base host is auto-trusted by the egress guard + OpenShell network policy (ADR 0008); other hosts are not (a private backend IP is denied outright). timeout=None keeps the factory default.
sdk.subagent_types
sdk.subagent_types() -> set[str]Ids of the configured subagents — for validating/listing recipe steps.
sdk.capture_tool_receipts
sdk.capture_tool_receipts()Collect native tool executions in a fresh, context-local invocation scope.
Use with sdk.capture_tool_receipts() as capture: around an awaited sdk.run_subagent(...); its text return is unchanged. After exit, capture.snapshot() returns a detached JSON-compatible envelope with schema_version=1, source="native_audit", a unique invocation_id, execution IDs, native tool-call IDs, start sequence, duration, redacted args, result state and bounded structured metadata. Result states are success, tool_error, exception, cancelled or unknown (unrecognized result objects). Native IDs may repeat on retries. Policy short-circuits produce no entries; each handler invocation inside a retry produces its own entry.
capture_status is unsupported until native receipt middleware participates (at agent start, independently of tools), captured even with zero calls, or partial on interruption, pending executions or data loss. Inspect dropped_count, partial_count, pending_count and each entry's dropped_fields. Limits are included: 128 entries, depth 6, 32 fields per container, 256 visited value nodes per entry, 512 bytes per string, 8192 bytes per entry and 131072 aggregate entry bytes. Oversize entries are dropped. Redaction precedes truncation; unknown objects, file bodies and multipart payloads are omitted. Result text is never copied or parsed as evidence. Structured result leaves are limited to recognized metadata fields (paths, statuses, counts, sizes, line ranges, MIME types, exit codes and revisions); unknown leaves are omitted.
Nested scopes replace inherited ownership; tasks and context-propagating threads share their scope's locked collector. Exit closes it and restores the parent even on cancellation; unfinished executions count as dropped and late writes are rejected. Raw threads without context propagation are outside capture. Use one scope per workflow step/run and associate its invocation ID externally. Receipts prove observed execution, not context isolation or review quality. This API adds no telemetry storage or workflow policy.
sdk.run_subagent
await sdk.run_subagent(subagent_type: str, prompt: str, *, description: str, extra_tools: Any = None, truncate: int | None = None, turn_model: str | None = None, profile: str | None = None, check_prompt_tools: bool = True, session_id: str = '') -> strRun a subagent to completion and return its text output.
Pulls the config + knowledge store + scheduler from runtime state, so a plugin tool only supplies the subagent + prompt. This is the capability the workflows plugin's engine injects as its per-step run_step.
extra_tools defaults to the host's plugin + MCP tools (the same set the lead graph and the console fan-out expose) — a subagent whose allowlist names a plugin tool (the review-finder's github_pr_diff, a finance backtester) must see it here too, or every SDK-driven workflow step silently degrades to "No tools available". Pass an explicit list (even []) to override.
turn_model is the calling turn's model override; None (the default) reads it from the in-flight turn (graph.subagent_model.current_turn_model, bound by the chat drivers), so a workflow step follows the model the operator picked for the turn — under the one precedence: the subagent's pin > the override > routing.aux_model > the main model (#3944, #3955). Outside a turn there is none and the step resolves pin > aux > main, as before.
The subagent inherits the enclosing turn's incognito memory-write protection.
profile names a generation profile, overriding the subagent's profile field by field.
check_prompt_tools (default True) refuses to start the subagent when the prompt names a tool it was never given, instead of letting it run to a "blocked" finish (#4327). Pass False when the prompt is your own fixed template with other steps' output interpolated into it: a tool name inside that data is not a request for the tool, and the check would refuse a step whose input merely mentions one (#4501).
session_id supplies a ledger-only fallback identity (#4612). An enclosing edge or run scope's session takes precedence, so workflow steps inside a chat stay in that chat's session. Outside a session the workflows plugin supplies workflow:<run_id>. This value never enters the subagent's tool state or telemetry session. The default "" preserves existing behavior.
sdk.renders_ui
sdk.renders_ui(state: Any) -> boolWhether the client of the turn a tool runs in can display rendered UI — an artifact, a component chip — read from the tool's injected graph state (#4144).
False for a plain API client that receives only the reply text (/v1/chat/completions, POST /api/chat, a relay plugin's HOST.invoke turn such as Discord); True for the console and A2A, and whenever the state doesn't say (a host that predates the flag, a direct call). Read it the way you read the session id — from state: Annotated[Any, InjectedState], since per-turn contextvars are empty inside a tool body — and pass it on to a provider that shapes its reply by it, e.g. sdk.service("artifact.show")(..., renders_ui=sdk.renders_ui(state)). A text-only reply should say what was made and that this client can't show it, never point at a view the client doesn't have.
sdk.turn_stop_reason
sdk.turn_stop_reason() -> strWhy the turn this code runs under was stopped from above, or "".
Read it on a CancelledError to tell the two apart (#3940): "" is a plain cancel (an operator's A2A CancelTask, a shutdown); otherwise the stall guard ended the turn because it stopped making progress, and this is its message — a failure, not a cancel. See graph.turn_liveness.
sdk.complete
await sdk.complete(prompt: str, *, system: str | None = None, model_name: str | None = None) -> strRun a single bare LLM completion and return the text — no tools, no agent loop, no persona, no memory. The clean primitive for a plugin that just needs the model to answer a prompt (e.g. an interactive artifact calling back to the agent, a one-shot classifier/summarizer). Distinct from run_subagent, which runs a full tool-using subagent. Uses the live config's model through the gateway; pass model_name to target a different model on the same gateway, system for a system instruction.
Delivering a file with the answer (#4252)
sdk.attach_file
sdk.attach_file(ref: Any, *, name: str = '', mime: str = '', ttl_s: int | None = None) -> boolAttach a file your tool produced to THIS turn's answer, for a client that isn't the console. Call it from a tool body after registry.save_media(...)::
ref = registry.save_media(mp4_path, "video/mp4", {"title": title})
sdk.attach_file(ref, name="overview.mp4")
return f"Rendered [{title}]({ref.url})" # the console renders the inline link
An A2A client then receives the file as a FilePart on the answer artifact — {"url": <absolute signed URL>, "mediaType": mime, "filename": name} (the A2A 1.0 file part) — and a /v1 caller gets an absolute link appended to the content plus an attachments: [{url, mime_type, name}] field on the message. The console (an A2A message/stream client) gets the same file part stored on its task, but ignores it and renders the inline link instead — so keep the link in your return text.
ref is the MediaRef save_media returned, or its signed /media/…?sig=… URL — only files the media store serves can be attached, never an arbitrary URL. The link is made absolute against the agent's public base: A2A_PUBLIC_URL (set it on any deployed agent), else the bound loopback port — the same address the A2A card advertises. name defaults to the stored filename; mime to the stored type.
The delivered link EXPIRES: ttl_s seconds (default media.remote_link_ttl_s, 7 days; 0 = a permanent link). Past it the agent answers 403. The console's inline ref.url never expires. An expiring link works without a bearer, on a standalone agent and through a fleet hub (/agents/<slug>/media/…) alike: the member verifies its own signature. A permanent one (ttl_s=0) through a hub still needs the hub's bearer.
Returns True when attached — in any A2A or /v1 turn, console streams included; False (a no-op) outside an armed turn (e.g. a background job), after the turn's answer was built (a late background attach), for a non-media, unsigned or expired URL, or past 16 files in one turn. Attaching the same content twice (or a re-run tool's fresh copy of the same bytes) collapses when either attachment is unnamed, keeping the given name. Two explicitly named attachments with different names stay separate even with identical bytes; a later file under the same explicit name replaces the earlier one. An unnamed attachment matching several named files refreshes only the first matching name, preserving the others. Files attached before a HITL pause are delivered with the answer of the turn that resumes it. Guard with getattr(sdk, "attach_file", None) on cores older than this seam (ttl_s needs a core with #4258).
The agent's own console URL + plugin-view deep links (#4256)
sdk.console_url
sdk.console_url() -> str | NoneThe base URL THIS agent's console is served on (no trailing slash), or None when it can't be known — so a plugin that drives its own console (records it, screenshots it, links into it) never guesses :7870, which on a multi-agent machine is another agent::
base = sdk.console_url() # e.g. "http://127.0.0.1:7953"
if base is None:
return "pass the console URL explicitly"
page.goto(f"{base}/app/")
The SAME derivation as the A2A card and the absolute /media links (infra.media):
A2A_PUBLIC_URLwhen set (whitespace and trailing slash trimmed) — a deployed agent's public address;- else the bound host/port of the server running in this process — the wildcards (
0.0.0.0/::) andlocalhostbecome127.0.0.1; any other bind (a LAN interface,::1) keeps its address, IPv6 bracketed, since that's what reaches it; - else
None— no server is serving in this process (a script, a test, the testkit).
Fleet members. A member spawned by a fleet hub inherits an A2A_PUBLIC_URL of {hub}/agents/<id>: the tenant path the hub proxies the member's A2A and API through. That is not a console base — the hub serves a member's console at {hub}/app/agent/<slug>/, and an SPA loaded under /agents/<id>/app/ would send its absolute /api calls to the hub's own agent. So on a hub-spawned member, a public URL of that shape is skipped and this returns the member's own bound console (its loopback port, on the same box as the hub and any local recorder). A member given an explicit A2A_PUBLIC_URL of any other shape keeps it.
Guard with getattr(sdk, "console_url", None) on cores older than this seam.
sdk.view_url
sdk.view_url(plugin_id: str, view_id: str | None = None, params: Any = None) -> str | NoneAn absolute console link that opens one of this agent's plugin views and hands the page params in its own location.search — for a QR label, a notification, an email::
url = sdk.view_url("inventory", "view", {"bin": "B07"})
# → "https://hub.example.ts.net/app/agent/merchantAgent-6604/?plugin=inventory&view=view&vp=bin%3DB07"
The console reads ?plugin=<id>&view=<view_id>&vp=<urlencoded params> on load (the same {plugin, view} pair as the ui.navigate intent; a blank view = the plugin's first), opens that view on desktop or the mobile shell, appends vp to the view's iframe src and strips the keys from the address bar. The path the frame loads always comes from the view the plugin declared, never from the link.
The base is the address a person can actually open:
- on a hub-spawned fleet member (
A2A_PUBLIC_URL={hub}/agents/<id>) — the hub's slug window for this member,{hub}/app/agent/<id>/, since the member's own loopback port is unreachable from a phone; - else the declared public console (
network.public_console_url) —{origin}/app/; - else
console_url—{base}/app/; - else
None(no server in this process).
params is a mapping (or a list of pairs, or an already-encoded query string) of plain query data, at most 2048 encoded characters. Raises ValueError on an id the console would refuse or an oversized query, so a bad label fails when it's minted, not when it's scanned.
Guard with getattr(sdk, "view_url", None) on cores older than this seam.
Model in-flight priority (the plugin↔limiter channel, ADR 0115 D6)
sdk.llm_priority
sdk.llm_priority(cls: str) -> _LlmPriorityScopeRun a block of model calls under the ADR 0115 D6 priority class cls on the gateway in-flight limiter, restoring the previous class on exit.
When a lane is saturated the limiter serves waiters by class — interactive (a chat turn an operator is watching) first, then default (A2A / background / scheduled turns), then bulk (wide fan-outs, sweeps, review-panel finders) — with a slot reserved for interactive callers. A plugin running a burst of non-urgent model work marks it bulk so it queues behind an operator's chat instead of racing it::
from graph import sdk
async with sdk.llm_priority("bulk"):
await asyncio.gather(*(run_finder(f) for f in finders))
Usable as either with or async with — pick whichever the call site is. The class is carried in a contextvars.ContextVar, so any subagent / asyncio task spawned inside the block inherits it, and nested scopes restore the enclosing class on exit. When the limiter is off (model.max_inflight: 0, the default) the class is still set but costs nothing — acquisition is a pass-through.
Args:
cls— one of"interactive"/"default"/"bulk".
Raises:
ValueError— ifclsis not one of the three classes — the message names the valid ones.
sdk.llm_lanes
sdk.llm_lanes() -> dictThe ADR 0115 D8 gateway in-flight-limiter snapshot for this process, read straight out of memory (no HTTP, no DB).
The same payload the operator console reads over GET /api/telemetry/llm-lanes, but a plugin running inside the agent process (pr-reviewer's review panels, say) reads it here without a round-trip. Use it to tell a lane that is deliberately queuing from a gateway that has gone slow: high queued / wait_p90_s_5m or saturated: true with slots full is local back-pressure this process is applying on purpose; retries or timeouts while slots are NOT saturated point at the gateway itself.
Returns:
{"enabled": bool, "generated_at": iso, "lanes": [...]}. enabled is false (and lanes empty) when the limiter is off (model.max_inflight: 0, the default). Each lane entry carries lane, limit, reserve, inflight, queued, queued_by_priority (interactive/default/bulk), oldest_wait_s, wait_p50_s_5m / wait_p90_s_5m, queue_timeouts_5m and saturated (queued > 0 held continuously for 60 s or more).
Knowledge graph (the plugin↔knowledge channel, ADR 0043 — "shared knowledge")
sdk.knowledge_search
await sdk.knowledge_search(query: str, *, k: int = 5, domain: str | None = None, epoch: str | None = None) -> list[dict]Search the agent's knowledge graph (hybrid FTS5 + embeddings); return the top-k matching chunks (each a dict with preview/content, domain, score …), or [] when no store is configured. domain scopes to one bucket (e.g. "loop-lessons"); epoch (#1634) scopes to chunks tagged with exactly that era via knowledge_add(..., epoch=...) — out-of-era and untagged chunks don't match (both search modes filter). None = unfiltered.
sdk.knowledge_add
await sdk.knowledge_add(content: str, *, domain: str = 'general', heading: str | None = None, epoch: str | None = None, memory_kind: str | None = None, delivery_policy: str | None = None, review_state: str | None = None, expires_at: str | None = None) -> int | NoneAdd one chunk to the agent's knowledge graph; return its id, or None when no store is configured / it was a no-op. domain is the bucket, heading an optional title — e.g. knowledge_add(lesson, domain="loop-lessons", heading=cls). epoch (#1634) tags the chunk with the era it was learned in — an opaque string, typically a reset date (epoch="2026-06-29"). On the next wipe the plugin just searches with the NEW epoch: old lessons stay for post-mortems but stop matching. memory_kind / delivery_policy (ADR 0108 D4) are the typed-memory columns — what the chunk IS ("fact", "note", "reference", …) and WHEN it enters the prompt ("always" / "retrieved" / "on_demand"). review_state / expires_at (ADR 0108 D7) are the write lifecycle: whether an operator has confirmed the row ("confirmed" / "pending" / "rejected") and an optional ISO-8601 UTC timestamp after which it lapses. Omitted = the store's own stamping: kind inferred from the domain, retrieved, "pending" (a plugin write is not operator intent), no expiry.
sdk.knowledge_purge
await sdk.knowledge_purge(domain: str, *, before: str | None = None) -> intHARD-delete every chunk in domain — optionally only those created strictly before before (an ISO-8601 timestamp) — and return how many were removed.
The knowledge-lifecycle primitive (#1634): retire a bucket of now-wrong lessons (knowledge_purge("st-routes")) or expire just the stale tail (knowledge_purge("st-routes", before="2026-06-01")). Deletes consistently from every index (main rows, FTS, vectors); on a layered store only the PRIVATE tier is purged — the shared commons is curated, never bulk-deleted. Keep-for-audit retirement is the epoch tag instead (see knowledge_add). Returns 0 when no store is configured (or the backend has no purge_domain), when domain is empty, or when before is unparseable — it refuses rather than risk deleting the wrong rows.
Durable memory (ADR 0125)
sdk.memory_search
await sdk.memory_search(query: str, *, k: int = 5, kind: str | None = None) -> list[dict]Search the agent's durable MEMORY and return the top-k matching entries as dicts (each the MemoryEntry fields: id, kind, subject, content, delivery, review_state, provenance …), or [] when no memory store is configured. kind scopes to one of profile|standing|fact|decision. Memory only — ingested documents are knowledge_search, and prior-conversation history is the session-search path.
sdk.memory_save
await sdk.memory_save(content: str, *, kind: str = 'fact', subject: str | None = None, heading: str | None = None) -> int | NoneSave one durable memory and return its id, or None when no memory store is configured. A plugin write is provenance="agent" and starts review_state="pending" (the store stamps it) until an operator confirms it. kind is profile|standing|fact| decision (default fact); subject is the entity it is ABOUT, so a later fact on the same subject can supersede it; heading is an optional short label. For an ingested document use knowledge_add instead.
Goal-driven recurring loop (the OODA pattern)
sdk.run_in_session
sdk.run_in_session(session_id: str, prompt: str, *, delay_seconds: float = 0.0, job_id: str | None = None, enqueue_key: str | None = None) -> dictEnqueue prompt as a one-shot agent turn in session_id — non-blocking.
This is the primitive behind "when a goal fires, prompt the agent." Call it from a goal on_achieved / on_failed hook (registry.register_goal_hook(...)) — or any plugin event handler — with a prompt built from the terminal GoalState (its condition / last_reason / last_evidence), and the agent runs a follow-up turn (with that session's memory and full tool set) reacting to what just happened::
async def on_achieved(goal):
sdk.run_in_session(
goal.session_id,
f"The goal '{goal.condition}' just completed. Evidence: {goal.last_evidence}. "
f"Write up a summary and open the follow-up PR.",
)
registry.register_goal_hook(on_achieved=on_achieved)
Mechanics: it schedules a one-shot job (an ISO fire time, not a cron) into the session's context via the scheduler, so the turn runs on the normal fire path (the same loopback A2A call cron ticks use) and the caller returns immediately. It NEVER runs the turn inline, so it is safe to call from a goal hook / monitor tick without blocking it.
Args:
session_id— the A2A contextId to run the turn in (e.g.goal.session_id). An explicit chat/goal/job session is a deliberate continuation and keeps its context; passing the Activity context requests an isolated background job session instead (ADR 0129) — the scheduler assigns the turn a durablejob:execution owner of its own, never the shared Activity thread.prompt— the message the agent processes as a turn.delay_seconds— fire at now + this delay (default 0 → the next poll tick, ~1s).job_id— a stable id so a re-call REPLACES the pending one-shot (idempotent). Replacement is pending-only (ADR 0129): a fire already accepted or parked keeps its occurrence and execution owner, and the re-added turn queues behind it under the same owner instead of superseding it.enqueue_key— optional durable enqueue receipt key. Reusing it acknowledges the original enqueue, even after completion; it never replaces or replays it.
Returns {"ok", "job_id", "fires_at", "message"}; ok=False with a readable message when the scheduler is unavailable or the inputs are bad.
sdk.create_watch
sdk.create_watch(*, condition: str, verifier: str, verifier_args: dict | None = None, watch_id: str | None = None, interval_s: float | None = None, deadline: float | None = None, stall_after: int | None = None, run_prompt: str = '', run_session: str = '', trigger: str = 'met', repeat: bool = False) -> dictRegister a WATCH from a plugin (ADR 0067): poll condition — ground-truthed by the plugin verifier named verifier ("<plugin-id>:<name>") — on a cadence, and when it trips run run_prompt as a follow-up turn in run_session (via run_in_session) and fire on_met hooks. Plugin-verifier only (like a set_goal-tool goal); hold as MANY as trigger + repeat decide WHEN it fires and whether firing ends it: met/False is the default one-shot tripwire; met/True fires on every rising EDGE of the predicate (never per-tick while it stays true); change/True is a standing monitor — it fires whenever the verifier's evidence MOVES, whatever the predicate says, and calls on_changed rather than on_met. A repeating watch only ends on its deadline or an explicit clear, so give it one if it shouldn't run forever.
you like (unlike a monitor goal, which is one-per-session). Returns {"ok", "watch_id", "message"} — ok=False with a readable message if the subsystem is off or the verifier is rejected.
sdk.list_watches
sdk.list_watches(prefix: str = '') -> list[dict]List the registered watches — each {"id", "condition", "status", "verifier"} — optionally filtered to ids starting with prefix. This is the read half create_watch was missing (#1638): a plugin that arms a watch suite under stable ids (st-credits, st-contract …) lists its own with list_watches("st-") to verify the suite or render it on a dashboard, and — paired with clear_watch — reconciles on upgrade: clear the ids no longer in its spec set, then create/replace the rest (stable-id replace alone only heals specs that still exist; a renamed/dropped spec would keep polling forever). Returns [] when the watch system is unavailable.
sdk.clear_watch
sdk.clear_watch(watch_id: str) -> boolRemove the watch watch_id (it stops being polled; its state is deleted). Returns True if it existed, False when it didn't — or when the watch system is unavailable. The remove half of the list_watches reconcile pattern.
sdk.update_watch
await sdk.update_watch(watch_id: str, **fields) -> dictEdit a live watch in place — {"ok", "watch_id", "message"}.
Accepts any of condition / interval_s / deadline / stall_after / run_prompt / run_session; an omitted field is untouched, and passing None CLEARS one (deadline=None drops the expiry). Async, unlike its sibling create_watch, because the edit takes the controller's per-watch lock so it can't interleave with a tick that is mid-evaluation on the same watch.
A plugin may only edit a watch whose verifier is plugin — the same trust boundary create_watch enforces (ADR 0067 D4) — and cannot change the verifier at all. Terminal watches are immutable; create a new one.
This completes the reconcile loop that list_watches / clear_watch began (#1638): a plugin whose spec changed a threshold no longer has to clear and recreate, which reset the watch's accumulated stall state every time it shipped a tweak.
sdk.start_goal_loop
sdk.start_goal_loop(*, goal: str, verifier: str, every: str, prompt: str, plugin_id: str, loop_id: str, session_id: str = '', verifier_args: dict | None = None, done_prompt: str = '', timezone: str | None = None, interval_s: float | None = None, deadline: float | None = None, stall_after: int | None = None) -> dictWire a goal-driven recurring loop in ONE call (the OODA / self-improving pattern): arm a WATCH on goal — ground-truthed by the plugin verifier verifier — and schedule the recurring prompt tick that drives it. Sugar over create_watch + schedule_recurring under a shared derived id (#2060); goals themselves stay drive-only (ADR 0067) — the "wait for the metric" half IS the watch.
The tick keeps firing until torn down: call stop_goal_loop from a watch on_met hook for a self-retiring cadence (or from a stop tool). Register the verifier — and that hook — in register() first; pass session_id from your tool's InjectedState so the tick (and the done_prompt reaction) run in the caller's session::
from graph.sdk import start_goal_loop, stop_goal_loop
start_goal_loop(goal="reach 1M credits", verifier="fleet:credits",
verifier_args={"min": 1_000_000}, every="30m",
prompt="Run the OODA tick and report.",
plugin_id=registry.plugin_id, loop_id="credits-1m",
session_id=sid)
PREFIX = f"{PLUGIN_ID}:goal-loop:"
async def on_met(watch): # self-retiring cadence
if watch.id.startswith(PREFIX):
stop_goal_loop(plugin_id=PLUGIN_ID, loop_id=watch.id.removeprefix(PREFIX))
registry.register_watch_hook(on_met=on_met)
Idempotent by (plugin_id, loop_id): a re-call REPLACES both the pending tick and the watch, so a plugin re-arms in register() without colliding. If the tick can't be scheduled the watch is rolled back — never half a loop.
Args:
goal— the condition the watch verifies (e.g."reach 1,000,000 credits").verifier— the registered plugin verifier name,"<plugin-id>:<name>".every— the tick cadence — a 5-field cron ("0 */6 * * *") or a duration shorthand"15m"/"2h"/"1d".prompt— the recurring tick prompt (e.g. "Run the manage-the-fleet OODA tick …").plugin_id— the owning plugin's id (registry.plugin_id) — the tick job rides the plugin namespace, so disable/uninstall sweeps it.':'is rejected.loop_id— a stable plugin-local id for this loop (e.g."credits-1m").session_id— the session the tick fires into; empty → the durable Activity thread.verifier_args— declarative args for the verifier (e.g.{"min": 1000000}).done_prompt— optional follow-up turn to run (insession_id) when the watch trips — the wrap-up/celebration half. Requires a non-emptysession_id.timezone— IANA tz the cron is evaluated in (None = UTC). interval_s, deadline, stall_after: watch knobs, passed through tocreate_watch(poll floor / epoch-seconds deadline / stall hook).
Returns {"ok", "goal", "loop_id", "watch_id", "job_id", "schedule", "message"}; ok=False with a readable message when a subsystem is off or the inputs are bad.
sdk.stop_goal_loop
sdk.stop_goal_loop(*, plugin_id: str, loop_id: str) -> dictTear down the goal loop (plugin_id, loop_id): cancel its recurring tick and clear its watch — both re-derived, so the caller only holds the two ids it started the loop with. Idempotent: stopping an absent loop is ok=True with both flags False. Call it from a watch on_met hook (the self-retiring pattern in start_goal_loop's example), a stop tool, or when winding down.
Background jobs (ADR 0050 spawn + ADR 0070 results pipeline)
sdk.spawn_background
await sdk.spawn_background(prompt: str, *, subagent_type: str, origin_session: str, label: str | None = None) -> dictSpawn a detached background subagent job (ADR 0050) and return immediately.
The job runs as its own detached A2A turn under the subagent_type role; when it finishes, the ADR 0070 pipeline delivers the report — a push-resume nudge into origin_session, the notified-gated <task-notification> drain, knowledge-store indexing, and the console report card. Poll in between with background_status (e.g. to render campaign progress on a plugin dashboard).
Args:
prompt— detailed instructions for the background worker.subagent_type— which subagent role runs the job — one of the registered roster (subagent_types), plugin-contributed subagents included.origin_session— the chat session the report drains back into (and gets the completion nudge). Required — a job with no origin has nowhere to report.label— short human description for the job card / report heading. Defaults to the first line ofprompt(clipped).
Returns {"ok", "task_id", "message"} — task_id is the bg-… job id (the handle for background_status, cancel, and the by-id API route); ok=False with a readable message when the background subsystem is off or the inputs are bad.
sdk.background_status
sdk.background_status(task_id: str) -> dictLook up a background job by its bg-… id — the status-query companion to spawn_background, so a plugin can render campaign progress on its own surface instead of being blind between launch and the ADR 0070 completion nudge.
Returns {"ok", "task_id", "status", "subagent_type", "description", "created_at", "completed_at", "message", "delivery", "peer_task_id", "amends"} plus — only once the job is terminal (completed/failed/canceled) — "report" with the full result text. delivery is in_flight / undelivered / delivered (whether the outcome has reached the origin session); peer_task_id is the task id a peer gave a delegated request, blank until the peer accepted it. An unknown id (or the subsystem being off) returns ok=False with status="unknown" and a readable message. Reads the durable jobs store directly (cheap local SQLite).
Reactive rules (ADR 0039 events → one-shot turns)
sdk.react_on
sdk.react_on(topic: str, *, prompt: Callable[[dict], str | None], job_id: str, session: str = 'system:activity', debounce_s: float = 0.0) -> Callable[[], None]When a bus event matching topic fires, enqueue a follow-up agent turn.
prompt is called with the full event payload ({"event", "data", "seq"}) at delivery time; return the turn's prompt text, or None/empty to skip that event (cheap filtering). The turn is enqueued via run_in_session with job_id, so a rule re-fires idempotently (a pending turn is REPLACED, never duplicated)::
unsub = sdk.react_on(
"spacetraders.opportunity",
prompt=lambda ev: f"A {ev['data']['margin']}% route appeared. Evaluate it.",
job_id="spacetraders-opportunity",
debounce_s=30,
)
Args:
topic— bus topic pattern (*= one segment,#= tail — any namespace; subscribing is read-only, likeregistry.on).prompt—(payload) -> str | None— the prompt builder / filter.job_id— stable id for the enqueued turn (run_in_session's idempotent-replace key). Required — it's what keeps a chatty rule from stacking turns.session— the session the turn runs in. An explicit chat/goal session is a deliberate continuation; the default (ACTIVITY_CONTEXT) requests an isolated background job session (ADR 0129) — the enqueued turn gets a durablejob:execution owner of its own and never executes on the shared Activity thread, which receives only the result summary on its feed.debounce_s— > 0 coalesces a burst into ONE turn — trailing-edge: the timer re-arms on every qualifying event, firesdebounce_safter the LAST one, and that last event's prompt text wins. Skipped events (promptreturnedNone/empty) neither fire nor extend the window. Note a sustained stream arriving faster thandebounce_skeeps deferring the turn (classic debounce). Thread-safe — the bus may deliver from worker threads.
Returns an unsubscribe callable (mirroring registry.on's seam): it stops delivery and cancels any pending debounce timer. When no host bus is wired (tests, headless), logs a warning and returns a no-op unsubscribe.
Plugin-owned recurring jobs (#1642)
sdk.plugin_job_prefix
sdk.plugin_job_prefix(plugin_id: str) -> strThe id prefix every scheduler job owned by plugin_id carries.
sdk.schedule_recurring
sdk.schedule_recurring(prompt: str, cron: str, *, plugin_id: str, job_id: str, session: str = '', timezone: str | None = None) -> dictSchedule prompt as a RECURRING agent turn on a cron cadence, owned by plugin_id.
Thin over STATE.scheduler.add_job with the id namespaced plugin:<plugin_id>:<job_id> — that ownership tag is what lets the host cancel the plugin's jobs on disable/uninstall (and cancel_scheduled / cancel_plugin_jobs find them). Idempotent by id: re-calling with the same job_id REPLACES the pending job, so a plugin re-arms its cadence in register() (or when a cadence knob changes) without colliding.
There is no ambient plugin identity in the SDK (functions are plain imports, not registry-bound), so plugin_id is an explicit required kwarg — pass registry.plugin_id. Note: a disable cancels the plugin's jobs; re-enabling relies on the plugin re-arming in register(), which runs after the scheduler is wired on both boot and hot-reload.
Args:
prompt— the message the agent processes each fire.cron— a 5-field cron expression (e.g."0 9 * * *"). One-shot turns belong torun_in_session, so an ISO datetime is rejected here.plugin_id— the owning plugin's id (registry.plugin_id).job_id— a stable plugin-local id for this cadence (e.g."strategist-tick").session— the A2A contextId to fire into; empty → an isolated background job session of its own (ADR 0129; the Activity feed carries each fire's result summary).timezone— IANA name the cron is evaluated in (None = UTC).
Returns {"ok", "job_id", "next_fire", "message"} — job_id is the full namespaced id; ok=False with a readable message when the scheduler is unavailable or the inputs are bad.
sdk.cancel_scheduled
sdk.cancel_scheduled(job_id: str, *, plugin_id: str) -> boolCancel the plugin-owned recurring job job_id (the same plugin-local id passed to schedule_recurring — namespacing is applied here, never by the caller). Returns True if a job was removed; False when there was none — or when the scheduler is unavailable.
sdk.cancel_plugin_jobs
sdk.cancel_plugin_jobs(plugin_id: str) -> intCancel EVERY scheduler job owned by plugin_id (ids plugin:<plugin_id>:*). Returns how many were cancelled (0 when the scheduler is unavailable).
This is the lifecycle hygiene hook (#1642): the loader sweeps a disabled plugin's jobs on (re)load and the installer sweeps on uninstall, so an orphan cadence can't keep firing prompts about a plugin that's gone. Only jobs under this instance's agent_name are visible (list_jobs filters), so the ADR 0004 scoping holds. Also useful to a plugin that wants to tear down its whole cadence at once.
Plugin state (own the file, never core's)
sdk.deleted_chat_sessions
sdk.deleted_chat_sessions(session_ids) -> set[str] | NoneWhich of these console chat session ids the operator has DELETED.
A deleted chat leaves a durable tombstone (ADR 0104; never count-evicted), so a session id in the returned set is gone for good: no transcript of it survives to point at anything a plugin keeps for it. Use it to retire per-session state only once its chat is gone — e.g. the artifact store keeps an inline answer while any chat that shows it still exists (#4138).
Returns None when it can't tell (no task store at this instance, or it can't be read). Treat None as "every one may still exist" — never delete on a guess. Synchronous and cheap (one indexed read); safe from sync code in any runtime process.
sdk.plugin_store
sdk.plugin_store(subdir: str = '', *, plugin_id: str) -> PathThis plugin's own writable directory, scoped to the running instance.
Use it for state the plugin owns — a SQLite file, a cache, exports, generated assets. The directory is created if absent, and it lives under the instance root (ADR 0004 / ADR 0065), so the dev sandbox and every fleet member get their own copy without the plugin doing anything: the isolation that used to be each plugin's job to remember.
Do not open core's databases (checkpoints.db, knowledge.db, the telemetry or tasks stores). They carry no compatibility promise for outside readers and core migrates them freely; reach core data through this SDK instead (knowledge_search, metric_history, …).
Prefer an existing seam where one fits — small numeric series belong in record_metric, retrievable facts in knowledge_add, and an index you can rebuild at load belongs in memory. Reach for a file when you have durable, structured, plugin-shaped state.
Args:
subdir— optional path under the plugin's directory ("exports","cache/thumbs"). Must be relative and must not escape upward; a traversing or absolute value is rejected.plugin_id— the owning plugin's id (registry.plugin_id) — explicit, likerecord_metric, since the SDK has no ambient plugin identity.
Returns the created directory as a Path.
Raises:
ValueError— on an empty/unsafeplugin_idor an escapingsubdir— a bad path is raised rather than silently redirected, because writing an operator's data somewhere unexpected is worse than failing loudly.
from graph import sdk
db = sdk.plugin_store(plugin_id=registry.plugin_id) / "state.db"Plugin metric timeseries (#1632)
sdk.record_metric
sdk.record_metric(name: str, value: float, *, ts: float | None = None, plugin_id: str) -> dictAppend one sample to the plugin metric timeseries name (#1632).
Small named numeric series — treasury, net worth, fleet size — are what history-dependent watch verifiers (ADR 0067 drawdown-vs-high-water, flatline detection) and dashboard sparklines need, and a verifier can only see live state (sdk.telemetry() is point-in-time). Series are namespaced <plugin_id>:<name>, SQLite-backed in the instance dir (observability/metrics_store.py), and retention-capped per series (90 days / 10k points, trimmed on write) — record freely from an engine tick.
Args:
name— the plugin-local series name (e.g."credits"). Namespacing is applied here, never by the caller.value— the sample — any real number (NaN/inf are rejected; they poison drawdown math downstream).ts— Unix epoch seconds for the sample;None→ now. Useful for backfill and deterministic tests.plugin_id— the owning plugin's id (registry.plugin_id) — explicit, likeschedule_recurring;':'is rejected.
Returns {"ok", "series", "message"} — ok=False with a readable message when the store is unavailable (non-server context) or the inputs are bad; a write failure never raises into an engine loop.
sdk.metric_history
sdk.metric_history(name: str, *, since: float | None = None, limit: int = 500, plugin_id: str) -> list[tuple[float, float]]The newest limit samples of the plugin metric series name — at/after since (Unix epoch seconds) when given — returned oldest→newest as (ts, value) tuples: chronological order, ready for verifier math (high_water = max(v for _, v in points)) or a sparkline. Returns [] when the store is unavailable, name/plugin_id are invalid, or the series has no samples. Same namespacing + plugin_id contract as record_metric.
sdk.metric_last
sdk.metric_last(name: str, *, plugin_id: str) -> tuple[float, float] | NoneThe most recent (ts, value) of the plugin metric series name, or None when there is no sample (never recorded / fully aged out / store unavailable). The cheap read for "what did I last see?" checks — e.g. a verifier comparing the live reading against the last recorded one. Same namespacing + plugin_id contract as record_metric.
Run tracing (one Langfuse trace per multi-step plugin run)
sdk.trace_attributes
sdk.trace_attributes(metadata: dict | None = None, *, tags: list[str] | None = None)Stamp metadata on every observation traced inside the block, tags on its trace.
Every subagent:<type> span a run_subagent call opens in the block — however deep — and every tool call and generation under it carries metadata in its own metadata. The keys stay on the OBSERVATIONS, not the trace: a trace holds a whole run, and trace metadata has one value per key, so a per-step key there would read as whichever step started last. tags are a set and do go on the trace. Scopes nest and merge; the innermost key wins. Use it around one step of a multi-step run, inside trace_run::
with sdk.trace_attributes({"step_id": sid}, tags=[f"step:{sid}"]):
out = await sdk.run_subagent(subagent, prompt, description=...)
Pass ids and labels — never raw content: nothing here is redacted for you. A no-op (beyond the contextvar) when tracing is disabled; the block always runs.
sdk.trace_run
sdk.trace_run(name: str, *, run_id: str = '', input: Any = None, metadata: dict | None = None, inputs: dict | None = None, tags: list[str] | None = None)Group a plugin's multi-step run into ONE Langfuse observation.
Every run_subagent call opens a subagent:<type> span in the CURRENT trace. One trace per execution SEGMENT: a run that parks (a human gate) and later resumes is two traces, grouped in Langfuse's Sessions view by the shared run_id. The OTel context isn't persisted across a pause of unbounded length.
Called from a turn (the run_workflow tool), that nests; called with no turn around it (a REST route, a detached Studio run, a scheduled fire), each step became its own sessionless ROOT trace — a 9-step review panel was 9 unrelated traces, and a busy reviewer buried every agent's turns under them.
Inside a traced turn this opens a nested span, in the turn's trace and session; otherwise it opens the run's own root trace, whose session is the caller's session when there is one and run_id when there isn't. Input/output are redacted and capped, and dropped entirely in an incognito turn. A block that raises marks the observation ERROR and re-raises unchanged. A no-op when tracing is disabled — the block always runs::
async with sdk.trace_run(f"workflow:{name}", run_id=run_id, input=inputs) as run:
result = await execute(...)
run.output(result["output"], failed=bool(result["failed"]))
Join keys (#3565). Every observation in the run — each step's subagent: span included — carries run_id, metadata, the caller's session as parent_session_id, and the run's scalar inputs (default: input when it is a dict) as redacted, capped input_<name> keys; the trace is tagged with name, run:<run_id> and tags. Incognito keeps the ids and drops the inputs. Add per-step keys with trace_attributes.
Delegation ledger (who handed what work to whom)
sdk.record_delegation
sdk.record_delegation(*, to_kind: str, to_name: str, to_instance: str = '', what: str = '', session_id: str = '', parent_task_id: str = '', task_id: str = '', outcome: str = 'ok', error: str = '', duration_ms: int = 0, cost_usd: float | None = None, origin: str = '') -> int | NoneRecord one delegation edge in the instance ledger — who handed what work to whom.
The seam a plugin uses to put its own dispatches on the org's record. A plugin that hands work to an agent or a coder outside the built-in funnels (a bespoke scheduler, a board loop of its own) is otherwise invisible: turn telemetry has no actor column and no edge, so its work would show up as spend with no explanation of who asked for it.
cost_usd=None means UNKNOWN, not free — pass a number only where one is actually measured. A confident zero makes an unmeasured delegate look free and silently understates every rollup built on the column.
Best-effort and never raises: a ledger failure must not break a dispatch.
Returns the new edge_id, or None when no store is wired or the write failed.
sdk.delegation
sdk.delegation(*, to_kind: str, to_name: str, to_instance: str = '', what: str = '', session_id: str = '', origin: str = '', task_id: str = '')Wrap a plugin's own dispatch so it is on the record WHILE it runs, not just after.
record_delegation writes a finished edge; this is the funnel delegate_to itself uses. The edge is live for the whole block — GET /api/ledger/live and the delegation.* bus frames carry it, so the org chart lights the delegate up as it works — and one row is written on every exit: ok, failed (the block raised, re-raised unchanged) or cancelled. Use it around any dispatch outside the built-in funnels, such as a board loop's coder run::
with sdk.delegation(to_kind="acp", to_name=coder.name, what=card_title, origin="project_board") as edge:
reply = await adapter.dispatch(coder, prompt)
edge.cost_usd = measured_cost # optional; leave None when unmeasured
The yielded handle takes cost_usd, task_id and to_instance for what the caller only learns during the dispatch. A ledger failure never breaks the block.
sdk.run_scope
sdk.run_scope(*, run_id: str = '', session_id: str = '')Group a plugin's work outside any turn into one run in the delegation ledger (#4726).
Every edge opened inside the block records run_id as its run (and session_id when given), so separate dispatches that belong together (a board card's coder run and its review) read back as one tree instead of unrelated roots. Inside a turn, prefer nothing: the turn already scopes its work. Empty arguments inherit the enclosing scope.
sdk.current_run_id
sdk.current_run_id() -> strThe delegation-ledger run the calling code belongs to, or "" outside any (#4726).
Subscription usage (the plugin↔quota channel, ADR 0122 D6/D7/D8)
sdk.provider_usage
sdk.provider_usage(account: str | None = None, *, delegate: Any = None, model: str | None = None) -> dict | NoneThis OAuth-subscription account's remaining headroom, or None when it can't be read.
The plugin read path for subscription usage (ADR 0122 D6/D8): a router that wants to avoid an account near its limit asks here, rather than reaching into core's usage store or fetchers. The account is resolved from account (a provider id, e.g. "anthropic-oauth") or, failing that, from delegate via account_for_delegate (the ACP coder → login mapping, D7) — delegate is a delegate name or its config entry.
The store is read without forcing a poll, so the on-demand ≤1/60 s contract holds: two calls inside a minute cause at most one network fetch. Returns None — never raises — when the account can't be resolved, the store holds no (or a stale/unknown) snapshot for it, any window has a missing or invalid usage percentage, or anything throws; the caller then behaves exactly as it does today (D4).
With a fresh snapshot, returns:
usable:usable_forformodel—Falseonly when a window that applies tomodelisblocked. A window scoped to another model is ignored, so a Fable-scoped block never makes Opus unusable (andmodel=Nonecounts only unscoped windows).headroom_pct:0when the binding window is blocked; otherwise100minus itsused_pct(100when none apply).binding_window: the id of the applicable blocked window with the latest reset, since every blocking window must reset before the account becomes usable. If none are blocked, the applicable window with the highestused_pct(Nonewhen none apply).resets_at: that window's reset time as an ISO-8601 UTC string (Nonewhen none apply).
Guard the lookup with getattr(sdk, "provider_usage", None) on cores older than this seam.
Managed Python runtime (ADR 0094 — the provisioned child interpreter)
sdk.managed_python_exe
sdk.managed_python_exe() -> Path | NoneThe managed Python runtime's interpreter, or None when it isn't provisioned.
This is the interpreter execute_code spawns on the packaged desktop app, where sys.executable is the frozen server binary and cannot run a script. It is a pinned CPython the host downloads on demand into the box-shared data dir (runtime.python_install), shared by every instance on the machine.
Returns the interpreter's Path only when a working install is present — the executable actually exists — so a half-extracted, wiped or never-provisioned runtime reads as None rather than as a path that fails to spawn. On None, a source run can fall back to sys.executable (what execute_code itself does); a frozen one should tell the operator to provision the managed Python runtime rather than guess at a system Python of arbitrary version.
Cheap (one stat), never spawns, never raises.
import sys
from graph import sdk
exe = sdk.managed_python_exe()
if exe is None and not getattr(sys, "frozen", False):
exe = Path(sys.executable) # source run: this process's own interpreterNode.js runtime (ADR 0085, amended 2026-10-15 — one resolver for every Node consumer)
sdk.node_executable
sdk.node_executable(min_version: str | None = None) -> Path | NoneAn ABSOLUTE path to a node executable at least min_version, or None.
min_version is a version floor — "20", "20.6", "22.11.0" (a leading >= is tolerated, so a manifest's requires_runtimes value can be passed straight through); None accepts any version. The host checks, in order: the node on the process PATH, the nvm / fnm / Volta / asdf / nodenv / Homebrew installs it can discover off PATH (newest first), then the managed runtime (protoagent runtime install-node, or the console's Install Node runtime button). Each candidate is version-checked by running it (cached per binary), and the first that satisfies the floor wins.
Spawn the returned path directly — [str(node), str(bundle), …] — rather than a bare "node": it is exactly the binary that was checked, so a stale Node earlier on PATH can't sneak in at spawn time. Ship a vendored single-file bundle and run it this way; never npx a package at runtime or run npm install from plugin code.
None means nothing satisfies the floor: degrade with an actionable message. Declaring requires_runtimes: {node: ">=20"} in the manifest makes the host raise the setup banner (with a one-click managed install) for you. Never raises — a malformed min_version is logged and reads as None.
from graph import sdk
node = sdk.node_executable("20")
if node is None:
return "Needs Node.js 20+: install it from the setup banner, or run `protoagent runtime install-node`."
subprocess.run([str(node), str(BUNDLE), "--json"], check=True)Per-plugin Python venv + worker (ADR 0085 amendment 2026-11-08, #4807 part 2)
sdk.plugin_python
sdk.plugin_python(plugin_id: str) -> Path | NoneThe interpreter of this plugin's provisioned Python venv, or None.
A plugin that declares requires_runtimes: {python: {version, requirements}} gets its OWN <plugin_store>/.venv (provisioned with uv on the operator's Install click, ADR 0085 amendment) — separate from the box-shared managed runtime (managed_python_exe), so its heavy deps never load into the host process. This returns that venv's python only when it is actually on disk; None means it is not provisioned (never installed, half-built, or wiped), so the caller degrades with an actionable message rather than spawning a path that fails. Honours the PROTOAGENT_PLUGIN_PYTHON_<ID> override (tests / CI point it at a fixture venv via graph.plugins.testkit.use_fixture_python so contract tests run without uv).
Cheap (one stat), never spawns, never raises — a bad id or a lookup failure reads as None. Most callers want worker / aworker instead of spawning by hand.
from graph import sdk
exe = sdk.plugin_python(registry.plugin_id)
if exe is None:
return "This plugin's Python runtime isn't provisioned — hit Provision Python runtime in Settings."sdk.worker
sdk.worker(plugin_id: str, argv_or_script, payload, *, timeout: float = 60) -> dictRun a short JSON-line worker in this plugin's venv and return its one-line result.
The protocol is deliberately tiny: the child is spawned under plugin_python, payload is written to its stdin as ONE json line, and its result is read as the LAST non-empty line of stdout (so the child may log to stderr freely, and its result is simply the final line it prints). The child must emit a JSON object; that parsed dict is returned verbatim on success. This is the seam a heavy-dependency plugin uses instead of loading its deps into the host — the compute lives in the venv, the host just hands it a payload and takes back a dict.
Structured, never-raising errors ({"ok": False, "error": …}) on:
- no runtime — the venv isn't provisioned (carries the setup-banner remedy);
- timeout — the child is killed and reaped, then an error is returned (
returncodeomitted); - non-zero exit —
errorcarries the stderr tail, withreturncodeandstderr; - non-JSON output — the child printed nothing parseable as a final JSON object. A success dict carrying the child's own
okfield is passed through untouched, so a worker that reports its own failure is distinguishable from these host-level ones.
Args:
plugin_id— the owning plugin's id (registry.plugin_id).argv_or_script— a single script path, or a full argv (["-m", "pkg.worker"]), appended after the interpreter.payload— a JSON-serializable value, written to the child's stdin as one line.timeout— seconds before the child is killed (default 60).
Blocking — call it from a thread / background surface, or use aworker on the event loop.
from graph import sdk
out = sdk.worker(registry.plugin_id, "cad_worker.py", {"op": "mesh", "stl": path}, timeout=120)
if not out.get("ok", True):
return f"CAD worker failed: {out['error']}"
return out["summary"]sdk.aworker
await sdk.aworker(plugin_id: str, argv_or_script, payload, *, timeout: float = 60) -> dictAwaitable worker — runs the blocking spawn in a thread so it never blocks the event loop (same shape as knowledge_search's asyncio.to_thread). Use this from async plugin code (a tool coroutine, a surface tick); the return value and error contract are identical to worker.