Integrate¶
These helpers put Module.decide behind a service. handle_request
is the main entry point: it takes a JSON-shaped body with an optional
saved state, an event and evidence, restores the state (or resets
when none is given), decides once and returns the record as a dict.
DecisionSession replays a stream of events through one module, and
EvidencePrior, Volatility and draw_snapshot generate noisy
evidence for simulations and tests.
Public here: handle_request, DecisionSession, EvidencePrior,
Volatility, draw_snapshot (top level); dumps_record,
loads_event_evidence, trajectory, ramp_snapshots and
random_walk_p from grounded_decision.integrate. Guide:
Integrate into a service; full working
service: Build a decision service.
codec ¶
JSON request/response helpers for embedding Module.decide in a service.
The intended worker pattern is stateless:
- Persist
DecisionRecord.extras["state"]next to the entity. - On the next event, send that state in the request body.
handle_requestrestores it, steps once, returns a JSON record.
One Module instance may be reused across requests only if you
restore state at the start of each call (or use a dedicated instance
per in-flight session). The chart itself is not thread-safe.
loads_event_evidence ¶
loads_event_evidence(
body: Mapping[str, Any],
) -> tuple[Event, EvidenceSnapshot]
Parse the two kernel inputs from a JSON object.
handle_request ¶
handle_request(
module: Module,
body: Mapping[str, Any],
*,
restore_state: bool = True,
) -> dict[str, Any]
One HTTP-shaped call: optional state, then event + evidence.
Returns DecisionRecord.to_dict(). Downstream stores
extras["state"] and the verdict. This is the integration point
for FastAPI/Flask/gRPC wrappers; those frameworks are not a
dependency of this package.
Source code in src/grounded_decision/integrate/codec.py
session ¶
Sequential decide over a stream of (event, evidence) ticks.
DecisionSession
dataclass
¶
DecisionSession(
module: Module,
reset_each_tick: bool = False,
records: list[DecisionRecord] = list[DecisionRecord](),
)
One module, many ticks, optional reset between ticks.
reset_each_tick=False is the workflow/incident pattern: the
chart keeps its state. reset_each_tick=True is the independent
scoring pattern: each tick is a fresh intake.
TrajectoryRow ¶
Bases: TypedDict
One row of :func:trajectory.
trajectory ¶
trajectory(
records: Sequence[DecisionRecord],
) -> list[TrajectoryRow]
Compact time series for plotting act/abstain against evidence.
Source code in src/grounded_decision/integrate/session.py
priors ¶
Priors and volatility for evidence over time.
A prior is the mean graph the operator believes in. Volatility is
independent Gaussian noise on degree, λ, density, and the conformal
score, clipped to the legal range. Adaptive examples draw a path of
EvidenceSnapshots from these two objects so the same module can be
probed under calm, noisy, and regime-shift conditions.
EvidencePrior
dataclass
¶
EvidencePrior(
n: int = 80,
p_threshold: float = 0.1,
degree: float = 8.0,
lambda_mean: float = 0.55,
p_mean: float = 0.35,
label_mean: float = 0.1,
n_cal: int = 40,
n_states: int = 2,
cal_loc: float = 0.4,
cal_step: float = 0.02,
)
Mean evidence a service believes before seeing this tick's graph.
Volatility
dataclass
¶
Volatility(
degree: float = 0.0,
lambda_hat: float = 0.0,
p_hat: float = 0.0,
label_score: float = 0.0,
)
Per-tick standard deviations. Zero is a frozen prior.
draw_snapshot ¶
draw_snapshot(
prior: EvidencePrior,
vol: Volatility = CALM,
*,
rng: Generator | None = None,
) -> EvidenceSnapshot
One noisy draw from prior under vol.
Source code in src/grounded_decision/integrate/priors.py
ramp_snapshots ¶
ramp_snapshots(
*,
steps: int,
start: EvidencePrior,
end: EvidencePrior,
vol: Volatility = CALM,
rng: Generator | None = None,
) -> list[EvidenceSnapshot]
Linear interpolation of the prior, one draw per step.
Source code in src/grounded_decision/integrate/priors.py
random_walk_p ¶
random_walk_p(
*,
steps: int,
start: float,
threshold: float,
sigma: float,
n: int,
degree: float,
lambda_hat: float,
rng: Generator | None = None,
) -> list[EvidenceSnapshot]
Random walk on density; other fields frozen. Probes the FK window.