Merge pull request #439 from buster-so/dal/retry-agent-logic

skip slack notification on no issues foudn + no major assumptions
This commit is contained in:
dal 2025-07-08 11:25:22 -07:00 committed by GitHub
commit 0693e4ce25
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 52 deletions

View File

@ -290,27 +290,11 @@ export const analystAgentTask: ReturnType<
throw new Error('BRAINTRUST_KEY is not set'); throw new Error('BRAINTRUST_KEY is not set');
} }
// Start Braintrust initialization immediately but don't block the critical path // Initialize Braintrust logger
const braintrustInitStart = Date.now(); const braintrustLogger = initLogger({
const braintrustInitPromise = Promise.resolve().then(async () => {
try {
initLogger({
apiKey: process.env.BRAINTRUST_KEY, apiKey: process.env.BRAINTRUST_KEY,
projectName: process.env.ENVIRONMENT || 'development', projectName: process.env.ENVIRONMENT || 'development',
}); });
logger.log('Braintrust initialization completed', {
messageId: payload.message_id,
braintrustInitTimeMs: Date.now() - braintrustInitStart,
});
} catch (error) {
logger.error('Braintrust initialization failed', {
messageId: payload.message_id,
error: error instanceof Error ? error.message : 'Unknown error',
braintrustInitTimeMs: Date.now() - braintrustInitStart,
});
// Don't throw - allow workflow to continue without Braintrust
}
});
try { try {
const dataLoadStart = Date.now(); const dataLoadStart = Date.now();
@ -424,19 +408,6 @@ export const analystAgentTask: ReturnType<
// Log performance after workflow run creation // Log performance after workflow run creation
logPerformanceMetrics('post-createrun', payload.message_id, taskStartTime, resourceTracker); logPerformanceMetrics('post-createrun', payload.message_id, taskStartTime, resourceTracker);
// Wait for Braintrust initialization if it's not ready yet
const braintrustWaitStart = Date.now();
await braintrustInitPromise;
const braintrustWaitTime = Date.now() - braintrustWaitStart;
if (braintrustWaitTime > 10) {
// Only log if we actually had to wait
logger.log('Waited for Braintrust initialization', {
messageId: payload.message_id,
braintrustWaitTimeMs: braintrustWaitTime,
});
}
// Execute workflow with tracing // Execute workflow with tracing
const workflowStartMethodStart = Date.now(); const workflowStartMethodStart = Date.now();
const tracedWorkflow = wrapTraced( const tracedWorkflow = wrapTraced(
@ -472,7 +443,6 @@ export const analystAgentTask: ReturnType<
workflowStartMethodTimeMs: workflowStartMethodTime, workflowStartMethodTimeMs: workflowStartMethodTime,
totalWorkflowTimeMs: totalWorkflowTime, totalWorkflowTimeMs: totalWorkflowTime,
createRunTimeMs: createRunTime, createRunTimeMs: createRunTime,
braintrustWaitTimeMs: braintrustWaitTime,
}); });
// Log final performance metrics // Log final performance metrics
@ -491,7 +461,6 @@ export const analystAgentTask: ReturnType<
dataLoadTimeMs: dataLoadTime, dataLoadTimeMs: dataLoadTime,
contextSetupTimeMs: contextSetupTime, contextSetupTimeMs: contextSetupTime,
createRunTimeMs: createRunTime, createRunTimeMs: createRunTime,
braintrustWaitTimeMs: braintrustWaitTime,
workflowStartMethodTimeMs: workflowStartMethodTime, workflowStartMethodTimeMs: workflowStartMethodTime,
totalWorkflowTimeMs: totalWorkflowTime, totalWorkflowTimeMs: totalWorkflowTime,
}, },
@ -549,6 +518,9 @@ export const analystAgentTask: ReturnType<
executionTimeMs: totalExecutionTime, executionTimeMs: totalExecutionTime,
}); });
// Need to flush the Braintrust logger to ensure all traces are sent
await braintrustLogger.flush();
return { return {
success: false, success: false,
messageId: payload.message_id, messageId: payload.message_id,

View File

@ -135,8 +135,8 @@ export const messagePostProcessingTask: ReturnType<
throw new Error('BRAINTRUST_KEY is not set'); throw new Error('BRAINTRUST_KEY is not set');
} }
// Initialize Braintrust logging for observability // Initialize Braintrust logger
initLogger({ const braintrustLogger = initLogger({
apiKey: process.env.BRAINTRUST_KEY, apiKey: process.env.BRAINTRUST_KEY,
projectName: process.env.ENVIRONMENT || 'development', projectName: process.env.ENVIRONMENT || 'development',
}); });
@ -302,6 +302,12 @@ export const messagePostProcessingTask: ReturnType<
// Step 6: Send Slack notification if conditions are met // Step 6: Send Slack notification if conditions are met
let slackNotificationSent = false; let slackNotificationSent = false;
// Skip Slack notification if tool_called is "noIssuesFound" and there are no major assumptions
const hasMajorAssumptions =
dbData.assumptions?.some((assumption) => assumption.label === 'major') ?? false;
const shouldSkipSlackNotification =
dbData.tool_called === 'noIssuesFound' && !hasMajorAssumptions;
try { try {
logger.log('Checking Slack notification conditions', { logger.log('Checking Slack notification conditions', {
messageId: payload.messageId, messageId: payload.messageId,
@ -309,8 +315,16 @@ export const messagePostProcessingTask: ReturnType<
summaryTitle: dbData.summary_title, summaryTitle: dbData.summary_title,
summaryMessage: dbData.summary_message, summaryMessage: dbData.summary_message,
toolCalled: dbData.tool_called, toolCalled: dbData.tool_called,
hasMajorAssumptions,
shouldSkipSlackNotification,
}); });
if (shouldSkipSlackNotification) {
logger.log('Skipping Slack notification: noIssuesFound with no major assumptions', {
messageId: payload.messageId,
organizationId: messageContext.organizationId,
});
} else {
const slackResult = await sendSlackNotification({ const slackResult = await sendSlackNotification({
organizationId: messageContext.organizationId, organizationId: messageContext.organizationId,
userName: messageContext.userName, userName: messageContext.userName,
@ -333,6 +347,7 @@ export const messagePostProcessingTask: ReturnType<
reason: slackResult.error, reason: slackResult.error,
}); });
} }
}
} catch (slackError) { } catch (slackError) {
const errorMessage = const errorMessage =
slackError instanceof Error ? slackError.message : 'Unknown Slack error'; slackError instanceof Error ? slackError.message : 'Unknown Slack error';
@ -385,6 +400,9 @@ export const messagePostProcessingTask: ReturnType<
executionTimeMs: Date.now() - startTime, executionTimeMs: Date.now() - startTime,
}); });
// Need to flush the Braintrust logger to ensure all traces are sent
await braintrustLogger.flush();
return { return {
success: false, success: false,
messageId: payload.messageId, messageId: payload.messageId,