interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / ng / memo / memo_policy.om

920 lines · 17958 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/memo/memo_policy.om: Profitability policy, disk-floor alarm, and explicit oldest-first
3note memo_prune for Stage 1 OMEGA-MEMO (Sovereign Intelligence 2.0 Pillar 2).
4note
5note Pure seeded OM-2-compatible library unit (no om2 header, no fn main; prefix mp_ / MP_).
6note Zero C/Python/Rust. Zero arbitrary hard entry cap on disk: walks sharded <root>/<2hex>/*.ome
7note directly via Linux AArch64 fopen/getdnts/stat/unlink.
8
9note Profitability decisions
10const MP_DEC_ADMIT ulong 1
11const MP_DEC_SKIP_TRIVIAL ulong 2
12const MP_DEC_FORCE_ADMIT ulong 3
13const MP_DEC_DISABLED ulong 4
14
15note Policy override modes
16const MP_OVR_AUTO ulong 0
17const MP_OVR_FORCE ulong 1
18const MP_OVR_DISABLE ulong 2
19
20note Disk-floor check status codes
21const MP_DISK_OK ulong 0
22const MP_ALARM_DISK_FLOOR ulong 1
23const MP_DISK_ERR_IO ulong 2
24
25note Default policy thresholds
26const MP_DEF_MIN_COMPUTE_NS ulong 5000
27const MP_DEF_RATIO_NUM ulong 2
28const MP_DEF_RATIO_DEN ulong 1
29const MP_DEF_WARMUP ulong 1
30const MP_DEF_MAX_STORE_BYTES ulong 1073741824
31const MP_DEF_FREE_FLOOR_BYTES ulong 67108864
32const MP_DEF_FS_CAP_BYTES ulong 2147483648
33
34note In-memory per-SEMANTIC_ID profitability telemetry cache (LRU-recycled; NEVER caps disk entries)
35const MP_PROF_SLOTS ulong 256
36const MP_PRUNE_WIN ulong 16
37const MP_PATH_STRIDE ulong 4096
38
39note Linux ARM64 O_RDONLY | O_DIRECTORY | O_NOFOLLOW = 0 | 16384 | 32768 = 49152
40const MP_O_DIR int 49152
41
42note Policy config:
43note [0]=override [1]=min_compute_ns [2]=ratio_num [3]=ratio_den [4]=warmup
44note [5]=max_store_bytes [6]=free_floor_bytes [7]=fs_cap_bytes [8]=has_ext_free
45note [9]=ext_free_bytes [10]=initialized [11]=alarm_silent
46global mp_cfg ulong 16
47
48note Scan, alarm, prune, and profitability telemetry:
49note [0]=total_entries [1]=total_store_bytes [2]=computed_free_bytes [3]=alarm_active
50note [4]=alarm_count [5]=prune_removed_entries [6]=prune_reclaimed_bytes
51note [7]=total_skips [8]=total_admits [9]=total_force_admits
52global mp_stat ulong 16
53
54note Structured alarm buffer (always populated on alarm; also written to stderr unless silent)
55global mp_alarm_buf uchar 1024
56global mp_alarm_len ulong 1
57
58note Per-SEMANTIC_ID profitability profiler tables
59global mp_p_used uchar 256
60global mp_p_sid uchar 8192
61global mp_p_calls ulong 256
62global mp_p_cns ulong 256
63global mp_p_ons ulong 256
64global mp_p_skips ulong 256
65global mp_p_hits ulong 256
66global mp_p_last ulong 256
67global mp_tick ulong 1
68global mp_q_sid uchar 32
69
70note Store root & streaming directory walk scratch buffers
71global mp_root uchar 4096
72global mp_root_len ulong 1
73global mp_dir_path uchar 4096
74global mp_dir_plen ulong 1
75global mp_file_path uchar 4096
76global mp_file_plen ulong 1
77global mp_dents uchar 65536
78global mp_sdents uchar 65536
79global mp_stbuf uchar 128
80global mp_ndig uchar 24
81
82note Streaming oldest-K candidate window for explicit memo_prune (no cap on total store size)
83global mp_cw_used uchar 16
84global mp_cw_sec ulong 16
85global mp_cw_nsec ulong 16
86global mp_cw_size ulong 16
87global mp_cw_plen ulong 16
88global mp_cw_path uchar 65536
89global mp_cw_cnt ulong 1
90global mp_cw_ord ulong 16
91
92text mp_al_pfx "OMEGA-MEMO ALARM disk_floor root="
93text mp_al_ent " entries="
94text mp_al_stb " store_bytes="
95text mp_al_max " max_bytes="
96text mp_al_fre " free_bytes="
97text mp_al_flr " floor_bytes="
98text mp_al_act " action=EXPLICIT_PRUNE_REQUIRED\n"
99
100note ---- Initialization & Configuration ----
101
102fn mp_init to void
103put mp_cfg 0 MP_OVR_AUTO
104put mp_cfg 1 MP_DEF_MIN_COMPUTE_NS
105put mp_cfg 2 MP_DEF_RATIO_NUM
106put mp_cfg 3 MP_DEF_RATIO_DEN
107put mp_cfg 4 MP_DEF_WARMUP
108put mp_cfg 5 MP_DEF_MAX_STORE_BYTES
109put mp_cfg 6 MP_DEF_FREE_FLOOR_BYTES
110put mp_cfg 7 MP_DEF_FS_CAP_BYTES
111put mp_cfg 8 0
112put mp_cfg 9 0
113put mp_cfg 10 1
114put mp_cfg 11 0
115for i ulong 16
116put mp_stat i 0
117end
118put mp_alarm_len 0 0
119put mp_tick 0 0
120for s ulong 256
121put mp_p_used s 0
122put mp_p_calls s 0
123put mp_p_cns s 0
124put mp_p_ons s 0
125put mp_p_skips s 0
126put mp_p_hits s 0
127put mp_p_last s 0
128end
129end
130
131fn mp_ensure_init to void
132if eq get mp_cfg 10 0
133mp_init
134end
135end
136
137fn mp_set_override ovr ulong to void
138mp_ensure_init
139put mp_cfg 0 ovr
140end
141
142fn mp_set_thresholds min_cns ulong rnum ulong rden ulong to void
143mp_ensure_init
144put mp_cfg 1 min_cns
145put mp_cfg 2 rnum
146if eq rden 0
147put mp_cfg 3 1
148else
149put mp_cfg 3 rden
150end
151end
152
153fn mp_set_disk_limits max_bytes ulong floor_bytes ulong cap_bytes ulong to void
154mp_ensure_init
155put mp_cfg 5 max_bytes
156put mp_cfg 6 floor_bytes
157put mp_cfg 7 cap_bytes
158end
159
160fn mp_set_ext_free enabled ulong free_bytes ulong to void
161mp_ensure_init
162put mp_cfg 8 enabled
163put mp_cfg 9 free_bytes
164end
165
166fn mp_set_alarm_silent silent ulong to void
167mp_ensure_init
168put mp_cfg 11 silent
169end
170
171note ---- Per-SEMANTIC_ID Profitability Policy ----
172
173fn mp_sid_match slot ulong to bool
174if eq get mp_p_used slot 0
175ret false
176end
177base = mul slot 32
178for i ulong 32
179if ne get mp_p_sid add base i get mp_q_sid i
180ret false
181end
182end
183ret true
184end
185
186fn mp_find_prof to ulong
187for s ulong 256
188if mp_sid_match s
189ret s
190end
191end
192ret 999999
193end
194
195fn mp_alloc_prof to ulong
196for s ulong 256
197if eq get mp_p_used s 0
198ret s
199end
200end
201best = ulong 0
202btick = get mp_p_last 0
203for s ulong 256
204tk = get mp_p_last s
205if lt tk btick
206btick = tk
207best = s
208end
209end
210ret best
211end
212
213fn mp_get_or_create_prof to ulong
214ex = mp_find_prof
215if ne ex 999999
216ret ex
217end
218sl = mp_alloc_prof
219put mp_p_used sl 1
220base = mul sl 32
221for i ulong 32
222put mp_p_sid add base i get mp_q_sid i
223end
224put mp_p_calls sl 0
225put mp_p_cns sl 0
226put mp_p_ons sl 0
227put mp_p_skips sl 0
228put mp_p_hits sl 0
229put mp_p_last sl 0
230ret sl
231end
232
233fn mp_is_profitable avg_cns ulong avg_ons ulong to bool
234min_c = get mp_cfg 1
235if lt avg_cns min_c
236ret false
237end
238if eq avg_ons 0
239ret true
240end
241rnum = get mp_cfg 2
242rden = get mp_cfg 3
243if eq rden 0
244rden = ulong 1
245end
246lhs = mul avg_cns rden
247rhs = mul avg_ons rnum
248ret ge lhs rhs
249end
250
251fn mp_precheck to ulong
252mp_ensure_init
253ovr = get mp_cfg 0
254if eq ovr MP_OVR_FORCE
255ret MP_DEC_FORCE_ADMIT
256end
257if eq ovr MP_OVR_DISABLE
258ret MP_DEC_DISABLED
259end
260sl = mp_find_prof
261if eq sl 999999
262ret MP_DEC_ADMIT
263end
264calls = get mp_p_calls sl
265if lt calls get mp_cfg 4
266ret MP_DEC_ADMIT
267end
268avg_c = div get mp_p_cns sl calls
269avg_o = div get mp_p_ons sl calls
270if not mp_is_profitable avg_c avg_o
271put mp_p_skips sl add get mp_p_skips sl 1
272put mp_stat 7 add get mp_stat 7 1
273ret MP_DEC_SKIP_TRIVIAL
274end
275ret MP_DEC_ADMIT
276end
277
278fn mp_should_memoize cns ulong ons ulong to ulong
279mp_ensure_init
280sl = mp_get_or_create_prof
281tk = add get mp_tick 0 1
282put mp_tick 0 tk
283put mp_p_last sl tk
284calls = add get mp_p_calls sl 1
285tot_c = add get mp_p_cns sl cns
286tot_o = add get mp_p_ons sl ons
287put mp_p_calls sl calls
288put mp_p_cns sl tot_c
289put mp_p_ons sl tot_o
290ovr = get mp_cfg 0
291if eq ovr MP_OVR_FORCE
292put mp_stat 9 add get mp_stat 9 1
293ret MP_DEC_FORCE_ADMIT
294end
295if eq ovr MP_OVR_DISABLE
296put mp_p_skips sl add get mp_p_skips sl 1
297put mp_stat 7 add get mp_stat 7 1
298ret MP_DEC_DISABLED
299end
300avg_c = div tot_c calls
301avg_o = div tot_o calls
302if not mp_is_profitable avg_c avg_o
303put mp_p_skips sl add get mp_p_skips sl 1
304put mp_stat 7 add get mp_stat 7 1
305ret MP_DEC_SKIP_TRIVIAL
306end
307put mp_stat 8 add get mp_stat 8 1
308ret MP_DEC_ADMIT
309end
310
311fn mp_record_hit to void
312mp_ensure_init
313sl = mp_get_or_create_prof
314tk = add get mp_tick 0 1
315put mp_tick 0 tk
316put mp_p_last sl tk
317put mp_p_hits sl add get mp_p_hits sl 1
318end
319
320note ---- Alarm Line Formatting ----
321
322fn mp_al_push_b ch uchar to void
323p = get mp_alarm_len 0
324if lt p 1020
325put mp_alarm_buf p ch
326put mp_alarm_len 0 add p 1
327end
328end
329
330fn mp_al_push_tx id ulong to void
331if eq id 0
332for i ulong len mp_al_pfx bound 64
333mp_al_push_b get mp_al_pfx i
334end
335ret
336end
337if eq id 1
338for i ulong len mp_al_ent bound 32
339mp_al_push_b get mp_al_ent i
340end
341ret
342end
343if eq id 2
344for i ulong len mp_al_stb bound 32
345mp_al_push_b get mp_al_stb i
346end
347ret
348end
349if eq id 3
350for i ulong len mp_al_max bound 32
351mp_al_push_b get mp_al_max i
352end
353ret
354end
355if eq id 4
356for i ulong len mp_al_fre bound 32
357mp_al_push_b get mp_al_fre i
358end
359ret
360end
361if eq id 5
362for i ulong len mp_al_flr bound 32
363mp_al_push_b get mp_al_flr i
364end
365ret
366end
367for i ulong len mp_al_act bound 64
368mp_al_push_b get mp_al_act i
369end
370end
371
372fn mp_al_push_num v ulong to void
373x = v
374p = ulong 24
375for i ulong 21
376p = sub p 1
377put mp_ndig p uchar add 48 rem x 10
378x = div x 10
379if eq x 0
380break
381end
382end
383cnt = sub 24 p
384for j ulong cnt bound 24
385mp_al_push_b get mp_ndig add p j
386end
387end
388
389fn mp_emit_alarm entries ulong sbytes ulong maxb ulong freeb ulong flrb ulong to void
390put mp_alarm_len 0 0
391mp_al_push_tx 0
392rlen = get mp_root_len 0
393if gt rlen 256
394rlen = ulong 256
395end
396for i ulong rlen bound 256
397mp_al_push_b get mp_root i
398end
399mp_al_push_tx 1
400mp_al_push_num entries
401mp_al_push_tx 2
402mp_al_push_num sbytes
403mp_al_push_tx 3
404mp_al_push_num maxb
405mp_al_push_tx 4
406mp_al_push_num freeb
407mp_al_push_tx 5
408mp_al_push_num flrb
409mp_al_push_tx 6
410if eq get mp_cfg 11 0
411w = write 2 mp_alarm_buf 0 get mp_alarm_len 0
412end
413end
414
415note ---- ARM64 struct stat & getdents64 Helpers ----
416
417fn mp_st_u64 off ulong to ulong
418v = ulong 0
419for j ulong 8
420b = ulong get mp_stbuf add off j
421v = or v shl b mul j 8
422end
423ret v
424end
425
426fn mp_is_lhex ch uchar to bool
427if and ge ch 48 le ch 57
428ret true
429end
430ret and ge ch 97 le ch 102
431end
432
433note Check whether mp_dents[off..] has a 2-lowercase-hex shard name
434fn mp_dent_is_hex2 off ulong lim ulong to bool
435p = add off 19
436if gt add p 3 lim
437ret false
438end
439c0 = get mp_dents p
440c1 = get mp_dents add p 1
441c2 = get mp_dents add p 2
442if ne c2 0
443ret false
444end
445if not mp_is_lhex c0
446ret false
447end
448ret mp_is_lhex c1
449end
450
451note Return name length of sdent at off (0 if not ending with ".ome" or too long)
452fn mp_sdent_ome_len off ulong lim ulong to ulong
453p = add off 19
454n = ulong 0
455while lt add p n lim bound 512
456ch = get mp_sdents add p n
457if eq ch 0
458break
459end
460n = add n 1
461end
462if lt n 5
463ret 0
464end
465e0 = get mp_sdents add p sub n 4
466e1 = get mp_sdents add p sub n 3
467e2 = get mp_sdents add p sub n 2
468e3 = get mp_sdents add p sub n 1
469if or ne e0 46 ne e1 111
470ret 0
471end
472if or ne e2 109 ne e3 101
473ret 0
474end
475ret n
476end
477
478note Build mp_dir_path = <mp_root>/<c0><c1>
479fn mp_build_shard_dir c0 uchar c1 uchar to bool
480rl = get mp_root_len 0
481if or eq rl 0 gt add rl 4 4090
482ret false
483end
484for i ulong rl bound 4090
485put mp_dir_path i get mp_root i
486end
487put mp_dir_path rl 47
488put mp_dir_path add rl 1 c0
489put mp_dir_path add rl 2 c1
490put mp_dir_plen 0 add rl 3
491ret true
492end
493
494note Build mp_file_path = <mp_dir_path>/<mp_sdents[off+19 .. +nlen)>
495fn mp_build_file_path off ulong nlen ulong to bool
496dl = get mp_dir_plen 0
497if gt add add dl 1 nlen 4094
498ret false
499end
500for i ulong dl bound 4094
501put mp_file_path i get mp_dir_path i
502end
503put mp_file_path dl 47
504base = add off 19
505for j ulong nlen bound 512
506put mp_file_path add add dl 1 j get mp_sdents add base j
507end
508put mp_file_plen 0 add add dl 1 nlen
509ret true
510end
511
512note ---- Oldest-K Candidate Window Helpers for Uncapped Streaming memo_prune ----
513
514fn mp_cw_reset to void
515put mp_cw_cnt 0 0
516for i ulong 16
517put mp_cw_used i 0
518put mp_cw_sec i 0
519put mp_cw_nsec i 0
520put mp_cw_size i 0
521put mp_cw_plen i 0
522end
523end
524
525note Compare mp_file_path against candidate slot s lexicographically (<0 if file_path < slot)
526fn mp_cmp_path_slot s ulong to int
527pl1 = get mp_file_plen 0
528pl2 = get mp_cw_plen s
529m = pl1
530if lt pl2 m
531m = pl2
532end
533base = mul s MP_PATH_STRIDE
534for i ulong m bound 4096
535b1 = get mp_file_path i
536b2 = get mp_cw_path add base i
537if lt b1 b2
538ret neg int 1
539end
540if gt b1 b2
541ret int 1
542end
543end
544if lt pl1 pl2
545ret neg int 1
546end
547if gt pl1 pl2
548ret int 1
549end
550ret int 0
551end
552
553note Compare two candidate slot paths lexicographically: true if slot a < slot b
554fn mp_slot_path_lt a ulong b ulong to bool
555la = get mp_cw_plen a
556lb = get mp_cw_plen b
557m = la
558if lt lb m
559m = lb
560end
561ba = mul a MP_PATH_STRIDE
562bb = mul b MP_PATH_STRIDE
563for i ulong m bound 4096
564ca = get mp_cw_path add ba i
565cb = get mp_cw_path add bb i
566if lt ca cb
567ret true
568end
569if gt ca cb
570ret false
571end
572end
573ret lt la lb
574end
575
576note Compare two candidate slots a and b: true if slot a is older than slot b
577fn mp_slot_older a ulong b ulong to bool
578x = get mp_cw_sec a
579y = get mp_cw_sec b
580if lt x y
581ret true
582end
583if gt x y
584ret false
585end
586x = get mp_cw_nsec a
587y = get mp_cw_nsec b
588if lt x y
589ret true
590end
591if gt x y
592ret false
593end
594ret mp_slot_path_lt a b
595end
596
597note True if current mp_file_path with (sec, nsec) is strictly older than slot s
598fn mp_cur_older_than_slot sec ulong nsec ulong s ulong to bool
599ss = get mp_cw_sec s
600if lt sec ss
601ret true
602end
603if gt sec ss
604ret false
605end
606sn = get mp_cw_nsec s
607if lt nsec sn
608ret true
609end
610if gt nsec sn
611ret false
612end
613ret lt mp_cmp_path_slot s 0
614end
615
616fn mp_cw_store_cur s ulong sec ulong nsec ulong sz ulong to void
617put mp_cw_used s 1
618put mp_cw_sec s sec
619put mp_cw_nsec s nsec
620put mp_cw_size s sz
621pl = get mp_file_plen 0
622put mp_cw_plen s pl
623base = mul s MP_PATH_STRIDE
624for i ulong pl bound 4096
625put mp_cw_path add base i get mp_file_path i
626end
627end
628
629fn mp_cw_consider sec ulong nsec ulong sz ulong to void
630cnt = get mp_cw_cnt 0
631if lt cnt MP_PRUNE_WIN
632mp_cw_store_cur cnt sec nsec sz
633put mp_cw_cnt 0 add cnt 1
634ret
635end
636newest = ulong 0
637for s ulong 16
638if mp_slot_older newest s
639newest = s
640end
641end
642if mp_cur_older_than_slot sec nsec newest
643mp_cw_store_cur newest sec nsec sz
644end
645end
646
647note ---- Uncapped Store Scanner & Disk-Floor Alarm ----
648
649fn mp_scan_one_ome o ulong lim ulong collect_prune bool to void
650nlen = mp_sdent_ome_len o lim
651if eq nlen 0
652ret
653end
654if not mp_build_file_path o nlen
655ret
656end
657sr = stat mp_file_path 0 get mp_file_plen 0 mp_stbuf 0 128
658if ne sr 0
659ret
660end
661sz = mp_st_u64 48
662put mp_stat 0 add get mp_stat 0 1
663put mp_stat 1 add get mp_stat 1 sz
664if collect_prune
665mp_cw_consider mp_st_u64 88 mp_st_u64 96 sz
666end
667end
668
669fn mp_sdent_recl o ulong to ulong
670b0 = ulong get mp_sdents add o 16
671b1 = shl ulong get mp_sdents add o 17 8
672ret or b0 b1
673end
674
675fn mp_dent_recl o ulong to ulong
676b0 = ulong get mp_dents add o 16
677b1 = shl ulong get mp_dents add o 17 8
678ret or b0 b1
679end
680
681fn mp_scan_shard_buf lim ulong collect_prune bool to void
682o = ulong 0
683while lt o lim bound 1000000
684rl = mp_sdent_recl o
685if eq rl 0
686break
687end
688mp_scan_one_ome o lim collect_prune
689o = add o rl
690end
691end
692
693fn mp_scan_shard collect_prune bool to void
694dfd = fopen mp_dir_path 0 get mp_dir_plen 0 MP_O_DIR 0
695if lt dfd 0
696ret
697end
698going = true
699while going bound 1000000
700nr = getdnts int dfd mp_sdents 0 65536
701if le nr 0
702going = false
703else
704mp_scan_shard_buf ulong nr collect_prune
705end
706end
707rc = fclose int dfd
708end
709
710fn mp_walk_one_dent o ulong lim ulong collect_prune bool to void
711if not mp_dent_is_hex2 o lim
712ret
713end
714c0 = get mp_dents add o 19
715c1 = get mp_dents add o 20
716if mp_build_shard_dir c0 c1
717mp_scan_shard collect_prune
718end
719end
720
721fn mp_walk_buf lim ulong collect_prune bool to void
722o = ulong 0
723while lt o lim bound 100000
724recl = mp_dent_recl o
725if eq recl 0
726break
727end
728mp_walk_one_dent o lim collect_prune
729o = add o recl
730end
731end
732
733fn mp_walk_store collect_prune bool to ulong
734mp_ensure_init
735put mp_stat 0 0
736put mp_stat 1 0
737if collect_prune
738mp_cw_reset
739end
740rl = get mp_root_len 0
741if eq rl 0
742ret MP_DISK_ERR_IO
743end
744rfd = fopen mp_root 0 rl MP_O_DIR 0
745if lt rfd 0
746if eq rfd neg long 2
747ret MP_DISK_OK
748end
749ret MP_DISK_ERR_IO
750end
751going = true
752while going bound 100000
753nr = getdnts int rfd mp_dents 0 65536
754if le nr 0
755going = false
756else
757mp_walk_buf ulong nr collect_prune
758end
759end
760rc = fclose int rfd
761ret MP_DISK_OK
762end
763
764fn mp_scan_store to ulong
765ret mp_walk_store false
766end
767
768fn mp_compute_free sbytes ulong to ulong
769if ne get mp_cfg 8 0
770ret get mp_cfg 9
771end
772cap = get mp_cfg 7
773if ge sbytes cap
774ret 0
775end
776ret sub cap sbytes
777end
778
779fn mp_check_disk_floor to ulong
780mp_ensure_init
781wrc = mp_scan_store
782if ne wrc MP_DISK_OK
783ret wrc
784end
785ent = get mp_stat 0
786sbytes = get mp_stat 1
787freeb = mp_compute_free sbytes
788put mp_stat 2 freeb
789maxb = get mp_cfg 5
790flrb = get mp_cfg 6
791breach = false
792if and gt maxb 0 gt sbytes maxb
793breach = true
794end
795if and gt flrb 0 lt freeb flrb
796breach = true
797end
798if breach
799put mp_stat 3 1
800put mp_stat 4 add get mp_stat 4 1
801mp_emit_alarm ent sbytes maxb freeb flrb
802ret MP_ALARM_DISK_FLOOR
803end
804put mp_stat 3 0
805ret MP_DISK_OK
806end
807
808fn mp_cw_find_oldest start ulong cnt ulong to ulong
809best = start
810j = add start 1
811while lt j cnt bound 16
812if mp_slot_older get mp_cw_ord j get mp_cw_ord best
813best = j
814end
815j = add j 1
816end
817ret best
818end
819
820note Sort mp_cw_ord[0..cnt) oldest-first (selection sort over <= 16 entries)
821fn mp_cw_sort to void
822cnt = get mp_cw_cnt 0
823for i ulong cnt bound 16
824put mp_cw_ord i i
825end
826for i ulong cnt bound 16
827best = mp_cw_find_oldest i cnt
828if ne best i
829tmp = get mp_cw_ord i
830put mp_cw_ord i get mp_cw_ord best
831put mp_cw_ord best tmp
832end
833end
834end
835
836fn mp_unlink_cw_slot s ulong to bool
837pl = get mp_cw_plen s
838base = mul s MP_PATH_STRIDE
839for i ulong pl bound 4096
840put mp_file_path i get mp_cw_path add base i
841end
842ur = unlink mp_file_path 0 pl
843ret eq ur 0
844end
845
846fn mp_prune_batch cur_bytes ulong target_bytes ulong to ulong
847mp_cw_sort
848cnt = get mp_cw_cnt 0
849rem_cnt = ulong 0
850cur = cur_bytes
851for i ulong cnt bound 16
852if le cur target_bytes
853break
854end
855s = get mp_cw_ord i
856sz = get mp_cw_size s
857if mp_unlink_cw_slot s
858rem_cnt = add rem_cnt 1
859put mp_stat 5 add get mp_stat 5 1
860put mp_stat 6 add get mp_stat 6 sz
861if ge cur sz
862cur = sub cur sz
863else
864cur = ulong 0
865end
866end
867end
868ret rem_cnt
869end
870
871fn mp_refresh_alarm_state to ulong
872rc = mp_scan_store
873if ne rc MP_DISK_OK
874ret rc
875end
876sbytes = get mp_stat 1
877freeb = mp_compute_free sbytes
878put mp_stat 2 freeb
879maxb = get mp_cfg 5
880flrb = get mp_cfg 6
881breach = false
882if and gt maxb 0 gt sbytes maxb
883breach = true
884end
885if and gt flrb 0 lt freeb flrb
886breach = true
887end
888if breach
889put mp_stat 3 1
890else
891put mp_stat 3 0
892end
893ret MP_DISK_OK
894end
895
896note Explicit oldest-first memo_prune down to target_bytes (no hard entry cap; never silent)
897fn mp_memo_prune target_bytes ulong to ulong
898mp_ensure_init
899put mp_stat 5 0
900put mp_stat 6 0
901pass = ulong 0
902while lt pass 1000000 bound 1000000
903rc = mp_walk_store true
904if ne rc MP_DISK_OK
905ret rc
906end
907cur_bytes = get mp_stat 1
908if le cur_bytes target_bytes
909break
910end
911if eq get mp_cw_cnt 0 0
912break
913end
914if eq mp_prune_batch cur_bytes target_bytes 0
915break
916end
917pass = add pass 1
918end
919ret mp_refresh_alarm_state
920end