notes

The sink protocol: one way to write bytes somewhere

Milestone 50, the protocol lane. partsfoundry/byte_sink_contract, fixtures/src/sink.rs, the std PAL's sys/stdio/nife.rs, and moho_abi::Error::Gone.

The problem, which was not the one anybody expected

The shell has no |, > or <, so the obvious reading is that a pipe is missing. It is not. patches/std-nife/.../pal/nife/rt.rs fixes STDOUT_SLOT = 1 and sys/stdio/nife.rs implements println! as a SEND on that slot, which means a program's output destination is already a capability its spawner chose. Redirection is putting a different capability in that slot. No kernel change, no pipe object, no dup2.

What blocked it was that we had four different protocols for "write these bytes there":

Sink Protocol before this lane
std println! SEND, register-only, 16 bytes per message, w0 = length, w1|w2 = bytes
line_editor CALL, shared page, OPERATION_WRITE, r0 = bytes consumed
file_system_contract CALL, handle plus offset plus shared page, WRITE
console server shared page, SEND the length, ACK on a second endpoint

A child cannot be indifferent to what is in its output slot until those are one protocol, and that unification is this lane. > and | are parser work afterwards.

The shape, and the two decisions inside it

A sink is a Rendezvous capability with WRITE, and nothing else.

Register-only, not a shared page

This is forced, not chosen. Milestone 50's finding is that redirection is substituting one capability in one slot. The moment a sink requires a page mapped at an agreed virtual address, substitution stops being one grant and becomes a spawn-time negotiation between the shell, the writer and the sink, and the finding evaporates. It decides the pipe: for a | b the shell creates an endpoint, hands SEND to a and RECEIVE to b, and that is the entire construction. A page-based sink would make a pipe cost a frame, a mapping in each of two address spaces, and a revocation record for each.

The cost is 16 bytes per message. That is the three-word fastpath (notes/abi.md §2) and it is what std's stdout already used, so nothing on the default path got slower; see the benchmarks below.

SEND, not CALL

The difference is whether the writer learns anything. A CALL would return "bytes consumed", which for a self-framing message is always "all of them", and it would pay a second IPC hop on the hottest path in the system to say so. Back-pressure does not need the reply: SEND blocks until a receiver takes the message, so the rendezvous is the flow control, which is the property terminal_contract::OPERATION_BYTES had already written down.

SEND makes the reader of a pipe an ordinary program that does nothing but receive. With a CALL protocol every pipe reader would owe a reply, which means every program on the right of a | would have to know it was on the right of a |.

What the protocol deliberately does not carry

A per-write error code. A sink that can no longer accept bytes says so by ceasing to exist: it destroys its receiving end and the writer's next SEND fails. That collapses "the reader exited", "the device is gone" and "the filesystem is full" into one fact, and the honest caveat is that the writer learns that the sink is over and not why. It is the right trade for a byte stream, whose only available response to any of the three is to stop, and it is the trade Unix makes as well: a writer gets EPIPE and never the reader's reason. A destination whose failures a client must tell apart is not a sink, it is a service, and it should be a CALL protocol like file_system_contract.

Types. This carries bytes. Typed pipelines are a separate and larger fork, recorded as one in design/roadmap/0050-pipes-and-redirection.md, and nothing in this framing is a step toward one.

Seek, truncate, re-read, stat. A sink appends, and that is the payoff milestone 50 (pipes and redirection) claims over Unix. > report.txt hands a program strictly less than fd 1 with full file semantics does, and it is the opcode list that makes that true rather than policy.

The wire

  w0 = (op << 56) | len          w1, w2 = up to 16 bytes, little-endian, low word first

  OPERATION_BYTES = 0    len = 1..=16   the bytes are in w1|w2
  OPERATION_END_OF_STREAM   = 1    len = 0        the writer is finished; w1 = w2 = 0

An end of stream is exactly (end_of_stream(), 0, 0): a writer sends zero in its two later words, and unpack refuses any other end of stream as malformed. A result due after the stream (rm's exit status) goes on an endpoint of its own.

OPERATION_BYTES is zero on purpose. With the opcode at zero a bytes message's first word is exactly its byte count, which is bit for bit the framing std's stdout already sent. So unifying the protocol changed no instruction on the fastpath, cost no message, and the benchmark that prices println! cannot tell that anything happened. An opcode is a claim about what a message means; the cheapest claim to make is the one the wire was already making.

OPERATION_END_OF_STREAM is new and it is not decoration. Without it a pipe's reader blocks forever after the writer exits, and "the producer is done" would have to be inferred from a death notification the reader may not even be the supervisor for. That is a fact about process supervision standing in for a fact about a stream. std sends it from the PAL's cleanup, which std's runtime calls after main returns and after it has flushed stdout.

"Gone" versus "never had one", which is the point of doing this now

The old code swallowed a failed SEND, with a comment saying a program without a console "just prints into the void, which is what every OS does to a process whose stdout is closed". That is right for a program with no console and wrong for a pipeline, where yes | head must end when head exits. So the protocol needs to tell two failures apart:

The kernel could not tell them apart, and that was the actual finding

Both arrived as moho_abi::Error::NoSuchSlot. A destroyed endpoint leaves the holder's capability in place (endpoints are named generationally, partsfoundry/generational_table), and the failure surfaces when sched::take_ipc_aborted is set; system_call.rs mapped that to NoSuchSlot, the same value an empty slot returns. The only available behavior was therefore the wrong one for a pipeline, and no amount of userspace protocol design could have recovered the distinction, because the fact lives in the kernel.

So the ABI grew one variant, moho_abi::Error::Gone (-11): the capability names an object that no longer exists. It applies to all five endpoint IPC paths (SEND, RECEIVE, SEND_CAPABILITY, RECEIVE_CAPABILITY, CALL), because the fact is about the endpoint and not about the direction.

This is the second time a distinction like this came up and it was resolved the other way the first time. DECISIONS §32 (a supervisor may collect a corpse) deliberately makes Rendezvous::REAP return one error for "already collected" and "not your child", because telling them apart would let a supervisor probe the tid space of children it has no relationship with. Gone carries no such risk: the capability is one the caller already holds, in its own capability table, so learning that its object died reveals nothing it was not entitled to know.

SIGPIPE, arriving through std's own seam

std already has exactly this two-way split and we had been defeating it. io::stdio's handle_ebadf swallows an error for which is_ebadf returns true and propagates everything else, and a propagated error makes println! panic. The old PAL returned true unconditionally, so every failure was swallowed. Now:

That is byte for byte what a Rust program on Linux does when its reader exits, because Rust sets SIGPIPE to SIG_IGN and lets the EPIPE reach the same panic. A third signal disappears, on the same grounds milestone 48 retired SIGTTIN and SIGTSTP: the question the signal answered is already answered by who holds what.

The sinks

Three programs in fixtures/, and the sink among them is the fs_file_caretaker shape: something that speaks the sink contract to its client and the underlying protocol to whatever is behind it.

They are three programs, not one binary dispatching ROLE_FILE, ROLE_VERIFY and ROLE_WRITER out of arg0. Role numbers would live in three files (the program, the kernel's sink_tests, and fs_service) with nothing keeping them equal, and a mismatch spawns the wrong role and hangs. Three programs have no role numbers to keep equal.

What the indifference test proves

kernel::user::sink_tests, both ISAs.

The same std_exerciser ELF is spawned twice with identical grants except for what is behind slot 1. Once it gets an endpoint the kernel test receives on directly (the pipe shape: the reader is an ordinary receiver). Once it gets an endpoint served by file_sink, which writes the bytes into a file on the real RedoxFS image through the real FS server. The test then reads that file back and compares it, byte for byte, with the transcript the first arm received.

Same binary, same transcript, two destinations that share nothing but the sixteen bytes of a message. The program is not told which one it has and has no way to find out.

The Gone half is asserted separately and by value, because it is a claim about a number. The kernel creates an endpoint out of a region it owns, spawns sink_transcript_writer with WRITE on it, takes some messages, destroys the region, and the writer reports that its next SEND classified as Sent::Gone rather than Sent::NoSink. Without the ABI variant that assertion is not expressible.

Benchmarks

The lane added no hop to println!'s path, and the icount benchmarks say so: ipc_rtt_el0, relay_rtt, call_reply and null_system_call all moved by less than 1%, in both directions, on aarch64 and riscv64. That is build noise, so the baseline file was deliberately not updated. No benchmark prices println! itself, so a regression inside the PAL's chunking loop would not show. The table and both caveats are in sink-protocol/benchmarks.md.

What is still missing, named where a reader meets it

The terminal's sink adapter, and the wall it hit

The last of milestone 50's remainders. A program's output slot can now hold the terminal, and it still cannot tell that from a pipe or a file.

  a declaring child ──sink_proto SEND──► terminal_sink_caretaker ──OPERATION_PRINT CALL──► line_editor ──► console

It is a process for a capability reason, which was known

The cheap move is to have line_editor serve the sink contract on the endpoint it already has: a SEND arrives there with no reply capability, so it is trivially distinguishable from the CALLs it serves. That is wrong. That endpoint also carries OPERATION_READLINE, and WRITE on an endpoint is the right to CALL, so a child handed it as its output slot would hold the terminal's keyboard. A sink capability that can read the keyboard is not a sink capability. This kernel offers no receive-on-a-set, so the terminal cannot serve two endpoints itself, and the answer is a separate process holding both contracts and handing out only one.

And it needed a new opcode, which was not known

Building the adapter found that OPERATION_WRITE reads from the client's one output page, and a second page-based client would need a second frame. So the terminal contract grew OPERATION_PRINT: up to eight bytes carried in the request's own words, replied when they are on the wire, sharing expand_output with OPERATION_WRITE. Two writers, one terminal, one set of manners, no second frame. Why eight, and the page problem it avoided, are in sink-protocol/print-opcode.md.

What it is for

DECISIONS §67's declared second stream. A program that declares diagnostics gets this endpoint in its declared slot by default, endowed by the progenitor from the manifest exactly as the clock is, and 2> replaces it with a file the shell backs. So a date complaining about a missing clock reaches the screen without passing through the shell at all, which is stronger than the shell printing it. Nothing the shell does to the output can touch those bytes, and caps says so in its diags row.

kernel::user::sink_tests::the_terminal_is_a_sink_like_any_other_and_the_writer_cannot_tell is the proof, and it is the indifference claim made a third time: the same sink_transcript_writer ELF, the same transcript, a pipe and a file and now a terminal, and the program holds one capability in each case.

BUGS