fix(ai): prevent duplicate user message in AI JSON payload
Some checks failed
CI Code / Check spelling (pull_request) Successful in 17s
CI Code / Check coding style (pull_request) Successful in 32s
CI Code / Code Coverage (pull_request) Failing after 1m9s
CI Code / Linux (debian) (pull_request) Failing after 2m53s
CI Code / Linux (ubuntu) (pull_request) Failing after 2m59s
CI Code / Linux (arch) (pull_request) Failing after 3m39s

The user message was being added to session->history before calling
_build_json_payload_from_list(), which independently appends the prompt
to the JSON payload. This caused the same message to appear twice in
the API request.

Fix: Build JSON payload first, then add user message to history under
the same lock to maintain atomicity.
This commit is contained in:
2026-05-14 03:50:12 +00:00
parent 5a074b280d
commit a9f0153c0f

View File

@@ -1441,20 +1441,18 @@ _ai_request_thread(gpointer data)
return NULL;
}
/* Add user message to history FIRST (under lock) */
/* Build JSON payload from history + prompt (under lock), then add user message to history */
log_debug("[AI-THREAD] Building JSON payload...");
pthread_mutex_lock(&session->lock);
auto_gchar gchar* json_payload = _build_json_payload_from_list(local_model, session->history, prompt);
/* Add user message to history now (it's in JSON but not yet in history) */
AIMessage* user_msg = g_new0(AIMessage, 1);
user_msg->role = g_strdup("user");
user_msg->content = g_strdup(prompt);
session->history = g_list_append(session->history, user_msg);
pthread_mutex_unlock(&session->lock);
log_debug("[AI-THREAD] Added user message to history");
/* Build JSON payload from local history copy (under lock) */
log_debug("[AI-THREAD] Building JSON payload...");
pthread_mutex_lock(&session->lock);
auto_gchar gchar* json_payload = _build_json_payload_from_list(local_model, session->history, prompt);
pthread_mutex_unlock(&session->lock);
log_debug("[AI-THREAD] JSON payload: %s", json_payload);
/* Build headers using shared helper */