nx_disk_budget.nx source
↩ module page · 118 lines · 3899 B
1// nx_disk_budget.nx -- sovereign hard-ceiling on bytes written to disk.
2//
3// Per user directive 2026-05-14: "your design created massive hard drive
4// wastes of 70 gbs and more in minutes ... nishi lang must be autonomous
5// and timely so that it can do at least a million [algorithms] in a
6// timely manner without massive issues like this".
7//
8// Root-cause class: runaway disk consumption when an ingester writes
9// without an upper bound. This primitive declares the bound at the
10// call site (substrate-additive-not-restrictive cardinal) and traps
11// fail-fast when the ceiling is hit (runaway-execution-substrate
12// cardinal).
13//
14// Usage pattern:
15//
16// let b: *NxDiskBudget = nx_disk_budget_new(5 * 1024 * 1024 * 1024)
17// // ... inside the per-record loop:
18// let rc: nx_int = nx_disk_budget_write(b, fd, line, line_len)
19// if rc == NX_DISK_BUDGET_EXCEEDED { ... emit checkpoint, halt cleanly ... }
20//
21// This is a COUNTER-based budget: it tracks what THIS process writes
22// through the wrapped API. It does not see writes by other processes.
23//
24// genealogy_id: hard_disk_quota
25// lineage_id: function
26
27// nx_safety_envelope:
28// intended_use: AUTO_APPLIED -- primitive-specific tuning queued
29// sil_target: SIL1
30// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail]
31// verdict: NOT_YET_EVALUATED
32
33import "nx_syscalls.nx"
34import "nx_tier.nx"
35
36// ===== error / status codes (named, never magic) =====================
37
38const NX_DISK_BUDGET_OK: nx_int = 0
39const NX_DISK_BUDGET_EXCEEDED: nx_int = -1
40const NX_DISK_BUDGET_WRITE_FAIL: nx_int = -2
41
42// ===== state struct ===================================================
43
44struct NxDiskBudget {
45 ceiling_bytes: nx_size,
46 used_bytes: nx_size,
47 n_writes_ok: nx_int,
48 n_refused: nx_int,
49}
50
51// ===== constructors ===================================================
52
53func nx_disk_budget_new(ceiling_bytes: nx_size) -> *NxDiskBudget {
54 let raw: *nx_byte = sys_mmap(NX_BUF_TINY)
55 let b: *NxDiskBudget = raw as *NxDiskBudget
56 b.ceiling_bytes = ceiling_bytes
57 b.used_bytes = 0
58 b.n_writes_ok = 0
59 b.n_refused = 0
60 return b
61}
62
63// ===== predicates ====================================================
64
65func nx_disk_budget_can_write(b: *NxDiskBudget, extra_bytes: nx_size) -> nx_int {
66 let total: nx_size = b.used_bytes + extra_bytes
67 if total > b.ceiling_bytes { return 0 }
68 return 1
69}
70
71// ===== accounting (no I/O) ===========================================
72
73func nx_disk_budget_account(b: *NxDiskBudget, n: nx_size) -> nx_size {
74 b.used_bytes = b.used_bytes + n
75 return b.used_bytes
76}
77
78// ===== safe wrapped write =============================================
79
80func nx_disk_budget_write(b: *NxDiskBudget, fd: nx_fd, buf: *nx_byte, n: nx_size) -> nx_int {
81 if nx_disk_budget_can_write(b, n) == 0 {
82 b.n_refused = b.n_refused + 1
83 return NX_DISK_BUDGET_EXCEEDED
84 }
85 let w: nx_int = sys_write(fd, buf, n)
86 if w < 0 {
87 return NX_DISK_BUDGET_WRITE_FAIL
88 }
89 b.used_bytes = b.used_bytes + (w as nx_size)
90 b.n_writes_ok = b.n_writes_ok + 1
91 return w
92}
93
94// ===== query =========================================================
95
96func nx_disk_budget_used(b: *NxDiskBudget) -> nx_size {
97 return b.used_bytes
98}
99
100func nx_disk_budget_remaining(b: *NxDiskBudget) -> nx_size {
101 if b.used_bytes >= b.ceiling_bytes { return 0 }
102 return b.ceiling_bytes - b.used_bytes
103}
104
105func nx_disk_budget_n_writes_ok(b: *NxDiskBudget) -> nx_int {
106 return b.n_writes_ok
107}
108
109func nx_disk_budget_n_refused(b: *NxDiskBudget) -> nx_int {
110 return b.n_refused
111}
112
113// ===== convenience ratios (integer percent, no f64) ==================
114
115func nx_disk_budget_pct_used(b: *NxDiskBudget) -> nx_int {
116 if b.ceiling_bytes == 0 { return 100 }
117 return (b.used_bytes * 100) / b.ceiling_bytes
118}