Repository navigation
Prototype opt-in scroll-region progress renderer to keep console output intact - #12606
Amaury Levé (Evangelink) wants to merge 2 commits into
Conversation
Pin interactive progress to rows reserved at the bottom of the terminal with a DECSTBM scroll region, so output written by the code under test scrolls above the progress instead of being overwritten by cursor-up redraws. Opt in with TESTINGPLATFORM_PROGRESS_SCROLL_REGION=1. No Console.Out interception and no new locks: frames are emitted as a single atomic console write, and the lock order (reporter lock, then Console.Out) is unchanged from the classic renderer. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
- Publish pinned-row state only after the batch is written, and let the ProcessExit handler wait (bounded) for another thread's batch before claiming it, so exactly one path writes each release. - Stop redrawing progress after teardown erased it, under the terminal lock. - Rename the opt-in switch to MTP_PROGRESS_SCROLL_REGION and accept the standard on values. - Add the decision record, glossary entry, and changelog entry. Relates to #4425 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
|
||
| public void Write(StringBuilder value) => _output.Append(value.ToString()); | ||
|
|
||
| public void WriteAtomically(StringBuilder value) => _output.Append(value.ToString()); |
| public void WriteAtomically(StringBuilder value) | ||
| { | ||
| AtomicWriteCount++; | ||
| _output.Append(value.ToString()); |
| catch | ||
| { | ||
| // The process is exiting, so nothing can be done when the console cannot be written to. | ||
| } |
| catch | ||
| { | ||
| // The process is exiting, so nothing can be done when the console cannot be written to. | ||
| } |
| catch | ||
| { | ||
| // Treat a window whose size cannot be read as a window of unknown size. | ||
| return int.MaxValue; | ||
| } |
There was a problem hiding this comment.
🟡 Changes recommended
Exit cleanup can hang indefinitely on Unix when another thread holds Console.Out because the 250 ms timeout does not bound subsequent geometry and output calls.
1 open issue
What changed in this PR
Adds an opt-in Microsoft.Testing.Platform progress renderer intended to preserve direct console output, addressing #4425 while remaining a draft prototype.
Changes:
- Reserves bottom terminal rows for progress using ANSI scroll regions and single-call output batches.
- Adds teardown and process-exit cleanup, and prevents stale progress redraws after teardown in both rendering modes.
- Adds configuration and lifecycle tests, plus documentation of experiments and limitations.
Test-value evidence: At e39480d0feb78f4ef960b88e67c32bdbb34bc99e, the author reports 558 passing targeted tests, 16 passing contract tests per framework, and six detected mutations. Execution artifacts were not independently verified; acceptance tests and broader live-terminal validation remain outstanding.
Confidence at a glance — 🟡 MTP terminal lifecycle
Static code and relevant .NET runtime locking behavior were inspected. No builds or tests were run during this review.
| File | Description |
|---|---|
test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/CTRLPlusCCancellationTokenSourceTests.cs |
Updates console fake for atomic writes. |
test/UnitTests/Microsoft.Testing.Platform.UnitTests/OutputDevice/Terminal/TerminalTestReporterTests.cs |
Tests scroll-region rendering, resize, and cleanup. |
test/UnitTests/Microsoft.Testing.Platform.UnitTests/OutputDevice/ProgressReportingConfigurationTests.cs |
Tests opt-in environment-variable parsing. |
test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/SystemWrappersTests.cs |
Updates recording console fake. |
test/UnitTests/Microsoft.Testing.Platform.UnitTests/DynamicExtensions/DynamicExtensionLoaderTests.cs |
Updates console fake for the new interface member. |
src/MTP/Microsoft.Testing.Platform/OutputDevice/TerminalOutputDevice.Initialization.cs |
Wires configuration into reporter options. |
src/MTP/Microsoft.Testing.Platform/OutputDevice/Terminal/TestProgressStateAwareTerminal.cs |
Serializes teardown and prevents later progress redraws. |
src/MTP/Microsoft.Testing.Platform/OutputDevice/Terminal/TerminalTestReporterOptions.cs |
Adds the scroll-region option. |
src/MTP/Microsoft.Testing.Platform/OutputDevice/Terminal/TerminalTestReporter.cs |
Passes the option to terminal and renderer. |
src/MTP/Microsoft.Testing.Platform/OutputDevice/Terminal/CursorProgressRenderer.cs |
Avoids erasing pinned progress before output. |
src/MTP/Microsoft.Testing.Platform/OutputDevice/Terminal/AnsiTerminalTestProgressFrame.Render.cs |
Collects and renders pinned progress rows. |
src/MTP/Microsoft.Testing.Platform/OutputDevice/Terminal/AnsiTerminal.cs |
Manages scroll regions, batches, and exit cleanup. |
src/MTP/Microsoft.Testing.Platform/OutputDevice/Terminal/AnsiCodes.cs |
Adds cursor and scroll-region escape sequences. |
src/MTP/Microsoft.Testing.Platform/OutputDevice/ProgressReportingConfiguration.cs |
Parses the experimental opt-in setting. |
src/MTP/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt |
Records new internal API members. |
src/MTP/Microsoft.Testing.Platform/Helpers/System/SystemConsole.cs |
Implements best-effort single-call stream writes. |
src/MTP/Microsoft.Testing.Platform/Helpers/System/IConsole.cs |
Defines the atomic-write abstraction. |
docs/glossary.md |
Documents experimental configuration. |
docs/decisions/scroll-region-progress.md |
Records design, experiments, risks, and validation gaps. |
docs/Changelog-Platform.md |
Announces the opt-in prototype. |
Review effort: Balanced
| AppendReleaseSequence(release, rows, pinnedHeight: (int)(state >> 32), TryGetHeight()); | ||
| _console.WriteAtomically(release); |
|
Final test-triage resolution — Azure Pipelines build 1696954 Status: inconclusive (evidence incomplete; 1 test-artifact download failed). No issue created. Failing leg: Windows Release, task "Test affected changes" (exit code 2). Signature: Assessment (medium confidence): timing-sensitive environmental one-off.
Other failed records in the results (the PR's own-change relevance is unverified). Failures appeared in Next step: Re-run the Windows Release leg. If the net462 test fails again with a similar margin, treat the 13s bound as too tight for slower Windows/.NET Framework agents. That would be a test-hardening issue, separate from this PR.
|

Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
e39480d0feb78f4ef960b88e67c32bdbb34bc99e↑ Mutations M1–M6 each turn the new tests red at this revision.
↑ Real conhost run: 0 of 106 user lines lost with the scroll region vs 27 lost with classic progress.
↓ No live run yet on Windows Terminal, VS Code, macOS or a Linux tty.
↓ xterm.js drops a pending wrap across a frame (known limitation).
↓ Small behavior change in the default renderer (no redraw after teardown erase).
↓ CI has not run on this head yet.
↑ Cause traced to the cursor-up erase and redraw of the progress frame landing on lines written directly to the console.
↓ The reporters' exact terminals and test frameworks are not confirmed.
Pull request
Summary
Important
Draft prototype, not ready to merge. The feature is opt-in (
MTP_PROGRESS_SCROLL_REGION=1). This PR stays in draft until every item in the Before marking ready for review checklist below is checked.Relates to #4425 (intentionally does not close it).
Problem. In an interactive ANSI terminal, the Microsoft.Testing.Platform progress renderer erases and redraws its frame by moving the cursor up. Test code that writes directly to
Console.Out,Console.Erroror native stdout bypasses MTP, so the next redraw lands on those lines and overwrites or erases them.--no-progressavoids it, at the cost of live progress.Change (Microsoft.Testing.Platform, opt-in). When
MTP_PROGRESS_SCROLL_REGIONis set to an on value (1,true,on,enable; trimmed, case-insensitive) and the cursor-based progress renderer is in use:CSI 1;n r). All other output, including direct console writes from tests, scrolls above it, so progress never moves the cursor over user output.IConsole.WriteAtomically.Console.Outinterception and no lock on the console write path.CSI r, then clear) on normal teardown and fromAppDomain.ProcessExit. The exit handler takes no lock: it waits up to 250 ms for a batch being written by another thread, then claims the written state withInterlocked.Exchange, so exactly one writer releases it.Without the environment variable the classic renderer is used, with the one change below.
Note
Default-mode change (review finding F2).
TestProgressStateAwareTerminalno longer redraws progress after teardown has erased it, and the teardown erase now runs under the existing_lock. This also applies to the default renderer: before, a write after teardown could redraw a stale progress frame.Before marking ready for review
--output detailed, multiple test assemblies, and hot reload. Assert terminal cells, not only captured bytes.WriteAtomically.CSI r).--progressvalue; if an option is added, update the--help/--infoacceptance expectations.IConsole.WriteAtomicallyto the vendored terminal-reporter copy in dotnet/sdk (itsIConsoleimplementations, such asCapturingConsole, must implement it).initaccessor on the internal options type.docs/Changelog-Platform.md.Known residual risks (detailed in the decision record):
ProcessExithandlers share about 2 s in total.Files changed:
AnsiTerminal.cs,AnsiCodes.cs,AnsiTerminalTestProgressFrame.Render.cs,CursorProgressRenderer.cs,TestProgressStateAwareTerminal.cs,TerminalTestReporter.cs,TerminalTestReporterOptions.cs,ProgressReportingConfiguration.cs,TerminalOutputDevice.Initialization.cs,IConsole.csandSystemConsole.cs, plusInternalAPI.Unshipped.txt.TerminalTestReporterTests.csand the newProgressReportingConfigurationTests.cs. Three testIConsolefakes gainWriteAtomically.Design and decision record
Decision record:
docs/decisions/scroll-region-progress.md. It covers the contract, the producer-consumer boundary with dotnet/sdk, the experiments, the review history and the open questions.Alternatives considered:
--no-progressor non-ANSI output. This is the existing workaround, but it loses live progress.Console.Outor native handles and never takes that lock.Console.Outearly with an MTP-owned writer that coordinates with the renderer. A probe kept all 5 of 5 lines, and a multi-model review converged on a design. It was rejected for two reasons. First, it must serialize arbitrary user writes against the renderer, which carries a high deadlock risk from writes in finalizers, inProcessExit, or under user locks. Second, it still misses native and handle-level writes.Demonstration
No screenshots or GIFs were captured; the results below are from reading the real terminal buffer back.
Original repro (#4425): TUnit 0.6.43 on MTP 1.4.3, 5 user lines written during the run. With progress on, 2 of 5 were visible; with
--no-progress, all 5 were.conhost (Windows). A probe test host runs 3 workers that write lines directly to
Console.Outwhile progress renders, then writes 45 lines after the run. It used the repository-builtMicrosoft.Testing.Platform.dll(hash-checked againstartifacts) in a freshconhost.exe. The screen buffer was read back withReadConsoleOutputCharacterW.MTP_PROGRESS_SCROLL_REGION=1Environment.Exit(7)mid-runLinux pty. A WSL Debian pty (120x30,
TERM=xterm-256color) was recorded and replayed through@xterm/headless5.5.0. Classic mode lost 28 lines and the scroll region lost 0. This ran before the last review fixes and has not been re-run since.Known xterm.js limitation (pending wrap). On a 10x6
@xterm/headless5.5.0 terminal with region1;4, the sequence is: writeAAAAAAAAAA(exactly fills the row, leaving a pending wrap), draw a progress frame, then writeB. The row ends asAAAAAAAAABinstead of wrapping. This happens because xterm.js clears the pending-wrap flag on cursor restore, while conhost preserves it. It only affects output that ends exactly at the right margin with no newline when a frame is drawn in between. It cannot be fixed without interception, and is an undraft decision item.Not yet inspected: Windows Terminal, the VS Code terminal (live), macOS Terminal, a Linux tty, resize, Ctrl+C,
--output detailed, multiple assemblies, and hot reload.Validation and regression proof
All runs used source state
e39480d0feb78f4ef960b88e67c32bdbb34bc99e, Debug, Windows, with the repository-local SDK.dotnet build src\MTP\Microsoft.Testing.Platform\Microsoft.Testing.Platform.csproj -c Debug --no-restore(all TFMs)dotnet run --project test\UnitTests\Microsoft.Testing.Platform.UnitTests -f net10.0 --no-build -- --filter "FullyQualifiedName~ProgressReportingConfigurationTests|FullyQualifiedName~Terminal"dotnet run --project test\UnitTests\Microsoft.Testing.Platform.TerminalReporterContract.UnitTests -f net10.0 --no-builddotnet run --project test\UnitTests\Microsoft.Testing.Platform.TerminalReporterContract.UnitTests -f net11.0 --no-buildnpx --yes markdownlint-cli2 docs/decisions/scroll-region-progress.md docs/glossary.md docs/Changelog-Platform.mdRequirement-to-test map:
AnsiTerminal_ScrollRegion_*unit tests inTerminalTestReporterTests.cs.TestProgressStateAwareTerminal_ScrollRegion_*tests.ProgressReportingConfigurationTests.CSI ryet; that is an undraft item.Not run: acceptance or integration tests (no
-pack), live runs on non-conhost terminals, macOS, Linux CI, and the dotnet/sdk vendored build.Test-value evidence
These are executed mutation checks at
e39480d0feb78f4ef960b88e67c32bdbb34bc99e. For each mutation, the production change was replaced in place, then the harness:dotnet build test\UnitTests\Microsoft.Testing.Platform.UnitTests\Microsoft.Testing.Platform.UnitTests.csproj -c Debug --no-restore -f net10.0(exit 0 in every case);dotnet run --project test\UnitTests\Microsoft.Testing.Platform.UnitTests -f net10.0 --no-build -- --filter "FullyQualifiedName~ScrollRegion|FullyQualifiedName~TestProgressStateAwareTerminal|FullyQualifiedName~ProgressReportingConfigurationTests";git statuswas clean afterward. Every red result below is an assertion failure, not a build or launch failure.AnsiTerminal_ScrollRegion_ProcessExitReleasesPinnedRowsAtomically,..._FrameThatReservesRowsWhileProcessExitsReleasesThemAgain,..._ProcessExitWaitsForBatchOfAnotherThread,..._ProcessExitReleasesRowsWhoseReleaseIsNotWrittenAnsiTerminal_ScrollRegion_FrameThatReservesRowsWhileProcessExitsReleasesThemAgain_progressErasedteardown gateTestProgressStateAwareTerminal_ScrollRegion_WritesAfterProgressStoppedDoNotPinProgressAgain1andtrueIsScrollRegionEnabled_AcceptsOnlyOnValuescases"on"," TRUE ","enable"AnsiTerminal_ScrollRegion_ProcessExitWaitsForBatchOfAnotherThreadAnsiTerminal_ScrollRegion_ProcessExitReleasesRowsWhoseReleaseIsNotWrittenThe rendered-surface benefit (27 lines lost to 0) is shown by the manual conhost run above, not by an automated test. The mutation log is local to the authoring session and not attached.