Skip to content

Handle multi-block sampling responses in SampleStructuredAsync#52

Merged
m-nash merged 1 commit into
mainfrom
fix/sampling-multiblock-json
Jul 2, 2026
Merged

Handle multi-block sampling responses in SampleStructuredAsync#52
m-nash merged 1 commit into
mainfrom
fix/sampling-multiblock-json

Conversation

@m-nash

@m-nash m-nash commented Jul 2, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes a sampling failure where monitoring a PR errors with Sampling failed to explain comment after 2 attempts / 'e' is invalid after a value.

The Copilot sampling backend can return the assistant answer across multiple content blocks — sometimes a truncated false-start block followed by the complete answer. ExtractText joined all blocks with \n, fusing them into invalid JSON (e.g. {"explanation": "...ind\n{"explanation": ...}), which then failed to deserialize.

Changes

  • SampleStructuredAsync now tries each content block individually (last→first) — the final block is usually the complete answer — then falls back to the joined text for providers that split one object across blocks. First candidate that deserializes into T wins.
  • Extracted SampleRawAsync, ExtractTextBlocks, and a TryDeserialize<T> helper. Added blocks=N diagnostic logging.
  • SampleTextAsync / ExtractText (plain-text path) keep the existing join behavior.
  • FakeSamplingMcpServer gains a params string[] constructor to simulate multi-block responses.

Tests

Three new tests in SamplingHelperTests:

  • MultipleBlocks_UsesCompleteBlock — truncated first + complete second (the exact failure)
  • MultipleBlocks_CompleteBlockFirst — either ordering works
  • MultipleBlocks_OneObjectSplitAcrossBlocks_FallsBackToJoin — back-compat

All 350 tests pass; the two multi-block tests are verified to fail against the previous code. Also validated live locally via a dev build before opening this PR.

The client can split a sampling answer across multiple content blocks,
sometimes a truncated false-start followed by the complete answer. Joining
them blindly produced invalid JSON. Try each block individually (last first),
then fall back to the joined text.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a structured-sampling parsing failure when the MCP sampling backend returns assistant output across multiple content blocks (e.g., a truncated false start followed by a complete JSON answer). The update changes SampleStructuredAsync to try deserializing each block individually (preferring the last block) before falling back to deserializing the joined text, preventing invalid JSON formed by naive concatenation.

Changes:

  • Update SampleStructuredAsync to attempt JSON deserialization per content block (last→first), then fall back to joined blocks when needed.
  • Refactor sampling helpers (SampleRawAsync, ExtractTextBlocks, TryDeserialize<T>) and add blocks=N diagnostics.
  • Add tests covering multi-block behaviors and extend FakeSamplingMcpServer to simulate multi-block responses.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
PrCopilot/tests/PrCopilot.Tests/SamplingHelperTests.cs Adds coverage for multi-block structured sampling responses (complete block preference + join fallback).
PrCopilot/tests/PrCopilot.Tests/FakeSamplingMcpServer.cs Enhances the test MCP server to emit multiple TextContentBlocks via a params string[] constructor.
PrCopilot/src/PrCopilot/Tools/SamplingHelper.cs Implements block-aware structured parsing with per-block deserialization attempts and a join fallback for split objects.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@m-nash m-nash merged commit d3bc877 into main Jul 2, 2026
8 checks passed
@m-nash m-nash deleted the fix/sampling-multiblock-json branch July 2, 2026 17:54
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