@sagentlab/navarch-runtime 0.1.174 → 0.1.175

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.
@@ -4,6 +4,7 @@ exports.LiveTranscriptPublisher = void 0;
4
4
  exports.renderSessionTranscript = renderSessionTranscript;
5
5
  exports.renderLiveTranscript = renderLiveTranscript;
6
6
  exports.liveUploadIntervalMs = liveUploadIntervalMs;
7
+ const upload_errors_cjs_1 = require("./upload-errors.cjs");
7
8
  /** The shape both the live snapshots and the completion upload use. */
8
9
  function renderSessionTranscript(attempts, redact) {
9
10
  return attempts
@@ -46,6 +47,8 @@ function renderLiveTranscript(attempts, currentStdout, redact, knownSecrets = []
46
47
  /** Signed upload URLs last an hour; refresh well before that. */
47
48
  const UPLOAD_URL_TTL_MS = 45 * 60_000;
48
49
  const DEFAULT_INTERVAL_MS = 10_000;
50
+ /** Longest wait between retries while storage keeps failing. */
51
+ const MAX_FAILURE_BACKOFF_MS = 5 * 60_000;
49
52
  function liveUploadIntervalMs(bytes, baseMs = DEFAULT_INTERVAL_MS) {
50
53
  if (bytes > 8 * 1024 * 1024)
51
54
  return Math.max(baseMs, 60_000);
@@ -66,6 +69,7 @@ class LiveTranscriptPublisher {
66
69
  lastUploaded = "";
67
70
  uploadUrl = null;
68
71
  published = null;
72
+ failures = 0;
69
73
  constructor(options) {
70
74
  this.options = options;
71
75
  this.now = options.now ?? Date.now;
@@ -100,7 +104,12 @@ class LiveTranscriptPublisher {
100
104
  schedule() {
101
105
  if (this.timer || this.inFlight || this.stopped || this.paused)
102
106
  return;
103
- const gap = liveUploadIntervalMs(this.lastUploaded.length, this.intervalMs);
107
+ const base = liveUploadIntervalMs(this.lastUploaded.length, this.intervalMs);
108
+ // While storage keeps failing, double the wait per failure instead of
109
+ // re-sending a multi-megabyte snapshot into an outage every interval.
110
+ const gap = this.failures
111
+ ? Math.max(base, Math.min(base * 2 ** this.failures, MAX_FAILURE_BACKOFF_MS))
112
+ : base;
104
113
  const wait = Math.max(0, this.lastUploadedAt + gap - this.now());
105
114
  this.timer = setTimeout(() => {
106
115
  this.timer = null;
@@ -129,11 +138,20 @@ class LiveTranscriptPublisher {
129
138
  await this.options.upload(this.uploadUrl.url, content);
130
139
  this.lastUploaded = content;
131
140
  this.published = this.uploadUrl.publicUrl;
141
+ if (this.failures)
142
+ this.options.onRecover?.(this.failures);
143
+ this.failures = 0;
132
144
  }
133
145
  catch (error) {
134
- // A rejected signature is refreshed on the next attempt.
135
- this.uploadUrl = null;
136
- this.options.onError?.(error);
146
+ // A storage outage says nothing about the signature, so keep the URL;
147
+ // anything else (an expired or rejected signature) gets a fresh one.
148
+ if (!(0, upload_errors_cjs_1.isTransientUploadError)(error))
149
+ this.uploadUrl = null;
150
+ this.failures += 1;
151
+ this.options.onError?.(error, this.failures);
152
+ // Retry the unsent snapshot even if the agent goes quiet.
153
+ if (!this.paused && !this.stopped)
154
+ this.dirty = true;
137
155
  }
138
156
  finally {
139
157
  this.lastUploadedAt = this.now();
package/dist/session.cjs CHANGED
@@ -64,6 +64,9 @@ const DELIVERY_CORRECTION_RETRIES = 1;
64
64
  */
65
65
  const STEERING_COMPLETION_RESENDS = 3;
66
66
  const STEERING_COMPLETION_RESEND_DELAY_MS = 1_000;
67
+ /** Final transcript PUT: 1 s, 2 s, 4 s between attempts rides out a short
68
+ * R2 blip without holding completion for long. */
69
+ const COMPLETION_UPLOAD_RETRY = { attempts: 4, baseDelayMs: 1_000 };
67
70
  /**
68
71
  * How long an agent process may run before the messages in its prompt count as
69
72
  * delivered, when it has printed nothing yet. A launch that fails does so
@@ -928,7 +931,10 @@ async function runClaimedSession(deps, claimed, sessionId, lifecycle) {
928
931
  },
929
932
  upload: upload_cjs_1.uploadTranscript,
930
933
  render: () => (0, live_transcript_cjs_1.renderLiveTranscript)(attempts, liveStdout, (text) => (0, redact_cjs_1.redactText)(text, registry.list()), registry.list()),
931
- onError: (error) => log.warn(`live transcript upload failed for ${leaseId}: ${(0, redact_cjs_1.redactText)(String(error), registry.list())}`),
934
+ onError: (error, failures) => log.warn(`live transcript upload failed for ${leaseId}` +
935
+ `${failures > 1 ? ` (${failures} in a row; retrying with backoff)` : ""}: ` +
936
+ (0, redact_cjs_1.redactText)(String(error), registry.list())),
937
+ onRecover: (failures) => log.info(`live transcript upload for ${leaseId} recovered after ${failures} failed attempt(s)`),
932
938
  });
933
939
  const agentBudgetStartedAt = node_perf_hooks_1.performance.now();
934
940
  const maxRuntimeMs = task.max_runtime_ms ?? config.sessionTimeoutMs;
@@ -1376,7 +1382,9 @@ async function runClaimedSession(deps, claimed, sessionId, lifecycle) {
1376
1382
  let transcriptUrl;
1377
1383
  try {
1378
1384
  const { upload_url, public_url } = await api.getTranscriptUploadUrl(leaseId);
1379
- await (0, upload_cjs_1.uploadTranscript)(upload_url, transcript);
1385
+ // The final transcript is the one the task view keeps, so ride out a
1386
+ // short storage blip (R2 InternalError 500s) instead of dropping it.
1387
+ await (0, upload_cjs_1.uploadTranscript)(upload_url, transcript, fetch, COMPLETION_UPLOAD_RETRY);
1380
1388
  transcriptUrl = public_url;
1381
1389
  }
1382
1390
  catch (err) {
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ /**
3
+ * Transcript upload failure classification, kept apart from upload.cts so
4
+ * modules that only classify failures (live-transcript.cts) do not depend on
5
+ * the upload implementation that tests replace wholesale.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TranscriptUploadError = void 0;
9
+ exports.isTransientUploadError = isTransientUploadError;
10
+ /** A failed transcript PUT. `status` is null when no response arrived. */
11
+ class TranscriptUploadError extends Error {
12
+ status;
13
+ constructor(message, status) {
14
+ super(message);
15
+ this.status = status;
16
+ this.name = "TranscriptUploadError";
17
+ }
18
+ }
19
+ exports.TranscriptUploadError = TranscriptUploadError;
20
+ /**
21
+ * Storage-side failures (R2 `InternalError` 500s, 503s, throttling, a dropped
22
+ * connection) say nothing about the signed URL, so the same URL is worth
23
+ * retrying. Any other failure, such as a 403 for an expired signature, needs a
24
+ * fresh URL.
25
+ */
26
+ function isTransientUploadError(error) {
27
+ if (!(error instanceof TranscriptUploadError))
28
+ return false;
29
+ return error.status === null || error.status === 429 || error.status >= 500;
30
+ }
package/dist/upload.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isTransientUploadError = exports.TranscriptUploadError = void 0;
3
4
  exports.uploadTranscript = uploadTranscript;
4
5
  /**
5
6
  * PUTs an already-redacted transcript to the presigned R2 URL the
@@ -7,18 +8,44 @@ exports.uploadTranscript = uploadTranscript;
7
8
  * implementation-plan.md WP-07). Kept as its own module so the storage
8
9
  * mechanism is a one-function swap if it ever changes.
9
10
  */
11
+ const upload_errors_cjs_1 = require("./upload-errors.cjs");
12
+ Object.defineProperty(exports, "TranscriptUploadError", { enumerable: true, get: function () { return upload_errors_cjs_1.TranscriptUploadError; } });
13
+ Object.defineProperty(exports, "isTransientUploadError", { enumerable: true, get: function () { return upload_errors_cjs_1.isTransientUploadError; } });
10
14
  /** Storage error bodies are small JSON blobs; cap in case we get an HTML page. */
11
15
  const MAX_ERROR_BODY = 500;
12
- async function uploadTranscript(uploadUrl, content, fetchImpl = fetch) {
13
- const response = await fetchImpl(uploadUrl, {
14
- method: "PUT",
15
- headers: { "content-type": "text/plain; charset=utf-8" },
16
- body: content,
17
- });
16
+ const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
17
+ async function uploadTranscript(uploadUrl, content, fetchImpl = fetch, retry = {}) {
18
+ const attempts = Math.max(1, retry.attempts ?? 1);
19
+ const baseDelayMs = retry.baseDelayMs ?? 1_000;
20
+ const sleep = retry.sleep ?? defaultSleep;
21
+ for (let attempt = 1;; attempt += 1) {
22
+ try {
23
+ await putOnce(uploadUrl, content, fetchImpl);
24
+ return;
25
+ }
26
+ catch (error) {
27
+ if (attempt >= attempts || !(0, upload_errors_cjs_1.isTransientUploadError)(error))
28
+ throw error;
29
+ await sleep(baseDelayMs * 2 ** (attempt - 1));
30
+ }
31
+ }
32
+ }
33
+ async function putOnce(uploadUrl, content, fetchImpl) {
34
+ let response;
35
+ try {
36
+ response = await fetchImpl(uploadUrl, {
37
+ method: "PUT",
38
+ headers: { "content-type": "text/plain; charset=utf-8" },
39
+ body: content,
40
+ });
41
+ }
42
+ catch (error) {
43
+ throw new upload_errors_cjs_1.TranscriptUploadError(`Transcript upload failed: ${String(error)}`, null);
44
+ }
18
45
  if (!response.ok) {
19
46
  // Include the bounded storage response to distinguish expired signatures
20
47
  // from bucket misconfiguration.
21
- throw new Error(`Transcript upload failed with ${response.status}: ${await readErrorBody(response)}`);
48
+ throw new upload_errors_cjs_1.TranscriptUploadError(`Transcript upload failed with ${response.status}: ${await readErrorBody(response)}`, response.status);
22
49
  }
23
50
  }
24
51
  async function readErrorBody(response) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sagentlab/navarch-runtime",
3
- "version": "0.1.174",
3
+ "version": "0.1.175",
4
4
  "description": "Navarch machine-side session manager: claims delivery tasks and runs them through local coding agents.",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",