Map: repl / loop
ral/src/repl.rs is the module-tree root and exposes one verb,
run_interactive, which boots a Session and drives it to an ExitCode
(startup).
The session
The REPL is a front-end that holds no Shell: it boots an
IdentityTransport from the repl installer and speaks only the
engine protocol — every evaluation a dispatch,
every read of engine state a probe — so what it can do is exactly what the
protocol carries (engine-protocol).
session::Session (session.rs) owns the transport, the REPL’s Host
(ReplHost), the boxed `Frontend`, a pending buffer
queued for re-edit, the probed terminal, the exit code, and the
TerminalClaim — last, so it drops last (below). There is
no job table: ral does not suspend
(ral-does-not-suspend).
Session::bootis two-staged. Stage one isIdentityTransport::bootwithstartup::engine::INSTALLERSand anAttachthat configures nothing — login environment adoption has already run inmain::run; thereplrecipe does what precedes rc (below). A refusal prints its sentence and exits 2. Stage two is the host’s first dispatch:Program::Hookon theSession "boot"hook, the_ral-bootbuiltin, applied toBoot {no-rc, recursion-limit, capabilities}(ral/src/boot_builtin.rs).capabilitiesholds absolute paths:Boot::profilesanchors each argument at the invocation cwd before the builtin runs, since the rc and earlier profiles maycdfirst, and the builtin refuses a relative one before sourcing anything.boot_builtin::settlereads itsEnding:Okcarries the rc’sRcSettings, a--capabilitiesfailure isErrstatus 2, an rc’sexit NisExited(N), and either ends the session there. The host then applies--surfaceover the rc’s choice, keeps the output theme, and dispatchesSession "startup"if the rc registered one.runloopsiterateuntil it returnsFlow::Break— a frontendEnd, anexit, or a severed transport.iterateis one cycle: break if thesession-endedreading names a cause (SIGTERM/SIGHUP or Ctrl-\ cancelled the durable root — cancellation is one-way, so exit with the cause’s code rather than deal an unusable prompt),process::clearany residual interrupt, write the terminal title from thecwdreading, render the prompt,read, and act on theRead.Read::Lineadds to history and evaluates;Read::Editbecomes next iteration’spending;Read::Interruptjust continues, nothing being in flight at the prompt;Read::End(code)records the exit status and breaks. Afterreadreturns the session flushes deferred plugin diagnostics, the one boundary, and checkssession-endedagain before interpreting input.readis handed the transport, the prompt, the pending buffer, and on Unix the exclusiveInputreader.- The Unix ambient forwarder first calls
ControlSender::hear, cancelling the engine’s scopes synchronously, then endsInputon a root cause. Its persistent shutdown flag and repeated thread-directed interrupt cover the interval between the root check and the blocking read; the frontend returns through normal cleanup. The signal thread never exits the process or restores its terminal. evalruns one trimmed line throughexec::step, recording anexitcode sorunbreaks cleanly.- Teardown — the
workersreading, history flush, a one-time notice naming any worker stillrunningorstopping(host::teardown_notice, overWorkerRows), andral_core::authority::sandbox::teardown_session(deletes the session’s per-projection AppContainer profiles on Windows; a no-op elsewhere) — lives inDrop for Session, so it covers a panic unwinding through the ownedSessionas well as the orderly exit.
session/boot.rs is the process-level setup that precedes the engine and
the frontend the rc settles on after it: setup_signals (the whole Unix
disposition table in one place — SIGINT Interrupt, which raises the
foreground interrupt and nothing else, SIGQUIT Abort, SIGTERM/SIGHUP
Shutdown, SIGTSTP/SIGTTOU/SIGTTIN/SIGPIPE Ignore — every entry set through
core’s process::install(sig, Disposition), the one place that snapshots the
inherited SIG_IGNs before the first change so reset_child_signals can hand
them on: the nohup rule), claim_terminal (run after
SIGTTOU is ignored, since its tcsetpgrp runs from a group not yet in the
foreground, and before SIGTTIN is: it parks the shell on SIGTTIN until it is
foregrounded (at most 64 parks, then an error asking whether ral was started in the background of a shell that has since exited; SIGTTIN is set through core’s install(SIGTTIN, Disposition::Default) first), so ral & does not seize the terminal from a parent shell’s
current job; it answers a TerminalClaim naming the group it left, if it
left one, which hands the terminal back on drop — else a parent doing no job
control, vim’s :sh or su, stops on its next terminal read),
setup_panic_hook (restore the
pre-raw terminal state — termios on Unix, console mode on Windows — then write
a crash log, a new file crash-<ts>-<random>.log in ral’s state directory
($XDG_STATE_HOME/ral), held as an OwnDir from install time so a changed
HOME cannot redirect it; with no state base, the panic message goes to
stderr), and create_frontend, which hands the line editor its history file,
$XDG_STATE_HOME/ral/history (config::dirs_history; none without a state
base, and then no history).
The boot, engine-side
The repl installer’s recipe (startup::engine::repl_runtime and repl_root)
boots the REPL surface — the batch surface plus the [[map/repl/plugins|_ed-*
editor builtins]] and the _external-editor builtin
(frontend) — through boot_shell, so the typechecker and
the runtime see one surface by construction; then exit hints, the
interactive flag, $TERMINAL, the external-editor session hook, and the boot
builtin’s registration. The umask
is inherited from login(1) or PAM unless the rc sets umask:.
The boot builtin (_ral-boot, one-shot: it unregisters its own hook) does the
rest in a run, since startup files evaluate ral: source_rc_file
(config/source.rs — the rc only; Boot has no login field), the CLI’s
--recursion-limit after the rc, --capabilities through
shell::load::profile::apply_session_profiles (the user’s ceiling is the last word,
and a session frame it pushes survives the builtin’s run), and
install_default_prompt (register the default Session/"prompt" hook,
{ return "❯ " }, only when the rc registered none). It answers
the rc’s RcSettings {edit-mode, bell, surface, theme, startup} — the only
part of the rc the host needs; everything else the rc configures lands on the
engine’s shell.
The rc ends in all twelve options written out, following SPEC §15.3.
source_rc_file uses shell::load::extend_session against the live session
and typecheck::table::declared(Form::Rc). The file’s registered FileId
keeps functions and persistent handlers naming their source for the session.
- During generation,
check_optionschecks each written key, just as atwithinandgrant. A bound record, spread or computed tail is T0077; nested tables must be record literals (T0079). Unknown keys are T0076. - Every rc key is required, including all keys of nested
limits:andtheme:. Each incomplete table produces one T0081 listing its missing required keys;()and an empty rc report the missing top-level keys. Every rc key has aKey::blankspelling, including`inheritfor each nestedlimits:andtheme:key, so missing keys have pasteable defaults hints at every depth.DEFAULT_RCwrites them all.`inheritkeeps what ral or the system already has;`set voverrides it. - Parse and type failures are reported and skipped before any phrase runs.
errfmt::report_failed_loaddrawsError::uncompiled’s caret report (diagnostics) and names the fileskipped, since it does not compile. Runtime failure instead reportssourcing stopped at the error above; completed bindings and effects stay.exit Nends the session with status N; other failures let boot continue. - Syntax guarantees result shape and keyset.
rc_configasserts the record carrier;RcPlan::decodechecks field value shapes and domains, reading the shell only. It vets prompt and startup blocks withvet_hookand persistent handlers withHandlerEntry::vet. Only after validation doesRcPlan::applyinstall the configuration. A plugin load failure is reported and skipped; anexitpropagates.
A startup: `set { … } registers the Session/"startup" hook, and the
host dispatches it under Denied terminal authority — a fresh frame whose
lets do not leak. startup: `inherit registers nothing.
The rc’s env: sets environment entries only. Top-level lets above the
return install lexical values and functions with their checked, generalised
schemes. Persistent command handlers come from its handlers: field,
installed by the host after the rc unit has run and been validated.
No statement or builtin value changes persistent frames;
interactive helpers use let, scoped interception uses within [handlers: …].
Install-time arm checking yields only a verdict, not a stored handler scheme.
The next unit imports handled names for diagnostics; its head types and gates
come only from Σ and lexical bindings.
The rc plugins: map is the only plugin-loading entry point. Plugins load
at startup; adding one requires a new session.
umask: `set mask takes one to four octal digits as a string, at most
0777, and sets the process mask before plugins load; `inherit keeps
it. Each limits: key (open-files, core-bytes, stack-bytes) likewise
writes `inherit or `set n. Runtime validation checks non-negative
values against ral’s hard limits, then stores child soft limits without
changing ral’s own limits. Grant limits apply after these session limits and
win. Windows accepts inheritance and refuses only actual settings.
theme: writes value-prefix and value-color at the same tags; colour
names remain a runtime domain check.
Every evaluation is a dispatch
Each line, prompt, and hook is one Run through dispatch_to_report with
the REPL host, and the engine’s run entry compiles, typechecks, and runs it against
the live session (a run, end to end).
exec.rs::step is the per-line entry. line_run builds the Run —
Program::Source(line), origin: SourceOrigin::named("<stdin>"), GrantStack::root(),
no wall, RunIo::Inherit, RequestedTerminalAccess::Leased,
RunStdin::Inherit, last: false — and ReplHost::dispatch sends it. Around it the
lifecycle hooks run as dispatches of their own. Before, pre_exec folds the
plugins’ pre-exec hooks over the typed line in load order, each dispatched
with {src}: a Str result is the next hook’s line and, last, the line
line_run carries; (), an exit, a fault or a wrong-shaped value
(plugins) leaves it. After, fire runs chpwd
with {old, new} if the cwd readings, taken before and after the line, are
both present and differ (a working directory that cannot travel as text reads
as none) — the session is the working directory’s outermost handler, so a cd
a within [dir:] undid fires nothing — then post-exec with
{src, status}, src being the line that ran — for every line, a static
failure’s status included. An Err(Severed) prints the cause and ends the
session.
It matches the one flat Report:
Static— a parse, type, or host failure that never reached evaluation; itsrenderedis the whole caret report, printed verbatim.Ran(Ran { ending, rendered, … })—Okprints viaprint_result;Err,StoppedandPanicprintrenderedverbatim (a closed-stdout stop renders nothing). A non-data result isErr, status 1; a run panic isPanic, status 101, with its checkpoint restored.Exited(code)ends the loop with its byte.Report::codesupplies the status for every report; the process edge isstd::process::ExitCode::from(u8).Stoppedis a cancellation, not a process stop: ral does not suspend (ral-does-not-suspend).Faulted— only a lost report: print its diagnostic; what ran is unknown.
The same entry backs every host program the REPL holds as a value: the prompt
body, an rc startup block, and a plugin hook are registered hooks run as
Program::Hook runs (plugins), whose registered policy
decides capture, terminal authority, and whether the run is taken aside. A
hook run is no ready boundary, and its fault comes back labelled with the
hook’s name (plugin 'p' hook 'h', prompt) by the engine; an exit from a
hook is ignored.
The host
host.rs::ReplHost is the REPL’s Host, and the one place every hook
dispatch goes through (dispatch, run_hook → HookResult {outcome, captured, ctx}, the Outcome one of Settled(value), Exited,
Faulted(text), Walled(text), Stopped(text), the text trimmed and
labelled once there). This Outcome is a hook-policy coarsening, not an
ending: it folds Err, Panic, static failure and report loss into Faulted.
enquireanswers the three classesenquiry.rstypes once for both ends:`repl-editor(anEditorOp—get,set,set-lbuffer,insert,push,accept,ghost,highlight,history,state-get,state-set— applied to the editor context installed for the dispatch in flight, refused in its own words when none is),`repl-plugin(loadedwith a manifest,unloadedwith a name — told to the plugin runtime, which may refuse) and`repl-clipboard(aClipboardReq, copied by the host).surfaceand theDeferredSinkimpl render what the engine surfaces: a`watchline (ral/src/surface.rs, shared with batch) through the frontend’s printer above the prompt, a`noticedim, an`observedobservation skipped by choice (the ral front-ends render no audit), and anything else dropped with a note naming its class.DoneEvents are held insettled: Mutex<Vec<DoneEvent>>untilSession::iteratecallsannounce_settledbefore drawing the next prompt. One workers probe gates the drained events: only adonefor whichDoneEvent::unclaimed_in(&rows)holds gets a faintnote: worker #id (title) …, worded by core’sDone::said. Claimed/absent rows or a failed probe announce nothing. A later claim cannot retract an earlier notice; batch silently consumes alldoneevents (endings).prompt_faultprints a render’s faults, joined, once while they repeat.
The selectable frontend
The loop drives a boxed Frontend, chosen after boot from the rc’s
Surface:
Surface::Minimal— the canonical-stdin editor.Surface::Readline(the default) — the rustyline editor.
create_frontend resolves it: the capability gate forces the minimal editor
on a dumb terminal whatever was asked, otherwise the surface preference
decides. The preference is set by the --surface flag (CLI wins)
or the rc surface: key. The two implementations, the Frontend trait,
and completion live in
frontend.
Prompt, rc, theme
prompt.rs—renderfirst announces still-unclaimed completions, then dispatches the registeredSession/"prompt"hook, registered with capture: its return value is the prompt, a returned unit falls back to its captured stdout. The prompt body readsuserandcwd, the ambient builtins, directly. Pluginprompthooks fold over the result, each a dispatch registered with capture and read through the sameprompt_text; the terminal title is written separately. A failing prompt — one whose value cannot cross included — falls back to the default❯(a plugin’s, to the prompt so far); the faults of one render print together throughprompt_fault, when they differ from the last render’s, and the session survives so the user can rebind it.config.rs— the rc’s twelve keys (env,prompt— registered as theSession/"prompt"hook —handlers,edit-mode,bell,surface,recursion-limit,plugins,startup,theme,umask,limits) are declared once, inForm::Rc’s table, andRcPlan::decodejudges them all engine-side, inside the boot builtin, beforeRcPlan::applycommits to the shell and theRcSettingsit answers. Decoder refusal applies no configuration; effects and bindings from evaluating the rc remain. The first-run skeleton is written withcreate_new, so a concurrent rc is never truncated. A wrong type at one of the scalar keys is the table’s static failure, above;decodecatches those keys’ further shape rules (e.g.edit-modemust be'emacs'/'vi', not just aString).theme.rs—OutputTheme(thevalue-prefix, default"=> ", and an optionalvalue-color, default yellow) governs value rendering; held bySessionand passed tostep. The checker requires both keys, each at`inheritor`set v; an unknown key is a static error.OutputTheme::from_mapvalidates colour names at runtime.errfmt.rs— the REPL-styled plugin notices (the breaker’s disable notice, a plugin warning), beside core’s full ariadne renderer, andreport_failed_load, the one printer for a failed rc, profile, or plugin load: the report, then aral: WHAT: …line saying how far it got.cursor.rs— the zsh-style partial-line marker, written before each rustyline prompt. On Unix it isPROMPT_SP’s fill-and-erase —%, thenwidth − 1spaces, then\r\x1b[K— which leaves the%only when the fill wrapped off a partial line, and asks the terminal nothing, so keys typed while a command ran reach the editor intact. On Windows the console API, which likewise reads no input, reports the cursor column directly.