# openloops.runbook

Run plans: the owner’s command-line manual tasks, planned by an agent, run without one.

A `manual-task` issue often ends in a shell command the owner is asked to paste. With
dozens of them, pasting becomes mindless, and the safety of running a command yourself
is lost. A **run plan** puts the judgement back (ADR-018):

- an agent (the `openloops-runbook` skill) re-checks each issue, orders the commands,
  has an independent reviewer rate each one, rewrites them to lower the risk, and
  writes a [`Plan`](#openloops.runbook.Plan);
- the owner approves the plan **once**, by its [`plan_hash()`](#openloops.runbook.plan_hash);
- [`openloops.runbook_run`](openloops.runbook_run.html.md#module-openloops.runbook_run) runs exactly the approved commands, with no model in the
  loop, and closes each issue only on its passing check (ADR-015).

This module is the pure part: the plan record, its hash, the one grammar of an issue
reference ([`parse_ref()`](#openloops.runbook.parse_ref)), and the checks a plan must pass before it can be approved
([`validate()`](#openloops.runbook.validate)). The risk reading of a command is [`openloops.command_risk`](openloops.command_risk.html.md#module-openloops.command_risk). No I/O,
no network, no model.

```pycon
>>> p = Plan('p', '2026-11-30', steps=[Step('s1', 'o/r/1', 'gh issue view 1 -R o/r')])
>>> validate(p), p.steps[0].issue
([], 'o/r#1')
```

### Functions

| [`parse_ref`](#openloops.runbook.parse_ref)(ref)                         | `(owner, repo, number)` from `o/r#3`, `o/r/3` or an issue URL.                           |
|-----------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------|
| [`plan_hash`](#openloops.runbook.plan_hash)(plan)                        | sha256 over what runs: each step's `HASHED_FIELDS`, in order.                            |
| [`validate`](#openloops.runbook.validate)(plan, \*[, discussed, check]) | What is wrong with *plan*, as messages; `[]` when it may be approved and run.            |
| [`counts`](#openloops.runbook.counts)(plan)                           | The one-line summary: steps that run unattended, need hands, wait for a yes; held items. |

### Classes

| [`Step`](#openloops.runbook.Step)(id, issue, command[, cwd, check, ...])       | One command of a plan, for one issue.                                 |
|----------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------|
| [`Plan`](#openloops.runbook.Plan)(id, created[, steps, discuss, skipped, ...]) | A run plan: the steps that will run, and everything kept out of them. |

### *class* openloops.runbook.Plan(id, created, steps=list(), discuss=list(), skipped=list(), made_by='', title='', page='', schema=1)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

A run plan: the steps that will run, and everything kept out of them.

`discuss` holds what is not in `steps` because it needs the owner’s word
(`{issue, why, recommendation, command}`); `skipped` holds what needs nothing
(`{issue, why}`: already done, closed, no command). `page` is where the owner reads
it (a link). Only `steps` is hashed: the approval binds to what runs.

### *class* openloops.runbook.Step(id, issue, command, cwd=None, check=None, expect=None, after=list(), close=True, timeout=900, interactive=False, level='show', reasons=list(), review=None, rewrites=list(), note='')

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

One command of a plan, for one issue.

`command` runs as written, in `cwd` (`~` expanded), with `bash`. `check` is
the read-only command whose exit 0 means the issue’s ask is done (the issue’s
`**Verify:**` field, or one the planner wrote, which the owner approves with the
plan); it is run before the command (already done: skip it) and after. `expect` is
a regular expression the command’s output must contain. `after` names the steps
that must have succeeded first. `close` says this is the issue’s last step, so a
passing check closes the issue. `rewrites` records each risk-lowering change as
`{before, after, why}`. `review` is the independent reviewer’s
`{level, reasons}`; `level` and `reasons` are the combined verdict.

### openloops.runbook.counts(plan)

The one-line summary: steps that run unattended, need hands, wait for a yes; held items.

```pycon
>>> counts(Plan('p', 'd', steps=[Step('s1', 'o/r#1', 'npm login', interactive=True)], skipped=[{}]))
{'runs': 0, 'hands': 1, 'decide': 0, 'skipped': 1}
```

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`int`](https://docs.python.org/3/builtins/functions.html#int)]

### openloops.runbook.parse_ref(ref)

`(owner, repo, number)` from `o/r#3`, `o/r/3` or an issue URL. One grammar.

```pycon
>>> parse_ref('acme/widget#12'), parse_ref('acme/widget/12')
(('acme', 'widget', 12), ('acme', 'widget', 12))
>>> parse_ref('https://github.com/acme/widget/issues/12')
('acme', 'widget', 12)
```

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`int`](https://docs.python.org/3/builtins/functions.html#int)]

### openloops.runbook.plan_hash(plan)

sha256 over what runs: each step’s `HASHED_FIELDS`, in order.

```pycon
>>> p = Plan('p', '2026-11-30', steps=[Step('s1', 'o/r#1', 'true')])
>>> h = plan_hash(p); len(h)
64
>>> p.steps[0].note = 'reworded'; plan_hash(p) == h
True
>>> p.steps[0].command = 'true && false'; plan_hash(p) == h
False
```

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

### openloops.runbook.validate(plan, \*, discussed=(), check=<function risk_floor>)

What is wrong with *plan*, as messages; `[]` when it may be approved and run.

The floor is recomputed here, from the text, by `check` (ADR-015 §3’s seam; the
default reads the text, [`openloops.command_risk.risk_floor()`](openloops.command_risk.html.md#openloops.command_risk.risk_floor)), so a plan that
claims `run` for a deploy is caught. A `discuss` step is allowed only if its id is in *discussed*
(the owner said yes to that step by name); `refuse` never is.

```pycon
>>> ok = Plan('p', 'd', steps=[Step('s1', 'o/r#1', 'gh issue view 1 -R o/r', check='true')])
>>> validate(ok)
[]
>>> bad = Plan('p', 'd', steps=[Step('s1', 'o/r#1', 'rm -rf ~/x', after=['s9'])])
>>> for m in validate(bad): print(m)
s1: needs the owner's yes by name (irreversible: deletes files)
s1: runs after s9, which is not an earlier step
```

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]
