@posthog/agent 2.3.401 → 2.3.403

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.
Files changed (40) hide show
  1. package/README.md +11 -14
  2. package/dist/agent.js +1 -7
  3. package/dist/agent.js.map +1 -1
  4. package/dist/handoff-checkpoint.d.ts +0 -3
  5. package/dist/handoff-checkpoint.js +38 -69
  6. package/dist/handoff-checkpoint.js.map +1 -1
  7. package/dist/index.d.ts +0 -2
  8. package/dist/index.js +0 -2
  9. package/dist/index.js.map +1 -1
  10. package/dist/posthog-api.js +1 -5
  11. package/dist/posthog-api.js.map +1 -1
  12. package/dist/resume.d.ts +5 -6
  13. package/dist/resume.js +2 -41
  14. package/dist/resume.js.map +1 -1
  15. package/dist/server/agent-server.d.ts +1 -2
  16. package/dist/server/agent-server.js +103 -815
  17. package/dist/server/agent-server.js.map +1 -1
  18. package/dist/server/bin.cjs +101 -806
  19. package/dist/server/bin.cjs.map +1 -1
  20. package/dist/types.d.ts +2 -13
  21. package/dist/types.js.map +1 -1
  22. package/package.json +3 -7
  23. package/src/acp-extensions.ts +0 -3
  24. package/src/handoff-checkpoint.test.ts +2 -17
  25. package/src/handoff-checkpoint.ts +15 -61
  26. package/src/resume.ts +5 -11
  27. package/src/sagas/resume-saga.test.ts +27 -77
  28. package/src/sagas/resume-saga.ts +3 -44
  29. package/src/sagas/test-fixtures.ts +17 -76
  30. package/src/server/agent-server.ts +22 -103
  31. package/src/test/fixtures/api.ts +2 -15
  32. package/src/types.ts +0 -16
  33. package/dist/tree-tracker.d.ts +0 -68
  34. package/dist/tree-tracker.js +0 -6462
  35. package/dist/tree-tracker.js.map +0 -1
  36. package/src/sagas/apply-snapshot-saga.test.ts +0 -691
  37. package/src/sagas/apply-snapshot-saga.ts +0 -114
  38. package/src/sagas/capture-tree-saga.test.ts +0 -910
  39. package/src/sagas/capture-tree-saga.ts +0 -165
  40. package/src/tree-tracker.ts +0 -173
@@ -34,11 +34,8 @@ declare class HandoffCheckpointTracker {
34
34
  private createGitTracker;
35
35
  private logCaptureMetrics;
36
36
  private logApplyMetrics;
37
- private buildMetricPayload;
38
37
  private sumRawBytes;
39
- private sumWireBytes;
40
38
  private removeIfPresent;
41
- private removeTmpDirIfEmpty;
42
39
  }
43
40
 
44
41
  export { HandoffCheckpointTracker, type HandoffCheckpointTrackerConfig };
@@ -897,19 +897,14 @@ var require_dist2 = __commonJS({
897
897
  });
898
898
 
899
899
  // src/handoff-checkpoint.ts
900
- import {
901
- mkdir as mkdir3,
902
- readdir,
903
- readFile as readFile2,
904
- rm as rm3,
905
- rmdir,
906
- writeFile
907
- } from "fs/promises";
908
- import { join as join2 } from "path";
900
+ import { mkdtemp as mkdtemp2, readFile as readFile2, rm as rm3, writeFile } from "fs/promises";
901
+ import { tmpdir as tmpdir2 } from "os";
902
+ import { dirname, join as join2 } from "path";
909
903
 
910
904
  // ../git/dist/handoff.js
911
905
  import { spawn as spawn2 } from "child_process";
912
- import { copyFile, mkdir as mkdir2, readFile, rm as rm2, stat as stat2 } from "fs/promises";
906
+ import { copyFile, mkdtemp, readFile, rm as rm2, stat as stat2 } from "fs/promises";
907
+ import { tmpdir } from "os";
913
908
  import path3 from "path";
914
909
 
915
910
  // ../../node_modules/simple-git/dist/esm/index.js
@@ -6055,7 +6050,7 @@ var GitHandoffTracker = class {
6055
6050
  this.repositoryPath = config.repositoryPath;
6056
6051
  this.logger = config.logger;
6057
6052
  }
6058
- async captureForHandoff(localGitState) {
6053
+ async captureForHandoff(_localGitState) {
6059
6054
  const captureSaga = new CaptureCheckpointSaga(this.logger);
6060
6055
  const result = await captureSaga.run({ baseDir: this.repositoryPath });
6061
6056
  if (!result.success) {
@@ -6063,15 +6058,19 @@ var GitHandoffTracker = class {
6063
6058
  }
6064
6059
  const checkpoint = result.data;
6065
6060
  const git = createGitClient(this.repositoryPath);
6066
- const tempDir = await this.getTempDir(git);
6061
+ const tempDir = await this.createTempDir(checkpoint.checkpointId);
6067
6062
  const checkpointRef = `${CHECKPOINT_REF_PREFIX2}${checkpoint.checkpointId}`;
6068
- const shouldIncludeHead = !!checkpoint.head && checkpoint.head !== localGitState?.head;
6069
- const headRef = shouldIncludeHead ? `${HANDOFF_HEAD_REF_PREFIX}${checkpoint.checkpointId}` : void 0;
6063
+ const packRefs = [
6064
+ checkpoint.head,
6065
+ checkpoint.indexTree,
6066
+ checkpoint.worktreeTree
6067
+ ].filter((ref) => !!ref);
6068
+ const headRef = checkpoint.head ? `${HANDOFF_HEAD_REF_PREFIX}${checkpoint.checkpointId}` : void 0;
6070
6069
  const packPrefix = path3.join(tempDir, checkpoint.checkpointId);
6071
6070
  try {
6072
6071
  const [headPack, indexFile, tracking] = await Promise.all([
6073
- shouldIncludeHead && checkpoint.head ? this.captureHeadPack(packPrefix, checkpoint.head) : Promise.resolve(void 0),
6074
- this.copyIndexFile(git, checkpoint.checkpointId),
6072
+ this.captureObjectPack(packPrefix, packRefs),
6073
+ this.copyIndexFile(git, checkpoint.checkpointId, tempDir),
6075
6074
  getTrackingMetadata(git, checkpoint.branch)
6076
6075
  ]);
6077
6076
  return {
@@ -6118,6 +6117,8 @@ var GitHandoffTracker = class {
6118
6117
  } else if (checkpoint.head) {
6119
6118
  await git.checkout(checkpoint.head);
6120
6119
  }
6120
+ await git.clean(["f", "d"]);
6121
+ await git.raw(["read-tree", "--reset", "-u", checkpoint.worktreeTree]);
6121
6122
  if (indexPath) {
6122
6123
  await this.restoreIndexFile(git, indexPath);
6123
6124
  }
@@ -6129,8 +6130,8 @@ var GitHandoffTracker = class {
6129
6130
  totalBytes: packBytes + indexBytes
6130
6131
  };
6131
6132
  }
6132
- async captureHeadPack(packPrefix, headCommit) {
6133
- const hash = await this.runGitWithInput(["pack-objects", packPrefix, "--revs"], `${headCommit}
6133
+ async captureObjectPack(packPrefix, refs) {
6134
+ const hash = await this.runGitWithInput(["pack-objects", packPrefix, "--revs"], `${refs.join("\n")}
6134
6135
  `);
6135
6136
  const packPath = `${packPrefix}-${hash.trim()}.pack`;
6136
6137
  const rawBytes = await this.getFileSize(packPath);
@@ -6138,9 +6139,8 @@ var GitHandoffTracker = class {
6138
6139
  });
6139
6140
  return { path: packPath, rawBytes };
6140
6141
  }
6141
- async copyIndexFile(git, checkpointId) {
6142
+ async copyIndexFile(git, checkpointId, tempDir) {
6142
6143
  const indexPath = await this.getGitPath(git, "index");
6143
- const tempDir = await this.getTempDir(git);
6144
6144
  const copiedIndexPath = path3.join(tempDir, `${checkpointId}.index`);
6145
6145
  await copyFile(indexPath, copiedIndexPath);
6146
6146
  return {
@@ -6268,13 +6268,8 @@ var GitHandoffTracker = class {
6268
6268
  ]);
6269
6269
  return exitCode === 0;
6270
6270
  }
6271
- async getTempDir(git) {
6272
- const raw = await git.raw(["rev-parse", "--git-common-dir"]);
6273
- const commonDir = raw.trim() || ".git";
6274
- const resolved = path3.isAbsolute(commonDir) ? commonDir : path3.resolve(this.repositoryPath, commonDir);
6275
- const tempDir = path3.join(resolved, "posthog-code-tmp");
6276
- await mkdir2(tempDir, { recursive: true });
6277
- return tempDir;
6271
+ async createTempDir(checkpointId) {
6272
+ return mkdtemp(joinTempPrefix(checkpointId));
6278
6273
  }
6279
6274
  async getGitPath(git, gitPath) {
6280
6275
  const raw = await git.raw(["rev-parse", "--git-path", gitPath]);
@@ -6341,6 +6336,9 @@ var GitHandoffTracker = class {
6341
6336
  });
6342
6337
  }
6343
6338
  };
6339
+ function joinTempPrefix(checkpointId) {
6340
+ return path3.join(tmpdir(), `posthog-code-handoff-${checkpointId}-`);
6341
+ }
6344
6342
  async function getCurrentBranchName(git) {
6345
6343
  try {
6346
6344
  const raw = await git.revparse(["--abbrev-ref", "HEAD"]);
@@ -6480,8 +6478,11 @@ var HandoffCheckpointTracker = class {
6480
6478
  indexArtifactPath: uploads.index?.storagePath
6481
6479
  };
6482
6480
  } finally {
6481
+ const tempDir = capture.headPack?.path ? dirname(capture.headPack.path) : dirname(capture.indexFile.path);
6483
6482
  await this.removeIfPresent(capture.headPack?.path);
6484
6483
  await this.removeIfPresent(capture.indexFile.path);
6484
+ await rm3(tempDir, { recursive: true, force: true }).catch(() => {
6485
+ });
6485
6486
  }
6486
6487
  }
6487
6488
  async applyFromHandoff(checkpoint, options) {
@@ -6491,8 +6492,9 @@ var HandoffCheckpointTracker = class {
6491
6492
  );
6492
6493
  }
6493
6494
  const gitTracker = this.createGitTracker();
6494
- const tmpDir = join2(this.repositoryPath, ".posthog", "tmp");
6495
- await mkdir3(tmpDir, { recursive: true });
6495
+ const tmpDir = await mkdtemp2(
6496
+ join2(tmpdir2(), `posthog-code-handoff-${checkpoint.checkpointId}-`)
6497
+ );
6496
6498
  const packPath = join2(tmpDir, `${checkpoint.checkpointId}.pack`);
6497
6499
  const indexPath = join2(tmpDir, `${checkpoint.checkpointId}.index`);
6498
6500
  try {
@@ -6526,7 +6528,8 @@ var HandoffCheckpointTracker = class {
6526
6528
  } finally {
6527
6529
  await this.removeIfPresent(packPath);
6528
6530
  await this.removeIfPresent(indexPath);
6529
- await this.removeTmpDirIfEmpty(tmpDir);
6531
+ await rm3(tmpDir, { recursive: true, force: true }).catch(() => {
6532
+ });
6530
6533
  }
6531
6534
  }
6532
6535
  toGitCheckpoint(checkpoint) {
@@ -6634,50 +6637,24 @@ var HandoffCheckpointTracker = class {
6634
6637
  }
6635
6638
  logCaptureMetrics(checkpoint, uploads) {
6636
6639
  this.logger.info("Captured handoff checkpoint", {
6637
- checkpointId: checkpoint.checkpointId,
6638
6640
  branch: checkpoint.branch,
6639
- head: checkpoint.head,
6640
- artifactPath: uploads.pack?.storagePath,
6641
- indexArtifactPath: uploads.index?.storagePath,
6642
- ...this.buildMetricPayload(uploads)
6641
+ head: checkpoint.head?.slice(0, 7),
6642
+ totalBytes: this.sumRawBytes(uploads.pack, uploads.index)
6643
6643
  });
6644
6644
  }
6645
- logApplyMetrics(checkpoint, downloads, totalBytes) {
6645
+ logApplyMetrics(checkpoint, _downloads, totalBytes) {
6646
6646
  this.logger.info("Applied handoff checkpoint", {
6647
- checkpointId: checkpoint.checkpointId,
6648
- commit: checkpoint.commit,
6649
6647
  branch: checkpoint.branch,
6650
- head: checkpoint.head,
6651
- packBytes: downloads.pack?.rawBytes ?? 0,
6652
- packWireBytes: downloads.pack?.wireBytes ?? 0,
6653
- indexBytes: downloads.index?.rawBytes ?? 0,
6654
- indexWireBytes: downloads.index?.wireBytes ?? 0,
6655
- totalBytes,
6656
- totalWireBytes: this.sumWireBytes(downloads.pack, downloads.index)
6648
+ head: checkpoint.head?.slice(0, 7),
6649
+ totalBytes
6657
6650
  });
6658
6651
  }
6659
- buildMetricPayload(metrics) {
6660
- return {
6661
- packBytes: metrics.pack?.rawBytes ?? 0,
6662
- packWireBytes: metrics.pack?.wireBytes ?? 0,
6663
- indexBytes: metrics.index?.rawBytes ?? 0,
6664
- indexWireBytes: metrics.index?.wireBytes ?? 0,
6665
- totalBytes: this.sumRawBytes(metrics.pack, metrics.index),
6666
- totalWireBytes: this.sumWireBytes(metrics.pack, metrics.index)
6667
- };
6668
- }
6669
6652
  sumRawBytes(...artifacts) {
6670
6653
  return artifacts.reduce(
6671
6654
  (total, artifact) => total + (artifact?.rawBytes ?? 0),
6672
6655
  0
6673
6656
  );
6674
6657
  }
6675
- sumWireBytes(...artifacts) {
6676
- return artifacts.reduce(
6677
- (total, artifact) => total + (artifact?.wireBytes ?? 0),
6678
- 0
6679
- );
6680
- }
6681
6658
  async removeIfPresent(filePath) {
6682
6659
  if (!filePath) {
6683
6660
  return;
@@ -6685,14 +6662,6 @@ var HandoffCheckpointTracker = class {
6685
6662
  await rm3(filePath, { force: true }).catch(() => {
6686
6663
  });
6687
6664
  }
6688
- async removeTmpDirIfEmpty(tmpDir) {
6689
- const entries = await readdir(tmpDir).catch(() => null);
6690
- if (!entries || entries.length > 0) {
6691
- return;
6692
- }
6693
- await rmdir(tmpDir).catch(() => {
6694
- });
6695
- }
6696
6665
  };
6697
6666
  export {
6698
6667
  HandoffCheckpointTracker