@hivehub/rulebook 5.5.1 → 5.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,34 +1,34 @@
1
- #!/usr/bin/env bash
2
- # Claude Code SessionStart hook (matcher: "compact").
3
- #
4
- # Re-injects critical architectural context after a conversation
5
- # compaction. Claude Code already re-loads CLAUDE.md on compact, so
6
- # this hook is defense-in-depth: it outputs a short, always-fresh
7
- # cheat sheet from `.rulebook/COMPACT_CONTEXT.md` so the model has
8
- # the load-bearing reminders immediately available without waiting
9
- # for the CLAUDE.md re-read.
10
- #
11
- # The file is user-editable. Rulebook seeds it during `init` from a
12
- # stack-specific template and never overwrites it afterward.
13
-
14
- set -euo pipefail
15
-
16
- PROJECT_ROOT="$(pwd)"
17
- CONTEXT_FILE="${PROJECT_ROOT}/.rulebook/COMPACT_CONTEXT.md"
18
-
19
- if [[ ! -f "$CONTEXT_FILE" ]]; then
20
- # Nothing to inject — emit a benign empty additionalContext.
21
- printf '%s' '{"hookSpecificOutput":{"hookEventName":"SessionStart","additionalContext":""}}'
22
- exit 0
23
- fi
24
-
25
- content="$(cat "$CONTEXT_FILE")"
26
-
27
- # Emit as additionalContext via jq so we correctly escape newlines/quotes.
28
- jq -nc --arg ctx "$content" '{
29
- hookSpecificOutput: {
30
- hookEventName: "SessionStart",
31
- additionalContext: $ctx
32
- }
33
- }'
34
- exit 0
1
+ #!/usr/bin/env bash
2
+ # Claude Code SessionStart hook (matcher: "compact").
3
+ #
4
+ # Re-injects critical architectural context after a conversation
5
+ # compaction. Claude Code already re-loads CLAUDE.md on compact, so
6
+ # this hook is defense-in-depth: it outputs a short, always-fresh
7
+ # cheat sheet from `.rulebook/COMPACT_CONTEXT.md` so the model has
8
+ # the load-bearing reminders immediately available without waiting
9
+ # for the CLAUDE.md re-read.
10
+ #
11
+ # The file is user-editable. Rulebook seeds it during `init` from a
12
+ # stack-specific template and never overwrites it afterward.
13
+
14
+ set -euo pipefail
15
+
16
+ PROJECT_ROOT="$(pwd)"
17
+ CONTEXT_FILE="${PROJECT_ROOT}/.rulebook/COMPACT_CONTEXT.md"
18
+
19
+ if [[ ! -f "$CONTEXT_FILE" ]]; then
20
+ # Nothing to inject — emit a benign empty additionalContext.
21
+ printf '%s' '{"hookSpecificOutput":{"hookEventName":"SessionStart","additionalContext":""}}'
22
+ exit 0
23
+ fi
24
+
25
+ content="$(cat "$CONTEXT_FILE")"
26
+
27
+ # Emit as additionalContext via jq so we correctly escape newlines/quotes.
28
+ jq -nc --arg ctx "$content" '{
29
+ hookSpecificOutput: {
30
+ hookEventName: "SessionStart",
31
+ additionalContext: $ctx
32
+ }
33
+ }'
34
+ exit 0
@@ -1,61 +1,61 @@
1
- #!/usr/bin/env bash
2
- # Claude Code SessionStart hook — auto-restore from handoff.
3
- #
4
- # Checks for `.rulebook/handoff/_pending.md`. If present, emits its
5
- # contents as additionalContext so the new session begins with full
6
- # prior-session context loaded, then archives the file to
7
- # `.rulebook/handoff/<ISO-timestamp>.md` for history.
8
-
9
- set -euo pipefail
10
-
11
- # Read hook input from stdin to get the actual project cwd
12
- input="$(cat || true)"
13
- PROJECT_ROOT=""
14
- if [[ -n "$input" ]] && command -v jq &>/dev/null; then
15
- PROJECT_ROOT="$(printf '%s' "$input" | jq -r '.cwd // empty' 2>/dev/null || true)"
16
- fi
17
- [[ -z "$PROJECT_ROOT" ]] && PROJECT_ROOT="${CLAUDE_PROJECT_DIR:-$(pwd)}"
18
- HANDOFF_DIR="${PROJECT_ROOT}/.rulebook/handoff"
19
- PENDING="${HANDOFF_DIR}/_pending.md"
20
- URGENT="${HANDOFF_DIR}/.urgent"
21
- CONFIG_FILE="${PROJECT_ROOT}/.rulebook/rulebook.json"
22
-
23
- # No pending handoff — nothing to inject
24
- if [[ ! -f "$PENDING" ]]; then
25
- printf '%s' '{}'
26
- exit 0
27
- fi
28
-
29
- content="$(cat "$PENDING")"
30
-
31
- # Archive the pending file with ISO timestamp
32
- timestamp=$(date -u +"%Y-%m-%dT%H-%M-%S")
33
- archive_name="${timestamp}.md"
34
- mv "$PENDING" "${HANDOFF_DIR}/${archive_name}"
35
-
36
- # Clear urgent sentinel if present
37
- rm -f "$URGENT"
38
-
39
- # Prune old handoff files (keep max N, default 50)
40
- max_history=50
41
- if [[ -f "$CONFIG_FILE" ]] && command -v jq &>/dev/null; then
42
- max_history=$(jq -r '.handoff.maxHistoryFiles // 50' "$CONFIG_FILE" 2>/dev/null || echo 50)
43
- fi
44
-
45
- # Count and prune (oldest first, skip _pending.md and .urgent)
46
- history_count=$(find "$HANDOFF_DIR" -maxdepth 1 -name "*.md" -type f 2>/dev/null | wc -l)
47
- if [[ "$history_count" -gt "$max_history" ]]; then
48
- excess=$(( history_count - max_history ))
49
- find "$HANDOFF_DIR" -maxdepth 1 -name "*.md" -type f -printf '%T@ %p\n' 2>/dev/null \
50
- | sort -n | head -"$excess" | awk '{print $2}' | xargs rm -f
51
- fi
52
-
53
- # Emit the handoff content as additionalContext
54
- header="## Session restored from handoff (${archive_name})\n\nThe following context was saved by the previous session's /handoff skill:\n\n"
55
- jq -nc --arg ctx "${header}${content}" '{
56
- hookSpecificOutput: {
57
- hookEventName: "SessionStart",
58
- additionalContext: $ctx
59
- }
60
- }'
61
- exit 0
1
+ #!/usr/bin/env bash
2
+ # Claude Code SessionStart hook — auto-restore from handoff.
3
+ #
4
+ # Checks for `.rulebook/handoff/_pending.md`. If present, emits its
5
+ # contents as additionalContext so the new session begins with full
6
+ # prior-session context loaded, then archives the file to
7
+ # `.rulebook/handoff/<ISO-timestamp>.md` for history.
8
+
9
+ set -euo pipefail
10
+
11
+ # Read hook input from stdin to get the actual project cwd
12
+ input="$(cat || true)"
13
+ PROJECT_ROOT=""
14
+ if [[ -n "$input" ]] && command -v jq &>/dev/null; then
15
+ PROJECT_ROOT="$(printf '%s' "$input" | jq -r '.cwd // empty' 2>/dev/null || true)"
16
+ fi
17
+ [[ -z "$PROJECT_ROOT" ]] && PROJECT_ROOT="${CLAUDE_PROJECT_DIR:-$(pwd)}"
18
+ HANDOFF_DIR="${PROJECT_ROOT}/.rulebook/handoff"
19
+ PENDING="${HANDOFF_DIR}/_pending.md"
20
+ URGENT="${HANDOFF_DIR}/.urgent"
21
+ CONFIG_FILE="${PROJECT_ROOT}/.rulebook/rulebook.json"
22
+
23
+ # No pending handoff — nothing to inject
24
+ if [[ ! -f "$PENDING" ]]; then
25
+ printf '%s' '{}'
26
+ exit 0
27
+ fi
28
+
29
+ content="$(cat "$PENDING")"
30
+
31
+ # Archive the pending file with ISO timestamp
32
+ timestamp=$(date -u +"%Y-%m-%dT%H-%M-%S")
33
+ archive_name="${timestamp}.md"
34
+ mv "$PENDING" "${HANDOFF_DIR}/${archive_name}"
35
+
36
+ # Clear urgent sentinel if present
37
+ rm -f "$URGENT"
38
+
39
+ # Prune old handoff files (keep max N, default 50)
40
+ max_history=50
41
+ if [[ -f "$CONFIG_FILE" ]] && command -v jq &>/dev/null; then
42
+ max_history=$(jq -r '.handoff.maxHistoryFiles // 50' "$CONFIG_FILE" 2>/dev/null || echo 50)
43
+ fi
44
+
45
+ # Count and prune (oldest first, skip _pending.md and .urgent)
46
+ history_count=$(find "$HANDOFF_DIR" -maxdepth 1 -name "*.md" -type f 2>/dev/null | wc -l)
47
+ if [[ "$history_count" -gt "$max_history" ]]; then
48
+ excess=$(( history_count - max_history ))
49
+ find "$HANDOFF_DIR" -maxdepth 1 -name "*.md" -type f -printf '%T@ %p\n' 2>/dev/null \
50
+ | sort -n | head -"$excess" | awk '{print $2}' | xargs rm -f
51
+ fi
52
+
53
+ # Emit the handoff content as additionalContext
54
+ header="## Session restored from handoff (${archive_name})\n\nThe following context was saved by the previous session's /handoff skill:\n\n"
55
+ jq -nc --arg ctx "${header}${content}" '{
56
+ hookSpecificOutput: {
57
+ hookEventName: "SessionStart",
58
+ additionalContext: $ctx
59
+ }
60
+ }'
61
+ exit 0
@@ -1,197 +1,197 @@
1
- #!/usr/bin/env bash
2
- # Claude Code SessionStart hook for rulebook-terse (v5.4.0).
3
- #
4
- # Resolves the active intensity mode, writes it to the project-local
5
- # flag file via a symlink-safe path, reads the installed SKILL.md,
6
- # filters the intensity table + example rows down to the active
7
- # level only, and emits the filtered body to stdout — Claude Code
8
- # injects SessionStart stdout as hidden `additionalContext`.
9
- #
10
- # Contract matches `.rulebook/specs/RULEBOOK_TERSE.md`. Silent-fails
11
- # on every filesystem error so a broken hook never blocks session
12
- # start.
13
- #
14
- # Configuration resolution (first match wins):
15
- # 1. RULEBOOK_TERSE_MODE env var
16
- # 2. $PROJECT_ROOT/.rulebook/rulebook.json → terse.defaultMode
17
- # 3. $XDG_CONFIG_HOME/rulebook/config.json → terse.defaultMode
18
- # 4. ~/.config/rulebook/config.json → terse.defaultMode
19
- # 5. "terse"
20
-
21
- set -u
22
-
23
- # Hook input may arrive on stdin as JSON — read it if present so we
24
- # can resolve PROJECT_ROOT from the `cwd` field (Claude Code may
25
- # invoke the hook from a sub-directory of the project).
26
- input=""
27
- if [ ! -t 0 ]; then
28
- input="$(cat)"
29
- fi
30
-
31
- PROJECT_ROOT=""
32
- if [ -n "$input" ]; then
33
- PROJECT_ROOT="$(printf '%s' "$input" | node -e "
34
- try {
35
- const data = JSON.parse(require('fs').readFileSync(0, 'utf8'));
36
- process.stdout.write(data.cwd || '');
37
- } catch { }
38
- " 2>/dev/null || true)"
39
- fi
40
- [ -z "$PROJECT_ROOT" ] && PROJECT_ROOT="${CLAUDE_PROJECT_DIR:-$(pwd)}"
41
-
42
- FLAG_PATH="${PROJECT_ROOT}/.rulebook/.terse-mode"
43
- CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/rulebook"
44
- USER_CONFIG="${CONFIG_DIR}/config.json"
45
- PROJECT_CONFIG="${PROJECT_ROOT}/.rulebook/rulebook.json"
46
-
47
- VALID_MODES_RE='^(off|brief|terse|ultra|commit|review)$'
48
-
49
- resolve_mode() {
50
- # 1. Env var
51
- if [ -n "${RULEBOOK_TERSE_MODE:-}" ]; then
52
- local m="$(printf '%s' "$RULEBOOK_TERSE_MODE" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')"
53
- if [[ "$m" =~ $VALID_MODES_RE ]]; then
54
- printf '%s' "$m"
55
- return
56
- fi
57
- fi
58
-
59
- # 2. Project config
60
- if [ -f "$PROJECT_CONFIG" ]; then
61
- local m
62
- m="$(node -e "
63
- try {
64
- const cfg = JSON.parse(require('fs').readFileSync(process.argv[1], 'utf8'));
65
- if (cfg.terse && cfg.terse.defaultMode) process.stdout.write(String(cfg.terse.defaultMode));
66
- } catch { }
67
- " "$PROJECT_CONFIG" 2>/dev/null | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]' || true)"
68
- if [ -n "$m" ] && [[ "$m" =~ $VALID_MODES_RE ]]; then
69
- printf '%s' "$m"
70
- return
71
- fi
72
- fi
73
-
74
- # 3. User config
75
- if [ -f "$USER_CONFIG" ]; then
76
- local m
77
- m="$(node -e "
78
- try {
79
- const cfg = JSON.parse(require('fs').readFileSync(process.argv[1], 'utf8'));
80
- if (cfg.terse && cfg.terse.defaultMode) process.stdout.write(String(cfg.terse.defaultMode));
81
- } catch { }
82
- " "$USER_CONFIG" 2>/dev/null | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]' || true)"
83
- if [ -n "$m" ] && [[ "$m" =~ $VALID_MODES_RE ]]; then
84
- printf '%s' "$m"
85
- return
86
- fi
87
- fi
88
-
89
- printf 'terse'
90
- }
91
-
92
- # Symlink-safe flag-file write. Refuses if target or parent is a
93
- # symlink; creates with 0600 via umask + atomic temp+rename.
94
- safe_write_flag() {
95
- local content="$1"
96
- local dir
97
- dir="$(dirname "$FLAG_PATH")"
98
-
99
- mkdir -p "$dir" 2>/dev/null || return 0
100
-
101
- # Refuse if parent is itself a symlink.
102
- [ -L "$dir" ] && return 0
103
- # Refuse if target already exists as a symlink.
104
- [ -L "$FLAG_PATH" ] && return 0
105
-
106
- local tmp
107
- tmp="$(mktemp "$dir/.terse-mode.XXXXXX" 2>/dev/null)" || return 0
108
- {
109
- umask 077
110
- printf '%s' "$content" > "$tmp" 2>/dev/null || { rm -f "$tmp"; return 0; }
111
- }
112
- chmod 600 "$tmp" 2>/dev/null || true
113
- mv -f "$tmp" "$FLAG_PATH" 2>/dev/null || rm -f "$tmp"
114
- }
115
-
116
- mode="$(resolve_mode)"
117
-
118
- # "off" → unlink the flag and exit cleanly (no hidden-context emission).
119
- if [ "$mode" = "off" ]; then
120
- rm -f "$FLAG_PATH" 2>/dev/null || true
121
- exit 0
122
- fi
123
-
124
- safe_write_flag "$mode"
125
-
126
- # Locate the SKILL.md. Prefer the installed copy; fall back to the
127
- # repo-local template when running inside the Rulebook source tree.
128
- SKILL_PATHS=(
129
- "${PROJECT_ROOT}/.claude/skills/rulebook-terse/SKILL.md"
130
- "${PROJECT_ROOT}/templates/skills/core/rulebook-terse/SKILL.md"
131
- )
132
-
133
- skill_body=""
134
- for p in "${SKILL_PATHS[@]}"; do
135
- if [ -f "$p" ]; then
136
- skill_body="$(cat "$p" 2>/dev/null || true)"
137
- break
138
- fi
139
- done
140
-
141
- # Emit the payload. When no SKILL.md is found, fall back to a
142
- # minimal hardcoded ruleset — matches the Caveman pattern for
143
- # standalone installs without templates.
144
- if [ -z "$skill_body" ]; then
145
- cat <<EOF
146
- RULEBOOK-TERSE MODE ACTIVE — level: ${mode}
147
-
148
- ## Persistence
149
- ACTIVE EVERY RESPONSE once set. Off only via "/rulebook-terse off", "normal mode", or session end.
150
-
151
- ## Rules
152
- Drop filler (just, really, basically), pleasantries, hedging. Keep technical terms exact. Code blocks byte-for-byte unchanged.
153
-
154
- ## Auto-Clarity
155
- Full prose for: security warnings, destructive-op confirmations, quality-gate failures, multi-step sequences, user confusion.
156
-
157
- ## Boundaries
158
- Code/tests/commits/specs: unchanged.
159
- EOF
160
- exit 0
161
- fi
162
-
163
- # Header + filtered body. Filtering:
164
- # - Strip YAML frontmatter (everything up through the second `---`).
165
- # - Intensity-table rows `| **<level>** | ...` — keep only the active one.
166
- # - Example lines `- **<level>**: "..."` — keep only the active one.
167
- # - All other lines pass through unchanged.
168
- #
169
- # Uses `node -e` for portability — BSD awk (macOS default) does not
170
- # support the 3-arg `match(str, re, array)` form that gawk ships with.
171
- printf 'RULEBOOK-TERSE MODE ACTIVE — level: %s\n\n' "$mode"
172
-
173
- printf '%s' "$skill_body" | node -e "
174
- let body = '';
175
- process.stdin.setEncoding('utf8');
176
- process.stdin.on('data', (c) => { body += c; });
177
- process.stdin.on('end', () => {
178
- const active = process.argv[1];
179
- // Strip YAML frontmatter.
180
- const stripped = body.replace(/^---\s*\n[\s\S]*?\n---\s*\n/, '');
181
- const out = [];
182
- for (const line of stripped.split('\n')) {
183
- const tableRow = line.match(/^\s*\|\s*\*\*([^*]+)\*\*\s*\|/);
184
- if (tableRow) {
185
- if (tableRow[1] === active) out.push(line);
186
- continue;
187
- }
188
- const exampleLine = line.match(/^\s*-\s*\*\*([^*]+)\*\*\s*:/);
189
- if (exampleLine) {
190
- if (exampleLine[1] === active) out.push(line);
191
- continue;
192
- }
193
- out.push(line);
194
- }
195
- process.stdout.write(out.join('\n'));
196
- });
197
- " "$mode"
1
+ #!/usr/bin/env bash
2
+ # Claude Code SessionStart hook for rulebook-terse (v5.4.0).
3
+ #
4
+ # Resolves the active intensity mode, writes it to the project-local
5
+ # flag file via a symlink-safe path, reads the installed SKILL.md,
6
+ # filters the intensity table + example rows down to the active
7
+ # level only, and emits the filtered body to stdout — Claude Code
8
+ # injects SessionStart stdout as hidden `additionalContext`.
9
+ #
10
+ # Contract matches `.rulebook/specs/RULEBOOK_TERSE.md`. Silent-fails
11
+ # on every filesystem error so a broken hook never blocks session
12
+ # start.
13
+ #
14
+ # Configuration resolution (first match wins):
15
+ # 1. RULEBOOK_TERSE_MODE env var
16
+ # 2. $PROJECT_ROOT/.rulebook/rulebook.json → terse.defaultMode
17
+ # 3. $XDG_CONFIG_HOME/rulebook/config.json → terse.defaultMode
18
+ # 4. ~/.config/rulebook/config.json → terse.defaultMode
19
+ # 5. "terse"
20
+
21
+ set -u
22
+
23
+ # Hook input may arrive on stdin as JSON — read it if present so we
24
+ # can resolve PROJECT_ROOT from the `cwd` field (Claude Code may
25
+ # invoke the hook from a sub-directory of the project).
26
+ input=""
27
+ if [ ! -t 0 ]; then
28
+ input="$(cat)"
29
+ fi
30
+
31
+ PROJECT_ROOT=""
32
+ if [ -n "$input" ]; then
33
+ PROJECT_ROOT="$(printf '%s' "$input" | node -e "
34
+ try {
35
+ const data = JSON.parse(require('fs').readFileSync(0, 'utf8'));
36
+ process.stdout.write(data.cwd || '');
37
+ } catch { }
38
+ " 2>/dev/null || true)"
39
+ fi
40
+ [ -z "$PROJECT_ROOT" ] && PROJECT_ROOT="${CLAUDE_PROJECT_DIR:-$(pwd)}"
41
+
42
+ FLAG_PATH="${PROJECT_ROOT}/.rulebook/.terse-mode"
43
+ CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/rulebook"
44
+ USER_CONFIG="${CONFIG_DIR}/config.json"
45
+ PROJECT_CONFIG="${PROJECT_ROOT}/.rulebook/rulebook.json"
46
+
47
+ VALID_MODES_RE='^(off|brief|terse|ultra|commit|review)$'
48
+
49
+ resolve_mode() {
50
+ # 1. Env var
51
+ if [ -n "${RULEBOOK_TERSE_MODE:-}" ]; then
52
+ local m="$(printf '%s' "$RULEBOOK_TERSE_MODE" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')"
53
+ if [[ "$m" =~ $VALID_MODES_RE ]]; then
54
+ printf '%s' "$m"
55
+ return
56
+ fi
57
+ fi
58
+
59
+ # 2. Project config
60
+ if [ -f "$PROJECT_CONFIG" ]; then
61
+ local m
62
+ m="$(node -e "
63
+ try {
64
+ const cfg = JSON.parse(require('fs').readFileSync(process.argv[1], 'utf8'));
65
+ if (cfg.terse && cfg.terse.defaultMode) process.stdout.write(String(cfg.terse.defaultMode));
66
+ } catch { }
67
+ " "$PROJECT_CONFIG" 2>/dev/null | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]' || true)"
68
+ if [ -n "$m" ] && [[ "$m" =~ $VALID_MODES_RE ]]; then
69
+ printf '%s' "$m"
70
+ return
71
+ fi
72
+ fi
73
+
74
+ # 3. User config
75
+ if [ -f "$USER_CONFIG" ]; then
76
+ local m
77
+ m="$(node -e "
78
+ try {
79
+ const cfg = JSON.parse(require('fs').readFileSync(process.argv[1], 'utf8'));
80
+ if (cfg.terse && cfg.terse.defaultMode) process.stdout.write(String(cfg.terse.defaultMode));
81
+ } catch { }
82
+ " "$USER_CONFIG" 2>/dev/null | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]' || true)"
83
+ if [ -n "$m" ] && [[ "$m" =~ $VALID_MODES_RE ]]; then
84
+ printf '%s' "$m"
85
+ return
86
+ fi
87
+ fi
88
+
89
+ printf 'terse'
90
+ }
91
+
92
+ # Symlink-safe flag-file write. Refuses if target or parent is a
93
+ # symlink; creates with 0600 via umask + atomic temp+rename.
94
+ safe_write_flag() {
95
+ local content="$1"
96
+ local dir
97
+ dir="$(dirname "$FLAG_PATH")"
98
+
99
+ mkdir -p "$dir" 2>/dev/null || return 0
100
+
101
+ # Refuse if parent is itself a symlink.
102
+ [ -L "$dir" ] && return 0
103
+ # Refuse if target already exists as a symlink.
104
+ [ -L "$FLAG_PATH" ] && return 0
105
+
106
+ local tmp
107
+ tmp="$(mktemp "$dir/.terse-mode.XXXXXX" 2>/dev/null)" || return 0
108
+ {
109
+ umask 077
110
+ printf '%s' "$content" > "$tmp" 2>/dev/null || { rm -f "$tmp"; return 0; }
111
+ }
112
+ chmod 600 "$tmp" 2>/dev/null || true
113
+ mv -f "$tmp" "$FLAG_PATH" 2>/dev/null || rm -f "$tmp"
114
+ }
115
+
116
+ mode="$(resolve_mode)"
117
+
118
+ # "off" → unlink the flag and exit cleanly (no hidden-context emission).
119
+ if [ "$mode" = "off" ]; then
120
+ rm -f "$FLAG_PATH" 2>/dev/null || true
121
+ exit 0
122
+ fi
123
+
124
+ safe_write_flag "$mode"
125
+
126
+ # Locate the SKILL.md. Prefer the installed copy; fall back to the
127
+ # repo-local template when running inside the Rulebook source tree.
128
+ SKILL_PATHS=(
129
+ "${PROJECT_ROOT}/.claude/skills/rulebook-terse/SKILL.md"
130
+ "${PROJECT_ROOT}/templates/skills/core/rulebook-terse/SKILL.md"
131
+ )
132
+
133
+ skill_body=""
134
+ for p in "${SKILL_PATHS[@]}"; do
135
+ if [ -f "$p" ]; then
136
+ skill_body="$(cat "$p" 2>/dev/null || true)"
137
+ break
138
+ fi
139
+ done
140
+
141
+ # Emit the payload. When no SKILL.md is found, fall back to a
142
+ # minimal hardcoded ruleset — matches the Caveman pattern for
143
+ # standalone installs without templates.
144
+ if [ -z "$skill_body" ]; then
145
+ cat <<EOF
146
+ RULEBOOK-TERSE MODE ACTIVE — level: ${mode}
147
+
148
+ ## Persistence
149
+ ACTIVE EVERY RESPONSE once set. Off only via "/rulebook-terse off", "normal mode", or session end.
150
+
151
+ ## Rules
152
+ Drop filler (just, really, basically), pleasantries, hedging. Keep technical terms exact. Code blocks byte-for-byte unchanged.
153
+
154
+ ## Auto-Clarity
155
+ Full prose for: security warnings, destructive-op confirmations, quality-gate failures, multi-step sequences, user confusion.
156
+
157
+ ## Boundaries
158
+ Code/tests/commits/specs: unchanged.
159
+ EOF
160
+ exit 0
161
+ fi
162
+
163
+ # Header + filtered body. Filtering:
164
+ # - Strip YAML frontmatter (everything up through the second `---`).
165
+ # - Intensity-table rows `| **<level>** | ...` — keep only the active one.
166
+ # - Example lines `- **<level>**: "..."` — keep only the active one.
167
+ # - All other lines pass through unchanged.
168
+ #
169
+ # Uses `node -e` for portability — BSD awk (macOS default) does not
170
+ # support the 3-arg `match(str, re, array)` form that gawk ships with.
171
+ printf 'RULEBOOK-TERSE MODE ACTIVE — level: %s\n\n' "$mode"
172
+
173
+ printf '%s' "$skill_body" | node -e "
174
+ let body = '';
175
+ process.stdin.setEncoding('utf8');
176
+ process.stdin.on('data', (c) => { body += c; });
177
+ process.stdin.on('end', () => {
178
+ const active = process.argv[1];
179
+ // Strip YAML frontmatter.
180
+ const stripped = body.replace(/^---\s*\n[\s\S]*?\n---\s*\n/, '');
181
+ const out = [];
182
+ for (const line of stripped.split('\n')) {
183
+ const tableRow = line.match(/^\s*\|\s*\*\*([^*]+)\*\*\s*\|/);
184
+ if (tableRow) {
185
+ if (tableRow[1] === active) out.push(line);
186
+ continue;
187
+ }
188
+ const exampleLine = line.match(/^\s*-\s*\*\*([^*]+)\*\*\s*:/);
189
+ if (exampleLine) {
190
+ if (exampleLine[1] === active) out.push(line);
191
+ continue;
192
+ }
193
+ out.push(line);
194
+ }
195
+ process.stdout.write(out.join('\n'));
196
+ });
197
+ " "$mode"