@openchamber/web 1.18.4 → 1.20.0

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 (251) hide show
  1. package/README.md +1 -0
  2. package/dist/assets/{CodeMirrorCommentWidgets-4qZbrEXU.js → CodeMirrorCommentWidgets-C36E738Y.js} +1 -1
  3. package/dist/assets/{CommitSection-MJmntVPJ.js → CommitSection-C-F3AX6D.js} +1 -1
  4. package/dist/assets/{DiagramEditor-jnG3Hbxg.js → DiagramEditor-BPECg1jS.js} +1 -1
  5. package/dist/assets/DiagramView-D0YNMix8.js +1 -0
  6. package/dist/assets/DiffView-BHWaMYCC.js +4 -0
  7. package/dist/assets/FilesView-NMBCW2Tm.js +14 -0
  8. package/dist/assets/{GitView-DBFrDDBv.js → GitView-Rp8P1KEI.js} +4 -4
  9. package/dist/assets/{JsonTreeView-DLW2Wy1g.js → JsonTreeView-KnXFN2A-.js} +1 -1
  10. package/dist/assets/MarkdownImageGallery-BMn3mtor.js +1 -0
  11. package/dist/assets/MarkdownRendererImpl-kD6N03pH.js +12 -0
  12. package/dist/assets/OnboardingScreen-CenIeD4v.js +1 -0
  13. package/dist/assets/{PatchDiff-BuLPx7AN.js → PatchDiff-BzVOVog8.js} +1 -1
  14. package/dist/assets/{PierreDiffViewer-BHm4Yr0X.js → PierreDiffViewer-Dlby4XqT.js} +2 -2
  15. package/dist/assets/PlanView-Czu1KMJa.js +4 -0
  16. package/dist/assets/SessionSwitcherDropdown-IETz58DB.js +1 -0
  17. package/dist/assets/SettingsView-B6vmuf-c.js +28 -0
  18. package/dist/assets/SettingsWindow-CauZpdD3.js +1 -0
  19. package/dist/assets/ToolOutputDialog-BB1c--Zr.js +15 -0
  20. package/dist/assets/ToolPartDiffPreview-DaOIDISD.js +8 -0
  21. package/dist/assets/{VirtualizedFileDiff-CwGocYQd.js → VirtualizedFileDiff-lESMT-2b.js} +4 -4
  22. package/dist/assets/{WalkthroughView-5DyvN03T.js → WalkthroughView-CaSYE0lw.js} +2 -2
  23. package/dist/assets/{computeVirtualFileMetrics-Da00Jg7M.js → computeVirtualFileMetrics-DkRR5b75.js} +2 -2
  24. package/dist/assets/contextPanelEmbeddedChat-BEARQjgn.js +1 -0
  25. package/dist/assets/de-DFdX3ABv.js +9 -0
  26. package/dist/assets/document-attachments-DFAWlG3U.js +31 -0
  27. package/dist/assets/es-iC-FKvV9.js +15 -0
  28. package/dist/assets/fr-BBbi_1AL.js +13 -0
  29. package/dist/assets/index-BiEgzzkg.js +1 -0
  30. package/dist/assets/index-Cwmuwk3V.js +7 -0
  31. package/dist/assets/index-GNm1wgUH.css +1 -0
  32. package/dist/assets/{isDiffPlainText-D9YpoLrV.js → isDiffPlainText-BG53Ixxy.js} +1 -1
  33. package/dist/assets/{isFilePlainText-gTibdNIe.js → isFilePlainText-BbV-39jl.js} +1 -1
  34. package/dist/assets/ja-DQLQJL2J.js +15 -0
  35. package/dist/assets/ko-BeElEB4Y.js +15 -0
  36. package/dist/assets/{lua-BgMRiT3U.js → lua-VAEuO923.js} +1 -1
  37. package/dist/assets/main-Dpzo18z7.js +2 -0
  38. package/dist/assets/main-WMjtYzJs.js +921 -0
  39. package/dist/assets/markdownCore--tDmyI_v.js +2 -0
  40. package/dist/assets/miniChat-ldEghBLB.js +2 -0
  41. package/dist/assets/mobile-Dk-VGreF.js +2 -0
  42. package/dist/assets/{patchFileDiff-KNkxM1C1.js → patchFileDiff-BcFaMtMu.js} +1 -1
  43. package/dist/assets/pl-CLHt2_vV.js +15 -0
  44. package/dist/assets/pt-BR-C_MPUq7e.js +15 -0
  45. package/dist/assets/renderElectronMiniChatApp-DIgCOmXY.js +2 -0
  46. package/dist/assets/renderMobileApp-CDfHS5rr.js +25 -0
  47. package/dist/assets/runtimeEndpointReset-ovNbaZ-x.js +15 -0
  48. package/dist/assets/scheduledTasksApi-BFSlqgnG.js +1 -0
  49. package/dist/assets/{shikiHighlight-C7LTbwFx.js → shikiHighlight-DCT2Sq_z.js} +1 -1
  50. package/dist/assets/uk-PhsC-c4-.js +15 -0
  51. package/dist/assets/{useAppFontEffects-BikB6enL.js → useAppFontEffects-B53Rb6m7.js} +168 -164
  52. package/dist/assets/useInlineCommentController-DSfrIBKT.js +2 -0
  53. package/dist/assets/{vendor-base-ui-D_V6hAcE.js → vendor-base-ui-DsHmyMwp.js} +1 -1
  54. package/dist/assets/vendor-codemirror-autocomplete-DzPz9Wgq.js +1 -0
  55. package/dist/assets/vendor-codemirror-commands-B7H_j0iU.js +1 -0
  56. package/dist/assets/{vendor-codemirror-lang-angular-CgY7_gQ1.js → vendor-codemirror-lang-angular-DEPs2M5t.js} +1 -1
  57. package/dist/assets/{vendor-codemirror-lang-cpp-B5Ta4enQ.js → vendor-codemirror-lang-cpp-DhkseaMo.js} +1 -1
  58. package/dist/assets/{vendor-codemirror-lang-css-VgNqFSH-.js → vendor-codemirror-lang-css-O0cZX78y.js} +1 -1
  59. package/dist/assets/{vendor-codemirror-lang-go-LxLz62pp.js → vendor-codemirror-lang-go-fMdQYcON.js} +1 -1
  60. package/dist/assets/vendor-codemirror-lang-html-CfgK872x.js +1 -0
  61. package/dist/assets/{vendor-codemirror-lang-java-Bksi1PH4.js → vendor-codemirror-lang-java-CQj4S3zl.js} +1 -1
  62. package/dist/assets/vendor-codemirror-lang-javascript-Cc2ZYbgX.js +13 -0
  63. package/dist/assets/{vendor-codemirror-lang-jinja-C7npuDD7.js → vendor-codemirror-lang-jinja-BmZZv8g-.js} +1 -1
  64. package/dist/assets/{vendor-codemirror-lang-json-ryCw7nab.js → vendor-codemirror-lang-json-CMBS2tG9.js} +1 -1
  65. package/dist/assets/{vendor-codemirror-lang-less-BxsZJD6N.js → vendor-codemirror-lang-less-C6Eo8hVS.js} +1 -1
  66. package/dist/assets/{vendor-codemirror-lang-liquid-De6qch10.js → vendor-codemirror-lang-liquid-DSTR47-H.js} +1 -1
  67. package/dist/assets/vendor-codemirror-lang-markdown-BPGOH9ye.js +1 -0
  68. package/dist/assets/{vendor-codemirror-lang-php-Cm1es33p.js → vendor-codemirror-lang-php-DD9udfPH.js} +1 -1
  69. package/dist/assets/{vendor-codemirror-lang-python-N414WYK-.js → vendor-codemirror-lang-python-HahnQaAS.js} +1 -1
  70. package/dist/assets/{vendor-codemirror-lang-rust-C_WuUStH.js → vendor-codemirror-lang-rust-nLTovbjv.js} +1 -1
  71. package/dist/assets/{vendor-codemirror-lang-sass-8UHTCAOM.js → vendor-codemirror-lang-sass-pL2Yc72v.js} +1 -1
  72. package/dist/assets/{vendor-codemirror-lang-sql-CV3sMA5D.js → vendor-codemirror-lang-sql-BVBnan19.js} +1 -1
  73. package/dist/assets/{vendor-codemirror-lang-vue-DzvC1Dhk.js → vendor-codemirror-lang-vue-BqGzJVm1.js} +1 -1
  74. package/dist/assets/{vendor-codemirror-lang-wast-jlr-LDdA.js → vendor-codemirror-lang-wast-B0_m5DqU.js} +1 -1
  75. package/dist/assets/{vendor-codemirror-lang-xml-CrO63OVp.js → vendor-codemirror-lang-xml-Dj3voadT.js} +1 -1
  76. package/dist/assets/{vendor-codemirror-lang-yaml-dxi6CISW.js → vendor-codemirror-lang-yaml-yu-9Z5u7.js} +1 -1
  77. package/dist/assets/vendor-codemirror-language-BgPj_McA.js +3 -0
  78. package/dist/assets/{vendor-codemirror-language-data-CLHw8NWn.js → vendor-codemirror-language-data-vch3tJ4c.js} +2 -2
  79. package/dist/assets/vendor-codemirror-search-BmS-GmcX.js +2 -0
  80. package/dist/assets/{vendor-codemirror-state-CNWM3Q8V.js → vendor-codemirror-state-C9awkmn-.js} +5 -5
  81. package/dist/assets/vendor-codemirror-view-CiLROUVV.js +4 -0
  82. package/dist/assets/vendor-replit-codemirror-vim-B2DJlAhT.js +6 -0
  83. package/dist/assets/vendor-replit-codemirror-vim-core-FHzKdNUS.js +34 -0
  84. package/dist/assets/vendor-zod-C9ioTTSo.js +39 -0
  85. package/dist/assets/{web-components-GMc3oR3i.js → web-components-pcwCTWjC.js} +4 -4
  86. package/dist/assets/zh-CN-wF-NOFHM.js +15 -0
  87. package/dist/assets/zh-TW-4m4-A7rT.js +15 -0
  88. package/dist/index.html +5 -4
  89. package/dist/mini-chat.html +4 -3
  90. package/dist/mobile.html +4 -3
  91. package/package.json +3 -5
  92. package/server/index.js +152 -3
  93. package/server/lib/agent-memory/actions.js +243 -0
  94. package/server/lib/agent-memory/actions.test.js +343 -0
  95. package/server/lib/agent-memory/feature-flag.js +19 -0
  96. package/server/lib/agent-memory/feature-flag.test.js +38 -0
  97. package/server/lib/agent-memory/project-resolution.js +65 -0
  98. package/server/lib/agent-memory/project-resolution.test.js +94 -0
  99. package/server/lib/agent-memory/routes.http.test.js +270 -0
  100. package/server/lib/agent-memory/routes.js +169 -0
  101. package/server/lib/agent-memory/runtime.js +432 -0
  102. package/server/lib/agent-memory/runtime.test.js +344 -0
  103. package/server/lib/agent-memory/threat-patterns.js +61 -0
  104. package/server/lib/agent-memory/threat-patterns.test.js +61 -0
  105. package/server/lib/agent-tool/DOCUMENTATION.md +14 -0
  106. package/server/lib/agent-tool/runtime.js +63 -13
  107. package/server/lib/agent-tool/runtime.test.js +85 -2
  108. package/server/lib/context-obligatory/runtime.js +34 -3
  109. package/server/lib/context-obligatory/runtime.test.js +114 -0
  110. package/server/lib/dictation/DOCUMENTATION.md +32 -8
  111. package/server/lib/dictation/local/sherpa-recognizer.js +35 -78
  112. package/server/lib/dictation/local/worker-process.js +2 -2
  113. package/server/lib/dictation/openai-compatible-session.js +3 -2
  114. package/server/lib/dictation/stream-manager.js +77 -34
  115. package/server/lib/dictation/stream-manager.test.js +60 -2
  116. package/server/lib/fs/DOCUMENTATION.md +3 -0
  117. package/server/lib/fs/routes.js +149 -32
  118. package/server/lib/fs/routes.test.js +301 -0
  119. package/server/lib/git/DOCUMENTATION.md +8 -0
  120. package/server/lib/git/routes.js +43 -0
  121. package/server/lib/git/service.js +206 -72
  122. package/server/lib/git/service.test.js +222 -0
  123. package/server/lib/github/DOCUMENTATION.md +14 -3
  124. package/server/lib/github/pr-status.js +188 -83
  125. package/server/lib/github/pr-status.test.js +182 -0
  126. package/server/lib/github/routes.js +36 -30
  127. package/server/lib/notifications/template-runtime.js +2 -3
  128. package/server/lib/openchamber-control/action-resolution.test.js +65 -0
  129. package/server/lib/openchamber-control/actions.js +78 -0
  130. package/server/lib/openchamber-control/service.js +7 -0
  131. package/server/lib/openchamber-sessions/routes.js +14 -0
  132. package/server/lib/opencode/DOCUMENTATION.md +12 -6
  133. package/server/lib/opencode/claude-cli-auth.js +60 -0
  134. package/server/lib/opencode/claude-cli-auth.test.js +54 -0
  135. package/server/lib/opencode/feature-routes-runtime.js +16 -9
  136. package/server/lib/opencode/lifecycle.js +202 -26
  137. package/server/lib/opencode/lifecycle.test.js +179 -1
  138. package/server/lib/opencode/plugins.js +12 -3
  139. package/server/lib/opencode/plugins.test.js +18 -0
  140. package/server/lib/opencode/providers.js +9 -4
  141. package/server/lib/opencode/providers.test.js +27 -0
  142. package/server/lib/opencode/proxy-agent-wiring.test.js +152 -0
  143. package/server/lib/opencode/proxy.js +116 -6
  144. package/server/lib/opencode/proxy.test.js +158 -1
  145. package/server/lib/opencode/restart-session-recovery.test.js +59 -0
  146. package/server/lib/opencode/routes.js +4 -1
  147. package/server/lib/opencode/session-runtime.js +39 -2
  148. package/server/lib/opencode/session-runtime.test.js +74 -0
  149. package/server/lib/opencode/settings-helpers.js +26 -0
  150. package/server/lib/opencode/settings-helpers.test.js +57 -0
  151. package/server/lib/opencode/settings-normalization-runtime.js +3 -0
  152. package/server/lib/opencode/settings-normalization-runtime.test.js +16 -0
  153. package/server/lib/opencode/settings-runtime.js +83 -10
  154. package/server/lib/opencode/settings-runtime.test.js +85 -0
  155. package/server/lib/opencode/shared.js +120 -16
  156. package/server/lib/opencode/shared.test.js +185 -1
  157. package/server/lib/opencode/skill-routes.js +29 -73
  158. package/server/lib/opencode/skill-routes.test.js +2 -5
  159. package/server/lib/project-context/DOCUMENTATION.md +160 -0
  160. package/server/lib/project-context/routes.http.test.js +264 -0
  161. package/server/lib/project-context/routes.js +237 -0
  162. package/server/lib/project-context/runtime.js +672 -0
  163. package/server/lib/project-context/runtime.test.js +498 -0
  164. package/server/lib/projects/project-config.js +7 -2
  165. package/server/lib/quota/DOCUMENTATION.md +17 -2
  166. package/server/lib/quota/providers/claude/auth.js +113 -0
  167. package/server/lib/quota/providers/claude/auth.test.js +117 -0
  168. package/server/lib/quota/providers/claude/index.js +151 -0
  169. package/server/lib/quota/providers/claude/index.test.js +147 -0
  170. package/server/lib/quota/providers/claude/transforms.js +127 -0
  171. package/server/lib/quota/providers/claude/transforms.test.js +82 -0
  172. package/server/lib/quota/providers/command-code.js +90 -0
  173. package/server/lib/quota/providers/command-code.test.js +85 -0
  174. package/server/lib/quota/providers/index.js +30 -2
  175. package/server/lib/quota/providers/index.test.js +10 -1
  176. package/server/lib/quota/providers/zai.js +26 -6
  177. package/server/lib/quota/providers/zai.test.js +33 -0
  178. package/server/lib/quota/utils/formatters.js +2 -1
  179. package/server/lib/scheduled-tasks/runtime.js +21 -2
  180. package/server/lib/session-knowledge/DOCUMENTATION.md +89 -0
  181. package/server/lib/session-knowledge/routes.js +107 -0
  182. package/server/lib/session-knowledge/runtime.js +331 -0
  183. package/server/lib/session-knowledge/runtime.test.js +275 -0
  184. package/server/lib/skills-catalog/DOCUMENTATION.md +18 -45
  185. package/server/lib/skills-catalog/cache.js +120 -1
  186. package/server/lib/skills-catalog/cache.test.js +77 -0
  187. package/server/lib/skills-catalog/curated-sources.js +21 -5
  188. package/server/lib/skills-catalog/curated-sources.test.js +10 -0
  189. package/server/lib/skills-catalog/disk-cache.js +52 -0
  190. package/server/lib/skills-catalog/github-meta.js +139 -0
  191. package/server/lib/skills-catalog/github-meta.test.js +71 -0
  192. package/server/lib/skills-catalog/source.js +0 -5
  193. package/server/lib/small-model/DOCUMENTATION.md +74 -6
  194. package/server/lib/small-model/call.js +46 -12
  195. package/server/lib/small-model/call.test.js +79 -0
  196. package/server/lib/small-model/index.js +57 -6
  197. package/server/lib/small-model/index.test.js +102 -2
  198. package/server/lib/small-model/routes.js +4 -2
  199. package/server/lib/small-model/runtime-providers.js +156 -0
  200. package/server/lib/small-model/runtime-providers.test.js +112 -0
  201. package/dist/assets/DiagramView-CFXxgg_a.js +0 -1
  202. package/dist/assets/DiffView-DZIoOnV7.js +0 -4
  203. package/dist/assets/FilesView-BKL6YlKG.js +0 -14
  204. package/dist/assets/MarkdownImageGallery-DKIylIK7.js +0 -1
  205. package/dist/assets/MarkdownRendererImpl-CLv8hxvv.js +0 -12
  206. package/dist/assets/OnboardingScreen-D2Y6djMw.js +0 -1
  207. package/dist/assets/PlanView-ZYEJn8A_.js +0 -4
  208. package/dist/assets/SessionSwitcherDropdown-rro-SsaO.js +0 -1
  209. package/dist/assets/SettingsView-5RmswFZe.js +0 -28
  210. package/dist/assets/SettingsWindow-BZhAYFdS.js +0 -1
  211. package/dist/assets/ToolOutputDialog-Za6V4VMJ.js +0 -15
  212. package/dist/assets/ToolPartDiffPreview-BL-9ixyC.js +0 -8
  213. package/dist/assets/contextPanelEmbeddedChat-B-bSg3P5.js +0 -1
  214. package/dist/assets/de-u7gSnqWs.js +0 -9
  215. package/dist/assets/document-attachments-CX_YaDsu.js +0 -30
  216. package/dist/assets/es-T9O7aAOr.js +0 -15
  217. package/dist/assets/fr-Oor7ewbE.js +0 -13
  218. package/dist/assets/index-BzIpNlNE.css +0 -1
  219. package/dist/assets/index-C0yNhuZw.js +0 -1
  220. package/dist/assets/index-DRpChWux.js +0 -7
  221. package/dist/assets/ja-DHpqbVmr.js +0 -15
  222. package/dist/assets/ko-CwLkS3nx.js +0 -15
  223. package/dist/assets/main-D7UcL3VQ.js +0 -2
  224. package/dist/assets/main-DPMd2iKS.js +0 -915
  225. package/dist/assets/markdownCore-DDrHVneM.js +0 -2
  226. package/dist/assets/miniChat-O_GVoEbc.js +0 -2
  227. package/dist/assets/mobile-Br4TtAqc.js +0 -2
  228. package/dist/assets/pl-CcZILN8q.js +0 -15
  229. package/dist/assets/pt-BR-DYHhVK6o.js +0 -15
  230. package/dist/assets/renderElectronMiniChatApp-CJpdxTT1.js +0 -2
  231. package/dist/assets/renderMobileApp-toj9cXoh.js +0 -25
  232. package/dist/assets/runtimeEndpointReset-BVmispdD.js +0 -13
  233. package/dist/assets/scheduledTasksApi-ByKya3Un.js +0 -1
  234. package/dist/assets/uk-BA2kZR38.js +0 -15
  235. package/dist/assets/useInlineCommentController-CYPvYJDt.js +0 -2
  236. package/dist/assets/vendor-codemirror-autocomplete-BjbBRsUA.js +0 -1
  237. package/dist/assets/vendor-codemirror-commands-ARAOgp1x.js +0 -1
  238. package/dist/assets/vendor-codemirror-lang-html-B3Qe3OpS.js +0 -1
  239. package/dist/assets/vendor-codemirror-lang-javascript-Bbw3D7r0.js +0 -13
  240. package/dist/assets/vendor-codemirror-lang-markdown-FGQydhTc.js +0 -1
  241. package/dist/assets/vendor-codemirror-language-DUOssd4j.js +0 -3
  242. package/dist/assets/vendor-codemirror-search-8kyQdB3f.js +0 -2
  243. package/dist/assets/vendor-codemirror-view-DSzLbZiS.js +0 -4
  244. package/dist/assets/vendor-replit-codemirror-vim-B0Ecqnlh.js +0 -39
  245. package/dist/assets/zh-CN-dlOQK6v9.js +0 -15
  246. package/dist/assets/zh-TW-BnqQ-WL5.js +0 -15
  247. package/server/lib/quota/providers/claude.js +0 -107
  248. package/server/lib/skills-catalog/clawdhub/api.js +0 -126
  249. package/server/lib/skills-catalog/clawdhub/install.js +0 -238
  250. package/server/lib/skills-catalog/clawdhub/install.test.js +0 -100
  251. package/server/lib/skills-catalog/clawdhub/scan.js +0 -61
@@ -1891,6 +1891,100 @@ const fetchRemoteBranchRef = async (primaryWorktree, remoteName, branchName) =>
1891
1891
  );
1892
1892
  };
1893
1893
 
1894
+ /**
1895
+ * Shared existing-mode resolver for validate + create.
1896
+ * Provisioned remotes (`ensureRemoteName`/`ensureRemoteUrl`) are used for fork
1897
+ * PR heads; other existing branches keep the local / already-fetched remote path.
1898
+ *
1899
+ * @param {'validate'|'create'} intent
1900
+ */
1901
+ const resolveExistingWorktreeSource = async (primaryWorktree, input = {}, intent = 'create') => {
1902
+ const preferredBranchName = cleanBranchName(String(input?.branchName || '').trim());
1903
+ const ensureRemoteName = String(input?.ensureRemoteName || '').trim();
1904
+ const ensureRemoteUrl = String(input?.ensureRemoteUrl || '').trim();
1905
+ const requestedExistingBranch = String(input?.existingBranch || '').trim();
1906
+ const wantUpstream = Boolean(input?.setUpstream);
1907
+ const explicitUpstreamRemote = String(input?.upstreamRemote || '').trim();
1908
+ const explicitUpstreamBranch = String(input?.upstreamBranch || '').trim();
1909
+ const parsedExistingRemote = await resolveRemoteBranchRef(primaryWorktree, requestedExistingBranch);
1910
+
1911
+ if (
1912
+ parsedExistingRemote
1913
+ && ensureRemoteName
1914
+ && ensureRemoteUrl
1915
+ && parsedExistingRemote.remote === ensureRemoteName
1916
+ ) {
1917
+ if (intent === 'validate') {
1918
+ const lsRemote = await runGitCommand(
1919
+ primaryWorktree,
1920
+ ['ls-remote', '--heads', ensureRemoteUrl, `refs/heads/${parsedExistingRemote.branch}`]
1921
+ );
1922
+ if (!lsRemote.success) {
1923
+ throw new Error(
1924
+ `Unable to reach remote ${ensureRemoteName} (${ensureRemoteUrl}). `
1925
+ + 'Check network access and credentials for that repository.'
1926
+ );
1927
+ }
1928
+ if (!String(lsRemote.stdout || '').trim()) {
1929
+ throw new Error(`Remote branch not found: ${parsedExistingRemote.remoteRef}`);
1930
+ }
1931
+ } else {
1932
+ await ensureRemoteWithUrl(primaryWorktree, ensureRemoteName, ensureRemoteUrl);
1933
+ try {
1934
+ await fetchRemoteBranchRef(
1935
+ primaryWorktree,
1936
+ parsedExistingRemote.remote,
1937
+ parsedExistingRemote.branch
1938
+ );
1939
+ } catch (error) {
1940
+ const detail = error instanceof Error ? error.message : String(error);
1941
+ throw new Error(
1942
+ `Unable to fetch ${parsedExistingRemote.remote}/${parsedExistingRemote.branch} `
1943
+ + `from ${ensureRemoteUrl}. ${detail}`
1944
+ );
1945
+ }
1946
+ }
1947
+
1948
+ const localBranch = cleanBranchName(preferredBranchName || parsedExistingRemote.branch);
1949
+ return {
1950
+ localBranch,
1951
+ checkoutRef: parsedExistingRemote.remoteRef,
1952
+ createLocalBranch: true,
1953
+ setUpstream: wantUpstream,
1954
+ upstream: {
1955
+ remote: explicitUpstreamRemote || parsedExistingRemote.remote,
1956
+ branch: explicitUpstreamBranch || parsedExistingRemote.branch,
1957
+ },
1958
+ };
1959
+ }
1960
+
1961
+ if (!requestedExistingBranch) {
1962
+ throw new Error('existingBranch is required in existing mode');
1963
+ }
1964
+
1965
+ const resolved = await resolveBranchForExistingMode(
1966
+ primaryWorktree,
1967
+ requestedExistingBranch,
1968
+ preferredBranchName
1969
+ );
1970
+ const upstream = resolved.remoteRef
1971
+ ? {
1972
+ remote: explicitUpstreamRemote || resolved.remoteRef.remote,
1973
+ branch: explicitUpstreamBranch || resolved.remoteRef.branch,
1974
+ }
1975
+ : (explicitUpstreamRemote && explicitUpstreamBranch
1976
+ ? { remote: explicitUpstreamRemote, branch: explicitUpstreamBranch }
1977
+ : null);
1978
+
1979
+ return {
1980
+ localBranch: resolved.localBranch,
1981
+ checkoutRef: resolved.checkoutRef,
1982
+ createLocalBranch: resolved.createLocalBranch,
1983
+ setUpstream: wantUpstream && Boolean(upstream),
1984
+ upstream,
1985
+ };
1986
+ };
1987
+
1894
1988
  const checkRemoteBranchExists = async (primaryWorktree, remoteName, branchName, remoteUrl = '') => {
1895
1989
  const remote = String(remoteName || '').trim();
1896
1990
  const branch = String(branchName || '').trim();
@@ -1914,19 +2008,6 @@ const checkRemoteBranchExists = async (primaryWorktree, remoteName, branchName,
1914
2008
  };
1915
2009
  };
1916
2010
 
1917
- const setBranchTrackingFallback = async (worktreeDirectory, localBranch, upstream) => {
1918
- await runGitCommandOrThrow(
1919
- worktreeDirectory,
1920
- ['config', `branch.${localBranch}.remote`, upstream.remote],
1921
- `Failed to set branch.${localBranch}.remote`
1922
- );
1923
- await runGitCommandOrThrow(
1924
- worktreeDirectory,
1925
- ['config', `branch.${localBranch}.merge`, `refs/heads/${upstream.branch}`],
1926
- `Failed to set branch.${localBranch}.merge`
1927
- );
1928
- };
1929
-
1930
2011
  const applyUpstreamConfiguration = async (args) => {
1931
2012
  const {
1932
2013
  primaryWorktree,
@@ -1952,23 +2033,19 @@ const applyUpstreamConfiguration = async (args) => {
1952
2033
  return;
1953
2034
  }
1954
2035
 
1955
- let fetched = true;
1956
2036
  try {
1957
2037
  await fetchRemoteBranchRef(primaryWorktree, upstream.remote, upstream.branch);
1958
2038
  } catch {
1959
- fetched = false;
1960
- }
1961
-
1962
- if (fetched) {
1963
- await runGitCommandOrThrow(
1964
- worktreeDirectory,
1965
- ['branch', `--set-upstream-to=${upstream.full}`, localBranch],
1966
- `Failed to set upstream to ${upstream.full}`
1967
- );
2039
+ // Fetch failed: leave tracking unset. Do not write branch.*.remote/merge
2040
+ // pointing at a ref that was never fetched.
1968
2041
  return;
1969
2042
  }
1970
2043
 
1971
- await setBranchTrackingFallback(worktreeDirectory, localBranch, upstream);
2044
+ await runGitCommandOrThrow(
2045
+ worktreeDirectory,
2046
+ ['branch', `--set-upstream-to=${upstream.full}`, localBranch],
2047
+ `Failed to set upstream to ${upstream.full}`
2048
+ );
1972
2049
  };
1973
2050
 
1974
2051
  export async function isGitRepository(directory) {
@@ -2577,6 +2654,71 @@ export async function getRangeDiff(directory, { base, head, path: filePath, cont
2577
2654
  return diff;
2578
2655
  }
2579
2656
 
2657
+ const BRANCH_CREATION_SOURCE_RE = /^branch: Created from (.+)$/;
2658
+
2659
+ /**
2660
+ * Parse a branch reflog (`git reflog show --format=%gs <branch>`) and return the
2661
+ * ref the branch was created from, when that source is itself a named ref.
2662
+ *
2663
+ * Returns null when the branch was created from `HEAD@{...}` or a raw commit
2664
+ * (detached start): the original branch name is not recorded anywhere in that
2665
+ * case, and guessing a base from commit topology would be a heuristic, not an
2666
+ * answer. Callers should ask the user to pick a base instead.
2667
+ */
2668
+ export function parseBranchCreationSource(reflogText) {
2669
+ const lines = String(reflogText || '')
2670
+ .split('\n')
2671
+ .map((line) => line.trim())
2672
+ .filter(Boolean);
2673
+ // Reflog lists newest entries first; the creation entry is the oldest one.
2674
+ for (let index = lines.length - 1; index >= 0; index -= 1) {
2675
+ const match = lines[index].match(BRANCH_CREATION_SOURCE_RE);
2676
+ if (!match) continue;
2677
+ const source = match[1].trim();
2678
+ if (!source || /^HEAD@/.test(source) || /^[0-9a-f]{7,40}$/i.test(source)) {
2679
+ return null;
2680
+ }
2681
+ return source;
2682
+ }
2683
+ return null;
2684
+ }
2685
+
2686
+ /**
2687
+ * Resolve the branch the given branch was created from, from its reflog.
2688
+ * Returns { base: null } when git has no authoritative record (clone, detached
2689
+ * start, reflog expired) — callers must not fall back to main/master.
2690
+ */
2691
+ export async function getBranchBase(directory, branch) {
2692
+ const branchName = String(branch || '').trim();
2693
+ if (!branchName) {
2694
+ throw new Error('branch is required');
2695
+ }
2696
+
2697
+ const { git } = await createRepositoryGitContext(directory);
2698
+
2699
+ let reflog = '';
2700
+ try {
2701
+ reflog = await git.raw(['reflog', 'show', '--format=%gs', branchName]);
2702
+ } catch {
2703
+ return { base: null };
2704
+ }
2705
+
2706
+ const source = parseBranchCreationSource(reflog);
2707
+ if (!source || source === branchName) {
2708
+ return { base: null };
2709
+ }
2710
+
2711
+ const resolves = await git
2712
+ .raw(['rev-parse', '--verify', '--quiet', source])
2713
+ .then((value) => Boolean(String(value || '').trim()))
2714
+ .catch(() => false);
2715
+ if (!resolves) {
2716
+ return { base: null };
2717
+ }
2718
+
2719
+ return { base: source };
2720
+ }
2721
+
2580
2722
  export async function getRangeFiles(directory, { base, head } = {}) {
2581
2723
  const { git } = await createRepositoryGitContext(directory);
2582
2724
  const baseRef = typeof base === 'string' ? base.trim() : '';
@@ -2596,11 +2738,26 @@ export async function getRangeFiles(directory, { base, head } = {}) {
2596
2738
  // ignore
2597
2739
  }
2598
2740
 
2599
- const raw = await git.raw(['diff', '--name-only', `${resolvedBase}...${headRef}`]);
2600
- return String(raw || '')
2601
- .split('\n')
2602
- .map((l) => l.trim())
2603
- .filter(Boolean);
2741
+ // `-C` (copy detection among changed files only, so cheap) makes copies
2742
+ // surface as C entries instead of plain additions; rename detection is on
2743
+ // by default.
2744
+ const raw = await git.raw(['diff', '--name-status', '-z', '-C', `${resolvedBase}...${headRef}`]);
2745
+ // -z format: STATUS\0PATH\0[ORIG\0] repeated. For rename/copy entries
2746
+ // (`R100`, `C75`) the first path token is the ORIGINAL path and the second
2747
+ // is the DESTINATION — the diff (and the UI) must address the destination.
2748
+ const tokens = String(raw || '').split('\0');
2749
+ const files = [];
2750
+ for (let index = 0; index < tokens.length; index += 1) {
2751
+ const status = (tokens[index] || '').trim();
2752
+ if (!status) continue;
2753
+ const isRenameOrCopy = status.startsWith('R') || status.startsWith('C');
2754
+ const path = isRenameOrCopy ? (tokens[index + 2] || '').trim() : (tokens[index + 1] || '').trim();
2755
+ index += isRenameOrCopy ? 2 : 1;
2756
+ if (path) {
2757
+ files.push({ path, status: status.charAt(0) });
2758
+ }
2759
+ }
2760
+ return files;
2604
2761
  }
2605
2762
 
2606
2763
  const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'ico', 'bmp', 'avif'];
@@ -3759,33 +3916,13 @@ export async function validateWorktreeCreate(directory, input = {}) {
3759
3916
 
3760
3917
  if (mode === 'existing') {
3761
3918
  try {
3762
- const requestedExistingBranch = String(input?.existingBranch || '').trim();
3763
- const parsedExistingRemote = await resolveRemoteBranchRef(context.primaryWorktree, requestedExistingBranch);
3764
- if (parsedExistingRemote && ensureRemoteName && ensureRemoteUrl && ensureRemoteName === parsedExistingRemote.remote) {
3765
- const lsRemote = await runGitCommand(
3766
- context.primaryWorktree,
3767
- ['ls-remote', '--heads', ensureRemoteUrl, `refs/heads/${parsedExistingRemote.branch}`]
3768
- );
3769
- if (!lsRemote.success) {
3770
- throw new Error(`Unable to query remote ${ensureRemoteName}`);
3771
- }
3772
- if (!String(lsRemote.stdout || '').trim()) {
3773
- throw new Error(`Remote branch not found: ${parsedExistingRemote.remoteRef}`);
3774
- }
3775
- localBranch = cleanBranchName(preferredBranchName || parsedExistingRemote.branch);
3919
+ const resolved = await resolveExistingWorktreeSource(context.primaryWorktree, input, 'validate');
3920
+ localBranch = resolved.localBranch || '';
3921
+ if (resolved.upstream) {
3776
3922
  inferredUpstream = {
3777
- remote: parsedExistingRemote.remote,
3778
- branch: parsedExistingRemote.branch,
3923
+ remote: resolved.upstream.remote,
3924
+ branch: resolved.upstream.branch,
3779
3925
  };
3780
- } else {
3781
- const resolved = await resolveBranchForExistingMode(context.primaryWorktree, requestedExistingBranch, preferredBranchName);
3782
- localBranch = resolved.localBranch || '';
3783
- if (resolved.remoteRef) {
3784
- inferredUpstream = {
3785
- remote: resolved.remoteRef.remote,
3786
- branch: resolved.remoteRef.branch,
3787
- };
3788
- }
3789
3926
  }
3790
3927
  } catch (error) {
3791
3928
  errors.push({
@@ -3954,25 +4091,19 @@ export async function previewWorktreeCreate(directory, input = {}) {
3954
4091
 
3955
4092
  async function attachGitWorktreeToCandidate(context, candidate, input = {}) {
3956
4093
  const mode = input?.mode === 'existing' ? 'existing' : 'new';
3957
- const preferredBranchName = cleanBranchName(String(input?.branchName || '').trim());
3958
4094
  const startRef = normalizeStartRef(input?.startRef);
3959
- const ensureRemoteName = String(input?.ensureRemoteName || '').trim();
3960
- const ensureRemoteUrl = String(input?.ensureRemoteUrl || '').trim();
4095
+ let ensureRemoteName = String(input?.ensureRemoteName || '').trim();
4096
+ let ensureRemoteUrl = String(input?.ensureRemoteUrl || '').trim();
3961
4097
 
3962
4098
  let localBranch = '';
3963
4099
  let inferredUpstream = null;
4100
+ let shouldSetUpstream = Boolean(input?.setUpstream);
3964
4101
  const worktreeAddArgs = ['worktree', 'add', '--no-checkout'];
3965
4102
 
3966
4103
  if (mode === 'existing') {
3967
- const requestedExistingBranch = String(input?.existingBranch || '').trim();
3968
- const parsedExistingRemote = await resolveRemoteBranchRef(context.primaryWorktree, requestedExistingBranch);
3969
- if (parsedExistingRemote && ensureRemoteName && ensureRemoteUrl && parsedExistingRemote.remote === ensureRemoteName) {
3970
- await ensureRemoteWithUrl(context.primaryWorktree, ensureRemoteName, ensureRemoteUrl);
3971
- await fetchRemoteBranchRef(context.primaryWorktree, parsedExistingRemote.remote, parsedExistingRemote.branch);
3972
- }
3973
-
3974
- const resolved = await resolveBranchForExistingMode(context.primaryWorktree, requestedExistingBranch, preferredBranchName);
4104
+ const resolved = await resolveExistingWorktreeSource(context.primaryWorktree, input, 'create');
3975
4105
  localBranch = resolved.localBranch;
4106
+ shouldSetUpstream = resolved.setUpstream;
3976
4107
 
3977
4108
  const inUse = await findBranchInUse(context.primaryWorktree, localBranch);
3978
4109
  if (inUse) {
@@ -3984,10 +4115,10 @@ async function attachGitWorktreeToCandidate(context, candidate, input = {}) {
3984
4115
  }
3985
4116
  worktreeAddArgs.push(candidate.directory, resolved.checkoutRef);
3986
4117
 
3987
- if (resolved.remoteRef) {
4118
+ if (resolved.upstream) {
3988
4119
  inferredUpstream = {
3989
- remote: resolved.remoteRef.remote,
3990
- branch: resolved.remoteRef.branch,
4120
+ remote: resolved.upstream.remote,
4121
+ branch: resolved.upstream.branch,
3991
4122
  };
3992
4123
  }
3993
4124
  } else {
@@ -4033,9 +4164,12 @@ async function attachGitWorktreeToCandidate(context, candidate, input = {}) {
4033
4164
 
4034
4165
  await runGitCommandOrThrow(context.primaryWorktree, worktreeAddArgs, 'Failed to create git worktree');
4035
4166
 
4036
- const shouldSetUpstream = Boolean(input?.setUpstream);
4037
- const upstreamRemote = String(input?.upstreamRemote || inferredUpstream?.remote || '').trim();
4038
- const upstreamBranch = String(input?.upstreamBranch || inferredUpstream?.branch || '').trim();
4167
+ const upstreamRemote = shouldSetUpstream
4168
+ ? String(inferredUpstream?.remote || input?.upstreamRemote || '').trim()
4169
+ : '';
4170
+ const upstreamBranch = shouldSetUpstream
4171
+ ? String(inferredUpstream?.branch || input?.upstreamBranch || '').trim()
4172
+ : '';
4039
4173
 
4040
4174
  const bootstrapStatus = setWorktreeBootstrapState(
4041
4175
  candidate.directory,
@@ -4697,7 +4831,7 @@ export async function renameBranch(directory, oldName, newName) {
4697
4831
  `Failed to set upstream to ${upstream.full}`
4698
4832
  );
4699
4833
  } catch {
4700
- await setBranchTrackingFallback(repoRoot, normalizedNewName, upstream);
4834
+ // Leave tracking unset rather than writing config for a missing ref.
4701
4835
  }
4702
4836
  }
4703
4837
  }
@@ -27,6 +27,9 @@ import {
27
27
  applyHunk,
28
28
  getDiff,
29
29
  getFileDiff,
30
+ validateWorktreeCreate,
31
+ parseBranchCreationSource,
32
+ getRangeFiles,
30
33
  } from './service.js';
31
34
 
32
35
  // ---------------------------------------------------------------------------
@@ -806,6 +809,134 @@ describe('createWorktree', () => {
806
809
  });
807
810
  });
808
811
 
812
+ // ---------------------------------------------------------------------------
813
+ // createWorktree from a forked GitHub PR head (issue #2422)
814
+ // ---------------------------------------------------------------------------
815
+
816
+ describe('createWorktree from a forked GitHub PR', () => {
817
+ const withDataHome = async (test) => {
818
+ const previousXdgDataHome = process.env.XDG_DATA_HOME;
819
+ const dataHome = createTempDir();
820
+ process.env.XDG_DATA_HOME = dataHome;
821
+ try {
822
+ await test(dataHome);
823
+ } finally {
824
+ if (previousXdgDataHome === undefined) {
825
+ delete process.env.XDG_DATA_HOME;
826
+ } else {
827
+ process.env.XDG_DATA_HOME = previousXdgDataHome;
828
+ }
829
+ }
830
+ };
831
+
832
+ const publishForkHead = (repository, forkBare, branchName) => {
833
+ fs.writeFileSync(path.join(repository, 'FORK.md'), `# ${branchName}\n`);
834
+ runGit(repository, ['add', 'FORK.md']);
835
+ runGit(repository, ['commit', '-m', `fork ${branchName}`]);
836
+ const sha = runGit(repository, ['rev-parse', 'HEAD']).trim();
837
+ runGit(repository, ['push', forkBare, `HEAD:refs/heads/${branchName}`]);
838
+ return sha;
839
+ };
840
+
841
+ const getBranchTrackingRemote = (directory, branch) => {
842
+ try {
843
+ return runGit(directory, ['config', '--get', `branch.${branch}.remote`]).trim();
844
+ } catch {
845
+ return '';
846
+ }
847
+ };
848
+
849
+ const forkWorktreeInput = ({ fork, worktreeName }) => ({
850
+ mode: 'existing',
851
+ branchName: 'feature/login',
852
+ worktreeName,
853
+ existingBranch: 'remotes/pr-alice/feature/login',
854
+ setUpstream: true,
855
+ upstreamRemote: 'pr-alice',
856
+ upstreamBranch: 'feature/login',
857
+ ensureRemoteName: 'pr-alice',
858
+ ensureRemoteUrl: fork,
859
+ });
860
+
861
+ it('creates a worktree from a reachable fork head remote', async () => {
862
+ if (!canRunGit()) return;
863
+
864
+ await withDataHome(async () => {
865
+ const { repository } = createRepositoryWithRemote();
866
+ const fork = createTempDir();
867
+ runGit(fork, ['init', '--bare']);
868
+ const sha = publishForkHead(repository, fork, 'feature/login');
869
+
870
+ const created = await createWorktree(repository, forkWorktreeInput({
871
+ fork,
872
+ worktreeName: 'pr-42',
873
+ }));
874
+
875
+ expect(created.branch).toBe('feature/login');
876
+ expect(runGit(created.path, ['rev-parse', 'HEAD']).trim()).toBe(sha);
877
+ await expect.poll(() => fs.existsSync(path.join(created.path, 'FORK.md')), { timeout: 5_000 }).toBe(true);
878
+ expect(runGit(repository, ['remote', 'get-url', 'pr-alice']).trim()).toBe(fork);
879
+ await expect.poll(
880
+ () => getBranchTrackingRemote(created.path, 'feature/login') === 'pr-alice',
881
+ { timeout: 5_000 }
882
+ ).toBe(true);
883
+ });
884
+ }, 30_000);
885
+
886
+ it('rejects an unreachable fork with an actionable error and no worktree', async () => {
887
+ if (!canRunGit()) return;
888
+
889
+ await withDataHome(async () => {
890
+ const { repository } = createRepositoryWithRemote();
891
+ const missingFork = path.join(createTempDir(), 'missing-fork.git');
892
+ const before = runGit(repository, ['worktree', 'list', '--porcelain']);
893
+
894
+ await expect(createWorktree(repository, forkWorktreeInput({
895
+ fork: missingFork,
896
+ worktreeName: 'pr-42-unreachable',
897
+ }))).rejects.toThrow(/Unable to (reach|fetch)/i);
898
+
899
+ expect(runGit(repository, ['worktree', 'list', '--porcelain'])).toBe(before);
900
+
901
+ const validation = await validateWorktreeCreate(repository, forkWorktreeInput({
902
+ fork: missingFork,
903
+ worktreeName: 'pr-42-unreachable',
904
+ }));
905
+ expect(validation.ok).toBe(false);
906
+ expect(validation.errors.some((error) => /Unable to (reach|fetch)/i.test(error.message))).toBe(true);
907
+ });
908
+ }, 30_000);
909
+
910
+ it('does not write upstream tracking when the upstream ref cannot be fetched', async () => {
911
+ if (!canRunGit()) return;
912
+
913
+ await withDataHome(async () => {
914
+ const { repository } = createRepositoryWithRemote();
915
+ runGit(repository, ['branch', 'feature/tracking']);
916
+ const emptyRemote = createTempDir();
917
+ runGit(emptyRemote, ['init', '--bare']);
918
+ runGit(repository, ['remote', 'add', 'broken-upstream', emptyRemote]);
919
+
920
+ const created = await createWorktree(repository, {
921
+ mode: 'existing',
922
+ branchName: 'feature/tracking-wt',
923
+ worktreeName: 'feature-tracking-wt',
924
+ existingBranch: 'feature/tracking',
925
+ setUpstream: true,
926
+ upstreamRemote: 'broken-upstream',
927
+ upstreamBranch: 'does-not-exist',
928
+ });
929
+
930
+ await expect.poll(
931
+ () => getWorktreeBootstrapStatus(created.path).then((status) => status.status === 'ready' || status.status === 'failed'),
932
+ { timeout: 5_000 }
933
+ ).toBe(true);
934
+
935
+ expect(getBranchTrackingRemote(created.path, 'feature/tracking-wt')).toBe('');
936
+ });
937
+ }, 30_000);
938
+ });
939
+
809
940
  // ---------------------------------------------------------------------------
810
941
  // removeWorktree
811
942
  // ---------------------------------------------------------------------------
@@ -1207,3 +1338,94 @@ describe.runIf(canRunGit())('getRangeDiff', () => {
1207
1338
  expect(diff).toContain('feature.txt');
1208
1339
  });
1209
1340
  });
1341
+
1342
+ describe('parseBranchCreationSource', () => {
1343
+ it('returns the source ref from the oldest creation entry', () => {
1344
+ // Reflog lists newest entries first; creation is the last line.
1345
+ const reflog = [
1346
+ 'commit: abc123',
1347
+ 'branch: Created from origin/main',
1348
+ ].join('\n');
1349
+ expect(parseBranchCreationSource(reflog)).toBe('origin/main');
1350
+ });
1351
+
1352
+ it('returns null when the branch was created from a detached HEAD pointer', () => {
1353
+ const reflog = 'branch: Created from HEAD@{0}';
1354
+ expect(parseBranchCreationSource(reflog)).toBeNull();
1355
+ });
1356
+
1357
+ it('returns null when the branch was created from a raw commit', () => {
1358
+ const reflog = 'branch: Created from 9a3b2c1d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b';
1359
+ expect(parseBranchCreationSource(reflog)).toBeNull();
1360
+ });
1361
+
1362
+ it('returns null when there is no creation entry', () => {
1363
+ const reflog = ['commit: abc123', 'reset: moving to HEAD'].join('\n');
1364
+ expect(parseBranchCreationSource(reflog)).toBeNull();
1365
+ });
1366
+
1367
+ it('returns null for empty input', () => {
1368
+ expect(parseBranchCreationSource('')).toBeNull();
1369
+ expect(parseBranchCreationSource(undefined)).toBeNull();
1370
+ });
1371
+ });
1372
+
1373
+ describe.runIf(canRunGit())('getRangeFiles', () => {
1374
+ it('returns added and modified paths with their status letters', async () => {
1375
+ const { repository } = createRepositoryWithRemote();
1376
+ fs.writeFileSync(path.join(repository, 'added.txt'), 'new\n');
1377
+ fs.writeFileSync(path.join(repository, 'README.md'), '# Test\nchanged\n');
1378
+ runGit(repository, ['add', 'added.txt', 'README.md']);
1379
+ runGit(repository, ['commit', '-m', 'changes']);
1380
+
1381
+ const files = await getRangeFiles(repository, { base: 'react', head: 'next' });
1382
+
1383
+ expect(files).toEqual(expect.arrayContaining([
1384
+ { path: 'added.txt', status: 'A' },
1385
+ { path: 'README.md', status: 'M' },
1386
+ ]));
1387
+ });
1388
+
1389
+ it('reports the destination path for renamed files, including spaces', async () => {
1390
+ const { repository } = createRepositoryWithRemote();
1391
+ // The original file must exist in the base: rename detection pairs a
1392
+ // deletion against an addition relative to base, not within the branch.
1393
+ fs.writeFileSync(path.join(repository, 'old name with spaces.md'), '# Test\n');
1394
+ runGit(repository, ['add', 'old name with spaces.md']);
1395
+ runGit(repository, ['commit', '-m', 'add file to rename']);
1396
+ runGit(repository, ['push', 'origin', 'HEAD:react']);
1397
+ // Spaces in filenames exercise the -z token split: a newline split would
1398
+ // mangle these paths long before status letters matter.
1399
+ fs.renameSync(path.join(repository, 'old name with spaces.md'), path.join(repository, 'new name with spaces.md'));
1400
+ runGit(repository, ['add', '-A']);
1401
+ runGit(repository, ['commit', '-m', 'rename']);
1402
+
1403
+ const files = await getRangeFiles(repository, { base: 'react', head: 'next' });
1404
+
1405
+ const renameEntry = files.find((file) => file.status === 'R');
1406
+ expect(renameEntry).toBeDefined();
1407
+ expect(renameEntry.path).toBe('new name with spaces.md');
1408
+ expect(files.some((file) => file.path === 'old name with spaces.md')).toBe(false);
1409
+ });
1410
+
1411
+ it('reports the destination path for copied files', async () => {
1412
+ const { repository } = createRepositoryWithRemote();
1413
+ // The source must exist in the base. Copy detection needs the repository's
1414
+ // own `diff.renames=copies` setting on top of the service's -C flag; the
1415
+ // parser must survive whatever C entries git emits.
1416
+ runGit(repository, ['config', 'diff.renames', 'copies']);
1417
+ fs.writeFileSync(path.join(repository, 'copied source.md'), '# Copy me\n');
1418
+ runGit(repository, ['add', 'copied source.md']);
1419
+ runGit(repository, ['commit', '-m', 'add source']);
1420
+ runGit(repository, ['push', 'origin', 'HEAD:react']);
1421
+ fs.copyFileSync(path.join(repository, 'copied source.md'), path.join(repository, 'copied destination.md'));
1422
+ runGit(repository, ['add', '-A']);
1423
+ runGit(repository, ['commit', '-m', 'copy']);
1424
+
1425
+ const files = await getRangeFiles(repository, { base: 'react', head: 'next' });
1426
+
1427
+ const copyEntry = files.find((file) => file.status === 'C');
1428
+ expect(copyEntry).toBeDefined();
1429
+ expect(copyEntry.path).toBe('copied destination.md');
1430
+ });
1431
+ });
@@ -75,8 +75,14 @@
75
75
  - It resolves those remotes into GitHub repos.
76
76
  - It expands each repo through `parent` and `source` so PRs in upstream repos can still be found.
77
77
  - It skips PR lookup when the current branch matches that repo's default branch.
78
- - It first searches for PRs by likely source owner plus exact head branch.
79
- - If that fails, it falls back to broader GitHub search for the branch name.
78
+ - It first searches for **open** PRs by likely source owner plus exact head branch.
79
+ - If that fails, it falls back to broader GitHub search for open PRs on the branch name.
80
+ - An **open PR from any candidate repo always wins** over a closed/merged one, so a merged fork PR can never hide an open upstream PR for the same head.
81
+ - Only when no target has an open PR does it return the branch's newest closed/merged PR, as history.
82
+ - History is looked up **only for the ranked-first remote and the branch's own name** — the repo it actually pushes to. Live status is worth searching the whole fork network for; history is not, and asking every target for it multiplies serial GitHub calls until the route hits its `12s` resolve timeout and returns no status at all.
83
+ - The history answer is remembered per repo+branch so discovery polls do not re-query it: a found closed/merged record for `6h`, and "no history yet" for `10m`. A found record only changes if a second PR appears on the same head, and while that one is open the open-PR path wins without ever reading this cache.
84
+ - Creating, merging, or closing a PR invalidates both the shared repo pull list and that remembered history.
85
+ - The route skips the checks summary and the merge-permission lookup for a closed/merged PR: neither is actionable, and both cost extra GitHub calls.
80
86
  - `403` and `404` during repo lookups are treated as expected gaps, not hard errors.
81
87
 
82
88
  ## Shared client state model
@@ -108,11 +114,16 @@
108
114
  - Open PR with pending checks -> refresh about every `1m`.
109
115
  - Open PR with non-pending checks -> refresh about every `5m`.
110
116
  - Open PR without a stable checks signal -> refresh about every `2m`.
111
- - Closed or merged PR -> stop regular polling.
117
+ - Closed or merged PR -> discovery refresh every `5m` (do not permanently stop polling).
112
118
  - Hidden tab -> skip polling.
113
119
  - Non-forced refreshes use a `90s` TTL.
114
120
  - Failed non-forced attempts also observe the `90s` TTL so transient server or rate-limit failures cannot retry on every sidebar update. Forced user/action refreshes bypass this guard.
115
121
 
122
+ ## Persistence notes for terminal PRs
123
+
124
+ - Closed/merged branch associations are persisted like open ones, so a reload still shows that the branch's PR was merged.
125
+ - Hydrate resets `lastDiscoveryPollAt` for them, so restored history revalidates on the first watcher tick instead of waiting out a discovery interval.
126
+
116
127
  ## Background tracking rules
117
128
 
118
129
  - Track up to `50` likely directories.