code wiki / (root) / nx_fsops_outline_t77.nx

nx_fsops_outline_t77.nx source

↩ module page · 101 lines · 5157 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_t77.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 clipped_lines:i64=0 54 var clipped_bytes:i64=0 55 var leaddone: i64 = 0 56 var leadcnt: i64 = 0 57 while i < n { 58 var le: i64 = i 59 var f: i64 = 1 60 while f == 1 { if le >= n { f = 0 } else { if buf[le] == (10 as u8) { f = 0 } else { le = le + 1 } } } 61 if leaddone == 0 { 62 if fsx_starts(buf, i, n, "//" as *u8) == 1 { 63 if leadcnt < 4 { 64 var w: i64 = le - i 65 if w > FSX_OUTLINE_LINEMAX { w = FSX_OUTLINE_LINEMAX } 66 sys_write(1, ((buf as i64 + i) as *u8), w) 67 if le-i>w {clipped_lines=clipped_lines+1;clipped_bytes=clipped_bytes+le-i-w;fsx_puts(" [LINE-CLIPPED omitted_bytes=");fsx_putn(le-i-w);fsx_puts("]")} 68 fsx_puts("\n" as *u8) 69 leadcnt = leadcnt + 1 70 } 71 } else { if le > i { leaddone = 1 } } 72 } 73 if fsx_is_decl(buf, i, n) == 1 { 74 decls = decls + 1 75 if shown < FSX_OUTLINE_CAP { 76 fsx_puts("L" as *u8); fsx_putn(lineno); fsx_puts(": " as *u8) 77 var w2: i64 = le - i 78 if w2 > FSX_OUTLINE_LINEMAX { w2 = FSX_OUTLINE_LINEMAX } 79 sys_write(1, ((buf as i64 + i) as *u8), w2) 80 if le-i>w2 {clipped_lines=clipped_lines+1;clipped_bytes=clipped_bytes+le-i-w2;fsx_puts(" [LINE-CLIPPED omitted_bytes=");fsx_putn(le-i-w2);fsx_puts("]")} 81 fsx_puts("\n" as *u8) 82 shown = shown + 1 83 } 84 } 85 i = le 86 if i < n { i = i + 1 } 87 lineno = lineno + 1 88 } 89 fsx_puts("NX-FS-OUTLINE decls_shown=" as *u8); fsx_putn(shown) 90 fsx_puts(" decls_total=" as *u8); fsx_putn(decls) 91 fsx_puts(" lines=" as *u8); fsx_putn(lineno - 1) 92 fsx_puts(" clipped_lines=");fsx_putn(clipped_lines);fsx_puts(" clipped_line_bytes=");fsx_putn(clipped_bytes) 93 fsx_puts(" content_scope=declaration-heads-only") 94 if decls > shown || clipped_lines>0 { fsx_puts(" truncated=1" as *u8) } else { fsx_puts(" truncated=0" as *u8) } 95 if decls == 0 { 96 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) 97 } else { 98 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) 99 } 100 return decls 101}