Skip to content

Commit 6e55a6a

Browse files
author
zenus
committed
Fix bitlesson codex stdin handling
1 parent 86bc7d9 commit 6e55a6a

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

scripts/bitlesson-select.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ if [[ "$BITLESSON_PROVIDER" == "codex" ]]; then
175175
fi
176176
CODEX_EXEC_ARGS+=("$CODEX_AUTO_FLAG" "-C" "$CODEX_PROJECT_ROOT")
177177

178-
RAW_OUTPUT="$(printf '%s' "$PROMPT" | run_with_timeout "$SELECTOR_TIMEOUT" codex exec "${CODEX_EXEC_ARGS[@]}")" || CODEX_EXIT_CODE=$?
178+
RAW_OUTPUT="$(printf '%s' "$PROMPT" | run_with_timeout "$SELECTOR_TIMEOUT" codex exec "${CODEX_EXEC_ARGS[@]}" -)" || CODEX_EXIT_CODE=$?
179179
elif [[ "$BITLESSON_PROVIDER" == "claude" ]]; then
180180
RAW_OUTPUT="$(printf '%s' "$PROMPT" | run_with_timeout "$SELECTOR_TIMEOUT" claude --print --model "$BITLESSON_MODEL")" || CODEX_EXIT_CODE=$?
181181
fi

tests/test-bitlesson-select-routing.sh

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,44 @@ create_mock_codex() {
3232
mkdir -p "$bin_dir"
3333
cat > "$bin_dir/codex" <<'EOF'
3434
#!/bin/bash
35-
# Mock codex that outputs valid bitlesson-selector format
35+
# Mock codex that only reads prompt content from stdin when invoked with trailing '-'
36+
if [[ "${*: -1}" != "-" ]]; then
37+
echo "mock codex expected trailing '-' to read prompt from stdin" >&2
38+
exit 9
39+
fi
40+
41+
stdin_content=$(cat)
42+
if [[ -z "$stdin_content" ]]; then
43+
echo "mock codex expected non-empty stdin prompt" >&2
44+
exit 10
45+
fi
46+
47+
cat <<'OUT'
48+
LESSON_IDS: NONE
49+
RATIONALE: No matching lessons found (mock codex).
50+
OUT
51+
EOF
52+
chmod +x "$bin_dir/codex"
53+
}
54+
55+
# Helper: create a mock codex binary that records stdin for assertions
56+
create_recording_mock_codex() {
57+
local bin_dir="$1"
58+
local stdin_file="$2"
59+
mkdir -p "$bin_dir"
60+
cat > "$bin_dir/codex" <<EOF
61+
#!/bin/bash
62+
if [[ "\${*: -1}" != "-" ]]; then
63+
echo "mock codex expected trailing '-' to read prompt from stdin" >&2
64+
exit 9
65+
fi
66+
67+
cat > "$stdin_file"
68+
if [[ ! -s "$stdin_file" ]]; then
69+
echo "mock codex expected non-empty stdin prompt" >&2
70+
exit 10
71+
fi
72+
3673
cat <<'OUT'
3774
LESSON_IDS: NONE
3875
RATIONALE: No matching lessons found (mock codex).
@@ -86,6 +123,41 @@ else
86123
fail "Codex branch: gpt-* model routes to codex" "LESSON_IDS: in output (exit 0)" "exit=$exit_code, output=$result"
87124
fi
88125

126+
# ========================================
127+
# Test 1b: Codex branch passes '-' and consumes stdin prompt
128+
# ========================================
129+
echo ""
130+
echo "--- Test 1b: gpt-* codex path passes stdin prompt via trailing '-' ---"
131+
echo ""
132+
133+
setup_test_dir
134+
create_mock_bitlesson "$TEST_DIR"
135+
BIN_DIR="$TEST_DIR/bin"
136+
STDIN_FILE="$TEST_DIR/codex-stdin.txt"
137+
create_recording_mock_codex "$BIN_DIR" "$STDIN_FILE"
138+
mkdir -p "$TEST_DIR/.humanize"
139+
printf '{"bitlesson_model": "gpt-4o"}' > "$TEST_DIR/.humanize/config.json"
140+
141+
result=""
142+
exit_code=0
143+
result=$(CLAUDE_PROJECT_DIR="$TEST_DIR" XDG_CONFIG_HOME="$TEST_DIR/no-user" \
144+
PATH="$BIN_DIR:$PATH" \
145+
bash "$BITLESSON_SELECT" \
146+
--task "Fix a bug" \
147+
--paths "scripts/bitlesson-select.sh" \
148+
--bitlesson-file "$TEST_DIR/.humanize/bitlesson.md" 2>/dev/null) || exit_code=$?
149+
150+
if [[ $exit_code -eq 0 ]] \
151+
&& [[ -s "$STDIN_FILE" ]] \
152+
&& grep -q "Sub-task description:" "$STDIN_FILE" \
153+
&& grep -q "Fix a bug" "$STDIN_FILE"; then
154+
pass "Codex branch: selector passes trailing '-' and prompt content through stdin"
155+
else
156+
fail "Codex branch: selector passes trailing '-' and prompt content through stdin" \
157+
"exit=0 with recorded stdin prompt" \
158+
"exit=$exit_code, output=$result, stdin=$(cat "$STDIN_FILE" 2>/dev/null || true)"
159+
fi
160+
89161
# ========================================
90162
# Test 2: Claude branch chosen for haiku model
91163
# ========================================

0 commit comments

Comments
 (0)