forked from PolyArch/humanize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitlesson-select.sh
More file actions
executable file
·253 lines (204 loc) · 7 KB
/
bitlesson-select.sh
File metadata and controls
executable file
·253 lines (204 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash
set -euo pipefail
# ========================================
# Source Shared Libraries
# ========================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
source "$SCRIPT_DIR/lib/config-loader.sh"
source "$SCRIPT_DIR/lib/model-router.sh"
PLUGIN_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
PROJECT_ROOT="${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}"
MERGED_CONFIG="$(load_merged_config "$PLUGIN_ROOT" "$PROJECT_ROOT")"
BITLESSON_MODEL="$(get_config_value "$MERGED_CONFIG" "bitlesson_model")"
BITLESSON_MODEL="${BITLESSON_MODEL:-haiku}"
CODEX_FALLBACK_MODEL="$(get_config_value "$MERGED_CONFIG" "codex_model")"
CODEX_FALLBACK_MODEL="${CODEX_FALLBACK_MODEL:-$DEFAULT_CODEX_MODEL}"
PROVIDER_MODE="$(get_config_value "$MERGED_CONFIG" "provider_mode")"
PROVIDER_MODE="${PROVIDER_MODE:-auto}"
# Source portable timeout wrapper
source "$SCRIPT_DIR/portable-timeout.sh"
# Source shared loop library (kept for consistency with ask-codex.sh)
HOOKS_LIB_DIR="$(cd "$SCRIPT_DIR/../hooks/lib" && pwd)"
source "$HOOKS_LIB_DIR/loop-common.sh"
usage() {
cat <<'USAGE_EOF' >&2
Usage:
bitlesson-select.sh --task <string> --paths <comma-separated> --bitlesson-file <path>
Output (exactly):
LESSON_IDS: <comma-separated IDs or NONE>
RATIONALE: <one concise sentence>
USAGE_EOF
}
TASK=""
PATHS=""
BITLESSON_FILE=""
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--task)
TASK="${2:-}"
shift 2
;;
--paths)
PATHS="${2:-}"
shift 2
;;
--bitlesson-file)
BITLESSON_FILE="${2:-}"
shift 2
;;
*)
echo "Error: Unknown argument: $1" >&2
usage
exit 1
;;
esac
done
if [[ -z "$TASK" ]]; then
echo "Error: --task is required and must be non-empty" >&2
usage
exit 1
fi
if [[ -z "$PATHS" ]]; then
echo "Error: --paths is required and must be non-empty" >&2
usage
exit 1
fi
if [[ -z "$BITLESSON_FILE" ]]; then
echo "Error: --bitlesson-file is required" >&2
usage
exit 1
fi
if [[ ! -f "$BITLESSON_FILE" ]]; then
echo "Error: BitLesson file not found: $BITLESSON_FILE" >&2
exit 1
fi
BITLESSON_CONTENT="$(cat "$BITLESSON_FILE")"
if [[ -z "$(printf '%s' "$BITLESSON_CONTENT" | tr -d ' \t\n\r')" ]]; then
echo "Error: BitLesson file is empty (whitespace only): $BITLESSON_FILE" >&2
exit 1
fi
if ! printf '%s\n' "$BITLESSON_CONTENT" | grep -Eq '^[[:space:]]*##[[:space:]]+Lesson:'; then
printf 'LESSON_IDS: NONE\n'
printf 'RATIONALE: The BitLesson file has no recorded lessons yet.\n'
exit 0
fi
# ========================================
# Determine Provider from BITLESSON_MODEL
# ========================================
BITLESSON_PROVIDER="$(detect_provider "$BITLESSON_MODEL")"
if [[ "$PROVIDER_MODE" == "codex-only" ]] && [[ "$BITLESSON_PROVIDER" == "claude" ]]; then
BITLESSON_MODEL="$CODEX_FALLBACK_MODEL"
BITLESSON_PROVIDER="codex"
fi
# ========================================
# Conditional Dependency Check (with fallback)
# ========================================
if ! check_provider_dependency "$BITLESSON_PROVIDER" 2>/dev/null; then
# Fall back to codex provider when the configured provider binary is missing
BITLESSON_MODEL="$DEFAULT_CODEX_MODEL"
BITLESSON_PROVIDER="codex"
check_provider_dependency "$BITLESSON_PROVIDER"
fi
# ========================================
# Detect Project Root (for -C)
# ========================================
BITLESSON_DIR="$(cd "$(dirname "$BITLESSON_FILE")" && pwd -P)"
if git -C "$BITLESSON_DIR" rev-parse --show-toplevel &>/dev/null; then
CODEX_PROJECT_ROOT="$(git -C "$BITLESSON_DIR" rev-parse --show-toplevel)"
else
CODEX_PROJECT_ROOT="$BITLESSON_DIR"
fi
# ========================================
# Build Selector Prompt
# ========================================
PROMPT="$(cat <<EOF
# BitLesson Selector
You select which lessons from the configured BitLesson file (normally \`.humanize/bitlesson.md\`) must be applied for a given sub-task.
## Input
Sub-task description:
$TASK
Related file paths (comma-separated):
$PATHS
BitLesson file content:
<<<BEGIN_BITLESSON_MD
$BITLESSON_CONTENT
<<<END_BITLESSON_MD
## Decision Rules
1. Match only lessons that are directly relevant to the sub-task scope and failure mode.
2. Prefer precision over recall: do not include weakly related lessons.
3. If nothing is relevant, return \`NONE\`.
4. Use only the information in this prompt. Do not use tools, shell commands, browser access, MCP servers, or repository inspection.
## Output Format (Stable)
Return exactly two lines (no code blocks, no extra whitespace, no additional sections):
LESSON_IDS: <comma-separated lesson IDs or NONE>
RATIONALE: <one concise sentence>
EOF
)"
# ========================================
# Run Selector (Codex or Claude)
# ========================================
SELECTOR_TIMEOUT=120
run_selector() {
local provider="$1"
local model="$2"
if [[ "$provider" == "codex" ]]; then
local codex_exec_args=(
"--disable" "codex_hooks"
"--skip-git-repo-check"
"--ephemeral"
"-s" "read-only"
"-m" "$model"
"-c" "model_reasoning_effort=low"
"-C" "$CODEX_PROJECT_ROOT"
)
printf '%s' "$PROMPT" | run_with_timeout "$SELECTOR_TIMEOUT" codex exec "${codex_exec_args[@]}" -
return $?
fi
if [[ "$provider" == "claude" ]]; then
printf '%s' "$PROMPT" | run_with_timeout "$SELECTOR_TIMEOUT" claude --print --model "$model" -
return $?
fi
echo "Error: Unsupported BitLesson provider '$provider'" >&2
return 1
}
CODEX_EXIT_CODE=0
RAW_OUTPUT="$(run_selector "$BITLESSON_PROVIDER" "$BITLESSON_MODEL" 2>&1)" || CODEX_EXIT_CODE=$?
if [[ $CODEX_EXIT_CODE -eq 124 ]]; then
echo "Error: BitLesson selector timed out after ${SELECTOR_TIMEOUT} seconds" >&2
exit 124
fi
if [[ $CODEX_EXIT_CODE -ne 0 ]]; then
echo "Error: BitLesson selector failed (exit code $CODEX_EXIT_CODE)" >&2
printf '%s\n' "$RAW_OUTPUT" >&2
exit "$CODEX_EXIT_CODE"
fi
# ========================================
# Enforce Stable Output Format
# ========================================
LESSON_IDS_VALUE="$(
printf '%s\n' "$RAW_OUTPUT" \
| sed -n 's/^[[:space:]]*LESSON_IDS:[[:space:]]*//p' \
| head -n 1 \
| tr -d '\r' \
| sed 's/[[:space:]]*$//'
)"
RATIONALE_VALUE="$(
printf '%s\n' "$RAW_OUTPUT" \
| sed -n 's/^[[:space:]]*RATIONALE:[[:space:]]*//p' \
| head -n 1 \
| tr -d '\r' \
| sed 's/[[:space:]]*$//'
)"
if [[ -z "$LESSON_IDS_VALUE" || -z "$RATIONALE_VALUE" ]]; then
echo "Error: Unexpected selector output format (expected LESSON_IDS and RATIONALE lines)" >&2
echo "" >&2
echo "Raw output:" >&2
printf '%s\n' "$RAW_OUTPUT" >&2
exit 1
fi
printf 'LESSON_IDS: %s\n' "$LESSON_IDS_VALUE"
printf 'RATIONALE: %s\n' "$RATIONALE_VALUE"