@@ -10,7 +10,25 @@ const ENVIRONMENT_SLUGS: Record<string, string> = {
1010 preview : "preview" ,
1111} ;
1212
13- const environmentSelect = { id : true , type : true } as const ;
13+ const environmentSelect = { id : true , type : true , archivedAt : true } as const ;
14+
15+ /** The name as given first, so a listed name like `refs/heads/x` round-trips, then as a git ref. */
16+ export function branchLookupNames ( raw : string ) : string [ ] {
17+ const normalized = sanitizeBranchName ( raw ) ;
18+ return normalized && normalized !== raw ? [ raw , normalized ] : [ raw ] ;
19+ }
20+
21+ /** The first non-null lookup, trying the names in order. */
22+ export async function firstBranchMatch < T > (
23+ names : string [ ] ,
24+ find : ( name : string ) => Promise < T | null >
25+ ) : Promise < T | null > {
26+ for ( const name of names ) {
27+ const found = await find ( name ) ;
28+ if ( found ) return found ;
29+ }
30+ return null ;
31+ }
1432
1533async function findProject ( where : Prisma . ProjectWhereInput ) {
1634 const select = { id : true , organizationId : true } as const ;
@@ -20,11 +38,19 @@ async function findProject(where: Prisma.ProjectWhereInput) {
2038 ) ;
2139}
2240
23- async function findEnvironment ( where : Prisma . RuntimeEnvironmentWhereInput ) {
24- return (
25- ( await $replica . runtimeEnvironment . findFirst ( { where, select : environmentSelect } ) ) ??
26- ( await prisma . runtimeEnvironment . findFirst ( { where, select : environmentSelect } ) )
27- ) ;
41+ async function findEnvironment (
42+ where : Prisma . RuntimeEnvironmentWhereInput ,
43+ orderBy ?: Prisma . RuntimeEnvironmentOrderByWithRelationInput
44+ ) {
45+ // A lagging replica can still show only an archived predecessor, so archived hits ask the primary.
46+ const replica = await $replica . runtimeEnvironment . findFirst ( {
47+ where,
48+ orderBy,
49+ select : environmentSelect ,
50+ } ) ;
51+ return replica && ! replica . archivedAt
52+ ? replica
53+ : await prisma . runtimeEnvironment . findFirst ( { where, orderBy, select : environmentSelect } ) ;
2854}
2955
3056export async function resolveMcpSubject (
@@ -61,13 +87,18 @@ export async function resolveMcpSubject(
6187 const branchName = typeof branch === "string" ? sanitizeBranchName ( branch ) : null ;
6288 const isDefaultBranch = slug === "dev" && ! ! branchName && isDefaultDevBranch ( branchName ) ;
6389 if ( runtimeEnvironment && branch !== undefined && ! isDefaultBranch ) {
64- if ( ( slug !== "preview" && slug !== "dev" ) || ! branchName ) return null ;
65- runtimeEnvironment = await findEnvironment ( {
66- projectId : project . id ,
67- parentEnvironmentId : runtimeEnvironment . id ,
68- branchName,
69- archivedAt : null ,
70- } ) ;
90+ if ( ( slug !== "preview" && slug !== "dev" ) || typeof branch !== "string" || ! branch ) {
91+ return null ;
92+ }
93+ const parentEnvironmentId = runtimeEnvironment . id ;
94+ // The first name that exists decides, even when archived, so a listed name never falls through.
95+ const found = await firstBranchMatch ( branchLookupNames ( branch ) , ( branchName ) =>
96+ findEnvironment (
97+ { projectId : project . id , parentEnvironmentId, branchName } ,
98+ { archivedAt : { sort : "desc" , nulls : "first" } }
99+ )
100+ ) ;
101+ runtimeEnvironment = found ?. archivedAt ? null : found ;
71102 }
72103 if ( ! runtimeEnvironment ) return null ;
73104
0 commit comments