Skip to content

Preserve assistant turn boundaries and cancel host-ended generation - #1112

Open
bmehta001 wants to merge 2 commits into
mainfrom
bmehta001/fix-chat-turn-boundaries
Open

bmehta001 wants to merge 2 commits into
mainfrom
bmehta001/fix-chat-turn-boundaries

Conversation

@bmehta001

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #1083 addressing the two remaining material review findings.

  • Keep generated output in its own assistant message. The native template already opens a new assistant turn with add_generation_prompt=true; merging the reply into trailing assistant input changed the conversation on subsequent turns. Remove the corresponding guard seeding and preserve the same input/output boundary when replaying stored Responses chains.
  • Cancel host-ended generation before retrieving backend usage. Engine generation is asynchronous, and usage retrieval waits for completion. Apply this ordering to both typed requests and Chat Completions JSON, including post-tool-call text, stop strings, callback cancellation, and applicable host token limits. Tool cutoffs still finish as tool_calls, and truncated raw-envelope output is not treated as naturally complete.

The existing adjacency grouping within supplied input and within one generated output remains unchanged.

Regression coverage

  • Native Qwen template boundaries, transcript commits, empty/reasoning-only replies, tool calls, participant names, undo, and warm/cold replay equivalence.
  • Supplied tool calls do not suppress a new reply's visible text, including streaming on both request paths.
  • Deterministic cancellation-before-usage checks for streaming and non-streaming host cutoffs, without a regression hanging the test.

Validation

Environment Result
Windows, Visual Studio 2026, RelWithDebInfo Build succeeded; 322 targeted C++ tests passed
Linux, Ubuntu/WSL, RelWithDebInfo Build succeeded; the same 322 targeted C++ tests passed
Repository lintrunner and git diff --check Passed

Both runs include all 11 checked-in CPU Engine lifecycle cases and the existing raw-envelope finalization coverage. No selected tests were skipped or disabled, and no negative test filters were used. Both builds use build.py and the repository's pinned native dependencies.

Preserve the assistant turn opened by native prompt rendering instead of
merging generated output into supplied assistant history. Separate stored
hop input and output so cold replay matches the live transcript.

Cancel abandoned backend generation before waiting for final usage so
tool dispatch does not wait for discarded tokens to reach EOS.

Files changed:
- sdk_v2/cpp/src/inferencing/generative/chat/chat_session.cc
- sdk_v2/cpp/src/inferencing/generative/chat/chat_transcript.cc
- sdk_v2/cpp/src/inferencing/generative/chat/chat_transcript.h
- sdk_v2/cpp/src/inferencing/generative/openresponses/response_converter.cc
- sdk_v2/cpp/src/inferencing/session/request.h
- sdk_v2/cpp/test/internal_api/chat/chat_session_test.cc
- sdk_v2/cpp/test/internal_api/chat/chat_transcript_test.cc
- sdk_v2/cpp/test/internal_api/replay_equivalence_test.cc

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 28c98e1b-686e-4f48-ac63-47e6c0ceb192
Copilot AI balanced review requested due to automatic review settings September 16, 2026 21:12
@vercel

vercel Bot commented Sep 16, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
foundry-local Ready Ready Preview Sep 16, 2026 10:08pm UTC

Request Review

Copilot AI 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.

🟡 Changes recommended

Three added lines exceed the mandatory 120-character C++ line limit.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Preserves assistant-turn boundaries during live generation and replay, while safely canceling asynchronous host-ended generation before usage retrieval.

Changes:

  • Separates supplied assistant input from generated replies.
  • Adds cancellation ordering for typed and Chat Completions requests.
  • Expands boundary, streaming, cutoff, and replay regression tests.
File summaries
File Description
replay_equivalence_test.cc Tests warm/cold boundary equivalence.
chat_transcript_test.cc Tests separate assistant messages and undo behavior.
chat_session_test.cc Tests native templates, streaming, and cancellation ordering.
request.h Clarifies replay segment semantics.
response_converter.cc Separates replayed input and output segments.
chat_transcript.h Updates transcript boundary API and documentation.
chat_transcript.cc Commits generated output as a separate message.
chat_session.cc Cancels host-ended generation before usage retrieval.
Review details

Suppressed comments (1)

sdk_v2/cpp/src/inferencing/generative/chat/chat_session.cc:1445

  • This added line is 123 characters long, exceeding the repository's 120-character C++ line limit. Wrap the arguments to keep the call within the project limit.
                                     original_request.canceled, stop_sequence_matched, /*host_output_limit_reached=*/false,
  • Files reviewed: 8/8 changed files
  • Comments generated: 2
  • Review effort level: Balanced (auto)

Note

Copilot is running an experiment and ran this review at Balanced.


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread sdk_v2/cpp/src/inferencing/generative/chat/chat_session.cc Outdated
Comment thread sdk_v2/cpp/test/internal_api/chat/chat_session_test.cc Outdated
@bmehta001 bmehta001 self-assigned this Sep 16, 2026
Address Copilot comment 4030853444 by wrapping both finalization calls
in chat_session.cc (original lines 1138 and 1445).

Address Copilot comment 4030853511 by placing the 12 test cases on
separate lines in chat_session_test.cc (original line 1195). Keep a
trailing initializer comma so clang-format does not collapse the matrix.

Files changed:
- sdk_v2/cpp/src/inferencing/generative/chat/chat_session.cc
- sdk_v2/cpp/test/internal_api/chat/chat_session_test.cc

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 28c98e1b-686e-4f48-ac63-47e6c0ceb192

Copilot AI 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.

🟢 Approval recommended

The implementation consistently preserves turn boundaries and covers host-ending paths with focused regression tests.

Review details
  • Files reviewed: 8/8 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced (auto)

Note

Copilot is running an experiment and ran this review at Balanced.

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.

2 participants