Skip to content

Commit 9ed78e7

Browse files
EvangelinkCopilot
andauthored
Add an opt-in indexed TRX projection for agents (#12671)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 510649f commit 9ed78e7

73 files changed

Lines changed: 6253 additions & 4 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/decisions/agent-output-experiments.md‎

Lines changed: 651 additions & 0 deletions
Large diffs are not rendered by default.

‎docs/mtp-for-agents.md‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,80 @@ period; its graduation and platform-aligned version are staged for 3.0. Consult
9696
the [CTRF package documentation](../src/MTP/Microsoft.Testing.Extensions.CtrfReport/PACKAGE.md)
9797
for the schema and version-specific identity/retry guarantees.
9898

99+
### Index a TRX report for selective retrieval
100+
101+
The development version of the TRX extension provides an opt-in `agent-trx`
102+
tool. After the test run, invoke the **test application** that registered the
103+
extension, not `dotnet test`:
104+
105+
```powershell
106+
dotnet .\Tests\bin\Release\net10.0\Tests.dll agent-trx --input-trx $reportPath --output-jsonl $indexPath
107+
```
108+
109+
Choose the final report for one module and a new `.jsonl` path in an existing
110+
directory. The tool does not overwrite the output. It does not change the test
111+
run or its exit code; save that exit code and the complete terminal log separately.
112+
Process each module independently, and select the final consolidated retry report.
113+
114+
The UTF-8 JSONL file starts with a header, followed by `failureCount` failure
115+
records, followed by detail records. A failure's `excerpt` contains at most 512
116+
UTF-8 bytes of its message and stack trace; `omittedBytes` records the rest.
117+
`offset` is a decimal **byte** offset from the beginning of the file, and `length`
118+
is the total byte length of its detail chunks including their newlines. The
119+
byte range contains one or more JSONL detail chunks. Each has the same
120+
`executionId`, a zero-based `part`, a `last` flag and a `trxOutputXml` fragment.
121+
Concatenating the fragments reconstructs that result's TRX `<Output>`, including
122+
captured output. When no `<Output>` exists, the single fragment is `null`.
123+
The header's `detailChunkByteLimit` bounds the UTF-8 bytes of each unescaped
124+
fragment; JSON escaping and record metadata add bytes. Read the index and seek
125+
to one detail without reading the whole file into the model context:
126+
127+
```powershell
128+
$reader = [System.IO.StreamReader]::new($indexPath, [System.Text.Encoding]::UTF8)
129+
try {
130+
$header = $reader.ReadLine() | ConvertFrom-Json
131+
$failures = @(for ($i = 0; $i -lt $header.failureCount; $i++) {
132+
$reader.ReadLine() | ConvertFrom-Json
133+
})
134+
} finally {
135+
$reader.Dispose()
136+
}
137+
138+
if ($failures.Count -gt 0) {
139+
$row = $failures[0]
140+
$stream = [System.IO.File]::OpenRead($indexPath)
141+
try {
142+
$stream.Position = [long]::Parse($row.offset)
143+
$details = [System.IO.StreamReader]::new($stream, [System.Text.Encoding]::UTF8, $false)
144+
$remaining = [long]$row.length
145+
$part = 0
146+
while ($remaining -gt 0) {
147+
$line = $details.ReadLine()
148+
if ($null -eq $line) { throw 'Incomplete detail record.' }
149+
$remaining -= [System.Text.Encoding]::UTF8.GetByteCount($line) + 1
150+
$chunk = $line | ConvertFrom-Json
151+
if ($remaining -lt 0 -or $chunk.executionId -ne $row.executionId -or
152+
$chunk.part -ne $part -or $chunk.last -ne ($remaining -eq 0)) {
153+
throw 'Invalid detail record.'
154+
}
155+
# Inspect this fragment or append it to reconstruct the complete XML.
156+
$part++
157+
}
158+
} finally {
159+
$stream.Dispose()
160+
}
161+
}
162+
```
163+
164+
`testId` and `executionId` identify TRX records, not framework rerun UIDs. The
165+
header marks execution completeness and diagnostic retention as unknown: a
166+
missing failure, empty index, or successfully created file does not establish a
167+
successful or complete run. TRX may itself truncate diagnostics. If the report
168+
or tool is unavailable, incomplete, or incompatible, use the saved exit code,
169+
terminal log and other original artifacts; do not treat the projection as a
170+
replacement for them. Nested TRX results and unknown result types are rejected,
171+
not silently omitted. The file may contain sensitive diagnostics.
172+
99173
When consuming a CTRF document, select failures before sending data to a model.
100174
For example, with `$reportPath` pointing to the chosen final report:
101175

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace AgentOutputBenchmark;
4+
5+
[TestClass]
6+
public sealed class DiagnosticsFixture
7+
{
8+
[TestInitialize]
9+
public void RecordLoadedBinaries()
10+
{
11+
string[] locations = AppDomain.CurrentDomain.GetAssemblies()
12+
.Where(assembly => !assembly.IsDynamic
13+
&& (assembly.GetName().Name?.StartsWith("Microsoft.Testing.", StringComparison.Ordinal) == true
14+
|| assembly.GetName().Name?.StartsWith("MSTest.", StringComparison.Ordinal) == true
15+
|| assembly.GetName().Name?.StartsWith("Microsoft.VisualStudio.TestPlatform.", StringComparison.Ordinal) == true))
16+
.Select(assembly => assembly.Location)
17+
.OrderBy(location => location, StringComparer.Ordinal)
18+
.ToArray();
19+
File.WriteAllText(
20+
Path.Combine(Environment.GetEnvironmentVariable("BENCH_STATE_DIRECTORY")!, "loaded-binaries.json"),
21+
System.Text.Json.JsonSerializer.Serialize(locations));
22+
}
23+
24+
[TestMethod]
25+
[DataRow("row\\one|line\n[sentinel]")]
26+
[DataRow("row-two")]
27+
[DataRow("row-three")]
28+
public void RepeatedRows(string row)
29+
{
30+
if (Environment.GetEnvironmentVariable("BENCH_CASE") is "wrapper" or "retry")
31+
{
32+
return;
33+
}
34+
35+
throw new InvalidOperationException(
36+
"Outer transport diagnostic. " + new string('N', 18000),
37+
new FormatException($"Malformed payload for {row}: expected integer 42, actual token QZ."));
38+
}
39+
40+
[TestMethod]
41+
public void WrappedCause()
42+
{
43+
if (Environment.GetEnvironmentVariable("BENCH_CASE") == "rows")
44+
{
45+
return;
46+
}
47+
48+
throw new InvalidOperationException(
49+
"Wrapper diagnostic. " + new string('W', 24000),
50+
new IOException("Configuration path C:\\agent\\input|special.cfg is absent; create the file before launching."));
51+
}
52+
53+
[TestMethod]
54+
public void RetryThenPass()
55+
{
56+
if (Environment.GetEnvironmentVariable("BENCH_CASE") != "retry")
57+
{
58+
return;
59+
}
60+
61+
string marker = Path.Combine(Environment.GetEnvironmentVariable("BENCH_STATE_DIRECTORY")!, "retry.marker");
62+
if (!File.Exists(marker))
63+
{
64+
File.WriteAllText(marker, "first attempt executed");
65+
throw new IOException("Transient first-attempt lock; not a final failure when retry passes.");
66+
}
67+
}
68+
69+
[TestMethod]
70+
public void PassingControl()
71+
{
72+
}
73+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<EnableMSTestRunner>true</EnableMSTestRunner>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<AssemblyName>AgentOutputBenchmarkFixture</AssemblyName>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="MSTest.TestFramework" Version="@MSTestVersion@" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="@MSTestVersion@" />
12+
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="@MTPVersion@" />
13+
<PackageReference Include="Microsoft.Testing.Extensions.CtrfReport" Version="@MTPVersion@" />
14+
<PackageReference Include="Microsoft.Testing.Extensions.Retry" Version="@MTPVersion@" />
15+
</ItemGroup>
16+
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project>
2+
<!-- A single build.cmd project argument avoids shell splitting a semicolon list. -->
3+
<ItemGroup>
4+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MSTest\MSTest.TestAdapter\MSTest.TestAdapter.csproj" />
5+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MSTest\TestFramework\TestFramework.csproj" />
6+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MSTest\MSTest.Analyzers.Package\MSTest.Analyzers.Package.csproj" />
7+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MSTest\MSTest.GlobalConfigsGenerator\MSTest.GlobalConfigsGenerator.csproj" />
8+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MSTest\MSTest.TestFramework.Extensions\MSTest.TestFramework.Extensions.csproj" />
9+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MSTest\MSTest.WinUI\MSTest.WinUI.csproj" Condition="'$(OS)' == 'Windows_NT'" />
10+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MSTest\MSTest.Uwp\MSTest.Uwp.csproj" Condition="'$(OS)' == 'Windows_NT'" />
11+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MTP\Microsoft.Testing.Platform\Microsoft.Testing.Platform.csproj" />
12+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MTP\Microsoft.Testing.Platform.MSBuild\Microsoft.Testing.Platform.MSBuild.csproj" />
13+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MTP\Microsoft.Testing.Extensions.TrxReport\Microsoft.Testing.Extensions.TrxReport.csproj" />
14+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MTP\Microsoft.Testing.Extensions.TrxReport.Abstractions\Microsoft.Testing.Extensions.TrxReport.Abstractions.csproj" />
15+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MTP\Microsoft.Testing.Extensions.CtrfReport\Microsoft.Testing.Extensions.CtrfReport.csproj" />
16+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MTP\Microsoft.Testing.Extensions.Retry\Microsoft.Testing.Extensions.Retry.csproj" />
17+
<Producer Include="$(MSBuildThisFileDirectory)..\..\src\MTP\Microsoft.Testing.Extensions.Telemetry\Microsoft.Testing.Extensions.Telemetry.csproj" />
18+
</ItemGroup>
19+
<Target Name="Restore">
20+
<MSBuild Projects="@(Producer)" Targets="Restore" BuildInParallel="false" />
21+
</Target>
22+
<Target Name="Build">
23+
<MSBuild Projects="@(Producer)" Targets="Build" BuildInParallel="true" />
24+
</Target>
25+
<Target Name="Pack">
26+
<MSBuild Projects="@(Producer)" Targets="Pack" BuildInParallel="true" />
27+
</Target>
28+
</Project>

0 commit comments

Comments
 (0)