@@ -9,7 +9,7 @@ import { decryptApiKey } from '@/lib/api-key/crypto'
99import { type ResourceOwner , resourceScopeFromOwner } from '@/lib/core/resource-scope'
1010import { resourceScopeCondition } from '@/lib/core/resource-scope.server'
1111import { createUserKnowledgeAccessProvider } from '@/lib/knowledge/access/scope'
12- import { groupToken } from '@/lib/knowledge/access/tokens'
12+ import { groupToken , userTokenEmail } from '@/lib/knowledge/access/tokens'
1313import {
1414 connectorsGrantingSubject ,
1515 hasConnectorPermissionGrant ,
@@ -19,11 +19,15 @@ import {
1919import { searchIntegrationAccessCondition } from '@/lib/knowledge/search/integration-policy'
2020import { dateSortDirection , hasDateBounds } from '@/lib/sim-search/live/dates'
2121import {
22+ GITLAB_AUTHORED_KINDS ,
2223 GITLAB_OPT_IN_KINDS ,
2324 type GitLabCodeScope ,
25+ type GitLabPerson ,
2426 gitLabDocumentUrl ,
2527 gitLabProjectPath ,
28+ isGitLabMergedCursor ,
2629 readGitLab ,
30+ resolveGitLabPerson ,
2731 searchGitLab ,
2832} from '@/lib/sim-search/live/gitlab'
2933import {
@@ -174,13 +178,37 @@ export async function resolveAdminGitLabAccount(owner: ResourceOwner, account: L
174178type Reference = Pick < NativeDocument , 'id' | 'container' | 'kind' | 'revision' >
175179type AdminSource = { id : string ; config : Record < string , unknown > }
176180
177- /** The source's kinds a search covers: the one it names, or all but the opt-in kinds. */
178- function defaultKinds ( kinds : GitLabSearchKind [ ] , requested ?: string ) : GitLabSearchKind [ ] {
179- return kinds . filter ( ( kind ) =>
180- requested ? kind === requested : ! GITLAB_OPT_IN_KINDS . includes ( kind )
181+ /**
182+ * The source's kinds a search covers: the one it names, or all but the opt-in kinds, and for a
183+ * person filter only the kinds whose items have an author.
184+ */
185+ function defaultKinds (
186+ kinds : GitLabSearchKind [ ] ,
187+ search : Pick < NativeSearchInput , 'native' | 'involving' >
188+ ) : GitLabSearchKind [ ] {
189+ const requested = search . native ?. kind
190+ if ( requested ) return kinds . filter ( ( kind ) => kind === requested )
191+ return kinds . filter (
192+ ( kind ) =>
193+ ! GITLAB_OPT_IN_KINDS . includes ( kind ) && ( ! search . involving || GITLAB_AUTHORED_KINDS . has ( kind ) )
181194 )
182195}
183196
197+ /**
198+ * The GitLab user a person filter names: for `me`, the viewer's own verified organization email,
199+ * never the source's service account; otherwise the email given.
200+ */
201+ async function gitLabPerson (
202+ client : NativeClient ,
203+ own : string ,
204+ involving : string | undefined
205+ ) : Promise < GitLabPerson | undefined > {
206+ if ( ! involving ) return undefined
207+ const email = involving === 'me' ? userTokenEmail ( own ) : involving
208+ if ( ! email ) throw new NativeSearchError ( 'unavailable' , 'GitLab search requires a verified email.' )
209+ return resolveGitLabPerson ( client , email )
210+ }
211+
184212/** The connector document ID prefix each kind's permissions are evaluated under. */
185213const GITLAB_DOCUMENT_PREFIXES = {
186214 code : 'file' ,
@@ -412,7 +440,9 @@ async function createProjectSession(
412440 async search ( search : NativeSearchInput ) {
413441 if ( search . native ?. project && ! [ project , projectPath ] . includes ( search . native . project ) )
414442 return { documents : [ ] }
415- const selected = defaultKinds ( kinds , search . native ?. kind )
443+ const selected = defaultKinds ( kinds , search )
444+ if ( ! selected . length ) return { documents : [ ] }
445+ const person = await gitLabPerson ( client , own , search . involving )
416446 const request = ( kind : GitLabSearchKind ) =>
417447 searchGitLab (
418448 client ,
@@ -426,7 +456,7 @@ async function createProjectSession(
426456 kind,
427457 } ,
428458 } ,
429- { ref : string ( config . ref ) || undefined }
459+ { ref : string ( config . ref ) || undefined , person }
430460 )
431461 const page =
432462 selected . length === 1
@@ -474,9 +504,16 @@ const groupCursorSchema = z
474504 . object ( {
475505 mode : z . enum ( [ 'group' , 'project' ] ) ,
476506 projectIndex : z . number ( ) . int ( ) . min ( 0 ) . max ( GITLAB_GROUP_MAX_PROJECTS ) ,
477- page : z . number ( ) . int ( ) . min ( 1 ) . max ( 10_000 ) ,
507+ /** The collection's own cursor; absent on its first page. */
508+ cursor : z . string ( ) . max ( 512 ) . optional ( ) ,
509+ /** A page number, as cursors issued before collection cursors carried it. */
510+ page : z . number ( ) . int ( ) . min ( 1 ) . max ( 10_000 ) . optional ( ) ,
478511 } )
479512 . strict ( )
513+ . transform ( ( { page, ...position } ) => ( {
514+ ...position ,
515+ cursor : position . cursor ?? ( page && page > 1 ? String ( page ) : undefined ) ,
516+ } ) )
480517 . nullable ( )
481518 )
482519 . min ( 1 )
@@ -564,6 +601,7 @@ async function createGroupSession(input: GitLabSessionInput, own: string) {
564601 kinds : GitLabSearchKind [ ] ,
565602 raw ?: string
566603 ) : Promise < NativePage > => {
604+ const person = await gitLabPerson ( client , own , search . involving )
567605 /** Group search takes no branch, so a source limited to one searches its projects instead. */
568606 const fallback = ( kind : string ) =>
569607 Boolean (
@@ -605,7 +643,7 @@ async function createGroupSession(input: GitLabSessionInput, own: string) {
605643 positions : kinds . map ( ( kind ) => ( {
606644 mode : fallback ( kind ) ? 'project' : 'group' ,
607645 projectIndex : 0 ,
608- page : 1 ,
646+ cursor : undefined ,
609647 } ) ) ,
610648 }
611649 }
@@ -629,14 +667,17 @@ async function createGroupSession(input: GitLabSessionInput, own: string) {
629667 query : search . query ,
630668 ...search . native ,
631669 kind,
632- cursor : String ( position . page ) ,
670+ cursor : position . cursor ,
633671 } ,
634672 }
635673 attempts += 1
636674 try {
637675 let result : NativePage
638676 if ( position . mode === 'group' ) {
639- result = await searchGitLab ( client , query , { groupId : metadata . group . id } )
677+ result = await searchGitLab ( client , query , {
678+ groupId : metadata . group . id ,
679+ person,
680+ } )
640681 const byId = new Set ( result . documents . map ( ( document ) => document . container ) )
641682 const candidates = projects . filter ( ( project ) => byId . has ( String ( project . id ) ) )
642683 const allowed = new Map (
@@ -676,10 +717,10 @@ async function createGroupSession(input: GitLabSessionInput, own: string) {
676717 documents . push ( ...result . documents )
677718 partial ||= Boolean ( result . partial )
678719 cursor . capped ||= Boolean ( result . hasMore && ! result . nextCursor )
679- if ( result . nextCursor ) position . page = Number ( result . nextCursor )
720+ if ( result . nextCursor ) position . cursor = result . nextCursor
680721 else if ( position . mode === 'project' ) {
681722 position . projectIndex += 1
682- position . page = 1
723+ position . cursor = undefined
683724 } else cursor . positions [ index ] = null
684725 } catch ( error ) {
685726 input . signal . throwIfAborted ( )
@@ -689,7 +730,7 @@ async function createGroupSession(input: GitLabSessionInput, own: string) {
689730 ( error . httpStatus === 400 || error . httpStatus === 422 )
690731 ) {
691732 position . mode = 'project'
692- position . page = 1
733+ position . cursor = undefined
693734 continue
694735 }
695736 if (
@@ -705,7 +746,7 @@ async function createGroupSession(input: GitLabSessionInput, own: string) {
705746 : 'One GitLab collection could not be searched.'
706747 )
707748 position . projectIndex += 1
708- position . page = 1
749+ position . cursor = undefined
709750 }
710751 }
711752 return {
@@ -752,23 +793,32 @@ async function createGroupSession(input: GitLabSessionInput, own: string) {
752793 const project = await referencedProject ( search . native . project )
753794 return project ? ( await session ( project ) ) . search ( search ) : { documents : [ ] }
754795 }
755- const kinds = defaultKinds ( gitLabSourceKinds ( source . config ) , search . native ?. kind )
796+ const kinds = defaultKinds ( gitLabSourceKinds ( source . config ) , search )
756797 if ( ! kinds . length ) return { documents : [ ] }
757798 const raw = search . native ?. cursor
758799 if ( metadata . mode === 'csv' && ( ! raw || raw . startsWith ( 'g1.' ) ) )
759800 return searchGroup ( search , kinds , raw )
760801 const projects = await fallbackProjects ( )
761802 const legacyCursor = raw ?? `${ stored . revision } :${ search . limit } :0:0:1`
762- if ( ! / ^ \d { 1 , 10 } : \d { 1 , 3 } : \d { 1 , 5 } : \d : \d { 1 , 4 } $ / . test ( legacyCursor ) )
803+ const parts = legacyCursor . split ( ':' )
804+ // The last part is the collection's own cursor: a page, or a person search's continuation.
805+ const collectionCursor = parts [ 4 ] ?? ''
806+ if (
807+ ! / ^ \d { 1 , 10 } : \d { 1 , 3 } : \d { 1 , 5 } : \d : / . test ( legacyCursor ) ||
808+ parts . length !== 5 ||
809+ ! ( / ^ [ 1 - 9 ] \d { 0 , 3 } $ / . test ( collectionCursor ) || isGitLabMergedCursor ( collectionCursor ) )
810+ )
763811 throw new NativeSearchError ( 'unavailable' , 'Invalid GitLab group page.' )
764- const [ revision , pageSize , ... position ] = legacyCursor . split ( ':' ) . map ( Number )
812+ const [ revision , pageSize , startProject = 0 , startKind = 0 ] = parts . slice ( 0 , 4 ) . map ( Number )
765813 if ( revision !== stored . revision || pageSize !== search . limit )
766814 throw new NativeSearchError (
767815 'unavailable' ,
768816 'GitLab permissions or page size changed. Restart the search.'
769817 )
770- let [ projectIndex = 0 , kindIndex = 0 , page = 1 ] = position
771- if ( page < 1 || projectIndex > projects . length || ( kinds . length && kindIndex >= kinds . length ) )
818+ let projectIndex = startProject
819+ let kindIndex = startKind
820+ let page = collectionCursor
821+ if ( projectIndex > projects . length || ( kinds . length && kindIndex >= kinds . length ) )
772822 throw new NativeSearchError ( 'unavailable' , 'Invalid GitLab group page.' )
773823 const documents : NativeDocument [ ] = [ ]
774824 const failures : string [ ] = [ ]
@@ -794,14 +844,14 @@ async function createGroupSession(input: GitLabSessionInput, own: string) {
794844 ...search . native ,
795845 project : String ( project . id ) ,
796846 kind,
797- cursor : String ( page ) ,
847+ cursor : page === '1' ? undefined : page ,
798848 } ,
799849 } )
800850 documents . push ( ...current . documents )
801851 partial ||= Boolean ( current . partial )
802- if ( current . nextCursor ) page = Number ( current . nextCursor )
852+ if ( current . nextCursor ) page = current . nextCursor
803853 else {
804- page = 1
854+ page = '1'
805855 kindIndex += 1
806856 if ( kindIndex >= kinds . length ) {
807857 kindIndex = 0
@@ -821,7 +871,7 @@ async function createGroupSession(input: GitLabSessionInput, own: string) {
821871 ? error . message
822872 : 'One GitLab collection could not be searched.'
823873 )
824- page = 1
874+ page = '1'
825875 kindIndex += 1
826876 if ( kindIndex >= kinds . length ) {
827877 kindIndex = 0
0 commit comments