@@ -438,6 +438,27 @@ export class MemoryService {
438438 . slice ( 0 , cap ) ;
439439 }
440440
441+ async remember ( content : string , scope : MemoryScope , viewTarget : MemoryTarget ) : Promise < number > {
442+ const normalized = content . trim ( ) ;
443+ if ( ! normalized ) throw new Error ( 'Memory content is empty' ) ;
444+ // 在入队前验证 view 是否具备 requested scope,避免产生永远无法执行的任务。
445+ this . entryTarget ( scope , viewTarget ) ;
446+ try {
447+ const jobId = this . store . pushPendingMessages (
448+ [ { role : MessageRole . Human , content : normalized } ] ,
449+ Date . now ( ) ,
450+ viewTarget ,
451+ scope ,
452+ ) ;
453+ this . logMemory ( 'info' , `显式记忆已入队:job=${ jobId } ,scope=${ scope } ,内容=${ truncateForLog ( normalized ) } ` ) ;
454+ void this . checkJobs ( ) ;
455+ return jobId ;
456+ } catch ( e : any ) {
457+ this . logMemory ( 'warn' , `显式记忆入队失败:scope=${ scope } ,错误=${ formatError ( e , true ) } ` ) ;
458+ throw e ;
459+ }
460+ }
461+
441462 getToolDescs ( ) : MemoryToolDescs {
442463 return DEFAULT_TOOL_DESCS ;
443464 }
@@ -602,20 +623,19 @@ export class MemoryService {
602623
603624 // ── MemoryLLM CRUD 抽取(原 MemoryWriterWorker) ──
604625
605- /**
606- * 单轮抽取:把一组对话消息喂给 MemoryLLM,应用返回的 ops。
607- * 模型调用失败会抛出,由 checkJobs 决定是否标记 pending job 为 failed。
608- */
626+ /** 普通 turn 和 remember_memory 共用同一条 extract 执行链;requestedScope 表示显式写入。 */
609627 private async extractFromMessages ( job : PendingMemoryJobRow , target : MemoryTarget ) : Promise < MemoryWriterOpStats > {
610628 const messages = job . messages ?? [ ] ;
629+ const explicitScope = job . requestedScope ;
611630 if ( messages . length === 0 ) {
612631 return { create : 0 , update : 0 , delete : 0 , noop : 1 , failed : 0 } ;
613632 }
614633
615634 const conversation = renderConversation ( messages ) ;
616635 const allRows = await this . listAll ( target ) ;
617- const selection = await this . selectExtractionCandidates ( conversation , allRows , target ) ;
618- if ( ! selection . shouldWrite && selection . rows . length === 0 ) {
636+ const candidateRows = explicitScope ? allRows . filter ( row => row . scope === explicitScope ) : allRows ;
637+ const selection = await this . selectExtractionCandidates ( conversation , candidateRows , target ) ;
638+ if ( ! explicitScope && ! selection . shouldWrite && selection . rows . length === 0 ) {
619639 return { create : 0 , update : 0 , delete : 0 , noop : 1 , failed : 0 } ;
620640 }
621641
@@ -631,6 +651,7 @@ export class MemoryService {
631651 row . body ,
632652 ] . join ( '\n' ) ) . join ( '\n\n---\n\n' ) ;
633653 const input = [
654+ ...( explicitScope ? [ `# Requested scope` , explicitScope , `` ] : [ ] ) ,
634655 `# Current workspace` ,
635656 `` ,
636657 target . scope === MemoryScope . Workspace
@@ -647,37 +668,59 @@ export class MemoryService {
647668 `` ,
648669 `---` ,
649670 `` ,
650- `Decide what — if anything — to record. Default to a single \`noop\` if` ,
651- `nothing in this transcript meets the high-signal bar.` ,
671+ explicitScope
672+ ? `Persist this explicitly requested memory now; do not apply the normal high-signal veto.`
673+ : `Decide what — if anything — to record. Default to a single \`noop\` if` ,
674+ ...( explicitScope ? [ ] : [ `nothing in this transcript meets the high-signal bar.` ] ) ,
652675 ] . join ( '\n' ) ;
653-
654676 const llmMessages : ChatMessage [ ] = [
655677 {
656678 role : MessageRole . System ,
657679 content : [
658680 this . writerPrompt ,
659- `# Authoritative two-stage curation contract` ,
660- `This is the final curation pass. The selected existing memories below include their full bodies.` ,
681+ explicitScope ? `# Explicit persistence contract` : `# Authoritative two-stage curation contract` ,
682+ explicitScope
683+ ? `Return exactly one create/update operation, or noop only when a selected entry already contains the same information. Never delete.`
684+ : `This is the final curation pass. The selected existing memories below include their full bodies.` ,
661685 `Only update or delete an existing memory shown in that selected set, preserving its exact scope and slug.` ,
662686 `When changing a body, return the complete final body without the H1 title line and use bodyMode="replace"; no later merge pass will run.` ,
663687 `Create only when none of the selected entries covers the durable fact.` ,
664688 `Delete only when the full body and conversation clearly prove the entry is false, superseded, or explicitly forgotten.` ,
665- `Default to noop when the selector was cautious but the full evidence does not justify a mutation.` ,
689+ explicitScope
690+ ? `Every mutation must use scope="${ explicitScope } "; the runtime also enforces it.`
691+ : `Default to noop when the selector was cautious but the full evidence does not justify a mutation.` ,
666692 `# Memory scope routing` ,
667693 MEMORY_SCOPE_WRITER_INSTRUCTION ,
668694 ] . join ( '\n\n' ) ,
669695 } ,
670696 { role : MessageRole . Human , content : input } ,
671697 ] ;
672-
673698 const result = await this . writerModel . invokeStructured < MemoryWriteOutput > ( MemoryWriteOutputSchema , llmMessages ) ;
674- // 抽取路径:本轮对话确实提到了被 update 的条目 → evidence +1
675- return await this . applyOps ( result . ops , {
699+
700+ if ( ! explicitScope ) {
701+ return this . applyOps ( result . ops , {
702+ evidenceDelta : 1 ,
703+ target,
704+ fixedScope : null ,
705+ allowedExisting : new Set ( selection . rows . map ( row => MemoryService . rowKey ( row ) ) ) ,
706+ } ) ;
707+ }
708+
709+ if ( result . ops . length !== 1 ) throw new Error ( `Explicit Writer must return exactly one operation` ) ;
710+ const op = result . ops [ 0 ] ;
711+ if ( op . action === MemoryOpAction . Delete ) throw new Error ( `Explicit Writer cannot delete memories` ) ;
712+ if ( op . action === MemoryOpAction . Noop ) {
713+ if ( selection . rows . length === 0 ) throw new Error ( `Writer returned noop without an existing match: ${ op . reason } ` ) ;
714+ return { create : 0 , update : 0 , delete : 0 , noop : 1 , failed : 0 } ;
715+ }
716+ const stats = await this . applyOps ( [ op ] , {
676717 evidenceDelta : 1 ,
677718 target,
678- fixedScope : null ,
719+ fixedScope : explicitScope ,
679720 allowedExisting : new Set ( selection . rows . map ( row => MemoryService . rowKey ( row ) ) ) ,
680721 } ) ;
722+ if ( stats . failed > 0 ) throw new Error ( `Writer operation was not applied` ) ;
723+ return stats ;
681724 }
682725
683726 private async selectExtractionCandidates (
0 commit comments