@@ -444,15 +444,9 @@ export class MemoryService {
444444 // 在入队前验证 view 是否具备 requested scope,避免产生永远无法执行的任务。
445445 this . entryTarget ( scope , viewTarget ) ;
446446 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 ;
447+ const id = this . store . pushPendingRememberMessage ( normalized , scope , Date . now ( ) , viewTarget ) ;
448+ this . logMemory ( 'info' , `显式记忆消息已暂存:id=${ id } ,scope=${ scope } ,内容=${ truncateForLog ( normalized ) } ` ) ;
449+ return id ;
456450 } catch ( e : any ) {
457451 this . logMemory ( 'warn' , `显式记忆入队失败:scope=${ scope } ,错误=${ formatError ( e , true ) } ` ) ;
458452 throw e ;
@@ -623,19 +617,22 @@ export class MemoryService {
623617
624618 // ── MemoryLLM CRUD 抽取(原 MemoryWriterWorker) ──
625619
626- /** 普通 turn 和 remember_memory 共用同一条 extract 执行链;requestedScope 表示显式写入 。 */
620+ /** pending remember message 已在入队时合并进 messages,后续完全复用普通抽取 。 */
627621 private async extractFromMessages ( job : PendingMemoryJobRow , target : MemoryTarget ) : Promise < MemoryWriterOpStats > {
628622 const messages = job . messages ?? [ ] ;
629- const explicitScope = job . requestedScope ;
630623 if ( messages . length === 0 ) {
631624 return { create : 0 , update : 0 , delete : 0 , noop : 1 , failed : 0 } ;
632625 }
633626
634627 const conversation = renderConversation ( messages ) ;
635628 const allRows = await this . listAll ( target ) ;
636- const candidateRows = explicitScope ? allRows . filter ( row => row . scope === explicitScope ) : allRows ;
629+ const explicitScopes = new Set ( messages
630+ . filter ( message => message . additional_kwargs ?. explicitMemory === true )
631+ . map ( message => message . additional_kwargs ?. memoryScope as MemoryScope ) ) ;
632+ const fixedScope = explicitScopes . size === 1 ? [ ...explicitScopes ] [ 0 ] : null ;
633+ const candidateRows = fixedScope ? allRows . filter ( row => row . scope === fixedScope ) : allRows ;
637634 const selection = await this . selectExtractionCandidates ( conversation , candidateRows , target ) ;
638- if ( ! explicitScope && ! selection . shouldWrite && selection . rows . length === 0 ) {
635+ if ( explicitScopes . size === 0 && ! selection . shouldWrite && selection . rows . length === 0 ) {
639636 return { create : 0 , update : 0 , delete : 0 , noop : 1 , failed : 0 } ;
640637 }
641638
@@ -651,7 +648,6 @@ export class MemoryService {
651648 row . body ,
652649 ] . join ( '\n' ) ) . join ( '\n\n---\n\n' ) ;
653650 const input = [
654- ...( explicitScope ? [ `# Requested scope` , explicitScope , `` ] : [ ] ) ,
655651 `# Current workspace` ,
656652 `` ,
657653 target . scope === MemoryScope . Workspace
@@ -668,59 +664,35 @@ export class MemoryService {
668664 `` ,
669665 `---` ,
670666 `` ,
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.` ] ) ,
667+ `Decide what — if anything — to record. Default to a single \`noop\` if` ,
668+ `nothing in this transcript meets the high-signal bar.` ,
675669 ] . join ( '\n' ) ;
676670 const llmMessages : ChatMessage [ ] = [
677671 {
678672 role : MessageRole . System ,
679673 content : [
680674 this . writerPrompt ,
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.` ,
675+ `# Authoritative two-stage curation contract` ,
676+ `This is the final curation pass. The selected existing memories below include their full bodies.` ,
677+ ...( explicitScopes . size > 0 ? [ `Messages marked [EXPLICIT MEMORY WRITE] must be persisted using their required scope.` ] : [ ] ) ,
685678 `Only update or delete an existing memory shown in that selected set, preserving its exact scope and slug.` ,
686679 `When changing a body, return the complete final body without the H1 title line and use bodyMode="replace"; no later merge pass will run.` ,
687680 `Create only when none of the selected entries covers the durable fact.` ,
688681 `Delete only when the full body and conversation clearly prove the entry is false, superseded, or explicitly forgotten.` ,
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.` ,
682+ `Default to noop when the selector was cautious but the full evidence does not justify a mutation.` ,
692683 `# Memory scope routing` ,
693684 MEMORY_SCOPE_WRITER_INSTRUCTION ,
694685 ] . join ( '\n\n' ) ,
695686 } ,
696687 { role : MessageRole . Human , content : input } ,
697688 ] ;
698689 const result = await this . writerModel . invokeStructured < MemoryWriteOutput > ( MemoryWriteOutputSchema , llmMessages ) ;
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 ] , {
690+ return await this . applyOps ( result . ops , {
717691 evidenceDelta : 1 ,
718692 target,
719- fixedScope : explicitScope ,
693+ fixedScope,
720694 allowedExisting : new Set ( selection . rows . map ( row => MemoryService . rowKey ( row ) ) ) ,
721695 } ) ;
722- if ( stats . failed > 0 ) throw new Error ( `Writer operation was not applied` ) ;
723- return stats ;
724696 }
725697
726698 private async selectExtractionCandidates (
0 commit comments