@@ -15,6 +15,7 @@ import { appendSessionTranscriptMessageByIdentity } from "openclaw/plugin-sdk/se
1515import { afterAll , afterEach , beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
1616import { applyCliRuntimeRecallTimeoutDefault } from "./config.js" ;
1717import plugin , { testing } from "./index.js" ;
18+ import { resolveActiveRecallForRun } from "./recall-state.js" ;
1819import { hasRememberAcrossConversationsAgent } from "./session-policy.js" ;
1920
2021// Match only lone surrogates so valid supplementary-plane characters remain allowed.
@@ -715,12 +716,13 @@ describe("active-memory plugin", () => {
715716 stateDir = "" ;
716717 } ) ;
717718
718- it ( "registers a before_prompt_build hook " , ( ) => {
719+ it ( "registers prompt-build and run-cleanup hooks " , ( ) => {
719720 const [ hookName , handler , options ] = firstHookRegistration ( ) ;
720721 expect ( hookName ) . toBe ( "before_prompt_build" ) ;
721722 expect ( typeof handler ) . toBe ( "function" ) ;
722723 expect ( options ) . toEqual ( { timeoutMs : 153_000 } ) ;
723724 expect ( hookOptions . before_prompt_build ?. timeoutMs ) . toBe ( 153_000 ) ;
725+ expect ( typeof hooks . agent_end ) . toBe ( "function" ) ;
724726 } ) ;
725727
726728 it ( "does not synthesize a main agent when every configured agent opts out" , ( ) => {
@@ -847,6 +849,178 @@ describe("active-memory plugin", () => {
847849 expect ( secondSessionKey ) . not . toBe ( firstSessionKey ) ;
848850 } ) ;
849851
852+ it ( "shares recall results across changed-prompt retries in one run" , async ( ) => {
853+ const context = {
854+ runId : "run-changed-prompt-retry" ,
855+ sessionKey : "agent:main:changed-prompt-retry" ,
856+ } ;
857+
858+ const first = await runPromptBuild ( { prompt : "what wings should i order?" } , context ) ;
859+ const second = await runPromptBuild (
860+ { prompt : "actually, what did I order last time?" } ,
861+ context ,
862+ ) ;
863+
864+ expectPrependContextContains ( first , "lemon pepper wings" ) ;
865+ expectPrependContextContains ( second , "lemon pepper wings" ) ;
866+ expect ( runEmbeddedAgent ) . toHaveBeenCalledTimes ( 1 ) ;
867+ } ) ;
868+
869+ it ( "joins concurrent recall attempts in one run" , async ( ) => {
870+ let releaseRecall : ( ) => void = ( ) => {
871+ throw new Error ( "recall gate was not initialized" ) ;
872+ } ;
873+ const recallGate = new Promise < void > ( ( resolve ) => {
874+ releaseRecall = resolve ;
875+ } ) ;
876+ runEmbeddedAgent . mockImplementationOnce ( async ( params : { sessionFile : string } ) => {
877+ await recallGate ;
878+ await writeUsableMemoryTranscript ( params . sessionFile , "lemon pepper wings" ) ;
879+ return { payloads : [ { text : "- lemon pepper wings" } ] } ;
880+ } ) ;
881+ const context = {
882+ runId : "run-concurrent-attempts" ,
883+ sessionKey : "agent:main:concurrent-attempts" ,
884+ } ;
885+
886+ const first = runPromptBuild ( { prompt : "what wings should i order?" } , context ) ;
887+ await vi . waitFor ( ( ) => {
888+ expect ( runEmbeddedAgent ) . toHaveBeenCalledTimes ( 1 ) ;
889+ } ) ;
890+ const second = runPromptBuild ( { prompt : "what wings did I order last time?" } , context ) ;
891+ await new Promise < void > ( ( resolve ) => {
892+ setImmediate ( resolve ) ;
893+ } ) ;
894+
895+ expect ( runEmbeddedAgent ) . toHaveBeenCalledTimes ( 1 ) ;
896+ releaseRecall ( ) ;
897+ const [ firstResult , secondResult ] = await Promise . all ( [ first , second ] ) ;
898+ expectPrependContextContains ( firstResult , "lemon pepper wings" ) ;
899+ expectPrependContextContains ( secondResult , "lemon pepper wings" ) ;
900+ } ) ;
901+
902+ it ( "waits for timeout cleanup before replacing a recall in the same run" , async ( ) => {
903+ testing . setMinimumTimeoutMsForTests ( 1 ) ;
904+ testing . setSetupGraceTimeoutMsForTests ( 0 ) ;
905+ registerPluginConfig ( { timeoutMs : 100 , logging : true } ) ;
906+ let releaseCleanup : ( ) => void = ( ) => {
907+ throw new Error ( "cleanup gate was not initialized" ) ;
908+ } ;
909+ const cleanupGate = new Promise < void > ( ( resolve ) => {
910+ releaseCleanup = resolve ;
911+ } ) ;
912+ hoisted . closeActiveMemorySearchManager . mockImplementationOnce ( async ( ) => {
913+ await cleanupGate ;
914+ } ) ;
915+ runEmbeddedAgent . mockImplementationOnce (
916+ async ( params : { abortSignal ?: AbortSignal } ) => await waitForAbort ( params . abortSignal ) ,
917+ ) ;
918+ const context = {
919+ runId : "run-timeout-retry" ,
920+ sessionKey : "agent:main:timeout-retry" ,
921+ } ;
922+
923+ await expect (
924+ runPromptBuild ( { prompt : "what wings should i order before timeout?" } , context ) ,
925+ ) . resolves . toBeUndefined ( ) ;
926+ await vi . waitFor ( ( ) => {
927+ expect ( hoisted . closeActiveMemorySearchManager ) . toHaveBeenCalledTimes ( 1 ) ;
928+ } ) ;
929+
930+ const retry = runPromptBuild ( { prompt : "what wings should i order after timeout?" } , context ) ;
931+ await new Promise < void > ( ( resolve ) => {
932+ setImmediate ( resolve ) ;
933+ } ) ;
934+ expect ( runEmbeddedAgent ) . toHaveBeenCalledTimes ( 1 ) ;
935+
936+ releaseCleanup ( ) ;
937+ await expect ( retry ) . resolves . toEqual (
938+ expect . objectContaining ( { prependContext : expect . stringContaining ( "lemon pepper wings" ) } ) ,
939+ ) ;
940+ expect ( runEmbeddedAgent ) . toHaveBeenCalledTimes ( 2 ) ;
941+ } ) ;
942+
943+ it ( "evicts a rejected replacement after timeout cleanup settles" , async ( ) => {
944+ let releaseCleanup : ( ) => void = ( ) => {
945+ throw new Error ( "cleanup gate was not initialized" ) ;
946+ } ;
947+ const cleanupGate = new Promise < void > ( ( resolve ) => {
948+ releaseCleanup = resolve ;
949+ } ) ;
950+ const initialResult = { status : "timeout" as const , elapsedMs : 1 , summary : null } ;
951+ await expect (
952+ resolveActiveRecallForRun ( "run-rejected-replacement" , async ( onTimeoutCleanup ) => {
953+ onTimeoutCleanup ( cleanupGate ) ;
954+ return initialResult ;
955+ } ) ,
956+ ) . resolves . toEqual ( initialResult ) ;
957+
958+ let rejectedReplacementStarts = 0 ;
959+ const rejectedReplacement = resolveActiveRecallForRun ( "run-rejected-replacement" , async ( ) => {
960+ rejectedReplacementStarts ++ ;
961+ throw new Error ( "retry deadline expired" ) ;
962+ } ) ;
963+ releaseCleanup ( ) ;
964+ await expect ( rejectedReplacement ) . rejects . toThrow ( "retry deadline expired" ) ;
965+
966+ const freshResult = {
967+ status : "ok" as const ,
968+ elapsedMs : 2 ,
969+ rawReply : "recovered" ,
970+ summary : "recovered" ,
971+ } ;
972+ let freshStarts = 0 ;
973+ await expect (
974+ resolveActiveRecallForRun ( "run-rejected-replacement" , async ( ) => {
975+ freshStarts ++ ;
976+ return freshResult ;
977+ } ) ,
978+ ) . resolves . toEqual ( freshResult ) ;
979+ expect ( { freshStarts, rejectedReplacementStarts } ) . toEqual ( {
980+ freshStarts : 1 ,
981+ rejectedReplacementStarts : 1 ,
982+ } ) ;
983+ } ) ;
984+
985+ it ( "deduplicates cache-disabled private recall until the run ends" , async ( ) => {
986+ setMemorySlot ( "memory-core" ) ;
987+ configFile = {
988+ ...configFile ,
989+ agents : {
990+ defaults : {
991+ model : { primary : "github-copilot/gpt-5.4-mini" } ,
992+ } ,
993+ list : [
994+ {
995+ id : "main" ,
996+ memory : { search : { rememberAcrossConversations : true } } ,
997+ } ,
998+ ] ,
999+ } ,
1000+ } ;
1001+ const context = {
1002+ runId : "run-private-recall" ,
1003+ sessionKey : "agent:main:telegram:direct:owner" ,
1004+ messageProvider : "telegram" ,
1005+ channelId : "owner" ,
1006+ } ;
1007+ seedSession ( context . sessionKey , "s-private-recall" , 0 ) ;
1008+
1009+ await runPromptBuild ( { prompt : "what wings should i order?" } , context ) ;
1010+ await runPromptBuild ( { prompt : "what wings should i order?" } , context ) ;
1011+
1012+ expect ( lastEmbeddedRunParams ( ) . conversationRecall ) . toEqual ( {
1013+ anchorSessionKey : context . sessionKey ,
1014+ scope : "same-agent-private" ,
1015+ corpus : "configured" ,
1016+ } ) ;
1017+ expect ( runEmbeddedAgent ) . toHaveBeenCalledTimes ( 1 ) ;
1018+
1019+ await requireHook ( "agent_end" ) ( { runId : context . runId , messages : [ ] , success : true } , context ) ;
1020+ await runPromptBuild ( { prompt : "what wings should i order?" } , context ) ;
1021+ expect ( runEmbeddedAgent ) . toHaveBeenCalledTimes ( 2 ) ;
1022+ } ) ;
1023+
8501024 it ( "retries transient SQLite recall cleanup failures" , async ( ) => {
8511025 hoisted . cleanupSessionLifecycleArtifacts . mockRejectedValueOnce (
8521026 new Error ( "session store is busy" ) ,
0 commit comments