Talking back to mdsh

A block can talk back to the run that started it with mdsh ctl: set a variable for the blocks after it, read one back, ask you a question, or say how far along it is. And a run can write an audit log, so a run that a block changed can be reviewed afterwards.

mdsh ctl can’t add, edit or run blocks, and it can’t change prose. You can still read the file and know what can run; a variable it sets can only decide whether a written block runs, through a run= condition, and the audit log records that.

mdsh ctl

```sh {#pick}
latest=$(git describe --tags --abbrev=0)
mdsh ctl set release "$latest"
mdsh ctl status "found $latest"
```

```sh {#ship run={{$MDSH_VAR_release}}}
./ship "$MDSH_VAR_release"
```
CommandDoes
mdsh ctl set NAME VALUESets $MDSH_VAR_NAME for blocks that start afterwards. - as the value reads stdin
mdsh ctl set --secret NAME VALUEThe same, and keeps the value out of the file and the log
mdsh ctl unset NAMERemoves a variable that set made
mdsh ctl get NAMEPrints $MDSH_VAR_NAME as it is now, including values set after this block started
mdsh ctl ask QUESTIONAsks you for text and prints the answer. --choice a,b,c limits the answers, --default V answers when nobody can, --secret hides the typing
mdsh ctl confirm QUESTIONAsks you yes or no; the exit code is the answer. --default yes or --default no answers when nobody can
mdsh ctl status TEXTShows progress for this block. --step N --of M adds a count. --state working|blocked|done|error says what the block is up to, as its card’s status; with blocked, --kind permission|question|auth says what it waits for

NAME is letters, digits and _, without the MDSH_VAR_ in front.

  • set changes the variable for blocks that start afterwards, in every env. A block that is already running keeps the environment it started with; it can read the new value with get.
  • run= and other conditions are checked just before their block runs, so they see every set made before it.
  • A name that a textbox or choice binds with var= belongs to that input, and set on it is refused. The exception is a nosave input: set fills it for this session, and the file keeps its body.
  • A --secret value reaches blocks and conditions as usual. A placeholder shows it as ***, and write-back stores ***, never the value.

The status line goes to stderr under mdsh; it is never written to the file. With --state, the block’s status shows in its card in mdsh -i and mdsh-gui while it runs, and mdsh and mdsh -i pass it to the terminal they run in under the block’s name, the way a program on a tty block’s terminal reports with the Program Status Protocol (OSC 7501):

mdsh ctl status --state blocked --kind permission "Apply 14 resources?"
read -r answer
mdsh ctl status --state working "applying"

Questions

```sh {#deploy}
env=$(mdsh ctl ask --choice staging,prod --default staging "Deploy where?")
mdsh ctl confirm --default no "Deploy to $env now?" || exit 0
./deploy "$env"
```

mdsh asks on the terminal and mdsh -i in the block’s card. Flags go before the question. Pressing enter takes the --default. When nobody can answer (a report, or a run with no terminal), the --default answers; with none, the call exits 4 and mdsh says “no answer” with the block. --yes answers yes to a confirm that has no default. A question is withdrawn if its block ends first, as when its timeout= does, and the time spent waiting counts toward that timeout. mdsh-gui can’t ask these yet, so there the --default answers.

Exit codes

CodeMeans
0Done, or confirm answered yes
1confirm answered no
2Refused: bad arguments, a name an input binds, or a token that has stopped working
3Not running under mdsh
4No answer: ask or confirm with nobody to answer and no --default

status is the exception to 3: outside mdsh it does nothing and exits 0, so a script that reports progress still runs on its own.

How it reaches mdsh

At the start of a run, or of a session in mdsh -i or mdsh-gui, mdsh opens a socket in a new private temp directory, and every block gets two variables, whatever its env shares:

  • $MDSH_SOCK, the socket’s path.
  • $MDSH_TOKEN, a token for this run of this block. It stops working when the block ends, so mdsh knows which block made each call, even from a process the block left in the background.

Only your own user can call it. On Windows there is no socket yet, so mdsh ctl exits 3 there.

The audit log

mdsh --audit run.jsonl deploy.md
jq -c 'select(.event == "block.end")' run.jsonl

--audit FILE writes one JSON object per line, in order, for the whole run, to a file only you can read. Each has seq, a time t and an event, with block and env when they apply.

EventRecords
run.start / run.endThe file and a SHA-256 of it, the mode, flags and arguments, the mdsh version; then the exit code
env.startAn env’s shell starting, and the names share let through (names, not values)
block.start / block.endA block running, then its exit code and time taken. For an http block, also the method, the URL as written, the status and each check’s result, never a header or a body
block.skipA block not run, and why: skip, the run= condition and the value it saw, once, --only, a failed depends=
ctlEvery mdsh ctl call, refused ones too, with the block, arguments and result
varA variable changing, with the old and new values and what changed it
askA question from ask or confirm, with its id, the answer, and who answered: user, default, yes-flag, or none
writemdsh saving the document

A secret value never appears in the log. Where it would go, the log has {"secret":true,"hmac":"…"}, keyed with a new random key each run, so you can tell whether two secrets in one run matched without being able to guess a short one.

--audit works for mdsh, -w, -o and reports. It is refused with -i for now, and mdsh-gui doesn’t save a log yet.