nx_search_emerge.nx source
↩ module page · 93 lines · 6035 B
1// nx_search_emerge.nx -- EMERGENCE DETECTION: turn the federated frontier feed into a "where is this field
2// moving" signal. Fetches arXiv (sorted newest-first) over sovereign TLS (reuses nx_search_extern's fetch),
3// extracts the <published> year-months, and computes the RECENCY-VELOCITY (share of sampled hits from the
4// newest month) + TOTAL VOLUME (opensearch:totalResults). High velocity = activity concentrated NOW (emergent
5// 'photonic/quantum'); low + huge volume = mature ('lightbulb'). HONEST: one snapshot = velocity; TRUE
6// emergence = this signal DRIFTING UP over the daily runs (nx_cap_census check tracks that). license_tier: ORIGINAL
7import "nx_search_extern.nx"
8import "nx_syscalls.nx"
9import "nx_x509_trust_store.nx"
10import "nx_trust_store_load_from_certdata.nx"
11const EM_MAGIC_4194304: i64 = 4194304
12const EM_MAGIC_1024: i64 = 1024
13
14const EM_HOT: i64 = 600 // >=600 permille of sampled hits from the newest month = hot production
15const EM_WARM: i64 = 250
16
17func ep(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
18func en(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(28); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } while k>0 { k=k-1; sys_write(1,(((t as i64)+k) as *u8),1) } return 0 }
19
20func em_digit(c: i64) -> i64 { if c>=48 { if c<=57 { return 1 } } return 0 }
21// copy the 7-char "YYYY-MM" at pos into out (NUL-terminated)
22func em_ym7(buf: *u8, pos: i64, out: *u8) -> i64 { var i: i64=0; while i<7 { out[i]=buf[pos+i]; i=i+1 } out[7]=0 as u8; return 0 }
23// -1/0/1 lexical compare of two 7-char YYYY-MM strings (ISO dates sort lexically = chronologically)
24func em_ymcmp(a: *u8, b: *u8) -> i64 { var i: i64=0; while i<7 { let ca: i64=a[i] as i64; let cb: i64=b[i] as i64; if ca<cb { return 0-1 } if ca>cb { return 1 } i=i+1 } return 0 }
25// after each `openm` occurrence, grab the following 7-char YYYY-MM into flat[k*8]; return count (cap maxd)
26func em_collect_yms(buf: *u8, n: i64, openm: *u8, flat: *u8, maxd: i64) -> i64 {
27 var ol: i64=0; while openm[ol]!=(0 as u8) { ol=ol+1 }
28 if ol==0 { return 0 }
29 var nd: i64=0; var i: i64=0
30 while i+ol<=n {
31 if nd>=maxd { i=n } else {
32 var j: i64=0; var ok: i64=1
33 while j<ol { if buf[i+j]!=openm[j] { ok=0; j=ol } else { j=j+1 } }
34 if ok==1 { let start: i64=i+ol; if start+7<=n { em_ym7(buf, start, ((flat as i64)+nd*8) as *u8); nd=nd+1 } i=start+7 } else { i=i+1 }
35 }
36 }
37 return nd
38}
39func em_maxym(flat: *u8, nd: i64, out: *u8) -> i64 {
40 if nd<=0 { out[0]=0 as u8; return 0 }
41 var i: i64=0; while i<7 { out[i]=flat[i]; i=i+1 } out[7]=0 as u8
42 var k: i64=1; while k<nd { let p: *u8=((flat as i64)+k*8) as *u8; if em_ymcmp(p, out)>0 { var j: i64=0; while j<7 { out[j]=p[j]; j=j+1 } } k=k+1 }
43 return 0
44}
45func em_count_eq(flat: *u8, nd: i64, ref: *u8) -> i64 { var c: i64=0; var k: i64=0; while k<nd { let p: *u8=((flat as i64)+k*8) as *u8; if em_ymcmp(p,ref)==0 { c=c+1 } k=k+1 } return c }
46func em_find_after(buf: *u8, n: i64, needle: *u8) -> i64 {
47 var nn: i64=0; while needle[nn]!=(0 as u8) { nn=nn+1 }
48 if nn==0 { return 0-1 }
49 var i: i64=0
50 while i+nn<=n { var j: i64=0; var ok: i64=1; while j<nn { if buf[i+j]!=needle[j] { ok=0; j=nn } else { j=j+1 } } if ok==1 { return i+nn } i=i+1 }
51 return 0-1
52}
53// parse the first integer within 24 bytes after `key` (handles `>NNN<` and `": NNN`)
54func em_int_after(buf: *u8, n: i64, key: *u8) -> i64 {
55 let p: i64=em_find_after(buf,n,key); if p<0 { return 0-1 }
56 var lim: i64=p+24; if lim>n { lim=n }
57 var ds: i64=0-1; var i: i64=p
58 while i<lim { if ds<0 { if em_digit(buf[i] as i64)==1 { ds=i } } i=i+1 }
59 if ds<0 { return 0-1 }
60 var v: i64=0; var j: i64=ds
61 while j<n { if em_digit(buf[j] as i64)==1 { v=v*10+((buf[j] as i64)-48); j=j+1 } else { j=n } }
62 return v
63}
64func em_label(share: i64) -> *u8 {
65 if share>=EM_HOT { return "EMERGING/HOT -- activity concentrated in the newest month (a 'photonic/quantum'-type frontier)" as *u8 }
66 if share>=EM_WARM { return "ACTIVE -- steady recent activity" as *u8 }
67 return "MATURE/SLOW -- activity spread over time (a 'lightbulb'-type established field)" as *u8
68}
69
70func main(argc: i64, argv: *i64) -> i64 {
71 ep("=== nx_search_emerge: recency-velocity of the federated frontier (where is this field moving?) ===\n" as *u8)
72 let r: i64=nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, EM_MAGIC_4194304)
73 if r<=0 { ep(" trust store load FAILED\n" as *u8); sys_exit(1); return 1 }
74 let store: *TrustStore=r as *TrustStore
75 let q: *u8=sys_mmap(512); let qn: i64=se_join(argc, argv, q)
76 ep(" query: "); sys_write(1, q, qn); ep("\n" as *u8)
77 let cap: i64=EM_MAGIC_4194304; let out: *u8=sys_mmap(cap)
78 let au: *u8=sys_mmap(EM_MAGIC_1024); var ao: i64=0
79 ao=se_app(au,ao,"https://export.arxiv.org/api/query?search_query=all:" as *u8); ao=se_app(au,ao,q)
80 ao=se_app(au,ao,"&start=0&max_results=12&sortBy=submittedDate&sortOrder=descending" as *u8); au[ao]=0 as u8
81 let an: i64=se_fetch(store, au, out, cap, "arXiv" as *u8)
82 if an<=0 { ep(" no data (fetch failed)\n" as *u8); sys_exit(1); return 1 }
83 let flat: *u8=sys_mmap(12*8); let nd: i64=em_collect_yms(out, an, "<published>" as *u8, flat, 12)
84 let newest: *u8=sys_mmap(8); em_maxym(flat, nd, newest)
85 let recent: i64=em_count_eq(flat, nd, newest)
86 var share: i64=0; if nd>0 { share=recent*1000/nd }
87 let total: i64=em_int_after(out, an, "opensearch:totalResults>" as *u8)
88 ep(" sampled hits="); en(nd); ep(" newest month="); ep(newest)
89 ep(" recent-month share="); en(share); ep("permille total volume="); en(total); ep("\n" as *u8)
90 ep(" VELOCITY: "); ep(em_label(share)); ep("\n" as *u8)
91 ep(" (single snapshot = velocity; TRUE emergence = this share DRIFTING UP over daily runs -> nx_cap_census check)\n" as *u8)
92 sys_exit(0); return 0
93}