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): fail fast on forge worker exit

Yuewei-LL9b380af3d956
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/forgeScaffoldCapability.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/forgeScaffoldCapability.test.ts
new file mode 100644
index 0000000000..534a56ef2d
--- /dev/null
+++ b/apps/desktop/src/main/cindy-brain/__tests__/forgeScaffoldCapability.test.ts
@@ -0,0 +1,82 @@
+import { EventEmitter } from 'node:events';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const mocks = vi.hoisted(() => ({
+ fork: vi.fn(),
+}));
+
+vi.mock('electron', () => ({
+ utilityProcess: { fork: mocks.fork },
+}));
+
+import { writeForgeScaffoldWithStableParent } from '../forgeScaffoldCapability';
+
+class FakeUtilityProcess extends EventEmitter {
+ stderr = new EventEmitter();
+ kill = vi.fn();
+ postMessage = vi.fn();
+}
+
+const request = {
+ parentDir: 'C:\\plugin-parent',
+ targetName: 'demo-plugin',
+ expectedParent: {
+ realPath: 'C:\\plugin-parent',
+ dev: 1n,
+ ino: 2n,
+ },
+ files: [],
+};
+
+describe('writeForgeScaffoldWithStableParent', () => {
+ beforeEach(() => {
+ mocks.fork.mockReset();
+ });
+
+ it('fails immediately when the worker exits cleanly before returning a result', async () => {
+ const child = new FakeUtilityProcess();
+ mocks.fork.mockReturnValue(child);
+
+ const result = writeForgeScaffoldWithStableParent(request);
+ child.emit('exit', 0);
+
+ await expect(result).resolves.toEqual({
+ ok: false,
+ errorCode: 'INTERNAL',
+ message: 'Forge scaffold worker exited (0)',
+ });
+ expect(child.kill).toHaveBeenCalledOnce();
+ });
+
+ it('reports worker stderr when exit happens before a result', async () => {
+ const child = new FakeUtilityProcess();
+ mocks.fork.mockReturnValue(child);
+
+ const result = writeForgeScaffoldWithStableParent(request);
+ child.stderr.emit('data', Buffer.from('worker startup failed'));
+ child.emit('exit', 1);
+
+ await expect(result).resolves.toEqual({
+ ok: false,
+ errorCode: 'INTERNAL',
+ message: 'worker startup failed',
+ });
+ expect(child.kill).toHaveBeenCalledOnce();
+ });
+
+ it('keeps worker stderr bounded when one chunk exceeds the limit', async () => {
+ const child = new FakeUtilityProcess();
+ mocks.fork.mockReturnValue(child);
+
+ const result = writeForgeScaffoldWithStableParent(request);
+ child.stderr.emit('data', Buffer.alloc(10 * 1024 * 1024, 'x'));
+ child.emit('exit', 1);
+
+ await expect(result).resolves.toMatchObject({
+ ok: false,
+ errorCode: 'INTERNAL',
+ message: 'x'.repeat(16 * 1024),
+ });
+ expect(child.kill).toHaveBeenCalledOnce();
+ });
+});
diff --git a/apps/desktop/src/main/cindy-brain/forgeScaffoldCapability.ts b/apps/desktop/src/main/cindy-brain/forgeScaffoldCapability.ts
index e48dfbbd71..11d75e0b2c 100644
--- a/apps/desktop/src/main/cindy-brain/forgeScaffoldCapability.ts
+++ b/apps/desktop/src/main/cindy-brain/forgeScaffoldCapability.ts
@@ -28,9 +28,15 @@ export function writeForgeScaffoldWithStableParent(
stdio: ['ignore', 'pipe', 'pipe'],
serviceName: 'cindy-forge-scaffold',
});
+ const maxStderrBytes = 16 * 1024;
const stderrChunks: Buffer[] = [];
+ let stderrBytes = 0;
child.stderr?.on('data', (chunk: Buffer) => {
- if (Buffer.concat(stderrChunks).length < 16 * 1024) stderrChunks.push(Buffer.from(chunk));
+ const remaining = maxStderrBytes - stderrBytes;
+ if (remaining <= 0) return;
+ const bounded = Buffer.from(chunk.subarray(0, remaining));
+ stderrChunks.push(bounded);
+ stderrBytes += bounded.length;
});
return new Promise((resolve) => {
@@ -83,10 +89,9 @@ export function writeForgeScaffoldWithStableParent(
});
});
child.on('exit', (code: number) => {
- if (!settled && code !== 0) {
- const stderr = Buffer.concat(stderrChunks).toString('utf8').trim();
- finish({ ok: false, errorCode: 'INTERNAL', message: stderr || `Forge scaffold worker exited (${code})` });
- }
+ if (settled) return;
+ const stderr = Buffer.concat(stderrChunks).toString('utf8').trim();
+ finish({ ok: false, errorCode: 'INTERNAL', message: stderr || `Forge scaffold worker exited (${code})` });
});
});
}