# fix: fan out code search across mirror placements, not just home cell

`6cdbe3c`→[main](/content/gh/entireio/cli/commits/main/index.html)·

evisdren·1w ago·2 files·+141 added/-6 removed

groupReposByCell only read the top-level Cell/Jurisdiction/ID from RepoIndexEntry, which represents the repo's home placement. Mirror placements in other regions (stored in the Placements array with their own Cell, Jurisdiction, and placement-specific repo ID) were ignored, so code search only hit the home region.

Now when Placements is non-empty, each placement is added to its own cell group with the correct per-placement repo ID — matching the BFF's fan-out behavior. resolveCellBaseURLs gains a jurisdiction-based fallback for the cluster catalog join, since placement-derived groups lack a cluster slug.

Closes ENT-998

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

## Sessions

01KWZSE8F2FAQRNV7ZR2MMQ44DView transcript

## Changes

2

- cmd/entire/cli

- Mcell_fanout.go+49/-6

- Mcell_fanout_test.go+92

```go
51 unmodified lines
```

// includes the jurisdiction so entries whose index row carries no cell don't collapse across jurisdictions into one group routed by whichever repo came first — they stay per-jurisdiction and route via the jurisdiction fallback.  
//  
// When a RepoIndexEntry has Placements, each placement is added to the group for its own cell/jurisdiction with its placement-specific repo ID. This ensures mirror placements in other regions (e.g. a US-homed repo with an EU mirror) are searched in both cells — matching the BFF's fan-out behavior.  
// When Placements is empty, the top-level Cell/Jurisdiction/ID are used as before (backward compatibility for index responses that predate placements).
func groupReposByCell(repos []coreapi.RepoIndexEntry) []cellGroup {
    byCell := make(map[string]*cellGroup)  
    for _, r := range repos {
        id := strings.TrimSpace(r.ID);

addToGroup := func(id, cell, jurisdiction, clusterSlug string) {
            id = strings.TrimSpace(id)
            if id == "" {
                continue
                return
            }
            cell := strings.ToLower(strings.TrimSpace(r.Cell))
            jurisdiction := strings.ToLower(strings.TrimSpace(r.Jurisdiction))
            cell = strings.ToLower(strings.TrimSpace(cell))
            jurisdiction = strings.ToLower(strings.TrimSpace(jurisdiction))
            clusterSlug = strings.ToLower(strings.TrimSpace(clusterSlug))
            key := cell + "\x00" + jurisdiction
            g, ok := byCell[key]
            if !ok {
                g = &cellGroup{
                    cell:         cell,
                    clusterSlug:  strings.ToLower(strings.TrimSpace(r.ClusterSlug)),
                    jurisdiction: jurisdiction,
                }
                byCell[key] = g
            }
            g.repoIDs = append(g.repoIDs, id)
        }

for _, r := range repos {
            if len(r.Placements) > 0 {
                for _, p := range r.Placements {
                    slug := ""
                    if strings.EqualFold(strings.TrimSpace(p.Cell), strings.TrimSpace(r.Cell)) {
                        slug = r.ClusterSlug
                    }
                    addToGroup(p.ID, p.Cell, p.Jurisdiction, slug)
                }
            } else {
                addToGroup(r.ID, r.Cell, r.Jurisdiction, r.ClusterSlug)
            }
        }

cells := make([]cellGroup, 0, len(byCell))
        for _, g := range byCell {
            cells = append(cells, *g)
        }
        return
    }
    bySlug := make(map[string]coreapi.Cluster, len(clusters.Clusters))
    byJurisdiction := make(map[string]coreapi.Cluster, len(clusters.Clusters))
    for _, cl := range clusters.Clusters {
        bySlug[strings.ToLower(strings.TrimSpace(cl.Slug))] = cl
        j := strings.ToLower(strings.TrimSpace(cl.Jurisdiction))
        if j != "" {
            if _, exists := byJurisdiction[j]; !exists {
                byJurisdiction[j] = cl
            }
        }
    }
    for i := range cells {
        cl, ok := bySlug[cells[i].clusterSlug]
        if !ok && cells[i].jurisdiction != "" {
            cl, ok = byJurisdiction[cells[i].jurisdiction]
        }
        if !ok {
            logging.Debug(ctx, "cell fan-out: cluster not in catalog, using jurisdiction routing",
                "cluster_slug", cells[i].clusterSlug, "cell", cells[i].cell)
        }
    }
}

// TestGroupReposByCell_Placements verifies that when a repo has Placements,  
// each placement is grouped into its own cell group with the placement-specific  
// repo ID. This is the fix for cross-region fan-out: a US-homed repo with an  
// EU mirror produces two cell groups so both cells are searched.
func TestGroupReposByCell_Placements(t *testing.T) {
    t.Parallel()
    repos := []coreapi.RepoIndexEntry{
        {
            ID: "01US", Cell: "aws-us-east-2", ClusterSlug: "us-prod", Jurisdiction: "us",
            Placements: []coreapi.RepoPlacement{
                {ID: "01US", Cell: "aws-us-east-2", Jurisdiction: "us"},
                {ID: "01EU", Cell: "aws-eu-central-1", Jurisdiction: "eu", Mirror: true},
            },
        },
        {
            ID: "01LEGACY", Cell: "aws-us-east-2", ClusterSlug: "us-prod", Jurisdiction: "us",
        },
    }
    cells := groupReposByCell(repos)
    if len(cells) != 2 {
        t.Fatalf("groups = %d, want 2 (one per cell): %+v", len(cells), cells)
    }
    eu := cells[0]
    us := cells[1]
    if eu.cell != "aws-eu-central-1" || eu.jurisdiction != "eu" {
        t.Fatalf("eu group = %+v", eu)
    }
    if got := strings.Join(eu.repoIDs, ","); got != "01EU" {
        t.Fatalf("eu repoIDs = %q, want 01EU", got)
    }
    if eu.clusterSlug != "" {
        t.Fatalf("eu clusterSlug = %q, want empty", eu.clusterSlug)
    }
    if us.cell != "aws-us-east-2" || us.jurisdiction != "us" {
        t.Fatalf("us group = %+v", us)
    }
    if got := strings.Join(us.repoIDs, ","); got != "01US,01LEGACY" {
        t.Fatalf("us repoIDs = %q, want 01US,01LEGACY", got)
    }
    if us.clusterSlug != "us-prod" {
        t.Fatalf("us clusterSlug = %q, want us-prod", us.clusterSlug)
    }
}

// TestGroupReposByCell_PlacementEmptyID verifies that placements with empty IDs  
// are skipped, matching the top-level behavior.
func TestGroupReposByCell_PlacementEmptyID(t *testing.T) {
    t.Parallel()
    repos := []coreapi.RepoIndexEntry{
        {
            ID: "01A", Cell: "aws-us-east-2", Jurisdiction: "us",
            Placements: []coreapi.RepoPlacement{
                {ID: "", Cell: "aws-us-east-2", Jurisdiction: "us"}, // empty ID → skipped
            },
        },
    }
    cells := groupReposByCell(repos)
    if len(cells) != 0 {
        t.Fatalf("groups = %d, want 0 (all placement IDs empty): %+v", len(cells), cells)
    }
}

// TestResolveCellBaseURLs_RefusesBaseURLWithoutJurisdiction verifies that  
// a concrete baseURL is only usable together with the jurisdiction its token must be minted for;  
// a catalog row with no jurisdiction leaves the group on

// TestResolveCellBaseURLs_JurisdictionFallbackForPlacements verifies that  
// groups without a cluster slug (from placement-derived groups) resolve their  
// baseURL via jurisdiction matching against the cluster catalog.
func TestResolveCellBaseURLs_JurisdictionFallbackForPlacements(t *testing.T) {
    t.Parallel()
    cells := []cellGroup{
        {cell: "aws-us-east-2", clusterSlug: "us-prod", jurisdiction: "us"},
        {cell: "aws-eu-central-1", clusterSlug: "", jurisdiction: "eu"},
    }
    fake := &fakeCellCore{clusters: []coreapi.Cluster{
        {Slug: "us-prod", Jurisdiction: "us", ApiUrl: coreapi.NewOptString("https://aws-us-east-2.api.entire.io")},
        {Slug: "eu-prod", Jurisdiction: "eu", ApiUrl: coreapi.NewOptString("https://aws-eu-central-1.api.entire.io")},
    }}
    resolveCellBaseURLs(context.Background(), fake, cells)
    if cells[0].baseURL != "https://aws-us-east-2.api.entire.io" {
        t.Fatalf("us baseURL = %q, want resolved via slug", cells[0].baseURL)
    }
    if cells[1].baseURL != "https://aws-eu-central-1.api.entire.io" {
        t.Fatalf("eu baseURL = %q, want resolved via jurisdiction fallback", cells[1].baseURL)
    }
}

func TestResolveCellBaseURLs_CatalogErrorLeavesJurisdictionRouting(t *testing.T) {
    t.Parallel()
    cells := []cellGroup{{cell: euWestCell, clusterSlug: "eu-prod", jurisdiction: "eu"}}
