@@ -518,7 +518,11 @@ export class AgentServer {
518518 await clientConnection . newSession ( {
519519 cwd : this . config . repositoryPath ,
520520 mcpServers : [ ] ,
521- _meta : { sessionId : payload . run_id } ,
521+ _meta : {
522+ sessionId : payload . run_id ,
523+ taskRunId : payload . run_id ,
524+ systemPrompt : { append : this . buildCloudSystemPrompt ( ) } ,
525+ } ,
522526 } ) ;
523527
524528 this . session = {
@@ -533,6 +537,15 @@ export class AgentServer {
533537
534538 this . logger . info ( "Session initialized successfully" ) ;
535539
540+ // Signal in_progress so the UI can start polling for updates
541+ this . posthogAPI
542+ . updateTaskRun ( payload . task_id , payload . run_id , {
543+ status : "in_progress" ,
544+ } )
545+ . catch ( ( err ) =>
546+ this . logger . warn ( "Failed to set task run to in_progress" , err ) ,
547+ ) ;
548+
536549 await this . sendInitialTaskMessage ( payload ) ;
537550 }
538551
@@ -579,6 +592,20 @@ export class AgentServer {
579592 }
580593 }
581594
595+ private buildCloudSystemPrompt ( ) : string {
596+ return `
597+ # Cloud Task Execution
598+
599+ After completing the requested changes:
600+ 1. Create a new branch with a descriptive name based on the work done
601+ 2. Stage and commit all changes with a clear commit message
602+ 3. Push the branch to origin
603+ 4. Create a pull request using \`gh pr create\` with a descriptive title and body
604+
605+ Important: Always create the PR. Do not ask for confirmation.
606+ ` ;
607+ }
608+
582609 private async signalTaskComplete (
583610 payload : JwtPayload ,
584611 stopReason : string ,
@@ -672,11 +699,97 @@ export class AgentServer {
672699 ) {
673700 await this . captureTreeState ( ) ;
674701 }
702+
703+ if (
704+ toolName &&
705+ ( toolName . includes ( "Bash" ) || toolName . includes ( "bash" ) )
706+ ) {
707+ this . detectAndAttachPrUrl ( payload , params . update ) ;
708+ }
675709 }
676710 } ,
677711 } ;
678712 }
679713
714+ private detectAndAttachPrUrl (
715+ payload : JwtPayload ,
716+ update : Record < string , unknown > ,
717+ ) : void {
718+ try {
719+ const meta = ( update ?. _meta as Record < string , unknown > ) ?. claudeCode as
720+ | Record < string , unknown >
721+ | undefined ;
722+ const toolResponse = meta ?. toolResponse ;
723+
724+ // Extract text content from tool response
725+ let textToSearch = "" ;
726+
727+ if ( toolResponse ) {
728+ if ( typeof toolResponse === "string" ) {
729+ textToSearch = toolResponse ;
730+ } else if ( typeof toolResponse === "object" && toolResponse !== null ) {
731+ const respObj = toolResponse as Record < string , unknown > ;
732+ textToSearch =
733+ String ( respObj . stdout || "" ) + String ( respObj . stderr || "" ) ;
734+ if ( ! textToSearch && respObj . output ) {
735+ textToSearch = String ( respObj . output ) ;
736+ }
737+ }
738+ }
739+
740+ // Also check content array
741+ const content = update ?. content ;
742+ if ( Array . isArray ( content ) ) {
743+ for ( const item of content ) {
744+ if ( item . type === "text" && item . text ) {
745+ textToSearch += ` ${ item . text } ` ;
746+ }
747+ }
748+ }
749+
750+ if ( ! textToSearch ) return ;
751+
752+ // Match GitHub PR URLs
753+ const prUrlMatch = textToSearch . match (
754+ / h t t p s : \/ \/ g i t h u b \. c o m \/ [ ^ / ] + \/ [ ^ / ] + \/ p u l l \/ \d + / ,
755+ ) ;
756+ if ( ! prUrlMatch ) return ;
757+
758+ const prUrl = prUrlMatch [ 0 ] ;
759+ this . logger . info ( "Detected PR URL in bash output" , {
760+ runId : payload . run_id ,
761+ prUrl,
762+ } ) ;
763+
764+ // Fire-and-forget: attach PR URL to the task run
765+ this . posthogAPI
766+ . updateTaskRun ( payload . task_id , payload . run_id , {
767+ output : { pr_url : prUrl } ,
768+ } )
769+ . then ( ( ) => {
770+ this . logger . info ( "PR URL attached to task run" , {
771+ taskId : payload . task_id ,
772+ runId : payload . run_id ,
773+ prUrl,
774+ } ) ;
775+ } )
776+ . catch ( ( err ) => {
777+ this . logger . error ( "Failed to attach PR URL to task run" , {
778+ taskId : payload . task_id ,
779+ runId : payload . run_id ,
780+ prUrl,
781+ error : err ,
782+ } ) ;
783+ } ) ;
784+ } catch ( err ) {
785+ // Never let detection errors break message flow
786+ this . logger . debug ( "Error in PR URL detection" , {
787+ runId : payload . run_id ,
788+ error : err ,
789+ } ) ;
790+ }
791+ }
792+
680793 private async cleanupSession ( ) : Promise < void > {
681794 if ( ! this . session ) return ;
682795
0 commit comments