@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.
@@ -5679,7 +5679,7 @@ async function isCommitOnRemote(baseDir, commit, options) {
5679
5679
  }
5680
5680
 
5681
5681
  // src/sagas/apply-snapshot-saga.ts
5682
- import { mkdir as mkdir3, rm as rm2, writeFile as writeFile3 } from "fs/promises";
5682
+ import { mkdir as mkdir3, readdir, rm as rm2, rmdir, writeFile as writeFile3 } from "fs/promises";
5683
5683
  import { join as join3 } from "path";
5684
5684
 
5685
5685
  // ../git/dist/sagas/tree.js
@@ -6094,68 +6094,83 @@ var ApplySnapshotSaga = class extends Saga {
6094
6094
  throw new Error("Cannot apply snapshot: no archive URL");
6095
6095
  }
6096
6096
  const archiveUrl = snapshot.archiveUrl;
6097
- await this.step({
6098
- name: "create_tmp_dir",
6099
- execute: () => mkdir3(tmpDir, { recursive: true }),
6100
- rollback: async () => {
6101
- }
6102
- });
6103
- const archivePath = join3(tmpDir, `${snapshot.treeHash}.tar.gz`);
6104
- this.archivePath = archivePath;
6105
- await this.step({
6106
- name: "download_archive",
6107
- execute: async () => {
6108
- const arrayBuffer = await apiClient.downloadArtifact(
6109
- taskId,
6110
- runId,
6111
- archiveUrl
6112
- );
6113
- if (!arrayBuffer) {
6114
- throw new Error("Failed to download archive");
6097
+ try {
6098
+ await this.step({
6099
+ name: "create_tmp_dir",
6100
+ execute: () => mkdir3(tmpDir, { recursive: true }),
6101
+ rollback: async () => {
6115
6102
  }
6116
- const base64Content = Buffer.from(arrayBuffer).toString("utf-8");
6117
- const binaryContent = Buffer.from(base64Content, "base64");
6118
- await writeFile3(archivePath, binaryContent);
6119
- this.log.info("Tree archive downloaded", {
6120
- treeHash: snapshot.treeHash,
6121
- snapshotBytes: binaryContent.byteLength,
6122
- snapshotWireBytes: arrayBuffer.byteLength,
6123
- totalBytes: binaryContent.byteLength,
6124
- totalWireBytes: arrayBuffer.byteLength
6125
- });
6126
- },
6127
- rollback: async () => {
6128
- if (this.archivePath) {
6129
- await rm2(this.archivePath, { force: true }).catch(() => {
6103
+ });
6104
+ const archivePath = join3(tmpDir, `${snapshot.treeHash}.tar.gz`);
6105
+ this.archivePath = archivePath;
6106
+ await this.step({
6107
+ name: "download_archive",
6108
+ execute: async () => {
6109
+ const arrayBuffer = await apiClient.downloadArtifact(
6110
+ taskId,
6111
+ runId,
6112
+ archiveUrl
6113
+ );
6114
+ if (!arrayBuffer) {
6115
+ throw new Error("Failed to download archive");
6116
+ }
6117
+ const base64Content = Buffer.from(arrayBuffer).toString("utf-8");
6118
+ const binaryContent = Buffer.from(base64Content, "base64");
6119
+ await writeFile3(archivePath, binaryContent);
6120
+ this.log.info("Tree archive downloaded", {
6121
+ treeHash: snapshot.treeHash,
6122
+ snapshotBytes: binaryContent.byteLength,
6123
+ snapshotWireBytes: arrayBuffer.byteLength,
6124
+ totalBytes: binaryContent.byteLength,
6125
+ totalWireBytes: arrayBuffer.byteLength
6130
6126
  });
6127
+ },
6128
+ rollback: async () => {
6129
+ if (this.archivePath) {
6130
+ await rm2(this.archivePath, { force: true }).catch(() => {
6131
+ });
6132
+ }
6131
6133
  }
6134
+ });
6135
+ const gitApplySaga = new ApplyTreeSaga(this.log);
6136
+ const applyResult = await gitApplySaga.run({
6137
+ baseDir: repositoryPath,
6138
+ treeHash: snapshot.treeHash,
6139
+ baseCommit: snapshot.baseCommit,
6140
+ changes: snapshot.changes,
6141
+ archivePath: this.archivePath
6142
+ });
6143
+ if (!applyResult.success) {
6144
+ throw new Error(`Failed to apply tree: ${applyResult.error}`);
6132
6145
  }
6133
- });
6134
- const gitApplySaga = new ApplyTreeSaga(this.log);
6135
- const applyResult = await gitApplySaga.run({
6136
- baseDir: repositoryPath,
6137
- treeHash: snapshot.treeHash,
6138
- baseCommit: snapshot.baseCommit,
6139
- changes: snapshot.changes,
6140
- archivePath: this.archivePath
6141
- });
6142
- if (!applyResult.success) {
6143
- throw new Error(`Failed to apply tree: ${applyResult.error}`);
6146
+ this.log.info("Tree snapshot applied", {
6147
+ treeHash: snapshot.treeHash,
6148
+ totalChanges: snapshot.changes.length,
6149
+ deletedFiles: snapshot.changes.filter((c) => c.status === "D").length
6150
+ });
6151
+ return { treeHash: snapshot.treeHash };
6152
+ } finally {
6153
+ if (this.archivePath) {
6154
+ await rm2(this.archivePath, { force: true }).catch(() => {
6155
+ });
6156
+ }
6157
+ await this.removeTmpDirIfEmpty(tmpDir);
6158
+ this.archivePath = null;
6144
6159
  }
6145
- await rm2(this.archivePath, { force: true }).catch(() => {
6146
- });
6147
- this.log.info("Tree snapshot applied", {
6148
- treeHash: snapshot.treeHash,
6149
- totalChanges: snapshot.changes.length,
6150
- deletedFiles: snapshot.changes.filter((c) => c.status === "D").length
6160
+ }
6161
+ async removeTmpDirIfEmpty(tmpDir) {
6162
+ const entries = await readdir(tmpDir).catch(() => null);
6163
+ if (!entries || entries.length > 0) {
6164
+ return;
6165
+ }
6166
+ await rmdir(tmpDir).catch(() => {
6151
6167
  });
6152
- return { treeHash: snapshot.treeHash };
6153
6168
  }
6154
6169
  };
6155
6170
 
6156
6171
  // src/sagas/capture-tree-saga.ts
6157
6172
  import { existsSync as existsSync2 } from "fs";
6158
- import { readFile as readFile3, rm as rm3 } from "fs/promises";
6173
+ import { readdir as readdir2, readFile as readFile3, rm as rm3, rmdir as rmdir2 } from "fs/promises";
6159
6174
  import { join as join4 } from "path";
6160
6175
  var CaptureTreeSaga2 = class extends Saga {
6161
6176
  sagaName = "CaptureTreeSaga";
@@ -6176,54 +6191,62 @@ var CaptureTreeSaga2 = class extends Saga {
6176
6191
  }
6177
6192
  const shouldArchive = !!apiClient;
6178
6193
  const archivePath = shouldArchive ? join4(tmpDir, `tree-${Date.now()}.tar.gz`) : void 0;
6179
- const gitCaptureSaga = new CaptureTreeSaga(this.log);
6180
- const captureResult = await gitCaptureSaga.run({
6181
- baseDir: repositoryPath,
6182
- lastTreeHash,
6183
- archivePath
6184
- });
6185
- if (!captureResult.success) {
6186
- throw new Error(`Failed to capture tree: ${captureResult.error}`);
6187
- }
6188
- const {
6189
- snapshot: gitSnapshot,
6190
- archivePath: createdArchivePath,
6191
- changed
6192
- } = captureResult.data;
6193
- if (!changed || !gitSnapshot) {
6194
- this.log.debug("No changes since last capture", { lastTreeHash });
6195
- return { snapshot: null, newTreeHash: lastTreeHash };
6196
- }
6197
- let archiveUrl;
6198
- if (apiClient && createdArchivePath) {
6199
- try {
6200
- archiveUrl = await this.uploadArchive(
6201
- createdArchivePath,
6202
- gitSnapshot.treeHash,
6203
- apiClient,
6204
- taskId,
6205
- runId
6206
- );
6207
- } finally {
6208
- await rm3(createdArchivePath, { force: true }).catch(() => {
6194
+ try {
6195
+ const gitCaptureSaga = new CaptureTreeSaga(this.log);
6196
+ const captureResult = await gitCaptureSaga.run({
6197
+ baseDir: repositoryPath,
6198
+ lastTreeHash,
6199
+ archivePath
6200
+ });
6201
+ if (!captureResult.success) {
6202
+ throw new Error(`Failed to capture tree: ${captureResult.error}`);
6203
+ }
6204
+ const {
6205
+ snapshot: gitSnapshot,
6206
+ archivePath: createdArchivePath,
6207
+ changed
6208
+ } = captureResult.data;
6209
+ if (!changed || !gitSnapshot) {
6210
+ this.log.debug("No changes since last capture", { lastTreeHash });
6211
+ return { snapshot: null, newTreeHash: lastTreeHash };
6212
+ }
6213
+ let archiveUrl;
6214
+ if (apiClient && createdArchivePath) {
6215
+ try {
6216
+ archiveUrl = await this.uploadArchive(
6217
+ createdArchivePath,
6218
+ gitSnapshot.treeHash,
6219
+ apiClient,
6220
+ taskId,
6221
+ runId
6222
+ );
6223
+ } finally {
6224
+ await rm3(createdArchivePath, { force: true }).catch(() => {
6225
+ });
6226
+ }
6227
+ }
6228
+ const snapshot = {
6229
+ treeHash: gitSnapshot.treeHash,
6230
+ baseCommit: gitSnapshot.baseCommit,
6231
+ changes: gitSnapshot.changes,
6232
+ timestamp: gitSnapshot.timestamp,
6233
+ interrupted,
6234
+ archiveUrl
6235
+ };
6236
+ this.log.info("Tree captured", {
6237
+ treeHash: snapshot.treeHash,
6238
+ changes: snapshot.changes.length,
6239
+ interrupted,
6240
+ archiveUrl
6241
+ });
6242
+ return { snapshot, newTreeHash: snapshot.treeHash };
6243
+ } finally {
6244
+ if (archivePath) {
6245
+ await rm3(archivePath, { force: true }).catch(() => {
6209
6246
  });
6210
6247
  }
6248
+ await this.removeTmpDirIfEmpty(tmpDir);
6211
6249
  }
6212
- const snapshot = {
6213
- treeHash: gitSnapshot.treeHash,
6214
- baseCommit: gitSnapshot.baseCommit,
6215
- changes: gitSnapshot.changes,
6216
- timestamp: gitSnapshot.timestamp,
6217
- interrupted,
6218
- archiveUrl
6219
- };
6220
- this.log.info("Tree captured", {
6221
- treeHash: snapshot.treeHash,
6222
- changes: snapshot.changes.length,
6223
- interrupted,
6224
- archiveUrl
6225
- });
6226
- return { snapshot, newTreeHash: snapshot.treeHash };
6227
6250
  }
6228
6251
  async uploadArchive(archivePath, treeHash, apiClient, taskId, runId) {
6229
6252
  const archiveUrl = await this.step({
@@ -6262,6 +6285,14 @@ var CaptureTreeSaga2 = class extends Saga {
6262
6285
  });
6263
6286
  return archiveUrl;
6264
6287
  }
6288
+ async removeTmpDirIfEmpty(tmpDir) {
6289
+ const entries = await readdir2(tmpDir).catch(() => null);
6290
+ if (!entries || entries.length > 0) {
6291
+ return;
6292
+ }
6293
+ await rmdir2(tmpDir).catch(() => {
6294
+ });
6295
+ }
6265
6296
  };
6266
6297
 
6267
6298
  // src/utils/logger.ts