This chapter details the standardized format for creating playbooks - detailed execution guides for AI agents within UnifyWeaver workflows.
A playbook is a specific, detailed plan for accomplishing a particular task. Unlike workflows (high-level strategies), playbooks provide:
Think of a playbook as a detailed recipe that an AI agent can follow reliably.
A well-designed playbook has these sections:
---
id: playbook-factorial-compile-v1
name: "Factorial Compilation Playbook"
version: 1.0
workflow_id: compile-and-test
pattern: linear_recursion
target: bash
difficulty: beginner
estimated_time: 5_minutes
prerequisites: [swipl, unifyweaver_installed]
---
Fields:
| Field | Required | Description |
|——-|———-|————-|
| id | Yes | Unique identifier |
| name | Yes | Human-readable title |
| version | Yes | Semantic version |
| workflow_id | No | Parent workflow |
| pattern | No | Pattern classification |
| target | No | Output format (bash, python, etc.) |
| difficulty | No | beginner/intermediate/advanced |
| estimated_time | No | Expected duration |
| prerequisites | No | Required tools/setup |
Clear, concise statement (1-2 sentences) of what the playbook achieves:
## Goal
Compile a factorial predicate from Prolog to optimized Bash,
generate a test runner, and verify results for factorial(0) through factorial(10).
Guidelines:
Background information, assumptions, and prerequisites:
## Context
This playbook demonstrates linear recursion compilation - one of
the most common patterns in UnifyWeaver.
**Assumptions**:
- UnifyWeaver is installed at `$UNIFYWEAVER_HOME`
- SWI-Prolog is available in PATH
- You have write access to `/tmp/`
**Background**:
Linear recursion is automatically optimized by UnifyWeaver's compiler
to use tail recursion and memoization where appropriate.
## Strategies
### Strategy A: Direct Compilation
- **Cost**: Low (single compilation step)
- **Speed**: Fast (<5 seconds)
- **Quality**: Standard
- **When to use**: Quick prototyping
### Strategy B: Optimized with Memoization
- **Cost**: Medium (requires analysis step)
- **Speed**: Slower to compile, faster at runtime
- **Quality**: High (optimized for repeated calls)
- **When to use**: Production deployment
**Heuristic**: Use Strategy A for development, Strategy B for deployment.
Step-by-step instructions with exact commands:
## Execution Steps
### Step 1: Create Prolog Source
Create the factorial predicate in `/tmp/factorial.pl`:
\```bash
cat > /tmp/factorial.pl <<'EOF'
factorial(0, 1).
factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
EOF
\```
**Verification**:
\```bash
test -f /tmp/factorial.pl && wc -l /tmp/factorial.pl
# Expected: 5 /tmp/factorial.pl
\```
Guidelines:
Use the [!output] callout for expected outputs:
## Expected Outputs
> [!output]
> language: bash
> purpose: Compiled factorial function
> format: executable
> location: education/output/advanced/factorial.sh
>
> \```bash
> #!/bin/bash
> factorial() {
> local N=$1
> if [ "$N" -eq 0 ]; then
> echo 1
> else
> local N1=$((N - 1))
> local F1=$(factorial $N1)
> echo $((N * F1))
> fi
> }
> \```
Constraint: Keep [!output] content <10% of total playbook.
## Verification
### Test the Compiled Script
\```bash
echo "5" | bash education/output/advanced/factorial.sh
\```
**Expected output**: `120`
### Success Criteria
All of the following must be true:
- `factorial.sh` exists and is executable
- `factorial 0` returns `1`
- `factorial 5` returns `120`
- `factorial 10` returns `3628800`
- Test runner exits with code 0
## Troubleshooting
### Error: "No such file: compiler_driver.pl"
**Cause**: `file_search_path` not set correctly
**Solution**:
\```bash
export UNIFYWEAVER_HOME=/path/to/UnifyWeaver
cd $UNIFYWEAVER_HOME
\```
### Error: "Predicate factorial/2 not found"
**Cause**: Prolog file not loaded before compilation
**Solution**: Ensure the load statement appears before `compile(...)`.
[!output] - Expected OutputSpecifies what the agent should generate:
> [!output]
> language: bash
> purpose: Compiled factorial function
> format: executable
> location: path/to/output.sh
>
> \```bash
> # Template content here
> \```
Mandatory fields:
language: Programming language or formatpurpose: What this output representsOptional fields:
format: Type (executable, data, config)location: Where to save outputexpected_size: small/medium/large[!example-record] - Library ReferenceTags reusable examples in library files:
> [!example-record]
> id: 20251108-factorial-compile
> name: unifyweaver.compilation.factorial
> pattern: linear_recursion
> child_examples: [unifyweaver.testing.factorial_runner]
>
> Example content here...
Mandatory fields:
id: Unique identifier (YYYYMMDD-name format)name: Namespaced nameOptional fields:
pattern: Classification tagparent_example: Parent in hierarchychild_examples: Related examplesdifficulty: beginner/intermediate/advancedtags: Additional classifications[!note] - Additional Information> [!note]
> **Performance Tip**
>
> For factorials >20, consider using memoization.
[!warning] - Cautionary Information> [!warning]
> The compilation step modifies files. Ensure you have backups.
[!tip] - Best Practices> [!tip]
> Run tests after every compilation to catch regressions.
Playbooks should reference examples rather than embedding large code blocks:
For a complete factorial example, see
[Factorial Compilation](/UnifyWeaver/education/book-04-workflows/examples_library/compilation_examples.html#factorial-compilation).
Best practices:
When a playbook requires Prolog code generation:
### Predicate: `factorial/2`
**Signature**: `factorial(+N:integer, -F:integer)`
**Semantics**:
- Computes F = N! (factorial of N)
- N must be non-negative integer
**Modes**:
- `factorial(+, -)`: Deterministic
- Input N must be ground
- Output F will be bound to result
**Examples**:
\```prolog
?- factorial(5, F).
F = 120.
\```
### Compilation Command
Navigate to `$UNIFYWEAVER_HOME` and run:
\```bash
swipl -q -g "
asserta(file_search_path(unifyweaver, 'src/unifyweaver')),
['/tmp/factorial.pl'],
use_module(unifyweaver(core/compiler_driver)),
compile(factorial/2, [
output_dir('education/output/advanced'),
target(bash)
], Scripts),
format('Generated: ~w~n', [Scripts]),
halt"
\```
---
id: playbook-<task>-v1
name: "<Task Name> Playbook"
version: 1.0
pattern: <pattern_type>
target: <output_format>
difficulty: <level>
estimated_time: <X>_minutes
prerequisites: [<tool1>, <tool2>]
---
# <Task Name> Playbook
## Goal
<Clear statement of what this playbook achieves>
## Context
<Background information>
**Assumptions**:
- <Assumption 1>
- <Assumption 2>
## Strategies
### Strategy A: <Approach>
- **Cost**: <Low|Medium|High>
- **Speed**: <Fast|Medium|Slow>
- **Quality**: <Standard|High>
- **When to use**: <Guidance>
**Heuristic**: <Decision guidance>
## Execution Steps
### Step 1: <Action Name>
<Description>
\```bash
<exact command>
\```
**Verification**:
\```bash
<verification command>
# Expected: <output>
\```
## Expected Outputs
> [!output]
> language: <language>
> purpose: <description>
> format: <format_type>
>
> \```<language>
> <template>
> \```
## Verification
### Success Criteria
- <Criterion 1>
- <Criterion 2>
## Troubleshooting
### Error: "<error message>"
**Cause**: <Why this happens>
**Solution**:
\```bash
<how to fix>
\```
## References
- [Example Library Link](examples_library/file.md#section)
Before publishing a playbook:
[!output] content <10% of totalA well-structured playbook enables AI agents to:
The next chapter covers pipeline orchestration for multi-stage workflows.
| ← Previous: Chapter 1: Thinking in Workflows | 📖 Book 4: Workflows | Next: Chapter 3: Pipeline Orchestration → |