Skip to content

prefactor_core.monitoring package

class prefactor_core.monitoring.TerminationMonitor(fetch_instance: Callable, poll_interval: float = 30.0)

Section titled “class prefactor_core.monitoring.TerminationMonitor(fetch_instance: Callable, poll_interval: float = 30.0)”

Bases: object

Monitors an agent instance for termination.

Thread-safety note: detect_termination and get_termination_event().is_set() are safe to call from sync worker threads — they only read/write a bool under the GIL (asyncio.Event._value is a plain bool).

Permanently shut down the monitor (no further events will fire).

detect_termination(reason: str | None) → None

Section titled “detect_termination(reason: str | None) → None”

Signal that the instance has been terminated.

No-op if already terminated, destroyed, or fenced (post-reset stale call).

Prepare monitor for the next agent run.

  • Creates a fresh (unset) event
  • Clears the termination reason
  • Cancels any in-flight poll
  • Sets fence so stale callbacks from the dying run are ignored
  • Increments generation so stale polls self-discard

subscribe(callback: Callable[[], None]) → Callable[[], None]

Section titled “subscribe(callback: Callable[[], None]) → Callable[[], None]”

Register a callback invoked on termination. Returns an unsubscribe fn.

Update the tracked instance ID and start/stop the fallback poll.

Idempotent: calling with the same non-None ID when a poll is already running does not restart it (preserving the sleep interval).