mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-08-29 06:00:52 +02:00
Fix finalization cleanup for pending tool executions
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -165,6 +165,32 @@ func TestProcessDetailsSummaryDoesNotReportPersistedOrphanAsRunning(t *testing.T
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessDetailsSummaryReportsUnmatchedToolCallAsRunningForActiveTurn(t *testing.T) {
|
||||
db, conversationID, messageID := setupProcessDetailsSummaryTest(t)
|
||||
if _, err := db.Exec(
|
||||
"UPDATE messages SET content = ?, updated_at = ? WHERE id = ?",
|
||||
"处理中...", "2026-08-10T08:00:00Z", messageID,
|
||||
); err != nil {
|
||||
t.Fatalf("update running message: %v", err)
|
||||
}
|
||||
if err := db.AddProcessDetail(messageID, conversationID, "tool_call", "call", map[string]interface{}{
|
||||
"toolName": "execute", "toolCallId": "pending",
|
||||
}); err != nil {
|
||||
t.Fatalf("AddProcessDetail(tool_call): %v", err)
|
||||
}
|
||||
|
||||
summary, err := db.GetProcessDetailsSummary(messageID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetProcessDetailsSummary: %v", err)
|
||||
}
|
||||
if summary.Status != "running" {
|
||||
t.Fatalf("summary status = %q, want running", summary.Status)
|
||||
}
|
||||
if len(summary.ToolExecutions) != 1 || summary.ToolExecutions[0].Status != "running" {
|
||||
t.Fatalf("tool executions = %#v, want running", summary.ToolExecutions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessDetailsSummaryIncludesPersistedTurnTiming(t *testing.T) {
|
||||
db, _, messageID := setupProcessDetailsSummaryTest(t)
|
||||
startedAt := "2026-08-10T08:00:00Z"
|
||||
|
||||
Reference in New Issue
Block a user