Skip to content

feat(dashboard): stream business context research through oRPC - #812

Merged
izadoesdev merged 7 commits into
stagingfrom
codex/business-context-streaming
Sep 16, 2026
Merged

izadoesdev merged 7 commits into
stagingfrom
codex/business-context-streaming

Conversation

@izadoesdev

@izadoesdev izadoesdev commented Sep 16, 2026

Copy link
Copy Markdown
Member

Business-context research now runs inside an active oRPC streaming request. The draft updates as sources are read and the brief is written; stopping, navigating away, or refreshing aborts the request and clears only its matching active generation. Manual edits and research inputs remain recoverable, and completed drafts still require explicit acceptance.

The API owns the existing bounded research runner instead of the insights queue. Atomic admission prevents duplicate runs, and cancellation is checked inside the publication transaction. Independent usage settlement preserves credit accounting after cancellation. The worker retires jobs admitted by an older API during rollout with a refresh/retry message; it no longer generates business context. The first draft remains included; subsequent drafts retain the existing credit checks. No new dependency or schema migration.

Validation: final-head CI is green, including all 53 browser tests, 27 PostgreSQL business-context tests, and the native worker regression for retiring legacy jobs. Repository lint, all 33 typecheck tasks, all 27 test tasks, 57 runner cases, and 27 RPC cases also pass. Coverage includes incremental streaming, desktop/mobile layout stability, cancellation before the first event and during billing/publication, saving during streaming, disconnect recovery, research-input recovery, and history/measurement controls. The runner uses the real installed AI SDK with a synthetic provider in tests. Live staging provider smoke remains pending a selected organization and website.

Scope: business-context generation, request cancellation, and recovery of its editor/research inputs. No outstanding dependencies; the separate scheduler fixture repair #813 is already merged into the base. Known file overlap: #790 also touches packages/rpc/src/orpc.ts for billing configuration; this change only adds the API-hosted generation callback. AI-assisted implementation and review.

@unkey-deploy

unkey-deploy Bot commented Sep 16, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Unkey Deploy

Name Status Preview Inspect Updated (UTC)
links (preview) Ready Visit Preview Inspect Sep 16, 2026 3:49pm

@vercel

vercel Bot commented Sep 16, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated
dashboard Ready Ready Preview Sep 16, 2026 3:50pm UTC
databuddy-status Ready Ready Preview Sep 16, 2026 3:50pm UTC
1 Skipped Deployment
Project Deployment Actions Updated
documentation Skipped Skipped Sep 16, 2026 3:50pm UTC

@coderabbitai

coderabbitai Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 3e85e657-00d7-49f4-a383-abd3b45608ee

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Comment thread apps/dashboard/test/e2e/business-context-stream.ts Fixed
@blacksmith-sh

This comment has been minimized.

@greptile-apps

greptile-apps Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR moves organization business-context research from the Insights queue into an API-hosted oRPC stream while preserving billing, recovery, and concurrency guarantees.

  • Streams source-reading, writing progress, completion, and failure states to the dashboard.
  • Cancels matching active research when the request stops, the user navigates away, or edits are saved.
  • Persists editor and research inputs for recovery without automatically accepting generated drafts.
  • Adds atomic generation admission and transaction-aware publication cancellation.
  • Retires legacy queued jobs during rolling deployment with refresh-and-retry guidance.
  • Adds API, RPC, browser, worker, billing, and PostgreSQL regression coverage.

Confidence Score: 5/5

The exact final head appears safe to merge; the previously reported cancellation and rolling-deployment failures are resolved and no new actionable issue remains.

Final publication now checks the request signal inside the database transaction and rolls back cancellation observed before commit authorization, while cleanup is constrained to the matching active generation. The worker compatibility handler matches the prior API’s legacy job payload and deterministic job ID and safely leaves newer or terminal generations unchanged. Both previous threads were manually resolved after their fixes, and no repository-rule violation or new merge-blocking behavior was found.

Important Files Changed

Filename Overview
apps/api/src/ai/organization-business-context.ts Moves the bounded research runner into the API, streams progress, propagates cancellation through publication, and independently settles consumed usage.
packages/rpc/src/routers/business-context.ts Replaces queued generation with an authenticated oRPC event stream, atomic admission, and matching active-generation cleanup.
packages/services/src/organization-business-context.ts Adds atomic conflict handling, active-only cancellation, and transaction-aware abort checks around final publication.
apps/dashboard/app/(main)/organizations/components/business-context-settings.tsx Manages the active stream lifecycle, updates query state incrementally, and aborts research during cancellation, saving, or navigation.
apps/dashboard/app/(main)/organizations/components/use-business-context-draft.ts Extends session recovery to preserve both manual draft edits and unfinished research inputs.
apps/insights/src/jobs.ts Removes generation ownership from the worker while retaining a validated compatibility path that retires legacy queued jobs.
packages/ai/src/ai/agents/execution.ts Supports explicit request loggers so asynchronous billing failures remain observable after the HTTP logging context expires.

Sequence Diagram

sequenceDiagram
    participant UI as Dashboard editor
    participant RPC as oRPC generate stream
    participant API as API research runner
    participant DB as Business-context service
    participant AI as AI provider
    participant Billing as Usage settlement

    UI->>RPC: generate(organization, website, sources)
    RPC->>DB: atomically begin generation
    DB-->>RPC: running generation
    RPC-->>UI: initial running state
    RPC->>API: generate(generationId, request signal)
    API->>DB: publish reading/writing progress
    DB-->>API: current state
    API-->>UI: streamed state updates
    API->>AI: research and synthesize
    AI-->>API: output and usage
    API->>Billing: settle consumed usage independently
    API->>DB: publish ready draft within cancellation-aware transaction
    DB-->>UI: ready draft for explicit acceptance
    alt request cancelled before completion
        UI--xRPC: abort stream
        RPC--xAPI: abort matching request
        RPC->>DB: remove matching active generation only
    end
Loading

Reviews (4): Last reviewed commit: "fix(api): retire queued research during ..." | Re-trigger Greptile

Comment thread apps/api/src/ai/organization-business-context.ts
Comment thread apps/insights/src/jobs.ts
@izadoesdev

Copy link
Copy Markdown
Member Author

The cancellation and queued-job findings are addressed in 2c5ccce and 0d80edb, with replies in their threads. The streaming fixture also uses portable Promise constructors in 2951dec, fixing the older Node runtime failure reported by CI. The resume.ts / legacy investigation billing removals mentioned in the summary belong to staging commit a6e0c25: git diff origin/staging...HEAD -- apps/insights/src/resume.ts is empty. This PR is rebased onto staging 8a3ecda, including the separate merged scheduler fixture repair #813. Please assess the final head against that current base.

@izadoesdev

Copy link
Copy Markdown
Member Author

@greptileai review this final head (0d80edb) against current staging (8a3ecda). The prior cancellation and rollout findings have fixes and regression coverage; all CI checks now pass, including 53 browser tests and both new PostgreSQL regressions. Please refresh the review on this exact head.

@izadoesdev
izadoesdev merged commit d4809f5 into staging Sep 16, 2026
21 checks passed
@izadoesdev
izadoesdev deleted the codex/business-context-streaming branch September 16, 2026 16:00
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.

2 participants