@@ -7,7 +7,21 @@ import { parseWikiLinks } from "./parser/wiki-link.js";
77import { extractQueryTags , applyTagBoost } from "./search/tag-boost.js" ;
88import { computeHeat } from "./heat.js" ;
99
10- const INJECT_TYPES = new Set ( [ "preference" , "project" , "decision" , "summary" , "user" , "fact" ] ) ;
10+ const INJECT_TYPES = new Set ( [ "preference" , "project" , "decision" , "summary" , "user" , "fact" , "rejected_solution" , "pitfall" , "constraint" ] ) ;
11+
12+ // 编码记忆类型(v0.7.11 codingRetrospect):rejected_solution / pitfall /
13+ // constraint 只在编码任务时注入(防噪声污染其他业务),且编码场景下按
14+ // codingBoostFactor 加权排序提前。
15+ const CODING_MEMORY_TYPES = new Set ( [ "rejected_solution" , "pitfall" , "constraint" ] ) ;
16+
17+ /**
18+ * 判断一段文本是否编码类任务(关键词匹配,v0.7.11 codingRetrospect 读取侧门控)。
19+ * 纯函数,无副作用,便于单测。
20+ */
21+ export function isCodingTask ( text , keywords = [ ] ) {
22+ const t = String ( text ?? "" ) . toLowerCase ( ) ;
23+ return keywords . some ( ( kw ) => t . includes ( String ( kw ) . toLowerCase ( ) ) ) ;
24+ }
1125
1226// Epistemic trust weights (v0.4.5): when config.trustEpistemicWeighting is on,
1327// each recall candidate's existing score is multiplied by the weight of its
@@ -1043,6 +1057,11 @@ export function createService({ store, mirror, config, onWrite, logger, settings
10431057 */
10441058 function injectCandidates ( { query = "" , maxItems = 5 , threshold = 3 , queryVector } = { } ) {
10451059 const q = String ( query ?? "" ) . trim ( ) ;
1060+ // v0.7.11 codingRetrospect 读取侧门控:编码记忆(rejected_solution / pitfall /
1061+ // constraint)只在编码任务时注入,防噪声污染其他业务;编码任务时按
1062+ // codingBoostFactor 加权,让编码记忆在编码场景更靠前。
1063+ const isCoding = isCodingTask ( q , config . codingKeywords ?? [ ] ) ;
1064+ const codingGate = ( m ) => isCoding || ! CODING_MEMORY_TYPES . has ( m . type ) ;
10461065 // Bug7: quality-weighted importance in the rule-based tier. Unassessed rows
10471066 // (quality_score null) count as 100 (weight 1), so legacy stores keep their
10481067 // exact summary>preference>importance ordering.
@@ -1052,11 +1071,24 @@ export function createService({ store, mirror, config, onWrite, logger, settings
10521071 // importance >= threshold — the frontend "常注入" badge maps to this split.
10531072 const items = store . list ( { limit : 200 , includeForgotten : false } )
10541073 . filter ( ( m ) => ! m . archived && INJECT_TYPES . has ( m . type ) && ! m . forgotten &&
1074+ codingGate ( m ) &&
10551075 ( m . type === "summary" || m . type === "preference" || m . type === "user" || m . importance >= threshold ) )
10561076 . sort ( ( a , b ) => {
1057- const pa = a . type === "summary" ? 0 : ( a . type === "preference" || a . type === "user" ) ? 1 : 2 ;
1058- const pb = b . type === "summary" ? 0 : ( b . type === "preference" || b . type === "user" ) ? 1 : 2 ;
1059- return pa - pb || ( b . importance * qualityWeight ( b ) ) - ( a . importance * qualityWeight ( a ) ) ;
1077+ // 编码记忆在编码任务时优先于普通 decision/fact(与 preference/user 同级),
1078+ // importance 乘 codingBoostFactor 加权(封顶 5,保持 importance 语义)。
1079+ const priority = ( m ) => {
1080+ if ( m . type === "summary" ) return 0 ;
1081+ if ( m . type === "preference" || m . type === "user" ) return 1 ;
1082+ if ( isCoding && CODING_MEMORY_TYPES . has ( m . type ) ) return 1 ;
1083+ return 2 ;
1084+ } ;
1085+ const effImportance = ( m ) =>
1086+ ( isCoding && CODING_MEMORY_TYPES . has ( m . type ) )
1087+ ? Math . min ( 5 , m . importance * ( config . codingBoostFactor ?? 2 ) )
1088+ : m . importance ;
1089+ const pa = priority ( a ) ;
1090+ const pb = priority ( b ) ;
1091+ return pa - pb || ( effImportance ( b ) * qualityWeight ( b ) ) - ( effImportance ( a ) * qualityWeight ( a ) ) ;
10601092 } ) ;
10611093 let candidates = items ;
10621094 if ( config . hybridInject !== false && q ) {
@@ -1071,6 +1103,7 @@ export function createService({ store, mirror, config, onWrite, logger, settings
10711103 const hits = vectorIndex . search ( queryVector , { limit : maxItems * 2 , threshold : 0 } ) ;
10721104 for ( const m of hits ) {
10731105 if ( m && ! m . archived && INJECT_TYPES . has ( m . type ) && ! m . forgotten &&
1106+ codingGate ( m ) &&
10741107 ( m . type === "summary" || m . type === "preference" || m . type === "user" || m . importance >= threshold ) ) {
10751108 semanticItems . push ( m ) ;
10761109 }
@@ -1079,7 +1112,7 @@ export function createService({ store, mirror, config, onWrite, logger, settings
10791112 }
10801113 if ( ! semanticItems . length && lastSemanticRecall ?. query === q && lastSemanticRecall . items ?. length ) {
10811114 for ( const m of lastSemanticRecall . items ) {
1082- if ( m && ! m . archived && INJECT_TYPES . has ( m . type ) && ! m . forgotten ) semanticItems . push ( m ) ;
1115+ if ( m && ! m . archived && INJECT_TYPES . has ( m . type ) && ! m . forgotten && codingGate ( m ) ) semanticItems . push ( m ) ;
10831116 }
10841117 }
10851118 if ( semanticItems . length ) {
0 commit comments