Skip to content
This repository was archived by the owner on May 29, 2026. It is now read-only.

Commit 4b46d0f

Browse files
authored
Optimize memory usage by conditionally instantiating trace when runTrace is disabled (#1877)
1 parent 79e81c3 commit 4b46d0f

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

packages/cli/src/server.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export async function startServer(
138138
string,
139139
{
140140
canceller: AbortSignalCancellationController;
141-
trace: MarkdownTrace;
141+
trace: MarkdownTrace | undefined;
142142
outputTrace: MarkdownTrace;
143143
runner: Promise<void>;
144144
}
@@ -391,15 +391,17 @@ export async function startServer(
391391
} satisfies PromptScriptProgressResponseEvent),
392392
),
393393
);
394-
chunkString(run.trace.content, WS_MAX_FRAME_CHUNK_LENGTH).forEach((c) =>
395-
ws.send(
396-
toPayload({
397-
type: "script.progress",
398-
runId,
399-
trace: c,
400-
} satisfies PromptScriptProgressResponseEvent),
401-
),
402-
);
394+
if (run.trace) {
395+
chunkString(run.trace.content, WS_MAX_FRAME_CHUNK_LENGTH).forEach((c) =>
396+
ws.send(
397+
toPayload({
398+
type: "script.progress",
399+
runId,
400+
trace: c,
401+
} satisfies PromptScriptProgressResponseEvent),
402+
),
403+
);
404+
}
403405
}
404406
} else if (lastRunResult) {
405407
sendLastRunResult();
@@ -472,11 +474,11 @@ export async function startServer(
472474
cancelAll();
473475
const canceller = new AbortSignalCancellationController();
474476
const cancellationToken = canceller.token;
475-
const trace = new MarkdownTrace({ cancellationToken });
477+
const trace = runTrace ? new MarkdownTrace({ cancellationToken }) : undefined;
476478
const outputTrace = new MarkdownTrace({
477479
cancellationToken,
478480
});
479-
if (runTrace) {
481+
if (runTrace && trace) {
480482
trace.addEventListener(TRACE_CHUNK, (ev) => {
481483
const tev = ev as TraceChunkEvent;
482484
chunkString(tev.chunk, WS_MAX_FRAME_CHUNK_LENGTH).forEach((c) =>
@@ -534,13 +536,13 @@ export async function startServer(
534536
runId,
535537
exitCode,
536538
result,
537-
trace: trace.content,
539+
trace: trace?.content || "",
538540
};
539541
sendLastRunResult();
540542
})
541543
.catch((e) => {
542544
if (canceller.controller.signal.aborted) return;
543-
if (!isCancelError(e)) trace.error(e);
545+
if (!isCancelError(e)) trace?.error(e);
544546
logError(`\nrun ${runId}: failed`);
545547
logError(e);
546548
send({

0 commit comments

Comments
 (0)