@@ -2,6 +2,7 @@ import type { AgentMessage } from "@earendil-works/pi-agent-core";
22import { SessionManager } from "@earendil-works/pi-coding-agent" ;
33import type { ReplyPayload } from "../auto-reply/reply-payload.js" ;
44import { SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js" ;
5+ import { appendSessionTranscriptMessage } from "../config/sessions/transcript-append.js" ;
56import { formatErrorMessage } from "../infra/errors.js" ;
67import { createSubsystemLogger } from "../logging/subsystem.js" ;
78import { buildAgentHookContextChannelFields } from "../plugins/hook-agent-context.js" ;
@@ -109,6 +110,35 @@ function buildCliContextEngineAssistantMessage(params: {
109110 return buildCliHookAssistantMessage ( params ) as AgentMessage ;
110111}
111112
113+ async function persistCliCurrentUserMessage ( params : RunCliAgentParams ) : Promise < boolean > {
114+ if ( params . suppressNextUserMessagePersistence === true ) {
115+ return false ;
116+ }
117+ const prompt = params . transcriptPrompt ?? params . prompt ;
118+ if ( ! prompt . trim ( ) ) {
119+ return false ;
120+ }
121+
122+ try {
123+ const { message } = await appendSessionTranscriptMessage ( {
124+ transcriptPath : params . sessionFile ,
125+ sessionId : params . sessionId ,
126+ cwd : params . workspaceDir ,
127+ config : params . config ,
128+ message : {
129+ role : "user" ,
130+ content : prompt ,
131+ timestamp : Date . now ( ) ,
132+ } ,
133+ } ) ;
134+ params . onUserMessagePersisted ?.( message as Extract < AgentMessage , { role : "user" } > ) ;
135+ return true ;
136+ } catch ( err ) {
137+ log . warn ( `CLI run: failed to persist user transcript message: ${ formatErrorMessage ( err ) } ` ) ;
138+ return false ;
139+ }
140+ }
141+
112142type CliAgentEndHookParams = Parameters < typeof runAgentHarnessAgentEndHook > [ 0 ] ;
113143
114144function shouldAwaitCliAgentEndHook ( params : RunCliAgentParams ) : boolean {
@@ -368,6 +398,14 @@ export async function runPreparedCliAgent(
368398 } ,
369399 } ) ;
370400
401+ let currentUserMessagePersisted = false ;
402+ const persistCurrentUserMessage = async ( ) : Promise < void > => {
403+ if ( currentUserMessagePersisted ) {
404+ return ;
405+ }
406+ currentUserMessagePersisted = await persistCliCurrentUserMessage ( params ) ;
407+ } ;
408+
371409 const persistBlockedBeforeAgentRun = async ( block : {
372410 message : string ;
373411 pluginId : string ;
@@ -620,6 +658,7 @@ export async function runPreparedCliAgent(
620658 }
621659 }
622660
661+ await persistCurrentUserMessage ( ) ;
623662 runAgentHarnessLlmInputHook ( {
624663 event : llmInputEvent ,
625664 ctx : hookContext ,
@@ -658,6 +697,7 @@ export async function runPreparedCliAgent(
658697
659698 // For now, retry without the session ID to create a new session
660699 try {
700+ await persistCurrentUserMessage ( ) ;
661701 const { output, lastAssistant } = await executeCliAttempt ( undefined ) ;
662702 const assistantText = output . text . trim ( ) ;
663703 const effectiveCliSessionId = output . sessionId ;
@@ -743,6 +783,8 @@ export function buildRunClaudeCliAgentParams(params: RunClaudeCliAgentParams): R
743783 images : params . images ,
744784 messageChannel : params . messageChannel ,
745785 messageProvider : params . messageProvider ,
786+ suppressNextUserMessagePersistence : params . suppressNextUserMessagePersistence ,
787+ onUserMessagePersisted : params . onUserMessagePersisted ,
746788 } ;
747789}
748790
0 commit comments