code wiki / (root) / nx_fsops_outline.nx

nx_fsops_outline.nx source

↩ module page · 95 lines · 4572 B

1// nx_fsops_outline.nx -- OUTLINE verb for the consolidated fs tool (MCP name: nx_fs). Sibling of 2// nx_fsops_lib.nx / nx_fsops_write.nx. THE READ-DIGEST LEVER (measured 2026-07-21): file Reads are 47% of 3// all Claude context bytes and 57.6% of that comes from files >=50KB. `outline` returns a STRUCTURAL DIGEST 4// -- the leading comment block + every top-level declaration with its line number -- so a caller ORIENTS in 5// ~2KB instead of dumping a 50KB file, then pulls the exact body with `nx_fs lines <path> <L> <count>`. 6// Scale-law envelope (shown/total/truncated ALWAYS declared); every refusal states WHY + a FIX command 7// (operator: refusal without feedback+recommendation is unacceptable). license_tier: ORIGINAL 8import "nx_fsops_lib.nx" 9import "nx_syscalls.nx" 10 11const FSX_OUTLINE_CAP: i64 = 400 12const FSX_OUTLINE_LINEMAX: i64 = 200 13 14func fsx_starts(buf: *u8, i: i64, n: i64, p: *u8) -> i64 { 15 var k: i64 = 0 16 while p[k] != (0 as u8) { 17 if i + k >= n { return 0 } 18 if buf[i+k] != p[k] { return 0 } 19 k = k + 1 20 } 21 return 1 22} 23func fsx_is_decl(buf: *u8, i: i64, n: i64) -> i64 { 24 if fsx_starts(buf, i, n, "func " as *u8) == 1 { return 1 } 25 if fsx_starts(buf, i, n, "const " as *u8) == 1 { return 1 } 26 if fsx_starts(buf, i, n, "import " as *u8) == 1 { return 1 } 27 if fsx_starts(buf, i, n, "type " as *u8) == 1 { return 1 } 28 if fsx_starts(buf, i, n, "struct " as *u8) == 1 { return 1 } 29 if fsx_starts(buf, i, n, "pub " as *u8) == 1 { return 1 } 30 if fsx_starts(buf, i, n, "enum " as *u8) == 1 { return 1 } 31 return 0 32} 33// outline: returns decl count; -1 absent; -2 DENIED. 34func fsx_outline(path: *u8) -> i64 { 35 if fsx_denied(path) == 1 { 36 fsx_puts("NX-FS-OUTLINE DENIED: path matches the secret deny-list. WHY: this tool never returns key material. FIX: point at source/text; if the name only coincidentally contains key/token/secret/passw/.pem, rename it.\n" as *u8) 37 return 0 - (2 as i64) 38 } 39 let buf: *u8 = sys_mmap(FSX_READ_CAP + 1) 40 let n: i64 = vw_read(path, buf, FSX_READ_CAP) 41 if n <= 0 { 42 fsx_puts("NX-FS-OUTLINE ABSENT: cannot read " as *u8); fsx_puts(path) 43 fsx_puts(" . FIX: confirm the path with `nx_fs ls <dir>`; the file may be empty or binary.\n" as *u8) 44 return 0 - 1 45 } 46 fsx_puts("NX-FS-OUTLINE " as *u8); fsx_puts(path); fsx_puts(" bytes=" as *u8); fsx_putn(n) 47 if n >= FSX_READ_CAP { fsx_puts(" (outline covers first " as *u8); fsx_putn(FSX_READ_CAP); fsx_puts(" bytes only)" as *u8) } 48 fsx_puts("\n" as *u8) 49 var i: i64 = 0 50 var lineno: i64 = 1 51 var decls: i64 = 0 52 var shown: i64 = 0 53 var leaddone: i64 = 0 54 var leadcnt: i64 = 0 55 while i < n { 56 var le: i64 = i 57 var f: i64 = 1 58 while f == 1 { if le >= n { f = 0 } else { if buf[le] == (10 as u8) { f = 0 } else { le = le + 1 } } } 59 if leaddone == 0 { 60 if fsx_starts(buf, i, n, "//" as *u8) == 1 { 61 if leadcnt < 4 { 62 var w: i64 = le - i 63 if w > FSX_OUTLINE_LINEMAX { w = FSX_OUTLINE_LINEMAX } 64 sys_write(1, ((buf as i64 + i) as *u8), w) 65 fsx_puts("\n" as *u8) 66 leadcnt = leadcnt + 1 67 } 68 } else { if le > i { leaddone = 1 } } 69 } 70 if fsx_is_decl(buf, i, n) == 1 { 71 decls = decls + 1 72 if shown < FSX_OUTLINE_CAP { 73 fsx_puts("L" as *u8); fsx_putn(lineno); fsx_puts(": " as *u8) 74 var w2: i64 = le - i 75 if w2 > FSX_OUTLINE_LINEMAX { w2 = FSX_OUTLINE_LINEMAX } 76 sys_write(1, ((buf as i64 + i) as *u8), w2) 77 fsx_puts("\n" as *u8) 78 shown = shown + 1 79 } 80 } 81 i = le 82 if i < n { i = i + 1 } 83 lineno = lineno + 1 84 } 85 fsx_puts("NX-FS-OUTLINE decls_shown=" as *u8); fsx_putn(shown) 86 fsx_puts(" decls_total=" as *u8); fsx_putn(decls) 87 fsx_puts(" lines=" as *u8); fsx_putn(lineno - 1) 88 if decls > shown { fsx_puts(" truncated=1" as *u8) } else { fsx_puts(" truncated=0" as *u8) } 89 if decls == 0 { 90 fsx_puts(" . WHY: no top-level func/const/import/type/struct. FIX: not NishiLang source -- use `nx_fs lines " as *u8); fsx_puts(path); fsx_puts(" 1 40` for a head, or `nx_fs read " as *u8); fsx_puts(path); fsx_puts("`.\n" as *u8) 91 } else { 92 fsx_puts(" . NEXT: pull one body with `nx_fs lines " as *u8); fsx_puts(path); fsx_puts(" <L> <count>` (L from the list).\n" as *u8) 93 } 94 return decls 95}