Skip to main content
NSPSession is the high-level interface for interacting with a single attached application. It bundles the process ID and app identifier together so you never have to pass them around manually. Every state read, action call, and streaming subscription goes through a session.

Creating a Session

You create a session by calling one of the attach methods on NSPClient:

Session Properties

Reading State

Typed Accessor Methods

Use the typed getters to read individual values from the cached state. These methods never raise — they return the provided default (or None) when a key is missing:

Prefix Query

Retrieve all state keys that share a common prefix as a flat dictionary:

Accessing the Raw State Object

For operations not covered by the typed helpers, work directly with the SubstrateState object:

Cached State vs. Fresh State

session.state holds the last snapshot received from the daemon. It does not automatically update. You have two ways to get newer data:

session.refresh()

Fetches the latest cached snapshot from the daemon (~1 ms). Use this after an action or when you need a more recent read without triggering a full probe.

session.refresh(fresh_probe=True)

Forces the daemon to run a new probe cycle before returning (50 ms–90 s). Use this only when you need a guaranteed up-to-date reading.

Executing Actions

execute() Method

action
str
required
The action name, as it appears in session.actions.
parameters
dict
default:"{}"
Key-value pairs matching the action’s parameter schema.
verify
bool | None
default:"None"
Enable post-action verification. Automatically set to True for IrreversibleWrite actions — you cannot set this to False for irreversible actions without raising NSPIrreversibleNotConfirmedError.
verify_expression
str | None
default:"None"
A JavaScript expression evaluated against the post-action state. If it returns falsy, the action is marked as failed.
verify_timeout_ms
int
default:"5000"
How long (in milliseconds) to poll for state settlement after the action runs. Range: 100–30000.
auto_refresh
bool
default:"True"
When True, refreshes session.state automatically after the action completes.

Usage Examples

ActionResponse Fields

success
bool
True if the action ran and (when enabled) verification passed.
latency_ms
int
Total round-trip time from dispatch to response, in milliseconds.
result
Any
The raw return value of the action as reported by the runtime. null for actions that return nothing (UI interactions, void methods).
verification
VerificationResult | None
Present when verify=True. Contains satisfied: bool, actual_value, expression, evaluated_at, and latency_ms. See VerificationResult.
error_message
str | None
Human-readable failure description when success is False.

Watch Stream

Open a real-time WebSocket stream of state-change events:
See the Streaming page for complete documentation.

Wait Patterns

wait_for(predicate, *, timeout, poll_interval, fresh_probe)

Poll the state until a lambda condition is satisfied:

wait_for_key(key, *, expected_value, timeout, poll_interval, fresh_probe)

Wait for a specific key to reach an exact value:

Waiting for Change After an Action

Capture a baseline value, execute the action, then wait for the value to change:

Complete Agent Example