cc-viewer 1.9.0 → 1.9.2

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 (160) hide show
  1. package/README.md +3 -1
  2. package/cli.js +33 -4
  3. package/concepts/ar/UltraPlan.md +1 -1
  4. package/concepts/da/UltraPlan.md +1 -1
  5. package/concepts/de/UltraPlan.md +1 -1
  6. package/concepts/en/UltraPlan.md +1 -1
  7. package/concepts/es/UltraPlan.md +1 -1
  8. package/concepts/fr/UltraPlan.md +1 -1
  9. package/concepts/it/UltraPlan.md +1 -1
  10. package/concepts/ja/UltraPlan.md +1 -1
  11. package/concepts/ko/UltraPlan.md +1 -1
  12. package/concepts/no/UltraPlan.md +1 -1
  13. package/concepts/pl/UltraPlan.md +1 -1
  14. package/concepts/pt-BR/UltraPlan.md +1 -1
  15. package/concepts/ru/UltraPlan.md +1 -1
  16. package/concepts/th/UltraPlan.md +1 -1
  17. package/concepts/tr/UltraPlan.md +1 -1
  18. package/concepts/uk/UltraPlan.md +1 -1
  19. package/concepts/zh/UltraPlan.md +1 -1
  20. package/concepts/zh-TW/UltraPlan.md +1 -1
  21. package/dist/assets/App-DJ4yNvwz.css +1 -0
  22. package/dist/assets/App-DPRn6oGn.js +2 -0
  23. package/dist/assets/{MdxEditorPanel-3j8rS3IT.js → MdxEditorPanel-Ci4ldhd7.js} +1 -1
  24. package/dist/assets/{MdxEditorPanel-Dpn-4ydO.css → MdxEditorPanel-DTfdj6GS.css} +1 -1
  25. package/dist/assets/Mobile-Dq-KcO1s.js +1 -0
  26. package/dist/assets/ProxyStatsModal-CUFpqt1s.js +1 -0
  27. package/dist/assets/{_baseUniq-B3e4JaL0.js → _baseUniq-CwQykUuc.js} +1 -1
  28. package/dist/assets/{arc-DnoPn_qY.js → arc-EdUdBxtG.js} +1 -1
  29. package/dist/assets/{architectureDiagram-Q4EWVU46-DrPKTbxH.js → architectureDiagram-Q4EWVU46-DtBIdElY.js} +1 -1
  30. package/dist/assets/{blockDiagram-DXYQGD6D-CEaO0Vcw.js → blockDiagram-DXYQGD6D-guNZIKkZ.js} +1 -1
  31. package/dist/assets/{c4Diagram-AHTNJAMY-BLaawFeg.js → c4Diagram-AHTNJAMY-xV5vB-Z9.js} +1 -1
  32. package/dist/assets/{channel-DnYR1fF4.js → channel-DYEktNap.js} +1 -1
  33. package/dist/assets/{chunk-4BX2VUAB-Bfd4edKZ.js → chunk-4BX2VUAB-ZTkItpvj.js} +1 -1
  34. package/dist/assets/{chunk-4TB4RGXK-t2nMQmY7.js → chunk-4TB4RGXK-dYIewyyR.js} +1 -1
  35. package/dist/assets/{chunk-55IACEB6-C6tLOjGX.js → chunk-55IACEB6-CSsPNgPX.js} +1 -1
  36. package/dist/assets/{chunk-EDXVE4YY-dWCvxbCb.js → chunk-EDXVE4YY-CnMiuqzQ.js} +1 -1
  37. package/dist/assets/{chunk-FMBD7UC4-BHELZLPK.js → chunk-FMBD7UC4-CVpATPZ3.js} +1 -1
  38. package/dist/assets/{chunk-OYMX7WX6-C8XTkH1S.js → chunk-OYMX7WX6-Dpq4s8dW.js} +1 -1
  39. package/dist/assets/{chunk-QZHKN3VN-CD0j6SAs.js → chunk-QZHKN3VN-Bnojh6X6.js} +1 -1
  40. package/dist/assets/{chunk-YZCP3GAM-D4dKfXJJ.js → chunk-YZCP3GAM-BYqK4Dsa.js} +1 -1
  41. package/dist/assets/classDiagram-6PBFFD2Q-ClGF33bF.js +1 -0
  42. package/dist/assets/classDiagram-v2-HSJHXN6E-ClGF33bF.js +1 -0
  43. package/dist/assets/clone-CFj55h49.js +1 -0
  44. package/dist/assets/{cose-bilkent-S5V4N54A-BF2HsiSD.js → cose-bilkent-S5V4N54A-OJ7mmWyV.js} +1 -1
  45. package/dist/assets/{dagre-KV5264BT-f3h967yx.js → dagre-KV5264BT-B1Kwd_Rk.js} +1 -1
  46. package/dist/assets/{diagram-5BDNPKRD-B9PzHQfh.js → diagram-5BDNPKRD-D8qrw8sp.js} +1 -1
  47. package/dist/assets/{diagram-G4DWMVQ6-kR953Bqm.js → diagram-G4DWMVQ6-ip-N4ho8.js} +1 -1
  48. package/dist/assets/{diagram-MMDJMWI5-DAewa2eg.js → diagram-MMDJMWI5-2PZaskUF.js} +1 -1
  49. package/dist/assets/{diagram-TYMM5635-C8HNTH6I.js → diagram-TYMM5635-CeAOQUZP.js} +1 -1
  50. package/dist/assets/{erDiagram-SMLLAGMA-BQ5P8wWK.js → erDiagram-SMLLAGMA-BCBaEYEb.js} +1 -1
  51. package/dist/assets/{flowDiagram-DWJPFMVM-BIpEkrMR.js → flowDiagram-DWJPFMVM-CsGyIqcX.js} +1 -1
  52. package/dist/assets/{ganttDiagram-T4ZO3ILL-CWqSfB5v.js → ganttDiagram-T4ZO3ILL-COsj3FZ-.js} +1 -1
  53. package/dist/assets/{gitGraphDiagram-UUTBAWPF-Ba-9yEbQ.js → gitGraphDiagram-UUTBAWPF-DvQVQGJG.js} +1 -1
  54. package/dist/assets/{graph-D3zXKQ6P.js → graph-BSB9fDSP.js} +1 -1
  55. package/dist/assets/index-BLzca-ta.css +1 -0
  56. package/dist/assets/{index-G3xd-gLo.js → index-BRw4Iz2O.js} +1 -1
  57. package/dist/assets/{index-DClD_EkN.js → index-Bo-12bug.js} +1 -1
  58. package/dist/assets/{index-DSd5P3WQ.js → index-BuVComcY.js} +1 -1
  59. package/dist/assets/{index-HLrnYsI1.js → index-CNNAYjkF.js} +1 -1
  60. package/dist/assets/index-Ci38xZiX.js +2 -0
  61. package/dist/assets/{index-Bm3A5VLb.js → index-DKufsAzx.js} +1 -1
  62. package/dist/assets/{index-C9N0rZ7Z.js → index-DXBfwPYD.js} +1 -1
  63. package/dist/assets/{index-Q8wp4Nof.js → index-eM2ih5n7.js} +1 -1
  64. package/dist/assets/{infoDiagram-42DDH7IO-Cxm79ZJR.js → infoDiagram-42DDH7IO-eNucS4iW.js} +1 -1
  65. package/dist/assets/{ishikawaDiagram-UXIWVN3A-Cd0vZP1B.js → ishikawaDiagram-UXIWVN3A-Dxxj-gUX.js} +1 -1
  66. package/dist/assets/{journeyDiagram-VCZTEJTY-B2b57HXk.js → journeyDiagram-VCZTEJTY-yJJzVEBb.js} +1 -1
  67. package/dist/assets/{jszip.min-Bj5uiEb2.js → jszip.min-ByI0ds5u.js} +1 -1
  68. package/dist/assets/{kanban-definition-6JOO6SKY-YCERiUCO.js → kanban-definition-6JOO6SKY-2rfTi2zw.js} +1 -1
  69. package/dist/assets/{layout-Do5TlO2U.js → layout-DTmHj0Wm.js} +1 -1
  70. package/dist/assets/{linear-BiJNTDgN.js → linear-BOA3a9FE.js} +1 -1
  71. package/dist/assets/{mermaid.core-Dg0McRZt.js → mermaid.core-5H-NR79H.js} +3 -3
  72. package/dist/assets/{min-T51iefht.js → min-CpjFTEec.js} +1 -1
  73. package/dist/assets/{mindmap-definition-QFDTVHPH-Cu_GJxY3.js → mindmap-definition-QFDTVHPH-B8KCjFgB.js} +1 -1
  74. package/dist/assets/{pieDiagram-DEJITSTG-DldHHZXi.js → pieDiagram-DEJITSTG-BARy96g5.js} +1 -1
  75. package/dist/assets/{quadrantDiagram-34T5L4WZ-BzyTPNAJ.js → quadrantDiagram-34T5L4WZ-DYoTapuI.js} +1 -1
  76. package/dist/assets/{requirementDiagram-MS252O5E-C5xxFkvG.js → requirementDiagram-MS252O5E-EeANLLK2.js} +1 -1
  77. package/dist/assets/{sankeyDiagram-XADWPNL6-OxdjoaE1.js → sankeyDiagram-XADWPNL6-DmQ-YVPj.js} +1 -1
  78. package/dist/assets/seqResourceLoaders-B0p7e_lM.js +2 -0
  79. package/dist/assets/seqResourceLoaders-BsMFNfvR.css +41 -0
  80. package/dist/assets/{sequenceDiagram-FGHM5R23-C7c89uIS.js → sequenceDiagram-FGHM5R23-SZtW0rYC.js} +1 -1
  81. package/dist/assets/{stateDiagram-FHFEXIEX-ssdJnDrO.js → stateDiagram-FHFEXIEX-CEygsIld.js} +1 -1
  82. package/dist/assets/{stateDiagram-v2-QKLJ7IA2-BP-0WClK.js → stateDiagram-v2-QKLJ7IA2-BU1XPhBe.js} +1 -1
  83. package/dist/assets/{timeline-definition-GMOUNBTQ-sWScEpSM.js → timeline-definition-GMOUNBTQ-DhPBaQBt.js} +1 -1
  84. package/dist/assets/{vendor-antd-DKIytXho.js → vendor-antd-CS_S2mBN.js} +1 -1
  85. package/dist/assets/{vendor-codemirror-4qgXq03k.js → vendor-codemirror-B5l-VbBO.js} +1 -1
  86. package/dist/assets/{vendor-mdxeditor-D5Z5D9fq.js → vendor-mdxeditor-Cz09a1_I.js} +2 -2
  87. package/dist/assets/{vendor-qrcode-B7lb5qk1.js → vendor-qrcode-qbbntD6-.js} +1 -1
  88. package/dist/assets/{vendor-virtuoso-BqoXlbhH.js → vendor-virtuoso-Sk8fQ4Hx.js} +1 -1
  89. package/dist/assets/{vennDiagram-DHZGUBPP-DNfXR0ge.js → vennDiagram-DHZGUBPP-NniTHxDw.js} +1 -1
  90. package/dist/assets/{wardley-RL74JXVD-JXd--CRD.js → wardley-RL74JXVD-D9SNOrDw.js} +1 -1
  91. package/dist/assets/{wardleyDiagram-NUSXRM2D-DHmNyVPM.js → wardleyDiagram-NUSXRM2D-DvHXKNza.js} +1 -1
  92. package/dist/assets/{xychartDiagram-5P7HB3ND-CTb0gltZ.js → xychartDiagram-5P7HB3ND-CaiwPTQB.js} +1 -1
  93. package/dist/index.html +5 -5
  94. package/package.json +1 -1
  95. package/server/interceptor.js +192 -24
  96. package/server/lib/ask/ask-store.js +7 -3
  97. package/server/lib/async-file-lock.js +29 -5
  98. package/server/lib/auth.js +71 -54
  99. package/server/lib/chat-queue.js +67 -4
  100. package/server/lib/config-backup.js +8 -5
  101. package/server/lib/credential-access.js +233 -0
  102. package/server/lib/credential-migrate.js +147 -0
  103. package/server/lib/credential-store.js +120 -0
  104. package/server/lib/credential-vault.js +112 -0
  105. package/server/lib/file-access-policy.js +16 -0
  106. package/server/lib/git-diff.js +49 -20
  107. package/server/lib/im/im-config.js +81 -31
  108. package/server/lib/im-deny.js +22 -8
  109. package/server/lib/json-store.js +264 -0
  110. package/server/lib/log-watcher.js +9 -1
  111. package/server/lib/prefs-store.js +8 -33
  112. package/server/lib/same-origin.js +22 -0
  113. package/server/lib/session-pin-store.js +7 -14
  114. package/server/lib/v2/convert.js +1 -1
  115. package/server/lib/v2/layout.js +53 -3
  116. package/server/lib/v2/live-feed.js +41 -4
  117. package/server/lib/v2/resume-list.js +150 -0
  118. package/server/lib/v2/session-select.js +63 -14
  119. package/server/lib/v2/v2-writer.js +172 -34
  120. package/server/lib/v2/view-router.js +83 -0
  121. package/server/lib/view-root.js +209 -0
  122. package/server/pty-manager.js +809 -163
  123. package/server/routes/ask-perm.js +21 -1
  124. package/server/routes/auth.js +21 -1
  125. package/server/routes/events.js +123 -7
  126. package/server/routes/expert.js +46 -14
  127. package/server/routes/files-content.js +72 -7
  128. package/server/routes/files-fs.js +28 -3
  129. package/server/routes/git.js +31 -5
  130. package/server/routes/im.js +15 -2
  131. package/server/routes/preferences.js +59 -46
  132. package/server/routes/project-meta.js +32 -3
  133. package/server/routes/proxy-stats.js +33 -5
  134. package/server/routes/resume.js +457 -0
  135. package/server/routes/search.js +23 -1
  136. package/server/routes/skills.js +18 -2
  137. package/server/routes/v2.js +6 -1
  138. package/server/routes/workspaces.js +18 -5
  139. package/server/server.js +199 -25
  140. package/server/system-prompt-templates/presets/GLM-5.2.md +0 -1
  141. package/server/system-prompt-templates/presets/GLM-5.3.md +0 -1
  142. package/server/system-prompt-templates/presets/Qwen-3.md +0 -1
  143. package/server/system-prompt-templates/presets/deepseek-v4-flash.md +0 -1
  144. package/server/system-prompt-templates/presets/deepseek-v4-pro.md +0 -1
  145. package/server/system-prompt-templates/presets/kimi-k2.7-code.md +0 -1
  146. package/server/system-prompt-templates/presets/kimi-k3.md +1 -1
  147. package/server/system-prompt-templates/systemPromptModel.md +0 -1
  148. package/server/workspace-registry.js +150 -75
  149. package/ultraAgents/code-expert.json +1 -1
  150. package/dist/assets/App-M61pqQEx.css +0 -1
  151. package/dist/assets/App-_HfxMJvv.js +0 -2
  152. package/dist/assets/Mobile-DaRLB7Gh.js +0 -1
  153. package/dist/assets/ProxyStatsModal-dlPIDlx-.js +0 -1
  154. package/dist/assets/classDiagram-6PBFFD2Q-BecPAAuk.js +0 -1
  155. package/dist/assets/classDiagram-v2-HSJHXN6E-BecPAAuk.js +0 -1
  156. package/dist/assets/clone-B0_ghF3F.js +0 -1
  157. package/dist/assets/index-CjbBUDGP.js +0 -2
  158. package/dist/assets/index-Nvk8r3tO.css +0 -1
  159. package/dist/assets/seqResourceLoaders-B_FSQh3U.css +0 -41
  160. package/dist/assets/seqResourceLoaders-hqLhsiGO.js +0 -2
@@ -3,7 +3,7 @@ import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
3
3
  import { join, dirname } from 'node:path';
4
4
  import { homedir } from 'node:os';
5
5
  import { LOG_DIR, setLogDir, getClaudeConfigDir, discoverClaudeExecutables, resolveExplicitClaudePath, CLAUDE_EXECUTABLE_PREF_KEY } from '../../findcc.js';
6
- import { PROFILE_PATH, _defaultConfig, getActiveProfileId, getStoredRoles, isOfficialDefaultEndpoint, setActiveProfileForWorkspace, _loadProxyProfile, RETRY_CONFIG_PATH, _retryConfigState, _loadRetryConfigState } from '../interceptor.js';
6
+ import { PROFILE_PATH, _defaultConfig, getActiveProfileId, getStoredRoles, isOfficialDefaultEndpoint, setActiveProfileForWorkspace, _loadProxyProfile, hydrateProfilesWithApiKeys, RETRY_CONFIG_PATH, _retryConfigState, _loadRetryConfigState } from '../interceptor.js';
7
7
  import { migrateProxyProfileList, isValidRoleValue, PROXY_ROLE_KEYS } from '../lib/interceptor-core.js';
8
8
  import { DEFAULT_RETRY_CONFIG, validateRetryConfig, resolveRetryConfig } from '../lib/proxy/proxy-retry.js';
9
9
  import { discoverCcSwitchProviders, mergeImportedProfiles } from '../lib/ccswitch-import.js';
@@ -15,7 +15,10 @@ import { inspectShellHook } from '../lib/shell-hook-inspect.js';
15
15
  import { sendEventToClients } from '../lib/log-watcher.js';
16
16
  import { listPlatforms } from '../lib/im/im-config.js';
17
17
  import { mutatePrefs, applyPrefsPatch, readPrefsRaw } from '../lib/prefs-store.js';
18
+ import { mutateJsonSync } from '../lib/json-store.js';
19
+ import { persistProfilesApiKeys } from '../lib/credential-access.js';
18
20
  import { isAdminReq } from '../lib/is-admin.js';
21
+ import { isSameOriginBrowserRequest } from '../lib/same-origin.js';
19
22
  import {
20
23
  getCurrentProjectKey, getCurrentProjectName, hasFork, listForks, resolveScoped,
21
24
  } from '../lib/project-prefs.js';
@@ -27,22 +30,6 @@ function stripImConfigs(obj) {
27
30
  if (obj) for (const id of listPlatforms()) delete obj[id];
28
31
  }
29
32
 
30
- // Loopback identifies the socket peer, not the browser page that initiated a
31
- // request. Since the server intentionally has permissive CORS for legacy APIs,
32
- // sensitive executable-selection endpoints need their own same-origin guard.
33
- function isSameOriginBrowserRequest(req, parsedUrl) {
34
- const origin = req.headers?.origin;
35
- if (!origin) return true; // CLI/native clients do not send Origin.
36
- if (req.headers?.['sec-fetch-site'] === 'cross-site') return false;
37
- try {
38
- const originUrl = new URL(origin);
39
- if (parsedUrl?.origin) return originUrl.origin.toLowerCase() === parsedUrl.origin.toLowerCase();
40
- return originUrl.host.toLowerCase() === String(req.headers?.host || '').toLowerCase();
41
- } catch {
42
- return false;
43
- }
44
- }
45
-
46
33
  // /theme 选择器特征:选项文案高特异、不太可能出现在普通生成输出里(ESC 兜底的门控签名)
47
34
  const THEME_PICKER_RE = /Auto \(match terminal\)|colorblind-friendly/;
48
35
 
@@ -163,12 +150,18 @@ function preferencesPost(req, res, parsedUrl, isLocal, deps) {
163
150
  // 切日志目录:把旧文件的完整内容(含 auth 密码 / prefsByProject forks / 其它偏好)带到新位置,
164
151
  // 避免切目录后密码与各项目 fork 凭空消失;并把写目标在 setLogDir 前后固定为"新文件",让本次
165
152
  // 合并只跑在一个文件 / 一把锁上(不再因 LOG_DIR 中途漂移而劈裂进程内锁队列)。
153
+ // logDir 是机器全局、可改变进程数据根的操作(移动 vault 根会影响 LAN 门与代理注入),
154
+ // 因此只允许 admin(本机 loopback 或已鉴权远程 admin)触发——未鉴权远程/项目客户端直接剥掉。
166
155
  let targetFile = deps.getPrefsFile();
167
156
  let carried = null;
168
157
  if (incoming.logDir && typeof incoming.logDir === 'string') {
169
- carried = readPrefsRaw(targetFile); // 旧文件全量(锁外读,迁移属罕见 admin 操作)
170
- setLogDir(incoming.logDir);
171
- targetFile = deps.getPrefsFile();
158
+ if (!isAdminReq(req, isLocal)) {
159
+ delete incoming.logDir; // non-admin cannot move the data root (would open the LAN gate)
160
+ } else {
161
+ carried = readPrefsRaw(targetFile); // 旧文件全量(锁外读,迁移属罕见 admin 操作)
162
+ setLogDir(incoming.logDir);
163
+ targetFile = deps.getPrefsFile();
164
+ }
172
165
  }
173
166
  // 全局写入:locked + atomic(prefs-store),与 fork 写共用同一把锁,避免并发写互相覆盖含密码的
174
167
  // preferences.json。Deep-merge approvalModal 的逻辑下沉到 applyPrefsPatch,与 /api/project-prefs
@@ -310,14 +303,28 @@ function proxyProfilesGet(req, res, parsedUrl, isLocal, deps) {
310
303
  if (Array.isArray(data.profiles)) {
311
304
  const { profiles: migrated, changed } = migrateProxyProfileList(data.profiles);
312
305
  if (changed) {
313
- data = { ...data, profiles: migrated };
314
306
  try {
315
- mkdirSync(dirname(PROFILE_PATH), { recursive: true });
316
- writeFileSync(PROFILE_PATH, JSON.stringify(data, null, 2), { mode: 0o600 });
307
+ // Write the migration back while pulling apiKeys into the vault (so the old plaintext is
308
+ // never re-persisted). Read-merge-write INSIDE the lock (mutateJsonSync mutates in place
309
+ // and the kernel writes back the same reference), mutexed with the startup credential-strip
310
+ // and proxyProfilesPost.
311
+ mutateJsonSync(PROFILE_PATH, (current) => {
312
+ const base = (current && typeof current === 'object' && !Array.isArray(current)) ? current : {};
313
+ const stripped = persistProfilesApiKeys(migrated, null, { isMaskedFn: () => false });
314
+ base.profiles = stripped;
315
+ return base;
316
+ }, { mode: 0o600, strictCorrupt: true, fallback: {} });
317
+ data = { ...data, profiles: migrated };
317
318
  _loadProxyProfile();
318
319
  } catch { /* 迁移落盘失败不阻塞 GET;下次仍会尝试 */ }
319
320
  }
320
321
  }
322
+ // profile.json no longer carries apiKeys (they live in the credential vault) — decrypt-backfill
323
+ // them onto the list for serving. The local admin gets plaintext to view/copy; a remote caller
324
+ // is masked by deps.maskProfiles below (same as before).
325
+ if (Array.isArray(data.profiles)) {
326
+ data = { ...data, profiles: hydrateProfilesWithApiKeys(data.profiles) };
327
+ }
321
328
  // 用 interceptor.getActiveProfileId() 返回 effective active(workspace > profile.json.active > 'max')
322
329
  const effectiveActive = getActiveProfileId();
323
330
  // 本机(127.0.0.1)= admin:下发明文 profile.apiKey 供本人在编辑表单(👁 折叠)里查阅/复制;已授权
@@ -353,22 +360,21 @@ function proxyProfilesPost(req, res, parsedUrl, isLocal, deps) {
353
360
  if (!incoming.profiles.some(p => p.id === 'max')) {
354
361
  incoming.profiles = [{ id: 'max', name: 'Default' }, ...(incoming.profiles || [])];
355
362
  }
356
- // 如果 apiKey 是 mask 值(未修改),从磁盘读取原始值保留
357
- let existing = {};
358
- try { if (existsSync(PROFILE_PATH)) existing = JSON.parse(readFileSync(PROFILE_PATH, 'utf-8')); } catch { }
359
- const existingMap = {};
360
- if (existing.profiles) existing.profiles.forEach(p => { if (p.apiKey) existingMap[p.id] = p.apiKey; });
361
- for (const p of incoming.profiles) {
362
- if (p.apiKey && deps.isMasked(p.apiKey) && existingMap[p.id]) {
363
- p.apiKey = existingMap[p.id];
364
- }
365
- }
366
- // 只写 profiles 列表到 profile.json;active 不再入文件(避免跨进程串台)
367
- // 保留老数据里的 active 字段不变,以便老版本 ccv 或手动编辑者的回退能力
368
- const toWrite = { ...existing, profiles: incoming.profiles };
369
- const dir = dirname(PROFILE_PATH);
370
- if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
371
- writeFileSync(PROFILE_PATH, JSON.stringify(toWrite, null, 2), { mode: 0o600 });
363
+ // Only the profiles list is written to profile.json; `active` is no longer persisted here
364
+ // (avoids cross-process clobbering). The existing file's `active` field is preserved so an
365
+ // older ccv or a hand editor can still fall back to it. Read-merge-write INSIDE the lock
366
+ // (mutateJsonSync), mutexed with the startup credential-strip and setActiveProfileForWorkspace —
367
+ // an unlocked snapshot would let persistProfilesApiKeys' deletion loop remove concurrently-added
368
+ // vault entries and overwrite the whole list.
369
+ // apiKey 抽出到凭证 vault(密文存 credentials.json),profile.json 不再携带 key。
370
+ // masked 回显 → 保留该 profile 的既有 vault 条目(不动、不回写 sentinel);
371
+ // 新明文 → 写 vault;空 → 删该条目;被移除的 profile(全量替换端点)→ 删其 vault 条目。
372
+ mutateJsonSync(PROFILE_PATH, (current) => {
373
+ const existing = (current && typeof current === 'object' && !Array.isArray(current)) ? current : {};
374
+ const strippedProfiles = persistProfilesApiKeys(incoming.profiles, existing.profiles, { isMaskedFn: deps.isMasked });
375
+ existing.profiles = strippedProfiles;
376
+ return existing;
377
+ }, { mode: 0o600, strictCorrupt: true, fallback: {} });
372
378
  // 角色分配校验:只认 subagent/teammate 两个 key(roles.main 之类的杂键直接丢弃);
373
379
  // 值必须是 follow / max / 入参 profiles 里存在的 id(按将落盘的列表校验,而非旧文件),
374
380
  // 非法值归 'follow'(宽容风格,不 400)。被删 profile 的悬空角色由读取时归 follow 兜底。
@@ -541,13 +547,20 @@ async function ccswitchImportPost(req, res, _parsedUrl, isLocal, deps) {
541
547
  }
542
548
  existing = parsed;
543
549
  }
544
- // merge
545
- const merged = mergeImportedProfiles(existing.profiles || [], result.profiles);
546
- const toWrite = { ...existing, profiles: merged.profiles };
547
- // 落盘
548
- const dir = dirname(PROFILE_PATH);
549
- if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
550
- writeFileSync(PROFILE_PATH, JSON.stringify(toWrite, null, 2), { mode: 0o600 });
550
+ // Persist: locked read-merge-write (mutateJsonSync), mutexed with the other profile.json
551
+ // writers. The merge MUST be recomputed inside the lock against the CURRENT on-disk list —
552
+ // computing it outside from a pre-lock snapshot and overwriting the whole list would erase a
553
+ // proxy-profiles POST that lands in that window. apiKeys are pulled into the vault; merge
554
+ // semantics never delete existing vault entries (no existingProfiles passed). cc-switch imports
555
+ // fresh plaintext keys (not a masked echo), so isMaskedFn is always false. `merged` is read
556
+ // back out of the lock for the response counts.
557
+ let merged = null;
558
+ mutateJsonSync(PROFILE_PATH, (current) => {
559
+ const base = (current && typeof current === 'object' && !Array.isArray(current)) ? current : {};
560
+ merged = mergeImportedProfiles(base.profiles || [], result.profiles);
561
+ base.profiles = persistProfilesApiKeys(merged.profiles, null, { isMaskedFn: () => false });
562
+ return base;
563
+ }, { mode: 0o600, strictCorrupt: true, fallback: {} });
551
564
  // active 处理:setActive=true 且 cc-switch 有 current → 切换;否则保持现状
552
565
  let activeChanged = false;
553
566
  if (setActive && result.currentId) {
@@ -4,8 +4,32 @@ import { join } from 'node:path';
4
4
  import { PACKAGE_JSON } from '../_paths.js';
5
5
  import { LOG_DIR } from '../../findcc.js';
6
6
  import { _projectName } from '../interceptor.js';
7
+ import { sanitizePathComponent } from '../lib/v2/layout.js';
8
+ import { resolveViewRoot } from '../lib/view-root.js';
9
+ import { loadWorkspaces } from '../workspace-registry.js';
10
+ import { listLivePtys } from '../pty-manager.js';
7
11
  import { detectHomebrewInstall } from '../lib/updater.js';
8
12
 
13
+ // Multi-project (2026-10, review P2-6): stats reads for a VIEWED project must
14
+ // not become a presence oracle over LOG_DIR — `?project=` is accepted ONLY
15
+ // when it resolves to a registered workspace / live PTY (or the bound
16
+ // project), never an arbitrary sanitized name.
17
+ function _resolveStatsName(projectParam, res) {
18
+ if (!projectParam) return _projectName || '';
19
+ const r = resolveViewRoot({
20
+ projectParam,
21
+ boundCwd: process.env.CCV_PROJECT_DIR || process.cwd(),
22
+ loadWorkspaces,
23
+ listLivePtys,
24
+ });
25
+ if (!r.ok) {
26
+ res.writeHead(r.status, { 'Content-Type': 'application/json' });
27
+ res.end(JSON.stringify({ error: r.error }));
28
+ return null;
29
+ }
30
+ return sanitizePathComponent(projectParam);
31
+ }
32
+
9
33
  // 判定当前 cc-viewer 的安装渠道,供前端精准匹配升级命令。
10
34
  // - electron:桌面版(in-process server),走 GitHub Releases 重新下载安装包。
11
35
  // - brew:Homebrew Cellar 布局命中 → `brew upgrade cc-viewer`(npm install -g 会跟 Cellar 打架)。
@@ -44,14 +68,19 @@ function versionInfo(req, res) {
44
68
  }
45
69
  }
46
70
 
47
- function projectStats(req, res) {
71
+ function projectStats(req, res, parsedUrl) {
48
72
  try {
49
- if (!_projectName) {
73
+ // Multi-project (2026-10): project-stats resolves the VIEWED project
74
+ // (?project=, registered/live only — never an arbitrary name).
75
+ const projectParam = parsedUrl && parsedUrl.searchParams ? parsedUrl.searchParams.get('project') : null;
76
+ const name = _resolveStatsName(projectParam, res);
77
+ if (name === null) return; // error already replied
78
+ if (!name) {
50
79
  res.writeHead(404, { 'Content-Type': 'application/json' });
51
80
  res.end(JSON.stringify({ error: 'No project name' }));
52
81
  return;
53
82
  }
54
- const statsFile = join(LOG_DIR, _projectName, `${_projectName}.json`);
83
+ const statsFile = join(LOG_DIR, name, `${name}.json`);
55
84
  if (!existsSync(statsFile)) {
56
85
  res.writeHead(404, { 'Content-Type': 'application/json' });
57
86
  res.end(JSON.stringify({ error: 'Stats file not found' }));
@@ -10,17 +10,45 @@ import { readFileSync, existsSync } from 'node:fs';
10
10
  import { join } from 'node:path';
11
11
  import { LOG_DIR } from '../../findcc.js';
12
12
  import { _projectName } from '../interceptor.js';
13
+ import { sanitizePathComponent } from '../lib/v2/layout.js';
14
+ import { resolveViewRoot } from '../lib/view-root.js';
15
+ import { loadWorkspaces } from '../workspace-registry.js';
16
+ import { listLivePtys } from '../pty-manager.js';
13
17
 
14
- // GET /api/proxy-stats — returns the current project's proxy retry stats (proxyStats field).
15
- // Response body: { proxyStats: {...} }; proxyStats is null when there is no data.
16
- function getProxyStats(req, res) {
18
+ // Multi-project (2026-10, review P2-6): stats reads for a VIEWED project must
19
+ // not become a presence oracle over LOG_DIR — `?project=` is accepted ONLY
20
+ // when it resolves to a registered workspace / live PTY (or the bound
21
+ // project), never an arbitrary sanitized name.
22
+ function _resolveStatsName(projectParam, res) {
23
+ if (!projectParam) return _projectName || '';
24
+ const r = resolveViewRoot({
25
+ projectParam,
26
+ boundCwd: process.env.CCV_PROJECT_DIR || process.cwd(),
27
+ loadWorkspaces,
28
+ listLivePtys,
29
+ });
30
+ if (!r.ok) {
31
+ res.writeHead(r.status, { 'Content-Type': 'application/json' });
32
+ res.end(JSON.stringify({ error: r.error }));
33
+ return null;
34
+ }
35
+ return sanitizePathComponent(projectParam);
36
+ }
37
+
38
+ // GET /api/proxy-stats — returns the VIEWED project's proxy retry stats
39
+ // (?project=, registered/live only; the log store is name-keyed, not fs-dir).
40
+ // Response body: { proxyStats: {...} }; null when there is no data.
41
+ function getProxyStats(req, res, parsedUrl) {
17
42
  try {
18
- if (!_projectName) {
43
+ const projectParam = parsedUrl && parsedUrl.searchParams ? parsedUrl.searchParams.get('project') : null;
44
+ const name = _resolveStatsName(projectParam, res);
45
+ if (name === null) return; // error already replied
46
+ if (!name) {
19
47
  res.writeHead(404, { 'Content-Type': 'application/json' });
20
48
  res.end(JSON.stringify({ error: 'No project name', proxyStats: null }));
21
49
  return;
22
50
  }
23
- const statsFile = join(LOG_DIR, _projectName, `${_projectName}.json`);
51
+ const statsFile = join(LOG_DIR, name, `${name}.json`);
24
52
  if (!existsSync(statsFile)) {
25
53
  res.writeHead(200, { 'Content-Type': 'application/json' });
26
54
  res.end(JSON.stringify({ proxyStats: null }));