Skip to content

Commit 0e18902

Browse files
committed
Use better json parsing
1 parent 2b1a138 commit 0e18902

1 file changed

Lines changed: 36 additions & 33 deletions

File tree

lib/zsh/functions/wiggum

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,15 @@ UNDER NO CIRCUMESTANCES SHOULD YOU MODIFY, DELETE, etc FILES LIKE:
8888

8989
This prompt is iteration number $iteration.
9090

91-
After you have completed the task, output a log entry as JSON on a single line prefixed with WIGGUM_LOG:
91+
ONLY WORK ON A SINGLE TASK.
9292

93-
\`\`\`
94-
WIGGUM_LOG:{"success":true,"summary":"CSS added to style header","steps_taken":["decided to name CSS file foo.css","created CSS file"],"commands_run":["touch ./foo.css"],"issues_encountered":[]}
95-
\`\`\`
93+
## Response format
9694

97-
ONLY WORK ON A SINGLE TASK.
95+
Your final line of output MUST be raw JSON (not wrapped in markdown) in this exact format:
9896

99-
If there is no further work to do, output exactly WIGGUM:OVER instead of a log entry.
97+
{"done":false,"success":true,"summary":"CSS added to style header","steps_taken":["decided to name CSS file foo.css","created CSS file"],"commands_run":["touch ./foo.css"],"issues_encountered":[]}
10098

101-
ONLY WORK ON A SINGLE TASK.
99+
Set "done":true when there is no further work to do. Set "done":false if more iterations are needed.
102100
EOF
103101
} | claude -p \
104102
--dangerously-skip-permissions \
@@ -107,43 +105,48 @@ EOF
107105
local end_time=$(date -Iseconds)
108106

109107
local result_text=$(echo "$output" | jq -r '.result // empty')
108+
local json_line=$(echo "$result_text" | grep '^{' | tail -1)
109+
local log_json=$(echo "$json_line" | jq -c '.' 2>/dev/null)
110110

111-
if [[ "$result_text" == *"WIGGUM:OVER"* ]]; then
112-
echo "Work complete (WIGGUM:OVER)"
111+
if [[ -z "$log_json" ]]; then
112+
echo "Warning: Failed to parse log JSON"
113+
echo "Last JSON-like line: $json_line"
114+
continue
115+
fi
116+
117+
if [[ $(echo "$log_json" | jq -r '.done') == "true" ]]; then
118+
echo "Work complete (done:true)"
113119
break
114120
fi
115-
local log_json=$(echo "$result_text" | grep -oE 'WIGGUM_LOG:\{[^}]+\}' | head -1 | sed 's/WIGGUM_LOG://')
116121
local input_tokens=$(echo "$output" | jq -r '.usage.input_tokens // 0')
117122
local output_tokens=$(echo "$output" | jq -r '.usage.output_tokens // 0')
118123
local start_epoch=$(date -j -f "%Y-%m-%dT%H:%M:%S" "${start_time%[-+]*}" "+%s" 2>/dev/null)
119124
local end_epoch=$(date -j -f "%Y-%m-%dT%H:%M:%S" "${end_time%[-+]*}" "+%s" 2>/dev/null)
120125
local duration_secs=$((end_epoch - start_epoch))
121126

122-
if [[ -n "$log_json" ]]; then
123-
local entry=$(echo "$log_json" | jq \
124-
--arg start "$start_time" \
125-
--arg end "$end_time" \
126-
--argjson iter "$iteration" \
127-
--argjson input "${input_tokens:-0}" \
128-
--argjson output "${output_tokens:-0}" \
129-
--argjson duration "$duration_secs" \
130-
'. + {iteration: $iter, start_time: $start, end_time: $end, duration_secs: $duration, input_tokens: $input, output_tokens: $output}')
131-
132-
if [[ -f "wiggum_log.json" ]]; then
133-
jq --argjson entry "$entry" '. += [$entry]' wiggum_log.json > wiggum_log.json.tmp && mv wiggum_log.json.tmp wiggum_log.json
134-
else
135-
echo "[$entry]" | jq '.' > wiggum_log.json
136-
fi
137-
138-
echo -e "\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
139-
echo "$entry" | jq -r '"Success: \(.success)\nSummary: \(.summary)\nSteps: \(.steps_taken | join(", "))\nCommands: \(.commands_run | join(", "))\nIssues: \(.issues_encountered | if length == 0 then "none" else join(", ") end)\nDuration: \(.duration_secs)s\nTokens: \(.input_tokens) in / \(.output_tokens) out"'
140-
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
141-
142-
total_duration=$((total_duration + duration_secs))
143-
total_input_tokens=$((total_input_tokens + input_tokens))
144-
total_output_tokens=$((total_output_tokens + output_tokens))
127+
local entry=$(echo "$log_json" | jq \
128+
--arg start "$start_time" \
129+
--arg end "$end_time" \
130+
--argjson iter "$iteration" \
131+
--argjson input "${input_tokens:-0}" \
132+
--argjson output "${output_tokens:-0}" \
133+
--argjson duration "$duration_secs" \
134+
'. + {iteration: $iter, start_time: $start, end_time: $end, duration_secs: $duration, input_tokens: $input, output_tokens: $output}')
135+
136+
if [[ -f "wiggum_log.json" ]]; then
137+
jq --argjson entry "$entry" '. += [$entry]' wiggum_log.json > wiggum_log.json.tmp && mv wiggum_log.json.tmp wiggum_log.json
138+
else
139+
echo "[$entry]" | jq '.' > wiggum_log.json
145140
fi
146141

142+
echo -e "\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
143+
echo "$entry" | jq -r '"Success: \(.success)\nSummary: \(.summary)\nSteps: \(.steps_taken | join(", "))\nCommands: \(.commands_run | join(", "))\nIssues: \(.issues_encountered | if length == 0 then "none" else join(", ") end)\nDuration: \(.duration_secs)s\nTokens: \(.input_tokens) in / \(.output_tokens) out"'
144+
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
145+
146+
total_duration=$((total_duration + duration_secs))
147+
total_input_tokens=$((total_input_tokens + input_tokens))
148+
total_output_tokens=$((total_output_tokens + output_tokens))
149+
147150
[[ -z "$no_git" ]] && git commit -am "Wiggum iteration $iteration"
148151
done
149152

0 commit comments

Comments
 (0)