diff --git a/backend/app/gateway/routers/thread_runs.py b/backend/app/gateway/routers/thread_runs.py index 26a56aed26..4a48f64f30 100644 --- a/backend/app/gateway/routers/thread_runs.py +++ b/backend/app/gateway/routers/thread_runs.py @@ -738,8 +738,8 @@ async def _find_target_run_id( if len(rows) >= REGENERATE_HISTORY_SCAN_LIMIT: logger.warning( "Could not find source run for regenerate message %s in recent run events for thread %s (limit=%s)", - message_id, - thread_id, + sanitize_log_param(message_id), + sanitize_log_param(thread_id), REGENERATE_HISTORY_SCAN_LIMIT, ) raise HTTPException(status_code=409, detail="Could not find source run for assistant message") @@ -807,8 +807,8 @@ async def _find_base_checkpoint_before_human( if len(checkpoints) >= REGENERATE_HISTORY_SCAN_LIMIT: logger.warning( "Could not locate target user message %s in recent checkpoint history for thread %s (limit=%s)", - human_message_id, - thread_id, + sanitize_log_param(human_message_id), + sanitize_log_param(thread_id), REGENERATE_HISTORY_SCAN_LIMIT, ) raise HTTPException( diff --git a/backend/tests/test_thread_regenerate_prepare.py b/backend/tests/test_thread_regenerate_prepare.py index 76cf24eee8..14377abb60 100644 --- a/backend/tests/test_thread_regenerate_prepare.py +++ b/backend/tests/test_thread_regenerate_prepare.py @@ -1717,6 +1717,62 @@ def test_prepare_regenerate_payload_reports_recent_checkpoint_scan_limit(): assert checkpointer.alist_limits == [400] +# Message ids come from checkpoint state, and run input keeps a client's id +# verbatim, so a thread owner controls them. A newline in one must not start a +# second line in the Gateway log. +_FORGED_LOG_LINE = "\n2026-01-01 00:00:00 INFO app.gateway.auth: forged line\r" + + +def test_prepare_regenerate_run_event_scan_warning_strips_newlines_from_message_id(caplog): + from app.gateway.routers.thread_runs import REGENERATE_HISTORY_SCAN_LIMIT, _prepare_regenerate_payload + + ai_id = f"ai-1{_FORGED_LOG_LINE}" + human = HumanMessage(id="human-1", content="question") + ai = AIMessage(id=ai_id, content="answer") + checkpointer = FakeCheckpointer([_checkpoint("ckpt-ai", [human, ai])]) + unrelated_events = [{"run_id": "run-other", "event_type": "llm.ai.response", "category": "message", "content": {"id": "ai-other", "type": "ai", "content": "other"}}] * REGENERATE_HISTORY_SCAN_LIMIT + + with caplog.at_level("WARNING", logger="app.gateway.routers.thread_runs"), pytest.raises(HTTPException) as exc: + asyncio.run(_prepare_regenerate_payload("thread-1", ai_id, _request(checkpointer, FakeEventStore(unrelated_events)))) + + assert exc.value.status_code == 409 + [record] = [r for r in caplog.records if r.getMessage().startswith("Could not find source run for regenerate message")] + logged = record.getMessage() + assert "\n" not in logged and "\r" not in logged + assert "ai-12026-01-01 00:00:00 INFO app.gateway.auth: forged line in recent run events" in logged + + +def test_prepare_regenerate_checkpoint_scan_warning_strips_newlines_from_human_message_id(caplog): + from app.gateway.routers.thread_runs import _prepare_regenerate_payload + + human_id = f"human-1{_FORGED_LOG_LINE}" + human = HumanMessage(id=human_id, content="question") + ai = AIMessage(id="ai-1", content="answer") + latest = _checkpoint("ckpt-latest", [human, ai]) + history_without_human = [_checkpoint(f"ckpt-{index}", []) for index in range(201)] + checkpointer = FakeCheckpointer(history_without_human, latest=latest) + event_store = FakeEventStore( + [ + { + "run_id": "run-old", + "event_type": "llm.ai.response", + "category": "message", + "content": {"id": "ai-1", "type": "ai", "content": "answer"}, + "metadata": {"caller": "lead_agent"}, + } + ] + ) + + with caplog.at_level("WARNING", logger="app.gateway.routers.thread_runs"), pytest.raises(HTTPException) as exc: + asyncio.run(_prepare_regenerate_payload("thread-1", "ai-1", _request(checkpointer, event_store))) + + assert exc.value.status_code == 409 + [record] = [r for r in caplog.records if r.getMessage().startswith("Could not locate target user message")] + logged = record.getMessage() + assert "\n" not in logged and "\r" not in logged + assert "human-12026-01-01 00:00:00 INFO app.gateway.auth: forged line in recent checkpoint history" in logged + + def test_find_base_checkpoint_ignores_duration_only_checkpoints() -> None: from app.gateway.routers.thread_runs import _find_base_checkpoint_before_human