@opengeni/worker-bundle 0.12.0 → 0.12.1

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/index.js CHANGED
@@ -5227,6 +5227,35 @@ function classifyCaptureEntryError(error) {
5227
5227
  }
5228
5228
  var TREE_MAX_ENTRIES = 2e4;
5229
5229
  var TREE_MAX_DEPTH = 600;
5230
+ async function readCaptureRepository(svc, root) {
5231
+ let status;
5232
+ try {
5233
+ status = await svc.gitStatus({ path: root });
5234
+ } catch (error) {
5235
+ const boxExiting = classifyCaptureEntryError(error);
5236
+ if (boxExiting) throw boxExiting;
5237
+ return { complete: false, degradedReason: "repository_read_unavailable" };
5238
+ }
5239
+ if (!status.isRepo) {
5240
+ return { complete: false, degradedReason: "repository_read_unavailable" };
5241
+ }
5242
+ try {
5243
+ const diff = await svc.gitDiff({
5244
+ path: root,
5245
+ staged: false,
5246
+ includeUntracked: true,
5247
+ fromRef: "HEAD",
5248
+ pathspec: [],
5249
+ contextLines: 3,
5250
+ maxBytesPerFile: PER_FILE_DIFF_GUARD_BYTES
5251
+ });
5252
+ return { complete: true, status, diff };
5253
+ } catch (error) {
5254
+ const boxExiting = classifyCaptureEntryError(error);
5255
+ if (boxExiting) throw boxExiting;
5256
+ return { complete: false, degradedReason: "repository_read_unavailable" };
5257
+ }
5258
+ }
5230
5259
  var loggedStorageNullOnce = false;
5231
5260
  async function captureWorkspaceRevision(input) {
5232
5261
  const { observability } = input;
@@ -5333,46 +5362,15 @@ async function runCapture(input, ctx, startedAt, signal) {
5333
5362
  );
5334
5363
  }
5335
5364
  if (!discovery.complete) {
5336
- const capturedAt2 = /* @__PURE__ */ new Date();
5337
5365
  const reason = captureDegradedReason(discovery.degradedReason);
5338
- const stats2 = {
5339
- degradedReason: reason,
5340
- discoveredRepoCount: discovery.repos.length,
5341
- durationMs: Date.now() - startedAt
5342
- };
5343
- const inserted2 = await insertFailedWorkspaceCapture(input.db, {
5344
- accountId: input.accountId,
5345
- workspaceId: input.workspaceId,
5346
- sessionId: input.sessionId,
5347
- turnId: input.turnId,
5348
- attemptId: input.attemptId,
5349
- sandboxGroupId: input.sandboxGroupId,
5350
- expectedEpoch: input.leaseEpoch,
5366
+ await persistDegradedCapture(
5367
+ input,
5368
+ startedAt,
5369
+ signal,
5351
5370
  revision,
5352
- stats: stats2,
5353
- capturedAt: capturedAt2
5354
- });
5355
- throwIfCaptureAborted(signal);
5356
- if (!inserted2) {
5357
- observability.incrementCounter({
5358
- name: "opengeni_workspace_capture_total",
5359
- labels: { result: "superseded" }
5360
- });
5361
- return;
5362
- }
5363
- observability.warn("workspace capture degraded \u2014 repository discovery incomplete", {
5364
- "opengeni.session_id": input.sessionId,
5365
- "opengeni.turn_id": input.turnId ?? "",
5366
- "workspace_capture.degraded_reason": reason,
5367
- "workspace_capture.discovered_repo_count": discovery.repos.length
5368
- });
5369
- observability.incrementCounter({
5370
- name: "opengeni_workspace_capture_total",
5371
- labels: { result: "degraded_repository_discovery" }
5372
- });
5373
- if (input.publish) {
5374
- await input.publish(inserted2.events).catch(() => void 0);
5375
- }
5371
+ reason,
5372
+ discovery.repos.length
5373
+ );
5376
5374
  return;
5377
5375
  }
5378
5376
  const repoRoots = discovery.repos;
@@ -5382,33 +5380,20 @@ async function runCapture(input, ctx, startedAt, signal) {
5382
5380
  let deletions = 0;
5383
5381
  for (const root of repoRoots) {
5384
5382
  throwIfCaptureAborted(signal);
5385
- let status;
5386
- try {
5387
- status = await svc.gitStatus({ path: root });
5388
- throwIfCaptureAborted(signal);
5389
- } catch (error) {
5390
- const boxExiting = classifyCaptureEntryError(error);
5391
- if (boxExiting) throw boxExiting;
5392
- continue;
5393
- }
5394
- if (!status.isRepo) continue;
5395
- let diff;
5396
- try {
5397
- diff = await svc.gitDiff({
5398
- path: root,
5399
- staged: false,
5400
- includeUntracked: true,
5401
- fromRef: "HEAD",
5402
- pathspec: [],
5403
- contextLines: 3,
5404
- maxBytesPerFile: PER_FILE_DIFF_GUARD_BYTES
5405
- });
5406
- throwIfCaptureAborted(signal);
5407
- } catch (error) {
5408
- const boxExiting = classifyCaptureEntryError(error);
5409
- if (boxExiting) throw boxExiting;
5410
- diff = { files: [], revision: 0 };
5383
+ const repository = await readCaptureRepository(svc, root);
5384
+ throwIfCaptureAborted(signal);
5385
+ if (!repository.complete) {
5386
+ await persistDegradedCapture(
5387
+ input,
5388
+ startedAt,
5389
+ signal,
5390
+ revision,
5391
+ repository.degradedReason,
5392
+ repoRoots.length
5393
+ );
5394
+ return;
5411
5395
  }
5396
+ const { status, diff } = repository;
5412
5397
  const diffFiles = diff.files.filter((f) => !isUnderResidueDir(joinRepoPath(root, f.path)));
5413
5398
  const statusFiles = status.files.filter((f) => !isUnderResidueDir(joinRepoPath(root, f.path)));
5414
5399
  repos.push({
@@ -5757,6 +5742,53 @@ function captureDegradedReason(reason) {
5757
5742
  return "repository_discovery_command_failed";
5758
5743
  }
5759
5744
  }
5745
+ async function persistDegradedCapture(input, startedAt, signal, revision, reason, discoveredRepoCount) {
5746
+ const capturedAt = /* @__PURE__ */ new Date();
5747
+ const stats = {
5748
+ degradedReason: reason,
5749
+ discoveredRepoCount,
5750
+ durationMs: Date.now() - startedAt
5751
+ };
5752
+ const inserted = await insertFailedWorkspaceCapture(input.db, {
5753
+ accountId: input.accountId,
5754
+ workspaceId: input.workspaceId,
5755
+ sessionId: input.sessionId,
5756
+ turnId: input.turnId,
5757
+ attemptId: input.attemptId,
5758
+ sandboxGroupId: input.sandboxGroupId,
5759
+ expectedEpoch: input.leaseEpoch,
5760
+ revision,
5761
+ stats,
5762
+ capturedAt
5763
+ });
5764
+ throwIfCaptureAborted(signal);
5765
+ if (!inserted) {
5766
+ input.observability.incrementCounter({
5767
+ name: "opengeni_workspace_capture_total",
5768
+ labels: { result: "superseded" }
5769
+ });
5770
+ return;
5771
+ }
5772
+ const discoveryFailure = reason.startsWith("repository_discovery_");
5773
+ input.observability.warn(
5774
+ discoveryFailure ? "workspace capture degraded \u2014 repository discovery incomplete" : "workspace capture degraded \u2014 repository read incomplete",
5775
+ {
5776
+ "opengeni.session_id": input.sessionId,
5777
+ "opengeni.turn_id": input.turnId ?? "",
5778
+ "workspace_capture.degraded_reason": reason,
5779
+ "workspace_capture.discovered_repo_count": discoveredRepoCount
5780
+ }
5781
+ );
5782
+ input.observability.incrementCounter({
5783
+ name: "opengeni_workspace_capture_total",
5784
+ labels: {
5785
+ result: discoveryFailure ? "degraded_repository_discovery" : "degraded_repository_read"
5786
+ }
5787
+ });
5788
+ if (input.publish) {
5789
+ await input.publish(inserted.events).catch(() => void 0);
5790
+ }
5791
+ }
5760
5792
  function throwIfCaptureAborted(signal) {
5761
5793
  if (signal.aborted) {
5762
5794
  throw new Error("workspace capture cancelled after its deadline");