@posthog/agent 2.3.398 → 2.3.401

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.
@@ -8729,7 +8729,7 @@ var import_zod3 = require("zod");
8729
8729
  // package.json
8730
8730
  var package_default = {
8731
8731
  name: "@posthog/agent",
8732
- version: "2.3.398",
8732
+ version: "2.3.401",
8733
8733
  repository: "https://github.com/PostHog/code",
8734
8734
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
8735
8735
  exports: {
@@ -19283,6 +19283,7 @@ var HandoffCheckpointTracker = class {
19283
19283
  } finally {
19284
19284
  await this.removeIfPresent(packPath);
19285
19285
  await this.removeIfPresent(indexPath);
19286
+ await this.removeTmpDirIfEmpty(tmpDir);
19286
19287
  }
19287
19288
  }
19288
19289
  toGitCheckpoint(checkpoint) {
@@ -19441,6 +19442,14 @@ var HandoffCheckpointTracker = class {
19441
19442
  await (0, import_promises3.rm)(filePath, { force: true }).catch(() => {
19442
19443
  });
19443
19444
  }
19445
+ async removeTmpDirIfEmpty(tmpDir) {
19446
+ const entries = await (0, import_promises3.readdir)(tmpDir).catch(() => null);
19447
+ if (!entries || entries.length > 0) {
19448
+ return;
19449
+ }
19450
+ await (0, import_promises3.rmdir)(tmpDir).catch(() => {
19451
+ });
19452
+ }
19444
19453
  };
19445
19454
 
19446
19455
  // src/utils/gateway.ts
@@ -20605,62 +20614,77 @@ var ApplySnapshotSaga = class extends Saga {
20605
20614
  throw new Error("Cannot apply snapshot: no archive URL");
20606
20615
  }
20607
20616
  const archiveUrl = snapshot.archiveUrl;
20608
- await this.step({
20609
- name: "create_tmp_dir",
20610
- execute: () => (0, import_promises5.mkdir)(tmpDir, { recursive: true }),
20611
- rollback: async () => {
20612
- }
20613
- });
20614
- const archivePath = (0, import_node_path8.join)(tmpDir, `${snapshot.treeHash}.tar.gz`);
20615
- this.archivePath = archivePath;
20616
- await this.step({
20617
- name: "download_archive",
20618
- execute: async () => {
20619
- const arrayBuffer = await apiClient.downloadArtifact(
20620
- taskId,
20621
- runId,
20622
- archiveUrl
20623
- );
20624
- if (!arrayBuffer) {
20625
- throw new Error("Failed to download archive");
20617
+ try {
20618
+ await this.step({
20619
+ name: "create_tmp_dir",
20620
+ execute: () => (0, import_promises5.mkdir)(tmpDir, { recursive: true }),
20621
+ rollback: async () => {
20626
20622
  }
20627
- const base64Content = Buffer.from(arrayBuffer).toString("utf-8");
20628
- const binaryContent = Buffer.from(base64Content, "base64");
20629
- await (0, import_promises5.writeFile)(archivePath, binaryContent);
20630
- this.log.info("Tree archive downloaded", {
20631
- treeHash: snapshot.treeHash,
20632
- snapshotBytes: binaryContent.byteLength,
20633
- snapshotWireBytes: arrayBuffer.byteLength,
20634
- totalBytes: binaryContent.byteLength,
20635
- totalWireBytes: arrayBuffer.byteLength
20636
- });
20637
- },
20638
- rollback: async () => {
20639
- if (this.archivePath) {
20640
- await (0, import_promises5.rm)(this.archivePath, { force: true }).catch(() => {
20623
+ });
20624
+ const archivePath = (0, import_node_path8.join)(tmpDir, `${snapshot.treeHash}.tar.gz`);
20625
+ this.archivePath = archivePath;
20626
+ await this.step({
20627
+ name: "download_archive",
20628
+ execute: async () => {
20629
+ const arrayBuffer = await apiClient.downloadArtifact(
20630
+ taskId,
20631
+ runId,
20632
+ archiveUrl
20633
+ );
20634
+ if (!arrayBuffer) {
20635
+ throw new Error("Failed to download archive");
20636
+ }
20637
+ const base64Content = Buffer.from(arrayBuffer).toString("utf-8");
20638
+ const binaryContent = Buffer.from(base64Content, "base64");
20639
+ await (0, import_promises5.writeFile)(archivePath, binaryContent);
20640
+ this.log.info("Tree archive downloaded", {
20641
+ treeHash: snapshot.treeHash,
20642
+ snapshotBytes: binaryContent.byteLength,
20643
+ snapshotWireBytes: arrayBuffer.byteLength,
20644
+ totalBytes: binaryContent.byteLength,
20645
+ totalWireBytes: arrayBuffer.byteLength
20641
20646
  });
20647
+ },
20648
+ rollback: async () => {
20649
+ if (this.archivePath) {
20650
+ await (0, import_promises5.rm)(this.archivePath, { force: true }).catch(() => {
20651
+ });
20652
+ }
20642
20653
  }
20654
+ });
20655
+ const gitApplySaga = new ApplyTreeSaga(this.log);
20656
+ const applyResult = await gitApplySaga.run({
20657
+ baseDir: repositoryPath,
20658
+ treeHash: snapshot.treeHash,
20659
+ baseCommit: snapshot.baseCommit,
20660
+ changes: snapshot.changes,
20661
+ archivePath: this.archivePath
20662
+ });
20663
+ if (!applyResult.success) {
20664
+ throw new Error(`Failed to apply tree: ${applyResult.error}`);
20643
20665
  }
20644
- });
20645
- const gitApplySaga = new ApplyTreeSaga(this.log);
20646
- const applyResult = await gitApplySaga.run({
20647
- baseDir: repositoryPath,
20648
- treeHash: snapshot.treeHash,
20649
- baseCommit: snapshot.baseCommit,
20650
- changes: snapshot.changes,
20651
- archivePath: this.archivePath
20652
- });
20653
- if (!applyResult.success) {
20654
- throw new Error(`Failed to apply tree: ${applyResult.error}`);
20666
+ this.log.info("Tree snapshot applied", {
20667
+ treeHash: snapshot.treeHash,
20668
+ totalChanges: snapshot.changes.length,
20669
+ deletedFiles: snapshot.changes.filter((c) => c.status === "D").length
20670
+ });
20671
+ return { treeHash: snapshot.treeHash };
20672
+ } finally {
20673
+ if (this.archivePath) {
20674
+ await (0, import_promises5.rm)(this.archivePath, { force: true }).catch(() => {
20675
+ });
20676
+ }
20677
+ await this.removeTmpDirIfEmpty(tmpDir);
20678
+ this.archivePath = null;
20655
20679
  }
20656
- await (0, import_promises5.rm)(this.archivePath, { force: true }).catch(() => {
20657
- });
20658
- this.log.info("Tree snapshot applied", {
20659
- treeHash: snapshot.treeHash,
20660
- totalChanges: snapshot.changes.length,
20661
- deletedFiles: snapshot.changes.filter((c) => c.status === "D").length
20680
+ }
20681
+ async removeTmpDirIfEmpty(tmpDir) {
20682
+ const entries = await (0, import_promises5.readdir)(tmpDir).catch(() => null);
20683
+ if (!entries || entries.length > 0) {
20684
+ return;
20685
+ }
20686
+ await (0, import_promises5.rmdir)(tmpDir).catch(() => {
20662
20687
  });
20663
- return { treeHash: snapshot.treeHash };
20664
20688
  }
20665
20689
  };
20666
20690
 
@@ -20687,54 +20711,62 @@ var CaptureTreeSaga2 = class extends Saga {
20687
20711
  }
20688
20712
  const shouldArchive = !!apiClient;
20689
20713
  const archivePath = shouldArchive ? (0, import_node_path9.join)(tmpDir, `tree-${Date.now()}.tar.gz`) : void 0;
20690
- const gitCaptureSaga = new CaptureTreeSaga(this.log);
20691
- const captureResult = await gitCaptureSaga.run({
20692
- baseDir: repositoryPath,
20693
- lastTreeHash,
20694
- archivePath
20695
- });
20696
- if (!captureResult.success) {
20697
- throw new Error(`Failed to capture tree: ${captureResult.error}`);
20698
- }
20699
- const {
20700
- snapshot: gitSnapshot,
20701
- archivePath: createdArchivePath,
20702
- changed
20703
- } = captureResult.data;
20704
- if (!changed || !gitSnapshot) {
20705
- this.log.debug("No changes since last capture", { lastTreeHash });
20706
- return { snapshot: null, newTreeHash: lastTreeHash };
20707
- }
20708
- let archiveUrl;
20709
- if (apiClient && createdArchivePath) {
20710
- try {
20711
- archiveUrl = await this.uploadArchive(
20712
- createdArchivePath,
20713
- gitSnapshot.treeHash,
20714
- apiClient,
20715
- taskId,
20716
- runId
20717
- );
20718
- } finally {
20719
- await (0, import_promises6.rm)(createdArchivePath, { force: true }).catch(() => {
20714
+ try {
20715
+ const gitCaptureSaga = new CaptureTreeSaga(this.log);
20716
+ const captureResult = await gitCaptureSaga.run({
20717
+ baseDir: repositoryPath,
20718
+ lastTreeHash,
20719
+ archivePath
20720
+ });
20721
+ if (!captureResult.success) {
20722
+ throw new Error(`Failed to capture tree: ${captureResult.error}`);
20723
+ }
20724
+ const {
20725
+ snapshot: gitSnapshot,
20726
+ archivePath: createdArchivePath,
20727
+ changed
20728
+ } = captureResult.data;
20729
+ if (!changed || !gitSnapshot) {
20730
+ this.log.debug("No changes since last capture", { lastTreeHash });
20731
+ return { snapshot: null, newTreeHash: lastTreeHash };
20732
+ }
20733
+ let archiveUrl;
20734
+ if (apiClient && createdArchivePath) {
20735
+ try {
20736
+ archiveUrl = await this.uploadArchive(
20737
+ createdArchivePath,
20738
+ gitSnapshot.treeHash,
20739
+ apiClient,
20740
+ taskId,
20741
+ runId
20742
+ );
20743
+ } finally {
20744
+ await (0, import_promises6.rm)(createdArchivePath, { force: true }).catch(() => {
20745
+ });
20746
+ }
20747
+ }
20748
+ const snapshot = {
20749
+ treeHash: gitSnapshot.treeHash,
20750
+ baseCommit: gitSnapshot.baseCommit,
20751
+ changes: gitSnapshot.changes,
20752
+ timestamp: gitSnapshot.timestamp,
20753
+ interrupted,
20754
+ archiveUrl
20755
+ };
20756
+ this.log.info("Tree captured", {
20757
+ treeHash: snapshot.treeHash,
20758
+ changes: snapshot.changes.length,
20759
+ interrupted,
20760
+ archiveUrl
20761
+ });
20762
+ return { snapshot, newTreeHash: snapshot.treeHash };
20763
+ } finally {
20764
+ if (archivePath) {
20765
+ await (0, import_promises6.rm)(archivePath, { force: true }).catch(() => {
20720
20766
  });
20721
20767
  }
20768
+ await this.removeTmpDirIfEmpty(tmpDir);
20722
20769
  }
20723
- const snapshot = {
20724
- treeHash: gitSnapshot.treeHash,
20725
- baseCommit: gitSnapshot.baseCommit,
20726
- changes: gitSnapshot.changes,
20727
- timestamp: gitSnapshot.timestamp,
20728
- interrupted,
20729
- archiveUrl
20730
- };
20731
- this.log.info("Tree captured", {
20732
- treeHash: snapshot.treeHash,
20733
- changes: snapshot.changes.length,
20734
- interrupted,
20735
- archiveUrl
20736
- });
20737
- return { snapshot, newTreeHash: snapshot.treeHash };
20738
20770
  }
20739
20771
  async uploadArchive(archivePath, treeHash, apiClient, taskId, runId) {
20740
20772
  const archiveUrl = await this.step({
@@ -20773,6 +20805,14 @@ var CaptureTreeSaga2 = class extends Saga {
20773
20805
  });
20774
20806
  return archiveUrl;
20775
20807
  }
20808
+ async removeTmpDirIfEmpty(tmpDir) {
20809
+ const entries = await (0, import_promises6.readdir)(tmpDir).catch(() => null);
20810
+ if (!entries || entries.length > 0) {
20811
+ return;
20812
+ }
20813
+ await (0, import_promises6.rmdir)(tmpDir).catch(() => {
20814
+ });
20815
+ }
20776
20816
  };
20777
20817
 
20778
20818
  // src/tree-tracker.ts