@posthog/agent 2.1.148 → 2.1.150

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/agent.js CHANGED
@@ -279,7 +279,7 @@ import { v7 as uuidv7 } from "uuid";
279
279
  // package.json
280
280
  var package_default = {
281
281
  name: "@posthog/agent",
282
- version: "2.1.148",
282
+ version: "2.1.150",
283
283
  repository: "https://github.com/PostHog/twig",
284
284
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
285
285
  exports: {
@@ -2555,7 +2555,7 @@ function getAbortController(userProvidedController) {
2555
2555
  }
2556
2556
  return controller;
2557
2557
  }
2558
- function buildSpawnWrapper(sessionId, onProcessSpawned, onProcessExited) {
2558
+ function buildSpawnWrapper(sessionId, onProcessSpawned, onProcessExited, logger) {
2559
2559
  return (spawnOpts) => {
2560
2560
  const child = spawn(spawnOpts.command, spawnOpts.args, {
2561
2561
  cwd: spawnOpts.cwd,
@@ -2569,6 +2569,12 @@ function buildSpawnWrapper(sessionId, onProcessSpawned, onProcessExited) {
2569
2569
  sessionId
2570
2570
  });
2571
2571
  }
2572
+ child.stderr?.on("data", (data) => {
2573
+ const msg = data.toString().trim();
2574
+ if (msg && logger) {
2575
+ logger.debug(`[claude-code:${child.pid}] stderr: ${msg}`);
2576
+ }
2577
+ });
2572
2578
  if (onProcessExited) {
2573
2579
  child.on("exit", () => {
2574
2580
  if (child.pid) {
@@ -2660,7 +2666,8 @@ function buildSessionOptions(params) {
2660
2666
  spawnClaudeCodeProcess: buildSpawnWrapper(
2661
2667
  params.sessionId,
2662
2668
  params.onProcessSpawned,
2663
- params.onProcessExited
2669
+ params.onProcessExited,
2670
+ params.logger
2664
2671
  )
2665
2672
  }
2666
2673
  };
@@ -3663,7 +3670,7 @@ function spawnCodexProcess(options) {
3663
3670
  detached: process.platform !== "win32"
3664
3671
  });
3665
3672
  child.stderr?.on("data", (data) => {
3666
- logger.debug("codex-acp stderr:", data.toString());
3673
+ logger.warn("codex-acp stderr:", data.toString());
3667
3674
  });
3668
3675
  child.on("error", (err) => {
3669
3676
  logger.error("codex-acp process error:", err);
@@ -4226,12 +4233,14 @@ var PostHogAPIClient = class {
4226
4233
 
4227
4234
  // src/session-log-writer.ts
4228
4235
  import fs4 from "fs";
4236
+ import fsp from "fs/promises";
4229
4237
  import path5 from "path";
4230
4238
  var SessionLogWriter = class _SessionLogWriter {
4231
4239
  static FLUSH_DEBOUNCE_MS = 500;
4232
4240
  static FLUSH_MAX_INTERVAL_MS = 5e3;
4233
4241
  static MAX_FLUSH_RETRIES = 10;
4234
4242
  static MAX_RETRY_DELAY_MS = 3e4;
4243
+ static SESSIONS_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1e3;
4235
4244
  posthogAPI;
4236
4245
  pendingEntries = /* @__PURE__ */ new Map();
4237
4246
  flushTimeouts = /* @__PURE__ */ new Map();
@@ -4368,10 +4377,16 @@ var SessionLogWriter = class _SessionLogWriter {
4368
4377
  );
4369
4378
  this.retryCounts.set(sessionId, 0);
4370
4379
  } else {
4371
- this.logger.error(
4372
- `Failed to persist session logs (attempt ${retryCount}/${_SessionLogWriter.MAX_FLUSH_RETRIES}):`,
4373
- error
4374
- );
4380
+ if (retryCount === 1) {
4381
+ this.logger.warn(
4382
+ `Failed to persist session logs, will retry (up to ${_SessionLogWriter.MAX_FLUSH_RETRIES} attempts)`,
4383
+ {
4384
+ taskId: session.context.taskId,
4385
+ runId: session.context.runId,
4386
+ error: error instanceof Error ? error.message : String(error)
4387
+ }
4388
+ );
4389
+ }
4375
4390
  const currentPending = this.pendingEntries.get(sessionId) ?? [];
4376
4391
  this.pendingEntries.set(sessionId, [...pending, ...currentPending]);
4377
4392
  this.scheduleFlush(sessionId);
@@ -4485,6 +4500,27 @@ var SessionLogWriter = class _SessionLogWriter {
4485
4500
  });
4486
4501
  }
4487
4502
  }
4503
+ static async cleanupOldSessions(localCachePath) {
4504
+ const sessionsDir = path5.join(localCachePath, "sessions");
4505
+ let deleted = 0;
4506
+ try {
4507
+ const entries = await fsp.readdir(sessionsDir);
4508
+ const now = Date.now();
4509
+ for (const entry of entries) {
4510
+ const entryPath = path5.join(sessionsDir, entry);
4511
+ try {
4512
+ const stats = await fsp.stat(entryPath);
4513
+ if (stats.isDirectory() && now - stats.birthtimeMs > _SessionLogWriter.SESSIONS_MAX_AGE_MS) {
4514
+ await fsp.rm(entryPath, { recursive: true, force: true });
4515
+ deleted++;
4516
+ }
4517
+ } catch {
4518
+ }
4519
+ }
4520
+ } catch {
4521
+ }
4522
+ return deleted;
4523
+ }
4488
4524
  };
4489
4525
 
4490
4526
  // src/agent.ts
@@ -4509,6 +4545,12 @@ var Agent = class {
4509
4545
  logger: this.logger.child("SessionLogWriter"),
4510
4546
  localCachePath: config.localCachePath
4511
4547
  });
4548
+ if (config.localCachePath) {
4549
+ SessionLogWriter.cleanupOldSessions(config.localCachePath).catch(
4550
+ () => {
4551
+ }
4552
+ );
4553
+ }
4512
4554
  }
4513
4555
  }
4514
4556
  _configureLlmGateway(_adapter) {
@@ -4530,7 +4572,9 @@ var Agent = class {
4530
4572
  }
4531
4573
  async run(taskId, taskRunId, options = {}) {
4532
4574
  const gatewayConfig = this._configureLlmGateway(options.adapter);
4533
- this.logger.info("Configured LLM gateway", options);
4575
+ this.logger.info("Configured LLM gateway", {
4576
+ adapter: options.adapter
4577
+ });
4534
4578
  this.taskRunId = taskRunId;
4535
4579
  let allowedModelIds;
4536
4580
  let sanitizedModel = options.model && !BLOCKED_MODELS.has(options.model) ? options.model : void 0;