From 2303346e9c524ac745159f582cc7cb4c376556c9 Mon Sep 17 00:00:00 2001 From: 777genius Date: Fri, 29 May 2026 14:42:42 +0300 Subject: [PATCH] perf: avoid duplicate bootstrap transcript parsing --- .../services/team/TeamProvisioningService.ts | 77 +++++++++---------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/src/main/services/team/TeamProvisioningService.ts b/src/main/services/team/TeamProvisioningService.ts index 3e714bd0..b5cc9314 100644 --- a/src/main/services/team/TeamProvisioningService.ts +++ b/src/main/services/team/TeamProvisioningService.ts @@ -698,6 +698,12 @@ interface BootstrapTranscriptOutcomeLookupCacheEntry { outcome: BootstrapTranscriptOutcome | null; } +interface BootstrapTranscriptOutcomeCandidate { + text: string; + observedAt: string; + parsedAgentName: string | null; +} + import type { ActiveToolCall, AgentActionMode, @@ -30298,7 +30304,9 @@ export class TeamProvisioningService { if (start > 0) { lines.shift(); } + const shouldCollectBootstrapContext = options.allowAnonymousFailure !== true; const bootstrapContextMembers = new Set(); + const candidates: BootstrapTranscriptOutcomeCandidate[] = []; for (const rawLine of lines) { const line = rawLine?.trim(); if (!line) continue; @@ -30327,62 +30335,49 @@ export class TeamProvisioningService { if (!text) { continue; } - for (const contextMemberName of contextMemberNames) { - if (isBootstrapTranscriptContextText(text, teamName, contextMemberName)) { - bootstrapContextMembers.add(contextMemberName.trim().toLowerCase()); + if (shouldCollectBootstrapContext) { + for (const contextMemberName of contextMemberNames) { + 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 = - bootstrapContextMembers.size === 1 && bootstrapContextMembers.has(normalizedMemberName); + shouldCollectBootstrapContext && + bootstrapContextMembers.size === 1 && + bootstrapContextMembers.has(normalizedMemberName); let outcome: BootstrapTranscriptOutcome | null = null; - for (let index = lines.length - 1; index >= 0; index -= 1) { - const line = lines[index]?.trim(); - if (!line) continue; - let parsed: { timestamp?: unknown } | null = null; - 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); + for (let index = candidates.length - 1; index >= 0; index -= 1) { + const candidate = candidates[index]; + if (!candidate) continue; + const reason = extractBootstrapFailureReason(candidate.text); if (reason) { if ( - !parsedAgentName && + !candidate.parsedAgentName && options.allowAnonymousFailure !== true && !hasUnambiguousMatchingBootstrapContext ) { continue; } - outcome = { kind: 'failure', observedAt, reason }; + outcome = { kind: 'failure', observedAt: candidate.observedAt, reason }; break; } - const successSource = getBootstrapTranscriptSuccessSource(text, teamName, memberName); + const successSource = getBootstrapTranscriptSuccessSource( + candidate.text, + teamName, + memberName + ); if (successSource) { - outcome = { kind: 'success', observedAt, source: successSource }; + outcome = { kind: 'success', observedAt: candidate.observedAt, source: successSource }; break; } }