1- import { beforeEach , describe , expect , it , vi } from "vitest" ;
1+ import path from "node:path" ;
2+ import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
3+ import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js" ;
4+ import {
5+ appendTranscriptMessage ,
6+ loadTranscriptEventsSync ,
7+ upsertSessionEntryCore ,
8+ } from "../../config/sessions/session-accessor.js" ;
29import { emitAgentEvent } from "../../infra/agent-events.js" ;
310import { createTestAdmittedRunContext } from "../admitted-run-context.test-support.js" ;
11+ import { loadCliSessionHistoryMessages } from "../cli-runner/session-history.js" ;
12+ import type { RunCliAgentParams } from "../cli-runner/types.js" ;
413import { resolveEmbeddedCliBackendDispatchEligibility } from "./cli-backend-dispatch-eligibility.js" ;
514import { runEmbeddedAgentViaCliBackendIfEligible } from "./cli-backend-dispatch.js" ;
6- import type { RunEmbeddedAgentParams } from "./run/params.js" ;
715import type { EmbeddedAgentRunResult } from "./types.js" ;
816
917const ensureAuthProfileStore = vi . hoisted ( ( ) => vi . fn ( ) ) ;
@@ -14,6 +22,7 @@ const resolveCliRuntimeExecutionProvider = vi.hoisted(() => vi.fn());
1422const runCliAgent = vi . hoisted ( ( ) => vi . fn ( ) ) ;
1523const retireSessionMcpRuntime = vi . hoisted ( ( ) => vi . fn ( ) ) ;
1624const retireSessionMcpRuntimeForSessionKey = vi . hoisted ( ( ) => vi . fn ( ) ) ;
25+ const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
1726
1827vi . mock ( "../model-auth.js" , ( ) => ( {
1928 ensureAuthProfileStore,
@@ -39,17 +48,29 @@ const transcriptRecorder = vi.hoisted(() => ({
3948 flushAssistantSnapshot : vi . fn ( ) ,
4049 finalize : vi . fn ( async ( ) => undefined ) ,
4150} ) ) ;
42- const createCliDispatchTranscriptRecorder = vi . hoisted ( ( ) => vi . fn ( ( ) => transcriptRecorder ) ) ;
51+ const createCliDispatchTranscriptRecorder = vi . hoisted ( ( ) =>
52+ vi . fn < typeof import ( "./cli-backend-dispatch-transcript.js" ) . createCliDispatchTranscriptRecorder > (
53+ ( ) => transcriptRecorder ,
54+ ) ,
55+ ) ;
4356vi . mock ( "./cli-backend-dispatch-transcript.js" , ( ) => ( {
4457 createCliDispatchTranscriptRecorder,
4558} ) ) ;
4659
47- function baseRunParams ( overrides : Partial < RunEmbeddedAgentParams > = { } ) : RunEmbeddedAgentParams {
60+ type CliDispatchParams = Parameters < typeof runEmbeddedAgentViaCliBackendIfEligible > [ 0 ] ;
61+
62+ function baseRunParams ( overrides : Partial < CliDispatchParams > = { } ) : CliDispatchParams {
4863 const runId = overrides . runId ?? "run-cli-dispatch-test" ;
4964 return {
5065 admittedRunContext : createTestAdmittedRunContext ( runId ) ,
5166 sessionId : "recall-session" ,
5267 sessionKey : "agent:main:recall" ,
68+ sessionTarget : {
69+ agentId : "main" ,
70+ sessionId : overrides . sessionId ?? "recall-session" ,
71+ sessionKey : overrides . sessionKey ?? "agent:main:recall" ,
72+ storePath : "/tmp/recall/openclaw-agent.sqlite" ,
73+ } ,
5374 sessionFile : "/tmp/recall/session.jsonl" ,
5475 workspaceDir : "/tmp/recall/workspace" ,
5576 prompt : "recall prompt" ,
@@ -213,7 +234,7 @@ describe("resolveEmbeddedCliBackendDispatchEligibility", () => {
213234} ) ;
214235
215236describe ( "runEmbeddedAgentViaCliBackendIfEligible gate" , ( ) => {
216- const runGate = ( overrides : Partial < RunEmbeddedAgentParams > = { } ) =>
237+ const runGate = ( overrides : Partial < CliDispatchParams > = { } ) =>
217238 runEmbeddedAgentViaCliBackendIfEligible ( baseRunParams ( overrides ) ) ;
218239
219240 it ( "returns undefined without the opt-in" , async ( ) => {
@@ -336,9 +357,69 @@ describe("runEmbeddedAgentViaCliBackendIfEligible gate", () => {
336357} ) ;
337358
338359describe ( "runEmbeddedAgentViaCliBackendIfEligible execution" , ( ) => {
360+ it ( "reads and mirrors the dispatched turn in the selected custom SQLite store" , async ( ) => {
361+ const dir = tempDirs . make ( "cli-dispatch-history-" ) ;
362+ const sessionTarget = {
363+ agentId : "main" ,
364+ sessionId : "recall-session" ,
365+ sessionKey : "agent:main:recall" ,
366+ storePath : path . join ( dir , "openclaw-agent.sqlite" ) ,
367+ } ;
368+ await upsertSessionEntryCore ( sessionTarget , {
369+ sessionId : sessionTarget . sessionId ,
370+ updatedAt : 1 ,
371+ } ) ;
372+ await appendTranscriptMessage ( sessionTarget , {
373+ message : { role : "user" , content : "persisted prior context" , timestamp : 1 } ,
374+ cwd : dir ,
375+ } ) ;
376+ const actualRecorder = await vi . importActual <
377+ typeof import ( "./cli-backend-dispatch-transcript.js" )
378+ > ( "./cli-backend-dispatch-transcript.js" ) ;
379+ createCliDispatchTranscriptRecorder . mockImplementationOnce (
380+ actualRecorder . createCliDispatchTranscriptRecorder ,
381+ ) ;
382+ runCliAgent . mockImplementationOnce ( async ( cliParams : RunCliAgentParams ) => {
383+ expect
384+ . soft ( await loadCliSessionHistoryMessages ( { sessionTarget : cliParams . sessionTarget } ) )
385+ . toEqual (
386+ expect . arrayContaining ( [ expect . objectContaining ( { content : "persisted prior context" } ) ] ) ,
387+ ) ;
388+ return cliRunResult ( ) ;
389+ } ) ;
390+
391+ await runEmbeddedAgentViaCliBackendIfEligible (
392+ baseRunParams ( { sessionTarget, workspaceDir : dir } ) ,
393+ ) ;
394+
395+ expect ( loadTranscriptEventsSync ( sessionTarget ) ) . toEqual (
396+ expect . arrayContaining ( [
397+ expect . objectContaining ( {
398+ message : expect . objectContaining ( {
399+ role : "user" ,
400+ content : [ { type : "text" , text : "recall prompt" } ] ,
401+ } ) ,
402+ } ) ,
403+ expect . objectContaining ( {
404+ message : expect . objectContaining ( {
405+ role : "assistant" ,
406+ content : [ { type : "text" , text : "recall summary" } ] ,
407+ } ) ,
408+ } ) ,
409+ ] ) ,
410+ ) ;
411+ } ) ;
412+
339413 it ( "maps the embedded run onto a one-shot restricted CLI run" , async ( ) => {
340414 runCliAgent . mockResolvedValue ( cliRunResult ( ) ) ;
415+ const sessionTarget = {
416+ agentId : "main" ,
417+ sessionId : "recall-session" ,
418+ sessionKey : "agent:main:recall" ,
419+ storePath : "/tmp/recall/custom/openclaw-agent.sqlite" ,
420+ } ;
341421 const params = baseRunParams ( {
422+ sessionTarget,
342423 toolsAllow : [ "memory_search" , "memory_get" , "notes_retrieve_context" ] ,
343424 } ) ;
344425
@@ -347,6 +428,7 @@ describe("runEmbeddedAgentViaCliBackendIfEligible execution", () => {
347428 expect ( result ?. payloads ?. [ 0 ] ?. text ) . toBe ( "recall summary" ) ;
348429 expect ( runCliAgent ) . toHaveBeenCalledTimes ( 1 ) ;
349430 const cliParams = runCliAgent . mock . calls [ 0 ] ?. [ 0 ] ;
431+ expect ( cliParams ?. sessionTarget ) . toBe ( sessionTarget ) ;
350432 expect ( cliParams ) . toMatchObject ( {
351433 provider : "claude-cli" ,
352434 model : "claude-opus-4-8" ,
@@ -395,7 +477,7 @@ describe("runEmbeddedAgentViaCliBackendIfEligible execution", () => {
395477 ] as const ) ( "refuses dispatch for %s" , async ( _label , overrides ) => {
396478 expect (
397479 await runEmbeddedAgentViaCliBackendIfEligible (
398- baseRunParams ( overrides as Partial < RunEmbeddedAgentParams > ) ,
480+ baseRunParams ( overrides as Partial < CliDispatchParams > ) ,
399481 ) ,
400482 ) . toBeUndefined ( ) ;
401483 expect ( runCliAgent ) . not . toHaveBeenCalled ( ) ;
@@ -432,7 +514,7 @@ describe("runEmbeddedAgentViaCliBackendIfEligible execution", () => {
432514 it ( "forwards execution phases from the CLI backend" , async ( ) => {
433515 const onExecutionPhase = vi . fn ( ) ;
434516 runCliAgent . mockImplementation (
435- async ( cliParams : { onExecutionPhase ?: RunEmbeddedAgentParams [ "onExecutionPhase" ] } ) => {
517+ async ( cliParams : { onExecutionPhase ?: CliDispatchParams [ "onExecutionPhase" ] } ) => {
436518 cliParams . onExecutionPhase ?.( {
437519 phase : "model_call_started" ,
438520 provider : "anthropic" ,
0 commit comments