Skip to content

Commit 26499bc

Browse files
committed
feat(telemetry): emit session_end for exec and surface on cli_start
1 parent 83c2aed commit 26499bc

5 files changed

Lines changed: 458 additions & 4 deletions

File tree

‎src/exec/runner.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,33 @@ export interface ExecResult {
371371
model?: string;
372372
}
373373

374+
/**
375+
* Builds the `session_end` payload for exec runs, mirroring the TUI exit
376+
* reporter's status/exit_reason contract. Falls back to the live turn count
377+
* and wall clock when the run never produced a result.
378+
*/
379+
export function execSessionEndProperties(
380+
result: ExecResult | undefined,
381+
startedAt: number,
382+
sinkTurns: number,
383+
): {
384+
status: "done" | "failed" | "cancelled";
385+
turn_count: number;
386+
duration_ms: number;
387+
session_mode: "exec";
388+
exit_reason: "done" | "error" | "cancelled";
389+
} {
390+
const status = result?.status ?? "failed";
391+
return {
392+
status,
393+
turn_count: result?.turnsUsed ?? sinkTurns,
394+
duration_ms: result?.durationMs ?? Date.now() - startedAt,
395+
session_mode: "exec",
396+
exit_reason:
397+
status === "done" ? "done" : status === "failed" ? "error" : "cancelled",
398+
};
399+
}
400+
374401
export function createExecToolCallGate(
375402
isAdvertised: (name: string) => boolean,
376403
): (name: string) => boolean {
@@ -406,7 +433,7 @@ export async function runExec(config: Config): Promise<ExecResult> {
406433
const task = config.task.trim();
407434
if (task.length === 0) {
408435
stderr.write('Usage: corbits exec "<prompt>"\n');
409-
return {
436+
const result: ExecResult = {
410437
exitCode: 2,
411438
sessionId: config.sessionId,
412439
text: "",
@@ -425,6 +452,11 @@ export async function runExec(config: Config): Promise<ExecResult> {
425452
provider: config.providerName,
426453
model: config.model,
427454
};
455+
liveTelemetry.capture(
456+
"session_end",
457+
execSessionEndProperties(result, Date.now(), 0),
458+
);
459+
return result;
428460
}
429461

430462
const sessionId =
@@ -1307,6 +1339,14 @@ export async function runExec(config: Config): Promise<ExecResult> {
13071339
result.error = `runtime dispose failed: ${message}`;
13081340
}
13091341
}
1342+
liveTelemetry.capture(
1343+
"session_end",
1344+
execSessionEndProperties(
1345+
result,
1346+
startedAt,
1347+
runSink?.getTurnCount() ?? turnsUsed,
1348+
),
1349+
);
13101350
clearActiveDisposeHost();
13111351
}
13121352
}

‎src/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ export async function mainWithRunners(
7979
if (settings?.telemetry?.noticeShown === true) {
8080
const telemetry = createTelemetry({ settings });
8181
setTelemetry(telemetry);
82-
telemetry.capture("cli_start");
82+
telemetry.capture("cli_start", {
83+
surface: config.command === "exec" ? "exec" : "tui",
84+
});
8385
}
8486
}
8587

‎src/telemetry/first-run.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export async function activateHeldTelemetry(
6060
globalSettingsPath: string,
6161
confirmIntent: () => boolean = () => true,
6262
deps: FirstRunDeps = defaultDeps,
63+
surface: "tui" | "exec" = "tui",
6364
): Promise<void> {
6465
try {
6566
await deps.markTelemetryNoticeShown(globalSettingsPath);
@@ -89,5 +90,5 @@ export async function activateHeldTelemetry(
8990
if (!confirmIntent()) return;
9091
const telemetry = deps.createTelemetry({ settings });
9192
deps.setTelemetry(telemetry);
92-
telemetry.capture("cli_start");
93+
telemetry.capture("cli_start", { surface });
9394
}

‎src/telemetry/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function getSessionId(): string {
106106
// capture() appends ($app_version, service_version, os_type, os_arch,
107107
// schema_version, session_id), this bounds everything telemetry can ever contain.
108108
const EVENT_PROPERTY_ALLOWLIST: Record<TelemetryEvent, readonly string[]> = {
109-
cli_start: [],
109+
cli_start: ["surface"],
110110
session_end: [
111111
"status",
112112
"turn_count",

0 commit comments

Comments
 (0)