cli: add caller-buffer lexical path APIs

Add pure std.path queries and failure-preserving normalization/join using
safe reverse measurement/emission and caller-provided output storage.
Component queries return explicit Copy ranges; filesystem authority is
checked independently. Complete the STD-7 implementation gate.

Validation: 1079 conformance cases, 18 path cases with per-step collection
and 12-seed schedule stress, independent 132-path/49-join capacity oracle,
68 CLI checks, fourteen previous source/std CLI suites, unit/Quick Reference,
formatting, hosted smoke, 117/117 source coverage and reference budgets.
This commit is contained in:
dev committed 2026-10-07 13:41:45 +03:00
1 parent 8d8bef8f3e
commit 8edec5b831
35 files changed
+1189 -8

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
#!/usr/bin/env python3
"""Forward-stack byte oracle, independent of reverse source algorithm."""
from itertools import product
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[1] / "tests/conformance/source/path"
HELPERS = '''import std.path;
import std.bytes;
fn written(result: std.path.WriteResult) -> usize effects [] {
match result { std.path.WriteResult::ok(n) => { return n; }, std.path.WriteResult::invalid_input => { trap; }, std.path.WriteResult::size_overflow => { trap; }, std.path.WriteResult::short_output => { trap; } }
}
fn query(path: slice<u8>,absolute: bool,bs: usize,be: usize,pe: usize,es: usize,ee: usize) -> void effects [] {
assert(std.path.is_absolute(path) == absolute);
let base = std.path.basename(path); let parent = std.path.parent(path); let extension = std.path.extension(path);
assert(base.start == bs); assert(base.end == be); assert(parent.start == 0usize); assert(parent.end == pe); assert(extension.start == es); assert(extension.end == ee);
assert(base.start <= base.end); assert(base.end <= path.len); assert(parent.end <= path.len); assert(extension.start <= extension.end); assert(extension.end <= path.len);
}
fn normalized(path: slice<u8>,expected: slice<u8>) -> void effects [] {
for capacity in 0usize..expected.len+2usize {
var bytes: [u8;64] = [255u8;64];
match std.path.normalize(mut bytes[1usize..capacity+1usize],path) {
std.path.WriteResult::ok(n) => { assert(capacity >= expected.len); assert(n == expected.len); assert(std.bytes.equal(ref bytes[1usize..n+1usize],expected)); for i in n+1usize..64usize { assert(bytes[i] == 255u8); } },
std.path.WriteResult::short_output => { assert(capacity < expected.len); for i in 0usize..64usize { assert(bytes[i] == 255u8); } },
std.path.WriteResult::invalid_input => { trap; }, std.path.WriteResult::size_overflow => { trap; }
}
assert(bytes[0] == 255u8);
}
var twice: [u8;64] = [255u8;64]; let n = written(std.path.normalize(mut twice,expected)); assert(std.bytes.equal(ref twice[0usize..n],expected));
}
fn joined(base: slice<u8>,child: slice<u8>,expected: slice<u8>) -> void effects [] {
for capacity in 0usize..expected.len+2usize {
var bytes: [u8;64] = [255u8;64];
match std.path.join(mut bytes[1usize..capacity+1usize],base,child) {
std.path.WriteResult::ok(n) => { assert(capacity >= expected.len); assert(n == expected.len); assert(std.bytes.equal(ref bytes[1usize..n+1usize],expected)); for i in n+1usize..64usize { assert(bytes[i] == 255u8); } },
std.path.WriteResult::short_output => { assert(capacity < expected.len); for i in 0usize..64usize { assert(bytes[i] == 255u8); } },
std.path.WriteResult::invalid_input => { trap; }, std.path.WriteResult::size_overflow => { trap; }
}
assert(bytes[0] == 255u8);
}
}
fn invalid(base: slice<u8>,child: slice<u8>) -> void effects [] {
for capacity in 0usize..3usize { var bytes: [u8;4] = [255u8;4];
match std.path.join(mut bytes[0usize..capacity],base,child) { std.path.WriteResult::invalid_input => {}, std.path.WriteResult::ok(n) => { trap; }, std.path.WriteResult::size_overflow => { trap; }, std.path.WriteResult::short_output => { trap; } }
for i in 0usize..4usize { assert(bytes[i] == 255u8); }
}
}
'''
def normal(path):
assert b"\0" not in path
rooted = path.startswith(b"/")
parts = []
for part in path.split(b"/"):
if part in (b"", b"."):
continue
if part == b"..":
if parts and parts[-1] != b"..":
parts.pop()
elif not rooted:
parts.append(part)
else:
parts.append(part)
result = b"/".join(parts)
return b"/" + result if rooted else result or b"."
def joining(base, child):
if child.startswith(b"/"):
return normal(child)
return normal(base + (b"/" if base and child else b"") + child)
def queries(path):
trimmed = path.rstrip(b"/")
end = len(trimmed)
start = trimmed.rfind(b"/") + 1 if trimmed else 0
prefix = len(path[:start].rstrip(b"/"))
if path.startswith(b"/") and not prefix:
prefix = 1
dot = path[start:end].rfind(b".")
ext = start + dot + 1 if 0 < dot < end - start - 1 else end
return path.startswith(b"/"), start, end, prefix, ext, end
def literal(value):
if not value:
return "ref blank[0usize..0usize]"
encoded = "".join(chr(byte) if 32 <= byte <= 126 and byte not in (34, 92) else f"\\x{byte:02x}" for byte in value)
return 'b"' + encoded + '"'
paths = {b"", b"/", b"///", b"//a//b///", b".profile", b".profile.json", b"a..b", b"a.", b"a/../b", b"a/../../b", b"/../../a", b"../a/..", b"x\\y/../z", b"C:\\file.txt", b"a/\xff.bin", b"\x80\xfe", b"/a/\xff/.."}
for size in range(1, 4):
for parts in product((b"a", b".", b".."), repeat=size):
path = b"/".join(parts)
paths.update((path, b"/" + path, path + b"//"))
paths = sorted(paths)
# Independently sanity-check canonical algebra before materializing fixtures.
for path in paths:
assert normal(normal(path)) == normal(path)
result = normal(path)
assert b"//" not in result and (result == b"/" or not result.endswith(b"/"))
calls = []
for path in paths:
absolute, *indices = queries(path)
calls.append(f"query({literal(path)},{str(absolute).lower()}," + ",".join(f"{n}usize" for n in indices) + ");")
calls.append(f"normalized({literal(path)},{literal(normal(path))});")
pairs = list(product((b"", b"a", b"/a", b"a/b/", b"../a", b"/", b"a/.."), (b"", b"b", b"..", b"../b", b"../../b", b"/b", b"//../b/")))
for base, child in pairs:
calls.append(f"joined({literal(base)},{literal(child)},{literal(joining(base,child))});")
invalids = ((b"late/name/\0", b"/b"), (b"a", b"b/\0"), (b"\0", b""), (b"", b"\0"))
for base, child in invalids:
calls.append(f"invalid({literal(base)},{literal(child)});")
# Keep each batch's byte literals in a separate frame. Holding every literal
# in main's frame adds hundreds of roots to every per-instruction GC pass,
# without exercising a different path contract. All vectors/capacities remain.
batches = [calls[index:index + 16] for index in range(0, len(calls), 16)]
functions = []
for index, batch in enumerate(batches):
functions.append(f'fn batch_{index}() -> void effects [] {{\nlet blank: [u8;1] = [0u8;1];\n' + "\n".join(batch) + '\n}\n')
expected = '''// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.19
// fuel: 2000000
module test.path_model
''' + HELPERS + "\n".join(functions) + 'fn main() -> u64 effects [] {\n' + "\n".join(f"batch_{index}();" for index in range(len(batches))) + '\nreturn 42;\n}\n'
path = ROOT / "model.aji"
if "--write" in sys.argv:
path.write_text(expected)
assert path.read_text() == expected, "Path fixture differs from independent forward-stack oracle"
print(f"path oracle passed ({len(paths)} paths/queries/idempotence, {len(pairs)} joins, 4 malformed pairs; all short/exact/extra capacities)")