@posthog/agent 2.1.115 → 2.1.118

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
@@ -276,7 +276,7 @@ import { v7 as uuidv7 } from "uuid";
276
276
  // package.json
277
277
  var package_default = {
278
278
  name: "@posthog/agent",
279
- version: "2.1.115",
279
+ version: "2.1.118",
280
280
  repository: "https://github.com/PostHog/twig",
281
281
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
282
282
  exports: {
@@ -2490,12 +2490,10 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
2490
2490
  toolUseCache;
2491
2491
  backgroundTerminals = {};
2492
2492
  clientCapabilities;
2493
- logWriter;
2494
2493
  options;
2495
2494
  lastSentConfigOptions;
2496
- constructor(client, logWriter, options) {
2495
+ constructor(client, options) {
2497
2496
  super(client);
2498
- this.logWriter = logWriter;
2499
2497
  this.options = options;
2500
2498
  this.toolUseCache = {};
2501
2499
  this.logger = new Logger({ debug: true, prefix: "[ClaudeAcpAgent]" });
@@ -2540,7 +2538,14 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
2540
2538
  async newSession(params) {
2541
2539
  this.checkAuthStatus();
2542
2540
  const meta = params._meta;
2541
+ const taskId = meta?.persistence?.taskId;
2543
2542
  const sessionId = uuidv7();
2543
+ this.logger.info("Creating new session", {
2544
+ sessionId,
2545
+ taskId,
2546
+ taskRunId: meta?.taskRunId,
2547
+ cwd: params.cwd
2548
+ });
2544
2549
  const permissionMode = meta?.permissionMode && TWIG_EXECUTION_MODES.includes(meta.permissionMode) ? meta.permissionMode : "default";
2545
2550
  const mcpServers = parseMcpServers(params);
2546
2551
  const options = buildSessionOptions({
@@ -2569,7 +2574,6 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
2569
2574
  options.abortController
2570
2575
  );
2571
2576
  session.taskRunId = meta?.taskRunId;
2572
- this.registerPersistence(sessionId, meta);
2573
2577
  if (meta?.taskRunId) {
2574
2578
  await this.client.extNotification("_posthog/sdk_session", {
2575
2579
  taskRunId: meta.taskRunId,
@@ -2595,6 +2599,7 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
2595
2599
  }
2596
2600
  async resumeSession(params) {
2597
2601
  const meta = params._meta;
2602
+ const taskId = meta?.persistence?.taskId;
2598
2603
  const sessionId = meta?.sessionId;
2599
2604
  if (!sessionId) {
2600
2605
  throw new Error("Cannot resume session without sessionId");
@@ -2602,6 +2607,12 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
2602
2607
  if (this.sessionId === sessionId) {
2603
2608
  return {};
2604
2609
  }
2610
+ this.logger.info("Resuming session", {
2611
+ sessionId,
2612
+ taskId,
2613
+ taskRunId: meta?.taskRunId,
2614
+ cwd: params.cwd
2615
+ });
2605
2616
  const mcpServers = parseMcpServers(params);
2606
2617
  const permissionMode = meta?.permissionMode && TWIG_EXECUTION_MODES.includes(meta.permissionMode) ? meta.permissionMode : "default";
2607
2618
  const { query: q, session } = await this.initializeQuery({
@@ -2614,15 +2625,36 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
2614
2625
  isResume: true,
2615
2626
  additionalDirectories: meta?.claudeCode?.options?.additionalDirectories
2616
2627
  });
2628
+ this.logger.info("Session query initialized, awaiting resumption", {
2629
+ sessionId,
2630
+ taskId,
2631
+ taskRunId: meta?.taskRunId
2632
+ });
2617
2633
  session.taskRunId = meta?.taskRunId;
2618
- this.registerPersistence(sessionId, meta);
2619
- const validation = await withTimeout(
2620
- q.initializationResult(),
2621
- SESSION_VALIDATION_TIMEOUT_MS
2622
- );
2623
- if (validation.result === "timeout") {
2624
- throw new Error("Session validation timed out");
2634
+ try {
2635
+ const result = await withTimeout(
2636
+ q.initializationResult(),
2637
+ SESSION_VALIDATION_TIMEOUT_MS
2638
+ );
2639
+ if (result.result === "timeout") {
2640
+ throw new Error(
2641
+ `Session resumption timed out for sessionId=${sessionId}`
2642
+ );
2643
+ }
2644
+ } catch (err) {
2645
+ this.logger.error("Session resumption failed", {
2646
+ sessionId,
2647
+ taskId,
2648
+ taskRunId: meta?.taskRunId,
2649
+ error: err instanceof Error ? err.message : String(err)
2650
+ });
2651
+ throw err;
2625
2652
  }
2653
+ this.logger.info("Session resumed successfully", {
2654
+ sessionId,
2655
+ taskId,
2656
+ taskRunId: meta?.taskRunId
2657
+ });
2626
2658
  this.deferBackgroundFetches(q, sessionId);
2627
2659
  const configOptions = await this.buildConfigOptions();
2628
2660
  return { configOptions };
@@ -2821,12 +2853,6 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
2821
2853
  this.logger.warn("Failed to fetch deferred session data", { err });
2822
2854
  });
2823
2855
  }
2824
- registerPersistence(sessionId, meta) {
2825
- const persistence = meta?.persistence;
2826
- if (persistence && this.logWriter) {
2827
- this.logWriter.register(sessionId, persistence);
2828
- }
2829
- }
2830
2856
  sendAvailableCommandsUpdate(sessionId, availableCommands) {
2831
2857
  setTimeout(() => {
2832
2858
  this.client.sessionUpdate({
@@ -3114,7 +3140,7 @@ function createClaudeConnection(config) {
3114
3140
  const agentStream = ndJsonStream(agentWritable, streams.agent.readable);
3115
3141
  let agent = null;
3116
3142
  const agentConnection = new AgentSideConnection((client) => {
3117
- agent = new ClaudeAcpAgent(client, logWriter, config.processCallbacks);
3143
+ agent = new ClaudeAcpAgent(client, config.processCallbacks);
3118
3144
  logger.info(`Created ${agent.adapterName} agent`);
3119
3145
  return agent;
3120
3146
  }, agentStream);
@@ -3554,11 +3580,15 @@ var SessionLogWriter = class _SessionLogWriter {
3554
3580
  }
3555
3581
  async flushAll() {
3556
3582
  const sessionIds = [...this.sessions.keys()];
3557
- const pendingCounts = sessionIds.map((id) => ({
3558
- id,
3559
- pending: this.pendingEntries.get(id)?.length ?? 0,
3560
- messages: this.messageCounts.get(id) ?? 0
3561
- }));
3583
+ const pendingCounts = sessionIds.map((id) => {
3584
+ const session = this.sessions.get(id);
3585
+ return {
3586
+ taskId: session?.context.taskId,
3587
+ runId: session?.context.runId,
3588
+ pending: this.pendingEntries.get(id)?.length ?? 0,
3589
+ messages: this.messageCounts.get(id) ?? 0
3590
+ };
3591
+ });
3562
3592
  this.logger.info("flushAll called", {
3563
3593
  sessions: sessionIds.length,
3564
3594
  pending: pendingCounts
@@ -3574,8 +3604,8 @@ var SessionLogWriter = class _SessionLogWriter {
3574
3604
  return;
3575
3605
  }
3576
3606
  this.logger.info("Session registered", {
3577
- sessionId,
3578
- taskId: context.taskId
3607
+ taskId: context.taskId,
3608
+ runId: context.runId
3579
3609
  });
3580
3610
  this.sessions.set(sessionId, { context });
3581
3611
  this.lastFlushAttemptTime.set(sessionId, Date.now());
@@ -3609,7 +3639,11 @@ var SessionLogWriter = class _SessionLogWriter {
3609
3639
  const count = (this.messageCounts.get(sessionId) ?? 0) + 1;
3610
3640
  this.messageCounts.set(sessionId, count);
3611
3641
  if (count % 10 === 1) {
3612
- this.logger.info("Messages received", { count, sessionId });
3642
+ this.logger.info("Messages received", {
3643
+ count,
3644
+ taskId: session.context.taskId,
3645
+ runId: session.context.runId
3646
+ });
3613
3647
  }
3614
3648
  try {
3615
3649
  const message = JSON.parse(line);
@@ -3640,7 +3674,8 @@ var SessionLogWriter = class _SessionLogWriter {
3640
3674
  }
3641
3675
  } catch {
3642
3676
  this.logger.warn("Failed to parse raw line for persistence", {
3643
- sessionId,
3677
+ taskId: session.context.taskId,
3678
+ runId: session.context.runId,
3644
3679
  lineLength: line.length
3645
3680
  });
3646
3681
  }
@@ -3655,7 +3690,8 @@ var SessionLogWriter = class _SessionLogWriter {
3655
3690
  const pending = this.pendingEntries.get(sessionId);
3656
3691
  if (!this.posthogAPI || !pending?.length) {
3657
3692
  this.logger.info("flush: nothing to persist", {
3658
- sessionId,
3693
+ taskId: session.context.taskId,
3694
+ runId: session.context.runId,
3659
3695
  hasPosthogAPI: !!this.posthogAPI,
3660
3696
  pendingCount: pending?.length ?? 0
3661
3697
  });
@@ -3676,7 +3712,8 @@ var SessionLogWriter = class _SessionLogWriter {
3676
3712
  );
3677
3713
  this.retryCounts.set(sessionId, 0);
3678
3714
  this.logger.info("Flushed session logs", {
3679
- sessionId,
3715
+ taskId: session.context.taskId,
3716
+ runId: session.context.runId,
3680
3717
  entryCount: pending.length
3681
3718
  });
3682
3719
  } catch (error) {
@@ -3685,7 +3722,11 @@ var SessionLogWriter = class _SessionLogWriter {
3685
3722
  if (retryCount >= _SessionLogWriter.MAX_FLUSH_RETRIES) {
3686
3723
  this.logger.error(
3687
3724
  `Dropping ${pending.length} session log entries after ${retryCount} failed flush attempts`,
3688
- { sessionId, error }
3725
+ {
3726
+ taskId: session.context.taskId,
3727
+ runId: session.context.runId,
3728
+ error
3729
+ }
3689
3730
  );
3690
3731
  this.retryCounts.set(sessionId, 0);
3691
3732
  } else {
@@ -3774,7 +3815,12 @@ var SessionLogWriter = class _SessionLogWriter {
3774
3815
  fs3.appendFileSync(logPath, `${JSON.stringify(entry)}
3775
3816
  `);
3776
3817
  } catch (error) {
3777
- this.logger.warn("Failed to write to local cache", { logPath, error });
3818
+ this.logger.warn("Failed to write to local cache", {
3819
+ taskId: session.context.taskId,
3820
+ runId: session.context.runId,
3821
+ logPath,
3822
+ error
3823
+ });
3778
3824
  }
3779
3825
  }
3780
3826
  };