experiment-hooks
Optional scripts that run at iteration boundaries in an experiment session. Two hooks, both transparent to the loop-running agent — their effect is a file on disk or a steer message.
experiment.hooks/
before.sh # fires before each iteration (prospective)
after.sh # fires after each experiment-log (retrospective)
Both files are optional. Files without the executable bit are silently ignored.
Contract
Stdin — before.sh
One JSON line. Parse with jq. Realistic example:
{
"event": "before",
"cwd": "/path/to/workdir",
"next_run": 6,
"last_run": {
"run": 5,
"status": "discard",
"metric": 42.1,
"description": "Simplified to sorted(arr) — copy cost dominates",
"asi": {
"hypothesis": "Built-in sort avoids Python overhead",
"next_focus": "list copy avoidance"
}
},
"session": {
"metric_name": "total_ms",
"metric_unit": "ms",
"direction": "lower",
"baseline_metric": 40.7,
"best_metric": 33.5,
"run_count": 5,
"goal": "optimize sort speed"
}
}
| Field |
Notes |
last_run |
The most recent run entry. null on a fresh session. |
session.direction |
"lower" or "higher" — which end of the scale wins. |
session.baseline_metric |
First run of the current segment. null until one run exists. |
session.best_metric |
Optimal metric across kept runs only. null until one is kept. |
session.goal |
The session name set by experiment-init. |
session.run_count |
Total runs logged so far (any status). |
Stdin — after.sh
{
"event": "after",
"cwd": "/path/to/workdir",
"run_entry": {
"run": 6,
"status": "discard",
"metric": 38.9,
"description": "Timsort hybrid slower on random",
"asi": {
"hypothesis": "Partial-sort heuristic on input distribution",
"learned": "Overhead dominates on random arrays"
}
},
"session": {
"metric_name": "total_ms",
"metric_unit": "ms",
"direction": "lower",
"baseline_metric": 40.7,
"best_metric": 33.5,
"run_count": 6,
"goal": "optimize sort speed"
}
}
| Field |
Notes |
run_entry |
The run just logged. Always present. |
session |
Same shape as in before.sh, reflecting state after the run. |
Output
- Stdout (up to 8 KB) — delivered to the agent as a steer message on the next turn. Empty = silent.
- Stderr + non-zero exit — surfaced as an error steer.
- Timeout — 30 s hard kill; flagged in the observability entry.
Preservation
experiment.hooks/** survives the auto-revert, like all paths matching experiment.*.
Examples
Runnable reference scripts live in this skill's examples/ directory — one file per pattern. Paths are resolved against the skill directory (parent of SKILL.md). Browse them for inspiration; they're not policy.
examples/before/ — external search, qmd document search, anti-thrash, idea rotator, hypothesis reflection, context rotation
examples/after/ — learnings journal, macOS notification on new best, auto-tag winning commits
Each example is a complete, self-contained script with named constants, short helper functions, guard clauses, and intention-revealing names. Read the header comment for its purpose, copy to experiment.hooks/<stage>.sh, adapt.
Steps to add a hook
Understand the session. Read experiment.md for the objective and metric; glance at experiment.sh for the workload. Your hook should complement the loop, not duplicate it.
Clarify the user's intent. What should happen, at which boundary? Research before / log after / notify on wins / intervene on thrash / etc.
Start from an example in examples/ that's closest to the intent (resolve against the skill directory). If nothing fits, write from scratch following the same style (named constants, short functions, guard clauses, JSON stdin parsed with jq). If the request combines retrospective + prospective concerns, use both before.sh and after.sh — don't overload one.
Copy, adapt, mark executable.
mkdir -p experiment.hooks
cp "<skill-dir>/examples/before/external-search.sh" experiment.hooks/before.sh
# ... adapt the script ...
chmod +x experiment.hooks/before.sh
Sanity-test with a piped mock before relying on it in the loop:
jq -n '
{
event: "before",
cwd: ".",
next_run: 1,
last_run: null,
session: {
metric_name: "total_ms",
metric_unit: "ms",
direction: "lower",
baseline_metric: null,
best_metric: null,
run_count: 0,
goal: "test"
}
}
' | ./experiment.hooks/before.sh
For after.sh, swap last_run: null for a run_entry object (see the schema above).
Commit the hook alongside other session files. It's preserved across reverts because the path matches experiment.*.
Rules of thumb
Read whatever fields the agent naturally writes — asi.hypothesis, asi.next_focus, asi.learned, description. Don't invent a "hook input" field and instruct the agent to populate it; that breaks the transparency principle.
Silent is the default. Only print to stdout when you have something useful for the agent. Empty stdout means no steer.
Guard with early exits. [ -z "$query" ] && exit 0 is cheaper and clearer than wrapping everything in if.
One concern per script. If you want research + learnings, put them in separate files (before.sh and after.sh). Don't bundle.
No environment variables. Everything is on stdin; extract cwd (and anything else) with jq. There is no $AUTORESEARCH_WORK_DIR.
1---
2name: experiment-hooks
3description: Author pre/post-iteration hooks for an experiment session. Use when the user asks to add research fetching, Slack/webhook notifications, persistent learnings, auto-tagging, anti-thrash intervention, idea rotation, or any side effect around iterations.
4metadata:
5 source: https://skillmd.com/skills/aibot88/experiment-hooks
6---
7
8# experiment-hooks
9
10Optional scripts that run at iteration boundaries in an experiment session. Two hooks, both transparent to the loop-running agent — their effect is a file on disk or a steer message.
11
12```
13experiment.hooks/
14 before.sh # fires before each iteration (prospective)
15 after.sh # fires after each experiment-log (retrospective)
16```
17
18Both files are optional. Files without the executable bit are silently ignored.
19
20---
21
22## Contract
23
24### Stdin — `before.sh`
25
26One JSON line. Parse with `jq`. Realistic example:
27
28```json
29{
30 "event": "before",
31 "cwd": "/path/to/workdir",
32 "next_run": 6,
33 "last_run": {
34 "run": 5,
35 "status": "discard",
36 "metric": 42.1,
37 "description": "Simplified to sorted(arr) — copy cost dominates",
38 "asi": {
39 "hypothesis": "Built-in sort avoids Python overhead",
40 "next_focus": "list copy avoidance"
41 }
42 },
43 "session": {
44 "metric_name": "total_ms",
45 "metric_unit": "ms",
46 "direction": "lower",
47 "baseline_metric": 40.7,
48 "best_metric": 33.5,
49 "run_count": 5,
50 "goal": "optimize sort speed"
51 }
52}
53```
54
55| Field | Notes |
56| ------------------------- | ------------------------------------------------------------------- |
57| `last_run` | The most recent run entry. `null` on a fresh session. |
58| `session.direction` | `"lower"` or `"higher"` — which end of the scale wins. |
59| `session.baseline_metric` | First run of the current segment. `null` until one run exists. |
60| `session.best_metric` | Optimal metric across **kept** runs only. `null` until one is kept. |
61| `session.goal` | The session name set by `experiment-init`. |
62| `session.run_count` | Total runs logged so far (any status). |
63
64### Stdin — `after.sh`
65
66```json
67{
68 "event": "after",
69 "cwd": "/path/to/workdir",
70 "run_entry": {
71 "run": 6,
72 "status": "discard",
73 "metric": 38.9,
74 "description": "Timsort hybrid slower on random",
75 "asi": {
76 "hypothesis": "Partial-sort heuristic on input distribution",
77 "learned": "Overhead dominates on random arrays"
78 }
79 },
80 "session": {
81 "metric_name": "total_ms",
82 "metric_unit": "ms",
83 "direction": "lower",
84 "baseline_metric": 40.7,
85 "best_metric": 33.5,
86 "run_count": 6,
87 "goal": "optimize sort speed"
88 }
89}
90```
91
92| Field | Notes |
93| ----------- | ------------------------------------------------------------- |
94| `run_entry` | The run just logged. Always present. |
95| `session` | Same shape as in `before.sh`, reflecting state after the run. |
96
97### Output
98
99- **Stdout** (up to 8 KB) — delivered to the agent as a steer message on the next turn. Empty = silent.
100- **Stderr + non-zero exit** — surfaced as an error steer.
101- **Timeout** — 30 s hard kill; flagged in the observability entry.
102
103### Preservation
104
105`experiment.hooks/**` survives the auto-revert, like all paths matching `experiment.*`.
106
107---
108
109## Examples
110
111Runnable reference scripts live in this skill's `examples/` directory — one file per pattern. Paths are resolved against the skill directory (parent of SKILL.md). Browse them for inspiration; they're not policy.
112
113- `examples/before/` — external search, qmd document search, anti-thrash, idea rotator, hypothesis reflection, context rotation
114- `examples/after/` — learnings journal, macOS notification on new best, auto-tag winning commits
115
116Each example is a complete, self-contained script with named constants, short helper functions, guard clauses, and intention-revealing names. Read the header comment for its purpose, copy to `experiment.hooks/<stage>.sh`, adapt.
117
118---
119
120## Steps to add a hook
121
1221. **Understand the session.** Read `experiment.md` for the objective and metric; glance at `experiment.sh` for the workload. Your hook should complement the loop, not duplicate it.
123
1242. **Clarify the user's intent.** What should happen, at which boundary? Research before / log after / notify on wins / intervene on thrash / etc.
125
1263. **Start from an example in `examples/`** that's closest to the intent (resolve against the skill directory). If nothing fits, write from scratch following the same style (named constants, short functions, guard clauses, JSON stdin parsed with `jq`). If the request combines retrospective + prospective concerns, use both `before.sh` and `after.sh` — don't overload one.
127
1284. **Copy, adapt, mark executable.**
129
130 ```bash
131 mkdir -p experiment.hooks
132 cp "<skill-dir>/examples/before/external-search.sh" experiment.hooks/before.sh
133 # ... adapt the script ...
134 chmod +x experiment.hooks/before.sh
135 ```
136
1375. **Sanity-test with a piped mock** before relying on it in the loop:
138
139 ```bash
140 jq -n '
141 {
142 event: "before",
143 cwd: ".",
144 next_run: 1,
145 last_run: null,
146 session: {
147 metric_name: "total_ms",
148 metric_unit: "ms",
149 direction: "lower",
150 baseline_metric: null,
151 best_metric: null,
152 run_count: 0,
153 goal: "test"
154 }
155 }
156 ' | ./experiment.hooks/before.sh
157 ```
158
159 For `after.sh`, swap `last_run: null` for a `run_entry` object (see the schema above).
160
1616. **Commit the hook** alongside other session files. It's preserved across reverts because the path matches `experiment.*`.
162
163---
164
165## Rules of thumb
166
167- **Read whatever fields the agent naturally writes** — `asi.hypothesis`, `asi.next_focus`, `asi.learned`, `description`. Don't invent a "hook input" field and instruct the agent to populate it; that breaks the transparency principle.
168
169- **Silent is the default.** Only print to stdout when you have something useful for the agent. Empty stdout means no steer.
170
171- **Guard with early exits.** `[ -z "$query" ] && exit 0` is cheaper and clearer than wrapping everything in `if`.
172
173- **One concern per script.** If you want research + learnings, put them in separate files (`before.sh` and `after.sh`). Don't bundle.
174
175- **No environment variables.** Everything is on stdin; extract `cwd` (and anything else) with `jq`. There is no `$AUTORESEARCH_WORK_DIR`.