# Core 5 labs: building workflows with Claude Code

Companion labs for **Core 5: Building Workflows with Claude Code**. Two kits you copy into a repo, a workflow skill and a formatter hook, each with a script that validates and exercises it the way Claude Code would, plus a security checklist runner for the repos you point Claude at.

Everything runs offline with Python 3.9+ and the standard library; `workflow_skill.py` also uses `git` (it builds a temp repo). No lab calls the model, needs the `claude` CLI, or writes outside `core-5/out/` and temp dirs. Run the commands from `labs/`.

| Lab | What it does | Session topic | Docs it encodes |
|---|---|---|---|
| `workflow_skill/kit/` + `workflow_skill.py` | A `/release-notes [since-ref] [audience]` skill: named `arguments`, an inline `` !`…` `` and a fenced ```` ```! ```` injection, a bundled script reached through `${CLAUDE_SKILL_DIR}`, a linked style guide, `disable-model-invocation: true`. The validator checks every frontmatter field, runs each injected command through the core-1 permission engine ("runs" or "would abort"), renders the skill for real in a temp copy of tiny-app with a tag and seven commits, and runs the script's tests. | Workflow skills: skills as workflows, arguments, dynamic context injection, scripts in skills | [skills](https://code.claude.com/docs/en/skills): frontmatter reference, string substitutions, pass arguments, inject dynamic context, permission checks on injected commands; [tools-reference](https://code.claude.com/docs/en/tools-reference): 2-minute timeout, exit-1 carve-out |
| `format_hook/kit/` + `format_hook.py` | A `PostToolUse` hook (matcher `Edit\|Write`, exec form with `${CLAUDE_PROJECT_DIR}`) that formats the edited file, tells Claude when the file changed under it, and reports files that no longer parse. The lab validates the settings entry, shows how matchers are read, and runs 10 cases through `common.hooks.run_hook`. | Deterministic steps: hooks for formatting and linting, code versus model decisions | [hooks](https://code.claude.com/docs/en/hooks): PostToolUse input and decision control, matcher patterns, exec and shell form, exit codes, onFailure; [hooks-guide](https://code.claude.com/docs/en/hooks-guide): auto-format recipe |
| `security_review.py` | A PASS/WARN/FAIL/MANUAL checklist: credential-shaped strings (redacted), key and `.env` files, unpinned dependencies and install scripts, and the Claude Code config a repo ships (broad allow rules, `defaultMode`, hooks, `env` secrets, helpers, `.mcp.json` literals and unpinned servers, skills' `allowed-tools`). Uses the core-1 engine to answer "can Claude's Read tool open `.env`?". | Security: security review, sandboxing, dependency review, secrets hygiene | [permissions](https://code.claude.com/docs/en/permissions) (Read and Edit, workspace trust, what runs before trust), [sandboxing](https://code.claude.com/docs/en/sandboxing), [mcp](https://code.claude.com/docs/en/mcp) (`${VAR}` expansion), [skills](https://code.claude.com/docs/en/skills#pre-approve-tools-for-a-skill), [settings-reference](https://code.claude.com/docs/en/settings-reference) |

The permission checks reuse `core-1/_perms.py`, the frontmatter parsing and boolean/list coercion reuse `common/frontmatter.py`, and the hook interpretation is `common/hooks.py`, so these labs read Claude Code formats the same way as the rest of the course.

## Commands

```bash
# Workflow skill
python3 core-5/workflow_skill.py                            # validate + render /release-notes v0.3.0 customers
python3 core-5/workflow_skill.py --args 'v0.3.0 "support team"'
python3 core-5/workflow_skill.py --deny 'Bash(python3 *)'   # a deny rule aborts the invocation
python3 core-5/workflow_skill.py --mode auto --strip-allowed-tools
python3 core-5/workflow_skill.py --skill path/to/SKILL.md   # dry run on another skill
python3 core-5/workflow_skill.py --skill path/to/SKILL.md --execute --yes   # run its ! commands in a temp repo (lists them first; refuses a bare Bash grant)
python3 -m unittest discover -s core-5/workflow_skill/tests -v   # the script's tests on their own

# Formatter hook
python3 core-5/format_hook.py                               # validate settings.json, run 10 hook cases
python3 core-5/format_hook.py --print-snippet               # just the settings block to merge
python3 core-5/format_hook.py --settings path/to/settings.json

# Security checklist
python3 core-5/security_review.py                           # seeded sample (generated in a temp dir) + tiny-app
python3 core-5/security_review.py --repo ~/code/my-app      # your repo (a path outside labs/), read-only
python3 core-5/security_review.py --repo ~/code/my-app --strict --json  # exit 1 on FAIL; write core-5/out/security-report.json
```

## Install the kits

```bash
# The skill (adds .claude/skills/release-notes/; doesn't touch settings.json)
cp -r core-5/workflow_skill/kit/.claude <your-repo>/
# then, in Claude Code:  /release-notes v1.2.0 developers

# The hook (-n never overwrites an existing file)
mkdir -p <your-repo>/.claude && cp -Rn core-5/format_hook/kit/.claude/. <your-repo>/.claude/
```

If `<your-repo>/.claude/settings.json` already existed, `cp -n` skipped it: merge the block from `python3 core-5/format_hook.py --print-snippet` into its `"hooks"` object by hand, then check it with `/hooks`. The exec form (`"command": "python3", "args": ["${CLAUDE_PROJECT_DIR}/..."]`) is what the hooks reference recommends for path placeholders and needs Claude Code v2.1.139 or later; on older versions use the shell form `"command": "python3 \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/format_on_edit.py"`.

## Output excerpts (real runs)

`python3 core-5/workflow_skill.py`: both injected commands are pre-approved, so the render runs them and Claude receives real output in their place.

```text
[2] Injected commands (they run before Claude reads the skill, and never prompt)
where    form    command                                                           check  result
───────  ──────  ────────────────────────────────────────────────────────────────  ─────  ──────
line 18  inline  git rev-parse --abbrev-ref HEAD                                   allow  runs
line 21  fenced  python3 <repo>/.claude/skills/release-notes/scripts/changes.py -  allow  runs
  rules used: allowed-tools from the frontmatter, mode default
...
  arguments: 'v0.3.0 customers' -> $since='v0.3.0', $audience='customers'
  ---- rendered prompt (what Claude receives) ------------------------
  | # Release notes since v0.3.0
  |
  | ## Context collected before you read this
  |
  | - Branch: main
  | - Changes since the latest tag, grouped by type:
  |
  | Range: v0.3.0..HEAD (latest tag v0.3.0), 7 commits
  |
  | ### Breaking changes
  | - pricing: rename DISCOUNTS to DISCOUNT_CODES (4027f48)
  |
  | ### Features
  | - pricing: accept lower-case discount codes (0eb476c)
...
[4] Same render with allowed-tools removed (what pre-approval buys you)
  Shell command permission check failed for pattern "```! ..." (prompt: shell commands outside the read-only set ask)
[5] Tests for scripts/changes.py (core-5/workflow_skill/tests)
  14 tests, OK
```

`python3 core-5/format_hook.py`: `Edit|Write` is an exact list, so it never fires for NotebookEdit, while a regex like `Edit.*` would.

```text
matcher         read as  Edit  Write  NotebookEdit  MultiEdit  Read  Bash
──────────────  ───────  ────  ─────  ────────────  ─────────  ────  ────
Edit|Write      exact    yes   yes    -             -          -     -
Edit, Write     exact    yes   yes    -             -          -     -
Edit.*          regex    yes   -      yes           yes        -     -
^(Edit|Write)$  regex    yes   yes    -             -          -     -
*               all      yes   yes    yes           yes        yes   yes
[2] Run the hook script on 10 cases (common.hooks.run_hook, FORMAT_HOOK_FORMATTER=builtin)
  argv: python3 <project>/.claude/hooks/format_on_edit.py
  PASS  1. Write: messy .py is normalized and Claude is told to re-read
          PostToolUse: success | proceeds | context: 1 item(s)
  PASS  2. Edit: an already-formatted file stays silent
          PostToolUse: success | proceeds
  PASS  3. Write: a .py syntax error comes back as decision block (feedback, not a block)
          PostToolUse: success | decision=block | proceeds | Claude sees: "format_on_edit: tinyapp/broken.py doesn't compile: invalid syntax (line 1). Fix "
...
  PASS 10. stdin that isn't JSON: exit 1, non-blocking error, the edit stands (fails open)
          PostToolUse: non_blocking_error | proceeds | failure (onFailure=continue)

  result: 10/10 cases behave as expected
```

`python3 core-5/security_review.py`: the seeded repo trips each check once; tiny-app passes everything except the MANUAL items. Matches are redacted to four characters and a length.

```text
  Secrets
    FAIL   S1   No credential-shaped strings in files
                .env:1  Anthropic API key  sk-a…(34 chars)
                .mcp.json:17  credentials in a URL  post…(35 chars)
                app/client.py:4  GitHub token  ghp_…(40 chars)
                app/client.py:5  hard-coded secret assignment  pass…(29 chars)
                app/settings.py:3  AWS access key ID  AKIA…(20 chars)
                app/settings.py:4  credentials in a URL  post…(33 chars)
                deploy/id_ed25519:1  private key block  ----…(35 chars)
...
    FAIL   C2   Repo settings don't request bypassPermissions or auto
                .claude/settings.json:8  defaultMode bypassPermissions
...
    FAIL   C7   .mcp.json holds no literal credentials
                .mcp.json:17  db.env.DB_URL = post…(56 chars)
                .mcp.json:7  tickets.headers.Authorization = Bear…(34 chars)
...
  summary: 6 FAIL, 11 WARN, 1 PASS, 5 MANUAL, 1 N/A

│ Security checklist: common/samples/tiny-app │
  No WARN or FAIL items. The MANUAL items still apply: they live in your user settings and habits.
  summary: 4 PASS, 5 MANUAL, 15 N/A  (brief: only WARN/FAIL shown)
```

## Notes and limits

- **What the renderer can't know.** The docs say `!` placeholders are substituted "once over the original file" but not whether argument placeholders are filled before injected commands run. The lab locates commands in the original text and keeps arguments out of them, and the validator warns when a skill puts `$name` or `$1` inside a `!` command (it's also a shell-injection risk). In auto mode the docs say the skill "loads with an instruction telling Claude to run the command first"; that instruction's wording isn't published, so the lab shows `[run this command first: …]`.
- **Read-only git.** `git rev-parse` passes without pre-approval because `core-1/_perms.py` counts it as a read-only git form; the docs say "read-only forms of git" without listing them, so the kit pre-approves it anyway.
- **Hooks fail open.** The formatter exits 1 on bad input, a non-blocking error, which is the right default for formatting. A hook that guards something needs `"onFailure": "block"` (v2.1.295+), and even then it's defense in depth: the boundary is permission deny rules plus the sandbox.
- **The checklist is a pre-pass.** It's pattern-based and offline. Follow it with `/security-review` in Claude Code and an online audit (`pip-audit`, `npm audit`). The seeded repo is generated at run time from string pieces, so no credential-shaped literal exists in this folder; the values are fake (AWS's documented example key ID and placeholder tokens).
