11import { createScopeResolver } from "./scope.js" ;
22import { createHotMemory } from "./hot-memory.js" ;
33import { STR , langOf } from "./lang.js" ;
4+ import { MEMORY_GUIDE_SECTION } from "./guide.js" ;
45import { adaptiveInjectBudget } from "./search/adaptive.js" ;
56
67// Best-effort extraction of the current user's latest message text from the
@@ -144,6 +145,10 @@ export function createInjector(ctx, service, settings, config) {
144145 // stale 1500) and an entry that would exceed it collapses to its title only.
145146 const maxContent = config . injectContentMaxChars ?? 300 ;
146147 const MAX_BLOCK = Math . max ( 1500 , maxContent + 600 ) ;
148+ // #249 第一批:pin 条目的正文硬顶。逐字保真不等于无界——单条上限取
149+ // injectContentMaxChars 与 2000 的较大者,默认档下约束/偏好不会因常规截断
150+ // 失真,同时给「一条超长约束每轮吃满常驻段」留一道闸(超顶仍带截断提示)。
151+ const PINNED_CONTENT_MAX = Math . max ( maxContent , 2000 ) ;
147152
148153 // Compressed injection (v0.5.0 2.1): a sleep-demoted row already carries its
149154 // summary in `content` with the original parked in `_full_content` — inject
@@ -193,26 +198,36 @@ export function createInjector(ctx, service, settings, config) {
193198 return escapePromptVars ( STR . hotHeader [ language ] ( rounds . length , body ) ) ;
194199 }
195200
196- function render ( candidates ) {
201+ function render ( candidates , pinnedStats = null ) {
197202 if ( ! candidates . length ) return "" ;
198203 const header = STR . memoryHeader [ language ] ;
199204 const lines = [ header ] ;
200205 let budget = MAX_BLOCK - header . length ;
201- for ( const m of candidates ) {
206+ // #249 第一批:pin 池(约束/偏好)排在块头且不参与块的塌缩预算——「与情景
207+ // 日志抢同一份预算后被降级」正是本议题要修的结构缺陷。单条仍有硬顶
208+ // (PINNED_CONTENT_MAX),超出照旧带截断提示,不静默。
209+ const pinnedCount = Math . max ( 0 , Math . min ( candidates . length , pinnedStats ?. shown ?? 0 ) ) ;
210+ for ( let i = 0 ; i < candidates . length ; i ++ ) {
211+ const m = candidates [ i ] ;
212+ const pinned = i < pinnedCount ;
202213 // Epistemic trust (v0.4.5): when enabled, measured observations are
203214 // flagged so the agent can weigh them above guesses/opinions.
204215 const verified = config . trustEpistemicWeighting === true && m . epistemic_status === "observation"
205216 ? STR . verified [ language ]
206217 : "" ;
207218 const title = STR . entryTitle [ language ] ( m . title , m . importance ) ;
208- const content = injectMemory ( m ) ;
219+ const content = injectMemory ( m , pinned ? PINNED_CONTENT_MAX : maxContent ) ;
209220 const full = STR . entryLine [ language ] ( m . type , verified , title , content ) ;
210- if ( budget - full . length >= 0 ) {
221+ if ( pinned || budget - full . length >= 0 ) {
211222 lines . push ( full ) ;
212223 budget -= full . length ;
213224 } else {
214225 lines . push ( `- [${ m . type } ] ${ verified } ${ title } ` ) ;
215226 }
227+ if ( pinned && i === pinnedCount - 1 ) {
228+ const hidden = Math . max ( 0 , pinnedStats ?. suppressed ?? 0 ) ;
229+ if ( hidden > 0 ) lines . push ( STR . pinnedOverflow [ language ] ( hidden ) ) ;
230+ }
216231 }
217232 return escapePromptVars ( lines . join ( "\n" ) ) ;
218233 }
@@ -251,7 +266,17 @@ export function createInjector(ctx, service, settings, config) {
251266 return escapePromptVars ( lines . join ( "\n" ) ) ;
252267 }
253268
269+ // #249 第一批:能力说明的系统提示段。order 150 = 插件指引段的既有惯例
270+ // (ACP 的 ACP_SYSTEM_PROMPT 与 mnemon 的 routing 段都在 150)。文本是常量、
271+ // 同会话内不随轮次变化,故不影响其后的前缀缓存,也不逐轮复读。宿主若不提供
272+ // section seam 就静默跳过——#249 §3 的口径是「只用现成 seam,拿不到位的语义
273+ // 降级处理」:能力说明仍落在工具描述上,不算失败。
274+ const guideSection = config . injectGuidanceEnabled === true && typeof ctx . systemPrompt ?. section === "function"
275+ ? ctx . systemPrompt . section ( { name : "memory-guide" , order : 150 , text : MEMORY_GUIDE_SECTION } )
276+ : null ;
277+
254278 const disposers = [
279+ ...( guideSection ? [ guideSection ] : [ ] ) ,
255280 ctx . systemPrompt . context ( {
256281 name : "memory" ,
257282 order : 90 ,
@@ -277,14 +302,18 @@ export function createInjector(ctx, service, settings, config) {
277302 const maxItems = config . injectUncertaintyAdaptive === true
278303 ? adaptiveInjectBudget ( query , baseMaxItems )
279304 : baseMaxItems ;
280- const candidates = service . injectCandidates ( { query, queryVector, maxItems, threshold, scope, rotate, rotateWindow : rotationTurns } ) ;
281- recordInjection ( sessionId , query , candidates ) ;
305+ // #249 第一批:pin 池统计走可选出参,不动 injectCandidates 的数组契约。
306+ const pinnedStats = { } ;
307+ const candidates = service . injectCandidates ( { query, queryVector, maxItems, threshold, scope, rotate, rotateWindow : rotationTurns , pinnedStats } ) ;
308+ // #249:pin 条目不进轮换历史——它们每轮固定前置,记进去只会占满轮换
309+ // 窗口、挤掉情景候选的新鲜度(验收:pin 不参与跨轮轮换)。
310+ recordInjection ( sessionId , query , pinnedStats . shown > 0 ? candidates . slice ( pinnedStats . shown ) : candidates ) ;
282311 // Hot memory (v0.5.0 1.3) leads the single memory block: the agent
283312 // sees the short-term rounds first, then the cross-session recall —
284313 // the documented injection order 1→2. Folding it here (instead of a
285314 // separate context) keeps the prompt assembly stable at two blocks.
286315 const hotText = renderHotContext ( ctx ) ;
287- const body = render ( candidates ) ;
316+ const body = render ( candidates , pinnedStats ) ;
288317 if ( ! hotText ) return body ;
289318 return body ? `${ hotText } \n\n${ body } ` : hotText ;
290319 }
0 commit comments