nx_flow_state.nx source
↩ module page · 284 lines · 11434 B
1// nx_flow_state.nx -- F1 first stone of NISHI_FLOW_DOCTRINE_ROADMAP.md.
2//
3// Aggregate flow-state classifier. Composes the substrate's
4// existing measurement layers (nx_perf_pathology, nx_etg outcomes,
5// conductor placement signals, cross-arch parity verdicts,
6// reclamation accounting) into a single sealed-enum verdict on
7// whether the system is currently at its empirical near-edge --
8// the substrate's flow state.
9//
10// Csikszentmihalyi's nine conditions for flow, translated to
11// substrate-testable signals:
12// 1. Clear goals -> reference band present
13// 2. Immediate feedback -> ETG attestation emitted
14// 3. Challenge-skill balance -> tournament selected variant
15// matching empirical ceiling
16// 4. Action-awareness merger -> conductor placement decided
17// 5. Loss of self-conscious -> substrate overhead < noise floor
18// 6. Distorted time -> N/A substrate tier (human-flow F7)
19// 7. Autotelic -> reclamation > 0 OR all confirmed
20// 8. Concentration on task -> no second-class resources idle
21// 9. Sense of control -> attestation chain complete
22//
23// All eight measurable conditions met simultaneously -> NX_FLOW_STATE_FLOW.
24// Cardinal violations (paywall honored, second-class resources,
25// single-arch overfit, latency spike) -> NX_FLOW_STATE_TILT.
26// Insufficient signals -> NX_FLOW_STATE_BROKEN.
27//
28// Composes:
29// [[NISHI_FLOW_DOCTRINE_ROADMAP]] F1 (this is the first stone)
30// nx_perf_pathology.nx (classifier feeds flow signals)
31// nx_etg.nx (attestation per probe)
32// [[feedback-hardware-agnostic-is-robustness]] (cross-arch parity
33// is one of the nine conditions)
34// [[feedback-jim-clark-racing-line-substrate]] (the racing line
35// IS the substrate's flow state)
36// [[feedback-conductor-heterogeneous-compute-no-second-class-resources]]
37// (P10 is TILT-inducing)
38// [[feedback-reclamation-doctrine-captain-moroni]] (P11 is
39// TILT-inducing)
40// [[feedback-physical-truth-over-published-docs-silicon-empirical-
41// gamification]] (autotelic = reward from empirical truth, not
42// marketing peak)
43// [[feedback-no-false-ok-substrate-honesty-audit]] (BROKEN is the
44// honest verdict when signals insufficient; never silent FLOW)
45
46// nx_safety_envelope:
47// intended_use: "aggregate flow-state classifier; composes
48// substrate's measurement layers; sealed-enum
49// verdict on system flow"
50// sil_target: SIL2
51// evidence: [kat_nine_conditions_complete,
52// kat_priority_tilt_over_broken_over_flow,
53// kat_cardinal_violations_yield_tilt]
54// hazard_register: [bug-tape-condition-count-off-by-one,
55// bug-tape-tilt-misclassified-as-flow,
56// bug-tape-broken-vs-near-flow-confusion]
57// verdict: NOT_YET_EVALUATED
58
59import "nx_syscalls.nx"
60import "nx_perf_pathology.nx"
61import "nx_etg.nx"
62
63// ===== Flow state sealed enum =====================================
64
65const NX_FLOW_STATE_NONE: i64 = 0
66const NX_FLOW_STATE_FLOW: i64 = 1 // all 8 measurable conditions met
67const NX_FLOW_STATE_NEAR_FLOW: i64 = 2 // 6-7 conditions met
68const NX_FLOW_STATE_SLIPPING: i64 = 3 // 4-5 conditions met
69const NX_FLOW_STATE_TILT: i64 = 4 // cardinal violation present
70const NX_FLOW_STATE_BROKEN: i64 = 5 // insufficient signals
71const NX_FLOW_STATE_N: i64 = 6
72
73func nx_flow_state_is_valid(s: i64) -> i64 {
74 if s < 0 { return 0 }
75 if s >= NX_FLOW_STATE_N { return 0 }
76 return 1
77}
78
79func nx_flow_state_name(s: i64) -> *u8 {
80 if s == NX_FLOW_STATE_NONE { return "NONE" }
81 if s == NX_FLOW_STATE_FLOW { return "FLOW" }
82 if s == NX_FLOW_STATE_NEAR_FLOW { return "NEAR_FLOW" }
83 if s == NX_FLOW_STATE_SLIPPING { return "SLIPPING" }
84 if s == NX_FLOW_STATE_TILT { return "TILT" }
85 if s == NX_FLOW_STATE_BROKEN { return "BROKEN" }
86 return "UNKNOWN"
87}
88
89// ===== NxFlowState struct =========================================
90//
91// Caller-allocated; nx_flow_classify reads + populates verdict.
92// Each condition is 1 (met), 0 (not met), or -1 (no signal).
93
94struct NxFlowState {
95 silicon_serial_hash: i64,
96 // Csikszentmihalyi #1 -- clear goals: reference band defined?
97 cond_clear_goals: i64,
98 // Csikszentmihalyi #2 -- immediate feedback: ETG attestation emitted?
99 cond_immediate_feedback: i64,
100 // Csikszentmihalyi #3 -- challenge-skill balance: tournament variant
101 // selected matches empirical ceiling?
102 cond_challenge_balance: i64,
103 // Csikszentmihalyi #4 -- action-awareness merger: conductor dispatch
104 // decided without intermediate translation?
105 cond_action_merged: i64,
106 // Csikszentmihalyi #5 -- loss of self-consciousness: substrate
107 // overhead < noise floor in current workload?
108 cond_overhead_invisible: i64,
109 // Csikszentmihalyi #7 -- autotelic: reward = empirical truth,
110 // not vendor-marketed? (Reclamation > 0 or all-confirmed.)
111 cond_autotelic: i64,
112 // Csikszentmihalyi #8 -- concentration on task: zero second-class
113 // resources idle (P10 absent)?
114 cond_concentration: i64,
115 // Csikszentmihalyi #9 -- sense of control: attestation chain
116 // complete for current workload + silicon?
117 cond_control: i64,
118 // Substrate-specific tenth condition (additive to Csikszentmihalyi):
119 // cross-arch parity verified (the hardware-agnostic-is-robustness
120 // cardinal).
121 cond_cross_arch_parity: i64,
122 // Substrate-cardinal violations (any one -> TILT):
123 cardinal_paywall_honored: i64, // P11
124 cardinal_second_class_idle: i64, // P10
125 cardinal_single_arch_overfit: i64, // P12
126 cardinal_latency_spike: i64, // P8 (high-tier; counts as TILT here)
127 // Output:
128 verdict: i64,
129 conditions_met: i64,
130 conditions_total: i64,
131 attestation_hash: i64,
132}
133
134// Initialize a fresh flow state with all signals at "no determination".
135func nx_flow_state_init(s: *NxFlowState, silicon_serial_hash: i64) {
136 s.silicon_serial_hash = silicon_serial_hash
137 s.cond_clear_goals = -1
138 s.cond_immediate_feedback = -1
139 s.cond_challenge_balance = -1
140 s.cond_action_merged = -1
141 s.cond_overhead_invisible = -1
142 s.cond_autotelic = -1
143 s.cond_concentration = -1
144 s.cond_control = -1
145 s.cond_cross_arch_parity = -1
146 s.cardinal_paywall_honored = 0
147 s.cardinal_second_class_idle = 0
148 s.cardinal_single_arch_overfit = 0
149 s.cardinal_latency_spike = 0
150 s.verdict = NX_FLOW_STATE_NONE
151 s.conditions_met = 0
152 s.conditions_total = 9 // 8 Csikszentmihalyi + 1 cross-arch
153 s.attestation_hash = 0
154}
155
156// ===== Classifier =================================================
157//
158// Priority order:
159// 1. TILT -- any cardinal violation
160// 2. BROKEN -- any condition unsignaled (-1)
161// 3. FLOW -- all 9 met
162// 4. NEAR_FLOW -- 7-8 met
163// 5. SLIPPING -- 4-6 met
164// 6. NONE -- 0-3 met or unfired
165
166func _flow_count_unsignaled(s: *NxFlowState) -> i64 {
167 var n: i64 = 0
168 if s.cond_clear_goals < 0 { n = n + 1 }
169 if s.cond_immediate_feedback < 0 { n = n + 1 }
170 if s.cond_challenge_balance < 0 { n = n + 1 }
171 if s.cond_action_merged < 0 { n = n + 1 }
172 if s.cond_overhead_invisible < 0 { n = n + 1 }
173 if s.cond_autotelic < 0 { n = n + 1 }
174 if s.cond_concentration < 0 { n = n + 1 }
175 if s.cond_control < 0 { n = n + 1 }
176 if s.cond_cross_arch_parity < 0 { n = n + 1 }
177 return n
178}
179
180func _flow_count_met(s: *NxFlowState) -> i64 {
181 var n: i64 = 0
182 if s.cond_clear_goals == 1 { n = n + 1 }
183 if s.cond_immediate_feedback == 1 { n = n + 1 }
184 if s.cond_challenge_balance == 1 { n = n + 1 }
185 if s.cond_action_merged == 1 { n = n + 1 }
186 if s.cond_overhead_invisible == 1 { n = n + 1 }
187 if s.cond_autotelic == 1 { n = n + 1 }
188 if s.cond_concentration == 1 { n = n + 1 }
189 if s.cond_control == 1 { n = n + 1 }
190 if s.cond_cross_arch_parity == 1 { n = n + 1 }
191 return n
192}
193
194func _flow_has_cardinal_violation(s: *NxFlowState) -> i64 {
195 if s.cardinal_paywall_honored == 1 { return 1 }
196 if s.cardinal_second_class_idle == 1 { return 1 }
197 if s.cardinal_single_arch_overfit == 1 { return 1 }
198 if s.cardinal_latency_spike == 1 { return 1 }
199 return 0
200}
201
202func nx_flow_classify(s: *NxFlowState) -> i64 {
203 // 1. TILT -- any cardinal violation (highest priority)
204 if _flow_has_cardinal_violation(s) == 1 {
205 s.verdict = NX_FLOW_STATE_TILT
206 s.conditions_met = _flow_count_met(s)
207 return NX_FLOW_STATE_TILT
208 }
209
210 // 2. BROKEN -- any condition unsignaled (substrate-honest: never
211 // claim FLOW when we don't know the truth)
212 if _flow_count_unsignaled(s) > 0 {
213 s.verdict = NX_FLOW_STATE_BROKEN
214 s.conditions_met = _flow_count_met(s)
215 return NX_FLOW_STATE_BROKEN
216 }
217
218 // 3-6. Count-based classification
219 let met: i64 = _flow_count_met(s)
220 s.conditions_met = met
221 if met == 9 {
222 s.verdict = NX_FLOW_STATE_FLOW
223 return NX_FLOW_STATE_FLOW
224 }
225 if met >= 7 {
226 s.verdict = NX_FLOW_STATE_NEAR_FLOW
227 return NX_FLOW_STATE_NEAR_FLOW
228 }
229 if met >= 4 {
230 s.verdict = NX_FLOW_STATE_SLIPPING
231 return NX_FLOW_STATE_SLIPPING
232 }
233 s.verdict = NX_FLOW_STATE_NONE
234 return NX_FLOW_STATE_NONE
235}
236
237// ===== Attestation via ETG entry ==================================
238//
239// Emits an NxEtgEntry where:
240// probe_kind = caller-supplied (e.g., NX_ETG_PROBE_CPU_ISA
241// when classifying flow during a CPU workload)
242// claim_source = NX_ETG_CLAIM_PRIOR_CALIBRATION (flow is the
243// composition of prior calibrations)
244// claim_value = conditions_total (9)
245// measurement_value = conditions_met
246// outcome = derived from verdict:
247// FLOW -> CONFIRMED
248// NEAR_FLOW -> CONFIRMED (substrate-honest: still
249// in the band)
250// SLIPPING -> INCONCLUSIVE (need more probes)
251// TILT -> FALSIFIED
252// BROKEN -> INCONCLUSIVE
253// NONE -> NONE
254
255func nx_flow_attest(
256 s: *NxFlowState,
257 entry: *NxEtgEntry,
258 probe_kind: i64,
259 selector_version: i64,
260 timestamp_q14: i64
261) -> i64 {
262 var outcome: i64 = NX_ETG_OUTCOME_NONE
263 if s.verdict == NX_FLOW_STATE_FLOW { outcome = NX_ETG_OUTCOME_CONFIRMED }
264 if s.verdict == NX_FLOW_STATE_NEAR_FLOW { outcome = NX_ETG_OUTCOME_CONFIRMED }
265 if s.verdict == NX_FLOW_STATE_SLIPPING { outcome = NX_ETG_OUTCOME_INCONCLUSIVE }
266 if s.verdict == NX_FLOW_STATE_TILT { outcome = NX_ETG_OUTCOME_FALSIFIED }
267 if s.verdict == NX_FLOW_STATE_BROKEN { outcome = NX_ETG_OUTCOME_INCONCLUSIVE }
268
269 let rc: i64 = nx_etg_entry_init(
270 entry,
271 s.silicon_serial_hash,
272 probe_kind,
273 NX_ETG_CLAIM_PRIOR_CALIBRATION,
274 s.conditions_total,
275 s.conditions_met,
276 outcome,
277 selector_version,
278 timestamp_q14
279 )
280 if rc != 0 { return rc }
281
282 s.attestation_hash = entry.attestation_hash
283 return 0
284}