Fix finalization cleanup for pending tool executions

This commit is contained in:
temp
2026-08-23 19:57:17 +08:00
parent bf761e9cd5
commit 3bcf4458c5
13 changed files with 522 additions and 81 deletions
+28 -4
View File
@@ -1538,6 +1538,11 @@ LIMIT 1`, messageID).Scan(&terminalEvent, &terminalCreatedAt)
return nil, fmt.Errorf("统计工具调用详情失败: %w", err)
}
pendingToolStatus := "result_missing"
if summary.Status == "running" {
pendingToolStatus = "running"
}
execRows, err := db.Query(
"SELECT id, event_type, data FROM process_details WHERE message_id = ? AND event_type IN ('tool_call', 'tool_result') ORDER BY created_at ASC, rowid ASC",
messageID,
@@ -1578,10 +1583,10 @@ LIMIT 1`, messageID).Scan(&terminalEvent, &terminalCreatedAt)
ProcessDetailID: strings.TrimSpace(detailID),
ToolName: toolName,
ToolCallID: toolCallID,
// This summary is reconstructed from persisted history, not live
// execution state. Until a matching result is found the honest state
// is "result_missing", never "running".
Status: "result_missing",
// This summary is reconstructed from persisted history. For an
// active assistant turn, a missing result means the call is still
// pending; after the turn is terminal it is genuinely incomplete.
Status: pendingToolStatus,
})
matchedToolIndexes = append(matchedToolIndexes, false)
if toolCallID != "" {
@@ -1636,6 +1641,7 @@ LIMIT 1`, messageID).Scan(&terminalEvent, &terminalCreatedAt)
return nil, fmt.Errorf("遍历工具执行摘要失败: %w", err)
}
execRows.Close()
db.applyPersistedToolExecutionStatuses(summary.ToolExecutions)
rows, err := db.Query(
"SELECT data FROM process_details WHERE message_id = ? AND event_type = 'iteration' ORDER BY created_at ASC, rowid ASC",
@@ -1704,6 +1710,24 @@ func toolResultStatusFromPayload(payload map[string]interface{}, eventType strin
return "completed"
}
func (db *DB) applyPersistedToolExecutionStatuses(executions []ProcessDetailsToolExecution) {
for i := range executions {
execID := strings.TrimSpace(executions[i].ExecutionID)
if execID == "" {
continue
}
var status string
if err := db.QueryRow(`SELECT status FROM tool_executions WHERE id = ?`, execID).Scan(&status); err != nil {
continue
}
status = strings.ToLower(strings.TrimSpace(status))
if status == "" {
continue
}
executions[i].Status = status
}
}
func matchToolExecutionIndex(
executions []ProcessDetailsToolExecution,
matched []bool,