@onlooker-community/ecosystem 0.15.2 → 0.17.0

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.
Files changed (54) hide show
  1. package/.claude-plugin/marketplace.json +39 -0
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/.release-please-manifest.json +5 -2
  4. package/CHANGELOG.md +15 -0
  5. package/CLAUDE.md +88 -0
  6. package/package.json +3 -3
  7. package/plugins/compass/.claude-plugin/plugin.json +14 -0
  8. package/plugins/compass/CHANGELOG.md +8 -0
  9. package/plugins/compass/config.json +71 -0
  10. package/plugins/compass/docs/adr/001-evaluate-prompts-in-context.md +82 -0
  11. package/plugins/compass/docs/design.md +421 -0
  12. package/plugins/compass/hooks/hooks.json +82 -0
  13. package/plugins/compass/scripts/hooks/compass-bash-gate.sh +95 -0
  14. package/plugins/compass/scripts/hooks/compass-pre-tool-use.sh +86 -0
  15. package/plugins/compass/scripts/hooks/compass-record-write.sh +97 -0
  16. package/plugins/compass/scripts/hooks/compass-session-start.sh +77 -0
  17. package/plugins/compass/scripts/lib/compass-config.sh +72 -0
  18. package/plugins/compass/scripts/lib/compass-evaluator.sh +374 -0
  19. package/plugins/compass/scripts/lib/compass-events.sh +81 -0
  20. package/plugins/compass/scripts/lib/compass-gate.sh +465 -0
  21. package/plugins/compass/scripts/lib/compass-sanitizer.sh +82 -0
  22. package/plugins/compass/scripts/lib/compass-transcript.sh +135 -0
  23. package/plugins/governor/.claude-plugin/plugin.json +14 -0
  24. package/plugins/governor/CHANGELOG.md +22 -0
  25. package/plugins/governor/config.json +19 -0
  26. package/plugins/governor/hooks/hooks.json +48 -0
  27. package/plugins/governor/scripts/hooks/governor-post-tool-use.sh +147 -0
  28. package/plugins/governor/scripts/hooks/governor-pre-tool-use.sh +199 -0
  29. package/plugins/governor/scripts/hooks/governor-session-start.sh +109 -0
  30. package/plugins/governor/scripts/hooks/governor-stop.sh +108 -0
  31. package/plugins/governor/scripts/lib/governor-config.sh +79 -0
  32. package/plugins/governor/scripts/lib/governor-estimate.sh +116 -0
  33. package/plugins/governor/scripts/lib/governor-events.sh +81 -0
  34. package/plugins/governor/scripts/lib/governor-ledger.sh +172 -0
  35. package/plugins/scribe/.claude-plugin/plugin.json +12 -0
  36. package/plugins/scribe/CHANGELOG.md +8 -0
  37. package/plugins/scribe/config.json +20 -0
  38. package/plugins/scribe/hooks/hooks.json +37 -0
  39. package/plugins/scribe/scripts/hooks/scribe-capture.sh +76 -0
  40. package/plugins/scribe/scripts/hooks/scribe-session-start.sh +58 -0
  41. package/plugins/scribe/scripts/hooks/scribe-stop.sh +67 -0
  42. package/plugins/scribe/scripts/lib/scribe-config.sh +72 -0
  43. package/plugins/scribe/scripts/lib/scribe-distill.sh +239 -0
  44. package/plugins/scribe/scripts/lib/scribe-events.sh +80 -0
  45. package/plugins/scribe/scripts/lib/scribe-extract.sh +147 -0
  46. package/plugins/scribe/scripts/lib/scribe-project-key.sh +89 -0
  47. package/plugins/scribe/scripts/lib/scribe-ulid.sh +50 -0
  48. package/release-please-config.json +48 -0
  49. package/test/bats/governor-config.bats +106 -0
  50. package/test/bats/governor-estimate.bats +86 -0
  51. package/test/bats/governor-events.bats +238 -0
  52. package/test/bats/governor-ledger.bats +220 -0
  53. package/test/bats/scribe-extract.bats +102 -0
  54. package/test/bats/scribe-project-key.bats +75 -0
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env bash
2
+ # Scribe Stop hook — intent distillation.
3
+ #
4
+ # Fires when the agent session ends. Reads the full session transcript,
5
+ # runs a Haiku extraction pass to identify the problem context, decisions,
6
+ # tradeoffs, and constraints, then writes a Markdown intent document to
7
+ # ~/.onlooker/scribe/<project_key>/<date>-<session>.md.
8
+ #
9
+ # Skip conditions (all silent):
10
+ # - scribe.enabled is false
11
+ # - no transcript_path in hook input, or file is unreadable
12
+ # - session has fewer turns than scribe.capture.min_turns
13
+ #
14
+ # Hook contract:
15
+ # - Always exits 0. Never blocks Stop.
16
+ # - Errors are written to stderr only; stdout is kept clean.
17
+
18
+ set -uo pipefail
19
+
20
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
21
+ PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
22
+
23
+ export CLAUDE_PLUGIN_ROOT="$PLUGIN_ROOT"
24
+
25
+ # shellcheck source=../lib/scribe-config.sh
26
+ source "${PLUGIN_ROOT}/scripts/lib/scribe-config.sh"
27
+ # shellcheck source=../lib/scribe-events.sh
28
+ source "${PLUGIN_ROOT}/scripts/lib/scribe-events.sh"
29
+ # shellcheck source=../lib/scribe-project-key.sh
30
+ source "${PLUGIN_ROOT}/scripts/lib/scribe-project-key.sh"
31
+ # shellcheck source=../lib/scribe-extract.sh
32
+ source "${PLUGIN_ROOT}/scripts/lib/scribe-extract.sh"
33
+ # shellcheck source=../lib/scribe-distill.sh
34
+ source "${PLUGIN_ROOT}/scripts/lib/scribe-distill.sh"
35
+
36
+ INPUT=$(cat)
37
+ SESSION_ID=$(printf '%s' "$INPUT" | jq -r '.session_id // ""' 2>/dev/null) || SESSION_ID=""
38
+ CWD=$(printf '%s' "$INPUT" | jq -r '.cwd // ""' 2>/dev/null) || CWD=""
39
+ TRANSCRIPT_PATH=$(printf '%s' "$INPUT" | jq -r '.transcript_path // ""' 2>/dev/null) || TRANSCRIPT_PATH=""
40
+
41
+ export _HOOK_SESSION_ID="$SESSION_ID"
42
+
43
+ _done() { exit 0; }
44
+
45
+ [[ -z "$SESSION_ID" ]] && _done
46
+
47
+ scribe_config_load "$CWD"
48
+
49
+ if ! scribe_config_enabled; then
50
+ _done
51
+ fi
52
+
53
+ if [[ -z "$TRANSCRIPT_PATH" || ! -f "$TRANSCRIPT_PATH" ]]; then
54
+ _done
55
+ fi
56
+
57
+ _distill_rc=0
58
+ output_path=$(scribe_distill "$SESSION_ID" "$CWD" "$TRANSCRIPT_PATH") || _distill_rc=$?
59
+ if [[ $_distill_rc -ne 0 ]]; then
60
+ # rc=2 means below min_turns — silent skip, not an error.
61
+ [[ $_distill_rc -ne 2 ]] && printf 'scribe-stop: distillation failed for session %s\n' "$SESSION_ID" >&2
62
+ _done
63
+ fi
64
+
65
+ [[ -n "$output_path" ]] && printf 'scribe: intent document written → %s\n' "$output_path" >&2
66
+
67
+ _done
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env bash
2
+ # Config resolution for Scribe.
3
+ #
4
+ # Reads three layers, latest wins:
5
+ # 1. plugins/scribe/config.json (defaults shipped with the plugin)
6
+ # 2. ~/.claude/settings.json
7
+ # 3. <repo>/.claude/settings.json
8
+ #
9
+ # Exposes:
10
+ # scribe_config_load <repo_root> # populates _SCRIBE_CONFIG (JSON)
11
+ # scribe_config_get <jq-path> # echoes string value (empty if unset)
12
+ # scribe_config_get_json <jq-path> # echoes JSON value (null if unset)
13
+ # scribe_config_enabled # 0 if scribe.enabled is true
14
+
15
+ _SCRIBE_CONFIG="{}"
16
+
17
+ scribe_config_load() {
18
+ local repo_root="${1:-}"
19
+ local plugin_root="${CLAUDE_PLUGIN_ROOT:-}"
20
+ local home_dir="${HOME:-}"
21
+
22
+ local merged="{}"
23
+ local file
24
+
25
+ file="${plugin_root}/config.json"
26
+ if [[ -f "$file" ]]; then
27
+ local defaults
28
+ defaults=$(jq '.' "$file" 2>/dev/null) || defaults="{}"
29
+ merged=$(jq -n --argjson a "$merged" --argjson b "$defaults" '$a * $b' 2>/dev/null) \
30
+ || merged="$defaults"
31
+ fi
32
+
33
+ local repo_settings=""
34
+ [[ -n "$repo_root" ]] && repo_settings="${repo_root}/.claude/settings.json"
35
+
36
+ for file in "${home_dir}/.claude/settings.json" "$repo_settings"; do
37
+ [[ -n "$file" && -f "$file" ]] || continue
38
+ local overlay
39
+ overlay=$(jq '{ scribe: (.scribe // {}) }' "$file" 2>/dev/null) || continue
40
+ [[ -z "$overlay" ]] && continue
41
+ local attempt
42
+ if attempt=$(jq -n --argjson a "$merged" --argjson b "$overlay" '
43
+ def deepmerge($a; $b):
44
+ if ($a|type) == "object" and ($b|type) == "object" then
45
+ reduce (($a|keys) + ($b|keys) | unique)[] as $k
46
+ ({}; .[$k] = deepmerge($a[$k]; $b[$k]))
47
+ elif $b == null then $a
48
+ else $b end;
49
+ deepmerge($a; $b)
50
+ ' 2>/dev/null) && [[ -n "$attempt" ]]; then
51
+ merged="$attempt"
52
+ fi
53
+ done
54
+
55
+ _SCRIBE_CONFIG="$merged"
56
+ }
57
+
58
+ scribe_config_get() {
59
+ local path="$1"
60
+ printf '%s' "$_SCRIBE_CONFIG" | jq -r "${path} // empty" 2>/dev/null
61
+ }
62
+
63
+ scribe_config_get_json() {
64
+ local path="$1"
65
+ printf '%s' "$_SCRIBE_CONFIG" | jq -c "${path}" 2>/dev/null
66
+ }
67
+
68
+ scribe_config_enabled() {
69
+ local v
70
+ v=$(scribe_config_get '.scribe.enabled')
71
+ [[ "$v" == "true" ]]
72
+ }
@@ -0,0 +1,239 @@
1
+ #!/usr/bin/env bash
2
+ # Distillation pipeline for Scribe.
3
+ #
4
+ # Orchestrates the full Stop-time flow:
5
+ # 1. Load session state (captured initial prompt)
6
+ # 2. Count transcript turns — skip if below min_turns
7
+ # 3. Call scribe_extract_intent (Haiku pass)
8
+ # 4. Format output as a readable Markdown document
9
+ # 5. Write to ~/.onlooker/scribe/<project_key>/
10
+ # 6. Optionally mirror to <repo_root>/<project_dir>/
11
+ # 7. Emit scribe.distill.complete
12
+ #
13
+ # Exposes:
14
+ # scribe_distill <session_id> <cwd> <transcript_path>
15
+
16
+ # shellcheck source=./scribe-extract.sh
17
+ # (caller must source scribe-extract.sh before scribe-distill.sh)
18
+
19
+ _scribe_format_document() {
20
+ local intent_json="${1:-}"
21
+ local session_id="${2:-unknown}"
22
+ local project_root="${3:-}"
23
+ local captured_prompt="${4:-}"
24
+ local date_str
25
+ date_str=$(date '+%Y-%m-%d' 2>/dev/null) || date_str="unknown"
26
+ local timestamp
27
+ timestamp=$(date '+%Y-%m-%dT%H:%M:%SZ' 2>/dev/null) || timestamp="unknown"
28
+
29
+ local summary problem decisions_json tradeoffs_json constraints_json out_of_scope_json
30
+ summary=$(printf '%s' "$intent_json" | jq -r '.summary // ""' 2>/dev/null) || summary=""
31
+ problem=$(printf '%s' "$intent_json" | jq -r '.problem // ""' 2>/dev/null) || problem=""
32
+ decisions_json=$(printf '%s' "$intent_json" | jq -c '.decisions // []' 2>/dev/null) || decisions_json="[]"
33
+ tradeoffs_json=$(printf '%s' "$intent_json" | jq -c '.tradeoffs // []' 2>/dev/null) || tradeoffs_json="[]"
34
+ constraints_json=$(printf '%s' "$intent_json" | jq -c '.constraints // []' 2>/dev/null) || constraints_json="[]"
35
+ out_of_scope_json=$(printf '%s' "$intent_json" | jq -c '.out_of_scope // []' 2>/dev/null) || out_of_scope_json="[]"
36
+
37
+ local session_short="${session_id:0:8}"
38
+
39
+ {
40
+ printf '# Session Intent: %s\n\n' "$date_str"
41
+ [[ -n "$summary" ]] && printf '> %s\n\n' "$summary"
42
+
43
+ printf '## Problem\n\n'
44
+ if [[ -n "$problem" ]]; then
45
+ printf '%s\n\n' "$problem"
46
+ else
47
+ printf '*No problem statement extracted.*\n\n'
48
+ fi
49
+
50
+ printf '## Decisions\n\n'
51
+ local decision_count
52
+ decision_count=$(printf '%s' "$decisions_json" | jq 'length' 2>/dev/null) || decision_count=0
53
+ if [[ "$decision_count" -gt 0 ]]; then
54
+ local i
55
+ for ((i = 0; i < decision_count; i++)); do
56
+ local d r alts
57
+ d=$(printf '%s' "$decisions_json" | jq -r ".[$i].decision // \"\"" 2>/dev/null) || d=""
58
+ r=$(printf '%s' "$decisions_json" | jq -r ".[$i].reason // \"\"" 2>/dev/null) || r=""
59
+ alts=$(printf '%s' "$decisions_json" | jq -r ".[$i].alternatives // [] | .[]" 2>/dev/null) || alts=""
60
+ [[ -z "$d" ]] && continue
61
+ printf '- **%s** — %s\n' "$d" "$r"
62
+ if [[ -n "$alts" ]]; then
63
+ printf ' - *Considered:* '
64
+ local first=1
65
+ while IFS= read -r alt; do
66
+ [[ -z "$alt" ]] && continue
67
+ [[ "$first" -eq 0 ]] && printf ', '
68
+ printf '%s' "$alt"
69
+ first=0
70
+ done <<< "$alts"
71
+ printf '\n'
72
+ fi
73
+ done
74
+ printf '\n'
75
+ else
76
+ printf '*None noted.*\n\n'
77
+ fi
78
+
79
+ printf '## Tradeoffs\n\n'
80
+ local tradeoff_count
81
+ tradeoff_count=$(printf '%s' "$tradeoffs_json" | jq 'length' 2>/dev/null) || tradeoff_count=0
82
+ if [[ "$tradeoff_count" -gt 0 ]]; then
83
+ printf '%s' "$tradeoffs_json" | jq -r '.[]' 2>/dev/null | while IFS= read -r item; do
84
+ [[ -n "$item" ]] && printf '- %s\n' "$item"
85
+ done
86
+ printf '\n'
87
+ else
88
+ printf '*None noted.*\n\n'
89
+ fi
90
+
91
+ printf '## Constraints\n\n'
92
+ local constraint_count
93
+ constraint_count=$(printf '%s' "$constraints_json" | jq 'length' 2>/dev/null) || constraint_count=0
94
+ if [[ "$constraint_count" -gt 0 ]]; then
95
+ printf '%s' "$constraints_json" | jq -r '.[]' 2>/dev/null | while IFS= read -r item; do
96
+ [[ -n "$item" ]] && printf '- %s\n' "$item"
97
+ done
98
+ printf '\n'
99
+ else
100
+ printf '*None noted.*\n\n'
101
+ fi
102
+
103
+ printf '## Out of Scope\n\n'
104
+ local oos_count
105
+ oos_count=$(printf '%s' "$out_of_scope_json" | jq 'length' 2>/dev/null) || oos_count=0
106
+ if [[ "$oos_count" -gt 0 ]]; then
107
+ printf '%s' "$out_of_scope_json" | jq -r '.[]' 2>/dev/null | while IFS= read -r item; do
108
+ [[ -n "$item" ]] && printf '- %s\n' "$item"
109
+ done
110
+ printf '\n'
111
+ else
112
+ printf '*None noted.*\n\n'
113
+ fi
114
+
115
+ if [[ -n "$captured_prompt" ]]; then
116
+ printf '## Initial Prompt\n\n'
117
+ printf '```\n%s\n```\n\n' "$captured_prompt"
118
+ fi
119
+
120
+ printf '---\n'
121
+ printf '*Generated by scribe · session `%s` · %s*\n' "$session_short" "$timestamp"
122
+ [[ -n "$project_root" ]] && printf '*Project: `%s`*\n' "$project_root"
123
+ }
124
+ }
125
+
126
+ scribe_distill() {
127
+ local session_id="${1:-}"
128
+ local cwd="${2:-}"
129
+ local transcript_path="${3:-}"
130
+
131
+ [[ -z "$session_id" ]] && return 1
132
+
133
+ local onlooker_dir="${ONLOOKER_DIR:-${HOME}/.onlooker}"
134
+ local state_file="${onlooker_dir}/scribe/sessions/${session_id}.json"
135
+
136
+ # Load captured prompt from session state (best-effort).
137
+ local captured_prompt=""
138
+ if [[ -f "$state_file" ]]; then
139
+ captured_prompt=$(jq -r '.captured_prompt // ""' "$state_file" 2>/dev/null) || captured_prompt=""
140
+ fi
141
+
142
+ # Transcript is required for extraction.
143
+ if [[ -z "$transcript_path" || ! -f "$transcript_path" ]]; then
144
+ printf 'scribe_distill: no transcript available for session %s\n' "$session_id" >&2
145
+ return 1
146
+ fi
147
+
148
+ # Count turns; skip trivial sessions.
149
+ local min_turns
150
+ min_turns=$(scribe_config_get '.scribe.capture.min_turns') || min_turns="3"
151
+ [[ -z "$min_turns" || "$min_turns" == "null" ]] && min_turns="3"
152
+
153
+ local turn_count
154
+ turn_count=$(scribe_count_turns "$transcript_path")
155
+
156
+ if [[ "$turn_count" -lt "$min_turns" ]]; then
157
+ return 2
158
+ fi
159
+
160
+ # Resolve config.
161
+ local model timeout_s max_tokens temperature transcript_chars_max
162
+ model=$(scribe_config_get '.scribe.evaluator.model')
163
+ [[ -z "$model" || "$model" == "null" ]] && model="claude-haiku-4-5-20251001"
164
+ timeout_s=$(scribe_config_get '.scribe.evaluator.timeout')
165
+ [[ -z "$timeout_s" || "$timeout_s" == "null" ]] && timeout_s="60"
166
+ max_tokens=$(scribe_config_get '.scribe.evaluator.max_tokens')
167
+ [[ -z "$max_tokens" || "$max_tokens" == "null" ]] && max_tokens="2048"
168
+ temperature=$(scribe_config_get '.scribe.evaluator.temperature')
169
+ [[ -z "$temperature" || "$temperature" == "null" ]] && temperature="0.3"
170
+ transcript_chars_max=$(scribe_config_get '.scribe.capture.transcript_chars_max')
171
+ [[ -z "$transcript_chars_max" || "$transcript_chars_max" == "null" ]] && transcript_chars_max="40000"
172
+
173
+ # Run extraction.
174
+ local intent_json
175
+ intent_json=$(scribe_extract_intent \
176
+ "$transcript_path" "$model" "$timeout_s" "$max_tokens" "$temperature" "$transcript_chars_max") || {
177
+ printf 'scribe_distill: extraction failed for session %s\n' "$session_id" >&2
178
+ return 1
179
+ }
180
+
181
+ # Resolve project key and output paths.
182
+ local project_key project_root output_dir
183
+ project_key=$(scribe_project_key "$cwd")
184
+ project_root=$(scribe_project_repo_root "$cwd")
185
+
186
+ if [[ -n "$project_key" ]]; then
187
+ output_dir=$(scribe_project_dir "$project_key")
188
+ else
189
+ output_dir="${onlooker_dir}/scribe/unknown"
190
+ fi
191
+
192
+ mkdir -p "$output_dir" 2>/dev/null || {
193
+ printf 'scribe_distill: cannot create output dir %s\n' "$output_dir" >&2
194
+ return 1
195
+ }
196
+
197
+ local date_str
198
+ date_str=$(date '+%Y-%m-%d' 2>/dev/null) || date_str="unknown"
199
+ local session_short="${session_id:0:8}"
200
+ local filename="${date_str}-${session_short}.md"
201
+ local output_path="${output_dir}/${filename}"
202
+
203
+ # Format and write the document.
204
+ local doc
205
+ doc=$(_scribe_format_document \
206
+ "$intent_json" "$session_id" "$project_root" "$captured_prompt")
207
+
208
+ printf '%s\n' "$doc" > "$output_path" 2>/dev/null || {
209
+ printf 'scribe_distill: failed to write %s\n' "$output_path" >&2
210
+ return 1
211
+ }
212
+
213
+ # Mirror to project tree if configured.
214
+ local mirror artifacts=1
215
+ mirror=$(scribe_config_get '.scribe.output.mirror_to_project')
216
+ if [[ "$mirror" == "true" && -n "$project_root" ]]; then
217
+ local project_dir
218
+ project_dir=$(scribe_config_get '.scribe.output.project_dir')
219
+ [[ -z "$project_dir" || "$project_dir" == "null" ]] && project_dir="docs/decisions"
220
+ local mirror_dir="${project_root}/${project_dir}"
221
+ if mkdir -p "$mirror_dir" 2>/dev/null; then
222
+ if cp "$output_path" "${mirror_dir}/${filename}" 2>/dev/null; then
223
+ artifacts=2
224
+ fi
225
+ fi
226
+ fi
227
+
228
+ # Emit scribe.distill.complete.
229
+ local payload
230
+ payload=$(jq -n \
231
+ --arg sid "$session_id" \
232
+ --argjson cap 1 \
233
+ --argjson art "$artifacts" \
234
+ '{session_id: $sid, captures_processed: $cap, artifacts_produced: $art}') || payload=""
235
+
236
+ [[ -n "$payload" ]] && scribe_emit_event "scribe.distill.complete" "$payload" || true
237
+
238
+ printf '%s' "$output_path"
239
+ }
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env bash
2
+ # Canonical scribe.* event emission.
3
+ #
4
+ # Thin wrapper around the ecosystem plugin's onlooker-event.mjs `emit` mode.
5
+ # Every emission is validated against @onlooker-community/schema before being
6
+ # appended to $ONLOOKER_EVENTS_LOG (defaults to $ONLOOKER_DIR/logs/onlooker-events.jsonl).
7
+ #
8
+ # Usage:
9
+ # scribe_emit_event "scribe.distill.complete" '{"session_id":"...","captures_processed":1,...}'
10
+
11
+ _SCRIBE_PLUGIN_NAME="scribe"
12
+
13
+ _scribe_event_js_path() {
14
+ if [[ -n "${_ONLOOKER_EVENT_JS:-}" && -f "$_ONLOOKER_EVENT_JS" ]]; then
15
+ printf '%s' "$_ONLOOKER_EVENT_JS"
16
+ return 0
17
+ fi
18
+ local plugin_root="${CLAUDE_PLUGIN_ROOT:-}"
19
+ local candidates=(
20
+ "${plugin_root}/scripts/lib/onlooker-event.mjs"
21
+ "${plugin_root}/../../scripts/lib/onlooker-event.mjs"
22
+ )
23
+ local c
24
+ for c in "${candidates[@]}"; do
25
+ [[ -f "$c" ]] && { printf '%s' "$c"; return 0; }
26
+ done
27
+ return 1
28
+ }
29
+
30
+ _scribe_session_id() {
31
+ if [[ -n "${_HOOK_SESSION_ID:-}" ]]; then
32
+ printf '%s' "$_HOOK_SESSION_ID"
33
+ return 0
34
+ fi
35
+ if [[ -n "${CLAUDE_SESSION_ID:-}" ]]; then
36
+ printf '%s' "$CLAUDE_SESSION_ID"
37
+ return 0
38
+ fi
39
+ printf 'unknown'
40
+ }
41
+
42
+ scribe_emit_event() {
43
+ local event_type="${1:-}"
44
+ local payload="${2:-}"
45
+
46
+ [[ -z "$event_type" || -z "$payload" ]] && return 1
47
+
48
+ local event_js
49
+ event_js=$(_scribe_event_js_path) || return 1
50
+
51
+ local session_id
52
+ session_id=$(_scribe_session_id)
53
+
54
+ local params
55
+ params=$(jq -n \
56
+ --arg plugin "$_SCRIBE_PLUGIN_NAME" \
57
+ --arg sid "$session_id" \
58
+ --arg type "$event_type" \
59
+ --argjson payload "$payload" \
60
+ '{plugin: $plugin, session_id: $sid, event_type: $type, payload: $payload}' \
61
+ 2>/dev/null) || return 1
62
+
63
+ local event
64
+ local stderr_file
65
+ stderr_file=$(mktemp -t scribe-event-err.XXXXXX 2>/dev/null) || stderr_file="/tmp/scribe-event-err.$$"
66
+ event=$(printf '%s' "$params" \
67
+ | ONLOOKER_DIR="${ONLOOKER_DIR:-$HOME/.onlooker}" \
68
+ ONLOOKER_PLUGIN_NAME="$_SCRIBE_PLUGIN_NAME" \
69
+ node "$event_js" emit 2>"$stderr_file") || {
70
+ printf 'scribe_emit_event: schema validation failed for %s\n' "$event_type" >&2
71
+ [[ -s "$stderr_file" ]] && cat "$stderr_file" >&2
72
+ rm -f "$stderr_file"
73
+ return 1
74
+ }
75
+ rm -f "$stderr_file"
76
+
77
+ local log_path="${ONLOOKER_EVENTS_LOG:-${ONLOOKER_DIR:-$HOME/.onlooker}/logs/onlooker-events.jsonl}"
78
+ mkdir -p "$(dirname "$log_path")" 2>/dev/null || return 1
79
+ printf '%s\n' "$event" >> "$log_path"
80
+ }
@@ -0,0 +1,147 @@
1
+ #!/usr/bin/env bash
2
+ # Intent extraction for Scribe.
3
+ #
4
+ # Reads a session transcript and runs a Haiku pass to extract structured
5
+ # intent documentation: the problem being solved, decisions made and why,
6
+ # tradeoffs, constraints, and what was explicitly left out.
7
+ #
8
+ # This is documentation from intent, not from code. The output answers
9
+ # WHY, not WHAT — git logs and code comments cover what.
10
+ #
11
+ # Exposes:
12
+ # scribe_count_turns <transcript_path>
13
+ # Echoes the number of user turns found in the transcript (integer).
14
+ #
15
+ # scribe_extract_intent <transcript_path> <model> <timeout> <max_tokens> <temperature>
16
+ # Echoes a JSON object on success, empty string on failure.
17
+ # JSON shape:
18
+ # {
19
+ # "problem": string,
20
+ # "decisions": [{decision, reason, alternatives:[]}],
21
+ # "tradeoffs": [string],
22
+ # "constraints": [string],
23
+ # "out_of_scope": [string],
24
+ # "summary": string
25
+ # }
26
+
27
+ _SCRIBE_EXTRACT_PROMPT='You are an intent documentation assistant. Analyze this agent session transcript and extract structured documentation about WHY changes were made — the problem context, decisions, tradeoffs, and constraints that shaped the work. This is documentation from intent, not from code.
28
+
29
+ Do NOT describe what was done. Focus exclusively on why decisions were made.
30
+
31
+ Return a JSON object with exactly these keys:
32
+ {
33
+ "problem": "1-3 sentences: what problem or goal initiated this session",
34
+ "decisions": [
35
+ {
36
+ "decision": "what was decided",
37
+ "reason": "why this approach was chosen",
38
+ "alternatives": ["alternative that was considered but rejected"]
39
+ }
40
+ ],
41
+ "tradeoffs": ["tradeoff description — what was gained vs. given up"],
42
+ "constraints": ["constraint that shaped decisions"],
43
+ "out_of_scope": ["what was explicitly not done, and why"],
44
+ "summary": "2-3 sentences: executive summary of the session intent and key decisions"
45
+ }
46
+
47
+ Rules:
48
+ - All fields are required; use empty arrays [] if no items found
49
+ - Keep each item to 1-2 sentences
50
+ - Return ONLY the JSON object — no prose, no markdown fences, no explanation
51
+
52
+ '
53
+
54
+ scribe_count_turns() {
55
+ local transcript_path="${1:-}"
56
+ [[ -f "$transcript_path" ]] || { printf '0'; return 0; }
57
+
58
+ local count=0
59
+ local line
60
+ while IFS= read -r line; do
61
+ [[ -z "$line" ]] && continue
62
+ local role
63
+ role=$(printf '%s' "$line" | jq -r '.role // empty' 2>/dev/null) || continue
64
+ [[ "$role" == "user" ]] && count=$((count + 1))
65
+ done < "$transcript_path"
66
+
67
+ printf '%s' "$count"
68
+ }
69
+
70
+ scribe_extract_intent() {
71
+ local transcript_path="${1:-}"
72
+ local model="${2:-claude-haiku-4-5-20251001}"
73
+ local timeout_s="${3:-60}"
74
+ local max_tokens="${4:-2048}"
75
+ local temperature="${5:-0.3}"
76
+ local transcript_chars_max="${6:-40000}"
77
+
78
+ [[ -f "$transcript_path" ]] || return 1
79
+
80
+ local transcript_content
81
+ transcript_content=$(jq -r '
82
+ select(.role != null) |
83
+ if .role == "user" then
84
+ "[User]\n" + (
85
+ if (.content | type) == "array" then
86
+ [.content[] | select(.type == "text") | .text] | join("\n")
87
+ else
88
+ (.content // "")
89
+ end
90
+ )
91
+ elif .role == "assistant" then
92
+ "[Assistant]\n" + (
93
+ if (.content | type) == "array" then
94
+ [.content[] | select(.type == "text") | .text] | join("\n")
95
+ else
96
+ (.content // "")
97
+ end
98
+ )
99
+ else empty end
100
+ ' "$transcript_path" 2>/dev/null | head -c "$transcript_chars_max") || transcript_content=""
101
+
102
+ [[ -z "$transcript_content" ]] && return 1
103
+
104
+ local prompt_file
105
+ prompt_file=$(mktemp -t scribe-extract.XXXXXX 2>/dev/null) || prompt_file="/tmp/scribe-extract.$$"
106
+ trap 'rm -f "$prompt_file"' RETURN
107
+
108
+ {
109
+ printf '%s' "$_SCRIBE_EXTRACT_PROMPT"
110
+ printf '<session_transcript>\n'
111
+ printf '%s\n' "$transcript_content"
112
+ printf '</session_transcript>\n'
113
+ } > "$prompt_file"
114
+
115
+ if ! command -v claude >/dev/null 2>&1; then
116
+ printf 'scribe_extract_intent: claude CLI not found\n' >&2
117
+ return 1
118
+ fi
119
+
120
+ local claude_args=(-p --max-turns 1 --model "$model" --max-tokens "$max_tokens")
121
+
122
+ local response=""
123
+ if command -v timeout >/dev/null 2>&1; then
124
+ response=$(timeout "$timeout_s" claude "${claude_args[@]}" < "$prompt_file" 2>/dev/null) || response=""
125
+ elif command -v gtimeout >/dev/null 2>&1; then
126
+ response=$(gtimeout "$timeout_s" claude "${claude_args[@]}" < "$prompt_file" 2>/dev/null) || response=""
127
+ else
128
+ response=$(claude "${claude_args[@]}" < "$prompt_file" 2>/dev/null) || response=""
129
+ fi
130
+
131
+ [[ -z "$response" ]] && return 1
132
+
133
+ # Strip markdown fences if present.
134
+ local clean
135
+ clean=$(printf '%s' "$response" \
136
+ | sed -e 's/^```json[[:space:]]*//' -e 's/^```[[:space:]]*//' -e 's/[[:space:]]*```$//')
137
+
138
+ # Validate all required keys from the extraction prompt.
139
+ if ! printf '%s' "$clean" | jq -e \
140
+ '.problem and (.decisions | type == "array") and (.tradeoffs | type == "array") and (.constraints | type == "array") and (.out_of_scope | type == "array") and .summary' \
141
+ >/dev/null 2>&1; then
142
+ printf 'scribe_extract_intent: response missing required keys\n' >&2
143
+ return 1
144
+ fi
145
+
146
+ printf '%s' "$clean"
147
+ }