@@ -2,13 +2,25 @@ import { randomUUID } from "node:crypto";
22import { mkdirSync , rmSync , symlinkSync } from "node:fs" ;
33import { tmpdir } from "node:os" ;
44import { join } from "node:path" ;
5- import { Agent , PermissionMode } from "@posthog/agent" ;
5+ import { Agent , type OnLogCallback , PermissionMode } from "@posthog/agent" ;
66import {
77 app ,
88 type BrowserWindow ,
99 type IpcMainInvokeEvent ,
1010 ipcMain ,
1111} from "electron" ;
12+ import { logger } from "../lib/logger" ;
13+
14+ const log = logger . scope ( "agent" ) ;
15+
16+ const onAgentLog : OnLogCallback = ( level , scope , message , data ) => {
17+ const scopedLog = logger . scope ( scope ) ;
18+ if ( data !== undefined ) {
19+ scopedLog [ level ] ( message , data ) ;
20+ } else {
21+ scopedLog [ level ] ( message ) ;
22+ }
23+ } ;
1224
1325interface AgentStartParams {
1426 taskId : string ;
@@ -35,18 +47,12 @@ export interface TaskController {
3547}
3648
3749function getClaudeCliPath ( ) : string {
38- const { existsSync } = require ( "node:fs" ) ;
3950 const appPath = app . getAppPath ( ) ;
4051
4152 const claudeCliPath = app . isPackaged
4253 ? join ( `${ appPath } .unpacked` , ".vite/build/claude-cli/cli.js" )
4354 : join ( appPath , ".vite/build/claude-cli/cli.js" ) ;
4455
45- console . log ( "[agent] Claude CLI path:" , claudeCliPath ) ;
46- console . log ( "[agent] Claude CLI exists:" , existsSync ( claudeCliPath ) ) ;
47- console . log ( "[agent] App path:" , appPath ) ;
48- console . log ( "[agent] Is packaged:" , app . isPackaged ) ;
49-
5056 return claudeCliPath ;
5157}
5258
@@ -106,12 +112,9 @@ export function registerAgentIpc(
106112 process . env . CLAUDE_CONFIG_DIR || join ( app . getPath ( "home" ) , ".claude" ) ;
107113 const statsigPath = join ( claudeConfigDir , "statsig" ) ;
108114 rmSync ( statsigPath , { recursive : true , force : true } ) ;
109- console . log (
110- "[agent] Cleared statsig cache to work around input_examples bug" ,
111- ) ;
115+ log . info ( "Cleared statsig cache to work around input_examples bug" ) ;
112116 } catch ( error ) {
113- // Ignore errors if the folder doesn't exist
114- console . warn ( "[agent] Could not clear statsig cache:" , error ) ;
117+ log . warn ( "Could not clear statsig cache:" , error ) ;
115118 }
116119
117120 const taskId = randomUUID ( ) ;
@@ -133,7 +136,7 @@ export function registerAgentIpc(
133136 if ( stderrBuffer . length > 50 ) {
134137 stderrBuffer . shift ( ) ;
135138 }
136- console . error ( `[agent] [claude-stderr] ${ text } ` ) ;
139+ log . error ( `[claude-stderr] ${ text } ` ) ;
137140 emitToRenderer ( {
138141 type : "status" ,
139142 ts : Date . now ( ) ,
@@ -156,6 +159,7 @@ export function registerAgentIpc(
156159 posthogApiUrl : apiHost ,
157160 posthogProjectId : projectId ,
158161 debug : true ,
162+ onLog : onAgentLog ,
159163 } ) ;
160164
161165 const controllerEntry : TaskController = {
@@ -191,7 +195,7 @@ export function registerAgentIpc(
191195 } ) ;
192196 }
193197 } catch ( err ) {
194- console . warn ( "[agent] failed to fetch task progress" , err ) ;
198+ log . warn ( "Failed to fetch task progress" , err ) ;
195199 }
196200 } ;
197201
@@ -224,7 +228,7 @@ export function registerAgentIpc(
224228 } catch { }
225229 symlinkSync ( process . execPath , nodeSymlinkPath ) ;
226230 } catch ( err ) {
227- console . warn ( "[agent] Failed to setup mock node environment" , err ) ;
231+ log . warn ( "Failed to setup mock node environment" , err ) ;
228232 }
229233
230234 const newPath = `${ mockNodeDir } :${ process . env . PATH || "" } ` ;
@@ -245,14 +249,6 @@ export function registerAgentIpc(
245249 const mcpOverrides = { } ;
246250 const claudeCliPath = getClaudeCliPath ( ) ;
247251
248- console . log ( "[agent] Query overrides:" , {
249- model,
250- pathToClaudeCodeExecutable : claudeCliPath ,
251- hasAbortController : ! ! abortController ,
252- hasStderr : ! ! forwardClaudeStderr ,
253- hasEnv : ! ! envOverrides ,
254- } ) ;
255-
256252 await agent . runTask ( posthogTaskId , taskRunId , {
257253 repositoryPath : repoPath ,
258254 permissionMode : resolvedPermission ,
@@ -277,7 +273,7 @@ export function registerAgentIpc(
277273
278274 emitToRenderer ( { type : "done" , success : true , ts : Date . now ( ) } ) ;
279275 } catch ( err ) {
280- console . error ( "[agent] task execution failed" , err ) ;
276+ log . error ( "Task execution failed" , err ) ;
281277 let errorMessage = err instanceof Error ? err . message : String ( err ) ;
282278 const cause =
283279 err instanceof Error && "cause" in err && err . cause
0 commit comments