@@ -4,6 +4,19 @@ import fs from "node:fs/promises";
44import os from "node:os" ;
55import path from "node:path" ;
66import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
7+
8+ const mocks = vi . hoisted ( ( ) => ( {
9+ readClaudeCliFallbackSeed : vi . fn ( ) ,
10+ } ) ) ;
11+
12+ vi . mock ( "../cli-runner/log.js" , ( ) => ( {
13+ cliBackendLog : { warn : vi . fn ( ) } ,
14+ } ) ) ;
15+
16+ vi . mock ( "../../gateway/cli-session-history.js" , ( ) => ( {
17+ readClaudeCliFallbackSeed : mocks . readClaudeCliFallbackSeed ,
18+ } ) ) ;
19+
720import { cliBackendLog } from "../cli-runner/log.js" ;
821import {
922 buildClaudeCliFallbackContextPrelude ,
@@ -41,16 +54,6 @@ describe("resolveFallbackRetryPrompt", () => {
4154 ) . toBe ( `[Retry after the previous model attempt failed or timed out]\n\n${ originalBody } ` ) ;
4255 } ) ;
4356
44- it ( "preserves original body for fallback retry when session has no history (subagent spawn)" , ( ) => {
45- expect (
46- resolveFallbackRetryPrompt ( {
47- body : originalBody ,
48- isFallbackRetry : true ,
49- sessionHasHistory : false ,
50- } ) ,
51- ) . toBe ( originalBody ) ;
52- } ) ;
53-
5457 it ( "preserves original body for fallback retry when sessionHasHistory is undefined" , ( ) => {
5558 expect (
5659 resolveFallbackRetryPrompt ( {
@@ -78,16 +81,6 @@ describe("resolveFallbackRetryPrompt", () => {
7881 ) . toBe ( originalBody ) ;
7982 } ) ;
8083
81- it ( "preserves original body on fallback retry without history" , ( ) => {
82- expect (
83- resolveFallbackRetryPrompt ( {
84- body : originalBody ,
85- isFallbackRetry : true ,
86- sessionHasHistory : false ,
87- } ) ,
88- ) . toBe ( originalBody ) ;
89- } ) ;
90-
9184 it ( "prepends priorContextPrelude before the retry marker on fallback retry" , ( ) => {
9285 const prelude = "## Prior session context (from claude-cli)\nuser: prior question" ;
9386 // Claude fallback prelude must come before the retry marker so the model
@@ -244,68 +237,60 @@ describe("formatClaudeCliFallbackPrelude", () => {
244237} ) ;
245238
246239describe ( "buildClaudeCliFallbackContextPrelude" , ( ) => {
240+ beforeEach ( ( ) => {
241+ mocks . readClaudeCliFallbackSeed . mockReset ( ) ;
242+ } ) ;
243+
247244 it ( "returns empty string when no sessionId is provided" , ( ) => {
248245 expect ( buildClaudeCliFallbackContextPrelude ( { cliSessionId : undefined } ) ) . toBe ( "" ) ;
249246 expect ( buildClaudeCliFallbackContextPrelude ( { cliSessionId : " " } ) ) . toBe ( "" ) ;
247+ expect ( mocks . readClaudeCliFallbackSeed ) . not . toHaveBeenCalled ( ) ;
250248 } ) ;
251249
252- it ( "returns empty string when the Claude session file does not exist" , async ( ) => {
253- const tmpHome = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-fallback-prelude-" ) ) ;
254- try {
255- expect (
256- buildClaudeCliFallbackContextPrelude ( {
257- cliSessionId : "missing-session" ,
258- homeDir : tmpHome ,
259- } ) ,
260- ) . toBe ( "" ) ;
261- } finally {
262- await fs . rm ( tmpHome , { recursive : true , force : true } ) ;
263- }
250+ it ( "returns empty string when the Claude session loader finds no seed" , ( ) => {
251+ mocks . readClaudeCliFallbackSeed . mockReturnValue ( undefined ) ;
252+
253+ expect (
254+ buildClaudeCliFallbackContextPrelude ( {
255+ cliSessionId : "missing-session" ,
256+ homeDir : "/tmp/test-home" ,
257+ } ) ,
258+ ) . toBe ( "" ) ;
259+ expect ( mocks . readClaudeCliFallbackSeed ) . toHaveBeenCalledWith ( {
260+ cliSessionId : "missing-session" ,
261+ homeDir : "/tmp/test-home" ,
262+ } ) ;
264263 } ) ;
265264
266- it ( "reads a real Claude JSONL fixture and emits a labeled prelude end-to-end" , async ( ) => {
267- const tmpHome = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-fallback-prelude-" ) ) ;
268- const sessionId = "e2e-session" ;
269- const projectsDir = path . join ( tmpHome , ".claude" , "projects" , "demo" ) ;
270- try {
271- // Use Claude's JSONL shape directly so parser and formatter behavior stay
272- // aligned with real CLI transcripts rather than synthetic message arrays.
273- await fs . mkdir ( projectsDir , { recursive : true } ) ;
274- const lines = [
265+ it ( "formats the Claude session loader seed into a labeled fallback prelude" , ( ) => {
266+ mocks . readClaudeCliFallbackSeed . mockReturnValue ( {
267+ recentTurns : [
275268 {
276- type : "user" ,
277- uuid : "u1" ,
278- message : { role : "user" , content : "prior question about deploys" } ,
269+ role : "user" ,
270+ content : "prior question about deploys" ,
279271 } ,
280272 {
281- type : "assistant" ,
282- uuid : "a1" ,
283- message : {
284- role : "assistant" ,
285- model : "claude-sonnet-4-6" ,
286- content : [
287- { type : "text" , text : "prior answer about blue-green" } ,
288- { type : "tool_use" , id : "toolu_1" , name : "Bash" , input : { command : "pwd" } } ,
289- ] ,
290- } ,
273+ role : "assistant" ,
274+ content : [
275+ { type : "text" , text : "prior answer about blue-green" } ,
276+ { type : "toolcall" , name : "Bash" } ,
277+ ] ,
291278 } ,
292- ] ;
293- await fs . writeFile (
294- path . join ( projectsDir , `${ sessionId } .jsonl` ) ,
295- `${ lines . map ( ( line ) => JSON . stringify ( line ) ) . join ( "\n" ) } \n` ,
296- "utf-8" ,
297- ) ;
298- const prelude = buildClaudeCliFallbackContextPrelude ( {
299- cliSessionId : sessionId ,
300- homeDir : tmpHome ,
301- } ) ;
302- expect ( prelude ) . toContain ( "## Prior session context (from claude-cli)" ) ;
303- expect ( prelude ) . toContain ( "user: prior question about deploys" ) ;
304- expect ( prelude ) . toContain ( "assistant: prior answer about blue-green" ) ;
305- expect ( prelude ) . toContain ( "(tool call: Bash)" ) ;
306- } finally {
307- await fs . rm ( tmpHome , { recursive : true , force : true } ) ;
308- }
279+ ] ,
280+ } ) ;
281+
282+ const prelude = buildClaudeCliFallbackContextPrelude ( {
283+ cliSessionId : " e2e-session " ,
284+ homeDir : "/tmp/test-home" ,
285+ } ) ;
286+ expect ( mocks . readClaudeCliFallbackSeed ) . toHaveBeenCalledWith ( {
287+ cliSessionId : "e2e-session" ,
288+ homeDir : "/tmp/test-home" ,
289+ } ) ;
290+ expect ( prelude ) . toContain ( "## Prior session context (from claude-cli)" ) ;
291+ expect ( prelude ) . toContain ( "user: prior question about deploys" ) ;
292+ expect ( prelude ) . toContain ( "assistant: prior answer about blue-green" ) ;
293+ expect ( prelude ) . toContain ( "(tool call: Bash)" ) ;
309294 } ) ;
310295} ) ;
311296
@@ -505,26 +490,6 @@ describe("claudeCliSessionTranscriptHasContent", () => {
505490
506491 const GRACE_MS = 250 ;
507492
508- it ( "returns false when the Claude project transcript is missing or empty" , async ( ) => {
509- const workspaceDir = await makeWorkspace ( ) ;
510- expect (
511- await claudeCliSessionTranscriptHasContent ( {
512- sessionId : "missing-session" ,
513- workspaceDir,
514- homeDir : tmpDir ,
515- } ) ,
516- ) . toBe ( false ) ;
517-
518- await writeClaudeProjectFile ( workspaceDir , "empty-session" , "" ) ;
519- expect (
520- await claudeCliSessionTranscriptHasContent ( {
521- sessionId : "empty-session" ,
522- workspaceDir,
523- homeDir : tmpDir ,
524- } ) ,
525- ) . toBe ( false ) ;
526- } ) ;
527-
528493 it ( "returns true when the Claude project transcript has an assistant message" , async ( ) => {
529494 const workspaceDir = await makeWorkspace ( ) ;
530495 await writeClaudeProjectFile (
0 commit comments