Skip to content

feat(code-mappings): Add directory prefix ranking helper - #126209

Merged
Abdkhan14 merged 7 commits into
masterfrom
abdullahkhan/vdy-245-m11-1-directory-prefix-helper-no-http
Oct 8, 2026
Merged

Abdkhan14 merged 7 commits into
masterfrom
abdullahkhan/vdy-245-m11-1-directory-prefix-helper-no-http

Conversation

@Abdkhan14

@Abdkhan14 Abdkhan14 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

For project: https://linear.app/getsentry/project/code-mappings-refactor-d3987c38f5f1/overview

directory_prefixes turns one file path into its directory prefixes, each with a trailing slash and never including the filename. rank_directory_prefixes unions those prefixes across many files, counts how many unique files sit under each, and keeps the top 200.

The repo-prefix and stack-prefix autocomplete endpoints will call this. This change has no HTTP surface.

Refs VDY-245

Split file paths into directory prefixes and rank them by how many unique files they cover, for the upcoming prefix autocomplete endpoints.

Refs VDY-245

Co-authored-by: Cursor <cursoragent@cursor.com>
@linear-code

linear-code Bot commented Oct 1, 2026

Copy link
Copy Markdown

VDY-245

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 1, 2026
@Abdkhan14
Abdkhan14 marked this pull request as ready for review October 1, 2026 18:04
@Abdkhan14
Abdkhan14 requested a review from a team as a code owner October 1, 2026 18:04

@cursor cursor Bot left a comment

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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 523bbf5. Configure here.

…efixes

Splitting on '/' dropped the empty segments between the triple slashes, collapsing app:///src/index.tsx into prefixes starting with app:/ instead of app:///. Peel the scheme before splitting and re-attach it to each prefix.

Refs VDY-245

Co-authored-by: Cursor <cursoragent@cursor.com>
@Abdkhan14
Abdkhan14 marked this pull request as draft October 2, 2026 02:32
…e raw path

Stack roots are matched with startswith before any slash rewrite. Rebuilding a path by dropping empty segments turned UNC, scheme:///, and backslash frames into prefixes the frame does not start with. Emit each prefix as a slice of the original path and keep that path's separator.

Refs VDY-245

Co-authored-by: Cursor <cursoragent@cursor.com>
…126616)

For project:
https://linear.app/getsentry/project/code-mappings-refactor-d3987c38f5f1/overview

Adds `GET
/api/0/organizations/{org}/code-mapping-prefixes/repo/?repositoryId=`
(M11-2, [VDY-246](https://linear.app/getsentry/issue/VDY-246)). Given a
single `repositoryId`, the endpoint resolves the active `Repository`,
fetches its already-cached source-code file list via
`get_cached_repo_files`, and passes those paths through the
`rank_directory_prefixes` helper from
[#126209](#126209) to return a
ranked `prefixes` list (`path`, `fileCount`). It intentionally avoids
`get_trees_for_org()` and only touches the one repo the user picked,
keeping the response fast on cache hits. The empty-string catch-all is
omitted from the JSON — the frontend combobox (M11-4) adds it. Errors
that `get_cached_repo_files` already handles internally (empty repo 409,
missing ref 404) fall through as `{prefixes: []}` so the field stays
typeable; any other `ApiError` returns 502 rather than blocking Save.

Refs VDY-246

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
@Abdkhan14
Abdkhan14 requested review from a team as code owners October 8, 2026 18:45
@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Oct 8, 2026
…126623)

For project:
https://linear.app/getsentry/project/code-mappings-refactor-d3987c38f5f1/overview

Adds `GET /api/0/projects/{org}/{project}/code-mapping-prefixes/stack/`
([VDY-247](https://linear.app/getsentry/issue/VDY-247)). It samples the
50 newest events from the last 7 days, keeps in-app stack filenames,
unions in this project's saved stack roots, and ranks directory prefixes
with the helper from #126209. An empty sample or a Snuba error returns
`{prefixes: []}` so the field stays typeable. Stacked on the
directory-prefix helper, in parallel with the repo-prefix endpoint.

Refs VDY-247

Made with [Cursor](https://cursor.com)

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
…es endpoint

Capture the RepositoryIntegration check before narrowing to RepoTreesIntegration so mypy can prove each isinstance independently. The prior ordering asked mypy to intersect IntegrationInstallation, RepoTreesIntegration, and RepositoryIntegration[Any] in the true branch, which produces an impossible MRO and triggers warn_unreachable=true as a hard error.

Co-authored-by: Cursor <cursoragent@cursor.com>
Comment on lines +59 to +103
integration = integration_service.get_integration(integration_id=repo.integration_id)
if integration is None:
return None, Response({"detail": "Integration not found."}, status=404)

installation = integration.get_installation(organization_id=organization.id)
repository_installation = (
installation if isinstance(installation, RepositoryIntegration) else None
)
if not isinstance(installation, RepoTreesIntegration):
return None, Response(
{"detail": "Integration does not support repository trees."}, status=404
)

cached = installation.get_repo_files_from_cache(repo.name)
if cached is not None:
# Warm cache hit — skip the provider branch lookup entirely.
files = cached
else:
if repository_installation is not None:
try:
branch = repository_installation.get_repository_default_branch(repo)
except ApiError as e:
logger.warning(
"code_mapping_repo_prefixes.branch_lookup_error",
extra={"repo": repo.name, "error_code": e.code},
)
branch = None
if not branch:
return None, Response(
{"detail": "Could not determine default branch for this repository."},
status=400,
)
else:
branch = "HEAD"

try:
files = installation.get_cached_repo_files(repo.name, branch, shifted_seconds=0)
except ApiError as e:
logger.warning(
"code_mapping_repo_prefixes.api_error",
extra={"repo": repo.name, "error_code": e.code},
)
return None, Response(
{"detail": "Failed to retrieve repository file list."}, status=502
)

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.

Uncaught missing organization integration during repository uninstall race

If this endpoint reads the active repository before the uninstall deletion task disassociates it, but the organization integration is removed before provider access, a cold-cache request can raise OrganizationIntegrationNotFound and return a 500. On the cache-miss path, get_repository_default_branch() and get_cached_repo_files() can call get_client(), which accesses org_integration; the surrounding handlers catch only ApiError. Pre-fetch and seed the organization integration, and handle OrganizationIntegrationNotFound around provider access (for example, return a 404 when the installation is no longer configured).

Evidence
  • The repository deletion task disassociates repositories before deleting the organization integration; a request that already read the repository can retain its integration ID during that race.
  • On a cache miss, get_repository_default_branch() calls get_repositories(), which reaches get_client(); GitHub and Cursor Origin clients access org_integration.
  • IntegrationInstallation.org_integration raises OrganizationIntegrationNotFound when the organization integration row is gone, and the endpoint catches only ApiError around both provider calls.
  • OrganizationRepositoryPlatformsEndpoint pre-fetches and seeds the organization integration and catches missing-installation errors, confirming this failure mode is not handled here.

Identified by Warden · sentry-backend-bugs · XZU-V8G

@Abdkhan14
Abdkhan14 merged commit 79b41d4 into master Oct 8, 2026
87 checks passed
@Abdkhan14
Abdkhan14 deleted the abdullahkhan/vdy-245-m11-1-directory-prefix-helper-no-http branch October 8, 2026 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants