Repository navigation
Add durable-host I/O primitives for Ny and Matrix - #42
Merged
Merged
Conversation
Matrix-authored Enigma exposed a general library gap: a checked atomic write synchronized the candidate inode and published its name, but left the parent directory undurable, a stream flush was not a filesystem barrier, and no supported advisory-lock API existed. Applications had to supply their own platform host. Add write_bytes_durable, sync_directory, acquire_file_lock, and release_file_lock to nil.std.io as an additive POSIX profile. Durable writes reuse the atomic publication helper and synchronize the same anchored directory descriptor used to publish; a post-publication sync failure returns an explicitly uncertain outcome rather than implying the old destination survived. Existing write_bytes_atomic and File.flush contracts are unchanged. Locks are nonblocking exclusive advisory locks on private 0600 regular files. A runtime-owned registry holds the descriptors and hands out process-monotonic tokens that are never recycled, never expose a host descriptor, are checked against their owning execution context, and are closed at invocation termination and context teardown. Execution checkpoints reject open locks. Unsupported platforms fail closed before any filesystem mutation. Tests cover the shared OS helpers, embedded VM invocation/reset/destructor cleanup, static and shared native context lifetimes, paired Ny/Matrix textual and binary VM plus AOT and JIT routes, malformed ABI declarations, private file modes, contention, and Linux file and directory sync fault injection including uncertain publication. This is a Linux-tested POSIX mechanism, not a production database protocol, power-loss certification, pathname containment service, or rollback defense. Private-directory creation, owner/mode inspection, standalone SHA-256, and a complete storage host remain separate APIs. Source declarations alone do not give an installed SDK the new provider; release qualification is independent work. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
NIL-0028 left private-directory creation, owner/mode inspection, and a standalone SHA-256 as separate APIs, so a storage application still had to supply a platform host for them. Each mechanism already existed and was exercised inside the framework: owner-only directory creation for toolchain scratch space, the private-regular predicate used when acquiring a lock, and SHA-256 for module fingerprints. The gap was a supported language surface, not an algorithm. Add create_private_directory, is_private_regular_file, is_private_directory, and sha256_hex. Directory creation applies its mode at creation rather than through a later chmod, which would expose a writable directory in between, and an existing entry is reported rather than adopted. The predicates return Result<bool, text> so three outcomes stay distinct: inspected and matching, inspected and not, or not inspectable. Collapsing absence into a false verdict would let a caller read missing as unsafe or the reverse. Inspection does not follow a final symlink, so a link is judged as itself. A path predicate describes an entry at inspection time and is not a safe substitute for opening it under O_NOFOLLOW; publication and lock acquisition keep their own checked opens. A digest detects accidental corruption and identifies content, and authenticates nothing: whoever can replace the bytes can replace the digest beside them. Tests cover creation mode and exclusivity, cross-kind verdicts, loosened file and directory modes, symlinks to private targets, multiply-linked files, FIFOs, uninspectable entries, unsupported-platform behavior before mutation, and RFC 6234 vectors including embedded NUL bytes. The paired Ny and Matrix fixtures exercise every surface across textual and binary VM, AOT, and JIT, with the host oracle checking directory modes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
read_line faults at end of input, which suits a program that requires input but not a service that reads requests until its caller closes the stream: a clean shutdown became a fault. The Matrix durable host needs this. Its Python predecessor exits zero when standard input closes, and the recovery suite closes the stream to end a session, so a host that faulted there would report failure for an ordinary shutdown. Add read_line_option, which reads a line exactly as read_line does but answers None at end of input. read_line keeps its contract. A genuine input error still faults, and that is the decision rather than a detail: a reader that must fail closed cannot be handed "no more input" when the truth is "cannot read". Distinguishing the two is not free. A C++ stream reports a genuine end of input and a host read error identically, with eofbit and failbit set and badbit clear, so reading a directory descriptor as standard input is indistinguishable from an empty pipe by stream state alone. Only errno separates them, so both providers clear errno immediately before the read and inspect it only when nothing was extracted. The ambiguous case is a failure: a reader that stops early on a healthy stream is a visible bug, while one that reports success after an unread request is a silent one. The surface rides the existing checked ordinary-I/O bridge, so it is lowered once and reaches the interpreter, AOT and JIT routes without separate codegen. Paired Ny and Matrix fixtures cover several lines then a clean stop, a final line without a terminator, and empty input. A separate case feeds a directory descriptor as standard input and requires a fault with no clean-stop output, which is the check that fails if the implementation reads stream state instead of errno. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
A Matrix application that draws a fresh identifier per session or per recovery could not be built natively: random.hex was interpreter-only, so native code generation refused the reachable declaration. The Enigma durable host draws a new incarnation on every recovery and hit exactly that. Route the intrinsic through the checked ordinary-I/O bridge and provide it from the same entropy helper and hex encoding the interpreter uses, so a draw does not depend on which route asked for it. Riding the bridge means the interpreter, AOT and JIT routes are all covered without separate codegen. random_bytes stays interpreter-only on purpose. It returns arbitrary bytes, which can include NUL, and native text is a NUL-terminated ABI, so that form cannot cross it. Callers that need raw bytes on a native route can draw hex and decode. Paired Ny and Matrix fixtures check the encoding, the lengths for one, 16 and 32 bytes, that independent draws differ, and that a zero, negative or oversized count is an error rather than a short draw, across every route. The existing secure-random fixtures stay interpreter-only because they also exercise random_bytes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Master's WebView provider and rooted file snapshots landed beside the durable-host I/O work, and the two sets of additions met in three files. Every collision was additive; both sides are kept whole. - native_runtime.cpp: the durable lock registry and the WebView context are both members of the one runtime context. - vm.cpp: both intrinsic headers are included, and an invocation terminal now clears the WebView context as well as the file locks. - secure_file_io.cpp: master's rooted-snapshot helpers and reader sit alongside the no-follow directory-operand check, and the rooted reader now precedes the durable write path. The atomic writer keeps this branch's rename to write_file_atomic_posix with its durable flag, so master's literal write_bytes_atomic returns become the operation-named failure lambda; the write_file_atomic wrapper master's callers use is unchanged. Validation on Linux x86_64: a clean Release configure and build of the merge result, then the full CTest suite, 609 of 609 passing, with PostgreSQL and UI display skipped for a missing server and display. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
bytetroll
marked this pull request as ready for review
November 22, 2026 22:21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Enigma's Matrix storage host could encode its state but needed an external host for filesystem durability, writer ownership, private entries, and request-stream shutdown. Add the shared mechanisms to NIL so Ny and Matrix can use the same runtime contracts:
file_lock_is_heldquery so an application handle cannot rely on a cached active flag after its runtime context has retired the lock. It reports current-registry ownership without releasing the lock or accessing a path.random.hexneeded by the Matrix consumer.NIL-0028 through NIL-0030 and the public library references describe the exact boundaries. The filesystem additions are a Linux-tested POSIX profile and fail closed on unsupported platforms. Storage ancestry remains trusted; these mechanisms do not provide a database recovery policy, containment service, or rollback defense.
Validation on Linux x86_64:
451a4ff: durable helper faults/lifecycle/routes, private directories, embedded console state, static/shared native ABI, existing byte and ordinary-I/O regressions, console routes, and paired secure-hex routes.ebc4eb5, including new paired Ny/MatrixOption<text>andResult<text,text>record/class fixtures through all standard routes, and existing nominal mismatch rejection checks.dbb4379, with live/stale/foreign token checks, embedded invocation teardown, paired Ny/Matrix routes, and forged return-schema rejection.dbb4379946f6800a79e485dcd890e8f5b0902b31, with matching compiler, runtime, and standard library. SDK packaging usedNIL_RUN_TESTS=OFF; the focused tests ran separately. This is not full-suite, cross-platform, or release-SDK qualification.Native arrays of nominal records remain outside the scalar-array profile; Enigma uses a bounded typed report batch with primitive columns. NILData's 1 MiB value limit is unchanged; Enigma's separately bounded worker framing is application code.
F93 records an open Matrix imported-choice naming defect found by the client. Application code avoids the
result/Resultname collision. Loader checks and the closed enum/class storage profile remain intact; the client uses direct typed reply records for class-bearing results.No product version or release tag changes.