Skip to content

[None][fix] Enforce Responses conversation history capacity - #15043

Open
fallintoplace wants to merge 1 commit into
NVIDIA:mainfrom
fallintoplace:fix/responses-history-capacity
Open

[None][fix] Enforce Responses conversation history capacity#15043
fallintoplace wants to merge 1 commit into
NVIDIA:mainfrom
fallintoplace:fix/responses-history-capacity

Conversation

@fallintoplace

@fallintoplace fallintoplace commented Jun 6, 2026

Copy link
Copy Markdown

Description

ConversationHistoryStore is meant to cap the number of stored messages per conversation, but two store paths were trimming through a response id that either was not mapped yet or skipped trimming after appending final output messages.

This changes trimming to operate on the conversation id after the store path has selected the conversation. That keeps normal stored Responses requests within conversation_capacity and avoids the previous-response append path spinning without making progress.

Test Coverage

  • Added focused unit coverage in tests/unittest/llmapi/test_responses_utils.py for the pre-stored request path and previous-response append path.
  • pre-commit run --files tensorrt_llm/serve/responses_utils.py tests/unittest/llmapi/test_responses_utils.py
  • python3 -m py_compile tests/unittest/llmapi/test_responses_utils.py
  • Not run locally: python3 -m pytest tests/unittest/llmapi/test_responses_utils.py because this local Python environment does not have pytest installed.

PR Checklist

  • Please check this after reviewing the above items as appropriate for this PR.

Summary by CodeRabbit

  • Refactor

    • Reorganized conversation history management logic for improved maintainability.
  • Tests

    • Added unit tests for conversation history trimming functionality.

@fallintoplace
fallintoplace requested a review from a team as a code owner June 6, 2026 14:00
@fallintoplace
fallintoplace requested a review from hchings June 6, 2026 14:00
@coderabbitai

coderabbitai Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR refactors ConversationHistoryStore to extract inline conversation trimming logic into dedicated helper methods and adds comprehensive unit tests for the trimming behavior. The refactoring centralizes capacity-based history removal while maintaining the same behavior.

Changes

Conversation History Trimming Refactor

Layer / File(s) Summary
Trimming helper methods and refactored call sites
tensorrt_llm/serve/responses_utils.py
_pop_conversation now delegates to _pop_conversation_by_conversation_id. New _trim_conversation(conversation_id) method loops to repeatedly remove messages until conversation size is within conversation_capacity. store_response and store_messages call sites are updated to use _trim_conversation instead of inline while loops.
Unit tests for conversation trimming
tests/unittest/llmapi/test_responses_utils.py
New test module with _turns(count) helper generating alternating user/assistant message sequences. test_store_response_trims_pre_stored_request_conversation verifies conversation history length is capped after storing responses. test_store_response_trims_previous_response_conversation validates trimming on chained responses and verifies response-to-conversation mapping consistency using monkeypatched _pop_conversation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 27.27% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: enforcing conversation history capacity in the Responses module through a bug fix to trimming logic.
Description check ✅ Passed The PR description clearly explains the issue (trimming via unmapped response ids), the solution (trim by conversation id), and provides specific test coverage details with verification steps performed.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
tensorrt_llm/serve/responses_utils.py (1)

372-377: ⚡ Quick win

Add parameter type annotations to new helper methods.

The new helpers introduce untyped parameters; please type conversation_id (and align _pop_conversation/_pop_conversation_by_conversation_id signatures consistently) to match repo typing standards.

Suggested patch
-    def _pop_conversation(self, resp_id) -> None:
+    def _pop_conversation(self, resp_id: str) -> None:
@@
-    def _trim_conversation(self, conversation_id) -> None:
+    def _trim_conversation(self, conversation_id: str) -> None:
         while len(self.conversations[conversation_id]
                   ) > self.conversation_capacity:
             self._pop_conversation_by_conversation_id(conversation_id)

-    def _pop_conversation_by_conversation_id(self, conversation_id) -> None:
+    def _pop_conversation_by_conversation_id(self, conversation_id: str) -> None:

As per coding guidelines, “Always annotate functions; make the return type None if the function does not return anything.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tensorrt_llm/serve/responses_utils.py` around lines 372 - 377, The helper
methods lack parameter type annotations; update
_trim_conversation(conversation_id) and
_pop_conversation_by_conversation_id(conversation_id) to annotate
conversation_id with the same type used by the existing _pop_conversation
signature (e.g., the repo's ConversationId type or Union[str, int]) and keep the
return type as -> None; ensure both signatures are consistent with each other
and the project's typing conventions.

Source: Coding guidelines

tests/unittest/llmapi/test_responses_utils.py (1)

57-60: ⚡ Quick win

Strengthen trimming assertions to verify latest output is preserved, not only capped.

Both tests currently assert only len(conversation) <= conversation_capacity; they can miss over-trimming regressions that drop the newly appended assistant output. Add explicit checks that "final" / "next" remains in the stored history after trimming.

Suggested patch
@@
     conversation = await store.get_conversation_history("resp_1")

     assert len(conversation) <= store.conversation_capacity
+    assert any(
+        msg.get("role") == "assistant" and msg.get("content") == "final"
+        for msg in conversation
+    )
@@
     conversation = await store.get_conversation_history("resp_next")

     assert len(conversation) <= store.conversation_capacity
+    assert any(
+        msg.get("role") == "assistant" and msg.get("content") == "next"
+        for msg in conversation
+    )
     assert (
         store.response_to_conversation["resp_next"] == store.response_to_conversation["resp_prev"]
     )

As per coding guidelines for tests/**, coverage review should be actionable and explicit about sufficiency; this is a targeted follow-up to make trimming coverage sufficient for data-retention behavior.

Also applies to: 86-89

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unittest/llmapi/test_responses_utils.py` around lines 57 - 60, The test
currently only checks trimming by asserting len(conversation) <=
store.conversation_capacity; update the assertions to also verify the
most-recent assistant output is retained after trimming by checking that the
saved conversation (from await store.get_conversation_history("resp_1") and the
analogous call in the second test) contains the expected assistant message text
("final" in the first case and "next" in the second). Keep the existing length
check, then add an explicit membership/assertion against conversation entries
(or their text field) to ensure the latest assistant output is present after
trimming.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tensorrt_llm/serve/responses_utils.py`:
- Around line 372-377: The helper methods lack parameter type annotations;
update _trim_conversation(conversation_id) and
_pop_conversation_by_conversation_id(conversation_id) to annotate
conversation_id with the same type used by the existing _pop_conversation
signature (e.g., the repo's ConversationId type or Union[str, int]) and keep the
return type as -> None; ensure both signatures are consistent with each other
and the project's typing conventions.

In `@tests/unittest/llmapi/test_responses_utils.py`:
- Around line 57-60: The test currently only checks trimming by asserting
len(conversation) <= store.conversation_capacity; update the assertions to also
verify the most-recent assistant output is retained after trimming by checking
that the saved conversation (from await store.get_conversation_history("resp_1")
and the analogous call in the second test) contains the expected assistant
message text ("final" in the first case and "next" in the second). Keep the
existing length check, then add an explicit membership/assertion against
conversation entries (or their text field) to ensure the latest assistant output
is present after trimming.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 22a070f0-e030-4632-8673-40623139fa76

📥 Commits

Reviewing files that changed from the base of the PR and between e47f26e and 4ad810d.

📒 Files selected for processing (2)
  • tensorrt_llm/serve/responses_utils.py
  • tests/unittest/llmapi/test_responses_utils.py

@hchings hchings left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for catching this issue.

@hchings

hchings commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53477 [ run ] triggered by Bot. Commit: 4ad810d Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53477 [ run ] completed with state SUCCESS. Commit: 4ad810d
/LLM/main/L0_MergeRequest_PR pipeline #42640 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@hchings
hchings force-pushed the fix/responses-history-capacity branch from 4ad810d to 3910b63 Compare June 11, 2026 21:45
@hchings

hchings commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53692 [ run ] triggered by Bot. Commit: 3910b63 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53692 [ run ] completed with state SUCCESS. Commit: 3910b63
/LLM/main/L0_MergeRequest_PR pipeline #42827 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@hchings

hchings commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #54233 [ run ] triggered by Bot. Commit: 3910b63 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #54233 [ run ] completed with state SUCCESS. Commit: 3910b63
/LLM/main/L0_MergeRequest_PR pipeline #43309 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@hchings

hchings commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #54743 [ run ] triggered by Bot. Commit: 3910b63 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #54743 [ run ] completed with state FAILURE. Commit: 3910b63
/LLM/main/L0_MergeRequest_PR pipeline #43762 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

@hchings
hchings force-pushed the fix/responses-history-capacity branch from 3910b63 to eb06536 Compare June 23, 2026 22:23
@hchings

hchings commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

/bot run

@hchings
hchings enabled auto-merge (squash) June 23, 2026 22:24
@fallintoplace

Copy link
Copy Markdown
Author

@hchings Thank you for your attention Erin.

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55333 [ run ] triggered by Bot. Commit: eb06536 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55333 [ run ] completed with state FAILURE. Commit: eb06536
/LLM/main/L0_MergeRequest_PR pipeline #44284 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@hchings
hchings force-pushed the fix/responses-history-capacity branch from eb06536 to be7ab43 Compare June 25, 2026 22:06
@hchings

hchings commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55898 [ run ] triggered by Bot. Commit: be7ab43 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55898 [ run ] completed with state FAILURE. Commit: be7ab43
/LLM/main/L0_MergeRequest_PR pipeline #44783 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@fallintoplace

Copy link
Copy Markdown
Author

Not sure why CI is still failing. Let me know if I need to do anything.

@hchings

hchings commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

/bot run

@hchings

hchings commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Not sure why CI is still failing. Let me know if I need to do anything.

Our CI is not stable lately.

@hchings
hchings force-pushed the fix/responses-history-capacity branch from be7ab43 to d4daeb2 Compare July 8, 2026 22:23
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58312 [ run ] triggered by Bot. Commit: d4daeb2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58312 [ run ] completed with state SUCCESS. Commit: d4daeb2
/LLM/main/L0_MergeRequest_PR pipeline #46943 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@fallintoplace

fallintoplace commented Jul 9, 2026

Copy link
Copy Markdown
Author

CI is failing again, sadly. If this was related to this PR, I will have a look.

@hchings

hchings commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Hi @fallintoplace can you rebase and check whether the following two cases are related to your MRs? They doesn't look related to me from a quick glance. We have some CI fixes in latest main branch.

`A30-PyTorch-2.unittest._torch.modeling.test_modeling_qwen3_5_vl.test_qwen35_dense_vl_resolves_mamba_ssm_cache_dtype (from A30-PyTorch-2)

Failing for the past 1 build (Since Failed#46943 )
Took 23 ms.
Error Message
AssertionError: assert torch.bfloat16 is torch.float32

  • where torch.bfloat16 = QuantConfig(quant_algo=None, kv_cache_quant_algo=None, group_size=128, smoothquant_val=0.5, clamp_val=None, use_meta_recipe=False, has_zero_point=False, pre_quant_scale=False, exclude_modules=None, mamba_ssm_cache_dtype=torch.bfloat16, mamba_ssm_stochastic_rounding=False, mamba_ssm_philox_rounds=10).mamba_ssm_cache_dtype
  • where QuantConfig(quant_algo=None, kv_cache_quant_algo=None, group_size=128, smoothquant_val=0.5, clamp_val=None, use_meta_recipe=False, has_zero_point=False, pre_quant_scale=False, exclude_modules=None, mamba_ssm_cache_dtype=torch.bfloat16, mamba_ssm_stochastic_rounding=False, mamba_ssm_philox_rounds=10) = <[TypeError('Object of type function is not JSON serializable') raised in repr()] ModelConfig object at 0x7fa4bf97b260>.quant_config
  • and torch.float32 = torch.float32
    Stacktrace
    unittest/_torch/modeling/test_modeling_qwen3_5_vl.py:147: in test_qwen35_dense_vl_resolves_mamba_ssm_cache_dtype
    assert model_config.quant_config.mamba_ssm_cache_dtype is torch.float32
    E AssertionError: assert torch.bfloat16 is torch.float32
    E + where torch.bfloat16 = QuantConfig(quant_algo=None, kv_cache_quant_algo=None, group_size=128, smoothquant_val=0.5, clamp_val=None, use_meta_recipe=False, has_zero_point=False, pre_quant_scale=False, exclude_modules=None, mamba_ssm_cache_dtype=torch.bfloat16, mamba_ssm_stochastic_rounding=False, mamba_ssm_philox_rounds=10).mamba_ssm_cache_dtype
    E + where QuantConfig(quant_algo=None, kv_cache_quant_algo=None, group_size=128, smoothquant_val=0.5, clamp_val=None, use_meta_recipe=False, has_zero_point=False, pre_quant_scale=False, exclude_modules=None, mamba_ssm_cache_dtype=torch.bfloat16, mamba_ssm_stochastic_rounding=False, mamba_ssm_philox_rounds=10) = <[TypeError('Object of type function is not JSON serializable') raised in repr()] ModelConfig object at 0x7fa4bf97b260>.quant_config
    E + and torch.float32 = torch.float32`

Signed-off-by: Minh Vu <vuhoangminh97@gmail.com>
auto-merge was automatically disabled July 14, 2026 00:55

Head branch was pushed to by a user without write access

@fallintoplace
fallintoplace force-pushed the fix/responses-history-capacity branch from d4daeb2 to db5bb7c Compare July 14, 2026 00:55
@fallintoplace

Copy link
Copy Markdown
Author

Hi Erin, thank you for your time. Rebased onto latest main. The reported Qwen/Mamba failure is unrelated to this PR and is now explicitly waived upstream by #16186. The branch is updated at db5bb7c. @hchings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants