Skip to content

Commit 5bbe70c

Browse files
authored
fix(tasks): avoid redundant terminal history fetches (#3744)
1 parent ac15077 commit 5bbe70c

1 file changed

Lines changed: 74 additions & 48 deletions

File tree

packages/core/src/sessions/sessionService.ts

Lines changed: 74 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5959,55 +5959,81 @@ export class SessionService {
59595959
return;
59605960
}
59615961
if (resumeFromRunId) {
5962-
const [ancestorResult, currentRunResult] = await Promise.all([
5963-
authStatus.auth.client.getTaskRunSessionLogsResult(
5964-
taskId,
5965-
resumeFromRunId,
5966-
{ limit: 100000 },
5967-
),
5968-
authStatus.auth.client.getTaskRunSessionLogsResult(
5969-
taskId,
5970-
taskRunId,
5971-
{ limit: 100000 },
5972-
),
5973-
]);
5974-
if (!ancestorResult.complete || !currentRunResult.complete) {
5975-
this.d.log.warn("Resume session log hydration was incomplete", {
5976-
taskId,
5977-
taskRunId,
5978-
resumeFromRunId,
5979-
ancestorComplete: ancestorResult.complete,
5980-
currentRunComplete: currentRunResult.complete,
5981-
});
5982-
return;
5962+
if (isTerminalStatus(runStatus)) {
5963+
const result =
5964+
await authStatus.auth.client.getTaskRunSessionLogsResult(
5965+
taskId,
5966+
taskRunId,
5967+
{ limit: 100000 },
5968+
);
5969+
if (!result.complete) {
5970+
this.d.log.warn("Resume session log hydration was incomplete", {
5971+
taskId,
5972+
taskRunId,
5973+
resumeFromRunId,
5974+
});
5975+
return;
5976+
}
5977+
rawEntries = result.entries;
5978+
const markedLeafStart = rawEntries.findIndex(
5979+
(entry) => getEntryTaskRunMarker(entry) === taskRunId,
5980+
);
5981+
resumeLeafEntryStartIndex =
5982+
markedLeafStart >= 0 ? markedLeafStart : undefined;
5983+
liveStreamLineCount =
5984+
markedLeafStart >= 0
5985+
? rawEntries.length - markedLeafStart
5986+
: rawEntries.length;
5987+
} else {
5988+
const [ancestorResult, currentRunResult] = await Promise.all([
5989+
authStatus.auth.client.getTaskRunSessionLogsResult(
5990+
taskId,
5991+
resumeFromRunId,
5992+
{ limit: 100000 },
5993+
),
5994+
authStatus.auth.client.getTaskRunSessionLogsResult(
5995+
taskId,
5996+
taskRunId,
5997+
{ limit: 100000 },
5998+
),
5999+
]);
6000+
if (!ancestorResult.complete || !currentRunResult.complete) {
6001+
this.d.log.warn("Resume session log hydration was incomplete", {
6002+
taskId,
6003+
taskRunId,
6004+
resumeFromRunId,
6005+
ancestorComplete: ancestorResult.complete,
6006+
currentRunComplete: currentRunResult.complete,
6007+
});
6008+
return;
6009+
}
6010+
const ancestorEntries: StoredLogEntry[] = ancestorResult.entries;
6011+
const currentRunEntries: StoredLogEntry[] = currentRunResult.entries;
6012+
const ancestorKeys = ancestorEntries.map((entry) =>
6013+
JSON.stringify(entry),
6014+
);
6015+
const currentKeys = currentRunEntries.map((entry) =>
6016+
JSON.stringify(entry),
6017+
);
6018+
const overlap = suffixPrefixOverlap(ancestorKeys, currentKeys);
6019+
const persistedLeafEntries = currentRunEntries.slice(overlap);
6020+
const leafLogs = await this.fetchSessionLogs(logUrl, taskRunId);
6021+
const leafKeys = new Set(
6022+
persistedLeafEntries.map((entry) => JSON.stringify(entry)),
6023+
);
6024+
rawEntries = [
6025+
...ancestorEntries,
6026+
...persistedLeafEntries,
6027+
...leafLogs.rawEntries.filter(
6028+
(entry) => !leafKeys.has(JSON.stringify(entry)),
6029+
),
6030+
];
6031+
resumeLeafEntryStartIndex = ancestorEntries.length;
6032+
liveStreamLineCount = Math.max(
6033+
leafLogs.totalLineCount,
6034+
persistedLeafEntries.length,
6035+
);
59836036
}
5984-
const ancestorEntries: StoredLogEntry[] = ancestorResult.entries;
5985-
const currentRunEntries: StoredLogEntry[] = currentRunResult.entries;
5986-
5987-
const ancestorKeys = ancestorEntries.map((entry) =>
5988-
JSON.stringify(entry),
5989-
);
5990-
const currentKeys = currentRunEntries.map((entry) =>
5991-
JSON.stringify(entry),
5992-
);
5993-
const overlap = suffixPrefixOverlap(ancestorKeys, currentKeys);
5994-
const persistedLeafEntries = currentRunEntries.slice(overlap);
5995-
const leafLogs = await this.fetchSessionLogs(logUrl, taskRunId);
5996-
const leafKeys = new Set(
5997-
persistedLeafEntries.map((entry) => JSON.stringify(entry)),
5998-
);
5999-
rawEntries = [
6000-
...ancestorEntries,
6001-
...persistedLeafEntries,
6002-
...leafLogs.rawEntries.filter(
6003-
(entry) => !leafKeys.has(JSON.stringify(entry)),
6004-
),
6005-
];
6006-
resumeLeafEntryStartIndex = ancestorEntries.length;
6007-
liveStreamLineCount = Math.max(
6008-
leafLogs.totalLineCount,
6009-
persistedLeafEntries.length,
6010-
);
60116037
} else {
60126038
const result = await authStatus.auth.client.getTaskRunSessionLogsResult(
60136039
taskId,

0 commit comments

Comments
 (0)