ShellGym is a background daemon that turns any Linux box into an interactive command-line trainer. The student works in a completely ordinary shell; a split screen (browser tab, or a future TUI) shows small, fast-changing assignments - reps, in the traditional gym sense - and reacts live as the student completes them.
This page covers architecture and internals. For the observation machinery in depth see detection.md; for the check commands see checks.md; for the content format see authoring-guide.md.
- Train fundamental Linux operations: navigation, files, permissions, redirection, pipes, processes, signals, networking basics.
- Fast scene changes; most reps take seconds to a minute.
- Zero interference with the student's shell: no prompt hooks, no wrappers, no PROMPT_COMMAND. All observation happens from the outside (procfs, the kernel proc connector, plain checks).
- Content-agnostic: the tool ships a format, not a curriculum. The
sample-linux-101path is the reference/test path. - Persistent progress: stop any time, resume days later.
- Repetition and review are intentionally external: the unit of repetition is the whole learning path - re-running it re-randomizes the parametric reps. There is no in-daemon skill tracking, review flagging, or scheduling.
Three independent subsystems, glued by a small event bus:
- Content engine (
internal/content) - loads a learning path from disk, defines the unit format (markdown + YAML frontmatter), resolves parameters (vars), renders markdown with custom components, strips solve scripts in live mode. Knows nothing about task execution. - Validation engine (
internal/engine) - executes init and check scripts, tracks per-task status with full run records (stdout, stderr, exit code, duration), provides built-in checks to task scripts over a unix socket, receiveshint_exitmessages. Knows nothing about markdown or rendering. - UI (
ui/) - theui.UIinterface is the pluggable frontend contract;ui/webuiis the built-in web UI (vanilla JS,<template>elements, no build step, embedded viago:embed);ui/tuiis reserved for a future terminal UI.
internal/state persists progress; internal/bus is a tiny pub/sub the
engine publishes events through and any number of UIs consume.
The daemon CLI is a typical spf13/cobra app:
shellgym serve --path <dir> [--addr 127.0.0.1:63636] [--state <dir>] [--run <dir>] [--user <login>] [--live] [--allowed-origin <origin>] [--auth-token-file <file>]- the daemon (the web UI listens on loopback only by default;--addr :63636opens it on every interface). POST requests and the event stream are refused when a browser sends them from another origin than the UI's own page;--allowed-originadds one for a proxy that rewrites the Host header. With a token (--auth-token-fileorSHELLGYM_AUTH_TOKEN), every route requires it: as?token=on the browser's first visit (traded for a cookie) or as anAuthorization: Bearerheader. Without one, the daemon logs a warning when it listens beyond loopbackshellgym solve --path <dir> [--api URL] [--unit <id>] [--auth-token-file <file>]- acceptance driver: spawns a real interactive bash on a pty (indistinguishable from a student terminal), walks the path via the daemon's API, types each task'ssolve:lines, reports PASS/FAIL per unitshellgym validate --path <dir>- lint + render without runningshellgym skills [name]- dump embedded authoring skills (markdown)shellgym check <name> [args...]- hidden; the target of the PATH shims task scripts call (wait_cwd,hint_exit, ...)
*--live is the student-facing mode: solve: blocks are stripped from the
on-disk unit files at startup and the debug API/UI is disabled.
A learning path is a folder:
paths/sample-linux-101/
path.yaml # id, title, description, shellUser
010.orientation/ # module (numeric prefix = order, stripped from id)
module.md # optional module intro scene
010.where-am-i/ # unit
unit.md # frontmatter + markdown body
img.png # unit-local static assets, referenced relatively
020.moving-around/
...
Units and modules are identified by their prefix-less folder names
(orientation/where-am-i); prefixes only encode order.
title: Change into a directory
labels: [ubuntu, debian] # distro filter (os-release ID/ID_LIKE); empty = any
requires: [systemd] # host capability filter; unmet -> unit shown but never activated
variant: scene=forest # per-attempt draw: one value per key, other values hidden
needs: [make-a-home] # units (same module) whose *state* this unit builds on
startOver: true # student-facing "Start over" button (re-runs init, keeps vars)
vars:
DIRNAME: { pick: [alpha, bravo, charlie] } # random choice, sticky per attempt
TOKEN: { shell: "head -c3 /dev/urandom | od -An -tx1 | tr -d ' \n'" }
PORT: { value: "8080" } # fixed value
PROJECT: { from: make-a-home.PROJECT } # inherited from a preceding unit
init: # ordered root scripts, run once when the unit activates
- name: create_tree
run: |
mkdir -p /tmp/gym/$DIRNAME
tasks:
chdir:
mode: edge # edge (default) | level
needs: [] # other tasks in this unit
check: |
wait_cwd "/tmp/gym/$DIRNAME"
hint: | # optional dynamic-hint script, stdout -> UI
echo "Your shell is still in $(shell_cwd)."
solve: | # hidden reference solution (typed by `shellgym solve`)
cd /tmp/gym/$DIRNAME- Vars resolve once per activation (persisted, stable across daemon
restarts), are exported into every task script, and interpolate into the
markdown as
${DIRNAME}.from:references let dependent units share randomized state. - Filtering:
labelsmatches the running distro;requiresmatches detected host capabilities (currentlysystemd,python3, andreadline- the optional command line watcher behindwait_line, which the daemon can only prove once its uprobe is installed, so the marking is re-applied after startup viaPath.ApplyCaps). Distro filtering drops units at load time; unmet requirements keep them browsable but unsupported. - Variants:
variant: key=valuetags a unit. The engine draws one value per key when the path is first served (state.Data.Variants, persisted, fresh per attempt) and marks units tagged with other valuesHidden. Hidden units stay in the model - activatable, listed by/api/path?hidden=1- soshellgym solvecovers every variant in one walk; the web UI never shows them. Dependencies (needs:,from:) may only point at units always shown alongside the dependent. - Init scripts run in order, as root, once per activation; they create
files as root and
chownto the student. Failures surface to the UI (author-facing) and block the unit's tasks; a later activation retries. - Task modes:
edge(default): the check runs (typically blocking on await_*built-in but any shell commands can be used) until it exits 0 once; then the task is completed forever. Failed attempts are recorded and restarted after a short delay.level: the check is polled (~1s); exit 0 = satisfied, non-zero = unsatisfied, may flip both ways until the unit completes.- Edge tasks may not depend on level tasks (load-time error). Tasks are
ordered dependency-first (topologically) - the order the UI shows and
solveexecutes.
- A unit completes when all edge tasks are completed and all level tasks are simultaneously satisfied. Completion is terminal.
- Dynamic hints, two mechanisms that can be mixed freely:
- a
hint:script runs after failing attempts; its stdout replaces the task's hint area live. It sees the unit env plusGYM_TASK_EXIT/STDOUT/STDERRof the failed check run. hint_exit [task] <message>inside acheck:pushes a message to the UI immediately (over the daemon's unix socket) and TERMINATES the check script with the distinct code 42 (the runner injects a shell function wrapping the binary). The task name defaults to$GYM_TASK.
- a
Standard markdown (goldmark, GFM) plus block components in two equivalent forms - inline attrs or an MDC-style YAML block:
::task{name="chdir"}
#active
Waiting for your shell to arrive in `/tmp/gym/${DIRNAME}`...
#completed
There you are.
::
::hint
---
title: Forgot the command?
---
Directories are entered with a two-letter command from the 70s.
::
::image{src="tree.png" alt="The tree"}
::
::taskrenders live task status (spinner/checkmark,#activeand#completedsections, hint area patched over WebSocket). If the unit has exactly one task,namemay be omitted.::hintrenders folded by default;::tip,::noteand::warnrender always-visible callouts (violet / neutral / amber) - tips for terminal-technique notes (Tab completion, history keys) that checks cannot verify, notes for asides, warnings for destructive or surprising behavior;::imageand plain![...]()serve unit-local files.${VAR}interpolation is applied server-side; task status/hints are patched live by the UI.
- Student shell discovery: processes owned by the configured user whose exe is a known shell and which have a controlling TTY. Checks operate on the most recently started one (or all).
- Exec watching: the daemon subscribes to the kernel proc connector
(netlink, needs
CAP_NET_ADMIN) and records every exec (pid, ppid, uid, tty, argv) into a bounded ring buffer. Only tty-attached processes of the observed user count as student activity; the daemon's own task scripts run undersetsid(no controlling tty) so they can never match themselves. - Built-in checks are PATH shims that exec
shellgym check <name>, which talks to the daemon over<run>/gym.sockwhere needed. Allwait_*checks block until met and accept--timeout <sec>or--now(instant, for level tasks). Full reference: checks.md. - Task scripts run as root via
bash -o pipefail, in their own session, with per-run timeouts and whole-tree kill; stdout/stderr/exit are captured without blocking on lingering background children. - Attempts: an edge check run that exits non-zero on its own (not
killed by the task timeout) is a rejected attempt. The engine then
moves the task's exec/line horizons to the current sequence numbers
(per task, in memory, reset on activation) and increments the task's
attempt count in
progress.json, exported to its check and hint scripts asGYM_CHECK_ATTEMPT.
The mechanisms (procfs scanning, the proc connector, direct state polling, the check API socket) are described in depth in detection.md.
A directory per path under --state (default /var/lib/shellgym):
<state>/<path-id>/
progress.json # unit/task statuses, attempt counts, vars,
# variant draw, timestamps (small, atomic
# tmp+rename writes)
runs/<module>__<unit>/<task>.jsonl # last N run records per task
Run records (potentially large stdout/stderr, truncated to 4 KiB per stream) append to per-task JSONL files compacted to the last 20 entries - they never bloat the progress document. The debug API reads them back.
- Vanilla ES modules + CSS, no framework, no build step; all markup lives
in
<template>elements in index.html, JS only clones and fills them. - Scenes = module intros and units, loaded lazily one at a time and slid horizontally to suggest movement along a path. On unit completion: a checkmark animation, ~1.4s pause, auto-advance (toggleable).
- Debug drawer (author-facing, hidden in
--live): per-task last runs with exit codes, streams, timings - fed by the run JSONL files. - Live updates over
WS /api/events(task status, hints, unit completion, init failures, run records).
GET /api/path[?hidden=1]- path tree + per-unit status + progress counters; units hidden by the variant draw are listed (flagged) only withhidden=1, and never countedGET /api/unit/{id}- rendered unit (HTML, tasks, vars)POST /api/activate/{id}- make unit current (resolve vars, run init). Returns 409 for a locked unit - one whoseneeds:dependencies are not all completed; there is no way around the gate. Browsing never activates: the web UI auto-activates only the next unit in path order and otherwise offers an explicit start button.POST /api/reset/{id}- forget unit progress (vars included), re-activate; the authoring resetPOST /api/start-over/{id}- the student's "Start over" button: forget unit progress but keep its vars, re-activate. Only for units withstartOver: true(403 otherwise); 409 for completed, locked, and unsupported unitsPOST /api/module-seen/{id}- mark a module intro viewedGET /api/module/{id}- rendered module introGET /api/debug/{id}- task run history (404 in live mode)GET /api/variants- variant keys with value pools + the current drawPOST /api/variants/{key}/{value}- switch the draw for one key (authoring preview; 404 in live mode)GET /api/status- distro, exec source, live flag, observed shellsGET /api/events- WebSocket event streamGET /unit-assets/{id}/...- unit-local static files
The daemon runs on a plain Linux host - e.g., a VM or an iximiuz Labs Linux playground -
as root, typically under systemd (systemd-run --unit=shellgym --collect ...). The student uses any terminal on the box; the UI is on port 63636,
bound to loopback unless --addr says otherwise (a platform that proxies the
UI from outside the box passes --addr :63636).
