@schoolai/shipyard 3.11.0 → 3.11.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.
Files changed (60) hide show
  1. package/dist/{auth-GGM253LQ.js → auth-AUY74PMB.js} +3 -3
  2. package/dist/capability-detector-worker.js +8 -8
  3. package/dist/{chunk-R3XQ6W7L.js → chunk-4SYLDZTY.js} +4 -4
  4. package/dist/{chunk-C6QOTETH.js → chunk-5LWD5W7O.js} +24 -10
  5. package/dist/chunk-5LWD5W7O.js.map +1 -0
  6. package/dist/{chunk-IJHF4OM4.js → chunk-5W5N5U2S.js} +2 -2
  7. package/dist/{chunk-L7ELOV3S.js → chunk-FVZ5BDZS.js} +4 -4
  8. package/dist/chunk-FVZ5BDZS.js.map +1 -0
  9. package/dist/{chunk-RW2OTTUA.js → chunk-KYLYGFMH.js} +4 -4
  10. package/dist/{chunk-QJP7JCIS.js → chunk-LRNGLC4V.js} +41 -3
  11. package/dist/chunk-LRNGLC4V.js.map +1 -0
  12. package/dist/{chunk-A2UK6TW2.js → chunk-LZSMNUAI.js} +18 -1
  13. package/dist/{chunk-A2UK6TW2.js.map → chunk-LZSMNUAI.js.map} +1 -1
  14. package/dist/{chunk-Z37T5W6S.js → chunk-P2HZDIN7.js} +12 -7
  15. package/dist/chunk-P2HZDIN7.js.map +1 -0
  16. package/dist/{chunk-ZRJTZLRF.js → chunk-QKJNVVQ3.js} +4 -4
  17. package/dist/{chunk-2EQOL57Z.js → chunk-TFRYQDDG.js} +2 -2
  18. package/dist/{chunk-YXPPZQBJ.js → chunk-VL5RUCRF.js} +164 -37
  19. package/dist/chunk-VL5RUCRF.js.map +1 -0
  20. package/dist/{chunk-3WEEGJJN.js → chunk-X5KCX6ZS.js} +2 -2
  21. package/dist/{chunk-GM6MH4CD.js → chunk-XIEOWUPV.js} +2 -2
  22. package/dist/{chunk-6LINHACK.js → chunk-Y5UWRARP.js} +47 -24
  23. package/dist/chunk-Y5UWRARP.js.map +1 -0
  24. package/dist/cursor-runner.js +88 -62
  25. package/dist/cursor-runner.js.map +1 -1
  26. package/dist/electron-utility.js +5 -5
  27. package/dist/{git-repo-QNGPCJLI.js → git-repo-CTZJS3ER.js} +6 -4
  28. package/dist/index.js +8 -8
  29. package/dist/{logger-2F3CBS3V.js → logger-AN7EUK2B.js} +7 -5
  30. package/dist/{login-U256OVOJ.js → login-YB34LF4L.js} +6 -6
  31. package/dist/{logout-HY3MPOY5.js → logout-GUXVSWLZ.js} +5 -5
  32. package/dist/{mcp-servers-ICHOWXZB.js → mcp-servers-OAPQNDA7.js} +4 -4
  33. package/dist/{roi-YM5OOWHG.js → roi-NXJHL5X2.js} +3 -3
  34. package/dist/{serve-D5GKV2RU.js → serve-P3U2C5YH.js} +1175 -739
  35. package/dist/{serve-D5GKV2RU.js.map → serve-P3U2C5YH.js.map} +1 -1
  36. package/dist/{skills-W2Y6TWHA.js → skills-2UBVHFQ5.js} +2 -2
  37. package/dist/{start-JY26XC5R.js → start-Y34X3WVF.js} +10 -10
  38. package/package.json +1 -1
  39. package/dist/chunk-6LINHACK.js.map +0 -1
  40. package/dist/chunk-C6QOTETH.js.map +0 -1
  41. package/dist/chunk-L7ELOV3S.js.map +0 -1
  42. package/dist/chunk-QJP7JCIS.js.map +0 -1
  43. package/dist/chunk-YXPPZQBJ.js.map +0 -1
  44. package/dist/chunk-Z37T5W6S.js.map +0 -1
  45. /package/dist/{auth-GGM253LQ.js.map → auth-AUY74PMB.js.map} +0 -0
  46. /package/dist/{chunk-R3XQ6W7L.js.map → chunk-4SYLDZTY.js.map} +0 -0
  47. /package/dist/{chunk-IJHF4OM4.js.map → chunk-5W5N5U2S.js.map} +0 -0
  48. /package/dist/{chunk-RW2OTTUA.js.map → chunk-KYLYGFMH.js.map} +0 -0
  49. /package/dist/{chunk-ZRJTZLRF.js.map → chunk-QKJNVVQ3.js.map} +0 -0
  50. /package/dist/{chunk-2EQOL57Z.js.map → chunk-TFRYQDDG.js.map} +0 -0
  51. /package/dist/{chunk-3WEEGJJN.js.map → chunk-X5KCX6ZS.js.map} +0 -0
  52. /package/dist/{chunk-GM6MH4CD.js.map → chunk-XIEOWUPV.js.map} +0 -0
  53. /package/dist/{git-repo-QNGPCJLI.js.map → git-repo-CTZJS3ER.js.map} +0 -0
  54. /package/dist/{logger-2F3CBS3V.js.map → logger-AN7EUK2B.js.map} +0 -0
  55. /package/dist/{login-U256OVOJ.js.map → login-YB34LF4L.js.map} +0 -0
  56. /package/dist/{logout-HY3MPOY5.js.map → logout-GUXVSWLZ.js.map} +0 -0
  57. /package/dist/{mcp-servers-ICHOWXZB.js.map → mcp-servers-OAPQNDA7.js.map} +0 -0
  58. /package/dist/{roi-YM5OOWHG.js.map → roi-NXJHL5X2.js.map} +0 -0
  59. /package/dist/{skills-W2Y6TWHA.js.map → skills-2UBVHFQ5.js.map} +0 -0
  60. /package/dist/{start-JY26XC5R.js.map → start-Y34X3WVF.js.map} +0 -0
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  getShipyardHome
4
- } from "./chunk-Z37T5W6S.js";
4
+ } from "./chunk-P2HZDIN7.js";
5
5
  import {
6
6
  external_exports
7
7
  } from "./chunk-CNR7O5YH.js";
@@ -90,4 +90,4 @@ export {
90
90
  deleteConfig,
91
91
  loadAuthToken
92
92
  };
93
- //# sourceMappingURL=chunk-3WEEGJJN.js.map
93
+ //# sourceMappingURL=chunk-X5KCX6ZS.js.map
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  resolveNodeExecPath
4
- } from "./chunk-Z37T5W6S.js";
4
+ } from "./chunk-P2HZDIN7.js";
5
5
 
6
6
  // src/shared/spawn-as-node.ts
7
7
  var isElectronOverride = null;
@@ -34,4 +34,4 @@ export {
34
34
  buildNodeSpawnEnv,
35
35
  rewriteElectronNodeScriptSpawn
36
36
  };
37
- //# sourceMappingURL=chunk-GM6MH4CD.js.map
37
+ //# sourceMappingURL=chunk-XIEOWUPV.js.map
@@ -7,10 +7,10 @@ import {
7
7
  } from "./chunk-RR6V6SNM.js";
8
8
  import {
9
9
  logger
10
- } from "./chunk-QJP7JCIS.js";
10
+ } from "./chunk-LRNGLC4V.js";
11
11
  import {
12
12
  getShipyardHome
13
- } from "./chunk-Z37T5W6S.js";
13
+ } from "./chunk-P2HZDIN7.js";
14
14
  import {
15
15
  external_exports
16
16
  } from "./chunk-CNR7O5YH.js";
@@ -252,7 +252,7 @@ async function refreshRepoPathsInBackground(log) {
252
252
  const startGen = cacheGeneration;
253
253
  const start = Date.now();
254
254
  try {
255
- const { findGitRepos: findGitRepos2, discoverWorktrees: discoverWorktrees2 } = await import("./git-repo-QNGPCJLI.js");
255
+ const { findGitRepos: findGitRepos2, discoverWorktrees: discoverWorktrees2 } = await import("./git-repo-CTZJS3ER.js");
256
256
  const { homedir: homedir2 } = await import("os");
257
257
  const repos = await findGitRepos2(homedir2());
258
258
  const worktrees = await discoverWorktrees2(repos);
@@ -483,7 +483,8 @@ var EXCLUDE_DIRS = /* @__PURE__ */ new Set([
483
483
  ]);
484
484
  var MAX_DEPTH = 4;
485
485
  var WORKTREE_DISCOVERY_CONCURRENCY = 8;
486
- var REPO_METADATA_CONCURRENCY = 16;
486
+ var REPO_METADATA_CONCURRENCY = 4;
487
+ var FIND_GIT_REPOS_DIR_CONCURRENCY = 8;
487
488
  async function mapLimit(items, limit, mapper) {
488
489
  const results = new Array(items.length);
489
490
  const indexedItems = items.map((item, index) => ({ item, index }));
@@ -503,6 +504,9 @@ async function mapLimit(items, limit, mapper) {
503
504
  }
504
505
  async function findGitRepos(dir, depth = 0) {
505
506
  if (depth > MAX_DEPTH) return [];
507
+ await new Promise((r) => {
508
+ setImmediate(r);
509
+ });
506
510
  try {
507
511
  const entries = await readdir(dir, { withFileTypes: true });
508
512
  for (const entry of entries) {
@@ -511,14 +515,18 @@ async function findGitRepos(dir, depth = 0) {
511
515
  }
512
516
  }
513
517
  if (depth >= MAX_DEPTH) return [];
514
- const promises = [];
518
+ const subdirs = [];
515
519
  for (const entry of entries) {
516
520
  if (!entry.isDirectory()) continue;
517
521
  if (entry.name.startsWith(".")) continue;
518
522
  if (EXCLUDE_DIRS.has(entry.name)) continue;
519
- promises.push(findGitRepos(join4(dir, entry.name), depth + 1));
523
+ subdirs.push(join4(dir, entry.name));
520
524
  }
521
- const results = await Promise.all(promises);
525
+ const results = await mapLimit(
526
+ subdirs,
527
+ FIND_GIT_REPOS_DIR_CONCURRENCY,
528
+ (subdir) => findGitRepos(subdir, depth + 1)
529
+ );
522
530
  return results.flat();
523
531
  } catch {
524
532
  return [];
@@ -612,7 +620,18 @@ async function processRepoPaths(path, idx, fingerprints, prevCache, nextCache) {
612
620
  }
613
621
  return info;
614
622
  }
615
- async function detectEnvironments(lastKnown) {
623
+ var inFlightDetect = null;
624
+ function detectEnvironments(lastKnown) {
625
+ if (inFlightDetect) return inFlightDetect;
626
+ const promise = runDetectEnvironments(lastKnown);
627
+ inFlightDetect = promise;
628
+ promise.catch(() => {
629
+ }).finally(() => {
630
+ if (inFlightDetect === promise) inFlightDetect = null;
631
+ });
632
+ return promise;
633
+ }
634
+ async function runDetectEnvironments(lastKnown) {
616
635
  const { homedir: homedir2 } = await import("os");
617
636
  const cachedPaths = getCachedRepoPaths();
618
637
  let repoPaths;
@@ -625,7 +644,7 @@ async function detectEnvironments(lastKnown) {
625
644
  repoPaths = await findGitRepos(homedir2());
626
645
  } catch (err) {
627
646
  if (lastKnown && lastKnown.length > 0) {
628
- const { logger: logger2 } = await import("./logger-2F3CBS3V.js");
647
+ const { logger: logger2 } = await import("./logger-AN7EUK2B.js");
629
648
  logger2.warn(
630
649
  { err, lastKnownCount: lastKnown.length },
631
650
  "detectEnvironments findGitRepos threw \u2014 preserving lastKnown"
@@ -635,7 +654,7 @@ async function detectEnvironments(lastKnown) {
635
654
  return [];
636
655
  }
637
656
  if (repoPaths.length === 0 && lastKnown && lastKnown.length > 0) {
638
- const { logger: logger2 } = await import("./logger-2F3CBS3V.js");
657
+ const { logger: logger2 } = await import("./logger-AN7EUK2B.js");
639
658
  logger2.warn(
640
659
  { lastKnownCount: lastKnown.length },
641
660
  "detectEnvironments walk empty but lastKnown non-empty \u2014 preserving lastKnown"
@@ -688,25 +707,24 @@ async function listLinkedWorktrees(repoPath) {
688
707
  const gitWorktreesDir = join4(repoPath, ".git", "worktrees");
689
708
  let entries;
690
709
  try {
691
- entries = await readdir(gitWorktreesDir);
710
+ const result = await withProbeTimeout(readdir(gitWorktreesDir), GIT_PROBE_TIMEOUT_MS);
711
+ if (result === null) return [];
712
+ entries = result;
692
713
  } catch {
693
714
  return [];
694
715
  }
695
- const paths = await Promise.all(
696
- entries.map(async (name) => {
697
- try {
716
+ const paths = await mapLimit(entries, FIND_GIT_REPOS_DIR_CONCURRENCY, async (name) => {
717
+ const result = await withProbeTimeout(
718
+ (async () => {
698
719
  const gitdirFile = join4(gitWorktreesDir, name, "gitdir");
699
720
  const content = await readFile3(gitdirFile, "utf8");
700
721
  const wtGitPath = content.trim();
701
- if (wtGitPath.endsWith("/.git")) {
702
- return wtGitPath.slice(0, -"/.git".length);
703
- }
704
- return null;
705
- } catch {
706
- return null;
707
- }
708
- })
709
- );
722
+ return wtGitPath.endsWith("/.git") ? wtGitPath.slice(0, -"/.git".length) : null;
723
+ })(),
724
+ GIT_PROBE_TIMEOUT_MS
725
+ );
726
+ return result ?? null;
727
+ });
710
728
  return paths.filter((p) => p !== null && p !== repoPath);
711
729
  }
712
730
  function parseWorktreeListOutput(output, repoPath) {
@@ -729,12 +747,16 @@ var _testing = {
729
747
  GIT_PROBE_TIMEOUT_MS,
730
748
  SLOW_PATH_LOG_THRESHOLD_MS,
731
749
  NEGATIVE_CACHE_TTL_MS,
750
+ FIND_GIT_REPOS_DIR_CONCURRENCY,
732
751
  readGitRepoCache,
733
752
  resetGitRepoCache: () => {
734
753
  gitRepoCache.clear();
735
754
  },
736
755
  resetGhAvailableCache: () => {
737
756
  ghAvailableCache = false;
757
+ },
758
+ resetInFlightDetect: () => {
759
+ inFlightDetect = null;
738
760
  }
739
761
  };
740
762
 
@@ -758,10 +780,11 @@ export {
758
780
  parseOwnerRepo,
759
781
  getRepoDefaultBranch,
760
782
  isAncestor,
783
+ FIND_GIT_REPOS_DIR_CONCURRENCY,
761
784
  findGitRepos,
762
785
  getRepoMetadata,
763
786
  detectEnvironments,
764
787
  discoverWorktrees,
765
788
  _testing
766
789
  };
767
- //# sourceMappingURL=chunk-6LINHACK.js.map
790
+ //# sourceMappingURL=chunk-Y5UWRARP.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/shared/capabilities/git-repo.ts","../src/shared/capabilities/environments-cache.ts","../src/shared/capabilities/repo-paths-cache.ts","../src/shared/capabilities/shell.ts"],"sourcesContent":["import { readdir, readFile } from 'node:fs/promises';\nimport { basename, join } from 'node:path';\nimport type { GitRepoInfo } from '@shipyard/session';\nimport { logger } from '../logger.js';\nimport {\n type CacheEntry,\n type GitCacheFingerprint,\n getGitCacheFingerprint,\n isCacheValid,\n loadCache,\n resolveHeadPath,\n saveCache,\n} from './environments-cache.js';\nimport { gitExecSafe } from './git-pool.js';\nimport { getCachedRepoPaths, setCachedRepoPaths } from './repo-paths-cache.js';\nimport { run, runWithTimeout, TIMEOUT_MS } from './shell.js';\n\n/**\n * Per-path hard timeout for the environments walk. Lowered from\n * `TIMEOUT_MS` (5s) so a single hung path (NFS stall, symlink loop,\n * dead network drive — the May 2026 detect_environments wedge scenario)\n * can't pin the whole detector for tens of seconds. The walk continues\n * past timed-out paths; the per-path slow log surfaces the culprit on\n * the next investigation.\n */\nconst GIT_PROBE_TIMEOUT_MS = 3_000;\n\n/** Single-path duration past which we emit a slow-log event for diagnosis. */\nconst SLOW_PATH_LOG_THRESHOLD_MS = 2_000;\n\n/**\n * Race `op` against a `timeoutMs` deadline. On timeout, resolves to `null`\n * — the walker treats this the same as a git-call failure (skip the path\n * but continue the walk). Used so a single hung NFS path can't pin the\n * whole environments walk.\n */\nasync function withProbeTimeout<T>(op: Promise<T>, timeoutMs: number): Promise<T | null> {\n return new Promise<T | null>((resolve) => {\n const timer = setTimeout(() => resolve(null), timeoutMs);\n timer.unref?.();\n op.then(\n (v) => {\n clearTimeout(timer);\n resolve(v);\n },\n () => {\n clearTimeout(timer);\n resolve(null);\n }\n );\n });\n}\n\n/**\n * Negative cache TTL for `isGitRepo`. A directory that is not yet a git\n * repo may become one (`git init`) during the daemon lifetime; positives\n * are permanent (a work-tree never stops being one) but negatives must\n * expire so we re-probe periodically.\n */\nexport const NEGATIVE_CACHE_TTL_MS = 5_000;\n\ntype GitRepoCacheEntry = { isRepo: boolean; expiresAt?: number };\n\n/** Cache of directory paths to whether they are inside a git repository. */\nexport const gitRepoCache = new Map<string, GitRepoCacheEntry>();\n\nfunction readGitRepoCache(cwd: string, now = Date.now()): boolean | undefined {\n const cached = gitRepoCache.get(cwd);\n if (cached === undefined) return undefined;\n if (cached.isRepo) return true;\n if (cached.expiresAt !== undefined && now >= cached.expiresAt) {\n gitRepoCache.delete(cwd);\n return undefined;\n }\n return false;\n}\n\n/**\n * Check whether the given directory is inside a git work-tree.\n * Results are cached per directory so repeated calls (e.g. during\n * debounced diff captures) do not re-spawn git processes.\n */\nexport async function isGitRepo(cwd: string): Promise<boolean> {\n const cached = readGitRepoCache(cwd);\n if (cached !== undefined) return cached;\n try {\n await runWithTimeout('git', ['rev-parse', '--git-dir'], cwd, TIMEOUT_MS);\n gitRepoCache.set(cwd, { isRepo: true });\n return true;\n } catch {\n gitRepoCache.set(cwd, {\n isRepo: false,\n expiresAt: Date.now() + NEGATIVE_CACHE_TTL_MS,\n });\n return false;\n }\n}\n\nlet ghAvailableCache = false;\n\nexport async function isGhAvailable(): Promise<boolean> {\n if (ghAvailableCache) return true;\n try {\n await run('which', ['gh']);\n ghAvailableCache = true;\n return true;\n } catch {\n return false;\n }\n}\n\nconst topLevelCache = new Map<string, string>();\n\nexport async function getGitTopLevel(cwd: string): Promise<string> {\n const cached = topLevelCache.get(cwd);\n if (cached !== undefined) return cached;\n const topLevel = (\n await runWithTimeout('git', ['rev-parse', '--show-toplevel'], cwd, TIMEOUT_MS)\n ).trim();\n topLevelCache.set(cwd, topLevel);\n return topLevel;\n}\n\nexport function parseOwnerRepo(remoteUrl: string): { owner: string; repo: string } | null {\n const match = remoteUrl.match(/github\\.com[:/]([^/]+)\\/([^/.]+)/);\n if (!match?.[1] || !match[2]) return null;\n return { owner: match[1], repo: match[2] };\n}\n\n/**\n * Look up the repo's default branch via the `origin/HEAD` symbolic ref.\n * Returns the branch name (e.g. `main`) or null when the symbolic ref\n * isn't set (rare — happens for repos cloned without `git remote\n * set-head` or local-only repos).\n *\n * The result is cached for 5 seconds via the per-cwd OneShotPool in\n * git-pool.ts so repeated calls from serve-factory and stack-detection\n * per request coalesce to a single subprocess.\n */\nexport async function getRepoDefaultBranch(cwd: string): Promise<string | null> {\n const head = await gitExecSafe(cwd, ['symbolic-ref', '--short', 'refs/remotes/origin/HEAD']);\n if (!head) return null;\n return head.replace(/^origin\\//, '') || null;\n}\n\nexport async function isAncestor(\n ancestor: string,\n descendant: string,\n cwd: string\n): Promise<boolean> {\n try {\n await runWithTimeout('git', ['merge-base', '--is-ancestor', ancestor, descendant], cwd, 5_000);\n return true;\n } catch {\n return false;\n }\n}\n\nconst EXCLUDE_DIRS = new Set([\n 'node_modules',\n 'Library',\n 'Applications',\n 'Pictures',\n 'Music',\n 'Movies',\n 'go',\n '.Trash',\n]);\n\nconst MAX_DEPTH = 4;\nconst WORKTREE_DISCOVERY_CONCURRENCY = 8;\n/**\n * Lowered from 16 → 4 (#4516): 16 concurrent stat+readFile calls per walk\n * saturated the shared libuv thread pool (default 4 workers), starving the\n * main thread's I/O and causing 10–35s event-loop stalls on the fleet.\n *\n * Budget math: worst-case (all paths time out) → ceil(N/4) × GIT_PROBE_TIMEOUT_MS.\n * At 150 repos: ceil(150/4) × 3_000ms = 114s. The full-detect worker timeout is\n * CAPABILITY_DETECTOR_FULL_WORKER_TIMEOUT_MS (105s), so the absolute worst case\n * (every single path hitting NFS timeout) would exceed the budget. In practice\n * only a handful of paths ever time out (dead mounts, not all repos), so the\n * effective walk time stays well under 30s. Raising to 6 would lower worst-case\n * to 75s if needed.\n */\nconst REPO_METADATA_CONCURRENCY = 4;\n/**\n * Per-level concurrency cap for the recursive readdir fan-out in\n * `findGitRepos`. Replaces the previous unbounded `Promise.all(promises)`,\n * which on a machine with 150+ repos could create hundreds of concurrent\n * readdir callbacks all landing on the event loop within milliseconds of\n * each other, effectively blocking it for the duration.\n */\nexport const FIND_GIT_REPOS_DIR_CONCURRENCY = 8;\n\nasync function mapLimit<T, R>(\n items: readonly T[],\n limit: number,\n mapper: (item: T, index: number) => Promise<R>\n): Promise<R[]> {\n const results = new Array<R>(items.length);\n const indexedItems = items.map((item, index) => ({ item, index }));\n let nextIndex = 0;\n const workerCount = Math.min(Math.max(1, limit), items.length);\n\n await Promise.all(\n Array.from({ length: workerCount }, async () => {\n for (;;) {\n const next = indexedItems[nextIndex];\n nextIndex += 1;\n if (!next) return;\n results[next.index] = await mapper(next.item, next.index);\n }\n })\n );\n\n return results;\n}\n\nexport async function findGitRepos(dir: string, depth = 0): Promise<string[]> {\n if (depth > MAX_DEPTH) return [];\n\n /**\n * Yield to the event loop between depth levels (#4516). Without this, a\n * single walk issues hundreds of readdir callbacks in rapid succession —\n * all queued on the JS event loop simultaneously — which can delay timer\n * callbacks (including the 100ms event-loop watchdog) for several seconds.\n */\n await new Promise<void>((r) => {\n setImmediate(r);\n });\n\n try {\n const entries = await readdir(dir, { withFileTypes: true });\n\n for (const entry of entries) {\n if (entry.name === '.git') {\n return [dir];\n }\n }\n\n if (depth >= MAX_DEPTH) return [];\n\n const subdirs: string[] = [];\n for (const entry of entries) {\n if (!entry.isDirectory()) continue;\n if (entry.name.startsWith('.')) continue;\n if (EXCLUDE_DIRS.has(entry.name)) continue;\n subdirs.push(join(dir, entry.name));\n }\n\n /**\n * Bounded fan-out replaces the previous unbounded `Promise.all(promises)`\n * (#4516). On a machine with 150+ repos at depth 4, the old approach could\n * spawn 1000s of concurrent readdir calls, all landing on the event loop\n * within milliseconds and saturating the shared libuv thread pool.\n */\n const results = await mapLimit(subdirs, FIND_GIT_REPOS_DIR_CONCURRENCY, (subdir) =>\n findGitRepos(subdir, depth + 1)\n );\n return results.flat();\n } catch {\n return [];\n }\n}\n\n/**\n * Read the current branch name by parsing `.git/HEAD` directly.\n *\n * Format of `.git/HEAD`:\n * - Checked-out branch: `ref: refs/heads/<branch>\\n`\n * - Detached HEAD: `<sha>\\n`\n *\n * First attempts to read `.git/HEAD` directly (fast path for normal repos).\n * Falls back to `resolveHeadPath` for linked worktrees where `.git` is a\n * file pointing to a gitdir rather than a directory.\n */\nasync function readBranchFromHead(repoPath: string): Promise<string> {\n const directHeadPath = join(repoPath, '.git', 'HEAD');\n try {\n const direct = await readFile(directHeadPath, 'utf8');\n const trimmed = direct.trim();\n if (trimmed.startsWith('ref: refs/heads/')) {\n return trimmed.slice('ref: refs/heads/'.length);\n }\n if (!trimmed.startsWith('gitdir:')) {\n return 'HEAD';\n }\n } catch {}\n\n try {\n const headPath = await resolveHeadPath(repoPath);\n if (!headPath) return 'HEAD';\n const contents = await readFile(headPath, 'utf8');\n const trimmed2 = contents.trim();\n if (trimmed2.startsWith('ref: refs/heads/')) {\n return trimmed2.slice('ref: refs/heads/'.length);\n }\n return 'HEAD';\n } catch {\n return 'HEAD';\n }\n}\n\nexport async function getRepoMetadata(\n repoPath: string,\n cached?: CacheEntry\n): Promise<GitRepoInfo | null> {\n try {\n if (cached) {\n return {\n path: repoPath,\n name: basename(repoPath),\n branch: cached.branch,\n ...(cached.remote !== null && { remote: cached.remote }),\n };\n }\n\n /**\n * Both reads are bounded by `GIT_PROBE_TIMEOUT_MS`. `gitExecSafe`\n * already has the 5s `OneShotPool` TTL but no hard kill — a hung\n * `git remote get-url` would pin the walker indefinitely without\n * this race. `readBranchFromHead` is plain `fs.readFile` and is\n * usually fast, but the timeout is cheap insurance against an NFS\n * stall on the HEAD file itself.\n */\n const [branchResult, remoteResult] = await Promise.all([\n withProbeTimeout(readBranchFromHead(repoPath), GIT_PROBE_TIMEOUT_MS),\n withProbeTimeout(\n gitExecSafe(repoPath, ['remote', 'get-url', 'origin']),\n GIT_PROBE_TIMEOUT_MS\n ),\n ]);\n const branch = branchResult ?? 'HEAD';\n const remote = remoteResult ? remoteResult || undefined : undefined;\n\n return {\n path: repoPath,\n name: basename(repoPath),\n branch,\n ...(remote && { remote }),\n };\n } catch {\n return null;\n }\n}\n\n/**\n * Process a single repo path during the environments walk: resolve cache hit,\n * run the timed metadata probe, update `nextCache`, and emit slow/timeout logs.\n * Extracted from the `mapLimit` callback in `detectEnvironments` to keep that\n * function under the cognitive-complexity ceiling.\n */\nasync function processRepoPaths(\n path: string,\n idx: number,\n fingerprints: GitCacheFingerprint[],\n prevCache: Map<string, CacheEntry>,\n nextCache: Map<string, CacheEntry>\n): Promise<GitRepoInfo | null> {\n const currentFingerprint = fingerprints[idx] ?? { headMtimeMs: null, configMtimeMs: null };\n const prev = prevCache.get(path);\n const cacheHit =\n prev !== undefined &&\n currentFingerprint.headMtimeMs !== null &&\n isCacheValid(prev, currentFingerprint);\n\n const start = Date.now();\n const info = await withProbeTimeout(\n getRepoMetadata(path, cacheHit ? prev : undefined),\n GIT_PROBE_TIMEOUT_MS\n );\n const durationMs = Date.now() - start;\n\n if (info === null && durationMs >= GIT_PROBE_TIMEOUT_MS * 0.9) {\n logger.warn(\n { event: 'environments_walk_path_timed_out', path, durationMs },\n 'environments walk skipped path past per-path timeout'\n );\n } else if (durationMs >= SLOW_PATH_LOG_THRESHOLD_MS) {\n logger.warn(\n { event: 'environments_walk_path_slow', path, durationMs },\n 'environments walk path resolved slowly'\n );\n }\n\n if (info !== null && currentFingerprint.headMtimeMs !== null) {\n nextCache.set(path, {\n path,\n branch: info.branch,\n remote: info.remote ?? null,\n headMtimeMs: currentFingerprint.headMtimeMs,\n configMtimeMs: currentFingerprint.configMtimeMs,\n cachedAt: cacheHit && prev ? prev.cachedAt : Date.now(),\n });\n }\n return info;\n}\n\n/**\n * Module-level single-flight for `detectEnvironments` (#4516).\n *\n * Deduplicates concurrent callers within the SAME JS context (same V8 isolate\n * — e.g. same worker thread or the main thread). When N callers call\n * `detectEnvironments()` before the current walk resolves, they all receive\n * the same promise rather than starting N independent walks.\n *\n * SCOPE: This covers same-context re-entrancy only. Full-detect workers are\n * spawned fresh per call (each isolate starts with `inFlightDetect = null`),\n * so cross-worker concurrency is prevented separately — see the backstop gate\n * on `daemon.capabilityDetectionReady` in serve.ts, which ensures the initial\n * detection worker completes before the first backstop tick fires.\n *\n * The first caller's `lastKnown` wins for the dedup window; subsequent callers\n * receive the same resolved value. `lastKnown` is a fallback for transient-\n * empty walks only, so this is safe — the walk result is authoritative.\n */\nlet inFlightDetect: Promise<GitRepoInfo[]> | null = null;\n\n/**\n * Detect git environments under $HOME.\n *\n * `lastKnown` preserves the previously-detected environment list when the\n * underlying `findGitRepos` walk returns `[]` for a non-authoritative reason\n * (root readdir failure: EMFILE, EACCES, EBUSY). Without this, a transient\n * filesystem hiccup in the 30s capability-refresh tick collapses environments\n * to `[]`, which `applyFreshCapabilities` writes wholesale, and the browser\n * briefly renders \"No environment\" until the next successful tick. Mirror of\n * the `lastKnownAgents` pattern in `agents.ts`.\n *\n * Discrimination: a successful walk that genuinely finds zero repos is\n * indistinguishable from a swallowed-error walk in the current shape (the\n * inner `try/catch` in `findGitRepos` returns `[]` on readdir failure). The\n * `lastKnown.length > 0` guard means we only preserve when there's something\n * worth preserving; a clean machine that genuinely has zero repos still gets\n * `[]`.\n */\nexport function detectEnvironments(lastKnown?: GitRepoInfo[]): Promise<GitRepoInfo[]> {\n if (inFlightDetect) return inFlightDetect;\n const promise = runDetectEnvironments(lastKnown);\n inFlightDetect = promise;\n /**\n * Swallow the rejection on the stored ref so the module-level holder never\n * becomes a tracked-but-unhandled rejected promise. Callers receive the real\n * rejection via the `promise` reference returned to them.\n */\n promise\n .catch(() => {})\n .finally(() => {\n if (inFlightDetect === promise) inFlightDetect = null;\n });\n return promise;\n}\n\nasync function runDetectEnvironments(lastKnown?: GitRepoInfo[]): Promise<GitRepoInfo[]> {\n const { homedir } = await import('node:os');\n\n /**\n * The repo-paths cache (`repo-paths-cache.ts`) holds the result of the\n * `findGitRepos(homedir())` walk and the N parallel `git worktree list`\n * spawns — together ~12-15s on a busy machine. Per-repo branch info is\n * still resolved fresh below via `getRepoMetadata` (cached separately by\n * git HEAD + config mtimes in `environments-cache.ts`). The list cache lives\n * for the daemon lifetime; `invalidateRepoPaths()` is called by\n * worktree-service (add/remove), capability-watcher (FSEvents rescan),\n * and explicit force-refresh paths.\n */\n const cachedPaths = getCachedRepoPaths();\n let repoPaths: string[];\n let worktreePaths: string[];\n\n if (cachedPaths) {\n repoPaths = cachedPaths.repos;\n worktreePaths = cachedPaths.worktrees;\n } else {\n try {\n repoPaths = await findGitRepos(homedir());\n } catch (err) {\n if (lastKnown && lastKnown.length > 0) {\n const { logger } = await import('../logger.js');\n logger.warn(\n { err, lastKnownCount: lastKnown.length },\n 'detectEnvironments findGitRepos threw — preserving lastKnown'\n );\n return lastKnown;\n }\n return [];\n }\n\n if (repoPaths.length === 0 && lastKnown && lastKnown.length > 0) {\n /**\n * Walk returned empty but we know there used to be repos — almost\n * certainly a transient root-readdir error swallowed inside\n * `findGitRepos`. Preserve rather than wipe.\n */\n const { logger } = await import('../logger.js');\n logger.warn(\n { lastKnownCount: lastKnown.length },\n 'detectEnvironments walk empty but lastKnown non-empty — preserving lastKnown'\n );\n return lastKnown;\n }\n\n worktreePaths = await discoverWorktrees(repoPaths);\n setCachedRepoPaths(repoPaths, worktreePaths);\n }\n\n const allPaths = [...new Set([...repoPaths, ...worktreePaths])];\n\n const prevCache = await loadCache();\n const fingerprints = await mapLimit(allPaths, REPO_METADATA_CONCURRENCY, getGitCacheFingerprint);\n const nextCache = new Map<string, CacheEntry>();\n\n /**\n * Per-path timing + hard timeout. Walk continues past timed-out paths so\n * one bad mount (NFS, dead symlink, removed volume) can't pin the entire\n * environments slice. Slow paths log a structured event for diagnosis.\n */\n const resolved = await mapLimit(allPaths, REPO_METADATA_CONCURRENCY, (path, idx) =>\n processRepoPaths(path, idx, fingerprints, prevCache, nextCache)\n );\n\n await saveCache(nextCache);\n\n return resolved.filter((info): info is GitRepoInfo => info !== null);\n}\n\n/**\n * For each repo, run `git worktree list --porcelain` to discover\n * linked worktrees (which may live in dot-directories like .claude/worktrees/).\n *\n * Dedupes across repos: linked worktrees that are themselves git repos get\n * walked AGAIN by `findGitRepos`, and running `git worktree list` from each\n * one reports the same set of paths back. Without dedupe the cache grew to\n * 7,420 entries on a machine with only 149 unique worktrees (2026-05-14\n * boot wedge investigation). `dedupePerRepoWorktreeOutputs` collapses to\n * unique paths while preserving the first-seen ordering for determinism.\n */\nexport async function discoverWorktrees(repoPaths: string[]): Promise<string[]> {\n const results = await mapLimit(\n repoPaths,\n WORKTREE_DISCOVERY_CONCURRENCY,\n async (repoPath): Promise<PromiseSettledResult<string[]>> => {\n try {\n return { status: 'fulfilled', value: await listLinkedWorktrees(repoPath) };\n } catch (reason) {\n return { status: 'rejected', reason };\n }\n }\n );\n return dedupePerRepoWorktreeOutputs(results);\n}\n\nfunction dedupePerRepoWorktreeOutputs(results: PromiseSettledResult<string[]>[]): string[] {\n const seen = new Set<string>();\n const out: string[] = [];\n for (const r of results) {\n if (r.status !== 'fulfilled') continue;\n for (const wt of r.value) {\n if (seen.has(wt)) continue;\n seen.add(wt);\n out.push(wt);\n }\n }\n return out;\n}\n\n/**\n * Enumerate linked worktrees for a git repository by reading the\n * `.git/worktrees/` directory directly rather than spawning a git subprocess.\n *\n * Git's on-disk format (gitrepository-layout(5), stable since git 2.5):\n * .git/worktrees/<name>/gitdir — contains the path to the worktree's\n * `.git` file, e.g. `/path/to/wt/.git`\n *\n * Stripping `/.git` from that content yields the worktree path. Missing\n * `.git/worktrees/` directory means no linked worktrees — return [].\n * Orphaned entries (gitdir file missing/unreadable) are silently skipped.\n */\nasync function listLinkedWorktrees(repoPath: string): Promise<string[]> {\n const gitWorktreesDir = join(repoPath, '.git', 'worktrees');\n let entries: string[];\n try {\n const result = await withProbeTimeout(readdir(gitWorktreesDir), GIT_PROBE_TIMEOUT_MS);\n if (result === null) return [];\n entries = result;\n } catch {\n return [];\n }\n\n /**\n * Bounded fan-out over gitdir files (#4516). A repo with many linked\n * worktrees would otherwise fan out N concurrent readFile calls, the same\n * class of issue as the unbounded `findGitRepos` fan-out. Each per-name op\n * is also timeout-guarded to match the `processRepoPaths` pattern.\n */\n const paths = await mapLimit(entries, FIND_GIT_REPOS_DIR_CONCURRENCY, async (name) => {\n const result = await withProbeTimeout(\n (async () => {\n const gitdirFile = join(gitWorktreesDir, name, 'gitdir');\n const content = await readFile(gitdirFile, 'utf8');\n const wtGitPath = content.trim();\n return wtGitPath.endsWith('/.git') ? wtGitPath.slice(0, -'/.git'.length) : null;\n })(),\n GIT_PROBE_TIMEOUT_MS\n );\n return result ?? null;\n });\n\n return paths.filter((p): p is string => p !== null && p !== repoPath);\n}\n\n/** @internal kept for tests that exercise parse logic on porcelain output */\nfunction parseWorktreeListOutput(output: string, repoPath: string): string[] {\n const paths: string[] = [];\n for (const line of output.split('\\n')) {\n if (line.startsWith('worktree ')) {\n const wt = line.slice('worktree '.length).trim();\n if (wt && wt !== repoPath) paths.push(wt);\n }\n }\n return paths;\n}\n\nexport const _testing = {\n parseWorktreeListOutput,\n dedupePerRepoWorktreeOutputs,\n listLinkedWorktrees,\n readBranchFromHead,\n mapLimit,\n withProbeTimeout,\n GIT_PROBE_TIMEOUT_MS,\n SLOW_PATH_LOG_THRESHOLD_MS,\n NEGATIVE_CACHE_TTL_MS,\n FIND_GIT_REPOS_DIR_CONCURRENCY,\n readGitRepoCache,\n resetGitRepoCache: () => {\n gitRepoCache.clear();\n },\n resetGhAvailableCache: () => {\n ghAvailableCache = false;\n },\n resetInFlightDetect: () => {\n inFlightDetect = null;\n },\n};\n","import { mkdir, readFile, rename, stat, unlink, writeFile } from 'node:fs/promises';\nimport { dirname, isAbsolute, join, resolve } from 'node:path';\nimport { z } from 'zod';\nimport { getShipyardHome } from '../env.js';\nimport { isEnoent } from '../fs-utils.js';\nimport { logger } from '../logger.js';\n\nconst CacheEntrySchema = z.object({\n path: z.string(),\n branch: z.string(),\n remote: z.string().nullable(),\n headMtimeMs: z.number(),\n /**\n * Added after the original cache shape. Optional so old disk caches parse,\n * but `isCacheValid` treats missing as invalid and refreshes once.\n */\n configMtimeMs: z.number().nullable().optional(),\n cachedAt: z.number(),\n});\n\nconst CacheFileSchema = z.object({\n entries: z.record(z.string(), CacheEntrySchema),\n});\n\nexport type CacheEntry = z.infer<typeof CacheEntrySchema>;\ntype CacheFile = z.infer<typeof CacheFileSchema>;\nexport interface GitCacheFingerprint {\n headMtimeMs: number | null;\n configMtimeMs: number | null;\n}\n\nfunction cacheFilePath(): string {\n return join(getShipyardHome(), 'data', 'environments-cache.json');\n}\n\nlet cacheMigrationDone = false;\n\nexport async function loadCache(): Promise<Map<string, CacheEntry>> {\n const filePath = cacheFilePath();\n try {\n const raw = await readFile(filePath, 'utf8');\n const parsed: unknown = JSON.parse(raw);\n const result = CacheFileSchema.safeParse(parsed);\n if (!result.success) {\n logger.debug({ path: filePath }, 'environments-cache: malformed, treating as empty');\n cacheMigrationDone = true;\n return new Map();\n }\n const cache = new Map(Object.entries(result.data.entries));\n if (!cacheMigrationDone) {\n cacheMigrationDone = true;\n await migrateConfigMtimes(cache);\n }\n return cache;\n } catch (err) {\n if (isEnoent(err)) {\n cacheMigrationDone = true;\n return new Map();\n }\n logger.debug({ err }, 'environments-cache: read failed, treating as empty');\n return new Map();\n }\n}\n\n/**\n * One-shot migration: entries written before #3619 lack `configMtimeMs`.\n * `isCacheValid` would force a re-spawn for every such entry, creating a\n * git subprocess storm on first boot after upgrade. Backfill the field by\n * reading `.git/config` mtimes directly, then persist so subsequent boots\n * hit the cache normally.\n */\nasync function migrateConfigMtimes(cache: Map<string, CacheEntry>): Promise<void> {\n const stale = [...cache.values()].filter((e) => e.configMtimeMs === undefined);\n if (stale.length === 0) return;\n await Promise.all(\n stale.map(async (entry) => {\n const fp = await getGitCacheFingerprint(entry.path);\n cache.set(entry.path, { ...entry, configMtimeMs: fp.configMtimeMs });\n })\n );\n logger.info({ count: stale.length }, 'environments-cache: cache_migration_seeded');\n await saveCache(cache);\n}\n\n/**\n * Resolve git metadata paths for a repository or linked worktree.\n *\n * - If `<path>/.git` is a directory, metadata lives inside that directory.\n * - If `<path>/.git` is a file, it contains `gitdir: <path>`. HEAD lives in\n * that worktree gitdir, while remotes live in the common git dir's config.\n * - Returns null on any error (missing, unreadable, malformed).\n */\nasync function readGitdirFile(dotGitPath: string): Promise<string | null> {\n const contents = await readFile(dotGitPath, 'utf8');\n const match = contents.match(/^gitdir:\\s*(.+)\\s*$/m);\n if (!match?.[1]) return null;\n const gitdirRaw = match[1].trim();\n return isAbsolute(gitdirRaw) ? gitdirRaw : resolve(dirname(dotGitPath), gitdirRaw);\n}\n\nasync function resolveCommonGitDir(gitdir: string): Promise<string> {\n try {\n const raw = await readFile(join(gitdir, 'commondir'), 'utf8');\n const commonDirRaw = raw.trim();\n if (commonDirRaw === '') return gitdir;\n return isAbsolute(commonDirRaw) ? commonDirRaw : resolve(gitdir, commonDirRaw);\n } catch {\n return gitdir;\n }\n}\n\nexport async function resolveHeadPath(worktreePath: string): Promise<string | null> {\n const paths = await resolveGitMetadataPaths(worktreePath);\n return paths?.headPath ?? null;\n}\n\nexport async function resolveGitMetadataPaths(\n worktreePath: string\n): Promise<{ headPath: string; configPath: string | null } | null> {\n const dotGitPath = join(worktreePath, '.git');\n try {\n const dotGitStat = await stat(dotGitPath);\n if (dotGitStat.isDirectory()) {\n return { headPath: join(dotGitPath, 'HEAD'), configPath: join(dotGitPath, 'config') };\n }\n if (dotGitStat.isFile()) {\n const gitdir = await readGitdirFile(dotGitPath);\n if (!gitdir) return null;\n const commonGitDir = await resolveCommonGitDir(gitdir);\n return { headPath: join(gitdir, 'HEAD'), configPath: join(commonGitDir, 'config') };\n }\n return null;\n } catch {\n return null;\n }\n}\n\nasync function getPathMtime(path: string | null): Promise<number | null> {\n if (!path) return null;\n try {\n const pathStat = await stat(path);\n return pathStat.mtimeMs;\n } catch {\n return null;\n }\n}\n\nexport async function getGitCacheFingerprint(worktreePath: string): Promise<GitCacheFingerprint> {\n const paths = await resolveGitMetadataPaths(worktreePath);\n if (!paths) return { headMtimeMs: null, configMtimeMs: null };\n const [headMtimeMs, configMtimeMs] = await Promise.all([\n getPathMtime(paths.headPath),\n getPathMtime(paths.configPath),\n ]);\n return { headMtimeMs, configMtimeMs };\n}\n\nexport function isCacheValid(entry: CacheEntry, current: GitCacheFingerprint): boolean {\n /**\n * `.git/HEAD` invalidates branch, and `.git/config` invalidates remote.\n * Entries without `configMtimeMs` are soft-migrated at `loadCache` time\n * (see `migrateConfigMtimes`), so by the time this is called the field\n * is always present. Treat `undefined` the same as `null` for safety.\n */\n return (\n entry.headMtimeMs === current.headMtimeMs &&\n (entry.configMtimeMs ?? null) === current.configMtimeMs\n );\n}\n\nexport async function saveCache(entries: Map<string, CacheEntry>): Promise<void> {\n const filePath = cacheFilePath();\n const tmpPath = `${filePath}.tmp`;\n try {\n await mkdir(dirname(filePath), { recursive: true });\n /**\n * Merge-on-save: re-read disk immediately before writing so a concurrent\n * saveCache call's entries are preserved rather than erased (new entries\n * win for paths present in both — they are fresher).\n */\n const current = await loadCache();\n for (const [path, entry] of entries) {\n current.set(path, entry);\n }\n const payload: CacheFile = { entries: Object.fromEntries(current) };\n await writeFile(tmpPath, JSON.stringify(payload), 'utf8');\n await rename(tmpPath, filePath);\n } catch (err) {\n logger.debug({ err, filePath }, 'environments-cache: save failed');\n void unlink(tmpPath).catch(() => {});\n }\n}\n","/**\n * In-memory cache of the list of git repo paths under $HOME and the linked\n * worktree paths discovered via `git worktree list --porcelain` for each.\n *\n * Distinct from `environments-cache.ts` (which caches per-repo branch/remote\n * info on disk, keyed by git HEAD + config mtimes). This one caches the LIST of repos and\n * worktrees — i.e. the `findGitRepos(homedir())` walk and the N parallel\n * `git worktree list` spawns. Those are the ~12-50s cost on a busy machine.\n *\n * Lifetime: persisted to disk at `~/.shipyard/data/repo-paths-cache.json`\n * and rehydrated on every daemon boot. The previous implementation rejected\n * the disk cache outright when its age exceeded 1h — which is almost every\n * reboot in practice — and forced the 30s `findGitRepos($HOME)` walk to fire\n * on each cold boot. We now serve any structurally-valid cache regardless of\n * age and kick off a background re-walk when the cache is older than 5\n * minutes. Boot time drops from 30-50s to milliseconds; freshness is\n * restored ~10-30s after boot when the background walk completes.\n *\n * Invalidation:\n * - `invalidateRepoPaths()` — called by:\n * * capability-watcher.ts (FSEvents rescan + escalation paths)\n * * explicit force-refresh control message\n * - `addWorktreeToCache()` / `removeWorktreeFromCache()` — incremental\n * update used by worktree create/remove. Avoids the cold-walk recompute\n * storm that otherwise stalls the event loop 12-15s and kills the\n * daemon's WebRTC + control channels mid-creation. If there is no\n * active cache (boot before first walk), the call is a no-op — the\n * next cold walk picks up the new state.\n *\n * The walk + worktree spawns ARE NOT triggered by the capability-watcher's\n * normal events: scoped re-detect (`refreshMcpServers` etc.) skips\n * `detectEnvironments` entirely and reuses the existing `daemon.capabilities\n * ?.environments`. The cache is only consulted by full `detectCapabilities`\n * runs (manual Refresh button, boot, cwd-changed).\n */\n\nimport { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';\nimport { dirname, join } from 'node:path';\nimport { z } from 'zod';\nimport { getShipyardHome } from '../env.js';\nimport { isEnoent } from '../fs-utils.js';\nimport type { createChildLogger } from '../logger.js';\nimport { logger } from '../logger.js';\n\nexport {\n getCachedRepoPaths,\n hydrateAndLogRepoPathsCache,\n hydrateRepoPathsCacheFromDisk,\n setCachedRepoPaths,\n invalidateRepoPaths,\n addWorktreeToCache,\n removeWorktreeFromCache,\n};\n\nexport type { HydrateResult };\n\ninterface CachedEntry {\n repos: string[];\n worktrees: string[];\n populatedAt: number;\n}\n\ninterface HydrateResult {\n hit: boolean;\n stale: boolean;\n}\n\nconst RepoPathsDiskSchema = z.object({\n repos: z.array(z.string()),\n worktrees: z.array(z.string()),\n populatedAt: z.number(),\n});\n\nconst STALE_AFTER_MS_DEFAULT = 5 * 60 * 1000;\n\nlet cache: CachedEntry | null = null;\n/**\n * Monotonic counter incremented on every mutating call. Async disk operations\n * capture the generation at the moment they were scheduled and no-op when\n * the generation has changed by the time they get to the syscall. Without\n * this, `invalidateRepoPaths()` (which fires `void unlinkDisk()`) racing\n * with a subsequent `setCachedRepoPaths()` (which fires `void persistDisk()`)\n * could leave the disk in either state — and if `unlinkDisk` wins after\n * `persistDisk`'s rename succeeds, the next daemon restart pays the full\n * cold-walk cost despite a valid cache having just been written.\n */\nlet cacheGeneration = 0;\nlet backgroundRefreshInFlight = false;\n\nfunction cacheFilePath(): string {\n return join(getShipyardHome(), 'data', 'repo-paths-cache.json');\n}\n\nfunction getCachedRepoPaths(): { repos: string[]; worktrees: string[] } | null {\n if (!cache) return null;\n return { repos: cache.repos, worktrees: cache.worktrees };\n}\n\nfunction setCachedRepoPaths(repos: string[], worktrees: string[], now: number = Date.now()): void {\n cache = { repos, worktrees, populatedAt: now };\n cacheGeneration += 1;\n void persistDisk(cache, cacheGeneration);\n}\n\nfunction invalidateRepoPaths(): void {\n cache = null;\n cacheGeneration += 1;\n void unlinkDisk(cacheGeneration);\n}\n\n/**\n * Best-effort hydrate the in-memory cache from disk on daemon boot. Always\n * accepts a structurally-valid on-disk entry regardless of age — the boot\n * path needs the data immediately and any staleness is repaired by a\n * background re-walk scheduled by `hydrateAndLogRepoPathsCache`. Returns\n * `{ hit, stale }` so the caller knows whether to schedule that refresh.\n *\n * The `staleAfterMs` threshold defines when the cache is \"old enough to\n * re-walk in the background\" — not when it's \"too old to serve\". Default\n * 5 minutes; data older than that triggers a background refresh but is\n * still returned to the boot path so `detect_environments` sees a populated\n * cache and skips the 30s `findGitRepos` walk + `git worktree list` storm.\n *\n * Wired from `serve.ts` BEFORE `createSignalingHandle()` so the first\n * `detectEnvironments` call inside the background `runCapabilityDetection`\n * (#3360 — detection moved off the boot path) sees the populated in-memory\n * cache. Without this, every daemon restart pays the full cold-walk cost —\n * observed at 30-50s on busy machines and the dominant component of 42.5s\n * worst-case boot.\n */\nasync function hydrateRepoPathsCacheFromDisk(opts?: {\n staleAfterMs?: number;\n}): Promise<HydrateResult> {\n if (cache !== null) return { hit: false, stale: false };\n const filePath = cacheFilePath();\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf8');\n } catch (err: unknown) {\n if (isEnoent(err)) return { hit: false, stale: false };\n logger.debug({ err, filePath }, 'repo-paths-cache: read failed');\n return { hit: false, stale: false };\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n return { hit: false, stale: false };\n }\n const result = RepoPathsDiskSchema.safeParse(parsed);\n if (!result.success) return { hit: false, stale: false };\n\n /**\n * Race-safe: another caller may have populated the slot between our\n * `cache !== null` check and the disk read. Don't clobber the live entry.\n */\n if (cache !== null) return { hit: false, stale: false };\n\n const staleAfterMs = opts?.staleAfterMs ?? STALE_AFTER_MS_DEFAULT;\n const ageMs = Math.max(0, Date.now() - result.data.populatedAt);\n const stale = ageMs > staleAfterMs;\n\n cache = result.data;\n logger.info(\n { repos: result.data.repos.length, worktrees: result.data.worktrees.length, ageMs, stale },\n 'repo-paths-cache: hydrated from disk'\n );\n return { hit: true, stale };\n}\n\nasync function persistDisk(entry: CachedEntry, generation: number): Promise<void> {\n const filePath = cacheFilePath();\n const tmpPath = `${filePath}.tmp`;\n try {\n await mkdir(dirname(filePath), { recursive: true });\n await writeFile(tmpPath, JSON.stringify(entry), 'utf8');\n /**\n * Drop the write if a later mutation (set/invalidate/add/remove) raced\n * us. The successor's persist/unlink will reach the rename target with\n * the right state. Without this, an in-flight rename can clobber the\n * successor's just-written file or, worse, race a fresh persist after\n * an invalidate.\n */\n if (generation !== cacheGeneration) {\n await unlink(tmpPath).catch(() => {});\n return;\n }\n await rename(tmpPath, filePath);\n } catch (err: unknown) {\n logger.debug({ err, filePath }, 'repo-paths-cache: save failed');\n void unlink(tmpPath).catch(() => {});\n }\n}\n\nasync function unlinkDisk(generation: number): Promise<void> {\n /**\n * Drop the unlink if a later `setCachedRepoPaths` already raced us — its\n * `persistDisk` will write the fresh file and the file the unlink would\n * have removed is the predecessor's. Without this generation check, an\n * `invalidate → set` sequence can end up with NO disk file even though\n * `set` persisted one.\n */\n if (generation !== cacheGeneration) return;\n try {\n await unlink(cacheFilePath());\n } catch {\n /** No-op — file may not exist yet. */\n }\n}\n\n/**\n * Append a freshly-created worktree to the existing cached result. Source\n * repo path is also added (deduped) because a brand-new repo can host a\n * brand-new worktree, and the next `detectEnvironments` call needs to see\n * both. No-op when there is no cache yet — the next cold walk will discover\n * the new entries naturally.\n */\nfunction addWorktreeToCache(worktreePath: string, sourceRepoPath: string): void {\n if (!cache) return;\n const repos = cache.repos.includes(sourceRepoPath)\n ? cache.repos\n : [...cache.repos, sourceRepoPath];\n const worktrees = cache.worktrees.includes(worktreePath)\n ? cache.worktrees\n : [...cache.worktrees, worktreePath];\n cache = { repos, worktrees, populatedAt: cache.populatedAt };\n cacheGeneration += 1;\n void persistDisk(cache, cacheGeneration);\n}\n\n/**\n * Splice a worktree path out of the cached worktree list. The source repo\n * stays — removing a single linked worktree never deletes its source. No-op\n * when there is no cache yet.\n */\nfunction removeWorktreeFromCache(worktreePath: string): void {\n if (!cache) return;\n const next = cache.worktrees.filter((p) => p !== worktreePath);\n if (next.length === cache.worktrees.length) return;\n cache = { repos: cache.repos, worktrees: next, populatedAt: cache.populatedAt };\n cacheGeneration += 1;\n void persistDisk(cache, cacheGeneration);\n}\n\n/**\n * Re-walk `$HOME` and rewrite the cache. Fired by `hydrateAndLogRepoPathsCache`\n * on boot when the hydrated cache is stale, restoring freshness ~10-30s after\n * the user is already interactive. Single-flight: a second call while one is\n * in progress is a no-op. Generation-guarded: if any synchronous mutation\n * (`setCachedRepoPaths`, `addWorktreeToCache`, `removeWorktreeFromCache`,\n * `invalidateRepoPaths`) lands while the walk is in flight, the refresh\n * result is dropped so it does not clobber a more-recent incremental update.\n *\n * Lazy-imports `./git-repo.js` to break the import cycle (`git-repo` already\n * imports this module).\n */\nasync function refreshRepoPathsInBackground(\n log: ReturnType<typeof createChildLogger>\n): Promise<void> {\n if (backgroundRefreshInFlight) return;\n backgroundRefreshInFlight = true;\n const startGen = cacheGeneration;\n const start = Date.now();\n try {\n const { findGitRepos, discoverWorktrees } = await import('./git-repo.js');\n const { homedir } = await import('node:os');\n const repos = await findGitRepos(homedir());\n const worktrees = await discoverWorktrees(repos);\n if (cacheGeneration !== startGen) {\n log.debug(\n { event: 'repo_paths_cache_refresh_dropped' },\n 'repo-paths-cache: background refresh dropped (cache mutated mid-walk)'\n );\n return;\n }\n /**\n * Defense in depth: a transient root-readdir failure inside\n * `findGitRepos` returns `[]`, indistinguishable from \"no repos exist\".\n * If the prior cache had entries, preserve it rather than wipe — the\n * next refresh (capability-watcher tick) will retry. Mirrors the same\n * guard inside `detectEnvironments`.\n */\n if (repos.length === 0 && cache && cache.repos.length > 0) {\n log.warn(\n {\n event: 'repo_paths_cache_refresh_empty_preserved',\n prevRepos: cache.repos.length,\n },\n 'repo-paths-cache: background refresh returned empty; preserving prior cache'\n );\n return;\n }\n setCachedRepoPaths(repos, worktrees);\n log.info(\n {\n event: 'repo_paths_cache_refresh_complete',\n repos: repos.length,\n worktrees: worktrees.length,\n durationMs: Date.now() - start,\n },\n 'repo-paths-cache: background refresh complete'\n );\n } catch (err) {\n log.warn(\n { err, event: 'repo_paths_cache_refresh_failed', durationMs: Date.now() - start },\n 'repo-paths-cache: background refresh failed'\n );\n } finally {\n backgroundRefreshInFlight = false;\n }\n}\n\n/**\n * Hydrate from disk and log the hit/miss outcome. Boot callers wrap this in\n * `bootstrapPhase('repo_paths_cache_hydrate', ...)` — the wrapper always\n * records `outcome: 'success'` because hydrate returns a result object (no\n * throw on ENOENT). The explicit log here is what surfaces whether the\n * upcoming `detect_environments` will pay the cold walk and whether a\n * background refresh was scheduled.\n */\nasync function hydrateAndLogRepoPathsCache(\n log: ReturnType<typeof createChildLogger>\n): Promise<boolean> {\n const result = await hydrateRepoPathsCacheFromDisk();\n log.info(\n {\n event: 'repo_paths_cache_hydrate_result',\n hit: result.hit,\n stale: result.stale,\n },\n result.hit\n ? result.stale\n ? 'repo-paths cache hydrated from disk (stale; background refresh scheduled)'\n : 'repo-paths cache hydrated from disk (fresh)'\n : 'repo-paths cache cold (walk will fire inside detect_environments)'\n );\n if (result.hit && result.stale) {\n void refreshRepoPathsInBackground(log);\n }\n return result.hit;\n}\n\nexport const _testing = {\n reset: invalidateRepoPaths,\n inspect: () => cache,\n refreshRepoPathsInBackground,\n cacheFilePath,\n isBackgroundRefreshInFlight: () => backgroundRefreshInFlight,\n};\n","import { execFile } from 'node:child_process';\nimport { homedir } from 'node:os';\nimport { join } from 'node:path';\n\nexport const TIMEOUT_MS = 5_000;\n/**\n * Cap on `claude auth status --json` during capability detection. Healthy\n * CLI responds in well under 1s; 15s was a worst-case-CLI-cold-start\n * accommodation that singlehandedly added up to 15s of boot stall when the\n * subprocess hung. 5s is generous slack while keeping the bootstrap\n * `detectCapabilities → signaling register` path bounded. See issue #3267.\n */\nexport const AUTH_STATUS_TIMEOUT_MS = 5_000;\n\n/**\n * Build the version-manager and user-local binary dirs for macOS.\n * These paths are absent when the daemon is launched from Finder/Spotlight\n * because macOS GUI apps inherit a stripped PATH that skips the user's\n * shell profile (where nvm/volta/mise/asdf entries normally live).\n *\n * Listed from most commonly needed (nvm, volta) to least common (cargo).\n * All are idempotently filtered by augmentPath — already-present entries\n * are never duplicated.\n */\nfunction buildDarwinVersionManagerPaths(home: string): string[] {\n return [\n join(home, '.nvm', 'current', 'bin'),\n join(home, '.volta', 'bin'),\n join(home, '.local', 'bin'),\n join(home, '.cargo', 'bin'),\n '/opt/homebrew/opt/node/bin',\n ];\n}\n\nexport const STANDARD_PATHS_BY_PLATFORM: Partial<Record<NodeJS.Platform, readonly string[]>> = {\n darwin: [\n '/usr/bin',\n '/usr/local/bin',\n '/opt/homebrew/bin',\n ...buildDarwinVersionManagerPaths(homedir()),\n ],\n linux: ['/usr/bin', '/usr/local/bin'],\n};\n\n/**\n * Daemons launched from Finder/Spotlight on macOS get a stripped PATH that may\n * omit /usr/bin where git lives. Prepend standard locations idempotently so\n * `execFile('git', ...)` resolves the binary. User-customized PATH entries\n * stay preferred — we only prepend dirs that aren't already present.\n */\nexport function augmentPath(currentPath: string, platform: NodeJS.Platform): string {\n const standardPaths = STANDARD_PATHS_BY_PLATFORM[platform];\n if (!standardPaths) return currentPath;\n\n const existing = currentPath.length > 0 ? currentPath.split(':') : [];\n const existingSet = new Set(existing);\n const toPrepend = standardPaths.filter((p) => !existingSet.has(p));\n if (toPrepend.length === 0) return currentPath;\n return existing.length > 0 ? `${toPrepend.join(':')}:${currentPath}` : toPrepend.join(':');\n}\n\nconst augmentedPath = augmentPath(process.env.PATH ?? '', process.platform);\nif (augmentedPath.length > 0) {\n process.env.PATH = augmentedPath;\n}\n\nexport function run(\n command: string,\n args: string[],\n cwd?: string,\n timeoutMs: number = TIMEOUT_MS\n): Promise<string> {\n return new Promise((resolve, reject) => {\n execFile(command, args, { timeout: timeoutMs, cwd }, (error, stdout) => {\n if (error) {\n reject(error);\n return;\n }\n resolve(stdout.trim());\n });\n });\n}\n\nexport function runWithTimeout(\n command: string,\n args: string[],\n cwd: string,\n timeoutMs: number\n): Promise<string> {\n return new Promise((resolve, reject) => {\n execFile(\n command,\n args,\n { timeout: timeoutMs, cwd, maxBuffer: 10 * 1024 * 1024 },\n (error, stdout) => {\n if (error) {\n reject(error);\n return;\n }\n resolve(stdout.trim());\n }\n );\n });\n}\n\nexport const DIFF_TIMEOUT_MS = 15_000;\nexport const MAX_DIFF_SIZE = 500_000;\n\nexport function isMaxBufferError(err: unknown): boolean {\n return err instanceof Error && err.message.includes('maxBuffer');\n}\n\nexport const BUFFER_OVERFLOW_MSG = '... diff too large (exceeded buffer limit) ...\\n';\n\nexport function truncateDiff(result: string): string {\n if (result.length <= MAX_DIFF_SIZE) return result;\n const boundary = result.lastIndexOf('\\ndiff --git ', MAX_DIFF_SIZE);\n const cutoff = boundary > 0 ? boundary : MAX_DIFF_SIZE;\n return `${result.slice(0, cutoff)}\\n\\n... diff truncated (exceeds 500KB) ...\\n`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA,SAAS,SAAS,YAAAA,iBAAgB;AAClC,SAAS,UAAU,QAAAC,aAAY;;;ACD/B,SAAS,OAAO,UAAU,QAAQ,MAAM,QAAQ,iBAAiB;AACjE,SAAS,SAAS,YAAY,MAAM,eAAe;AAMnD,IAAM,mBAAmB,iBAAE,OAAO;AAAA,EAChC,MAAM,iBAAE,OAAO;AAAA,EACf,QAAQ,iBAAE,OAAO;AAAA,EACjB,QAAQ,iBAAE,OAAO,EAAE,SAAS;AAAA,EAC5B,aAAa,iBAAE,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA,EAKtB,eAAe,iBAAE,OAAO,EAAE,SAAS,EAAE,SAAS;AAAA,EAC9C,UAAU,iBAAE,OAAO;AACrB,CAAC;AAED,IAAM,kBAAkB,iBAAE,OAAO;AAAA,EAC/B,SAAS,iBAAE,OAAO,iBAAE,OAAO,GAAG,gBAAgB;AAChD,CAAC;AASD,SAAS,gBAAwB;AAC/B,SAAO,KAAK,gBAAgB,GAAG,QAAQ,yBAAyB;AAClE;AAEA,IAAI,qBAAqB;AAEzB,eAAsB,YAA8C;AAClE,QAAM,WAAW,cAAc;AAC/B,MAAI;AACF,UAAM,MAAM,MAAM,SAAS,UAAU,MAAM;AAC3C,UAAM,SAAkB,KAAK,MAAM,GAAG;AACtC,UAAM,SAAS,gBAAgB,UAAU,MAAM;AAC/C,QAAI,CAAC,OAAO,SAAS;AACnB,aAAO,MAAM,EAAE,MAAM,SAAS,GAAG,kD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B,MAAM;AAC3B,uBAAmB;AAAA,EACrB;AAAA,EACA,qBAAqB,MAAM;AACzB,qBAAiB;AAAA,EACnB;AACF;","names":["readFile","join","cache","mkdir","readFile","rename","unlink","writeFile","dirname","join","cacheFilePath","join","cacheFilePath","readFile","mkdir","dirname","writeFile","unlink","rename","findGitRepos","discoverWorktrees","homedir","join","resolve","resolve","join","readFile","homedir","logger"]}
@@ -1,24 +1,23 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  parseDaemonToRunner
4
- } from "./chunk-A2UK6TW2.js";
4
+ } from "./chunk-LZSMNUAI.js";
5
5
  import {
6
6
  ContentBlockSchema,
7
7
  HARNESS_SERVER_NAME,
8
8
  buildCursorUserPrompt
9
- } from "./chunk-YXPPZQBJ.js";
9
+ } from "./chunk-VL5RUCRF.js";
10
10
  import "./chunk-RMLQ5DRP.js";
11
11
  import "./chunk-EHQITHQX.js";
12
12
  import "./chunk-X3MULCV5.js";
13
- import "./chunk-QJP7JCIS.js";
14
- import "./chunk-Z37T5W6S.js";
13
+ import "./chunk-LRNGLC4V.js";
14
+ import "./chunk-P2HZDIN7.js";
15
15
  import "./chunk-CNR7O5YH.js";
16
16
  import "./chunk-2H7UOFLK.js";
17
17
 
18
18
  // src/services/session/cursor-runner.ts
19
19
  import {
20
- Agent,
21
- Cursor
20
+ Agent
22
21
  } from "@cursor/sdk";
23
22
 
24
23
  // src/shared/capabilities/runtime/cursor-ripgrep.ts
@@ -56,25 +55,7 @@ function resolveCursorBundledRipgrepPath(deps = {}) {
56
55
  }
57
56
 
58
57
  // src/services/session/cursor-init-error-formatter.ts
59
- function formatCursorInitError(input) {
60
- const { requestedModelId, originalError, availableModels, listError } = input;
61
- if (availableModels === null) {
62
- const listPart = listError ? ` (Cursor.models.list() also failed: ${listError})` : " (Cursor.models.list() unavailable)";
63
- return `Agent.create failed for model '${requestedModelId}'. ${originalError}${listPart}`;
64
- }
65
- if (availableModels.length === 0) {
66
- return `Agent.create failed for model '${requestedModelId}'. Cursor.models.list() returned no models. ${originalError}`;
67
- }
68
- const requested = requestedModelId.toLowerCase();
69
- const exactMatch = availableModels.find(
70
- (m) => m.id.toLowerCase() === requested || (m.aliases ?? []).some((a) => a.toLowerCase() === requested)
71
- );
72
- const idsList = availableModels.map((m) => m.id).join(", ");
73
- const aliasParts = availableModels.filter((m) => (m.aliases?.length ?? 0) > 0).map((m) => `${m.id}=[${(m.aliases ?? []).join("|")}]`);
74
- const aliasesList = aliasParts.length > 0 ? aliasParts.join(", ") : "(none)";
75
- const matchPart = exactMatch ? ` The id IS in the available set (${exactMatch.id}) \u2014 failure is unrelated to model resolution.` : ` The id is NOT in the available set \u2014 likely renamed or deprecated server-side.`;
76
- return `Agent.create failed for model '${requestedModelId}'.${matchPart} Available ids: ${idsList}. Aliases: ${aliasesList}. Original: ${originalError}`;
77
- }
58
+ import { Cursor } from "@cursor/sdk";
78
59
 
79
60
  // src/services/session/cursor-runner-error.ts
80
61
  function readStringField(rec, key) {
@@ -108,6 +89,76 @@ function serializeCursorRunnerError(err) {
108
89
  return { error: String(err), isRetryable: false };
109
90
  }
110
91
 
92
+ // src/services/session/cursor-init-error-formatter.ts
93
+ function formatCursorInitError(input) {
94
+ const { requestedModelId, originalError, availableModels, listError } = input;
95
+ if (availableModels === null) {
96
+ const listPart = listError ? ` (Cursor.models.list() also failed: ${listError})` : " (Cursor.models.list() unavailable)";
97
+ return `Agent.create failed for model '${requestedModelId}'. ${originalError}${listPart}`;
98
+ }
99
+ if (availableModels.length === 0) {
100
+ return `Agent.create failed for model '${requestedModelId}'. Cursor.models.list() returned no models. ${originalError}`;
101
+ }
102
+ const requested = requestedModelId.toLowerCase();
103
+ const exactMatch = availableModels.find(
104
+ (m) => m.id.toLowerCase() === requested || (m.aliases ?? []).some((a) => a.toLowerCase() === requested)
105
+ );
106
+ const idsList = availableModels.map((m) => m.id).join(", ");
107
+ const aliasParts = availableModels.filter((m) => (m.aliases?.length ?? 0) > 0).map((m) => `${m.id}=[${(m.aliases ?? []).join("|")}]`);
108
+ const aliasesList = aliasParts.length > 0 ? aliasParts.join(", ") : "(none)";
109
+ const matchPart = exactMatch ? ` The id IS in the available set (${exactMatch.id}) \u2014 failure is unrelated to model resolution.` : ` The id is NOT in the available set \u2014 likely renamed or deprecated server-side.`;
110
+ return `Agent.create failed for model '${requestedModelId}'.${matchPart} Available ids: ${idsList}. Aliases: ${aliasesList}. Original: ${originalError}`;
111
+ }
112
+ var INIT_MODEL_LIST_TIMEOUT_MS = 5e3;
113
+ async function sendInitErrorFromAgentCreateFailure(args) {
114
+ const originalError = args.err instanceof Error ? args.err.message : String(args.err);
115
+ let availableModels = null;
116
+ let listError;
117
+ let timeoutHandle;
118
+ try {
119
+ const listPromise = Cursor.models.list({ apiKey: args.apiKey });
120
+ const timeoutPromise = new Promise((_, reject) => {
121
+ timeoutHandle = setTimeout(
122
+ () => reject(new Error(`timed out after ${INIT_MODEL_LIST_TIMEOUT_MS}ms`)),
123
+ INIT_MODEL_LIST_TIMEOUT_MS
124
+ );
125
+ });
126
+ const models = await Promise.race([listPromise, timeoutPromise]);
127
+ availableModels = models.map((m) => ({ id: m.id, aliases: m.aliases }));
128
+ } catch (listErr) {
129
+ listError = listErr instanceof Error ? listErr.message : String(listErr);
130
+ } finally {
131
+ if (timeoutHandle) clearTimeout(timeoutHandle);
132
+ }
133
+ const serialized = serializeCursorRunnerError(args.err);
134
+ args.send({
135
+ kind: "init_error",
136
+ error: formatCursorInitError({
137
+ requestedModelId: args.modelId,
138
+ originalError,
139
+ availableModels,
140
+ listError
141
+ }),
142
+ ...serialized.errorName !== void 0 ? { errorName: serialized.errorName } : {},
143
+ ...serialized.errorCode !== void 0 ? { errorCode: serialized.errorCode } : {},
144
+ ...serialized.statusCode !== void 0 ? { statusCode: serialized.statusCode } : {},
145
+ retryable: serialized.isRetryable
146
+ });
147
+ }
148
+
149
+ // src/services/session/cursor-tool-execution-edge.ts
150
+ function decideToolExecutionEdge(inFlightCount, updateType) {
151
+ if (updateType === "tool-call-started") {
152
+ const newCount = inFlightCount + 1;
153
+ return { count: newCount, edge: newCount === 1 ? "started" : null };
154
+ }
155
+ if (updateType === "tool-call-completed") {
156
+ const newCount = Math.max(0, inFlightCount - 1);
157
+ return { count: newCount, edge: inFlightCount === 1 ? "settled" : null };
158
+ }
159
+ return { count: inFlightCount, edge: null };
160
+ }
161
+
111
162
  // src/services/session/cursor-runner.ts
112
163
  configureCursorRipgrepPath();
113
164
  var state = {
@@ -143,6 +194,7 @@ function log(level, message, data) {
143
194
  }
144
195
  var STREAM_ACTIVITY_IPC_THROTTLE_MS = 2e3;
145
196
  var lastStreamActivitySentAt = 0;
197
+ var inFlightToolCount = 0;
146
198
  function maybeSendStreamActivity(generation) {
147
199
  if (state.currentGeneration !== generation) return;
148
200
  const now = Date.now();
@@ -150,42 +202,6 @@ function maybeSendStreamActivity(generation) {
150
202
  lastStreamActivitySentAt = now;
151
203
  send({ kind: "stream_activity", generation });
152
204
  }
153
- var INIT_MODEL_LIST_TIMEOUT_MS = 5e3;
154
- async function sendInitErrorFromAgentCreateFailure(args) {
155
- const originalError = args.err instanceof Error ? args.err.message : String(args.err);
156
- let availableModels = null;
157
- let listError;
158
- let timeoutHandle;
159
- try {
160
- const listPromise = Cursor.models.list({ apiKey: args.apiKey });
161
- const timeoutPromise = new Promise((_, reject) => {
162
- timeoutHandle = setTimeout(
163
- () => reject(new Error(`timed out after ${INIT_MODEL_LIST_TIMEOUT_MS}ms`)),
164
- INIT_MODEL_LIST_TIMEOUT_MS
165
- );
166
- });
167
- const models = await Promise.race([listPromise, timeoutPromise]);
168
- availableModels = models.map((m) => ({ id: m.id, aliases: m.aliases }));
169
- } catch (listErr) {
170
- listError = listErr instanceof Error ? listErr.message : String(listErr);
171
- } finally {
172
- if (timeoutHandle) clearTimeout(timeoutHandle);
173
- }
174
- const serialized = serializeCursorRunnerError(args.err);
175
- send({
176
- kind: "init_error",
177
- error: formatCursorInitError({
178
- requestedModelId: args.modelId,
179
- originalError,
180
- availableModels,
181
- listError
182
- }),
183
- ...serialized.errorName !== void 0 ? { errorName: serialized.errorName } : {},
184
- ...serialized.errorCode !== void 0 ? { errorCode: serialized.errorCode } : {},
185
- ...serialized.statusCode !== void 0 ? { statusCode: serialized.statusCode } : {},
186
- retryable: serialized.isRetryable
187
- });
188
- }
189
205
  function buildHarnessServerEntry(args) {
190
206
  const headers = {
191
207
  Authorization: `Bearer ${args.harnessToken}`
@@ -303,7 +319,8 @@ async function handleInit(args) {
303
319
  await sendInitErrorFromAgentCreateFailure({
304
320
  apiKey: args.apiKey,
305
321
  modelId: args.modelId,
306
- err
322
+ err,
323
+ send
307
324
  });
308
325
  }
309
326
  }
@@ -470,6 +487,7 @@ async function handlePushMessage(args) {
470
487
  }
471
488
  state.currentGeneration = args.generation;
472
489
  lastStreamActivitySentAt = 0;
490
+ inFlightToolCount = 0;
473
491
  const parsedContent = ContentBlockSchema.array().safeParse(args.content);
474
492
  if (!parsedContent.success) {
475
493
  log("warn", "runner_push_message_invalid_content", {
@@ -513,6 +531,13 @@ async function handlePushMessage(args) {
513
531
  mcpServers: state.mcpServers,
514
532
  onDelta: ({ update }) => {
515
533
  maybeSendStreamActivity(args.generation);
534
+ if (state.currentGeneration === args.generation) {
535
+ const edge = decideToolExecutionEdge(inFlightToolCount, update.type);
536
+ inFlightToolCount = edge.count;
537
+ if (edge.edge !== null) {
538
+ send({ kind: "tool_execution", generation: args.generation, phase: edge.edge });
539
+ }
540
+ }
516
541
  forwardSummaryUpdate(update, args.generation, latestUsage, tokenDeltaAccumulator);
517
542
  },
518
543
  onStep: () => {
@@ -567,6 +592,7 @@ async function handleClose() {
567
592
  state.fastMode = false;
568
593
  state.fastModeParam = null;
569
594
  state.mode = void 0;
595
+ inFlightToolCount = 0;
570
596
  send({ kind: "pooled_idle" });
571
597
  return;
572
598
  }