@@ -331,31 +331,39 @@ def assemble_project_sources(
331331 return [("\n " .join (by_file [c ]), [c ]) for c in order ]
332332
333333
334+ def longctx_max_projects () -> int :
335+ try :
336+ return max (1 , int (os .environ .get ("OWASP_CX_LONGCTX_PROJECTS" , "3" )))
337+ except ValueError :
338+ return 3
339+
340+
334341def project_sources (question : str ) -> List [tuple ]:
335- """The whole text of the single project the question names ([] if none/several/ too big) ."""
342+ """Whole text of the projects the question names (1..N); [] if none, too many or too big."""
336343 try :
337344 from sqlalchemy import text as sql_text
338345
339346 from application import sqla
340347
348+ limit = longctx_max_projects ()
341349 with sqla .session .get_bind ().connect () as conn :
342- slugs = matched_slugs (question , _known_slugs (conn ))
343- if len (slugs ) != 1 :
350+ slugs = matched_slugs (question , _known_slugs (conn ), limit = limit + 1 )
351+ if not slugs or len (slugs ) > limit :
344352 return []
345- rows = conn . execute (
346- sql_text (
347- "SELECT embeddings_url, embeddings_content FROM embeddings "
348- "WHERE source_channel = :channel AND embeddings_url LIKE :pat "
349- "ORDER BY embeddings_url"
350- ),
351- {
352- "channel" : CHANNEL_AGENT ,
353- "pat" : f"owasp-agent://evidence/comprehension- { slugs [ 0 ] } -raw-%" ,
354- } ,
355- ). fetchall ()
356- return assemble_project_sources (
357- [( r . embeddings_url , r . embeddings_content ) for r in rows ],
358- max_chars = longctx_max_chars (),
359- )
353+ rows : List [ tuple ] = []
354+ for slug in slugs :
355+ found = conn . execute (
356+ sql_text (
357+ "SELECT embeddings_url, embeddings_content FROM embeddings "
358+ "WHERE source_channel = :channel AND embeddings_url LIKE :pat "
359+ "ORDER BY embeddings_url"
360+ ) ,
361+ {
362+ "channel" : CHANNEL_AGENT ,
363+ "pat" : f"owasp-agent://evidence/comprehension- { slug } -raw-%" ,
364+ },
365+ ). fetchall ()
366+ rows += [( r . embeddings_url , r . embeddings_content ) for r in found ]
367+ return assemble_project_sources ( rows , max_chars = longctx_max_chars () )
360368 except Exception :
361369 return []
0 commit comments