fix: debug mode now extracts tool calls from Pi session file

Pi doesn't log tool calls to stderr in print mode. Instead, we save
the session (.jsonl) and parse it to extract every tool_use entry
showing which files were read, grep patterns used, etc.
This commit is contained in:
Markus Hofstetter
2026-05-18 23:51:36 +02:00
parent 3654e36a7b
commit bb34a3f2ec

View File

@@ -124,12 +124,16 @@ PROMPT="${PROMPT}
The git diff is at /tmp/pi-diff.txt. Start by reading it, then read any files you need for full context." The git diff is at /tmp/pi-diff.txt. Start by reading it, then read any files you need for full context."
# Run Pi in print mode (non-interactive, no session persistence) # Run Pi in print mode.
# Capture stderr (tool call log) always — only show it when debug is on # Always save session to /tmp/pi-session so we can extract tool calls.
pi --no-session \ # Capture stderr for error diagnostics.
mkdir -p /tmp/pi-session
pi \
${PROVIDER_FLAG} \ ${PROVIDER_FLAG} \
--model "${PI_MODEL}" \ --model "${PI_MODEL}" \
--tools "${PI_TOOLS}" \ --tools "${PI_TOOLS}" \
--session-dir /tmp/pi-session \
-p "${PROMPT}" \ -p "${PROMPT}" \
> /tmp/pi-review.md 2>/tmp/pi-agent.log > /tmp/pi-review.md 2>/tmp/pi-agent.log
@@ -140,23 +144,59 @@ if [ ! -s /tmp/pi-review.md ]; then
exit 1 exit 1
fi fi
# If debug mode, append the agent's tool log to the review # If debug mode, extract tool calls from session and append to review
if [ "${PI_DEBUG}" = "true" ]; then if [ "${PI_DEBUG}" = "true" ]; then
AGENT_LOG=$(sed 's/\x1b\[[0-9;]*m//g' /tmp/pi-agent.log | head -200) SESSION_FILE=$(find /tmp/pi-session -name '*.jsonl' -type f 2>/dev/null | head -1)
TOOL_LOG=""
if [ -n "${SESSION_FILE}" ]; then
# Extract tool-use entries: each line is a JSON object.
# Tool calls appear as messages with tool_use/function_call content.
TOOL_LOG=$(node -e "
const fs = require('fs');
const lines = fs.readFileSync('${SESSION_FILE}', 'utf8').trim().split('\n');
const entries = [];
for (const line of lines) {
try {
const entry = JSON.parse(line);
if (entry.type === 'message') {
const msg = entry.message;
// Model requesting tool use
if (msg.role === 'assistant' && msg.content) {
const parts = Array.isArray(msg.content) ? msg.content : [msg.content];
for (const part of parts) {
if (typeof part === 'object' && part.type === 'tool_use') {
const input = part.input || {};
const args = Object.entries(input).map(([k,v]) => k + '=' + JSON.stringify(v)).join(' ');
entries.push('[tool:' + part.name + '] ' + args);
}
}
}
}
} catch {}
}
console.log(entries.join('\n'));
" 2>/dev/null || echo "Could not parse session file")
fi
if [ -z "${TOOL_LOG}" ]; then
TOOL_LOG="(no tool calls found — agent may have answered from the diff alone)"
fi
cat >> /tmp/pi-review.md << LOGEOF cat >> /tmp/pi-review.md << LOGEOF
--- ---
<details> <details>
<summary>🔍 <strong>Agent Tool Log</strong> (debug)</summary> <summary>🔍 <strong>Agent Tool Calls</strong> (${PI_MODEL})</summary>
\`\`\` \`\`\`
${AGENT_LOG} ${TOOL_LOG}
\`\`\` \`\`\`
</details> </details>
LOGEOF LOGEOF
echo "Debug: agent log appended to review" echo "Debug: tool log appended to review"
fi fi
echo "Review generated successfully." echo "Review generated successfully."