nx_competitive_intel.nx source
↩ module page · 211 lines · 7350 B
1// nx_competitive_intel.nx -- continuous competitive-intelligence ledger.
2//
3// Cardinal: feedback-always-racing-out-compete-by-default.
4//
5// The substrate's standing job: track every named incumbent on every
6// competition track + record honest delta-to-incumbent so the roadmap
7// is driven by real position, not aspiration.
8//
9// Per-record schema (64 bytes / 8 i64):
10// [0] incumbent_tag (FNV-1a hash of incumbent name)
11// [1] track_tag (which competition axis)
12// [2] their_version_hash (snapshot of public version surveyed)
13// [3] their_strength_q10 (substrate's read of their position, 0..1024)
14// [4] our_gap_q10 (substrate position vs theirs)
15// [5] our_plan_sessions (sized arc to close gap)
16// [6] last_surveyed_iso (epoch-day since 2026-01-01)
17// [7] next_survey_due_iso (epoch-day; default last + 90)
18
19import "syscalls.nx"
20const NX_MAGIC_1024: i64 = 1024
21
22static NX_CINT_ACTIVE: i64
23static NX_CINT_LEDGER_PTR: i64
24static NX_CINT_N_RECORDS: i64
25static NX_CINT_CAPACITY: i64
26
27const NX_CINT_REC_SIZE: i64 = 64
28const NX_CINT_CHUNK_BYTES: i64 = 32768
29
30func nx_cint_init() -> i64 {
31 if NX_CINT_LEDGER_PTR != 0 { return 0 }
32 let raw: *u8 = sys_mmap(NX_CINT_CHUNK_BYTES)
33 if (raw as i64) == 0 { return 1 }
34 if (raw as i64) == 0 - 1 { return 2 }
35 NX_CINT_LEDGER_PTR = raw as i64
36 NX_CINT_N_RECORDS = 0
37 NX_CINT_CAPACITY = NX_CINT_CHUNK_BYTES / NX_CINT_REC_SIZE
38 NX_CINT_ACTIVE = 1
39 return 0
40}
41
42func nx_cint_enable() -> i64 { NX_CINT_ACTIVE = 1; return 0 }
43func nx_cint_disable() -> i64 { NX_CINT_ACTIVE = 0; return 0 }
44func nx_cint_is_active() -> i64 { return NX_CINT_ACTIVE }
45
46// Append one survey row. Caller has just finished surveying the
47// incumbent's public state (their bug tracker / RFCs / docs).
48// Returns 0 OK.
49func nx_cint_record(incumbent_tag: i64, track_tag: i64,
50 their_version_hash: i64, their_strength_q10: i64,
51 our_gap_q10: i64, our_plan_sessions: i64,
52 last_surveyed_day: i64) -> i64 {
53 if NX_CINT_ACTIVE == 0 { return 0 }
54 nx_cint_init()
55 if NX_CINT_LEDGER_PTR == 0 { return 0 }
56 if NX_CINT_N_RECORDS >= NX_CINT_CAPACITY { return 3 }
57
58 let base: i64 = NX_CINT_LEDGER_PTR + (NX_CINT_N_RECORDS * NX_CINT_REC_SIZE)
59 let rec: *i64 = base as *i64
60 rec[0] = incumbent_tag
61 rec[1] = track_tag
62 rec[2] = their_version_hash
63 rec[3] = their_strength_q10
64 rec[4] = our_gap_q10
65 rec[5] = our_plan_sessions
66 rec[6] = last_surveyed_day
67 rec[7] = last_surveyed_day + 90 // default 90-day resurvey cadence
68 NX_CINT_N_RECORDS = NX_CINT_N_RECORDS + 1
69 return 0
70}
71
72// Override the next-survey-due date (e.g. urgent re-survey).
73func nx_cint_set_next_due(slot: i64, due_day: i64) -> i64 {
74 nx_cint_init()
75 if slot < 0 { return 1 }
76 if slot >= NX_CINT_N_RECORDS { return 2 }
77 let base: i64 = NX_CINT_LEDGER_PTR + (slot * NX_CINT_REC_SIZE)
78 let rec: *i64 = base as *i64
79 rec[7] = due_day
80 return 0
81}
82
83// Find the most-recent slot for (incumbent, track). Returns slot
84// index or -1 if not yet tracked.
85func nx_cint_lookup(incumbent_tag: i64, track_tag: i64) -> i64 {
86 nx_cint_init()
87 var i: i64 = NX_CINT_N_RECORDS - 1
88 while i >= 0 {
89 let base: i64 = NX_CINT_LEDGER_PTR + (i * NX_CINT_REC_SIZE)
90 let rec: *i64 = base as *i64
91 if rec[0] == incumbent_tag {
92 if rec[1] == track_tag { return i }
93 }
94 i = i - 1
95 }
96 return 0 - 1
97}
98
99// Read field from a slot. field is 0..7.
100func nx_cint_field(slot: i64, field: i64) -> i64 {
101 nx_cint_init()
102 if slot < 0 { return 0 - 1 }
103 if slot >= NX_CINT_N_RECORDS { return 0 - 1 }
104 if field < 0 { return 0 - 1 }
105 if field > 7 { return 0 - 1 }
106 let base: i64 = NX_CINT_LEDGER_PTR + (slot * NX_CINT_REC_SIZE)
107 let rec: *i64 = base as *i64
108 return rec[field]
109}
110
111// Count rows whose next_survey_due_day <= now_day.
112func nx_cint_due_for_survey(now_day: i64) -> i64 {
113 nx_cint_init()
114 var n: i64 = 0
115 var i: i64 = 0
116 while i < NX_CINT_N_RECORDS {
117 let base: i64 = NX_CINT_LEDGER_PTR + (i * NX_CINT_REC_SIZE)
118 let rec: *i64 = base as *i64
119 if rec[7] <= now_day { n = n + 1 }
120 i = i + 1
121 }
122 return n
123}
124
125// Find the slot with the LARGEST gap (most-behind axis). Returns
126// slot index or -1 if no records.
127func nx_cint_largest_gap() -> i64 {
128 nx_cint_init()
129 if NX_CINT_N_RECORDS == 0 { return 0 - 1 }
130 var best: i64 = 0
131 var best_gap: i64 = 0
132 var i: i64 = 0
133 while i < NX_CINT_N_RECORDS {
134 let base: i64 = NX_CINT_LEDGER_PTR + (i * NX_CINT_REC_SIZE)
135 let rec: *i64 = base as *i64
136 if rec[4] > best_gap {
137 best_gap = rec[4]
138 best = i
139 }
140 i = i + 1
141 }
142 return best
143}
144
145func nx_cint_n_records() -> i64 {
146 nx_cint_init()
147 return NX_CINT_N_RECORDS
148}
149
150// ---- self-test ---------------------------------------------------
151// expect_exit: 0
152
153func main() -> i64 {
154 if nx_cint_init() != 0 { return 1 }
155
156 // Disabled-mode short-circuit.
157 nx_cint_disable()
158 nx_cint_record(1, 1, 0, NX_MAGIC_1024, 0, 0, 100)
159 if nx_cint_n_records() != 0 { return 10 }
160
161 // Enable + record 4 named-incumbent surveys.
162 nx_cint_enable()
163
164 // eBPF on JIT-probes axis: their strength 1024 (mature), our gap
165 // 900 (we're ~12% of the way), 30 sessions to close.
166 if nx_cint_record(0xEBBF1111, 0x71C3001, 8, NX_MAGIC_1024, 900, 30, 142) != 0 { return 20 }
167
168 // OpenTelemetry on ecosystem axis: their strength 1024 (11 SDKs),
169 // our gap 950 (1 SDK), 60 sessions.
170 if nx_cint_record(0xCABA2222, 0x71C3002, 1, NX_MAGIC_1024, 950, 60, 142) != 0 { return 21 }
171
172 // Honeycomb on high-cardinality query: gap 850, 25 sessions.
173 if nx_cint_record(0xBEE13333, 0x71C3003, 5, NX_MAGIC_1024, 850, 25, 142) != 0 { return 22 }
174
175 // libpng on PNG decoding: smaller gap 300, 4 sessions.
176 if nx_cint_record(0x10AD4444, 0x71C3004, 16, NX_MAGIC_1024, 300, 4, 142) != 0 { return 23 }
177
178 if nx_cint_n_records() != 4 { return 30 }
179
180 // Lookup OpenTelemetry on ecosystem track.
181 let slot: i64 = nx_cint_lookup(0xCABA2222, 0x71C3002)
182 if slot < 0 { return 40 }
183 if nx_cint_field(slot, 4) != 950 { return 41 } // our_gap
184 if nx_cint_field(slot, 5) != 60 { return 42 } // sessions
185
186 // Largest-gap slot must be OpenTelemetry (950 > others).
187 let worst: i64 = nx_cint_largest_gap()
188 if worst < 0 { return 50 }
189 if nx_cint_field(worst, 0) != 0xCABA2222 { return 51 }
190 if nx_cint_field(worst, 4) != 950 { return 52 }
191
192 // Due-for-survey at day 142+90 = 232 (just before resurvey cadence)
193 // should count 0 due; at day 233 should count all 4.
194 if nx_cint_due_for_survey(231) != 0 { return 60 }
195 if nx_cint_due_for_survey(232) != 4 { return 61 }
196 if nx_cint_due_for_survey(300) != 4 { return 62 }
197
198 // Override one row's due date.
199 if nx_cint_set_next_due(0, 999) != 0 { return 70 }
200 if nx_cint_due_for_survey(232) != 3 { return 71 } // one moved out
201
202 // Unknown lookup returns -1.
203 if nx_cint_lookup(0xDEAD9999, 0xFEED) != 0 - 1 { return 80 }
204
205 // Disabled-mode re-test.
206 nx_cint_disable()
207 nx_cint_record(0xDEAD, 0xFEED, 0, 0, 0, 0, 0)
208 if nx_cint_n_records() != 4 { return 90 }
209
210 return 0
211}