perf: avoid duplicate bootstrap transcript parsing

This commit is contained in:
777genius 2026-05-29 14:42:42 +03:00
parent f0514a7d17
commit 2303346e9c

View file

@ -698,6 +698,12 @@ interface BootstrapTranscriptOutcomeLookupCacheEntry {
outcome: BootstrapTranscriptOutcome | null; outcome: BootstrapTranscriptOutcome | null;
} }
interface BootstrapTranscriptOutcomeCandidate {
text: string;
observedAt: string;
parsedAgentName: string | null;
}
import type { import type {
ActiveToolCall, ActiveToolCall,
AgentActionMode, AgentActionMode,
@ -30298,7 +30304,9 @@ export class TeamProvisioningService {
if (start > 0) { if (start > 0) {
lines.shift(); lines.shift();
} }
const shouldCollectBootstrapContext = options.allowAnonymousFailure !== true;
const bootstrapContextMembers = new Set<string>(); const bootstrapContextMembers = new Set<string>();
const candidates: BootstrapTranscriptOutcomeCandidate[] = [];
for (const rawLine of lines) { for (const rawLine of lines) {
const line = rawLine?.trim(); const line = rawLine?.trim();
if (!line) continue; if (!line) continue;
@ -30327,62 +30335,49 @@ export class TeamProvisioningService {
if (!text) { if (!text) {
continue; continue;
} }
for (const contextMemberName of contextMemberNames) { if (shouldCollectBootstrapContext) {
if (isBootstrapTranscriptContextText(text, teamName, contextMemberName)) { for (const contextMemberName of contextMemberNames) {
bootstrapContextMembers.add(contextMemberName.trim().toLowerCase()); if (isBootstrapTranscriptContextText(text, teamName, contextMemberName)) {
bootstrapContextMembers.add(contextMemberName.trim().toLowerCase());
}
} }
} }
candidates.push({
text,
observedAt:
typeof parsed.timestamp === 'string' && parsed.timestamp.trim().length > 0
? parsed.timestamp.trim()
: new Date().toISOString(),
parsedAgentName,
});
} }
const hasUnambiguousMatchingBootstrapContext = const hasUnambiguousMatchingBootstrapContext =
bootstrapContextMembers.size === 1 && bootstrapContextMembers.has(normalizedMemberName); shouldCollectBootstrapContext &&
bootstrapContextMembers.size === 1 &&
bootstrapContextMembers.has(normalizedMemberName);
let outcome: BootstrapTranscriptOutcome | null = null; let outcome: BootstrapTranscriptOutcome | null = null;
for (let index = lines.length - 1; index >= 0; index -= 1) { for (let index = candidates.length - 1; index >= 0; index -= 1) {
const line = lines[index]?.trim(); const candidate = candidates[index];
if (!line) continue; if (!candidate) continue;
let parsed: { timestamp?: unknown } | null = null; const reason = extractBootstrapFailureReason(candidate.text);
try {
parsed = JSON.parse(line) as { timestamp?: unknown };
} catch {
continue;
}
const timestampMs =
typeof parsed.timestamp === 'string' ? Date.parse(parsed.timestamp) : Number.NaN;
if (sinceMs != null) {
if (!Number.isFinite(timestampMs) || timestampMs < sinceMs) {
continue;
}
}
const parsedAgentName =
typeof (parsed as { agentName?: unknown }).agentName === 'string'
? (parsed as { agentName?: string }).agentName?.trim().toLowerCase() || null
: null;
if (
parsedAgentName &&
!matchesObservedMemberNameForExpected(parsedAgentName, normalizedMemberName)
) {
continue;
}
const text = extractTranscriptMessageText(parsed);
if (!text) continue;
const observedAt =
typeof parsed.timestamp === 'string' && parsed.timestamp.trim().length > 0
? parsed.timestamp.trim()
: new Date().toISOString();
const reason = extractBootstrapFailureReason(text);
if (reason) { if (reason) {
if ( if (
!parsedAgentName && !candidate.parsedAgentName &&
options.allowAnonymousFailure !== true && options.allowAnonymousFailure !== true &&
!hasUnambiguousMatchingBootstrapContext !hasUnambiguousMatchingBootstrapContext
) { ) {
continue; continue;
} }
outcome = { kind: 'failure', observedAt, reason }; outcome = { kind: 'failure', observedAt: candidate.observedAt, reason };
break; break;
} }
const successSource = getBootstrapTranscriptSuccessSource(text, teamName, memberName); const successSource = getBootstrapTranscriptSuccessSource(
candidate.text,
teamName,
memberName
);
if (successSource) { if (successSource) {
outcome = { kind: 'success', observedAt, source: successSource }; outcome = { kind: 'success', observedAt: candidate.observedAt, source: successSource };
break; break;
} }
} }