openloops.runbook_run#

Run an approved run plan: exactly the approved commands, no model, close on the check.

ADR-018. A plan (openloops.runbook.Plan) is written by an agent and approved by the owner once, by its hash. This module is the executor, and it holds no judgement:

  • it refuses a plan with no approval for its current hash, an approval older than DFLT_APPROVAL_HOURS, or a plan openloops.runbook.validate() rejects (the

    risk floor is recomputed from the text, so a plan cannot talk its way past it);

  • it runs each step’s command exactly as written, in order, with a time limit, its output captured to a local log (mode 0600) and never posted anywhere;

  • before a step it runs the step’s check, and skips the command when the check already passes, so running a plan twice is harmless;

  • a step whose dependencies did not succeed is skipped; a step that needs a person at a terminal (Step.interactive) runs only when the owner started the run from one (tty=True), on his own terminal and with no time limit (DFLT_ATTENDED_TIMEOUT): he is there, and Ctrl-C stops the run (what succeeded stays done; the same line resumes);

  • at a terminal, say= is told what each step is before it runs (its id, issue and note: what to have ready) and how it ended after, and ask= is asked before going on past a step that failed, so no step’s end runs silently into the next one’s prompt;

  • after an issue’s last step it hands the outcome to openloops.closer, which closes the issue only on a passing check (ADR-015).

ol runbook run PLAN --only-checks needs no approval: it runs no command, only the checks, and closes what already passes (the closer’s four conditions still apply).

Nothing in the core imports this module. run_step() is the one place a step’s command is run; ol do (ADR-015 §2) is meant to call it too.

Functions

runbooks_store([root])

~/.local/share/openloops/runbooks/: plans/, approvals/, reports/.

save_plan(plan, *, store)

Write plan under plans/<id>.json; returns its hash.

load_plan(ref, *, store)

A plan by id, or by the path of a plan file.

approve(plan, *, via[, discussed, now])

Record the owner's approval of plan's current hash.

approval_of(plan, *, store[, now, hours])

The live approval of plan's current hash, or None.

plans(*, store[, now, include_ran])

The stored plans, newest first, each {plan, title, hash12, state, counts, page, created, issues, approve} (issues: every issue it covers; approve: approve_line()).

run_step(step, *[, runner, tty, log_path, ...])

Run one step's command; ok when it exits 0 and its output matches expect.

run_task(step, *[, runner, run_check, tty, ...])

One task step end to end, short of reporting: check first, then run.

run_plan(plan, *, store[, runner, ...])

Run plan's approved steps, in order; return the run report (also stored).

shell_runner(command, *, cwd, timeout, tty, ...)

Run command with bash; return (exit status, output).

approve_line(plan_id, hash12)

The one command that approves a plan's current text and runs it at the terminal.

openloops.runbook_run.approval_of(plan, *, store, now=None, hours=48)[source]#

The live approval of plan’s current hash, or None.

None too for an approval of another plan with the same steps, one dated in the future or without a time zone, and one older than hours.

Return type:

dict[str, Any] | None

openloops.runbook_run.approve(plan, *, via, discussed=(), store, now=None)[source]#

Record the owner’s approval of plan’s current hash.

via says how the owner approved ("terminal", "board:<event id>", "session: <the owner's words>"); discussed names the discuss-level steps the owner said yes to by name. A plan that does not validate is not approvable.

Return type:

dict[str, Any]

openloops.runbook_run.approve_line(plan_id, hash12)[source]#

The one command that approves a plan’s current text and runs it at the terminal.

>>> approve_line("p1", "abc123def456")
'cd ~ && ol runbook-run p1 --tty --approve abc123def456'
Return type:

str

openloops.runbook_run.load_plan(ref, *, store)[source]#

A plan by id, or by the path of a plan file.

Return type:

Plan

openloops.runbook_run.plans(*, store, now=None, include_ran=False)[source]#

The stored plans, newest first, each {plan, title, hash12, state, counts, page, created, issues, approve} (issues: every issue it covers; approve: approve_line()).

state is waiting (no live approval for its current hash: it needs the owner), approved (approved, no run since), or ran (a report exists since the approval). ran plans are left out unless include_ran.

Return type:

list[dict[str, Any]]

openloops.runbook_run.run_plan(plan, *, store, runner=<function shell_runner>, run_check=<function default_run_check>, close=None, log_dir=None, tty=False, only_checks=False, only=None, dry_run=False, now=None, say=None, ask=None, attended_timeout=None)[source]#

Run plan’s approved steps, in order; return the run report (also stored).

say (the owner’s terminal) is told what each step is before it runs and how it ended after; ask is asked, after a step fails, whether to go on: q holds the rest (rerun the same line later: what succeeded does not run twice).

only limits the run to some step ids (a pilot): the others are held, and an issue with a held step is not reported (it closes when its last step has run). only_checks runs no command, needs no approval, and closes only on the issue’s own Verify line (a check the planner wrote needs the owner’s approval of the plan). dry_run runs nothing and writes nothing. close defaults to openloops.closer.report() with the owners and login resolved once per run; log_dir to ~/.local/share/openloops/runbooks/logs.

Return type:

dict[str, Any]

openloops.runbook_run.run_step(step, *, runner=<function shell_runner>, tty=False, log_path=None, attended_timeout=None)[source]#

Run one step’s command; ok when it exits 0 and its output matches expect.

Returns {status, ok, why, tail}; tail is the end of the output, for the local report only. A step that needs the owner’s hands, run at his terminal, gets attended_timeout instead of its own limit (default: none).

Return type:

dict[str, Any]

openloops.runbook_run.run_task(step, *, runner=<function shell_runner>, run_check=<function default_run_check>, tty=False, log_path=None, checks_only=False, attended_timeout=None)[source]#

One task step end to end, short of reporting: check first, then run.

result is already-done (its check passes: the command did not run), not-run (checks_only), needs-terminal (an interactive step without tty), ok or failed. ADR-015 §2’s ol do is this plus openloops.closer.report().

Return type:

dict[str, Any]

openloops.runbook_run.runbooks_store(root=None)[source]#

~/.local/share/openloops/runbooks/: plans/, approvals/, reports/.

Return type:

MutableMapping[str, str]

openloops.runbook_run.save_plan(plan, *, store)[source]#

Write plan under plans/<id>.json; returns its hash.

Return type:

str

openloops.runbook_run.shell_runner(command, *, cwd, timeout, tty, log_path)[source]#

Run command with bash; return (exit status, output).

Output goes to log_path (created 0600). With tty the step runs on the owner’s own terminal, as if he had typed it: his keys reach it (a paste, a one-time code, a y), and his Ctrl-C reaches it and this process (which then stops the run). Its output is shown there and not kept. Without tty input is closed, so a step that waits for input fails instead of hanging, and its output is captured. None is a step that could not run, ran out of time, or was stopped with Ctrl-C (timeout=None waits for as long as it takes).

Return type:

tuple[int | None, str]