Skip to content

Filter MTP/MSTest internal stack frames from terminal failure output #8826

Closed as not planned

Description

Summary

When MTP renders a failed-test stack trace, hide internal MTP/MSTest plumbing frames so the user (and any LLM agent reading the output) only sees frames that come from their own code or the dependencies they care about.

Motivation

The current TerminalTestReporter.Formatting.cs:FormatStackTrace simply line-splits exception.StackTrace and prints every frame. Every failure dumps frames like:

   at Microsoft.Testing.Platform.Hosts.CommonTestHost.ExecuteRequestAsync(...)
   at Microsoft.Testing.Platform.MSTest.MSTestBridgedTestFramework.ExecuteRequestAsync(...)
   at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTestRunner.RunSingleTest(...)
   …

These frames have zero diagnostic value when the user is debugging their own test — the relevant frames are the user's TestMethod, the assertion site, and the production code under test. MSTest's Assert.* helpers are already annotated with [StackTraceHidden], so those frames are already invisible; the remaining noise is the MTP/adapter plumbing chain.

For LLM consumers this matters most: each hidden frame is ~80-120 tokens that is pure noise. A typical failed test today emits 8-15 frames where only 2-4 are user-relevant.

Proposed design

Add a frame-filter at the rendering layer (not in ExceptionFlattener):

  • In TerminalTestReporter.Formatting.cs:FormatStackTrace, before calling AppendStackFrame, run each parsed frame through a predicate that returns false for frames whose declaring type matches one of the known internal namespaces.
  • Internal namespace prefixes to hide:
    • Microsoft.Testing.Platform. (excluding Microsoft.Testing.Platform.Extensions.* — extension authors' own code? TBD)
    • Microsoft.Testing.Extensions. (TrxReport, HtmlReport, etc. — plumbing only)
    • Microsoft.VisualStudio.TestPlatform.MSTestAdapter. (VSTest adapter plumbing)
    • Possibly Microsoft.VisualStudio.TestTools.UnitTesting. for any non-[StackTraceHidden] member.
  • Collapse contiguous hidden frames into a single ...skipped N frames in Microsoft.Testing.Platform.* ... line so the user can tell something was hidden (and so the line count still indicates real depth).
  • Apply at the rendering layer only. ExceptionFlattener.FlatException.StackTrace continues to carry the raw string so TRX/HTML/AzDO/JSON-report consumers receive untouched data.

Configuration

  • Default-on when LLMEnvironmentDetector.IsLLMEnvironment() returns true.
  • Default-off otherwise (preserves today's human-mode transcript exactly).
  • Add a new option --stack-trace <filtered|full> (or --full-stack-trace flag) so humans can opt in to filtering and LLM-mode agents can opt out for debugging extensions.
  • For v1 we may skip the flag entirely and only flip on LLM mode, to avoid help/info-acceptance and XLF churn — see umbrella [RFC] Agent/LLM-efficient test output for Microsoft.Testing.Platform #8824 for the trade-off.

Risks / open questions

  • Could hide a relevant frame if a user has derived from an MTP type and is debugging their own override. Mitigation: opt-out flag + only hide frames whose declaring type's namespace starts with one of the known prefixes; user-derived classes will live in user namespaces and so will not be filtered.
  • AOT mode may have different frame visibility (some frames stripped, others present only as __Thunk__). Filter should be defensive: if regex parse fails for a frame, print it unfiltered.

Acceptance criteria

  • A failing test in LLM mode shows only user-code frames (plus a collapsed ...N skipped frames... marker).
  • Same failing test in human mode shows today's full trace.
  • TRX / HTML reports for the same run still contain the full unfiltered trace.
  • Unit tests cover: pure-user frames pass through, pure-internal trace collapses to one line, mixed trace shows user frames + collapse markers, malformed lines are passed through unchanged.

References

Activity

  1. added
    needs/triage
    area/mtp
    area/terminal-reporter
    on Jun 4, 2026
  2. Youssef1313 commented on Jun 5, 2026

    @Youssef1313
    Member

    Why not use StackTraceHidden instead of trying to alter stack trace manually. I don't think it's a good idea to try to manually mess up with stack traces.

  3. Evangelink commented on Jul 1, 2026

    @Evangelink
    MemberAuthor

    After #8854 (which annotated the MTP host + MSTest adapter execution plumbing with [StackTraceHidden], per Youssef Fahmy (@Youssef1313)'s suggestion), I re-measured a real failing-test transcript to see what noise actually remains. My conclusion is that we should close this as won't-do — the declarative [StackTraceHidden] approach already delivered the win, and the leftover frames are standard .NET behavior we shouldn't manipulate.

    Post-#8854 transcript (4 failing tests: sync assert, sync throw-from-helper, async assert, async throw-from-helper)

    failed SyncAssertFails
      Assert.AreEqual(42, result)
        at Repro.Tests.SyncAssertFails() in Tests.cs:15
        at System.RuntimeMethodHandle.InvokeMethod(...)
        at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(...)
    failed SyncThrowsFromHelper
      System.InvalidOperationException: boom from helper
        at Repro.Tests.Level2() in Tests.cs:29
        at Repro.Tests.Level1() in Tests.cs:27
        at Repro.Tests.SyncThrowsFromHelper() in Tests.cs:24
        at System.RuntimeMethodHandle.InvokeMethod(...)
        at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(...)
    failed AsyncAssertFails
      Assert.AreEqual(1, value)
        at Repro.Tests.AsyncAssertFails() in Tests.cs:36
        at ...MSTestAdapter...TestMethodInfo.ExecuteInternalAsync(...)
    failed AsyncThrowsFromHelper
      System.InvalidOperationException: boom from async helper
        at Repro.Tests.InnerAsync() in Tests.cs:62
        at Repro.Tests.OuterAsync() in Tests.cs:56
        at Repro.Tests.AsyncThrowsFromHelper() in Tests.cs:50
        at ...MSTestAdapter...TestMethodInfo.ExecuteInternalAsync(...)
    

    Why the original motivation is now obsolete

    The three example frames this issue was filed to remove — CommonTestHost.ExecuteRequestAsync, MSTestBridgedTestFramework.ExecuteRequestAsync, UnitTestRunner.RunSingleTest — no longer appear at all. The whole MTP-host + adapter plumbing chain is gone thanks to [StackTraceHidden]. The "8–15 frames, only 2–4 user-relevant" claim is outdated: failures are now 2–5 frames with only 1–2 non-user frames each. Async user frames also unwind cleanly (no MoveNext/closure spam) thanks to modern .NET stack-trace demystification.

    Why we should NOT add a rendering-layer filter for what's left

    The residual frames are not MTP noise — they're .NET's own default stack-trace surface:

    1. System.RuntimeMethodHandle.InvokeMethod + System.Reflection.MethodBaseInvoker.InvokeWithNoArgs — the standard frames any reflection-based invoker shows (xUnit, NUnit, ASP.NET model binding, etc.). They're a familiar "reached via reflection" signal, not confusing noise.
    2. TestMethodInfo.ExecuteInternalAsync leaking despite the type being [StackTraceHidden] — the documented runtime limitation for [StackTraceHidden] on async methods. If the runtime tightens that, the frame disappears for free; a rendering filter would just be papering over it.

    A rendering-layer deny-list would be guesswork about a frame's meaning. It can't tell "MTP invoked the test reflectively" from "the user's own code called MethodInfo.Invoke and that reflection call is the bug" — so filtering RuntimeMethodHandle risks eating a genuinely relevant user frame. It also reintroduces exactly the fragility we avoided by choosing [StackTraceHidden]: localization, AOT, obfuscation, and a permanent terminal-vs-report divergence to maintain. Keeping the terminal faithful to .NET's default trace is a feature, not a gap.

    Recommendation

    Close as won't-do; #8854 is the delivered solution. The one genuinely separate observation is that the async frame can carry an absolute build-machine path (from the shipped PDBs) — that's a deterministic-build / source-path config topic, not a stack-rendering concern, and can be filed separately if we think it's worth pursuing.

  4. removed
    needs/triage
    on Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/mtparea/terminal-reporter

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions