Skip to content

Commit aa6654c

Browse files
committed
fix(mcp): bring trace bundles under output budget accounting
Per review on #41021. The previous "We don't own writing so we cannot track file size for this" comment meant traces were pinned outside the budget, so a single trace could blow the cap unboundedly. Drop `evictable: false` from the traces directory and, on tracingStop, walk the produced trace files via OutputDir.trackTree to register their sizes. Old traces become evictable like any other artifact. We still can't account for the trace mid-recording (no hooks), but post-stop accounting is enough to keep multi-trace sessions under cap.
1 parent d34feaf commit aa6654c

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

packages/playwright-core/src/tools/backend/outputDir.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ export class OutputDir {
7070
}
7171
}
7272

73+
// Register every file under `dir` with its current size on disk. Useful for
74+
// bringing externally-written files (e.g., trace bundles) under accounting.
75+
async trackTree(dir: string): Promise<void> {
76+
if (this.maxSize === 0)
77+
return;
78+
let entries: fs.Dirent[];
79+
try {
80+
entries = await fs.promises.readdir(dir, { withFileTypes: true });
81+
} catch {
82+
return;
83+
}
84+
for (const entry of entries) {
85+
const full = path.join(dir, entry.name);
86+
if (entry.isDirectory()) {
87+
await this.trackTree(full);
88+
} else if (entry.isFile()) {
89+
const file = await this.resolve(full);
90+
await file.trackSize();
91+
}
92+
}
93+
}
94+
7395
}
7496

7597
export class OutputFile {

packages/playwright-core/src/tools/backend/tracing.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ const tracingStart = defineTool({
3131

3232
handle: async (context, params, response) => {
3333
const browserContext = await context.ensureBrowserContext();
34-
// We don't own writing so we cannot track file size for this.
35-
const { path: tracesDir } = await context.outputFile({ prefix: '', suggestedFilename: `traces`, ext: '' }, { origin: 'code', evictable: false });
34+
const { path: tracesDir } = await context.outputFile({ prefix: '', suggestedFilename: `traces`, ext: '' }, { origin: 'code' });
3635
const name = 'trace-' + Date.now();
3736
await browserContext.tracing.start({
3837
name,
@@ -70,6 +69,9 @@ const tracingStop = defineTool({
7069
// eslint-disable-next-line no-restricted-syntax
7170
delete (browserContext.tracing as any)[traceLegendSymbol];
7271

72+
// Now that the trace bundle is on disk, bring it under budget accounting.
73+
await context.outputDir.trackTree(traceLegend.tracesDir);
74+
7375
response.addTextResult(`Trace recording stopped.`);
7476
response.addFileLink('Trace', `${traceLegend.tracesDir}/${traceLegend.name}.trace`);
7577
response.addFileLink('Network log', `${traceLegend.tracesDir}/${traceLegend.name}.network`);

0 commit comments

Comments
 (0)