@@ -9,6 +9,7 @@ const STAGE_QUERIES = {
99 plan : 'architecture business rule lifecycle flow endpoint message security operation testing configuration ownership decision change impact' ,
1010 audit : 'fact evidence rule branch failure security transaction contract'
1111} ;
12+ const CORE_MODELS = [ 'system' , 'business' , 'flows' , 'catalogs' ] ;
1213
1314function ftsQuery ( value ) {
1415 const tokens = String ( value ?? '' ) . toLowerCase ( ) . match ( / [ a - z 0 - 9 _ . $ / @ : - ] { 2 , } / g) ?? [ ] ;
@@ -49,15 +50,16 @@ function fitBudget(base, facts, models, maxTokens) {
4950 return { selectedFacts, selectedModels, estimatedTokens : used } ;
5051}
5152
52- export function compileContext ( root , { stage, target = '' , query = '' , maxTokens, factLimit = 800 , modelLimit = 500 , allowedModels = null , metadata = { } } ) {
53+ export function compileContext ( root , { stage, target = '' , query = '' , maxTokens, factLimit = 800 , modelLimit = 500 , allowedModels = undefined , metadata = { } } ) {
5354 const paths = projectPaths ( root ) ; const config = loadConfig ( root ) ; const configured = config . context ?. maxTokens ?. [ stage ] ?? config . context ?. maxTokens ?. default ?? 60000 ;
55+ const modelScope = allowedModels !== undefined ? allowedModels : stage === 'modelCore' ? false : stage === 'modelEnterprise' ? CORE_MODELS : null ;
5456 const budget = Math . max ( 256 , Number ( maxTokens ?? configured ) ) ; const db = openDatabase ( paths . database ) ;
5557 const effectiveQuery = [ STAGE_QUERIES [ stage ] ?? '' , query , target ] . filter ( Boolean ) . join ( ' ' ) ;
56- const facts = rowsForFacts ( db , effectiveQuery , factLimit ) . map ( compactFact ) ; const models = rowsForModels ( db , effectiveQuery , modelLimit , allowedModels ) . map ( compactModel ) ;
57- const base = { schemaVersion : '2.0' , stage, target : target || null , query : effectiveQuery , modelScope : allowedModels === false ? [ ] : allowedModels , metadata } ;
58+ const facts = rowsForFacts ( db , effectiveQuery , factLimit ) . map ( compactFact ) ; const models = rowsForModels ( db , effectiveQuery , modelLimit , modelScope ) . map ( compactModel ) ;
59+ const base = { schemaVersion : '2.0' , stage, target : target || null , query : effectiveQuery , modelScope : modelScope === false ? [ ] : modelScope , metadata } ;
5860 const fitted = fitBudget ( base , facts , models , budget ) ;
5961 const payload = { ...base , generatedAt : now ( ) , tokenBudget : budget , estimatedTokens : fitted . estimatedTokens , facts : fitted . selectedFacts , modelItems : fitted . selectedModels , omissions : { facts : Math . max ( 0 , facts . length - fitted . selectedFacts . length ) , modelItems : Math . max ( 0 , models . length - fitted . selectedModels . length ) } } ;
60- payload . inputHash = stableHash ( { stage, target, query : effectiveQuery , modelScope : allowedModels , facts : payload . facts . map ( ( item ) => item . hash ) , models : payload . modelItems . map ( ( item ) => item . hash ) , metadata } ) ;
62+ payload . inputHash = stableHash ( { stage, target, query : effectiveQuery , modelScope, facts : payload . facts . map ( ( item ) => item . hash ) , models : payload . modelItems . map ( ( item ) => item . hash ) , metadata } ) ;
6163 const id = sha256 ( `${ stage } \0${ target } \0${ payload . inputHash } ` ) . slice ( 0 , 24 ) ; payload . id = id ;
6264 const file = path . join ( paths . context , stage , `${ target ? target . replace ( / [ ^ a - z 0 - 9 _ . - ] + / gi, '-' ) : 'global' } .json` ) ; writeJson ( file , payload ) ;
6365 db . prepare ( 'INSERT INTO contexts(id,stage,target,query,input_hash,estimated_tokens,payload,created_at) VALUES(?,?,?,?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET input_hash=excluded.input_hash,estimated_tokens=excluded.estimated_tokens,payload=excluded.payload,created_at=excluded.created_at' ) . run ( id , stage , target || null , effectiveQuery , payload . inputHash , payload . estimatedTokens , JSON . stringify ( payload ) , now ( ) ) ;
0 commit comments