Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): reduce dev build memory peaks

MagicLizi08b214e5a1a0
diff --git a/apps/desktop/forge.config.ts b/apps/desktop/forge.config.ts
index f2e1b7664d..39a7a97ab4 100644
--- a/apps/desktop/forge.config.ts
+++ b/apps/desktop/forge.config.ts
@@ -1676,6 +1676,10 @@ const config: ForgeConfig = {
// 使其在 packaged 应用中可以被 require()——asar 会阻止原生模块的 dlopen 调用。
new AutoUnpackNativesPlugin({}),
new VitePlugin({
+ // Dev keeps all watcher targets in one process. Build them serially at
+ // startup so the initial graph does not multiply the same high baseline;
+ // packaged builds retain Forge's normal concurrency.
+ concurrent: isDev ? false : true,
build: [
{
entry: 'src/main/index.ts',
diff --git a/apps/desktop/scripts/dev-local-env.mjs b/apps/desktop/scripts/dev-local-env.mjs
index ee385856fc..11422e2308 100644
--- a/apps/desktop/scripts/dev-local-env.mjs
+++ b/apps/desktop/scripts/dev-local-env.mjs
@@ -17,6 +17,7 @@ import {
applyDesktopDevStartupConfig,
stripDesktopDevRegionArgs,
} from '../../../scripts/shared/desktop-dev-region.mjs';
+import { withDesktopDevNodeOptions } from '../../../scripts/shared/desktop-dev-node-options.mjs';
import { generateEndpointLocalFile } from '../../../scripts/shared/endpoint-local-file.mjs';
const [command, ...rawArgs] = process.argv.slice(2);
@@ -26,7 +27,7 @@ if (!command) {
}
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '..');
-const env = { ...process.env, XDT_DESKTOP_DEV_MODE: 'local' };
+const env = withDesktopDevNodeOptions({ ...process.env, XDT_DESKTOP_DEV_MODE: 'local' });
const startupConfig = applyDesktopDevStartupConfig({ argv: rawArgs, env, mode: 'local' });
const args = stripDesktopDevRegionArgs(rawArgs);
if (!env.XDT_ENDPOINT_MANIFEST_FILE?.trim()) {
diff --git a/apps/desktop/scripts/dev-remote-env.mjs b/apps/desktop/scripts/dev-remote-env.mjs
index ee4d12f97b..742c00855d 100644
--- a/apps/desktop/scripts/dev-remote-env.mjs
+++ b/apps/desktop/scripts/dev-remote-env.mjs
@@ -16,6 +16,7 @@ import {
applyDesktopDevStartupConfig,
stripDesktopDevRegionArgs,
} from '../../../scripts/shared/desktop-dev-region.mjs';
+import { withDesktopDevNodeOptions } from '../../../scripts/shared/desktop-dev-node-options.mjs';
const [command, ...rawArgs] = process.argv.slice(2);
if (!command) {
@@ -25,11 +26,11 @@ if (!command) {
const startupConfig = applyDesktopDevStartupConfig({ argv: rawArgs, mode: 'remote' });
const args = stripDesktopDevRegionArgs(rawArgs);
-const env = {
+const env = withDesktopDevNodeOptions({
...process.env,
XDT_DESKTOP_DEV_MODE: 'remote',
VITE_CINDY_AUTH_REGION: startupConfig.region,
-};
+});
const isWindows = process.platform === 'win32';
// Windows 下 electron-forge 等 .cmd shim 需要经 shell 解析;shell 模式下 Node 不转义
diff --git a/apps/desktop/vite.main.config.ts b/apps/desktop/vite.main.config.ts
index 9c13839c05..9e2b351041 100644
--- a/apps/desktop/vite.main.config.ts
+++ b/apps/desktop/vite.main.config.ts
@@ -11,6 +11,11 @@ const loginFixturesStub = path.resolve(
'../../packages/auth-client/fixtures/loginScenarios.production-stub.ts',
);
+// Git operations can deliver a burst of file events. Waiting briefly before a
+// development rebuild lets Rollup invalidate that burst as one graph update,
+// keeping the watcher peak bounded without changing packaged builds.
+const DEV_WATCH_BUILD_DELAY_MS = 250;
+
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), 'VITE_');
// Local dev does not go through dev-remote-env.mjs, so provide the same
@@ -102,6 +107,8 @@ export default defineConfig(({ mode }) => {
),
},
build: {
+ watch:
+ mode === 'development' ? { buildDelay: DEV_WATCH_BUILD_DELAY_MS } : undefined,
rollupOptions: {
output: {
// Keep the vendored @cindy/browser-control-runtime (including its
diff --git a/dependency-patches/[email protected] b/dependency-patches/[email protected]
new file mode 100644
index 0000000000..a4a37f8304
--- /dev/null
+++ b/dependency-patches/[email protected]
@@ -0,0 +1,19 @@
+diff --git a/dist/es/shared/node-entry.js b/dist/es/shared/node-entry.js
+index 0c67f4b0befbb8af05adc5c4b0216cacbf8c169a..d270a376f10c5da4c36f8579914954c824804522 100644
+--- a/dist/es/shared/node-entry.js
++++ b/dist/es/shared/node-entry.js
+@@ -16953,6 +16953,14 @@ function resetSourcemapCache(map, sourcemapChain) {
+ }
+ }
+ function decodedSourcemap(map) {
++ // Watch mode can pass a sourcemap wrapper from the previous graph generation
++ // back through the cache. Re-wrapping it reads the lazy mappings accessor,
++ // copies the decoded array, and leaves the old array retained by the cached
++ // module. Reuse the wrapper so resetSourcemapCache can release its decoded
++ // cache without creating a second large array.
++ if (map && typeof map === 'object' && sourceMapCache.has(map)) {
++ return map;
++ }
+ if (!map)
+ return null;
+ if (typeof map === 'string') {
diff --git a/package.json b/package.json
index 303899cb17..05b7de3d23 100644
--- a/package.json
+++ b/package.json
@@ -64,7 +64,8 @@
"[email protected]": "dependency-patches/[email protected]",
"[email protected]": "dependency-patches/[email protected]",
"[email protected]": "dependency-patches/[email protected]",
- "[email protected]": "dependency-patches/[email protected]"
+ "[email protected]": "dependency-patches/[email protected]",
+ "[email protected]": "dependency-patches/[email protected]"
},
"allowUnusedPatches": true
},
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4551fd5934..9fc6974595 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -37,6 +37,9 @@ patchedDependencies:
hash: 9442031604caa9cd185e0b2be6867055459fc3a6d2781296027989db098f972d
path: dependency-patches/[email protected]
+ hash: 6d3d17b477e7f051a92906a213d0dfafe6a1f32abd24e526ddfb29603b925037
+ path: dependency-patches/[email protected]
importers:
diff --git a/scripts/__tests__/desktop-dev-node-options.test.mjs b/scripts/__tests__/desktop-dev-node-options.test.mjs
new file mode 100644
index 0000000000..1441e666e1
--- /dev/null
+++ b/scripts/__tests__/desktop-dev-node-options.test.mjs
@@ -0,0 +1,32 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+
+import {
+ DESKTOP_DEV_MAX_OLD_SPACE,
+ withDesktopDevNodeOptions,
+} from '../shared/desktop-dev-node-options.mjs';
+
+test('adds the development heap headroom without dropping inherited options', () => {
+ const result = withDesktopDevNodeOptions({ NODE_OPTIONS: '--trace-warnings' });
+
+ assert.equal(
+ result.NODE_OPTIONS,
+ `--trace-warnings --max-old-space-size=${DESKTOP_DEV_MAX_OLD_SPACE}`,
+ );
+});
+
+test('preserves an explicit max old-space setting', () => {
+ const result = withDesktopDevNodeOptions({
+ NODE_OPTIONS: '--max-old-space-size=4096 --trace-warnings',
+ });
+
+ assert.equal(result.NODE_OPTIONS, '--max-old-space-size=4096 --trace-warnings');
+});
+
+test('does not mutate the parent environment object', () => {
+ const input = { NODE_OPTIONS: '' };
+ const result = withDesktopDevNodeOptions(input);
+
+ assert.equal(input.NODE_OPTIONS, '');
+ assert.notEqual(result, input);
+});
diff --git a/scripts/shared/desktop-dev-node-options.mjs b/scripts/shared/desktop-dev-node-options.mjs
new file mode 100644
index 0000000000..d3c0b6b329
--- /dev/null
+++ b/scripts/shared/desktop-dev-node-options.mjs
@@ -0,0 +1,18 @@
+/**
+ * Keep the development Forge process above the one-shot rebuild peak observed
+ * in the desktop main target. An explicit developer override always wins.
+ */
+export const DESKTOP_DEV_MAX_OLD_SPACE = 6144;
+
+const MAX_OLD_SPACE_OPTION = /(?:^|\s)--max[-_]old[-_]space[-_]size(?:=|\s)/;
+
+export function withDesktopDevNodeOptions(env = process.env) {
+ const next = { ...env };
+ const existing = next.NODE_OPTIONS?.trim() ?? '';
+ if (!MAX_OLD_SPACE_OPTION.test(existing)) {
+ next.NODE_OPTIONS = [existing, `--max-old-space-size=${DESKTOP_DEV_MAX_OLD_SPACE}`]
+ .filter(Boolean)
+ .join(' ');
+ }
+ return next;
+}