HookContext#

class brainstate.HookContext(operation, state_ref, timestamp=<factory>, metadata=<factory>)[source]#

Base context object passed to all hooks.

This context provides information about the hook execution environment, including the operation type, the state being operated on, timing information, and user-defined metadata.

operation#

Type of operation (‘read’, ‘write_before’, ‘write_after’, ‘restore’, ‘init’)

state_ref#

Weak reference to the State instance (avoids circular references)

timestamp#

Unix timestamp when the operation occurred

metadata#

Dictionary for user-defined metadata

property state: State | None#

Get the State instance if it’s still alive.

Return type:

The State instance, or None if it has been garbage collected.

property state_name: str | None#

Get the name of the State instance.

Returns:

  • The state’s name, or None if the state has been garbage collected

  • or doesn’t have a name.