Skip to content

Add durable-host I/O primitives for Ny and Matrix - #42

Merged
bytetroll merged 11 commits into
masterfrom
codex/enigma-durable-io
Nov 22, 2026
Merged

bytetroll merged 11 commits into
masterfrom
codex/enigma-durable-io

Conversation

@bytetroll

@bytetroll bytetroll commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Durable byte publication with directory synchronization and an explicit uncertain outcome after a failed publication barrier; retain the existing atomic-write and stream-flush semantics.
  • Context-owned, nonblocking advisory locks with private backing files, nonreused tokens, invocation cleanup, and execution-checkpoint refusal while held.
  • A checked file_lock_is_held query 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.
  • Private directory creation, no-follow owner/mode verdicts, and standalone SHA-256; reject named directory operands ending in a separator to preserve final-component no-follow checks.
  • Optional line input that separates a blank line, clean EOF, and input failure, including failed embedded streams with zero errno.
  • Native providers for the existing byte-text helpers and random.hex needed by the Matrix consumer.
  • Canonical primitive aliases in record/class field metadata, so optional/result text fields match their concrete enum schemas on native routes. Nominal lookup checks remain unchanged.

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:

  • The original 14 host-I/O CTest groups pass at 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.
  • Ten focused field/enum/ABI route groups pass at ebc4eb5, including new paired Ny/Matrix Option<text> and Result<text,text> record/class fixtures through all standard routes, and existing nominal mismatch rejection checks.
  • Seven focused ownership/helper/lifecycle/ABI/ordinary-I/O groups pass at dbb4379, with live/stale/foreign token checks, embedded invocation teardown, paired Ny/Matrix routes, and forged return-schema rejection.
  • A complete development SDK was packaged from clean runtime source dbb4379946f6800a79e485dcd890e8f5b0902b31, with matching compiler, runtime, and standard library. SDK packaging used NIL_RUN_TESTS=OFF; the focused tests ran separately. This is not full-suite, cross-platform, or release-SDK qualification.
  • Enigma passes 170 core Matrix test executions and 44 application/shared-session executions across VM/native. Nine external application tests exercise all typed operations, restart admission, failed-open cleanup, explicit lock release, stale owners, poisoned commits, path pinning, and full-capacity typed discovery on both routes. The Matrix VM/native hosts and Python host each pass 13 recovery and six storage-fault tests, and each host opens the other's checkpoint.

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/Result name 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.

bytetroll and others added 10 commits November 15, 2026 13:10
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
bytetroll marked this pull request as ready for review November 22, 2026 22:21
@bytetroll
bytetroll merged commit 5f73dfe into master Nov 22, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant