@posthog/agent 2.3.398 → 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 -2
  5. package/dist/handoff-checkpoint.js +38 -53
  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 -768
  17. package/dist/server/agent-server.js.map +1 -1
  18. package/dist/server/bin.cjs +101 -766
  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 +3 -17
  25. package/src/handoff-checkpoint.ts +15 -45
  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 -6431
  35. package/dist/tree-tracker.js.map +0 -1
  36. package/src/sagas/apply-snapshot-saga.test.ts +0 -690
  37. package/src/sagas/apply-snapshot-saga.ts +0 -100
  38. package/src/sagas/capture-tree-saga.test.ts +0 -892
  39. package/src/sagas/capture-tree-saga.ts +0 -150
  40. package/src/tree-tracker.ts +0 -173
@@ -34,9 +34,7 @@ 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
39
  }
42
40
 
@@ -897,12 +897,14 @@ var require_dist2 = __commonJS({
897
897
  });
898
898
 
899
899
  // src/handoff-checkpoint.ts
900
- import { mkdir as mkdir3, readFile as readFile2, rm as rm3, writeFile } from "fs/promises";
901
- 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";
902
903
 
903
904
  // ../git/dist/handoff.js
904
905
  import { spawn as spawn2 } from "child_process";
905
- 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";
906
908
  import path3 from "path";
907
909
 
908
910
  // ../../node_modules/simple-git/dist/esm/index.js
@@ -6048,7 +6050,7 @@ var GitHandoffTracker = class {
6048
6050
  this.repositoryPath = config.repositoryPath;
6049
6051
  this.logger = config.logger;
6050
6052
  }
6051
- async captureForHandoff(localGitState) {
6053
+ async captureForHandoff(_localGitState) {
6052
6054
  const captureSaga = new CaptureCheckpointSaga(this.logger);
6053
6055
  const result = await captureSaga.run({ baseDir: this.repositoryPath });
6054
6056
  if (!result.success) {
@@ -6056,15 +6058,19 @@ var GitHandoffTracker = class {
6056
6058
  }
6057
6059
  const checkpoint = result.data;
6058
6060
  const git = createGitClient(this.repositoryPath);
6059
- const tempDir = await this.getTempDir(git);
6061
+ const tempDir = await this.createTempDir(checkpoint.checkpointId);
6060
6062
  const checkpointRef = `${CHECKPOINT_REF_PREFIX2}${checkpoint.checkpointId}`;
6061
- const shouldIncludeHead = !!checkpoint.head && checkpoint.head !== localGitState?.head;
6062
- 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;
6063
6069
  const packPrefix = path3.join(tempDir, checkpoint.checkpointId);
6064
6070
  try {
6065
6071
  const [headPack, indexFile, tracking] = await Promise.all([
6066
- shouldIncludeHead && checkpoint.head ? this.captureHeadPack(packPrefix, checkpoint.head) : Promise.resolve(void 0),
6067
- this.copyIndexFile(git, checkpoint.checkpointId),
6072
+ this.captureObjectPack(packPrefix, packRefs),
6073
+ this.copyIndexFile(git, checkpoint.checkpointId, tempDir),
6068
6074
  getTrackingMetadata(git, checkpoint.branch)
6069
6075
  ]);
6070
6076
  return {
@@ -6111,6 +6117,8 @@ var GitHandoffTracker = class {
6111
6117
  } else if (checkpoint.head) {
6112
6118
  await git.checkout(checkpoint.head);
6113
6119
  }
6120
+ await git.clean(["f", "d"]);
6121
+ await git.raw(["read-tree", "--reset", "-u", checkpoint.worktreeTree]);
6114
6122
  if (indexPath) {
6115
6123
  await this.restoreIndexFile(git, indexPath);
6116
6124
  }
@@ -6122,8 +6130,8 @@ var GitHandoffTracker = class {
6122
6130
  totalBytes: packBytes + indexBytes
6123
6131
  };
6124
6132
  }
6125
- async captureHeadPack(packPrefix, headCommit) {
6126
- 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")}
6127
6135
  `);
6128
6136
  const packPath = `${packPrefix}-${hash.trim()}.pack`;
6129
6137
  const rawBytes = await this.getFileSize(packPath);
@@ -6131,9 +6139,8 @@ var GitHandoffTracker = class {
6131
6139
  });
6132
6140
  return { path: packPath, rawBytes };
6133
6141
  }
6134
- async copyIndexFile(git, checkpointId) {
6142
+ async copyIndexFile(git, checkpointId, tempDir) {
6135
6143
  const indexPath = await this.getGitPath(git, "index");
6136
- const tempDir = await this.getTempDir(git);
6137
6144
  const copiedIndexPath = path3.join(tempDir, `${checkpointId}.index`);
6138
6145
  await copyFile(indexPath, copiedIndexPath);
6139
6146
  return {
@@ -6261,13 +6268,8 @@ var GitHandoffTracker = class {
6261
6268
  ]);
6262
6269
  return exitCode === 0;
6263
6270
  }
6264
- async getTempDir(git) {
6265
- const raw = await git.raw(["rev-parse", "--git-common-dir"]);
6266
- const commonDir = raw.trim() || ".git";
6267
- const resolved = path3.isAbsolute(commonDir) ? commonDir : path3.resolve(this.repositoryPath, commonDir);
6268
- const tempDir = path3.join(resolved, "posthog-code-tmp");
6269
- await mkdir2(tempDir, { recursive: true });
6270
- return tempDir;
6271
+ async createTempDir(checkpointId) {
6272
+ return mkdtemp(joinTempPrefix(checkpointId));
6271
6273
  }
6272
6274
  async getGitPath(git, gitPath) {
6273
6275
  const raw = await git.raw(["rev-parse", "--git-path", gitPath]);
@@ -6334,6 +6336,9 @@ var GitHandoffTracker = class {
6334
6336
  });
6335
6337
  }
6336
6338
  };
6339
+ function joinTempPrefix(checkpointId) {
6340
+ return path3.join(tmpdir(), `posthog-code-handoff-${checkpointId}-`);
6341
+ }
6337
6342
  async function getCurrentBranchName(git) {
6338
6343
  try {
6339
6344
  const raw = await git.revparse(["--abbrev-ref", "HEAD"]);
@@ -6473,8 +6478,11 @@ var HandoffCheckpointTracker = class {
6473
6478
  indexArtifactPath: uploads.index?.storagePath
6474
6479
  };
6475
6480
  } finally {
6481
+ const tempDir = capture.headPack?.path ? dirname(capture.headPack.path) : dirname(capture.indexFile.path);
6476
6482
  await this.removeIfPresent(capture.headPack?.path);
6477
6483
  await this.removeIfPresent(capture.indexFile.path);
6484
+ await rm3(tempDir, { recursive: true, force: true }).catch(() => {
6485
+ });
6478
6486
  }
6479
6487
  }
6480
6488
  async applyFromHandoff(checkpoint, options) {
@@ -6484,8 +6492,9 @@ var HandoffCheckpointTracker = class {
6484
6492
  );
6485
6493
  }
6486
6494
  const gitTracker = this.createGitTracker();
6487
- const tmpDir = join2(this.repositoryPath, ".posthog", "tmp");
6488
- await mkdir3(tmpDir, { recursive: true });
6495
+ const tmpDir = await mkdtemp2(
6496
+ join2(tmpdir2(), `posthog-code-handoff-${checkpoint.checkpointId}-`)
6497
+ );
6489
6498
  const packPath = join2(tmpDir, `${checkpoint.checkpointId}.pack`);
6490
6499
  const indexPath = join2(tmpDir, `${checkpoint.checkpointId}.index`);
6491
6500
  try {
@@ -6519,6 +6528,8 @@ var HandoffCheckpointTracker = class {
6519
6528
  } finally {
6520
6529
  await this.removeIfPresent(packPath);
6521
6530
  await this.removeIfPresent(indexPath);
6531
+ await rm3(tmpDir, { recursive: true, force: true }).catch(() => {
6532
+ });
6522
6533
  }
6523
6534
  }
6524
6535
  toGitCheckpoint(checkpoint) {
@@ -6626,50 +6637,24 @@ var HandoffCheckpointTracker = class {
6626
6637
  }
6627
6638
  logCaptureMetrics(checkpoint, uploads) {
6628
6639
  this.logger.info("Captured handoff checkpoint", {
6629
- checkpointId: checkpoint.checkpointId,
6630
6640
  branch: checkpoint.branch,
6631
- head: checkpoint.head,
6632
- artifactPath: uploads.pack?.storagePath,
6633
- indexArtifactPath: uploads.index?.storagePath,
6634
- ...this.buildMetricPayload(uploads)
6641
+ head: checkpoint.head?.slice(0, 7),
6642
+ totalBytes: this.sumRawBytes(uploads.pack, uploads.index)
6635
6643
  });
6636
6644
  }
6637
- logApplyMetrics(checkpoint, downloads, totalBytes) {
6645
+ logApplyMetrics(checkpoint, _downloads, totalBytes) {
6638
6646
  this.logger.info("Applied handoff checkpoint", {
6639
- checkpointId: checkpoint.checkpointId,
6640
- commit: checkpoint.commit,
6641
6647
  branch: checkpoint.branch,
6642
- head: checkpoint.head,
6643
- packBytes: downloads.pack?.rawBytes ?? 0,
6644
- packWireBytes: downloads.pack?.wireBytes ?? 0,
6645
- indexBytes: downloads.index?.rawBytes ?? 0,
6646
- indexWireBytes: downloads.index?.wireBytes ?? 0,
6647
- totalBytes,
6648
- totalWireBytes: this.sumWireBytes(downloads.pack, downloads.index)
6648
+ head: checkpoint.head?.slice(0, 7),
6649
+ totalBytes
6649
6650
  });
6650
6651
  }
6651
- buildMetricPayload(metrics) {
6652
- return {
6653
- packBytes: metrics.pack?.rawBytes ?? 0,
6654
- packWireBytes: metrics.pack?.wireBytes ?? 0,
6655
- indexBytes: metrics.index?.rawBytes ?? 0,
6656
- indexWireBytes: metrics.index?.wireBytes ?? 0,
6657
- totalBytes: this.sumRawBytes(metrics.pack, metrics.index),
6658
- totalWireBytes: this.sumWireBytes(metrics.pack, metrics.index)
6659
- };
6660
- }
6661
6652
  sumRawBytes(...artifacts) {
6662
6653
  return artifacts.reduce(
6663
6654
  (total, artifact) => total + (artifact?.rawBytes ?? 0),
6664
6655
  0
6665
6656
  );
6666
6657
  }
6667
- sumWireBytes(...artifacts) {
6668
- return artifacts.reduce(
6669
- (total, artifact) => total + (artifact?.wireBytes ?? 0),
6670
- 0
6671
- );
6672
- }
6673
6658
  async removeIfPresent(filePath) {
6674
6659
  if (!filePath) {
6675
6660
  return;