@@ -110,14 +110,22 @@ function tailLines(content: string, count: number): string[] {
110110
111111async function streamLogUpdates ( logPath : string , offset : number ) : Promise < never > {
112112 let position = offset ;
113+ let reading = false ;
114+ let pending = false ;
113115
114116 const readFromPosition = ( ) : void => {
117+ if ( reading ) {
118+ pending = true ;
119+ return ;
120+ }
121+
115122 const size = statSync ( logPath ) . size ;
116123 if ( size < position ) {
117124 position = 0 ;
118125 }
119126 if ( size === position ) return ;
120127
128+ reading = true ;
121129 const stream = createReadStream ( logPath , {
122130 encoding : "utf-8" ,
123131 start : position ,
@@ -130,17 +138,33 @@ async function streamLogUpdates(logPath: string, offset: number): Promise<never>
130138
131139 stream . on ( "end" , ( ) => {
132140 position = size ;
141+ reading = false ;
142+ if ( pending ) {
143+ pending = false ;
144+ readFromPosition ( ) ;
145+ }
133146 } ) ;
134- } ;
135147
136- readFromPosition ( ) ;
148+ stream . on ( "error" , ( ) => {
149+ reading = false ;
150+ } ) ;
151+ } ;
137152
138- await new Promise < never > ( ( _resolve ) => {
139- watch ( logPath , { persistent : true } , ( eventType ) => {
153+ await new Promise < never > ( ( ) => {
154+ const watcher = watch ( logPath , { persistent : true } , ( eventType ) => {
140155 if ( eventType === "change" || eventType === "rename" ) {
141156 readFromPosition ( ) ;
142157 }
143158 } ) ;
159+
160+ const handleSigint = ( ) : void => {
161+ watcher . close ( ) ;
162+ process . removeListener ( "SIGINT" , handleSigint ) ;
163+ process . stdout . write ( "\n" ) ;
164+ process . exit ( 0 ) ;
165+ } ;
166+
167+ process . on ( "SIGINT" , handleSigint ) ;
144168 } ) ;
145169
146170 process . exit ( 0 ) ;
@@ -149,12 +173,12 @@ async function streamLogUpdates(logPath: string, offset: number): Promise<never>
149173async function logAgentRuntime ( args : AgentArgs ) : Promise < void > {
150174 const agentId = args . id ;
151175 if ( ! agentId ) {
152- console . error ( "Usage: tps agent logs --id <agent-id> [--lines <N>] [--follow]" ) ;
176+ console . error ( "Usage: tps agent logs --id <agent-id> [--lines <N>] [--follow|-f ]" ) ;
153177 process . exit ( 1 ) ;
154178 }
155179
156180 const lineCount = normalizeLogLineCount ( args . lines ) ;
157- const logPath = join ( healthcheckHomeDir ( ) , ".tps" , "logs " , ` ${ agentId } .log` ) ;
181+ const logPath = join ( healthcheckHomeDir ( ) , ".tps" , "agents " , agentId , "session .log" ) ;
158182 if ( ! existsSync ( logPath ) ) {
159183 console . error ( `No log file found for ${ agentId } ` ) ;
160184 process . exit ( 1 ) ;
0 commit comments