diff --git a/.github/workflows/deploy-automator.yml b/.github/workflows/deploy-automator.yml index 33756de..8c43554 100644 --- a/.github/workflows/deploy-automator.yml +++ b/.github/workflows/deploy-automator.yml @@ -16,6 +16,10 @@ on: type: choice options: [ml, genmedia] default: ml + faq_probe: + description: 'Ask the live FAQ assistant one question with the Lambda URL/secret (no Slack posts) and require a 200 answer' + type: boolean + default: false proof_keep_seconds: description: 'Seconds to keep the verified question and reply visible before cleanup' type: string @@ -98,6 +102,17 @@ jobs: --output text echo "Automator Lambda function updated successfully" + - name: Probe the live FAQ assistant + if: github.event_name == 'workflow_dispatch' && inputs.faq_probe + run: | + umask 077 + lambda_env=$(mktemp) + trap 'rm -f "$lambda_env"' EXIT + aws lambda get-function-configuration \ + --function-name "$FUNCTION_NAME" --region "$AWS_REGION" \ + --query 'Environment.Variables' --output json > "$lambda_env" + uv run python scripts/probe_faq_assistant.py < "$lambda_env" + - name: Verify real Slack question and answer if: github.event_name == 'workflow_dispatch' && inputs.live_proof env: diff --git a/automator/scripts/probe_faq_assistant.py b/automator/scripts/probe_faq_assistant.py new file mode 100644 index 0000000..1ead638 --- /dev/null +++ b/automator/scripts/probe_faq_assistant.py @@ -0,0 +1,47 @@ +"""Ask the live FAQ assistant one question the way the automator does. + +Reads the automator Lambda environment JSON (FAQ_ASSISTANT_URL and +FAQ_ASSISTANT_SHARED_SECRET) on stdin, POSTs a course-scoped question to /ask +and fails unless it gets HTTP 200 with a non-empty answer. Nothing is posted to +Slack. The secret is never printed. +""" + +import json +import sys + +import requests + +QUESTION = 'How do I submit the homework?' +COURSE = 'ai-dev-tools-zoomcamp' + + +def main() -> int: + env = json.load(sys.stdin) + url = (env.get('FAQ_ASSISTANT_URL') or '').strip().rstrip('/') + secret = env.get('FAQ_ASSISTANT_SHARED_SECRET') or '' + if secret: + print(f'::add-mask::{secret}', flush=True) # GitHub Actions log masking + if not url: + print('FAQ_ASSISTANT_URL is not set on the automator Lambda') + return 1 + endpoint = url if url.endswith('/ask') else f'{url}/ask' + headers = {'x-faq-assistant-secret': secret} if secret else {} + response = requests.post( + endpoint, + json={'question': QUESTION, 'scope': 'course', 'course': COURSE}, + headers=headers, + timeout=60, + ) + print(f'HTTP {response.status_code}') + if response.status_code != 200: + print(response.text[:500]) + return 1 + body = response.json() + answer = (body.get('answer') or '').strip() + print(f"found_answer={body.get('found_answer')} sources={len(body.get('sources') or [])}") + print(f'answer: {answer[:300]}') + return 0 if answer else 1 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/automator/src/lambda_function.py b/automator/src/lambda_function.py index 44b89a3..25a72bf 100644 --- a/automator/src/lambda_function.py +++ b/automator/src/lambda_function.py @@ -24,6 +24,7 @@ 'The FAQ assistant is currently not available. ' 'In the meantime, you can try to find the answer here:' ) +FAQ_ASSISTANT_UNAVAILABLE_MARKER = 'FAQ_ASSISTANT_UNAVAILABLE' FAQ_ASSISTANT_GENERIC_RESOURCES = { 'docs': 'https://datatalks.club/docs/', 'faq': 'https://datatalks.club/faq/', @@ -167,9 +168,13 @@ def post_faq_assistant_answer(channel, thread_ts, payload, student_user=None): response = getattr(exc, 'response', None) status = getattr(response, 'status_code', None) body = getattr(response, 'text', '') if response is not None else '' - logger.info( - f"FAQ assistant request failed: status={status} " - f"error={exc} body={body[:500]}" + # FAQ_ASSISTANT_UNAVAILABLE is matched by a CloudWatch metric filter + # (aws-infra main/automator) that alarms to operator-alerts; keep the + # token stable. + logger.error( + f"{FAQ_ASSISTANT_UNAVAILABLE_MARKER} FAQ assistant request failed: " + f"status={status} course={payload.get('course', '')} " + f"channel={channel} error={exc} body={body[:500]}" ) slack.post_message_to_thread( channel, thread_ts, format_faq_assistant_error_message(payload) diff --git a/automator/tests/test_automator_lambda.py b/automator/tests/test_automator_lambda.py index 6cbbb4a..b6a86da 100644 --- a/automator/tests/test_automator_lambda.py +++ b/automator/tests/test_automator_lambda.py @@ -1217,9 +1217,10 @@ def test_handle_app_mention_appends_sources(self, mock_call, mock_slack): self.assertIn('*Sources:*', posted) self.assertIn('', posted) + @patch('automator_lambda_function.logger') @patch('automator_lambda_function.slack') @patch('automator_lambda_function.call_faq_assistant') - def test_faq_assistant_failure_posts_error_without_raising(self, mock_call, mock_slack): + def test_faq_assistant_failure_posts_error_without_raising(self, mock_call, mock_slack, mock_logger): response = MagicMock() response.status_code = 500 response.text = 'upstream exploded' @@ -1238,6 +1239,13 @@ def test_faq_assistant_failure_posts_error_without_raising(self, mock_call, mock ) mock_slack.github_to_slack_markdown.assert_not_called() + alert_lines = [ + call.args[0] for call in mock_logger.error.call_args_list + if call.args[0].startswith('FAQ_ASSISTANT_UNAVAILABLE ') + ] + self.assertEqual(len(alert_lines), 1) + self.assertIn('status=500', alert_lines[0]) + self.assertIn('course=llm-zoomcamp', alert_lines[0]) posted = mock_slack.post_message_to_thread.call_args[0][2] self.assertIn('The FAQ assistant is currently not available.', posted) self.assertIn('In the meantime, you can try to find the answer here:', posted) diff --git a/docs/logs.md b/docs/logs.md index d32eb9c..22534a0 100644 --- a/docs/logs.md +++ b/docs/logs.md @@ -84,3 +84,25 @@ On Windows with Git Bash, paths like `/aws/lambda/...` get converted to Windows 1. Go to CloudWatch Logs in AWS Console 2. Navigate to `/aws/lambda/automator-process-reaction` 3. View logs in real-time or search + +## FAQ assistant unavailable alert + +When the FAQ assistant call fails, the bot still replies in the thread with the +"The FAQ assistant is currently not available" fallback, and logs one line +starting with `FAQ_ASSISTANT_UNAVAILABLE` (status, course, channel, error, body). + +The CloudWatch metric filter `automator-faq-assistant-unavailable` (Terraform: +`aws-infra/main/automator/alerts.tf`) counts those lines into +`Automator/FaqAssistantUnavailable`. The alarm with the same name fires on any +occurrence in 5 minutes and notifies the `operator-alerts` SNS topic, which +emails dev@dtcdev.click (the oncall mailbox). Keep the marker token stable. + +```bash +aws logs filter-log-events --log-group-name /aws/lambda/automator-process-reaction \ + --filter-pattern FAQ_ASSISTANT_UNAVAILABLE --start-time +``` + +To check the live FAQ assistant without posting to Slack, run the deploy +workflow with `faq_probe=true` (`gh workflow run deploy-automator.yml -f faq_probe=true`): +it reads the URL and shared secret from the Lambda config and requires a 200 +answer from `/ask`.