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): Windows 上 Node 插件 worker 虚拟 stdin 装载崩溃 (#337)

Lizibe09745cf90c
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeVirtualStdin.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeVirtualStdin.test.ts
new file mode 100644
index 0000000000..31be11fb21
--- /dev/null
+++ b/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeVirtualStdin.test.ts
@@ -0,0 +1,74 @@
+/**
+ * nodeRuntimeVirtualStdin.test — 虚拟 stdin 装载的两条路径。
+ *
+ * Windows 上 Electron(lib/common/init.ts)把 process.stdin 钉成
+ * configurable: false 的 getter,返回一条创建时即 EOF 的假 Readable;
+ * 直接 defineProperty 会抛 "Cannot redefine property: stdin"。这里用
+ * 同形状的假体验证原地复活路径,防 Node 升级悄悄改掉 _readableState
+ * 的 bitfield setter 行为。
+ */
+
+import { Readable } from 'node:stream';
+
+import { describe, expect, it } from 'vitest';
+
+import { installVirtualStdin } from '../nodeRuntimeVirtualStdin';
+
+type ProcessLike = Pick<NodeJS.Process, 'stdin'>;
+
+async function collectUntilEnd(stream: Readable): Promise<Buffer> {
+ const chunks: Buffer[] = [];
+ stream.on('data', (chunk: Buffer) => chunks.push(chunk));
+ await new Promise<void>((resolve) => stream.on('end', resolve));
+ return Buffer.concat(chunks);
+}
+
+describe('installVirtualStdin', () => {
+ it('stdin 可配置时整体替换,喂入的字节按序可读', async () => {
+ const proc = {} as ProcessLike;
+ const sink = installVirtualStdin(proc);
+ const stream = proc.stdin as unknown as Readable;
+ expect(stream).toBeInstanceOf(Readable);
+
+ const done = collectUntilEnd(stream);
+ sink.feed('{"jsonrpc":"2.0"}\n');
+ sink.feed(Buffer.from('第二行\n', 'utf8'));
+ sink.end();
+ expect((await done).toString('utf8')).toBe('{"jsonrpc":"2.0"}\n第二行\n');
+ });
+
+ it('stdin 被钉成不可配置 EOF 流时原地复活(Electron win32 形状)', async () => {
+ // 复刻 Electron common/init 的 win32 行为:EOF Readable + 不可配置 getter。
+ const stub = new Readable();
+ stub.push(null);
+ // 让 EOF 引发的 nextTick 排水先跑完,贴近真实装载时机。
+ await new Promise((resolve) => setImmediate(resolve));
+
+ const proc = {} as ProcessLike;
+ Object.defineProperty(proc, 'stdin', {
+ configurable: false,
+ enumerable: true,
+ get: () => stub,
+ });
+
+ const sink = installVirtualStdin(proc);
+ // 属性无法重定义,插件读到的必须仍是同一个对象。
+ expect(proc.stdin as unknown).toBe(stub);
+
+ const done = collectUntilEnd(stub);
+ sink.feed('hello ');
+ sink.feed(Buffer.from('world'));
+ sink.end();
+ expect((await done).toString('utf8')).toBe('hello world');
+ });
+
+ it('stdin 不可重定义且形状不符时抛错,不让插件读到静默 EOF', () => {
+ const proc = {} as ProcessLike;
+ Object.defineProperty(proc, 'stdin', {
+ configurable: false,
+ enumerable: true,
+ get: () => 42,
+ });
+ expect(() => installVirtualStdin(proc)).toThrow(/stdin/);
+ });
+});
diff --git a/apps/desktop/src/main/cindy-brain/nodeRuntimeVirtualStdin.ts b/apps/desktop/src/main/cindy-brain/nodeRuntimeVirtualStdin.ts
new file mode 100644
index 0000000000..1531c68106
--- /dev/null
+++ b/apps/desktop/src/main/cindy-brain/nodeRuntimeVirtualStdin.ts
@@ -0,0 +1,66 @@
+/**
+ * nodeRuntimeVirtualStdin — 把 process.stdin 接管为宿主可喂字节的虚拟流。
+ *
+ * macOS / Linux:Node 自身把 stdin 定义为可配置属性,直接整体替换成 PassThrough。
+ * Windows:Electron 的 lib/common/init.ts 在 win32 把 process.stdin 钉成
+ * configurable: false 的 getter,返回一条创建时即压入 EOF 的假 Readable
+ * (browser / utility 进程一律如此),此时 defineProperty 会抛
+ * "Cannot redefine property: stdin"。属性无法重定义,唯一出路是把这条假流
+ * 原地复活:清掉 EOF 标志、补上空 _read,之后按 Readable.push 喂字节——
+ * 插件从 process.stdin 读到的仍是同一个对象,require('node:process') 一致。
+ * 复活依赖 _readableState.ended 的 setter(Node 22 / 24 的 bitfield 属性
+ * 描述符均提供);复活失败时抛错让 worker 明确退出,不让插件读到静默 EOF。
+ */
+
+import { PassThrough } from 'node:stream';
+import type { Readable } from 'node:stream';
+
+export interface VirtualStdinSink {
+ /** 宿主送来的插件方向字节从这里写入。 */
+ feed(chunk: string | Buffer): void;
+ /** 宿主宣布 stdin 结束。 */
+ end(): void;
+}
+
+type StdinStub = Readable & {
+ _read?: (size: number) => void;
+ _readableState?: { ended?: boolean; endEmitted?: boolean };
+};
+
+export function installVirtualStdin(proc: Pick<NodeJS.Process, 'stdin'>): VirtualStdinSink {
+ const descriptor = Object.getOwnPropertyDescriptor(proc, 'stdin');
+ if (!descriptor || descriptor.configurable) {
+ const virtual = new PassThrough();
+ Object.defineProperty(proc, 'stdin', {
+ configurable: true,
+ enumerable: true,
+ value: virtual,
+ });
+ return {
+ feed(chunk) {
+ virtual.write(chunk);
+ },
+ end() {
+ virtual.end();
+ },
+ };
+ }
+ const stub = proc.stdin as unknown as StdinStub;
+ const state = stub?._readableState;
+ if (!state || typeof stub.push !== 'function' || state.endEmitted) {
+ throw new Error('process.stdin 不可重定义且流形状不符合预期,虚拟 stdin 装载失败');
+ }
+ stub._read = () => {};
+ state.ended = false;
+ if (state.ended !== false) {
+ throw new Error('process.stdin 的 EOF 标志清除失败,虚拟 stdin 装载失败');
+ }
+ return {
+ feed(chunk) {
+ stub.push(typeof chunk === 'string' ? Buffer.from(chunk, 'utf8') : chunk);
+ },
+ end() {
+ stub.push(null);
+ },
+ };
+}
diff --git a/apps/desktop/src/main/cindy-brain/nodeRuntimeWorkerProcess.ts b/apps/desktop/src/main/cindy-brain/nodeRuntimeWorkerProcess.ts
index e1600bf8d2..b608a1857c 100644
--- a/apps/desktop/src/main/cindy-brain/nodeRuntimeWorkerProcess.ts
+++ b/apps/desktop/src/main/cindy-brain/nodeRuntimeWorkerProcess.ts
@@ -28,6 +28,7 @@ import { PassThrough } from 'node:stream';
import type { GhostNodeChildToWorkerMessage } from '../../shared/ghost.js';
import { GHOST_NODE_CHILD_MODE_FLAG } from '../../shared/ghost.js';
+import { installVirtualStdin } from './nodeRuntimeVirtualStdin.js';
interface ParentPortLike {
postMessage(message: unknown): void;
@@ -52,12 +53,9 @@ if (
throw new Error('Node 插件工作进程入口不合法');
}
-const virtualStdin = new PassThrough();
-Object.defineProperty(process, 'stdin', {
- configurable: true,
- enumerable: true,
- value: virtualStdin,
-});
+// Windows 上 Electron 把 process.stdin 钉成不可配置 getter,不能整体替换;
+// 替换或原地复活的分支收敛在 installVirtualStdin 里。
+const virtualStdin = installVirtualStdin(process);
/* ── spawnEntry 窄接口(仅普通 worker 模式;子进程模式不给,树深恒为 1)── */
@@ -184,12 +182,12 @@ parentPort.on('message', (event) => {
const data = event.data;
if (!isRecord(data)) return;
if (data.type === 'stdin' && typeof data.chunk === 'string') {
- if (Buffer.byteLength(data.chunk, 'utf8') <= 1024 * 1024) virtualStdin.write(data.chunk);
+ if (Buffer.byteLength(data.chunk, 'utf8') <= 1024 * 1024) virtualStdin.feed(data.chunk);
return;
}
// 子进程原样模式的字节口(base64,防多字节字符被 chunk 边界切坏)。
if (data.type === 'stdin-b64' && typeof data.chunk === 'string') {
- if (data.chunk.length <= 2 * 1024 * 1024) virtualStdin.write(Buffer.from(data.chunk, 'base64'));
+ if (data.chunk.length <= 2 * 1024 * 1024) virtualStdin.feed(Buffer.from(data.chunk, 'base64'));
return;
}
if (data.type === 'stdin-end') {