Metadata-Version: 2.4
Name: ashmatics-playbook-grammar
Version: 0.3.0
Summary: FORGE playbook interior grammar, save and publish validators, and content hash (ADR 070 D4)
Author-email: JF Kalafut <john@asherinformatics.com>, Asher Informatics PBC <info@ashmatics.com>
License: Apache-2.0
Project-URL: Homepage, https://gitlab.com/ashmatics/platform/ashmatics-aigov-framework/-/tree/main/tools/playbook-grammar
Project-URL: Repository, https://gitlab.com/ashmatics/platform/ashmatics-aigov-framework
Project-URL: Bug Tracker, https://gitlab.com/ashmatics/platform/ashmatics-aigov-framework/-/issues
Keywords: ashmatics,forge,playbook,ai-governance,healthcare-ai
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Healthcare Industry
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: pyyaml>=6.0.0
Provides-Extra: dev
Requires-Dist: pytest>=7.4.0; extra == "dev"
Requires-Dist: pytest-cov>=4.1.0; extra == "dev"
Requires-Dist: ruff>=0.1.0; extra == "dev"

# ashmatics-playbook-grammar

**Version:** 0.3.0
**Last Updated:** 2026-11-03

> See [CHANGELOG.md](CHANGELOG.md) for version history.

The FORGE playbook interior grammar, its save and publish validators, and the content hash
stamped at publish. One copy, imported at a pinned version by both services that need it: the
KB runs it when Asher publishes a playbook template version, and FORGE (coreapp) runs it when it
pins a copy and when a site authors its own playbook (coreapp ADR 070 D4).

Moved from coreapp with no change in behavior. Function names and error messages are coreapp's.
Since 0.2.0 the package also holds the rules FORGE owns that the publish validator runs, so the
KB publishes a template with exactly the checks coreapp runs, over the data the KB holds.

## Install

One runtime dependency: pyyaml. Nothing here imports Azure, a database driver or Django.

```bash
pip install ashmatics-playbook-grammar      # from PyPI
```

The package lives in the framework repo at `tools/playbook-grammar/`. It has no git tags of its
own; the framework's `vX.Y.Z` is the only tag. To work on it, use a checkout:

```bash
git clone https://gitlab.com/ashmatics/platform/ashmatics-aigov-framework.git
cd ashmatics-aigov-framework/tools/playbook-grammar
uv sync --extra dev        # or: pip install -e ".[dev]"
uv run pytest
```

`ashmatics-aigov-tools` depends on it and re-exports it as `ashmatics_aigov_tools.playbook_grammar`.

## Use

```python
import ashmatics_playbook_grammar as pg

pg.validate_template_document(document)          # save: raises InvalidPlaybookDocument
pg.validate_for_publish(document, base_practices,
                        playbook_class="governance",
                        lookups=my_lookups)       # publish: the CHAR reads come in lookups
pg.content_hash(document)                         # the hash stamped at publish
pg.load_grammar("1.2")                            # one grammar version, as a dict

# Grammar 1.3: org unit band and target tailoring points
pg.resolve(document, context)                     # the document every reader sees
done = pg.resolve_with_record(document, context)  # the same, and what it removed
done.document, done.stamp()                       # stamp() is what compile writes
```

`context` is coreapp's `PlaybookContext.to_dict()` (`unit_level`, `level_source`, `band`,
`target_ids`, `target_names`). The resolver reads `band` and `target_ids` only.

**Grammar 1.3 adds tailoring points** (coreapp ADR-069 Amendment 1, A4). The terms:

- A **tailoring point** is a marked place in a playbook template where the words, a default,
  who decides, or whether a step exists can change by org unit band or by target.
- **Tailored text** is the version the author writes for one band or target, beside the
  enterprise text.
- To **tailor** is what FORGE does: it applies the tailoring points from the context. The
  customer never sees or answers one.
- A **tailored playbook** is the result for one unit.

Tailoring adapts one playbook. An overlay adds to it (ADR-069 D2, not built). A variant
replaces it (ADR-069 D3).

In the document, a tailoring point is a `choice` step that FORGE applies from the context:
`answered_by: {"context": "org_unit_band"}`, with the four org unit bands as outcomes, or
`{"context": "target:acr_arch_ai"}`, with `on` and `off`. A node, a setting or a lane may
carry `by_choice: {<tailoring point id>: {<outcome>: {<field>: <value>}}}`: that is its
tailored text and defaults for an answer. The resolver prunes what each answer excludes,
drops the settings a tailoring point hides, applies `by_choice`, then removes the tailoring
points and joins their edges through. A document with no tailoring point comes back equal to
its input. Publish resolves a document with tailoring points for every org unit band and
every target on and off, and runs every publish rule on each result. The band set, the base
outcomes and the fields `by_choice` may change are data in `grammar-1.3.yaml`.
`ACTIVE_GRAMMAR_VERSION` is 1.3: new templates use the latest grammar. A 1.3 document with no
tailoring point is the same as a 1.2 one.

`lookups` is any object with the nine data reads of `PublishLookups`: the four CHAR reads
(`known_sops`, `step_anchors`, `gate_ids`, `phases_of_sop`) and, since 0.2.0,
`control_base_practices`, `harm_model`, `failure_mode_taxonomy`, `stakeholder_roles` and
`risk_bands`. `lookups.py` documents the shape each one returns and where coreapp reads it. It is
required: there is no default that passes, because "cannot check" is not "checked".

A 0.1.0 `lookups`, which also answered six FORGE rules itself (`LegacyPublishLookups`), still
works in 0.2.x: the validator calls each of those methods it finds instead of the package's rule.
They go in 0.3.0.

| Module | What it holds | Moved from (coreapp) |
|---|---|---|
| `grammar` | the grammar files and their readers | `core/services/govflow_grammar.py`, `core/config/govflow/` |
| `validators` | save and publish validators, `attr_is_shown`, `choice_answers_in_force`, anchor patterns | `core/services/playbook_service.py` |
| `setup_shapes` | save-time shape rules for structured settings | `core/services/setup_lists.py` |
| `setup_lists` | publish rules for structured settings and the lists they name (0.2.0) | `core/services/setup_lists.py` |
| `controls` | the REALISES rules, `claim_errors` (0.2.0) | `core/services/playbook_controls_service.py` |
| `hazard` | `gate_for_step`, the per-record gates (0.2.0) | `core/services/hazard_run_service.py` |
| `triage` | `questions_for_node` (0.2.0) | `core/services/triage_service.py` |
| `registration` | `REGISTRATION_SOP_STEP` (0.2.0) | `core/services/registration_service.py` |
| `hashing` | `content_hash` | `core/services/playbook_service.py` |
| `tailoring` | grammar 1.3: `resolve`, `resolve_with_record`, the tailoring point save and publish rules (0.3.0) | new |
| `lookups` | `PublishLookups`, `LegacyPublishLookups` | new |

What stayed in coreapp: the data behind `lookups` (the CHAR mirror, the controls catalogue, the
stakeholder roles mirror, the data contract), the organization's own roles, each list's version
label, the owed reasons, `freeze`, and `grammar_for_api` with its gate titles, which read CHAR's
lifecycle data.

## Releasing

A version change in `pyproject.toml`, merged to `main`, publishes that version to PyPI from CI
(`.gitlab-ci.yml` at the repo root, plan row AT-9). There is no token: PyPI trusted publishing
(OIDC). The publish job runs only after the package's tests pass, playbook-grammar before
aigov-tools, in the GitLab environment `pypi`. A version already on PyPI is skipped with a
message, so a merge that does not move the version publishes nothing. Never upload by hand.

**One-time setup on pypi.org, for each project** (`ashmatics-playbook-grammar` and
`ashmatics-aigov-tools`): the project's Manage page, Publishing, "Add a new publisher", GitLab:

| Field | Value |
|---|---|
| Namespace | `ashmatics/platform` |
| Project name | `ashmatics-aigov-framework` |
| Top-level pipeline file path | `.gitlab-ci.yml` |
| Environment name | `pypi` |

**On GitLab, first** (before adding the publishers on pypi.org): protect the environment `pypi`
(Settings, CI/CD, Protected environments; allowed to deploy: Maintainers, with an approval if
wanted). PyPI checks the project, the pipeline file and the environment, but not the branch, and
a merge request pipeline runs the branch's own `.gitlab-ci.yml`; so without the protection any
Developer could add a job that publishes. The group is on GitLab Ultimate, which has protected
environments; on 2026-11-02 none was set up and `main` is protected (Maintainers push and merge).

**A playbook-grammar version bump also re-locks aigov-tools in the same merge request**
(`uv lock` in `tools/aigov-tools`): its `uv.lock` records the sibling's version, and
`aigov-tools-tests` runs `uv sync --locked`, which fails on a stale lock.

Until a project has its publisher, its publish job fails at the token exchange for a new
version, and nothing is uploaded. A merge request pipeline never publishes; its
`pypi-version-check` job says which versions are already on PyPI.

## Tests

The tests replay what coreapp's own functions returned (`tests/fixtures/`):

- every published interior in coreapp's `interiors-v0.25.0.json` passes both validators and
  reproduces its published hash;
- for the bundle and 64 broken documents, the moved functions return exactly what coreapp's
  returned, in the same order, and make exactly the CHAR reads coreapp made;
- the publish validator over data reads alone (the 0.2.0 shape) returns the same, and every
  function and constant moved in 0.2.0 matches coreapp's, vector by vector
  (`tests/test_moved_rules.py`). The recorder reads the framework's stakeholder roles registry
  from this repo, at the commit coreapp's mirror was made from.

To re-record after a change on the coreapp side, run the recorder from coreapp's venv:

```bash
/path/to/ashmatics-coreapp/.venv/bin/python scripts/record_playbook_grammar_fixtures.py \
    --coreapp /path/to/ashmatics-coreapp
```
