ncm_engine
NCM Homeostasis Engine.
Applies YAML-defined rules to the neurochemical vector, producing regulatory deltas and UI cues. Purely computational — no Redis dependency.
v3 update: full condition evaluator supporting is_high, is_low, is_mid, is_mid_or_high, not_high, rising, {all: […]}, {any: […]}.
- class ncm_engine.NCMHomeostasisEngine(rules_dir=None)[source]
Bases:
objectRule-driven regulator that keeps Star’s neurochemical vector in band.
Loads a corpus of YAML-defined homeostasis rules from
ncm_rulesand, each turn, evaluates them against the current neurochemical vector to produce regulatory gain deltas, UI cues, and behavioral route flags – the negative-feedback machinery that pulls runaway nodes back toward their resting band. It is deliberately a pure computational component: no Redis, KG, LLM, or network access; the only state it carries between turns is the per-channel previous vector used to evaluaterisingconditions. Instantiated once byLimbicCoordinatorasself.engineand driven throughregulateinside the exhale pipeline.- Parameters:
rules_dir (str | None)
- __init__(rules_dir=None)[source]
Build the engine and eagerly load its homeostasis rule corpus.
Resolves the rules directory (defaulting to the
ncm_rulesfolder beside this module), reads every rule file through_load_rulesintoself.rules, and initializes the empty per-channelrising-state cache. Touches the filesystem at construction time to load YAML; called byLimbicCoordinatorwhen it stands upself.engine.
- evaluate(current_vector, channel_id='')[source]
Evaluate every rule against the vector and gather their outcomes.
Walks the priority-ordered rule corpus and, for each rule whose
when/conditionmatches (and whose optionalunlessguard does not), accumulates itsgain_deltasinto a combined delta map, collects any UI cues, and harvests behavioralroute_flags; branching rules additionally evaluate their nestedbranchesand apply the first matching branch. Condition matching is delegated to_check_condition. The only mutation of engine state is recording thecurrent_vectorinto the per-channel previous-vector cache so the next call can resolverisingpredicates. Called byregulate(and usable directly when only the deltas, not the mutated vector, are wanted).- Parameters:
- Returns:
A 3-tuple of
(regulatory_deltas, ui_cues, route_flags)– the summed gain deltas per node, the de-duplicated UI cue list, and the de-duplicated behavioral route-flag list.- Return type:
- regulate(current_vector, channel_id='')[source]
Apply the rules and return the regulated vector plus its signals.
The primary entry point of the homeostasis engine: it runs
evaluateto collect regulatory deltas, then applies those deltas to a copy of the input vector, clamping each node into the legal0.0to3.0supraphysiological range. The original vector is left untouched, but the engine’s per-channel previous-vector cache is refreshed for next-turnrisingdetection. Called byLimbicCoordinatorduring the exhale pipeline to produce the homeostatically-corrected vector together with its UI cues and route flags.- Parameters:
- Returns:
A 3-tuple of
(new_vector, ui_cues, route_flags)– the clamped, delta-applied vector and the cue and route-flag lists fromevaluate.- Return type: