perf: avoid sort-for-single selection

This commit is contained in:
Peter Steinberger
2026-04-20 23:19:20 +01:00
parent 2d010306e4
commit f1a544ef6d
7 changed files with 156 additions and 34 deletions

View File

@@ -36,9 +36,14 @@ export function selectPendingApprovalRequest(params: {
}
if (normalizeLowercaseStringOrEmpty(params.requested) === "latest") {
return {
pending: [...params.pending].toSorted((a, b) => (b.ts ?? 0) - (a.ts ?? 0))[0],
};
let latest = params.pending[0];
for (let index = 1; index < params.pending.length; index += 1) {
const pending = params.pending[index];
if ((pending.ts ?? 0) > (latest.ts ?? 0)) {
latest = pending;
}
}
return { pending: latest };
}
return {

View File

@@ -677,6 +677,22 @@ function isRoutingSummary(summary: SectionSummary): boolean {
return summary.scores.routing > 0 || REM_ROUTING_SIGNAL_RE.test(summary.text);
}
function findStrongestSummary(
summaries: SectionSummary[],
predicate: (summary: SectionSummary) => boolean,
): SectionSummary | undefined {
let strongest: SectionSummary | undefined;
for (const summary of summaries) {
if (!predicate(summary)) {
continue;
}
if (!strongest || summary.scores.overall > strongest.scores.overall) {
strongest = summary;
}
}
return strongest;
}
function previewGroundedRemForFile(params: {
relPath: string;
content: string;
@@ -847,15 +863,15 @@ function previewGroundedRemForFile(params: {
(sum, { section, snippets }) => sum + scoreSection(section, snippets).tasks,
0,
);
const strongestRoutingSummary = summaries
.filter((summary) => isRoutingSummary(summary))
.toSorted((left, right) => right.scores.overall - left.scores.overall)[0];
const strongestIncidentSummary = summaries
.filter((summary) => summary.scores.incident > 0)
.toSorted((left, right) => right.scores.overall - left.scores.overall)[0];
const strongestExternalizationSummary = summaries
.filter((summary) => summary.scores.externalization > 0)
.toSorted((left, right) => right.scores.overall - left.scores.overall)[0];
const strongestRoutingSummary = findStrongestSummary(summaries, isRoutingSummary);
const strongestIncidentSummary = findStrongestSummary(
summaries,
(summary) => summary.scores.incident > 0,
);
const strongestExternalizationSummary = findStrongestSummary(
summaries,
(summary) => summary.scores.externalization > 0,
);
if (facts.length === 0 && monitoringSignal >= 3) {
addReflection(

View File

@@ -207,12 +207,16 @@ async function mapWithConcurrency<T, U>(
}
function extractTranscript(result: QaSuiteResult) {
const details = result.scenarios.flatMap((scenario) =>
scenario.steps
.map((step) => step.details)
.filter((detail): detail is string => Boolean(detail)),
);
return details.toSorted((left, right) => right.length - left.length)[0] ?? result.report;
let longestDetail: string | undefined;
for (const scenario of result.scenarios) {
for (const step of scenario.steps) {
const detail = step.details;
if (detail && (!longestDetail || detail.length > longestDetail.length)) {
longestDetail = detail;
}
}
}
return longestDetail ?? result.report;
}
function collectTranscriptStats(transcript: string) {