@sagentlab/navarch-runtime 0.1.167 → 0.1.168

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/README.md CHANGED
@@ -552,7 +552,7 @@ unchanged across the deployment.
552
552
  | `NAVARCH_ENROLLMENT_TOKEN` | — | Alternative to `register --token` / `connect --token`. |
553
553
  | `NAVARCH_PROJECT_ID` | — | Alternative to `connect --project`. |
554
554
  | `NAVARCH_CONFIG_DIR` | `~/.navarch` | Per-instance state root. Equivalent to `--config-dir`; use a different directory for every agent on the same host. |
555
- | `NAVARCH_WORKSPACE_ROOT` | `<config dir>/sandboxes` | Persistent bare repo caches plus isolated per-session worktrees. |
555
+ | `NAVARCH_WORKSPACE_ROOT` | `<config dir>/sandboxes` | Persistent bare repo caches plus isolated per-session worktrees. Sessions that carry a repo-local GitHub token or resume an owner-assigned delivery branch clone their private repository from `repository-seeds/<project>.git`, so only new commits are downloaded. |
556
556
  | `NAVARCH_MAX_SESSIONS` | `5` | Local concurrent-session capacity cap — see `src/capacity.cts`. |
557
557
  | `NAVARCH_CAPABILITIES` | `shell,browser-use` (`docker-sandbox,shell` in Docker mode) | Comma list reported at heartbeat/claim time. Docker mode does not advertise browser use until the configured image provides it. |
558
558
  | `NAVARCH_BROWSER_PROBE` | on | Advertise `browser-use` only after a startup headless-Chromium launch (host, plus each sandboxed runtime) succeeds. `off` trusts `NAVARCH_CAPABILITIES` without a check. |
@@ -41,7 +41,8 @@ const TRANSIENT_GIT_AUTH_FAILURE = /Repository not found|Authentication failed|c
41
41
  * Maintains one bare repository cache per project and checks out each session
42
42
  * into its own uniquely named worktree. Sessions that need a repo-local secret
43
43
  * use an isolated bare repository under their session root because linked
44
- * worktrees share their repository-local config.
44
+ * worktrees share their repository-local config; that repository is cloned
45
+ * from a credential-free per-project seed cache rather than from scratch.
45
46
  */
46
47
  class GitWorktree {
47
48
  sessionRoot;
@@ -56,6 +57,8 @@ class GitWorktree {
56
57
  authRetryDelaysMs;
57
58
  sleep;
58
59
  checkpointRoot;
60
+ /** Credential-free object cache that seeds an isolated session repository. */
61
+ seedRepositoryPath;
59
62
  cloneUrl;
60
63
  githubToken;
61
64
  githubTokenProvider;
@@ -84,9 +87,13 @@ class GitWorktree {
84
87
  this.worktreePath = node_path_1.default.join(this.sessionRoot, "repo");
85
88
  // An owner-assigned branch can have any name. Keep its local ref isolated
86
89
  // from other tasks and abandoned sessions that use the shared project cache.
87
- this.repositoryPath = options.repoLocalGithubToken || options.deliveryBinding
90
+ const isolatedRepository = Boolean(options.repoLocalGithubToken || options.deliveryBinding);
91
+ this.repositoryPath = isolatedRepository
88
92
  ? node_path_1.default.join(this.sessionRoot, "repository.git")
89
93
  : node_path_1.default.join(options.workspaceRoot, "repositories", `${projectKey}.git`);
94
+ this.seedRepositoryPath = isolatedRepository
95
+ ? node_path_1.default.join(options.workspaceRoot, "repository-seeds", `${projectKey}.git`)
96
+ : null;
90
97
  // One delivery task owns one remote branch across every retry. A
91
98
  // session-scoped branch lets a failed attempt push useful work, then makes
92
99
  // the retry start from main and open a second PR for the same task.
@@ -224,11 +231,13 @@ class GitWorktree {
224
231
  /** Clones the bare cache if missing, repoints origin if needed, and fetches all branches. */
225
232
  async ensureRepositoryCache() {
226
233
  if (!(await pathExists(node_path_1.default.join(this.repositoryPath, "HEAD")))) {
227
- await this.runAuthenticatedGitWithRetry(["clone", "--bare", this.cloneUrl, this.repositoryPath],
228
234
  // A failed clone can leave a partial bare repository behind, and git
229
235
  // refuses to clone into a non-empty directory. No HEAD means nothing
230
236
  // usable (or shared) lives there yet.
231
- () => node_fs_1.promises.rm(this.repositoryPath, { recursive: true, force: true }));
237
+ const clearPartialClone = () => node_fs_1.promises.rm(this.repositoryPath, { recursive: true, force: true });
238
+ if (!(await this.cloneFromSeed(clearPartialClone))) {
239
+ await this.runAuthenticatedGitWithRetry(["clone", "--bare", this.cloneUrl, this.repositoryPath], clearPartialClone);
240
+ }
232
241
  }
233
242
  else {
234
243
  const origin = await this.runGit(["--git-dir", this.repositoryPath, "remote", "get-url", "origin"], false);
@@ -245,6 +254,76 @@ class GitWorktree {
245
254
  "+refs/heads/*:refs/remotes/origin/*",
246
255
  ]);
247
256
  }
257
+ /**
258
+ * Clones an isolated session repository by borrowing objects from the
259
+ * project's seed cache, so only commits the seed lacks cross the network.
260
+ * Without it every token-bearing session downloads the full history, and
261
+ * concurrent sessions of a large repository starve each other's clones for
262
+ * an hour or more. `--dissociate` copies the borrowed objects in, leaving the
263
+ * session repository self-contained: the agent's sandbox never needs the
264
+ * seed, and the seed never holds the session's credential.
265
+ *
266
+ * The seed is an optimization only. Any failure returns false and the caller
267
+ * falls back to a plain network clone.
268
+ */
269
+ async cloneFromSeed(clearPartialClone) {
270
+ const seed = this.seedRepositoryPath;
271
+ if (!seed)
272
+ return false;
273
+ try {
274
+ await withRepositoryLock(seed, () => this.refreshSeedRepository(seed));
275
+ await this.runAuthenticatedGitWithRetry(["clone", "--bare", "--reference", seed, "--dissociate", this.cloneUrl, this.repositoryPath], clearPartialClone);
276
+ return true;
277
+ }
278
+ catch {
279
+ await clearPartialClone();
280
+ return false;
281
+ }
282
+ }
283
+ /**
284
+ * Creates the seed on first use and tops it up with new branch objects. The
285
+ * in-process lock makes concurrent sessions wait for one download instead of
286
+ * each starting their own; the temp-directory rename keeps a second runtime
287
+ * process from ever seeing a half-cloned seed.
288
+ */
289
+ async refreshSeedRepository(seed) {
290
+ if (!(await pathExists(node_path_1.default.join(seed, "HEAD")))) {
291
+ await node_fs_1.promises.mkdir(node_path_1.default.dirname(seed), { recursive: true });
292
+ await removeStaleSeedClones(seed);
293
+ const partial = `${seed}${SEED_PARTIAL_INFIX}${process.pid}-${Date.now()}`;
294
+ const clearPartial = () => node_fs_1.promises.rm(partial, { recursive: true, force: true });
295
+ try {
296
+ await this.runAuthenticatedGitWithRetry(["clone", "--bare", this.cloneUrl, partial], clearPartial);
297
+ // Session clones read the seed's packs while other sessions fetch into
298
+ // it, so nothing may ever repack or prune them underneath a reader.
299
+ await this.runGit(["--git-dir", partial, "config", "gc.auto", "0"], false);
300
+ await this.runGit(["--git-dir", partial, "config", "maintenance.auto", "false"], false);
301
+ await node_fs_1.promises.rename(partial, seed);
302
+ }
303
+ catch (error) {
304
+ await clearPartial();
305
+ // Another runtime process published the seed first; use theirs.
306
+ if (!(await pathExists(node_path_1.default.join(seed, "HEAD"))))
307
+ throw error;
308
+ }
309
+ return;
310
+ }
311
+ try {
312
+ // Fetch by URL, not by the seed's origin, so a moved repository needs no repair.
313
+ await this.runAuthenticatedGitWithRetry([
314
+ "--git-dir",
315
+ seed,
316
+ "fetch",
317
+ "--prune",
318
+ this.cloneUrl,
319
+ "+refs/heads/*:refs/heads/*",
320
+ ]);
321
+ }
322
+ catch {
323
+ // A stale seed still saves most of the download; the session clone
324
+ // fetches whatever is missing from origin.
325
+ }
326
+ }
248
327
  /**
249
328
  * Runs a token-authenticated git command, retrying with backoff while GitHub
250
329
  * rejects the credential in a way a not-yet-propagated token produces.
@@ -862,6 +941,23 @@ async function withRepositoryLock(key, work, signal) {
862
941
  repositoryLocks.delete(key); });
863
942
  }
864
943
  }
944
+ const SEED_PARTIAL_INFIX = ".partial-";
945
+ const STALE_SEED_CLONE_MS = 24 * 60 * 60 * 1000;
946
+ /** Removes seed clones a crashed runtime left behind; a live one is far younger. */
947
+ async function removeStaleSeedClones(seed) {
948
+ const directory = node_path_1.default.dirname(seed);
949
+ const prefix = `${node_path_1.default.basename(seed)}${SEED_PARTIAL_INFIX}`;
950
+ const entries = await node_fs_1.promises.readdir(directory).catch(() => []);
951
+ await Promise.all(entries
952
+ .filter((entry) => entry.startsWith(prefix))
953
+ .map(async (entry) => {
954
+ const target = node_path_1.default.join(directory, entry);
955
+ const stat = await node_fs_1.promises.stat(target).catch(() => null);
956
+ if (stat && Date.now() - stat.mtimeMs > STALE_SEED_CLONE_MS) {
957
+ await node_fs_1.promises.rm(target, { recursive: true, force: true });
958
+ }
959
+ }));
960
+ }
865
961
  async function pathExists(value) {
866
962
  try {
867
963
  await node_fs_1.promises.access(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sagentlab/navarch-runtime",
3
- "version": "0.1.167",
3
+ "version": "0.1.168",
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",