@openchamber/web 1.18.3 → 1.19.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.
- package/README.md +1 -0
- package/dist/assets/{CodeMirrorCommentWidgets-DbUi6I9s.js → CodeMirrorCommentWidgets-DAvopJvy.js} +1 -1
- package/dist/assets/{CommitSection-Jc2_hNPE.js → CommitSection-D9cV-qR9.js} +1 -1
- package/dist/assets/{DiagramEditor-PCrYHBko.js → DiagramEditor-B9NCadU7.js} +1 -1
- package/dist/assets/DiagramView-LKGBwf4G.js +1 -0
- package/dist/assets/{DiffView-DEGlNjMH.js → DiffView-BWNq-0ov.js} +2 -2
- package/dist/assets/FilesView-DB-Q0Gkz.js +14 -0
- package/dist/assets/{GitView-C-n17voy.js → GitView-DsWXqWx3.js} +3 -3
- package/dist/assets/{JsonTreeView-BYcgdTOQ.js → JsonTreeView-h4dHASlq.js} +1 -1
- package/dist/assets/MarkdownImageGallery-n0ikH-xm.js +1 -0
- package/dist/assets/MarkdownRendererImpl-CT2mjx-s.js +12 -0
- package/dist/assets/OnboardingScreen-CDjgzxEe.js +1 -0
- package/dist/assets/{PatchDiff-Bn7bfgUv.js → PatchDiff-jPKTJje5.js} +1 -1
- package/dist/assets/{PierreDiffViewer-D1fWvrUn.js → PierreDiffViewer-D_Gt4A_6.js} +1 -1
- package/dist/assets/PlanView-UvuxIl9-.js +4 -0
- package/dist/assets/SessionSwitcherDropdown-xKU4LRuQ.js +1 -0
- package/dist/assets/SettingsView-3B9VN6Ys.js +28 -0
- package/dist/assets/SettingsWindow-CqQNDKNd.js +1 -0
- package/dist/assets/{ToolOutputDialog-BtIJkFno.js → ToolOutputDialog-PmjlAxVE.js} +6 -6
- package/dist/assets/{ToolPartDiffPreview-i3PQo6_0.js → ToolPartDiffPreview-B34Xw9wq.js} +1 -1
- package/dist/assets/{VirtualizedFileDiff-DkcusvMa.js → VirtualizedFileDiff-B37yB9jZ.js} +3 -3
- package/dist/assets/{WalkthroughView-Cc5Hd6P5.js → WalkthroughView-Bxun3k66.js} +2 -2
- package/dist/assets/{computeVirtualFileMetrics-xPena3W2.js → computeVirtualFileMetrics-DYFv_yZS.js} +2 -2
- package/dist/assets/contextPanelEmbeddedChat-BO1wRpaN.js +1 -0
- package/dist/assets/de-DbV6CLxv.js +9 -0
- package/dist/assets/document-attachments-DFAWlG3U.js +31 -0
- package/dist/assets/es-CVDlyzZ4.js +15 -0
- package/dist/assets/fr-Czc-p0g-.js +13 -0
- package/dist/assets/index-1OhiMDBz.css +1 -0
- package/dist/assets/index-BSvmTvZl.js +1 -0
- package/dist/assets/index-DzriVon2.js +7 -0
- package/dist/assets/{isDiffPlainText-Dudf-dWx.js → isDiffPlainText-DwHKxDYc.js} +1 -1
- package/dist/assets/{isFilePlainText-DS4QV1dd.js → isFilePlainText-Dwq6ILxo.js} +1 -1
- package/dist/assets/ja-dwc9NM_S.js +15 -0
- package/dist/assets/ko-Ds1DZ8FX.js +15 -0
- package/dist/assets/{lua-BgMRiT3U.js → lua-VAEuO923.js} +1 -1
- package/dist/assets/main-BF51IOVF.js +921 -0
- package/dist/assets/{main-C0DyIwTb.js → main-D7UPARut.js} +2 -2
- package/dist/assets/markdownCore-BgeBDwD3.js +2 -0
- package/dist/assets/{miniChat-CGfrxKpJ.js → miniChat-BISXSGOo.js} +2 -2
- package/dist/assets/mobile-BnYxVuvw.js +2 -0
- package/dist/assets/{patchFileDiff-C5D1OPCa.js → patchFileDiff-BEDPDypx.js} +2 -2
- package/dist/assets/pl-CLiTO7i-.js +15 -0
- package/dist/assets/pt-BR-GIimJgpK.js +15 -0
- package/dist/assets/renderElectronMiniChatApp-BaOy72Gi.js +2 -0
- package/dist/assets/{renderMobileApp-B5HA4qbi.js → renderMobileApp-BiMbzPq9.js} +7 -7
- package/dist/assets/runtimeEndpointReset-BMBRhIbf.js +15 -0
- package/dist/assets/scheduledTasksApi-01zzfzqH.js +1 -0
- package/dist/assets/{shikiHighlight-CmanopUB.js → shikiHighlight-DXYW46vW.js} +1 -1
- package/dist/assets/uk-DLfgD1ki.js +15 -0
- package/dist/assets/{useAppFontEffects-sHmU5b8u.js → useAppFontEffects-CbzHzFop.js} +139 -142
- package/dist/assets/{useInlineCommentController-gt56WkxJ.js → useInlineCommentController-CegKdDXi.js} +2 -2
- package/dist/assets/{vendor-base-ui-D_V6hAcE.js → vendor-base-ui-BYlZTzrw.js} +1 -1
- package/dist/assets/vendor-codemirror-autocomplete-DzPz9Wgq.js +1 -0
- package/dist/assets/vendor-codemirror-commands-B7H_j0iU.js +1 -0
- package/dist/assets/{vendor-codemirror-lang-angular-CgY7_gQ1.js → vendor-codemirror-lang-angular-DEPs2M5t.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-cpp-B5Ta4enQ.js → vendor-codemirror-lang-cpp-DhkseaMo.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-css-VgNqFSH-.js → vendor-codemirror-lang-css-O0cZX78y.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-go-LxLz62pp.js → vendor-codemirror-lang-go-fMdQYcON.js} +1 -1
- package/dist/assets/vendor-codemirror-lang-html-CfgK872x.js +1 -0
- package/dist/assets/{vendor-codemirror-lang-java-Bksi1PH4.js → vendor-codemirror-lang-java-CQj4S3zl.js} +1 -1
- package/dist/assets/vendor-codemirror-lang-javascript-Cc2ZYbgX.js +13 -0
- package/dist/assets/{vendor-codemirror-lang-jinja-C7npuDD7.js → vendor-codemirror-lang-jinja-BmZZv8g-.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-json-ryCw7nab.js → vendor-codemirror-lang-json-CMBS2tG9.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-less-BxsZJD6N.js → vendor-codemirror-lang-less-C6Eo8hVS.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-liquid-De6qch10.js → vendor-codemirror-lang-liquid-DSTR47-H.js} +1 -1
- package/dist/assets/vendor-codemirror-lang-markdown-BPGOH9ye.js +1 -0
- package/dist/assets/{vendor-codemirror-lang-php-Cm1es33p.js → vendor-codemirror-lang-php-DD9udfPH.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-python-N414WYK-.js → vendor-codemirror-lang-python-HahnQaAS.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-rust-C_WuUStH.js → vendor-codemirror-lang-rust-nLTovbjv.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-sass-8UHTCAOM.js → vendor-codemirror-lang-sass-pL2Yc72v.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-sql-CV3sMA5D.js → vendor-codemirror-lang-sql-BVBnan19.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-vue-DzvC1Dhk.js → vendor-codemirror-lang-vue-BqGzJVm1.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-wast-jlr-LDdA.js → vendor-codemirror-lang-wast-B0_m5DqU.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-xml-CrO63OVp.js → vendor-codemirror-lang-xml-Dj3voadT.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-yaml-dxi6CISW.js → vendor-codemirror-lang-yaml-yu-9Z5u7.js} +1 -1
- package/dist/assets/vendor-codemirror-language-BgPj_McA.js +3 -0
- package/dist/assets/{vendor-codemirror-language-data-Dihw9MtL.js → vendor-codemirror-language-data-CiAYDJXN.js} +2 -2
- package/dist/assets/vendor-codemirror-search-BmS-GmcX.js +2 -0
- package/dist/assets/{vendor-codemirror-state-CNWM3Q8V.js → vendor-codemirror-state-C9awkmn-.js} +5 -5
- package/dist/assets/vendor-codemirror-view-CiLROUVV.js +4 -0
- package/dist/assets/vendor-replit-codemirror-vim-B2DJlAhT.js +6 -0
- package/dist/assets/vendor-replit-codemirror-vim-core-FHzKdNUS.js +34 -0
- package/dist/assets/vendor-zod-adaMkgkm.js +39 -0
- package/dist/assets/{web-components-BL-Ur2Tb.js → web-components-DCLjCO9n.js} +1 -1
- package/dist/assets/zh-CN-DkeP5awK.js +15 -0
- package/dist/assets/zh-TW-BocaFm3d.js +15 -0
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +3 -3
- package/dist/mobile.html +3 -3
- package/package.json +1 -1
- package/server/index.js +116 -3
- package/server/lib/agent-memory/actions.js +243 -0
- package/server/lib/agent-memory/actions.test.js +343 -0
- package/server/lib/agent-memory/feature-flag.js +19 -0
- package/server/lib/agent-memory/feature-flag.test.js +38 -0
- package/server/lib/agent-memory/project-resolution.js +56 -0
- package/server/lib/agent-memory/project-resolution.test.js +85 -0
- package/server/lib/agent-memory/routes.http.test.js +270 -0
- package/server/lib/agent-memory/routes.js +169 -0
- package/server/lib/agent-memory/runtime.js +427 -0
- package/server/lib/agent-memory/runtime.test.js +344 -0
- package/server/lib/agent-memory/threat-patterns.js +61 -0
- package/server/lib/agent-memory/threat-patterns.test.js +61 -0
- package/server/lib/agent-tool/DOCUMENTATION.md +14 -0
- package/server/lib/agent-tool/runtime.js +63 -13
- package/server/lib/agent-tool/runtime.test.js +85 -2
- package/server/lib/context-obligatory/runtime.js +34 -3
- package/server/lib/context-obligatory/runtime.test.js +114 -0
- package/server/lib/dev-tunnel/client.js +20 -1
- package/server/lib/dev-tunnel/tunnel.test.js +9 -0
- package/server/lib/fs/DOCUMENTATION.md +3 -0
- package/server/lib/fs/routes.js +149 -32
- package/server/lib/fs/routes.test.js +225 -0
- package/server/lib/git/DOCUMENTATION.md +8 -0
- package/server/lib/git/service.js +121 -67
- package/server/lib/git/service.test.js +129 -0
- package/server/lib/github/DOCUMENTATION.md +14 -3
- package/server/lib/github/pr-status.js +188 -83
- package/server/lib/github/pr-status.test.js +182 -0
- package/server/lib/github/routes.js +36 -30
- package/server/lib/markdown-image-grants/DOCUMENTATION.md +36 -0
- package/server/lib/markdown-image-grants/routes.js +340 -0
- package/server/lib/markdown-image-grants/routes.test.js +221 -0
- package/server/lib/notifications/template-runtime.js +2 -3
- package/server/lib/openchamber-control/action-resolution.test.js +65 -0
- package/server/lib/openchamber-control/actions.js +78 -0
- package/server/lib/openchamber-control/service.js +7 -0
- package/server/lib/openchamber-sessions/routes.js +14 -0
- package/server/lib/opencode/DOCUMENTATION.md +6 -4
- package/server/lib/opencode/claude-cli-auth.js +60 -0
- package/server/lib/opencode/claude-cli-auth.test.js +54 -0
- package/server/lib/opencode/feature-routes-runtime.js +22 -0
- package/server/lib/opencode/plugins.js +12 -3
- package/server/lib/opencode/plugins.test.js +18 -0
- package/server/lib/opencode/proxy-agent-wiring.test.js +152 -0
- package/server/lib/opencode/proxy.js +116 -6
- package/server/lib/opencode/proxy.test.js +158 -1
- package/server/lib/opencode/routes.js +4 -1
- package/server/lib/opencode/server-startup-runtime.js +22 -1
- package/server/lib/opencode/server-startup-runtime.test.js +44 -0
- package/server/lib/opencode/settings-helpers.js +11 -0
- package/server/lib/opencode/settings-helpers.test.js +35 -0
- package/server/lib/opencode/settings-runtime.js +51 -0
- package/server/lib/opencode/shared.js +120 -16
- package/server/lib/opencode/shared.test.js +185 -1
- package/server/lib/project-context/DOCUMENTATION.md +158 -0
- package/server/lib/project-context/routes.http.test.js +264 -0
- package/server/lib/project-context/routes.js +237 -0
- package/server/lib/project-context/runtime.js +667 -0
- package/server/lib/project-context/runtime.test.js +498 -0
- package/server/lib/quota/DOCUMENTATION.md +16 -1
- package/server/lib/quota/providers/claude/auth.js +113 -0
- package/server/lib/quota/providers/claude/auth.test.js +117 -0
- package/server/lib/quota/providers/claude/index.js +151 -0
- package/server/lib/quota/providers/claude/index.test.js +147 -0
- package/server/lib/quota/providers/claude/transforms.js +127 -0
- package/server/lib/quota/providers/claude/transforms.test.js +82 -0
- package/server/lib/quota/providers/command-code.js +90 -0
- package/server/lib/quota/providers/command-code.test.js +72 -0
- package/server/lib/quota/providers/index.js +22 -2
- package/server/lib/quota/providers/index.test.js +10 -1
- package/server/lib/quota/utils/formatters.js +2 -1
- package/server/lib/scheduled-tasks/runtime.js +21 -2
- package/server/lib/session-knowledge/DOCUMENTATION.md +87 -0
- package/server/lib/session-knowledge/routes.js +107 -0
- package/server/lib/session-knowledge/runtime.js +331 -0
- package/server/lib/session-knowledge/runtime.test.js +275 -0
- package/server/lib/skills-catalog/DOCUMENTATION.md +22 -22
- package/server/lib/skills-catalog/curated-sources.js +1 -1
- package/server/lib/skills-catalog/curated-sources.test.js +10 -0
- package/server/lib/small-model/index.js +8 -0
- package/server/lib/small-model/index.test.js +31 -1
- package/server/lib/small-model/routes.js +3 -1
- package/dist/assets/DiagramView-CndEcZNu.js +0 -1
- package/dist/assets/FilesView-CzUgIvUm.js +0 -14
- package/dist/assets/MarkdownImageGallery-DzHwkVIH.js +0 -1
- package/dist/assets/MarkdownRendererImpl-DUcIcWgs.js +0 -12
- package/dist/assets/OnboardingScreen-BmN0lhk4.js +0 -1
- package/dist/assets/PlanView-CWM0iyFk.js +0 -4
- package/dist/assets/SessionSwitcherDropdown-Cez-9xRh.js +0 -1
- package/dist/assets/SettingsView-Bl1drwPw.js +0 -28
- package/dist/assets/SettingsWindow-DL5axjDZ.js +0 -1
- package/dist/assets/contextPanelEmbeddedChat-tQekJUAF.js +0 -1
- package/dist/assets/de-u7gSnqWs.js +0 -9
- package/dist/assets/document-attachments-CX_YaDsu.js +0 -30
- package/dist/assets/es-T9O7aAOr.js +0 -15
- package/dist/assets/fr-Oor7ewbE.js +0 -13
- package/dist/assets/index-CVNuER6G.css +0 -1
- package/dist/assets/index-Crz1YOFl.js +0 -1
- package/dist/assets/index-iFY_N5aO.js +0 -7
- package/dist/assets/ja-DHpqbVmr.js +0 -15
- package/dist/assets/ko-CwLkS3nx.js +0 -15
- package/dist/assets/main-CAxX6DAN.js +0 -915
- package/dist/assets/markdownCore-DhfzuGXu.js +0 -2
- package/dist/assets/markdownImageAssets-BTNlRyjd.js +0 -1
- package/dist/assets/mobile-DEmhwNcV.js +0 -2
- package/dist/assets/pl-CcZILN8q.js +0 -15
- package/dist/assets/pt-BR-DYHhVK6o.js +0 -15
- package/dist/assets/renderElectronMiniChatApp-Dpbswpom.js +0 -2
- package/dist/assets/runtimeEndpointReset-cbahJ3hD.js +0 -13
- package/dist/assets/scheduledTasksApi-BxgGWTL8.js +0 -1
- package/dist/assets/uk-BA2kZR38.js +0 -15
- package/dist/assets/vendor-codemirror-autocomplete-BjbBRsUA.js +0 -1
- package/dist/assets/vendor-codemirror-commands-ARAOgp1x.js +0 -1
- package/dist/assets/vendor-codemirror-lang-html-B3Qe3OpS.js +0 -1
- package/dist/assets/vendor-codemirror-lang-javascript-Bbw3D7r0.js +0 -13
- package/dist/assets/vendor-codemirror-lang-markdown-FGQydhTc.js +0 -1
- package/dist/assets/vendor-codemirror-language-DUOssd4j.js +0 -3
- package/dist/assets/vendor-codemirror-search-8kyQdB3f.js +0 -2
- package/dist/assets/vendor-codemirror-view-DSzLbZiS.js +0 -4
- package/dist/assets/vendor-replit-codemirror-vim-B0Ecqnlh.js +0 -39
- package/dist/assets/zh-CN-dlOQK6v9.js +0 -15
- package/dist/assets/zh-TW-BnqQ-WL5.js +0 -15
- package/server/lib/quota/providers/claude.js +0 -107
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import https from 'node:https';
|
|
3
|
+
|
|
1
4
|
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
2
5
|
|
|
3
6
|
import {
|
|
@@ -11,6 +14,96 @@ import { recordStartupPerformance } from './startup-performance.js';
|
|
|
11
14
|
|
|
12
15
|
const DEFAULT_SSE_HEARTBEAT_INTERVAL_MS = 20_000;
|
|
13
16
|
|
|
17
|
+
const OPENCODE_AGENT_KEEP_ALIVE_MS = 30_000;
|
|
18
|
+
// Node's own default. A lower cap evicts pooled sockets under concurrency,
|
|
19
|
+
// which reintroduces exactly the per-request connection churn this agent
|
|
20
|
+
// exists to prevent (measured: at 64 concurrent requests, a cap of 32 left
|
|
21
|
+
// 303 sockets in TIME_WAIT versus 0 at 256).
|
|
22
|
+
const OPENCODE_AGENT_MAX_FREE_SOCKETS = 256;
|
|
23
|
+
// Evicts idle free sockets from our side. Without it the only thing that
|
|
24
|
+
// retires an idle pooled socket is the upstream closing it. Note this is
|
|
25
|
+
// distinct from `keepAliveMsecs`, which is the TCP keep-alive probe delay.
|
|
26
|
+
const OPENCODE_AGENT_IDLE_TIMEOUT_MS = 60_000;
|
|
27
|
+
|
|
28
|
+
const OPENCODE_AGENT_OPTIONS = {
|
|
29
|
+
keepAlive: true,
|
|
30
|
+
keepAliveMsecs: OPENCODE_AGENT_KEEP_ALIVE_MS,
|
|
31
|
+
maxSockets: Infinity,
|
|
32
|
+
maxFreeSockets: OPENCODE_AGENT_MAX_FREE_SOCKETS,
|
|
33
|
+
timeout: OPENCODE_AGENT_IDLE_TIMEOUT_MS,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const isHttpsProxyTarget = (target) => {
|
|
37
|
+
if (typeof target !== 'string') {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
return new URL(target).protocol === 'https:';
|
|
42
|
+
} catch {
|
|
43
|
+
return /^https:/i.test(target.trim());
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Agent for proxied OpenCode API requests.
|
|
49
|
+
*
|
|
50
|
+
* When no agent is supplied, `http-proxy` falls back to `agent: false`, which
|
|
51
|
+
* both disables connection pooling and forces `Connection: close` on every
|
|
52
|
+
* proxied request (http-proxy/lib/http-proxy/common.js). That consumes one
|
|
53
|
+
* ephemeral port per request, and sustained traffic can exhaust the host's
|
|
54
|
+
* ephemeral port range — after which every process on the machine fails to
|
|
55
|
+
* open outbound connections with EADDRNOTAVAIL.
|
|
56
|
+
*
|
|
57
|
+
* The agent must match the target scheme: http-proxy dispatches through
|
|
58
|
+
* `https.request` when `target.protocol === 'https:'`
|
|
59
|
+
* (http-proxy/lib/http-proxy/passes/web-incoming.js), and an `http.Agent`
|
|
60
|
+
* would open a plaintext socket to a TLS port. External servers may be
|
|
61
|
+
* configured over https via `OPENCODE_HOST` (see env-config.js), so derive the
|
|
62
|
+
* agent class from the resolved target.
|
|
63
|
+
*
|
|
64
|
+
* `maxSockets: Infinity` preserves the unbounded concurrency of `agent: false`,
|
|
65
|
+
* so this changes connection reuse only, not request throughput.
|
|
66
|
+
*/
|
|
67
|
+
export const createOpenCodeProxyAgent = (target) => (
|
|
68
|
+
isHttpsProxyTarget(target)
|
|
69
|
+
? new https.Agent(OPENCODE_AGENT_OPTIONS)
|
|
70
|
+
: new http.Agent(OPENCODE_AGENT_OPTIONS)
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Lazily resolves the proxy agent, memoized per scheme.
|
|
75
|
+
*
|
|
76
|
+
* The scheme cannot be decided at registration time: `setupProxy()` runs before
|
|
77
|
+
* `bootstrapOpenCodeAtStartup()` (startup-pipeline-runtime.js), so on a cold
|
|
78
|
+
* start `state.openCodePort` is still null, `buildOpenCodeUrl()` throws
|
|
79
|
+
* (network-runtime.js) and `resolveProxyTarget()` falls back to the http
|
|
80
|
+
* loopback default. An external server configured over https via
|
|
81
|
+
* `OPENCODE_HOST` only becomes visible on `state.openCodeBaseUrl` after
|
|
82
|
+
* bootstrap completes.
|
|
83
|
+
*
|
|
84
|
+
* http-proxy-middleware rebuilds its per-request options with
|
|
85
|
+
* `Object.assign({}, this.proxyOptions)` inside `prepareProxyRequest`, which
|
|
86
|
+
* invokes getters, so exposing `agent` as a getter defers resolution to request
|
|
87
|
+
* time. Memoizing per scheme keeps a single shared pool per scheme rather than
|
|
88
|
+
* allocating an agent per request.
|
|
89
|
+
*/
|
|
90
|
+
const createOpenCodeProxyAgentResolver = (resolveTarget) => {
|
|
91
|
+
const agents = new Map();
|
|
92
|
+
|
|
93
|
+
return () => {
|
|
94
|
+
const target = resolveTarget();
|
|
95
|
+
const scheme = isHttpsProxyTarget(target) ? 'https:' : 'http:';
|
|
96
|
+
let agent = agents.get(scheme);
|
|
97
|
+
if (!agent) {
|
|
98
|
+
// Construct through the shared factory rather than inline, so both
|
|
99
|
+
// schemes are built from OPENCODE_AGENT_OPTIONS by the same code path.
|
|
100
|
+
agent = createOpenCodeProxyAgent(target);
|
|
101
|
+
agents.set(scheme, agent);
|
|
102
|
+
}
|
|
103
|
+
return agent;
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
|
|
14
107
|
export const createDirectoryQueryCanonicalizer = ({ realpath, ...cacheOptions } = {}) => {
|
|
15
108
|
const realpathCache = createRealpathCache({ fallbackOnError: true, realpath, ...cacheOptions });
|
|
16
109
|
|
|
@@ -285,15 +378,22 @@ export const registerOpenCodeProxy = (app, deps) => {
|
|
|
285
378
|
// and direct fetch helpers use. This avoids split-brain state where /health
|
|
286
379
|
// succeeds against an external host but /api/* still proxies to 127.0.0.1.
|
|
287
380
|
const resolveProxyTarget = () => {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
381
|
+
const runtimeState = getRuntime();
|
|
382
|
+
|
|
383
|
+
// `buildOpenCodeUrl` throws while the port is unknown, and the port is
|
|
384
|
+
// nulled on several runtime paths (health-check failure, failed restart),
|
|
385
|
+
// not just cold start. Checking first keeps a degraded OpenCode from
|
|
386
|
+
// making every proxied request pay for a thrown-and-caught exception.
|
|
387
|
+
if (runtimeState.openCodePort) {
|
|
388
|
+
try {
|
|
389
|
+
const resolved = normalizeProxyTarget(buildOpenCodeUrl('/', ''));
|
|
390
|
+
if (resolved) {
|
|
391
|
+
return resolved;
|
|
392
|
+
}
|
|
393
|
+
} catch {
|
|
292
394
|
}
|
|
293
|
-
} catch {
|
|
294
395
|
}
|
|
295
396
|
|
|
296
|
-
const runtimeState = getRuntime();
|
|
297
397
|
const externalBase = normalizeProxyTarget(runtimeState.openCodeBaseUrl);
|
|
298
398
|
if (externalBase) {
|
|
299
399
|
return externalBase;
|
|
@@ -767,8 +867,18 @@ export const registerOpenCodeProxy = (app, deps) => {
|
|
|
767
867
|
});
|
|
768
868
|
|
|
769
869
|
// Generic proxy for non-SSE OpenCode API routes.
|
|
870
|
+
// The agent is exposed as a getter so its class is resolved per request, not
|
|
871
|
+
// at registration: the proxy is registered before OpenCode bootstraps, so an
|
|
872
|
+
// https target configured via OPENCODE_HOST is not yet visible here. Agents
|
|
873
|
+
// are memoized per scheme, so this is still one shared pool per scheme across
|
|
874
|
+
// `apiProxy` and `interactiveOAuthProxy`.
|
|
875
|
+
const resolveOpenCodeProxyAgent = createOpenCodeProxyAgentResolver(resolveProxyTarget);
|
|
876
|
+
|
|
770
877
|
const createApiProxy = (timeoutMs) => createProxyMiddleware({
|
|
771
878
|
target: resolveProxyTarget(),
|
|
879
|
+
get agent() {
|
|
880
|
+
return resolveOpenCodeProxyAgent();
|
|
881
|
+
},
|
|
772
882
|
changeOrigin: true,
|
|
773
883
|
pathRewrite: { '^/api': '' },
|
|
774
884
|
timeout: timeoutMs,
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import https from 'node:https';
|
|
3
|
+
|
|
4
|
+
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
1
5
|
import { describe, expect, it } from 'vitest';
|
|
2
6
|
|
|
3
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
createDirectoryQueryCanonicalizer,
|
|
9
|
+
createOpenCodeProxyAgent,
|
|
10
|
+
normalizeForwardedDirectoryHeaders,
|
|
11
|
+
} from './proxy.js';
|
|
4
12
|
|
|
5
13
|
describe('createDirectoryQueryCanonicalizer', () => {
|
|
6
14
|
it('canonicalizes directory query params and preserves other params', async () => {
|
|
@@ -93,3 +101,152 @@ describe('normalizeForwardedDirectoryHeaders', () => {
|
|
|
93
101
|
});
|
|
94
102
|
});
|
|
95
103
|
});
|
|
104
|
+
|
|
105
|
+
const listen = (server) => new Promise((resolve, reject) => {
|
|
106
|
+
server.once('error', reject);
|
|
107
|
+
server.listen(0, '127.0.0.1', () => {
|
|
108
|
+
server.removeListener('error', reject);
|
|
109
|
+
resolve(server.address().port);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const closeServer = (server) => new Promise((resolve) => {
|
|
114
|
+
server.close(resolve);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const request = (port, agent) => new Promise((resolve, reject) => {
|
|
118
|
+
const req = http.request({ host: '127.0.0.1', port, path: '/', method: 'GET', agent }, (res) => {
|
|
119
|
+
res.resume();
|
|
120
|
+
res.on('end', resolve);
|
|
121
|
+
res.on('error', reject);
|
|
122
|
+
});
|
|
123
|
+
req.on('error', reject);
|
|
124
|
+
req.end();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Proxies two sequential requests through `createProxyMiddleware` and reports
|
|
129
|
+
* what the upstream server observed for each one.
|
|
130
|
+
*/
|
|
131
|
+
const proxyTwoRequests = async (proxyAgent) => {
|
|
132
|
+
const seen = [];
|
|
133
|
+
let middleware;
|
|
134
|
+
const upstream = http.createServer((req, res) => {
|
|
135
|
+
seen.push({ connection: req.headers.connection, remotePort: req.socket.remotePort });
|
|
136
|
+
res.end('ok');
|
|
137
|
+
});
|
|
138
|
+
const front = http.createServer((req, res) => {
|
|
139
|
+
middleware(req, res, () => {
|
|
140
|
+
res.statusCode = 502;
|
|
141
|
+
res.end();
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
const clientAgent = new http.Agent({ keepAlive: true });
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
const upstreamPort = await listen(upstream);
|
|
148
|
+
middleware = createProxyMiddleware({
|
|
149
|
+
target: `http://127.0.0.1:${upstreamPort}`,
|
|
150
|
+
...(proxyAgent ? { agent: proxyAgent } : {}),
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const frontPort = await listen(front);
|
|
154
|
+
await request(frontPort, clientAgent);
|
|
155
|
+
await request(frontPort, clientAgent);
|
|
156
|
+
} finally {
|
|
157
|
+
clientAgent.destroy();
|
|
158
|
+
proxyAgent?.destroy();
|
|
159
|
+
await closeServer(front);
|
|
160
|
+
await closeServer(upstream);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return seen;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
describe('createOpenCodeProxyAgent', () => {
|
|
167
|
+
it('reuses a single upstream socket across sequential proxied requests', async () => {
|
|
168
|
+
const seen = await proxyTwoRequests(createOpenCodeProxyAgent('http://127.0.0.1'));
|
|
169
|
+
|
|
170
|
+
expect(seen).toHaveLength(2);
|
|
171
|
+
expect(seen[0].connection).not.toBe('close');
|
|
172
|
+
expect(seen[1].remotePort).toBe(seen[0].remotePort);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('without an agent, http-proxy forces Connection: close and a new socket per request', async () => {
|
|
176
|
+
const seen = await proxyTwoRequests(null);
|
|
177
|
+
|
|
178
|
+
expect(seen).toHaveLength(2);
|
|
179
|
+
expect(seen[0].connection).toBe('close');
|
|
180
|
+
expect(seen[1].remotePort).not.toBe(seen[0].remotePort);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// http-proxy dispatches through `https.request` when the target protocol is
|
|
184
|
+
// `https:`, so an http.Agent would open a plaintext socket to a TLS port.
|
|
185
|
+
// External OpenCode servers can be configured over https via OPENCODE_HOST.
|
|
186
|
+
it('returns an https agent for https targets', () => {
|
|
187
|
+
const agent = createOpenCodeProxyAgent('https://opencode.example.com:4096');
|
|
188
|
+
|
|
189
|
+
expect(agent).toBeInstanceOf(https.Agent);
|
|
190
|
+
expect(agent.options.keepAlive).toBe(true);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('returns a plain http agent for http targets', () => {
|
|
194
|
+
const agent = createOpenCodeProxyAgent('http://127.0.0.1:4096');
|
|
195
|
+
|
|
196
|
+
// https.Agent extends http.Agent, so the negative assertion is the load-bearing one.
|
|
197
|
+
expect(agent).toBeInstanceOf(http.Agent);
|
|
198
|
+
expect(agent).not.toBeInstanceOf(https.Agent);
|
|
199
|
+
expect(agent.options.keepAlive).toBe(true);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('falls back to an http agent for missing or unparseable targets', () => {
|
|
203
|
+
expect(createOpenCodeProxyAgent(undefined)).not.toBeInstanceOf(https.Agent);
|
|
204
|
+
expect(createOpenCodeProxyAgent('not a url')).not.toBeInstanceOf(https.Agent);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// The cold-start fix relies on http-proxy-middleware rebuilding its per-request
|
|
208
|
+
// options via `Object.assign({}, this.proxyOptions)` in prepareProxyRequest,
|
|
209
|
+
// which invokes getters. If that ever changes to a cached or shallow-reference
|
|
210
|
+
// copy, the agent would freeze at its registration-time value and https targets
|
|
211
|
+
// would silently regress — so pin the behavior here against the real library.
|
|
212
|
+
it('http-proxy-middleware re-reads the agent option on every proxied request', async () => {
|
|
213
|
+
let reads = 0;
|
|
214
|
+
let middleware;
|
|
215
|
+
const agent = createOpenCodeProxyAgent('http://127.0.0.1');
|
|
216
|
+
const upstream = http.createServer((_req, res) => res.end('ok'));
|
|
217
|
+
const front = http.createServer((req, res) => {
|
|
218
|
+
middleware(req, res, () => {
|
|
219
|
+
res.statusCode = 502;
|
|
220
|
+
res.end();
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
const clientAgent = new http.Agent({ keepAlive: true });
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
const upstreamPort = await listen(upstream);
|
|
227
|
+
middleware = createProxyMiddleware({
|
|
228
|
+
target: `http://127.0.0.1:${upstreamPort}`,
|
|
229
|
+
get agent() {
|
|
230
|
+
reads += 1;
|
|
231
|
+
return agent;
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// Construction itself must not read the getter — otherwise the assertion
|
|
236
|
+
// below could be satisfied without any per-request resolution happening.
|
|
237
|
+
expect(reads).toBe(0);
|
|
238
|
+
|
|
239
|
+
const frontPort = await listen(front);
|
|
240
|
+
await request(frontPort, clientAgent);
|
|
241
|
+
expect(reads).toBe(1);
|
|
242
|
+
|
|
243
|
+
await request(frontPort, clientAgent);
|
|
244
|
+
expect(reads).toBe(2);
|
|
245
|
+
} finally {
|
|
246
|
+
clientAgent.destroy();
|
|
247
|
+
agent.destroy();
|
|
248
|
+
await closeServer(front);
|
|
249
|
+
await closeServer(upstream);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
});
|
|
@@ -6,6 +6,7 @@ import path from 'path';
|
|
|
6
6
|
import {
|
|
7
7
|
buildDeferredRestartResponse,
|
|
8
8
|
} from './config-mutation-response.js';
|
|
9
|
+
import { getClaudeCliAuthStatus } from './claude-cli-auth.js';
|
|
9
10
|
|
|
10
11
|
export const registerOpenCodeRoutes = (app, dependencies) => {
|
|
11
12
|
const {
|
|
@@ -568,7 +569,9 @@ ${desktopReturn ? `<a class="return" href="openchamber://focus/mcp-auth">Return
|
|
|
568
569
|
const sources = getProviderSources(providerId, directory);
|
|
569
570
|
const { getProviderAuth } = await getAuthLibrary();
|
|
570
571
|
const auth = getProviderAuth(providerId);
|
|
571
|
-
sources.sources.auth.exists =
|
|
572
|
+
sources.sources.auth.exists = providerId === 'claude-code'
|
|
573
|
+
? getClaudeCliAuthStatus().connected
|
|
574
|
+
: Boolean(auth);
|
|
572
575
|
|
|
573
576
|
return res.json({
|
|
574
577
|
providerId,
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** One stray uncaught exception is survivable; a storm means the process is broken. */
|
|
2
|
+
const UNCAUGHT_STORM_LIMIT = 10;
|
|
3
|
+
const UNCAUGHT_STORM_WINDOW_MS = 60_000;
|
|
4
|
+
|
|
1
5
|
export const createServerStartupRuntime = (dependencies) => {
|
|
2
6
|
const {
|
|
3
7
|
process,
|
|
@@ -148,9 +152,26 @@ export const createServerStartupRuntime = (dependencies) => {
|
|
|
148
152
|
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
149
153
|
});
|
|
150
154
|
|
|
155
|
+
// A single stray exception — a socket teardown race, a Node-internal bug
|
|
156
|
+
// like `setTypeOfService EINVAL` — must not take the server down. Nothing
|
|
157
|
+
// restarts this process (it is embedded in the desktop app or run by hand
|
|
158
|
+
// in a terminal), so shutting down turns every such stray into "the
|
|
159
|
+
// instance is unreachable until I restart it". Mirror the
|
|
160
|
+
// unhandledRejection policy above: log and keep serving. A sustained storm
|
|
161
|
+
// of exceptions is a different situation — the process is genuinely
|
|
162
|
+
// broken — so that still shuts down rather than limping along half-alive.
|
|
163
|
+
const exceptionTimes = [];
|
|
151
164
|
process.on('uncaughtException', (error) => {
|
|
152
165
|
console.error('Uncaught Exception:', error);
|
|
153
|
-
|
|
166
|
+
const now = Date.now();
|
|
167
|
+
exceptionTimes.push(now);
|
|
168
|
+
while (exceptionTimes.length > 0 && now - exceptionTimes[0] > UNCAUGHT_STORM_WINDOW_MS) {
|
|
169
|
+
exceptionTimes.shift();
|
|
170
|
+
}
|
|
171
|
+
if (exceptionTimes.length > UNCAUGHT_STORM_LIMIT) {
|
|
172
|
+
console.error(`More than ${UNCAUGHT_STORM_LIMIT} uncaught exceptions within a minute; shutting down.`);
|
|
173
|
+
gracefulShutdown();
|
|
174
|
+
}
|
|
154
175
|
});
|
|
155
176
|
};
|
|
156
177
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import { createServerStartupRuntime } from './server-startup-runtime.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The desktop app embeds this server and nothing restarts it, so shutting down
|
|
7
|
+
* on a single uncaught exception turned every stray socket error into "the
|
|
8
|
+
* instance is unreachable until restarted". Only a sustained storm shuts down.
|
|
9
|
+
*/
|
|
10
|
+
describe('uncaught exception policy', () => {
|
|
11
|
+
const setup = () => {
|
|
12
|
+
const fakeProcess = new EventEmitter();
|
|
13
|
+
let shutdowns = 0;
|
|
14
|
+
const runtime = createServerStartupRuntime({
|
|
15
|
+
process: fakeProcess,
|
|
16
|
+
gracefulShutdown: () => { shutdowns += 1; },
|
|
17
|
+
getSignalsAttached: () => true,
|
|
18
|
+
setSignalsAttached: () => {},
|
|
19
|
+
syncToHmrState: () => {},
|
|
20
|
+
});
|
|
21
|
+
runtime.attachProcessHandlers({ attachSignals: false });
|
|
22
|
+
return { fakeProcess, shutdowns: () => shutdowns };
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
test('a single uncaught exception keeps the server running', () => {
|
|
26
|
+
const { fakeProcess, shutdowns } = setup();
|
|
27
|
+
fakeProcess.emit('uncaughtException', new Error('setTypeOfService EINVAL'));
|
|
28
|
+
expect(shutdowns()).toBe(0);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('a storm of uncaught exceptions still shuts down', () => {
|
|
32
|
+
const { fakeProcess, shutdowns } = setup();
|
|
33
|
+
for (let i = 0; i < 11; i += 1) {
|
|
34
|
+
fakeProcess.emit('uncaughtException', new Error(`stray ${i}`));
|
|
35
|
+
}
|
|
36
|
+
expect(shutdowns()).toBeGreaterThan(0);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('an unhandled rejection is logged without shutting down', () => {
|
|
40
|
+
const { fakeProcess, shutdowns } = setup();
|
|
41
|
+
fakeProcess.emit('unhandledRejection', new Error('late failure'), Promise.resolve());
|
|
42
|
+
expect(shutdowns()).toBe(0);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isAgentMemoryFeatureAvailable } from '../agent-memory/feature-flag.js';
|
|
2
|
+
|
|
1
3
|
export const createSettingsHelpers = (dependencies) => {
|
|
2
4
|
const {
|
|
3
5
|
normalizePathForPersistence,
|
|
@@ -375,6 +377,9 @@ export const createSettingsHelpers = (dependencies) => {
|
|
|
375
377
|
const normalizedDays = Math.max(1, Math.min(365, Math.round(candidate.autoDeleteAfterDays)));
|
|
376
378
|
result.autoDeleteAfterDays = normalizedDays;
|
|
377
379
|
}
|
|
380
|
+
if (candidate.sessionRetentionAction === 'archive' || candidate.sessionRetentionAction === 'delete') {
|
|
381
|
+
result.sessionRetentionAction = candidate.sessionRetentionAction;
|
|
382
|
+
}
|
|
378
383
|
if (candidate.tunnelBootstrapTtlMs === null) {
|
|
379
384
|
result.tunnelBootstrapTtlMs = null;
|
|
380
385
|
} else if (typeof candidate.tunnelBootstrapTtlMs === 'number' && Number.isFinite(candidate.tunnelBootstrapTtlMs)) {
|
|
@@ -508,6 +513,9 @@ export const createSettingsHelpers = (dependencies) => {
|
|
|
508
513
|
if (typeof candidate.agentControlToolEnabled === 'boolean') {
|
|
509
514
|
result.agentControlToolEnabled = candidate.agentControlToolEnabled;
|
|
510
515
|
}
|
|
516
|
+
if (typeof candidate.agentMemoryToolEnabled === 'boolean') {
|
|
517
|
+
result.agentMemoryToolEnabled = candidate.agentMemoryToolEnabled;
|
|
518
|
+
}
|
|
511
519
|
if (typeof candidate.optimizeSystemPrompt === 'boolean') {
|
|
512
520
|
result.optimizeSystemPrompt = candidate.optimizeSystemPrompt;
|
|
513
521
|
}
|
|
@@ -905,6 +913,9 @@ export const createSettingsHelpers = (dependencies) => {
|
|
|
905
913
|
return {
|
|
906
914
|
...sanitized,
|
|
907
915
|
hasManagedRemoteTunnelToken,
|
|
916
|
+
// Tells the client whether agent memory exists in this build at all, so
|
|
917
|
+
// its settings row and panel tab can be absent rather than merely off.
|
|
918
|
+
agentMemoryFeatureAvailable: isAgentMemoryFeatureAvailable(),
|
|
908
919
|
...(pwaAppName ? { pwaAppName } : {}),
|
|
909
920
|
pwaOrientation,
|
|
910
921
|
mobileKeyboardMode,
|
|
@@ -466,4 +466,39 @@ describe('settings helpers', () => {
|
|
|
466
466
|
expect(sanitized.recentModels).toEqual(payload.recentModels);
|
|
467
467
|
});
|
|
468
468
|
});
|
|
469
|
+
|
|
470
|
+
describe('session retention settings persistence', () => {
|
|
471
|
+
it('round-trips sessionRetentionAction archive and delete through the sanitizer', () => {
|
|
472
|
+
const helpers = createTestHelpersWithRealSanitizers();
|
|
473
|
+
|
|
474
|
+
expect(helpers.sanitizeSettingsUpdate({ sessionRetentionAction: 'archive' })).toEqual({
|
|
475
|
+
sessionRetentionAction: 'archive',
|
|
476
|
+
});
|
|
477
|
+
expect(helpers.sanitizeSettingsUpdate({ sessionRetentionAction: 'delete' })).toEqual({
|
|
478
|
+
sessionRetentionAction: 'delete',
|
|
479
|
+
});
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it('rejects invalid sessionRetentionAction values', () => {
|
|
483
|
+
const helpers = createTestHelpersWithRealSanitizers();
|
|
484
|
+
|
|
485
|
+
expect(helpers.sanitizeSettingsUpdate({ sessionRetentionAction: 'remove' })).toEqual({});
|
|
486
|
+
expect(helpers.sanitizeSettingsUpdate({ sessionRetentionAction: true })).toEqual({});
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it('survives a full settings payload containing sessionRetentionAction (regression)', () => {
|
|
490
|
+
const helpers = createTestHelpersWithRealSanitizers();
|
|
491
|
+
const payload = {
|
|
492
|
+
autoDeleteEnabled: true,
|
|
493
|
+
autoDeleteAfterDays: 60,
|
|
494
|
+
sessionRetentionAction: 'delete',
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
const sanitized = helpers.sanitizeSettingsUpdate(payload);
|
|
498
|
+
|
|
499
|
+
expect(sanitized.autoDeleteEnabled).toBe(true);
|
|
500
|
+
expect(sanitized.autoDeleteAfterDays).toBe(60);
|
|
501
|
+
expect(sanitized.sessionRetentionAction).toBe('delete');
|
|
502
|
+
});
|
|
503
|
+
});
|
|
469
504
|
});
|
|
@@ -212,6 +212,56 @@ export const createSettingsRuntime = (deps) => {
|
|
|
212
212
|
}
|
|
213
213
|
};
|
|
214
214
|
|
|
215
|
+
/**
|
|
216
|
+
* Merge the server-owned `context.json` (notes/todos/plans) across a project
|
|
217
|
+
* id change.
|
|
218
|
+
*
|
|
219
|
+
* `moveDirectoryContents` only renames a file when the destination is free,
|
|
220
|
+
* so without this step an existing `<newId>/context.json` would silently
|
|
221
|
+
* discard everything stored under `<oldId>`. Every list is merged by identity
|
|
222
|
+
* so neither side loses entries.
|
|
223
|
+
*
|
|
224
|
+
* A version 1 context stored notes as a single string. It is left untouched
|
|
225
|
+
* here: `project-context` converts it on read, and converting in two places
|
|
226
|
+
* would mean two definitions of the same migration.
|
|
227
|
+
*/
|
|
228
|
+
const mergeProjectContextFiles = async (oldStorageDir, newStorageDir) => {
|
|
229
|
+
const oldContextPath = path.join(oldStorageDir, 'context.json');
|
|
230
|
+
const newContextPath = path.join(newStorageDir, 'context.json');
|
|
231
|
+
|
|
232
|
+
const [oldContext, newContext] = await Promise.all([
|
|
233
|
+
readJsonFile(oldContextPath).catch(() => null),
|
|
234
|
+
readJsonFile(newContextPath).catch(() => null),
|
|
235
|
+
]);
|
|
236
|
+
|
|
237
|
+
if (!oldContext || !newContext) {
|
|
238
|
+
// Nothing to reconcile: the plain directory move handles a single side.
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const mergeNotes = () => {
|
|
243
|
+
// One side may still be a version 1 string; keep whichever is a list, and
|
|
244
|
+
// prefer the destination when both are strings.
|
|
245
|
+
const oldIsList = Array.isArray(oldContext.notes);
|
|
246
|
+
const newIsList = Array.isArray(newContext.notes);
|
|
247
|
+
if (oldIsList && newIsList) {
|
|
248
|
+
return mergeByKey(oldContext.notes, newContext.notes, (item) => item.id);
|
|
249
|
+
}
|
|
250
|
+
if (newIsList) return newContext.notes;
|
|
251
|
+
if (oldIsList) return oldContext.notes;
|
|
252
|
+
return newContext.notes || oldContext.notes || '';
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
await writeJsonFile(newContextPath, {
|
|
256
|
+
...oldContext,
|
|
257
|
+
...newContext,
|
|
258
|
+
notes: mergeNotes(),
|
|
259
|
+
todos: mergeByKey(oldContext.todos, newContext.todos, (item) => item.id),
|
|
260
|
+
plans: mergeByKey(oldContext.plans, newContext.plans, (item) => item.id || item.file),
|
|
261
|
+
});
|
|
262
|
+
await fsPromises.rm(oldContextPath, { force: true });
|
|
263
|
+
};
|
|
264
|
+
|
|
215
265
|
const migrateProjectScopedStorage = async ({ oldId, newId, projectPath }) => {
|
|
216
266
|
if (!oldId || !newId || oldId === newId) {
|
|
217
267
|
return;
|
|
@@ -232,6 +282,7 @@ export const createSettingsRuntime = (deps) => {
|
|
|
232
282
|
await writeJsonFile(newConfigPath, merged);
|
|
233
283
|
}
|
|
234
284
|
|
|
285
|
+
await mergeProjectContextFiles(oldStorageDir, newStorageDir);
|
|
235
286
|
await moveDirectoryContents(oldStorageDir, newStorageDir);
|
|
236
287
|
await fsPromises.rm(oldConfigPath, { force: true });
|
|
237
288
|
};
|