@@ -11,6 +11,8 @@ import type {
1111import { serializeError } from "@posthog/shared" ;
1212import { Hono } from "hono" ;
1313import { z } from "zod/v4" ;
14+ import { POSTHOG_NOTIFICATIONS } from "../acp-extensions" ;
15+ import { OtelRunTelemetry } from "../otel-telemetry" ;
1416import { createPiRpcClient , type PiRpcClient } from "../pi/rpc-client" ;
1517import { piRpcCommandSchema , type RpcCommand } from "../pi/rpc-transport" ;
1618import { PiRuntime } from "../pi/runtime" ;
@@ -132,6 +134,37 @@ export class PiAgentServer {
132134 this . app = this . createApp ( ) ;
133135 }
134136
137+ private createRunTelemetry (
138+ payload : JwtPayload ,
139+ ) : OtelRunTelemetry | undefined {
140+ const { otelLogsUrl, otelLogsToken } = this . config ;
141+ if ( ! otelLogsUrl || ! otelLogsToken ) return undefined ;
142+ try {
143+ return new OtelRunTelemetry (
144+ {
145+ url : otelLogsUrl ,
146+ token : otelLogsToken ,
147+ tracesUrl : this . config . otelTracesUrl ,
148+ } ,
149+ {
150+ taskId : payload . task_id ,
151+ runId : payload . run_id ,
152+ deviceType : "cloud" ,
153+ teamId : payload . team_id ,
154+ userId : payload . user_id ,
155+ distinctId : payload . distinct_id ,
156+ adapter : "pi" ,
157+ mode : payload . mode ?? this . config . mode ,
158+ agentVersion : this . config . version ,
159+ } ,
160+ new Logger ( { debug : false , prefix : "[OtelRunTelemetry]" } ) ,
161+ ) ;
162+ } catch ( error ) {
163+ this . logger . warn ( "Failed to initialize OTel run telemetry" , error ) ;
164+ return undefined ;
165+ }
166+ }
167+
135168 async start ( ) : Promise < void > {
136169 await new Promise < void > ( ( resolve ) => {
137170 this . server = serve (
@@ -388,6 +421,29 @@ export class PiAgentServer {
388421 try {
389422 await initializationPromise ;
390423 } catch ( error ) {
424+ const telemetry = this . createRunTelemetry ( payload ) ;
425+ telemetry ?. append ( payload . run_id , {
426+ type : "notification" ,
427+ timestamp : new Date ( ) . toISOString ( ) ,
428+ notification : {
429+ jsonrpc : "2.0" ,
430+ method : POSTHOG_NOTIFICATIONS . INITIALIZATION_FAILED ,
431+ params : {
432+ runtimeAdapter : "pi" ,
433+ initializationPhase : "session_setup" ,
434+ initMs : Date . now ( ) - initStartedAt ,
435+ requestedModel : this . config . model ,
436+ gatewayConfigured : Boolean (
437+ process . env . LLM_GATEWAY_URL || this . config . apiUrl ,
438+ ) ,
439+ errorType :
440+ error instanceof Error && error . name === "TimeoutError"
441+ ? "timeout"
442+ : "error" ,
443+ } ,
444+ } ,
445+ } ) ;
446+ await telemetry ?. shutdown ( ) ;
391447 this . logger . error ( "Pi session initialization failed" , {
392448 runtimeAdapter : "pi" ,
393449 initializationPhase : "session_setup" ,
0 commit comments