@@ -4,7 +4,21 @@ import { evaluateMemoryQuality } from "./quality-filter.js";
44import { createBM25Index } from "./search/bm25.js" ;
55import { adaptiveThreshold } from "./search/adaptive.js" ;
66
7- const INJECT_TYPES = new Set ( [ "preference" , "project" , "decision" , "summary" ] ) ;
7+ const INJECT_TYPES = new Set ( [ "preference" , "project" , "decision" , "summary" , "rejected_solution" , "pitfall" , "constraint" ] ) ;
8+
9+ // 编码记忆类型(codingRetrospect):rejected_solution / pitfall / constraint
10+ // 只在编码任务时注入(防噪声污染其他业务),且编码场景下按 codingBoostFactor
11+ // 加权排序提前。
12+ const CODING_MEMORY_TYPES = new Set ( [ "rejected_solution" , "pitfall" , "constraint" ] ) ;
13+
14+ /**
15+ * 判断一段文本是否编码类任务(关键词匹配,codingRetrospect 读取侧门控)。
16+ * 纯函数,无副作用,便于单测。
17+ */
18+ export function isCodingTask ( text , keywords = [ ] ) {
19+ const t = String ( text ?? "" ) . toLowerCase ( ) ;
20+ return keywords . some ( ( kw ) => t . includes ( String ( kw ) . toLowerCase ( ) ) ) ;
21+ }
822
923// Epistemic trust weights (v0.4.5): when config.trustEpistemicWeighting is on,
1024// each recall candidate's existing score is multiplied by the weight of its
@@ -863,17 +877,35 @@ export function createService({ store, mirror, config, onWrite, logger }) {
863877 */
864878 function injectCandidates ( { query = "" , maxItems = 5 , threshold = 3 , queryVector } = { } ) {
865879 const q = String ( query ?? "" ) . trim ( ) ;
880+ // codingRetrospect 读取侧门控:编码记忆(rejected_solution / pitfall /
881+ // constraint)只在编码任务时注入,防噪声污染其他业务;编码任务时按
882+ // codingBoostFactor 加权,让编码记忆在编码场景更靠前。
883+ const isCoding = isCodingTask ( q , config . codingKeywords ?? [ ] ) ;
884+ const codingGate = ( m ) => isCoding || ! CODING_MEMORY_TYPES . has ( m . type ) ;
866885 // Bug7: quality-weighted importance in the rule-based tier. Unassessed rows
867886 // (quality_score null) count as 100 (weight 1), so legacy stores keep their
868887 // exact summary>preference>importance ordering.
869888 const qualityWeight = ( m ) => ( m . quality_score != null ? m . quality_score / 100 : 1 ) ;
870889 const items = store . list ( { limit : 200 , includeForgotten : false } )
871890 . filter ( ( m ) => ! m . archived && INJECT_TYPES . has ( m . type ) && ! m . forgotten &&
891+ codingGate ( m ) &&
872892 ( m . type === "summary" || m . type === "preference" || m . importance >= threshold ) )
873893 . sort ( ( a , b ) => {
874- const pa = a . type === "summary" ? 0 : a . type === "preference" ? 1 : 2 ;
875- const pb = b . type === "summary" ? 0 : b . type === "preference" ? 1 : 2 ;
876- return pa - pb || ( b . importance * qualityWeight ( b ) ) - ( a . importance * qualityWeight ( a ) ) ;
894+ // 编码记忆在编码任务时优先于普通 decision(与 preference 同级),
895+ // importance 乘 codingBoostFactor 加权(封顶 5,保持 importance 语义)。
896+ const priority = ( m ) => {
897+ if ( m . type === "summary" ) return 0 ;
898+ if ( m . type === "preference" ) return 1 ;
899+ if ( isCoding && CODING_MEMORY_TYPES . has ( m . type ) ) return 1 ;
900+ return 2 ;
901+ } ;
902+ const effImportance = ( m ) =>
903+ ( isCoding && CODING_MEMORY_TYPES . has ( m . type ) )
904+ ? Math . min ( 5 , m . importance * ( config . codingBoostFactor ?? 2 ) )
905+ : m . importance ;
906+ const pa = priority ( a ) ;
907+ const pb = priority ( b ) ;
908+ return pa - pb || ( effImportance ( b ) * qualityWeight ( b ) ) - ( effImportance ( a ) * qualityWeight ( a ) ) ;
877909 } ) ;
878910 let candidates = items ;
879911 if ( config . hybridInject !== false && q ) {
@@ -888,6 +920,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
888920 const hits = vectorIndex . search ( queryVector , { limit : maxItems * 2 , threshold : 0 } ) ;
889921 for ( const m of hits ) {
890922 if ( m && ! m . archived && INJECT_TYPES . has ( m . type ) && ! m . forgotten &&
923+ codingGate ( m ) &&
891924 ( m . type === "summary" || m . type === "preference" || m . importance >= threshold ) ) {
892925 semanticItems . push ( m ) ;
893926 }
@@ -896,7 +929,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
896929 }
897930 if ( ! semanticItems . length && lastSemanticRecall ?. query === q && lastSemanticRecall . items ?. length ) {
898931 for ( const m of lastSemanticRecall . items ) {
899- if ( m && ! m . archived && INJECT_TYPES . has ( m . type ) && ! m . forgotten ) semanticItems . push ( m ) ;
932+ if ( m && ! m . archived && INJECT_TYPES . has ( m . type ) && ! m . forgotten && codingGate ( m ) ) semanticItems . push ( m ) ;
900933 }
901934 }
902935 if ( semanticItems . length ) {
0 commit comments