Skip to content

feat(ai): deliver canonical organization context to chat agents - #768

Merged
izadoesdev merged 5 commits into
stagingfrom
codex/business-context-agent-delivery
Sep 8, 2026
Merged

feat(ai): deliver canonical organization context to chat agents#768
izadoesdev merged 5 commits into
stagingfrom
codex/business-context-agent-delivery

Conversation

@izadoesdev

@izadoesdev izadoesdev commented Sep 8, 2026

Copy link
Copy Markdown
Member

Saved organization business context reaches Insights but is absent from ordinary dashboard, Slack and MCP chat. This delivers the canonical saved profile through one bounded loader and formatter in the dashboard route and shared ask/stream/trace preparation. Context carries organization, revision and source provenance; it remains untrusted background and attributed assertions, never instructions or measured evidence.

Depends on #766 (merged). Rebased onto staging 656051c; the final diff contains only the agent-delivery slice. Ready for final review; do not merge automatically.

  • Structured priority, success definition and exclusions retain separate team-assertion provenance, including team-only profiles. Mixed background preserves explicit team event meanings while inherited public claims remain unverified.
  • Organization and accessible-website checks precede the canonical read. Foreign selections and mixed-organization mentions cannot receive the profile; shared analytics tools retain organization scope.
  • One canonical read per turn, a 1.5-second wait and a 48,000-character output cap. Oversized references are omitted first with an explicit count and explanation, retaining the complete brief and team assertions. If the remaining escaped record still exceeds the cap, it is omitted intact. The service lacks database cancellation, so a timed-out read may finish in the background but cannot deliver context late.

The extra one-time attribution instruction was tested and removed. The three-case present-context follow-up remained at 16/18 on the small manual rubric, still missed the targeted team-defined emitter attribution, and increased words by 7.3%. Commit 24f8c49 removes that rule and its tests, preserving the original attributed-assertions guidance and reference-budget fallback. The small fixture's formatted context is again byte-for-byte identical to the original paired run (2,824 characters). Existing live artifacts are retained outside git; no further live calls were made.

Validation at 24f8c49: root lint passed; root types 33/33; normal pre-push tests 27/27 tasks (AI 852 passed, 21 skipped); targeted shared-agent tests 50 passed, 1 skipped; API tests 7/7. Native AI SDK provider-mock pairs inspect actual model input for synthetic event meanings, priorities, team-only/mixed context, tenant isolation and no-profile fallbacks. Maximum-length plaintext brief, team fields and all source references are retained; oversized escaped references are explicitly omitted without losing final meanings or exclusions. These deterministic checks establish delivery, not model answer quality.

Scope: packages/ai context loading, agent/tool context, package export and isolated test runner, plus the API agent route and tests. No service, shared schema, Insights or dashboard changes remain in this PR's diff. No new memory source, scraping or unbounded tool loop.

@vercel

vercel Bot commented Sep 8, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated
databuddy-status Ready Ready Preview Sep 8, 2026 9:40pm UTC
2 Skipped Deployments
Project Deployment Actions Updated
dashboard Skipped Skipped Sep 8, 2026 9:40pm UTC
documentation Skipped Skipped Sep 8, 2026 9:40pm UTC

@coderabbitai

coderabbitai Bot commented Sep 8, 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: ef09a81b-f43b-4b3e-88a0-cb15e0a1f1e4

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.

@izadoesdev
izadoesdev force-pushed the codex/business-context-agent-delivery branch from ffa3ebf to 0c33ee5 Compare September 8, 2026 21:19
@vercel
vercel Bot temporarily deployed to Preview – dashboard September 8, 2026 21:19 Inactive
@vercel
vercel Bot temporarily deployed to Preview – documentation September 8, 2026 21:19 Inactive
@unkey-deploy

unkey-deploy Bot commented Sep 8, 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 8, 2026 9:40pm

The three-case present-context follow-up stayed at 16/18 on the small manual rubric, still missed the targeted emitter attribution, and increased words by 7.3%. Remove the extra rule and its tests rather than retaining prompt growth without a demonstrated benefit.

Preserve the original attributed-assertions guidance and reference-budget fallback. The small fixture context again matches the original paired run byte for byte. This reverses the rule from 7aa476d after rebasing onto merged parent #766.
@izadoesdev
izadoesdev force-pushed the codex/business-context-agent-delivery branch from 3672994 to 24f8c49 Compare September 8, 2026 21:40
@vercel
vercel Bot temporarily deployed to Preview – documentation September 8, 2026 21:40 Inactive
@vercel
vercel Bot temporarily deployed to Preview – dashboard September 8, 2026 21:40 Inactive
@izadoesdev
izadoesdev marked this pull request as ready for review September 8, 2026 21:40
@izadoesdev
izadoesdev merged commit 12f1abd into staging Sep 8, 2026
21 checks passed
@izadoesdev
izadoesdev deleted the codex/business-context-agent-delivery branch September 8, 2026 21:44
@greptile-apps

greptile-apps Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR loads canonical saved organization business profiles into dashboard and shared Slack/MCP agent turns, adds organization-aware tool context, and introduces bounded formatting, timeout handling, tenant-isolation checks, and delivery tests.

  • Adds a single formatter and 1.5-second canonical profile loader with a 48,000-character output budget.
  • Injects the resulting context into dashboard and shared ask/stream/trace model input.
  • Propagates organization and accessible-website scope to shared analytics tools.
  • Adds native provider-mock coverage for provenance, mixed profiles, authorization, failures, cancellation, and size limits.
  • Changes the AI package test runner to isolate test files.

Confidence Score: 3/5

This PR is not safe to merge until valid domain variants are accepted, zero-website organizations can receive their saved organization profiles, and the explicit-type repository requirement is satisfied.

The new domain preflight rejects authorized calls that the existing resolver accepts, while the loader withholds valid canonical profiles solely because an organization has no websites; the test file also violates an explicit repository rule.

Files Needing Attention: packages/ai/src/ai/mcp/run-agent.ts, packages/ai/src/lib/organization-business-context.ts, apps/api/src/routes/agent-business-context.test.ts

Important Files Changed

Filename Overview
packages/ai/src/lib/organization-business-context.ts Adds the bounded canonical profile loader and formatter, but incorrectly makes website existence a prerequisite for organization-wide saved context.
packages/ai/src/ai/mcp/run-agent.ts Adds organization resolution, website authorization, and prompt delivery, but its exact domain comparison rejects valid case variants.
apps/api/src/routes/agent.ts Loads canonical context after route-level authorization and adds it to dashboard agent extras.
packages/ai/src/ai/mcp/agent-tools.ts Propagates the resolved organization into the changed shared analytics-tool access checks.
packages/ai/src/ai/mcp/tool-context.ts Adds an organization equality check before existing website authorization.
apps/api/src/routes/agent-business-context.test.ts Adds broad HTTP-to-model delivery coverage but violates the repository’s explicit-type requirement in its mocks.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Authenticated dashboard, Slack, or MCP turn] --> B[Resolve organization and accessible websites]
    B --> C{Requested site or domain authorized?}
    C -- No --> D[Reject request or omit business context]
    C -- Yes --> E[Read canonical organization profile]
    E --> F{Read completes within 1.5 seconds?}
    F -- No --> G[Inject explicit unavailable/unknown context]
    F -- Yes --> H[Format provenance and assertions]
    H --> I{Within 48,000 characters?}
    I -- Yes --> J[Inject into model user context]
    I -- No --> K[Omit references and retry]
    K --> L{Reduced record fits?}
    L -- Yes --> J
    L -- No --> G
    J --> M[Run authorized analytics tools]
Loading

Comments Outside Diff (1)

  1. packages/ai/src/lib/organization-business-context.ts, line 1346-1348 (link)

    P1 Zero-Site Profiles Are Withheld

    An authorized member of an organization with no websites cannot receive its saved team-authored profile because an empty accessibleWebsites list stops the canonical read. These profiles can be saved without a generated website source, so shared ask, stream, and trace calls incorrectly report the context as unavailable despite valid organization access.

Reviews (1): Last reviewed commit: "revert(ai): drop ineffective event-attri..." | Re-trigger Greptile

Comment on lines +269 to +274
(options.websiteDomain &&
!accessibleWebsites.some(
(site) =>
site.domain === options.websiteDomain &&
(!options.websiteId || site.id === options.websiteId)
))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Valid Domains Are Rejected

The new preflight uses exact, case-sensitive equality for websiteDomain. For example, a caller sending example.com for a stored Example.com site is rejected as inaccessible, even though the existing downstream resolver matches domains case-insensitively. This breaks otherwise authorized shared-agent requests.

profile: null as OrganizationBusinessProfile | null,
read: vi.fn(),
prompts: [] as Parameters<MockLanguageModelV3["doStream"]>[0][],
contexts: [] as Record<string, unknown>[],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Mocks Use Forbidden Types

This test uses Record<string, unknown> instead of an explicit context type. The same pattern appears on lines 112 and 174, while line 148 also uses an unknown model parameter. This violates the repository directive to avoid any, unknown, and never in favor of explicit types, so the requirement must be satisfied before merging.

Context Used: Basic guidelines for the project so vibe coders do... (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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