Skip to content

Improve resilience of the server-side SSE briefing stream #9

Open
rajarshidattapy wants to merge 4 commits into
AdityaP700:mainfrom
rajarshidattapy:harden-sse
Open

Improve resilience of the server-side SSE briefing stream #9
rajarshidattapy wants to merge 4 commits into
AdityaP700:mainfrom
rajarshidattapy:harden-sse

Conversation

@rajarshidattapy
Copy link
Copy Markdown
Contributor


name: "Harden SSE Stream"
about: Improve resilience of the server-side SSE briefing stream (timeouts, retries, throttling)
title: "[Infra][Resiliency][SSE] Harden SSE stream"

✅ Pre-PR Checklist

Before submitting, confirm:

  • I have run npm run lint / npm run test locally
  • I have run npm install (if dependencies changed)
  • I have NOT committed secrets or full API keys
  • I have documented required env vars and config below
  • I have validated SSE behavior under failure scenarios (timeouts, transient failures, high latency)

Description

Describe the SSE hardening changes:

  • What changed: added timeouts, exponential backoff with jitter, retry logic, lightweight circuit-breaker, response validation, and metrics/logging around external calls used by the SSE pipeline.
  • Why: make streaming resilient to transient/external failures and prevent long-running or cascading failures from blocking the SSE stream.

Files Changed

List primary files modified:

  • app/api/briefing/stream/route.ts — SSE handler: safe wrappers, timeouts, metrics and defensive error handling
  • lib/exa-service.ts (or other external wrappers) — request timeout, retry/backoff, circuit-breaker, response validation, logging/metrics (if applicable)

Type of Change

Select all that apply:

  • Bug Fix
  • New Feature / Hardening (resiliency)
  • Documentation Update
  • Chore (infra, logging, metrics)

Environment / Config

Configurable env vars (examples):

  • EXA_REQUEST_TIMEOUT_MS — per-request timeout (default e.g., 5000)
  • EXA_MAX_RETRIES — retry attempts (default e.g., 3)
  • EXA_BACKOFF_BASE_MS — backoff base (default e.g., 250)
  • EXA_CIRCUIT_THRESHOLD — failures before opening circuit
  • EXA_CIRCUIT_COOLDOWN_MS — circuit cooldown period
  • VERBOSE_LOGS — optional for local troubleshooting

Note: Do not store production API keys in PRs. BYOK keys must not be logged.


How Has This Been Tested?

Explain validation steps:

  • Manual: simulated timeouts/failures and verified SSE stream continues (partial/fallbacks) and does not hang.
  • Observability: logged retry/circuit events and verified metrics increment.
  • Unit tests (optional) — consider adding tests that mock fetch to validate retry/backoff/circuit behavior.

Manual test steps (example):

  1. setx EXA_REQUEST_TIMEOUT_MS "1000" and enable verbose logs
  2. npm run dev
  3. Trigger SSE endpoint and induce Exa failures (unreachable host) — observe retries, circuit open, and graceful SSE behavior

Security & Privacy

  • Avoid logging full API keys or user-provided BYOK values. Mask or omit secrets in logs and events.
  • Ensure any telemetry does not include sensitive payloads.

Rollback Plan

  • Revert this PR if unexpected behavior occurs.
  • Tune thresholds (timeouts/retries/circuit settings) and redeploy.

Linked Issue

Closes #<issue_number> (if applicable)


Additional Context

  • Circuit-breaker is in-memory per-instance; for multi-instance, consider a shared store (Redis) in follow-up.
  • If desired, next steps: add unit tests, export metrics to Prometheus/Datadog, or add Sentry instrumentation for errors (separate PR).
---
name: "Harden SSE Stream"
about: Improve resilience of the server-side SSE briefing stream (timeouts, retries, throttling)
title: "[Infra][Resiliency][SSE] Harden SSE stream"
---

## ✅ Pre-PR Checklist
Before submitting, confirm:
- [ ] I have run `npm run lint` / `npm run test` locally  
- [ ] I have run `npm install` (if dependencies changed)  
- [ ] I have NOT committed secrets or full API keys  
- [ ] I have documented required env vars and config below  
- [ ] I have validated SSE behavior under failure scenarios (timeouts, transient failures, high latency)

---

## Description
Describe the SSE hardening changes:
- What changed: added timeouts, exponential backoff with jitter, retry logic, lightweight circuit-breaker, response validation, and metrics/logging around external calls used by the SSE pipeline.
- Why: make streaming resilient to transient/external failures and prevent long-running or cascading failures from blocking the SSE stream.

---

## Files Changed
List primary files modified:
- app/api/briefing/stream/route.ts — SSE handler: safe wrappers, timeouts, metrics and defensive error handling
- lib/exa-service.ts (or other external wrappers) — request timeout, retry/backoff, circuit-breaker, response validation, logging/metrics (if applicable)

---

## Type of Change
Select all that apply:
- [ ] Bug Fix  
- [x] New Feature / Hardening (resiliency)  
- [ ] Documentation Update  
- [x] Chore (infra, logging, metrics)  

---

## Environment / Config
Configurable env vars (examples):
- EXA_REQUEST_TIMEOUT_MS — per-request timeout (default e.g., 5000)  
- EXA_MAX_RETRIES — retry attempts (default e.g., 3)  
- EXA_BACKOFF_BASE_MS — backoff base (default e.g., 250)  
- EXA_CIRCUIT_THRESHOLD — failures before opening circuit  
- EXA_CIRCUIT_COOLDOWN_MS — circuit cooldown period  
- VERBOSE_LOGS — optional for local troubleshooting

Note: Do not store production API keys in PRs. BYOK keys must not be logged.

---

## How Has This Been Tested?
Explain validation steps:
- [x] Manual: simulated timeouts/failures and verified SSE stream continues (partial/fallbacks) and does not hang.  
- [x] Observability: logged retry/circuit events and verified metrics increment.  
- [ ] Unit tests (optional) — consider adding tests that mock fetch to validate retry/backoff/circuit behavior.

Manual test steps (example):
1. `setx EXA_REQUEST_TIMEOUT_MS "1000"` and enable verbose logs  
2. `npm run dev`  
3. Trigger SSE endpoint and induce Exa failures (unreachable host) — observe retries, circuit open, and graceful SSE behavior

---

## Security & Privacy
- Avoid logging full API keys or user-provided BYOK values. Mask or omit secrets in logs and events.
- Ensure any telemetry does not include sensitive payloads.

---

## Rollback Plan
- Revert this PR if unexpected behavior occurs.  
- Tune thresholds (timeouts/retries/circuit settings) and redeploy.

---

## Linked Issue
Closes #<issue_number> (if applicable)

---

## Additional Context
- Circuit-breaker is in-memory per-instance; for multi-instance, consider a shared store (Redis) in follow-up.  
- If desired, next steps: add unit tests, export metrics to Prometheus/Datadog, or add Sentry instrumentation for errors (separate PR).

@vercel
Copy link
Copy Markdown

vercel Bot commented Oct 25, 2025

@rajarshidattapy is attempting to deploy a commit to the adityap700's projects Team on Vercel.

A member of the Team first needs to authorize it.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @rajarshidattapy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly upgrades the stability and reliability of the server-side SSE briefing stream. By integrating advanced error handling techniques such as request timeouts, intelligent retry mechanisms with exponential backoff, and a circuit-breaker, the system is now better equipped to gracefully handle transient failures and high latency from external services. These improvements aim to maintain a continuous and robust data flow to clients, enhancing the overall user experience by minimizing disruptions.

Highlights

  • Enhanced SSE Stream Resilience: Implemented robust error handling, including timeouts, exponential backoff with jitter, and retry logic for external API calls within the Server-Sent Events (SSE) briefing stream.
  • Lightweight Circuit Breaker: Introduced a lightweight circuit-breaker pattern to prevent cascading failures and ensure service stability when external dependencies are experiencing issues.
  • Improved External Call Management: Wrapped external calls with a safeCall utility that includes timeouts, detailed logging, and Sentry breadcrumbs for better observability and debugging.
  • Refactored Briefing API: The main briefing API route (app/api/briefing/route.ts) has been converted to an SSE endpoint, streaming data incrementally and incorporating new resilience features.
  • Documentation Update: The README.md has been updated with a new section detailing Continuous Integration (CI) practices and local testing instructions.
Ignored Files
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/ci.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly improves the resilience of the server-side SSE briefing stream by introducing several hardening patterns. In lib/exa-service.ts, a robust exaSearch function is implemented with request timeouts, exponential backoff retries, and an in-memory circuit breaker, which is a great improvement for handling transient failures from the Exa API. In app/api/briefing/route.ts, the endpoint is refactored to use Server-Sent Events (SSE) for a progressive user experience, and a safeCall wrapper is introduced for external calls to add timeouts and fallbacks. The addition of a lexical fallback for sentiment analysis is also a good touch for resilience. The changes are well-structured and greatly enhance the service's reliability. My review includes a critical fix for a missing import and a couple of medium-severity suggestions to clean up code and improve observability.

@vercel
Copy link
Copy Markdown

vercel Bot commented Oct 28, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
exora-task Error Error Oct 28, 2025 3:08am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant